OLD | NEW |
1 /* | 1 /* |
2 ** 2013-11-12 | 2 ** 2013-11-12 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** | 12 ** |
13 ** This file contains structure and macro definitions for the query | 13 ** This file contains structure and macro definitions for the query |
14 ** planner logic in "where.c". These definitions are broken out into | 14 ** planner logic in "where.c". These definitions are broken out into |
15 ** a separate source file for easier editing. | 15 ** a separate source file for easier editing. |
16 */ | 16 */ |
17 | 17 |
18 /* | 18 /* |
19 ** Trace output macros | 19 ** Trace output macros |
20 */ | 20 */ |
21 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) | 21 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
22 /***/ int sqlite3WhereTrace = 0; | 22 /***/ int sqlite3WhereTrace; |
23 #endif | 23 #endif |
24 #if defined(SQLITE_DEBUG) \ | 24 #if defined(SQLITE_DEBUG) \ |
25 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) | 25 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
26 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X | 26 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
27 # define WHERETRACE_ENABLED 1 | 27 # define WHERETRACE_ENABLED 1 |
28 #else | 28 #else |
29 # define WHERETRACE(K,X) | 29 # define WHERETRACE(K,X) |
30 #endif | 30 #endif |
31 | 31 |
32 /* Forward references | 32 /* Forward references |
(...skipping 29 matching lines...) Expand all Loading... |
62 struct WhereLevel { | 62 struct WhereLevel { |
63 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ | 63 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
64 int iTabCur; /* The VDBE cursor used to access the table */ | 64 int iTabCur; /* The VDBE cursor used to access the table */ |
65 int iIdxCur; /* The VDBE cursor used to access pIdx */ | 65 int iIdxCur; /* The VDBE cursor used to access pIdx */ |
66 int addrBrk; /* Jump here to break out of the loop */ | 66 int addrBrk; /* Jump here to break out of the loop */ |
67 int addrNxt; /* Jump here to start the next IN combination */ | 67 int addrNxt; /* Jump here to start the next IN combination */ |
68 int addrSkip; /* Jump here for next iteration of skip-scan */ | 68 int addrSkip; /* Jump here for next iteration of skip-scan */ |
69 int addrCont; /* Jump here to continue with the next loop cycle */ | 69 int addrCont; /* Jump here to continue with the next loop cycle */ |
70 int addrFirst; /* First instruction of interior of the loop */ | 70 int addrFirst; /* First instruction of interior of the loop */ |
71 int addrBody; /* Beginning of the body of this loop */ | 71 int addrBody; /* Beginning of the body of this loop */ |
| 72 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 73 int iLikeRepCntr; /* LIKE range processing counter register */ |
| 74 int addrLikeRep; /* LIKE range processing address */ |
| 75 #endif |
72 u8 iFrom; /* Which entry in the FROM clause */ | 76 u8 iFrom; /* Which entry in the FROM clause */ |
73 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ | 77 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ |
74 int p1, p2; /* Operands of the opcode used to ends the loop */ | 78 int p1, p2; /* Operands of the opcode used to ends the loop */ |
75 union { /* Information that depends on pWLoop->wsFlags */ | 79 union { /* Information that depends on pWLoop->wsFlags */ |
76 struct { | 80 struct { |
77 int nIn; /* Number of entries in aInLoop[] */ | 81 int nIn; /* Number of entries in aInLoop[] */ |
78 struct InLoop { | 82 struct InLoop { |
79 int iCur; /* The VDBE cursor used by this IN operator */ | 83 int iCur; /* The VDBE cursor used by this IN operator */ |
80 int addrInTop; /* Top of the IN loop */ | 84 int addrInTop; /* Top of the IN loop */ |
81 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ | 85 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
82 } *aInLoop; /* Information about each nested IN operator */ | 86 } *aInLoop; /* Information about each nested IN operator */ |
83 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ | 87 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
84 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ | 88 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
85 } u; | 89 } u; |
86 struct WhereLoop *pWLoop; /* The selected WhereLoop object */ | 90 struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
87 Bitmask notReady; /* FROM entries not usable at this level */ | 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 |
88 }; | 95 }; |
89 | 96 |
90 /* | 97 /* |
91 ** Each instance of this object represents an algorithm for evaluating one | 98 ** Each instance of this object represents an algorithm for evaluating one |
92 ** term of a join. Every term of the FROM clause will have at least | 99 ** term of a join. Every term of the FROM clause will have at least |
93 ** one corresponding WhereLoop object (unless INDEXED BY constraints | 100 ** one corresponding WhereLoop object (unless INDEXED BY constraints |
94 ** prevent a query solution - which is an error) and many terms of the | 101 ** prevent a query solution - which is an error) and many terms of the |
95 ** FROM clause will have multiple WhereLoop objects, each describing a | 102 ** FROM clause will have multiple WhereLoop objects, each describing a |
96 ** potential way of implementing that FROM-clause term, together with | 103 ** potential way of implementing that FROM-clause term, together with |
97 ** dependencies and cost estimates for using the chosen algorithm. | 104 ** dependencies and cost estimates for using the chosen algorithm. |
(...skipping 10 matching lines...) Expand all Loading... |
108 char cId; /* Symbolic ID of this loop for debugging use */ | 115 char cId; /* Symbolic ID of this loop for debugging use */ |
109 #endif | 116 #endif |
110 u8 iTab; /* Position in FROM clause of table for this loop */ | 117 u8 iTab; /* Position in FROM clause of table for this loop */ |
111 u8 iSortIdx; /* Sorting index number. 0==None */ | 118 u8 iSortIdx; /* Sorting index number. 0==None */ |
112 LogEst rSetup; /* One-time setup cost (ex: create transient index) */ | 119 LogEst rSetup; /* One-time setup cost (ex: create transient index) */ |
113 LogEst rRun; /* Cost of running each loop */ | 120 LogEst rRun; /* Cost of running each loop */ |
114 LogEst nOut; /* Estimated number of output rows */ | 121 LogEst nOut; /* Estimated number of output rows */ |
115 union { | 122 union { |
116 struct { /* Information for internal btree tables */ | 123 struct { /* Information for internal btree tables */ |
117 u16 nEq; /* Number of equality constraints */ | 124 u16 nEq; /* Number of equality constraints */ |
118 u16 nSkip; /* Number of initial index columns to skip */ | |
119 Index *pIndex; /* Index used, or NULL */ | 125 Index *pIndex; /* Index used, or NULL */ |
120 } btree; | 126 } btree; |
121 struct { /* Information for virtual tables */ | 127 struct { /* Information for virtual tables */ |
122 int idxNum; /* Index number */ | 128 int idxNum; /* Index number */ |
123 u8 needFree; /* True if sqlite3_free(idxStr) is needed */ | 129 u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
124 i8 isOrdered; /* True if satisfies ORDER BY */ | 130 i8 isOrdered; /* True if satisfies ORDER BY */ |
125 u16 omitMask; /* Terms that may be omitted */ | 131 u16 omitMask; /* Terms that may be omitted */ |
126 char *idxStr; /* Index identifier string */ | 132 char *idxStr; /* Index identifier string */ |
127 } vtab; | 133 } vtab; |
128 } u; | 134 } u; |
129 u32 wsFlags; /* WHERE_* flags describing the plan */ | 135 u32 wsFlags; /* WHERE_* flags describing the plan */ |
130 u16 nLTerm; /* Number of entries in aLTerm[] */ | 136 u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 137 u16 nSkip; /* Number of NULL aLTerm[] entries */ |
131 /**** whereLoopXfer() copies fields above ***********************/ | 138 /**** whereLoopXfer() copies fields above ***********************/ |
132 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) | 139 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
133 u16 nLSlot; /* Number of slots allocated for aLTerm[] */ | 140 u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
134 WhereTerm **aLTerm; /* WhereTerms used */ | 141 WhereTerm **aLTerm; /* WhereTerms used */ |
135 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ | 142 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
136 WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */ | 143 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ |
137 }; | 144 }; |
138 | 145 |
139 /* This object holds the prerequisites and the cost of running a | 146 /* This object holds the prerequisites and the cost of running a |
140 ** subquery on one operand of an OR operator in the WHERE clause. | 147 ** subquery on one operand of an OR operator in the WHERE clause. |
141 ** See WhereOrSet for additional information | 148 ** See WhereOrSet for additional information |
142 */ | 149 */ |
143 struct WhereOrCost { | 150 struct WhereOrCost { |
144 Bitmask prereq; /* Prerequisites */ | 151 Bitmask prereq; /* Prerequisites */ |
145 LogEst rRun; /* Cost of running this subquery */ | 152 LogEst rRun; /* Cost of running this subquery */ |
146 LogEst nOut; /* Number of outputs for this subquery */ | 153 LogEst nOut; /* Number of outputs for this subquery */ |
147 }; | 154 }; |
148 | 155 |
149 /* The WhereOrSet object holds a set of possible WhereOrCosts that | 156 /* The WhereOrSet object holds a set of possible WhereOrCosts that |
150 ** correspond to the subquery(s) of OR-clause processing. Only the | 157 ** correspond to the subquery(s) of OR-clause processing. Only the |
151 ** best N_OR_COST elements are retained. | 158 ** best N_OR_COST elements are retained. |
152 */ | 159 */ |
153 #define N_OR_COST 3 | 160 #define N_OR_COST 3 |
154 struct WhereOrSet { | 161 struct WhereOrSet { |
155 u16 n; /* Number of valid a[] entries */ | 162 u16 n; /* Number of valid a[] entries */ |
156 WhereOrCost a[N_OR_COST]; /* Set of best costs */ | 163 WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
157 }; | 164 }; |
158 | 165 |
159 | |
160 /* Forward declaration of methods */ | |
161 static int whereLoopResize(sqlite3*, WhereLoop*, int); | |
162 | |
163 /* | 166 /* |
164 ** Each instance of this object holds a sequence of WhereLoop objects | 167 ** Each instance of this object holds a sequence of WhereLoop objects |
165 ** that implement some or all of a query plan. | 168 ** that implement some or all of a query plan. |
166 ** | 169 ** |
167 ** Think of each WhereLoop object as a node in a graph with arcs | 170 ** Think of each WhereLoop object as a node in a graph with arcs |
168 ** showing dependencies and costs for travelling between nodes. (That is | 171 ** showing dependencies and costs for travelling between nodes. (That is |
169 ** not a completely accurate description because WhereLoop costs are a | 172 ** not a completely accurate description because WhereLoop costs are a |
170 ** vector, not a scalar, and because dependencies are many-to-one, not | 173 ** vector, not a scalar, and because dependencies are many-to-one, not |
171 ** one-to-one as are graph nodes. But it is a useful visualization aid.) | 174 ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
172 ** Then a WherePath object is a path through the graph that visits some | 175 ** Then a WherePath object is a path through the graph that visits some |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 Expr *pExpr; /* Pointer to the subexpression that is this term */ | 246 Expr *pExpr; /* Pointer to the subexpression that is this term */ |
244 int iParent; /* Disable pWC->a[iParent] when this term disabled */ | 247 int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
245 int leftCursor; /* Cursor number of X in "X <op> <expr>" */ | 248 int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
246 union { | 249 union { |
247 int leftColumn; /* Column number of X in "X <op> <expr>" */ | 250 int leftColumn; /* Column number of X in "X <op> <expr>" */ |
248 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ | 251 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
249 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ | 252 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
250 } u; | 253 } u; |
251 LogEst truthProb; /* Probability of truth for this expression */ | 254 LogEst truthProb; /* Probability of truth for this expression */ |
252 u16 eOperator; /* A WO_xx value describing <op> */ | 255 u16 eOperator; /* A WO_xx value describing <op> */ |
253 u8 wtFlags; /* TERM_xxx bit flags. See below */ | 256 u16 wtFlags; /* TERM_xxx bit flags. See below */ |
254 u8 nChild; /* Number of children that must disable us */ | 257 u8 nChild; /* Number of children that must disable us */ |
| 258 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */ |
255 WhereClause *pWC; /* The clause this term is part of */ | 259 WhereClause *pWC; /* The clause this term is part of */ |
256 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ | 260 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
257 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ | 261 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
258 }; | 262 }; |
259 | 263 |
260 /* | 264 /* |
261 ** Allowed values of WhereTerm.wtFlags | 265 ** Allowed values of WhereTerm.wtFlags |
262 */ | 266 */ |
263 #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ | 267 #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
264 #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ | 268 #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
265 #define TERM_CODED 0x04 /* This term is already coded */ | 269 #define TERM_CODED 0x04 /* This term is already coded */ |
266 #define TERM_COPIED 0x08 /* Has a child */ | 270 #define TERM_COPIED 0x08 /* Has a child */ |
267 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ | 271 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
268 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ | 272 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
269 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ | 273 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
270 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 274 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
271 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ | 275 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
272 #else | 276 #else |
273 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ | 277 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
274 #endif | 278 #endif |
| 279 #define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */ |
| 280 #define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */ |
| 281 #define TERM_LIKE 0x400 /* The original LIKE operator */ |
| 282 #define TERM_IS 0x800 /* Term.pExpr is an IS operator */ |
275 | 283 |
276 /* | 284 /* |
277 ** An instance of the WhereScan object is used as an iterator for locating | 285 ** An instance of the WhereScan object is used as an iterator for locating |
278 ** terms in the WHERE clause that are useful to the query planner. | 286 ** terms in the WHERE clause that are useful to the query planner. |
279 */ | 287 */ |
280 struct WhereScan { | 288 struct WhereScan { |
281 WhereClause *pOrigWC; /* Original, innermost WhereClause */ | 289 WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
282 WhereClause *pWC; /* WhereClause currently being scanned */ | 290 WhereClause *pWC; /* WhereClause currently being scanned */ |
283 char *zCollName; /* Required collating sequence, if not NULL */ | 291 const char *zCollName; /* Required collating sequence, if not NULL */ |
| 292 Expr *pIdxExpr; /* Search for this index expression */ |
284 char idxaff; /* Must match this affinity, if zCollName!=NULL */ | 293 char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
285 unsigned char nEquiv; /* Number of entries in aEquiv[] */ | 294 unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
286 unsigned char iEquiv; /* Next unused slot in aEquiv[] */ | 295 unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
287 u32 opMask; /* Acceptable operators */ | 296 u32 opMask; /* Acceptable operators */ |
288 int k; /* Resume scanning at this->pWC->a[this->k] */ | 297 int k; /* Resume scanning at this->pWC->a[this->k] */ |
289 int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */ | 298 int aiCur[11]; /* Cursors in the equivalence class */ |
| 299 i16 aiColumn[11]; /* Corresponding column number in the eq-class */ |
290 }; | 300 }; |
291 | 301 |
292 /* | 302 /* |
293 ** An instance of the following structure holds all information about a | 303 ** An instance of the following structure holds all information about a |
294 ** WHERE clause. Mostly this is a container for one or more WhereTerms. | 304 ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
295 ** | 305 ** |
296 ** Explanation of pOuter: For a WHERE clause of the form | 306 ** Explanation of pOuter: For a WHERE clause of the form |
297 ** | 307 ** |
298 ** a AND ((b AND c) OR (d AND e)) AND f | 308 ** a AND ((b AND c) OR (d AND e)) AND f |
299 ** | 309 ** |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 ** does not really matter. What is important is that sparse cursor | 367 ** does not really matter. What is important is that sparse cursor |
358 ** numbers all get mapped into bit numbers that begin with 0 and contain | 368 ** numbers all get mapped into bit numbers that begin with 0 and contain |
359 ** no gaps. | 369 ** no gaps. |
360 */ | 370 */ |
361 struct WhereMaskSet { | 371 struct WhereMaskSet { |
362 int n; /* Number of assigned cursor values */ | 372 int n; /* Number of assigned cursor values */ |
363 int ix[BMS]; /* Cursor assigned to each bit */ | 373 int ix[BMS]; /* Cursor assigned to each bit */ |
364 }; | 374 }; |
365 | 375 |
366 /* | 376 /* |
| 377 ** Initialize a WhereMaskSet object |
| 378 */ |
| 379 #define initMaskSet(P) (P)->n=0 |
| 380 |
| 381 /* |
367 ** This object is a convenience wrapper holding all information needed | 382 ** This object is a convenience wrapper holding all information needed |
368 ** to construct WhereLoop objects for a particular query. | 383 ** to construct WhereLoop objects for a particular query. |
369 */ | 384 */ |
370 struct WhereLoopBuilder { | 385 struct WhereLoopBuilder { |
371 WhereInfo *pWInfo; /* Information about this WHERE */ | 386 WhereInfo *pWInfo; /* Information about this WHERE */ |
372 WhereClause *pWC; /* WHERE clause terms */ | 387 WhereClause *pWC; /* WHERE clause terms */ |
373 ExprList *pOrderBy; /* ORDER BY clause */ | 388 ExprList *pOrderBy; /* ORDER BY clause */ |
374 WhereLoop *pNew; /* Template WhereLoop */ | 389 WhereLoop *pNew; /* Template WhereLoop */ |
375 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ | 390 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
376 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 391 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
(...skipping 16 matching lines...) Expand all Loading... |
393 Parse *pParse; /* Parsing and code generating context */ | 408 Parse *pParse; /* Parsing and code generating context */ |
394 SrcList *pTabList; /* List of tables in the join */ | 409 SrcList *pTabList; /* List of tables in the join */ |
395 ExprList *pOrderBy; /* The ORDER BY clause or NULL */ | 410 ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
396 ExprList *pResultSet; /* Result set. DISTINCT operates on these */ | 411 ExprList *pResultSet; /* Result set. DISTINCT operates on these */ |
397 WhereLoop *pLoops; /* List of all WhereLoop objects */ | 412 WhereLoop *pLoops; /* List of all WhereLoop objects */ |
398 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ | 413 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
399 LogEst nRowOut; /* Estimated number of output rows */ | 414 LogEst nRowOut; /* Estimated number of output rows */ |
400 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ | 415 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
401 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */ | 416 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */ |
402 u8 sorted; /* True if really sorted (not just grouped) */ | 417 u8 sorted; /* True if really sorted (not just grouped) */ |
403 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */ | 418 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */ |
404 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ | 419 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
405 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ | 420 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */ |
406 u8 nLevel; /* Number of nested loop */ | 421 u8 nLevel; /* Number of nested loop */ |
407 int iTop; /* The very beginning of the WHERE loop */ | 422 int iTop; /* The very beginning of the WHERE loop */ |
408 int iContinue; /* Jump here to continue with next record */ | 423 int iContinue; /* Jump here to continue with next record */ |
409 int iBreak; /* Jump here to break out of the loop */ | 424 int iBreak; /* Jump here to break out of the loop */ |
410 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ | 425 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
411 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ | 426 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
412 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ | 427 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
413 WhereClause sWC; /* Decomposition of the WHERE clause */ | 428 WhereClause sWC; /* Decomposition of the WHERE clause */ |
414 WhereLevel a[1]; /* Information about each nest loop in WHERE */ | 429 WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
415 }; | 430 }; |
416 | 431 |
417 /* | 432 /* |
| 433 ** Private interfaces - callable only by other where.c routines. |
| 434 ** |
| 435 ** where.c: |
| 436 */ |
| 437 Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); |
| 438 WhereTerm *sqlite3WhereFindTerm( |
| 439 WhereClause *pWC, /* The WHERE clause to be searched */ |
| 440 int iCur, /* Cursor number of LHS */ |
| 441 int iColumn, /* Column number of LHS */ |
| 442 Bitmask notReady, /* RHS must not overlap with this mask */ |
| 443 u32 op, /* Mask of WO_xx values describing operator */ |
| 444 Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 445 ); |
| 446 |
| 447 /* wherecode.c: */ |
| 448 #ifndef SQLITE_OMIT_EXPLAIN |
| 449 int sqlite3WhereExplainOneScan( |
| 450 Parse *pParse, /* Parse context */ |
| 451 SrcList *pTabList, /* Table list this loop refers to */ |
| 452 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 453 int iLevel, /* Value for "level" column of output */ |
| 454 int iFrom, /* Value for "from" column of output */ |
| 455 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 456 ); |
| 457 #else |
| 458 # define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0 |
| 459 #endif /* SQLITE_OMIT_EXPLAIN */ |
| 460 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 461 void sqlite3WhereAddScanStatus( |
| 462 Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 463 SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 464 WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 465 int addrExplain /* Address of OP_Explain (or 0) */ |
| 466 ); |
| 467 #else |
| 468 # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d) |
| 469 #endif |
| 470 Bitmask sqlite3WhereCodeOneLoopStart( |
| 471 WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 472 int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 473 Bitmask notReady /* Which tables are currently available */ |
| 474 ); |
| 475 |
| 476 /* whereexpr.c: */ |
| 477 void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); |
| 478 void sqlite3WhereClauseClear(WhereClause*); |
| 479 void sqlite3WhereSplit(WhereClause*,Expr*,u8); |
| 480 Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
| 481 Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 482 void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
| 483 void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*); |
| 484 |
| 485 |
| 486 |
| 487 |
| 488 |
| 489 /* |
418 ** Bitmasks for the operators on WhereTerm objects. These are all | 490 ** Bitmasks for the operators on WhereTerm objects. These are all |
419 ** operators that are of interest to the query planner. An | 491 ** operators that are of interest to the query planner. An |
420 ** OR-ed combination of these values can be used when searching for | 492 ** OR-ed combination of these values can be used when searching for |
421 ** particular WhereTerms within a WhereClause. | 493 ** particular WhereTerms within a WhereClause. |
422 */ | 494 */ |
423 #define WO_IN 0x001 | 495 #define WO_IN 0x0001 |
424 #define WO_EQ 0x002 | 496 #define WO_EQ 0x0002 |
425 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) | 497 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
426 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) | 498 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
427 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) | 499 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
428 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) | 500 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
429 #define WO_MATCH 0x040 | 501 #define WO_MATCH 0x0040 |
430 #define WO_ISNULL 0x080 | 502 #define WO_IS 0x0080 |
431 #define WO_OR 0x100 /* Two or more OR-connected terms */ | 503 #define WO_ISNULL 0x0100 |
432 #define WO_AND 0x200 /* Two or more AND-connected terms */ | 504 #define WO_OR 0x0200 /* Two or more OR-connected terms */ |
433 #define WO_EQUIV 0x400 /* Of the form A==B, both columns */ | 505 #define WO_AND 0x0400 /* Two or more AND-connected terms */ |
434 #define WO_NOOP 0x800 /* This term does not restrict search space */ | 506 #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */ |
| 507 #define WO_NOOP 0x1000 /* This term does not restrict search space */ |
435 | 508 |
436 #define WO_ALL 0xfff /* Mask of all possible WO_* values */ | 509 #define WO_ALL 0x1fff /* Mask of all possible WO_* values */ |
437 #define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ | 510 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */ |
438 | 511 |
439 /* | 512 /* |
440 ** These are definitions of bits in the WhereLoop.wsFlags field. | 513 ** These are definitions of bits in the WhereLoop.wsFlags field. |
441 ** The particular combination of bits in each WhereLoop help to | 514 ** The particular combination of bits in each WhereLoop help to |
442 ** determine the algorithm that WhereLoop represents. | 515 ** determine the algorithm that WhereLoop represents. |
443 */ | 516 */ |
444 #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ | 517 #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ |
445 #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ | 518 #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
446 #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ | 519 #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
447 #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ | 520 #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
448 #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ | 521 #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
449 #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ | 522 #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
450 #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ | 523 #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
451 #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ | 524 #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
452 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ | 525 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
453 #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ | 526 #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
454 #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ | 527 #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
455 #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ | 528 #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
456 #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ | 529 #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
457 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ | 530 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
458 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ | 531 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
459 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ | 532 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
460 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ | 533 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
461 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ | 534 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 535 #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
OLD | NEW |