Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / third_party / sqlite / sqlite-src-3080704 / src / where.c
blobbc0110779ea051f46e02c825002686879fffab5d
1 /*
2 ** 2001 September 15
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.
11 *************************************************************************
12 ** This module contains C code that generates VDBE code used to process
13 ** the WHERE clause of SQL statements. This module is responsible for
14 ** generating the code that loops through a table looking for applicable
15 ** rows. Indices are selected and used to speed the search when doing
16 ** so is applicable. Because this module is responsible for selecting
17 ** indices, you might also think of this module as the "query optimizer".
19 #include "sqliteInt.h"
20 #include "whereInt.h"
23 ** Return the estimated number of output rows from a WHERE clause
25 u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
26 return sqlite3LogEstToInt(pWInfo->nRowOut);
30 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
31 ** WHERE clause returns outputs for DISTINCT processing.
33 int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
34 return pWInfo->eDistinct;
38 ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
39 ** Return FALSE if the output needs to be sorted.
41 int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
42 return pWInfo->nOBSat;
46 ** Return the VDBE address or label to jump to in order to continue
47 ** immediately with the next row of a WHERE clause.
49 int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
50 assert( pWInfo->iContinue!=0 );
51 return pWInfo->iContinue;
55 ** Return the VDBE address or label to jump to in order to break
56 ** out of a WHERE loop.
58 int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
59 return pWInfo->iBreak;
63 ** Return TRUE if an UPDATE or DELETE statement can operate directly on
64 ** the rowids returned by a WHERE clause. Return FALSE if doing an
65 ** UPDATE or DELETE might change subsequent WHERE clause results.
67 ** If the ONEPASS optimization is used (if this routine returns true)
68 ** then also write the indices of open cursors used by ONEPASS
69 ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
70 ** table and iaCur[1] gets the cursor used by an auxiliary index.
71 ** Either value may be -1, indicating that cursor is not used.
72 ** Any cursors returned will have been opened for writing.
74 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
75 ** unable to use the ONEPASS optimization.
77 int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
78 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
79 return pWInfo->okOnePass;
83 ** Move the content of pSrc into pDest
85 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
86 pDest->n = pSrc->n;
87 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
91 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
93 ** The new entry might overwrite an existing entry, or it might be
94 ** appended, or it might be discarded. Do whatever is the right thing
95 ** so that pSet keeps the N_OR_COST best entries seen so far.
97 static int whereOrInsert(
98 WhereOrSet *pSet, /* The WhereOrSet to be updated */
99 Bitmask prereq, /* Prerequisites of the new entry */
100 LogEst rRun, /* Run-cost of the new entry */
101 LogEst nOut /* Number of outputs for the new entry */
103 u16 i;
104 WhereOrCost *p;
105 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
106 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
107 goto whereOrInsert_done;
109 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
110 return 0;
113 if( pSet->n<N_OR_COST ){
114 p = &pSet->a[pSet->n++];
115 p->nOut = nOut;
116 }else{
117 p = pSet->a;
118 for(i=1; i<pSet->n; i++){
119 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
121 if( p->rRun<=rRun ) return 0;
123 whereOrInsert_done:
124 p->prereq = prereq;
125 p->rRun = rRun;
126 if( p->nOut>nOut ) p->nOut = nOut;
127 return 1;
131 ** Initialize a preallocated WhereClause structure.
133 static void whereClauseInit(
134 WhereClause *pWC, /* The WhereClause to be initialized */
135 WhereInfo *pWInfo /* The WHERE processing context */
137 pWC->pWInfo = pWInfo;
138 pWC->pOuter = 0;
139 pWC->nTerm = 0;
140 pWC->nSlot = ArraySize(pWC->aStatic);
141 pWC->a = pWC->aStatic;
144 /* Forward reference */
145 static void whereClauseClear(WhereClause*);
148 ** Deallocate all memory associated with a WhereOrInfo object.
150 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
151 whereClauseClear(&p->wc);
152 sqlite3DbFree(db, p);
156 ** Deallocate all memory associated with a WhereAndInfo object.
158 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
159 whereClauseClear(&p->wc);
160 sqlite3DbFree(db, p);
164 ** Deallocate a WhereClause structure. The WhereClause structure
165 ** itself is not freed. This routine is the inverse of whereClauseInit().
167 static void whereClauseClear(WhereClause *pWC){
168 int i;
169 WhereTerm *a;
170 sqlite3 *db = pWC->pWInfo->pParse->db;
171 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
172 if( a->wtFlags & TERM_DYNAMIC ){
173 sqlite3ExprDelete(db, a->pExpr);
175 if( a->wtFlags & TERM_ORINFO ){
176 whereOrInfoDelete(db, a->u.pOrInfo);
177 }else if( a->wtFlags & TERM_ANDINFO ){
178 whereAndInfoDelete(db, a->u.pAndInfo);
181 if( pWC->a!=pWC->aStatic ){
182 sqlite3DbFree(db, pWC->a);
187 ** Add a single new WhereTerm entry to the WhereClause object pWC.
188 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
189 ** The index in pWC->a[] of the new WhereTerm is returned on success.
190 ** 0 is returned if the new WhereTerm could not be added due to a memory
191 ** allocation error. The memory allocation failure will be recorded in
192 ** the db->mallocFailed flag so that higher-level functions can detect it.
194 ** This routine will increase the size of the pWC->a[] array as necessary.
196 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
197 ** for freeing the expression p is assumed by the WhereClause object pWC.
198 ** This is true even if this routine fails to allocate a new WhereTerm.
200 ** WARNING: This routine might reallocate the space used to store
201 ** WhereTerms. All pointers to WhereTerms should be invalidated after
202 ** calling this routine. Such pointers may be reinitialized by referencing
203 ** the pWC->a[] array.
205 static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
206 WhereTerm *pTerm;
207 int idx;
208 testcase( wtFlags & TERM_VIRTUAL );
209 if( pWC->nTerm>=pWC->nSlot ){
210 WhereTerm *pOld = pWC->a;
211 sqlite3 *db = pWC->pWInfo->pParse->db;
212 pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
213 if( pWC->a==0 ){
214 if( wtFlags & TERM_DYNAMIC ){
215 sqlite3ExprDelete(db, p);
217 pWC->a = pOld;
218 return 0;
220 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
221 if( pOld!=pWC->aStatic ){
222 sqlite3DbFree(db, pOld);
224 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
226 pTerm = &pWC->a[idx = pWC->nTerm++];
227 if( p && ExprHasProperty(p, EP_Unlikely) ){
228 pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
229 }else{
230 pTerm->truthProb = 1;
232 pTerm->pExpr = sqlite3ExprSkipCollate(p);
233 pTerm->wtFlags = wtFlags;
234 pTerm->pWC = pWC;
235 pTerm->iParent = -1;
236 return idx;
240 ** This routine identifies subexpressions in the WHERE clause where
241 ** each subexpression is separated by the AND operator or some other
242 ** operator specified in the op parameter. The WhereClause structure
243 ** is filled with pointers to subexpressions. For example:
245 ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
246 ** \________/ \_______________/ \________________/
247 ** slot[0] slot[1] slot[2]
249 ** The original WHERE clause in pExpr is unaltered. All this routine
250 ** does is make slot[] entries point to substructure within pExpr.
252 ** In the previous sentence and in the diagram, "slot[]" refers to
253 ** the WhereClause.a[] array. The slot[] array grows as needed to contain
254 ** all terms of the WHERE clause.
256 static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
257 pWC->op = op;
258 if( pExpr==0 ) return;
259 if( pExpr->op!=op ){
260 whereClauseInsert(pWC, pExpr, 0);
261 }else{
262 whereSplit(pWC, pExpr->pLeft, op);
263 whereSplit(pWC, pExpr->pRight, op);
268 ** Initialize a WhereMaskSet object
270 #define initMaskSet(P) (P)->n=0
273 ** Return the bitmask for the given cursor number. Return 0 if
274 ** iCursor is not in the set.
276 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
277 int i;
278 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
279 for(i=0; i<pMaskSet->n; i++){
280 if( pMaskSet->ix[i]==iCursor ){
281 return MASKBIT(i);
284 return 0;
288 ** Create a new mask for cursor iCursor.
290 ** There is one cursor per table in the FROM clause. The number of
291 ** tables in the FROM clause is limited by a test early in the
292 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
293 ** array will never overflow.
295 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
296 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
297 pMaskSet->ix[pMaskSet->n++] = iCursor;
301 ** These routines walk (recursively) an expression tree and generate
302 ** a bitmask indicating which tables are used in that expression
303 ** tree.
305 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
306 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
307 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
308 Bitmask mask = 0;
309 if( p==0 ) return 0;
310 if( p->op==TK_COLUMN ){
311 mask = getMask(pMaskSet, p->iTable);
312 return mask;
314 mask = exprTableUsage(pMaskSet, p->pRight);
315 mask |= exprTableUsage(pMaskSet, p->pLeft);
316 if( ExprHasProperty(p, EP_xIsSelect) ){
317 mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
318 }else{
319 mask |= exprListTableUsage(pMaskSet, p->x.pList);
321 return mask;
323 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
324 int i;
325 Bitmask mask = 0;
326 if( pList ){
327 for(i=0; i<pList->nExpr; i++){
328 mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
331 return mask;
333 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
334 Bitmask mask = 0;
335 while( pS ){
336 SrcList *pSrc = pS->pSrc;
337 mask |= exprListTableUsage(pMaskSet, pS->pEList);
338 mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
339 mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
340 mask |= exprTableUsage(pMaskSet, pS->pWhere);
341 mask |= exprTableUsage(pMaskSet, pS->pHaving);
342 if( ALWAYS(pSrc!=0) ){
343 int i;
344 for(i=0; i<pSrc->nSrc; i++){
345 mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
346 mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
349 pS = pS->pPrior;
351 return mask;
355 ** Return TRUE if the given operator is one of the operators that is
356 ** allowed for an indexable WHERE clause term. The allowed operators are
357 ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
359 static int allowedOp(int op){
360 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
361 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
362 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
363 assert( TK_GE==TK_EQ+4 );
364 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
368 ** Commute a comparison operator. Expressions of the form "X op Y"
369 ** are converted into "Y op X".
371 ** If left/right precedence rules come into play when determining the
372 ** collating sequence, then COLLATE operators are adjusted to ensure
373 ** that the collating sequence does not change. For example:
374 ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
375 ** the left hand side of a comparison overrides any collation sequence
376 ** attached to the right. For the same reason the EP_Collate flag
377 ** is not commuted.
379 static void exprCommute(Parse *pParse, Expr *pExpr){
380 u16 expRight = (pExpr->pRight->flags & EP_Collate);
381 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
382 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
383 if( expRight==expLeft ){
384 /* Either X and Y both have COLLATE operator or neither do */
385 if( expRight ){
386 /* Both X and Y have COLLATE operators. Make sure X is always
387 ** used by clearing the EP_Collate flag from Y. */
388 pExpr->pRight->flags &= ~EP_Collate;
389 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
390 /* Neither X nor Y have COLLATE operators, but X has a non-default
391 ** collating sequence. So add the EP_Collate marker on X to cause
392 ** it to be searched first. */
393 pExpr->pLeft->flags |= EP_Collate;
396 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
397 if( pExpr->op>=TK_GT ){
398 assert( TK_LT==TK_GT+2 );
399 assert( TK_GE==TK_LE+2 );
400 assert( TK_GT>TK_EQ );
401 assert( TK_GT<TK_LE );
402 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
403 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
408 ** Translate from TK_xx operator to WO_xx bitmask.
410 static u16 operatorMask(int op){
411 u16 c;
412 assert( allowedOp(op) );
413 if( op==TK_IN ){
414 c = WO_IN;
415 }else if( op==TK_ISNULL ){
416 c = WO_ISNULL;
417 }else{
418 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
419 c = (u16)(WO_EQ<<(op-TK_EQ));
421 assert( op!=TK_ISNULL || c==WO_ISNULL );
422 assert( op!=TK_IN || c==WO_IN );
423 assert( op!=TK_EQ || c==WO_EQ );
424 assert( op!=TK_LT || c==WO_LT );
425 assert( op!=TK_LE || c==WO_LE );
426 assert( op!=TK_GT || c==WO_GT );
427 assert( op!=TK_GE || c==WO_GE );
428 return c;
432 ** Advance to the next WhereTerm that matches according to the criteria
433 ** established when the pScan object was initialized by whereScanInit().
434 ** Return NULL if there are no more matching WhereTerms.
436 static WhereTerm *whereScanNext(WhereScan *pScan){
437 int iCur; /* The cursor on the LHS of the term */
438 int iColumn; /* The column on the LHS of the term. -1 for IPK */
439 Expr *pX; /* An expression being tested */
440 WhereClause *pWC; /* Shorthand for pScan->pWC */
441 WhereTerm *pTerm; /* The term being tested */
442 int k = pScan->k; /* Where to start scanning */
444 while( pScan->iEquiv<=pScan->nEquiv ){
445 iCur = pScan->aEquiv[pScan->iEquiv-2];
446 iColumn = pScan->aEquiv[pScan->iEquiv-1];
447 while( (pWC = pScan->pWC)!=0 ){
448 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
449 if( pTerm->leftCursor==iCur
450 && pTerm->u.leftColumn==iColumn
451 && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
453 if( (pTerm->eOperator & WO_EQUIV)!=0
454 && pScan->nEquiv<ArraySize(pScan->aEquiv)
456 int j;
457 pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
458 assert( pX->op==TK_COLUMN );
459 for(j=0; j<pScan->nEquiv; j+=2){
460 if( pScan->aEquiv[j]==pX->iTable
461 && pScan->aEquiv[j+1]==pX->iColumn ){
462 break;
465 if( j==pScan->nEquiv ){
466 pScan->aEquiv[j] = pX->iTable;
467 pScan->aEquiv[j+1] = pX->iColumn;
468 pScan->nEquiv += 2;
471 if( (pTerm->eOperator & pScan->opMask)!=0 ){
472 /* Verify the affinity and collating sequence match */
473 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
474 CollSeq *pColl;
475 Parse *pParse = pWC->pWInfo->pParse;
476 pX = pTerm->pExpr;
477 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
478 continue;
480 assert(pX->pLeft);
481 pColl = sqlite3BinaryCompareCollSeq(pParse,
482 pX->pLeft, pX->pRight);
483 if( pColl==0 ) pColl = pParse->db->pDfltColl;
484 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
485 continue;
488 if( (pTerm->eOperator & WO_EQ)!=0
489 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
490 && pX->iTable==pScan->aEquiv[0]
491 && pX->iColumn==pScan->aEquiv[1]
493 continue;
495 pScan->k = k+1;
496 return pTerm;
500 pScan->pWC = pScan->pWC->pOuter;
501 k = 0;
503 pScan->pWC = pScan->pOrigWC;
504 k = 0;
505 pScan->iEquiv += 2;
507 return 0;
511 ** Initialize a WHERE clause scanner object. Return a pointer to the
512 ** first match. Return NULL if there are no matches.
514 ** The scanner will be searching the WHERE clause pWC. It will look
515 ** for terms of the form "X <op> <expr>" where X is column iColumn of table
516 ** iCur. The <op> must be one of the operators described by opMask.
518 ** If the search is for X and the WHERE clause contains terms of the
519 ** form X=Y then this routine might also return terms of the form
520 ** "Y <op> <expr>". The number of levels of transitivity is limited,
521 ** but is enough to handle most commonly occurring SQL statements.
523 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with
524 ** index pIdx.
526 static WhereTerm *whereScanInit(
527 WhereScan *pScan, /* The WhereScan object being initialized */
528 WhereClause *pWC, /* The WHERE clause to be scanned */
529 int iCur, /* Cursor to scan for */
530 int iColumn, /* Column to scan for */
531 u32 opMask, /* Operator(s) to scan for */
532 Index *pIdx /* Must be compatible with this index */
534 int j;
536 /* memset(pScan, 0, sizeof(*pScan)); */
537 pScan->pOrigWC = pWC;
538 pScan->pWC = pWC;
539 if( pIdx && iColumn>=0 ){
540 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
541 for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
542 if( NEVER(j>pIdx->nColumn) ) return 0;
544 pScan->zCollName = pIdx->azColl[j];
545 }else{
546 pScan->idxaff = 0;
547 pScan->zCollName = 0;
549 pScan->opMask = opMask;
550 pScan->k = 0;
551 pScan->aEquiv[0] = iCur;
552 pScan->aEquiv[1] = iColumn;
553 pScan->nEquiv = 2;
554 pScan->iEquiv = 2;
555 return whereScanNext(pScan);
559 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
560 ** where X is a reference to the iColumn of table iCur and <op> is one of
561 ** the WO_xx operator codes specified by the op parameter.
562 ** Return a pointer to the term. Return 0 if not found.
564 ** The term returned might by Y=<expr> if there is another constraint in
565 ** the WHERE clause that specifies that X=Y. Any such constraints will be
566 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
567 ** aEquiv[] array holds X and all its equivalents, with each SQL variable
568 ** taking up two slots in aEquiv[]. The first slot is for the cursor number
569 ** and the second is for the column number. There are 22 slots in aEquiv[]
570 ** so that means we can look for X plus up to 10 other equivalent values.
571 ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
572 ** and ... and A9=A10 and A10=<expr>.
574 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
575 ** then try for the one with no dependencies on <expr> - in other words where
576 ** <expr> is a constant expression of some kind. Only return entries of
577 ** the form "X <op> Y" where Y is a column in another table if no terms of
578 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
579 ** exist, try to return a term that does not use WO_EQUIV.
581 static WhereTerm *findTerm(
582 WhereClause *pWC, /* The WHERE clause to be searched */
583 int iCur, /* Cursor number of LHS */
584 int iColumn, /* Column number of LHS */
585 Bitmask notReady, /* RHS must not overlap with this mask */
586 u32 op, /* Mask of WO_xx values describing operator */
587 Index *pIdx /* Must be compatible with this index, if not NULL */
589 WhereTerm *pResult = 0;
590 WhereTerm *p;
591 WhereScan scan;
593 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
594 while( p ){
595 if( (p->prereqRight & notReady)==0 ){
596 if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
597 return p;
599 if( pResult==0 ) pResult = p;
601 p = whereScanNext(&scan);
603 return pResult;
606 /* Forward reference */
607 static void exprAnalyze(SrcList*, WhereClause*, int);
610 ** Call exprAnalyze on all terms in a WHERE clause.
612 static void exprAnalyzeAll(
613 SrcList *pTabList, /* the FROM clause */
614 WhereClause *pWC /* the WHERE clause to be analyzed */
616 int i;
617 for(i=pWC->nTerm-1; i>=0; i--){
618 exprAnalyze(pTabList, pWC, i);
622 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
624 ** Check to see if the given expression is a LIKE or GLOB operator that
625 ** can be optimized using inequality constraints. Return TRUE if it is
626 ** so and false if not.
628 ** In order for the operator to be optimizible, the RHS must be a string
629 ** literal that does not begin with a wildcard.
631 static int isLikeOrGlob(
632 Parse *pParse, /* Parsing and code generating context */
633 Expr *pExpr, /* Test this expression */
634 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
635 int *pisComplete, /* True if the only wildcard is % in the last character */
636 int *pnoCase /* True if uppercase is equivalent to lowercase */
638 const char *z = 0; /* String on RHS of LIKE operator */
639 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
640 ExprList *pList; /* List of operands to the LIKE operator */
641 int c; /* One character in z[] */
642 int cnt; /* Number of non-wildcard prefix characters */
643 char wc[3]; /* Wildcard characters */
644 sqlite3 *db = pParse->db; /* Database connection */
645 sqlite3_value *pVal = 0;
646 int op; /* Opcode of pRight */
648 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
649 return 0;
651 #ifdef SQLITE_EBCDIC
652 if( *pnoCase ) return 0;
653 #endif
654 pList = pExpr->x.pList;
655 pLeft = pList->a[1].pExpr;
656 if( pLeft->op!=TK_COLUMN
657 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
658 || IsVirtual(pLeft->pTab)
660 /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
661 ** be the name of an indexed column with TEXT affinity. */
662 return 0;
664 assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
666 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
667 op = pRight->op;
668 if( op==TK_VARIABLE ){
669 Vdbe *pReprepare = pParse->pReprepare;
670 int iCol = pRight->iColumn;
671 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
672 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
673 z = (char *)sqlite3_value_text(pVal);
675 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
676 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
677 }else if( op==TK_STRING ){
678 z = pRight->u.zToken;
680 if( z ){
681 cnt = 0;
682 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
683 cnt++;
685 if( cnt!=0 && 255!=(u8)z[cnt-1] ){
686 Expr *pPrefix;
687 *pisComplete = c==wc[0] && z[cnt+1]==0;
688 pPrefix = sqlite3Expr(db, TK_STRING, z);
689 if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
690 *ppPrefix = pPrefix;
691 if( op==TK_VARIABLE ){
692 Vdbe *v = pParse->pVdbe;
693 sqlite3VdbeSetVarmask(v, pRight->iColumn);
694 if( *pisComplete && pRight->u.zToken[1] ){
695 /* If the rhs of the LIKE expression is a variable, and the current
696 ** value of the variable means there is no need to invoke the LIKE
697 ** function, then no OP_Variable will be added to the program.
698 ** This causes problems for the sqlite3_bind_parameter_name()
699 ** API. To work around them, add a dummy OP_Variable here.
701 int r1 = sqlite3GetTempReg(pParse);
702 sqlite3ExprCodeTarget(pParse, pRight, r1);
703 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
704 sqlite3ReleaseTempReg(pParse, r1);
707 }else{
708 z = 0;
712 sqlite3ValueFree(pVal);
713 return (z!=0);
715 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
718 #ifndef SQLITE_OMIT_VIRTUALTABLE
720 ** Check to see if the given expression is of the form
722 ** column MATCH expr
724 ** If it is then return TRUE. If not, return FALSE.
726 static int isMatchOfColumn(
727 Expr *pExpr /* Test this expression */
729 ExprList *pList;
731 if( pExpr->op!=TK_FUNCTION ){
732 return 0;
734 if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
735 return 0;
737 pList = pExpr->x.pList;
738 if( pList->nExpr!=2 ){
739 return 0;
741 if( pList->a[1].pExpr->op != TK_COLUMN ){
742 return 0;
744 return 1;
746 #endif /* SQLITE_OMIT_VIRTUALTABLE */
749 ** If the pBase expression originated in the ON or USING clause of
750 ** a join, then transfer the appropriate markings over to derived.
752 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
753 if( pDerived ){
754 pDerived->flags |= pBase->flags & EP_FromJoin;
755 pDerived->iRightJoinTable = pBase->iRightJoinTable;
759 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
761 ** Analyze a term that consists of two or more OR-connected
762 ** subterms. So in:
764 ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
765 ** ^^^^^^^^^^^^^^^^^^^^
767 ** This routine analyzes terms such as the middle term in the above example.
768 ** A WhereOrTerm object is computed and attached to the term under
769 ** analysis, regardless of the outcome of the analysis. Hence:
771 ** WhereTerm.wtFlags |= TERM_ORINFO
772 ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
774 ** The term being analyzed must have two or more of OR-connected subterms.
775 ** A single subterm might be a set of AND-connected sub-subterms.
776 ** Examples of terms under analysis:
778 ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
779 ** (B) x=expr1 OR expr2=x OR x=expr3
780 ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
781 ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
782 ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
784 ** CASE 1:
786 ** If all subterms are of the form T.C=expr for some single column of C and
787 ** a single table T (as shown in example B above) then create a new virtual
788 ** term that is an equivalent IN expression. In other words, if the term
789 ** being analyzed is:
791 ** x = expr1 OR expr2 = x OR x = expr3
793 ** then create a new virtual term like this:
795 ** x IN (expr1,expr2,expr3)
797 ** CASE 2:
799 ** If all subterms are indexable by a single table T, then set
801 ** WhereTerm.eOperator = WO_OR
802 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
804 ** A subterm is "indexable" if it is of the form
805 ** "T.C <op> <expr>" where C is any column of table T and
806 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
807 ** A subterm is also indexable if it is an AND of two or more
808 ** subsubterms at least one of which is indexable. Indexable AND
809 ** subterms have their eOperator set to WO_AND and they have
810 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
812 ** From another point of view, "indexable" means that the subterm could
813 ** potentially be used with an index if an appropriate index exists.
814 ** This analysis does not consider whether or not the index exists; that
815 ** is decided elsewhere. This analysis only looks at whether subterms
816 ** appropriate for indexing exist.
818 ** All examples A through E above satisfy case 2. But if a term
819 ** also satisfies case 1 (such as B) we know that the optimizer will
820 ** always prefer case 1, so in that case we pretend that case 2 is not
821 ** satisfied.
823 ** It might be the case that multiple tables are indexable. For example,
824 ** (E) above is indexable on tables P, Q, and R.
826 ** Terms that satisfy case 2 are candidates for lookup by using
827 ** separate indices to find rowids for each subterm and composing
828 ** the union of all rowids using a RowSet object. This is similar
829 ** to "bitmap indices" in other database engines.
831 ** OTHERWISE:
833 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
834 ** zero. This term is not useful for search.
836 static void exprAnalyzeOrTerm(
837 SrcList *pSrc, /* the FROM clause */
838 WhereClause *pWC, /* the complete WHERE clause */
839 int idxTerm /* Index of the OR-term to be analyzed */
841 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
842 Parse *pParse = pWInfo->pParse; /* Parser context */
843 sqlite3 *db = pParse->db; /* Database connection */
844 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
845 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
846 int i; /* Loop counters */
847 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
848 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
849 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
850 Bitmask chngToIN; /* Tables that might satisfy case 1 */
851 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
854 ** Break the OR clause into its separate subterms. The subterms are
855 ** stored in a WhereClause structure containing within the WhereOrInfo
856 ** object that is attached to the original OR clause term.
858 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
859 assert( pExpr->op==TK_OR );
860 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
861 if( pOrInfo==0 ) return;
862 pTerm->wtFlags |= TERM_ORINFO;
863 pOrWc = &pOrInfo->wc;
864 whereClauseInit(pOrWc, pWInfo);
865 whereSplit(pOrWc, pExpr, TK_OR);
866 exprAnalyzeAll(pSrc, pOrWc);
867 if( db->mallocFailed ) return;
868 assert( pOrWc->nTerm>=2 );
871 ** Compute the set of tables that might satisfy cases 1 or 2.
873 indexable = ~(Bitmask)0;
874 chngToIN = ~(Bitmask)0;
875 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
876 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
877 WhereAndInfo *pAndInfo;
878 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
879 chngToIN = 0;
880 pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
881 if( pAndInfo ){
882 WhereClause *pAndWC;
883 WhereTerm *pAndTerm;
884 int j;
885 Bitmask b = 0;
886 pOrTerm->u.pAndInfo = pAndInfo;
887 pOrTerm->wtFlags |= TERM_ANDINFO;
888 pOrTerm->eOperator = WO_AND;
889 pAndWC = &pAndInfo->wc;
890 whereClauseInit(pAndWC, pWC->pWInfo);
891 whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
892 exprAnalyzeAll(pSrc, pAndWC);
893 pAndWC->pOuter = pWC;
894 testcase( db->mallocFailed );
895 if( !db->mallocFailed ){
896 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
897 assert( pAndTerm->pExpr );
898 if( allowedOp(pAndTerm->pExpr->op) ){
899 b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
903 indexable &= b;
905 }else if( pOrTerm->wtFlags & TERM_COPIED ){
906 /* Skip this term for now. We revisit it when we process the
907 ** corresponding TERM_VIRTUAL term */
908 }else{
909 Bitmask b;
910 b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
911 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
912 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
913 b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
915 indexable &= b;
916 if( (pOrTerm->eOperator & WO_EQ)==0 ){
917 chngToIN = 0;
918 }else{
919 chngToIN &= b;
925 ** Record the set of tables that satisfy case 2. The set might be
926 ** empty.
928 pOrInfo->indexable = indexable;
929 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
932 ** chngToIN holds a set of tables that *might* satisfy case 1. But
933 ** we have to do some additional checking to see if case 1 really
934 ** is satisfied.
936 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
937 ** that there is no possibility of transforming the OR clause into an
938 ** IN operator because one or more terms in the OR clause contain
939 ** something other than == on a column in the single table. The 1-bit
940 ** case means that every term of the OR clause is of the form
941 ** "table.column=expr" for some single table. The one bit that is set
942 ** will correspond to the common table. We still need to check to make
943 ** sure the same column is used on all terms. The 2-bit case is when
944 ** the all terms are of the form "table1.column=table2.column". It
945 ** might be possible to form an IN operator with either table1.column
946 ** or table2.column as the LHS if either is common to every term of
947 ** the OR clause.
949 ** Note that terms of the form "table.column1=table.column2" (the
950 ** same table on both sizes of the ==) cannot be optimized.
952 if( chngToIN ){
953 int okToChngToIN = 0; /* True if the conversion to IN is valid */
954 int iColumn = -1; /* Column index on lhs of IN operator */
955 int iCursor = -1; /* Table cursor common to all terms */
956 int j = 0; /* Loop counter */
958 /* Search for a table and column that appears on one side or the
959 ** other of the == operator in every subterm. That table and column
960 ** will be recorded in iCursor and iColumn. There might not be any
961 ** such table and column. Set okToChngToIN if an appropriate table
962 ** and column is found but leave okToChngToIN false if not found.
964 for(j=0; j<2 && !okToChngToIN; j++){
965 pOrTerm = pOrWc->a;
966 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
967 assert( pOrTerm->eOperator & WO_EQ );
968 pOrTerm->wtFlags &= ~TERM_OR_OK;
969 if( pOrTerm->leftCursor==iCursor ){
970 /* This is the 2-bit case and we are on the second iteration and
971 ** current term is from the first iteration. So skip this term. */
972 assert( j==1 );
973 continue;
975 if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
976 /* This term must be of the form t1.a==t2.b where t2 is in the
977 ** chngToIN set but t1 is not. This term will be either preceded
978 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
979 ** and use its inversion. */
980 testcase( pOrTerm->wtFlags & TERM_COPIED );
981 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
982 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
983 continue;
985 iColumn = pOrTerm->u.leftColumn;
986 iCursor = pOrTerm->leftCursor;
987 break;
989 if( i<0 ){
990 /* No candidate table+column was found. This can only occur
991 ** on the second iteration */
992 assert( j==1 );
993 assert( IsPowerOfTwo(chngToIN) );
994 assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
995 break;
997 testcase( j==1 );
999 /* We have found a candidate table and column. Check to see if that
1000 ** table and column is common to every term in the OR clause */
1001 okToChngToIN = 1;
1002 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
1003 assert( pOrTerm->eOperator & WO_EQ );
1004 if( pOrTerm->leftCursor!=iCursor ){
1005 pOrTerm->wtFlags &= ~TERM_OR_OK;
1006 }else if( pOrTerm->u.leftColumn!=iColumn ){
1007 okToChngToIN = 0;
1008 }else{
1009 int affLeft, affRight;
1010 /* If the right-hand side is also a column, then the affinities
1011 ** of both right and left sides must be such that no type
1012 ** conversions are required on the right. (Ticket #2249)
1014 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
1015 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
1016 if( affRight!=0 && affRight!=affLeft ){
1017 okToChngToIN = 0;
1018 }else{
1019 pOrTerm->wtFlags |= TERM_OR_OK;
1025 /* At this point, okToChngToIN is true if original pTerm satisfies
1026 ** case 1. In that case, construct a new virtual term that is
1027 ** pTerm converted into an IN operator.
1029 if( okToChngToIN ){
1030 Expr *pDup; /* A transient duplicate expression */
1031 ExprList *pList = 0; /* The RHS of the IN operator */
1032 Expr *pLeft = 0; /* The LHS of the IN operator */
1033 Expr *pNew; /* The complete IN operator */
1035 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
1036 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
1037 assert( pOrTerm->eOperator & WO_EQ );
1038 assert( pOrTerm->leftCursor==iCursor );
1039 assert( pOrTerm->u.leftColumn==iColumn );
1040 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
1041 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
1042 pLeft = pOrTerm->pExpr->pLeft;
1044 assert( pLeft!=0 );
1045 pDup = sqlite3ExprDup(db, pLeft, 0);
1046 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
1047 if( pNew ){
1048 int idxNew;
1049 transferJoinMarkings(pNew, pExpr);
1050 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
1051 pNew->x.pList = pList;
1052 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
1053 testcase( idxNew==0 );
1054 exprAnalyze(pSrc, pWC, idxNew);
1055 pTerm = &pWC->a[idxTerm];
1056 pWC->a[idxNew].iParent = idxTerm;
1057 pTerm->nChild = 1;
1058 }else{
1059 sqlite3ExprListDelete(db, pList);
1061 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */
1065 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
1068 ** The input to this routine is an WhereTerm structure with only the
1069 ** "pExpr" field filled in. The job of this routine is to analyze the
1070 ** subexpression and populate all the other fields of the WhereTerm
1071 ** structure.
1073 ** If the expression is of the form "<expr> <op> X" it gets commuted
1074 ** to the standard form of "X <op> <expr>".
1076 ** If the expression is of the form "X <op> Y" where both X and Y are
1077 ** columns, then the original expression is unchanged and a new virtual
1078 ** term of the form "Y <op> X" is added to the WHERE clause and
1079 ** analyzed separately. The original term is marked with TERM_COPIED
1080 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
1081 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
1082 ** is a commuted copy of a prior term.) The original term has nChild=1
1083 ** and the copy has idxParent set to the index of the original term.
1085 static void exprAnalyze(
1086 SrcList *pSrc, /* the FROM clause */
1087 WhereClause *pWC, /* the WHERE clause */
1088 int idxTerm /* Index of the term to be analyzed */
1090 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
1091 WhereTerm *pTerm; /* The term to be analyzed */
1092 WhereMaskSet *pMaskSet; /* Set of table index masks */
1093 Expr *pExpr; /* The expression to be analyzed */
1094 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
1095 Bitmask prereqAll; /* Prerequesites of pExpr */
1096 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
1097 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
1098 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
1099 int noCase = 0; /* LIKE/GLOB distinguishes case */
1100 int op; /* Top-level operator. pExpr->op */
1101 Parse *pParse = pWInfo->pParse; /* Parsing context */
1102 sqlite3 *db = pParse->db; /* Database connection */
1104 if( db->mallocFailed ){
1105 return;
1107 pTerm = &pWC->a[idxTerm];
1108 pMaskSet = &pWInfo->sMaskSet;
1109 pExpr = pTerm->pExpr;
1110 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
1111 prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
1112 op = pExpr->op;
1113 if( op==TK_IN ){
1114 assert( pExpr->pRight==0 );
1115 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
1116 pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
1117 }else{
1118 pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
1120 }else if( op==TK_ISNULL ){
1121 pTerm->prereqRight = 0;
1122 }else{
1123 pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
1125 prereqAll = exprTableUsage(pMaskSet, pExpr);
1126 if( ExprHasProperty(pExpr, EP_FromJoin) ){
1127 Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
1128 prereqAll |= x;
1129 extraRight = x-1; /* ON clause terms may not be used with an index
1130 ** on left table of a LEFT JOIN. Ticket #3015 */
1132 pTerm->prereqAll = prereqAll;
1133 pTerm->leftCursor = -1;
1134 pTerm->iParent = -1;
1135 pTerm->eOperator = 0;
1136 if( allowedOp(op) ){
1137 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
1138 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
1139 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
1140 if( pLeft->op==TK_COLUMN ){
1141 pTerm->leftCursor = pLeft->iTable;
1142 pTerm->u.leftColumn = pLeft->iColumn;
1143 pTerm->eOperator = operatorMask(op) & opMask;
1145 if( pRight && pRight->op==TK_COLUMN ){
1146 WhereTerm *pNew;
1147 Expr *pDup;
1148 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
1149 if( pTerm->leftCursor>=0 ){
1150 int idxNew;
1151 pDup = sqlite3ExprDup(db, pExpr, 0);
1152 if( db->mallocFailed ){
1153 sqlite3ExprDelete(db, pDup);
1154 return;
1156 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
1157 if( idxNew==0 ) return;
1158 pNew = &pWC->a[idxNew];
1159 pNew->iParent = idxTerm;
1160 pTerm = &pWC->a[idxTerm];
1161 pTerm->nChild = 1;
1162 pTerm->wtFlags |= TERM_COPIED;
1163 if( pExpr->op==TK_EQ
1164 && !ExprHasProperty(pExpr, EP_FromJoin)
1165 && OptimizationEnabled(db, SQLITE_Transitive)
1167 pTerm->eOperator |= WO_EQUIV;
1168 eExtraOp = WO_EQUIV;
1170 }else{
1171 pDup = pExpr;
1172 pNew = pTerm;
1174 exprCommute(pParse, pDup);
1175 pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
1176 pNew->leftCursor = pLeft->iTable;
1177 pNew->u.leftColumn = pLeft->iColumn;
1178 testcase( (prereqLeft | extraRight) != prereqLeft );
1179 pNew->prereqRight = prereqLeft | extraRight;
1180 pNew->prereqAll = prereqAll;
1181 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
1185 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
1186 /* If a term is the BETWEEN operator, create two new virtual terms
1187 ** that define the range that the BETWEEN implements. For example:
1189 ** a BETWEEN b AND c
1191 ** is converted into:
1193 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
1195 ** The two new terms are added onto the end of the WhereClause object.
1196 ** The new terms are "dynamic" and are children of the original BETWEEN
1197 ** term. That means that if the BETWEEN term is coded, the children are
1198 ** skipped. Or, if the children are satisfied by an index, the original
1199 ** BETWEEN term is skipped.
1201 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
1202 ExprList *pList = pExpr->x.pList;
1203 int i;
1204 static const u8 ops[] = {TK_GE, TK_LE};
1205 assert( pList!=0 );
1206 assert( pList->nExpr==2 );
1207 for(i=0; i<2; i++){
1208 Expr *pNewExpr;
1209 int idxNew;
1210 pNewExpr = sqlite3PExpr(pParse, ops[i],
1211 sqlite3ExprDup(db, pExpr->pLeft, 0),
1212 sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
1213 transferJoinMarkings(pNewExpr, pExpr);
1214 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1215 testcase( idxNew==0 );
1216 exprAnalyze(pSrc, pWC, idxNew);
1217 pTerm = &pWC->a[idxTerm];
1218 pWC->a[idxNew].iParent = idxTerm;
1220 pTerm->nChild = 2;
1222 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
1224 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
1225 /* Analyze a term that is composed of two or more subterms connected by
1226 ** an OR operator.
1228 else if( pExpr->op==TK_OR ){
1229 assert( pWC->op==TK_AND );
1230 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
1231 pTerm = &pWC->a[idxTerm];
1233 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
1235 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
1236 /* Add constraints to reduce the search space on a LIKE or GLOB
1237 ** operator.
1239 ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
1241 ** x>='abc' AND x<'abd' AND x LIKE 'abc%'
1243 ** The last character of the prefix "abc" is incremented to form the
1244 ** termination condition "abd".
1246 if( pWC->op==TK_AND
1247 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
1249 Expr *pLeft; /* LHS of LIKE/GLOB operator */
1250 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
1251 Expr *pNewExpr1;
1252 Expr *pNewExpr2;
1253 int idxNew1;
1254 int idxNew2;
1255 Token sCollSeqName; /* Name of collating sequence */
1257 pLeft = pExpr->x.pList->a[1].pExpr;
1258 pStr2 = sqlite3ExprDup(db, pStr1, 0);
1259 if( !db->mallocFailed ){
1260 u8 c, *pC; /* Last character before the first wildcard */
1261 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
1262 c = *pC;
1263 if( noCase ){
1264 /* The point is to increment the last character before the first
1265 ** wildcard. But if we increment '@', that will push it into the
1266 ** alphabetic range where case conversions will mess up the
1267 ** inequality. To avoid this, make sure to also run the full
1268 ** LIKE on all candidate expressions by clearing the isComplete flag
1270 if( c=='A'-1 ) isComplete = 0;
1271 c = sqlite3UpperToLower[c];
1273 *pC = c + 1;
1275 sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
1276 sCollSeqName.n = 6;
1277 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
1278 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
1279 sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
1280 pStr1, 0);
1281 transferJoinMarkings(pNewExpr1, pExpr);
1282 idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
1283 testcase( idxNew1==0 );
1284 exprAnalyze(pSrc, pWC, idxNew1);
1285 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
1286 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
1287 sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
1288 pStr2, 0);
1289 transferJoinMarkings(pNewExpr2, pExpr);
1290 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
1291 testcase( idxNew2==0 );
1292 exprAnalyze(pSrc, pWC, idxNew2);
1293 pTerm = &pWC->a[idxTerm];
1294 if( isComplete ){
1295 pWC->a[idxNew1].iParent = idxTerm;
1296 pWC->a[idxNew2].iParent = idxTerm;
1297 pTerm->nChild = 2;
1300 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
1302 #ifndef SQLITE_OMIT_VIRTUALTABLE
1303 /* Add a WO_MATCH auxiliary term to the constraint set if the
1304 ** current expression is of the form: column MATCH expr.
1305 ** This information is used by the xBestIndex methods of
1306 ** virtual tables. The native query optimizer does not attempt
1307 ** to do anything with MATCH functions.
1309 if( isMatchOfColumn(pExpr) ){
1310 int idxNew;
1311 Expr *pRight, *pLeft;
1312 WhereTerm *pNewTerm;
1313 Bitmask prereqColumn, prereqExpr;
1315 pRight = pExpr->x.pList->a[0].pExpr;
1316 pLeft = pExpr->x.pList->a[1].pExpr;
1317 prereqExpr = exprTableUsage(pMaskSet, pRight);
1318 prereqColumn = exprTableUsage(pMaskSet, pLeft);
1319 if( (prereqExpr & prereqColumn)==0 ){
1320 Expr *pNewExpr;
1321 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
1322 0, sqlite3ExprDup(db, pRight, 0), 0);
1323 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1324 testcase( idxNew==0 );
1325 pNewTerm = &pWC->a[idxNew];
1326 pNewTerm->prereqRight = prereqExpr;
1327 pNewTerm->leftCursor = pLeft->iTable;
1328 pNewTerm->u.leftColumn = pLeft->iColumn;
1329 pNewTerm->eOperator = WO_MATCH;
1330 pNewTerm->iParent = idxTerm;
1331 pTerm = &pWC->a[idxTerm];
1332 pTerm->nChild = 1;
1333 pTerm->wtFlags |= TERM_COPIED;
1334 pNewTerm->prereqAll = pTerm->prereqAll;
1337 #endif /* SQLITE_OMIT_VIRTUALTABLE */
1339 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1340 /* When sqlite_stat3 histogram data is available an operator of the
1341 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
1342 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
1343 ** virtual term of that form.
1345 ** Note that the virtual term must be tagged with TERM_VNULL. This
1346 ** TERM_VNULL tag will suppress the not-null check at the beginning
1347 ** of the loop. Without the TERM_VNULL flag, the not-null check at
1348 ** the start of the loop will prevent any results from being returned.
1350 if( pExpr->op==TK_NOTNULL
1351 && pExpr->pLeft->op==TK_COLUMN
1352 && pExpr->pLeft->iColumn>=0
1353 && OptimizationEnabled(db, SQLITE_Stat3)
1355 Expr *pNewExpr;
1356 Expr *pLeft = pExpr->pLeft;
1357 int idxNew;
1358 WhereTerm *pNewTerm;
1360 pNewExpr = sqlite3PExpr(pParse, TK_GT,
1361 sqlite3ExprDup(db, pLeft, 0),
1362 sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
1364 idxNew = whereClauseInsert(pWC, pNewExpr,
1365 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
1366 if( idxNew ){
1367 pNewTerm = &pWC->a[idxNew];
1368 pNewTerm->prereqRight = 0;
1369 pNewTerm->leftCursor = pLeft->iTable;
1370 pNewTerm->u.leftColumn = pLeft->iColumn;
1371 pNewTerm->eOperator = WO_GT;
1372 pNewTerm->iParent = idxTerm;
1373 pTerm = &pWC->a[idxTerm];
1374 pTerm->nChild = 1;
1375 pTerm->wtFlags |= TERM_COPIED;
1376 pNewTerm->prereqAll = pTerm->prereqAll;
1379 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1381 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
1382 ** an index for tables to the left of the join.
1384 pTerm->prereqRight |= extraRight;
1388 ** This function searches pList for an entry that matches the iCol-th column
1389 ** of index pIdx.
1391 ** If such an expression is found, its index in pList->a[] is returned. If
1392 ** no expression is found, -1 is returned.
1394 static int findIndexCol(
1395 Parse *pParse, /* Parse context */
1396 ExprList *pList, /* Expression list to search */
1397 int iBase, /* Cursor for table associated with pIdx */
1398 Index *pIdx, /* Index to match column of */
1399 int iCol /* Column of index to match */
1401 int i;
1402 const char *zColl = pIdx->azColl[iCol];
1404 for(i=0; i<pList->nExpr; i++){
1405 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
1406 if( p->op==TK_COLUMN
1407 && p->iColumn==pIdx->aiColumn[iCol]
1408 && p->iTable==iBase
1410 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1411 if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
1412 return i;
1417 return -1;
1421 ** Return true if the DISTINCT expression-list passed as the third argument
1422 ** is redundant.
1424 ** A DISTINCT list is redundant if the database contains some subset of
1425 ** columns that are unique and non-null.
1427 static int isDistinctRedundant(
1428 Parse *pParse, /* Parsing context */
1429 SrcList *pTabList, /* The FROM clause */
1430 WhereClause *pWC, /* The WHERE clause */
1431 ExprList *pDistinct /* The result set that needs to be DISTINCT */
1433 Table *pTab;
1434 Index *pIdx;
1435 int i;
1436 int iBase;
1438 /* If there is more than one table or sub-select in the FROM clause of
1439 ** this query, then it will not be possible to show that the DISTINCT
1440 ** clause is redundant. */
1441 if( pTabList->nSrc!=1 ) return 0;
1442 iBase = pTabList->a[0].iCursor;
1443 pTab = pTabList->a[0].pTab;
1445 /* If any of the expressions is an IPK column on table iBase, then return
1446 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
1447 ** current SELECT is a correlated sub-query.
1449 for(i=0; i<pDistinct->nExpr; i++){
1450 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
1451 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
1454 /* Loop through all indices on the table, checking each to see if it makes
1455 ** the DISTINCT qualifier redundant. It does so if:
1457 ** 1. The index is itself UNIQUE, and
1459 ** 2. All of the columns in the index are either part of the pDistinct
1460 ** list, or else the WHERE clause contains a term of the form "col=X",
1461 ** where X is a constant value. The collation sequences of the
1462 ** comparison and select-list expressions must match those of the index.
1464 ** 3. All of those index columns for which the WHERE clause does not
1465 ** contain a "col=X" term are subject to a NOT NULL constraint.
1467 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1468 if( !IsUniqueIndex(pIdx) ) continue;
1469 for(i=0; i<pIdx->nKeyCol; i++){
1470 i16 iCol = pIdx->aiColumn[i];
1471 if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
1472 int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
1473 if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
1474 break;
1478 if( i==pIdx->nKeyCol ){
1479 /* This index implies that the DISTINCT qualifier is redundant. */
1480 return 1;
1484 return 0;
1489 ** Estimate the logarithm of the input value to base 2.
1491 static LogEst estLog(LogEst N){
1492 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
1496 ** Two routines for printing the content of an sqlite3_index_info
1497 ** structure. Used for testing and debugging only. If neither
1498 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
1499 ** are no-ops.
1501 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
1502 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
1503 int i;
1504 if( !sqlite3WhereTrace ) return;
1505 for(i=0; i<p->nConstraint; i++){
1506 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
1508 p->aConstraint[i].iColumn,
1509 p->aConstraint[i].iTermOffset,
1510 p->aConstraint[i].op,
1511 p->aConstraint[i].usable);
1513 for(i=0; i<p->nOrderBy; i++){
1514 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
1516 p->aOrderBy[i].iColumn,
1517 p->aOrderBy[i].desc);
1520 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
1521 int i;
1522 if( !sqlite3WhereTrace ) return;
1523 for(i=0; i<p->nConstraint; i++){
1524 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
1526 p->aConstraintUsage[i].argvIndex,
1527 p->aConstraintUsage[i].omit);
1529 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
1530 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
1531 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
1532 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
1533 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
1535 #else
1536 #define TRACE_IDX_INPUTS(A)
1537 #define TRACE_IDX_OUTPUTS(A)
1538 #endif
1540 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
1542 ** Return TRUE if the WHERE clause term pTerm is of a form where it
1543 ** could be used with an index to access pSrc, assuming an appropriate
1544 ** index existed.
1546 static int termCanDriveIndex(
1547 WhereTerm *pTerm, /* WHERE clause term to check */
1548 struct SrcList_item *pSrc, /* Table we are trying to access */
1549 Bitmask notReady /* Tables in outer loops of the join */
1551 char aff;
1552 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
1553 if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
1554 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
1555 if( pTerm->u.leftColumn<0 ) return 0;
1556 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
1557 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
1558 return 1;
1560 #endif
1563 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
1565 ** Generate code to construct the Index object for an automatic index
1566 ** and to set up the WhereLevel object pLevel so that the code generator
1567 ** makes use of the automatic index.
1569 static void constructAutomaticIndex(
1570 Parse *pParse, /* The parsing context */
1571 WhereClause *pWC, /* The WHERE clause */
1572 struct SrcList_item *pSrc, /* The FROM clause term to get the next index */
1573 Bitmask notReady, /* Mask of cursors that are not available */
1574 WhereLevel *pLevel /* Write new index here */
1576 int nKeyCol; /* Number of columns in the constructed index */
1577 WhereTerm *pTerm; /* A single term of the WHERE clause */
1578 WhereTerm *pWCEnd; /* End of pWC->a[] */
1579 Index *pIdx; /* Object describing the transient index */
1580 Vdbe *v; /* Prepared statement under construction */
1581 int addrInit; /* Address of the initialization bypass jump */
1582 Table *pTable; /* The table being indexed */
1583 int addrTop; /* Top of the index fill loop */
1584 int regRecord; /* Register holding an index record */
1585 int n; /* Column counter */
1586 int i; /* Loop counter */
1587 int mxBitCol; /* Maximum column in pSrc->colUsed */
1588 CollSeq *pColl; /* Collating sequence to on a column */
1589 WhereLoop *pLoop; /* The Loop object */
1590 char *zNotUsed; /* Extra space on the end of pIdx */
1591 Bitmask idxCols; /* Bitmap of columns used for indexing */
1592 Bitmask extraCols; /* Bitmap of additional columns */
1593 u8 sentWarning = 0; /* True if a warnning has been issued */
1595 /* Generate code to skip over the creation and initialization of the
1596 ** transient index on 2nd and subsequent iterations of the loop. */
1597 v = pParse->pVdbe;
1598 assert( v!=0 );
1599 addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v);
1601 /* Count the number of columns that will be added to the index
1602 ** and used to match WHERE clause constraints */
1603 nKeyCol = 0;
1604 pTable = pSrc->pTab;
1605 pWCEnd = &pWC->a[pWC->nTerm];
1606 pLoop = pLevel->pWLoop;
1607 idxCols = 0;
1608 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
1609 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
1610 int iCol = pTerm->u.leftColumn;
1611 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
1612 testcase( iCol==BMS );
1613 testcase( iCol==BMS-1 );
1614 if( !sentWarning ){
1615 sqlite3_log(SQLITE_WARNING_AUTOINDEX,
1616 "automatic index on %s(%s)", pTable->zName,
1617 pTable->aCol[iCol].zName);
1618 sentWarning = 1;
1620 if( (idxCols & cMask)==0 ){
1621 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return;
1622 pLoop->aLTerm[nKeyCol++] = pTerm;
1623 idxCols |= cMask;
1627 assert( nKeyCol>0 );
1628 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
1629 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
1630 | WHERE_AUTO_INDEX;
1632 /* Count the number of additional columns needed to create a
1633 ** covering index. A "covering index" is an index that contains all
1634 ** columns that are needed by the query. With a covering index, the
1635 ** original table never needs to be accessed. Automatic indices must
1636 ** be a covering index because the index will not be updated if the
1637 ** original table changes and the index and table cannot both be used
1638 ** if they go out of sync.
1640 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
1641 mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
1642 testcase( pTable->nCol==BMS-1 );
1643 testcase( pTable->nCol==BMS-2 );
1644 for(i=0; i<mxBitCol; i++){
1645 if( extraCols & MASKBIT(i) ) nKeyCol++;
1647 if( pSrc->colUsed & MASKBIT(BMS-1) ){
1648 nKeyCol += pTable->nCol - BMS + 1;
1650 pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
1652 /* Construct the Index object to describe this index */
1653 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
1654 if( pIdx==0 ) return;
1655 pLoop->u.btree.pIndex = pIdx;
1656 pIdx->zName = "auto-index";
1657 pIdx->pTable = pTable;
1658 n = 0;
1659 idxCols = 0;
1660 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
1661 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
1662 int iCol = pTerm->u.leftColumn;
1663 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
1664 testcase( iCol==BMS-1 );
1665 testcase( iCol==BMS );
1666 if( (idxCols & cMask)==0 ){
1667 Expr *pX = pTerm->pExpr;
1668 idxCols |= cMask;
1669 pIdx->aiColumn[n] = pTerm->u.leftColumn;
1670 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
1671 pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
1672 n++;
1676 assert( (u32)n==pLoop->u.btree.nEq );
1678 /* Add additional columns needed to make the automatic index into
1679 ** a covering index */
1680 for(i=0; i<mxBitCol; i++){
1681 if( extraCols & MASKBIT(i) ){
1682 pIdx->aiColumn[n] = i;
1683 pIdx->azColl[n] = "BINARY";
1684 n++;
1687 if( pSrc->colUsed & MASKBIT(BMS-1) ){
1688 for(i=BMS-1; i<pTable->nCol; i++){
1689 pIdx->aiColumn[n] = i;
1690 pIdx->azColl[n] = "BINARY";
1691 n++;
1694 assert( n==nKeyCol );
1695 pIdx->aiColumn[n] = -1;
1696 pIdx->azColl[n] = "BINARY";
1698 /* Create the automatic index */
1699 assert( pLevel->iIdxCur>=0 );
1700 pLevel->iIdxCur = pParse->nTab++;
1701 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
1702 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
1703 VdbeComment((v, "for %s", pTable->zName));
1705 /* Fill the automatic index with content */
1706 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
1707 regRecord = sqlite3GetTempReg(pParse);
1708 sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
1709 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
1710 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
1711 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
1712 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
1713 sqlite3VdbeJumpHere(v, addrTop);
1714 sqlite3ReleaseTempReg(pParse, regRecord);
1716 /* Jump here when skipping the initialization */
1717 sqlite3VdbeJumpHere(v, addrInit);
1719 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
1721 #ifndef SQLITE_OMIT_VIRTUALTABLE
1723 ** Allocate and populate an sqlite3_index_info structure. It is the
1724 ** responsibility of the caller to eventually release the structure
1725 ** by passing the pointer returned by this function to sqlite3_free().
1727 static sqlite3_index_info *allocateIndexInfo(
1728 Parse *pParse,
1729 WhereClause *pWC,
1730 struct SrcList_item *pSrc,
1731 ExprList *pOrderBy
1733 int i, j;
1734 int nTerm;
1735 struct sqlite3_index_constraint *pIdxCons;
1736 struct sqlite3_index_orderby *pIdxOrderBy;
1737 struct sqlite3_index_constraint_usage *pUsage;
1738 WhereTerm *pTerm;
1739 int nOrderBy;
1740 sqlite3_index_info *pIdxInfo;
1742 /* Count the number of possible WHERE clause constraints referring
1743 ** to this virtual table */
1744 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
1745 if( pTerm->leftCursor != pSrc->iCursor ) continue;
1746 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
1747 testcase( pTerm->eOperator & WO_IN );
1748 testcase( pTerm->eOperator & WO_ISNULL );
1749 testcase( pTerm->eOperator & WO_ALL );
1750 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
1751 if( pTerm->wtFlags & TERM_VNULL ) continue;
1752 nTerm++;
1755 /* If the ORDER BY clause contains only columns in the current
1756 ** virtual table then allocate space for the aOrderBy part of
1757 ** the sqlite3_index_info structure.
1759 nOrderBy = 0;
1760 if( pOrderBy ){
1761 int n = pOrderBy->nExpr;
1762 for(i=0; i<n; i++){
1763 Expr *pExpr = pOrderBy->a[i].pExpr;
1764 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
1766 if( i==n){
1767 nOrderBy = n;
1771 /* Allocate the sqlite3_index_info structure
1773 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
1774 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
1775 + sizeof(*pIdxOrderBy)*nOrderBy );
1776 if( pIdxInfo==0 ){
1777 sqlite3ErrorMsg(pParse, "out of memory");
1778 return 0;
1781 /* Initialize the structure. The sqlite3_index_info structure contains
1782 ** many fields that are declared "const" to prevent xBestIndex from
1783 ** changing them. We have to do some funky casting in order to
1784 ** initialize those fields.
1786 pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
1787 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
1788 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
1789 *(int*)&pIdxInfo->nConstraint = nTerm;
1790 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
1791 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
1792 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
1793 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
1794 pUsage;
1796 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
1797 u8 op;
1798 if( pTerm->leftCursor != pSrc->iCursor ) continue;
1799 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
1800 testcase( pTerm->eOperator & WO_IN );
1801 testcase( pTerm->eOperator & WO_ISNULL );
1802 testcase( pTerm->eOperator & WO_ALL );
1803 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
1804 if( pTerm->wtFlags & TERM_VNULL ) continue;
1805 pIdxCons[j].iColumn = pTerm->u.leftColumn;
1806 pIdxCons[j].iTermOffset = i;
1807 op = (u8)pTerm->eOperator & WO_ALL;
1808 if( op==WO_IN ) op = WO_EQ;
1809 pIdxCons[j].op = op;
1810 /* The direct assignment in the previous line is possible only because
1811 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
1812 ** following asserts verify this fact. */
1813 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
1814 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
1815 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
1816 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
1817 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
1818 assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
1819 assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
1820 j++;
1822 for(i=0; i<nOrderBy; i++){
1823 Expr *pExpr = pOrderBy->a[i].pExpr;
1824 pIdxOrderBy[i].iColumn = pExpr->iColumn;
1825 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
1828 return pIdxInfo;
1832 ** The table object reference passed as the second argument to this function
1833 ** must represent a virtual table. This function invokes the xBestIndex()
1834 ** method of the virtual table with the sqlite3_index_info object that
1835 ** comes in as the 3rd argument to this function.
1837 ** If an error occurs, pParse is populated with an error message and a
1838 ** non-zero value is returned. Otherwise, 0 is returned and the output
1839 ** part of the sqlite3_index_info structure is left populated.
1841 ** Whether or not an error is returned, it is the responsibility of the
1842 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
1843 ** that this is required.
1845 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
1846 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
1847 int i;
1848 int rc;
1850 TRACE_IDX_INPUTS(p);
1851 rc = pVtab->pModule->xBestIndex(pVtab, p);
1852 TRACE_IDX_OUTPUTS(p);
1854 if( rc!=SQLITE_OK ){
1855 if( rc==SQLITE_NOMEM ){
1856 pParse->db->mallocFailed = 1;
1857 }else if( !pVtab->zErrMsg ){
1858 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
1859 }else{
1860 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
1863 sqlite3_free(pVtab->zErrMsg);
1864 pVtab->zErrMsg = 0;
1866 for(i=0; i<p->nConstraint; i++){
1867 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
1868 sqlite3ErrorMsg(pParse,
1869 "table %s: xBestIndex returned an invalid plan", pTab->zName);
1873 return pParse->nErr;
1875 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
1878 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1880 ** Estimate the location of a particular key among all keys in an
1881 ** index. Store the results in aStat as follows:
1883 ** aStat[0] Est. number of rows less than pVal
1884 ** aStat[1] Est. number of rows equal to pVal
1886 ** Return SQLITE_OK on success.
1888 static void whereKeyStats(
1889 Parse *pParse, /* Database connection */
1890 Index *pIdx, /* Index to consider domain of */
1891 UnpackedRecord *pRec, /* Vector of values to consider */
1892 int roundUp, /* Round up if true. Round down if false */
1893 tRowcnt *aStat /* OUT: stats written here */
1895 IndexSample *aSample = pIdx->aSample;
1896 int iCol; /* Index of required stats in anEq[] etc. */
1897 int iMin = 0; /* Smallest sample not yet tested */
1898 int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */
1899 int iTest; /* Next sample to test */
1900 int res; /* Result of comparison operation */
1902 #ifndef SQLITE_DEBUG
1903 UNUSED_PARAMETER( pParse );
1904 #endif
1905 assert( pRec!=0 );
1906 iCol = pRec->nField - 1;
1907 assert( pIdx->nSample>0 );
1908 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
1910 iTest = (iMin+i)/2;
1911 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
1912 if( res<0 ){
1913 iMin = iTest+1;
1914 }else{
1915 i = iTest;
1917 }while( res && iMin<i );
1919 #ifdef SQLITE_DEBUG
1920 /* The following assert statements check that the binary search code
1921 ** above found the right answer. This block serves no purpose other
1922 ** than to invoke the asserts. */
1923 if( res==0 ){
1924 /* If (res==0) is true, then sample $i must be equal to pRec */
1925 assert( i<pIdx->nSample );
1926 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
1927 || pParse->db->mallocFailed );
1928 }else{
1929 /* Otherwise, pRec must be smaller than sample $i and larger than
1930 ** sample ($i-1). */
1931 assert( i==pIdx->nSample
1932 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
1933 || pParse->db->mallocFailed );
1934 assert( i==0
1935 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
1936 || pParse->db->mallocFailed );
1938 #endif /* ifdef SQLITE_DEBUG */
1940 /* At this point, aSample[i] is the first sample that is greater than
1941 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
1942 ** than pVal. If aSample[i]==pVal, then res==0.
1944 if( res==0 ){
1945 aStat[0] = aSample[i].anLt[iCol];
1946 aStat[1] = aSample[i].anEq[iCol];
1947 }else{
1948 tRowcnt iLower, iUpper, iGap;
1949 if( i==0 ){
1950 iLower = 0;
1951 iUpper = aSample[0].anLt[iCol];
1952 }else{
1953 i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
1954 iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
1955 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
1957 aStat[1] = pIdx->aAvgEq[iCol];
1958 if( iLower>=iUpper ){
1959 iGap = 0;
1960 }else{
1961 iGap = iUpper - iLower;
1963 if( roundUp ){
1964 iGap = (iGap*2)/3;
1965 }else{
1966 iGap = iGap/3;
1968 aStat[0] = iLower + iGap;
1971 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1974 ** If it is not NULL, pTerm is a term that provides an upper or lower
1975 ** bound on a range scan. Without considering pTerm, it is estimated
1976 ** that the scan will visit nNew rows. This function returns the number
1977 ** estimated to be visited after taking pTerm into account.
1979 ** If the user explicitly specified a likelihood() value for this term,
1980 ** then the return value is the likelihood multiplied by the number of
1981 ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
1982 ** has a likelihood of 0.50, and any other term a likelihood of 0.25.
1984 static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
1985 LogEst nRet = nNew;
1986 if( pTerm ){
1987 if( pTerm->truthProb<=0 ){
1988 nRet += pTerm->truthProb;
1989 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
1990 nRet -= 20; assert( 20==sqlite3LogEst(4) );
1993 return nRet;
1996 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1998 ** This function is called to estimate the number of rows visited by a
1999 ** range-scan on a skip-scan index. For example:
2001 ** CREATE INDEX i1 ON t1(a, b, c);
2002 ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
2004 ** Value pLoop->nOut is currently set to the estimated number of rows
2005 ** visited for scanning (a=? AND b=?). This function reduces that estimate
2006 ** by some factor to account for the (c BETWEEN ? AND ?) expression based
2007 ** on the stat4 data for the index. this scan will be peformed multiple
2008 ** times (once for each (a,b) combination that matches a=?) is dealt with
2009 ** by the caller.
2011 ** It does this by scanning through all stat4 samples, comparing values
2012 ** extracted from pLower and pUpper with the corresponding column in each
2013 ** sample. If L and U are the number of samples found to be less than or
2014 ** equal to the values extracted from pLower and pUpper respectively, and
2015 ** N is the total number of samples, the pLoop->nOut value is adjusted
2016 ** as follows:
2018 ** nOut = nOut * ( min(U - L, 1) / N )
2020 ** If pLower is NULL, or a value cannot be extracted from the term, L is
2021 ** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
2022 ** U is set to N.
2024 ** Normally, this function sets *pbDone to 1 before returning. However,
2025 ** if no value can be extracted from either pLower or pUpper (and so the
2026 ** estimate of the number of rows delivered remains unchanged), *pbDone
2027 ** is left as is.
2029 ** If an error occurs, an SQLite error code is returned. Otherwise,
2030 ** SQLITE_OK.
2032 static int whereRangeSkipScanEst(
2033 Parse *pParse, /* Parsing & code generating context */
2034 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
2035 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
2036 WhereLoop *pLoop, /* Update the .nOut value of this loop */
2037 int *pbDone /* Set to true if at least one expr. value extracted */
2039 Index *p = pLoop->u.btree.pIndex;
2040 int nEq = pLoop->u.btree.nEq;
2041 sqlite3 *db = pParse->db;
2042 int nLower = -1;
2043 int nUpper = p->nSample+1;
2044 int rc = SQLITE_OK;
2045 int iCol = p->aiColumn[nEq];
2046 u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
2047 CollSeq *pColl;
2049 sqlite3_value *p1 = 0; /* Value extracted from pLower */
2050 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
2051 sqlite3_value *pVal = 0; /* Value extracted from record */
2053 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
2054 if( pLower ){
2055 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
2056 nLower = 0;
2058 if( pUpper && rc==SQLITE_OK ){
2059 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
2060 nUpper = p2 ? 0 : p->nSample;
2063 if( p1 || p2 ){
2064 int i;
2065 int nDiff;
2066 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
2067 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
2068 if( rc==SQLITE_OK && p1 ){
2069 int res = sqlite3MemCompare(p1, pVal, pColl);
2070 if( res>=0 ) nLower++;
2072 if( rc==SQLITE_OK && p2 ){
2073 int res = sqlite3MemCompare(p2, pVal, pColl);
2074 if( res>=0 ) nUpper++;
2077 nDiff = (nUpper - nLower);
2078 if( nDiff<=0 ) nDiff = 1;
2080 /* If there is both an upper and lower bound specified, and the
2081 ** comparisons indicate that they are close together, use the fallback
2082 ** method (assume that the scan visits 1/64 of the rows) for estimating
2083 ** the number of rows visited. Otherwise, estimate the number of rows
2084 ** using the method described in the header comment for this function. */
2085 if( nDiff!=1 || pUpper==0 || pLower==0 ){
2086 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
2087 pLoop->nOut -= nAdjust;
2088 *pbDone = 1;
2089 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
2090 nLower, nUpper, nAdjust*-1, pLoop->nOut));
2093 }else{
2094 assert( *pbDone==0 );
2097 sqlite3ValueFree(p1);
2098 sqlite3ValueFree(p2);
2099 sqlite3ValueFree(pVal);
2101 return rc;
2103 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
2106 ** This function is used to estimate the number of rows that will be visited
2107 ** by scanning an index for a range of values. The range may have an upper
2108 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
2109 ** and lower bounds are represented by pLower and pUpper respectively. For
2110 ** example, assuming that index p is on t1(a):
2112 ** ... FROM t1 WHERE a > ? AND a < ? ...
2113 ** |_____| |_____|
2114 ** | |
2115 ** pLower pUpper
2117 ** If either of the upper or lower bound is not present, then NULL is passed in
2118 ** place of the corresponding WhereTerm.
2120 ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
2121 ** column subject to the range constraint. Or, equivalently, the number of
2122 ** equality constraints optimized by the proposed index scan. For example,
2123 ** assuming index p is on t1(a, b), and the SQL query is:
2125 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
2127 ** then nEq is set to 1 (as the range restricted column, b, is the second
2128 ** left-most column of the index). Or, if the query is:
2130 ** ... FROM t1 WHERE a > ? AND a < ? ...
2132 ** then nEq is set to 0.
2134 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
2135 ** number of rows that the index scan is expected to visit without
2136 ** considering the range constraints. If nEq is 0, this is the number of
2137 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
2138 ** to account for the range constraints pLower and pUpper.
2140 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
2141 ** used, a single range inequality reduces the search space by a factor of 4.
2142 ** and a pair of constraints (x>? AND x<?) reduces the expected number of
2143 ** rows visited by a factor of 64.
2145 static int whereRangeScanEst(
2146 Parse *pParse, /* Parsing & code generating context */
2147 WhereLoopBuilder *pBuilder,
2148 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
2149 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
2150 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
2152 int rc = SQLITE_OK;
2153 int nOut = pLoop->nOut;
2154 LogEst nNew;
2156 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
2157 Index *p = pLoop->u.btree.pIndex;
2158 int nEq = pLoop->u.btree.nEq;
2160 if( p->nSample>0
2161 && nEq<p->nSampleCol
2162 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
2164 if( nEq==pBuilder->nRecValid ){
2165 UnpackedRecord *pRec = pBuilder->pRec;
2166 tRowcnt a[2];
2167 u8 aff;
2169 /* Variable iLower will be set to the estimate of the number of rows in
2170 ** the index that are less than the lower bound of the range query. The
2171 ** lower bound being the concatenation of $P and $L, where $P is the
2172 ** key-prefix formed by the nEq values matched against the nEq left-most
2173 ** columns of the index, and $L is the value in pLower.
2175 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
2176 ** is not a simple variable or literal value), the lower bound of the
2177 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
2178 ** if $L is available, whereKeyStats() is called for both ($P) and
2179 ** ($P:$L) and the larger of the two returned values used.
2181 ** Similarly, iUpper is to be set to the estimate of the number of rows
2182 ** less than the upper bound of the range query. Where the upper bound
2183 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
2184 ** of iUpper are requested of whereKeyStats() and the smaller used.
2186 tRowcnt iLower;
2187 tRowcnt iUpper;
2189 if( pRec ){
2190 testcase( pRec->nField!=pBuilder->nRecValid );
2191 pRec->nField = pBuilder->nRecValid;
2193 if( nEq==p->nKeyCol ){
2194 aff = SQLITE_AFF_INTEGER;
2195 }else{
2196 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
2198 /* Determine iLower and iUpper using ($P) only. */
2199 if( nEq==0 ){
2200 iLower = 0;
2201 iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
2202 }else{
2203 /* Note: this call could be optimized away - since the same values must
2204 ** have been requested when testing key $P in whereEqualScanEst(). */
2205 whereKeyStats(pParse, p, pRec, 0, a);
2206 iLower = a[0];
2207 iUpper = a[0] + a[1];
2210 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
2211 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
2212 assert( p->aSortOrder!=0 );
2213 if( p->aSortOrder[nEq] ){
2214 /* The roles of pLower and pUpper are swapped for a DESC index */
2215 SWAP(WhereTerm*, pLower, pUpper);
2218 /* If possible, improve on the iLower estimate using ($P:$L). */
2219 if( pLower ){
2220 int bOk; /* True if value is extracted from pExpr */
2221 Expr *pExpr = pLower->pExpr->pRight;
2222 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
2223 if( rc==SQLITE_OK && bOk ){
2224 tRowcnt iNew;
2225 whereKeyStats(pParse, p, pRec, 0, a);
2226 iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
2227 if( iNew>iLower ) iLower = iNew;
2228 nOut--;
2229 pLower = 0;
2233 /* If possible, improve on the iUpper estimate using ($P:$U). */
2234 if( pUpper ){
2235 int bOk; /* True if value is extracted from pExpr */
2236 Expr *pExpr = pUpper->pExpr->pRight;
2237 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
2238 if( rc==SQLITE_OK && bOk ){
2239 tRowcnt iNew;
2240 whereKeyStats(pParse, p, pRec, 1, a);
2241 iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
2242 if( iNew<iUpper ) iUpper = iNew;
2243 nOut--;
2244 pUpper = 0;
2248 pBuilder->pRec = pRec;
2249 if( rc==SQLITE_OK ){
2250 if( iUpper>iLower ){
2251 nNew = sqlite3LogEst(iUpper - iLower);
2252 }else{
2253 nNew = 10; assert( 10==sqlite3LogEst(2) );
2255 if( nNew<nOut ){
2256 nOut = nNew;
2258 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
2259 (u32)iLower, (u32)iUpper, nOut));
2261 }else{
2262 int bDone = 0;
2263 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
2264 if( bDone ) return rc;
2267 #else
2268 UNUSED_PARAMETER(pParse);
2269 UNUSED_PARAMETER(pBuilder);
2270 assert( pLower || pUpper );
2271 #endif
2272 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
2273 nNew = whereRangeAdjust(pLower, nOut);
2274 nNew = whereRangeAdjust(pUpper, nNew);
2276 /* TUNING: If there is both an upper and lower limit, assume the range is
2277 ** reduced by an additional 75%. This means that, by default, an open-ended
2278 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
2279 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
2280 ** match 1/64 of the index. */
2281 if( pLower && pUpper ) nNew -= 20;
2283 nOut -= (pLower!=0) + (pUpper!=0);
2284 if( nNew<10 ) nNew = 10;
2285 if( nNew<nOut ) nOut = nNew;
2286 #if defined(WHERETRACE_ENABLED)
2287 if( pLoop->nOut>nOut ){
2288 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
2289 pLoop->nOut, nOut));
2291 #endif
2292 pLoop->nOut = (LogEst)nOut;
2293 return rc;
2296 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
2298 ** Estimate the number of rows that will be returned based on
2299 ** an equality constraint x=VALUE and where that VALUE occurs in
2300 ** the histogram data. This only works when x is the left-most
2301 ** column of an index and sqlite_stat3 histogram data is available
2302 ** for that index. When pExpr==NULL that means the constraint is
2303 ** "x IS NULL" instead of "x=VALUE".
2305 ** Write the estimated row count into *pnRow and return SQLITE_OK.
2306 ** If unable to make an estimate, leave *pnRow unchanged and return
2307 ** non-zero.
2309 ** This routine can fail if it is unable to load a collating sequence
2310 ** required for string comparison, or if unable to allocate memory
2311 ** for a UTF conversion required for comparison. The error is stored
2312 ** in the pParse structure.
2314 static int whereEqualScanEst(
2315 Parse *pParse, /* Parsing & code generating context */
2316 WhereLoopBuilder *pBuilder,
2317 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
2318 tRowcnt *pnRow /* Write the revised row estimate here */
2320 Index *p = pBuilder->pNew->u.btree.pIndex;
2321 int nEq = pBuilder->pNew->u.btree.nEq;
2322 UnpackedRecord *pRec = pBuilder->pRec;
2323 u8 aff; /* Column affinity */
2324 int rc; /* Subfunction return code */
2325 tRowcnt a[2]; /* Statistics */
2326 int bOk;
2328 assert( nEq>=1 );
2329 assert( nEq<=p->nColumn );
2330 assert( p->aSample!=0 );
2331 assert( p->nSample>0 );
2332 assert( pBuilder->nRecValid<nEq );
2334 /* If values are not available for all fields of the index to the left
2335 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
2336 if( pBuilder->nRecValid<(nEq-1) ){
2337 return SQLITE_NOTFOUND;
2340 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
2341 ** below would return the same value. */
2342 if( nEq>=p->nColumn ){
2343 *pnRow = 1;
2344 return SQLITE_OK;
2347 aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
2348 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
2349 pBuilder->pRec = pRec;
2350 if( rc!=SQLITE_OK ) return rc;
2351 if( bOk==0 ) return SQLITE_NOTFOUND;
2352 pBuilder->nRecValid = nEq;
2354 whereKeyStats(pParse, p, pRec, 0, a);
2355 WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1]));
2356 *pnRow = a[1];
2358 return rc;
2360 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
2362 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
2364 ** Estimate the number of rows that will be returned based on
2365 ** an IN constraint where the right-hand side of the IN operator
2366 ** is a list of values. Example:
2368 ** WHERE x IN (1,2,3,4)
2370 ** Write the estimated row count into *pnRow and return SQLITE_OK.
2371 ** If unable to make an estimate, leave *pnRow unchanged and return
2372 ** non-zero.
2374 ** This routine can fail if it is unable to load a collating sequence
2375 ** required for string comparison, or if unable to allocate memory
2376 ** for a UTF conversion required for comparison. The error is stored
2377 ** in the pParse structure.
2379 static int whereInScanEst(
2380 Parse *pParse, /* Parsing & code generating context */
2381 WhereLoopBuilder *pBuilder,
2382 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
2383 tRowcnt *pnRow /* Write the revised row estimate here */
2385 Index *p = pBuilder->pNew->u.btree.pIndex;
2386 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
2387 int nRecValid = pBuilder->nRecValid;
2388 int rc = SQLITE_OK; /* Subfunction return code */
2389 tRowcnt nEst; /* Number of rows for a single term */
2390 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
2391 int i; /* Loop counter */
2393 assert( p->aSample!=0 );
2394 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
2395 nEst = nRow0;
2396 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
2397 nRowEst += nEst;
2398 pBuilder->nRecValid = nRecValid;
2401 if( rc==SQLITE_OK ){
2402 if( nRowEst > nRow0 ) nRowEst = nRow0;
2403 *pnRow = nRowEst;
2404 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
2406 assert( pBuilder->nRecValid==nRecValid );
2407 return rc;
2409 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
2412 ** Disable a term in the WHERE clause. Except, do not disable the term
2413 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
2414 ** or USING clause of that join.
2416 ** Consider the term t2.z='ok' in the following queries:
2418 ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
2419 ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
2420 ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
2422 ** The t2.z='ok' is disabled in the in (2) because it originates
2423 ** in the ON clause. The term is disabled in (3) because it is not part
2424 ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
2426 ** Disabling a term causes that term to not be tested in the inner loop
2427 ** of the join. Disabling is an optimization. When terms are satisfied
2428 ** by indices, we disable them to prevent redundant tests in the inner
2429 ** loop. We would get the correct results if nothing were ever disabled,
2430 ** but joins might run a little slower. The trick is to disable as much
2431 ** as we can without disabling too much. If we disabled in (1), we'd get
2432 ** the wrong answer. See ticket #813.
2434 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
2435 if( pTerm
2436 && (pTerm->wtFlags & TERM_CODED)==0
2437 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
2438 && (pLevel->notReady & pTerm->prereqAll)==0
2440 pTerm->wtFlags |= TERM_CODED;
2441 if( pTerm->iParent>=0 ){
2442 WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
2443 if( (--pOther->nChild)==0 ){
2444 disableTerm(pLevel, pOther);
2451 ** Code an OP_Affinity opcode to apply the column affinity string zAff
2452 ** to the n registers starting at base.
2454 ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
2455 ** beginning and end of zAff are ignored. If all entries in zAff are
2456 ** SQLITE_AFF_NONE, then no code gets generated.
2458 ** This routine makes its own copy of zAff so that the caller is free
2459 ** to modify zAff after this routine returns.
2461 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
2462 Vdbe *v = pParse->pVdbe;
2463 if( zAff==0 ){
2464 assert( pParse->db->mallocFailed );
2465 return;
2467 assert( v!=0 );
2469 /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
2470 ** and end of the affinity string.
2472 while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
2473 n--;
2474 base++;
2475 zAff++;
2477 while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
2478 n--;
2481 /* Code the OP_Affinity opcode if there is anything left to do. */
2482 if( n>0 ){
2483 sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
2484 sqlite3VdbeChangeP4(v, -1, zAff, n);
2485 sqlite3ExprCacheAffinityChange(pParse, base, n);
2491 ** Generate code for a single equality term of the WHERE clause. An equality
2492 ** term can be either X=expr or X IN (...). pTerm is the term to be
2493 ** coded.
2495 ** The current value for the constraint is left in register iReg.
2497 ** For a constraint of the form X=expr, the expression is evaluated and its
2498 ** result is left on the stack. For constraints of the form X IN (...)
2499 ** this routine sets up a loop that will iterate over all values of X.
2501 static int codeEqualityTerm(
2502 Parse *pParse, /* The parsing context */
2503 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
2504 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
2505 int iEq, /* Index of the equality term within this level */
2506 int bRev, /* True for reverse-order IN operations */
2507 int iTarget /* Attempt to leave results in this register */
2509 Expr *pX = pTerm->pExpr;
2510 Vdbe *v = pParse->pVdbe;
2511 int iReg; /* Register holding results */
2513 assert( iTarget>0 );
2514 if( pX->op==TK_EQ ){
2515 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
2516 }else if( pX->op==TK_ISNULL ){
2517 iReg = iTarget;
2518 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
2519 #ifndef SQLITE_OMIT_SUBQUERY
2520 }else{
2521 int eType;
2522 int iTab;
2523 struct InLoop *pIn;
2524 WhereLoop *pLoop = pLevel->pWLoop;
2526 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
2527 && pLoop->u.btree.pIndex!=0
2528 && pLoop->u.btree.pIndex->aSortOrder[iEq]
2530 testcase( iEq==0 );
2531 testcase( bRev );
2532 bRev = !bRev;
2534 assert( pX->op==TK_IN );
2535 iReg = iTarget;
2536 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
2537 if( eType==IN_INDEX_INDEX_DESC ){
2538 testcase( bRev );
2539 bRev = !bRev;
2541 iTab = pX->iTable;
2542 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
2543 VdbeCoverageIf(v, bRev);
2544 VdbeCoverageIf(v, !bRev);
2545 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
2546 pLoop->wsFlags |= WHERE_IN_ABLE;
2547 if( pLevel->u.in.nIn==0 ){
2548 pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
2550 pLevel->u.in.nIn++;
2551 pLevel->u.in.aInLoop =
2552 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
2553 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
2554 pIn = pLevel->u.in.aInLoop;
2555 if( pIn ){
2556 pIn += pLevel->u.in.nIn - 1;
2557 pIn->iCur = iTab;
2558 if( eType==IN_INDEX_ROWID ){
2559 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
2560 }else{
2561 pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
2563 pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
2564 sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
2565 }else{
2566 pLevel->u.in.nIn = 0;
2568 #endif
2570 disableTerm(pLevel, pTerm);
2571 return iReg;
2575 ** Generate code that will evaluate all == and IN constraints for an
2576 ** index scan.
2578 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
2579 ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
2580 ** The index has as many as three equality constraints, but in this
2581 ** example, the third "c" value is an inequality. So only two
2582 ** constraints are coded. This routine will generate code to evaluate
2583 ** a==5 and b IN (1,2,3). The current values for a and b will be stored
2584 ** in consecutive registers and the index of the first register is returned.
2586 ** In the example above nEq==2. But this subroutine works for any value
2587 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
2588 ** The only thing it does is allocate the pLevel->iMem memory cell and
2589 ** compute the affinity string.
2591 ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
2592 ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
2593 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
2594 ** occurs after the nEq quality constraints.
2596 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
2597 ** the index of the first memory cell in that range. The code that
2598 ** calls this routine will use that memory range to store keys for
2599 ** start and termination conditions of the loop.
2600 ** key value of the loop. If one or more IN operators appear, then
2601 ** this routine allocates an additional nEq memory cells for internal
2602 ** use.
2604 ** Before returning, *pzAff is set to point to a buffer containing a
2605 ** copy of the column affinity string of the index allocated using
2606 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
2607 ** with equality constraints that use NONE affinity are set to
2608 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
2610 ** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
2611 ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
2613 ** In the example above, the index on t1(a) has TEXT affinity. But since
2614 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
2615 ** no conversion should be attempted before using a t2.b value as part of
2616 ** a key to search the index. Hence the first byte in the returned affinity
2617 ** string in this example would be set to SQLITE_AFF_NONE.
2619 static int codeAllEqualityTerms(
2620 Parse *pParse, /* Parsing context */
2621 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
2622 int bRev, /* Reverse the order of IN operators */
2623 int nExtraReg, /* Number of extra registers to allocate */
2624 char **pzAff /* OUT: Set to point to affinity string */
2626 u16 nEq; /* The number of == or IN constraints to code */
2627 u16 nSkip; /* Number of left-most columns to skip */
2628 Vdbe *v = pParse->pVdbe; /* The vm under construction */
2629 Index *pIdx; /* The index being used for this loop */
2630 WhereTerm *pTerm; /* A single constraint term */
2631 WhereLoop *pLoop; /* The WhereLoop object */
2632 int j; /* Loop counter */
2633 int regBase; /* Base register */
2634 int nReg; /* Number of registers to allocate */
2635 char *zAff; /* Affinity string to return */
2637 /* This module is only called on query plans that use an index. */
2638 pLoop = pLevel->pWLoop;
2639 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
2640 nEq = pLoop->u.btree.nEq;
2641 nSkip = pLoop->u.btree.nSkip;
2642 pIdx = pLoop->u.btree.pIndex;
2643 assert( pIdx!=0 );
2645 /* Figure out how many memory cells we will need then allocate them.
2647 regBase = pParse->nMem + 1;
2648 nReg = pLoop->u.btree.nEq + nExtraReg;
2649 pParse->nMem += nReg;
2651 zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
2652 if( !zAff ){
2653 pParse->db->mallocFailed = 1;
2656 if( nSkip ){
2657 int iIdxCur = pLevel->iIdxCur;
2658 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
2659 VdbeCoverageIf(v, bRev==0);
2660 VdbeCoverageIf(v, bRev!=0);
2661 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
2662 j = sqlite3VdbeAddOp0(v, OP_Goto);
2663 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
2664 iIdxCur, 0, regBase, nSkip);
2665 VdbeCoverageIf(v, bRev==0);
2666 VdbeCoverageIf(v, bRev!=0);
2667 sqlite3VdbeJumpHere(v, j);
2668 for(j=0; j<nSkip; j++){
2669 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
2670 assert( pIdx->aiColumn[j]>=0 );
2671 VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
2675 /* Evaluate the equality constraints
2677 assert( zAff==0 || (int)strlen(zAff)>=nEq );
2678 for(j=nSkip; j<nEq; j++){
2679 int r1;
2680 pTerm = pLoop->aLTerm[j];
2681 assert( pTerm!=0 );
2682 /* The following testcase is true for indices with redundant columns.
2683 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
2684 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
2685 testcase( pTerm->wtFlags & TERM_VIRTUAL );
2686 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
2687 if( r1!=regBase+j ){
2688 if( nReg==1 ){
2689 sqlite3ReleaseTempReg(pParse, regBase);
2690 regBase = r1;
2691 }else{
2692 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
2695 testcase( pTerm->eOperator & WO_ISNULL );
2696 testcase( pTerm->eOperator & WO_IN );
2697 if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
2698 Expr *pRight = pTerm->pExpr->pRight;
2699 if( sqlite3ExprCanBeNull(pRight) ){
2700 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
2701 VdbeCoverage(v);
2703 if( zAff ){
2704 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
2705 zAff[j] = SQLITE_AFF_NONE;
2707 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
2708 zAff[j] = SQLITE_AFF_NONE;
2713 *pzAff = zAff;
2714 return regBase;
2717 #ifndef SQLITE_OMIT_EXPLAIN
2719 ** This routine is a helper for explainIndexRange() below
2721 ** pStr holds the text of an expression that we are building up one term
2722 ** at a time. This routine adds a new term to the end of the expression.
2723 ** Terms are separated by AND so add the "AND" text for second and subsequent
2724 ** terms only.
2726 static void explainAppendTerm(
2727 StrAccum *pStr, /* The text expression being built */
2728 int iTerm, /* Index of this term. First is zero */
2729 const char *zColumn, /* Name of the column */
2730 const char *zOp /* Name of the operator */
2732 if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
2733 sqlite3StrAccumAppendAll(pStr, zColumn);
2734 sqlite3StrAccumAppend(pStr, zOp, 1);
2735 sqlite3StrAccumAppend(pStr, "?", 1);
2739 ** Argument pLevel describes a strategy for scanning table pTab. This
2740 ** function appends text to pStr that describes the subset of table
2741 ** rows scanned by the strategy in the form of an SQL expression.
2743 ** For example, if the query:
2745 ** SELECT * FROM t1 WHERE a=1 AND b>2;
2747 ** is run and there is an index on (a, b), then this function returns a
2748 ** string similar to:
2750 ** "a=? AND b>?"
2752 static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
2753 Index *pIndex = pLoop->u.btree.pIndex;
2754 u16 nEq = pLoop->u.btree.nEq;
2755 u16 nSkip = pLoop->u.btree.nSkip;
2756 int i, j;
2757 Column *aCol = pTab->aCol;
2758 i16 *aiColumn = pIndex->aiColumn;
2760 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
2761 sqlite3StrAccumAppend(pStr, " (", 2);
2762 for(i=0; i<nEq; i++){
2763 char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
2764 if( i>=nSkip ){
2765 explainAppendTerm(pStr, i, z, "=");
2766 }else{
2767 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
2768 sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
2772 j = i;
2773 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
2774 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
2775 explainAppendTerm(pStr, i++, z, ">");
2777 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
2778 char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
2779 explainAppendTerm(pStr, i, z, "<");
2781 sqlite3StrAccumAppend(pStr, ")", 1);
2785 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
2786 ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
2787 ** record is added to the output to describe the table scan strategy in
2788 ** pLevel.
2790 static void explainOneScan(
2791 Parse *pParse, /* Parse context */
2792 SrcList *pTabList, /* Table list this loop refers to */
2793 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
2794 int iLevel, /* Value for "level" column of output */
2795 int iFrom, /* Value for "from" column of output */
2796 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
2798 #ifndef SQLITE_DEBUG
2799 if( pParse->explain==2 )
2800 #endif
2802 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
2803 Vdbe *v = pParse->pVdbe; /* VM being constructed */
2804 sqlite3 *db = pParse->db; /* Database handle */
2805 int iId = pParse->iSelectId; /* Select id (left-most output column) */
2806 int isSearch; /* True for a SEARCH. False for SCAN. */
2807 WhereLoop *pLoop; /* The controlling WhereLoop object */
2808 u32 flags; /* Flags that describe this loop */
2809 char *zMsg; /* Text to add to EQP output */
2810 StrAccum str; /* EQP output string */
2811 char zBuf[100]; /* Initial space for EQP output string */
2813 pLoop = pLevel->pWLoop;
2814 flags = pLoop->wsFlags;
2815 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
2817 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
2818 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
2819 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
2821 sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
2822 str.db = db;
2823 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
2824 if( pItem->pSelect ){
2825 sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
2826 }else{
2827 sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
2830 if( pItem->zAlias ){
2831 sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
2833 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
2834 const char *zFmt = 0;
2835 Index *pIdx;
2837 assert( pLoop->u.btree.pIndex!=0 );
2838 pIdx = pLoop->u.btree.pIndex;
2839 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
2840 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
2841 if( isSearch ){
2842 zFmt = "PRIMARY KEY";
2844 }else if( flags & WHERE_AUTO_INDEX ){
2845 zFmt = "AUTOMATIC COVERING INDEX";
2846 }else if( flags & WHERE_IDX_ONLY ){
2847 zFmt = "COVERING INDEX %s";
2848 }else{
2849 zFmt = "INDEX %s";
2851 if( zFmt ){
2852 sqlite3StrAccumAppend(&str, " USING ", 7);
2853 sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
2854 explainIndexRange(&str, pLoop, pItem->pTab);
2856 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
2857 const char *zRange;
2858 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
2859 zRange = "(rowid=?)";
2860 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
2861 zRange = "(rowid>? AND rowid<?)";
2862 }else if( flags&WHERE_BTM_LIMIT ){
2863 zRange = "(rowid>?)";
2864 }else{
2865 assert( flags&WHERE_TOP_LIMIT);
2866 zRange = "(rowid<?)";
2868 sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
2869 sqlite3StrAccumAppendAll(&str, zRange);
2871 #ifndef SQLITE_OMIT_VIRTUALTABLE
2872 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
2873 sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
2874 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
2876 #endif
2877 #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
2878 if( pLoop->nOut>=10 ){
2879 sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
2880 }else{
2881 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
2883 #endif
2884 zMsg = sqlite3StrAccumFinish(&str);
2885 sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
2888 #else
2889 # define explainOneScan(u,v,w,x,y,z)
2890 #endif /* SQLITE_OMIT_EXPLAIN */
2894 ** Generate code for the start of the iLevel-th loop in the WHERE clause
2895 ** implementation described by pWInfo.
2897 static Bitmask codeOneLoopStart(
2898 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
2899 int iLevel, /* Which level of pWInfo->a[] should be coded */
2900 Bitmask notReady /* Which tables are currently available */
2902 int j, k; /* Loop counters */
2903 int iCur; /* The VDBE cursor for the table */
2904 int addrNxt; /* Where to jump to continue with the next IN case */
2905 int omitTable; /* True if we use the index only */
2906 int bRev; /* True if we need to scan in reverse order */
2907 WhereLevel *pLevel; /* The where level to be coded */
2908 WhereLoop *pLoop; /* The WhereLoop object being coded */
2909 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
2910 WhereTerm *pTerm; /* A WHERE clause term */
2911 Parse *pParse; /* Parsing context */
2912 sqlite3 *db; /* Database connection */
2913 Vdbe *v; /* The prepared stmt under constructions */
2914 struct SrcList_item *pTabItem; /* FROM clause term being coded */
2915 int addrBrk; /* Jump here to break out of the loop */
2916 int addrCont; /* Jump here to continue with next cycle */
2917 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
2918 int iReleaseReg = 0; /* Temp register to free before returning */
2920 pParse = pWInfo->pParse;
2921 v = pParse->pVdbe;
2922 pWC = &pWInfo->sWC;
2923 db = pParse->db;
2924 pLevel = &pWInfo->a[iLevel];
2925 pLoop = pLevel->pWLoop;
2926 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
2927 iCur = pTabItem->iCursor;
2928 pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
2929 bRev = (pWInfo->revMask>>iLevel)&1;
2930 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
2931 && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
2932 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
2934 /* Create labels for the "break" and "continue" instructions
2935 ** for the current loop. Jump to addrBrk to break out of a loop.
2936 ** Jump to cont to go immediately to the next iteration of the
2937 ** loop.
2939 ** When there is an IN operator, we also have a "addrNxt" label that
2940 ** means to continue with the next IN value combination. When
2941 ** there are no IN operators in the constraints, the "addrNxt" label
2942 ** is the same as "addrBrk".
2944 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
2945 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
2947 /* If this is the right table of a LEFT OUTER JOIN, allocate and
2948 ** initialize a memory cell that records if this table matches any
2949 ** row of the left table of the join.
2951 if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
2952 pLevel->iLeftJoin = ++pParse->nMem;
2953 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
2954 VdbeComment((v, "init LEFT JOIN no-match flag"));
2957 /* Special case of a FROM clause subquery implemented as a co-routine */
2958 if( pTabItem->viaCoroutine ){
2959 int regYield = pTabItem->regReturn;
2960 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
2961 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
2962 VdbeCoverage(v);
2963 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
2964 pLevel->op = OP_Goto;
2965 }else
2967 #ifndef SQLITE_OMIT_VIRTUALTABLE
2968 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
2969 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
2970 ** to access the data.
2972 int iReg; /* P3 Value for OP_VFilter */
2973 int addrNotFound;
2974 int nConstraint = pLoop->nLTerm;
2976 sqlite3ExprCachePush(pParse);
2977 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
2978 addrNotFound = pLevel->addrBrk;
2979 for(j=0; j<nConstraint; j++){
2980 int iTarget = iReg+j+2;
2981 pTerm = pLoop->aLTerm[j];
2982 if( pTerm==0 ) continue;
2983 if( pTerm->eOperator & WO_IN ){
2984 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
2985 addrNotFound = pLevel->addrNxt;
2986 }else{
2987 sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
2990 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
2991 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
2992 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
2993 pLoop->u.vtab.idxStr,
2994 pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
2995 VdbeCoverage(v);
2996 pLoop->u.vtab.needFree = 0;
2997 for(j=0; j<nConstraint && j<16; j++){
2998 if( (pLoop->u.vtab.omitMask>>j)&1 ){
2999 disableTerm(pLevel, pLoop->aLTerm[j]);
3002 pLevel->op = OP_VNext;
3003 pLevel->p1 = iCur;
3004 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
3005 sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
3006 sqlite3ExprCachePop(pParse);
3007 }else
3008 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3010 if( (pLoop->wsFlags & WHERE_IPK)!=0
3011 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
3013 /* Case 2: We can directly reference a single row using an
3014 ** equality comparison against the ROWID field. Or
3015 ** we reference multiple rows using a "rowid IN (...)"
3016 ** construct.
3018 assert( pLoop->u.btree.nEq==1 );
3019 pTerm = pLoop->aLTerm[0];
3020 assert( pTerm!=0 );
3021 assert( pTerm->pExpr!=0 );
3022 assert( omitTable==0 );
3023 testcase( pTerm->wtFlags & TERM_VIRTUAL );
3024 iReleaseReg = ++pParse->nMem;
3025 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
3026 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
3027 addrNxt = pLevel->addrNxt;
3028 sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
3029 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
3030 VdbeCoverage(v);
3031 sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
3032 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
3033 VdbeComment((v, "pk"));
3034 pLevel->op = OP_Noop;
3035 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
3036 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
3038 /* Case 3: We have an inequality comparison against the ROWID field.
3040 int testOp = OP_Noop;
3041 int start;
3042 int memEndValue = 0;
3043 WhereTerm *pStart, *pEnd;
3045 assert( omitTable==0 );
3046 j = 0;
3047 pStart = pEnd = 0;
3048 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
3049 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
3050 assert( pStart!=0 || pEnd!=0 );
3051 if( bRev ){
3052 pTerm = pStart;
3053 pStart = pEnd;
3054 pEnd = pTerm;
3056 if( pStart ){
3057 Expr *pX; /* The expression that defines the start bound */
3058 int r1, rTemp; /* Registers for holding the start boundary */
3060 /* The following constant maps TK_xx codes into corresponding
3061 ** seek opcodes. It depends on a particular ordering of TK_xx
3063 const u8 aMoveOp[] = {
3064 /* TK_GT */ OP_SeekGT,
3065 /* TK_LE */ OP_SeekLE,
3066 /* TK_LT */ OP_SeekLT,
3067 /* TK_GE */ OP_SeekGE
3069 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */
3070 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */
3071 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */
3073 assert( (pStart->wtFlags & TERM_VNULL)==0 );
3074 testcase( pStart->wtFlags & TERM_VIRTUAL );
3075 pX = pStart->pExpr;
3076 assert( pX!=0 );
3077 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
3078 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
3079 sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
3080 VdbeComment((v, "pk"));
3081 VdbeCoverageIf(v, pX->op==TK_GT);
3082 VdbeCoverageIf(v, pX->op==TK_LE);
3083 VdbeCoverageIf(v, pX->op==TK_LT);
3084 VdbeCoverageIf(v, pX->op==TK_GE);
3085 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
3086 sqlite3ReleaseTempReg(pParse, rTemp);
3087 disableTerm(pLevel, pStart);
3088 }else{
3089 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
3090 VdbeCoverageIf(v, bRev==0);
3091 VdbeCoverageIf(v, bRev!=0);
3093 if( pEnd ){
3094 Expr *pX;
3095 pX = pEnd->pExpr;
3096 assert( pX!=0 );
3097 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
3098 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
3099 testcase( pEnd->wtFlags & TERM_VIRTUAL );
3100 memEndValue = ++pParse->nMem;
3101 sqlite3ExprCode(pParse, pX->pRight, memEndValue);
3102 if( pX->op==TK_LT || pX->op==TK_GT ){
3103 testOp = bRev ? OP_Le : OP_Ge;
3104 }else{
3105 testOp = bRev ? OP_Lt : OP_Gt;
3107 disableTerm(pLevel, pEnd);
3109 start = sqlite3VdbeCurrentAddr(v);
3110 pLevel->op = bRev ? OP_Prev : OP_Next;
3111 pLevel->p1 = iCur;
3112 pLevel->p2 = start;
3113 assert( pLevel->p5==0 );
3114 if( testOp!=OP_Noop ){
3115 iRowidReg = ++pParse->nMem;
3116 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
3117 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
3118 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
3119 VdbeCoverageIf(v, testOp==OP_Le);
3120 VdbeCoverageIf(v, testOp==OP_Lt);
3121 VdbeCoverageIf(v, testOp==OP_Ge);
3122 VdbeCoverageIf(v, testOp==OP_Gt);
3123 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
3125 }else if( pLoop->wsFlags & WHERE_INDEXED ){
3126 /* Case 4: A scan using an index.
3128 ** The WHERE clause may contain zero or more equality
3129 ** terms ("==" or "IN" operators) that refer to the N
3130 ** left-most columns of the index. It may also contain
3131 ** inequality constraints (>, <, >= or <=) on the indexed
3132 ** column that immediately follows the N equalities. Only
3133 ** the right-most column can be an inequality - the rest must
3134 ** use the "==" and "IN" operators. For example, if the
3135 ** index is on (x,y,z), then the following clauses are all
3136 ** optimized:
3138 ** x=5
3139 ** x=5 AND y=10
3140 ** x=5 AND y<10
3141 ** x=5 AND y>5 AND y<10
3142 ** x=5 AND y=5 AND z<=10
3144 ** The z<10 term of the following cannot be used, only
3145 ** the x=5 term:
3147 ** x=5 AND z<10
3149 ** N may be zero if there are inequality constraints.
3150 ** If there are no inequality constraints, then N is at
3151 ** least one.
3153 ** This case is also used when there are no WHERE clause
3154 ** constraints but an index is selected anyway, in order
3155 ** to force the output order to conform to an ORDER BY.
3157 static const u8 aStartOp[] = {
3160 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
3161 OP_Last, /* 3: (!start_constraints && startEq && bRev) */
3162 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */
3163 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */
3164 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */
3165 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */
3167 static const u8 aEndOp[] = {
3168 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */
3169 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */
3170 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */
3171 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */
3173 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
3174 int regBase; /* Base register holding constraint values */
3175 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
3176 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
3177 int startEq; /* True if range start uses ==, >= or <= */
3178 int endEq; /* True if range end uses ==, >= or <= */
3179 int start_constraints; /* Start of range is constrained */
3180 int nConstraint; /* Number of constraint terms */
3181 Index *pIdx; /* The index we will be using */
3182 int iIdxCur; /* The VDBE cursor for the index */
3183 int nExtraReg = 0; /* Number of extra registers needed */
3184 int op; /* Instruction opcode */
3185 char *zStartAff; /* Affinity for start of range constraint */
3186 char cEndAff = 0; /* Affinity for end of range constraint */
3187 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
3188 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
3190 pIdx = pLoop->u.btree.pIndex;
3191 iIdxCur = pLevel->iIdxCur;
3192 assert( nEq>=pLoop->u.btree.nSkip );
3194 /* If this loop satisfies a sort order (pOrderBy) request that
3195 ** was passed to this function to implement a "SELECT min(x) ..."
3196 ** query, then the caller will only allow the loop to run for
3197 ** a single iteration. This means that the first row returned
3198 ** should not have a NULL value stored in 'x'. If column 'x' is
3199 ** the first one after the nEq equality constraints in the index,
3200 ** this requires some special handling.
3202 assert( pWInfo->pOrderBy==0
3203 || pWInfo->pOrderBy->nExpr==1
3204 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
3205 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
3206 && pWInfo->nOBSat>0
3207 && (pIdx->nKeyCol>nEq)
3209 assert( pLoop->u.btree.nSkip==0 );
3210 bSeekPastNull = 1;
3211 nExtraReg = 1;
3214 /* Find any inequality constraint terms for the start and end
3215 ** of the range.
3217 j = nEq;
3218 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
3219 pRangeStart = pLoop->aLTerm[j++];
3220 nExtraReg = 1;
3222 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
3223 pRangeEnd = pLoop->aLTerm[j++];
3224 nExtraReg = 1;
3225 if( pRangeStart==0
3226 && (j = pIdx->aiColumn[nEq])>=0
3227 && pIdx->pTable->aCol[j].notNull==0
3229 bSeekPastNull = 1;
3232 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
3234 /* Generate code to evaluate all constraint terms using == or IN
3235 ** and store the values of those terms in an array of registers
3236 ** starting at regBase.
3238 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
3239 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
3240 if( zStartAff ) cEndAff = zStartAff[nEq];
3241 addrNxt = pLevel->addrNxt;
3243 /* If we are doing a reverse order scan on an ascending index, or
3244 ** a forward order scan on a descending index, interchange the
3245 ** start and end terms (pRangeStart and pRangeEnd).
3247 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
3248 || (bRev && pIdx->nKeyCol==nEq)
3250 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
3251 SWAP(u8, bSeekPastNull, bStopAtNull);
3254 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
3255 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
3256 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
3257 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
3258 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
3259 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
3260 start_constraints = pRangeStart || nEq>0;
3262 /* Seek the index cursor to the start of the range. */
3263 nConstraint = nEq;
3264 if( pRangeStart ){
3265 Expr *pRight = pRangeStart->pExpr->pRight;
3266 sqlite3ExprCode(pParse, pRight, regBase+nEq);
3267 if( (pRangeStart->wtFlags & TERM_VNULL)==0
3268 && sqlite3ExprCanBeNull(pRight)
3270 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
3271 VdbeCoverage(v);
3273 if( zStartAff ){
3274 if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
3275 /* Since the comparison is to be performed with no conversions
3276 ** applied to the operands, set the affinity to apply to pRight to
3277 ** SQLITE_AFF_NONE. */
3278 zStartAff[nEq] = SQLITE_AFF_NONE;
3280 if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
3281 zStartAff[nEq] = SQLITE_AFF_NONE;
3284 nConstraint++;
3285 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
3286 }else if( bSeekPastNull ){
3287 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
3288 nConstraint++;
3289 startEq = 0;
3290 start_constraints = 1;
3292 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
3293 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
3294 assert( op!=0 );
3295 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
3296 VdbeCoverage(v);
3297 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
3298 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
3299 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
3300 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
3301 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
3302 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
3304 /* Load the value for the inequality constraint at the end of the
3305 ** range (if any).
3307 nConstraint = nEq;
3308 if( pRangeEnd ){
3309 Expr *pRight = pRangeEnd->pExpr->pRight;
3310 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
3311 sqlite3ExprCode(pParse, pRight, regBase+nEq);
3312 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
3313 && sqlite3ExprCanBeNull(pRight)
3315 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
3316 VdbeCoverage(v);
3318 if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
3319 && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
3321 codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
3323 nConstraint++;
3324 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
3325 }else if( bStopAtNull ){
3326 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
3327 endEq = 0;
3328 nConstraint++;
3330 sqlite3DbFree(db, zStartAff);
3332 /* Top of the loop body */
3333 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
3335 /* Check if the index cursor is past the end of the range. */
3336 if( nConstraint ){
3337 op = aEndOp[bRev*2 + endEq];
3338 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
3339 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
3340 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
3341 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
3342 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
3345 /* Seek the table cursor, if required */
3346 disableTerm(pLevel, pRangeStart);
3347 disableTerm(pLevel, pRangeEnd);
3348 if( omitTable ){
3349 /* pIdx is a covering index. No need to access the main table. */
3350 }else if( HasRowid(pIdx->pTable) ){
3351 iRowidReg = ++pParse->nMem;
3352 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
3353 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
3354 sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
3355 }else if( iCur!=iIdxCur ){
3356 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
3357 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
3358 for(j=0; j<pPk->nKeyCol; j++){
3359 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
3360 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
3362 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
3363 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
3366 /* Record the instruction used to terminate the loop. Disable
3367 ** WHERE clause terms made redundant by the index range scan.
3369 if( pLoop->wsFlags & WHERE_ONEROW ){
3370 pLevel->op = OP_Noop;
3371 }else if( bRev ){
3372 pLevel->op = OP_Prev;
3373 }else{
3374 pLevel->op = OP_Next;
3376 pLevel->p1 = iIdxCur;
3377 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
3378 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
3379 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
3380 }else{
3381 assert( pLevel->p5==0 );
3383 }else
3385 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
3386 if( pLoop->wsFlags & WHERE_MULTI_OR ){
3387 /* Case 5: Two or more separately indexed terms connected by OR
3389 ** Example:
3391 ** CREATE TABLE t1(a,b,c,d);
3392 ** CREATE INDEX i1 ON t1(a);
3393 ** CREATE INDEX i2 ON t1(b);
3394 ** CREATE INDEX i3 ON t1(c);
3396 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
3398 ** In the example, there are three indexed terms connected by OR.
3399 ** The top of the loop looks like this:
3401 ** Null 1 # Zero the rowset in reg 1
3403 ** Then, for each indexed term, the following. The arguments to
3404 ** RowSetTest are such that the rowid of the current row is inserted
3405 ** into the RowSet. If it is already present, control skips the
3406 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
3408 ** sqlite3WhereBegin(<term>)
3409 ** RowSetTest # Insert rowid into rowset
3410 ** Gosub 2 A
3411 ** sqlite3WhereEnd()
3413 ** Following the above, code to terminate the loop. Label A, the target
3414 ** of the Gosub above, jumps to the instruction right after the Goto.
3416 ** Null 1 # Zero the rowset in reg 1
3417 ** Goto B # The loop is finished.
3419 ** A: <loop body> # Return data, whatever.
3421 ** Return 2 # Jump back to the Gosub
3423 ** B: <after the loop>
3425 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
3426 ** use an ephemeral index instead of a RowSet to record the primary
3427 ** keys of the rows we have already seen.
3430 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
3431 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
3432 Index *pCov = 0; /* Potential covering index (or NULL) */
3433 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
3435 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
3436 int regRowset = 0; /* Register for RowSet object */
3437 int regRowid = 0; /* Register holding rowid */
3438 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
3439 int iRetInit; /* Address of regReturn init */
3440 int untestedTerms = 0; /* Some terms not completely tested */
3441 int ii; /* Loop counter */
3442 u16 wctrlFlags; /* Flags for sub-WHERE clause */
3443 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
3444 Table *pTab = pTabItem->pTab;
3446 pTerm = pLoop->aLTerm[0];
3447 assert( pTerm!=0 );
3448 assert( pTerm->eOperator & WO_OR );
3449 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
3450 pOrWc = &pTerm->u.pOrInfo->wc;
3451 pLevel->op = OP_Return;
3452 pLevel->p1 = regReturn;
3454 /* Set up a new SrcList in pOrTab containing the table being scanned
3455 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
3456 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
3458 if( pWInfo->nLevel>1 ){
3459 int nNotReady; /* The number of notReady tables */
3460 struct SrcList_item *origSrc; /* Original list of tables */
3461 nNotReady = pWInfo->nLevel - iLevel - 1;
3462 pOrTab = sqlite3StackAllocRaw(db,
3463 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
3464 if( pOrTab==0 ) return notReady;
3465 pOrTab->nAlloc = (u8)(nNotReady + 1);
3466 pOrTab->nSrc = pOrTab->nAlloc;
3467 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
3468 origSrc = pWInfo->pTabList->a;
3469 for(k=1; k<=nNotReady; k++){
3470 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
3472 }else{
3473 pOrTab = pWInfo->pTabList;
3476 /* Initialize the rowset register to contain NULL. An SQL NULL is
3477 ** equivalent to an empty rowset. Or, create an ephemeral index
3478 ** capable of holding primary keys in the case of a WITHOUT ROWID.
3480 ** Also initialize regReturn to contain the address of the instruction
3481 ** immediately following the OP_Return at the bottom of the loop. This
3482 ** is required in a few obscure LEFT JOIN cases where control jumps
3483 ** over the top of the loop into the body of it. In this case the
3484 ** correct response for the end-of-loop code (the OP_Return) is to
3485 ** fall through to the next instruction, just as an OP_Next does if
3486 ** called on an uninitialized cursor.
3488 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
3489 if( HasRowid(pTab) ){
3490 regRowset = ++pParse->nMem;
3491 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
3492 }else{
3493 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
3494 regRowset = pParse->nTab++;
3495 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
3496 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
3498 regRowid = ++pParse->nMem;
3500 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
3502 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
3503 ** Then for every term xN, evaluate as the subexpression: xN AND z
3504 ** That way, terms in y that are factored into the disjunction will
3505 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
3507 ** Actually, each subexpression is converted to "xN AND w" where w is
3508 ** the "interesting" terms of z - terms that did not originate in the
3509 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
3510 ** indices.
3512 ** This optimization also only applies if the (x1 OR x2 OR ...) term
3513 ** is not contained in the ON clause of a LEFT JOIN.
3514 ** See ticket http://www.sqlite.org/src/info/f2369304e4
3516 if( pWC->nTerm>1 ){
3517 int iTerm;
3518 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
3519 Expr *pExpr = pWC->a[iTerm].pExpr;
3520 if( &pWC->a[iTerm] == pTerm ) continue;
3521 if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
3522 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
3523 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
3524 if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue;
3525 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
3526 pExpr = sqlite3ExprDup(db, pExpr, 0);
3527 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
3529 if( pAndExpr ){
3530 pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
3534 /* Run a separate WHERE clause for each term of the OR clause. After
3535 ** eliminating duplicates from other WHERE clauses, the action for each
3536 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
3538 wctrlFlags = WHERE_OMIT_OPEN_CLOSE
3539 | WHERE_FORCE_TABLE
3540 | WHERE_ONETABLE_ONLY;
3541 for(ii=0; ii<pOrWc->nTerm; ii++){
3542 WhereTerm *pOrTerm = &pOrWc->a[ii];
3543 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
3544 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
3545 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
3546 int j1 = 0; /* Address of jump operation */
3547 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
3548 pAndExpr->pLeft = pOrExpr;
3549 pOrExpr = pAndExpr;
3551 /* Loop through table entries that match term pOrTerm. */
3552 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
3553 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
3554 wctrlFlags, iCovCur);
3555 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
3556 if( pSubWInfo ){
3557 WhereLoop *pSubLoop;
3558 explainOneScan(
3559 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
3561 /* This is the sub-WHERE clause body. First skip over
3562 ** duplicate rows from prior sub-WHERE clauses, and record the
3563 ** rowid (or PRIMARY KEY) for the current row so that the same
3564 ** row will be skipped in subsequent sub-WHERE clauses.
3566 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
3567 int r;
3568 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
3569 if( HasRowid(pTab) ){
3570 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
3571 j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
3572 VdbeCoverage(v);
3573 }else{
3574 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
3575 int nPk = pPk->nKeyCol;
3576 int iPk;
3578 /* Read the PK into an array of temp registers. */
3579 r = sqlite3GetTempRange(pParse, nPk);
3580 for(iPk=0; iPk<nPk; iPk++){
3581 int iCol = pPk->aiColumn[iPk];
3582 sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
3585 /* Check if the temp table already contains this key. If so,
3586 ** the row has already been included in the result set and
3587 ** can be ignored (by jumping past the Gosub below). Otherwise,
3588 ** insert the key into the temp table and proceed with processing
3589 ** the row.
3591 ** Use some of the same optimizations as OP_RowSetTest: If iSet
3592 ** is zero, assume that the key cannot already be present in
3593 ** the temp table. And if iSet is -1, assume that there is no
3594 ** need to insert the key into the temp table, as it will never
3595 ** be tested for. */
3596 if( iSet ){
3597 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
3598 VdbeCoverage(v);
3600 if( iSet>=0 ){
3601 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
3602 sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
3603 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
3606 /* Release the array of temp registers */
3607 sqlite3ReleaseTempRange(pParse, r, nPk);
3611 /* Invoke the main loop body as a subroutine */
3612 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
3614 /* Jump here (skipping the main loop body subroutine) if the
3615 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
3616 if( j1 ) sqlite3VdbeJumpHere(v, j1);
3618 /* The pSubWInfo->untestedTerms flag means that this OR term
3619 ** contained one or more AND term from a notReady table. The
3620 ** terms from the notReady table could not be tested and will
3621 ** need to be tested later.
3623 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
3625 /* If all of the OR-connected terms are optimized using the same
3626 ** index, and the index is opened using the same cursor number
3627 ** by each call to sqlite3WhereBegin() made by this loop, it may
3628 ** be possible to use that index as a covering index.
3630 ** If the call to sqlite3WhereBegin() above resulted in a scan that
3631 ** uses an index, and this is either the first OR-connected term
3632 ** processed or the index is the same as that used by all previous
3633 ** terms, set pCov to the candidate covering index. Otherwise, set
3634 ** pCov to NULL to indicate that no candidate covering index will
3635 ** be available.
3637 pSubLoop = pSubWInfo->a[0].pWLoop;
3638 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
3639 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
3640 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
3641 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
3643 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
3644 pCov = pSubLoop->u.btree.pIndex;
3645 wctrlFlags |= WHERE_REOPEN_IDX;
3646 }else{
3647 pCov = 0;
3650 /* Finish the loop through table entries that match term pOrTerm. */
3651 sqlite3WhereEnd(pSubWInfo);
3655 pLevel->u.pCovidx = pCov;
3656 if( pCov ) pLevel->iIdxCur = iCovCur;
3657 if( pAndExpr ){
3658 pAndExpr->pLeft = 0;
3659 sqlite3ExprDelete(db, pAndExpr);
3661 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
3662 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
3663 sqlite3VdbeResolveLabel(v, iLoopBody);
3665 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
3666 if( !untestedTerms ) disableTerm(pLevel, pTerm);
3667 }else
3668 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
3671 /* Case 6: There is no usable index. We must do a complete
3672 ** scan of the entire table.
3674 static const u8 aStep[] = { OP_Next, OP_Prev };
3675 static const u8 aStart[] = { OP_Rewind, OP_Last };
3676 assert( bRev==0 || bRev==1 );
3677 if( pTabItem->isRecursive ){
3678 /* Tables marked isRecursive have only a single row that is stored in
3679 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
3680 pLevel->op = OP_Noop;
3681 }else{
3682 pLevel->op = aStep[bRev];
3683 pLevel->p1 = iCur;
3684 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
3685 VdbeCoverageIf(v, bRev==0);
3686 VdbeCoverageIf(v, bRev!=0);
3687 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
3691 /* Insert code to test every subexpression that can be completely
3692 ** computed using the current set of tables.
3694 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
3695 Expr *pE;
3696 testcase( pTerm->wtFlags & TERM_VIRTUAL );
3697 testcase( pTerm->wtFlags & TERM_CODED );
3698 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
3699 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
3700 testcase( pWInfo->untestedTerms==0
3701 && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
3702 pWInfo->untestedTerms = 1;
3703 continue;
3705 pE = pTerm->pExpr;
3706 assert( pE!=0 );
3707 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
3708 continue;
3710 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
3711 pTerm->wtFlags |= TERM_CODED;
3714 /* Insert code to test for implied constraints based on transitivity
3715 ** of the "==" operator.
3717 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
3718 ** and we are coding the t1 loop and the t2 loop has not yet coded,
3719 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
3720 ** the implied "t1.a=123" constraint.
3722 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
3723 Expr *pE, *pEAlt;
3724 WhereTerm *pAlt;
3725 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
3726 if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
3727 if( pTerm->leftCursor!=iCur ) continue;
3728 if( pLevel->iLeftJoin ) continue;
3729 pE = pTerm->pExpr;
3730 assert( !ExprHasProperty(pE, EP_FromJoin) );
3731 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
3732 pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
3733 if( pAlt==0 ) continue;
3734 if( pAlt->wtFlags & (TERM_CODED) ) continue;
3735 testcase( pAlt->eOperator & WO_EQ );
3736 testcase( pAlt->eOperator & WO_IN );
3737 VdbeModuleComment((v, "begin transitive constraint"));
3738 pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
3739 if( pEAlt ){
3740 *pEAlt = *pAlt->pExpr;
3741 pEAlt->pLeft = pE->pLeft;
3742 sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
3743 sqlite3StackFree(db, pEAlt);
3747 /* For a LEFT OUTER JOIN, generate code that will record the fact that
3748 ** at least one row of the right table has matched the left table.
3750 if( pLevel->iLeftJoin ){
3751 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
3752 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
3753 VdbeComment((v, "record LEFT JOIN hit"));
3754 sqlite3ExprCacheClear(pParse);
3755 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
3756 testcase( pTerm->wtFlags & TERM_VIRTUAL );
3757 testcase( pTerm->wtFlags & TERM_CODED );
3758 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
3759 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
3760 assert( pWInfo->untestedTerms );
3761 continue;
3763 assert( pTerm->pExpr );
3764 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
3765 pTerm->wtFlags |= TERM_CODED;
3769 return pLevel->notReady;
3772 #ifdef WHERETRACE_ENABLED
3774 ** Print the content of a WhereTerm object
3776 static void whereTermPrint(WhereTerm *pTerm, int iTerm){
3777 if( pTerm==0 ){
3778 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
3779 }else{
3780 char zType[4];
3781 memcpy(zType, "...", 4);
3782 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
3783 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
3784 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
3785 sqlite3DebugPrintf("TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x\n",
3786 iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb,
3787 pTerm->eOperator);
3788 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
3791 #endif
3793 #ifdef WHERETRACE_ENABLED
3795 ** Print a WhereLoop object for debugging purposes
3797 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
3798 WhereInfo *pWInfo = pWC->pWInfo;
3799 int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
3800 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
3801 Table *pTab = pItem->pTab;
3802 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
3803 p->iTab, nb, p->maskSelf, nb, p->prereq);
3804 sqlite3DebugPrintf(" %12s",
3805 pItem->zAlias ? pItem->zAlias : pTab->zName);
3806 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
3807 const char *zName;
3808 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
3809 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
3810 int i = sqlite3Strlen30(zName) - 1;
3811 while( zName[i]!='_' ) i--;
3812 zName += i;
3814 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
3815 }else{
3816 sqlite3DebugPrintf("%20s","");
3818 }else{
3819 char *z;
3820 if( p->u.vtab.idxStr ){
3821 z = sqlite3_mprintf("(%d,\"%s\",%x)",
3822 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
3823 }else{
3824 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
3826 sqlite3DebugPrintf(" %-19s", z);
3827 sqlite3_free(z);
3829 if( p->wsFlags & WHERE_SKIPSCAN ){
3830 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->u.btree.nSkip);
3831 }else{
3832 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
3834 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
3835 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
3836 int i;
3837 for(i=0; i<p->nLTerm; i++){
3838 whereTermPrint(p->aLTerm[i], i);
3842 #endif
3845 ** Convert bulk memory into a valid WhereLoop that can be passed
3846 ** to whereLoopClear harmlessly.
3848 static void whereLoopInit(WhereLoop *p){
3849 p->aLTerm = p->aLTermSpace;
3850 p->nLTerm = 0;
3851 p->nLSlot = ArraySize(p->aLTermSpace);
3852 p->wsFlags = 0;
3856 ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
3858 static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
3859 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
3860 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
3861 sqlite3_free(p->u.vtab.idxStr);
3862 p->u.vtab.needFree = 0;
3863 p->u.vtab.idxStr = 0;
3864 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
3865 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
3866 sqlite3KeyInfoUnref(p->u.btree.pIndex->pKeyInfo);
3867 sqlite3DbFree(db, p->u.btree.pIndex);
3868 p->u.btree.pIndex = 0;
3874 ** Deallocate internal memory used by a WhereLoop object
3876 static void whereLoopClear(sqlite3 *db, WhereLoop *p){
3877 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
3878 whereLoopClearUnion(db, p);
3879 whereLoopInit(p);
3883 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
3885 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
3886 WhereTerm **paNew;
3887 if( p->nLSlot>=n ) return SQLITE_OK;
3888 n = (n+7)&~7;
3889 paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
3890 if( paNew==0 ) return SQLITE_NOMEM;
3891 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
3892 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
3893 p->aLTerm = paNew;
3894 p->nLSlot = n;
3895 return SQLITE_OK;
3899 ** Transfer content from the second pLoop into the first.
3901 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
3902 whereLoopClearUnion(db, pTo);
3903 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
3904 memset(&pTo->u, 0, sizeof(pTo->u));
3905 return SQLITE_NOMEM;
3907 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
3908 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
3909 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
3910 pFrom->u.vtab.needFree = 0;
3911 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
3912 pFrom->u.btree.pIndex = 0;
3914 return SQLITE_OK;
3918 ** Delete a WhereLoop object
3920 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
3921 whereLoopClear(db, p);
3922 sqlite3DbFree(db, p);
3926 ** Free a WhereInfo structure
3928 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
3929 if( ALWAYS(pWInfo) ){
3930 whereClauseClear(&pWInfo->sWC);
3931 while( pWInfo->pLoops ){
3932 WhereLoop *p = pWInfo->pLoops;
3933 pWInfo->pLoops = p->pNextLoop;
3934 whereLoopDelete(db, p);
3936 sqlite3DbFree(db, pWInfo);
3941 ** Return TRUE if both of the following are true:
3943 ** (1) X has the same or lower cost that Y
3944 ** (2) X is a proper subset of Y
3946 ** By "proper subset" we mean that X uses fewer WHERE clause terms
3947 ** than Y and that every WHERE clause term used by X is also used
3948 ** by Y.
3950 ** If X is a proper subset of Y then Y is a better choice and ought
3951 ** to have a lower cost. This routine returns TRUE when that cost
3952 ** relationship is inverted and needs to be adjusted.
3954 static int whereLoopCheaperProperSubset(
3955 const WhereLoop *pX, /* First WhereLoop to compare */
3956 const WhereLoop *pY /* Compare against this WhereLoop */
3958 int i, j;
3959 if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */
3960 if( pX->rRun >= pY->rRun ){
3961 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
3962 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
3964 for(i=pX->nLTerm-1; i>=0; i--){
3965 for(j=pY->nLTerm-1; j>=0; j--){
3966 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
3968 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
3970 return 1; /* All conditions meet */
3974 ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
3975 ** that:
3977 ** (1) pTemplate costs less than any other WhereLoops that are a proper
3978 ** subset of pTemplate
3980 ** (2) pTemplate costs more than any other WhereLoops for which pTemplate
3981 ** is a proper subset.
3983 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
3984 ** WHERE clause terms than Y and that every WHERE clause term used by X is
3985 ** also used by Y.
3987 ** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the
3988 ** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE
3989 ** clause terms covered, since some of the first nLTerm entries in aLTerm[]
3990 ** will be NULL (because they are skipped). That makes it more difficult
3991 ** to compare the loops. We could add extra code to do the comparison, and
3992 ** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this
3993 ** adjustment is sufficient minor, that it is very difficult to construct
3994 ** a test case where the extra code would improve the query plan. Better
3995 ** to avoid the added complexity and just omit cost adjustments to SKIPSCAN
3996 ** loops.
3998 static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
3999 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
4000 if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return;
4001 for(; p; p=p->pNextLoop){
4002 if( p->iTab!=pTemplate->iTab ) continue;
4003 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
4004 if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue;
4005 if( whereLoopCheaperProperSubset(p, pTemplate) ){
4006 /* Adjust pTemplate cost downward so that it is cheaper than its
4007 ** subset p */
4008 pTemplate->rRun = p->rRun;
4009 pTemplate->nOut = p->nOut - 1;
4010 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
4011 /* Adjust pTemplate cost upward so that it is costlier than p since
4012 ** pTemplate is a proper subset of p */
4013 pTemplate->rRun = p->rRun;
4014 pTemplate->nOut = p->nOut + 1;
4020 ** Search the list of WhereLoops in *ppPrev looking for one that can be
4021 ** supplanted by pTemplate.
4023 ** Return NULL if the WhereLoop list contains an entry that can supplant
4024 ** pTemplate, in other words if pTemplate does not belong on the list.
4026 ** If pX is a WhereLoop that pTemplate can supplant, then return the
4027 ** link that points to pX.
4029 ** If pTemplate cannot supplant any existing element of the list but needs
4030 ** to be added to the list, then return a pointer to the tail of the list.
4032 static WhereLoop **whereLoopFindLesser(
4033 WhereLoop **ppPrev,
4034 const WhereLoop *pTemplate
4036 WhereLoop *p;
4037 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
4038 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
4039 /* If either the iTab or iSortIdx values for two WhereLoop are different
4040 ** then those WhereLoops need to be considered separately. Neither is
4041 ** a candidate to replace the other. */
4042 continue;
4044 /* In the current implementation, the rSetup value is either zero
4045 ** or the cost of building an automatic index (NlogN) and the NlogN
4046 ** is the same for compatible WhereLoops. */
4047 assert( p->rSetup==0 || pTemplate->rSetup==0
4048 || p->rSetup==pTemplate->rSetup );
4050 /* whereLoopAddBtree() always generates and inserts the automatic index
4051 ** case first. Hence compatible candidate WhereLoops never have a larger
4052 ** rSetup. Call this SETUP-INVARIANT */
4053 assert( p->rSetup>=pTemplate->rSetup );
4055 /* Any loop using an appliation-defined index (or PRIMARY KEY or
4056 ** UNIQUE constraint) with one or more == constraints is better
4057 ** than an automatic index. */
4058 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
4059 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
4060 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
4061 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
4063 break;
4066 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
4067 ** discarded. WhereLoop p is better if:
4068 ** (1) p has no more dependencies than pTemplate, and
4069 ** (2) p has an equal or lower cost than pTemplate
4071 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
4072 && p->rSetup<=pTemplate->rSetup /* (2a) */
4073 && p->rRun<=pTemplate->rRun /* (2b) */
4074 && p->nOut<=pTemplate->nOut /* (2c) */
4076 return 0; /* Discard pTemplate */
4079 /* If pTemplate is always better than p, then cause p to be overwritten
4080 ** with pTemplate. pTemplate is better than p if:
4081 ** (1) pTemplate has no more dependences than p, and
4082 ** (2) pTemplate has an equal or lower cost than p.
4084 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
4085 && p->rRun>=pTemplate->rRun /* (2a) */
4086 && p->nOut>=pTemplate->nOut /* (2b) */
4088 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
4089 break; /* Cause p to be overwritten by pTemplate */
4092 return ppPrev;
4096 ** Insert or replace a WhereLoop entry using the template supplied.
4098 ** An existing WhereLoop entry might be overwritten if the new template
4099 ** is better and has fewer dependencies. Or the template will be ignored
4100 ** and no insert will occur if an existing WhereLoop is faster and has
4101 ** fewer dependencies than the template. Otherwise a new WhereLoop is
4102 ** added based on the template.
4104 ** If pBuilder->pOrSet is not NULL then we care about only the
4105 ** prerequisites and rRun and nOut costs of the N best loops. That
4106 ** information is gathered in the pBuilder->pOrSet object. This special
4107 ** processing mode is used only for OR clause processing.
4109 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
4110 ** still might overwrite similar loops with the new template if the
4111 ** new template is better. Loops may be overwritten if the following
4112 ** conditions are met:
4114 ** (1) They have the same iTab.
4115 ** (2) They have the same iSortIdx.
4116 ** (3) The template has same or fewer dependencies than the current loop
4117 ** (4) The template has the same or lower cost than the current loop
4119 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
4120 WhereLoop **ppPrev, *p;
4121 WhereInfo *pWInfo = pBuilder->pWInfo;
4122 sqlite3 *db = pWInfo->pParse->db;
4124 /* If pBuilder->pOrSet is defined, then only keep track of the costs
4125 ** and prereqs.
4127 if( pBuilder->pOrSet!=0 ){
4128 #if WHERETRACE_ENABLED
4129 u16 n = pBuilder->pOrSet->n;
4130 int x =
4131 #endif
4132 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
4133 pTemplate->nOut);
4134 #if WHERETRACE_ENABLED /* 0x8 */
4135 if( sqlite3WhereTrace & 0x8 ){
4136 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
4137 whereLoopPrint(pTemplate, pBuilder->pWC);
4139 #endif
4140 return SQLITE_OK;
4143 /* Look for an existing WhereLoop to replace with pTemplate
4145 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
4146 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
4148 if( ppPrev==0 ){
4149 /* There already exists a WhereLoop on the list that is better
4150 ** than pTemplate, so just ignore pTemplate */
4151 #if WHERETRACE_ENABLED /* 0x8 */
4152 if( sqlite3WhereTrace & 0x8 ){
4153 sqlite3DebugPrintf(" skip: ");
4154 whereLoopPrint(pTemplate, pBuilder->pWC);
4156 #endif
4157 return SQLITE_OK;
4158 }else{
4159 p = *ppPrev;
4162 /* If we reach this point it means that either p[] should be overwritten
4163 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
4164 ** WhereLoop and insert it.
4166 #if WHERETRACE_ENABLED /* 0x8 */
4167 if( sqlite3WhereTrace & 0x8 ){
4168 if( p!=0 ){
4169 sqlite3DebugPrintf("replace: ");
4170 whereLoopPrint(p, pBuilder->pWC);
4172 sqlite3DebugPrintf(" add: ");
4173 whereLoopPrint(pTemplate, pBuilder->pWC);
4175 #endif
4176 if( p==0 ){
4177 /* Allocate a new WhereLoop to add to the end of the list */
4178 *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
4179 if( p==0 ) return SQLITE_NOMEM;
4180 whereLoopInit(p);
4181 p->pNextLoop = 0;
4182 }else{
4183 /* We will be overwriting WhereLoop p[]. But before we do, first
4184 ** go through the rest of the list and delete any other entries besides
4185 ** p[] that are also supplated by pTemplate */
4186 WhereLoop **ppTail = &p->pNextLoop;
4187 WhereLoop *pToDel;
4188 while( *ppTail ){
4189 ppTail = whereLoopFindLesser(ppTail, pTemplate);
4190 if( ppTail==0 ) break;
4191 pToDel = *ppTail;
4192 if( pToDel==0 ) break;
4193 *ppTail = pToDel->pNextLoop;
4194 #if WHERETRACE_ENABLED /* 0x8 */
4195 if( sqlite3WhereTrace & 0x8 ){
4196 sqlite3DebugPrintf(" delete: ");
4197 whereLoopPrint(pToDel, pBuilder->pWC);
4199 #endif
4200 whereLoopDelete(db, pToDel);
4203 whereLoopXfer(db, p, pTemplate);
4204 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
4205 Index *pIndex = p->u.btree.pIndex;
4206 if( pIndex && pIndex->tnum==0 ){
4207 p->u.btree.pIndex = 0;
4210 return SQLITE_OK;
4214 ** Adjust the WhereLoop.nOut value downward to account for terms of the
4215 ** WHERE clause that reference the loop but which are not used by an
4216 ** index.
4218 ** In the current implementation, the first extra WHERE clause term reduces
4219 ** the number of output rows by a factor of 10 and each additional term
4220 ** reduces the number of output rows by sqrt(2).
4222 static void whereLoopOutputAdjust(
4223 WhereClause *pWC, /* The WHERE clause */
4224 WhereLoop *pLoop, /* The loop to adjust downward */
4225 LogEst nRow /* Number of rows in the entire table */
4227 WhereTerm *pTerm, *pX;
4228 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
4229 int i, j;
4230 int nEq = 0; /* Number of = constraints not within likely()/unlikely() */
4232 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
4233 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
4234 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
4235 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
4236 for(j=pLoop->nLTerm-1; j>=0; j--){
4237 pX = pLoop->aLTerm[j];
4238 if( pX==0 ) continue;
4239 if( pX==pTerm ) break;
4240 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
4242 if( j<0 ){
4243 if( pTerm->truthProb<=0 ){
4244 pLoop->nOut += pTerm->truthProb;
4245 }else{
4246 pLoop->nOut--;
4247 if( pTerm->eOperator&WO_EQ ) nEq++;
4251 /* TUNING: If there is at least one equality constraint in the WHERE
4252 ** clause that does not have a likelihood() explicitly assigned to it
4253 ** then do not let the estimated number of output rows exceed half
4254 ** the number of rows in the table. */
4255 if( nEq && pLoop->nOut>nRow-10 ){
4256 pLoop->nOut = nRow - 10;
4261 ** Adjust the cost C by the costMult facter T. This only occurs if
4262 ** compiled with -DSQLITE_ENABLE_COSTMULT
4264 #ifdef SQLITE_ENABLE_COSTMULT
4265 # define ApplyCostMultiplier(C,T) C += T
4266 #else
4267 # define ApplyCostMultiplier(C,T)
4268 #endif
4271 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
4272 ** index pIndex. Try to match one more.
4274 ** When this function is called, pBuilder->pNew->nOut contains the
4275 ** number of rows expected to be visited by filtering using the nEq
4276 ** terms only. If it is modified, this value is restored before this
4277 ** function returns.
4279 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
4280 ** INTEGER PRIMARY KEY.
4282 static int whereLoopAddBtreeIndex(
4283 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
4284 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
4285 Index *pProbe, /* An index on pSrc */
4286 LogEst nInMul /* log(Number of iterations due to IN) */
4288 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
4289 Parse *pParse = pWInfo->pParse; /* Parsing context */
4290 sqlite3 *db = pParse->db; /* Database connection malloc context */
4291 WhereLoop *pNew; /* Template WhereLoop under construction */
4292 WhereTerm *pTerm; /* A WhereTerm under consideration */
4293 int opMask; /* Valid operators for constraints */
4294 WhereScan scan; /* Iterator for WHERE terms */
4295 Bitmask saved_prereq; /* Original value of pNew->prereq */
4296 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
4297 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
4298 u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */
4299 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
4300 LogEst saved_nOut; /* Original value of pNew->nOut */
4301 int iCol; /* Index of the column in the table */
4302 int rc = SQLITE_OK; /* Return code */
4303 LogEst rSize; /* Number of rows in the table */
4304 LogEst rLogSize; /* Logarithm of table size */
4305 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
4307 pNew = pBuilder->pNew;
4308 if( db->mallocFailed ) return SQLITE_NOMEM;
4310 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
4311 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
4312 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
4313 opMask = WO_LT|WO_LE;
4314 }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
4315 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
4316 }else{
4317 opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
4319 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
4321 assert( pNew->u.btree.nEq<pProbe->nColumn );
4322 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
4324 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
4325 opMask, pProbe);
4326 saved_nEq = pNew->u.btree.nEq;
4327 saved_nSkip = pNew->u.btree.nSkip;
4328 saved_nLTerm = pNew->nLTerm;
4329 saved_wsFlags = pNew->wsFlags;
4330 saved_prereq = pNew->prereq;
4331 saved_nOut = pNew->nOut;
4332 pNew->rSetup = 0;
4333 rSize = pProbe->aiRowLogEst[0];
4334 rLogSize = estLog(rSize);
4336 /* Consider using a skip-scan if there are no WHERE clause constraints
4337 ** available for the left-most terms of the index, and if the average
4338 ** number of repeats in the left-most terms is at least 18.
4340 ** The magic number 18 is selected on the basis that scanning 17 rows
4341 ** is almost always quicker than an index seek (even though if the index
4342 ** contains fewer than 2^17 rows we assume otherwise in other parts of
4343 ** the code). And, even if it is not, it should not be too much slower.
4344 ** On the other hand, the extra seeks could end up being significantly
4345 ** more expensive. */
4346 assert( 42==sqlite3LogEst(18) );
4347 if( saved_nEq==saved_nSkip
4348 && saved_nEq+1<pProbe->nKeyCol
4349 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
4350 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
4352 LogEst nIter;
4353 pNew->u.btree.nEq++;
4354 pNew->u.btree.nSkip++;
4355 pNew->aLTerm[pNew->nLTerm++] = 0;
4356 pNew->wsFlags |= WHERE_SKIPSCAN;
4357 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
4358 if( pTerm ){
4359 /* TUNING: When estimating skip-scan for a term that is also indexable,
4360 ** multiply the cost of the skip-scan by 2.0, to make it a little less
4361 ** desirable than the regular index lookup. */
4362 nIter += 10; assert( 10==sqlite3LogEst(2) );
4364 pNew->nOut -= nIter;
4365 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
4366 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
4367 nIter += 5;
4368 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
4369 pNew->nOut = saved_nOut;
4370 pNew->u.btree.nEq = saved_nEq;
4371 pNew->u.btree.nSkip = saved_nSkip;
4373 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
4374 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
4375 LogEst rCostIdx;
4376 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
4377 int nIn = 0;
4378 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
4379 int nRecValid = pBuilder->nRecValid;
4380 #endif
4381 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
4382 && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
4384 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
4386 if( pTerm->prereqRight & pNew->maskSelf ) continue;
4388 pNew->wsFlags = saved_wsFlags;
4389 pNew->u.btree.nEq = saved_nEq;
4390 pNew->nLTerm = saved_nLTerm;
4391 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
4392 pNew->aLTerm[pNew->nLTerm++] = pTerm;
4393 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
4395 assert( nInMul==0
4396 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
4397 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
4398 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
4401 if( eOp & WO_IN ){
4402 Expr *pExpr = pTerm->pExpr;
4403 pNew->wsFlags |= WHERE_COLUMN_IN;
4404 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
4405 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
4406 nIn = 46; assert( 46==sqlite3LogEst(25) );
4407 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
4408 /* "x IN (value, value, ...)" */
4409 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
4411 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
4412 ** changes "x IN (?)" into "x=?". */
4414 }else if( eOp & (WO_EQ) ){
4415 pNew->wsFlags |= WHERE_COLUMN_EQ;
4416 if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
4417 if( iCol>=0 && !IsUniqueIndex(pProbe) ){
4418 pNew->wsFlags |= WHERE_UNQ_WANTED;
4419 }else{
4420 pNew->wsFlags |= WHERE_ONEROW;
4423 }else if( eOp & WO_ISNULL ){
4424 pNew->wsFlags |= WHERE_COLUMN_NULL;
4425 }else if( eOp & (WO_GT|WO_GE) ){
4426 testcase( eOp & WO_GT );
4427 testcase( eOp & WO_GE );
4428 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
4429 pBtm = pTerm;
4430 pTop = 0;
4431 }else{
4432 assert( eOp & (WO_LT|WO_LE) );
4433 testcase( eOp & WO_LT );
4434 testcase( eOp & WO_LE );
4435 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
4436 pTop = pTerm;
4437 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
4438 pNew->aLTerm[pNew->nLTerm-2] : 0;
4441 /* At this point pNew->nOut is set to the number of rows expected to
4442 ** be visited by the index scan before considering term pTerm, or the
4443 ** values of nIn and nInMul. In other words, assuming that all
4444 ** "x IN(...)" terms are replaced with "x = ?". This block updates
4445 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
4446 assert( pNew->nOut==saved_nOut );
4447 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
4448 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
4449 ** data, using some other estimate. */
4450 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
4451 }else{
4452 int nEq = ++pNew->u.btree.nEq;
4453 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) );
4455 assert( pNew->nOut==saved_nOut );
4456 if( pTerm->truthProb<=0 && iCol>=0 ){
4457 assert( (eOp & WO_IN) || nIn==0 );
4458 testcase( eOp & WO_IN );
4459 pNew->nOut += pTerm->truthProb;
4460 pNew->nOut -= nIn;
4461 }else{
4462 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
4463 tRowcnt nOut = 0;
4464 if( nInMul==0
4465 && pProbe->nSample
4466 && pNew->u.btree.nEq<=pProbe->nSampleCol
4467 && OptimizationEnabled(db, SQLITE_Stat3)
4468 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
4470 Expr *pExpr = pTerm->pExpr;
4471 if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
4472 testcase( eOp & WO_EQ );
4473 testcase( eOp & WO_ISNULL );
4474 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
4475 }else{
4476 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
4478 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
4479 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
4480 if( nOut ){
4481 pNew->nOut = sqlite3LogEst(nOut);
4482 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
4483 pNew->nOut -= nIn;
4486 if( nOut==0 )
4487 #endif
4489 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
4490 if( eOp & WO_ISNULL ){
4491 /* TUNING: If there is no likelihood() value, assume that a
4492 ** "col IS NULL" expression matches twice as many rows
4493 ** as (col=?). */
4494 pNew->nOut += 10;
4500 /* Set rCostIdx to the cost of visiting selected rows in index. Add
4501 ** it to pNew->rRun, which is currently set to the cost of the index
4502 ** seek only. Then, if this is a non-covering index, add the cost of
4503 ** visiting the rows in the main table. */
4504 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
4505 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
4506 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
4507 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
4509 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
4511 nOutUnadjusted = pNew->nOut;
4512 pNew->rRun += nInMul + nIn;
4513 pNew->nOut += nInMul + nIn;
4514 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
4515 rc = whereLoopInsert(pBuilder, pNew);
4517 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
4518 pNew->nOut = saved_nOut;
4519 }else{
4520 pNew->nOut = nOutUnadjusted;
4523 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
4524 && pNew->u.btree.nEq<pProbe->nColumn
4526 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
4528 pNew->nOut = saved_nOut;
4529 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
4530 pBuilder->nRecValid = nRecValid;
4531 #endif
4533 pNew->prereq = saved_prereq;
4534 pNew->u.btree.nEq = saved_nEq;
4535 pNew->u.btree.nSkip = saved_nSkip;
4536 pNew->wsFlags = saved_wsFlags;
4537 pNew->nOut = saved_nOut;
4538 pNew->nLTerm = saved_nLTerm;
4539 return rc;
4543 ** Return True if it is possible that pIndex might be useful in
4544 ** implementing the ORDER BY clause in pBuilder.
4546 ** Return False if pBuilder does not contain an ORDER BY clause or
4547 ** if there is no way for pIndex to be useful in implementing that
4548 ** ORDER BY clause.
4550 static int indexMightHelpWithOrderBy(
4551 WhereLoopBuilder *pBuilder,
4552 Index *pIndex,
4553 int iCursor
4555 ExprList *pOB;
4556 int ii, jj;
4558 if( pIndex->bUnordered ) return 0;
4559 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
4560 for(ii=0; ii<pOB->nExpr; ii++){
4561 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
4562 if( pExpr->op!=TK_COLUMN ) return 0;
4563 if( pExpr->iTable==iCursor ){
4564 if( pExpr->iColumn<0 ) return 1;
4565 for(jj=0; jj<pIndex->nKeyCol; jj++){
4566 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
4570 return 0;
4574 ** Return a bitmask where 1s indicate that the corresponding column of
4575 ** the table is used by an index. Only the first 63 columns are considered.
4577 static Bitmask columnsInIndex(Index *pIdx){
4578 Bitmask m = 0;
4579 int j;
4580 for(j=pIdx->nColumn-1; j>=0; j--){
4581 int x = pIdx->aiColumn[j];
4582 if( x>=0 ){
4583 testcase( x==BMS-1 );
4584 testcase( x==BMS-2 );
4585 if( x<BMS-1 ) m |= MASKBIT(x);
4588 return m;
4591 /* Check to see if a partial index with pPartIndexWhere can be used
4592 ** in the current query. Return true if it can be and false if not.
4594 static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
4595 int i;
4596 WhereTerm *pTerm;
4597 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
4598 if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1;
4600 return 0;
4604 ** Add all WhereLoop objects for a single table of the join where the table
4605 ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be
4606 ** a b-tree table, not a virtual table.
4608 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
4609 ** are calculated as follows:
4611 ** For a full scan, assuming the table (or index) contains nRow rows:
4613 ** cost = nRow * 3.0 // full-table scan
4614 ** cost = nRow * K // scan of covering index
4615 ** cost = nRow * (K+3.0) // scan of non-covering index
4617 ** where K is a value between 1.1 and 3.0 set based on the relative
4618 ** estimated average size of the index and table records.
4620 ** For an index scan, where nVisit is the number of index rows visited
4621 ** by the scan, and nSeek is the number of seek operations required on
4622 ** the index b-tree:
4624 ** cost = nSeek * (log(nRow) + K * nVisit) // covering index
4625 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
4627 ** Normally, nSeek is 1. nSeek values greater than 1 come about if the
4628 ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
4629 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
4631 ** The estimated values (nRow, nVisit, nSeek) often contain a large amount
4632 ** of uncertainty. For this reason, scoring is designed to pick plans that
4633 ** "do the least harm" if the estimates are inaccurate. For example, a
4634 ** log(nRow) factor is omitted from a non-covering index scan in order to
4635 ** bias the scoring in favor of using an index, since the worst-case
4636 ** performance of using an index is far better than the worst-case performance
4637 ** of a full table scan.
4639 static int whereLoopAddBtree(
4640 WhereLoopBuilder *pBuilder, /* WHERE clause information */
4641 Bitmask mExtra /* Extra prerequesites for using this table */
4643 WhereInfo *pWInfo; /* WHERE analysis context */
4644 Index *pProbe; /* An index we are evaluating */
4645 Index sPk; /* A fake index object for the primary key */
4646 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
4647 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
4648 SrcList *pTabList; /* The FROM clause */
4649 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
4650 WhereLoop *pNew; /* Template WhereLoop object */
4651 int rc = SQLITE_OK; /* Return code */
4652 int iSortIdx = 1; /* Index number */
4653 int b; /* A boolean value */
4654 LogEst rSize; /* number of rows in the table */
4655 LogEst rLogSize; /* Logarithm of the number of rows in the table */
4656 WhereClause *pWC; /* The parsed WHERE clause */
4657 Table *pTab; /* Table being queried */
4659 pNew = pBuilder->pNew;
4660 pWInfo = pBuilder->pWInfo;
4661 pTabList = pWInfo->pTabList;
4662 pSrc = pTabList->a + pNew->iTab;
4663 pTab = pSrc->pTab;
4664 pWC = pBuilder->pWC;
4665 assert( !IsVirtual(pSrc->pTab) );
4667 if( pSrc->pIndex ){
4668 /* An INDEXED BY clause specifies a particular index to use */
4669 pProbe = pSrc->pIndex;
4670 }else if( !HasRowid(pTab) ){
4671 pProbe = pTab->pIndex;
4672 }else{
4673 /* There is no INDEXED BY clause. Create a fake Index object in local
4674 ** variable sPk to represent the rowid primary key index. Make this
4675 ** fake index the first in a chain of Index objects with all of the real
4676 ** indices to follow */
4677 Index *pFirst; /* First of real indices on the table */
4678 memset(&sPk, 0, sizeof(Index));
4679 sPk.nKeyCol = 1;
4680 sPk.nColumn = 1;
4681 sPk.aiColumn = &aiColumnPk;
4682 sPk.aiRowLogEst = aiRowEstPk;
4683 sPk.onError = OE_Replace;
4684 sPk.pTable = pTab;
4685 sPk.szIdxRow = pTab->szTabRow;
4686 aiRowEstPk[0] = pTab->nRowLogEst;
4687 aiRowEstPk[1] = 0;
4688 pFirst = pSrc->pTab->pIndex;
4689 if( pSrc->notIndexed==0 ){
4690 /* The real indices of the table are only considered if the
4691 ** NOT INDEXED qualifier is omitted from the FROM clause */
4692 sPk.pNext = pFirst;
4694 pProbe = &sPk;
4696 rSize = pTab->nRowLogEst;
4697 rLogSize = estLog(rSize);
4699 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
4700 /* Automatic indexes */
4701 if( !pBuilder->pOrSet
4702 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
4703 && pSrc->pIndex==0
4704 && !pSrc->viaCoroutine
4705 && !pSrc->notIndexed
4706 && HasRowid(pTab)
4707 && !pSrc->isCorrelated
4708 && !pSrc->isRecursive
4710 /* Generate auto-index WhereLoops */
4711 WhereTerm *pTerm;
4712 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
4713 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
4714 if( pTerm->prereqRight & pNew->maskSelf ) continue;
4715 if( termCanDriveIndex(pTerm, pSrc, 0) ){
4716 pNew->u.btree.nEq = 1;
4717 pNew->u.btree.nSkip = 0;
4718 pNew->u.btree.pIndex = 0;
4719 pNew->nLTerm = 1;
4720 pNew->aLTerm[0] = pTerm;
4721 /* TUNING: One-time cost for computing the automatic index is
4722 ** estimated to be X*N*log2(N) where N is the number of rows in
4723 ** the table being indexed and where X is 7 (LogEst=28) for normal
4724 ** tables or 1.375 (LogEst=4) for views and subqueries. The value
4725 ** of X is smaller for views and subqueries so that the query planner
4726 ** will be more aggressive about generating automatic indexes for
4727 ** those objects, since there is no opportunity to add schema
4728 ** indexes on subqueries and views. */
4729 pNew->rSetup = rLogSize + rSize + 4;
4730 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
4731 pNew->rSetup += 24;
4733 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
4734 /* TUNING: Each index lookup yields 20 rows in the table. This
4735 ** is more than the usual guess of 10 rows, since we have no way
4736 ** of knowing how selective the index will ultimately be. It would
4737 ** not be unreasonable to make this value much larger. */
4738 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
4739 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
4740 pNew->wsFlags = WHERE_AUTO_INDEX;
4741 pNew->prereq = mExtra | pTerm->prereqRight;
4742 rc = whereLoopInsert(pBuilder, pNew);
4746 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
4748 /* Loop over all indices
4750 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
4751 if( pProbe->pPartIdxWhere!=0
4752 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
4753 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
4754 continue; /* Partial index inappropriate for this query */
4756 rSize = pProbe->aiRowLogEst[0];
4757 pNew->u.btree.nEq = 0;
4758 pNew->u.btree.nSkip = 0;
4759 pNew->nLTerm = 0;
4760 pNew->iSortIdx = 0;
4761 pNew->rSetup = 0;
4762 pNew->prereq = mExtra;
4763 pNew->nOut = rSize;
4764 pNew->u.btree.pIndex = pProbe;
4765 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
4766 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
4767 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
4768 if( pProbe->tnum<=0 ){
4769 /* Integer primary key index */
4770 pNew->wsFlags = WHERE_IPK;
4772 /* Full table scan */
4773 pNew->iSortIdx = b ? iSortIdx : 0;
4774 /* TUNING: Cost of full table scan is (N*3.0). */
4775 pNew->rRun = rSize + 16;
4776 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
4777 whereLoopOutputAdjust(pWC, pNew, rSize);
4778 rc = whereLoopInsert(pBuilder, pNew);
4779 pNew->nOut = rSize;
4780 if( rc ) break;
4781 }else{
4782 Bitmask m;
4783 if( pProbe->isCovering ){
4784 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
4785 m = 0;
4786 }else{
4787 m = pSrc->colUsed & ~columnsInIndex(pProbe);
4788 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
4791 /* Full scan via index */
4792 if( b
4793 || !HasRowid(pTab)
4794 || ( m==0
4795 && pProbe->bUnordered==0
4796 && (pProbe->szIdxRow<pTab->szTabRow)
4797 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
4798 && sqlite3GlobalConfig.bUseCis
4799 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
4802 pNew->iSortIdx = b ? iSortIdx : 0;
4804 /* The cost of visiting the index rows is N*K, where K is
4805 ** between 1.1 and 3.0, depending on the relative sizes of the
4806 ** index and table rows. If this is a non-covering index scan,
4807 ** also add the cost of visiting table rows (N*3.0). */
4808 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
4809 if( m!=0 ){
4810 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
4812 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
4813 whereLoopOutputAdjust(pWC, pNew, rSize);
4814 rc = whereLoopInsert(pBuilder, pNew);
4815 pNew->nOut = rSize;
4816 if( rc ) break;
4820 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
4821 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
4822 sqlite3Stat4ProbeFree(pBuilder->pRec);
4823 pBuilder->nRecValid = 0;
4824 pBuilder->pRec = 0;
4825 #endif
4827 /* If there was an INDEXED BY clause, then only that one index is
4828 ** considered. */
4829 if( pSrc->pIndex ) break;
4831 return rc;
4834 #ifndef SQLITE_OMIT_VIRTUALTABLE
4836 ** Add all WhereLoop objects for a table of the join identified by
4837 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
4839 static int whereLoopAddVirtual(
4840 WhereLoopBuilder *pBuilder, /* WHERE clause information */
4841 Bitmask mExtra
4843 WhereInfo *pWInfo; /* WHERE analysis context */
4844 Parse *pParse; /* The parsing context */
4845 WhereClause *pWC; /* The WHERE clause */
4846 struct SrcList_item *pSrc; /* The FROM clause term to search */
4847 Table *pTab;
4848 sqlite3 *db;
4849 sqlite3_index_info *pIdxInfo;
4850 struct sqlite3_index_constraint *pIdxCons;
4851 struct sqlite3_index_constraint_usage *pUsage;
4852 WhereTerm *pTerm;
4853 int i, j;
4854 int iTerm, mxTerm;
4855 int nConstraint;
4856 int seenIn = 0; /* True if an IN operator is seen */
4857 int seenVar = 0; /* True if a non-constant constraint is seen */
4858 int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */
4859 WhereLoop *pNew;
4860 int rc = SQLITE_OK;
4862 pWInfo = pBuilder->pWInfo;
4863 pParse = pWInfo->pParse;
4864 db = pParse->db;
4865 pWC = pBuilder->pWC;
4866 pNew = pBuilder->pNew;
4867 pSrc = &pWInfo->pTabList->a[pNew->iTab];
4868 pTab = pSrc->pTab;
4869 assert( IsVirtual(pTab) );
4870 pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
4871 if( pIdxInfo==0 ) return SQLITE_NOMEM;
4872 pNew->prereq = 0;
4873 pNew->rSetup = 0;
4874 pNew->wsFlags = WHERE_VIRTUALTABLE;
4875 pNew->nLTerm = 0;
4876 pNew->u.vtab.needFree = 0;
4877 pUsage = pIdxInfo->aConstraintUsage;
4878 nConstraint = pIdxInfo->nConstraint;
4879 if( whereLoopResize(db, pNew, nConstraint) ){
4880 sqlite3DbFree(db, pIdxInfo);
4881 return SQLITE_NOMEM;
4884 for(iPhase=0; iPhase<=3; iPhase++){
4885 if( !seenIn && (iPhase&1)!=0 ){
4886 iPhase++;
4887 if( iPhase>3 ) break;
4889 if( !seenVar && iPhase>1 ) break;
4890 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
4891 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
4892 j = pIdxCons->iTermOffset;
4893 pTerm = &pWC->a[j];
4894 switch( iPhase ){
4895 case 0: /* Constants without IN operator */
4896 pIdxCons->usable = 0;
4897 if( (pTerm->eOperator & WO_IN)!=0 ){
4898 seenIn = 1;
4900 if( pTerm->prereqRight!=0 ){
4901 seenVar = 1;
4902 }else if( (pTerm->eOperator & WO_IN)==0 ){
4903 pIdxCons->usable = 1;
4905 break;
4906 case 1: /* Constants with IN operators */
4907 assert( seenIn );
4908 pIdxCons->usable = (pTerm->prereqRight==0);
4909 break;
4910 case 2: /* Variables without IN */
4911 assert( seenVar );
4912 pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
4913 break;
4914 default: /* Variables with IN */
4915 assert( seenVar && seenIn );
4916 pIdxCons->usable = 1;
4917 break;
4920 memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
4921 if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
4922 pIdxInfo->idxStr = 0;
4923 pIdxInfo->idxNum = 0;
4924 pIdxInfo->needToFreeIdxStr = 0;
4925 pIdxInfo->orderByConsumed = 0;
4926 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
4927 pIdxInfo->estimatedRows = 25;
4928 rc = vtabBestIndex(pParse, pTab, pIdxInfo);
4929 if( rc ) goto whereLoopAddVtab_exit;
4930 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
4931 pNew->prereq = mExtra;
4932 mxTerm = -1;
4933 assert( pNew->nLSlot>=nConstraint );
4934 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
4935 pNew->u.vtab.omitMask = 0;
4936 for(i=0; i<nConstraint; i++, pIdxCons++){
4937 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
4938 j = pIdxCons->iTermOffset;
4939 if( iTerm>=nConstraint
4940 || j<0
4941 || j>=pWC->nTerm
4942 || pNew->aLTerm[iTerm]!=0
4944 rc = SQLITE_ERROR;
4945 sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
4946 goto whereLoopAddVtab_exit;
4948 testcase( iTerm==nConstraint-1 );
4949 testcase( j==0 );
4950 testcase( j==pWC->nTerm-1 );
4951 pTerm = &pWC->a[j];
4952 pNew->prereq |= pTerm->prereqRight;
4953 assert( iTerm<pNew->nLSlot );
4954 pNew->aLTerm[iTerm] = pTerm;
4955 if( iTerm>mxTerm ) mxTerm = iTerm;
4956 testcase( iTerm==15 );
4957 testcase( iTerm==16 );
4958 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
4959 if( (pTerm->eOperator & WO_IN)!=0 ){
4960 if( pUsage[i].omit==0 ){
4961 /* Do not attempt to use an IN constraint if the virtual table
4962 ** says that the equivalent EQ constraint cannot be safely omitted.
4963 ** If we do attempt to use such a constraint, some rows might be
4964 ** repeated in the output. */
4965 break;
4967 /* A virtual table that is constrained by an IN clause may not
4968 ** consume the ORDER BY clause because (1) the order of IN terms
4969 ** is not necessarily related to the order of output terms and
4970 ** (2) Multiple outputs from a single IN value will not merge
4971 ** together. */
4972 pIdxInfo->orderByConsumed = 0;
4976 if( i>=nConstraint ){
4977 pNew->nLTerm = mxTerm+1;
4978 assert( pNew->nLTerm<=pNew->nLSlot );
4979 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
4980 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
4981 pIdxInfo->needToFreeIdxStr = 0;
4982 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
4983 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
4984 pIdxInfo->nOrderBy : 0);
4985 pNew->rSetup = 0;
4986 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
4987 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
4988 whereLoopInsert(pBuilder, pNew);
4989 if( pNew->u.vtab.needFree ){
4990 sqlite3_free(pNew->u.vtab.idxStr);
4991 pNew->u.vtab.needFree = 0;
4996 whereLoopAddVtab_exit:
4997 if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
4998 sqlite3DbFree(db, pIdxInfo);
4999 return rc;
5001 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5004 ** Add WhereLoop entries to handle OR terms. This works for either
5005 ** btrees or virtual tables.
5007 static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
5008 WhereInfo *pWInfo = pBuilder->pWInfo;
5009 WhereClause *pWC;
5010 WhereLoop *pNew;
5011 WhereTerm *pTerm, *pWCEnd;
5012 int rc = SQLITE_OK;
5013 int iCur;
5014 WhereClause tempWC;
5015 WhereLoopBuilder sSubBuild;
5016 WhereOrSet sSum, sCur;
5017 struct SrcList_item *pItem;
5019 pWC = pBuilder->pWC;
5020 pWCEnd = pWC->a + pWC->nTerm;
5021 pNew = pBuilder->pNew;
5022 memset(&sSum, 0, sizeof(sSum));
5023 pItem = pWInfo->pTabList->a + pNew->iTab;
5024 iCur = pItem->iCursor;
5026 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
5027 if( (pTerm->eOperator & WO_OR)!=0
5028 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
5030 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
5031 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
5032 WhereTerm *pOrTerm;
5033 int once = 1;
5034 int i, j;
5036 sSubBuild = *pBuilder;
5037 sSubBuild.pOrderBy = 0;
5038 sSubBuild.pOrSet = &sCur;
5040 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
5041 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
5042 if( (pOrTerm->eOperator & WO_AND)!=0 ){
5043 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
5044 }else if( pOrTerm->leftCursor==iCur ){
5045 tempWC.pWInfo = pWC->pWInfo;
5046 tempWC.pOuter = pWC;
5047 tempWC.op = TK_AND;
5048 tempWC.nTerm = 1;
5049 tempWC.a = pOrTerm;
5050 sSubBuild.pWC = &tempWC;
5051 }else{
5052 continue;
5054 sCur.n = 0;
5055 #ifdef WHERETRACE_ENABLED
5056 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
5057 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
5058 if( sqlite3WhereTrace & 0x400 ){
5059 for(i=0; i<sSubBuild.pWC->nTerm; i++){
5060 whereTermPrint(&sSubBuild.pWC->a[i], i);
5063 #endif
5064 #ifndef SQLITE_OMIT_VIRTUALTABLE
5065 if( IsVirtual(pItem->pTab) ){
5066 rc = whereLoopAddVirtual(&sSubBuild, mExtra);
5067 }else
5068 #endif
5070 rc = whereLoopAddBtree(&sSubBuild, mExtra);
5072 if( rc==SQLITE_OK ){
5073 rc = whereLoopAddOr(&sSubBuild, mExtra);
5075 assert( rc==SQLITE_OK || sCur.n==0 );
5076 if( sCur.n==0 ){
5077 sSum.n = 0;
5078 break;
5079 }else if( once ){
5080 whereOrMove(&sSum, &sCur);
5081 once = 0;
5082 }else{
5083 WhereOrSet sPrev;
5084 whereOrMove(&sPrev, &sSum);
5085 sSum.n = 0;
5086 for(i=0; i<sPrev.n; i++){
5087 for(j=0; j<sCur.n; j++){
5088 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
5089 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
5090 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
5095 pNew->nLTerm = 1;
5096 pNew->aLTerm[0] = pTerm;
5097 pNew->wsFlags = WHERE_MULTI_OR;
5098 pNew->rSetup = 0;
5099 pNew->iSortIdx = 0;
5100 memset(&pNew->u, 0, sizeof(pNew->u));
5101 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
5102 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
5103 ** of all sub-scans required by the OR-scan. However, due to rounding
5104 ** errors, it may be that the cost of the OR-scan is equal to its
5105 ** most expensive sub-scan. Add the smallest possible penalty
5106 ** (equivalent to multiplying the cost by 1.07) to ensure that
5107 ** this does not happen. Otherwise, for WHERE clauses such as the
5108 ** following where there is an index on "y":
5110 ** WHERE likelihood(x=?, 0.99) OR y=?
5112 ** the planner may elect to "OR" together a full-table scan and an
5113 ** index lookup. And other similarly odd results. */
5114 pNew->rRun = sSum.a[i].rRun + 1;
5115 pNew->nOut = sSum.a[i].nOut;
5116 pNew->prereq = sSum.a[i].prereq;
5117 rc = whereLoopInsert(pBuilder, pNew);
5119 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
5122 return rc;
5126 ** Add all WhereLoop objects for all tables
5128 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
5129 WhereInfo *pWInfo = pBuilder->pWInfo;
5130 Bitmask mExtra = 0;
5131 Bitmask mPrior = 0;
5132 int iTab;
5133 SrcList *pTabList = pWInfo->pTabList;
5134 struct SrcList_item *pItem;
5135 sqlite3 *db = pWInfo->pParse->db;
5136 int nTabList = pWInfo->nLevel;
5137 int rc = SQLITE_OK;
5138 u8 priorJoinType = 0;
5139 WhereLoop *pNew;
5141 /* Loop over the tables in the join, from left to right */
5142 pNew = pBuilder->pNew;
5143 whereLoopInit(pNew);
5144 for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
5145 pNew->iTab = iTab;
5146 pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
5147 if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
5148 mExtra = mPrior;
5150 priorJoinType = pItem->jointype;
5151 if( IsVirtual(pItem->pTab) ){
5152 rc = whereLoopAddVirtual(pBuilder, mExtra);
5153 }else{
5154 rc = whereLoopAddBtree(pBuilder, mExtra);
5156 if( rc==SQLITE_OK ){
5157 rc = whereLoopAddOr(pBuilder, mExtra);
5159 mPrior |= pNew->maskSelf;
5160 if( rc || db->mallocFailed ) break;
5162 whereLoopClear(db, pNew);
5163 return rc;
5167 ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
5168 ** parameters) to see if it outputs rows in the requested ORDER BY
5169 ** (or GROUP BY) without requiring a separate sort operation. Return N:
5171 ** N>0: N terms of the ORDER BY clause are satisfied
5172 ** N==0: No terms of the ORDER BY clause are satisfied
5173 ** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
5175 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
5176 ** strict. With GROUP BY and DISTINCT the only requirement is that
5177 ** equivalent rows appear immediately adjacent to one another. GROUP BY
5178 ** and DISTINCT do not require rows to appear in any particular order as long
5179 ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
5180 ** the pOrderBy terms can be matched in any order. With ORDER BY, the
5181 ** pOrderBy terms must be matched in strict left-to-right order.
5183 static i8 wherePathSatisfiesOrderBy(
5184 WhereInfo *pWInfo, /* The WHERE clause */
5185 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
5186 WherePath *pPath, /* The WherePath to check */
5187 u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
5188 u16 nLoop, /* Number of entries in pPath->aLoop[] */
5189 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
5190 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
5192 u8 revSet; /* True if rev is known */
5193 u8 rev; /* Composite sort order */
5194 u8 revIdx; /* Index sort order */
5195 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
5196 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
5197 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
5198 u16 nKeyCol; /* Number of key columns in pIndex */
5199 u16 nColumn; /* Total number of ordered columns in the index */
5200 u16 nOrderBy; /* Number terms in the ORDER BY clause */
5201 int iLoop; /* Index of WhereLoop in pPath being processed */
5202 int i, j; /* Loop counters */
5203 int iCur; /* Cursor number for current WhereLoop */
5204 int iColumn; /* A column number within table iCur */
5205 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
5206 WhereTerm *pTerm; /* A single term of the WHERE clause */
5207 Expr *pOBExpr; /* An expression from the ORDER BY clause */
5208 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
5209 Index *pIndex; /* The index associated with pLoop */
5210 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
5211 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
5212 Bitmask obDone; /* Mask of all ORDER BY terms */
5213 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
5214 Bitmask ready; /* Mask of inner loops */
5217 ** We say the WhereLoop is "one-row" if it generates no more than one
5218 ** row of output. A WhereLoop is one-row if all of the following are true:
5219 ** (a) All index columns match with WHERE_COLUMN_EQ.
5220 ** (b) The index is unique
5221 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
5222 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
5224 ** We say the WhereLoop is "order-distinct" if the set of columns from
5225 ** that WhereLoop that are in the ORDER BY clause are different for every
5226 ** row of the WhereLoop. Every one-row WhereLoop is automatically
5227 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
5228 ** is not order-distinct. To be order-distinct is not quite the same as being
5229 ** UNIQUE since a UNIQUE column or index can have multiple rows that
5230 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
5231 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
5233 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
5234 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
5235 ** automatically order-distinct.
5238 assert( pOrderBy!=0 );
5239 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
5241 nOrderBy = pOrderBy->nExpr;
5242 testcase( nOrderBy==BMS-1 );
5243 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
5244 isOrderDistinct = 1;
5245 obDone = MASKBIT(nOrderBy)-1;
5246 orderDistinctMask = 0;
5247 ready = 0;
5248 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
5249 if( iLoop>0 ) ready |= pLoop->maskSelf;
5250 pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
5251 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
5252 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
5253 break;
5255 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
5257 /* Mark off any ORDER BY term X that is a column in the table of
5258 ** the current loop for which there is term in the WHERE
5259 ** clause of the form X IS NULL or X=? that reference only outer
5260 ** loops.
5262 for(i=0; i<nOrderBy; i++){
5263 if( MASKBIT(i) & obSat ) continue;
5264 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
5265 if( pOBExpr->op!=TK_COLUMN ) continue;
5266 if( pOBExpr->iTable!=iCur ) continue;
5267 pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
5268 ~ready, WO_EQ|WO_ISNULL, 0);
5269 if( pTerm==0 ) continue;
5270 if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
5271 const char *z1, *z2;
5272 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
5273 if( !pColl ) pColl = db->pDfltColl;
5274 z1 = pColl->zName;
5275 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
5276 if( !pColl ) pColl = db->pDfltColl;
5277 z2 = pColl->zName;
5278 if( sqlite3StrICmp(z1, z2)!=0 ) continue;
5280 obSat |= MASKBIT(i);
5283 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
5284 if( pLoop->wsFlags & WHERE_IPK ){
5285 pIndex = 0;
5286 nKeyCol = 0;
5287 nColumn = 1;
5288 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
5289 return 0;
5290 }else{
5291 nKeyCol = pIndex->nKeyCol;
5292 nColumn = pIndex->nColumn;
5293 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
5294 assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
5295 isOrderDistinct = IsUniqueIndex(pIndex);
5298 /* Loop through all columns of the index and deal with the ones
5299 ** that are not constrained by == or IN.
5301 rev = revSet = 0;
5302 distinctColumns = 0;
5303 for(j=0; j<nColumn; j++){
5304 u8 bOnce; /* True to run the ORDER BY search loop */
5306 /* Skip over == and IS NULL terms */
5307 if( j<pLoop->u.btree.nEq
5308 && pLoop->u.btree.nSkip==0
5309 && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
5311 if( i & WO_ISNULL ){
5312 testcase( isOrderDistinct );
5313 isOrderDistinct = 0;
5315 continue;
5318 /* Get the column number in the table (iColumn) and sort order
5319 ** (revIdx) for the j-th column of the index.
5321 if( pIndex ){
5322 iColumn = pIndex->aiColumn[j];
5323 revIdx = pIndex->aSortOrder[j];
5324 if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
5325 }else{
5326 iColumn = -1;
5327 revIdx = 0;
5330 /* An unconstrained column that might be NULL means that this
5331 ** WhereLoop is not well-ordered
5333 if( isOrderDistinct
5334 && iColumn>=0
5335 && j>=pLoop->u.btree.nEq
5336 && pIndex->pTable->aCol[iColumn].notNull==0
5338 isOrderDistinct = 0;
5341 /* Find the ORDER BY term that corresponds to the j-th column
5342 ** of the index and mark that ORDER BY term off
5344 bOnce = 1;
5345 isMatch = 0;
5346 for(i=0; bOnce && i<nOrderBy; i++){
5347 if( MASKBIT(i) & obSat ) continue;
5348 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
5349 testcase( wctrlFlags & WHERE_GROUPBY );
5350 testcase( wctrlFlags & WHERE_DISTINCTBY );
5351 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
5352 if( pOBExpr->op!=TK_COLUMN ) continue;
5353 if( pOBExpr->iTable!=iCur ) continue;
5354 if( pOBExpr->iColumn!=iColumn ) continue;
5355 if( iColumn>=0 ){
5356 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
5357 if( !pColl ) pColl = db->pDfltColl;
5358 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
5360 isMatch = 1;
5361 break;
5363 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
5364 /* Make sure the sort order is compatible in an ORDER BY clause.
5365 ** Sort order is irrelevant for a GROUP BY clause. */
5366 if( revSet ){
5367 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
5368 }else{
5369 rev = revIdx ^ pOrderBy->a[i].sortOrder;
5370 if( rev ) *pRevMask |= MASKBIT(iLoop);
5371 revSet = 1;
5374 if( isMatch ){
5375 if( iColumn<0 ){
5376 testcase( distinctColumns==0 );
5377 distinctColumns = 1;
5379 obSat |= MASKBIT(i);
5380 }else{
5381 /* No match found */
5382 if( j==0 || j<nKeyCol ){
5383 testcase( isOrderDistinct!=0 );
5384 isOrderDistinct = 0;
5386 break;
5388 } /* end Loop over all index columns */
5389 if( distinctColumns ){
5390 testcase( isOrderDistinct==0 );
5391 isOrderDistinct = 1;
5393 } /* end-if not one-row */
5395 /* Mark off any other ORDER BY terms that reference pLoop */
5396 if( isOrderDistinct ){
5397 orderDistinctMask |= pLoop->maskSelf;
5398 for(i=0; i<nOrderBy; i++){
5399 Expr *p;
5400 Bitmask mTerm;
5401 if( MASKBIT(i) & obSat ) continue;
5402 p = pOrderBy->a[i].pExpr;
5403 mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
5404 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
5405 if( (mTerm&~orderDistinctMask)==0 ){
5406 obSat |= MASKBIT(i);
5410 } /* End the loop over all WhereLoops from outer-most down to inner-most */
5411 if( obSat==obDone ) return (i8)nOrderBy;
5412 if( !isOrderDistinct ){
5413 for(i=nOrderBy-1; i>0; i--){
5414 Bitmask m = MASKBIT(i) - 1;
5415 if( (obSat&m)==m ) return i;
5417 return 0;
5419 return -1;
5424 ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
5425 ** the planner assumes that the specified pOrderBy list is actually a GROUP
5426 ** BY clause - and so any order that groups rows as required satisfies the
5427 ** request.
5429 ** Normally, in this case it is not possible for the caller to determine
5430 ** whether or not the rows are really being delivered in sorted order, or
5431 ** just in some other order that provides the required grouping. However,
5432 ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
5433 ** this function may be called on the returned WhereInfo object. It returns
5434 ** true if the rows really will be sorted in the specified order, or false
5435 ** otherwise.
5437 ** For example, assuming:
5439 ** CREATE INDEX i1 ON t1(x, Y);
5441 ** then
5443 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
5444 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
5446 int sqlite3WhereIsSorted(WhereInfo *pWInfo){
5447 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
5448 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
5449 return pWInfo->sorted;
5452 #ifdef WHERETRACE_ENABLED
5453 /* For debugging use only: */
5454 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
5455 static char zName[65];
5456 int i;
5457 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
5458 if( pLast ) zName[i++] = pLast->cId;
5459 zName[i] = 0;
5460 return zName;
5462 #endif
5465 ** Return the cost of sorting nRow rows, assuming that the keys have
5466 ** nOrderby columns and that the first nSorted columns are already in
5467 ** order.
5469 static LogEst whereSortingCost(
5470 WhereInfo *pWInfo,
5471 LogEst nRow,
5472 int nOrderBy,
5473 int nSorted
5475 /* TUNING: Estimated cost of a full external sort, where N is
5476 ** the number of rows to sort is:
5478 ** cost = (3.0 * N * log(N)).
5480 ** Or, if the order-by clause has X terms but only the last Y
5481 ** terms are out of order, then block-sorting will reduce the
5482 ** sorting cost to:
5484 ** cost = (3.0 * N * log(N)) * (Y/X)
5486 ** The (Y/X) term is implemented using stack variable rScale
5487 ** below. */
5488 LogEst rScale, rSortCost;
5489 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
5490 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
5491 rSortCost = nRow + estLog(nRow) + rScale + 16;
5493 /* TUNING: The cost of implementing DISTINCT using a B-TREE is
5494 ** similar but with a larger constant of proportionality.
5495 ** Multiply by an additional factor of 3.0. */
5496 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
5497 rSortCost += 16;
5500 return rSortCost;
5504 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
5505 ** attempts to find the lowest cost path that visits each WhereLoop
5506 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
5508 ** Assume that the total number of output rows that will need to be sorted
5509 ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
5510 ** costs if nRowEst==0.
5512 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
5513 ** error occurs.
5515 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
5516 int mxChoice; /* Maximum number of simultaneous paths tracked */
5517 int nLoop; /* Number of terms in the join */
5518 Parse *pParse; /* Parsing context */
5519 sqlite3 *db; /* The database connection */
5520 int iLoop; /* Loop counter over the terms of the join */
5521 int ii, jj; /* Loop counters */
5522 int mxI = 0; /* Index of next entry to replace */
5523 int nOrderBy; /* Number of ORDER BY clause terms */
5524 LogEst mxCost = 0; /* Maximum cost of a set of paths */
5525 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
5526 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
5527 WherePath *aFrom; /* All nFrom paths at the previous level */
5528 WherePath *aTo; /* The nTo best paths at the current level */
5529 WherePath *pFrom; /* An element of aFrom[] that we are working on */
5530 WherePath *pTo; /* An element of aTo[] that we are working on */
5531 WhereLoop *pWLoop; /* One of the WhereLoop objects */
5532 WhereLoop **pX; /* Used to divy up the pSpace memory */
5533 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
5534 char *pSpace; /* Temporary memory used by this routine */
5535 int nSpace; /* Bytes of space allocated at pSpace */
5537 pParse = pWInfo->pParse;
5538 db = pParse->db;
5539 nLoop = pWInfo->nLevel;
5540 /* TUNING: For simple queries, only the best path is tracked.
5541 ** For 2-way joins, the 5 best paths are followed.
5542 ** For joins of 3 or more tables, track the 10 best paths */
5543 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
5544 assert( nLoop<=pWInfo->pTabList->nSrc );
5545 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
5547 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
5548 ** case the purpose of this call is to estimate the number of rows returned
5549 ** by the overall query. Once this estimate has been obtained, the caller
5550 ** will invoke this function a second time, passing the estimate as the
5551 ** nRowEst parameter. */
5552 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
5553 nOrderBy = 0;
5554 }else{
5555 nOrderBy = pWInfo->pOrderBy->nExpr;
5558 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
5559 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
5560 nSpace += sizeof(LogEst) * nOrderBy;
5561 pSpace = sqlite3DbMallocRaw(db, nSpace);
5562 if( pSpace==0 ) return SQLITE_NOMEM;
5563 aTo = (WherePath*)pSpace;
5564 aFrom = aTo+mxChoice;
5565 memset(aFrom, 0, sizeof(aFrom[0]));
5566 pX = (WhereLoop**)(aFrom+mxChoice);
5567 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
5568 pFrom->aLoop = pX;
5570 if( nOrderBy ){
5571 /* If there is an ORDER BY clause and it is not being ignored, set up
5572 ** space for the aSortCost[] array. Each element of the aSortCost array
5573 ** is either zero - meaning it has not yet been initialized - or the
5574 ** cost of sorting nRowEst rows of data where the first X terms of
5575 ** the ORDER BY clause are already in order, where X is the array
5576 ** index. */
5577 aSortCost = (LogEst*)pX;
5578 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
5580 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] );
5581 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX );
5583 /* Seed the search with a single WherePath containing zero WhereLoops.
5585 ** TUNING: Do not let the number of iterations go above 25. If the cost
5586 ** of computing an automatic index is not paid back within the first 25
5587 ** rows, then do not use the automatic index. */
5588 aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
5589 nFrom = 1;
5590 assert( aFrom[0].isOrdered==0 );
5591 if( nOrderBy ){
5592 /* If nLoop is zero, then there are no FROM terms in the query. Since
5593 ** in this case the query may return a maximum of one row, the results
5594 ** are already in the requested order. Set isOrdered to nOrderBy to
5595 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
5596 ** -1, indicating that the result set may or may not be ordered,
5597 ** depending on the loops added to the current plan. */
5598 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
5601 /* Compute successively longer WherePaths using the previous generation
5602 ** of WherePaths as the basis for the next. Keep track of the mxChoice
5603 ** best paths at each generation */
5604 for(iLoop=0; iLoop<nLoop; iLoop++){
5605 nTo = 0;
5606 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
5607 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
5608 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
5609 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
5610 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
5611 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
5612 Bitmask maskNew; /* Mask of src visited by (..) */
5613 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
5615 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
5616 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
5617 /* At this point, pWLoop is a candidate to be the next loop.
5618 ** Compute its cost */
5619 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
5620 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
5621 nOut = pFrom->nRow + pWLoop->nOut;
5622 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
5623 if( isOrdered<0 ){
5624 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
5625 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
5626 iLoop, pWLoop, &revMask);
5627 }else{
5628 revMask = pFrom->revLoop;
5630 if( isOrdered>=0 && isOrdered<nOrderBy ){
5631 if( aSortCost[isOrdered]==0 ){
5632 aSortCost[isOrdered] = whereSortingCost(
5633 pWInfo, nRowEst, nOrderBy, isOrdered
5636 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
5638 WHERETRACE(0x002,
5639 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
5640 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
5641 rUnsorted, rCost));
5642 }else{
5643 rCost = rUnsorted;
5646 /* Check to see if pWLoop should be added to the set of
5647 ** mxChoice best-so-far paths.
5649 ** First look for an existing path among best-so-far paths
5650 ** that covers the same set of loops and has the same isOrdered
5651 ** setting as the current path candidate.
5653 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
5654 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
5655 ** of legal values for isOrdered, -1..64.
5657 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
5658 if( pTo->maskLoop==maskNew
5659 && ((pTo->isOrdered^isOrdered)&0x80)==0
5661 testcase( jj==nTo-1 );
5662 break;
5665 if( jj>=nTo ){
5666 /* None of the existing best-so-far paths match the candidate. */
5667 if( nTo>=mxChoice
5668 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
5670 /* The current candidate is no better than any of the mxChoice
5671 ** paths currently in the best-so-far buffer. So discard
5672 ** this candidate as not viable. */
5673 #ifdef WHERETRACE_ENABLED /* 0x4 */
5674 if( sqlite3WhereTrace&0x4 ){
5675 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n",
5676 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
5677 isOrdered>=0 ? isOrdered+'0' : '?');
5679 #endif
5680 continue;
5682 /* If we reach this points it means that the new candidate path
5683 ** needs to be added to the set of best-so-far paths. */
5684 if( nTo<mxChoice ){
5685 /* Increase the size of the aTo set by one */
5686 jj = nTo++;
5687 }else{
5688 /* New path replaces the prior worst to keep count below mxChoice */
5689 jj = mxI;
5691 pTo = &aTo[jj];
5692 #ifdef WHERETRACE_ENABLED /* 0x4 */
5693 if( sqlite3WhereTrace&0x4 ){
5694 sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n",
5695 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
5696 isOrdered>=0 ? isOrdered+'0' : '?');
5698 #endif
5699 }else{
5700 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
5701 ** same set of loops and has the sam isOrdered setting as the
5702 ** candidate path. Check to see if the candidate should replace
5703 ** pTo or if the candidate should be skipped */
5704 if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){
5705 #ifdef WHERETRACE_ENABLED /* 0x4 */
5706 if( sqlite3WhereTrace&0x4 ){
5707 sqlite3DebugPrintf(
5708 "Skip %s cost=%-3d,%3d order=%c",
5709 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
5710 isOrdered>=0 ? isOrdered+'0' : '?');
5711 sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n",
5712 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
5713 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
5715 #endif
5716 /* Discard the candidate path from further consideration */
5717 testcase( pTo->rCost==rCost );
5718 continue;
5720 testcase( pTo->rCost==rCost+1 );
5721 /* Control reaches here if the candidate path is better than the
5722 ** pTo path. Replace pTo with the candidate. */
5723 #ifdef WHERETRACE_ENABLED /* 0x4 */
5724 if( sqlite3WhereTrace&0x4 ){
5725 sqlite3DebugPrintf(
5726 "Update %s cost=%-3d,%3d order=%c",
5727 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
5728 isOrdered>=0 ? isOrdered+'0' : '?');
5729 sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n",
5730 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
5731 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
5733 #endif
5735 /* pWLoop is a winner. Add it to the set of best so far */
5736 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
5737 pTo->revLoop = revMask;
5738 pTo->nRow = nOut;
5739 pTo->rCost = rCost;
5740 pTo->rUnsorted = rUnsorted;
5741 pTo->isOrdered = isOrdered;
5742 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
5743 pTo->aLoop[iLoop] = pWLoop;
5744 if( nTo>=mxChoice ){
5745 mxI = 0;
5746 mxCost = aTo[0].rCost;
5747 mxUnsorted = aTo[0].nRow;
5748 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
5749 if( pTo->rCost>mxCost
5750 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
5752 mxCost = pTo->rCost;
5753 mxUnsorted = pTo->rUnsorted;
5754 mxI = jj;
5761 #ifdef WHERETRACE_ENABLED /* >=2 */
5762 if( sqlite3WhereTrace>=2 ){
5763 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
5764 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
5765 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
5766 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
5767 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
5768 if( pTo->isOrdered>0 ){
5769 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
5770 }else{
5771 sqlite3DebugPrintf("\n");
5775 #endif
5777 /* Swap the roles of aFrom and aTo for the next generation */
5778 pFrom = aTo;
5779 aTo = aFrom;
5780 aFrom = pFrom;
5781 nFrom = nTo;
5784 if( nFrom==0 ){
5785 sqlite3ErrorMsg(pParse, "no query solution");
5786 sqlite3DbFree(db, pSpace);
5787 return SQLITE_ERROR;
5790 /* Find the lowest cost path. pFrom will be left pointing to that path */
5791 pFrom = aFrom;
5792 for(ii=1; ii<nFrom; ii++){
5793 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
5795 assert( pWInfo->nLevel==nLoop );
5796 /* Load the lowest cost path into pWInfo */
5797 for(iLoop=0; iLoop<nLoop; iLoop++){
5798 WhereLevel *pLevel = pWInfo->a + iLoop;
5799 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
5800 pLevel->iFrom = pWLoop->iTab;
5801 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
5803 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
5804 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
5805 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
5806 && nRowEst
5808 Bitmask notUsed;
5809 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
5810 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
5811 if( rc==pWInfo->pResultSet->nExpr ){
5812 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
5815 if( pWInfo->pOrderBy ){
5816 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
5817 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
5818 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
5820 }else{
5821 pWInfo->nOBSat = pFrom->isOrdered;
5822 if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
5823 pWInfo->revMask = pFrom->revLoop;
5825 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
5826 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr
5828 Bitmask revMask = 0;
5829 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
5830 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
5832 assert( pWInfo->sorted==0 );
5833 if( nOrder==pWInfo->pOrderBy->nExpr ){
5834 pWInfo->sorted = 1;
5835 pWInfo->revMask = revMask;
5841 pWInfo->nRowOut = pFrom->nRow;
5843 /* Free temporary memory and return success */
5844 sqlite3DbFree(db, pSpace);
5845 return SQLITE_OK;
5849 ** Most queries use only a single table (they are not joins) and have
5850 ** simple == constraints against indexed fields. This routine attempts
5851 ** to plan those simple cases using much less ceremony than the
5852 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
5853 ** times for the common case.
5855 ** Return non-zero on success, if this query can be handled by this
5856 ** no-frills query planner. Return zero if this query needs the
5857 ** general-purpose query planner.
5859 static int whereShortCut(WhereLoopBuilder *pBuilder){
5860 WhereInfo *pWInfo;
5861 struct SrcList_item *pItem;
5862 WhereClause *pWC;
5863 WhereTerm *pTerm;
5864 WhereLoop *pLoop;
5865 int iCur;
5866 int j;
5867 Table *pTab;
5868 Index *pIdx;
5870 pWInfo = pBuilder->pWInfo;
5871 if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
5872 assert( pWInfo->pTabList->nSrc>=1 );
5873 pItem = pWInfo->pTabList->a;
5874 pTab = pItem->pTab;
5875 if( IsVirtual(pTab) ) return 0;
5876 if( pItem->zIndex ) return 0;
5877 iCur = pItem->iCursor;
5878 pWC = &pWInfo->sWC;
5879 pLoop = pBuilder->pNew;
5880 pLoop->wsFlags = 0;
5881 pLoop->u.btree.nSkip = 0;
5882 pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
5883 if( pTerm ){
5884 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
5885 pLoop->aLTerm[0] = pTerm;
5886 pLoop->nLTerm = 1;
5887 pLoop->u.btree.nEq = 1;
5888 /* TUNING: Cost of a rowid lookup is 10 */
5889 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
5890 }else{
5891 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
5892 assert( pLoop->aLTermSpace==pLoop->aLTerm );
5893 assert( ArraySize(pLoop->aLTermSpace)==4 );
5894 if( !IsUniqueIndex(pIdx)
5895 || pIdx->pPartIdxWhere!=0
5896 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
5897 ) continue;
5898 for(j=0; j<pIdx->nKeyCol; j++){
5899 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
5900 if( pTerm==0 ) break;
5901 pLoop->aLTerm[j] = pTerm;
5903 if( j!=pIdx->nKeyCol ) continue;
5904 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
5905 if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
5906 pLoop->wsFlags |= WHERE_IDX_ONLY;
5908 pLoop->nLTerm = j;
5909 pLoop->u.btree.nEq = j;
5910 pLoop->u.btree.pIndex = pIdx;
5911 /* TUNING: Cost of a unique index lookup is 15 */
5912 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
5913 break;
5916 if( pLoop->wsFlags ){
5917 pLoop->nOut = (LogEst)1;
5918 pWInfo->a[0].pWLoop = pLoop;
5919 pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
5920 pWInfo->a[0].iTabCur = iCur;
5921 pWInfo->nRowOut = 1;
5922 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
5923 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
5924 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
5926 #ifdef SQLITE_DEBUG
5927 pLoop->cId = '0';
5928 #endif
5929 return 1;
5931 return 0;
5935 ** Generate the beginning of the loop used for WHERE clause processing.
5936 ** The return value is a pointer to an opaque structure that contains
5937 ** information needed to terminate the loop. Later, the calling routine
5938 ** should invoke sqlite3WhereEnd() with the return value of this function
5939 ** in order to complete the WHERE clause processing.
5941 ** If an error occurs, this routine returns NULL.
5943 ** The basic idea is to do a nested loop, one loop for each table in
5944 ** the FROM clause of a select. (INSERT and UPDATE statements are the
5945 ** same as a SELECT with only a single table in the FROM clause.) For
5946 ** example, if the SQL is this:
5948 ** SELECT * FROM t1, t2, t3 WHERE ...;
5950 ** Then the code generated is conceptually like the following:
5952 ** foreach row1 in t1 do \ Code generated
5953 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
5954 ** foreach row3 in t3 do /
5955 ** ...
5956 ** end \ Code generated
5957 ** end |-- by sqlite3WhereEnd()
5958 ** end /
5960 ** Note that the loops might not be nested in the order in which they
5961 ** appear in the FROM clause if a different order is better able to make
5962 ** use of indices. Note also that when the IN operator appears in
5963 ** the WHERE clause, it might result in additional nested loops for
5964 ** scanning through all values on the right-hand side of the IN.
5966 ** There are Btree cursors associated with each table. t1 uses cursor
5967 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
5968 ** And so forth. This routine generates code to open those VDBE cursors
5969 ** and sqlite3WhereEnd() generates the code to close them.
5971 ** The code that sqlite3WhereBegin() generates leaves the cursors named
5972 ** in pTabList pointing at their appropriate entries. The [...] code
5973 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
5974 ** data from the various tables of the loop.
5976 ** If the WHERE clause is empty, the foreach loops must each scan their
5977 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
5978 ** the tables have indices and there are terms in the WHERE clause that
5979 ** refer to those indices, a complete table scan can be avoided and the
5980 ** code will run much faster. Most of the work of this routine is checking
5981 ** to see if there are indices that can be used to speed up the loop.
5983 ** Terms of the WHERE clause are also used to limit which rows actually
5984 ** make it to the "..." in the middle of the loop. After each "foreach",
5985 ** terms of the WHERE clause that use only terms in that loop and outer
5986 ** loops are evaluated and if false a jump is made around all subsequent
5987 ** inner loops (or around the "..." if the test occurs within the inner-
5988 ** most loop)
5990 ** OUTER JOINS
5992 ** An outer join of tables t1 and t2 is conceptally coded as follows:
5994 ** foreach row1 in t1 do
5995 ** flag = 0
5996 ** foreach row2 in t2 do
5997 ** start:
5998 ** ...
5999 ** flag = 1
6000 ** end
6001 ** if flag==0 then
6002 ** move the row2 cursor to a null row
6003 ** goto start
6004 ** fi
6005 ** end
6007 ** ORDER BY CLAUSE PROCESSING
6009 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
6010 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
6011 ** if there is one. If there is no ORDER BY clause or if this routine
6012 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
6014 ** The iIdxCur parameter is the cursor number of an index. If
6015 ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index
6016 ** to use for OR clause processing. The WHERE clause should use this
6017 ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
6018 ** the first cursor in an array of cursors for all indices. iIdxCur should
6019 ** be used to compute the appropriate cursor depending on which index is
6020 ** used.
6022 WhereInfo *sqlite3WhereBegin(
6023 Parse *pParse, /* The parser context */
6024 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
6025 Expr *pWhere, /* The WHERE clause */
6026 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
6027 ExprList *pResultSet, /* Result set of the query */
6028 u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
6029 int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */
6031 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
6032 int nTabList; /* Number of elements in pTabList */
6033 WhereInfo *pWInfo; /* Will become the return value of this function */
6034 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
6035 Bitmask notReady; /* Cursors that are not yet positioned */
6036 WhereLoopBuilder sWLB; /* The WhereLoop builder */
6037 WhereMaskSet *pMaskSet; /* The expression mask set */
6038 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
6039 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
6040 int ii; /* Loop counter */
6041 sqlite3 *db; /* Database connection */
6042 int rc; /* Return code */
6045 /* Variable initialization */
6046 db = pParse->db;
6047 memset(&sWLB, 0, sizeof(sWLB));
6049 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
6050 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
6051 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
6052 sWLB.pOrderBy = pOrderBy;
6054 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
6055 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
6056 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
6057 wctrlFlags &= ~WHERE_WANT_DISTINCT;
6060 /* The number of tables in the FROM clause is limited by the number of
6061 ** bits in a Bitmask
6063 testcase( pTabList->nSrc==BMS );
6064 if( pTabList->nSrc>BMS ){
6065 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
6066 return 0;
6069 /* This function normally generates a nested loop for all tables in
6070 ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should
6071 ** only generate code for the first table in pTabList and assume that
6072 ** any cursors associated with subsequent tables are uninitialized.
6074 nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
6076 /* Allocate and initialize the WhereInfo structure that will become the
6077 ** return value. A single allocation is used to store the WhereInfo
6078 ** struct, the contents of WhereInfo.a[], the WhereClause structure
6079 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
6080 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
6081 ** some architectures. Hence the ROUND8() below.
6083 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
6084 pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
6085 if( db->mallocFailed ){
6086 sqlite3DbFree(db, pWInfo);
6087 pWInfo = 0;
6088 goto whereBeginError;
6090 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
6091 pWInfo->nLevel = nTabList;
6092 pWInfo->pParse = pParse;
6093 pWInfo->pTabList = pTabList;
6094 pWInfo->pOrderBy = pOrderBy;
6095 pWInfo->pResultSet = pResultSet;
6096 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v);
6097 pWInfo->wctrlFlags = wctrlFlags;
6098 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
6099 pMaskSet = &pWInfo->sMaskSet;
6100 sWLB.pWInfo = pWInfo;
6101 sWLB.pWC = &pWInfo->sWC;
6102 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
6103 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
6104 whereLoopInit(sWLB.pNew);
6105 #ifdef SQLITE_DEBUG
6106 sWLB.pNew->cId = '*';
6107 #endif
6109 /* Split the WHERE clause into separate subexpressions where each
6110 ** subexpression is separated by an AND operator.
6112 initMaskSet(pMaskSet);
6113 whereClauseInit(&pWInfo->sWC, pWInfo);
6114 whereSplit(&pWInfo->sWC, pWhere, TK_AND);
6116 /* Special case: a WHERE clause that is constant. Evaluate the
6117 ** expression and either jump over all of the code or fall thru.
6119 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
6120 if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
6121 sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
6122 SQLITE_JUMPIFNULL);
6123 sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
6127 /* Special case: No FROM clause
6129 if( nTabList==0 ){
6130 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
6131 if( wctrlFlags & WHERE_WANT_DISTINCT ){
6132 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
6136 /* Assign a bit from the bitmask to every term in the FROM clause.
6138 ** When assigning bitmask values to FROM clause cursors, it must be
6139 ** the case that if X is the bitmask for the N-th FROM clause term then
6140 ** the bitmask for all FROM clause terms to the left of the N-th term
6141 ** is (X-1). An expression from the ON clause of a LEFT JOIN can use
6142 ** its Expr.iRightJoinTable value to find the bitmask of the right table
6143 ** of the join. Subtracting one from the right table bitmask gives a
6144 ** bitmask for all tables to the left of the join. Knowing the bitmask
6145 ** for all tables to the left of a left join is important. Ticket #3015.
6147 ** Note that bitmasks are created for all pTabList->nSrc tables in
6148 ** pTabList, not just the first nTabList tables. nTabList is normally
6149 ** equal to pTabList->nSrc but might be shortened to 1 if the
6150 ** WHERE_ONETABLE_ONLY flag is set.
6152 for(ii=0; ii<pTabList->nSrc; ii++){
6153 createMask(pMaskSet, pTabList->a[ii].iCursor);
6155 #ifndef NDEBUG
6157 Bitmask toTheLeft = 0;
6158 for(ii=0; ii<pTabList->nSrc; ii++){
6159 Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
6160 assert( (m-1)==toTheLeft );
6161 toTheLeft |= m;
6164 #endif
6166 /* Analyze all of the subexpressions. Note that exprAnalyze() might
6167 ** add new virtual terms onto the end of the WHERE clause. We do not
6168 ** want to analyze these virtual terms, so start analyzing at the end
6169 ** and work forward so that the added virtual terms are never processed.
6171 exprAnalyzeAll(pTabList, &pWInfo->sWC);
6172 if( db->mallocFailed ){
6173 goto whereBeginError;
6176 if( wctrlFlags & WHERE_WANT_DISTINCT ){
6177 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
6178 /* The DISTINCT marking is pointless. Ignore it. */
6179 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
6180 }else if( pOrderBy==0 ){
6181 /* Try to ORDER BY the result set to make distinct processing easier */
6182 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
6183 pWInfo->pOrderBy = pResultSet;
6187 /* Construct the WhereLoop objects */
6188 WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
6189 #if defined(WHERETRACE_ENABLED)
6190 /* Display all terms of the WHERE clause */
6191 if( sqlite3WhereTrace & 0x100 ){
6192 int i;
6193 for(i=0; i<sWLB.pWC->nTerm; i++){
6194 whereTermPrint(&sWLB.pWC->a[i], i);
6197 #endif
6199 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
6200 rc = whereLoopAddAll(&sWLB);
6201 if( rc ) goto whereBeginError;
6203 /* Display all of the WhereLoop objects if wheretrace is enabled */
6204 #ifdef WHERETRACE_ENABLED /* !=0 */
6205 if( sqlite3WhereTrace ){
6206 WhereLoop *p;
6207 int i;
6208 static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
6209 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
6210 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
6211 p->cId = zLabel[i%sizeof(zLabel)];
6212 whereLoopPrint(p, sWLB.pWC);
6215 #endif
6217 wherePathSolver(pWInfo, 0);
6218 if( db->mallocFailed ) goto whereBeginError;
6219 if( pWInfo->pOrderBy ){
6220 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
6221 if( db->mallocFailed ) goto whereBeginError;
6224 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
6225 pWInfo->revMask = (Bitmask)(-1);
6227 if( pParse->nErr || NEVER(db->mallocFailed) ){
6228 goto whereBeginError;
6230 #ifdef WHERETRACE_ENABLED /* !=0 */
6231 if( sqlite3WhereTrace ){
6232 int ii;
6233 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
6234 if( pWInfo->nOBSat>0 ){
6235 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
6237 switch( pWInfo->eDistinct ){
6238 case WHERE_DISTINCT_UNIQUE: {
6239 sqlite3DebugPrintf(" DISTINCT=unique");
6240 break;
6242 case WHERE_DISTINCT_ORDERED: {
6243 sqlite3DebugPrintf(" DISTINCT=ordered");
6244 break;
6246 case WHERE_DISTINCT_UNORDERED: {
6247 sqlite3DebugPrintf(" DISTINCT=unordered");
6248 break;
6251 sqlite3DebugPrintf("\n");
6252 for(ii=0; ii<pWInfo->nLevel; ii++){
6253 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
6256 #endif
6257 /* Attempt to omit tables from the join that do not effect the result */
6258 if( pWInfo->nLevel>=2
6259 && pResultSet!=0
6260 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
6262 Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
6263 if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
6264 while( pWInfo->nLevel>=2 ){
6265 WhereTerm *pTerm, *pEnd;
6266 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
6267 if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
6268 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
6269 && (pLoop->wsFlags & WHERE_ONEROW)==0
6271 break;
6273 if( (tabUsed & pLoop->maskSelf)!=0 ) break;
6274 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
6275 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
6276 if( (pTerm->prereqAll & pLoop->maskSelf)!=0
6277 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
6279 break;
6282 if( pTerm<pEnd ) break;
6283 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
6284 pWInfo->nLevel--;
6285 nTabList--;
6288 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
6289 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
6291 /* If the caller is an UPDATE or DELETE statement that is requesting
6292 ** to use a one-pass algorithm, determine if this is appropriate.
6293 ** The one-pass algorithm only works if the WHERE clause constrains
6294 ** the statement to update a single row.
6296 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
6297 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
6298 && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
6299 pWInfo->okOnePass = 1;
6300 if( HasRowid(pTabList->a[0].pTab) ){
6301 pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
6305 /* Open all tables in the pTabList and any indices selected for
6306 ** searching those tables.
6308 notReady = ~(Bitmask)0;
6309 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
6310 Table *pTab; /* Table to open */
6311 int iDb; /* Index of database containing table/index */
6312 struct SrcList_item *pTabItem;
6314 pTabItem = &pTabList->a[pLevel->iFrom];
6315 pTab = pTabItem->pTab;
6316 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
6317 pLoop = pLevel->pWLoop;
6318 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
6319 /* Do nothing */
6320 }else
6321 #ifndef SQLITE_OMIT_VIRTUALTABLE
6322 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
6323 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
6324 int iCur = pTabItem->iCursor;
6325 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
6326 }else if( IsVirtual(pTab) ){
6327 /* noop */
6328 }else
6329 #endif
6330 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
6331 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
6332 int op = OP_OpenRead;
6333 if( pWInfo->okOnePass ){
6334 op = OP_OpenWrite;
6335 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
6337 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
6338 assert( pTabItem->iCursor==pLevel->iTabCur );
6339 testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
6340 testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
6341 if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){
6342 Bitmask b = pTabItem->colUsed;
6343 int n = 0;
6344 for(; b; b=b>>1, n++){}
6345 sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
6346 SQLITE_INT_TO_PTR(n), P4_INT32);
6347 assert( n<=pTab->nCol );
6349 }else{
6350 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
6352 if( pLoop->wsFlags & WHERE_INDEXED ){
6353 Index *pIx = pLoop->u.btree.pIndex;
6354 int iIndexCur;
6355 int op = OP_OpenRead;
6356 /* iIdxCur is always set if to a positive value if ONEPASS is possible */
6357 assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
6358 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
6359 && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0
6361 /* This is one term of an OR-optimization using the PRIMARY KEY of a
6362 ** WITHOUT ROWID table. No need for a separate index */
6363 iIndexCur = pLevel->iTabCur;
6364 op = 0;
6365 }else if( pWInfo->okOnePass ){
6366 Index *pJ = pTabItem->pTab->pIndex;
6367 iIndexCur = iIdxCur;
6368 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
6369 while( ALWAYS(pJ) && pJ!=pIx ){
6370 iIndexCur++;
6371 pJ = pJ->pNext;
6373 op = OP_OpenWrite;
6374 pWInfo->aiCurOnePass[1] = iIndexCur;
6375 }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
6376 iIndexCur = iIdxCur;
6377 if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx;
6378 }else{
6379 iIndexCur = pParse->nTab++;
6381 pLevel->iIdxCur = iIndexCur;
6382 assert( pIx->pSchema==pTab->pSchema );
6383 assert( iIndexCur>=0 );
6384 if( op ){
6385 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
6386 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
6387 VdbeComment((v, "%s", pIx->zName));
6390 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
6391 notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
6393 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
6394 if( db->mallocFailed ) goto whereBeginError;
6396 /* Generate the code to do the search. Each iteration of the for
6397 ** loop below generates code for a single nested loop of the VM
6398 ** program.
6400 notReady = ~(Bitmask)0;
6401 for(ii=0; ii<nTabList; ii++){
6402 pLevel = &pWInfo->a[ii];
6403 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
6404 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
6405 constructAutomaticIndex(pParse, &pWInfo->sWC,
6406 &pTabList->a[pLevel->iFrom], notReady, pLevel);
6407 if( db->mallocFailed ) goto whereBeginError;
6409 #endif
6410 explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
6411 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
6412 notReady = codeOneLoopStart(pWInfo, ii, notReady);
6413 pWInfo->iContinue = pLevel->addrCont;
6416 /* Done. */
6417 VdbeModuleComment((v, "Begin WHERE-core"));
6418 return pWInfo;
6420 /* Jump here if malloc fails */
6421 whereBeginError:
6422 if( pWInfo ){
6423 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
6424 whereInfoFree(db, pWInfo);
6426 return 0;
6430 ** Generate the end of the WHERE loop. See comments on
6431 ** sqlite3WhereBegin() for additional information.
6433 void sqlite3WhereEnd(WhereInfo *pWInfo){
6434 Parse *pParse = pWInfo->pParse;
6435 Vdbe *v = pParse->pVdbe;
6436 int i;
6437 WhereLevel *pLevel;
6438 WhereLoop *pLoop;
6439 SrcList *pTabList = pWInfo->pTabList;
6440 sqlite3 *db = pParse->db;
6442 /* Generate loop termination code.
6444 VdbeModuleComment((v, "End WHERE-core"));
6445 sqlite3ExprCacheClear(pParse);
6446 for(i=pWInfo->nLevel-1; i>=0; i--){
6447 int addr;
6448 pLevel = &pWInfo->a[i];
6449 pLoop = pLevel->pWLoop;
6450 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
6451 if( pLevel->op!=OP_Noop ){
6452 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
6453 sqlite3VdbeChangeP5(v, pLevel->p5);
6454 VdbeCoverage(v);
6455 VdbeCoverageIf(v, pLevel->op==OP_Next);
6456 VdbeCoverageIf(v, pLevel->op==OP_Prev);
6457 VdbeCoverageIf(v, pLevel->op==OP_VNext);
6459 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
6460 struct InLoop *pIn;
6461 int j;
6462 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
6463 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
6464 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
6465 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
6466 VdbeCoverage(v);
6467 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
6468 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
6469 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
6471 sqlite3DbFree(db, pLevel->u.in.aInLoop);
6473 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
6474 if( pLevel->addrSkip ){
6475 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
6476 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
6477 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
6478 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
6480 if( pLevel->iLeftJoin ){
6481 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
6482 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
6483 || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
6484 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
6485 sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
6487 if( pLoop->wsFlags & WHERE_INDEXED ){
6488 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
6490 if( pLevel->op==OP_Return ){
6491 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
6492 }else{
6493 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
6495 sqlite3VdbeJumpHere(v, addr);
6497 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
6498 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
6501 /* The "break" point is here, just past the end of the outer loop.
6502 ** Set it.
6504 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
6506 assert( pWInfo->nLevel<=pTabList->nSrc );
6507 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
6508 int k, last;
6509 VdbeOp *pOp;
6510 Index *pIdx = 0;
6511 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
6512 Table *pTab = pTabItem->pTab;
6513 assert( pTab!=0 );
6514 pLoop = pLevel->pWLoop;
6516 /* For a co-routine, change all OP_Column references to the table of
6517 ** the co-routine into OP_SCopy of result contained in a register.
6518 ** OP_Rowid becomes OP_Null.
6520 if( pTabItem->viaCoroutine && !db->mallocFailed ){
6521 last = sqlite3VdbeCurrentAddr(v);
6522 k = pLevel->addrBody;
6523 pOp = sqlite3VdbeGetOp(v, k);
6524 for(; k<last; k++, pOp++){
6525 if( pOp->p1!=pLevel->iTabCur ) continue;
6526 if( pOp->opcode==OP_Column ){
6527 pOp->opcode = OP_Copy;
6528 pOp->p1 = pOp->p2 + pTabItem->regResult;
6529 pOp->p2 = pOp->p3;
6530 pOp->p3 = 0;
6531 }else if( pOp->opcode==OP_Rowid ){
6532 pOp->opcode = OP_Null;
6533 pOp->p1 = 0;
6534 pOp->p3 = 0;
6537 continue;
6540 /* Close all of the cursors that were opened by sqlite3WhereBegin.
6541 ** Except, do not close cursors that will be reused by the OR optimization
6542 ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors
6543 ** created for the ONEPASS optimization.
6545 if( (pTab->tabFlags & TF_Ephemeral)==0
6546 && pTab->pSelect==0
6547 && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
6549 int ws = pLoop->wsFlags;
6550 if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
6551 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
6553 if( (ws & WHERE_INDEXED)!=0
6554 && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
6555 && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
6557 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
6561 /* If this scan uses an index, make VDBE code substitutions to read data
6562 ** from the index instead of from the table where possible. In some cases
6563 ** this optimization prevents the table from ever being read, which can
6564 ** yield a significant performance boost.
6566 ** Calls to the code generator in between sqlite3WhereBegin and
6567 ** sqlite3WhereEnd will have created code that references the table
6568 ** directly. This loop scans all that code looking for opcodes
6569 ** that reference the table and converts them into opcodes that
6570 ** reference the index.
6572 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
6573 pIdx = pLoop->u.btree.pIndex;
6574 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
6575 pIdx = pLevel->u.pCovidx;
6577 if( pIdx && !db->mallocFailed ){
6578 last = sqlite3VdbeCurrentAddr(v);
6579 k = pLevel->addrBody;
6580 pOp = sqlite3VdbeGetOp(v, k);
6581 for(; k<last; k++, pOp++){
6582 if( pOp->p1!=pLevel->iTabCur ) continue;
6583 if( pOp->opcode==OP_Column ){
6584 int x = pOp->p2;
6585 assert( pIdx->pTable==pTab );
6586 if( !HasRowid(pTab) ){
6587 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
6588 x = pPk->aiColumn[x];
6590 x = sqlite3ColumnOfIndex(pIdx, x);
6591 if( x>=0 ){
6592 pOp->p2 = x;
6593 pOp->p1 = pLevel->iIdxCur;
6595 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
6596 }else if( pOp->opcode==OP_Rowid ){
6597 pOp->p1 = pLevel->iIdxCur;
6598 pOp->opcode = OP_IdxRowid;
6604 /* Final cleanup
6606 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
6607 whereInfoFree(db, pWInfo);
6608 return;