1e54df42dSdrh /* 2e54df42dSdrh ** 2013-11-12 3e54df42dSdrh ** 4e54df42dSdrh ** The author disclaims copyright to this source code. In place of 5e54df42dSdrh ** a legal notice, here is a blessing: 6e54df42dSdrh ** 7e54df42dSdrh ** May you do good and not evil. 8e54df42dSdrh ** May you find forgiveness for yourself and forgive others. 9e54df42dSdrh ** May you share freely, never taking more than you give. 10e54df42dSdrh ** 11e54df42dSdrh ************************************************************************* 12e54df42dSdrh ** 13e54df42dSdrh ** This file contains structure and macro definitions for the query 14e54df42dSdrh ** planner logic in "where.c". These definitions are broken out into 15e54df42dSdrh ** a separate source file for easier editing. 16e54df42dSdrh */ 17d9bc6e89Smistachkin #ifndef SQLITE_WHEREINT_H 18d9bc6e89Smistachkin #define SQLITE_WHEREINT_H 19e54df42dSdrh 20e54df42dSdrh 21e54df42dSdrh /* Forward references 22e54df42dSdrh */ 23e54df42dSdrh typedef struct WhereClause WhereClause; 24e54df42dSdrh typedef struct WhereMaskSet WhereMaskSet; 25e54df42dSdrh typedef struct WhereOrInfo WhereOrInfo; 26e54df42dSdrh typedef struct WhereAndInfo WhereAndInfo; 27e54df42dSdrh typedef struct WhereLevel WhereLevel; 28e54df42dSdrh typedef struct WhereLoop WhereLoop; 29e54df42dSdrh typedef struct WherePath WherePath; 30e54df42dSdrh typedef struct WhereTerm WhereTerm; 31e54df42dSdrh typedef struct WhereLoopBuilder WhereLoopBuilder; 32e54df42dSdrh typedef struct WhereScan WhereScan; 33e54df42dSdrh typedef struct WhereOrCost WhereOrCost; 34e54df42dSdrh typedef struct WhereOrSet WhereOrSet; 35f8bdcfa3Sdrh typedef struct WhereMemBlock WhereMemBlock; 367c1734b0Sdrh typedef struct WhereRightJoin WhereRightJoin; 37f8bdcfa3Sdrh 38f8bdcfa3Sdrh /* 39f8bdcfa3Sdrh ** This object is a header on a block of allocated memory that will be 40f8bdcfa3Sdrh ** automatically freed when its WInfo oject is destructed. 41f8bdcfa3Sdrh */ 42f8bdcfa3Sdrh struct WhereMemBlock { 43f8bdcfa3Sdrh WhereMemBlock *pNext; /* Next block in the chain */ 44cc212e44Sdrh u64 sz; /* Bytes of space */ 45f8bdcfa3Sdrh }; 46e54df42dSdrh 47e54df42dSdrh /* 487c1734b0Sdrh ** Extra information attached to a WhereLevel that is a RIGHT JOIN. 497c1734b0Sdrh */ 507c1734b0Sdrh struct WhereRightJoin { 517c1734b0Sdrh int iMatch; /* Cursor used to determine prior matched rows */ 527c1734b0Sdrh int regBloom; /* Bloom filter for iRJMatch */ 537c1734b0Sdrh int regReturn; /* Return register for the interior subroutine */ 547c1734b0Sdrh int addrSubrtn; /* Starting address for the interior subroutine */ 55b77c3129Sdrh int endSubrtn; /* The last opcode in the interior subroutine */ 567c1734b0Sdrh }; 577c1734b0Sdrh 587c1734b0Sdrh /* 59e54df42dSdrh ** This object contains information needed to implement a single nested 60e54df42dSdrh ** loop in WHERE clause. 61e54df42dSdrh ** 62e54df42dSdrh ** Contrast this object with WhereLoop. This object describes the 63e54df42dSdrh ** implementation of the loop. WhereLoop describes the algorithm. 64e54df42dSdrh ** This object contains a pointer to the WhereLoop algorithm as one of 65e54df42dSdrh ** its elements. 66e54df42dSdrh ** 67e54df42dSdrh ** The WhereInfo object contains a single instance of this object for 68e54df42dSdrh ** each term in the FROM clause (which is to say, for each of the 69e54df42dSdrh ** nested loops as implemented). The order of WhereLevel objects determines 70e54df42dSdrh ** the loop nested order, with WhereInfo.a[0] being the outer loop and 71e54df42dSdrh ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. 72e54df42dSdrh */ 73e54df42dSdrh struct WhereLevel { 74e54df42dSdrh int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ 75e54df42dSdrh int iTabCur; /* The VDBE cursor used to access the table */ 76e54df42dSdrh int iIdxCur; /* The VDBE cursor used to access pIdx */ 77e54df42dSdrh int addrBrk; /* Jump here to break out of the loop */ 78e54df42dSdrh int addrNxt; /* Jump here to start the next IN combination */ 79cd8629e4Sdrh int addrSkip; /* Jump here for next iteration of skip-scan */ 80e54df42dSdrh int addrCont; /* Jump here to continue with the next loop cycle */ 81e54df42dSdrh int addrFirst; /* First instruction of interior of the loop */ 82e54df42dSdrh int addrBody; /* Beginning of the body of this loop */ 83ec3dda5bSdrh int regBignull; /* big-null flag reg. True if a NULL-scan is needed */ 8415750a26Sdan int addrBignull; /* Jump here for next part of big-null scan */ 8541d2e66eSdrh #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS 8644aebff2Sdrh u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */ 87f07cf6e2Sdrh int addrLikeRep; /* LIKE range processing address */ 8841d2e66eSdrh #endif 892db144c3Sdrh int regFilter; /* Bloom filter */ 907c1734b0Sdrh WhereRightJoin *pRJ; /* Extra information for RIGHT JOIN */ 91e54df42dSdrh u8 iFrom; /* Which entry in the FROM clause */ 92e39a732cSdrh u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ 9315750a26Sdan int p1, p2; /* Operands of the opcode used to end the loop */ 94e54df42dSdrh union { /* Information that depends on pWLoop->wsFlags */ 95e54df42dSdrh struct { 96e54df42dSdrh int nIn; /* Number of entries in aInLoop[] */ 97e54df42dSdrh struct InLoop { 98e54df42dSdrh int iCur; /* The VDBE cursor used by this IN operator */ 99e54df42dSdrh int addrInTop; /* Top of the IN loop */ 100a0368d93Sdrh int iBase; /* Base register of multi-key index record */ 101a0368d93Sdrh int nPrefix; /* Number of prior entires in the key */ 102e54df42dSdrh u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ 103e54df42dSdrh } *aInLoop; /* Information about each nested IN operator */ 104e54df42dSdrh } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ 1050475629dSdrh Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */ 106e54df42dSdrh } u; 107e54df42dSdrh struct WhereLoop *pWLoop; /* The selected WhereLoop object */ 108e54df42dSdrh Bitmask notReady; /* FROM entries not usable at this level */ 1096f9702edSdan #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 1106f9702edSdan int addrVisit; /* Address at which row is visited */ 1116f9702edSdan #endif 112e54df42dSdrh }; 113e54df42dSdrh 114e54df42dSdrh /* 115e54df42dSdrh ** Each instance of this object represents an algorithm for evaluating one 116e54df42dSdrh ** term of a join. Every term of the FROM clause will have at least 117e54df42dSdrh ** one corresponding WhereLoop object (unless INDEXED BY constraints 118e54df42dSdrh ** prevent a query solution - which is an error) and many terms of the 119e54df42dSdrh ** FROM clause will have multiple WhereLoop objects, each describing a 120e54df42dSdrh ** potential way of implementing that FROM-clause term, together with 121e54df42dSdrh ** dependencies and cost estimates for using the chosen algorithm. 122e54df42dSdrh ** 123e54df42dSdrh ** Query planning consists of building up a collection of these WhereLoop 124e54df42dSdrh ** objects, then computing a particular sequence of WhereLoop objects, with 125e54df42dSdrh ** one WhereLoop object per FROM clause term, that satisfy all dependencies 126e54df42dSdrh ** and that minimize the overall cost. 127e54df42dSdrh */ 128e54df42dSdrh struct WhereLoop { 129e54df42dSdrh Bitmask prereq; /* Bitmask of other loops that must run first */ 130e54df42dSdrh Bitmask maskSelf; /* Bitmask identifying table iTab */ 131e54df42dSdrh #ifdef SQLITE_DEBUG 132e54df42dSdrh char cId; /* Symbolic ID of this loop for debugging use */ 133e54df42dSdrh #endif 134e54df42dSdrh u8 iTab; /* Position in FROM clause of table for this loop */ 135e54df42dSdrh u8 iSortIdx; /* Sorting index number. 0==None */ 136e54df42dSdrh LogEst rSetup; /* One-time setup cost (ex: create transient index) */ 137e54df42dSdrh LogEst rRun; /* Cost of running each loop */ 138e54df42dSdrh LogEst nOut; /* Estimated number of output rows */ 139e54df42dSdrh union { 140e54df42dSdrh struct { /* Information for internal btree tables */ 1415e6790cbSdrh u16 nEq; /* Number of equality constraints */ 14271c57db0Sdan u16 nBtm; /* Size of BTM vector */ 14371c57db0Sdan u16 nTop; /* Size of TOP vector */ 144a79a0e73Sdan u16 nDistinctCol; /* Index columns used to sort for DISTINCT */ 145e54df42dSdrh Index *pIndex; /* Index used, or NULL */ 146e54df42dSdrh } btree; 147e54df42dSdrh struct { /* Information for virtual tables */ 148e54df42dSdrh int idxNum; /* Index number */ 14969b0ce33Sdrh u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */ 15069b0ce33Sdrh u32 bOmitOffset : 1; /* True to let virtual table handle offset */ 1510401acecSdrh i8 isOrdered; /* True if satisfies ORDER BY */ 152e54df42dSdrh u16 omitMask; /* Terms that may be omitted */ 153e54df42dSdrh char *idxStr; /* Index identifier string */ 1540fe7e7d9Sdrh u32 mHandleIn; /* Terms to handle as IN(...) instead of == */ 155e54df42dSdrh } vtab; 156e54df42dSdrh } u; 157e54df42dSdrh u32 wsFlags; /* WHERE_* flags describing the plan */ 158e54df42dSdrh u16 nLTerm; /* Number of entries in aLTerm[] */ 159c8bbce1eSdrh u16 nSkip; /* Number of NULL aLTerm[] entries */ 160e54df42dSdrh /**** whereLoopXfer() copies fields above ***********************/ 161e54df42dSdrh # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) 162e54df42dSdrh u16 nLSlot; /* Number of slots allocated for aLTerm[] */ 163e54df42dSdrh WhereTerm **aLTerm; /* WhereTerms used */ 164e54df42dSdrh WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ 165c8bbce1eSdrh WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ 166e54df42dSdrh }; 167e54df42dSdrh 168e54df42dSdrh /* This object holds the prerequisites and the cost of running a 169e54df42dSdrh ** subquery on one operand of an OR operator in the WHERE clause. 170e54df42dSdrh ** See WhereOrSet for additional information 171e54df42dSdrh */ 172e54df42dSdrh struct WhereOrCost { 173e54df42dSdrh Bitmask prereq; /* Prerequisites */ 174e54df42dSdrh LogEst rRun; /* Cost of running this subquery */ 175e54df42dSdrh LogEst nOut; /* Number of outputs for this subquery */ 176e54df42dSdrh }; 177e54df42dSdrh 178e54df42dSdrh /* The WhereOrSet object holds a set of possible WhereOrCosts that 179e54df42dSdrh ** correspond to the subquery(s) of OR-clause processing. Only the 180e54df42dSdrh ** best N_OR_COST elements are retained. 181e54df42dSdrh */ 182e54df42dSdrh #define N_OR_COST 3 183e54df42dSdrh struct WhereOrSet { 184e54df42dSdrh u16 n; /* Number of valid a[] entries */ 185e54df42dSdrh WhereOrCost a[N_OR_COST]; /* Set of best costs */ 186e54df42dSdrh }; 187e54df42dSdrh 188e54df42dSdrh /* 189e54df42dSdrh ** Each instance of this object holds a sequence of WhereLoop objects 190e54df42dSdrh ** that implement some or all of a query plan. 191e54df42dSdrh ** 192e54df42dSdrh ** Think of each WhereLoop object as a node in a graph with arcs 193e54df42dSdrh ** showing dependencies and costs for travelling between nodes. (That is 194e54df42dSdrh ** not a completely accurate description because WhereLoop costs are a 195e54df42dSdrh ** vector, not a scalar, and because dependencies are many-to-one, not 196e54df42dSdrh ** one-to-one as are graph nodes. But it is a useful visualization aid.) 197e54df42dSdrh ** Then a WherePath object is a path through the graph that visits some 198e54df42dSdrh ** or all of the WhereLoop objects once. 199e54df42dSdrh ** 200e54df42dSdrh ** The "solver" works by creating the N best WherePath objects of length 201e54df42dSdrh ** 1. Then using those as a basis to compute the N best WherePath objects 202e54df42dSdrh ** of length 2. And so forth until the length of WherePaths equals the 203e54df42dSdrh ** number of nodes in the FROM clause. The best (lowest cost) WherePath 20460ec914cSpeter.d.reid ** at the end is the chosen query plan. 205e54df42dSdrh */ 206e54df42dSdrh struct WherePath { 207e54df42dSdrh Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ 208e54df42dSdrh Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ 209e54df42dSdrh LogEst nRow; /* Estimated number of rows generated by this path */ 210e54df42dSdrh LogEst rCost; /* Total cost of this path */ 21150ae31e6Sdan LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */ 2120401acecSdrh i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */ 213e54df42dSdrh WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ 214e54df42dSdrh }; 215e54df42dSdrh 216e54df42dSdrh /* 217e54df42dSdrh ** The query generator uses an array of instances of this structure to 218e54df42dSdrh ** help it analyze the subexpressions of the WHERE clause. Each WHERE 219e54df42dSdrh ** clause subexpression is separated from the others by AND operators, 220e54df42dSdrh ** usually, or sometimes subexpressions separated by OR. 221e54df42dSdrh ** 222e54df42dSdrh ** All WhereTerms are collected into a single WhereClause structure. 223e54df42dSdrh ** The following identity holds: 224e54df42dSdrh ** 225e54df42dSdrh ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm 226e54df42dSdrh ** 227e54df42dSdrh ** When a term is of the form: 228e54df42dSdrh ** 229e54df42dSdrh ** X <op> <expr> 230e54df42dSdrh ** 231e54df42dSdrh ** where X is a column name and <op> is one of certain operators, 232e54df42dSdrh ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the 233e54df42dSdrh ** cursor number and column number for X. WhereTerm.eOperator records 234e54df42dSdrh ** the <op> using a bitmask encoding defined by WO_xxx below. The 235e54df42dSdrh ** use of a bitmask encoding for the operator allows us to search 236e54df42dSdrh ** quickly for terms that match any of several different operators. 237e54df42dSdrh ** 238e54df42dSdrh ** A WhereTerm might also be two or more subterms connected by OR: 239e54df42dSdrh ** 240e54df42dSdrh ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... 241e54df42dSdrh ** 242e54df42dSdrh ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR 243e54df42dSdrh ** and the WhereTerm.u.pOrInfo field points to auxiliary information that 244e54df42dSdrh ** is collected about the OR clause. 245e54df42dSdrh ** 246e54df42dSdrh ** If a term in the WHERE clause does not match either of the two previous 247e54df42dSdrh ** categories, then eOperator==0. The WhereTerm.pExpr field is still set 248e54df42dSdrh ** to the original subexpression content and wtFlags is set up appropriately 249e54df42dSdrh ** but no other fields in the WhereTerm object are meaningful. 250e54df42dSdrh ** 251e54df42dSdrh ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, 252e54df42dSdrh ** but they do so indirectly. A single WhereMaskSet structure translates 253e54df42dSdrh ** cursor number into bits and the translated bit is stored in the prereq 254e54df42dSdrh ** fields. The translation is used in order to maximize the number of 255e54df42dSdrh ** bits that will fit in a Bitmask. The VDBE cursor numbers might be 256e54df42dSdrh ** spread out over the non-negative integers. For example, the cursor 257e54df42dSdrh ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet 258e54df42dSdrh ** translates these sparse cursor numbers into consecutive integers 259e54df42dSdrh ** beginning with 0 in order to make the best possible use of the available 260e54df42dSdrh ** bits in the Bitmask. So, in the example above, the cursor numbers 261e54df42dSdrh ** would be mapped into integers 0 through 7. 262e54df42dSdrh ** 263e54df42dSdrh ** The number of terms in a join is limited by the number of bits 264e54df42dSdrh ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite 265e54df42dSdrh ** is only able to process joins with 64 or fewer tables. 266e54df42dSdrh */ 267e54df42dSdrh struct WhereTerm { 268e54df42dSdrh Expr *pExpr; /* Pointer to the subexpression that is this term */ 26987c05f0cSdrh WhereClause *pWC; /* The clause this term is part of */ 27087c05f0cSdrh LogEst truthProb; /* Probability of truth for this expression */ 27187c05f0cSdrh u16 wtFlags; /* TERM_xxx bit flags. See below */ 27287c05f0cSdrh u16 eOperator; /* A WO_xx value describing <op> */ 27387c05f0cSdrh u8 nChild; /* Number of children that must disable us */ 27487c05f0cSdrh u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */ 275e54df42dSdrh int iParent; /* Disable pWC->a[iParent] when this term disabled */ 276e54df42dSdrh int leftCursor; /* Cursor number of X in "X <op> <expr>" */ 277e54df42dSdrh union { 27875fa2663Sdrh struct { 279e54df42dSdrh int leftColumn; /* Column number of X in "X <op> <expr>" */ 28075fa2663Sdrh int iField; /* Field in (?,?,?) IN (SELECT...) vector */ 28175fa2663Sdrh } x; /* Opcode other than OP_OR or OP_AND */ 282e54df42dSdrh WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ 283e54df42dSdrh WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ 284e54df42dSdrh } u; 285e54df42dSdrh Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ 286e54df42dSdrh Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ 287e54df42dSdrh }; 288e54df42dSdrh 289e54df42dSdrh /* 290e54df42dSdrh ** Allowed values of WhereTerm.wtFlags 291e54df42dSdrh */ 2927d07a5f4Sdrh #define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */ 2937d07a5f4Sdrh #define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */ 2947d07a5f4Sdrh #define TERM_CODED 0x0004 /* This term is already coded */ 2957d07a5f4Sdrh #define TERM_COPIED 0x0008 /* Has a child */ 2967d07a5f4Sdrh #define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */ 2977d07a5f4Sdrh #define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */ 2988a95d3d4Sdrh #define TERM_OK 0x0040 /* Used during OR-clause processing */ 2997d07a5f4Sdrh #define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */ 3007d07a5f4Sdrh #define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */ 3017d07a5f4Sdrh #define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */ 3027d07a5f4Sdrh #define TERM_LIKE 0x0400 /* The original LIKE operator */ 3037d07a5f4Sdrh #define TERM_IS 0x0800 /* Term.pExpr is an IS operator */ 304d3930b12Sdan #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */ 30589efac94Sdrh #define TERM_HEURTRUTH 0x2000 /* Heuristic truthProb used */ 306f06cdde2Sdrh #ifdef SQLITE_ENABLE_STAT4 307f06cdde2Sdrh # define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */ 308f06cdde2Sdrh #else 309f06cdde2Sdrh # define TERM_HIGHTRUTH 0 /* Only used with STAT4 */ 310f06cdde2Sdrh #endif 31102e3e041Sdrh #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */ 312e54df42dSdrh 313e54df42dSdrh /* 314e54df42dSdrh ** An instance of the WhereScan object is used as an iterator for locating 315e54df42dSdrh ** terms in the WHERE clause that are useful to the query planner. 316e54df42dSdrh */ 317e54df42dSdrh struct WhereScan { 318e54df42dSdrh WhereClause *pOrigWC; /* Original, innermost WhereClause */ 319e54df42dSdrh WhereClause *pWC; /* WhereClause currently being scanned */ 320f19aa5faSdrh const char *zCollName; /* Required collating sequence, if not NULL */ 3216860e6faSdrh Expr *pIdxExpr; /* Search for this index expression */ 322e54df42dSdrh int k; /* Resume scanning at this->pWC->a[this->k] */ 323dae2a109Sdrh u32 opMask; /* Acceptable operators */ 324dae2a109Sdrh char idxaff; /* Must match this affinity, if zCollName!=NULL */ 325dae2a109Sdrh unsigned char iEquiv; /* Current slot in aiCur[] and aiColumn[] */ 326dae2a109Sdrh unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */ 327a3f108e9Sdrh int aiCur[11]; /* Cursors in the equivalence class */ 328a3f108e9Sdrh i16 aiColumn[11]; /* Corresponding column number in the eq-class */ 329e54df42dSdrh }; 330e54df42dSdrh 331e54df42dSdrh /* 332e54df42dSdrh ** An instance of the following structure holds all information about a 333e54df42dSdrh ** WHERE clause. Mostly this is a container for one or more WhereTerms. 334e54df42dSdrh ** 335e54df42dSdrh ** Explanation of pOuter: For a WHERE clause of the form 336e54df42dSdrh ** 337e54df42dSdrh ** a AND ((b AND c) OR (d AND e)) AND f 338e54df42dSdrh ** 339e54df42dSdrh ** There are separate WhereClause objects for the whole clause and for 340e54df42dSdrh ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the 341e54df42dSdrh ** subclauses points to the WhereClause object for the whole clause. 342e54df42dSdrh */ 343e54df42dSdrh struct WhereClause { 344e54df42dSdrh WhereInfo *pWInfo; /* WHERE clause processing context */ 345e54df42dSdrh WhereClause *pOuter; /* Outer conjunction */ 346e54df42dSdrh u8 op; /* Split operator. TK_AND or TK_OR */ 347da230bd4Sdrh u8 hasOr; /* True if any a[].eOperator is WO_OR */ 348e54df42dSdrh int nTerm; /* Number of terms */ 349e54df42dSdrh int nSlot; /* Number of entries in a[] */ 350132f96fcSdrh int nBase; /* Number of terms through the last non-Virtual */ 351e54df42dSdrh WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ 352e54df42dSdrh #if defined(SQLITE_SMALL_STACK) 353e54df42dSdrh WhereTerm aStatic[1]; /* Initial static space for a[] */ 354e54df42dSdrh #else 355e54df42dSdrh WhereTerm aStatic[8]; /* Initial static space for a[] */ 356e54df42dSdrh #endif 357e54df42dSdrh }; 358e54df42dSdrh 359e54df42dSdrh /* 360e54df42dSdrh ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to 361e54df42dSdrh ** a dynamically allocated instance of the following structure. 362e54df42dSdrh */ 363e54df42dSdrh struct WhereOrInfo { 364e54df42dSdrh WhereClause wc; /* Decomposition into subterms */ 365e54df42dSdrh Bitmask indexable; /* Bitmask of all indexable tables in the clause */ 366e54df42dSdrh }; 367e54df42dSdrh 368e54df42dSdrh /* 369e54df42dSdrh ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to 370e54df42dSdrh ** a dynamically allocated instance of the following structure. 371e54df42dSdrh */ 372e54df42dSdrh struct WhereAndInfo { 373e54df42dSdrh WhereClause wc; /* The subexpression broken out */ 374e54df42dSdrh }; 375e54df42dSdrh 376e54df42dSdrh /* 377e54df42dSdrh ** An instance of the following structure keeps track of a mapping 378e54df42dSdrh ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. 379e54df42dSdrh ** 380e54df42dSdrh ** The VDBE cursor numbers are small integers contained in 3813b88065dSdrh ** SrcItem.iCursor and Expr.iTable fields. For any given WHERE 382e54df42dSdrh ** clause, the cursor numbers might not begin with 0 and they might 383e54df42dSdrh ** contain gaps in the numbering sequence. But we want to make maximum 384e54df42dSdrh ** use of the bits in our bitmasks. This structure provides a mapping 385e54df42dSdrh ** from the sparse cursor numbers into consecutive integers beginning 386e54df42dSdrh ** with 0. 387e54df42dSdrh ** 388e54df42dSdrh ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask 389e54df42dSdrh ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. 390e54df42dSdrh ** 391e54df42dSdrh ** For example, if the WHERE clause expression used these VDBE 392e54df42dSdrh ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure 393e54df42dSdrh ** would map those cursor numbers into bits 0 through 5. 394e54df42dSdrh ** 395e54df42dSdrh ** Note that the mapping is not necessarily ordered. In the example 396e54df42dSdrh ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, 397e54df42dSdrh ** 57->5, 73->4. Or one of 719 other combinations might be used. It 398e54df42dSdrh ** does not really matter. What is important is that sparse cursor 399e54df42dSdrh ** numbers all get mapped into bit numbers that begin with 0 and contain 400e54df42dSdrh ** no gaps. 401e54df42dSdrh */ 402e54df42dSdrh struct WhereMaskSet { 403d3930b12Sdan int bVarSelect; /* Used by sqlite3WhereExprUsage() */ 404e54df42dSdrh int n; /* Number of assigned cursor values */ 405e54df42dSdrh int ix[BMS]; /* Cursor assigned to each bit */ 406e54df42dSdrh }; 407e54df42dSdrh 408e54df42dSdrh /* 409e54df42dSdrh ** This object is a convenience wrapper holding all information needed 410e54df42dSdrh ** to construct WhereLoop objects for a particular query. 411e54df42dSdrh */ 412e54df42dSdrh struct WhereLoopBuilder { 413e54df42dSdrh WhereInfo *pWInfo; /* Information about this WHERE */ 414e54df42dSdrh WhereClause *pWC; /* WHERE clause terms */ 415e54df42dSdrh WhereLoop *pNew; /* Template WhereLoop */ 416e54df42dSdrh WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ 417175b8f06Sdrh #ifdef SQLITE_ENABLE_STAT4 418e54df42dSdrh UnpackedRecord *pRec; /* Probe for stat4 (if required) */ 419e54df42dSdrh int nRecValid; /* Number of valid fields currently in pRec */ 420e54df42dSdrh #endif 42189efac94Sdrh unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */ 42289efac94Sdrh unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */ 423fc9098a4Sdrh unsigned int iPlanLimit; /* Search limiter */ 424e54df42dSdrh }; 425e54df42dSdrh 426a3928dd7Sdrh /* Allowed values for WhereLoopBuider.bldFlags */ 42789efac94Sdrh #define SQLITE_BLDF1_INDEXED 0x0001 /* An index is used */ 42889efac94Sdrh #define SQLITE_BLDF1_UNIQUE 0x0002 /* All keys of a UNIQUE index used */ 42989efac94Sdrh 43089efac94Sdrh #define SQLITE_BLDF2_2NDPASS 0x0004 /* Second builder pass needed */ 431a3928dd7Sdrh 4326fb5d358Sdrh /* The WhereLoopBuilder.iPlanLimit is used to limit the number of 4336fb5d358Sdrh ** index+constraint combinations the query planner will consider for a 4346fb5d358Sdrh ** particular query. If this parameter is unlimited, then certain 4356fb5d358Sdrh ** pathological queries can spend excess time in the sqlite3WhereBegin() 4366fb5d358Sdrh ** routine. The limit is high enough that is should not impact real-world 4376fb5d358Sdrh ** queries. 4386fb5d358Sdrh ** 4396fb5d358Sdrh ** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is 4406fb5d358Sdrh ** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM 4416fb5d358Sdrh ** clause is processed, so that every table in a join is guaranteed to be 4426fb5d358Sdrh ** able to propose a some index+constraint combinations even if the initial 4436fb5d358Sdrh ** baseline limit was exhausted by prior tables of the join. 4446fb5d358Sdrh */ 4456fb5d358Sdrh #ifndef SQLITE_QUERY_PLANNER_LIMIT 4466fb5d358Sdrh # define SQLITE_QUERY_PLANNER_LIMIT 20000 4476fb5d358Sdrh #endif 4486fb5d358Sdrh #ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR 4496fb5d358Sdrh # define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000 4506fb5d358Sdrh #endif 4516fb5d358Sdrh 452e54df42dSdrh /* 453e54df42dSdrh ** The WHERE clause processing routine has two halves. The 454e54df42dSdrh ** first part does the start of the WHERE loop and the second 455e54df42dSdrh ** half does the tail of the WHERE loop. An instance of 456e54df42dSdrh ** this structure is returned by the first half and passed 457e54df42dSdrh ** into the second half to give some continuity. 458e54df42dSdrh ** 459e54df42dSdrh ** An instance of this object holds the complete state of the query 460e54df42dSdrh ** planner. 461e54df42dSdrh */ 462e54df42dSdrh struct WhereInfo { 463e54df42dSdrh Parse *pParse; /* Parsing and code generating context */ 464e54df42dSdrh SrcList *pTabList; /* List of tables in the join */ 465e54df42dSdrh ExprList *pOrderBy; /* The ORDER BY clause or NULL */ 466e9ba910fSdrh ExprList *pResultSet; /* Result set of the query */ 46763b3a64cSdrh #if WHERETRACE_ENABLED 468aca19e19Sdrh Expr *pWhere; /* The complete WHERE clause */ 46963b3a64cSdrh #endif 470*f55a7dadSdrh Select *pSelect; /* The entire SELECT statement containing WHERE */ 47187c05f0cSdrh int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ 47287c05f0cSdrh int iContinue; /* Jump here to continue with next record */ 47387c05f0cSdrh int iBreak; /* Jump here to break out of the loop */ 47487c05f0cSdrh int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ 475e54df42dSdrh u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ 47636e678bcSdrh LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */ 47787c05f0cSdrh u8 nLevel; /* Number of nested loop */ 478ddba0c22Sdrh i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */ 479b0264eecSdrh u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */ 480a536df4eSdrh u8 eDistinct; /* One of the WHERE_DISTINCT_* values */ 48117aceebaSdrh unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */ 48217aceebaSdrh unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */ 48317aceebaSdrh unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */ 48417aceebaSdrh unsigned sorted :1; /* True if really sorted (not just grouped) */ 48536e678bcSdrh LogEst nRowOut; /* Estimated number of output rows */ 486e54df42dSdrh int iTop; /* The very beginning of the WHERE loop */ 4875e6d90feSdrh int iEndWhere; /* End of the WHERE clause itself */ 48887c05f0cSdrh WhereLoop *pLoops; /* List of all WhereLoop objects */ 489f8bdcfa3Sdrh WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */ 49087c05f0cSdrh Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ 491e54df42dSdrh WhereClause sWC; /* Decomposition of the WHERE clause */ 49287c05f0cSdrh WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ 493e54df42dSdrh WhereLevel a[1]; /* Information about each nest loop in WHERE */ 494e54df42dSdrh }; 495e54df42dSdrh 496e54df42dSdrh /* 4976f82e85aSdrh ** Private interfaces - callable only by other where.c routines. 4986c1f4ef2Sdrh ** 4996c1f4ef2Sdrh ** where.c: 5006f82e85aSdrh */ 5016f82e85aSdrh Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); 502c84a4020Sdrh #ifdef WHERETRACE_ENABLED 503c84a4020Sdrh void sqlite3WhereClausePrint(WhereClause *pWC); 504cacdf207Sdrh void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm); 505cacdf207Sdrh void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC); 506c84a4020Sdrh #endif 5076f82e85aSdrh WhereTerm *sqlite3WhereFindTerm( 5086f82e85aSdrh WhereClause *pWC, /* The WHERE clause to be searched */ 5096f82e85aSdrh int iCur, /* Cursor number of LHS */ 5106f82e85aSdrh int iColumn, /* Column number of LHS */ 5116f82e85aSdrh Bitmask notReady, /* RHS must not overlap with this mask */ 5126f82e85aSdrh u32 op, /* Mask of WO_xx values describing operator */ 5136f82e85aSdrh Index *pIdx /* Must be compatible with this index, if not NULL */ 5146f82e85aSdrh ); 515f8bdcfa3Sdrh void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte); 516f8bdcfa3Sdrh void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte); 5176c1f4ef2Sdrh 5186c1f4ef2Sdrh /* wherecode.c: */ 5196f82e85aSdrh #ifndef SQLITE_OMIT_EXPLAIN 5206f82e85aSdrh int sqlite3WhereExplainOneScan( 5216f82e85aSdrh Parse *pParse, /* Parse context */ 5226f82e85aSdrh SrcList *pTabList, /* Table list this loop refers to */ 5236f82e85aSdrh WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ 5246f82e85aSdrh u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ 5256f82e85aSdrh ); 5266ae49e67Sdrh int sqlite3WhereExplainBloomFilter( 5276ae49e67Sdrh const Parse *pParse, /* Parse context */ 5286ae49e67Sdrh const WhereInfo *pWInfo, /* WHERE clause */ 5296ae49e67Sdrh const WhereLevel *pLevel /* Bloom filter on this level */ 5306ae49e67Sdrh ); 5316f82e85aSdrh #else 532e2188f0bSdrh # define sqlite3WhereExplainOneScan(u,v,w,x) 0 5336ae49e67Sdrh # define sqlite3WhereExplainBloomFilter(u,v,w) 0 5346f82e85aSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 5356f82e85aSdrh #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 5366f82e85aSdrh void sqlite3WhereAddScanStatus( 5376f82e85aSdrh Vdbe *v, /* Vdbe to add scanstatus entry to */ 5386f82e85aSdrh SrcList *pSrclist, /* FROM clause pLvl reads data from */ 5396f82e85aSdrh WhereLevel *pLvl, /* Level to add scanstatus() entry for */ 5406f82e85aSdrh int addrExplain /* Address of OP_Explain (or 0) */ 5416f82e85aSdrh ); 5426f82e85aSdrh #else 5436f82e85aSdrh # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d) 5446f82e85aSdrh #endif 5456f82e85aSdrh Bitmask sqlite3WhereCodeOneLoopStart( 54647df8a2cSdrh Parse *pParse, /* Parsing context */ 54747df8a2cSdrh Vdbe *v, /* Prepared statement under construction */ 5486f82e85aSdrh WhereInfo *pWInfo, /* Complete information about the WHERE clause */ 5496f82e85aSdrh int iLevel, /* Which level of pWInfo->a[] should be coded */ 55047df8a2cSdrh WhereLevel *pLevel, /* The current level pointer */ 5516f82e85aSdrh Bitmask notReady /* Which tables are currently available */ 5526f82e85aSdrh ); 553949e2ab4Sdrh SQLITE_NOINLINE void sqlite3WhereRightJoinLoop( 554949e2ab4Sdrh WhereInfo *pWInfo, 555949e2ab4Sdrh int iLevel, 556949e2ab4Sdrh WhereLevel *pLevel 557949e2ab4Sdrh ); 5586f82e85aSdrh 5596c1f4ef2Sdrh /* whereexpr.c: */ 5606c1f4ef2Sdrh void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); 5616c1f4ef2Sdrh void sqlite3WhereClauseClear(WhereClause*); 5626c1f4ef2Sdrh void sqlite3WhereSplit(WhereClause*,Expr*,u8); 563895bab33Sdrh void sqlite3WhereAddLimit(WhereClause*, Select*); 5646c1f4ef2Sdrh Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); 565ccf6db5eSdrh Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*); 5666c1f4ef2Sdrh Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); 5676c1f4ef2Sdrh void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); 5687601294aSdrh void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*); 5696f82e85aSdrh 5706f82e85aSdrh 5716f82e85aSdrh 5726f82e85aSdrh 5736f82e85aSdrh 5746f82e85aSdrh /* 575e54df42dSdrh ** Bitmasks for the operators on WhereTerm objects. These are all 576e54df42dSdrh ** operators that are of interest to the query planner. An 577e54df42dSdrh ** OR-ed combination of these values can be used when searching for 578e54df42dSdrh ** particular WhereTerms within a WhereClause. 57949711600Sdrh ** 58049711600Sdrh ** Value constraints: 58149711600Sdrh ** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ 58249711600Sdrh ** WO_LT == SQLITE_INDEX_CONSTRAINT_LT 58349711600Sdrh ** WO_LE == SQLITE_INDEX_CONSTRAINT_LE 58449711600Sdrh ** WO_GT == SQLITE_INDEX_CONSTRAINT_GT 58549711600Sdrh ** WO_GE == SQLITE_INDEX_CONSTRAINT_GE 586e54df42dSdrh */ 587e8d0c61fSdrh #define WO_IN 0x0001 588e8d0c61fSdrh #define WO_EQ 0x0002 589e54df42dSdrh #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) 590e54df42dSdrh #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) 591e54df42dSdrh #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) 592e54df42dSdrh #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) 593303a69b5Sdrh #define WO_AUX 0x0040 /* Op useful to virtual tables only */ 594e8d0c61fSdrh #define WO_IS 0x0080 595e8d0c61fSdrh #define WO_ISNULL 0x0100 596e8d0c61fSdrh #define WO_OR 0x0200 /* Two or more OR-connected terms */ 597e8d0c61fSdrh #define WO_AND 0x0400 /* Two or more AND-connected terms */ 598e8d0c61fSdrh #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */ 599e8d0c61fSdrh #define WO_NOOP 0x1000 /* This term does not restrict search space */ 6000f4b534bSdrh #define WO_ROWVAL 0x2000 /* A row-value term */ 601e54df42dSdrh 6020f4b534bSdrh #define WO_ALL 0x3fff /* Mask of all possible WO_* values */ 603e8d0c61fSdrh #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */ 604e54df42dSdrh 605e54df42dSdrh /* 606e54df42dSdrh ** These are definitions of bits in the WhereLoop.wsFlags field. 607e54df42dSdrh ** The particular combination of bits in each WhereLoop help to 608e54df42dSdrh ** determine the algorithm that WhereLoop represents. 609e54df42dSdrh */ 610e54df42dSdrh #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ 611e54df42dSdrh #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ 612e54df42dSdrh #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ 613e54df42dSdrh #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ 614e54df42dSdrh #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ 615e54df42dSdrh #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ 616e54df42dSdrh #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ 617e54df42dSdrh #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ 618e54df42dSdrh #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ 619e54df42dSdrh #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ 620e54df42dSdrh #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ 621e54df42dSdrh #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ 622e54df42dSdrh #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ 623e54df42dSdrh #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ 624e54df42dSdrh #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ 625e54df42dSdrh #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ 6262e5ef4edSdrh #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ 627e39a732cSdrh #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ 628051575cbSdrh #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ 629f7b0a5f3Sdrh #define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */ 63015750a26Sdan #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */ 63168cf0aceSdrh #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ 63267656ac7Sdrh #define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ 633fa35f5c5Sdrh #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */ 6347e910f64Sdrh #define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */ 6358f2c0b59Sdrh #define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */ 636a3fc683cSdrh #define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */ 637d9bc6e89Smistachkin 638d9bc6e89Smistachkin #endif /* !defined(SQLITE_WHEREINT_H) */ 639