xref: /sqlite-3.40.0/src/where.c (revision ef5ecb41)
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.
14 **
15 ** $Id: where.c,v 1.104 2004/06/10 10:51:48 danielk1977 Exp $
16 */
17 #include "sqliteInt.h"
18 
19 /*
20 ** The query generator uses an array of instances of this structure to
21 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
22 ** clause subexpression is separated from the others by an AND operator.
23 */
24 typedef struct ExprInfo ExprInfo;
25 struct ExprInfo {
26   Expr *p;                /* Pointer to the subexpression */
27   u8 indexable;           /* True if this subexprssion is usable by an index */
28   short int idxLeft;      /* p->pLeft is a column in this table number. -1 if
29                           ** p->pLeft is not the column of any table */
30   short int idxRight;     /* p->pRight is a column in this table number. -1 if
31                           ** p->pRight is not the column of any table */
32   unsigned prereqLeft;    /* Bitmask of tables referenced by p->pLeft */
33   unsigned prereqRight;   /* Bitmask of tables referenced by p->pRight */
34   unsigned prereqAll;     /* Bitmask of tables referenced by p */
35 };
36 
37 /*
38 ** An instance of the following structure keeps track of a mapping
39 ** between VDBE cursor numbers and bitmasks.  The VDBE cursor numbers
40 ** are small integers contained in SrcList_item.iCursor and Expr.iTable
41 ** fields.  For any given WHERE clause, we want to track which cursors
42 ** are being used, so we assign a single bit in a 32-bit word to track
43 ** that cursor.  Then a 32-bit integer is able to show the set of all
44 ** cursors being used.
45 */
46 typedef struct ExprMaskSet ExprMaskSet;
47 struct ExprMaskSet {
48   int n;          /* Number of assigned cursor values */
49   int ix[32];     /* Cursor assigned to each bit */
50 };
51 
52 /*
53 ** Determine the number of elements in an array.
54 */
55 #define ARRAYSIZE(X)  (sizeof(X)/sizeof(X[0]))
56 
57 /*
58 ** This routine is used to divide the WHERE expression into subexpressions
59 ** separated by the AND operator.
60 **
61 ** aSlot[] is an array of subexpressions structures.
62 ** There are nSlot spaces left in this array.  This routine attempts to
63 ** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
64 ** The return value is the number of slots filled.
65 */
66 static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
67   int cnt = 0;
68   if( pExpr==0 || nSlot<1 ) return 0;
69   if( nSlot==1 || pExpr->op!=TK_AND ){
70     aSlot[0].p = pExpr;
71     return 1;
72   }
73   if( pExpr->pLeft->op!=TK_AND ){
74     aSlot[0].p = pExpr->pLeft;
75     cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
76   }else{
77     cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
78     cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
79   }
80   return cnt;
81 }
82 
83 /*
84 ** Initialize an expression mask set
85 */
86 #define initMaskSet(P)  memset(P, 0, sizeof(*P))
87 
88 /*
89 ** Return the bitmask for the given cursor.  Assign a new bitmask
90 ** if this is the first time the cursor has been seen.
91 */
92 static int getMask(ExprMaskSet *pMaskSet, int iCursor){
93   int i;
94   for(i=0; i<pMaskSet->n; i++){
95     if( pMaskSet->ix[i]==iCursor ) return 1<<i;
96   }
97   if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
98     pMaskSet->n++;
99     pMaskSet->ix[i] = iCursor;
100     return 1<<i;
101   }
102   return 0;
103 }
104 
105 /*
106 ** Destroy an expression mask set
107 */
108 #define freeMaskSet(P)   /* NO-OP */
109 
110 /*
111 ** This routine walks (recursively) an expression tree and generates
112 ** a bitmask indicating which tables are used in that expression
113 ** tree.
114 **
115 ** In order for this routine to work, the calling function must have
116 ** previously invoked sqlite3ExprResolveIds() on the expression.  See
117 ** the header comment on that routine for additional information.
118 ** The sqlite3ExprResolveIds() routines looks for column names and
119 ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
120 ** the VDBE cursor number of the table.
121 */
122 static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
123   unsigned int mask = 0;
124   if( p==0 ) return 0;
125   if( p->op==TK_COLUMN ){
126     return getMask(pMaskSet, p->iTable);
127   }
128   if( p->pRight ){
129     mask = exprTableUsage(pMaskSet, p->pRight);
130   }
131   if( p->pLeft ){
132     mask |= exprTableUsage(pMaskSet, p->pLeft);
133   }
134   if( p->pList ){
135     int i;
136     for(i=0; i<p->pList->nExpr; i++){
137       mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
138     }
139   }
140   return mask;
141 }
142 
143 /*
144 ** Return TRUE if the given operator is one of the operators that is
145 ** allowed for an indexable WHERE clause.  The allowed operators are
146 ** "=", "<", ">", "<=", ">=", and "IN".
147 */
148 static int allowedOp(int op){
149   switch( op ){
150     case TK_LT:
151     case TK_LE:
152     case TK_GT:
153     case TK_GE:
154     case TK_EQ:
155     case TK_IN:
156       return 1;
157     default:
158       return 0;
159   }
160 }
161 
162 /*
163 ** The input to this routine is an ExprInfo structure with only the
164 ** "p" field filled in.  The job of this routine is to analyze the
165 ** subexpression and populate all the other fields of the ExprInfo
166 ** structure.
167 */
168 static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
169   Expr *pExpr = pInfo->p;
170   pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
171   pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
172   pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
173   pInfo->indexable = 0;
174   pInfo->idxLeft = -1;
175   pInfo->idxRight = -1;
176   if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
177     if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
178       pInfo->idxRight = pExpr->pRight->iTable;
179       pInfo->indexable = 1;
180     }
181     if( pExpr->pLeft->op==TK_COLUMN ){
182       pInfo->idxLeft = pExpr->pLeft->iTable;
183       pInfo->indexable = 1;
184     }
185   }
186 }
187 
188 /*
189 ** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
190 ** left-most table in the FROM clause of that same SELECT statement and
191 ** the table has a cursor number of "base".
192 **
193 ** This routine attempts to find an index for pTab that generates the
194 ** correct record sequence for the given ORDER BY clause.  The return value
195 ** is a pointer to an index that does the job.  NULL is returned if the
196 ** table has no index that will generate the correct sort order.
197 **
198 ** If there are two or more indices that generate the correct sort order
199 ** and pPreferredIdx is one of those indices, then return pPreferredIdx.
200 **
201 ** nEqCol is the number of columns of pPreferredIdx that are used as
202 ** equality constraints.  Any index returned must have exactly this same
203 ** set of columns.  The ORDER BY clause only matches index columns beyond the
204 ** the first nEqCol columns.
205 **
206 ** All terms of the ORDER BY clause must be either ASC or DESC.  The
207 ** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
208 ** set to 0 if the ORDER BY clause is all ASC.
209 */
210 static Index *findSortingIndex(
211   Parse *pParse,
212   Table *pTab,            /* The table to be sorted */
213   int base,               /* Cursor number for pTab */
214   ExprList *pOrderBy,     /* The ORDER BY clause */
215   Index *pPreferredIdx,   /* Use this index, if possible and not NULL */
216   int nEqCol,             /* Number of index columns used with == constraints */
217   int *pbRev              /* Set to 1 if ORDER BY is DESC */
218 ){
219   int i, j;
220   Index *pMatch;
221   Index *pIdx;
222   int sortOrder;
223   sqlite *db = pParse->db;
224 
225   assert( pOrderBy!=0 );
226   assert( pOrderBy->nExpr>0 );
227   sortOrder = pOrderBy->a[0].sortOrder;
228   for(i=0; i<pOrderBy->nExpr; i++){
229     Expr *p;
230     if( pOrderBy->a[i].sortOrder!=sortOrder ){
231       /* Indices can only be used if all ORDER BY terms are either
232       ** DESC or ASC.  Indices cannot be used on a mixture. */
233       return 0;
234     }
235     p = pOrderBy->a[i].pExpr;
236     if( p->op!=TK_COLUMN || p->iTable!=base ){
237       /* Can not use an index sort on anything that is not a column in the
238       ** left-most table of the FROM clause */
239       return 0;
240     }
241   }
242 
243   /* If we get this far, it means the ORDER BY clause consists only of
244   ** ascending columns in the left-most table of the FROM clause.  Now
245   ** check for a matching index.
246   */
247   pMatch = 0;
248   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
249     int nExpr = pOrderBy->nExpr;
250     if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
251     for(i=j=0; i<nEqCol; i++){
252       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[j].pExpr);
253       if( !pColl ) pColl = db->pDfltColl;
254       if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
255       if( pPreferredIdx->keyInfo.aColl[i]!=pIdx->keyInfo.aColl[i] ) break;
256       if( j<nExpr &&
257           pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] &&
258           pColl==pIdx->keyInfo.aColl[i]
259       ){
260         j++;
261       }
262     }
263     if( i<nEqCol ) continue;
264     for(i=0; i+j<nExpr; i++){
265       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[i+j].pExpr);
266       if( !pColl ) pColl = db->pDfltColl;
267       if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ||
268           pColl!=pIdx->keyInfo.aColl[i+nEqCol] ) break;
269     }
270     if( i+j>=nExpr ){
271       pMatch = pIdx;
272       if( pIdx==pPreferredIdx ) break;
273     }
274   }
275   if( pMatch && pbRev ){
276     *pbRev = sortOrder==SQLITE_SO_DESC;
277   }
278   return pMatch;
279 }
280 
281 /*
282 ** Generate the beginning of the loop used for WHERE clause processing.
283 ** The return value is a pointer to an (opaque) structure that contains
284 ** information needed to terminate the loop.  Later, the calling routine
285 ** should invoke sqlite3WhereEnd() with the return value of this function
286 ** in order to complete the WHERE clause processing.
287 **
288 ** If an error occurs, this routine returns NULL.
289 **
290 ** The basic idea is to do a nested loop, one loop for each table in
291 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
292 ** same as a SELECT with only a single table in the FROM clause.)  For
293 ** example, if the SQL is this:
294 **
295 **       SELECT * FROM t1, t2, t3 WHERE ...;
296 **
297 ** Then the code generated is conceptually like the following:
298 **
299 **      foreach row1 in t1 do       \    Code generated
300 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
301 **          foreach row3 in t3 do   /
302 **            ...
303 **          end                     \    Code generated
304 **        end                        |-- by sqlite3WhereEnd()
305 **      end                         /
306 **
307 ** There are Btree cursors associated with each table.  t1 uses cursor
308 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
309 ** And so forth.  This routine generates code to open those VDBE cursors
310 ** and sqlite3WhereEnd() generates the code to close them.
311 **
312 ** If the WHERE clause is empty, the foreach loops must each scan their
313 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
314 ** the tables have indices and there are terms in the WHERE clause that
315 ** refer to those indices, a complete table scan can be avoided and the
316 ** code will run much faster.  Most of the work of this routine is checking
317 ** to see if there are indices that can be used to speed up the loop.
318 **
319 ** Terms of the WHERE clause are also used to limit which rows actually
320 ** make it to the "..." in the middle of the loop.  After each "foreach",
321 ** terms of the WHERE clause that use only terms in that loop and outer
322 ** loops are evaluated and if false a jump is made around all subsequent
323 ** inner loops (or around the "..." if the test occurs within the inner-
324 ** most loop)
325 **
326 ** OUTER JOINS
327 **
328 ** An outer join of tables t1 and t2 is conceptally coded as follows:
329 **
330 **    foreach row1 in t1 do
331 **      flag = 0
332 **      foreach row2 in t2 do
333 **        start:
334 **          ...
335 **          flag = 1
336 **      end
337 **      if flag==0 then
338 **        move the row2 cursor to a null row
339 **        goto start
340 **      fi
341 **    end
342 **
343 ** ORDER BY CLAUSE PROCESSING
344 **
345 ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
346 ** if there is one.  If there is no ORDER BY clause or if this routine
347 ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
348 **
349 ** If an index can be used so that the natural output order of the table
350 ** scan is correct for the ORDER BY clause, then that index is used and
351 ** *ppOrderBy is set to NULL.  This is an optimization that prevents an
352 ** unnecessary sort of the result set if an index appropriate for the
353 ** ORDER BY clause already exists.
354 **
355 ** If the where clause loops cannot be arranged to provide the correct
356 ** output order, then the *ppOrderBy is unchanged.
357 */
358 WhereInfo *sqlite3WhereBegin(
359   Parse *pParse,       /* The parser context */
360   SrcList *pTabList,   /* A list of all tables to be scanned */
361   Expr *pWhere,        /* The WHERE clause */
362   int pushKey,         /* If TRUE, leave the table key on the stack */
363   ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
364 ){
365   int i;                     /* Loop counter */
366   WhereInfo *pWInfo;         /* Will become the return value of this function */
367   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
368   int brk, cont = 0;         /* Addresses used during code generation */
369   int nExpr;           /* Number of subexpressions in the WHERE clause */
370   int loopMask;        /* One bit set for each outer loop */
371   int haveKey;         /* True if KEY is on the stack */
372   ExprMaskSet maskSet; /* The expression mask set */
373   int iDirectEq[32];   /* Term of the form ROWID==X for the N-th table */
374   int iDirectLt[32];   /* Term of the form ROWID<X or ROWID<=X */
375   int iDirectGt[32];   /* Term of the form ROWID>X or ROWID>=X */
376   ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
377 
378   /* pushKey is only allowed if there is a single table (as in an INSERT or
379   ** UPDATE statement)
380   */
381   assert( pushKey==0 || pTabList->nSrc==1 );
382 
383   /* Split the WHERE clause into separate subexpressions where each
384   ** subexpression is separated by an AND operator.  If the aExpr[]
385   ** array fills up, the last entry might point to an expression which
386   ** contains additional unfactored AND operators.
387   */
388   initMaskSet(&maskSet);
389   memset(aExpr, 0, sizeof(aExpr));
390   nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
391   if( nExpr==ARRAYSIZE(aExpr) ){
392     sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more "
393        "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
394     return 0;
395   }
396 
397   /* Allocate and initialize the WhereInfo structure that will become the
398   ** return value.
399   */
400   pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
401   if( sqlite3_malloc_failed ){
402     sqliteFree(pWInfo);
403     return 0;
404   }
405   pWInfo->pParse = pParse;
406   pWInfo->pTabList = pTabList;
407   pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
408   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
409 
410   /* Special case: a WHERE clause that is constant.  Evaluate the
411   ** expression and either jump over all of the code or fall thru.
412   */
413   if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
414     sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
415     pWhere = 0;
416   }
417 
418   /* Analyze all of the subexpressions.
419   */
420   for(i=0; i<nExpr; i++){
421     exprAnalyze(&maskSet, &aExpr[i]);
422 
423     /* If we are executing a trigger body, remove all references to
424     ** new.* and old.* tables from the prerequisite masks.
425     */
426     if( pParse->trigStack ){
427       int x;
428       if( (x = pParse->trigStack->newIdx) >= 0 ){
429         int mask = ~getMask(&maskSet, x);
430         aExpr[i].prereqRight &= mask;
431         aExpr[i].prereqLeft &= mask;
432         aExpr[i].prereqAll &= mask;
433       }
434       if( (x = pParse->trigStack->oldIdx) >= 0 ){
435         int mask = ~getMask(&maskSet, x);
436         aExpr[i].prereqRight &= mask;
437         aExpr[i].prereqLeft &= mask;
438         aExpr[i].prereqAll &= mask;
439       }
440     }
441   }
442 
443   /* Figure out what index to use (if any) for each nested loop.
444   ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
445   ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
446   ** loop.
447   **
448   ** If terms exist that use the ROWID of any table, then set the
449   ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
450   ** to the index of the term containing the ROWID.  We always prefer
451   ** to use a ROWID which can directly access a table rather than an
452   ** index which requires reading an index first to get the rowid then
453   ** doing a second read of the actual database table.
454   **
455   ** Actually, if there are more than 32 tables in the join, only the
456   ** first 32 tables are candidates for indices.  This is (again) due
457   ** to the limit of 32 bits in an integer bitmask.
458   */
459   loopMask = 0;
460   for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
461     int j;
462     int iCur = pTabList->a[i].iCursor;    /* The cursor for this table */
463     int mask = getMask(&maskSet, iCur);   /* Cursor mask for this table */
464     Table *pTab = pTabList->a[i].pTab;
465     Index *pIdx;
466     Index *pBestIdx = 0;
467     int bestScore = 0;
468 
469     /* Check to see if there is an expression that uses only the
470     ** ROWID field of this table.  For terms of the form ROWID==expr
471     ** set iDirectEq[i] to the index of the term.  For terms of the
472     ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
473     ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
474     **
475     ** (Added:) Treat ROWID IN expr like ROWID=expr.
476     */
477     pWInfo->a[i].iCur = -1;
478     iDirectEq[i] = -1;
479     iDirectLt[i] = -1;
480     iDirectGt[i] = -1;
481     for(j=0; j<nExpr; j++){
482       if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
483             && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
484         switch( aExpr[j].p->op ){
485           case TK_IN:
486           case TK_EQ: iDirectEq[i] = j; break;
487           case TK_LE:
488           case TK_LT: iDirectLt[i] = j; break;
489           case TK_GE:
490           case TK_GT: iDirectGt[i] = j;  break;
491         }
492       }
493       if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
494             && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
495         switch( aExpr[j].p->op ){
496           case TK_EQ: iDirectEq[i] = j;  break;
497           case TK_LE:
498           case TK_LT: iDirectGt[i] = j;  break;
499           case TK_GE:
500           case TK_GT: iDirectLt[i] = j;  break;
501         }
502       }
503     }
504     if( iDirectEq[i]>=0 ){
505       loopMask |= mask;
506       pWInfo->a[i].pIdx = 0;
507       continue;
508     }
509 
510     /* Do a search for usable indices.  Leave pBestIdx pointing to
511     ** the "best" index.  pBestIdx is left set to NULL if no indices
512     ** are usable.
513     **
514     ** The best index is determined as follows.  For each of the
515     ** left-most terms that is fixed by an equality operator, add
516     ** 8 to the score.  The right-most term of the index may be
517     ** constrained by an inequality.  Add 1 if for an "x<..." constraint
518     ** and add 2 for an "x>..." constraint.  Chose the index that
519     ** gives the best score.
520     **
521     ** This scoring system is designed so that the score can later be
522     ** used to determine how the index is used.  If the score&7 is 0
523     ** then all constraints are equalities.  If score&1 is not 0 then
524     ** there is an inequality used as a termination key.  (ex: "x<...")
525     ** If score&2 is not 0 then there is an inequality used as the
526     ** start key.  (ex: "x>...").  A score or 4 is the special case
527     ** of an IN operator constraint.  (ex:  "x IN ...").
528     **
529     ** The IN operator (as in "<expr> IN (...)") is treated the same as
530     ** an equality comparison except that it can only be used on the
531     ** left-most column of an index and other terms of the WHERE clause
532     ** cannot be used in conjunction with the IN operator to help satisfy
533     ** other columns of the index.
534     */
535     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
536       int eqMask = 0;  /* Index columns covered by an x=... term */
537       int ltMask = 0;  /* Index columns covered by an x<... term */
538       int gtMask = 0;  /* Index columns covered by an x>... term */
539       int inMask = 0;  /* Index columns covered by an x IN .. term */
540       int nEq, m, score;
541 
542       if( pIdx->nColumn>32 ) continue;  /* Ignore indices too many columns */
543       for(j=0; j<nExpr; j++){
544         CollSeq *pColl = sqlite3ExprCollSeq(pParse, aExpr[j].p->pLeft);
545         if( !pColl && aExpr[j].p->pRight ){
546           pColl = sqlite3ExprCollSeq(pParse, aExpr[j].p->pRight);
547         }
548         if( !pColl ){
549           pColl = pParse->db->pDfltColl;
550         }
551         if( aExpr[j].idxLeft==iCur
552              && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
553           int iColumn = aExpr[j].p->pLeft->iColumn;
554           int k;
555           char idxaff = pIdx->pTable->aCol[iColumn].affinity;
556           for(k=0; k<pIdx->nColumn; k++){
557             /* If the collating sequences or affinities don't match,
558             ** ignore this index.  */
559             if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
560             if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
561             if( pIdx->aiColumn[k]==iColumn ){
562               switch( aExpr[j].p->op ){
563                 case TK_IN: {
564                   if( k==0 ) inMask |= 1;
565                   break;
566                 }
567                 case TK_EQ: {
568                   eqMask |= 1<<k;
569                   break;
570                 }
571                 case TK_LE:
572                 case TK_LT: {
573                   ltMask |= 1<<k;
574                   break;
575                 }
576                 case TK_GE:
577                 case TK_GT: {
578                   gtMask |= 1<<k;
579                   break;
580                 }
581                 default: {
582                   /* CANT_HAPPEN */
583                   assert( 0 );
584                   break;
585                 }
586               }
587               break;
588             }
589           }
590         }
591         if( aExpr[j].idxRight==iCur
592              && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
593           int iColumn = aExpr[j].p->pRight->iColumn;
594           int k;
595           char idxaff = pIdx->pTable->aCol[iColumn].affinity;
596           for(k=0; k<pIdx->nColumn; k++){
597             /* If the collating sequences or affinities don't match,
598             ** ignore this index.  */
599             if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
600             if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
601             if( pIdx->aiColumn[k]==iColumn ){
602               switch( aExpr[j].p->op ){
603                 case TK_EQ: {
604                   eqMask |= 1<<k;
605                   break;
606                 }
607                 case TK_LE:
608                 case TK_LT: {
609                   gtMask |= 1<<k;
610                   break;
611                 }
612                 case TK_GE:
613                 case TK_GT: {
614                   ltMask |= 1<<k;
615                   break;
616                 }
617                 default: {
618                   /* CANT_HAPPEN */
619                   assert( 0 );
620                   break;
621                 }
622               }
623               break;
624             }
625           }
626         }
627       }
628 
629       /* The following loop ends with nEq set to the number of columns
630       ** on the left of the index with == constraints.
631       */
632       for(nEq=0; nEq<pIdx->nColumn; nEq++){
633         m = (1<<(nEq+1))-1;
634         if( (m & eqMask)!=m ) break;
635       }
636       score = nEq*8;   /* Base score is 8 times number of == constraints */
637       m = 1<<nEq;
638       if( m & ltMask ) score++;    /* Increase score for a < constraint */
639       if( m & gtMask ) score+=2;   /* Increase score for a > constraint */
640       if( score==0 && inMask ) score = 4;  /* Default score for IN constraint */
641       if( score>bestScore ){
642         pBestIdx = pIdx;
643         bestScore = score;
644       }
645     }
646     pWInfo->a[i].pIdx = pBestIdx;
647     pWInfo->a[i].score = bestScore;
648     pWInfo->a[i].bRev = 0;
649     loopMask |= mask;
650     if( pBestIdx ){
651       pWInfo->a[i].iCur = pParse->nTab++;
652       pWInfo->peakNTab = pParse->nTab;
653     }
654   }
655 
656   /* Check to see if the ORDER BY clause is or can be satisfied by the
657   ** use of an index on the first table.
658   */
659   if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
660      Index *pSortIdx;
661      Index *pIdx;
662      Table *pTab;
663      int bRev = 0;
664 
665      pTab = pTabList->a[0].pTab;
666      pIdx = pWInfo->a[0].pIdx;
667      if( pIdx && pWInfo->a[0].score==4 ){
668        /* If there is already an IN index on the left-most table,
669        ** it will not give the correct sort order.
670        ** So, pretend that no suitable index is found.
671        */
672        pSortIdx = 0;
673      }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
674        /* If the left-most column is accessed using its ROWID, then do
675        ** not try to sort by index.
676        */
677        pSortIdx = 0;
678      }else{
679        int nEqCol = (pWInfo->a[0].score+4)/8;
680        pSortIdx = findSortingIndex(pParse, pTab, pTabList->a[0].iCursor,
681                                    *ppOrderBy, pIdx, nEqCol, &bRev);
682      }
683      if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
684        if( pIdx==0 ){
685          pWInfo->a[0].pIdx = pSortIdx;
686          pWInfo->a[0].iCur = pParse->nTab++;
687          pWInfo->peakNTab = pParse->nTab;
688        }
689        pWInfo->a[0].bRev = bRev;
690        *ppOrderBy = 0;
691      }
692   }
693 
694   /* Open all tables in the pTabList and all indices used by those tables.
695   */
696   sqlite3CodeVerifySchema(pParse, 1);  /* Inserts the cookie verifier Goto */
697   for(i=0; i<pTabList->nSrc; i++){
698     Table *pTab;
699     Index *pIx;
700 
701     pTab = pTabList->a[i].pTab;
702     if( pTab->isTransient || pTab->pSelect ) continue;
703     sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
704     sqlite3VdbeAddOp(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum);
705     sqlite3VdbeAddOp(v, OP_SetNumColumns, pTabList->a[i].iCursor, pTab->nCol);
706     if( pTab->tnum>1 ){
707       sqlite3CodeVerifySchema(pParse, pTab->iDb);
708     }
709     if( (pIx = pWInfo->a[i].pIdx)!=0 ){
710       sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
711       sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum,
712                      (char*)&pIx->keyInfo, P3_KEYINFO);
713     }
714   }
715 
716   /* Generate the code to do the search
717   */
718   loopMask = 0;
719   for(i=0; i<pTabList->nSrc; i++){
720     int j, k;
721     int iCur = pTabList->a[i].iCursor;
722     Index *pIdx;
723     WhereLevel *pLevel = &pWInfo->a[i];
724 
725     /* If this is the right table of a LEFT OUTER JOIN, allocate and
726     ** initialize a memory cell that records if this table matches any
727     ** row of the left table of the join.
728     */
729     if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
730       if( !pParse->nMem ) pParse->nMem++;
731       pLevel->iLeftJoin = pParse->nMem++;
732       sqlite3VdbeAddOp(v, OP_String8, 0, 0);
733       sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
734     }
735 
736     pIdx = pLevel->pIdx;
737     pLevel->inOp = OP_Noop;
738     if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
739       /* Case 1:  We can directly reference a single row using an
740       **          equality comparison against the ROWID field.  Or
741       **          we reference multiple rows using a "rowid IN (...)"
742       **          construct.
743       */
744       k = iDirectEq[i];
745       assert( k<nExpr );
746       assert( aExpr[k].p!=0 );
747       assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
748       brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
749       if( aExpr[k].idxLeft==iCur ){
750         Expr *pX = aExpr[k].p;
751         if( pX->op!=TK_IN ){
752           sqlite3ExprCode(pParse, aExpr[k].p->pRight);
753         }else{
754           sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
755           sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
756           pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
757           pLevel->inOp = OP_Next;
758           pLevel->inP1 = pX->iTable;
759         }
760       }else{
761         sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
762       }
763       aExpr[k].p = 0;
764       cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
765       sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
766       haveKey = 0;
767       sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
768       pLevel->op = OP_Noop;
769     }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
770       /* Case 2:  There is an index and all terms of the WHERE clause that
771       **          refer to the index use the "==" or "IN" operators.
772       */
773       int start;
774       int nColumn = (pLevel->score+4)/8;
775       brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
776 
777       /* For each column of the index, find the term of the WHERE clause that
778       ** constraints that column.  If the WHERE clause term is X=expr, then
779       ** evaluation expr and leave the result on the stack */
780       for(j=0; j<nColumn; j++){
781         for(k=0; k<nExpr; k++){
782           Expr *pX = aExpr[k].p;
783           if( pX==0 ) continue;
784           if( aExpr[k].idxLeft==iCur
785              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
786              && pX->pLeft->iColumn==pIdx->aiColumn[j]
787           ){
788             char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
789             if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
790               if( pX->op==TK_EQ ){
791                 sqlite3ExprCode(pParse, pX->pRight);
792                 aExpr[k].p = 0;
793                 break;
794               }
795               if( pX->op==TK_IN && nColumn==1 ){
796                 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
797                 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
798                 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
799                 pLevel->inOp = OP_Next;
800                 pLevel->inP1 = pX->iTable;
801                 aExpr[k].p = 0;
802                 break;
803               }
804             }
805           }
806           if( aExpr[k].idxRight==iCur
807              && aExpr[k].p->op==TK_EQ
808              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
809              && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
810           ){
811             char idxaff = pIdx->pTable->aCol[pX->pRight->iColumn].affinity;
812             if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
813               sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
814               aExpr[k].p = 0;
815               break;
816             }
817           }
818         }
819       }
820       pLevel->iMem = pParse->nMem++;
821       cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
822 
823       /* At this point, the top nColumn elements of the stack are the
824       ** values of columns in the index we are using.  Check to see if
825       ** any of these values are NULL.  If they are, they will not match any
826       ** index entries, so skip immediately to the next iteration of the loop */
827       sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
828       sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
829       sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
830 
831       /* Generate an index key from the top nColumn elements of the stack */
832       sqlite3VdbeAddOp(v, OP_MakeKey, nColumn, 0);
833       sqlite3IndexAffinityStr(v, pIdx);
834       sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
835 
836       /* Generate code (1) to move to the first matching element of the table.
837       ** Then generate code (2) that jumps to "brk" after the cursor is past
838       ** the last matching element of the table.  The code (1) is executed
839       ** once to initialize the search, the code (2) is executed before each
840       ** iteration of the scan to see if the scan has finished. */
841       if( pLevel->bRev ){
842         /* Scan in reverse order */
843         sqlite3VdbeAddOp(v, OP_MoveLe, pLevel->iCur, brk);
844         start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
845         sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
846         pLevel->op = OP_Prev;
847       }else{
848         /* Scan in the forward order */
849         sqlite3VdbeAddOp(v, OP_MoveGe, pLevel->iCur, brk);
850         start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
851         sqlite3VdbeOp3(v, OP_IdxGE, pLevel->iCur, brk, "+", P3_STATIC);
852         pLevel->op = OP_Next;
853       }
854       sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
855       sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
856       sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
857       if( i==pTabList->nSrc-1 && pushKey ){
858         haveKey = 1;
859       }else{
860         sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
861         haveKey = 0;
862       }
863       pLevel->p1 = pLevel->iCur;
864       pLevel->p2 = start;
865     }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
866       /* Case 3:  We have an inequality comparison against the ROWID field.
867       */
868       int testOp = OP_Noop;
869       int start;
870 
871       brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
872       cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
873       if( iDirectGt[i]>=0 ){
874         k = iDirectGt[i];
875         assert( k<nExpr );
876         assert( aExpr[k].p!=0 );
877         assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
878         if( aExpr[k].idxLeft==iCur ){
879           sqlite3ExprCode(pParse, aExpr[k].p->pRight);
880         }else{
881           sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
882         }
883         sqlite3VdbeAddOp(v, OP_ForceInt,
884           aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
885         sqlite3VdbeAddOp(v, OP_MoveGe, iCur, brk);
886         aExpr[k].p = 0;
887       }else{
888         sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
889       }
890       if( iDirectLt[i]>=0 ){
891         k = iDirectLt[i];
892         assert( k<nExpr );
893         assert( aExpr[k].p!=0 );
894         assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
895         if( aExpr[k].idxLeft==iCur ){
896           sqlite3ExprCode(pParse, aExpr[k].p->pRight);
897         }else{
898           sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
899         }
900         /* sqlite3VdbeAddOp(v, OP_MustBeInt, 0, sqlite3VdbeCurrentAddr(v)+1); */
901         pLevel->iMem = pParse->nMem++;
902         sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
903         if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
904           testOp = OP_Ge;
905         }else{
906           testOp = OP_Gt;
907         }
908         aExpr[k].p = 0;
909       }
910       start = sqlite3VdbeCurrentAddr(v);
911       pLevel->op = OP_Next;
912       pLevel->p1 = iCur;
913       pLevel->p2 = start;
914       if( testOp!=OP_Noop ){
915         sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
916         sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
917         sqlite3VdbeAddOp(v, testOp, 0, brk);
918       }
919       haveKey = 0;
920     }else if( pIdx==0 ){
921       /* Case 4:  There is no usable index.  We must do a complete
922       **          scan of the entire database table.
923       */
924       int start;
925 
926       brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
927       cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
928       sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
929       start = sqlite3VdbeCurrentAddr(v);
930       pLevel->op = OP_Next;
931       pLevel->p1 = iCur;
932       pLevel->p2 = start;
933       haveKey = 0;
934     }else{
935       /* Case 5: The WHERE clause term that refers to the right-most
936       **         column of the index is an inequality.  For example, if
937       **         the index is on (x,y,z) and the WHERE clause is of the
938       **         form "x=5 AND y<10" then this case is used.  Only the
939       **         right-most column can be an inequality - the rest must
940       **         use the "==" operator.
941       **
942       **         This case is also used when there are no WHERE clause
943       **         constraints but an index is selected anyway, in order
944       **         to force the output order to conform to an ORDER BY.
945       */
946       int score = pLevel->score;
947       int nEqColumn = score/8;
948       int start;
949       int leFlag, geFlag;
950       int testOp;
951 
952       /* Evaluate the equality constraints
953       */
954       for(j=0; j<nEqColumn; j++){
955         for(k=0; k<nExpr; k++){
956           if( aExpr[k].p==0 ) continue;
957           if( aExpr[k].idxLeft==iCur
958              && aExpr[k].p->op==TK_EQ
959              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
960              && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
961           ){
962             sqlite3ExprCode(pParse, aExpr[k].p->pRight);
963             aExpr[k].p = 0;
964             break;
965           }
966           if( aExpr[k].idxRight==iCur
967              && aExpr[k].p->op==TK_EQ
968              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
969              && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
970           ){
971             sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
972             aExpr[k].p = 0;
973             break;
974           }
975         }
976       }
977 
978       /* Duplicate the equality term values because they will all be
979       ** used twice: once to make the termination key and once to make the
980       ** start key.
981       */
982       for(j=0; j<nEqColumn; j++){
983         sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
984       }
985 
986       /* Labels for the beginning and end of the loop
987       */
988       cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
989       brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
990 
991       /* Generate the termination key.  This is the key value that
992       ** will end the search.  There is no termination key if there
993       ** are no equality terms and no "X<..." term.
994       **
995       ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
996       ** key computed here really ends up being the start key.
997       */
998       if( (score & 1)!=0 ){
999         for(k=0; k<nExpr; k++){
1000           Expr *pExpr = aExpr[k].p;
1001           if( pExpr==0 ) continue;
1002           if( aExpr[k].idxLeft==iCur
1003              && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1004              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1005              && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1006           ){
1007             sqlite3ExprCode(pParse, pExpr->pRight);
1008             leFlag = pExpr->op==TK_LE;
1009             aExpr[k].p = 0;
1010             break;
1011           }
1012           if( aExpr[k].idxRight==iCur
1013              && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1014              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1015              && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1016           ){
1017             sqlite3ExprCode(pParse, pExpr->pLeft);
1018             leFlag = pExpr->op==TK_GE;
1019             aExpr[k].p = 0;
1020             break;
1021           }
1022         }
1023         testOp = OP_IdxGE;
1024       }else{
1025         testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1026         leFlag = 1;
1027       }
1028       if( testOp!=OP_Noop ){
1029         int nCol = nEqColumn + (score & 1);
1030         pLevel->iMem = pParse->nMem++;
1031         sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1032         sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1033         sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1034         sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
1035         sqlite3IndexAffinityStr(v, pIdx);
1036         if( pLevel->bRev ){
1037           int op = leFlag ? OP_MoveLe : OP_MoveLt;
1038           sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
1039         }else{
1040           sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1041         }
1042       }else if( pLevel->bRev ){
1043         sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
1044       }
1045 
1046       /* Generate the start key.  This is the key that defines the lower
1047       ** bound on the search.  There is no start key if there are no
1048       ** equality terms and if there is no "X>..." term.  In
1049       ** that case, generate a "Rewind" instruction in place of the
1050       ** start key search.
1051       **
1052       ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1053       ** "start" key really ends up being used as the termination key.
1054       */
1055       if( (score & 2)!=0 ){
1056         for(k=0; k<nExpr; k++){
1057           Expr *pExpr = aExpr[k].p;
1058           if( pExpr==0 ) continue;
1059           if( aExpr[k].idxLeft==iCur
1060              && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1061              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1062              && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1063           ){
1064             sqlite3ExprCode(pParse, pExpr->pRight);
1065             geFlag = pExpr->op==TK_GE;
1066             aExpr[k].p = 0;
1067             break;
1068           }
1069           if( aExpr[k].idxRight==iCur
1070              && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1071              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1072              && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1073           ){
1074             sqlite3ExprCode(pParse, pExpr->pLeft);
1075             geFlag = pExpr->op==TK_LE;
1076             aExpr[k].p = 0;
1077             break;
1078           }
1079         }
1080       }else{
1081         geFlag = 1;
1082       }
1083       if( nEqColumn>0 || (score&2)!=0 ){
1084         int nCol = nEqColumn + ((score&2)!=0);
1085         sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1086         sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1087         sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1088         sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
1089         sqlite3IndexAffinityStr(v, pIdx);
1090         if( pLevel->bRev ){
1091           pLevel->iMem = pParse->nMem++;
1092           sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1093           testOp = OP_IdxLT;
1094         }else{
1095           int op = geFlag ? OP_MoveGe : OP_MoveGt;
1096           sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
1097         }
1098       }else if( pLevel->bRev ){
1099         testOp = OP_Noop;
1100       }else{
1101         sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
1102       }
1103 
1104       /* Generate the the top of the loop.  If there is a termination
1105       ** key we have to test for that key and abort at the top of the
1106       ** loop.
1107       */
1108       start = sqlite3VdbeCurrentAddr(v);
1109       if( testOp!=OP_Noop ){
1110         sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1111         sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
1112         if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1113           sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1114         }
1115       }
1116       sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1117       sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1118       sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
1119       if( i==pTabList->nSrc-1 && pushKey ){
1120         haveKey = 1;
1121       }else{
1122         sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
1123         haveKey = 0;
1124       }
1125 
1126       /* Record the instruction used to terminate the loop.
1127       */
1128       pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
1129       pLevel->p1 = pLevel->iCur;
1130       pLevel->p2 = start;
1131     }
1132     loopMask |= getMask(&maskSet, iCur);
1133 
1134     /* Insert code to test every subexpression that can be completely
1135     ** computed using the current set of tables.
1136     */
1137     for(j=0; j<nExpr; j++){
1138       if( aExpr[j].p==0 ) continue;
1139       if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1140       if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1141         continue;
1142       }
1143       if( haveKey ){
1144         haveKey = 0;
1145         sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
1146       }
1147       sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
1148       aExpr[j].p = 0;
1149     }
1150     brk = cont;
1151 
1152     /* For a LEFT OUTER JOIN, generate code that will record the fact that
1153     ** at least one row of the right table has matched the left table.
1154     */
1155     if( pLevel->iLeftJoin ){
1156       pLevel->top = sqlite3VdbeCurrentAddr(v);
1157       sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1158       sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
1159       for(j=0; j<nExpr; j++){
1160         if( aExpr[j].p==0 ) continue;
1161         if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1162         if( haveKey ){
1163           /* Cannot happen.  "haveKey" can only be true if pushKey is true
1164           ** an pushKey can only be true for DELETE and UPDATE and there are
1165           ** no outer joins with DELETE and UPDATE.
1166           */
1167           haveKey = 0;
1168           sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
1169         }
1170         sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
1171         aExpr[j].p = 0;
1172       }
1173     }
1174   }
1175   pWInfo->iContinue = cont;
1176   if( pushKey && !haveKey ){
1177     sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
1178   }
1179   freeMaskSet(&maskSet);
1180   return pWInfo;
1181 }
1182 
1183 /*
1184 ** Generate the end of the WHERE loop.  See comments on
1185 ** sqlite3WhereBegin() for additional information.
1186 */
1187 void sqlite3WhereEnd(WhereInfo *pWInfo){
1188   Vdbe *v = pWInfo->pParse->pVdbe;
1189   int i;
1190   WhereLevel *pLevel;
1191   SrcList *pTabList = pWInfo->pTabList;
1192 
1193   for(i=pTabList->nSrc-1; i>=0; i--){
1194     pLevel = &pWInfo->a[i];
1195     sqlite3VdbeResolveLabel(v, pLevel->cont);
1196     if( pLevel->op!=OP_Noop ){
1197       sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
1198     }
1199     sqlite3VdbeResolveLabel(v, pLevel->brk);
1200     if( pLevel->inOp!=OP_Noop ){
1201       sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
1202     }
1203     if( pLevel->iLeftJoin ){
1204       int addr;
1205       addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1206       sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1207       sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
1208       if( pLevel->iCur>=0 ){
1209         sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
1210       }
1211       sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
1212     }
1213   }
1214   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
1215   for(i=0; i<pTabList->nSrc; i++){
1216     Table *pTab = pTabList->a[i].pTab;
1217     assert( pTab!=0 );
1218     if( pTab->isTransient || pTab->pSelect ) continue;
1219     pLevel = &pWInfo->a[i];
1220     sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
1221     if( pLevel->pIdx!=0 ){
1222       sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
1223     }
1224   }
1225 #if 0  /* Never reuse a cursor */
1226   if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1227     pWInfo->pParse->nTab = pWInfo->savedNTab;
1228   }
1229 #endif
1230   sqliteFree(pWInfo);
1231   return;
1232 }
1233