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