drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2013-11-12 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This file contains structure and macro definitions for the query |
| 14 | ** planner logic in "where.c". These definitions are broken out into |
| 15 | ** a separate source file for easier editing. |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | ** Trace output macros |
| 20 | */ |
| 21 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | 1d7b0a5 | 2018-04-05 12:02:27 +0000 | [diff] [blame] | 22 | /***/ extern int sqlite3WhereTrace; |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 23 | #endif |
| 24 | #if defined(SQLITE_DEBUG) \ |
| 25 | && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 26 | # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
| 27 | # define WHERETRACE_ENABLED 1 |
| 28 | #else |
| 29 | # define WHERETRACE(K,X) |
| 30 | #endif |
| 31 | |
| 32 | /* Forward references |
| 33 | */ |
| 34 | typedef struct WhereClause WhereClause; |
| 35 | typedef struct WhereMaskSet WhereMaskSet; |
| 36 | typedef struct WhereOrInfo WhereOrInfo; |
| 37 | typedef struct WhereAndInfo WhereAndInfo; |
| 38 | typedef struct WhereLevel WhereLevel; |
| 39 | typedef struct WhereLoop WhereLoop; |
| 40 | typedef struct WherePath WherePath; |
| 41 | typedef struct WhereTerm WhereTerm; |
| 42 | typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 43 | typedef struct WhereScan WhereScan; |
| 44 | typedef struct WhereOrCost WhereOrCost; |
| 45 | typedef struct WhereOrSet WhereOrSet; |
| 46 | |
| 47 | /* |
| 48 | ** This object contains information needed to implement a single nested |
| 49 | ** loop in WHERE clause. |
| 50 | ** |
| 51 | ** Contrast this object with WhereLoop. This object describes the |
| 52 | ** implementation of the loop. WhereLoop describes the algorithm. |
| 53 | ** This object contains a pointer to the WhereLoop algorithm as one of |
| 54 | ** its elements. |
| 55 | ** |
| 56 | ** The WhereInfo object contains a single instance of this object for |
| 57 | ** each term in the FROM clause (which is to say, for each of the |
| 58 | ** nested loops as implemented). The order of WhereLevel objects determines |
| 59 | ** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 60 | ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
| 61 | */ |
| 62 | struct WhereLevel { |
| 63 | int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 64 | int iTabCur; /* The VDBE cursor used to access the table */ |
| 65 | int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 66 | int addrBrk; /* Jump here to break out of the loop */ |
| 67 | int addrNxt; /* Jump here to start the next IN combination */ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 68 | int addrSkip; /* Jump here for next iteration of skip-scan */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 69 | int addrCont; /* Jump here to continue with the next loop cycle */ |
| 70 | int addrFirst; /* First instruction of interior of the loop */ |
| 71 | int addrBody; /* Beginning of the body of this loop */ |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 72 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 44aebff | 2016-05-02 10:25:42 +0000 | [diff] [blame] | 73 | u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */ |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 74 | int addrLikeRep; /* LIKE range processing address */ |
drh | 41d2e66 | 2015-12-01 21:23:07 +0000 | [diff] [blame] | 75 | #endif |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 76 | u8 iFrom; /* Which entry in the FROM clause */ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 77 | u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 78 | int p1, p2; /* Operands of the opcode used to ends the loop */ |
| 79 | union { /* Information that depends on pWLoop->wsFlags */ |
| 80 | struct { |
| 81 | int nIn; /* Number of entries in aInLoop[] */ |
| 82 | struct InLoop { |
| 83 | int iCur; /* The VDBE cursor used by this IN operator */ |
| 84 | int addrInTop; /* Top of the IN loop */ |
drh | a0368d9 | 2018-05-30 00:54:23 +0000 | [diff] [blame] | 85 | int iBase; /* Base register of multi-key index record */ |
| 86 | int nPrefix; /* Number of prior entires in the key */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 87 | u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 88 | } *aInLoop; /* Information about each nested IN operator */ |
| 89 | } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 90 | Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 91 | } u; |
| 92 | struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 93 | Bitmask notReady; /* FROM entries not usable at this level */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 94 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 95 | int addrVisit; /* Address at which row is visited */ |
| 96 | #endif |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /* |
| 100 | ** Each instance of this object represents an algorithm for evaluating one |
| 101 | ** term of a join. Every term of the FROM clause will have at least |
| 102 | ** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 103 | ** prevent a query solution - which is an error) and many terms of the |
| 104 | ** FROM clause will have multiple WhereLoop objects, each describing a |
| 105 | ** potential way of implementing that FROM-clause term, together with |
| 106 | ** dependencies and cost estimates for using the chosen algorithm. |
| 107 | ** |
| 108 | ** Query planning consists of building up a collection of these WhereLoop |
| 109 | ** objects, then computing a particular sequence of WhereLoop objects, with |
| 110 | ** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 111 | ** and that minimize the overall cost. |
| 112 | */ |
| 113 | struct WhereLoop { |
| 114 | Bitmask prereq; /* Bitmask of other loops that must run first */ |
| 115 | Bitmask maskSelf; /* Bitmask identifying table iTab */ |
| 116 | #ifdef SQLITE_DEBUG |
| 117 | char cId; /* Symbolic ID of this loop for debugging use */ |
| 118 | #endif |
| 119 | u8 iTab; /* Position in FROM clause of table for this loop */ |
| 120 | u8 iSortIdx; /* Sorting index number. 0==None */ |
| 121 | LogEst rSetup; /* One-time setup cost (ex: create transient index) */ |
| 122 | LogEst rRun; /* Cost of running each loop */ |
| 123 | LogEst nOut; /* Estimated number of output rows */ |
| 124 | union { |
| 125 | struct { /* Information for internal btree tables */ |
drh | 5e6790c | 2013-11-12 20:18:14 +0000 | [diff] [blame] | 126 | u16 nEq; /* Number of equality constraints */ |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 127 | u16 nBtm; /* Size of BTM vector */ |
| 128 | u16 nTop; /* Size of TOP vector */ |
drh | 172806e | 2017-04-13 21:29:02 +0000 | [diff] [blame] | 129 | u16 nIdxCol; /* Index column used for ORDER BY */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 130 | Index *pIndex; /* Index used, or NULL */ |
| 131 | } btree; |
| 132 | struct { /* Information for virtual tables */ |
| 133 | int idxNum; /* Index number */ |
| 134 | u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 135 | i8 isOrdered; /* True if satisfies ORDER BY */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 136 | u16 omitMask; /* Terms that may be omitted */ |
| 137 | char *idxStr; /* Index identifier string */ |
| 138 | } vtab; |
| 139 | } u; |
| 140 | u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 141 | u16 nLTerm; /* Number of entries in aLTerm[] */ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 142 | u16 nSkip; /* Number of NULL aLTerm[] entries */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 143 | /**** whereLoopXfer() copies fields above ***********************/ |
| 144 | # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 145 | u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 146 | WhereTerm **aLTerm; /* WhereTerms used */ |
| 147 | WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 148 | WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /* This object holds the prerequisites and the cost of running a |
| 152 | ** subquery on one operand of an OR operator in the WHERE clause. |
| 153 | ** See WhereOrSet for additional information |
| 154 | */ |
| 155 | struct WhereOrCost { |
| 156 | Bitmask prereq; /* Prerequisites */ |
| 157 | LogEst rRun; /* Cost of running this subquery */ |
| 158 | LogEst nOut; /* Number of outputs for this subquery */ |
| 159 | }; |
| 160 | |
| 161 | /* The WhereOrSet object holds a set of possible WhereOrCosts that |
| 162 | ** correspond to the subquery(s) of OR-clause processing. Only the |
| 163 | ** best N_OR_COST elements are retained. |
| 164 | */ |
| 165 | #define N_OR_COST 3 |
| 166 | struct WhereOrSet { |
| 167 | u16 n; /* Number of valid a[] entries */ |
| 168 | WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
| 169 | }; |
| 170 | |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 171 | /* |
| 172 | ** Each instance of this object holds a sequence of WhereLoop objects |
| 173 | ** that implement some or all of a query plan. |
| 174 | ** |
| 175 | ** Think of each WhereLoop object as a node in a graph with arcs |
| 176 | ** showing dependencies and costs for travelling between nodes. (That is |
| 177 | ** not a completely accurate description because WhereLoop costs are a |
| 178 | ** vector, not a scalar, and because dependencies are many-to-one, not |
| 179 | ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 180 | ** Then a WherePath object is a path through the graph that visits some |
| 181 | ** or all of the WhereLoop objects once. |
| 182 | ** |
| 183 | ** The "solver" works by creating the N best WherePath objects of length |
| 184 | ** 1. Then using those as a basis to compute the N best WherePath objects |
| 185 | ** of length 2. And so forth until the length of WherePaths equals the |
| 186 | ** number of nodes in the FROM clause. The best (lowest cost) WherePath |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 187 | ** at the end is the chosen query plan. |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 188 | */ |
| 189 | struct WherePath { |
| 190 | Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
| 191 | Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
| 192 | LogEst nRow; /* Estimated number of rows generated by this path */ |
| 193 | LogEst rCost; /* Total cost of this path */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 194 | LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 195 | i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 196 | WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
| 197 | }; |
| 198 | |
| 199 | /* |
| 200 | ** The query generator uses an array of instances of this structure to |
| 201 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 202 | ** clause subexpression is separated from the others by AND operators, |
| 203 | ** usually, or sometimes subexpressions separated by OR. |
| 204 | ** |
| 205 | ** All WhereTerms are collected into a single WhereClause structure. |
| 206 | ** The following identity holds: |
| 207 | ** |
| 208 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
| 209 | ** |
| 210 | ** When a term is of the form: |
| 211 | ** |
| 212 | ** X <op> <expr> |
| 213 | ** |
| 214 | ** where X is a column name and <op> is one of certain operators, |
| 215 | ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 216 | ** cursor number and column number for X. WhereTerm.eOperator records |
| 217 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 218 | ** use of a bitmask encoding for the operator allows us to search |
| 219 | ** quickly for terms that match any of several different operators. |
| 220 | ** |
| 221 | ** A WhereTerm might also be two or more subterms connected by OR: |
| 222 | ** |
| 223 | ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 224 | ** |
| 225 | ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR |
| 226 | ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
| 227 | ** is collected about the OR clause. |
| 228 | ** |
| 229 | ** If a term in the WHERE clause does not match either of the two previous |
| 230 | ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 231 | ** to the original subexpression content and wtFlags is set up appropriately |
| 232 | ** but no other fields in the WhereTerm object are meaningful. |
| 233 | ** |
| 234 | ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
| 235 | ** but they do so indirectly. A single WhereMaskSet structure translates |
| 236 | ** cursor number into bits and the translated bit is stored in the prereq |
| 237 | ** fields. The translation is used in order to maximize the number of |
| 238 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 239 | ** spread out over the non-negative integers. For example, the cursor |
| 240 | ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet |
| 241 | ** translates these sparse cursor numbers into consecutive integers |
| 242 | ** beginning with 0 in order to make the best possible use of the available |
| 243 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 244 | ** would be mapped into integers 0 through 7. |
| 245 | ** |
| 246 | ** The number of terms in a join is limited by the number of bits |
| 247 | ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 248 | ** is only able to process joins with 64 or fewer tables. |
| 249 | */ |
| 250 | struct WhereTerm { |
| 251 | Expr *pExpr; /* Pointer to the subexpression that is this term */ |
drh | 87c05f0 | 2016-10-03 14:44:47 +0000 | [diff] [blame] | 252 | WhereClause *pWC; /* The clause this term is part of */ |
| 253 | LogEst truthProb; /* Probability of truth for this expression */ |
| 254 | u16 wtFlags; /* TERM_xxx bit flags. See below */ |
| 255 | u16 eOperator; /* A WO_xx value describing <op> */ |
| 256 | u8 nChild; /* Number of children that must disable us */ |
| 257 | u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 258 | int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 259 | int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 260 | int iField; /* Field in (?,?,?) IN (SELECT...) vector */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 261 | union { |
| 262 | int leftColumn; /* Column number of X in "X <op> <expr>" */ |
| 263 | WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 264 | WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
| 265 | } u; |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 266 | Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 267 | Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
| 268 | }; |
| 269 | |
| 270 | /* |
| 271 | ** Allowed values of WhereTerm.wtFlags |
| 272 | */ |
| 273 | #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
| 274 | #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 275 | #define TERM_CODED 0x04 /* This term is already coded */ |
| 276 | #define TERM_COPIED 0x08 /* Has a child */ |
| 277 | #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 278 | #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 279 | #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
| 280 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 281 | # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 282 | #else |
| 283 | # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 284 | #endif |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 285 | #define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */ |
| 286 | #define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */ |
| 287 | #define TERM_LIKE 0x400 /* The original LIKE operator */ |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 288 | #define TERM_IS 0x800 /* Term.pExpr is an IS operator */ |
dan | d3930b1 | 2017-07-10 15:17:30 +0000 | [diff] [blame] | 289 | #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | ** An instance of the WhereScan object is used as an iterator for locating |
| 293 | ** terms in the WHERE clause that are useful to the query planner. |
| 294 | */ |
| 295 | struct WhereScan { |
| 296 | WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 297 | WhereClause *pWC; /* WhereClause currently being scanned */ |
drh | f19aa5f | 2015-12-30 16:51:20 +0000 | [diff] [blame] | 298 | const char *zCollName; /* Required collating sequence, if not NULL */ |
drh | 6860e6f | 2015-08-27 18:24:02 +0000 | [diff] [blame] | 299 | Expr *pIdxExpr; /* Search for this index expression */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 300 | char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 301 | unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 302 | unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 303 | u32 opMask; /* Acceptable operators */ |
| 304 | int k; /* Resume scanning at this->pWC->a[this->k] */ |
drh | a3f108e | 2015-08-26 21:08:04 +0000 | [diff] [blame] | 305 | int aiCur[11]; /* Cursors in the equivalence class */ |
| 306 | i16 aiColumn[11]; /* Corresponding column number in the eq-class */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | /* |
| 310 | ** An instance of the following structure holds all information about a |
| 311 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 312 | ** |
| 313 | ** Explanation of pOuter: For a WHERE clause of the form |
| 314 | ** |
| 315 | ** a AND ((b AND c) OR (d AND e)) AND f |
| 316 | ** |
| 317 | ** There are separate WhereClause objects for the whole clause and for |
| 318 | ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 319 | ** subclauses points to the WhereClause object for the whole clause. |
| 320 | */ |
| 321 | struct WhereClause { |
| 322 | WhereInfo *pWInfo; /* WHERE clause processing context */ |
| 323 | WhereClause *pOuter; /* Outer conjunction */ |
| 324 | u8 op; /* Split operator. TK_AND or TK_OR */ |
drh | da230bd | 2018-06-09 00:09:58 +0000 | [diff] [blame] | 325 | u8 hasOr; /* True if any a[].eOperator is WO_OR */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 326 | int nTerm; /* Number of terms */ |
| 327 | int nSlot; /* Number of entries in a[] */ |
| 328 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 329 | #if defined(SQLITE_SMALL_STACK) |
| 330 | WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 331 | #else |
| 332 | WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 333 | #endif |
| 334 | }; |
| 335 | |
| 336 | /* |
| 337 | ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 338 | ** a dynamically allocated instance of the following structure. |
| 339 | */ |
| 340 | struct WhereOrInfo { |
| 341 | WhereClause wc; /* Decomposition into subterms */ |
| 342 | Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
| 343 | }; |
| 344 | |
| 345 | /* |
| 346 | ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 347 | ** a dynamically allocated instance of the following structure. |
| 348 | */ |
| 349 | struct WhereAndInfo { |
| 350 | WhereClause wc; /* The subexpression broken out */ |
| 351 | }; |
| 352 | |
| 353 | /* |
| 354 | ** An instance of the following structure keeps track of a mapping |
| 355 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
| 356 | ** |
| 357 | ** The VDBE cursor numbers are small integers contained in |
| 358 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 359 | ** clause, the cursor numbers might not begin with 0 and they might |
| 360 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 361 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 362 | ** from the sparse cursor numbers into consecutive integers beginning |
| 363 | ** with 0. |
| 364 | ** |
| 365 | ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask |
| 366 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 367 | ** |
| 368 | ** For example, if the WHERE clause expression used these VDBE |
| 369 | ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
| 370 | ** would map those cursor numbers into bits 0 through 5. |
| 371 | ** |
| 372 | ** Note that the mapping is not necessarily ordered. In the example |
| 373 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 374 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 375 | ** does not really matter. What is important is that sparse cursor |
| 376 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 377 | ** no gaps. |
| 378 | */ |
| 379 | struct WhereMaskSet { |
dan | d3930b1 | 2017-07-10 15:17:30 +0000 | [diff] [blame] | 380 | int bVarSelect; /* Used by sqlite3WhereExprUsage() */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 381 | int n; /* Number of assigned cursor values */ |
| 382 | int ix[BMS]; /* Cursor assigned to each bit */ |
| 383 | }; |
| 384 | |
| 385 | /* |
drh | 6c1f4ef | 2015-06-08 14:23:15 +0000 | [diff] [blame] | 386 | ** Initialize a WhereMaskSet object |
| 387 | */ |
| 388 | #define initMaskSet(P) (P)->n=0 |
| 389 | |
| 390 | /* |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 391 | ** This object is a convenience wrapper holding all information needed |
| 392 | ** to construct WhereLoop objects for a particular query. |
| 393 | */ |
| 394 | struct WhereLoopBuilder { |
| 395 | WhereInfo *pWInfo; /* Information about this WHERE */ |
| 396 | WhereClause *pWC; /* WHERE clause terms */ |
| 397 | ExprList *pOrderBy; /* ORDER BY clause */ |
| 398 | WhereLoop *pNew; /* Template WhereLoop */ |
| 399 | WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
| 400 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 401 | UnpackedRecord *pRec; /* Probe for stat4 (if required) */ |
| 402 | int nRecValid; /* Number of valid fields currently in pRec */ |
| 403 | #endif |
drh | a3928dd | 2017-02-17 15:26:36 +0000 | [diff] [blame] | 404 | unsigned int bldFlags; /* SQLITE_BLDF_* flags */ |
drh | fc9098a | 2018-09-21 18:43:51 +0000 | [diff] [blame] | 405 | unsigned int iPlanLimit; /* Search limiter */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 406 | }; |
| 407 | |
drh | a3928dd | 2017-02-17 15:26:36 +0000 | [diff] [blame] | 408 | /* Allowed values for WhereLoopBuider.bldFlags */ |
| 409 | #define SQLITE_BLDF_INDEXED 0x0001 /* An index is used */ |
| 410 | #define SQLITE_BLDF_UNIQUE 0x0002 /* All keys of a UNIQUE index used */ |
| 411 | |
drh | 6fb5d35 | 2018-09-24 12:37:01 +0000 | [diff] [blame] | 412 | /* The WhereLoopBuilder.iPlanLimit is used to limit the number of |
| 413 | ** index+constraint combinations the query planner will consider for a |
| 414 | ** particular query. If this parameter is unlimited, then certain |
| 415 | ** pathological queries can spend excess time in the sqlite3WhereBegin() |
| 416 | ** routine. The limit is high enough that is should not impact real-world |
| 417 | ** queries. |
| 418 | ** |
| 419 | ** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is |
| 420 | ** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM |
| 421 | ** clause is processed, so that every table in a join is guaranteed to be |
| 422 | ** able to propose a some index+constraint combinations even if the initial |
| 423 | ** baseline limit was exhausted by prior tables of the join. |
| 424 | */ |
| 425 | #ifndef SQLITE_QUERY_PLANNER_LIMIT |
| 426 | # define SQLITE_QUERY_PLANNER_LIMIT 20000 |
| 427 | #endif |
| 428 | #ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR |
| 429 | # define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000 |
| 430 | #endif |
| 431 | |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 432 | /* |
| 433 | ** The WHERE clause processing routine has two halves. The |
| 434 | ** first part does the start of the WHERE loop and the second |
| 435 | ** half does the tail of the WHERE loop. An instance of |
| 436 | ** this structure is returned by the first half and passed |
| 437 | ** into the second half to give some continuity. |
| 438 | ** |
| 439 | ** An instance of this object holds the complete state of the query |
| 440 | ** planner. |
| 441 | */ |
| 442 | struct WhereInfo { |
| 443 | Parse *pParse; /* Parsing and code generating context */ |
| 444 | SrcList *pTabList; /* List of tables in the join */ |
| 445 | ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
drh | e9ba910 | 2017-02-16 20:52:52 +0000 | [diff] [blame] | 446 | ExprList *pResultSet; /* Result set of the query */ |
drh | aca19e1 | 2017-04-07 19:41:31 +0000 | [diff] [blame] | 447 | Expr *pWhere; /* The complete WHERE clause */ |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 448 | LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */ |
drh | 87c05f0 | 2016-10-03 14:44:47 +0000 | [diff] [blame] | 449 | int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
| 450 | int iContinue; /* Jump here to continue with next record */ |
| 451 | int iBreak; /* Jump here to break out of the loop */ |
| 452 | int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 453 | u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
drh | 87c05f0 | 2016-10-03 14:44:47 +0000 | [diff] [blame] | 454 | u8 nLevel; /* Number of nested loop */ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 455 | i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */ |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 456 | u8 sorted; /* True if really sorted (not just grouped) */ |
drh | b0264ee | 2015-09-14 14:45:50 +0000 | [diff] [blame] | 457 | u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 458 | u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
drh | a536df4 | 2016-05-19 22:13:37 +0000 | [diff] [blame] | 459 | u8 eDistinct; /* One of the WHERE_DISTINCT_* values */ |
drh | a536df4 | 2016-05-19 22:13:37 +0000 | [diff] [blame] | 460 | u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 461 | int iTop; /* The very beginning of the WHERE loop */ |
drh | 87c05f0 | 2016-10-03 14:44:47 +0000 | [diff] [blame] | 462 | WhereLoop *pLoops; /* List of all WhereLoop objects */ |
| 463 | Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
| 464 | LogEst nRowOut; /* Estimated number of output rows */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 465 | WhereClause sWC; /* Decomposition of the WHERE clause */ |
drh | 87c05f0 | 2016-10-03 14:44:47 +0000 | [diff] [blame] | 466 | WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 467 | WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 468 | }; |
| 469 | |
| 470 | /* |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame] | 471 | ** Private interfaces - callable only by other where.c routines. |
drh | 6c1f4ef | 2015-06-08 14:23:15 +0000 | [diff] [blame] | 472 | ** |
| 473 | ** where.c: |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame] | 474 | */ |
| 475 | Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); |
drh | c84a402 | 2016-05-27 12:30:20 +0000 | [diff] [blame] | 476 | #ifdef WHERETRACE_ENABLED |
| 477 | void sqlite3WhereClausePrint(WhereClause *pWC); |
| 478 | #endif |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame] | 479 | WhereTerm *sqlite3WhereFindTerm( |
| 480 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 481 | int iCur, /* Cursor number of LHS */ |
| 482 | int iColumn, /* Column number of LHS */ |
| 483 | Bitmask notReady, /* RHS must not overlap with this mask */ |
| 484 | u32 op, /* Mask of WO_xx values describing operator */ |
| 485 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 486 | ); |
drh | 6c1f4ef | 2015-06-08 14:23:15 +0000 | [diff] [blame] | 487 | |
| 488 | /* wherecode.c: */ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame] | 489 | #ifndef SQLITE_OMIT_EXPLAIN |
| 490 | int sqlite3WhereExplainOneScan( |
| 491 | Parse *pParse, /* Parse context */ |
| 492 | SrcList *pTabList, /* Table list this loop refers to */ |
| 493 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame] | 494 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 495 | ); |
| 496 | #else |
drh | e2188f0 | 2018-05-07 11:37:34 +0000 | [diff] [blame] | 497 | # define sqlite3WhereExplainOneScan(u,v,w,x) 0 |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame] | 498 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 499 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 500 | void sqlite3WhereAddScanStatus( |
| 501 | Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 502 | SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 503 | WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 504 | int addrExplain /* Address of OP_Explain (or 0) */ |
| 505 | ); |
| 506 | #else |
| 507 | # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d) |
| 508 | #endif |
| 509 | Bitmask sqlite3WhereCodeOneLoopStart( |
| 510 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 511 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 512 | Bitmask notReady /* Which tables are currently available */ |
| 513 | ); |
| 514 | |
drh | 6c1f4ef | 2015-06-08 14:23:15 +0000 | [diff] [blame] | 515 | /* whereexpr.c: */ |
| 516 | void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); |
| 517 | void sqlite3WhereClauseClear(WhereClause*); |
| 518 | void sqlite3WhereSplit(WhereClause*,Expr*,u8); |
| 519 | Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
drh | ccf6db5 | 2018-06-09 02:49:11 +0000 | [diff] [blame] | 520 | Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*); |
drh | 6c1f4ef | 2015-06-08 14:23:15 +0000 | [diff] [blame] | 521 | Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 522 | void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
drh | 01d230c | 2015-08-19 17:11:37 +0000 | [diff] [blame] | 523 | void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*); |
drh | 6f82e85 | 2015-06-06 20:12:09 +0000 | [diff] [blame] | 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | /* |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 530 | ** Bitmasks for the operators on WhereTerm objects. These are all |
| 531 | ** operators that are of interest to the query planner. An |
| 532 | ** OR-ed combination of these values can be used when searching for |
| 533 | ** particular WhereTerms within a WhereClause. |
drh | 4971160 | 2016-04-14 16:40:13 +0000 | [diff] [blame] | 534 | ** |
| 535 | ** Value constraints: |
| 536 | ** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ |
| 537 | ** WO_LT == SQLITE_INDEX_CONSTRAINT_LT |
| 538 | ** WO_LE == SQLITE_INDEX_CONSTRAINT_LE |
| 539 | ** WO_GT == SQLITE_INDEX_CONSTRAINT_GT |
| 540 | ** WO_GE == SQLITE_INDEX_CONSTRAINT_GE |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 541 | */ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 542 | #define WO_IN 0x0001 |
| 543 | #define WO_EQ 0x0002 |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 544 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 545 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 546 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 547 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
drh | 303a69b | 2017-09-11 19:47:37 +0000 | [diff] [blame] | 548 | #define WO_AUX 0x0040 /* Op useful to virtual tables only */ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 549 | #define WO_IS 0x0080 |
| 550 | #define WO_ISNULL 0x0100 |
| 551 | #define WO_OR 0x0200 /* Two or more OR-connected terms */ |
| 552 | #define WO_AND 0x0400 /* Two or more AND-connected terms */ |
| 553 | #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */ |
| 554 | #define WO_NOOP 0x1000 /* This term does not restrict search space */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 555 | |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 556 | #define WO_ALL 0x1fff /* Mask of all possible WO_* values */ |
| 557 | #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */ |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 558 | |
| 559 | /* |
| 560 | ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 561 | ** The particular combination of bits in each WhereLoop help to |
| 562 | ** determine the algorithm that WhereLoop represents. |
| 563 | */ |
| 564 | #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ |
| 565 | #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 566 | #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 567 | #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
| 568 | #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
| 569 | #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 570 | #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 571 | #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 572 | #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 573 | #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 574 | #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 575 | #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 576 | #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
| 577 | #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 578 | #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 579 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 580 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 581 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame] | 582 | #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
drh | f7b0a5f | 2018-06-07 14:59:22 +0000 | [diff] [blame] | 583 | #define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */ |