xref: /illumos-gate/usr/src/lib/libsqlite/src/where.c (revision c5c4113d)
27c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
47c478bd9Sstevel@tonic-gate /*
57c478bd9Sstevel@tonic-gate ** 2001 September 15
67c478bd9Sstevel@tonic-gate **
77c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code.  In place of
87c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
97c478bd9Sstevel@tonic-gate **
107c478bd9Sstevel@tonic-gate **    May you do good and not evil.
117c478bd9Sstevel@tonic-gate **    May you find forgiveness for yourself and forgive others.
127c478bd9Sstevel@tonic-gate **    May you share freely, never taking more than you give.
137c478bd9Sstevel@tonic-gate **
147c478bd9Sstevel@tonic-gate *************************************************************************
157c478bd9Sstevel@tonic-gate ** This module contains C code that generates VDBE code used to process
167c478bd9Sstevel@tonic-gate ** the WHERE clause of SQL statements.
177c478bd9Sstevel@tonic-gate **
187c478bd9Sstevel@tonic-gate ** $Id: where.c,v 2004/07/19 19:30:50 drh Exp $
197c478bd9Sstevel@tonic-gate */
207c478bd9Sstevel@tonic-gate #include "sqliteInt.h"
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate ** The query generator uses an array of instances of this structure to
247c478bd9Sstevel@tonic-gate ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
257c478bd9Sstevel@tonic-gate ** clause subexpression is separated from the others by an AND operator.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate typedef struct ExprInfo ExprInfo;
287c478bd9Sstevel@tonic-gate struct ExprInfo {
297c478bd9Sstevel@tonic-gate   Expr *p;                /* Pointer to the subexpression */
307c478bd9Sstevel@tonic-gate   u8 indexable;           /* True if this subexprssion is usable by an index */
317c478bd9Sstevel@tonic-gate   short int idxLeft;      /* p->pLeft is a column in this table number. -1 if
327c478bd9Sstevel@tonic-gate                           ** p->pLeft is not the column of any table */
337c478bd9Sstevel@tonic-gate   short int idxRight;     /* p->pRight is a column in this table number. -1 if
347c478bd9Sstevel@tonic-gate                           ** p->pRight is not the column of any table */
357c478bd9Sstevel@tonic-gate   unsigned prereqLeft;    /* Bitmask of tables referenced by p->pLeft */
367c478bd9Sstevel@tonic-gate   unsigned prereqRight;   /* Bitmask of tables referenced by p->pRight */
377c478bd9Sstevel@tonic-gate   unsigned prereqAll;     /* Bitmask of tables referenced by p */
387c478bd9Sstevel@tonic-gate };
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate ** An instance of the following structure keeps track of a mapping
427c478bd9Sstevel@tonic-gate ** between VDBE cursor numbers and bitmasks.  The VDBE cursor numbers
437c478bd9Sstevel@tonic-gate ** are small integers contained in SrcList_item.iCursor and Expr.iTable
447c478bd9Sstevel@tonic-gate ** fields.  For any given WHERE clause, we want to track which cursors
457c478bd9Sstevel@tonic-gate ** are being used, so we assign a single bit in a 32-bit word to track
467c478bd9Sstevel@tonic-gate ** that cursor.  Then a 32-bit integer is able to show the set of all
477c478bd9Sstevel@tonic-gate ** cursors being used.
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate typedef struct ExprMaskSet ExprMaskSet;
507c478bd9Sstevel@tonic-gate struct ExprMaskSet {
517c478bd9Sstevel@tonic-gate   int n;          /* Number of assigned cursor values */
527c478bd9Sstevel@tonic-gate   int ix[31];     /* Cursor assigned to each bit */
537c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate ** Determine the number of elements in an array.
577c478bd9Sstevel@tonic-gate */
587c478bd9Sstevel@tonic-gate #define ARRAYSIZE(X)  (sizeof(X)/sizeof(X[0]))
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate ** This routine is used to divide the WHERE expression into subexpressions
627c478bd9Sstevel@tonic-gate ** separated by the AND operator.
637c478bd9Sstevel@tonic-gate **
647c478bd9Sstevel@tonic-gate ** aSlot[] is an array of subexpressions structures.
657c478bd9Sstevel@tonic-gate ** There are nSlot spaces left in this array.  This routine attempts to
667c478bd9Sstevel@tonic-gate ** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
677c478bd9Sstevel@tonic-gate ** The return value is the number of slots filled.
687c478bd9Sstevel@tonic-gate */
exprSplit(int nSlot,ExprInfo * aSlot,Expr * pExpr)697c478bd9Sstevel@tonic-gate static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
707c478bd9Sstevel@tonic-gate   int cnt = 0;
717c478bd9Sstevel@tonic-gate   if( pExpr==0 || nSlot<1 ) return 0;
727c478bd9Sstevel@tonic-gate   if( nSlot==1 || pExpr->op!=TK_AND ){
737c478bd9Sstevel@tonic-gate     aSlot[0].p = pExpr;
747c478bd9Sstevel@tonic-gate     return 1;
757c478bd9Sstevel@tonic-gate   }
767c478bd9Sstevel@tonic-gate   if( pExpr->pLeft->op!=TK_AND ){
777c478bd9Sstevel@tonic-gate     aSlot[0].p = pExpr->pLeft;
787c478bd9Sstevel@tonic-gate     cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
797c478bd9Sstevel@tonic-gate   }else{
807c478bd9Sstevel@tonic-gate     cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
817c478bd9Sstevel@tonic-gate     cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
827c478bd9Sstevel@tonic-gate   }
837c478bd9Sstevel@tonic-gate   return cnt;
847c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate ** Initialize an expression mask set
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate #define initMaskSet(P)  memset(P, 0, sizeof(*P))
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate ** Return the bitmask for the given cursor.  Assign a new bitmask
937c478bd9Sstevel@tonic-gate ** if this is the first time the cursor has been seen.
947c478bd9Sstevel@tonic-gate */
getMask(ExprMaskSet * pMaskSet,int iCursor)957c478bd9Sstevel@tonic-gate static int getMask(ExprMaskSet *pMaskSet, int iCursor){
967c478bd9Sstevel@tonic-gate   int i;
977c478bd9Sstevel@tonic-gate   for(i=0; i<pMaskSet->n; i++){
987c478bd9Sstevel@tonic-gate     if( pMaskSet->ix[i]==iCursor ) return 1<<i;
997c478bd9Sstevel@tonic-gate   }
1007c478bd9Sstevel@tonic-gate   if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
1017c478bd9Sstevel@tonic-gate     pMaskSet->n++;
1027c478bd9Sstevel@tonic-gate     pMaskSet->ix[i] = iCursor;
1037c478bd9Sstevel@tonic-gate     return 1<<i;
1047c478bd9Sstevel@tonic-gate   }
1057c478bd9Sstevel@tonic-gate   return 0;
1067c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate ** Destroy an expression mask set
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate #define freeMaskSet(P)   /* NO-OP */
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate ** This routine walks (recursively) an expression tree and generates
1157c478bd9Sstevel@tonic-gate ** a bitmask indicating which tables are used in that expression
1167c478bd9Sstevel@tonic-gate ** tree.
1177c478bd9Sstevel@tonic-gate **
1187c478bd9Sstevel@tonic-gate ** In order for this routine to work, the calling function must have
1197c478bd9Sstevel@tonic-gate ** previously invoked sqliteExprResolveIds() on the expression.  See
1207c478bd9Sstevel@tonic-gate ** the header comment on that routine for additional information.
1217c478bd9Sstevel@tonic-gate ** The sqliteExprResolveIds() routines looks for column names and
1227c478bd9Sstevel@tonic-gate ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
1237c478bd9Sstevel@tonic-gate ** the VDBE cursor number of the table.
1247c478bd9Sstevel@tonic-gate */
exprTableUsage(ExprMaskSet * pMaskSet,Expr * p)1257c478bd9Sstevel@tonic-gate static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
1267c478bd9Sstevel@tonic-gate   unsigned int mask = 0;
1277c478bd9Sstevel@tonic-gate   if( p==0 ) return 0;
1287c478bd9Sstevel@tonic-gate   if( p->op==TK_COLUMN ){
1297c478bd9Sstevel@tonic-gate     mask = getMask(pMaskSet, p->iTable);
1307c478bd9Sstevel@tonic-gate     if( mask==0 ) mask = -1;
1317c478bd9Sstevel@tonic-gate     return mask;
1327c478bd9Sstevel@tonic-gate   }
1337c478bd9Sstevel@tonic-gate   if( p->pRight ){
1347c478bd9Sstevel@tonic-gate     mask = exprTableUsage(pMaskSet, p->pRight);
1357c478bd9Sstevel@tonic-gate   }
1367c478bd9Sstevel@tonic-gate   if( p->pLeft ){
1377c478bd9Sstevel@tonic-gate     mask |= exprTableUsage(pMaskSet, p->pLeft);
1387c478bd9Sstevel@tonic-gate   }
1397c478bd9Sstevel@tonic-gate   if( p->pList ){
1407c478bd9Sstevel@tonic-gate     int i;
1417c478bd9Sstevel@tonic-gate     for(i=0; i<p->pList->nExpr; i++){
1427c478bd9Sstevel@tonic-gate       mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
1437c478bd9Sstevel@tonic-gate     }
1447c478bd9Sstevel@tonic-gate   }
1457c478bd9Sstevel@tonic-gate   return mask;
1467c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate ** Return TRUE if the given operator is one of the operators that is
1507c478bd9Sstevel@tonic-gate ** allowed for an indexable WHERE clause.  The allowed operators are
1517c478bd9Sstevel@tonic-gate ** "=", "<", ">", "<=", ">=", and "IN".
1527c478bd9Sstevel@tonic-gate */
allowedOp(int op)1537c478bd9Sstevel@tonic-gate static int allowedOp(int op){
1547c478bd9Sstevel@tonic-gate   switch( op ){
1557c478bd9Sstevel@tonic-gate     case TK_LT:
1567c478bd9Sstevel@tonic-gate     case TK_LE:
1577c478bd9Sstevel@tonic-gate     case TK_GT:
1587c478bd9Sstevel@tonic-gate     case TK_GE:
1597c478bd9Sstevel@tonic-gate     case TK_EQ:
1607c478bd9Sstevel@tonic-gate     case TK_IN:
1617c478bd9Sstevel@tonic-gate       return 1;
1627c478bd9Sstevel@tonic-gate     default:
1637c478bd9Sstevel@tonic-gate       return 0;
1647c478bd9Sstevel@tonic-gate   }
1657c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate ** The input to this routine is an ExprInfo structure with only the
1697c478bd9Sstevel@tonic-gate ** "p" field filled in.  The job of this routine is to analyze the
1707c478bd9Sstevel@tonic-gate ** subexpression and populate all the other fields of the ExprInfo
1717c478bd9Sstevel@tonic-gate ** structure.
1727c478bd9Sstevel@tonic-gate */
exprAnalyze(ExprMaskSet * pMaskSet,ExprInfo * pInfo)1737c478bd9Sstevel@tonic-gate static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
1747c478bd9Sstevel@tonic-gate   Expr *pExpr = pInfo->p;
1757c478bd9Sstevel@tonic-gate   pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
1767c478bd9Sstevel@tonic-gate   pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
1777c478bd9Sstevel@tonic-gate   pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
1787c478bd9Sstevel@tonic-gate   pInfo->indexable = 0;
1797c478bd9Sstevel@tonic-gate   pInfo->idxLeft = -1;
1807c478bd9Sstevel@tonic-gate   pInfo->idxRight = -1;
1817c478bd9Sstevel@tonic-gate   if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
1827c478bd9Sstevel@tonic-gate     if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
1837c478bd9Sstevel@tonic-gate       pInfo->idxRight = pExpr->pRight->iTable;
1847c478bd9Sstevel@tonic-gate       pInfo->indexable = 1;
1857c478bd9Sstevel@tonic-gate     }
1867c478bd9Sstevel@tonic-gate     if( pExpr->pLeft->op==TK_COLUMN ){
1877c478bd9Sstevel@tonic-gate       pInfo->idxLeft = pExpr->pLeft->iTable;
1887c478bd9Sstevel@tonic-gate       pInfo->indexable = 1;
1897c478bd9Sstevel@tonic-gate     }
1907c478bd9Sstevel@tonic-gate   }
1917c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate ** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
1957c478bd9Sstevel@tonic-gate ** left-most table in the FROM clause of that same SELECT statement and
1967c478bd9Sstevel@tonic-gate ** the table has a cursor number of "base".
1977c478bd9Sstevel@tonic-gate **
1987c478bd9Sstevel@tonic-gate ** This routine attempts to find an index for pTab that generates the
1997c478bd9Sstevel@tonic-gate ** correct record sequence for the given ORDER BY clause.  The return value
2007c478bd9Sstevel@tonic-gate ** is a pointer to an index that does the job.  NULL is returned if the
2017c478bd9Sstevel@tonic-gate ** table has no index that will generate the correct sort order.
2027c478bd9Sstevel@tonic-gate **
2037c478bd9Sstevel@tonic-gate ** If there are two or more indices that generate the correct sort order
2047c478bd9Sstevel@tonic-gate ** and pPreferredIdx is one of those indices, then return pPreferredIdx.
2057c478bd9Sstevel@tonic-gate **
2067c478bd9Sstevel@tonic-gate ** nEqCol is the number of columns of pPreferredIdx that are used as
2077c478bd9Sstevel@tonic-gate ** equality constraints.  Any index returned must have exactly this same
2087c478bd9Sstevel@tonic-gate ** set of columns.  The ORDER BY clause only matches index columns beyond the
2097c478bd9Sstevel@tonic-gate ** the first nEqCol columns.
2107c478bd9Sstevel@tonic-gate **
2117c478bd9Sstevel@tonic-gate ** All terms of the ORDER BY clause must be either ASC or DESC.  The
2127c478bd9Sstevel@tonic-gate ** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
2137c478bd9Sstevel@tonic-gate ** set to 0 if the ORDER BY clause is all ASC.
2147c478bd9Sstevel@tonic-gate */
findSortingIndex(Table * pTab,int base,ExprList * pOrderBy,Index * pPreferredIdx,int nEqCol,int * pbRev)2157c478bd9Sstevel@tonic-gate static Index *findSortingIndex(
2167c478bd9Sstevel@tonic-gate   Table *pTab,            /* The table to be sorted */
2177c478bd9Sstevel@tonic-gate   int base,               /* Cursor number for pTab */
2187c478bd9Sstevel@tonic-gate   ExprList *pOrderBy,     /* The ORDER BY clause */
2197c478bd9Sstevel@tonic-gate   Index *pPreferredIdx,   /* Use this index, if possible and not NULL */
2207c478bd9Sstevel@tonic-gate   int nEqCol,             /* Number of index columns used with == constraints */
2217c478bd9Sstevel@tonic-gate   int *pbRev              /* Set to 1 if ORDER BY is DESC */
2227c478bd9Sstevel@tonic-gate ){
2237c478bd9Sstevel@tonic-gate   int i, j;
2247c478bd9Sstevel@tonic-gate   Index *pMatch;
2257c478bd9Sstevel@tonic-gate   Index *pIdx;
2267c478bd9Sstevel@tonic-gate   int sortOrder;
2287c478bd9Sstevel@tonic-gate   assert( pOrderBy!=0 );
2297c478bd9Sstevel@tonic-gate   assert( pOrderBy->nExpr>0 );
2307c478bd9Sstevel@tonic-gate   sortOrder = pOrderBy->a[0].sortOrder & SQLITE_SO_DIRMASK;
2317c478bd9Sstevel@tonic-gate   for(i=0; i<pOrderBy->nExpr; i++){
2327c478bd9Sstevel@tonic-gate     Expr *p;
2337c478bd9Sstevel@tonic-gate     if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){
2347c478bd9Sstevel@tonic-gate       /* Indices can only be used if all ORDER BY terms are either
2357c478bd9Sstevel@tonic-gate       ** DESC or ASC.  Indices cannot be used on a mixture. */
2367c478bd9Sstevel@tonic-gate       return 0;
2377c478bd9Sstevel@tonic-gate     }
2387c478bd9Sstevel@tonic-gate     if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){
2397c478bd9Sstevel@tonic-gate       /* Do not sort by index if there is a COLLATE clause */
2407c478bd9Sstevel@tonic-gate       return 0;
2417c478bd9Sstevel@tonic-gate     }
2427c478bd9Sstevel@tonic-gate     p = pOrderBy->a[i].pExpr;
2437c478bd9Sstevel@tonic-gate     if( p->op!=TK_COLUMN || p->iTable!=base ){
2447c478bd9Sstevel@tonic-gate       /* Can not use an index sort on anything that is not a column in the
2457c478bd9Sstevel@tonic-gate       ** left-most table of the FROM clause */
2467c478bd9Sstevel@tonic-gate       return 0;
2477c478bd9Sstevel@tonic-gate     }
2487c478bd9Sstevel@tonic-gate   }
2507c478bd9Sstevel@tonic-gate   /* If we get this far, it means the ORDER BY clause consists only of
2517c478bd9Sstevel@tonic-gate   ** ascending columns in the left-most table of the FROM clause.  Now
2527c478bd9Sstevel@tonic-gate   ** check for a matching index.
2537c478bd9Sstevel@tonic-gate   */
2547c478bd9Sstevel@tonic-gate   pMatch = 0;
2557c478bd9Sstevel@tonic-gate   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2567c478bd9Sstevel@tonic-gate     int nExpr = pOrderBy->nExpr;
2577c478bd9Sstevel@tonic-gate     if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
2587c478bd9Sstevel@tonic-gate     for(i=j=0; i<nEqCol; i++){
2597c478bd9Sstevel@tonic-gate       if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
2607c478bd9Sstevel@tonic-gate       if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; }
2617c478bd9Sstevel@tonic-gate     }
2627c478bd9Sstevel@tonic-gate     if( i<nEqCol ) continue;
2637c478bd9Sstevel@tonic-gate     for(i=0; i+j<nExpr; i++){
2647c478bd9Sstevel@tonic-gate       if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break;
2657c478bd9Sstevel@tonic-gate     }
2667c478bd9Sstevel@tonic-gate     if( i+j>=nExpr ){
2677c478bd9Sstevel@tonic-gate       pMatch = pIdx;
2687c478bd9Sstevel@tonic-gate       if( pIdx==pPreferredIdx ) break;
2697c478bd9Sstevel@tonic-gate     }
2707c478bd9Sstevel@tonic-gate   }
2717c478bd9Sstevel@tonic-gate   if( pMatch && pbRev ){
2727c478bd9Sstevel@tonic-gate     *pbRev = sortOrder==SQLITE_SO_DESC;
2737c478bd9Sstevel@tonic-gate   }
2747c478bd9Sstevel@tonic-gate   return pMatch;
2757c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate ** Disable a term in the WHERE clause.  Except, do not disable the term
2797c478bd9Sstevel@tonic-gate ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
2807c478bd9Sstevel@tonic-gate ** or USING clause of that join.
2817c478bd9Sstevel@tonic-gate **
2827c478bd9Sstevel@tonic-gate ** Consider the term t2.z='ok' in the following queries:
2837c478bd9Sstevel@tonic-gate **
2847c478bd9Sstevel@tonic-gate **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
2857c478bd9Sstevel@tonic-gate **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
2867c478bd9Sstevel@tonic-gate **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
2877c478bd9Sstevel@tonic-gate **
2887c478bd9Sstevel@tonic-gate ** The t2.z='ok' is disabled in the in (2) because it did not originate
2897c478bd9Sstevel@tonic-gate ** in the ON clause.  The term is disabled in (3) because it is not part
2907c478bd9Sstevel@tonic-gate ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
2917c478bd9Sstevel@tonic-gate **
2927c478bd9Sstevel@tonic-gate ** Disabling a term causes that term to not be tested in the inner loop
2937c478bd9Sstevel@tonic-gate ** of the join.  Disabling is an optimization.  We would get the correct
2947c478bd9Sstevel@tonic-gate ** results if nothing were ever disabled, but joins might run a little
2957c478bd9Sstevel@tonic-gate ** slower.  The trick is to disable as much as we can without disabling
2967c478bd9Sstevel@tonic-gate ** too much.  If we disabled in (1), we'd get the wrong answer.
2977c478bd9Sstevel@tonic-gate ** See ticket #813.
2987c478bd9Sstevel@tonic-gate */
disableTerm(WhereLevel * pLevel,Expr ** ppExpr)2997c478bd9Sstevel@tonic-gate static void disableTerm(WhereLevel *pLevel, Expr **ppExpr){
3007c478bd9Sstevel@tonic-gate   Expr *pExpr = *ppExpr;
3017c478bd9Sstevel@tonic-gate   if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){
3027c478bd9Sstevel@tonic-gate     *ppExpr = 0;
3037c478bd9Sstevel@tonic-gate   }
3047c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate ** Generate the beginning of the loop used for WHERE clause processing.
3087c478bd9Sstevel@tonic-gate ** The return value is a pointer to an (opaque) structure that contains
3097c478bd9Sstevel@tonic-gate ** information needed to terminate the loop.  Later, the calling routine
3107c478bd9Sstevel@tonic-gate ** should invoke sqliteWhereEnd() with the return value of this function
3117c478bd9Sstevel@tonic-gate ** in order to complete the WHERE clause processing.
3127c478bd9Sstevel@tonic-gate **
3137c478bd9Sstevel@tonic-gate ** If an error occurs, this routine returns NULL.
3147c478bd9Sstevel@tonic-gate **
3157c478bd9Sstevel@tonic-gate ** The basic idea is to do a nested loop, one loop for each table in
3167c478bd9Sstevel@tonic-gate ** the FROM clause of a select.  (INSERT and UPDATE statements are the
3177c478bd9Sstevel@tonic-gate ** same as a SELECT with only a single table in the FROM clause.)  For
3187c478bd9Sstevel@tonic-gate ** example, if the SQL is this:
3197c478bd9Sstevel@tonic-gate **
3207c478bd9Sstevel@tonic-gate **       SELECT * FROM t1, t2, t3 WHERE ...;
3217c478bd9Sstevel@tonic-gate **
3227c478bd9Sstevel@tonic-gate ** Then the code generated is conceptually like the following:
3237c478bd9Sstevel@tonic-gate **
3247c478bd9Sstevel@tonic-gate **      foreach row1 in t1 do       \    Code generated
3257c478bd9Sstevel@tonic-gate **        foreach row2 in t2 do      |-- by sqliteWhereBegin()
3267c478bd9Sstevel@tonic-gate **          foreach row3 in t3 do   /
3277c478bd9Sstevel@tonic-gate **            ...
3287c478bd9Sstevel@tonic-gate **          end                     \    Code generated
3297c478bd9Sstevel@tonic-gate **        end                        |-- by sqliteWhereEnd()
3307c478bd9Sstevel@tonic-gate **      end                         /
3317c478bd9Sstevel@tonic-gate **
3327c478bd9Sstevel@tonic-gate ** There are Btree cursors associated with each table.  t1 uses cursor
3337c478bd9Sstevel@tonic-gate ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
3347c478bd9Sstevel@tonic-gate ** And so forth.  This routine generates code to open those VDBE cursors
3357c478bd9Sstevel@tonic-gate ** and sqliteWhereEnd() generates the code to close them.
3367c478bd9Sstevel@tonic-gate **
3377c478bd9Sstevel@tonic-gate ** If the WHERE clause is empty, the foreach loops must each scan their
3387c478bd9Sstevel@tonic-gate ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
3397c478bd9Sstevel@tonic-gate ** the tables have indices and there are terms in the WHERE clause that
3407c478bd9Sstevel@tonic-gate ** refer to those indices, a complete table scan can be avoided and the
3417c478bd9Sstevel@tonic-gate ** code will run much faster.  Most of the work of this routine is checking
3427c478bd9Sstevel@tonic-gate ** to see if there are indices that can be used to speed up the loop.
3437c478bd9Sstevel@tonic-gate **
3447c478bd9Sstevel@tonic-gate ** Terms of the WHERE clause are also used to limit which rows actually
3457c478bd9Sstevel@tonic-gate ** make it to the "..." in the middle of the loop.  After each "foreach",
3467c478bd9Sstevel@tonic-gate ** terms of the WHERE clause that use only terms in that loop and outer
3477c478bd9Sstevel@tonic-gate ** loops are evaluated and if false a jump is made around all subsequent
3487c478bd9Sstevel@tonic-gate ** inner loops (or around the "..." if the test occurs within the inner-
3497c478bd9Sstevel@tonic-gate ** most loop)
3507c478bd9Sstevel@tonic-gate **
3517c478bd9Sstevel@tonic-gate ** OUTER JOINS
3527c478bd9Sstevel@tonic-gate **
3537c478bd9Sstevel@tonic-gate ** An outer join of tables t1 and t2 is conceptally coded as follows:
3547c478bd9Sstevel@tonic-gate **
3557c478bd9Sstevel@tonic-gate **    foreach row1 in t1 do
3567c478bd9Sstevel@tonic-gate **      flag = 0
3577c478bd9Sstevel@tonic-gate **      foreach row2 in t2 do
3587c478bd9Sstevel@tonic-gate **        start:
3597c478bd9Sstevel@tonic-gate **          ...
3607c478bd9Sstevel@tonic-gate **          flag = 1
3617c478bd9Sstevel@tonic-gate **      end
3627c478bd9Sstevel@tonic-gate **      if flag==0 then
3637c478bd9Sstevel@tonic-gate **        move the row2 cursor to a null row
3647c478bd9Sstevel@tonic-gate **        goto start
3657c478bd9Sstevel@tonic-gate **      fi
3667c478bd9Sstevel@tonic-gate **    end
3677c478bd9Sstevel@tonic-gate **
3687c478bd9Sstevel@tonic-gate ** ORDER BY CLAUSE PROCESSING
3697c478bd9Sstevel@tonic-gate **
3707c478bd9Sstevel@tonic-gate ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
3717c478bd9Sstevel@tonic-gate ** if there is one.  If there is no ORDER BY clause or if this routine
3727c478bd9Sstevel@tonic-gate ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
3737c478bd9Sstevel@tonic-gate **
3747c478bd9Sstevel@tonic-gate ** If an index can be used so that the natural output order of the table
3757c478bd9Sstevel@tonic-gate ** scan is correct for the ORDER BY clause, then that index is used and
3767c478bd9Sstevel@tonic-gate ** *ppOrderBy is set to NULL.  This is an optimization that prevents an
3777c478bd9Sstevel@tonic-gate ** unnecessary sort of the result set if an index appropriate for the
3787c478bd9Sstevel@tonic-gate ** ORDER BY clause already exists.
3797c478bd9Sstevel@tonic-gate **
3807c478bd9Sstevel@tonic-gate ** If the where clause loops cannot be arranged to provide the correct
3817c478bd9Sstevel@tonic-gate ** output order, then the *ppOrderBy is unchanged.
3827c478bd9Sstevel@tonic-gate */
sqliteWhereBegin(Parse * pParse,SrcList * pTabList,Expr * pWhere,int pushKey,ExprList ** ppOrderBy)3837c478bd9Sstevel@tonic-gate WhereInfo *sqliteWhereBegin(
3847c478bd9Sstevel@tonic-gate   Parse *pParse,       /* The parser context */
3857c478bd9Sstevel@tonic-gate   SrcList *pTabList,   /* A list of all tables to be scanned */
3867c478bd9Sstevel@tonic-gate   Expr *pWhere,        /* The WHERE clause */
3877c478bd9Sstevel@tonic-gate   int pushKey,         /* If TRUE, leave the table key on the stack */
3887c478bd9Sstevel@tonic-gate   ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
3897c478bd9Sstevel@tonic-gate ){
3907c478bd9Sstevel@tonic-gate   int i;                     /* Loop counter */
3917c478bd9Sstevel@tonic-gate   WhereInfo *pWInfo;         /* Will become the return value of this function */
3927c478bd9Sstevel@tonic-gate   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
3937c478bd9Sstevel@tonic-gate   int brk, cont = 0;         /* Addresses used during code generation */
3947c478bd9Sstevel@tonic-gate   int nExpr;           /* Number of subexpressions in the WHERE clause */
3957c478bd9Sstevel@tonic-gate   int loopMask;        /* One bit set for each outer loop */
3967c478bd9Sstevel@tonic-gate   int haveKey;         /* True if KEY is on the stack */
3977c478bd9Sstevel@tonic-gate   ExprMaskSet maskSet; /* The expression mask set */
3987c478bd9Sstevel@tonic-gate   int iDirectEq[32];   /* Term of the form ROWID==X for the N-th table */
3997c478bd9Sstevel@tonic-gate   int iDirectLt[32];   /* Term of the form ROWID<X or ROWID<=X */
4007c478bd9Sstevel@tonic-gate   int iDirectGt[32];   /* Term of the form ROWID>X or ROWID>=X */
4017c478bd9Sstevel@tonic-gate   ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
4037c478bd9Sstevel@tonic-gate   /* pushKey is only allowed if there is a single table (as in an INSERT or
4047c478bd9Sstevel@tonic-gate   ** UPDATE statement)
4057c478bd9Sstevel@tonic-gate   */
4067c478bd9Sstevel@tonic-gate   assert( pushKey==0 || pTabList->nSrc==1 );
4087c478bd9Sstevel@tonic-gate   /* Split the WHERE clause into separate subexpressions where each
4097c478bd9Sstevel@tonic-gate   ** subexpression is separated by an AND operator.  If the aExpr[]
4107c478bd9Sstevel@tonic-gate   ** array fills up, the last entry might point to an expression which
4117c478bd9Sstevel@tonic-gate   ** contains additional unfactored AND operators.
4127c478bd9Sstevel@tonic-gate   */
4137c478bd9Sstevel@tonic-gate   initMaskSet(&maskSet);
4147c478bd9Sstevel@tonic-gate   memset(aExpr, 0, sizeof(aExpr));
4157c478bd9Sstevel@tonic-gate   nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
4167c478bd9Sstevel@tonic-gate   if( nExpr==ARRAYSIZE(aExpr) ){
4177c478bd9Sstevel@tonic-gate     sqliteErrorMsg(pParse, "WHERE clause too complex - no more "
4187c478bd9Sstevel@tonic-gate        "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
4197c478bd9Sstevel@tonic-gate     return 0;
4207c478bd9Sstevel@tonic-gate   }
4227c478bd9Sstevel@tonic-gate   /* Allocate and initialize the WhereInfo structure that will become the
4237c478bd9Sstevel@tonic-gate   ** return value.
4247c478bd9Sstevel@tonic-gate   */
4257c478bd9Sstevel@tonic-gate   pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
4267c478bd9Sstevel@tonic-gate   if( sqlite_malloc_failed ){
4277c478bd9Sstevel@tonic-gate     sqliteFree(pWInfo);
4287c478bd9Sstevel@tonic-gate     return 0;
4297c478bd9Sstevel@tonic-gate   }
4307c478bd9Sstevel@tonic-gate   pWInfo->pParse = pParse;
4317c478bd9Sstevel@tonic-gate   pWInfo->pTabList = pTabList;
4327c478bd9Sstevel@tonic-gate   pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
4337c478bd9Sstevel@tonic-gate   pWInfo->iBreak = sqliteVdbeMakeLabel(v);
4357c478bd9Sstevel@tonic-gate   /* Special case: a WHERE clause that is constant.  Evaluate the
4367c478bd9Sstevel@tonic-gate   ** expression and either jump over all of the code or fall thru.
4377c478bd9Sstevel@tonic-gate   */
4387c478bd9Sstevel@tonic-gate   if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){
4397c478bd9Sstevel@tonic-gate     sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
4407c478bd9Sstevel@tonic-gate     pWhere = 0;
4417c478bd9Sstevel@tonic-gate   }
4437c478bd9Sstevel@tonic-gate   /* Analyze all of the subexpressions.
4447c478bd9Sstevel@tonic-gate   */
4457c478bd9Sstevel@tonic-gate   for(i=0; i<nExpr; i++){
4467c478bd9Sstevel@tonic-gate     exprAnalyze(&maskSet, &aExpr[i]);
4487c478bd9Sstevel@tonic-gate     /* If we are executing a trigger body, remove all references to
4497c478bd9Sstevel@tonic-gate     ** new.* and old.* tables from the prerequisite masks.
4507c478bd9Sstevel@tonic-gate     */
4517c478bd9Sstevel@tonic-gate     if( pParse->trigStack ){
4527c478bd9Sstevel@tonic-gate       int x;
4537c478bd9Sstevel@tonic-gate       if( (x = pParse->trigStack->newIdx) >= 0 ){
4547c478bd9Sstevel@tonic-gate         int mask = ~getMask(&maskSet, x);
4557c478bd9Sstevel@tonic-gate         aExpr[i].prereqRight &= mask;
4567c478bd9Sstevel@tonic-gate         aExpr[i].prereqLeft &= mask;
4577c478bd9Sstevel@tonic-gate         aExpr[i].prereqAll &= mask;
4587c478bd9Sstevel@tonic-gate       }
4597c478bd9Sstevel@tonic-gate       if( (x = pParse->trigStack->oldIdx) >= 0 ){
4607c478bd9Sstevel@tonic-gate         int mask = ~getMask(&maskSet, x);
4617c478bd9Sstevel@tonic-gate         aExpr[i].prereqRight &= mask;
4627c478bd9Sstevel@tonic-gate         aExpr[i].prereqLeft &= mask;
4637c478bd9Sstevel@tonic-gate         aExpr[i].prereqAll &= mask;
4647c478bd9Sstevel@tonic-gate       }
4657c478bd9Sstevel@tonic-gate     }
4667c478bd9Sstevel@tonic-gate   }
4687c478bd9Sstevel@tonic-gate   /* Figure out what index to use (if any) for each nested loop.
4697c478bd9Sstevel@tonic-gate   ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
4707c478bd9Sstevel@tonic-gate   ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
4717c478bd9Sstevel@tonic-gate   ** loop.
4727c478bd9Sstevel@tonic-gate   **
4737c478bd9Sstevel@tonic-gate   ** If terms exist that use the ROWID of any table, then set the
4747c478bd9Sstevel@tonic-gate   ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
4757c478bd9Sstevel@tonic-gate   ** to the index of the term containing the ROWID.  We always prefer
4767c478bd9Sstevel@tonic-gate   ** to use a ROWID which can directly access a table rather than an
4777c478bd9Sstevel@tonic-gate   ** index which requires reading an index first to get the rowid then
4787c478bd9Sstevel@tonic-gate   ** doing a second read of the actual database table.
4797c478bd9Sstevel@tonic-gate   **
4807c478bd9Sstevel@tonic-gate   ** Actually, if there are more than 32 tables in the join, only the
4817c478bd9Sstevel@tonic-gate   ** first 32 tables are candidates for indices.  This is (again) due
4827c478bd9Sstevel@tonic-gate   ** to the limit of 32 bits in an integer bitmask.
4837c478bd9Sstevel@tonic-gate   */
4847c478bd9Sstevel@tonic-gate   loopMask = 0;
4857c478bd9Sstevel@tonic-gate   for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
4867c478bd9Sstevel@tonic-gate     int j;
4877c478bd9Sstevel@tonic-gate     int iCur = pTabList->a[i].iCursor;    /* The cursor for this table */
4887c478bd9Sstevel@tonic-gate     int mask = getMask(&maskSet, iCur);   /* Cursor mask for this table */
4897c478bd9Sstevel@tonic-gate     Table *pTab = pTabList->a[i].pTab;
4907c478bd9Sstevel@tonic-gate     Index *pIdx;
4917c478bd9Sstevel@tonic-gate     Index *pBestIdx = 0;
4927c478bd9Sstevel@tonic-gate     int bestScore = 0;
4947c478bd9Sstevel@tonic-gate     /* Check to see if there is an expression that uses only the
4957c478bd9Sstevel@tonic-gate     ** ROWID field of this table.  For terms of the form ROWID==expr
4967c478bd9Sstevel@tonic-gate     ** set iDirectEq[i] to the index of the term.  For terms of the
4977c478bd9Sstevel@tonic-gate     ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
4987c478bd9Sstevel@tonic-gate     ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
4997c478bd9Sstevel@tonic-gate     **
5007c478bd9Sstevel@tonic-gate     ** (Added:) Treat ROWID IN expr like ROWID=expr.
5017c478bd9Sstevel@tonic-gate     */
5027c478bd9Sstevel@tonic-gate     pWInfo->a[i].iCur = -1;
5037c478bd9Sstevel@tonic-gate     iDirectEq[i] = -1;
5047c478bd9Sstevel@tonic-gate     iDirectLt[i] = -1;
5057c478bd9Sstevel@tonic-gate     iDirectGt[i] = -1;
5067c478bd9Sstevel@tonic-gate     for(j=0; j<nExpr; j++){
5077c478bd9Sstevel@tonic-gate       if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
5087c478bd9Sstevel@tonic-gate             && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
5097c478bd9Sstevel@tonic-gate         switch( aExpr[j].p->op ){
5107c478bd9Sstevel@tonic-gate           case TK_IN:
5117c478bd9Sstevel@tonic-gate           case TK_EQ: iDirectEq[i] = j; break;
5127c478bd9Sstevel@tonic-gate           case TK_LE:
5137c478bd9Sstevel@tonic-gate           case TK_LT: iDirectLt[i] = j; break;
5147c478bd9Sstevel@tonic-gate           case TK_GE:
5157c478bd9Sstevel@tonic-gate           case TK_GT: iDirectGt[i] = j;  break;
5167c478bd9Sstevel@tonic-gate         }
5177c478bd9Sstevel@tonic-gate       }
5187c478bd9Sstevel@tonic-gate       if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
5197c478bd9Sstevel@tonic-gate             && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
5207c478bd9Sstevel@tonic-gate         switch( aExpr[j].p->op ){
5217c478bd9Sstevel@tonic-gate           case TK_EQ: iDirectEq[i] = j;  break;
5227c478bd9Sstevel@tonic-gate           case TK_LE:
5237c478bd9Sstevel@tonic-gate           case TK_LT: iDirectGt[i] = j;  break;
5247c478bd9Sstevel@tonic-gate           case TK_GE:
5257c478bd9Sstevel@tonic-gate           case TK_GT: iDirectLt[i] = j;  break;
5267c478bd9Sstevel@tonic-gate         }
5277c478bd9Sstevel@tonic-gate       }
5287c478bd9Sstevel@tonic-gate     }
5297c478bd9Sstevel@tonic-gate     if( iDirectEq[i]>=0 ){
5307c478bd9Sstevel@tonic-gate       loopMask |= mask;
5317c478bd9Sstevel@tonic-gate       pWInfo->a[i].pIdx = 0;
5327c478bd9Sstevel@tonic-gate       continue;
5337c478bd9Sstevel@tonic-gate     }
5357c478bd9Sstevel@tonic-gate     /* Do a search for usable indices.  Leave pBestIdx pointing to
5367c478bd9Sstevel@tonic-gate     ** the "best" index.  pBestIdx is left set to NULL if no indices
5377c478bd9Sstevel@tonic-gate     ** are usable.
5387c478bd9Sstevel@tonic-gate     **
5397c478bd9Sstevel@tonic-gate     ** The best index is determined as follows.  For each of the
5407c478bd9Sstevel@tonic-gate     ** left-most terms that is fixed by an equality operator, add
5417c478bd9Sstevel@tonic-gate     ** 8 to the score.  The right-most term of the index may be
5427c478bd9Sstevel@tonic-gate     ** constrained by an inequality.  Add 1 if for an "x<..." constraint
5437c478bd9Sstevel@tonic-gate     ** and add 2 for an "x>..." constraint.  Chose the index that
5447c478bd9Sstevel@tonic-gate     ** gives the best score.
5457c478bd9Sstevel@tonic-gate     **
5467c478bd9Sstevel@tonic-gate     ** This scoring system is designed so that the score can later be
5477c478bd9Sstevel@tonic-gate     ** used to determine how the index is used.  If the score&7 is 0
5487c478bd9Sstevel@tonic-gate     ** then all constraints are equalities.  If score&1 is not 0 then
5497c478bd9Sstevel@tonic-gate     ** there is an inequality used as a termination key.  (ex: "x<...")
5507c478bd9Sstevel@tonic-gate     ** If score&2 is not 0 then there is an inequality used as the
5517c478bd9Sstevel@tonic-gate     ** start key.  (ex: "x>...").  A score or 4 is the special case
5527c478bd9Sstevel@tonic-gate     ** of an IN operator constraint.  (ex:  "x IN ...").
5537c478bd9Sstevel@tonic-gate     **
5547c478bd9Sstevel@tonic-gate     ** The IN operator (as in "<expr> IN (...)") is treated the same as
5557c478bd9Sstevel@tonic-gate     ** an equality comparison except that it can only be used on the
5567c478bd9Sstevel@tonic-gate     ** left-most column of an index and other terms of the WHERE clause
5577c478bd9Sstevel@tonic-gate     ** cannot be used in conjunction with the IN operator to help satisfy
5587c478bd9Sstevel@tonic-gate     ** other columns of the index.
5597c478bd9Sstevel@tonic-gate     */
5607c478bd9Sstevel@tonic-gate     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
5617c478bd9Sstevel@tonic-gate       int eqMask = 0;  /* Index columns covered by an x=... term */
5627c478bd9Sstevel@tonic-gate       int ltMask = 0;  /* Index columns covered by an x<... term */
5637c478bd9Sstevel@tonic-gate       int gtMask = 0;  /* Index columns covered by an x>... term */
5647c478bd9Sstevel@tonic-gate       int inMask = 0;  /* Index columns covered by an x IN .. term */
5657c478bd9Sstevel@tonic-gate       int nEq, m, score;
5677c478bd9Sstevel@tonic-gate       if( pIdx->nColumn>32 ) continue;  /* Ignore indices too many columns */
5687c478bd9Sstevel@tonic-gate       for(j=0; j<nExpr; j++){
5697c478bd9Sstevel@tonic-gate         if( aExpr[j].idxLeft==iCur
5707c478bd9Sstevel@tonic-gate              && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
5717c478bd9Sstevel@tonic-gate           int iColumn = aExpr[j].p->pLeft->iColumn;
5727c478bd9Sstevel@tonic-gate           int k;
5737c478bd9Sstevel@tonic-gate           for(k=0; k<pIdx->nColumn; k++){
5747c478bd9Sstevel@tonic-gate             if( pIdx->aiColumn[k]==iColumn ){
5757c478bd9Sstevel@tonic-gate               switch( aExpr[j].p->op ){
5767c478bd9Sstevel@tonic-gate                 case TK_IN: {
5777c478bd9Sstevel@tonic-gate                   if( k==0 ) inMask |= 1;
5787c478bd9Sstevel@tonic-gate                   break;
5797c478bd9Sstevel@tonic-gate                 }
5807c478bd9Sstevel@tonic-gate                 case TK_EQ: {
5817c478bd9Sstevel@tonic-gate                   eqMask |= 1<<k;
5827c478bd9Sstevel@tonic-gate                   break;
5837c478bd9Sstevel@tonic-gate                 }
5847c478bd9Sstevel@tonic-gate                 case TK_LE:
5857c478bd9Sstevel@tonic-gate                 case TK_LT: {
5867c478bd9Sstevel@tonic-gate                   ltMask |= 1<<k;
5877c478bd9Sstevel@tonic-gate                   break;
5887c478bd9Sstevel@tonic-gate                 }
5897c478bd9Sstevel@tonic-gate                 case TK_GE:
5907c478bd9Sstevel@tonic-gate                 case TK_GT: {
5917c478bd9Sstevel@tonic-gate                   gtMask |= 1<<k;
5927c478bd9Sstevel@tonic-gate                   break;
5937c478bd9Sstevel@tonic-gate                 }
5947c478bd9Sstevel@tonic-gate                 default: {
5957c478bd9Sstevel@tonic-gate                   /* CANT_HAPPEN */
5967c478bd9Sstevel@tonic-gate                   assert( 0 );
5977c478bd9Sstevel@tonic-gate                   break;
5987c478bd9Sstevel@tonic-gate                 }
5997c478bd9Sstevel@tonic-gate               }
6007c478bd9Sstevel@tonic-gate               break;
6017c478bd9Sstevel@tonic-gate             }
6027c478bd9Sstevel@tonic-gate           }
6037c478bd9Sstevel@tonic-gate         }
6047c478bd9Sstevel@tonic-gate         if( aExpr[j].idxRight==iCur
6057c478bd9Sstevel@tonic-gate              && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
6067c478bd9Sstevel@tonic-gate           int iColumn = aExpr[j].p->pRight->iColumn;
6077c478bd9Sstevel@tonic-gate           int k;
6087c478bd9Sstevel@tonic-gate           for(k=0; k<pIdx->nColumn; k++){
6097c478bd9Sstevel@tonic-gate             if( pIdx->aiColumn[k]==iColumn ){
6107c478bd9Sstevel@tonic-gate               switch( aExpr[j].p->op ){
6117c478bd9Sstevel@tonic-gate                 case TK_EQ: {
6127c478bd9Sstevel@tonic-gate                   eqMask |= 1<<k;
6137c478bd9Sstevel@tonic-gate                   break;
6147c478bd9Sstevel@tonic-gate                 }
6157c478bd9Sstevel@tonic-gate                 case TK_LE:
6167c478bd9Sstevel@tonic-gate                 case TK_LT: {
6177c478bd9Sstevel@tonic-gate                   gtMask |= 1<<k;
6187c478bd9Sstevel@tonic-gate                   break;
6197c478bd9Sstevel@tonic-gate                 }
6207c478bd9Sstevel@tonic-gate                 case TK_GE:
6217c478bd9Sstevel@tonic-gate                 case TK_GT: {
6227c478bd9Sstevel@tonic-gate                   ltMask |= 1<<k;
6237c478bd9Sstevel@tonic-gate                   break;
6247c478bd9Sstevel@tonic-gate                 }
6257c478bd9Sstevel@tonic-gate                 default: {
6267c478bd9Sstevel@tonic-gate                   /* CANT_HAPPEN */
6277c478bd9Sstevel@tonic-gate                   assert( 0 );
6287c478bd9Sstevel@tonic-gate                   break;
6297c478bd9Sstevel@tonic-gate                 }
6307c478bd9Sstevel@tonic-gate               }
6317c478bd9Sstevel@tonic-gate               break;
6327c478bd9Sstevel@tonic-gate             }
6337c478bd9Sstevel@tonic-gate           }
6347c478bd9Sstevel@tonic-gate         }
6357c478bd9Sstevel@tonic-gate       }
6377c478bd9Sstevel@tonic-gate       /* The following loop ends with nEq set to the number of columns
6387c478bd9Sstevel@tonic-gate       ** on the left of the index with == constraints.
6397c478bd9Sstevel@tonic-gate       */
6407c478bd9Sstevel@tonic-gate       for(nEq=0; nEq<pIdx->nColumn; nEq++){
6417c478bd9Sstevel@tonic-gate         m = (1<<(nEq+1))-1;
6427c478bd9Sstevel@tonic-gate         if( (m & eqMask)!=m ) break;
6437c478bd9Sstevel@tonic-gate       }
6447c478bd9Sstevel@tonic-gate       score = nEq*8;   /* Base score is 8 times number of == constraints */
6457c478bd9Sstevel@tonic-gate       m = 1<<nEq;
6467c478bd9Sstevel@tonic-gate       if( m & ltMask ) score++;    /* Increase score for a < constraint */
6477c478bd9Sstevel@tonic-gate       if( m & gtMask ) score+=2;   /* Increase score for a > constraint */
6487c478bd9Sstevel@tonic-gate       if( score==0 && inMask ) score = 4;  /* Default score for IN constraint */
6497c478bd9Sstevel@tonic-gate       if( score>bestScore ){
6507c478bd9Sstevel@tonic-gate         pBestIdx = pIdx;
6517c478bd9Sstevel@tonic-gate         bestScore = score;
6527c478bd9Sstevel@tonic-gate       }
6537c478bd9Sstevel@tonic-gate     }
6547c478bd9Sstevel@tonic-gate     pWInfo->a[i].pIdx = pBestIdx;
6557c478bd9Sstevel@tonic-gate     pWInfo->a[i].score = bestScore;
6567c478bd9Sstevel@tonic-gate     pWInfo->a[i].bRev = 0;
6577c478bd9Sstevel@tonic-gate     loopMask |= mask;
6587c478bd9Sstevel@tonic-gate     if( pBestIdx ){
6597c478bd9Sstevel@tonic-gate       pWInfo->a[i].iCur = pParse->nTab++;
6607c478bd9Sstevel@tonic-gate       pWInfo->peakNTab = pParse->nTab;
6617c478bd9Sstevel@tonic-gate     }
6627c478bd9Sstevel@tonic-gate   }
6647c478bd9Sstevel@tonic-gate   /* Check to see if the ORDER BY clause is or can be satisfied by the
6657c478bd9Sstevel@tonic-gate   ** use of an index on the first table.
6667c478bd9Sstevel@tonic-gate   */
6677c478bd9Sstevel@tonic-gate   if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
6687c478bd9Sstevel@tonic-gate      Index *pSortIdx;
6697c478bd9Sstevel@tonic-gate      Index *pIdx;
6707c478bd9Sstevel@tonic-gate      Table *pTab;
6717c478bd9Sstevel@tonic-gate      int bRev = 0;
6737c478bd9Sstevel@tonic-gate      pTab = pTabList->a[0].pTab;
6747c478bd9Sstevel@tonic-gate      pIdx = pWInfo->a[0].pIdx;
6757c478bd9Sstevel@tonic-gate      if( pIdx && pWInfo->a[0].score==4 ){
6767c478bd9Sstevel@tonic-gate        /* If there is already an IN index on the left-most table,
6777c478bd9Sstevel@tonic-gate        ** it will not give the correct sort order.
6787c478bd9Sstevel@tonic-gate        ** So, pretend that no suitable index is found.
6797c478bd9Sstevel@tonic-gate        */
6807c478bd9Sstevel@tonic-gate        pSortIdx = 0;
6817c478bd9Sstevel@tonic-gate      }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
6827c478bd9Sstevel@tonic-gate        /* If the left-most column is accessed using its ROWID, then do
6837c478bd9Sstevel@tonic-gate        ** not try to sort by index.
6847c478bd9Sstevel@tonic-gate        */
6857c478bd9Sstevel@tonic-gate        pSortIdx = 0;
6867c478bd9Sstevel@tonic-gate      }else{
6877c478bd9Sstevel@tonic-gate        int nEqCol = (pWInfo->a[0].score+4)/8;
6887c478bd9Sstevel@tonic-gate        pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
6897c478bd9Sstevel@tonic-gate                                    *ppOrderBy, pIdx, nEqCol, &bRev);
6907c478bd9Sstevel@tonic-gate      }
6917c478bd9Sstevel@tonic-gate      if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
6927c478bd9Sstevel@tonic-gate        if( pIdx==0 ){
6937c478bd9Sstevel@tonic-gate          pWInfo->a[0].pIdx = pSortIdx;
6947c478bd9Sstevel@tonic-gate          pWInfo->a[0].iCur = pParse->nTab++;
6957c478bd9Sstevel@tonic-gate          pWInfo->peakNTab = pParse->nTab;
6967c478bd9Sstevel@tonic-gate        }
6977c478bd9Sstevel@tonic-gate        pWInfo->a[0].bRev = bRev;
6987c478bd9Sstevel@tonic-gate        *ppOrderBy = 0;
6997c478bd9Sstevel@tonic-gate      }
7007c478bd9Sstevel@tonic-gate   }
7027c478bd9Sstevel@tonic-gate   /* Open all tables in the pTabList and all indices used by those tables.
7037c478bd9Sstevel@tonic-gate   */
7047c478bd9Sstevel@tonic-gate   for(i=0; i<pTabList->nSrc; i++){
7057c478bd9Sstevel@tonic-gate     Table *pTab;
7067c478bd9Sstevel@tonic-gate     Index *pIx;
7087c478bd9Sstevel@tonic-gate     pTab = pTabList->a[i].pTab;
7097c478bd9Sstevel@tonic-gate     if( pTab->isTransient || pTab->pSelect ) continue;
7107c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
7117c478bd9Sstevel@tonic-gate     sqliteVdbeOp3(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum,
7127c478bd9Sstevel@tonic-gate                      pTab->zName, P3_STATIC);
7137c478bd9Sstevel@tonic-gate     sqliteCodeVerifySchema(pParse, pTab->iDb);
7147c478bd9Sstevel@tonic-gate     if( (pIx = pWInfo->a[i].pIdx)!=0 ){
7157c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, pIx->iDb, 0);
7167c478bd9Sstevel@tonic-gate       sqliteVdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum, pIx->zName,0);
7177c478bd9Sstevel@tonic-gate     }
7187c478bd9Sstevel@tonic-gate   }
7207c478bd9Sstevel@tonic-gate   /* Generate the code to do the search
7217c478bd9Sstevel@tonic-gate   */
7227c478bd9Sstevel@tonic-gate   loopMask = 0;
7237c478bd9Sstevel@tonic-gate   for(i=0; i<pTabList->nSrc; i++){
7247c478bd9Sstevel@tonic-gate     int j, k;
7257c478bd9Sstevel@tonic-gate     int iCur = pTabList->a[i].iCursor;
7267c478bd9Sstevel@tonic-gate     Index *pIdx;
7277c478bd9Sstevel@tonic-gate     WhereLevel *pLevel = &pWInfo->a[i];
7297c478bd9Sstevel@tonic-gate     /* If this is the right table of a LEFT OUTER JOIN, allocate and
7307c478bd9Sstevel@tonic-gate     ** initialize a memory cell that records if this table matches any
7317c478bd9Sstevel@tonic-gate     ** row of the left table of the join.
7327c478bd9Sstevel@tonic-gate     */
7337c478bd9Sstevel@tonic-gate     if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
7347c478bd9Sstevel@tonic-gate       if( !pParse->nMem ) pParse->nMem++;
7357c478bd9Sstevel@tonic-gate       pLevel->iLeftJoin = pParse->nMem++;
7367c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_String, 0, 0);
7377c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
7387c478bd9Sstevel@tonic-gate     }
7407c478bd9Sstevel@tonic-gate     pIdx = pLevel->pIdx;
7417c478bd9Sstevel@tonic-gate     pLevel->inOp = OP_Noop;
7427c478bd9Sstevel@tonic-gate     if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
7437c478bd9Sstevel@tonic-gate       /* Case 1:  We can directly reference a single row using an
7447c478bd9Sstevel@tonic-gate       **          equality comparison against the ROWID field.  Or
7457c478bd9Sstevel@tonic-gate       **          we reference multiple rows using a "rowid IN (...)"
7467c478bd9Sstevel@tonic-gate       **          construct.
7477c478bd9Sstevel@tonic-gate       */
7487c478bd9Sstevel@tonic-gate       k = iDirectEq[i];
7497c478bd9Sstevel@tonic-gate       assert( k<nExpr );
7507c478bd9Sstevel@tonic-gate       assert( aExpr[k].p!=0 );
7517c478bd9Sstevel@tonic-gate       assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
7527c478bd9Sstevel@tonic-gate       brk = pLevel->brk = sqliteVdbeMakeLabel(v);
7537c478bd9Sstevel@tonic-gate       if( aExpr[k].idxLeft==iCur ){
7547c478bd9Sstevel@tonic-gate         Expr *pX = aExpr[k].p;
7557c478bd9Sstevel@tonic-gate         if( pX->op!=TK_IN ){
7567c478bd9Sstevel@tonic-gate           sqliteExprCode(pParse, aExpr[k].p->pRight);
7577c478bd9Sstevel@tonic-gate         }else if( pX->pList ){
7587c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
7597c478bd9Sstevel@tonic-gate           pLevel->inOp = OP_SetNext;
7607c478bd9Sstevel@tonic-gate           pLevel->inP1 = pX->iTable;
7617c478bd9Sstevel@tonic-gate           pLevel->inP2 = sqliteVdbeCurrentAddr(v);
7627c478bd9Sstevel@tonic-gate         }else{
7637c478bd9Sstevel@tonic-gate           assert( pX->pSelect );
7647c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
7657c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
7667c478bd9Sstevel@tonic-gate           pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
7677c478bd9Sstevel@tonic-gate           pLevel->inOp = OP_Next;
7687c478bd9Sstevel@tonic-gate           pLevel->inP1 = pX->iTable;
7697c478bd9Sstevel@tonic-gate         }
7707c478bd9Sstevel@tonic-gate       }else{
7717c478bd9Sstevel@tonic-gate         sqliteExprCode(pParse, aExpr[k].p->pLeft);
7727c478bd9Sstevel@tonic-gate       }
7737c478bd9Sstevel@tonic-gate       disableTerm(pLevel, &aExpr[k].p);
7747c478bd9Sstevel@tonic-gate       cont = pLevel->cont = sqliteVdbeMakeLabel(v);
7757c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk);
7767c478bd9Sstevel@tonic-gate       haveKey = 0;
7777c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NotExists, iCur, brk);
7787c478bd9Sstevel@tonic-gate       pLevel->op = OP_Noop;
7797c478bd9Sstevel@tonic-gate     }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
7807c478bd9Sstevel@tonic-gate       /* Case 2:  There is an index and all terms of the WHERE clause that
7817c478bd9Sstevel@tonic-gate       **          refer to the index use the "==" or "IN" operators.
7827c478bd9Sstevel@tonic-gate       */
7837c478bd9Sstevel@tonic-gate       int start;
7847c478bd9Sstevel@tonic-gate       int testOp;
7857c478bd9Sstevel@tonic-gate       int nColumn = (pLevel->score+4)/8;
7867c478bd9Sstevel@tonic-gate       brk = pLevel->brk = sqliteVdbeMakeLabel(v);
7877c478bd9Sstevel@tonic-gate       for(j=0; j<nColumn; j++){
7887c478bd9Sstevel@tonic-gate         for(k=0; k<nExpr; k++){
7897c478bd9Sstevel@tonic-gate           Expr *pX = aExpr[k].p;
7907c478bd9Sstevel@tonic-gate           if( pX==0 ) continue;
7917c478bd9Sstevel@tonic-gate           if( aExpr[k].idxLeft==iCur
7927c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
7937c478bd9Sstevel@tonic-gate              && pX->pLeft->iColumn==pIdx->aiColumn[j]
7947c478bd9Sstevel@tonic-gate           ){
7957c478bd9Sstevel@tonic-gate             if( pX->op==TK_EQ ){
7967c478bd9Sstevel@tonic-gate               sqliteExprCode(pParse, pX->pRight);
7977c478bd9Sstevel@tonic-gate               disableTerm(pLevel, &aExpr[k].p);
7987c478bd9Sstevel@tonic-gate               break;
7997c478bd9Sstevel@tonic-gate             }
8007c478bd9Sstevel@tonic-gate             if( pX->op==TK_IN && nColumn==1 ){
8017c478bd9Sstevel@tonic-gate               if( pX->pList ){
8027c478bd9Sstevel@tonic-gate                 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
8037c478bd9Sstevel@tonic-gate                 pLevel->inOp = OP_SetNext;
8047c478bd9Sstevel@tonic-gate                 pLevel->inP1 = pX->iTable;
8057c478bd9Sstevel@tonic-gate                 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
8067c478bd9Sstevel@tonic-gate               }else{
8077c478bd9Sstevel@tonic-gate                 assert( pX->pSelect );
8087c478bd9Sstevel@tonic-gate                 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
8097c478bd9Sstevel@tonic-gate                 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
8107c478bd9Sstevel@tonic-gate                 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
8117c478bd9Sstevel@tonic-gate                 pLevel->inOp = OP_Next;
8127c478bd9Sstevel@tonic-gate                 pLevel->inP1 = pX->iTable;
8137c478bd9Sstevel@tonic-gate               }
8147c478bd9Sstevel@tonic-gate               disableTerm(pLevel, &aExpr[k].p);
8157c478bd9Sstevel@tonic-gate               break;
8167c478bd9Sstevel@tonic-gate             }
8177c478bd9Sstevel@tonic-gate           }
8187c478bd9Sstevel@tonic-gate           if( aExpr[k].idxRight==iCur
8197c478bd9Sstevel@tonic-gate              && aExpr[k].p->op==TK_EQ
8207c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
8217c478bd9Sstevel@tonic-gate              && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
8227c478bd9Sstevel@tonic-gate           ){
8237c478bd9Sstevel@tonic-gate             sqliteExprCode(pParse, aExpr[k].p->pLeft);
8247c478bd9Sstevel@tonic-gate             disableTerm(pLevel, &aExpr[k].p);
8257c478bd9Sstevel@tonic-gate             break;
8267c478bd9Sstevel@tonic-gate           }
8277c478bd9Sstevel@tonic-gate         }
8287c478bd9Sstevel@tonic-gate       }
8297c478bd9Sstevel@tonic-gate       pLevel->iMem = pParse->nMem++;
8307c478bd9Sstevel@tonic-gate       cont = pLevel->cont = sqliteVdbeMakeLabel(v);
8317c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NotNull, -nColumn, sqliteVdbeCurrentAddr(v)+3);
8327c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
8337c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Goto, 0, brk);
8347c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
8357c478bd9Sstevel@tonic-gate       sqliteAddIdxKeyType(v, pIdx);
8367c478bd9Sstevel@tonic-gate       if( nColumn==pIdx->nColumn || pLevel->bRev ){
8377c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
8387c478bd9Sstevel@tonic-gate         testOp = OP_IdxGT;
8397c478bd9Sstevel@tonic-gate       }else{
8407c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Dup, 0, 0);
8417c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
8427c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
8437c478bd9Sstevel@tonic-gate         testOp = OP_IdxGE;
8447c478bd9Sstevel@tonic-gate       }
8457c478bd9Sstevel@tonic-gate       if( pLevel->bRev ){
8467c478bd9Sstevel@tonic-gate         /* Scan in reverse order */
8477c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
8487c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
8497c478bd9Sstevel@tonic-gate         start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
8507c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
8517c478bd9Sstevel@tonic-gate         pLevel->op = OP_Prev;
8527c478bd9Sstevel@tonic-gate       }else{
8537c478bd9Sstevel@tonic-gate         /* Scan in the forward order */
8547c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
8557c478bd9Sstevel@tonic-gate         start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
8567c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
8577c478bd9Sstevel@tonic-gate         pLevel->op = OP_Next;
8587c478bd9Sstevel@tonic-gate       }
8597c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
8607c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
8617c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
8627c478bd9Sstevel@tonic-gate       if( i==pTabList->nSrc-1 && pushKey ){
8637c478bd9Sstevel@tonic-gate         haveKey = 1;
8647c478bd9Sstevel@tonic-gate       }else{
8657c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
8667c478bd9Sstevel@tonic-gate         haveKey = 0;
8677c478bd9Sstevel@tonic-gate       }
8687c478bd9Sstevel@tonic-gate       pLevel->p1 = pLevel->iCur;
8697c478bd9Sstevel@tonic-gate       pLevel->p2 = start;
8707c478bd9Sstevel@tonic-gate     }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
8717c478bd9Sstevel@tonic-gate       /* Case 3:  We have an inequality comparison against the ROWID field.
8727c478bd9Sstevel@tonic-gate       */
8737c478bd9Sstevel@tonic-gate       int testOp = OP_Noop;
8747c478bd9Sstevel@tonic-gate       int start;
8767c478bd9Sstevel@tonic-gate       brk = pLevel->brk = sqliteVdbeMakeLabel(v);
8777c478bd9Sstevel@tonic-gate       cont = pLevel->cont = sqliteVdbeMakeLabel(v);
8787c478bd9Sstevel@tonic-gate       if( iDirectGt[i]>=0 ){
8797c478bd9Sstevel@tonic-gate         k = iDirectGt[i];
8807c478bd9Sstevel@tonic-gate         assert( k<nExpr );
8817c478bd9Sstevel@tonic-gate         assert( aExpr[k].p!=0 );
8827c478bd9Sstevel@tonic-gate         assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
8837c478bd9Sstevel@tonic-gate         if( aExpr[k].idxLeft==iCur ){
8847c478bd9Sstevel@tonic-gate           sqliteExprCode(pParse, aExpr[k].p->pRight);
8857c478bd9Sstevel@tonic-gate         }else{
8867c478bd9Sstevel@tonic-gate           sqliteExprCode(pParse, aExpr[k].p->pLeft);
8877c478bd9Sstevel@tonic-gate         }
8887c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_ForceInt,
8897c478bd9Sstevel@tonic-gate           aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
8907c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MoveTo, iCur, brk);
8917c478bd9Sstevel@tonic-gate         disableTerm(pLevel, &aExpr[k].p);
8927c478bd9Sstevel@tonic-gate       }else{
8937c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
8947c478bd9Sstevel@tonic-gate       }
8957c478bd9Sstevel@tonic-gate       if( iDirectLt[i]>=0 ){
8967c478bd9Sstevel@tonic-gate         k = iDirectLt[i];
8977c478bd9Sstevel@tonic-gate         assert( k<nExpr );
8987c478bd9Sstevel@tonic-gate         assert( aExpr[k].p!=0 );
8997c478bd9Sstevel@tonic-gate         assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
9007c478bd9Sstevel@tonic-gate         if( aExpr[k].idxLeft==iCur ){
9017c478bd9Sstevel@tonic-gate           sqliteExprCode(pParse, aExpr[k].p->pRight);
9027c478bd9Sstevel@tonic-gate         }else{
9037c478bd9Sstevel@tonic-gate           sqliteExprCode(pParse, aExpr[k].p->pLeft);
9047c478bd9Sstevel@tonic-gate         }
9057c478bd9Sstevel@tonic-gate         /* sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1); */
9067c478bd9Sstevel@tonic-gate         pLevel->iMem = pParse->nMem++;
9077c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
9087c478bd9Sstevel@tonic-gate         if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
9097c478bd9Sstevel@tonic-gate           testOp = OP_Ge;
9107c478bd9Sstevel@tonic-gate         }else{
9117c478bd9Sstevel@tonic-gate           testOp = OP_Gt;
9127c478bd9Sstevel@tonic-gate         }
9137c478bd9Sstevel@tonic-gate         disableTerm(pLevel, &aExpr[k].p);
9147c478bd9Sstevel@tonic-gate       }
9157c478bd9Sstevel@tonic-gate       start = sqliteVdbeCurrentAddr(v);
9167c478bd9Sstevel@tonic-gate       pLevel->op = OP_Next;
9177c478bd9Sstevel@tonic-gate       pLevel->p1 = iCur;
9187c478bd9Sstevel@tonic-gate       pLevel->p2 = start;
9197c478bd9Sstevel@tonic-gate       if( testOp!=OP_Noop ){
9207c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
9217c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
9227c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, testOp, 0, brk);
9237c478bd9Sstevel@tonic-gate       }
9247c478bd9Sstevel@tonic-gate       haveKey = 0;
9257c478bd9Sstevel@tonic-gate     }else if( pIdx==0 ){
9267c478bd9Sstevel@tonic-gate       /* Case 4:  There is no usable index.  We must do a complete
9277c478bd9Sstevel@tonic-gate       **          scan of the entire database table.
9287c478bd9Sstevel@tonic-gate       */
9297c478bd9Sstevel@tonic-gate       int start;
9317c478bd9Sstevel@tonic-gate       brk = pLevel->brk = sqliteVdbeMakeLabel(v);
9327c478bd9Sstevel@tonic-gate       cont = pLevel->cont = sqliteVdbeMakeLabel(v);
9337c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
9347c478bd9Sstevel@tonic-gate       start = sqliteVdbeCurrentAddr(v);
9357c478bd9Sstevel@tonic-gate       pLevel->op = OP_Next;
9367c478bd9Sstevel@tonic-gate       pLevel->p1 = iCur;
9377c478bd9Sstevel@tonic-gate       pLevel->p2 = start;
9387c478bd9Sstevel@tonic-gate       haveKey = 0;
9397c478bd9Sstevel@tonic-gate     }else{
9407c478bd9Sstevel@tonic-gate       /* Case 5: The WHERE clause term that refers to the right-most
9417c478bd9Sstevel@tonic-gate       **         column of the index is an inequality.  For example, if
9427c478bd9Sstevel@tonic-gate       **         the index is on (x,y,z) and the WHERE clause is of the
9437c478bd9Sstevel@tonic-gate       **         form "x=5 AND y<10" then this case is used.  Only the
9447c478bd9Sstevel@tonic-gate       **         right-most column can be an inequality - the rest must
9457c478bd9Sstevel@tonic-gate       **         use the "==" operator.
9467c478bd9Sstevel@tonic-gate       **
9477c478bd9Sstevel@tonic-gate       **         This case is also used when there are no WHERE clause
9487c478bd9Sstevel@tonic-gate       **         constraints but an index is selected anyway, in order
9497c478bd9Sstevel@tonic-gate       **         to force the output order to conform to an ORDER BY.
9507c478bd9Sstevel@tonic-gate       */
9517c478bd9Sstevel@tonic-gate       int score = pLevel->score;
9527c478bd9Sstevel@tonic-gate       int nEqColumn = score/8;
9537c478bd9Sstevel@tonic-gate       int start;
9547c478bd9Sstevel@tonic-gate       int leFlag, geFlag;
9557c478bd9Sstevel@tonic-gate       int testOp;
9577c478bd9Sstevel@tonic-gate       /* Evaluate the equality constraints
9587c478bd9Sstevel@tonic-gate       */
9597c478bd9Sstevel@tonic-gate       for(j=0; j<nEqColumn; j++){
9607c478bd9Sstevel@tonic-gate         for(k=0; k<nExpr; k++){
9617c478bd9Sstevel@tonic-gate           if( aExpr[k].p==0 ) continue;
9627c478bd9Sstevel@tonic-gate           if( aExpr[k].idxLeft==iCur
9637c478bd9Sstevel@tonic-gate              && aExpr[k].p->op==TK_EQ
9647c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
9657c478bd9Sstevel@tonic-gate              && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
9667c478bd9Sstevel@tonic-gate           ){
9677c478bd9Sstevel@tonic-gate             sqliteExprCode(pParse, aExpr[k].p->pRight);
9687c478bd9Sstevel@tonic-gate             disableTerm(pLevel, &aExpr[k].p);
9697c478bd9Sstevel@tonic-gate             break;
9707c478bd9Sstevel@tonic-gate           }
9717c478bd9Sstevel@tonic-gate           if( aExpr[k].idxRight==iCur
9727c478bd9Sstevel@tonic-gate              && aExpr[k].p->op==TK_EQ
9737c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
9747c478bd9Sstevel@tonic-gate              && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
9757c478bd9Sstevel@tonic-gate           ){
9767c478bd9Sstevel@tonic-gate             sqliteExprCode(pParse, aExpr[k].p->pLeft);
9777c478bd9Sstevel@tonic-gate             disableTerm(pLevel, &aExpr[k].p);
9787c478bd9Sstevel@tonic-gate             break;
9797c478bd9Sstevel@tonic-gate           }
9807c478bd9Sstevel@tonic-gate         }
9817c478bd9Sstevel@tonic-gate       }
9837c478bd9Sstevel@tonic-gate       /* Duplicate the equality term values because they will all be
9847c478bd9Sstevel@tonic-gate       ** used twice: once to make the termination key and once to make the
9857c478bd9Sstevel@tonic-gate       ** start key.
9867c478bd9Sstevel@tonic-gate       */
9877c478bd9Sstevel@tonic-gate       for(j=0; j<nEqColumn; j++){
9887c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
9897c478bd9Sstevel@tonic-gate       }
9917c478bd9Sstevel@tonic-gate       /* Labels for the beginning and end of the loop
9927c478bd9Sstevel@tonic-gate       */
9937c478bd9Sstevel@tonic-gate       cont = pLevel->cont = sqliteVdbeMakeLabel(v);
9947c478bd9Sstevel@tonic-gate       brk = pLevel->brk = sqliteVdbeMakeLabel(v);
9967c478bd9Sstevel@tonic-gate       /* Generate the termination key.  This is the key value that
9977c478bd9Sstevel@tonic-gate       ** will end the search.  There is no termination key if there
9987c478bd9Sstevel@tonic-gate       ** are no equality terms and no "X<..." term.
9997c478bd9Sstevel@tonic-gate       **
10007c478bd9Sstevel@tonic-gate       ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
10017c478bd9Sstevel@tonic-gate       ** key computed here really ends up being the start key.
10027c478bd9Sstevel@tonic-gate       */
10037c478bd9Sstevel@tonic-gate       if( (score & 1)!=0 ){
10047c478bd9Sstevel@tonic-gate         for(k=0; k<nExpr; k++){
10057c478bd9Sstevel@tonic-gate           Expr *pExpr = aExpr[k].p;
10067c478bd9Sstevel@tonic-gate           if( pExpr==0 ) continue;
10077c478bd9Sstevel@tonic-gate           if( aExpr[k].idxLeft==iCur
10087c478bd9Sstevel@tonic-gate              && (pExpr->op==TK_LT || pExpr->op==TK_LE)
10097c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
10107c478bd9Sstevel@tonic-gate              && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
10117c478bd9Sstevel@tonic-gate           ){
10127c478bd9Sstevel@tonic-gate             sqliteExprCode(pParse, pExpr->pRight);
10137c478bd9Sstevel@tonic-gate             leFlag = pExpr->op==TK_LE;
10147c478bd9Sstevel@tonic-gate             disableTerm(pLevel, &aExpr[k].p);
10157c478bd9Sstevel@tonic-gate             break;
10167c478bd9Sstevel@tonic-gate           }
10177c478bd9Sstevel@tonic-gate           if( aExpr[k].idxRight==iCur
10187c478bd9Sstevel@tonic-gate              && (pExpr->op==TK_GT || pExpr->op==TK_GE)
10197c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
10207c478bd9Sstevel@tonic-gate              && pExpr->pRight->iColumn==pIdx->aiColumn[j]
10217c478bd9Sstevel@tonic-gate           ){
10227c478bd9Sstevel@tonic-gate             sqliteExprCode(pParse, pExpr->pLeft);
10237c478bd9Sstevel@tonic-gate             leFlag = pExpr->op==TK_GE;
10247c478bd9Sstevel@tonic-gate             disableTerm(pLevel, &aExpr[k].p);
10257c478bd9Sstevel@tonic-gate             break;
10267c478bd9Sstevel@tonic-gate           }
10277c478bd9Sstevel@tonic-gate         }
10287c478bd9Sstevel@tonic-gate         testOp = OP_IdxGE;
10297c478bd9Sstevel@tonic-gate       }else{
10307c478bd9Sstevel@tonic-gate         testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
10317c478bd9Sstevel@tonic-gate         leFlag = 1;
10327c478bd9Sstevel@tonic-gate       }
10337c478bd9Sstevel@tonic-gate       if( testOp!=OP_Noop ){
10347c478bd9Sstevel@tonic-gate         int nCol = nEqColumn + (score & 1);
10357c478bd9Sstevel@tonic-gate         pLevel->iMem = pParse->nMem++;
10367c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
10377c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
10387c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Goto, 0, brk);
10397c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
10407c478bd9Sstevel@tonic-gate         sqliteAddIdxKeyType(v, pIdx);
10417c478bd9Sstevel@tonic-gate         if( leFlag ){
10427c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
10437c478bd9Sstevel@tonic-gate         }
10447c478bd9Sstevel@tonic-gate         if( pLevel->bRev ){
10457c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
10467c478bd9Sstevel@tonic-gate         }else{
10477c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
10487c478bd9Sstevel@tonic-gate         }
10497c478bd9Sstevel@tonic-gate       }else if( pLevel->bRev ){
10507c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Last, pLevel->iCur, brk);
10517c478bd9Sstevel@tonic-gate       }
10537c478bd9Sstevel@tonic-gate       /* Generate the start key.  This is the key that defines the lower
10547c478bd9Sstevel@tonic-gate       ** bound on the search.  There is no start key if there are no
10557c478bd9Sstevel@tonic-gate       ** equality terms and if there is no "X>..." term.  In
10567c478bd9Sstevel@tonic-gate       ** that case, generate a "Rewind" instruction in place of the
10577c478bd9Sstevel@tonic-gate       ** start key search.
10587c478bd9Sstevel@tonic-gate       **
10597c478bd9Sstevel@tonic-gate       ** 2002-Dec-04: In the case of a reverse-order search, the so-called
10607c478bd9Sstevel@tonic-gate       ** "start" key really ends up being used as the termination key.
10617c478bd9Sstevel@tonic-gate       */
10627c478bd9Sstevel@tonic-gate       if( (score & 2)!=0 ){
10637c478bd9Sstevel@tonic-gate         for(k=0; k<nExpr; k++){
10647c478bd9Sstevel@tonic-gate           Expr *pExpr = aExpr[k].p;
10657c478bd9Sstevel@tonic-gate           if( pExpr==0 ) continue;
10667c478bd9Sstevel@tonic-gate           if( aExpr[k].idxLeft==iCur
10677c478bd9Sstevel@tonic-gate              && (pExpr->op==TK_GT || pExpr->op==TK_GE)
10687c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
10697c478bd9Sstevel@tonic-gate              && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
10707c478bd9Sstevel@tonic-gate           ){
10717c478bd9Sstevel@tonic-gate             sqliteExprCode(pParse, pExpr->pRight);
10727c478bd9Sstevel@tonic-gate             geFlag = pExpr->op==TK_GE;
10737c478bd9Sstevel@tonic-gate             disableTerm(pLevel, &aExpr[k].p);
10747c478bd9Sstevel@tonic-gate             break;
10757c478bd9Sstevel@tonic-gate           }
10767c478bd9Sstevel@tonic-gate           if( aExpr[k].idxRight==iCur
10777c478bd9Sstevel@tonic-gate              && (pExpr->op==TK_LT || pExpr->op==TK_LE)
10787c478bd9Sstevel@tonic-gate              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
10797c478bd9Sstevel@tonic-gate              && pExpr->pRight->iColumn==pIdx->aiColumn[j]
10807c478bd9Sstevel@tonic-gate           ){
10817c478bd9Sstevel@tonic-gate             sqliteExprCode(pParse, pExpr->pLeft);
10827c478bd9Sstevel@tonic-gate             geFlag = pExpr->op==TK_LE;
10837c478bd9Sstevel@tonic-gate             disableTerm(pLevel, &aExpr[k].p);
10847c478bd9Sstevel@tonic-gate             break;
10857c478bd9Sstevel@tonic-gate           }
10867c478bd9Sstevel@tonic-gate         }
10877c478bd9Sstevel@tonic-gate       }else{
10887c478bd9Sstevel@tonic-gate         geFlag = 1;
10897c478bd9Sstevel@tonic-gate       }
10907c478bd9Sstevel@tonic-gate       if( nEqColumn>0 || (score&2)!=0 ){
10917c478bd9Sstevel@tonic-gate         int nCol = nEqColumn + ((score&2)!=0);
10927c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
10937c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
10947c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Goto, 0, brk);
10957c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
10967c478bd9Sstevel@tonic-gate         sqliteAddIdxKeyType(v, pIdx);
10977c478bd9Sstevel@tonic-gate         if( !geFlag ){
10987c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
10997c478bd9Sstevel@tonic-gate         }
11007c478bd9Sstevel@tonic-gate         if( pLevel->bRev ){
11017c478bd9Sstevel@tonic-gate           pLevel->iMem = pParse->nMem++;
11027c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
11037c478bd9Sstevel@tonic-gate           testOp = OP_IdxLT;
11047c478bd9Sstevel@tonic-gate         }else{
11057c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
11067c478bd9Sstevel@tonic-gate         }
11077c478bd9Sstevel@tonic-gate       }else if( pLevel->bRev ){
11087c478bd9Sstevel@tonic-gate         testOp = OP_Noop;
11097c478bd9Sstevel@tonic-gate       }else{
11107c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
11117c478bd9Sstevel@tonic-gate       }
11137c478bd9Sstevel@tonic-gate       /* Generate the the top of the loop.  If there is a termination
11147c478bd9Sstevel@tonic-gate       ** key we have to test for that key and abort at the top of the
11157c478bd9Sstevel@tonic-gate       ** loop.
11167c478bd9Sstevel@tonic-gate       */
11177c478bd9Sstevel@tonic-gate       start = sqliteVdbeCurrentAddr(v);
11187c478bd9Sstevel@tonic-gate       if( testOp!=OP_Noop ){
11197c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
11207c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
11217c478bd9Sstevel@tonic-gate       }
11227c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
11237c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
11247c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
11257c478bd9Sstevel@tonic-gate       if( i==pTabList->nSrc-1 && pushKey ){
11267c478bd9Sstevel@tonic-gate         haveKey = 1;
11277c478bd9Sstevel@tonic-gate       }else{
11287c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
11297c478bd9Sstevel@tonic-gate         haveKey = 0;
11307c478bd9Sstevel@tonic-gate       }
11327c478bd9Sstevel@tonic-gate       /* Record the instruction used to terminate the loop.
11337c478bd9Sstevel@tonic-gate       */
11347c478bd9Sstevel@tonic-gate       pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
11357c478bd9Sstevel@tonic-gate       pLevel->p1 = pLevel->iCur;
11367c478bd9Sstevel@tonic-gate       pLevel->p2 = start;
11377c478bd9Sstevel@tonic-gate     }
11387c478bd9Sstevel@tonic-gate     loopMask |= getMask(&maskSet, iCur);
11407c478bd9Sstevel@tonic-gate     /* Insert code to test every subexpression that can be completely
11417c478bd9Sstevel@tonic-gate     ** computed using the current set of tables.
11427c478bd9Sstevel@tonic-gate     */
11437c478bd9Sstevel@tonic-gate     for(j=0; j<nExpr; j++){
11447c478bd9Sstevel@tonic-gate       if( aExpr[j].p==0 ) continue;
11457c478bd9Sstevel@tonic-gate       if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
11467c478bd9Sstevel@tonic-gate       if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
11477c478bd9Sstevel@tonic-gate         continue;
11487c478bd9Sstevel@tonic-gate       }
11497c478bd9Sstevel@tonic-gate       if( haveKey ){
11507c478bd9Sstevel@tonic-gate         haveKey = 0;
11517c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
11527c478bd9Sstevel@tonic-gate       }
11537c478bd9Sstevel@tonic-gate       sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
11547c478bd9Sstevel@tonic-gate       aExpr[j].p = 0;
11557c478bd9Sstevel@tonic-gate     }
11567c478bd9Sstevel@tonic-gate     brk = cont;
11587c478bd9Sstevel@tonic-gate     /* For a LEFT OUTER JOIN, generate code that will record the fact that
11597c478bd9Sstevel@tonic-gate     ** at least one row of the right table has matched the left table.
11607c478bd9Sstevel@tonic-gate     */
11617c478bd9Sstevel@tonic-gate     if( pLevel->iLeftJoin ){
11627c478bd9Sstevel@tonic-gate       pLevel->top = sqliteVdbeCurrentAddr(v);
11637c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Integer, 1, 0);
11647c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
11657c478bd9Sstevel@tonic-gate       for(j=0; j<nExpr; j++){
11667c478bd9Sstevel@tonic-gate         if( aExpr[j].p==0 ) continue;
11677c478bd9Sstevel@tonic-gate         if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
11687c478bd9Sstevel@tonic-gate         if( haveKey ){
11697c478bd9Sstevel@tonic-gate           /* Cannot happen.  "haveKey" can only be true if pushKey is true
11707c478bd9Sstevel@tonic-gate           ** an pushKey can only be true for DELETE and UPDATE and there are
11717c478bd9Sstevel@tonic-gate           ** no outer joins with DELETE and UPDATE.
11727c478bd9Sstevel@tonic-gate           */
11737c478bd9Sstevel@tonic-gate           haveKey = 0;
11747c478bd9Sstevel@tonic-gate           sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
11757c478bd9Sstevel@tonic-gate         }
11767c478bd9Sstevel@tonic-gate         sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
11777c478bd9Sstevel@tonic-gate         aExpr[j].p = 0;
11787c478bd9Sstevel@tonic-gate       }
11797c478bd9Sstevel@tonic-gate     }
11807c478bd9Sstevel@tonic-gate   }
11817c478bd9Sstevel@tonic-gate   pWInfo->iContinue = cont;
11827c478bd9Sstevel@tonic-gate   if( pushKey && !haveKey ){
11837c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
11847c478bd9Sstevel@tonic-gate   }
11857c478bd9Sstevel@tonic-gate   freeMaskSet(&maskSet);
11867c478bd9Sstevel@tonic-gate   return pWInfo;
11877c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate /*
11907c478bd9Sstevel@tonic-gate ** Generate the end of the WHERE loop.  See comments on
11917c478bd9Sstevel@tonic-gate ** sqliteWhereBegin() for additional information.
11927c478bd9Sstevel@tonic-gate */
sqliteWhereEnd(WhereInfo * pWInfo)11937c478bd9Sstevel@tonic-gate void sqliteWhereEnd(WhereInfo *pWInfo){
11947c478bd9Sstevel@tonic-gate   Vdbe *v = pWInfo->pParse->pVdbe;
11957c478bd9Sstevel@tonic-gate   int i;
11967c478bd9Sstevel@tonic-gate   WhereLevel *pLevel;
11977c478bd9Sstevel@tonic-gate   SrcList *pTabList = pWInfo->pTabList;
11997c478bd9Sstevel@tonic-gate   for(i=pTabList->nSrc-1; i>=0; i--){
12007c478bd9Sstevel@tonic-gate     pLevel = &pWInfo->a[i];
12017c478bd9Sstevel@tonic-gate     sqliteVdbeResolveLabel(v, pLevel->cont);
12027c478bd9Sstevel@tonic-gate     if( pLevel->op!=OP_Noop ){
12037c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
12047c478bd9Sstevel@tonic-gate     }
12057c478bd9Sstevel@tonic-gate     sqliteVdbeResolveLabel(v, pLevel->brk);
12067c478bd9Sstevel@tonic-gate     if( pLevel->inOp!=OP_Noop ){
12077c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
12087c478bd9Sstevel@tonic-gate     }
12097c478bd9Sstevel@tonic-gate     if( pLevel->iLeftJoin ){
12107c478bd9Sstevel@tonic-gate       int addr;
12117c478bd9Sstevel@tonic-gate       addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
12127c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
12137c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
12147c478bd9Sstevel@tonic-gate       if( pLevel->iCur>=0 ){
12157c478bd9Sstevel@tonic-gate         sqliteVdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
12167c478bd9Sstevel@tonic-gate       }
12177c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
12187c478bd9Sstevel@tonic-gate     }
12197c478bd9Sstevel@tonic-gate   }
12207c478bd9Sstevel@tonic-gate   sqliteVdbeResolveLabel(v, pWInfo->iBreak);
12217c478bd9Sstevel@tonic-gate   for(i=0; i<pTabList->nSrc; i++){
12227c478bd9Sstevel@tonic-gate     Table *pTab = pTabList->a[i].pTab;
12237c478bd9Sstevel@tonic-gate     assert( pTab!=0 );
12247c478bd9Sstevel@tonic-gate     if( pTab->isTransient || pTab->pSelect ) continue;
12257c478bd9Sstevel@tonic-gate     pLevel = &pWInfo->a[i];
12267c478bd9Sstevel@tonic-gate     sqliteVdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
12277c478bd9Sstevel@tonic-gate     if( pLevel->pIdx!=0 ){
12287c478bd9Sstevel@tonic-gate       sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
12297c478bd9Sstevel@tonic-gate     }
12307c478bd9Sstevel@tonic-gate   }
12317c478bd9Sstevel@tonic-gate #if 0  /* Never reuse a cursor */
12327c478bd9Sstevel@tonic-gate   if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
12337c478bd9Sstevel@tonic-gate     pWInfo->pParse->nTab = pWInfo->savedNTab;
12347c478bd9Sstevel@tonic-gate   }
12357c478bd9Sstevel@tonic-gate #endif
12367c478bd9Sstevel@tonic-gate   sqliteFree(pWInfo);
12377c478bd9Sstevel@tonic-gate   return;
12387c478bd9Sstevel@tonic-gate }