drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This module contains C code that generates VDBE code used to process |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 13 | ** the WHERE clause of SQL statements. This module is responsible for |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 14 | ** generating the code that loops through a table looking for applicable |
| 15 | ** rows. Indices are selected and used to speed the search when doing |
| 16 | ** so is applicable. Because this module is responsible for selecting |
| 17 | ** indices, you might also think of this module as the "query optimizer". |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | |
drh | 7924f3e | 2011-02-09 03:04:27 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 23 | ** Trace output macros |
| 24 | */ |
| 25 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 26 | /***/ int sqlite3WhereTrace = 0; |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 27 | #endif |
drh | cef4fc8 | 2012-09-21 22:50:45 +0000 | [diff] [blame] | 28 | #if defined(SQLITE_DEBUG) \ |
| 29 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 30 | # define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 31 | # define WHERETRACE_ENABLED 1 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 32 | #else |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 33 | # define WHERETRACE(X) |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 36 | /* Forward reference |
| 37 | */ |
| 38 | typedef struct WhereClause WhereClause; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 39 | typedef struct WhereMaskSet WhereMaskSet; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 40 | typedef struct WhereOrInfo WhereOrInfo; |
| 41 | typedef struct WhereAndInfo WhereAndInfo; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 42 | typedef struct WhereLevel WhereLevel; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 43 | typedef struct WhereLoop WhereLoop; |
| 44 | typedef struct WherePath WherePath; |
| 45 | typedef struct WhereTerm WhereTerm; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 46 | typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 47 | typedef struct WhereScan WhereScan; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | ** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The |
| 51 | ** maximum cost is 64*(2**63) which becomes 6900. So all costs can be |
| 52 | ** be stored in a 16-bit unsigned integer without risk of overflow. |
| 53 | */ |
| 54 | typedef unsigned short int WhereCost; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 55 | |
| 56 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 57 | ** For each nested loop in a WHERE clause implementation, the WhereInfo |
| 58 | ** structure contains a single instance of this structure. This structure |
| 59 | ** is intended to be private to the where.c module and should not be |
| 60 | ** access or modified by other modules. |
| 61 | ** |
| 62 | ** The pIdxInfo field is used to help pick the best index on a |
| 63 | ** virtual table. The pIdxInfo pointer contains indexing |
| 64 | ** information for the i-th table in the FROM clause before reordering. |
| 65 | ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c. |
| 66 | ** All other information in the i-th WhereLevel object for the i-th table |
| 67 | ** after FROM clause ordering. |
| 68 | */ |
| 69 | struct WhereLevel { |
| 70 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 71 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 72 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 73 | int addrBrk; /* Jump here to break out of the loop */ |
| 74 | int addrNxt; /* Jump here to start the next IN combination */ |
| 75 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 76 | int addrFirst; /* First instruction of interior of the loop */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 77 | u8 iFrom; /* Which entry in the FROM clause */ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 78 | u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */ |
| 79 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
| 80 | union { /* Information that depends on plan.wsFlags */ |
| 81 | struct { |
| 82 | int nIn; /* Number of entries in aInLoop[] */ |
| 83 | struct InLoop { |
| 84 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 85 | int addrInTop; /* Top of the IN loop */ |
| 86 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 87 | } *aInLoop; /* Information about each nested IN operator */ |
| 88 | } in; /* Used when plan.wsFlags&WHERE_IN_ABLE */ |
| 89 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 90 | } u; |
| 91 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 92 | }; |
| 93 | |
| 94 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 95 | ** Each instance of this object represents a way of evaluating one |
| 96 | ** term of a join. The WhereClause object holds a table of these |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 97 | ** objects using (maskSelf,prereq,) as the primary key. Note that the |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 98 | ** same join term might have multiple associated WhereLoop objects. |
| 99 | */ |
| 100 | struct WhereLoop { |
| 101 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 102 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 103 | #ifdef SQLITE_DEBUG |
| 104 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 105 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 106 | u8 iTab; /* Position in FROM clause of table for this loop */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 107 | u8 iSortIdx; /* Sorting index number. 0==None */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 108 | WhereCost rSetup; /* One-time setup cost (ex: create transient index) */ |
| 109 | WhereCost rRun; /* Cost of running each loop */ |
| 110 | WhereCost nOut; /* Estimated number of output rows */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 111 | union { |
| 112 | struct { /* Information for internal btree tables */ |
| 113 | int nEq; /* Number of equality constraints */ |
| 114 | Index *pIndex; /* Index used, or NULL */ |
| 115 | } btree; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 116 | struct { /* Information for virtual tables */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 117 | int idxNum; /* Index number */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 118 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 119 | u8 isOrdered; /* True if satisfies ORDER BY */ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 120 | u16 omitMask; /* Terms that may be omitted */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 121 | char *idxStr; /* Index identifier string */ |
| 122 | } vtab; |
| 123 | } u; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 124 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 125 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 126 | /**** whereLoopXfer() copies fields above ***********************/ |
| 127 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 128 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 129 | WhereTerm **aLTerm; /* WhereTerms used */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 130 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 131 | WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 134 | /* Forward declaration of methods */ |
| 135 | static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 136 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 137 | /* |
| 138 | ** Each instance of this object holds a sequence of WhereLoop objects |
| 139 | ** that implement some or all of the entire query plan. |
| 140 | */ |
| 141 | struct WherePath { |
| 142 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 143 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 144 | WhereCost nRow; /* Estimated number of rows generated by this path */ |
| 145 | WhereCost rCost; /* Total cost of this path */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 146 | u8 isOrdered; /* True if this path satisfies ORDER BY */ |
| 147 | u8 isOrderedValid; /* True if the isOrdered field is valid */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 148 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 149 | }; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 150 | |
| 151 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 152 | ** The query generator uses an array of instances of this structure to |
| 153 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 154 | ** clause subexpression is separated from the others by AND operators, |
| 155 | ** usually, or sometimes subexpressions separated by OR. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 156 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 157 | ** All WhereTerms are collected into a single WhereClause structure. |
| 158 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 159 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 160 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 161 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 162 | ** When a term is of the form: |
| 163 | ** |
| 164 | ** X <op> <expr> |
| 165 | ** |
| 166 | ** where X is a column name and <op> is one of certain operators, |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 167 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 168 | ** cursor number and column number for X. WhereTerm.eOperator records |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 169 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 170 | ** use of a bitmask encoding for the operator allows us to search |
| 171 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 172 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 173 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 174 | ** |
| 175 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 176 | ** |
| 177 | ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR |
| 178 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
| 179 | ** is collected about the |
| 180 | ** |
| 181 | ** If a term in the WHERE clause does not match either of the two previous |
| 182 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 183 | ** to the original subexpression content and wtFlags is set up appropriately |
| 184 | ** but no other fields in the WhereTerm object are meaningful. |
| 185 | ** |
| 186 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 187 | ** but they do so indirectly. A single WhereMaskSet structure translates |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 188 | ** cursor number into bits and the translated bit is stored in the prereq |
| 189 | ** fields. The translation is used in order to maximize the number of |
| 190 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 191 | ** spread out over the non-negative integers. For example, the cursor |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 192 | ** 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] | 193 | ** translates these sparse cursor numbers into consecutive integers |
| 194 | ** beginning with 0 in order to make the best possible use of the available |
| 195 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 196 | ** would be mapped into integers 0 through 7. |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 197 | ** |
| 198 | ** The number of terms in a join is limited by the number of bits |
| 199 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 200 | ** is only able to process joins with 64 or fewer tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 201 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 202 | struct WhereTerm { |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 203 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 204 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 205 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 206 | union { |
| 207 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 208 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 209 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 210 | } u; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 211 | u16 eOperator; /* A WO_xx value describing <op> */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 212 | u8 wtFlags; /* TERM_xxx bit flags. See below */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 213 | u8 nChild; /* Number of children that must disable us */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 214 | WhereClause *pWC; /* The clause this term is part of */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 215 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 216 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | /* |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 220 | ** Allowed values of WhereTerm.wtFlags |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 221 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 222 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 223 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 224 | #define TERM_CODED 0x04 /* This term is already coded */ |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 225 | #define TERM_COPIED 0x08 /* Has a child */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 226 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 227 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 228 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 229 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 230 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 231 | #else |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 232 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
drh | 59b6188 | 2011-02-11 02:43:14 +0000 | [diff] [blame] | 233 | #endif |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 236 | ** An instance of the WhereScan object is used as an iterator for locating |
| 237 | ** terms in the WHERE clause that are useful to the query planner. |
| 238 | */ |
| 239 | struct WhereScan { |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 240 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 241 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 242 | char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 243 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 244 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 245 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 246 | u32 opMask; /* Acceptable operators */ |
| 247 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 248 | int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ |
| 249 | }; |
| 250 | |
| 251 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 252 | ** An instance of the following structure holds all information about a |
| 253 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 254 | ** |
| 255 | ** Explanation of pOuter: For a WHERE clause of the form |
| 256 | ** |
| 257 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 258 | ** |
| 259 | ** There are separate WhereClause objects for the whole clause and for |
| 260 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 261 | ** subclauses points to the WhereClause object for the whole clause. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 262 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 263 | struct WhereClause { |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 264 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 265 | WhereClause *pOuter; /* Outer conjunction */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 266 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 267 | int nTerm; /* Number of terms */ |
| 268 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 269 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 270 | #if defined(SQLITE_SMALL_STACK) |
| 271 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 272 | #else |
| 273 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 274 | #endif |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 278 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 279 | ** a dynamically allocated instance of the following structure. |
| 280 | */ |
| 281 | struct WhereOrInfo { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 282 | WhereClause wc; /* Decomposition into subterms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 283 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | /* |
| 287 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 288 | ** a dynamically allocated instance of the following structure. |
| 289 | */ |
| 290 | struct WhereAndInfo { |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 291 | WhereClause wc; /* The subexpression broken out */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 295 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 296 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 297 | ** |
| 298 | ** The VDBE cursor numbers are small integers contained in |
| 299 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 300 | ** clause, the cursor numbers might not begin with 0 and they might |
| 301 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 302 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 303 | ** from the sparse cursor numbers into consecutive integers beginning |
| 304 | ** with 0. |
| 305 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 306 | ** 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] | 307 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 308 | ** |
| 309 | ** For example, if the WHERE clause expression used these VDBE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 310 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 311 | ** would map those cursor numbers into bits 0 through 5. |
| 312 | ** |
| 313 | ** Note that the mapping is not necessarily ordered. In the example |
| 314 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 315 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 316 | ** does not really matter. What is important is that sparse cursor |
| 317 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 318 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 319 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 320 | struct WhereMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 321 | int n; /* Number of assigned cursor values */ |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 322 | int ix[BMS]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 323 | }; |
| 324 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 325 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 326 | ** This object is a factory for WhereLoop objects for a particular query. |
| 327 | */ |
| 328 | struct WhereLoopBuilder { |
| 329 | WhereInfo *pWInfo; /* Information about this WHERE */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 330 | WhereClause *pWC; /* WHERE clause terms */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 331 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 332 | WhereLoop *pNew; /* Template WhereLoop */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 333 | WhereLoop *pBest; /* If non-NULL, store single best loop here */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | /* |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 337 | ** The WHERE clause processing routine has two halves. The |
| 338 | ** first part does the start of the WHERE loop and the second |
| 339 | ** half does the tail of the WHERE loop. An instance of |
| 340 | ** this structure is returned by the first half and passed |
| 341 | ** into the second half to give some continuity. |
| 342 | */ |
| 343 | struct WhereInfo { |
| 344 | Parse *pParse; /* Parsing and code generating context */ |
| 345 | SrcList *pTabList; /* List of tables in the join */ |
| 346 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 347 | ExprList *pDistinct; /* DISTINCT ON values, or NULL */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 348 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 349 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 350 | WhereCost nRowOut; /* Estimated number of output rows */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 351 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 352 | u8 bOBSat; /* ORDER BY satisfied by indices */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 353 | u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ |
| 354 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 355 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
| 356 | int iTop; /* The very beginning of the WHERE loop */ |
| 357 | int iContinue; /* Jump here to continue with next record */ |
| 358 | int iBreak; /* Jump here to break out of the loop */ |
| 359 | int nLevel; /* Number of nested loop */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 360 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 361 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 362 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 363 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 364 | }; |
| 365 | |
| 366 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 367 | ** Bitmasks for the operators that indices are able to exploit. An |
| 368 | ** OR-ed combination of these values can be used when searching for |
| 369 | ** terms in the where clause. |
| 370 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 371 | #define WO_IN 0x001 |
| 372 | #define WO_EQ 0x002 |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 373 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 374 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 375 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 376 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 377 | #define WO_MATCH 0x040 |
| 378 | #define WO_ISNULL 0x080 |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 379 | #define WO_OR 0x100 /* Two or more OR-connected terms */ |
| 380 | #define WO_AND 0x200 /* Two or more AND-connected terms */ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 381 | #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 382 | #define WO_NOOP 0x800 /* This term does not restrict search space */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 383 | |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 384 | #define WO_ALL 0xfff /* Mask of all possible WO_* values */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 385 | #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 386 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 387 | /* |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 388 | ** Value for wsFlags returned by bestIndex() and stored in |
| 389 | ** WhereLevel.wsFlags. These flags determine which search |
| 390 | ** strategies are appropriate. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 391 | */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 392 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR or x IN (...) or x IS NULL */ |
| 393 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 394 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 395 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 396 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 397 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 398 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 399 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 400 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 401 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 402 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 403 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 404 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 405 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 406 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 407 | #define WHERE_TEMP_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 408 | #define WHERE_COVER_SCAN 0x00008000 /* Full scan of a covering index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 409 | |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 410 | |
| 411 | /* Convert a WhereCost value (10 times log2(X)) into its integer value X. |
| 412 | */ |
| 413 | static u64 whereCostToInt(WhereCost x){ |
| 414 | u64 n; |
| 415 | if( x<=10 ) return 1; |
| 416 | n = x%10; |
| 417 | x /= 10; |
| 418 | if( n>=5 ) n -= 2; |
| 419 | else if( n>=1 ) n -= 1; |
| 420 | if( x>=3 ) return (n+8)<<(x-3); |
| 421 | return (n+8)>>(3-x); |
| 422 | } |
| 423 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 424 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 425 | ** Return the estimated number of output rows from a WHERE clause |
| 426 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 427 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 428 | return whereCostToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* |
| 432 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 433 | ** WHERE clause returns outputs for DISTINCT processing. |
| 434 | */ |
| 435 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 436 | return pWInfo->eDistinct; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 441 | ** Return FALSE if the output needs to be sorted. |
| 442 | */ |
| 443 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 444 | return pWInfo->bOBSat!=0; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
| 448 | ** Return the VDBE address or label to jump to in order to continue |
| 449 | ** immediately with the next row of a WHERE clause. |
| 450 | */ |
| 451 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 452 | return pWInfo->iContinue; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | ** Return the VDBE address or label to jump to in order to break |
| 457 | ** out of a WHERE loop. |
| 458 | */ |
| 459 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 460 | return pWInfo->iBreak; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 465 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 466 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
| 467 | */ |
| 468 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo){ |
| 469 | return pWInfo->okOnePass; |
| 470 | } |
| 471 | |
| 472 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 473 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 474 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 475 | static void whereClauseInit( |
| 476 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 477 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 478 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 479 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 480 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 481 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 482 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 483 | pWC->a = pWC->aStatic; |
| 484 | } |
| 485 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 486 | /* Forward reference */ |
| 487 | static void whereClauseClear(WhereClause*); |
| 488 | |
| 489 | /* |
| 490 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 491 | */ |
| 492 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 493 | whereClauseClear(&p->wc); |
| 494 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
| 498 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 499 | */ |
| 500 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 501 | whereClauseClear(&p->wc); |
| 502 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 503 | } |
| 504 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 505 | /* |
| 506 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 507 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 508 | */ |
| 509 | static void whereClauseClear(WhereClause *pWC){ |
| 510 | int i; |
| 511 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 512 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 513 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 514 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 515 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 516 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 517 | if( a->wtFlags & TERM_ORINFO ){ |
| 518 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 519 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 520 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 521 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 522 | } |
| 523 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 524 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 529 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 530 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 531 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 532 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 533 | ** allocation error. The memory allocation failure will be recorded in |
| 534 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 535 | ** |
| 536 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 537 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 538 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 539 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 540 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 541 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 542 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 543 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 544 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 545 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 546 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 547 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 548 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 549 | int idx; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 550 | testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 551 | if( pWC->nTerm>=pWC->nSlot ){ |
| 552 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 553 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 554 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 555 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 556 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 557 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 558 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 559 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 560 | return 0; |
| 561 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 562 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 563 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 564 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 565 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 566 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 567 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 568 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 569 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 570 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 571 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 572 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 573 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 574 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 575 | |
| 576 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 577 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 578 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 579 | ** operator specified in the op parameter. The WhereClause structure |
| 580 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 581 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 582 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 583 | ** \________/ \_______________/ \________________/ |
| 584 | ** slot[0] slot[1] slot[2] |
| 585 | ** |
| 586 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 587 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 588 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 589 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 590 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 591 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 592 | */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 593 | static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 594 | pWC->op = (u8)op; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 595 | if( pExpr==0 ) return; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 596 | if( pExpr->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 597 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 598 | }else{ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 599 | whereSplit(pWC, pExpr->pLeft, op); |
| 600 | whereSplit(pWC, pExpr->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 601 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 605 | ** Initialize an expression mask set (a WhereMaskSet object) |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 606 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame^] | 607 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 608 | |
| 609 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 610 | ** Return the bitmask for the given cursor number. Return 0 if |
| 611 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 612 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 613 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 614 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 615 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 616 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 617 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 618 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 619 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 620 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 625 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 626 | ** |
| 627 | ** There is one cursor per table in the FROM clause. The number of |
| 628 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 629 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 630 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 631 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 632 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 633 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 634 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 638 | ** This routine walks (recursively) an expression tree and generates |
| 639 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 640 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 641 | ** |
| 642 | ** In order for this routine to work, the calling function must have |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 643 | ** previously invoked sqlite3ResolveExprNames() on the expression. See |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 644 | ** the header comment on that routine for additional information. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 645 | ** The sqlite3ResolveExprNames() routines looks for column names and |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 646 | ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 647 | ** the VDBE cursor number of the table. This routine just has to |
| 648 | ** translate the cursor numbers into bitmask values and OR all |
| 649 | ** the bitmasks together. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 650 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 651 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 652 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 653 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 654 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 655 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 656 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 657 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 658 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 659 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 660 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 661 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 662 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 663 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 664 | }else{ |
| 665 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 666 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 667 | return mask; |
| 668 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 669 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 670 | int i; |
| 671 | Bitmask mask = 0; |
| 672 | if( pList ){ |
| 673 | for(i=0; i<pList->nExpr; i++){ |
| 674 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 677 | return mask; |
| 678 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 679 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 680 | Bitmask mask = 0; |
| 681 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 682 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 683 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 684 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 685 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 686 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 687 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 688 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 689 | int i; |
| 690 | for(i=0; i<pSrc->nSrc; i++){ |
| 691 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 692 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 693 | } |
| 694 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 695 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 696 | } |
| 697 | return mask; |
| 698 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 699 | |
| 700 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 701 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 702 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 703 | ** "=", "<", ">", "<=", ">=", and "IN". |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 704 | ** |
| 705 | ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be |
| 706 | ** of one of the following forms: column = expression column > expression |
| 707 | ** column >= expression column < expression column <= expression |
| 708 | ** expression = column expression > column expression >= column |
| 709 | ** expression < column expression <= column column IN |
| 710 | ** (expression-list) column IN (subquery) column IS NULL |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 711 | */ |
| 712 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 713 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 714 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 715 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 716 | assert( TK_GE==TK_EQ+4 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 717 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 721 | ** Swap two objects of type TYPE. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 722 | */ |
| 723 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 724 | |
| 725 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 726 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 727 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 728 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 729 | ** If left/right precedence rules come into play when determining the |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 730 | ** collating |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 731 | ** side of the comparison, it remains associated with the same side after |
| 732 | ** the commutation. So "Y collate NOCASE op X" becomes |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 733 | ** "X op Y". This is because any collation sequence on |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 734 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 735 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 736 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 737 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 738 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 739 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 740 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 741 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 742 | if( expRight==expLeft ){ |
| 743 | /* Either X and Y both have COLLATE operator or neither do */ |
| 744 | if( expRight ){ |
| 745 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 746 | ** used by clearing the EP_Collate flag from Y. */ |
| 747 | pExpr->pRight->flags &= ~EP_Collate; |
| 748 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 749 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 750 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 751 | ** it to be searched first. */ |
| 752 | pExpr->pLeft->flags |= EP_Collate; |
| 753 | } |
| 754 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 755 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 756 | if( pExpr->op>=TK_GT ){ |
| 757 | assert( TK_LT==TK_GT+2 ); |
| 758 | assert( TK_GE==TK_LE+2 ); |
| 759 | assert( TK_GT>TK_EQ ); |
| 760 | assert( TK_GT<TK_LE ); |
| 761 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 762 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 763 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 767 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 768 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 769 | static u16 operatorMask(int op){ |
| 770 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 771 | assert( allowedOp(op) ); |
| 772 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 773 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 774 | }else if( op==TK_ISNULL ){ |
| 775 | c = WO_ISNULL; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 776 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 777 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 778 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 779 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 780 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 781 | assert( op!=TK_IN || c==WO_IN ); |
| 782 | assert( op!=TK_EQ || c==WO_EQ ); |
| 783 | assert( op!=TK_LT || c==WO_LT ); |
| 784 | assert( op!=TK_LE || c==WO_LE ); |
| 785 | assert( op!=TK_GT || c==WO_GT ); |
| 786 | assert( op!=TK_GE || c==WO_GE ); |
| 787 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 791 | ** Advance to the next WhereTerm that matches according to the criteria |
| 792 | ** established when the pScan object was initialized by whereScanInit(). |
| 793 | ** Return NULL if there are no more matching WhereTerms. |
| 794 | */ |
| 795 | WhereTerm *whereScanNext(WhereScan *pScan){ |
| 796 | int iCur; /* The cursor on the LHS of the term */ |
| 797 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 798 | Expr *pX; /* An expression being tested */ |
| 799 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 800 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 801 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 802 | |
| 803 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 804 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 805 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 806 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 807 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 808 | if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){ |
| 809 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 810 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 811 | ){ |
| 812 | int j; |
| 813 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 814 | assert( pX->op==TK_COLUMN ); |
| 815 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 816 | if( pScan->aEquiv[j]==pX->iTable |
| 817 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | if( j==pScan->nEquiv ){ |
| 822 | pScan->aEquiv[j] = pX->iTable; |
| 823 | pScan->aEquiv[j+1] = pX->iColumn; |
| 824 | pScan->nEquiv += 2; |
| 825 | } |
| 826 | } |
| 827 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 828 | /* Verify the affinity and collating sequence match */ |
| 829 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 830 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 831 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 832 | pX = pTerm->pExpr; |
| 833 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 834 | continue; |
| 835 | } |
| 836 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 837 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 838 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 839 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 840 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 841 | continue; |
| 842 | } |
| 843 | } |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 844 | if( (pTerm->eOperator & WO_EQ)!=0 |
| 845 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 846 | && pX->iTable==pScan->aEquiv[0] |
| 847 | && pX->iColumn==pScan->aEquiv[1] |
| 848 | ){ |
| 849 | continue; |
| 850 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 851 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 852 | return pTerm; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | pWC = pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 857 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 858 | } |
| 859 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 860 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 861 | pScan->iEquiv += 2; |
| 862 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 868 | ** first match. Return NULL if there are no matches. |
| 869 | ** |
| 870 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 871 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 872 | ** iCur. The <op> must be one of the operators described by opMask. |
| 873 | ** |
| 874 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 875 | ** index pIdx. |
| 876 | */ |
| 877 | WhereTerm *whereScanInit( |
| 878 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 879 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 880 | int iCur, /* Cursor to scan for */ |
| 881 | int iColumn, /* Column to scan for */ |
| 882 | u32 opMask, /* Operator(s) to scan for */ |
| 883 | Index *pIdx /* Must be compatible with this index */ |
| 884 | ){ |
| 885 | int j; |
| 886 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 887 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 888 | pScan->pOrigWC = pWC; |
| 889 | pScan->pWC = pWC; |
| 890 | if( pIdx && iColumn>=0 ){ |
| 891 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 892 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
| 893 | if( NEVER(j>=pIdx->nColumn) ) return 0; |
| 894 | } |
| 895 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 896 | }else{ |
| 897 | pScan->idxaff = 0; |
| 898 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 899 | } |
| 900 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 901 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 902 | pScan->aEquiv[0] = iCur; |
| 903 | pScan->aEquiv[1] = iColumn; |
| 904 | pScan->nEquiv = 2; |
| 905 | pScan->iEquiv = 2; |
| 906 | return whereScanNext(pScan); |
| 907 | } |
| 908 | |
| 909 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 910 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 911 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 912 | ** the WO_xx operator codes specified by the op parameter. |
| 913 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 914 | ** |
| 915 | ** The term returned might by Y=<expr> if there is another constraint in |
| 916 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 917 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 918 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 919 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 920 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 921 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 922 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 923 | ** and ... and A9=A10 and A10=<expr>. |
| 924 | ** |
| 925 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 926 | ** then try for the one with no dependencies on <expr> - in other words where |
| 927 | ** <expr> is a constant expression of some kind. Only return entries of |
| 928 | ** 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] | 929 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 930 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 931 | */ |
| 932 | static WhereTerm *findTerm( |
| 933 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 934 | int iCur, /* Cursor number of LHS */ |
| 935 | int iColumn, /* Column number of LHS */ |
| 936 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 937 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 938 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 939 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 940 | WhereTerm *pResult = 0; |
| 941 | WhereTerm *p; |
| 942 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 943 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 944 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 945 | while( p ){ |
| 946 | if( (p->prereqRight & notReady)==0 ){ |
| 947 | if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){ |
| 948 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 949 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 950 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 951 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 952 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 953 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 954 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 955 | } |
| 956 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 957 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 958 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 959 | |
| 960 | /* |
| 961 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 962 | ** |
| 963 | ** |
| 964 | */ |
| 965 | static void exprAnalyzeAll( |
| 966 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 967 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 968 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 969 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 970 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 971 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 975 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 976 | /* |
| 977 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 978 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 979 | ** so and false if not. |
| 980 | ** |
| 981 | ** In order for the operator to be optimizible, the RHS must be a string |
| 982 | ** literal that does not begin with a wildcard. |
| 983 | */ |
| 984 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 985 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 986 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 987 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 988 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 989 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 990 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 991 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 992 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 993 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 994 | int c; /* One character in z[] */ |
| 995 | int cnt; /* Number of non-wildcard prefix characters */ |
| 996 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 997 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 998 | sqlite3_value *pVal = 0; |
| 999 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1000 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1001 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1004 | #ifdef SQLITE_EBCDIC |
| 1005 | if( *pnoCase ) return 0; |
| 1006 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1007 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 1008 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 1009 | if( pLeft->op!=TK_COLUMN |
| 1010 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 1011 | || IsVirtual(pLeft->pTab) |
| 1012 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1013 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 1014 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1015 | return 0; |
| 1016 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 1017 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1018 | |
| 1019 | pRight = pList->a[0].pExpr; |
| 1020 | op = pRight->op; |
| 1021 | if( op==TK_REGISTER ){ |
| 1022 | op = pRight->op2; |
| 1023 | } |
| 1024 | if( op==TK_VARIABLE ){ |
| 1025 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 1026 | int iCol = pRight->iColumn; |
| 1027 | pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1028 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 1029 | z = (char *)sqlite3_value_text(pVal); |
| 1030 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1031 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1032 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 1033 | }else if( op==TK_STRING ){ |
| 1034 | z = pRight->u.zToken; |
| 1035 | } |
| 1036 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1037 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1038 | 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] | 1039 | cnt++; |
| 1040 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1041 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1042 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 1043 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1044 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 1045 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 1046 | *ppPrefix = pPrefix; |
| 1047 | if( op==TK_VARIABLE ){ |
| 1048 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 1049 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1050 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 1051 | /* If the rhs of the LIKE expression is a variable, and the current |
| 1052 | ** value of the variable means there is no need to invoke the LIKE |
| 1053 | ** function, then no OP_Variable will be added to the program. |
| 1054 | ** This causes problems for the sqlite3_bind_parameter_name() |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1055 | ** API. To workaround them, add a dummy OP_Variable here. |
| 1056 | */ |
| 1057 | int r1 = sqlite3GetTempReg(pParse); |
| 1058 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1059 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 1060 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | }else{ |
| 1064 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1065 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1066 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1067 | |
| 1068 | sqlite3ValueFree(pVal); |
| 1069 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1070 | } |
| 1071 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 1072 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1073 | |
| 1074 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1075 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1076 | ** Check to see if the given expression is of the form |
| 1077 | ** |
| 1078 | ** column MATCH expr |
| 1079 | ** |
| 1080 | ** If it is then return TRUE. If not, return FALSE. |
| 1081 | */ |
| 1082 | static int isMatchOfColumn( |
| 1083 | Expr *pExpr /* Test this expression */ |
| 1084 | ){ |
| 1085 | ExprList *pList; |
| 1086 | |
| 1087 | if( pExpr->op!=TK_FUNCTION ){ |
| 1088 | return 0; |
| 1089 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1090 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1093 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1094 | if( pList->nExpr!=2 ){ |
| 1095 | return 0; |
| 1096 | } |
| 1097 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 1098 | return 0; |
| 1099 | } |
| 1100 | return 1; |
| 1101 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 1102 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1103 | |
| 1104 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1105 | ** If the pBase expression originated in the ON or USING clause of |
| 1106 | ** a join, then transfer the appropriate markings over to derived. |
| 1107 | */ |
| 1108 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 1109 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 1110 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 1111 | } |
| 1112 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1113 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1114 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1115 | ** Analyze a term that consists of two or more OR-connected |
| 1116 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1117 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1118 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 1119 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1120 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1121 | ** This routine analyzes terms such as the middle term in the above example. |
| 1122 | ** A WhereOrTerm object is computed and attached to the term under |
| 1123 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1124 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1125 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 1126 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1127 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1128 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 1129 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1130 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1131 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1132 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 1133 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 1134 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 1135 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 1136 | ** (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] | 1137 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1138 | ** CASE 1: |
| 1139 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1140 | ** 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] | 1141 | ** a single table T (as shown in example B above) then create a new virtual |
| 1142 | ** term that is an equivalent IN expression. In other words, if the term |
| 1143 | ** being analyzed is: |
| 1144 | ** |
| 1145 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 1146 | ** |
| 1147 | ** then create a new virtual term like this: |
| 1148 | ** |
| 1149 | ** x IN (expr1,expr2,expr3) |
| 1150 | ** |
| 1151 | ** CASE 2: |
| 1152 | ** |
| 1153 | ** If all subterms are indexable by a single table T, then set |
| 1154 | ** |
| 1155 | ** WhereTerm.eOperator = WO_OR |
| 1156 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 1157 | ** |
| 1158 | ** A subterm is "indexable" if it is of the form |
| 1159 | ** "T.C <op> <expr>" where C is any column of table T and |
| 1160 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 1161 | ** A subterm is also indexable if it is an AND of two or more |
| 1162 | ** subsubterms at least one of which is indexable. Indexable AND |
| 1163 | ** subterms have their eOperator set to WO_AND and they have |
| 1164 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 1165 | ** |
| 1166 | ** From another point of view, "indexable" means that the subterm could |
| 1167 | ** potentially be used with an index if an appropriate index exists. |
| 1168 | ** This analysis does not consider whether or not the index exists; that |
| 1169 | ** is something the bestIndex() routine will determine. This analysis |
| 1170 | ** only looks at whether subterms appropriate for indexing exist. |
| 1171 | ** |
| 1172 | ** All examples A through E above all satisfy case 2. But if a term |
| 1173 | ** also statisfies case 1 (such as B) we know that the optimizer will |
| 1174 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 1175 | ** satisfied. |
| 1176 | ** |
| 1177 | ** It might be the case that multiple tables are indexable. For example, |
| 1178 | ** (E) above is indexable on tables P, Q, and R. |
| 1179 | ** |
| 1180 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 1181 | ** separate indices to find rowids for each subterm and composing |
| 1182 | ** the union of all rowids using a RowSet object. This is similar |
| 1183 | ** to "bitmap indices" in other database engines. |
| 1184 | ** |
| 1185 | ** OTHERWISE: |
| 1186 | ** |
| 1187 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 1188 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1189 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1190 | static void exprAnalyzeOrTerm( |
| 1191 | SrcList *pSrc, /* the FROM clause */ |
| 1192 | WhereClause *pWC, /* the complete WHERE clause */ |
| 1193 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 1194 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1195 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 1196 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1197 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1198 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 1199 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1200 | int i; /* Loop counters */ |
| 1201 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 1202 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 1203 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 1204 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 1205 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1206 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1207 | /* |
| 1208 | ** Break the OR clause into its separate subterms. The subterms are |
| 1209 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 1210 | ** object that is attached to the original OR clause term. |
| 1211 | */ |
| 1212 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 1213 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 1214 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1215 | if( pOrInfo==0 ) return; |
| 1216 | pTerm->wtFlags |= TERM_ORINFO; |
| 1217 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1218 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1219 | whereSplit(pOrWc, pExpr, TK_OR); |
| 1220 | exprAnalyzeAll(pSrc, pOrWc); |
| 1221 | if( db->mallocFailed ) return; |
| 1222 | assert( pOrWc->nTerm>=2 ); |
| 1223 | |
| 1224 | /* |
| 1225 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 1226 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 1227 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 1228 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1229 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 1230 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1231 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1232 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1233 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1234 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 1235 | if( pAndInfo ){ |
| 1236 | WhereClause *pAndWC; |
| 1237 | WhereTerm *pAndTerm; |
| 1238 | int j; |
| 1239 | Bitmask b = 0; |
| 1240 | pOrTerm->u.pAndInfo = pAndInfo; |
| 1241 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 1242 | pOrTerm->eOperator = WO_AND; |
| 1243 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1244 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1245 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 1246 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 1247 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 1248 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1249 | if( !db->mallocFailed ){ |
| 1250 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1251 | assert( pAndTerm->pExpr ); |
| 1252 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1253 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1254 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | indexable &= b; |
| 1258 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1259 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1260 | /* Skip this term for now. We revisit it when we process the |
| 1261 | ** corresponding TERM_VIRTUAL term */ |
| 1262 | }else{ |
| 1263 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1264 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1265 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1266 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1267 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1268 | } |
| 1269 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1270 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1271 | chngToIN = 0; |
| 1272 | }else{ |
| 1273 | chngToIN &= b; |
| 1274 | } |
| 1275 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1276 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1277 | |
| 1278 | /* |
| 1279 | ** Record the set of tables that satisfy case 2. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1280 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1281 | */ |
| 1282 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1283 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1284 | |
| 1285 | /* |
| 1286 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1287 | ** we have to do some additional checking to see if case 1 really |
| 1288 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1289 | ** |
| 1290 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1291 | ** that there is no possibility of transforming the OR clause into an |
| 1292 | ** IN operator because one or more terms in the OR clause contain |
| 1293 | ** something other than == on a column in the single table. The 1-bit |
| 1294 | ** case means that every term of the OR clause is of the form |
| 1295 | ** "table.column=expr" for some single table. The one bit that is set |
| 1296 | ** will correspond to the common table. We still need to check to make |
| 1297 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1298 | ** the all terms are of the form "table1.column=table2.column". It |
| 1299 | ** might be possible to form an IN operator with either table1.column |
| 1300 | ** or table2.column as the LHS if either is common to every term of |
| 1301 | ** the OR clause. |
| 1302 | ** |
| 1303 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1304 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1305 | */ |
| 1306 | if( chngToIN ){ |
| 1307 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1308 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1309 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1310 | int j = 0; /* Loop counter */ |
| 1311 | |
| 1312 | /* Search for a table and column that appears on one side or the |
| 1313 | ** other of the == operator in every subterm. That table and column |
| 1314 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1315 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1316 | ** and column is found but leave okToChngToIN false if not found. |
| 1317 | */ |
| 1318 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1319 | pOrTerm = pOrWc->a; |
| 1320 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1321 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1322 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1323 | if( pOrTerm->leftCursor==iCursor ){ |
| 1324 | /* This is the 2-bit case and we are on the second iteration and |
| 1325 | ** current term is from the first iteration. So skip this term. */ |
| 1326 | assert( j==1 ); |
| 1327 | continue; |
| 1328 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1329 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1330 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 1331 | ** chngToIN set but t1 is not. This term will be either preceeded |
| 1332 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1333 | ** and use its inversion. */ |
| 1334 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1335 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1336 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1337 | continue; |
| 1338 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1339 | iColumn = pOrTerm->u.leftColumn; |
| 1340 | iCursor = pOrTerm->leftCursor; |
| 1341 | break; |
| 1342 | } |
| 1343 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1344 | /* No candidate table+column was found. This can only occur |
| 1345 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1346 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1347 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1348 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1349 | break; |
| 1350 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1351 | testcase( j==1 ); |
| 1352 | |
| 1353 | /* We have found a candidate table and column. Check to see if that |
| 1354 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1355 | okToChngToIN = 1; |
| 1356 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1357 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1358 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1359 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1360 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1361 | okToChngToIN = 0; |
| 1362 | }else{ |
| 1363 | int affLeft, affRight; |
| 1364 | /* If the right-hand side is also a column, then the affinities |
| 1365 | ** of both right and left sides must be such that no type |
| 1366 | ** conversions are required on the right. (Ticket #2249) |
| 1367 | */ |
| 1368 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1369 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1370 | if( affRight!=0 && affRight!=affLeft ){ |
| 1371 | okToChngToIN = 0; |
| 1372 | }else{ |
| 1373 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1380 | ** case 1. In that case, construct a new virtual term that is |
| 1381 | ** pTerm converted into an IN operator. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1382 | ** |
| 1383 | ** EV: R-00211-15100 |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1384 | */ |
| 1385 | if( okToChngToIN ){ |
| 1386 | Expr *pDup; /* A transient duplicate expression */ |
| 1387 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1388 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1389 | Expr *pNew; /* The complete IN operator */ |
| 1390 | |
| 1391 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1392 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1393 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1394 | assert( pOrTerm->leftCursor==iCursor ); |
| 1395 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1396 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1397 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1398 | pLeft = pOrTerm->pExpr->pLeft; |
| 1399 | } |
| 1400 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1401 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1402 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1403 | if( pNew ){ |
| 1404 | int idxNew; |
| 1405 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1406 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1407 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1408 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1409 | testcase( idxNew==0 ); |
| 1410 | exprAnalyze(pSrc, pWC, idxNew); |
| 1411 | pTerm = &pWC->a[idxTerm]; |
| 1412 | pWC->a[idxNew].iParent = idxTerm; |
| 1413 | pTerm->nChild = 1; |
| 1414 | }else{ |
| 1415 | sqlite3ExprListDelete(db, pList); |
| 1416 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1417 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1418 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1419 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1420 | } |
| 1421 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1422 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1423 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1424 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1425 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1426 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1427 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1428 | ** |
| 1429 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1430 | ** to the standard form of "X <op> <expr>". |
| 1431 | ** |
| 1432 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1433 | ** columns, then the original expression is unchanged and a new virtual |
| 1434 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1435 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1436 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1437 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1438 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1439 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1440 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1441 | static void exprAnalyze( |
| 1442 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1443 | WhereClause *pWC, /* the WHERE clause */ |
| 1444 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1445 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1446 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1447 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1448 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1449 | Expr *pExpr; /* The expression to be analyzed */ |
| 1450 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1451 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1452 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1453 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1454 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1455 | int noCase = 0; /* LIKE/GLOB distinguishes case */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1456 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1457 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1458 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1459 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1460 | if( db->mallocFailed ){ |
| 1461 | return; |
| 1462 | } |
| 1463 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1464 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1465 | pExpr = pTerm->pExpr; |
| 1466 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1467 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1468 | op = pExpr->op; |
| 1469 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1470 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1471 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1472 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1473 | }else{ |
| 1474 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1475 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1476 | }else if( op==TK_ISNULL ){ |
| 1477 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1478 | }else{ |
| 1479 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1480 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1481 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1482 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1483 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1484 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1485 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1486 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1487 | } |
| 1488 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1489 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1490 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1491 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1492 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1493 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1494 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1495 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1496 | if( pLeft->op==TK_COLUMN ){ |
| 1497 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1498 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1499 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1500 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1501 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1502 | WhereTerm *pNew; |
| 1503 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1504 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1505 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1506 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1507 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1508 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1509 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1512 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1513 | if( idxNew==0 ) return; |
| 1514 | pNew = &pWC->a[idxNew]; |
| 1515 | pNew->iParent = idxTerm; |
| 1516 | pTerm = &pWC->a[idxTerm]; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1517 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1518 | pTerm->wtFlags |= TERM_COPIED; |
drh | eb5bc92 | 2013-01-17 16:43:33 +0000 | [diff] [blame] | 1519 | if( pExpr->op==TK_EQ |
| 1520 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1521 | && OptimizationEnabled(db, SQLITE_Transitive) |
| 1522 | ){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1523 | pTerm->eOperator |= WO_EQUIV; |
| 1524 | eExtraOp = WO_EQUIV; |
| 1525 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1526 | }else{ |
| 1527 | pDup = pExpr; |
| 1528 | pNew = pTerm; |
| 1529 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1530 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1531 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1532 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1533 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1534 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1535 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1536 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1537 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1538 | } |
| 1539 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1540 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1541 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1542 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1543 | ** that define the range that the BETWEEN implements. For example: |
| 1544 | ** |
| 1545 | ** a BETWEEN b AND c |
| 1546 | ** |
| 1547 | ** is converted into: |
| 1548 | ** |
| 1549 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1550 | ** |
| 1551 | ** The two new terms are added onto the end of the WhereClause object. |
| 1552 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1553 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1554 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1555 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1556 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1557 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1558 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1559 | int i; |
| 1560 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1561 | assert( pList!=0 ); |
| 1562 | assert( pList->nExpr==2 ); |
| 1563 | for(i=0; i<2; i++){ |
| 1564 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1565 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1566 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1567 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1568 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1569 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1570 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1571 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1572 | pTerm = &pWC->a[idxTerm]; |
| 1573 | pWC->a[idxNew].iParent = idxTerm; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1574 | } |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1575 | pTerm->nChild = 2; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1576 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1577 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1578 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1579 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1580 | /* Analyze a term that is composed of two or more subterms connected by |
| 1581 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1582 | */ |
| 1583 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1584 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1585 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1586 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1587 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1588 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1589 | |
| 1590 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1591 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1592 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1593 | ** |
| 1594 | ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints |
| 1595 | ** |
| 1596 | ** x>='abc' AND x<'abd' AND x LIKE 'abc%' |
| 1597 | ** |
| 1598 | ** The last character of the prefix "abc" is incremented to form the |
shane | 7bc71e5 | 2008-05-28 18:01:44 +0000 | [diff] [blame] | 1599 | ** termination condition "abd". |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1600 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1601 | if( pWC->op==TK_AND |
| 1602 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1603 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1604 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1605 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1606 | Expr *pNewExpr1; |
| 1607 | Expr *pNewExpr2; |
| 1608 | int idxNew1; |
| 1609 | int idxNew2; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1610 | Token sCollSeqName; /* Name of collating sequence */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1611 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1612 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1613 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1614 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1615 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1616 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1617 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1618 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1619 | /* The point is to increment the last character before the first |
| 1620 | ** wildcard. But if we increment '@', that will push it into the |
| 1621 | ** alphabetic range where case conversions will mess up the |
| 1622 | ** inequality. To avoid this, make sure to also run the full |
| 1623 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1624 | */ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 1625 | if( c=='A'-1 ) isComplete = 0; /* EV: R-64339-08207 */ |
| 1626 | |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1627 | |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1628 | c = sqlite3UpperToLower[c]; |
| 1629 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1630 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1631 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1632 | sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; |
| 1633 | sCollSeqName.n = 6; |
| 1634 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1635 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1636 | sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1637 | pStr1, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1638 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1639 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1640 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1641 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1642 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 1643 | sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1644 | pStr2, 0); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1645 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1646 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1647 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1648 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1649 | if( isComplete ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1650 | pWC->a[idxNew1].iParent = idxTerm; |
| 1651 | pWC->a[idxNew2].iParent = idxTerm; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1652 | pTerm->nChild = 2; |
| 1653 | } |
| 1654 | } |
| 1655 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1656 | |
| 1657 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1658 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1659 | ** current expression is of the form: column MATCH expr. |
| 1660 | ** This information is used by the xBestIndex methods of |
| 1661 | ** virtual tables. The native query optimizer does not attempt |
| 1662 | ** to do anything with MATCH functions. |
| 1663 | */ |
| 1664 | if( isMatchOfColumn(pExpr) ){ |
| 1665 | int idxNew; |
| 1666 | Expr *pRight, *pLeft; |
| 1667 | WhereTerm *pNewTerm; |
| 1668 | Bitmask prereqColumn, prereqExpr; |
| 1669 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1670 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1671 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1672 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1673 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1674 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1675 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1676 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1677 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1678 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1679 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1680 | pNewTerm = &pWC->a[idxNew]; |
| 1681 | pNewTerm->prereqRight = prereqExpr; |
| 1682 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1683 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1684 | pNewTerm->eOperator = WO_MATCH; |
| 1685 | pNewTerm->iParent = idxTerm; |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1686 | pTerm = &pWC->a[idxTerm]; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1687 | pTerm->nChild = 1; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1688 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1689 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1690 | } |
| 1691 | } |
| 1692 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1693 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1694 | #ifdef SQLITE_ENABLE_STAT3 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1695 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1696 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1697 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1698 | ** virtual term of that form. |
| 1699 | ** |
| 1700 | ** Note that the virtual term must be tagged with TERM_VNULL. This |
| 1701 | ** TERM_VNULL tag will suppress the not-null check at the beginning |
| 1702 | ** of the loop. Without the TERM_VNULL flag, the not-null check at |
| 1703 | ** the start of the loop will prevent any results from being returned. |
| 1704 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1705 | if( pExpr->op==TK_NOTNULL |
| 1706 | && pExpr->pLeft->op==TK_COLUMN |
| 1707 | && pExpr->pLeft->iColumn>=0 |
| 1708 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1709 | Expr *pNewExpr; |
| 1710 | Expr *pLeft = pExpr->pLeft; |
| 1711 | int idxNew; |
| 1712 | WhereTerm *pNewTerm; |
| 1713 | |
| 1714 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1715 | sqlite3ExprDup(db, pLeft, 0), |
| 1716 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1717 | |
| 1718 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1719 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1720 | if( idxNew ){ |
| 1721 | pNewTerm = &pWC->a[idxNew]; |
| 1722 | pNewTerm->prereqRight = 0; |
| 1723 | pNewTerm->leftCursor = pLeft->iTable; |
| 1724 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1725 | pNewTerm->eOperator = WO_GT; |
| 1726 | pNewTerm->iParent = idxTerm; |
| 1727 | pTerm = &pWC->a[idxTerm]; |
| 1728 | pTerm->nChild = 1; |
| 1729 | pTerm->wtFlags |= TERM_COPIED; |
| 1730 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1731 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1732 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 1733 | #endif /* SQLITE_ENABLE_STAT */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1734 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1735 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1736 | ** an index for tables to the left of the join. |
| 1737 | */ |
| 1738 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1741 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1742 | ** This function searches the expression list passed as the second argument |
| 1743 | ** for an expression of type TK_COLUMN that refers to the same column and |
| 1744 | ** uses the same collation sequence as the iCol'th column of index pIdx. |
| 1745 | ** Argument iBase is the cursor number used for the table that pIdx refers |
| 1746 | ** to. |
| 1747 | ** |
| 1748 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1749 | ** no expression is found, -1 is returned. |
| 1750 | */ |
| 1751 | static int findIndexCol( |
| 1752 | Parse *pParse, /* Parse context */ |
| 1753 | ExprList *pList, /* Expression list to search */ |
| 1754 | int iBase, /* Cursor for table associated with pIdx */ |
| 1755 | Index *pIdx, /* Index to match column of */ |
| 1756 | int iCol /* Column of index to match */ |
| 1757 | ){ |
| 1758 | int i; |
| 1759 | const char *zColl = pIdx->azColl[iCol]; |
| 1760 | |
| 1761 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1762 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1763 | if( p->op==TK_COLUMN |
| 1764 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1765 | && p->iTable==iBase |
| 1766 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1767 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1768 | if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1769 | return i; |
| 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | return -1; |
| 1775 | } |
| 1776 | |
| 1777 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1778 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1779 | ** is redundant. |
| 1780 | ** |
| 1781 | ** A DISTINCT list is redundant if the database contains some set of |
| 1782 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1783 | */ |
| 1784 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1785 | Parse *pParse, /* Parsing context */ |
| 1786 | SrcList *pTabList, /* The FROM clause */ |
| 1787 | WhereClause *pWC, /* The WHERE clause */ |
| 1788 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1789 | ){ |
| 1790 | Table *pTab; |
| 1791 | Index *pIdx; |
| 1792 | int i; |
| 1793 | int iBase; |
| 1794 | |
| 1795 | /* If there is more than one table or sub-select in the FROM clause of |
| 1796 | ** this query, then it will not be possible to show that the DISTINCT |
| 1797 | ** clause is redundant. */ |
| 1798 | if( pTabList->nSrc!=1 ) return 0; |
| 1799 | iBase = pTabList->a[0].iCursor; |
| 1800 | pTab = pTabList->a[0].pTab; |
| 1801 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1802 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1803 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1804 | ** current SELECT is a correlated sub-query. |
| 1805 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1806 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1807 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1808 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /* Loop through all indices on the table, checking each to see if it makes |
| 1812 | ** the DISTINCT qualifier redundant. It does so if: |
| 1813 | ** |
| 1814 | ** 1. The index is itself UNIQUE, and |
| 1815 | ** |
| 1816 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1817 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1818 | ** where X is a constant value. The collation sequences of the |
| 1819 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1820 | ** |
| 1821 | ** 3. All of those index columns for which the WHERE clause does not |
| 1822 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1823 | */ |
| 1824 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1825 | if( pIdx->onError==OE_None ) continue; |
| 1826 | for(i=0; i<pIdx->nColumn; i++){ |
| 1827 | int iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1828 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1829 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
| 1830 | if( iIdxCol<0 || pTab->aCol[pIdx->aiColumn[i]].notNull==0 ){ |
| 1831 | break; |
| 1832 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | if( i==pIdx->nColumn ){ |
| 1836 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1837 | return 1; |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | return 0; |
| 1842 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1843 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1844 | /* |
| 1845 | ** The sum of two WhereCosts |
| 1846 | */ |
| 1847 | static WhereCost whereCostAdd(WhereCost a, WhereCost b){ |
| 1848 | static const unsigned char x[] = { |
| 1849 | 10, 10, /* 0,1 */ |
| 1850 | 9, 9, /* 2,3 */ |
| 1851 | 8, 8, /* 4,5 */ |
| 1852 | 7, 7, 7, /* 6,7,8 */ |
| 1853 | 6, 6, 6, /* 9,10,11 */ |
| 1854 | 5, 5, 5, /* 12-14 */ |
| 1855 | 4, 4, 4, 4, /* 15-18 */ |
| 1856 | 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 1857 | 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 1858 | }; |
| 1859 | if( a>=b ){ |
| 1860 | if( a>b+49 ) return a; |
| 1861 | if( a>b+31 ) return a+1; |
| 1862 | return a+x[a-b]; |
| 1863 | }else{ |
| 1864 | if( b>a+49 ) return b; |
| 1865 | if( b>a+31 ) return b+1; |
| 1866 | return b+x[b-a]; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* |
| 1871 | ** Convert an integer into a WhereCost |
| 1872 | */ |
| 1873 | static WhereCost whereCostFromInt(tRowcnt x){ |
| 1874 | static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 1875 | WhereCost y = 40; |
| 1876 | if( x<8 ){ |
| 1877 | if( x<2 ) return 0; |
| 1878 | while( x<8 ){ y -= 10; x <<= 1; } |
| 1879 | }else{ |
| 1880 | while( x>255 ){ y += 40; x >>= 4; } |
| 1881 | while( x>15 ){ y += 10; x >>= 1; } |
| 1882 | } |
| 1883 | return a[x&7] + y - 10; |
| 1884 | } |
| 1885 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1886 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1887 | /* |
| 1888 | ** Convert a double (as received from xBestIndex of a virtual table) |
| 1889 | ** into a WhereCost |
| 1890 | */ |
| 1891 | static WhereCost whereCostFromDouble(double x){ |
| 1892 | u64 a; |
| 1893 | WhereCost e; |
| 1894 | assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 1895 | if( x<=1 ) return 0; |
| 1896 | if( x<=2000000000 ) return whereCostFromInt((tRowcnt)x); |
| 1897 | memcpy(&a, &x, 8); |
| 1898 | e = (a>>52) - 1022; |
| 1899 | return e*10; |
| 1900 | } |
| 1901 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1902 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1903 | /* |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 1904 | ** Prepare a crude estimate of the logarithm of the input value. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1905 | ** The results need not be exact. This is only used for estimating |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 1906 | ** the total cost of performing operations with O(logN) or O(NlogN) |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1907 | ** complexity. Because N is just a guess, it is no great tragedy if |
| 1908 | ** logN is a little off. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1909 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 1910 | static WhereCost estLog(WhereCost N){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 1911 | return whereCostFromInt(N) - 33; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1914 | /* |
| 1915 | ** Two routines for printing the content of an sqlite3_index_info |
| 1916 | ** structure. Used for testing and debugging only. If neither |
| 1917 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1918 | ** are no-ops. |
| 1919 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1920 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1921 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1922 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1923 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1924 | for(i=0; i<p->nConstraint; i++){ |
| 1925 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1926 | i, |
| 1927 | p->aConstraint[i].iColumn, |
| 1928 | p->aConstraint[i].iTermOffset, |
| 1929 | p->aConstraint[i].op, |
| 1930 | p->aConstraint[i].usable); |
| 1931 | } |
| 1932 | for(i=0; i<p->nOrderBy; i++){ |
| 1933 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1934 | i, |
| 1935 | p->aOrderBy[i].iColumn, |
| 1936 | p->aOrderBy[i].desc); |
| 1937 | } |
| 1938 | } |
| 1939 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1940 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1941 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1942 | for(i=0; i<p->nConstraint; i++){ |
| 1943 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1944 | i, |
| 1945 | p->aConstraintUsage[i].argvIndex, |
| 1946 | p->aConstraintUsage[i].omit); |
| 1947 | } |
| 1948 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1949 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1950 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1951 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 1952 | } |
| 1953 | #else |
| 1954 | #define TRACE_IDX_INPUTS(A) |
| 1955 | #define TRACE_IDX_OUTPUTS(A) |
| 1956 | #endif |
| 1957 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1958 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1959 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1960 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1961 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1962 | ** index existed. |
| 1963 | */ |
| 1964 | static int termCanDriveIndex( |
| 1965 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1966 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1967 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1968 | ){ |
| 1969 | char aff; |
| 1970 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1971 | if( (pTerm->eOperator & WO_EQ)==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1972 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 1973 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1974 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 1975 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 1976 | return 1; |
| 1977 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1978 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1979 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1980 | |
| 1981 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1982 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1983 | ** Generate code to construct the Index object for an automatic index |
| 1984 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 1985 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1986 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1987 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1988 | Parse *pParse, /* The parsing context */ |
| 1989 | WhereClause *pWC, /* The WHERE clause */ |
| 1990 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 1991 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 1992 | WhereLevel *pLevel /* Write new index here */ |
| 1993 | ){ |
| 1994 | int nColumn; /* Number of columns in the constructed index */ |
| 1995 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 1996 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 1997 | int nByte; /* Byte of memory needed for pIdx */ |
| 1998 | Index *pIdx; /* Object describing the transient index */ |
| 1999 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2000 | int addrInit; /* Address of the initialization bypass jump */ |
| 2001 | Table *pTable; /* The table being indexed */ |
| 2002 | KeyInfo *pKeyinfo; /* Key information for the index */ |
| 2003 | int addrTop; /* Top of the index fill loop */ |
| 2004 | int regRecord; /* Register holding an index record */ |
| 2005 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2006 | int i; /* Loop counter */ |
| 2007 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2008 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2009 | WhereLoop *pLoop; /* The Loop object */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2010 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 2011 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 2012 | const int mxConstraint = 10; /* Maximum number of constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2013 | |
| 2014 | /* Generate code to skip over the creation and initialization of the |
| 2015 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 2016 | v = pParse->pVdbe; |
| 2017 | assert( v!=0 ); |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2018 | addrInit = sqlite3CodeOnce(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2019 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2020 | /* Count the number of columns that will be added to the index |
| 2021 | ** and used to match WHERE clause constraints */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2022 | nColumn = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2023 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2024 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2025 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2026 | idxCols = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2027 | for(pTerm=pWC->a; pTerm<pWCEnd && pLoop->nLTerm<mxConstraint; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2028 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 2029 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2030 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2031 | testcase( iCol==BMS ); |
| 2032 | testcase( iCol==BMS-1 ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2033 | if( (idxCols & cMask)==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2034 | if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return; |
| 2035 | pLoop->aLTerm[nColumn++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2036 | idxCols |= cMask; |
| 2037 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | assert( nColumn>0 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2041 | pLoop->u.btree.nEq = pLoop->nLTerm = nColumn; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 2042 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
| 2043 | | WHERE_TEMP_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2044 | |
| 2045 | /* Count the number of additional columns needed to create a |
| 2046 | ** covering index. A "covering index" is an index that contains all |
| 2047 | ** columns that are needed by the query. With a covering index, the |
| 2048 | ** original table never needs to be accessed. Automatic indices must |
| 2049 | ** be a covering index because the index will not be updated if the |
| 2050 | ** original table changes and the index and table cannot both be used |
| 2051 | ** if they go out of sync. |
| 2052 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2053 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2054 | mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 2055 | testcase( pTable->nCol==BMS-1 ); |
| 2056 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2057 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2058 | if( extraCols & MASKBIT(i) ) nColumn++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2059 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2060 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2061 | nColumn += pTable->nCol - BMS + 1; |
| 2062 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2063 | pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2064 | |
| 2065 | /* Construct the Index object to describe this index */ |
| 2066 | nByte = sizeof(Index); |
| 2067 | nByte += nColumn*sizeof(int); /* Index.aiColumn */ |
| 2068 | nByte += nColumn*sizeof(char*); /* Index.azColl */ |
| 2069 | nByte += nColumn; /* Index.aSortOrder */ |
| 2070 | pIdx = sqlite3DbMallocZero(pParse->db, nByte); |
| 2071 | if( pIdx==0 ) return; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2072 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2073 | pIdx->azColl = (char**)&pIdx[1]; |
| 2074 | pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; |
| 2075 | pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; |
| 2076 | pIdx->zName = "auto-index"; |
| 2077 | pIdx->nColumn = nColumn; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 2078 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2079 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2080 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2081 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2082 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2083 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2084 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2085 | if( (idxCols & cMask)==0 ){ |
| 2086 | Expr *pX = pTerm->pExpr; |
| 2087 | idxCols |= cMask; |
| 2088 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 2089 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 6f2e6c0 | 2011-02-17 13:33:15 +0000 | [diff] [blame] | 2090 | pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 2091 | n++; |
| 2092 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2095 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2096 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2097 | /* Add additional columns needed to make the automatic index into |
| 2098 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2099 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2100 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2101 | pIdx->aiColumn[n] = i; |
| 2102 | pIdx->azColl[n] = "BINARY"; |
| 2103 | n++; |
| 2104 | } |
| 2105 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 2106 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 2107 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 2108 | pIdx->aiColumn[n] = i; |
| 2109 | pIdx->azColl[n] = "BINARY"; |
| 2110 | n++; |
| 2111 | } |
| 2112 | } |
| 2113 | assert( n==nColumn ); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2114 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2115 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2116 | pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); |
| 2117 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 2118 | pLevel->iIdxCur = pParse->nTab++; |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2119 | sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2120 | (char*)pKeyinfo, P4_KEYINFO_HANDOFF); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2121 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2122 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2123 | /* Fill the automatic index with content */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2124 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); |
| 2125 | regRecord = sqlite3GetTempReg(pParse); |
| 2126 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1); |
| 2127 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 2128 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2129 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 2130 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2131 | sqlite3VdbeJumpHere(v, addrTop); |
| 2132 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 2133 | |
| 2134 | /* Jump here when skipping the initialization */ |
| 2135 | sqlite3VdbeJumpHere(v, addrInit); |
| 2136 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 2137 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 2138 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 2139 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2140 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2141 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 2142 | ** responsibility of the caller to eventually release the structure |
| 2143 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 2144 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 2145 | static sqlite3_index_info *allocateIndexInfo( |
| 2146 | Parse *pParse, |
| 2147 | WhereClause *pWC, |
| 2148 | struct SrcList_item *pSrc, |
| 2149 | ExprList *pOrderBy |
| 2150 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2151 | int i, j; |
| 2152 | int nTerm; |
| 2153 | struct sqlite3_index_constraint *pIdxCons; |
| 2154 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 2155 | struct sqlite3_index_constraint_usage *pUsage; |
| 2156 | WhereTerm *pTerm; |
| 2157 | int nOrderBy; |
| 2158 | sqlite3_index_info *pIdxInfo; |
| 2159 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2160 | /*WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));*/ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2161 | |
| 2162 | /* Count the number of possible WHERE clause constraints referring |
| 2163 | ** to this virtual table */ |
| 2164 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 2165 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2166 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2167 | testcase( pTerm->eOperator & WO_IN ); |
| 2168 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2169 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2170 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2171 | nTerm++; |
| 2172 | } |
| 2173 | |
| 2174 | /* If the ORDER BY clause contains only columns in the current |
| 2175 | ** virtual table then allocate space for the aOrderBy part of |
| 2176 | ** the sqlite3_index_info structure. |
| 2177 | */ |
| 2178 | nOrderBy = 0; |
| 2179 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2180 | int n = pOrderBy->nExpr; |
| 2181 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2182 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2183 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 2184 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 2185 | if( i==n){ |
| 2186 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | /* Allocate the sqlite3_index_info structure |
| 2191 | */ |
| 2192 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 2193 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 2194 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 2195 | if( pIdxInfo==0 ){ |
| 2196 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2197 | return 0; |
| 2198 | } |
| 2199 | |
| 2200 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 2201 | ** many fields that are declared "const" to prevent xBestIndex from |
| 2202 | ** changing them. We have to do some funky casting in order to |
| 2203 | ** initialize those fields. |
| 2204 | */ |
| 2205 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 2206 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 2207 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 2208 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 2209 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 2210 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 2211 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 2212 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 2213 | pUsage; |
| 2214 | |
| 2215 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2216 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2217 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2218 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 2219 | testcase( pTerm->eOperator & WO_IN ); |
| 2220 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2221 | if( pTerm->eOperator & (WO_ISNULL) ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2222 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2223 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2224 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2225 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2226 | if( op==WO_IN ) op = WO_EQ; |
| 2227 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2228 | /* The direct assignment in the previous line is possible only because |
| 2229 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2230 | ** following asserts verify this fact. */ |
| 2231 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2232 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2233 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2234 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2235 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2236 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2237 | 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] | 2238 | j++; |
| 2239 | } |
| 2240 | for(i=0; i<nOrderBy; i++){ |
| 2241 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2242 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2243 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2244 | } |
| 2245 | |
| 2246 | return pIdxInfo; |
| 2247 | } |
| 2248 | |
| 2249 | /* |
| 2250 | ** The table object reference passed as the second argument to this function |
| 2251 | ** must represent a virtual table. This function invokes the xBestIndex() |
| 2252 | ** method of the virtual table with the sqlite3_index_info pointer passed |
| 2253 | ** as the argument. |
| 2254 | ** |
| 2255 | ** If an error occurs, pParse is populated with an error message and a |
| 2256 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2257 | ** part of the sqlite3_index_info structure is left populated. |
| 2258 | ** |
| 2259 | ** Whether or not an error is returned, it is the responsibility of the |
| 2260 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2261 | ** that this is required. |
| 2262 | */ |
| 2263 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2264 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2265 | int i; |
| 2266 | int rc; |
| 2267 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2268 | /*WHERETRACE(("xBestIndex for %s\n", pTab->zName));*/ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2269 | TRACE_IDX_INPUTS(p); |
| 2270 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2271 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2272 | |
| 2273 | if( rc!=SQLITE_OK ){ |
| 2274 | if( rc==SQLITE_NOMEM ){ |
| 2275 | pParse->db->mallocFailed = 1; |
| 2276 | }else if( !pVtab->zErrMsg ){ |
| 2277 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2278 | }else{ |
| 2279 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2280 | } |
| 2281 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2282 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2283 | pVtab->zErrMsg = 0; |
| 2284 | |
| 2285 | for(i=0; i<p->nConstraint; i++){ |
| 2286 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2287 | sqlite3ErrorMsg(pParse, |
| 2288 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | return pParse->nErr; |
| 2293 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2294 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2295 | |
| 2296 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2297 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2298 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2299 | ** Estimate the location of a particular key among all keys in an |
| 2300 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2301 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2302 | ** aStat[0] Est. number of rows less than pVal |
| 2303 | ** aStat[1] Est. number of rows equal to pVal |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2304 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2305 | ** Return SQLITE_OK on success. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2306 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2307 | static int whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2308 | Parse *pParse, /* Database connection */ |
| 2309 | Index *pIdx, /* Index to consider domain of */ |
| 2310 | sqlite3_value *pVal, /* Value to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2311 | int roundUp, /* Round up if true. Round down if false */ |
| 2312 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2313 | ){ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2314 | tRowcnt n; |
| 2315 | IndexSample *aSample; |
| 2316 | int i, eType; |
| 2317 | int isEq = 0; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2318 | i64 v; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2319 | double r, rS; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2320 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2321 | assert( roundUp==0 || roundUp==1 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2322 | assert( pIdx->nSample>0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2323 | if( pVal==0 ) return SQLITE_ERROR; |
| 2324 | n = pIdx->aiRowEst[0]; |
| 2325 | aSample = pIdx->aSample; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2326 | eType = sqlite3_value_type(pVal); |
| 2327 | |
| 2328 | if( eType==SQLITE_INTEGER ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2329 | v = sqlite3_value_int64(pVal); |
| 2330 | r = (i64)v; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2331 | for(i=0; i<pIdx->nSample; i++){ |
| 2332 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2333 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2334 | if( aSample[i].eType==SQLITE_INTEGER ){ |
| 2335 | if( aSample[i].u.i>=v ){ |
| 2336 | isEq = aSample[i].u.i==v; |
| 2337 | break; |
| 2338 | } |
| 2339 | }else{ |
| 2340 | assert( aSample[i].eType==SQLITE_FLOAT ); |
| 2341 | if( aSample[i].u.r>=r ){ |
| 2342 | isEq = aSample[i].u.r==r; |
| 2343 | break; |
| 2344 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2345 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2346 | } |
| 2347 | }else if( eType==SQLITE_FLOAT ){ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2348 | r = sqlite3_value_double(pVal); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2349 | for(i=0; i<pIdx->nSample; i++){ |
| 2350 | if( aSample[i].eType==SQLITE_NULL ) continue; |
| 2351 | if( aSample[i].eType>=SQLITE_TEXT ) break; |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2352 | if( aSample[i].eType==SQLITE_FLOAT ){ |
| 2353 | rS = aSample[i].u.r; |
| 2354 | }else{ |
| 2355 | rS = aSample[i].u.i; |
| 2356 | } |
| 2357 | if( rS>=r ){ |
| 2358 | isEq = rS==r; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2359 | break; |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2360 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2361 | } |
| 2362 | }else if( eType==SQLITE_NULL ){ |
| 2363 | i = 0; |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2364 | if( aSample[0].eType==SQLITE_NULL ) isEq = 1; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2365 | }else{ |
| 2366 | assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); |
| 2367 | for(i=0; i<pIdx->nSample; i++){ |
| 2368 | if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){ |
| 2369 | break; |
| 2370 | } |
| 2371 | } |
| 2372 | if( i<pIdx->nSample ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2373 | sqlite3 *db = pParse->db; |
| 2374 | CollSeq *pColl; |
| 2375 | const u8 *z; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2376 | if( eType==SQLITE_BLOB ){ |
| 2377 | z = (const u8 *)sqlite3_value_blob(pVal); |
| 2378 | pColl = db->pDfltColl; |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2379 | assert( pColl->enc==SQLITE_UTF8 ); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2380 | }else{ |
drh | 79e72a5 | 2012-10-05 14:43:40 +0000 | [diff] [blame] | 2381 | pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl); |
drh | 9aeda79 | 2009-08-20 02:34:15 +0000 | [diff] [blame] | 2382 | if( pColl==0 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2383 | return SQLITE_ERROR; |
| 2384 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2385 | z = (const u8 *)sqlite3ValueText(pVal, pColl->enc); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2386 | if( !z ){ |
| 2387 | return SQLITE_NOMEM; |
| 2388 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2389 | assert( z && pColl && pColl->xCmp ); |
| 2390 | } |
| 2391 | n = sqlite3ValueBytes(pVal, pColl->enc); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2392 | |
| 2393 | for(; i<pIdx->nSample; i++){ |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2394 | int c; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2395 | int eSampletype = aSample[i].eType; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2396 | if( eSampletype<eType ) continue; |
| 2397 | if( eSampletype!=eType ) break; |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2398 | #ifndef SQLITE_OMIT_UTF16 |
| 2399 | if( pColl->enc!=SQLITE_UTF8 ){ |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2400 | int nSample; |
| 2401 | char *zSample = sqlite3Utf8to16( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2402 | db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample |
| 2403 | ); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2404 | if( !zSample ){ |
| 2405 | assert( db->mallocFailed ); |
| 2406 | return SQLITE_NOMEM; |
| 2407 | } |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2408 | c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z); |
dan | e275dc3 | 2009-08-18 16:24:58 +0000 | [diff] [blame] | 2409 | sqlite3DbFree(db, zSample); |
dan | e83c4f3 | 2009-09-21 16:34:24 +0000 | [diff] [blame] | 2410 | }else |
| 2411 | #endif |
| 2412 | { |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2413 | 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] | 2414 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2415 | if( c>=0 ){ |
| 2416 | if( c==0 ) isEq = 1; |
| 2417 | break; |
| 2418 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2419 | } |
| 2420 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2421 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2422 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2423 | /* At this point, aSample[i] is the first sample that is greater than |
| 2424 | ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less |
| 2425 | ** than pVal. If aSample[i]==pVal, then isEq==1. |
| 2426 | */ |
| 2427 | if( isEq ){ |
| 2428 | assert( i<pIdx->nSample ); |
| 2429 | aStat[0] = aSample[i].nLt; |
| 2430 | aStat[1] = aSample[i].nEq; |
| 2431 | }else{ |
| 2432 | tRowcnt iLower, iUpper, iGap; |
| 2433 | if( i==0 ){ |
| 2434 | iLower = 0; |
| 2435 | iUpper = aSample[0].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2436 | }else{ |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2437 | iUpper = i>=pIdx->nSample ? n : aSample[i].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2438 | iLower = aSample[i-1].nEq + aSample[i-1].nLt; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2439 | } |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 2440 | aStat[1] = pIdx->avgEq; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2441 | if( iLower>=iUpper ){ |
| 2442 | iGap = 0; |
| 2443 | }else{ |
| 2444 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2445 | } |
| 2446 | if( roundUp ){ |
| 2447 | iGap = (iGap*2)/3; |
| 2448 | }else{ |
| 2449 | iGap = iGap/3; |
| 2450 | } |
| 2451 | aStat[0] = iLower + iGap; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2452 | } |
| 2453 | return SQLITE_OK; |
| 2454 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2455 | #endif /* SQLITE_ENABLE_STAT3 */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2456 | |
| 2457 | /* |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2458 | ** If expression pExpr represents a literal value, set *pp to point to |
| 2459 | ** an sqlite3_value structure containing the same value, with affinity |
| 2460 | ** aff applied to it, before returning. It is the responsibility of the |
| 2461 | ** caller to eventually release this structure by passing it to |
| 2462 | ** sqlite3ValueFree(). |
| 2463 | ** |
| 2464 | ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr |
| 2465 | ** is an SQL variable that currently has a non-NULL value bound to it, |
| 2466 | ** create an sqlite3_value structure containing this value, again with |
| 2467 | ** affinity aff applied to it, instead. |
| 2468 | ** |
| 2469 | ** If neither of the above apply, set *pp to NULL. |
| 2470 | ** |
| 2471 | ** If an error occurs, return an error code. Otherwise, SQLITE_OK. |
| 2472 | */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2473 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2474 | static int valueFromExpr( |
| 2475 | Parse *pParse, |
| 2476 | Expr *pExpr, |
| 2477 | u8 aff, |
| 2478 | sqlite3_value **pp |
| 2479 | ){ |
drh | 4278d53 | 2010-12-16 19:52:52 +0000 | [diff] [blame] | 2480 | if( pExpr->op==TK_VARIABLE |
| 2481 | || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) |
| 2482 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2483 | int iVar = pExpr->iColumn; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 2484 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2485 | *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); |
| 2486 | return SQLITE_OK; |
| 2487 | } |
| 2488 | return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); |
| 2489 | } |
dan | f7b0b0a | 2009-10-19 15:52:32 +0000 | [diff] [blame] | 2490 | #endif |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2491 | |
| 2492 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2493 | ** This function is used to estimate the number of rows that will be visited |
| 2494 | ** by scanning an index for a range of values. The range may have an upper |
| 2495 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2496 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2497 | ** example, assuming that index p is on t1(a): |
| 2498 | ** |
| 2499 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2500 | ** |_____| |_____| |
| 2501 | ** | | |
| 2502 | ** pLower pUpper |
| 2503 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2504 | ** 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] | 2505 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2506 | ** |
| 2507 | ** The nEq parameter is passed the index of the index column subject to the |
| 2508 | ** range constraint. Or, equivalently, the number of equality constraints |
| 2509 | ** optimized by the proposed index scan. For example, assuming index p is |
| 2510 | ** on t1(a, b), and the SQL query is: |
| 2511 | ** |
| 2512 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2513 | ** |
| 2514 | ** then nEq should be passed the value 1 (as the range restricted column, |
| 2515 | ** b, is the second left-most column of the index). Or, if the query is: |
| 2516 | ** |
| 2517 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2518 | ** |
| 2519 | ** then nEq should be passed 0. |
| 2520 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2521 | ** The returned value is an integer divisor to reduce the estimated |
| 2522 | ** search space. A return value of 1 means that range constraints are |
| 2523 | ** no help at all. A return value of 2 means range constraints are |
| 2524 | ** expected to reduce the search space by half. And so forth... |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2525 | ** |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2526 | ** In the absence of sqlite_stat3 ANALYZE data, each range inequality |
| 2527 | ** reduces the search space by a factor of 4. Hence a single constraint (x>?) |
| 2528 | ** results in a return of 4 and a range constraint (x>? AND x<?) results |
| 2529 | ** in a return of 16. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2530 | */ |
| 2531 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2532 | Parse *pParse, /* Parsing & code generating context */ |
| 2533 | Index *p, /* The index containing the range-compared column; "x" */ |
| 2534 | int nEq, /* index into p->aCol[] of the range-compared column */ |
| 2535 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2536 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2537 | WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2538 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2539 | int rc = SQLITE_OK; |
| 2540 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2541 | #ifdef SQLITE_ENABLE_STAT3 |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2542 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2543 | if( nEq==0 && p->nSample ){ |
| 2544 | sqlite3_value *pRangeVal; |
| 2545 | tRowcnt iLower = 0; |
| 2546 | tRowcnt iUpper = p->aiRowEst[0]; |
| 2547 | tRowcnt a[2]; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2548 | u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2549 | |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2550 | if( pLower ){ |
| 2551 | Expr *pExpr = pLower->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2552 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2553 | assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2554 | if( rc==SQLITE_OK |
| 2555 | && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK |
| 2556 | ){ |
| 2557 | iLower = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2558 | if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2559 | } |
| 2560 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2561 | } |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2562 | if( rc==SQLITE_OK && pUpper ){ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2563 | Expr *pExpr = pUpper->pExpr->pRight; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2564 | rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2565 | assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2566 | if( rc==SQLITE_OK |
| 2567 | && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK |
| 2568 | ){ |
| 2569 | iUpper = a[0]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2570 | if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2571 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2572 | sqlite3ValueFree(pRangeVal); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2573 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2574 | if( rc==SQLITE_OK ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2575 | WhereCost iBase = whereCostFromInt(p->aiRowEst[0]); |
| 2576 | if( iUpper>iLower ){ |
| 2577 | iBase -= whereCostFromInt(iUpper - iLower); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2578 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2579 | *pRangeDiv = iBase; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2580 | /*WHERETRACE(("range scan regions: %u..%u div=%g\n", |
| 2581 | (u32)iLower, (u32)iUpper, *pRangeDiv));*/ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2582 | return SQLITE_OK; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2583 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2584 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2585 | #else |
| 2586 | UNUSED_PARAMETER(pParse); |
| 2587 | UNUSED_PARAMETER(p); |
| 2588 | UNUSED_PARAMETER(nEq); |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2589 | #endif |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2590 | assert( pLower || pUpper ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2591 | *pRangeDiv = 0; |
| 2592 | if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){ |
| 2593 | *pRangeDiv += 20; assert( 20==whereCostFromInt(4) ); |
| 2594 | } |
| 2595 | if( pUpper ){ |
| 2596 | *pRangeDiv += 20; assert( 20==whereCostFromInt(4) ); |
| 2597 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2598 | return rc; |
| 2599 | } |
| 2600 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2601 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2602 | /* |
| 2603 | ** Estimate the number of rows that will be returned based on |
| 2604 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2605 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2606 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2607 | ** for that index. When pExpr==NULL that means the constraint is |
| 2608 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2609 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2610 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2611 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2612 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2613 | ** |
| 2614 | ** This routine can fail if it is unable to load a collating sequence |
| 2615 | ** required for string comparison, or if unable to allocate memory |
| 2616 | ** for a UTF conversion required for comparison. The error is stored |
| 2617 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2618 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2619 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2620 | Parse *pParse, /* Parsing & code generating context */ |
| 2621 | Index *p, /* The index whose left-most column is pTerm */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2622 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2623 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2624 | ){ |
| 2625 | sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2626 | u8 aff; /* Column affinity */ |
| 2627 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2628 | tRowcnt a[2]; /* Statistics */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2629 | |
| 2630 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2631 | assert( p->nSample>0 ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2632 | aff = p->pTable->aCol[p->aiColumn[0]].affinity; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2633 | if( pExpr ){ |
| 2634 | rc = valueFromExpr(pParse, pExpr, aff, &pRhs); |
| 2635 | if( rc ) goto whereEqualScanEst_cancel; |
| 2636 | }else{ |
| 2637 | pRhs = sqlite3ValueNew(pParse->db); |
| 2638 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2639 | if( pRhs==0 ) return SQLITE_NOTFOUND; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2640 | rc = whereKeyStats(pParse, p, pRhs, 0, a); |
| 2641 | if( rc==SQLITE_OK ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2642 | /*WHERETRACE(("equality scan regions: %d\n", (int)a[1]));*/ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2643 | *pnRow = a[1]; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2644 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2645 | whereEqualScanEst_cancel: |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2646 | sqlite3ValueFree(pRhs); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2647 | return rc; |
| 2648 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2649 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2650 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2651 | #ifdef SQLITE_ENABLE_STAT3 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2652 | /* |
| 2653 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2654 | ** an IN constraint where the right-hand side of the IN operator |
| 2655 | ** is a list of values. Example: |
| 2656 | ** |
| 2657 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2658 | ** |
| 2659 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2660 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2661 | ** non-zero. |
| 2662 | ** |
| 2663 | ** This routine can fail if it is unable to load a collating sequence |
| 2664 | ** required for string comparison, or if unable to allocate memory |
| 2665 | ** for a UTF conversion required for comparison. The error is stored |
| 2666 | ** in the pParse structure. |
| 2667 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2668 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2669 | Parse *pParse, /* Parsing & code generating context */ |
| 2670 | Index *p, /* The index whose left-most column is pTerm */ |
| 2671 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2672 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2673 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2674 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2675 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2676 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2677 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2678 | |
| 2679 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2680 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 2681 | nEst = p->aiRowEst[0]; |
| 2682 | rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst); |
| 2683 | nRowEst += nEst; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2684 | } |
| 2685 | if( rc==SQLITE_OK ){ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2686 | if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; |
| 2687 | *pnRow = nRowEst; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 2688 | /*WHERETRACE(("IN row estimate: est=%g\n", nRowEst));*/ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2689 | } |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2690 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2691 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2692 | #endif /* defined(SQLITE_ENABLE_STAT3) */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2693 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2694 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2695 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2696 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2697 | ** or USING clause of that join. |
| 2698 | ** |
| 2699 | ** Consider the term t2.z='ok' in the following queries: |
| 2700 | ** |
| 2701 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2702 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2703 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2704 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2705 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2706 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2707 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2708 | ** |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2709 | ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are |
| 2710 | ** completely satisfied by indices. |
| 2711 | ** |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2712 | ** 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] | 2713 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2714 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2715 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2716 | ** but joins might run a little slower. The trick is to disable as much |
| 2717 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2718 | ** the wrong answer. See ticket #813. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2719 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2720 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 2721 | if( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2722 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2723 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 2724 | ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 2725 | pTerm->wtFlags |= TERM_CODED; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 2726 | if( pTerm->iParent>=0 ){ |
| 2727 | WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; |
| 2728 | if( (--pOther->nChild)==0 ){ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 2729 | disableTerm(pLevel, pOther); |
| 2730 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2731 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2736 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2737 | ** to the n registers starting at base. |
| 2738 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2739 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2740 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2741 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2742 | ** |
| 2743 | ** This routine makes its own copy of zAff so that the caller is free |
| 2744 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2745 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2746 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2747 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2748 | if( zAff==0 ){ |
| 2749 | assert( pParse->db->mallocFailed ); |
| 2750 | return; |
| 2751 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2752 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2753 | |
| 2754 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2755 | ** and end of the affinity string. |
| 2756 | */ |
| 2757 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2758 | n--; |
| 2759 | base++; |
| 2760 | zAff++; |
| 2761 | } |
| 2762 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2763 | n--; |
| 2764 | } |
| 2765 | |
| 2766 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2767 | if( n>0 ){ |
| 2768 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2769 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2770 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2771 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2774 | |
| 2775 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2776 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2777 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2778 | ** coded. |
| 2779 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2780 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2781 | ** |
| 2782 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2783 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2784 | ** 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] | 2785 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2786 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2787 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2788 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2789 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2790 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2791 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2792 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2793 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2794 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2795 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2796 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2797 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2798 | assert( iTarget>0 ); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2799 | if( pX->op==TK_EQ ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2800 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2801 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2802 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2803 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2804 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2805 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2806 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2807 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2808 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2809 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2810 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2811 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2812 | && pLoop->u.btree.pIndex!=0 |
| 2813 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2814 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2815 | testcase( iEq==0 ); |
| 2816 | testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 ); |
| 2817 | testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn ); |
| 2818 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2819 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2820 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2821 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2822 | iReg = iTarget; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2823 | eType = sqlite3FindInIndex(pParse, pX, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2824 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2825 | testcase( bRev ); |
| 2826 | bRev = !bRev; |
| 2827 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2828 | iTab = pX->iTable; |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2829 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2830 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2831 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2832 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2833 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2834 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2835 | pLevel->u.in.nIn++; |
| 2836 | pLevel->u.in.aInLoop = |
| 2837 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2838 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2839 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2840 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2841 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2842 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2843 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2844 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2845 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2846 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2847 | } |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 2848 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2849 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2850 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2851 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2852 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2853 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2854 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2855 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2856 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2859 | /* |
| 2860 | ** Generate code that will evaluate all == and IN constraints for an |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2861 | ** index. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2862 | ** |
| 2863 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2864 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2865 | ** The index has as many as three equality constraints, but in this |
| 2866 | ** example, the third "c" value is an inequality. So only two |
| 2867 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2868 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2869 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2870 | ** |
| 2871 | ** In the example above nEq==2. But this subroutine works for any value |
| 2872 | ** 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] | 2873 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2874 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2875 | ** |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2876 | ** This routine always allocates at least one memory cell and returns |
| 2877 | ** the index of that memory cell. The code that |
| 2878 | ** calls this routine will use that memory cell to store the termination |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2879 | ** key value of the loop. If one or more IN operators appear, then |
| 2880 | ** this routine allocates an additional nEq memory cells for internal |
| 2881 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2882 | ** |
| 2883 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2884 | ** copy of the column affinity string of the index allocated using |
| 2885 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2886 | ** with equality constraints that use NONE affinity are set to |
| 2887 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2888 | ** |
| 2889 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2890 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2891 | ** |
| 2892 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2893 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2894 | ** no conversion should be attempted before using a t2.b value as part of |
| 2895 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2896 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2897 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2898 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2899 | Parse *pParse, /* Parsing context */ |
| 2900 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 2901 | WhereClause *pWC, /* The WHERE clause */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2902 | Bitmask notReady, /* Which parts of FROM have not yet been coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2903 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2904 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2905 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2906 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2907 | int nEq; /* The number of == or IN constraints to code */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2908 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2909 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2910 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2911 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2912 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2913 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2914 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2915 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2916 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2917 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2918 | pLoop = pLevel->pWLoop; |
| 2919 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2920 | nEq = pLoop->u.btree.nEq; |
| 2921 | pIdx = pLoop->u.btree.pIndex; |
| 2922 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2923 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2924 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2925 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2926 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2927 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2928 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2929 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2930 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2931 | if( !zAff ){ |
| 2932 | pParse->db->mallocFailed = 1; |
| 2933 | } |
| 2934 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2935 | /* Evaluate the equality constraints |
| 2936 | */ |
drh | c49de5d | 2007-01-19 01:06:01 +0000 | [diff] [blame] | 2937 | assert( pIdx->nColumn>=nEq ); |
| 2938 | for(j=0; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2939 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 2940 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2941 | assert( pTerm!=0 ); |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2942 | /* The following true for indices with redundant columns. |
| 2943 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 2944 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 2945 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2946 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2947 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2948 | if( nReg==1 ){ |
| 2949 | sqlite3ReleaseTempReg(pParse, regBase); |
| 2950 | regBase = r1; |
| 2951 | }else{ |
| 2952 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 2953 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2954 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 2955 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 2956 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2957 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2958 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 2959 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2960 | if( zAff ){ |
| 2961 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 2962 | zAff[j] = SQLITE_AFF_NONE; |
| 2963 | } |
| 2964 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 2965 | zAff[j] = SQLITE_AFF_NONE; |
| 2966 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2967 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2968 | } |
| 2969 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2970 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2971 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 2974 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2975 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 2976 | ** This routine is a helper for explainIndexRange() below |
| 2977 | ** |
| 2978 | ** pStr holds the text of an expression that we are building up one term |
| 2979 | ** at a time. This routine adds a new term to the end of the expression. |
| 2980 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 2981 | ** terms only. |
| 2982 | */ |
| 2983 | static void explainAppendTerm( |
| 2984 | StrAccum *pStr, /* The text expression being built */ |
| 2985 | int iTerm, /* Index of this term. First is zero */ |
| 2986 | const char *zColumn, /* Name of the column */ |
| 2987 | const char *zOp /* Name of the operator */ |
| 2988 | ){ |
| 2989 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 2990 | sqlite3StrAccumAppend(pStr, zColumn, -1); |
| 2991 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 2992 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 2993 | } |
| 2994 | |
| 2995 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 2996 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 2997 | ** function returns a pointer to a string buffer containing a description |
| 2998 | ** of the subset of table rows scanned by the strategy in the form of an |
| 2999 | ** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 3000 | ** |
| 3001 | ** For example, if the query: |
| 3002 | ** |
| 3003 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 3004 | ** |
| 3005 | ** is run and there is an index on (a, b), then this function returns a |
| 3006 | ** string similar to: |
| 3007 | ** |
| 3008 | ** "a=? AND b>?" |
| 3009 | ** |
| 3010 | ** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 3011 | ** It is the responsibility of the caller to free the buffer when it is |
| 3012 | ** no longer required. |
| 3013 | */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3014 | static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 3015 | Index *pIndex = pLoop->u.btree.pIndex; |
| 3016 | int nEq = pLoop->u.btree.nEq; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3017 | int i, j; |
| 3018 | Column *aCol = pTab->aCol; |
| 3019 | int *aiColumn = pIndex->aiColumn; |
| 3020 | StrAccum txt; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3021 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3022 | if( pIndex==0 ) return 0; |
| 3023 | if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3024 | return 0; |
| 3025 | } |
| 3026 | sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
drh | 03b6df1 | 2010-11-15 16:29:30 +0000 | [diff] [blame] | 3027 | txt.db = db; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3028 | sqlite3StrAccumAppend(&txt, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3029 | for(i=0; i<nEq; i++){ |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3030 | explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "="); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3033 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3034 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3035 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3036 | explainAppendTerm(&txt, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3037 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3038 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3039 | char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName; |
| 3040 | explainAppendTerm(&txt, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3041 | } |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3042 | sqlite3StrAccumAppend(&txt, ")", 1); |
| 3043 | return sqlite3StrAccumFinish(&txt); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3046 | /* |
| 3047 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 3048 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| 3049 | ** record is added to the output to describe the table scan strategy in |
| 3050 | ** pLevel. |
| 3051 | */ |
| 3052 | static void explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3053 | Parse *pParse, /* Parse context */ |
| 3054 | SrcList *pTabList, /* Table list this loop refers to */ |
| 3055 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 3056 | int iLevel, /* Value for "level" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3057 | int iFrom, /* Value for "from" column of output */ |
| 3058 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3059 | ){ |
| 3060 | if( pParse->explain==2 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3061 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3062 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 3063 | sqlite3 *db = pParse->db; /* Database handle */ |
| 3064 | char *zMsg; /* Text to add to EQP output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3065 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3066 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3067 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 3068 | u32 flags; /* Flags that describe this loop */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3069 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3070 | pLoop = pLevel->pWLoop; |
| 3071 | flags = pLoop->wsFlags; |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3072 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3073 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3074 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 3075 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 3076 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3077 | |
| 3078 | zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3079 | if( pItem->pSelect ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3080 | zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3081 | }else{ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3082 | zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3085 | if( pItem->zAlias ){ |
| 3086 | zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 3087 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3088 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
| 3089 | && pLoop->u.btree.pIndex!=0 |
| 3090 | ){ |
| 3091 | char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3092 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3093 | ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), |
| 3094 | ((flags & WHERE_IDX_ONLY)?"COVERING ":""), |
| 3095 | ((flags & WHERE_TEMP_INDEX)?"":" "), |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3096 | ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName), |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3097 | zWhere |
| 3098 | ); |
| 3099 | sqlite3DbFree(db, zWhere); |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 3100 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3101 | zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3102 | |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 3103 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3104 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3105 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3106 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 3107 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 3108 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| 3109 | }else if( flags&WHERE_TOP_LIMIT ){ |
| 3110 | zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 3111 | } |
| 3112 | } |
| 3113 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3114 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3115 | zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3116 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3117 | } |
| 3118 | #endif |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3119 | zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3120 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | #else |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3124 | # define explainOneScan(u,v,w,x,y,z) |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3125 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 3126 | |
| 3127 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3128 | /* |
| 3129 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 3130 | ** implementation described by pWInfo. |
| 3131 | */ |
| 3132 | static Bitmask codeOneLoopStart( |
| 3133 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 3134 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3135 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3136 | ){ |
| 3137 | int j, k; /* Loop counters */ |
| 3138 | int iCur; /* The VDBE cursor for the table */ |
| 3139 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 3140 | int omitTable; /* True if we use the index only */ |
| 3141 | int bRev; /* True if we need to scan in reverse order */ |
| 3142 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3143 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3144 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 3145 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 3146 | Parse *pParse; /* Parsing context */ |
| 3147 | Vdbe *v; /* The prepared stmt under constructions */ |
| 3148 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3149 | int addrBrk; /* Jump here to break out of the loop */ |
| 3150 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3151 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 3152 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3153 | Bitmask newNotReady; /* Return value */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3154 | |
| 3155 | pParse = pWInfo->pParse; |
| 3156 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3157 | pWC = &pWInfo->sWC; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3158 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3159 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3160 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 3161 | iCur = pTabItem->iCursor; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3162 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 3163 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3164 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3165 | VdbeNoopComment((v, "Begin Join Loop %d", iLevel)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3166 | |
| 3167 | /* Create labels for the "break" and "continue" instructions |
| 3168 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 3169 | ** Jump to cont to go immediately to the next iteration of the |
| 3170 | ** loop. |
| 3171 | ** |
| 3172 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 3173 | ** means to continue with the next IN value combination. When |
| 3174 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 3175 | ** is the same as "addrBrk". |
| 3176 | */ |
| 3177 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 3178 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 3179 | |
| 3180 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 3181 | ** initialize a memory cell that records if this table matches any |
| 3182 | ** row of the left table of the join. |
| 3183 | */ |
| 3184 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3185 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3186 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3187 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3188 | } |
| 3189 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3190 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3191 | if( pTabItem->viaCoroutine ){ |
| 3192 | int regYield = pTabItem->regReturn; |
| 3193 | sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield); |
| 3194 | pLevel->p2 = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 3195 | VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName)); |
| 3196 | sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk); |
| 3197 | pLevel->op = OP_Goto; |
| 3198 | }else |
| 3199 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3200 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3201 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3202 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3203 | ** to access the data. |
| 3204 | */ |
| 3205 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3206 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3207 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3208 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3209 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3210 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3211 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3212 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3213 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3214 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3215 | if( pTerm->eOperator & WO_IN ){ |
| 3216 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3217 | addrNotFound = pLevel->addrNxt; |
| 3218 | }else{ |
| 3219 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3220 | } |
| 3221 | } |
| 3222 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3223 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3224 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3225 | pLoop->u.vtab.idxStr, |
| 3226 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
| 3227 | pLoop->u.vtab.needFree = 0; |
| 3228 | for(j=0; j<nConstraint && j<16; j++){ |
| 3229 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3230 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | pLevel->op = OP_VNext; |
| 3234 | pLevel->p1 = iCur; |
| 3235 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3236 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3237 | sqlite3ExprCachePop(pParse, 1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3238 | }else |
| 3239 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3240 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3241 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3242 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3243 | ){ |
| 3244 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3245 | ** equality comparison against the ROWID field. Or |
| 3246 | ** we reference multiple rows using a "rowid IN (...)" |
| 3247 | ** construct. |
| 3248 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3249 | assert( pLoop->u.btree.nEq==1 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3250 | iReleaseReg = sqlite3GetTempReg(pParse); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3251 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3252 | assert( pTerm!=0 ); |
| 3253 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3254 | assert( omitTable==0 ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3255 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3256 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3257 | addrNxt = pLevel->addrNxt; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3258 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 3259 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3260 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3261 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3262 | VdbeComment((v, "pk")); |
| 3263 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3264 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3265 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3266 | ){ |
| 3267 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3268 | */ |
| 3269 | int testOp = OP_Noop; |
| 3270 | int start; |
| 3271 | int memEndValue = 0; |
| 3272 | WhereTerm *pStart, *pEnd; |
| 3273 | |
| 3274 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3275 | j = 0; |
| 3276 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3277 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3278 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3279 | if( bRev ){ |
| 3280 | pTerm = pStart; |
| 3281 | pStart = pEnd; |
| 3282 | pEnd = pTerm; |
| 3283 | } |
| 3284 | if( pStart ){ |
| 3285 | Expr *pX; /* The expression that defines the start bound */ |
| 3286 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3287 | |
| 3288 | /* The following constant maps TK_xx codes into corresponding |
| 3289 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3290 | */ |
| 3291 | const u8 aMoveOp[] = { |
| 3292 | /* TK_GT */ OP_SeekGt, |
| 3293 | /* TK_LE */ OP_SeekLe, |
| 3294 | /* TK_LT */ OP_SeekLt, |
| 3295 | /* TK_GE */ OP_SeekGe |
| 3296 | }; |
| 3297 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3298 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3299 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3300 | |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3301 | testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3302 | pX = pStart->pExpr; |
| 3303 | assert( pX!=0 ); |
| 3304 | assert( pStart->leftCursor==iCur ); |
| 3305 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3306 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
| 3307 | VdbeComment((v, "pk")); |
| 3308 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3309 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3310 | disableTerm(pLevel, pStart); |
| 3311 | }else{ |
| 3312 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 3313 | } |
| 3314 | if( pEnd ){ |
| 3315 | Expr *pX; |
| 3316 | pX = pEnd->pExpr; |
| 3317 | assert( pX!=0 ); |
| 3318 | assert( pEnd->leftCursor==iCur ); |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3319 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3320 | memEndValue = ++pParse->nMem; |
| 3321 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3322 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3323 | testOp = bRev ? OP_Le : OP_Ge; |
| 3324 | }else{ |
| 3325 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3326 | } |
| 3327 | disableTerm(pLevel, pEnd); |
| 3328 | } |
| 3329 | start = sqlite3VdbeCurrentAddr(v); |
| 3330 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3331 | pLevel->p1 = iCur; |
| 3332 | pLevel->p2 = start; |
drh | afc266a | 2010-03-31 17:47:44 +0000 | [diff] [blame] | 3333 | if( pStart==0 && pEnd==0 ){ |
| 3334 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3335 | }else{ |
| 3336 | assert( pLevel->p5==0 ); |
| 3337 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3338 | if( testOp!=OP_Noop ){ |
| 3339 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3340 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3341 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3342 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 3343 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3344 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3345 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3346 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3347 | ** |
| 3348 | ** The WHERE clause may contain zero or more equality |
| 3349 | ** terms ("==" or "IN" operators) that refer to the N |
| 3350 | ** left-most columns of the index. It may also contain |
| 3351 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3352 | ** column that immediately follows the N equalities. Only |
| 3353 | ** the right-most column can be an inequality - the rest must |
| 3354 | ** use the "==" and "IN" operators. For example, if the |
| 3355 | ** index is on (x,y,z), then the following clauses are all |
| 3356 | ** optimized: |
| 3357 | ** |
| 3358 | ** x=5 |
| 3359 | ** x=5 AND y=10 |
| 3360 | ** x=5 AND y<10 |
| 3361 | ** x=5 AND y>5 AND y<10 |
| 3362 | ** x=5 AND y=5 AND z<=10 |
| 3363 | ** |
| 3364 | ** The z<10 term of the following cannot be used, only |
| 3365 | ** the x=5 term: |
| 3366 | ** |
| 3367 | ** x=5 AND z<10 |
| 3368 | ** |
| 3369 | ** N may be zero if there are inequality constraints. |
| 3370 | ** If there are no inequality constraints, then N is at |
| 3371 | ** least one. |
| 3372 | ** |
| 3373 | ** This case is also used when there are no WHERE clause |
| 3374 | ** constraints but an index is selected anyway, in order |
| 3375 | ** to force the output order to conform to an ORDER BY. |
| 3376 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3377 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3378 | 0, |
| 3379 | 0, |
| 3380 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3381 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 3382 | OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3383 | OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ |
| 3384 | OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ |
| 3385 | OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ |
| 3386 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3387 | static const u8 aEndOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3388 | OP_Noop, /* 0: (!end_constraints) */ |
| 3389 | OP_IdxGE, /* 1: (end_constraints && !bRev) */ |
| 3390 | OP_IdxLT /* 2: (end_constraints && bRev) */ |
| 3391 | }; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3392 | int nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 3393 | int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3394 | int regBase; /* Base register holding constraint values */ |
| 3395 | int r1; /* Temp register */ |
| 3396 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3397 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3398 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3399 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3400 | int start_constraints; /* Start of range is constrained */ |
| 3401 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3402 | Index *pIdx; /* The index we will be using */ |
| 3403 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3404 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3405 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3406 | char *zStartAff; /* Affinity for start of range constraint */ |
| 3407 | char *zEndAff; /* Affinity for end of range constraint */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3408 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3409 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3410 | iIdxCur = pLevel->iIdxCur; |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3411 | k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3412 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3413 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3414 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3415 | ** query, then the caller will only allow the loop to run for |
| 3416 | ** a single iteration. This means that the first row returned |
| 3417 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3418 | ** the first one after the nEq equality constraints in the index, |
| 3419 | ** this requires some special handling. |
| 3420 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3421 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 3422 | && (pWInfo->bOBSat!=0) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3423 | && (pIdx->nColumn>nEq) |
| 3424 | ){ |
| 3425 | /* assert( pOrderBy->nExpr==1 ); */ |
| 3426 | /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ |
| 3427 | isMinQuery = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3428 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | /* Find any inequality constraint terms for the start and end |
| 3432 | ** of the range. |
| 3433 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3434 | j = nEq; |
| 3435 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3436 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3437 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3438 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3439 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3440 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3441 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3444 | /* Generate code to evaluate all constraint terms using == or IN |
| 3445 | ** and store the values of those terms in an array of registers |
| 3446 | ** starting at regBase. |
| 3447 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3448 | regBase = codeAllEqualityTerms( |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3449 | pParse, pLevel, pWC, notReady, bRev, nExtraReg, &zStartAff |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3450 | ); |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3451 | zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3452 | addrNxt = pLevel->addrNxt; |
| 3453 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3454 | /* If we are doing a reverse order scan on an ascending index, or |
| 3455 | ** a forward order scan on a descending index, interchange the |
| 3456 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3457 | */ |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3458 | if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3459 | || (bRev && pIdx->nColumn==nEq) |
| 3460 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3461 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 3462 | } |
| 3463 | |
| 3464 | testcase( pRangeStart && pRangeStart->eOperator & WO_LE ); |
| 3465 | testcase( pRangeStart && pRangeStart->eOperator & WO_GE ); |
| 3466 | testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE ); |
| 3467 | testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE ); |
| 3468 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3469 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3470 | start_constraints = pRangeStart || nEq>0; |
| 3471 | |
| 3472 | /* Seek the index cursor to the start of the range. */ |
| 3473 | nConstraint = nEq; |
| 3474 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3475 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3476 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3477 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ |
| 3478 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3479 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3480 | if( zStartAff ){ |
| 3481 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3482 | /* Since the comparison is to be performed with no conversions |
| 3483 | ** applied to the operands, set the affinity to apply to pRight to |
| 3484 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3485 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3486 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3487 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3488 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3489 | } |
| 3490 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3491 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3492 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3493 | }else if( isMinQuery ){ |
| 3494 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3495 | nConstraint++; |
| 3496 | startEq = 0; |
| 3497 | start_constraints = 1; |
| 3498 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3499 | codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3500 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3501 | assert( op!=0 ); |
| 3502 | testcase( op==OP_Rewind ); |
| 3503 | testcase( op==OP_Last ); |
| 3504 | testcase( op==OP_SeekGt ); |
| 3505 | testcase( op==OP_SeekGe ); |
| 3506 | testcase( op==OP_SeekLe ); |
| 3507 | testcase( op==OP_SeekLt ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3508 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3509 | |
| 3510 | /* Load the value for the inequality constraint at the end of the |
| 3511 | ** range (if any). |
| 3512 | */ |
| 3513 | nConstraint = nEq; |
| 3514 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3515 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3516 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3517 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3518 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ |
| 3519 | sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); |
| 3520 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3521 | if( zEndAff ){ |
| 3522 | if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3523 | /* Since the comparison is to be performed with no conversions |
| 3524 | ** applied to the operands, set the affinity to apply to pRight to |
| 3525 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3526 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3527 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3528 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ |
| 3529 | zEndAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3530 | } |
| 3531 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3532 | codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3533 | nConstraint++; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3534 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3535 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3536 | sqlite3DbFree(pParse->db, zStartAff); |
| 3537 | sqlite3DbFree(pParse->db, zEndAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3538 | |
| 3539 | /* Top of the loop body */ |
| 3540 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3541 | |
| 3542 | /* Check if the index cursor is past the end of the range. */ |
| 3543 | op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; |
| 3544 | testcase( op==OP_Noop ); |
| 3545 | testcase( op==OP_IdxGE ); |
| 3546 | testcase( op==OP_IdxLT ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3547 | if( op!=OP_Noop ){ |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3548 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3549 | sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); |
| 3550 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3551 | |
| 3552 | /* If there are inequality constraints, check that the value |
| 3553 | ** of the table column that the inequality contrains is not NULL. |
| 3554 | ** If it is, jump to the next iteration of the loop. |
| 3555 | */ |
| 3556 | r1 = sqlite3GetTempReg(pParse); |
| 3557 | testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ); |
| 3558 | testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3559 | if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3560 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); |
| 3561 | sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); |
| 3562 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3563 | sqlite3ReleaseTempReg(pParse, r1); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3564 | |
| 3565 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3566 | disableTerm(pLevel, pRangeStart); |
| 3567 | disableTerm(pLevel, pRangeEnd); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3568 | if( !omitTable ){ |
| 3569 | iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); |
| 3570 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3571 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3572 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3573 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3574 | |
| 3575 | /* Record the instruction used to terminate the loop. Disable |
| 3576 | ** WHERE clause terms made redundant by the index range scan. |
| 3577 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3578 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3579 | pLevel->op = OP_Noop; |
| 3580 | }else if( bRev ){ |
| 3581 | pLevel->op = OP_Prev; |
| 3582 | }else{ |
| 3583 | pLevel->op = OP_Next; |
| 3584 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3585 | pLevel->p1 = iIdxCur; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3586 | if( (pLoop->wsFlags & (WHERE_COLUMN_EQ | WHERE_COLUMN_RANGE | |
| 3587 | WHERE_COLUMN_NULL | WHERE_COLUMN_IN))==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3588 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3589 | }else{ |
| 3590 | assert( pLevel->p5==0 ); |
| 3591 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3592 | }else |
| 3593 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3594 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3595 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3596 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3597 | ** |
| 3598 | ** Example: |
| 3599 | ** |
| 3600 | ** CREATE TABLE t1(a,b,c,d); |
| 3601 | ** CREATE INDEX i1 ON t1(a); |
| 3602 | ** CREATE INDEX i2 ON t1(b); |
| 3603 | ** CREATE INDEX i3 ON t1(c); |
| 3604 | ** |
| 3605 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3606 | ** |
| 3607 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3608 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3609 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3610 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3611 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3612 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3613 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3614 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3615 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3616 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3617 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3618 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3619 | ** Gosub 2 A |
| 3620 | ** sqlite3WhereEnd() |
| 3621 | ** |
| 3622 | ** Following the above, code to terminate the loop. Label A, the target |
| 3623 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3624 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3625 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3626 | ** Goto B # The loop is finished. |
| 3627 | ** |
| 3628 | ** A: <loop body> # Return data, whatever. |
| 3629 | ** |
| 3630 | ** Return 2 # Jump back to the Gosub |
| 3631 | ** |
| 3632 | ** B: <after the loop> |
| 3633 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3634 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3635 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3636 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3637 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3638 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3639 | |
| 3640 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3641 | int regRowset = 0; /* Register for RowSet object */ |
| 3642 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3643 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3644 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3645 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3646 | int ii; /* Loop counter */ |
| 3647 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3648 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3649 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3650 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3651 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3652 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3653 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3654 | pLevel->op = OP_Return; |
| 3655 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3656 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3657 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3658 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3659 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3660 | */ |
| 3661 | if( pWInfo->nLevel>1 ){ |
| 3662 | int nNotReady; /* The number of notReady tables */ |
| 3663 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3664 | nNotReady = pWInfo->nLevel - iLevel - 1; |
| 3665 | pOrTab = sqlite3StackAllocRaw(pParse->db, |
| 3666 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3667 | if( pOrTab==0 ) return notReady; |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3668 | pOrTab->nAlloc = (i16)(nNotReady + 1); |
| 3669 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3670 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3671 | origSrc = pWInfo->pTabList->a; |
| 3672 | for(k=1; k<=nNotReady; k++){ |
| 3673 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3674 | } |
| 3675 | }else{ |
| 3676 | pOrTab = pWInfo->pTabList; |
| 3677 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3678 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3679 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 3680 | ** equivalent to an empty rowset. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3681 | ** |
| 3682 | ** Also initialize regReturn to contain the address of the instruction |
| 3683 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3684 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3685 | ** over the top of the loop into the body of it. In this case the |
| 3686 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3687 | ** fall through to the next instruction, just as an OP_Next does if |
| 3688 | ** called on an uninitialized cursor. |
| 3689 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3690 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3691 | regRowset = ++pParse->nMem; |
| 3692 | regRowid = ++pParse->nMem; |
| 3693 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3694 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3695 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3696 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3697 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3698 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3699 | ** That way, terms in y that are factored into the disjunction will |
| 3700 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3701 | ** |
| 3702 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3703 | ** the "interesting" terms of z - terms that did not originate in the |
| 3704 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3705 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3706 | ** |
| 3707 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3708 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3709 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3710 | */ |
| 3711 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3712 | int iTerm; |
| 3713 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3714 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3715 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3716 | if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue; |
| 3717 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3718 | pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 3719 | pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr); |
| 3720 | } |
| 3721 | if( pAndExpr ){ |
| 3722 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3723 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3726 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3727 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3728 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3729 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3730 | Expr *pOrExpr = pOrTerm->pExpr; |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3731 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3732 | pAndExpr->pLeft = pOrExpr; |
| 3733 | pOrExpr = pAndExpr; |
| 3734 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3735 | /* Loop through table entries that match term pOrTerm. */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3736 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 3737 | WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY | |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3738 | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur); |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3739 | assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3740 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3741 | WhereLoop *pSubLoop; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3742 | explainOneScan( |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3743 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3744 | ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3745 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3746 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3747 | int r; |
| 3748 | r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3749 | regRowid, 0); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3750 | sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, |
| 3751 | sqlite3VdbeCurrentAddr(v)+2, r, iSet); |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3752 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3753 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 3754 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3755 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 3756 | ** contained one or more AND term from a notReady table. The |
| 3757 | ** terms from the notReady table could not be tested and will |
| 3758 | ** need to be tested later. |
| 3759 | */ |
| 3760 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 3761 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3762 | /* If all of the OR-connected terms are optimized using the same |
| 3763 | ** index, and the index is opened using the same cursor number |
| 3764 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 3765 | ** be possible to use that index as a covering index. |
| 3766 | ** |
| 3767 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 3768 | ** uses an index, and this is either the first OR-connected term |
| 3769 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3770 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 3771 | ** pCov to NULL to indicate that no candidate covering index will |
| 3772 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3773 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3774 | pSubLoop = pSubWInfo->a[0].pWLoop; |
| 3775 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 3776 | && (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 |
| 3777 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3778 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3779 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 3780 | pCov = pSubLoop->u.btree.pIndex; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3781 | }else{ |
| 3782 | pCov = 0; |
| 3783 | } |
| 3784 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3785 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 3786 | sqlite3WhereEnd(pSubWInfo); |
| 3787 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3788 | } |
| 3789 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 3790 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 3791 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3792 | if( pAndExpr ){ |
| 3793 | pAndExpr->pLeft = 0; |
| 3794 | sqlite3ExprDelete(pParse->db, pAndExpr); |
| 3795 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3796 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3797 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 3798 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 3799 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3800 | if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab); |
| 3801 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3802 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3803 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3804 | |
| 3805 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3806 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3807 | ** scan of the entire table. |
| 3808 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3809 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 3810 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 3811 | assert( bRev==0 || bRev==1 ); |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3812 | pLevel->op = aStep[bRev]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3813 | pLevel->p1 = iCur; |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 3814 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3815 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3816 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3817 | newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3818 | |
| 3819 | /* Insert code to test every subexpression that can be completely |
| 3820 | ** computed using the current set of tables. |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3821 | ** |
| 3822 | ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through |
| 3823 | ** the use of indices become tests that are evaluated against each row of |
| 3824 | ** the relevant input tables. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3825 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3826 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3827 | Expr *pE; |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3828 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3829 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3830 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3831 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3832 | testcase( pWInfo->untestedTerms==0 |
| 3833 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 3834 | pWInfo->untestedTerms = 1; |
| 3835 | continue; |
| 3836 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3837 | pE = pTerm->pExpr; |
| 3838 | assert( pE!=0 ); |
| 3839 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 3840 | continue; |
| 3841 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3842 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3843 | pTerm->wtFlags |= TERM_CODED; |
| 3844 | } |
| 3845 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3846 | /* Insert code to test for implied constraints based on transitivity |
| 3847 | ** of the "==" operator. |
| 3848 | ** |
| 3849 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 3850 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 3851 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 3852 | ** the implied "t1.a=123" constraint. |
| 3853 | */ |
| 3854 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 3855 | Expr *pE; |
| 3856 | WhereTerm *pAlt; |
| 3857 | Expr sEq; |
| 3858 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 3859 | if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue; |
| 3860 | if( pTerm->leftCursor!=iCur ) continue; |
| 3861 | pE = pTerm->pExpr; |
| 3862 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 3863 | assert( (pTerm->prereqRight & newNotReady)!=0 ); |
| 3864 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0); |
| 3865 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 3866 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3867 | VdbeNoopComment((v, "begin transitive constraint")); |
| 3868 | sEq = *pAlt->pExpr; |
| 3869 | sEq.pLeft = pE->pLeft; |
| 3870 | sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL); |
| 3871 | } |
| 3872 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3873 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 3874 | ** at least one row of the right table has matched the left table. |
| 3875 | */ |
| 3876 | if( pLevel->iLeftJoin ){ |
| 3877 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 3878 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 3879 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3880 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3881 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | e9cdcea | 2010-07-22 22:40:03 +0000 | [diff] [blame] | 3882 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3883 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 3884 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3885 | if( (pTerm->prereqAll & newNotReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 3886 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3887 | continue; |
| 3888 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3889 | assert( pTerm->pExpr ); |
| 3890 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 3891 | pTerm->wtFlags |= TERM_CODED; |
| 3892 | } |
| 3893 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3894 | sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3895 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 3896 | return newNotReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3899 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3900 | /* |
| 3901 | ** Print a WhereLoop object for debugging purposes |
| 3902 | */ |
| 3903 | static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3904 | int nb = 1+(pTabList->nSrc+7)/8; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3905 | struct SrcList_item *pItem = pTabList->a + p->iTab; |
| 3906 | Table *pTab = pItem->pTab; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 3907 | sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 3908 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
| 3909 | sqlite3DebugPrintf(" %8s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3910 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3911 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 3912 | if( p->u.btree.pIndex ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3913 | const char *zName = p->u.btree.pIndex->zName; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 3914 | if( zName==0 ) zName = "ipk"; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 3915 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 3916 | int i = sqlite3Strlen30(zName) - 1; |
| 3917 | while( zName[i]!='_' ) i--; |
| 3918 | zName += i; |
| 3919 | } |
| 3920 | sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3921 | }else{ |
| 3922 | sqlite3DebugPrintf("%16s",""); |
| 3923 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3924 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3925 | char *z; |
| 3926 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3927 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 3928 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3929 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 3930 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3931 | } |
| 3932 | sqlite3DebugPrintf(" %-15s", z); |
| 3933 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3934 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3935 | sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nLTerm); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 3936 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 3937 | } |
| 3938 | #endif |
| 3939 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 3940 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3941 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 3942 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3943 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3944 | static void whereLoopInit(WhereLoop *p){ |
| 3945 | p->aLTerm = p->aLTermSpace; |
| 3946 | p->nLTerm = 0; |
| 3947 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 3948 | p->wsFlags = 0; |
| 3949 | } |
| 3950 | |
| 3951 | /* |
| 3952 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 3953 | */ |
| 3954 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 3955 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){ |
| 3956 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 3957 | sqlite3_free(p->u.vtab.idxStr); |
| 3958 | p->u.vtab.needFree = 0; |
| 3959 | p->u.vtab.idxStr = 0; |
| 3960 | }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
| 3961 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 3962 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 3963 | p->u.btree.pIndex = 0; |
| 3964 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 3965 | } |
| 3966 | } |
| 3967 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3968 | /* |
| 3969 | ** Deallocate internal memory used by a WhereLoop object |
| 3970 | */ |
| 3971 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 3972 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3973 | whereLoopClearUnion(db, p); |
| 3974 | whereLoopInit(p); |
| 3975 | } |
| 3976 | |
| 3977 | /* |
| 3978 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 3979 | */ |
| 3980 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 3981 | WhereTerm **paNew; |
| 3982 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 3983 | n = (n+7)&~7; |
| 3984 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 3985 | if( paNew==0 ) return SQLITE_NOMEM; |
| 3986 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 3987 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 3988 | p->aLTerm = paNew; |
| 3989 | p->nLSlot = n; |
| 3990 | return SQLITE_OK; |
| 3991 | } |
| 3992 | |
| 3993 | /* |
| 3994 | ** Transfer content from the second pLoop into the first. |
| 3995 | */ |
| 3996 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 3997 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM; |
| 3998 | whereLoopClearUnion(db, pTo); |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 3999 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 4000 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4001 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 4002 | pFrom->u.vtab.needFree = 0; |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4003 | }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4004 | pFrom->u.btree.pIndex = 0; |
| 4005 | } |
| 4006 | return SQLITE_OK; |
| 4007 | } |
| 4008 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4009 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4010 | ** Delete a WhereLoop object |
| 4011 | */ |
| 4012 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4013 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4014 | sqlite3DbFree(db, p); |
| 4015 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 4016 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4017 | /* |
| 4018 | ** Free a WhereInfo structure |
| 4019 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 4020 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 4021 | if( ALWAYS(pWInfo) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4022 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4023 | while( pWInfo->pLoops ){ |
| 4024 | WhereLoop *p = pWInfo->pLoops; |
| 4025 | pWInfo->pLoops = p->pNextLoop; |
| 4026 | whereLoopDelete(db, p); |
| 4027 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4028 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4032 | /* |
| 4033 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 4034 | ** |
| 4035 | ** An existing WhereLoop entry might be overwritten if the new template |
| 4036 | ** is better and has fewer dependencies. Or the template will be ignored |
| 4037 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 4038 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4039 | ** added based on the template. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4040 | ** |
| 4041 | ** If pBuilder->pBest is not NULL then we only care about the very |
| 4042 | ** best template and that template should be stored in pBuilder->pBest. |
| 4043 | ** If pBuilder->pBest is NULL then a list of the best templates are stored |
| 4044 | ** in pBuilder->pWInfo->pLoops. |
| 4045 | ** |
| 4046 | ** When accumulating multiple loops (when pBuilder->pBest is NULL) we |
| 4047 | ** still might overwrite similar loops with the new template if the |
| 4048 | ** template is better. Loops may be overwritten if the following |
| 4049 | ** conditions are met: |
| 4050 | ** |
| 4051 | ** (1) They have the same iTab. |
| 4052 | ** (2) They have the same iSortIdx. |
| 4053 | ** (3) The template has same or fewer dependencies than the current loop |
| 4054 | ** (4) The template has the same or lower cost than the current loop |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4055 | ** (5) The template uses more terms of the same index but has no additional |
| 4056 | ** dependencies |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4057 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4058 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4059 | WhereLoop **ppPrev, *p, *pNext = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4060 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4061 | sqlite3 *db = pWInfo->pParse->db; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4062 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4063 | /* If pBuilder->pBest is defined, then only keep track of the single |
| 4064 | ** best WhereLoop. pBuilder->pBest->maskSelf==0 indicates that no |
| 4065 | ** prior WhereLoops have been evaluated and that the current pTemplate |
| 4066 | ** is therefore the first and hence the best and should be retained. |
| 4067 | */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4068 | if( (p = pBuilder->pBest)!=0 ){ |
| 4069 | if( p->maskSelf!=0 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4070 | WhereCost rCost = whereCostAdd(p->rRun,p->rSetup); |
| 4071 | WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4072 | if( rCost < rTemplate ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4073 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4074 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4075 | if( rCost == rTemplate && p->prereq <= pTemplate->prereq ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4076 | goto whereLoopInsert_noop; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4077 | } |
| 4078 | } |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4079 | #if WHERETRACE_ENABLED |
| 4080 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 4081 | sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4082 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4083 | } |
| 4084 | #endif |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 4085 | whereLoopXfer(db, p, pTemplate); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4086 | return SQLITE_OK; |
| 4087 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4088 | |
| 4089 | /* Search for an existing WhereLoop to overwrite, or which takes |
| 4090 | ** priority over pTemplate. |
| 4091 | */ |
| 4092 | for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4093 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ) continue; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4094 | if( (p->prereq & pTemplate->prereq)==p->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4095 | && p->rSetup<=pTemplate->rSetup |
| 4096 | && p->rRun<=pTemplate->rRun |
| 4097 | ){ |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4098 | /* p is equal or better than pTemplate */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4099 | if( p->nLTerm<pTemplate->nLTerm |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4100 | && (p->wsFlags & WHERE_INDEXED)!=0 |
| 4101 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4102 | && p->u.btree.pIndex==pTemplate->u.btree.pIndex |
| 4103 | && p->prereq==pTemplate->prereq |
| 4104 | ){ |
| 4105 | /* Overwrite an existing WhereLoop with an similar one that uses |
| 4106 | ** more terms of the index */ |
| 4107 | pNext = p->pNextLoop; |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4108 | break; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4109 | }else if( p->nOut>pTemplate->nOut |
| 4110 | && p->rSetup==pTemplate->rSetup |
| 4111 | && p->rRun==pTemplate->rRun |
| 4112 | ){ |
| 4113 | /* Overwrite an existing WhereLoop with the same cost but more |
| 4114 | ** outputs */ |
| 4115 | pNext = p->pNextLoop; |
| 4116 | break; |
drh | cd0f407 | 2013-06-05 12:47:59 +0000 | [diff] [blame] | 4117 | }else{ |
| 4118 | /* pTemplate is not helpful. |
| 4119 | ** Return without changing or adding anything */ |
| 4120 | goto whereLoopInsert_noop; |
| 4121 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4122 | } |
| 4123 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4124 | && p->rSetup>=pTemplate->rSetup |
| 4125 | && p->rRun>=pTemplate->rRun |
| 4126 | ){ |
| 4127 | /* Overwrite an existing WhereLoop with a better one */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4128 | pNext = p->pNextLoop; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4129 | break; |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | /* If we reach this point it means that either p[] should be overwritten |
| 4134 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4135 | ** WhereLoop and insert it. |
| 4136 | */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4137 | #if WHERETRACE_ENABLED |
| 4138 | if( sqlite3WhereTrace & 0x8 ){ |
| 4139 | if( p!=0 ){ |
| 4140 | sqlite3DebugPrintf("ins-del: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4141 | whereLoopPrint(p, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4142 | } |
| 4143 | sqlite3DebugPrintf("ins-new: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4144 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4145 | } |
| 4146 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4147 | if( p==0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4148 | p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4149 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4150 | whereLoopInit(p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4151 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4152 | whereLoopXfer(db, p, pTemplate); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4153 | p->pNextLoop = pNext; |
| 4154 | *ppPrev = p; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4155 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4156 | Index *pIndex = p->u.btree.pIndex; |
| 4157 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4158 | p->u.btree.pIndex = 0; |
| 4159 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4160 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4161 | return SQLITE_OK; |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4162 | |
| 4163 | /* Jump here if the insert is a no-op */ |
| 4164 | whereLoopInsert_noop: |
| 4165 | #if WHERETRACE_ENABLED |
| 4166 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 4167 | sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: "); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4168 | whereLoopPrint(pTemplate, pWInfo->pTabList); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4169 | } |
| 4170 | #endif |
| 4171 | return SQLITE_OK; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | /* |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4175 | ** 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] | 4176 | ** Try to match one more. |
| 4177 | ** |
| 4178 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4179 | ** INTEGER PRIMARY KEY. |
| 4180 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4181 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4182 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4183 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4184 | Index *pProbe, /* An index on pSrc */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4185 | WhereCost nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4186 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4187 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4188 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4189 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4190 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4191 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4192 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4193 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4194 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4195 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 4196 | int saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 4197 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 4198 | WhereCost saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4199 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4200 | int rc = SQLITE_OK; /* Return code */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4201 | WhereCost nRowEst; /* Estimated index selectivity */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4202 | WhereCost rLogSize; /* Logarithm of table size */ |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4203 | WhereTerm *pTop, *pBtm; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4204 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4205 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4206 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4207 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4208 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4209 | assert( pNew->u.btree.nEq<=pProbe->nColumn ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4210 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4211 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4212 | opMask = WO_LT|WO_LE; |
| 4213 | }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){ |
| 4214 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4215 | }else{ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4216 | 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] | 4217 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4218 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4219 | |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4220 | if( pNew->u.btree.nEq < pProbe->nColumn ){ |
| 4221 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4222 | nRowEst = whereCostFromInt(pProbe->aiRowEst[pNew->u.btree.nEq+1]); |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4223 | }else{ |
| 4224 | iCol = -1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4225 | nRowEst = 0; |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4226 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4227 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4228 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4229 | saved_nEq = pNew->u.btree.nEq; |
| 4230 | saved_nLTerm = pNew->nLTerm; |
| 4231 | saved_wsFlags = pNew->wsFlags; |
| 4232 | saved_prereq = pNew->prereq; |
| 4233 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4234 | pNew->rSetup = 0; |
| 4235 | rLogSize = estLog(whereCostFromInt(pProbe->aiRowEst[0])); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4236 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4237 | int nIn = 0; |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4238 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4239 | pNew->wsFlags = saved_wsFlags; |
| 4240 | pNew->u.btree.nEq = saved_nEq; |
| 4241 | pNew->nLTerm = saved_nLTerm; |
| 4242 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4243 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4244 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4245 | pNew->rRun = rLogSize; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4246 | if( pTerm->eOperator & WO_IN ){ |
| 4247 | Expr *pExpr = pTerm->pExpr; |
| 4248 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4249 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 4250 | /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4251 | nIn = 46; assert( 46==whereCostFromInt(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4252 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4253 | /* "x IN (value, value, ...)" */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4254 | nIn = whereCostFromInt(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4255 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4256 | pNew->rRun += nIn; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4257 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4258 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4259 | }else if( pTerm->eOperator & (WO_EQ) ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4260 | assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4261 | || nInMul==0 ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4262 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4263 | if( iCol<0 |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4264 | || (pProbe->onError!=OE_None && nInMul==0 |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4265 | && pNew->u.btree.nEq==pProbe->nColumn-1) |
| 4266 | ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4267 | testcase( pNew->wsFlags & WHERE_COLUMN_IN ); |
| 4268 | pNew->wsFlags |= WHERE_ONEROW; |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4269 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4270 | pNew->u.btree.nEq++; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4271 | pNew->nOut = nRowEst + nInMul; |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4272 | }else if( pTerm->eOperator & (WO_ISNULL) ){ |
| 4273 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 4274 | pNew->u.btree.nEq++; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4275 | nIn = 10; assert( 10==whereCostFromInt(2) ); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4276 | pNew->nOut = nRowEst + nInMul + nIn; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4277 | }else if( pTerm->eOperator & (WO_GT|WO_GE) ){ |
| 4278 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4279 | pBtm = pTerm; |
| 4280 | pTop = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4281 | }else if( pTerm->eOperator & (WO_LT|WO_LE) ){ |
| 4282 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4283 | pTop = pTerm; |
| 4284 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4285 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4286 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4287 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4288 | /* Adjust nOut and rRun for STAT3 range values */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4289 | WhereCost rDiv; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4290 | whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq, |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4291 | pBtm, pTop, &rDiv); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4292 | pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4293 | } |
| 4294 | #ifdef SQLITE_ENABLE_STAT3 |
| 4295 | if( pNew->u.btree.nEq==1 && pProbe->nSample ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4296 | tRowcnt nOut = 0; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4297 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4298 | rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4299 | }else if( (pTerm->eOperator & WO_IN) |
| 4300 | && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4301 | rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4302 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4303 | pNew->nOut = whereCostFromInt(nOut); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4304 | } |
| 4305 | #endif |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4306 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4307 | /* Each row involves a step of the index, then a binary search of |
| 4308 | ** the main table */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4309 | pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4310 | } |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4311 | /* Step cost for each output row */ |
| 4312 | pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4313 | /* TBD: Adjust nOut for additional constraints */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4314 | rc = whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4315 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4316 | && pNew->u.btree.nEq<=pProbe->nColumn |
| 4317 | && pProbe->zName!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4318 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4319 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4320 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4321 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4322 | pNew->prereq = saved_prereq; |
| 4323 | pNew->u.btree.nEq = saved_nEq; |
| 4324 | pNew->wsFlags = saved_wsFlags; |
| 4325 | pNew->nOut = saved_nOut; |
| 4326 | pNew->nLTerm = saved_nLTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4327 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4331 | ** Return True if it is possible that pIndex might be useful in |
| 4332 | ** implementing the ORDER BY clause in pBuilder. |
| 4333 | ** |
| 4334 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 4335 | ** if there is no way for pIndex to be useful in implementing that |
| 4336 | ** ORDER BY clause. |
| 4337 | */ |
| 4338 | static int indexMightHelpWithOrderBy( |
| 4339 | WhereLoopBuilder *pBuilder, |
| 4340 | Index *pIndex, |
| 4341 | int iCursor |
| 4342 | ){ |
| 4343 | ExprList *pOB; |
| 4344 | int iCol; |
| 4345 | int ii; |
| 4346 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4347 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4348 | iCol = pIndex->aiColumn[0]; |
| 4349 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 4350 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4351 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 4352 | if( pExpr->iTable==iCursor ){ |
| 4353 | if( pExpr->iColumn==iCol ) return 1; |
| 4354 | return 0; |
| 4355 | } |
| 4356 | } |
| 4357 | return 0; |
| 4358 | } |
| 4359 | |
| 4360 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4361 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 4362 | ** the table is used by an index. Only the first 63 columns are considered. |
| 4363 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame^] | 4364 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 4365 | Bitmask m = 0; |
| 4366 | int j; |
| 4367 | for(j=pIdx->nColumn-1; j>=0; j--){ |
| 4368 | int x = pIdx->aiColumn[j]; |
| 4369 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 4370 | } |
| 4371 | return m; |
| 4372 | } |
| 4373 | |
| 4374 | |
| 4375 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4376 | ** Add all WhereLoop objects a single table of the join were the table |
| 4377 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 4378 | ** a b-tree table, not a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4379 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4380 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4381 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4382 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4383 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4384 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4385 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4386 | Index sPk; /* A fake index object for the primary key */ |
| 4387 | tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ |
| 4388 | int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4389 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4390 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4391 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4392 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4393 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4394 | int b; /* A boolean value */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4395 | WhereCost rSize; /* number of rows in the table */ |
| 4396 | WhereCost rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4397 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4398 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4399 | pWInfo = pBuilder->pWInfo; |
| 4400 | pTabList = pWInfo->pTabList; |
| 4401 | pSrc = pTabList->a + pNew->iTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4402 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4403 | |
| 4404 | if( pSrc->pIndex ){ |
| 4405 | /* An INDEXED BY clause specifies a particular index to use */ |
| 4406 | pProbe = pSrc->pIndex; |
| 4407 | }else{ |
| 4408 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 4409 | ** variable sPk to represent the rowid primary key index. Make this |
| 4410 | ** fake index the first in a chain of Index objects with all of the real |
| 4411 | ** indices to follow */ |
| 4412 | Index *pFirst; /* First of real indices on the table */ |
| 4413 | memset(&sPk, 0, sizeof(Index)); |
| 4414 | sPk.nColumn = 1; |
| 4415 | sPk.aiColumn = &aiColumnPk; |
| 4416 | sPk.aiRowEst = aiRowEstPk; |
| 4417 | sPk.onError = OE_Replace; |
| 4418 | sPk.pTable = pSrc->pTab; |
| 4419 | aiRowEstPk[0] = pSrc->pTab->nRowEst; |
| 4420 | aiRowEstPk[1] = 1; |
| 4421 | pFirst = pSrc->pTab->pIndex; |
| 4422 | if( pSrc->notIndexed==0 ){ |
| 4423 | /* The real indices of the table are only considered if the |
| 4424 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 4425 | sPk.pNext = pFirst; |
| 4426 | } |
| 4427 | pProbe = &sPk; |
| 4428 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4429 | rSize = whereCostFromInt(pSrc->pTab->nRowEst); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4430 | rLogSize = estLog(rSize); |
| 4431 | |
| 4432 | /* Automatic indexes */ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4433 | if( !pBuilder->pBest |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4434 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4435 | && !pSrc->viaCoroutine |
| 4436 | && !pSrc->notIndexed |
| 4437 | && !pSrc->isCorrelated |
| 4438 | ){ |
| 4439 | /* Generate auto-index WhereLoops */ |
| 4440 | WhereClause *pWC = pBuilder->pWC; |
| 4441 | WhereTerm *pTerm; |
| 4442 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 4443 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 4444 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4445 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 4446 | pNew->u.btree.nEq = 1; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4447 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4448 | pNew->nLTerm = 1; |
| 4449 | pNew->aLTerm[0] = pTerm; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4450 | assert( 43==whereCostFromInt(20) ); |
| 4451 | pNew->rSetup = 43 + rLogSize + rSize; |
| 4452 | pNew->nOut = 33; assert( 33==whereCostFromInt(10) ); |
| 4453 | pNew->rRun = whereCostAdd(rLogSize,pNew->nOut); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4454 | pNew->wsFlags = WHERE_TEMP_INDEX; |
| 4455 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4456 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4460 | |
| 4461 | /* Loop over all indices |
| 4462 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4463 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4464 | pNew->u.btree.nEq = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4465 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4466 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4467 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4468 | pNew->prereq = mExtra; |
| 4469 | pNew->u.btree.pIndex = pProbe; |
| 4470 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4471 | if( pProbe->tnum<=0 ){ |
| 4472 | /* Integer primary key index */ |
| 4473 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4474 | |
| 4475 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 4476 | pNew->iSortIdx = b ? iSortIdx : 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4477 | pNew->nOut = rSize; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame^] | 4478 | pNew->rRun = whereCostAdd(rSize,rLogSize) + 16 - b; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4479 | rc = whereLoopInsert(pBuilder, pNew); |
| 4480 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4481 | }else{ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame^] | 4482 | Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 4483 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4484 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4485 | /* Full scan via index */ |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4486 | if( (m==0 || b) |
| 4487 | && pProbe->bUnordered==0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4488 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4489 | && sqlite3GlobalConfig.bUseCis |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4490 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 4491 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4492 | pNew->iSortIdx = b ? iSortIdx : 0; |
| 4493 | pNew->nOut = rSize; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4494 | pNew->rRun = whereCostAdd(rSize,rLogSize); |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame^] | 4495 | pNew->rRun += ((m!=0) ? rLogSize : 10) - b; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4496 | rc = whereLoopInsert(pBuilder, pNew); |
| 4497 | if( rc ) break; |
| 4498 | } |
| 4499 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4500 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4501 | |
| 4502 | /* If there was an INDEXED BY clause, then only that one index is |
| 4503 | ** considered. */ |
| 4504 | if( pSrc->pIndex ) break; |
| 4505 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4506 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4509 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4510 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4511 | ** Add all WhereLoop objects for a table of the join identified by |
| 4512 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4513 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4514 | static int whereLoopAddVirtual( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4515 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4516 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4517 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4518 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4519 | Parse *pParse; /* The parsing context */ |
| 4520 | WhereClause *pWC; /* The WHERE clause */ |
| 4521 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 4522 | Table *pTab; |
| 4523 | sqlite3 *db; |
| 4524 | sqlite3_index_info *pIdxInfo; |
| 4525 | struct sqlite3_index_constraint *pIdxCons; |
| 4526 | struct sqlite3_index_constraint_usage *pUsage; |
| 4527 | WhereTerm *pTerm; |
| 4528 | int i, j; |
| 4529 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4530 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4531 | int seenIn = 0; /* True if an IN operator is seen */ |
| 4532 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 4533 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 4534 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4535 | int rc = SQLITE_OK; |
| 4536 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4537 | pWInfo = pBuilder->pWInfo; |
| 4538 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4539 | db = pParse->db; |
| 4540 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4541 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4542 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4543 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4544 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4545 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4546 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4547 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4548 | pNew->rSetup = 0; |
| 4549 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4550 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4551 | pNew->u.vtab.needFree = 0; |
| 4552 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4553 | nConstraint = pIdxInfo->nConstraint; |
| 4554 | if( whereLoopResize(db, pNew, nConstraint) ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4555 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 4556 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4557 | if( !seenIn && (iPhase&1)!=0 ){ |
| 4558 | iPhase++; |
| 4559 | if( iPhase>3 ) break; |
| 4560 | } |
| 4561 | if( !seenVar && iPhase>1 ) break; |
| 4562 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4563 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 4564 | j = pIdxCons->iTermOffset; |
| 4565 | pTerm = &pWC->a[j]; |
| 4566 | switch( iPhase ){ |
| 4567 | case 0: /* Constants without IN operator */ |
| 4568 | pIdxCons->usable = 0; |
| 4569 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4570 | seenIn = 1; |
| 4571 | }else if( pTerm->prereqRight!=0 ){ |
| 4572 | seenVar = 1; |
| 4573 | }else{ |
| 4574 | pIdxCons->usable = 1; |
| 4575 | } |
| 4576 | break; |
| 4577 | case 1: /* Constants with IN operators */ |
| 4578 | assert( seenIn ); |
| 4579 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 4580 | break; |
| 4581 | case 2: /* Variables without IN */ |
| 4582 | assert( seenVar ); |
| 4583 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 4584 | break; |
| 4585 | default: /* Variables with IN */ |
| 4586 | assert( seenVar && seenIn ); |
| 4587 | pIdxCons->usable = 1; |
| 4588 | break; |
| 4589 | } |
| 4590 | } |
| 4591 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 4592 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4593 | pIdxInfo->idxStr = 0; |
| 4594 | pIdxInfo->idxNum = 0; |
| 4595 | pIdxInfo->needToFreeIdxStr = 0; |
| 4596 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4597 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4598 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 4599 | if( rc ) goto whereLoopAddVtab_exit; |
| 4600 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 4601 | pNew->prereq = 0; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 4602 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4603 | assert( pNew->nLSlot>=nConstraint ); |
| 4604 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4605 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4606 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4607 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 4608 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4609 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4610 | || j<0 |
| 4611 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4612 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4613 | ){ |
| 4614 | rc = SQLITE_ERROR; |
| 4615 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 4616 | goto whereLoopAddVtab_exit; |
| 4617 | } |
| 4618 | pTerm = &pWC->a[j]; |
| 4619 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4620 | assert( iTerm<pNew->nLSlot ); |
| 4621 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4622 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 4623 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4624 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 4625 | if( pUsage[i].omit==0 ){ |
| 4626 | /* Do not attempt to use an IN constraint if the virtual table |
| 4627 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 4628 | ** If we do attempt to use such a constraint, some rows might be |
| 4629 | ** repeated in the output. */ |
| 4630 | break; |
| 4631 | } |
| 4632 | /* A virtual table that is constrained by an IN clause may not |
| 4633 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 4634 | ** is not necessarily related to the order of output terms and |
| 4635 | ** (2) Multiple outputs from a single IN value will not merge |
| 4636 | ** together. */ |
| 4637 | pIdxInfo->orderByConsumed = 0; |
| 4638 | } |
| 4639 | } |
| 4640 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4641 | if( i>=nConstraint ){ |
| 4642 | pNew->nLTerm = mxTerm+1; |
| 4643 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4644 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 4645 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 4646 | pIdxInfo->needToFreeIdxStr = 0; |
| 4647 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 3b1d808 | 2013-06-03 16:56:37 +0000 | [diff] [blame] | 4648 | pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0) |
| 4649 | && pIdxInfo->orderByConsumed); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4650 | pNew->rSetup = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4651 | pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4652 | pNew->nOut = 46; assert( 46 == whereCostFromInt(25) ); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4653 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4654 | if( pNew->u.vtab.needFree ){ |
| 4655 | sqlite3_free(pNew->u.vtab.idxStr); |
| 4656 | pNew->u.vtab.needFree = 0; |
| 4657 | } |
| 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | whereLoopAddVtab_exit: |
| 4662 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 4663 | sqlite3DbFree(db, pIdxInfo); |
| 4664 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4665 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4666 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4667 | |
| 4668 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4669 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 4670 | ** btrees or virtual tables. |
| 4671 | */ |
| 4672 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4673 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4674 | WhereClause *pWC; |
| 4675 | WhereLoop *pNew; |
| 4676 | WhereTerm *pTerm, *pWCEnd; |
| 4677 | int rc = SQLITE_OK; |
| 4678 | int iCur; |
| 4679 | WhereClause tempWC; |
| 4680 | WhereLoopBuilder sSubBuild; |
| 4681 | WhereLoop sBest; |
| 4682 | struct SrcList_item *pItem; |
| 4683 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4684 | pWC = pBuilder->pWC; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4685 | if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4686 | pWCEnd = pWC->a + pWC->nTerm; |
| 4687 | pNew = pBuilder->pNew; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4688 | |
| 4689 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 4690 | if( (pTerm->eOperator & WO_OR)!=0 |
| 4691 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 4692 | ){ |
| 4693 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 4694 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 4695 | WhereTerm *pOrTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4696 | WhereCost rTotal = 0; |
| 4697 | WhereCost nRow = 0; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4698 | Bitmask prereq = mExtra; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4699 | |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4700 | whereLoopInit(&sBest); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4701 | pItem = pWInfo->pTabList->a + pNew->iTab; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4702 | iCur = pItem->iCursor; |
| 4703 | sSubBuild = *pBuilder; |
| 4704 | sSubBuild.pOrderBy = 0; |
| 4705 | sSubBuild.pBest = &sBest; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4706 | |
| 4707 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4708 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4709 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 4710 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4711 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4712 | tempWC.pOuter = pWC; |
| 4713 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 4714 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4715 | tempWC.a = pOrTerm; |
| 4716 | sSubBuild.pWC = &tempWC; |
| 4717 | }else{ |
| 4718 | continue; |
| 4719 | } |
| 4720 | sBest.maskSelf = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4721 | sBest.rSetup = 0; |
| 4722 | sBest.rRun = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4723 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4724 | if( IsVirtual(pItem->pTab) ){ |
| 4725 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 4726 | }else |
| 4727 | #endif |
| 4728 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4729 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 4730 | } |
| 4731 | if( sBest.maskSelf==0 ) break; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4732 | assert( sBest.rSetup==0 ); |
| 4733 | rTotal = whereCostAdd(rTotal, sBest.rRun); |
| 4734 | nRow = whereCostAdd(nRow, sBest.nOut); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4735 | prereq |= sBest.prereq; |
| 4736 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4737 | assert( pNew->nLSlot>=1 ); |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame^] | 4738 | if( sBest.maskSelf ){ |
| 4739 | pNew->nLTerm = 1; |
| 4740 | pNew->aLTerm[0] = pTerm; |
| 4741 | pNew->wsFlags = WHERE_MULTI_OR; |
| 4742 | pNew->rSetup = 0; |
| 4743 | pNew->rRun = rTotal; |
| 4744 | pNew->nOut = nRow; |
| 4745 | pNew->prereq = prereq; |
| 4746 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 4747 | rc = whereLoopInsert(pBuilder, pNew); |
| 4748 | } |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4749 | whereLoopClear(pWInfo->pParse->db, &sBest); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4750 | } |
| 4751 | } |
| 4752 | return rc; |
| 4753 | } |
| 4754 | |
| 4755 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4756 | ** Add all WhereLoop objects for all tables |
| 4757 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4758 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4759 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4760 | Bitmask mExtra = 0; |
| 4761 | Bitmask mPrior = 0; |
| 4762 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4763 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4764 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4765 | sqlite3 *db = pWInfo->pParse->db; |
| 4766 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4767 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4768 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4769 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4770 | |
| 4771 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4772 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4773 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4774 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4775 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4776 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4777 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4778 | mExtra = mPrior; |
| 4779 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 4780 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4781 | if( IsVirtual(pItem->pTab) ){ |
| 4782 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
| 4783 | }else{ |
| 4784 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 4785 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4786 | if( rc==SQLITE_OK ){ |
| 4787 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 4788 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 4789 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4790 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4791 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4792 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4793 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4796 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4797 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4798 | ** parameters) to see if it outputs rows in the requested ORDER BY |
| 4799 | ** (or GROUP BY) without requiring a separate source operation. Return: |
| 4800 | ** |
| 4801 | ** 0: ORDER BY is not satisfied. Sorting required |
| 4802 | ** 1: ORDER BY is satisfied. Omit sorting |
| 4803 | ** -1: Unknown at this time |
| 4804 | ** |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4805 | */ |
| 4806 | static int wherePathSatisfiesOrderBy( |
| 4807 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4808 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4809 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4810 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 4811 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
| 4812 | u8 isLastLoop, /* True if pLast is the inner-most loop */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4813 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4814 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4815 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4816 | u8 revSet; /* True if rev is known */ |
| 4817 | u8 rev; /* Composite sort order */ |
| 4818 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4819 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 4820 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 4821 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4822 | u16 nColumn; /* Number of columns in pIndex */ |
| 4823 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 4824 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 4825 | int i, j; /* Loop counters */ |
| 4826 | int iCur; /* Cursor number for current WhereLoop */ |
| 4827 | int iColumn; /* A column number within table iCur */ |
| 4828 | WhereLoop *pLoop; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4829 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 4830 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 4831 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 4832 | Index *pIndex; /* The index associated with pLoop */ |
| 4833 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 4834 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 4835 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4836 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4837 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4838 | |
| 4839 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4840 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 4841 | ** 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] | 4842 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 4843 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4844 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 4845 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4846 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4847 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 4848 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 4849 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 4850 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 4851 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 4852 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 4853 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 4854 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 4855 | ** |
| 4856 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 4857 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 4858 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4859 | */ |
| 4860 | |
| 4861 | assert( pOrderBy!=0 ); |
| 4862 | |
| 4863 | /* Sortability of virtual tables is determined by the xBestIndex method |
| 4864 | ** of the virtual table itself */ |
| 4865 | if( pLast->wsFlags & WHERE_VIRTUALTABLE ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4866 | testcase( nLoop>0 ); /* True when outer loops are one-row and match |
| 4867 | ** no ORDER BY terms */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4868 | return pLast->u.vtab.isOrdered; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 4869 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4870 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4871 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4872 | nOrderBy = pOrderBy->nExpr; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4873 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 4874 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4875 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4876 | orderDistinctMask = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4877 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4878 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4879 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4880 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4881 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 4882 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 4883 | pIndex = 0; |
| 4884 | nColumn = 0; |
| 4885 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4886 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4887 | }else{ |
| 4888 | nColumn = pIndex->nColumn; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4889 | isOrderDistinct = pIndex->onError!=OE_None; |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 4890 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4891 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4892 | /* For every term of the index that is constrained by == or IS NULL, |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4893 | ** mark off corresponding ORDER BY terms wherever they occur |
| 4894 | ** in the ORDER BY clause. |
| 4895 | */ |
| 4896 | for(i=0; i<pLoop->u.btree.nEq; i++){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4897 | pTerm = pLoop->aLTerm[i]; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4898 | if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))==0 ) continue; |
| 4899 | iColumn = pTerm->u.leftColumn; |
| 4900 | for(j=0; j<nOrderBy; j++){ |
| 4901 | if( MASKBIT(j) & obSat ) continue; |
| 4902 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[j].pExpr); |
| 4903 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 4904 | if( pOBExpr->iTable!=iCur ) continue; |
| 4905 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 4906 | if( iColumn>=0 ){ |
| 4907 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[j].pExpr); |
| 4908 | if( !pColl ) pColl = db->pDfltColl; |
| 4909 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[i])!=0 ) continue; |
| 4910 | } |
| 4911 | obSat |= MASKBIT(j); |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4912 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4913 | if( obSat==obDone ) return 1; |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 4914 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4915 | |
| 4916 | /* Loop through all columns of the index and deal with the ones |
| 4917 | ** that are not constrained by == or IN. |
| 4918 | */ |
| 4919 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4920 | distinctColumns = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4921 | for(j=0; j<=nColumn; j++){ |
| 4922 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 4923 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4924 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4925 | if( j<pLoop->u.btree.nEq |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4926 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4927 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4928 | if( i & WO_ISNULL ) isOrderDistinct = 0; |
| 4929 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4932 | /* Get the column number in the table (iColumn) and sort order |
| 4933 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4934 | */ |
| 4935 | if( j<nColumn ){ |
| 4936 | /* Normal index columns */ |
| 4937 | iColumn = pIndex->aiColumn[j]; |
| 4938 | revIdx = pIndex->aSortOrder[j]; |
| 4939 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4940 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4941 | /* The ROWID column at the end */ |
| 4942 | iColumn = -1; |
| 4943 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 4944 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4945 | |
| 4946 | /* An unconstrained column that might be NULL means that this |
| 4947 | ** WhereLoop is not well-ordered |
| 4948 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4949 | if( isOrderDistinct |
| 4950 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4951 | && j>=pLoop->u.btree.nEq |
| 4952 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 4953 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4954 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
| 4957 | /* Find the ORDER BY term that corresponds to the j-th column |
| 4958 | ** of the index and and mark that ORDER BY term off |
| 4959 | */ |
| 4960 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4961 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4962 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 4963 | if( MASKBIT(i) & obSat ) continue; |
| 4964 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 4965 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4966 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4967 | if( pOBExpr->iTable!=iCur ) continue; |
| 4968 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 4969 | if( iColumn>=0 ){ |
| 4970 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 4971 | if( !pColl ) pColl = db->pDfltColl; |
| 4972 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 4973 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4974 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4975 | break; |
| 4976 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4977 | if( isMatch ){ |
| 4978 | if( iColumn<0 ) distinctColumns = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4979 | obSat |= MASKBIT(i); |
| 4980 | if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4981 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 4982 | ** Sort order is irrelevant for a GROUP BY clause. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4983 | if( revSet ){ |
| 4984 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0; |
| 4985 | }else{ |
| 4986 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 4987 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 4988 | revSet = 1; |
| 4989 | } |
| 4990 | } |
| 4991 | }else{ |
| 4992 | /* No match found */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4993 | if( j==0 || j<nColumn ) isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4994 | break; |
| 4995 | } |
| 4996 | } /* end Loop over all index columns */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 4997 | if( distinctColumns ) isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 4998 | } /* end-if not one-row */ |
| 4999 | |
| 5000 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5001 | if( isOrderDistinct ){ |
| 5002 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5003 | for(i=0; i<nOrderBy; i++){ |
| 5004 | Expr *p; |
| 5005 | if( MASKBIT(i) & obSat ) continue; |
| 5006 | p = pOrderBy->a[i].pExpr; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5007 | if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5008 | obSat |= MASKBIT(i); |
| 5009 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5010 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5011 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5012 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5013 | if( obSat==obDone ) return 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5014 | if( !isOrderDistinct ) return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5015 | if( isLastLoop ) return 1; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5016 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5019 | #ifdef WHERETRACE_ENABLED |
| 5020 | /* For debugging use only: */ |
| 5021 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5022 | static char zName[65]; |
| 5023 | int i; |
| 5024 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5025 | if( pLast ) zName[i++] = pLast->cId; |
| 5026 | zName[i] = 0; |
| 5027 | return zName; |
| 5028 | } |
| 5029 | #endif |
| 5030 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5031 | |
| 5032 | /* |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5033 | ** Given the list of WhereLoop objects on pWInfo->pLoops, this routine |
| 5034 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5035 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5036 | ** |
| 5037 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5038 | ** error occurs. |
| 5039 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5040 | static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5041 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5042 | int nLoop; /* Number of terms in the join */ |
| 5043 | sqlite3 *db; /* The database connection */ |
| 5044 | int iLoop; /* Loop counter over the terms of the join */ |
| 5045 | int ii, jj; /* Loop counters */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5046 | WhereCost rCost; /* Cost of a path */ |
| 5047 | WhereCost mxCost; /* Maximum cost of a set of paths */ |
| 5048 | WhereCost rSortCost; /* Cost to do a sort */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5049 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 5050 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 5051 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 5052 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 5053 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 5054 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 5055 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 5056 | char *pSpace; /* Temporary memory used by this routine */ |
| 5057 | |
| 5058 | db = pWInfo->pParse->db; |
| 5059 | nLoop = pWInfo->nLevel; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5060 | mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5061 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5062 | #ifdef WHERETRACE_ENABLED |
| 5063 | if( sqlite3WhereTrace>=2 ) sqlite3DebugPrintf("---- begin solver\n"); |
| 5064 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5065 | |
| 5066 | /* Allocate and initialize space for aTo and aFrom */ |
| 5067 | ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 5068 | pSpace = sqlite3DbMallocRaw(db, ii); |
| 5069 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 5070 | aTo = (WherePath*)pSpace; |
| 5071 | aFrom = aTo+mxChoice; |
| 5072 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 5073 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 5074 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5075 | pFrom->aLoop = pX; |
| 5076 | } |
| 5077 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5078 | /* Seed the search with a single WherePath containing zero WhereLoops */ |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 5079 | aFrom[0].nRow = pWInfo->pParse->nQueryLoop; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5080 | nFrom = 1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5081 | |
| 5082 | /* Precompute the cost of sorting the final result set, if the caller |
| 5083 | ** to sqlite3WhereBegin() was concerned about sorting */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5084 | rSortCost = 0; |
| 5085 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5086 | aFrom[0].isOrderedValid = 1; |
| 5087 | }else{ |
| 5088 | /* Compute an estimate on the cost to sort the entire result set */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5089 | rSortCost = nRowEst + estLog(nRowEst); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5090 | #ifdef WHERETRACE_ENABLED |
| 5091 | if( sqlite3WhereTrace>=2 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5092 | sqlite3DebugPrintf("---- sort cost=%-3d\n", rSortCost); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5093 | } |
| 5094 | #endif |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | /* Compute successively longer WherePaths using the previous generation |
| 5098 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 5099 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5100 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 5101 | nTo = 0; |
| 5102 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 5103 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 5104 | Bitmask maskNew; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5105 | Bitmask revMask = 0; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5106 | u8 isOrderedValid = pFrom->isOrderedValid; |
| 5107 | u8 isOrdered = pFrom->isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5108 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 5109 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5110 | /* At this point, pWLoop is a candidate to be the next loop. |
| 5111 | ** Compute its cost */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5112 | rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 5113 | rCost = whereCostAdd(rCost, pFrom->rCost); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5114 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5115 | if( !isOrderedValid ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5116 | switch( wherePathSatisfiesOrderBy(pWInfo, |
| 5117 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
| 5118 | iLoop, iLoop==nLoop-1, pWLoop, &revMask) ){ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5119 | case 1: /* Yes. pFrom+pWLoop does satisfy the ORDER BY clause */ |
| 5120 | isOrdered = 1; |
| 5121 | isOrderedValid = 1; |
| 5122 | break; |
| 5123 | case 0: /* No. pFrom+pWLoop will require a separate sort */ |
| 5124 | isOrdered = 0; |
| 5125 | isOrderedValid = 1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5126 | rCost = whereCostAdd(rCost, rSortCost); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5127 | break; |
| 5128 | default: /* Cannot tell yet. Try again on the next iteration */ |
| 5129 | break; |
| 5130 | } |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 5131 | }else{ |
| 5132 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5133 | } |
| 5134 | /* Check to see if pWLoop should be added to the mxChoice best so far */ |
| 5135 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 5136 | if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){ |
| 5137 | break; |
| 5138 | } |
| 5139 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5140 | if( jj>=nTo ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5141 | if( nTo>=mxChoice && rCost>=mxCost ){ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5142 | #ifdef WHERETRACE_ENABLED |
| 5143 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5144 | sqlite3DebugPrintf("Skip %s cost=%3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5145 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5146 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5147 | } |
| 5148 | #endif |
| 5149 | continue; |
| 5150 | } |
| 5151 | /* Add a new Path to the aTo[] set */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5152 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5153 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5154 | jj = nTo++; |
| 5155 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5156 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 5157 | for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5158 | } |
| 5159 | pTo = &aTo[jj]; |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5160 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5161 | if( sqlite3WhereTrace&0x4 ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5162 | sqlite3DebugPrintf("New %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5163 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5164 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
| 5165 | } |
| 5166 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5167 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5168 | if( pTo->rCost<=rCost ){ |
| 5169 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5170 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5171 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5172 | "Skip %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5173 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5174 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5175 | sqlite3DebugPrintf(" vs %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5176 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5177 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5178 | } |
| 5179 | #endif |
| 5180 | continue; |
| 5181 | } |
| 5182 | /* A new and better score for a previously created equivalent path */ |
| 5183 | #ifdef WHERETRACE_ENABLED |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5184 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5185 | sqlite3DebugPrintf( |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5186 | "Update %s cost=%-3d order=%c", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5187 | wherePathName(pFrom, iLoop, pWLoop), rCost, |
| 5188 | isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?'); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5189 | sqlite3DebugPrintf(" was %s cost=%-3d order=%c\n", |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5190 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, |
| 5191 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
| 5192 | } |
| 5193 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5194 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5195 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5196 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5197 | pTo->revLoop = revMask; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5198 | pTo->nRow = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5199 | pTo->rCost = rCost; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5200 | pTo->isOrderedValid = isOrderedValid; |
| 5201 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5202 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 5203 | pTo->aLoop[iLoop] = pWLoop; |
| 5204 | if( nTo>=mxChoice ){ |
| 5205 | mxCost = aTo[0].rCost; |
| 5206 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 5207 | if( pTo->rCost>mxCost ) mxCost = pTo->rCost; |
| 5208 | } |
| 5209 | } |
| 5210 | } |
| 5211 | } |
| 5212 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5213 | #ifdef WHERETRACE_ENABLED |
| 5214 | if( sqlite3WhereTrace>=2 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5215 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5216 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5217 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5218 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5219 | pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?'); |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5220 | if( pTo->isOrderedValid && pTo->isOrdered ){ |
| 5221 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 5222 | }else{ |
| 5223 | sqlite3DebugPrintf("\n"); |
| 5224 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 5225 | } |
| 5226 | } |
| 5227 | #endif |
| 5228 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5229 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5230 | pFrom = aTo; |
| 5231 | aTo = aFrom; |
| 5232 | aFrom = pFrom; |
| 5233 | nFrom = nTo; |
| 5234 | } |
| 5235 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5236 | if( nFrom==0 ){ |
| 5237 | sqlite3ErrorMsg(pWInfo->pParse, "no query solution"); |
| 5238 | sqlite3DbFree(db, pSpace); |
| 5239 | return SQLITE_ERROR; |
| 5240 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5241 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5242 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5243 | pFrom = aFrom; |
| 5244 | for(ii=1; ii<nFrom; ii++){ |
| 5245 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 5246 | } |
| 5247 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5248 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5249 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5250 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 5251 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5252 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5253 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5254 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5255 | if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 5256 | && pWInfo->pDistinct |
| 5257 | && nRowEst |
| 5258 | ){ |
| 5259 | Bitmask notUsed; |
| 5260 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinct, pFrom, |
| 5261 | WHERE_DISTINCTBY, nLoop-1, 1, pFrom->aLoop[nLoop-1], ¬Used); |
| 5262 | if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5263 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5264 | if( pFrom->isOrdered ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5265 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 5266 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 5267 | }else{ |
| 5268 | pWInfo->bOBSat = 1; |
| 5269 | pWInfo->revMask = pFrom->revLoop; |
| 5270 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5271 | } |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5272 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5273 | |
| 5274 | /* Free temporary memory and return success */ |
| 5275 | sqlite3DbFree(db, pSpace); |
| 5276 | return SQLITE_OK; |
| 5277 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 5278 | |
| 5279 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5280 | ** Most queries use only a single table (they are not joins) and have |
| 5281 | ** simple == constraints against indexed fields. This routine attempts |
| 5282 | ** to plan those simple cases using much less ceremony than the |
| 5283 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 5284 | ** times for the common case. |
| 5285 | ** |
| 5286 | ** Return non-zero on success, if this query can be handled by this |
| 5287 | ** no-frills query planner. Return zero if this query needs the |
| 5288 | ** general-purpose query planner. |
| 5289 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5290 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5291 | WhereInfo *pWInfo; |
| 5292 | struct SrcList_item *pItem; |
| 5293 | WhereClause *pWC; |
| 5294 | WhereTerm *pTerm; |
| 5295 | WhereLoop *pLoop; |
| 5296 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5297 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5298 | Table *pTab; |
| 5299 | Index *pIdx; |
| 5300 | |
| 5301 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 5302 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5303 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 5304 | pItem = pWInfo->pTabList->a; |
| 5305 | pTab = pItem->pTab; |
| 5306 | if( IsVirtual(pTab) ) return 0; |
| 5307 | if( pItem->zIndex ) return 0; |
| 5308 | iCur = pItem->iCursor; |
| 5309 | pWC = &pWInfo->sWC; |
| 5310 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5311 | pLoop->wsFlags = 0; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5312 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5313 | if( pTerm ){ |
| 5314 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 5315 | pLoop->aLTerm[0] = pTerm; |
| 5316 | pLoop->nLTerm = 1; |
| 5317 | pLoop->u.btree.nEq = 1; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5318 | pLoop->rRun = 33; /* 33 == whereCostFromInt(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5319 | }else{ |
| 5320 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 5321 | if( pIdx->onError==OE_None ) continue; |
| 5322 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5323 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5324 | if( pTerm==0 ) break; |
| 5325 | whereLoopResize(pWInfo->pParse->db, pLoop, j); |
| 5326 | pLoop->aLTerm[j] = pTerm; |
| 5327 | } |
| 5328 | if( j!=pIdx->nColumn ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5329 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame^] | 5330 | if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5331 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 5332 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5333 | pLoop->nLTerm = j; |
| 5334 | pLoop->u.btree.nEq = j; |
| 5335 | pLoop->u.btree.pIndex = pIdx; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5336 | pLoop->rRun = 39; /* 39 == whereCostFromInt(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5337 | break; |
| 5338 | } |
| 5339 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5340 | if( pLoop->wsFlags ){ |
| 5341 | pLoop->nOut = (WhereCost)1; |
| 5342 | pWInfo->a[0].pWLoop = pLoop; |
| 5343 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 5344 | pWInfo->a[0].iTabCur = iCur; |
| 5345 | pWInfo->nRowOut = 1; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5346 | if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1; |
| 5347 | if( pWInfo->pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 5348 | #ifdef SQLITE_DEBUG |
| 5349 | pLoop->cId = '0'; |
| 5350 | #endif |
| 5351 | return 1; |
| 5352 | } |
| 5353 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
| 5356 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5357 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 5358 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5359 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5360 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5361 | ** in order to complete the WHERE clause processing. |
| 5362 | ** |
| 5363 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5364 | ** |
| 5365 | ** The basic idea is to do a nested loop, one loop for each table in |
| 5366 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 5367 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 5368 | ** example, if the SQL is this: |
| 5369 | ** |
| 5370 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 5371 | ** |
| 5372 | ** Then the code generated is conceptually like the following: |
| 5373 | ** |
| 5374 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5375 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5376 | ** foreach row3 in t3 do / |
| 5377 | ** ... |
| 5378 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5379 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5380 | ** end / |
| 5381 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5382 | ** Note that the loops might not be nested in the order in which they |
| 5383 | ** 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] | 5384 | ** use of indices. Note also that when the IN operator appears in |
| 5385 | ** the WHERE clause, it might result in additional nested loops for |
| 5386 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5387 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5388 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 5389 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 5390 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5391 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5392 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5393 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 5394 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5395 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 5396 | ** data from the various tables of the loop. |
| 5397 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5398 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 5399 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 5400 | ** the tables have indices and there are terms in the WHERE clause that |
| 5401 | ** refer to those indices, a complete table scan can be avoided and the |
| 5402 | ** code will run much faster. Most of the work of this routine is checking |
| 5403 | ** to see if there are indices that can be used to speed up the loop. |
| 5404 | ** |
| 5405 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 5406 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 5407 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 5408 | ** loops are evaluated and if false a jump is made around all subsequent |
| 5409 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 5410 | ** most loop) |
| 5411 | ** |
| 5412 | ** OUTER JOINS |
| 5413 | ** |
| 5414 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 5415 | ** |
| 5416 | ** foreach row1 in t1 do |
| 5417 | ** flag = 0 |
| 5418 | ** foreach row2 in t2 do |
| 5419 | ** start: |
| 5420 | ** ... |
| 5421 | ** flag = 1 |
| 5422 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5423 | ** if flag==0 then |
| 5424 | ** move the row2 cursor to a null row |
| 5425 | ** goto start |
| 5426 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5427 | ** end |
| 5428 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5429 | ** ORDER BY CLAUSE PROCESSING |
| 5430 | ** |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5431 | ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 5432 | ** 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] | 5433 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5434 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5435 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 5436 | Parse *pParse, /* The parser context */ |
| 5437 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 5438 | Expr *pWhere, /* The WHERE clause */ |
drh | 46ec5b6 | 2012-09-24 15:30:54 +0000 | [diff] [blame] | 5439 | ExprList *pOrderBy, /* An ORDER BY clause, or NULL */ |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5440 | ExprList *pDistinct, /* The select-list for DISTINCT queries - or NULL */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 5441 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 5442 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5443 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5444 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5445 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5446 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 5447 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5448 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5449 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5450 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5451 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5452 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5453 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5454 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5455 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5456 | |
| 5457 | /* Variable initialization */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5458 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5459 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5460 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5461 | /* 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] | 5462 | ** bits in a Bitmask |
| 5463 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5464 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5465 | if( pTabList->nSrc>BMS ){ |
| 5466 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 5467 | return 0; |
| 5468 | } |
| 5469 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5470 | /* This function normally generates a nested loop for all tables in |
| 5471 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 5472 | ** only generate code for the first table in pTabList and assume that |
| 5473 | ** any cursors associated with subsequent tables are uninitialized. |
| 5474 | */ |
| 5475 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 5476 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5477 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 5478 | ** return value. A single allocation is used to store the WhereInfo |
| 5479 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 5480 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 5481 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 5482 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5483 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5484 | db = pParse->db; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5485 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5486 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5487 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5488 | sqlite3DbFree(db, pWInfo); |
| 5489 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5490 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5491 | } |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5492 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5493 | pWInfo->pParse = pParse; |
| 5494 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5495 | pWInfo->pOrderBy = pOrderBy; |
| 5496 | pWInfo->pDistinct = pDistinct; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5497 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5498 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5499 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5500 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5501 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5502 | sWLB.pWC = &pWInfo->sWC; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5503 | sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList]; |
| 5504 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5505 | #ifdef SQLITE_DEBUG |
| 5506 | sWLB.pNew->cId = '*'; |
| 5507 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5508 | |
drh | a9b1b91 | 2011-07-08 13:07:02 +0000 | [diff] [blame] | 5509 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 5510 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
drh | 7e5418e | 2012-09-27 15:05:54 +0000 | [diff] [blame] | 5511 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0; |
drh | a9b1b91 | 2011-07-08 13:07:02 +0000 | [diff] [blame] | 5512 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5513 | /* Split the WHERE clause into separate subexpressions where each |
| 5514 | ** subexpression is separated by an AND operator. |
| 5515 | */ |
| 5516 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5517 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5518 | sqlite3ExprCodeConstants(pParse, pWhere); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5519 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5520 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5521 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 5522 | ** expression and either jump over all of the code or fall thru. |
| 5523 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5524 | if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 5525 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 5526 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 5527 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5528 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5529 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 5530 | ** |
| 5531 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 5532 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 5533 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 5534 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 5535 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 5536 | ** of the join. Subtracting one from the right table bitmask gives a |
| 5537 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 5538 | ** 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] | 5539 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5540 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 5541 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 5542 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 5543 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5544 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5545 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5546 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5547 | } |
| 5548 | #ifndef NDEBUG |
| 5549 | { |
| 5550 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5551 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 5552 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 5553 | assert( (m-1)==toTheLeft ); |
| 5554 | toTheLeft |= m; |
| 5555 | } |
| 5556 | } |
| 5557 | #endif |
| 5558 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5559 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 5560 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 5561 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 5562 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5563 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5564 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5565 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5566 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 5567 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5568 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5569 | /* If the ORDER BY (or GROUP BY) clause contains references to general |
| 5570 | ** expressions, then we won't be able to satisfy it using indices, so |
| 5571 | ** go ahead and disable it now. |
| 5572 | */ |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5573 | if( pOrderBy && pDistinct ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5574 | for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 5575 | Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr); |
| 5576 | if( pExpr->op!=TK_COLUMN ){ |
| 5577 | pWInfo->pOrderBy = pOrderBy = 0; |
| 5578 | break; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 5579 | }else if( pExpr->iColumn<0 ){ |
| 5580 | break; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5581 | } |
| 5582 | } |
| 5583 | } |
| 5584 | |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5585 | /* Check if the DISTINCT qualifier, if there is one, is redundant. |
| 5586 | ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to |
| 5587 | ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT. |
| 5588 | */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5589 | if( pDistinct ){ |
| 5590 | if( isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){ |
| 5591 | pDistinct = 0; |
| 5592 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 5593 | }else if( pOrderBy==0 ){ |
| 5594 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
| 5595 | pWInfo->pOrderBy = pDistinct; |
| 5596 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5599 | /* Construct the WhereLoop objects */ |
| 5600 | WHERETRACE(("*** Optimizer Start ***\n")); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5601 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5602 | rc = whereLoopAddAll(&sWLB); |
| 5603 | if( rc ) goto whereBeginError; |
| 5604 | |
| 5605 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5606 | #ifdef WHERETRACE_ENABLED |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5607 | if( sqlite3WhereTrace ){ |
| 5608 | WhereLoop *p; |
| 5609 | int i = 0; |
| 5610 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 5611 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 5612 | for(p=pWInfo->pLoops; p; p=p->pNextLoop){ |
| 5613 | p->cId = zLabel[(i++)%sizeof(zLabel)]; |
| 5614 | whereLoopPrint(p, pTabList); |
| 5615 | } |
| 5616 | } |
| 5617 | #endif |
| 5618 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5619 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5620 | if( db->mallocFailed ) goto whereBeginError; |
| 5621 | if( pWInfo->pOrderBy ){ |
| 5622 | wherePathSolver(pWInfo, pWInfo->nRowOut); |
| 5623 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5624 | } |
| 5625 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 5626 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 5627 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 5628 | } |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 5629 | if( pParse->nErr || db->mallocFailed ){ |
| 5630 | goto whereBeginError; |
| 5631 | } |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5632 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5633 | if( sqlite3WhereTrace ){ |
| 5634 | int ii; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5635 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 5636 | if( pWInfo->bOBSat ){ |
| 5637 | sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5638 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5639 | switch( pWInfo->eDistinct ){ |
| 5640 | case WHERE_DISTINCT_UNIQUE: { |
| 5641 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 5642 | break; |
| 5643 | } |
| 5644 | case WHERE_DISTINCT_ORDERED: { |
| 5645 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 5646 | break; |
| 5647 | } |
| 5648 | case WHERE_DISTINCT_UNORDERED: { |
| 5649 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 5650 | break; |
| 5651 | } |
| 5652 | } |
| 5653 | sqlite3DebugPrintf("\n"); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5654 | for(ii=0; ii<nTabList; ii++){ |
| 5655 | whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5656 | } |
| 5657 | } |
| 5658 | #endif |
drh | 0edc94d | 2013-05-31 19:14:56 +0000 | [diff] [blame] | 5659 | WHERETRACE(("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 5660 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5661 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5662 | #if 0 /* FIXME: Add this back in? */ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5663 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 5664 | ** to use a one-pass algorithm, determine if this is appropriate. |
| 5665 | ** The one-pass algorithm only works if the WHERE clause constraints |
| 5666 | ** the statement to update a single row. |
| 5667 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 5668 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5669 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5670 | pWInfo->okOnePass = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5671 | pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5672 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5673 | #endif |
| 5674 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5675 | /* Open all tables in the pTabList and any indices selected for |
| 5676 | ** searching those tables. |
| 5677 | */ |
| 5678 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5679 | notReady = ~(Bitmask)0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5680 | pWInfo->nRowOut = (WhereCost)1; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5681 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5682 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5683 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 5684 | struct SrcList_item *pTabItem; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5685 | WhereLoop *pLoop; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5686 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5687 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5688 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5689 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5690 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 5691 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 5692 | /* Do nothing */ |
| 5693 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5694 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5695 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5696 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 5697 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 5698 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 5699 | }else if( IsVirtual(pTab) ){ |
| 5700 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 5701 | }else |
| 5702 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5703 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5704 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 5705 | int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; |
| 5706 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 5707 | testcase( pTab->nCol==BMS-1 ); |
| 5708 | testcase( pTab->nCol==BMS ); |
danielk1977 | 2343297 | 2008-11-17 16:42:00 +0000 | [diff] [blame] | 5709 | if( !pWInfo->okOnePass && pTab->nCol<BMS ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5710 | Bitmask b = pTabItem->colUsed; |
| 5711 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5712 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 5713 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 5714 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 5715 | assert( n<=pTab->nCol ); |
| 5716 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 5717 | }else{ |
| 5718 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5719 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5720 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5721 | if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5722 | constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel); |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 5723 | }else |
| 5724 | #endif |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 5725 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5726 | Index *pIx = pLoop->u.btree.pIndex; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 5727 | KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 5728 | /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */ |
| 5729 | int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5730 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 5731 | assert( iIndexCur>=0 ); |
| 5732 | sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5733 | (char*)pKey, P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 5734 | VdbeComment((v, "%s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5735 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5736 | sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5737 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5738 | } |
| 5739 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 5740 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5741 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5742 | /* Generate the code to do the search. Each iteration of the for |
| 5743 | ** loop below generates code for a single nested loop of the VM |
| 5744 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5745 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 5746 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 5747 | for(ii=0; ii<nTabList; ii++){ |
| 5748 | pLevel = &pWInfo->a[ii]; |
| 5749 | explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5750 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 5751 | pWInfo->iContinue = pLevel->addrCont; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5752 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 5753 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 5754 | /* Done. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5755 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5756 | |
| 5757 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5758 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5759 | if( pWInfo ){ |
| 5760 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5761 | whereInfoFree(db, pWInfo); |
| 5762 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5763 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
| 5766 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 5767 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5768 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5769 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5770 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5771 | Parse *pParse = pWInfo->pParse; |
| 5772 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5773 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5774 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5775 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 5776 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 5777 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5778 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5779 | /* Generate loop termination code. |
| 5780 | */ |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5781 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5782 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5783 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5784 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5785 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5786 | if( pLevel->op!=OP_Noop ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 5787 | sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 5788 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5789 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5790 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 5791 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5792 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5793 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5794 | 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] | 5795 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 5796 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5797 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 5798 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 5799 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 5800 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 5801 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5802 | if( pLevel->iLeftJoin ){ |
| 5803 | int addr; |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5804 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5805 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 5806 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 5807 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 5808 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 5809 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 5810 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 5811 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 5812 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 5813 | if( pLevel->op==OP_Return ){ |
| 5814 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 5815 | }else{ |
| 5816 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 5817 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 5818 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 5819 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5820 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5821 | |
| 5822 | /* The "break" point is here, just past the end of the outer loop. |
| 5823 | ** Set it. |
| 5824 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5825 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5826 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5827 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5828 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 5829 | assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc ); |
| 5830 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5831 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 5832 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5833 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 5834 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5835 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5836 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 5837 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 5838 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 5839 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5840 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 5841 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5842 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 5843 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5844 | if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 5845 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 5846 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 5849 | /* If this scan uses an index, make code substitutions to read data |
| 5850 | ** from the index in preference to the table. Sometimes, this means |
| 5851 | ** the table need never be read from. This is a performance boost, |
| 5852 | ** as the vdbe level waits until the table is read before actually |
| 5853 | ** seeking the table cursor to the record corresponding to the current |
| 5854 | ** position in the index. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5855 | ** |
| 5856 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 5857 | ** sqlite3WhereEnd will have created code that references the table |
| 5858 | ** directly. This loop scans all that code looking for opcodes |
| 5859 | ** that reference the table and converts them into opcodes that |
| 5860 | ** reference the index. |
| 5861 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5862 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 5863 | pIdx = pLoop->u.btree.pIndex; |
| 5864 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 5865 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 5866 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5867 | if( pIdx && !db->mallocFailed ){ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 5868 | int k, j, last; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5869 | VdbeOp *pOp; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5870 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5871 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 5872 | last = sqlite3VdbeCurrentAddr(v); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 5873 | for(k=pWInfo->iTop; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5874 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 5875 | if( pOp->opcode==OP_Column ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5876 | for(j=0; j<pIdx->nColumn; j++){ |
| 5877 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 5878 | pOp->p2 = j; |
danielk1977 | 21de2e7 | 2007-11-29 17:43:27 +0000 | [diff] [blame] | 5879 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5880 | break; |
| 5881 | } |
| 5882 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 5883 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5884 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5885 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 5886 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5887 | } |
| 5888 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 5889 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 5890 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 5891 | |
| 5892 | /* Final cleanup |
| 5893 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 5894 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 5895 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5896 | return; |
| 5897 | } |