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