persistentstorage/sql/SQLite364/where.c
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 /*
       
     2 ** 2001 September 15
       
     3 **
       
     4 ** The author disclaims copyright to this source code.  In place of
       
     5 ** a legal notice, here is a blessing:
       
     6 **
       
     7 **    May you do good and not evil.
       
     8 **    May you find forgiveness for yourself and forgive others.
       
     9 **    May you share freely, never taking more than you give.
       
    10 **
       
    11 *************************************************************************
       
    12 ** This module contains C code that generates VDBE code used to process
       
    13 ** the WHERE clause of SQL statements.  This module is responsible for
       
    14 ** generating the code that loops through a table looking for applicable
       
    15 ** rows.  Indices are selected and used to speed the search when doing
       
    16 ** so is applicable.  Because this module is responsible for selecting
       
    17 ** indices, you might also think of this module as the "query optimizer".
       
    18 **
       
    19 ** $Id: where.c,v 1.326 2008/10/11 16:47:36 drh Exp $
       
    20 */
       
    21 #include "sqliteInt.h"
       
    22 
       
    23 /*
       
    24 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
       
    25 */
       
    26 #define BMS  (sizeof(Bitmask)*8)
       
    27 
       
    28 /*
       
    29 ** Trace output macros
       
    30 */
       
    31 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
       
    32 int sqlite3WhereTrace = 0;
       
    33 #endif
       
    34 #if 0
       
    35 # define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
       
    36 #else
       
    37 # define WHERETRACE(X)
       
    38 #endif
       
    39 
       
    40 /* Forward reference
       
    41 */
       
    42 typedef struct WhereClause WhereClause;
       
    43 typedef struct ExprMaskSet ExprMaskSet;
       
    44 
       
    45 /*
       
    46 ** The query generator uses an array of instances of this structure to
       
    47 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
       
    48 ** clause subexpression is separated from the others by an AND operator.
       
    49 **
       
    50 ** All WhereTerms are collected into a single WhereClause structure.  
       
    51 ** The following identity holds:
       
    52 **
       
    53 **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
       
    54 **
       
    55 ** When a term is of the form:
       
    56 **
       
    57 **              X <op> <expr>
       
    58 **
       
    59 ** where X is a column name and <op> is one of certain operators,
       
    60 ** then WhereTerm.leftCursor and WhereTerm.leftColumn record the
       
    61 ** cursor number and column number for X.  WhereTerm.operator records
       
    62 ** the <op> using a bitmask encoding defined by WO_xxx below.  The
       
    63 ** use of a bitmask encoding for the operator allows us to search
       
    64 ** quickly for terms that match any of several different operators.
       
    65 **
       
    66 ** prereqRight and prereqAll record sets of cursor numbers,
       
    67 ** but they do so indirectly.  A single ExprMaskSet structure translates
       
    68 ** cursor number into bits and the translated bit is stored in the prereq
       
    69 ** fields.  The translation is used in order to maximize the number of
       
    70 ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
       
    71 ** spread out over the non-negative integers.  For example, the cursor
       
    72 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The ExprMaskSet
       
    73 ** translates these sparse cursor numbers into consecutive integers
       
    74 ** beginning with 0 in order to make the best possible use of the available
       
    75 ** bits in the Bitmask.  So, in the example above, the cursor numbers
       
    76 ** would be mapped into integers 0 through 7.
       
    77 */
       
    78 typedef struct WhereTerm WhereTerm;
       
    79 struct WhereTerm {
       
    80   Expr *pExpr;            /* Pointer to the subexpression */
       
    81   i16 iParent;            /* Disable pWC->a[iParent] when this term disabled */
       
    82   i16 leftCursor;         /* Cursor number of X in "X <op> <expr>" */
       
    83   i16 leftColumn;         /* Column number of X in "X <op> <expr>" */
       
    84   u16 eOperator;          /* A WO_xx value describing <op> */
       
    85   u8 flags;               /* Bit flags.  See below */
       
    86   u8 nChild;              /* Number of children that must disable us */
       
    87   WhereClause *pWC;       /* The clause this term is part of */
       
    88   Bitmask prereqRight;    /* Bitmask of tables used by pRight */
       
    89   Bitmask prereqAll;      /* Bitmask of tables referenced by p */
       
    90 };
       
    91 
       
    92 /*
       
    93 ** Allowed values of WhereTerm.flags
       
    94 */
       
    95 #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
       
    96 #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
       
    97 #define TERM_CODED      0x04   /* This term is already coded */
       
    98 #define TERM_COPIED     0x08   /* Has a child */
       
    99 #define TERM_OR_OK      0x10   /* Used during OR-clause processing */
       
   100 
       
   101 /*
       
   102 ** An instance of the following structure holds all information about a
       
   103 ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
       
   104 */
       
   105 struct WhereClause {
       
   106   Parse *pParse;           /* The parser context */
       
   107   ExprMaskSet *pMaskSet;   /* Mapping of table indices to bitmasks */
       
   108   int nTerm;               /* Number of terms */
       
   109   int nSlot;               /* Number of entries in a[] */
       
   110   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
       
   111   WhereTerm aStatic[10];   /* Initial static space for a[] */
       
   112 };
       
   113 
       
   114 /*
       
   115 ** An instance of the following structure keeps track of a mapping
       
   116 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
       
   117 **
       
   118 ** The VDBE cursor numbers are small integers contained in 
       
   119 ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE 
       
   120 ** clause, the cursor numbers might not begin with 0 and they might
       
   121 ** contain gaps in the numbering sequence.  But we want to make maximum
       
   122 ** use of the bits in our bitmasks.  This structure provides a mapping
       
   123 ** from the sparse cursor numbers into consecutive integers beginning
       
   124 ** with 0.
       
   125 **
       
   126 ** If ExprMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
       
   127 ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
       
   128 **
       
   129 ** For example, if the WHERE clause expression used these VDBE
       
   130 ** cursors:  4, 5, 8, 29, 57, 73.  Then the  ExprMaskSet structure
       
   131 ** would map those cursor numbers into bits 0 through 5.
       
   132 **
       
   133 ** Note that the mapping is not necessarily ordered.  In the example
       
   134 ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
       
   135 ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
       
   136 ** does not really matter.  What is important is that sparse cursor
       
   137 ** numbers all get mapped into bit numbers that begin with 0 and contain
       
   138 ** no gaps.
       
   139 */
       
   140 struct ExprMaskSet {
       
   141   int n;                        /* Number of assigned cursor values */
       
   142   int ix[sizeof(Bitmask)*8];    /* Cursor assigned to each bit */
       
   143 };
       
   144 
       
   145 
       
   146 /*
       
   147 ** Bitmasks for the operators that indices are able to exploit.  An
       
   148 ** OR-ed combination of these values can be used when searching for
       
   149 ** terms in the where clause.
       
   150 */
       
   151 #define WO_IN     1
       
   152 #define WO_EQ     2
       
   153 #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
       
   154 #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
       
   155 #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
       
   156 #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
       
   157 #define WO_MATCH  64
       
   158 #define WO_ISNULL 128
       
   159 
       
   160 /*
       
   161 ** Value for flags returned by bestIndex().  
       
   162 **
       
   163 ** The least significant byte is reserved as a mask for WO_ values above.
       
   164 ** The WhereLevel.flags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
       
   165 ** But if the table is the right table of a left join, WhereLevel.flags
       
   166 ** is set to WO_IN|WO_EQ.  The WhereLevel.flags field can then be used as
       
   167 ** the "op" parameter to findTerm when we are resolving equality constraints.
       
   168 ** ISNULL constraints will then not be used on the right table of a left
       
   169 ** join.  Tickets #2177 and #2189.
       
   170 */
       
   171 #define WHERE_ROWID_EQ     0x000100   /* rowid=EXPR or rowid IN (...) */
       
   172 #define WHERE_ROWID_RANGE  0x000200   /* rowid<EXPR and/or rowid>EXPR */
       
   173 #define WHERE_COLUMN_EQ    0x001000   /* x=EXPR or x IN (...) */
       
   174 #define WHERE_COLUMN_RANGE 0x002000   /* x<EXPR and/or x>EXPR */
       
   175 #define WHERE_COLUMN_IN    0x004000   /* x IN (...) */
       
   176 #define WHERE_TOP_LIMIT    0x010000   /* x<EXPR or x<=EXPR constraint */
       
   177 #define WHERE_BTM_LIMIT    0x020000   /* x>EXPR or x>=EXPR constraint */
       
   178 #define WHERE_IDX_ONLY     0x080000   /* Use index only - omit table */
       
   179 #define WHERE_ORDERBY      0x100000   /* Output will appear in correct order */
       
   180 #define WHERE_REVERSE      0x200000   /* Scan in reverse order */
       
   181 #define WHERE_UNIQUE       0x400000   /* Selects no more than one row */
       
   182 #define WHERE_VIRTUALTABLE 0x800000   /* Use virtual-table processing */
       
   183 
       
   184 /*
       
   185 ** Initialize a preallocated WhereClause structure.
       
   186 */
       
   187 static void whereClauseInit(
       
   188   WhereClause *pWC,        /* The WhereClause to be initialized */
       
   189   Parse *pParse,           /* The parsing context */
       
   190   ExprMaskSet *pMaskSet    /* Mapping from table indices to bitmasks */
       
   191 ){
       
   192   pWC->pParse = pParse;
       
   193   pWC->pMaskSet = pMaskSet;
       
   194   pWC->nTerm = 0;
       
   195   pWC->nSlot = ArraySize(pWC->aStatic);
       
   196   pWC->a = pWC->aStatic;
       
   197 }
       
   198 
       
   199 /*
       
   200 ** Deallocate a WhereClause structure.  The WhereClause structure
       
   201 ** itself is not freed.  This routine is the inverse of whereClauseInit().
       
   202 */
       
   203 static void whereClauseClear(WhereClause *pWC){
       
   204   int i;
       
   205   WhereTerm *a;
       
   206   sqlite3 *db = pWC->pParse->db;
       
   207   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
       
   208     if( a->flags & TERM_DYNAMIC ){
       
   209       sqlite3ExprDelete(db, a->pExpr);
       
   210     }
       
   211   }
       
   212   if( pWC->a!=pWC->aStatic ){
       
   213     sqlite3DbFree(db, pWC->a);
       
   214   }
       
   215 }
       
   216 
       
   217 /*
       
   218 ** Add a new entries to the WhereClause structure.  Increase the allocated
       
   219 ** space as necessary.
       
   220 **
       
   221 ** If the flags argument includes TERM_DYNAMIC, then responsibility
       
   222 ** for freeing the expression p is assumed by the WhereClause object.
       
   223 **
       
   224 ** WARNING:  This routine might reallocate the space used to store
       
   225 ** WhereTerms.  All pointers to WhereTerms should be invalidated after
       
   226 ** calling this routine.  Such pointers may be reinitialized by referencing
       
   227 ** the pWC->a[] array.
       
   228 */
       
   229 static int whereClauseInsert(WhereClause *pWC, Expr *p, int flags){
       
   230   WhereTerm *pTerm;
       
   231   int idx;
       
   232   if( pWC->nTerm>=pWC->nSlot ){
       
   233     WhereTerm *pOld = pWC->a;
       
   234     sqlite3 *db = pWC->pParse->db;
       
   235     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
       
   236     if( pWC->a==0 ){
       
   237       if( flags & TERM_DYNAMIC ){
       
   238         sqlite3ExprDelete(db, p);
       
   239       }
       
   240       pWC->a = pOld;
       
   241       return 0;
       
   242     }
       
   243     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
       
   244     if( pOld!=pWC->aStatic ){
       
   245       sqlite3DbFree(db, pOld);
       
   246     }
       
   247     pWC->nSlot *= 2;
       
   248   }
       
   249   pTerm = &pWC->a[idx = pWC->nTerm];
       
   250   pWC->nTerm++;
       
   251   pTerm->pExpr = p;
       
   252   pTerm->flags = flags;
       
   253   pTerm->pWC = pWC;
       
   254   pTerm->iParent = -1;
       
   255   return idx;
       
   256 }
       
   257 
       
   258 /*
       
   259 ** This routine identifies subexpressions in the WHERE clause where
       
   260 ** each subexpression is separated by the AND operator or some other
       
   261 ** operator specified in the op parameter.  The WhereClause structure
       
   262 ** is filled with pointers to subexpressions.  For example:
       
   263 **
       
   264 **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
       
   265 **           \________/     \_______________/     \________________/
       
   266 **            slot[0]            slot[1]               slot[2]
       
   267 **
       
   268 ** The original WHERE clause in pExpr is unaltered.  All this routine
       
   269 ** does is make slot[] entries point to substructure within pExpr.
       
   270 **
       
   271 ** In the previous sentence and in the diagram, "slot[]" refers to
       
   272 ** the WhereClause.a[] array.  This array grows as needed to contain
       
   273 ** all terms of the WHERE clause.
       
   274 */
       
   275 static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
       
   276   if( pExpr==0 ) return;
       
   277   if( pExpr->op!=op ){
       
   278     whereClauseInsert(pWC, pExpr, 0);
       
   279   }else{
       
   280     whereSplit(pWC, pExpr->pLeft, op);
       
   281     whereSplit(pWC, pExpr->pRight, op);
       
   282   }
       
   283 }
       
   284 
       
   285 /*
       
   286 ** Initialize an expression mask set
       
   287 */
       
   288 #define initMaskSet(P)  memset(P, 0, sizeof(*P))
       
   289 
       
   290 /*
       
   291 ** Return the bitmask for the given cursor number.  Return 0 if
       
   292 ** iCursor is not in the set.
       
   293 */
       
   294 static Bitmask getMask(ExprMaskSet *pMaskSet, int iCursor){
       
   295   int i;
       
   296   for(i=0; i<pMaskSet->n; i++){
       
   297     if( pMaskSet->ix[i]==iCursor ){
       
   298       return ((Bitmask)1)<<i;
       
   299     }
       
   300   }
       
   301   return 0;
       
   302 }
       
   303 
       
   304 /*
       
   305 ** Create a new mask for cursor iCursor.
       
   306 **
       
   307 ** There is one cursor per table in the FROM clause.  The number of
       
   308 ** tables in the FROM clause is limited by a test early in the
       
   309 ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
       
   310 ** array will never overflow.
       
   311 */
       
   312 static void createMask(ExprMaskSet *pMaskSet, int iCursor){
       
   313   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
       
   314   pMaskSet->ix[pMaskSet->n++] = iCursor;
       
   315 }
       
   316 
       
   317 /*
       
   318 ** This routine walks (recursively) an expression tree and generates
       
   319 ** a bitmask indicating which tables are used in that expression
       
   320 ** tree.
       
   321 **
       
   322 ** In order for this routine to work, the calling function must have
       
   323 ** previously invoked sqlite3ResolveExprNames() on the expression.  See
       
   324 ** the header comment on that routine for additional information.
       
   325 ** The sqlite3ResolveExprNames() routines looks for column names and
       
   326 ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
       
   327 ** the VDBE cursor number of the table.  This routine just has to
       
   328 ** translate the cursor numbers into bitmask values and OR all
       
   329 ** the bitmasks together.
       
   330 */
       
   331 static Bitmask exprListTableUsage(ExprMaskSet*, ExprList*);
       
   332 static Bitmask exprSelectTableUsage(ExprMaskSet*, Select*);
       
   333 static Bitmask exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
       
   334   Bitmask mask = 0;
       
   335   if( p==0 ) return 0;
       
   336   if( p->op==TK_COLUMN ){
       
   337     mask = getMask(pMaskSet, p->iTable);
       
   338     return mask;
       
   339   }
       
   340   mask = exprTableUsage(pMaskSet, p->pRight);
       
   341   mask |= exprTableUsage(pMaskSet, p->pLeft);
       
   342   mask |= exprListTableUsage(pMaskSet, p->pList);
       
   343   mask |= exprSelectTableUsage(pMaskSet, p->pSelect);
       
   344   return mask;
       
   345 }
       
   346 static Bitmask exprListTableUsage(ExprMaskSet *pMaskSet, ExprList *pList){
       
   347   int i;
       
   348   Bitmask mask = 0;
       
   349   if( pList ){
       
   350     for(i=0; i<pList->nExpr; i++){
       
   351       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
       
   352     }
       
   353   }
       
   354   return mask;
       
   355 }
       
   356 static Bitmask exprSelectTableUsage(ExprMaskSet *pMaskSet, Select *pS){
       
   357   Bitmask mask = 0;
       
   358   while( pS ){
       
   359     mask |= exprListTableUsage(pMaskSet, pS->pEList);
       
   360     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
       
   361     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
       
   362     mask |= exprTableUsage(pMaskSet, pS->pWhere);
       
   363     mask |= exprTableUsage(pMaskSet, pS->pHaving);
       
   364     pS = pS->pPrior;
       
   365   }
       
   366   return mask;
       
   367 }
       
   368 
       
   369 /*
       
   370 ** Return TRUE if the given operator is one of the operators that is
       
   371 ** allowed for an indexable WHERE clause term.  The allowed operators are
       
   372 ** "=", "<", ">", "<=", ">=", and "IN".
       
   373 */
       
   374 static int allowedOp(int op){
       
   375   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
       
   376   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
       
   377   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
       
   378   assert( TK_GE==TK_EQ+4 );
       
   379   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
       
   380 }
       
   381 
       
   382 /*
       
   383 ** Swap two objects of type T.
       
   384 */
       
   385 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
       
   386 
       
   387 /*
       
   388 ** Commute a comparison operator.  Expressions of the form "X op Y"
       
   389 ** are converted into "Y op X".
       
   390 **
       
   391 ** If a collation sequence is associated with either the left or right
       
   392 ** side of the comparison, it remains associated with the same side after
       
   393 ** the commutation. So "Y collate NOCASE op X" becomes 
       
   394 ** "X collate NOCASE op Y". This is because any collation sequence on
       
   395 ** the left hand side of a comparison overrides any collation sequence 
       
   396 ** attached to the right. For the same reason the EP_ExpCollate flag
       
   397 ** is not commuted.
       
   398 */
       
   399 static void exprCommute(Parse *pParse, Expr *pExpr){
       
   400   u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
       
   401   u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
       
   402   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
       
   403   pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
       
   404   pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
       
   405   SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
       
   406   pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
       
   407   pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
       
   408   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
       
   409   if( pExpr->op>=TK_GT ){
       
   410     assert( TK_LT==TK_GT+2 );
       
   411     assert( TK_GE==TK_LE+2 );
       
   412     assert( TK_GT>TK_EQ );
       
   413     assert( TK_GT<TK_LE );
       
   414     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
       
   415     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
       
   416   }
       
   417 }
       
   418 
       
   419 /*
       
   420 ** Translate from TK_xx operator to WO_xx bitmask.
       
   421 */
       
   422 static int operatorMask(int op){
       
   423   int c;
       
   424   assert( allowedOp(op) );
       
   425   if( op==TK_IN ){
       
   426     c = WO_IN;
       
   427   }else if( op==TK_ISNULL ){
       
   428     c = WO_ISNULL;
       
   429   }else{
       
   430     c = WO_EQ<<(op-TK_EQ);
       
   431   }
       
   432   assert( op!=TK_ISNULL || c==WO_ISNULL );
       
   433   assert( op!=TK_IN || c==WO_IN );
       
   434   assert( op!=TK_EQ || c==WO_EQ );
       
   435   assert( op!=TK_LT || c==WO_LT );
       
   436   assert( op!=TK_LE || c==WO_LE );
       
   437   assert( op!=TK_GT || c==WO_GT );
       
   438   assert( op!=TK_GE || c==WO_GE );
       
   439   return c;
       
   440 }
       
   441 
       
   442 /*
       
   443 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
       
   444 ** where X is a reference to the iColumn of table iCur and <op> is one of
       
   445 ** the WO_xx operator codes specified by the op parameter.
       
   446 ** Return a pointer to the term.  Return 0 if not found.
       
   447 */
       
   448 static WhereTerm *findTerm(
       
   449   WhereClause *pWC,     /* The WHERE clause to be searched */
       
   450   int iCur,             /* Cursor number of LHS */
       
   451   int iColumn,          /* Column number of LHS */
       
   452   Bitmask notReady,     /* RHS must not overlap with this mask */
       
   453   u16 op,               /* Mask of WO_xx values describing operator */
       
   454   Index *pIdx           /* Must be compatible with this index, if not NULL */
       
   455 ){
       
   456   WhereTerm *pTerm;
       
   457   int k;
       
   458   assert( iCur>=0 );
       
   459   for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
       
   460     if( pTerm->leftCursor==iCur
       
   461        && (pTerm->prereqRight & notReady)==0
       
   462        && pTerm->leftColumn==iColumn
       
   463        && (pTerm->eOperator & op)!=0
       
   464     ){
       
   465       if( pIdx && pTerm->eOperator!=WO_ISNULL ){
       
   466         Expr *pX = pTerm->pExpr;
       
   467         CollSeq *pColl;
       
   468         char idxaff;
       
   469         int j;
       
   470         Parse *pParse = pWC->pParse;
       
   471 
       
   472         idxaff = pIdx->pTable->aCol[iColumn].affinity;
       
   473         if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
       
   474 
       
   475         /* Figure out the collation sequence required from an index for
       
   476         ** it to be useful for optimising expression pX. Store this
       
   477         ** value in variable pColl.
       
   478         */
       
   479         assert(pX->pLeft);
       
   480         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
       
   481         if( !pColl ){
       
   482           pColl = pParse->db->pDfltColl;
       
   483         }
       
   484 
       
   485         for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
       
   486           if( NEVER(j>=pIdx->nColumn) ) return 0;
       
   487         }
       
   488         if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
       
   489       }
       
   490       return pTerm;
       
   491     }
       
   492   }
       
   493   return 0;
       
   494 }
       
   495 
       
   496 /* Forward reference */
       
   497 static void exprAnalyze(SrcList*, WhereClause*, int);
       
   498 
       
   499 /*
       
   500 ** Call exprAnalyze on all terms in a WHERE clause.  
       
   501 **
       
   502 **
       
   503 */
       
   504 static void exprAnalyzeAll(
       
   505   SrcList *pTabList,       /* the FROM clause */
       
   506   WhereClause *pWC         /* the WHERE clause to be analyzed */
       
   507 ){
       
   508   int i;
       
   509   for(i=pWC->nTerm-1; i>=0; i--){
       
   510     exprAnalyze(pTabList, pWC, i);
       
   511   }
       
   512 }
       
   513 
       
   514 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
       
   515 /*
       
   516 ** Check to see if the given expression is a LIKE or GLOB operator that
       
   517 ** can be optimized using inequality constraints.  Return TRUE if it is
       
   518 ** so and false if not.
       
   519 **
       
   520 ** In order for the operator to be optimizible, the RHS must be a string
       
   521 ** literal that does not begin with a wildcard.  
       
   522 */
       
   523 static int isLikeOrGlob(
       
   524   Parse *pParse,    /* Parsing and code generating context */
       
   525   Expr *pExpr,      /* Test this expression */
       
   526   int *pnPattern,   /* Number of non-wildcard prefix characters */
       
   527   int *pisComplete, /* True if the only wildcard is % in the last character */
       
   528   int *pnoCase      /* True if uppercase is equivalent to lowercase */
       
   529 ){
       
   530   const char *z;
       
   531   Expr *pRight, *pLeft;
       
   532   ExprList *pList;
       
   533   int c, cnt;
       
   534   char wc[3];
       
   535   CollSeq *pColl;
       
   536   sqlite3 *db = pParse->db;
       
   537 
       
   538   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
       
   539     return 0;
       
   540   }
       
   541 #ifdef SQLITE_EBCDIC
       
   542   if( *pnoCase ) return 0;
       
   543 #endif
       
   544   pList = pExpr->pList;
       
   545   pRight = pList->a[0].pExpr;
       
   546   if( pRight->op!=TK_STRING
       
   547    && (pRight->op!=TK_REGISTER || pRight->iColumn!=TK_STRING) ){
       
   548     return 0;
       
   549   }
       
   550   pLeft = pList->a[1].pExpr;
       
   551   if( pLeft->op!=TK_COLUMN ){
       
   552     return 0;
       
   553   }
       
   554   pColl = sqlite3ExprCollSeq(pParse, pLeft);
       
   555   assert( pColl!=0 || pLeft->iColumn==-1 );
       
   556   if( pColl==0 ){
       
   557     /* No collation is defined for the ROWID.  Use the default. */
       
   558     pColl = db->pDfltColl;
       
   559   }
       
   560   if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
       
   561       (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
       
   562     return 0;
       
   563   }
       
   564   sqlite3DequoteExpr(db, pRight);
       
   565   z = (char *)pRight->token.z;
       
   566   cnt = 0;
       
   567   if( z ){
       
   568     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ cnt++; }
       
   569   }
       
   570   if( cnt==0 || 255==(u8)z[cnt] ){
       
   571     return 0;
       
   572   }
       
   573   *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
       
   574   *pnPattern = cnt;
       
   575   return 1;
       
   576 }
       
   577 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
       
   578 
       
   579 
       
   580 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
   581 /*
       
   582 ** Check to see if the given expression is of the form
       
   583 **
       
   584 **         column MATCH expr
       
   585 **
       
   586 ** If it is then return TRUE.  If not, return FALSE.
       
   587 */
       
   588 static int isMatchOfColumn(
       
   589   Expr *pExpr      /* Test this expression */
       
   590 ){
       
   591   ExprList *pList;
       
   592 
       
   593   if( pExpr->op!=TK_FUNCTION ){
       
   594     return 0;
       
   595   }
       
   596   if( pExpr->token.n!=5 ||
       
   597        sqlite3StrNICmp((const char*)pExpr->token.z,"match",5)!=0 ){
       
   598     return 0;
       
   599   }
       
   600   pList = pExpr->pList;
       
   601   if( pList->nExpr!=2 ){
       
   602     return 0;
       
   603   }
       
   604   if( pList->a[1].pExpr->op != TK_COLUMN ){
       
   605     return 0;
       
   606   }
       
   607   return 1;
       
   608 }
       
   609 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
   610 
       
   611 /*
       
   612 ** If the pBase expression originated in the ON or USING clause of
       
   613 ** a join, then transfer the appropriate markings over to derived.
       
   614 */
       
   615 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
       
   616   pDerived->flags |= pBase->flags & EP_FromJoin;
       
   617   pDerived->iRightJoinTable = pBase->iRightJoinTable;
       
   618 }
       
   619 
       
   620 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
       
   621 /*
       
   622 ** Return TRUE if the given term of an OR clause can be converted
       
   623 ** into an IN clause.  The iCursor and iColumn define the left-hand
       
   624 ** side of the IN clause.
       
   625 **
       
   626 ** The context is that we have multiple OR-connected equality terms
       
   627 ** like this:
       
   628 **
       
   629 **           a=<expr1> OR  a=<expr2> OR b=<expr3>  OR ...
       
   630 **
       
   631 ** The pOrTerm input to this routine corresponds to a single term of
       
   632 ** this OR clause.  In order for the term to be a candidate for
       
   633 ** conversion to an IN operator, the following must be true:
       
   634 **
       
   635 **     *  The left-hand side of the term must be the column which
       
   636 **        is identified by iCursor and iColumn.
       
   637 **
       
   638 **     *  If the right-hand side is also a column, then the affinities
       
   639 **        of both right and left sides must be such that no type
       
   640 **        conversions are required on the right.  (Ticket #2249)
       
   641 **
       
   642 ** If both of these conditions are true, then return true.  Otherwise
       
   643 ** return false.
       
   644 */
       
   645 static int orTermIsOptCandidate(WhereTerm *pOrTerm, int iCursor, int iColumn){
       
   646   int affLeft, affRight;
       
   647   assert( pOrTerm->eOperator==WO_EQ );
       
   648   if( pOrTerm->leftCursor!=iCursor ){
       
   649     return 0;
       
   650   }
       
   651   if( pOrTerm->leftColumn!=iColumn ){
       
   652     return 0;
       
   653   }
       
   654   affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
       
   655   if( affRight==0 ){
       
   656     return 1;
       
   657   }
       
   658   affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
       
   659   if( affRight!=affLeft ){
       
   660     return 0;
       
   661   }
       
   662   return 1;
       
   663 }
       
   664 
       
   665 /*
       
   666 ** Return true if the given term of an OR clause can be ignored during
       
   667 ** a check to make sure all OR terms are candidates for optimization.
       
   668 ** In other words, return true if a call to the orTermIsOptCandidate()
       
   669 ** above returned false but it is not necessary to disqualify the
       
   670 ** optimization.
       
   671 **
       
   672 ** Suppose the original OR phrase was this:
       
   673 **
       
   674 **           a=4  OR  a=11  OR  a=b
       
   675 **
       
   676 ** During analysis, the third term gets flipped around and duplicate
       
   677 ** so that we are left with this:
       
   678 **
       
   679 **           a=4  OR  a=11  OR  a=b  OR  b=a
       
   680 **
       
   681 ** Since the last two terms are duplicates, only one of them
       
   682 ** has to qualify in order for the whole phrase to qualify.  When
       
   683 ** this routine is called, we know that pOrTerm did not qualify.
       
   684 ** This routine merely checks to see if pOrTerm has a duplicate that
       
   685 ** might qualify.  If there is a duplicate that has not yet been
       
   686 ** disqualified, then return true.  If there are no duplicates, or
       
   687 ** the duplicate has also been disqualified, return false.
       
   688 */
       
   689 static int orTermHasOkDuplicate(WhereClause *pOr, WhereTerm *pOrTerm){
       
   690   if( pOrTerm->flags & TERM_COPIED ){
       
   691     /* This is the original term.  The duplicate is to the left had
       
   692     ** has not yet been analyzed and thus has not yet been disqualified. */
       
   693     return 1;
       
   694   }
       
   695   if( (pOrTerm->flags & TERM_VIRTUAL)!=0
       
   696      && (pOr->a[pOrTerm->iParent].flags & TERM_OR_OK)!=0 ){
       
   697     /* This is a duplicate term.  The original qualified so this one
       
   698     ** does not have to. */
       
   699     return 1;
       
   700   }
       
   701   /* This is either a singleton term or else it is a duplicate for
       
   702   ** which the original did not qualify.  Either way we are done for. */
       
   703   return 0;
       
   704 }
       
   705 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
       
   706 
       
   707 /*
       
   708 ** The input to this routine is an WhereTerm structure with only the
       
   709 ** "pExpr" field filled in.  The job of this routine is to analyze the
       
   710 ** subexpression and populate all the other fields of the WhereTerm
       
   711 ** structure.
       
   712 **
       
   713 ** If the expression is of the form "<expr> <op> X" it gets commuted
       
   714 ** to the standard form of "X <op> <expr>".  If the expression is of
       
   715 ** the form "X <op> Y" where both X and Y are columns, then the original
       
   716 ** expression is unchanged and a new virtual expression of the form
       
   717 ** "Y <op> X" is added to the WHERE clause and analyzed separately.
       
   718 */
       
   719 static void exprAnalyze(
       
   720   SrcList *pSrc,            /* the FROM clause */
       
   721   WhereClause *pWC,         /* the WHERE clause */
       
   722   int idxTerm               /* Index of the term to be analyzed */
       
   723 ){
       
   724   WhereTerm *pTerm;
       
   725   ExprMaskSet *pMaskSet;
       
   726   Expr *pExpr;
       
   727   Bitmask prereqLeft;
       
   728   Bitmask prereqAll;
       
   729   Bitmask extraRight = 0;
       
   730   int nPattern;
       
   731   int isComplete;
       
   732   int noCase;
       
   733   int op;
       
   734   Parse *pParse = pWC->pParse;
       
   735   sqlite3 *db = pParse->db;
       
   736 
       
   737   if( db->mallocFailed ){
       
   738     return;
       
   739   }
       
   740   pTerm = &pWC->a[idxTerm];
       
   741   pMaskSet = pWC->pMaskSet;
       
   742   pExpr = pTerm->pExpr;
       
   743   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
       
   744   op = pExpr->op;
       
   745   if( op==TK_IN ){
       
   746     assert( pExpr->pRight==0 );
       
   747     pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->pList)
       
   748                           | exprSelectTableUsage(pMaskSet, pExpr->pSelect);
       
   749   }else if( op==TK_ISNULL ){
       
   750     pTerm->prereqRight = 0;
       
   751   }else{
       
   752     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
       
   753   }
       
   754   prereqAll = exprTableUsage(pMaskSet, pExpr);
       
   755   if( ExprHasProperty(pExpr, EP_FromJoin) ){
       
   756     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
       
   757     prereqAll |= x;
       
   758     extraRight = x-1;  /* ON clause terms may not be used with an index
       
   759                        ** on left table of a LEFT JOIN.  Ticket #3015 */
       
   760   }
       
   761   pTerm->prereqAll = prereqAll;
       
   762   pTerm->leftCursor = -1;
       
   763   pTerm->iParent = -1;
       
   764   pTerm->eOperator = 0;
       
   765   if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){
       
   766     Expr *pLeft = pExpr->pLeft;
       
   767     Expr *pRight = pExpr->pRight;
       
   768     if( pLeft->op==TK_COLUMN ){
       
   769       pTerm->leftCursor = pLeft->iTable;
       
   770       pTerm->leftColumn = pLeft->iColumn;
       
   771       pTerm->eOperator = operatorMask(op);
       
   772     }
       
   773     if( pRight && pRight->op==TK_COLUMN ){
       
   774       WhereTerm *pNew;
       
   775       Expr *pDup;
       
   776       if( pTerm->leftCursor>=0 ){
       
   777         int idxNew;
       
   778         pDup = sqlite3ExprDup(db, pExpr);
       
   779         if( db->mallocFailed ){
       
   780           sqlite3ExprDelete(db, pDup);
       
   781           return;
       
   782         }
       
   783         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
       
   784         if( idxNew==0 ) return;
       
   785         pNew = &pWC->a[idxNew];
       
   786         pNew->iParent = idxTerm;
       
   787         pTerm = &pWC->a[idxTerm];
       
   788         pTerm->nChild = 1;
       
   789         pTerm->flags |= TERM_COPIED;
       
   790       }else{
       
   791         pDup = pExpr;
       
   792         pNew = pTerm;
       
   793       }
       
   794       exprCommute(pParse, pDup);
       
   795       pLeft = pDup->pLeft;
       
   796       pNew->leftCursor = pLeft->iTable;
       
   797       pNew->leftColumn = pLeft->iColumn;
       
   798       pNew->prereqRight = prereqLeft;
       
   799       pNew->prereqAll = prereqAll;
       
   800       pNew->eOperator = operatorMask(pDup->op);
       
   801     }
       
   802   }
       
   803 
       
   804 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
       
   805   /* If a term is the BETWEEN operator, create two new virtual terms
       
   806   ** that define the range that the BETWEEN implements.
       
   807   */
       
   808   else if( pExpr->op==TK_BETWEEN ){
       
   809     ExprList *pList = pExpr->pList;
       
   810     int i;
       
   811     static const u8 ops[] = {TK_GE, TK_LE};
       
   812     assert( pList!=0 );
       
   813     assert( pList->nExpr==2 );
       
   814     for(i=0; i<2; i++){
       
   815       Expr *pNewExpr;
       
   816       int idxNew;
       
   817       pNewExpr = sqlite3Expr(db, ops[i], sqlite3ExprDup(db, pExpr->pLeft),
       
   818                              sqlite3ExprDup(db, pList->a[i].pExpr), 0);
       
   819       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
       
   820       exprAnalyze(pSrc, pWC, idxNew);
       
   821       pTerm = &pWC->a[idxTerm];
       
   822       pWC->a[idxNew].iParent = idxTerm;
       
   823     }
       
   824     pTerm->nChild = 2;
       
   825   }
       
   826 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
       
   827 
       
   828 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
       
   829   /* Attempt to convert OR-connected terms into an IN operator so that
       
   830   ** they can make use of indices.  Example:
       
   831   **
       
   832   **      x = expr1  OR  expr2 = x  OR  x = expr3
       
   833   **
       
   834   ** is converted into
       
   835   **
       
   836   **      x IN (expr1,expr2,expr3)
       
   837   **
       
   838   ** This optimization must be omitted if OMIT_SUBQUERY is defined because
       
   839   ** the compiler for the the IN operator is part of sub-queries.
       
   840   */
       
   841   else if( pExpr->op==TK_OR ){
       
   842     int ok;
       
   843     int i, j;
       
   844     int iColumn, iCursor;
       
   845     WhereClause sOr;
       
   846     WhereTerm *pOrTerm;
       
   847 
       
   848     assert( (pTerm->flags & TERM_DYNAMIC)==0 );
       
   849     whereClauseInit(&sOr, pWC->pParse, pMaskSet);
       
   850     whereSplit(&sOr, pExpr, TK_OR);
       
   851     exprAnalyzeAll(pSrc, &sOr);
       
   852     assert( sOr.nTerm>=2 );
       
   853     j = 0;
       
   854     if( db->mallocFailed ) goto or_not_possible;
       
   855     do{
       
   856       assert( j<sOr.nTerm );
       
   857       iColumn = sOr.a[j].leftColumn;
       
   858       iCursor = sOr.a[j].leftCursor;
       
   859       ok = iCursor>=0;
       
   860       for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
       
   861         if( pOrTerm->eOperator!=WO_EQ ){
       
   862           goto or_not_possible;
       
   863         }
       
   864         if( orTermIsOptCandidate(pOrTerm, iCursor, iColumn) ){
       
   865           pOrTerm->flags |= TERM_OR_OK;
       
   866         }else if( orTermHasOkDuplicate(&sOr, pOrTerm) ){
       
   867           pOrTerm->flags &= ~TERM_OR_OK;
       
   868         }else{
       
   869           ok = 0;
       
   870         }
       
   871       }
       
   872     }while( !ok && (sOr.a[j++].flags & TERM_COPIED)!=0 && j<2 );
       
   873     if( ok ){
       
   874       ExprList *pList = 0;
       
   875       Expr *pNew, *pDup;
       
   876       Expr *pLeft = 0;
       
   877       for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0; i--, pOrTerm++){
       
   878         if( (pOrTerm->flags & TERM_OR_OK)==0 ) continue;
       
   879         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight);
       
   880         pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup, 0);
       
   881         pLeft = pOrTerm->pExpr->pLeft;
       
   882       }
       
   883       assert( pLeft!=0 );
       
   884       pDup = sqlite3ExprDup(db, pLeft);
       
   885       pNew = sqlite3Expr(db, TK_IN, pDup, 0, 0);
       
   886       if( pNew ){
       
   887         int idxNew;
       
   888         transferJoinMarkings(pNew, pExpr);
       
   889         pNew->pList = pList;
       
   890         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
       
   891         exprAnalyze(pSrc, pWC, idxNew);
       
   892         pTerm = &pWC->a[idxTerm];
       
   893         pWC->a[idxNew].iParent = idxTerm;
       
   894         pTerm->nChild = 1;
       
   895       }else{
       
   896         sqlite3ExprListDelete(db, pList);
       
   897       }
       
   898     }
       
   899 or_not_possible:
       
   900     whereClauseClear(&sOr);
       
   901   }
       
   902 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
       
   903 
       
   904 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
       
   905   /* Add constraints to reduce the search space on a LIKE or GLOB
       
   906   ** operator.
       
   907   **
       
   908   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
       
   909   **
       
   910   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
       
   911   **
       
   912   ** The last character of the prefix "abc" is incremented to form the
       
   913   ** termination condition "abd".
       
   914   */
       
   915   if( isLikeOrGlob(pParse, pExpr, &nPattern, &isComplete, &noCase) ){
       
   916     Expr *pLeft, *pRight;
       
   917     Expr *pStr1, *pStr2;
       
   918     Expr *pNewExpr1, *pNewExpr2;
       
   919     int idxNew1, idxNew2;
       
   920 
       
   921     pLeft = pExpr->pList->a[1].pExpr;
       
   922     pRight = pExpr->pList->a[0].pExpr;
       
   923     pStr1 = sqlite3PExpr(pParse, TK_STRING, 0, 0, 0);
       
   924     if( pStr1 ){
       
   925       sqlite3TokenCopy(db, &pStr1->token, &pRight->token);
       
   926       pStr1->token.n = nPattern;
       
   927       pStr1->flags = EP_Dequoted;
       
   928     }
       
   929     pStr2 = sqlite3ExprDup(db, pStr1);
       
   930     if( !db->mallocFailed ){
       
   931       u8 c, *pC;
       
   932       assert( pStr2->token.dyn );
       
   933       pC = (u8*)&pStr2->token.z[nPattern-1];
       
   934       c = *pC;
       
   935       if( noCase ){
       
   936         if( c=='@' ) isComplete = 0;
       
   937         c = sqlite3UpperToLower[c];
       
   938       }
       
   939       *pC = c + 1;
       
   940     }
       
   941     pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft), pStr1, 0);
       
   942     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
       
   943     exprAnalyze(pSrc, pWC, idxNew1);
       
   944     pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft), pStr2, 0);
       
   945     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
       
   946     exprAnalyze(pSrc, pWC, idxNew2);
       
   947     pTerm = &pWC->a[idxTerm];
       
   948     if( isComplete ){
       
   949       pWC->a[idxNew1].iParent = idxTerm;
       
   950       pWC->a[idxNew2].iParent = idxTerm;
       
   951       pTerm->nChild = 2;
       
   952     }
       
   953   }
       
   954 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
       
   955 
       
   956 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
   957   /* Add a WO_MATCH auxiliary term to the constraint set if the
       
   958   ** current expression is of the form:  column MATCH expr.
       
   959   ** This information is used by the xBestIndex methods of
       
   960   ** virtual tables.  The native query optimizer does not attempt
       
   961   ** to do anything with MATCH functions.
       
   962   */
       
   963   if( isMatchOfColumn(pExpr) ){
       
   964     int idxNew;
       
   965     Expr *pRight, *pLeft;
       
   966     WhereTerm *pNewTerm;
       
   967     Bitmask prereqColumn, prereqExpr;
       
   968 
       
   969     pRight = pExpr->pList->a[0].pExpr;
       
   970     pLeft = pExpr->pList->a[1].pExpr;
       
   971     prereqExpr = exprTableUsage(pMaskSet, pRight);
       
   972     prereqColumn = exprTableUsage(pMaskSet, pLeft);
       
   973     if( (prereqExpr & prereqColumn)==0 ){
       
   974       Expr *pNewExpr;
       
   975       pNewExpr = sqlite3Expr(db, TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0);
       
   976       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
       
   977       pNewTerm = &pWC->a[idxNew];
       
   978       pNewTerm->prereqRight = prereqExpr;
       
   979       pNewTerm->leftCursor = pLeft->iTable;
       
   980       pNewTerm->leftColumn = pLeft->iColumn;
       
   981       pNewTerm->eOperator = WO_MATCH;
       
   982       pNewTerm->iParent = idxTerm;
       
   983       pTerm = &pWC->a[idxTerm];
       
   984       pTerm->nChild = 1;
       
   985       pTerm->flags |= TERM_COPIED;
       
   986       pNewTerm->prereqAll = pTerm->prereqAll;
       
   987     }
       
   988   }
       
   989 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
   990 
       
   991   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
       
   992   ** an index for tables to the left of the join.
       
   993   */
       
   994   pTerm->prereqRight |= extraRight;
       
   995 }
       
   996 
       
   997 /*
       
   998 ** Return TRUE if any of the expressions in pList->a[iFirst...] contain
       
   999 ** a reference to any table other than the iBase table.
       
  1000 */
       
  1001 static int referencesOtherTables(
       
  1002   ExprList *pList,          /* Search expressions in ths list */
       
  1003   ExprMaskSet *pMaskSet,    /* Mapping from tables to bitmaps */
       
  1004   int iFirst,               /* Be searching with the iFirst-th expression */
       
  1005   int iBase                 /* Ignore references to this table */
       
  1006 ){
       
  1007   Bitmask allowed = ~getMask(pMaskSet, iBase);
       
  1008   while( iFirst<pList->nExpr ){
       
  1009     if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
       
  1010       return 1;
       
  1011     }
       
  1012   }
       
  1013   return 0;
       
  1014 }
       
  1015 
       
  1016 
       
  1017 /*
       
  1018 ** This routine decides if pIdx can be used to satisfy the ORDER BY
       
  1019 ** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
       
  1020 ** ORDER BY clause, this routine returns 0.
       
  1021 **
       
  1022 ** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
       
  1023 ** left-most table in the FROM clause of that same SELECT statement and
       
  1024 ** the table has a cursor number of "base".  pIdx is an index on pTab.
       
  1025 **
       
  1026 ** nEqCol is the number of columns of pIdx that are used as equality
       
  1027 ** constraints.  Any of these columns may be missing from the ORDER BY
       
  1028 ** clause and the match can still be a success.
       
  1029 **
       
  1030 ** All terms of the ORDER BY that match against the index must be either
       
  1031 ** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
       
  1032 ** index do not need to satisfy this constraint.)  The *pbRev value is
       
  1033 ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
       
  1034 ** the ORDER BY clause is all ASC.
       
  1035 */
       
  1036 static int isSortingIndex(
       
  1037   Parse *pParse,          /* Parsing context */
       
  1038   ExprMaskSet *pMaskSet,  /* Mapping from table indices to bitmaps */
       
  1039   Index *pIdx,            /* The index we are testing */
       
  1040   int base,               /* Cursor number for the table to be sorted */
       
  1041   ExprList *pOrderBy,     /* The ORDER BY clause */
       
  1042   int nEqCol,             /* Number of index columns with == constraints */
       
  1043   int *pbRev              /* Set to 1 if ORDER BY is DESC */
       
  1044 ){
       
  1045   int i, j;                       /* Loop counters */
       
  1046   int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
       
  1047   int nTerm;                      /* Number of ORDER BY terms */
       
  1048   struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
       
  1049   sqlite3 *db = pParse->db;
       
  1050 
       
  1051   assert( pOrderBy!=0 );
       
  1052   nTerm = pOrderBy->nExpr;
       
  1053   assert( nTerm>0 );
       
  1054 
       
  1055   /* Match terms of the ORDER BY clause against columns of
       
  1056   ** the index.
       
  1057   **
       
  1058   ** Note that indices have pIdx->nColumn regular columns plus
       
  1059   ** one additional column containing the rowid.  The rowid column
       
  1060   ** of the index is also allowed to match against the ORDER BY
       
  1061   ** clause.
       
  1062   */
       
  1063   for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
       
  1064     Expr *pExpr;       /* The expression of the ORDER BY pTerm */
       
  1065     CollSeq *pColl;    /* The collating sequence of pExpr */
       
  1066     int termSortOrder; /* Sort order for this term */
       
  1067     int iColumn;       /* The i-th column of the index.  -1 for rowid */
       
  1068     int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
       
  1069     const char *zColl; /* Name of the collating sequence for i-th index term */
       
  1070 
       
  1071     pExpr = pTerm->pExpr;
       
  1072     if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
       
  1073       /* Can not use an index sort on anything that is not a column in the
       
  1074       ** left-most table of the FROM clause */
       
  1075       break;
       
  1076     }
       
  1077     pColl = sqlite3ExprCollSeq(pParse, pExpr);
       
  1078     if( !pColl ){
       
  1079       pColl = db->pDfltColl;
       
  1080     }
       
  1081     if( i<pIdx->nColumn ){
       
  1082       iColumn = pIdx->aiColumn[i];
       
  1083       if( iColumn==pIdx->pTable->iPKey ){
       
  1084         iColumn = -1;
       
  1085       }
       
  1086       iSortOrder = pIdx->aSortOrder[i];
       
  1087       zColl = pIdx->azColl[i];
       
  1088     }else{
       
  1089       iColumn = -1;
       
  1090       iSortOrder = 0;
       
  1091       zColl = pColl->zName;
       
  1092     }
       
  1093     if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){
       
  1094       /* Term j of the ORDER BY clause does not match column i of the index */
       
  1095       if( i<nEqCol ){
       
  1096         /* If an index column that is constrained by == fails to match an
       
  1097         ** ORDER BY term, that is OK.  Just ignore that column of the index
       
  1098         */
       
  1099         continue;
       
  1100       }else if( i==pIdx->nColumn ){
       
  1101         /* Index column i is the rowid.  All other terms match. */
       
  1102         break;
       
  1103       }else{
       
  1104         /* If an index column fails to match and is not constrained by ==
       
  1105         ** then the index cannot satisfy the ORDER BY constraint.
       
  1106         */
       
  1107         return 0;
       
  1108       }
       
  1109     }
       
  1110     assert( pIdx->aSortOrder!=0 );
       
  1111     assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
       
  1112     assert( iSortOrder==0 || iSortOrder==1 );
       
  1113     termSortOrder = iSortOrder ^ pTerm->sortOrder;
       
  1114     if( i>nEqCol ){
       
  1115       if( termSortOrder!=sortOrder ){
       
  1116         /* Indices can only be used if all ORDER BY terms past the
       
  1117         ** equality constraints are all either DESC or ASC. */
       
  1118         return 0;
       
  1119       }
       
  1120     }else{
       
  1121       sortOrder = termSortOrder;
       
  1122     }
       
  1123     j++;
       
  1124     pTerm++;
       
  1125     if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
       
  1126       /* If the indexed column is the primary key and everything matches
       
  1127       ** so far and none of the ORDER BY terms to the right reference other
       
  1128       ** tables in the join, then we are assured that the index can be used 
       
  1129       ** to sort because the primary key is unique and so none of the other
       
  1130       ** columns will make any difference
       
  1131       */
       
  1132       j = nTerm;
       
  1133     }
       
  1134   }
       
  1135 
       
  1136   *pbRev = sortOrder!=0;
       
  1137   if( j>=nTerm ){
       
  1138     /* All terms of the ORDER BY clause are covered by this index so
       
  1139     ** this index can be used for sorting. */
       
  1140     return 1;
       
  1141   }
       
  1142   if( pIdx->onError!=OE_None && i==pIdx->nColumn
       
  1143       && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
       
  1144     /* All terms of this index match some prefix of the ORDER BY clause
       
  1145     ** and the index is UNIQUE and no terms on the tail of the ORDER BY
       
  1146     ** clause reference other tables in a join.  If this is all true then
       
  1147     ** the order by clause is superfluous. */
       
  1148     return 1;
       
  1149   }
       
  1150   return 0;
       
  1151 }
       
  1152 
       
  1153 /*
       
  1154 ** Check table to see if the ORDER BY clause in pOrderBy can be satisfied
       
  1155 ** by sorting in order of ROWID.  Return true if so and set *pbRev to be
       
  1156 ** true for reverse ROWID and false for forward ROWID order.
       
  1157 */
       
  1158 static int sortableByRowid(
       
  1159   int base,               /* Cursor number for table to be sorted */
       
  1160   ExprList *pOrderBy,     /* The ORDER BY clause */
       
  1161   ExprMaskSet *pMaskSet,  /* Mapping from tables to bitmaps */
       
  1162   int *pbRev              /* Set to 1 if ORDER BY is DESC */
       
  1163 ){
       
  1164   Expr *p;
       
  1165 
       
  1166   assert( pOrderBy!=0 );
       
  1167   assert( pOrderBy->nExpr>0 );
       
  1168   p = pOrderBy->a[0].pExpr;
       
  1169   if( p->op==TK_COLUMN && p->iTable==base && p->iColumn==-1
       
  1170     && !referencesOtherTables(pOrderBy, pMaskSet, 1, base) ){
       
  1171     *pbRev = pOrderBy->a[0].sortOrder;
       
  1172     return 1;
       
  1173   }
       
  1174   return 0;
       
  1175 }
       
  1176 
       
  1177 /*
       
  1178 ** Prepare a crude estimate of the logarithm of the input value.
       
  1179 ** The results need not be exact.  This is only used for estimating
       
  1180 ** the total cost of performing operations with O(logN) or O(NlogN)
       
  1181 ** complexity.  Because N is just a guess, it is no great tragedy if
       
  1182 ** logN is a little off.
       
  1183 */
       
  1184 static double estLog(double N){
       
  1185   double logN = 1;
       
  1186   double x = 10;
       
  1187   while( N>x ){
       
  1188     logN += 1;
       
  1189     x *= 10;
       
  1190   }
       
  1191   return logN;
       
  1192 }
       
  1193 
       
  1194 /*
       
  1195 ** Two routines for printing the content of an sqlite3_index_info
       
  1196 ** structure.  Used for testing and debugging only.  If neither
       
  1197 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
       
  1198 ** are no-ops.
       
  1199 */
       
  1200 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
       
  1201 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
       
  1202   int i;
       
  1203   if( !sqlite3WhereTrace ) return;
       
  1204   for(i=0; i<p->nConstraint; i++){
       
  1205     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
       
  1206        i,
       
  1207        p->aConstraint[i].iColumn,
       
  1208        p->aConstraint[i].iTermOffset,
       
  1209        p->aConstraint[i].op,
       
  1210        p->aConstraint[i].usable);
       
  1211   }
       
  1212   for(i=0; i<p->nOrderBy; i++){
       
  1213     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
       
  1214        i,
       
  1215        p->aOrderBy[i].iColumn,
       
  1216        p->aOrderBy[i].desc);
       
  1217   }
       
  1218 }
       
  1219 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
       
  1220   int i;
       
  1221   if( !sqlite3WhereTrace ) return;
       
  1222   for(i=0; i<p->nConstraint; i++){
       
  1223     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
       
  1224        i,
       
  1225        p->aConstraintUsage[i].argvIndex,
       
  1226        p->aConstraintUsage[i].omit);
       
  1227   }
       
  1228   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
       
  1229   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
       
  1230   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
       
  1231   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
       
  1232 }
       
  1233 #else
       
  1234 #define TRACE_IDX_INPUTS(A)
       
  1235 #define TRACE_IDX_OUTPUTS(A)
       
  1236 #endif
       
  1237 
       
  1238 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  1239 /*
       
  1240 ** Compute the best index for a virtual table.
       
  1241 **
       
  1242 ** The best index is computed by the xBestIndex method of the virtual
       
  1243 ** table module.  This routine is really just a wrapper that sets up
       
  1244 ** the sqlite3_index_info structure that is used to communicate with
       
  1245 ** xBestIndex.
       
  1246 **
       
  1247 ** In a join, this routine might be called multiple times for the
       
  1248 ** same virtual table.  The sqlite3_index_info structure is created
       
  1249 ** and initialized on the first invocation and reused on all subsequent
       
  1250 ** invocations.  The sqlite3_index_info structure is also used when
       
  1251 ** code is generated to access the virtual table.  The whereInfoDelete() 
       
  1252 ** routine takes care of freeing the sqlite3_index_info structure after
       
  1253 ** everybody has finished with it.
       
  1254 */
       
  1255 static double bestVirtualIndex(
       
  1256   Parse *pParse,                 /* The parsing context */
       
  1257   WhereClause *pWC,              /* The WHERE clause */
       
  1258   struct SrcList_item *pSrc,     /* The FROM clause term to search */
       
  1259   Bitmask notReady,              /* Mask of cursors that are not available */
       
  1260   ExprList *pOrderBy,            /* The order by clause */
       
  1261   int orderByUsable,             /* True if we can potential sort */
       
  1262   sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
       
  1263 ){
       
  1264   Table *pTab = pSrc->pTab;
       
  1265   sqlite3_vtab *pVtab = pTab->pVtab;
       
  1266   sqlite3_index_info *pIdxInfo;
       
  1267   struct sqlite3_index_constraint *pIdxCons;
       
  1268   struct sqlite3_index_orderby *pIdxOrderBy;
       
  1269   struct sqlite3_index_constraint_usage *pUsage;
       
  1270   WhereTerm *pTerm;
       
  1271   int i, j;
       
  1272   int nOrderBy;
       
  1273   int rc;
       
  1274 
       
  1275   /* If the sqlite3_index_info structure has not been previously
       
  1276   ** allocated and initialized for this virtual table, then allocate
       
  1277   ** and initialize it now
       
  1278   */
       
  1279   pIdxInfo = *ppIdxInfo;
       
  1280   if( pIdxInfo==0 ){
       
  1281     WhereTerm *pTerm;
       
  1282     int nTerm;
       
  1283     WHERETRACE(("Recomputing index info for %s...\n", pTab->zName));
       
  1284 
       
  1285     /* Count the number of possible WHERE clause constraints referring
       
  1286     ** to this virtual table */
       
  1287     for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       
  1288       if( pTerm->leftCursor != pSrc->iCursor ) continue;
       
  1289       assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
       
  1290       testcase( pTerm->eOperator==WO_IN );
       
  1291       testcase( pTerm->eOperator==WO_ISNULL );
       
  1292       if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
       
  1293       nTerm++;
       
  1294     }
       
  1295 
       
  1296     /* If the ORDER BY clause contains only columns in the current 
       
  1297     ** virtual table then allocate space for the aOrderBy part of
       
  1298     ** the sqlite3_index_info structure.
       
  1299     */
       
  1300     nOrderBy = 0;
       
  1301     if( pOrderBy ){
       
  1302       for(i=0; i<pOrderBy->nExpr; i++){
       
  1303         Expr *pExpr = pOrderBy->a[i].pExpr;
       
  1304         if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
       
  1305       }
       
  1306       if( i==pOrderBy->nExpr ){
       
  1307         nOrderBy = pOrderBy->nExpr;
       
  1308       }
       
  1309     }
       
  1310 
       
  1311     /* Allocate the sqlite3_index_info structure
       
  1312     */
       
  1313     pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
       
  1314                              + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
       
  1315                              + sizeof(*pIdxOrderBy)*nOrderBy );
       
  1316     if( pIdxInfo==0 ){
       
  1317       sqlite3ErrorMsg(pParse, "out of memory");
       
  1318       return 0.0;
       
  1319     }
       
  1320     *ppIdxInfo = pIdxInfo;
       
  1321 
       
  1322     /* Initialize the structure.  The sqlite3_index_info structure contains
       
  1323     ** many fields that are declared "const" to prevent xBestIndex from
       
  1324     ** changing them.  We have to do some funky casting in order to
       
  1325     ** initialize those fields.
       
  1326     */
       
  1327     pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
       
  1328     pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
       
  1329     pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
       
  1330     *(int*)&pIdxInfo->nConstraint = nTerm;
       
  1331     *(int*)&pIdxInfo->nOrderBy = nOrderBy;
       
  1332     *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
       
  1333     *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
       
  1334     *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
       
  1335                                                                      pUsage;
       
  1336 
       
  1337     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
       
  1338       if( pTerm->leftCursor != pSrc->iCursor ) continue;
       
  1339       assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 );
       
  1340       testcase( pTerm->eOperator==WO_IN );
       
  1341       testcase( pTerm->eOperator==WO_ISNULL );
       
  1342       if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue;
       
  1343       pIdxCons[j].iColumn = pTerm->leftColumn;
       
  1344       pIdxCons[j].iTermOffset = i;
       
  1345       pIdxCons[j].op = pTerm->eOperator;
       
  1346       /* The direct assignment in the previous line is possible only because
       
  1347       ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
       
  1348       ** following asserts verify this fact. */
       
  1349       assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
       
  1350       assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
       
  1351       assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
       
  1352       assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
       
  1353       assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
       
  1354       assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
       
  1355       assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
       
  1356       j++;
       
  1357     }
       
  1358     for(i=0; i<nOrderBy; i++){
       
  1359       Expr *pExpr = pOrderBy->a[i].pExpr;
       
  1360       pIdxOrderBy[i].iColumn = pExpr->iColumn;
       
  1361       pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
       
  1362     }
       
  1363   }
       
  1364 
       
  1365   /* At this point, the sqlite3_index_info structure that pIdxInfo points
       
  1366   ** to will have been initialized, either during the current invocation or
       
  1367   ** during some prior invocation.  Now we just have to customize the
       
  1368   ** details of pIdxInfo for the current invocation and pass it to
       
  1369   ** xBestIndex.
       
  1370   */
       
  1371 
       
  1372   /* The module name must be defined. Also, by this point there must
       
  1373   ** be a pointer to an sqlite3_vtab structure. Otherwise
       
  1374   ** sqlite3ViewGetColumnNames() would have picked up the error. 
       
  1375   */
       
  1376   assert( pTab->azModuleArg && pTab->azModuleArg[0] );
       
  1377   assert( pVtab );
       
  1378 #if 0
       
  1379   if( pTab->pVtab==0 ){
       
  1380     sqlite3ErrorMsg(pParse, "undefined module %s for table %s",
       
  1381         pTab->azModuleArg[0], pTab->zName);
       
  1382     return 0.0;
       
  1383   }
       
  1384 #endif
       
  1385 
       
  1386   /* Set the aConstraint[].usable fields and initialize all 
       
  1387   ** output variables to zero.
       
  1388   **
       
  1389   ** aConstraint[].usable is true for constraints where the right-hand
       
  1390   ** side contains only references to tables to the left of the current
       
  1391   ** table.  In other words, if the constraint is of the form:
       
  1392   **
       
  1393   **           column = expr
       
  1394   **
       
  1395   ** and we are evaluating a join, then the constraint on column is 
       
  1396   ** only valid if all tables referenced in expr occur to the left
       
  1397   ** of the table containing column.
       
  1398   **
       
  1399   ** The aConstraints[] array contains entries for all constraints
       
  1400   ** on the current table.  That way we only have to compute it once
       
  1401   ** even though we might try to pick the best index multiple times.
       
  1402   ** For each attempt at picking an index, the order of tables in the
       
  1403   ** join might be different so we have to recompute the usable flag
       
  1404   ** each time.
       
  1405   */
       
  1406   pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
       
  1407   pUsage = pIdxInfo->aConstraintUsage;
       
  1408   for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
       
  1409     j = pIdxCons->iTermOffset;
       
  1410     pTerm = &pWC->a[j];
       
  1411     pIdxCons->usable =  (pTerm->prereqRight & notReady)==0;
       
  1412   }
       
  1413   memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
       
  1414   if( pIdxInfo->needToFreeIdxStr ){
       
  1415     sqlite3_free(pIdxInfo->idxStr);
       
  1416   }
       
  1417   pIdxInfo->idxStr = 0;
       
  1418   pIdxInfo->idxNum = 0;
       
  1419   pIdxInfo->needToFreeIdxStr = 0;
       
  1420   pIdxInfo->orderByConsumed = 0;
       
  1421   pIdxInfo->estimatedCost = SQLITE_BIG_DBL / 2.0;
       
  1422   nOrderBy = pIdxInfo->nOrderBy;
       
  1423   if( pIdxInfo->nOrderBy && !orderByUsable ){
       
  1424     *(int*)&pIdxInfo->nOrderBy = 0;
       
  1425   }
       
  1426 
       
  1427   (void)sqlite3SafetyOff(pParse->db);
       
  1428   WHERETRACE(("xBestIndex for %s\n", pTab->zName));
       
  1429   TRACE_IDX_INPUTS(pIdxInfo);
       
  1430   rc = pVtab->pModule->xBestIndex(pVtab, pIdxInfo);
       
  1431   TRACE_IDX_OUTPUTS(pIdxInfo);
       
  1432   (void)sqlite3SafetyOn(pParse->db);
       
  1433 
       
  1434   if( rc!=SQLITE_OK ){
       
  1435     if( rc==SQLITE_NOMEM ){
       
  1436       pParse->db->mallocFailed = 1;
       
  1437     }else if( !pVtab->zErrMsg ){
       
  1438       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
       
  1439     }else{
       
  1440       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
       
  1441     }
       
  1442   }
       
  1443   sqlite3DbFree(pParse->db, pVtab->zErrMsg);
       
  1444   pVtab->zErrMsg = 0;
       
  1445 
       
  1446   for(i=0; i<pIdxInfo->nConstraint; i++){
       
  1447     if( !pIdxInfo->aConstraint[i].usable && pUsage[i].argvIndex>0 ){
       
  1448       sqlite3ErrorMsg(pParse, 
       
  1449           "table %s: xBestIndex returned an invalid plan", pTab->zName);
       
  1450       return 0.0;
       
  1451     }
       
  1452   }
       
  1453 
       
  1454   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
       
  1455   return pIdxInfo->estimatedCost;
       
  1456 }
       
  1457 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
  1458 
       
  1459 /*
       
  1460 ** Find the best index for accessing a particular table.  Return a pointer
       
  1461 ** to the index, flags that describe how the index should be used, the
       
  1462 ** number of equality constraints, and the "cost" for this index.
       
  1463 **
       
  1464 ** The lowest cost index wins.  The cost is an estimate of the amount of
       
  1465 ** CPU and disk I/O need to process the request using the selected index.
       
  1466 ** Factors that influence cost include:
       
  1467 **
       
  1468 **    *  The estimated number of rows that will be retrieved.  (The
       
  1469 **       fewer the better.)
       
  1470 **
       
  1471 **    *  Whether or not sorting must occur.
       
  1472 **
       
  1473 **    *  Whether or not there must be separate lookups in the
       
  1474 **       index and in the main table.
       
  1475 **
       
  1476 ** If there was an INDEXED BY clause attached to the table in the SELECT
       
  1477 ** statement, then this function only considers strategies using the 
       
  1478 ** named index. If one cannot be found, then the returned cost is
       
  1479 ** SQLITE_BIG_DBL. If a strategy can be found that uses the named index, 
       
  1480 ** then the cost is calculated in the usual way.
       
  1481 **
       
  1482 ** If a NOT INDEXED clause was attached to the table in the SELECT 
       
  1483 ** statement, then no indexes are considered. However, the selected 
       
  1484 ** stategy may still take advantage of the tables built-in rowid
       
  1485 ** index.
       
  1486 */
       
  1487 static double bestIndex(
       
  1488   Parse *pParse,              /* The parsing context */
       
  1489   WhereClause *pWC,           /* The WHERE clause */
       
  1490   struct SrcList_item *pSrc,  /* The FROM clause term to search */
       
  1491   Bitmask notReady,           /* Mask of cursors that are not available */
       
  1492   ExprList *pOrderBy,         /* The order by clause */
       
  1493   Index **ppIndex,            /* Make *ppIndex point to the best index */
       
  1494   int *pFlags,                /* Put flags describing this choice in *pFlags */
       
  1495   int *pnEq                   /* Put the number of == or IN constraints here */
       
  1496 ){
       
  1497   WhereTerm *pTerm;
       
  1498   Index *bestIdx = 0;         /* Index that gives the lowest cost */
       
  1499   double lowestCost;          /* The cost of using bestIdx */
       
  1500   int bestFlags = 0;          /* Flags associated with bestIdx */
       
  1501   int bestNEq = 0;            /* Best value for nEq */
       
  1502   int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
       
  1503   Index *pProbe;              /* An index we are evaluating */
       
  1504   int rev;                    /* True to scan in reverse order */
       
  1505   int flags;                  /* Flags associated with pProbe */
       
  1506   int nEq;                    /* Number of == or IN constraints */
       
  1507   int eqTermMask;             /* Mask of valid equality operators */
       
  1508   double cost;                /* Cost of using pProbe */
       
  1509 
       
  1510   WHERETRACE(("bestIndex: tbl=%s notReady=%llx\n", pSrc->pTab->zName, notReady));
       
  1511   lowestCost = SQLITE_BIG_DBL;
       
  1512   pProbe = pSrc->pTab->pIndex;
       
  1513   if( pSrc->notIndexed ){
       
  1514     pProbe = 0;
       
  1515   }
       
  1516 
       
  1517   /* If the table has no indices and there are no terms in the where
       
  1518   ** clause that refer to the ROWID, then we will never be able to do
       
  1519   ** anything other than a full table scan on this table.  We might as
       
  1520   ** well put it first in the join order.  That way, perhaps it can be
       
  1521   ** referenced by other tables in the join.
       
  1522   */
       
  1523   if( pProbe==0 &&
       
  1524      findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IN|WO_LT|WO_LE|WO_GT|WO_GE,0)==0 &&
       
  1525      (pOrderBy==0 || !sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev)) ){
       
  1526     *pFlags = 0;
       
  1527     *ppIndex = 0;
       
  1528     *pnEq = 0;
       
  1529     return 0.0;
       
  1530   }
       
  1531 
       
  1532   /* Check for a rowid=EXPR or rowid IN (...) constraints. If there was
       
  1533   ** an INDEXED BY clause attached to this table, skip this step.
       
  1534   */
       
  1535   if( !pSrc->pIndex ){
       
  1536     pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
       
  1537     if( pTerm ){
       
  1538       Expr *pExpr;
       
  1539       *ppIndex = 0;
       
  1540       bestFlags = WHERE_ROWID_EQ;
       
  1541       if( pTerm->eOperator & WO_EQ ){
       
  1542         /* Rowid== is always the best pick.  Look no further.  Because only
       
  1543         ** a single row is generated, output is always in sorted order */
       
  1544         *pFlags = WHERE_ROWID_EQ | WHERE_UNIQUE;
       
  1545         *pnEq = 1;
       
  1546         WHERETRACE(("... best is rowid\n"));
       
  1547         return 0.0;
       
  1548       }else if( (pExpr = pTerm->pExpr)->pList!=0 ){
       
  1549         /* Rowid IN (LIST): cost is NlogN where N is the number of list
       
  1550         ** elements.  */
       
  1551         lowestCost = pExpr->pList->nExpr;
       
  1552         lowestCost *= estLog(lowestCost);
       
  1553       }else{
       
  1554         /* Rowid IN (SELECT): cost is NlogN where N is the number of rows
       
  1555         ** in the result of the inner select.  We have no way to estimate
       
  1556         ** that value so make a wild guess. */
       
  1557         lowestCost = 200;
       
  1558       }
       
  1559       WHERETRACE(("... rowid IN cost: %.9g\n", lowestCost));
       
  1560     }
       
  1561   
       
  1562     /* Estimate the cost of a table scan.  If we do not know how many
       
  1563     ** entries are in the table, use 1 million as a guess.
       
  1564     */
       
  1565     cost = pProbe ? pProbe->aiRowEst[0] : 1000000;
       
  1566     WHERETRACE(("... table scan base cost: %.9g\n", cost));
       
  1567     flags = WHERE_ROWID_RANGE;
       
  1568   
       
  1569     /* Check for constraints on a range of rowids in a table scan.
       
  1570     */
       
  1571     pTerm = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE|WO_GT|WO_GE, 0);
       
  1572     if( pTerm ){
       
  1573       if( findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0) ){
       
  1574         flags |= WHERE_TOP_LIMIT;
       
  1575         cost /= 3;  /* Guess that rowid<EXPR eliminates two-thirds or rows */
       
  1576       }
       
  1577       if( findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0) ){
       
  1578         flags |= WHERE_BTM_LIMIT;
       
  1579         cost /= 3;  /* Guess that rowid>EXPR eliminates two-thirds of rows */
       
  1580       }
       
  1581       WHERETRACE(("... rowid range reduces cost to %.9g\n", cost));
       
  1582     }else{
       
  1583       flags = 0;
       
  1584     }
       
  1585   
       
  1586     /* If the table scan does not satisfy the ORDER BY clause, increase
       
  1587     ** the cost by NlogN to cover the expense of sorting. */
       
  1588     if( pOrderBy ){
       
  1589       if( sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev) ){
       
  1590         flags |= WHERE_ORDERBY|WHERE_ROWID_RANGE;
       
  1591         if( rev ){
       
  1592           flags |= WHERE_REVERSE;
       
  1593         }
       
  1594       }else{
       
  1595         cost += cost*estLog(cost);
       
  1596         WHERETRACE(("... sorting increases cost to %.9g\n", cost));
       
  1597       }
       
  1598     }
       
  1599     if( cost<lowestCost ){
       
  1600       lowestCost = cost;
       
  1601       bestFlags = flags;
       
  1602     }
       
  1603   }
       
  1604 
       
  1605   /* If the pSrc table is the right table of a LEFT JOIN then we may not
       
  1606   ** use an index to satisfy IS NULL constraints on that table.  This is
       
  1607   ** because columns might end up being NULL if the table does not match -
       
  1608   ** a circumstance which the index cannot help us discover.  Ticket #2177.
       
  1609   */
       
  1610   if( (pSrc->jointype & JT_LEFT)!=0 ){
       
  1611     eqTermMask = WO_EQ|WO_IN;
       
  1612   }else{
       
  1613     eqTermMask = WO_EQ|WO_IN|WO_ISNULL;
       
  1614   }
       
  1615 
       
  1616   /* Look at each index.
       
  1617   */
       
  1618   if( pSrc->pIndex ){
       
  1619     pProbe = pSrc->pIndex;
       
  1620   }
       
  1621   for(; pProbe; pProbe=(pSrc->pIndex ? 0 : pProbe->pNext)){
       
  1622     int i;                       /* Loop counter */
       
  1623     double inMultiplier = 1;
       
  1624 
       
  1625     WHERETRACE(("... index %s:\n", pProbe->zName));
       
  1626 
       
  1627     /* Count the number of columns in the index that are satisfied
       
  1628     ** by x=EXPR constraints or x IN (...) constraints.
       
  1629     */
       
  1630     flags = 0;
       
  1631     for(i=0; i<pProbe->nColumn; i++){
       
  1632       int j = pProbe->aiColumn[i];
       
  1633       pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pProbe);
       
  1634       if( pTerm==0 ) break;
       
  1635       flags |= WHERE_COLUMN_EQ;
       
  1636       if( pTerm->eOperator & WO_IN ){
       
  1637         Expr *pExpr = pTerm->pExpr;
       
  1638         flags |= WHERE_COLUMN_IN;
       
  1639         if( pExpr->pSelect!=0 ){
       
  1640           inMultiplier *= 25;
       
  1641         }else if( ALWAYS(pExpr->pList) ){
       
  1642           inMultiplier *= pExpr->pList->nExpr + 1;
       
  1643         }
       
  1644       }
       
  1645     }
       
  1646     cost = pProbe->aiRowEst[i] * inMultiplier * estLog(inMultiplier);
       
  1647     nEq = i;
       
  1648     if( pProbe->onError!=OE_None && (flags & WHERE_COLUMN_IN)==0
       
  1649          && nEq==pProbe->nColumn ){
       
  1650       flags |= WHERE_UNIQUE;
       
  1651     }
       
  1652     WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n",nEq,inMultiplier,cost));
       
  1653 
       
  1654     /* Look for range constraints
       
  1655     */
       
  1656     if( nEq<pProbe->nColumn ){
       
  1657       int j = pProbe->aiColumn[nEq];
       
  1658       pTerm = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pProbe);
       
  1659       if( pTerm ){
       
  1660         flags |= WHERE_COLUMN_RANGE;
       
  1661         if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pProbe) ){
       
  1662           flags |= WHERE_TOP_LIMIT;
       
  1663           cost /= 3;
       
  1664         }
       
  1665         if( findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pProbe) ){
       
  1666           flags |= WHERE_BTM_LIMIT;
       
  1667           cost /= 3;
       
  1668         }
       
  1669         WHERETRACE(("...... range reduces cost to %.9g\n", cost));
       
  1670       }
       
  1671     }
       
  1672 
       
  1673     /* Add the additional cost of sorting if that is a factor.
       
  1674     */
       
  1675     if( pOrderBy ){
       
  1676       if( (flags & WHERE_COLUMN_IN)==0 &&
       
  1677            isSortingIndex(pParse,pWC->pMaskSet,pProbe,iCur,pOrderBy,nEq,&rev) ){
       
  1678         if( flags==0 ){
       
  1679           flags = WHERE_COLUMN_RANGE;
       
  1680         }
       
  1681         flags |= WHERE_ORDERBY;
       
  1682         if( rev ){
       
  1683           flags |= WHERE_REVERSE;
       
  1684         }
       
  1685       }else{
       
  1686         cost += cost*estLog(cost);
       
  1687         WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
       
  1688       }
       
  1689     }
       
  1690 
       
  1691     /* Check to see if we can get away with using just the index without
       
  1692     ** ever reading the table.  If that is the case, then halve the
       
  1693     ** cost of this index.
       
  1694     */
       
  1695     if( flags && pSrc->colUsed < (((Bitmask)1)<<(BMS-1)) ){
       
  1696       Bitmask m = pSrc->colUsed;
       
  1697       int j;
       
  1698       for(j=0; j<pProbe->nColumn; j++){
       
  1699         int x = pProbe->aiColumn[j];
       
  1700         if( x<BMS-1 ){
       
  1701           m &= ~(((Bitmask)1)<<x);
       
  1702         }
       
  1703       }
       
  1704       if( m==0 ){
       
  1705         flags |= WHERE_IDX_ONLY;
       
  1706         cost /= 2;
       
  1707         WHERETRACE(("...... idx-only reduces cost to %.9g\n", cost));
       
  1708       }
       
  1709     }
       
  1710 
       
  1711     /* If this index has achieved the lowest cost so far, then use it.
       
  1712     */
       
  1713     if( flags && cost < lowestCost ){
       
  1714       bestIdx = pProbe;
       
  1715       lowestCost = cost;
       
  1716       bestFlags = flags;
       
  1717       bestNEq = nEq;
       
  1718     }
       
  1719   }
       
  1720 
       
  1721   /* Report the best result
       
  1722   */
       
  1723   *ppIndex = bestIdx;
       
  1724   WHERETRACE(("best index is %s, cost=%.9g, flags=%x, nEq=%d\n",
       
  1725         bestIdx ? bestIdx->zName : "(none)", lowestCost, bestFlags, bestNEq));
       
  1726   *pFlags = bestFlags | eqTermMask;
       
  1727   *pnEq = bestNEq;
       
  1728   return lowestCost;
       
  1729 }
       
  1730 
       
  1731 
       
  1732 /*
       
  1733 ** Disable a term in the WHERE clause.  Except, do not disable the term
       
  1734 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
       
  1735 ** or USING clause of that join.
       
  1736 **
       
  1737 ** Consider the term t2.z='ok' in the following queries:
       
  1738 **
       
  1739 **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
       
  1740 **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
       
  1741 **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
       
  1742 **
       
  1743 ** The t2.z='ok' is disabled in the in (2) because it originates
       
  1744 ** in the ON clause.  The term is disabled in (3) because it is not part
       
  1745 ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
       
  1746 **
       
  1747 ** Disabling a term causes that term to not be tested in the inner loop
       
  1748 ** of the join.  Disabling is an optimization.  When terms are satisfied
       
  1749 ** by indices, we disable them to prevent redundant tests in the inner
       
  1750 ** loop.  We would get the correct results if nothing were ever disabled,
       
  1751 ** but joins might run a little slower.  The trick is to disable as much
       
  1752 ** as we can without disabling too much.  If we disabled in (1), we'd get
       
  1753 ** the wrong answer.  See ticket #813.
       
  1754 */
       
  1755 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
       
  1756   if( pTerm
       
  1757       && ALWAYS((pTerm->flags & TERM_CODED)==0)
       
  1758       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
       
  1759   ){
       
  1760     pTerm->flags |= TERM_CODED;
       
  1761     if( pTerm->iParent>=0 ){
       
  1762       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
       
  1763       if( (--pOther->nChild)==0 ){
       
  1764         disableTerm(pLevel, pOther);
       
  1765       }
       
  1766     }
       
  1767   }
       
  1768 }
       
  1769 
       
  1770 /*
       
  1771 ** Apply the affinities associated with the first n columns of index
       
  1772 ** pIdx to the values in the n registers starting at base.
       
  1773 */
       
  1774 static void codeApplyAffinity(Parse *pParse, int base, int n, Index *pIdx){
       
  1775   if( n>0 ){
       
  1776     Vdbe *v = pParse->pVdbe;
       
  1777     assert( v!=0 );
       
  1778     sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
       
  1779     sqlite3IndexAffinityStr(v, pIdx);
       
  1780     sqlite3ExprCacheAffinityChange(pParse, base, n);
       
  1781   }
       
  1782 }
       
  1783 
       
  1784 
       
  1785 /*
       
  1786 ** Generate code for a single equality term of the WHERE clause.  An equality
       
  1787 ** term can be either X=expr or X IN (...).   pTerm is the term to be 
       
  1788 ** coded.
       
  1789 **
       
  1790 ** The current value for the constraint is left in register iReg.
       
  1791 **
       
  1792 ** For a constraint of the form X=expr, the expression is evaluated and its
       
  1793 ** result is left on the stack.  For constraints of the form X IN (...)
       
  1794 ** this routine sets up a loop that will iterate over all values of X.
       
  1795 */
       
  1796 static int codeEqualityTerm(
       
  1797   Parse *pParse,      /* The parsing context */
       
  1798   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
       
  1799   WhereLevel *pLevel, /* When level of the FROM clause we are working on */
       
  1800   int iTarget         /* Attempt to leave results in this register */
       
  1801 ){
       
  1802   Expr *pX = pTerm->pExpr;
       
  1803   Vdbe *v = pParse->pVdbe;
       
  1804   int iReg;                  /* Register holding results */
       
  1805 
       
  1806   assert( iTarget>0 );
       
  1807   if( pX->op==TK_EQ ){
       
  1808     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
       
  1809   }else if( pX->op==TK_ISNULL ){
       
  1810     iReg = iTarget;
       
  1811     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
       
  1812 #ifndef SQLITE_OMIT_SUBQUERY
       
  1813   }else{
       
  1814     int eType;
       
  1815     int iTab;
       
  1816     struct InLoop *pIn;
       
  1817 
       
  1818     assert( pX->op==TK_IN );
       
  1819     iReg = iTarget;
       
  1820     eType = sqlite3FindInIndex(pParse, pX, 0);
       
  1821     iTab = pX->iTable;
       
  1822     sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
       
  1823     VdbeComment((v, "%.*s", pX->span.n, pX->span.z));
       
  1824     if( pLevel->nIn==0 ){
       
  1825       pLevel->nxt = sqlite3VdbeMakeLabel(v);
       
  1826     }
       
  1827     pLevel->nIn++;
       
  1828     pLevel->aInLoop = sqlite3DbReallocOrFree(pParse->db, pLevel->aInLoop,
       
  1829                                     sizeof(pLevel->aInLoop[0])*pLevel->nIn);
       
  1830     pIn = pLevel->aInLoop;
       
  1831     if( pIn ){
       
  1832       pIn += pLevel->nIn - 1;
       
  1833       pIn->iCur = iTab;
       
  1834       if( eType==IN_INDEX_ROWID ){
       
  1835         pIn->topAddr = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
       
  1836       }else{
       
  1837         pIn->topAddr = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
       
  1838       }
       
  1839       sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
       
  1840     }else{
       
  1841       pLevel->nIn = 0;
       
  1842     }
       
  1843 #endif
       
  1844   }
       
  1845   disableTerm(pLevel, pTerm);
       
  1846   return iReg;
       
  1847 }
       
  1848 
       
  1849 /*
       
  1850 ** Generate code that will evaluate all == and IN constraints for an
       
  1851 ** index.  The values for all constraints are left on the stack.
       
  1852 **
       
  1853 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
       
  1854 ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
       
  1855 ** The index has as many as three equality constraints, but in this
       
  1856 ** example, the third "c" value is an inequality.  So only two 
       
  1857 ** constraints are coded.  This routine will generate code to evaluate
       
  1858 ** a==5 and b IN (1,2,3).  The current values for a and b will be left
       
  1859 ** on the stack - a is the deepest and b the shallowest.
       
  1860 **
       
  1861 ** In the example above nEq==2.  But this subroutine works for any value
       
  1862 ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
       
  1863 ** The only thing it does is allocate the pLevel->iMem memory cell.
       
  1864 **
       
  1865 ** This routine always allocates at least one memory cell and puts
       
  1866 ** the address of that memory cell in pLevel->iMem.  The code that
       
  1867 ** calls this routine will use pLevel->iMem to store the termination
       
  1868 ** key value of the loop.  If one or more IN operators appear, then
       
  1869 ** this routine allocates an additional nEq memory cells for internal
       
  1870 ** use.
       
  1871 */
       
  1872 static int codeAllEqualityTerms(
       
  1873   Parse *pParse,        /* Parsing context */
       
  1874   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
       
  1875   WhereClause *pWC,     /* The WHERE clause */
       
  1876   Bitmask notReady,     /* Which parts of FROM have not yet been coded */
       
  1877   int nExtraReg         /* Number of extra registers to allocate */
       
  1878 ){
       
  1879   int nEq = pLevel->nEq;        /* The number of == or IN constraints to code */
       
  1880   Vdbe *v = pParse->pVdbe;      /* The virtual machine under construction */
       
  1881   Index *pIdx = pLevel->pIdx;   /* The index being used for this loop */
       
  1882   int iCur = pLevel->iTabCur;   /* The cursor of the table */
       
  1883   WhereTerm *pTerm;             /* A single constraint term */
       
  1884   int j;                        /* Loop counter */
       
  1885   int regBase;                  /* Base register */
       
  1886 
       
  1887   /* Figure out how many memory cells we will need then allocate them.
       
  1888   ** We always need at least one used to store the loop terminator
       
  1889   ** value.  If there are IN operators we'll need one for each == or
       
  1890   ** IN constraint.
       
  1891   */
       
  1892   pLevel->iMem = pParse->nMem + 1;
       
  1893   regBase = pParse->nMem + 2;
       
  1894   pParse->nMem += pLevel->nEq + 2 + nExtraReg;
       
  1895 
       
  1896   /* Evaluate the equality constraints
       
  1897   */
       
  1898   assert( pIdx->nColumn>=nEq );
       
  1899   for(j=0; j<nEq; j++){
       
  1900     int r1;
       
  1901     int k = pIdx->aiColumn[j];
       
  1902     pTerm = findTerm(pWC, iCur, k, notReady, pLevel->flags, pIdx);
       
  1903     if( NEVER(pTerm==0) ) break;
       
  1904     assert( (pTerm->flags & TERM_CODED)==0 );
       
  1905     r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j);
       
  1906     if( r1!=regBase+j ){
       
  1907       sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
       
  1908     }
       
  1909     testcase( pTerm->eOperator & WO_ISNULL );
       
  1910     testcase( pTerm->eOperator & WO_IN );
       
  1911     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
       
  1912       sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->brk);
       
  1913     }
       
  1914   }
       
  1915   return regBase;
       
  1916 }
       
  1917 
       
  1918 #if defined(SQLITE_TEST)
       
  1919 /*
       
  1920 ** The following variable holds a text description of query plan generated
       
  1921 ** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
       
  1922 ** overwrites the previous.  This information is used for testing and
       
  1923 ** analysis only.
       
  1924 */
       
  1925 char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
       
  1926 static int nQPlan = 0;              /* Next free slow in _query_plan[] */
       
  1927 
       
  1928 #endif /* SQLITE_TEST */
       
  1929 
       
  1930 
       
  1931 /*
       
  1932 ** Free a WhereInfo structure
       
  1933 */
       
  1934 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
       
  1935   if( pWInfo ){
       
  1936     int i;
       
  1937     for(i=0; i<pWInfo->nLevel; i++){
       
  1938       sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
       
  1939       if( pInfo ){
       
  1940         assert( pInfo->needToFreeIdxStr==0 );
       
  1941         sqlite3DbFree(db, pInfo);
       
  1942       }
       
  1943     }
       
  1944     sqlite3DbFree(db, pWInfo);
       
  1945   }
       
  1946 }
       
  1947 
       
  1948 
       
  1949 /*
       
  1950 ** Generate the beginning of the loop used for WHERE clause processing.
       
  1951 ** The return value is a pointer to an opaque structure that contains
       
  1952 ** information needed to terminate the loop.  Later, the calling routine
       
  1953 ** should invoke sqlite3WhereEnd() with the return value of this function
       
  1954 ** in order to complete the WHERE clause processing.
       
  1955 **
       
  1956 ** If an error occurs, this routine returns NULL.
       
  1957 **
       
  1958 ** The basic idea is to do a nested loop, one loop for each table in
       
  1959 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
       
  1960 ** same as a SELECT with only a single table in the FROM clause.)  For
       
  1961 ** example, if the SQL is this:
       
  1962 **
       
  1963 **       SELECT * FROM t1, t2, t3 WHERE ...;
       
  1964 **
       
  1965 ** Then the code generated is conceptually like the following:
       
  1966 **
       
  1967 **      foreach row1 in t1 do       \    Code generated
       
  1968 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
       
  1969 **          foreach row3 in t3 do   /
       
  1970 **            ...
       
  1971 **          end                     \    Code generated
       
  1972 **        end                        |-- by sqlite3WhereEnd()
       
  1973 **      end                         /
       
  1974 **
       
  1975 ** Note that the loops might not be nested in the order in which they
       
  1976 ** appear in the FROM clause if a different order is better able to make
       
  1977 ** use of indices.  Note also that when the IN operator appears in
       
  1978 ** the WHERE clause, it might result in additional nested loops for
       
  1979 ** scanning through all values on the right-hand side of the IN.
       
  1980 **
       
  1981 ** There are Btree cursors associated with each table.  t1 uses cursor
       
  1982 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
       
  1983 ** And so forth.  This routine generates code to open those VDBE cursors
       
  1984 ** and sqlite3WhereEnd() generates the code to close them.
       
  1985 **
       
  1986 ** The code that sqlite3WhereBegin() generates leaves the cursors named
       
  1987 ** in pTabList pointing at their appropriate entries.  The [...] code
       
  1988 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
       
  1989 ** data from the various tables of the loop.
       
  1990 **
       
  1991 ** If the WHERE clause is empty, the foreach loops must each scan their
       
  1992 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
       
  1993 ** the tables have indices and there are terms in the WHERE clause that
       
  1994 ** refer to those indices, a complete table scan can be avoided and the
       
  1995 ** code will run much faster.  Most of the work of this routine is checking
       
  1996 ** to see if there are indices that can be used to speed up the loop.
       
  1997 **
       
  1998 ** Terms of the WHERE clause are also used to limit which rows actually
       
  1999 ** make it to the "..." in the middle of the loop.  After each "foreach",
       
  2000 ** terms of the WHERE clause that use only terms in that loop and outer
       
  2001 ** loops are evaluated and if false a jump is made around all subsequent
       
  2002 ** inner loops (or around the "..." if the test occurs within the inner-
       
  2003 ** most loop)
       
  2004 **
       
  2005 ** OUTER JOINS
       
  2006 **
       
  2007 ** An outer join of tables t1 and t2 is conceptally coded as follows:
       
  2008 **
       
  2009 **    foreach row1 in t1 do
       
  2010 **      flag = 0
       
  2011 **      foreach row2 in t2 do
       
  2012 **        start:
       
  2013 **          ...
       
  2014 **          flag = 1
       
  2015 **      end
       
  2016 **      if flag==0 then
       
  2017 **        move the row2 cursor to a null row
       
  2018 **        goto start
       
  2019 **      fi
       
  2020 **    end
       
  2021 **
       
  2022 ** ORDER BY CLAUSE PROCESSING
       
  2023 **
       
  2024 ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
       
  2025 ** if there is one.  If there is no ORDER BY clause or if this routine
       
  2026 ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
       
  2027 **
       
  2028 ** If an index can be used so that the natural output order of the table
       
  2029 ** scan is correct for the ORDER BY clause, then that index is used and
       
  2030 ** *ppOrderBy is set to NULL.  This is an optimization that prevents an
       
  2031 ** unnecessary sort of the result set if an index appropriate for the
       
  2032 ** ORDER BY clause already exists.
       
  2033 **
       
  2034 ** If the where clause loops cannot be arranged to provide the correct
       
  2035 ** output order, then the *ppOrderBy is unchanged.
       
  2036 */
       
  2037 WhereInfo *sqlite3WhereBegin(
       
  2038   Parse *pParse,        /* The parser context */
       
  2039   SrcList *pTabList,    /* A list of all tables to be scanned */
       
  2040   Expr *pWhere,         /* The WHERE clause */
       
  2041   ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
       
  2042   u8 wflags             /* One of the WHERE_* flags defined in sqliteInt.h */
       
  2043 ){
       
  2044   int i;                     /* Loop counter */
       
  2045   WhereInfo *pWInfo;         /* Will become the return value of this function */
       
  2046   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
       
  2047   int brk, cont = 0;         /* Addresses used during code generation */
       
  2048   Bitmask notReady;          /* Cursors that are not yet positioned */
       
  2049   WhereTerm *pTerm;          /* A single term in the WHERE clause */
       
  2050   ExprMaskSet maskSet;       /* The expression mask set */
       
  2051   WhereClause wc;            /* The WHERE clause is divided into these terms */
       
  2052   struct SrcList_item *pTabItem;  /* A single entry from pTabList */
       
  2053   WhereLevel *pLevel;             /* A single level in the pWInfo list */
       
  2054   int iFrom;                      /* First unused FROM clause element */
       
  2055   int andFlags;              /* AND-ed combination of all wc.a[].flags */
       
  2056   sqlite3 *db;               /* Database connection */
       
  2057   ExprList *pOrderBy = 0;
       
  2058 
       
  2059   /* The number of tables in the FROM clause is limited by the number of
       
  2060   ** bits in a Bitmask 
       
  2061   */
       
  2062   if( pTabList->nSrc>BMS ){
       
  2063     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
       
  2064     return 0;
       
  2065   }
       
  2066 
       
  2067   if( ppOrderBy ){
       
  2068     pOrderBy = *ppOrderBy;
       
  2069   }
       
  2070 
       
  2071   /* Split the WHERE clause into separate subexpressions where each
       
  2072   ** subexpression is separated by an AND operator.
       
  2073   */
       
  2074   initMaskSet(&maskSet);
       
  2075   whereClauseInit(&wc, pParse, &maskSet);
       
  2076   sqlite3ExprCodeConstants(pParse, pWhere);
       
  2077   whereSplit(&wc, pWhere, TK_AND);
       
  2078     
       
  2079   /* Allocate and initialize the WhereInfo structure that will become the
       
  2080   ** return value.
       
  2081   */
       
  2082   db = pParse->db;
       
  2083   pWInfo = sqlite3DbMallocZero(db,  
       
  2084                       sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
       
  2085   if( db->mallocFailed ){
       
  2086     goto whereBeginError;
       
  2087   }
       
  2088   pWInfo->nLevel = pTabList->nSrc;
       
  2089   pWInfo->pParse = pParse;
       
  2090   pWInfo->pTabList = pTabList;
       
  2091   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
       
  2092 
       
  2093   /* Special case: a WHERE clause that is constant.  Evaluate the
       
  2094   ** expression and either jump over all of the code or fall thru.
       
  2095   */
       
  2096   if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
       
  2097     sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
       
  2098     pWhere = 0;
       
  2099   }
       
  2100 
       
  2101   /* Assign a bit from the bitmask to every term in the FROM clause.
       
  2102   **
       
  2103   ** When assigning bitmask values to FROM clause cursors, it must be
       
  2104   ** the case that if X is the bitmask for the N-th FROM clause term then
       
  2105   ** the bitmask for all FROM clause terms to the left of the N-th term
       
  2106   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
       
  2107   ** its Expr.iRightJoinTable value to find the bitmask of the right table
       
  2108   ** of the join.  Subtracting one from the right table bitmask gives a
       
  2109   ** bitmask for all tables to the left of the join.  Knowing the bitmask
       
  2110   ** for all tables to the left of a left join is important.  Ticket #3015.
       
  2111   */
       
  2112   for(i=0; i<pTabList->nSrc; i++){
       
  2113     createMask(&maskSet, pTabList->a[i].iCursor);
       
  2114   }
       
  2115 #ifndef NDEBUG
       
  2116   {
       
  2117     Bitmask toTheLeft = 0;
       
  2118     for(i=0; i<pTabList->nSrc; i++){
       
  2119       Bitmask m = getMask(&maskSet, pTabList->a[i].iCursor);
       
  2120       assert( (m-1)==toTheLeft );
       
  2121       toTheLeft |= m;
       
  2122     }
       
  2123   }
       
  2124 #endif
       
  2125 
       
  2126   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
       
  2127   ** add new virtual terms onto the end of the WHERE clause.  We do not
       
  2128   ** want to analyze these virtual terms, so start analyzing at the end
       
  2129   ** and work forward so that the added virtual terms are never processed.
       
  2130   */
       
  2131   exprAnalyzeAll(pTabList, &wc);
       
  2132   if( db->mallocFailed ){
       
  2133     goto whereBeginError;
       
  2134   }
       
  2135 
       
  2136   /* Chose the best index to use for each table in the FROM clause.
       
  2137   **
       
  2138   ** This loop fills in the following fields:
       
  2139   **
       
  2140   **   pWInfo->a[].pIdx      The index to use for this level of the loop.
       
  2141   **   pWInfo->a[].flags     WHERE_xxx flags associated with pIdx
       
  2142   **   pWInfo->a[].nEq       The number of == and IN constraints
       
  2143   **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
       
  2144   **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
       
  2145   **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
       
  2146   **
       
  2147   ** This loop also figures out the nesting order of tables in the FROM
       
  2148   ** clause.
       
  2149   */
       
  2150   notReady = ~(Bitmask)0;
       
  2151   pTabItem = pTabList->a;
       
  2152   pLevel = pWInfo->a;
       
  2153   andFlags = ~0;
       
  2154   WHERETRACE(("*** Optimizer Start ***\n"));
       
  2155   for(i=iFrom=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
       
  2156     Index *pIdx;                /* Index for FROM table at pTabItem */
       
  2157     int flags;                  /* Flags asssociated with pIdx */
       
  2158     int nEq;                    /* Number of == or IN constraints */
       
  2159     double cost;                /* The cost for pIdx */
       
  2160     int j;                      /* For looping over FROM tables */
       
  2161     Index *pBest = 0;           /* The best index seen so far */
       
  2162     int bestFlags = 0;          /* Flags associated with pBest */
       
  2163     int bestNEq = 0;            /* nEq associated with pBest */
       
  2164     double lowestCost;          /* Cost of the pBest */
       
  2165     int bestJ = 0;              /* The value of j */
       
  2166     Bitmask m;                  /* Bitmask value for j or bestJ */
       
  2167     int once = 0;               /* True when first table is seen */
       
  2168     sqlite3_index_info *pIndex; /* Current virtual index */
       
  2169 
       
  2170     lowestCost = SQLITE_BIG_DBL;
       
  2171     for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){
       
  2172       int doNotReorder;  /* True if this table should not be reordered */
       
  2173 
       
  2174       doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
       
  2175       if( once && doNotReorder ) break;
       
  2176       m = getMask(&maskSet, pTabItem->iCursor);
       
  2177       if( (m & notReady)==0 ){
       
  2178         if( j==iFrom ) iFrom++;
       
  2179         continue;
       
  2180       }
       
  2181       assert( pTabItem->pTab );
       
  2182 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  2183       if( IsVirtual(pTabItem->pTab) ){
       
  2184         sqlite3_index_info **ppIdxInfo = &pWInfo->a[j].pIdxInfo;
       
  2185         cost = bestVirtualIndex(pParse, &wc, pTabItem, notReady,
       
  2186                                 ppOrderBy ? *ppOrderBy : 0, i==0,
       
  2187                                 ppIdxInfo);
       
  2188         flags = WHERE_VIRTUALTABLE;
       
  2189         pIndex = *ppIdxInfo;
       
  2190         if( pIndex && pIndex->orderByConsumed ){
       
  2191           flags = WHERE_VIRTUALTABLE | WHERE_ORDERBY;
       
  2192         }
       
  2193         pIdx = 0;
       
  2194         nEq = 0;
       
  2195         if( (SQLITE_BIG_DBL/2.0)<cost ){
       
  2196           /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
       
  2197           ** inital value of lowestCost in this loop. If it is, then
       
  2198           ** the (cost<lowestCost) test below will never be true and
       
  2199           ** pLevel->pBestIdx never set.
       
  2200           */ 
       
  2201           cost = (SQLITE_BIG_DBL/2.0);
       
  2202         }
       
  2203       }else 
       
  2204 #endif
       
  2205       {
       
  2206         cost = bestIndex(pParse, &wc, pTabItem, notReady,
       
  2207                          (i==0 && ppOrderBy) ? *ppOrderBy : 0,
       
  2208                          &pIdx, &flags, &nEq);
       
  2209         pIndex = 0;
       
  2210       }
       
  2211       if( cost<lowestCost ){
       
  2212         once = 1;
       
  2213         lowestCost = cost;
       
  2214         pBest = pIdx;
       
  2215         bestFlags = flags;
       
  2216         bestNEq = nEq;
       
  2217         bestJ = j;
       
  2218         pLevel->pBestIdx = pIndex;
       
  2219       }
       
  2220       if( doNotReorder ) break;
       
  2221     }
       
  2222     WHERETRACE(("*** Optimizer selects table %d for loop %d\n", bestJ,
       
  2223            pLevel-pWInfo->a));
       
  2224     if( (bestFlags & WHERE_ORDERBY)!=0 ){
       
  2225       *ppOrderBy = 0;
       
  2226     }
       
  2227     andFlags &= bestFlags;
       
  2228     pLevel->flags = bestFlags;
       
  2229     pLevel->pIdx = pBest;
       
  2230     pLevel->nEq = bestNEq;
       
  2231     pLevel->aInLoop = 0;
       
  2232     pLevel->nIn = 0;
       
  2233     if( pBest ){
       
  2234       pLevel->iIdxCur = pParse->nTab++;
       
  2235     }else{
       
  2236       pLevel->iIdxCur = -1;
       
  2237     }
       
  2238     notReady &= ~getMask(&maskSet, pTabList->a[bestJ].iCursor);
       
  2239     pLevel->iFrom = bestJ;
       
  2240 
       
  2241     /* Check that if the table scanned by this loop iteration had an
       
  2242     ** INDEXED BY clause attached to it, that the named index is being
       
  2243     ** used for the scan. If not, then query compilation has failed.
       
  2244     ** Return an error.
       
  2245     */
       
  2246     pIdx = pTabList->a[bestJ].pIndex;
       
  2247     assert( !pIdx || !pBest || pIdx==pBest );
       
  2248     if( pIdx && pBest!=pIdx ){
       
  2249       sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
       
  2250       goto whereBeginError;
       
  2251     }
       
  2252   }
       
  2253   WHERETRACE(("*** Optimizer Finished ***\n"));
       
  2254 
       
  2255   /* If the total query only selects a single row, then the ORDER BY
       
  2256   ** clause is irrelevant.
       
  2257   */
       
  2258   if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
       
  2259     *ppOrderBy = 0;
       
  2260   }
       
  2261 
       
  2262   /* If the caller is an UPDATE or DELETE statement that is requesting
       
  2263   ** to use a one-pass algorithm, determine if this is appropriate.
       
  2264   ** The one-pass algorithm only works if the WHERE clause constraints
       
  2265   ** the statement to update a single row.
       
  2266   */
       
  2267   assert( (wflags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
       
  2268   if( (wflags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
       
  2269     pWInfo->okOnePass = 1;
       
  2270     pWInfo->a[0].flags &= ~WHERE_IDX_ONLY;
       
  2271   }
       
  2272 
       
  2273   /* Open all tables in the pTabList and any indices selected for
       
  2274   ** searching those tables.
       
  2275   */
       
  2276   sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
       
  2277   for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
       
  2278     Table *pTab;     /* Table to open */
       
  2279     Index *pIx;      /* Index used to access pTab (if any) */
       
  2280     int iDb;         /* Index of database containing table/index */
       
  2281     int iIdxCur = pLevel->iIdxCur;
       
  2282 
       
  2283 #ifndef SQLITE_OMIT_EXPLAIN
       
  2284     if( pParse->explain==2 ){
       
  2285       char *zMsg;
       
  2286       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
       
  2287       zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName);
       
  2288       if( pItem->zAlias ){
       
  2289         zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
       
  2290       }
       
  2291       if( (pIx = pLevel->pIdx)!=0 ){
       
  2292         zMsg = sqlite3MAppendf(db, zMsg, "%s WITH INDEX %s", zMsg, pIx->zName);
       
  2293       }else if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
       
  2294         zMsg = sqlite3MAppendf(db, zMsg, "%s USING PRIMARY KEY", zMsg);
       
  2295       }
       
  2296 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  2297       else if( pLevel->pBestIdx ){
       
  2298         sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
       
  2299         zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
       
  2300                     pBestIdx->idxNum, pBestIdx->idxStr);
       
  2301       }
       
  2302 #endif
       
  2303       if( pLevel->flags & WHERE_ORDERBY ){
       
  2304         zMsg = sqlite3MAppendf(db, zMsg, "%s ORDER BY", zMsg);
       
  2305       }
       
  2306       sqlite3VdbeAddOp4(v, OP_Explain, i, pLevel->iFrom, 0, zMsg, P4_DYNAMIC);
       
  2307     }
       
  2308 #endif /* SQLITE_OMIT_EXPLAIN */
       
  2309     pTabItem = &pTabList->a[pLevel->iFrom];
       
  2310     pTab = pTabItem->pTab;
       
  2311     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
       
  2312     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue;
       
  2313 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  2314     if( pLevel->pBestIdx ){
       
  2315       int iCur = pTabItem->iCursor;
       
  2316       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0,
       
  2317                         (const char*)pTab->pVtab, P4_VTAB);
       
  2318     }else
       
  2319 #endif
       
  2320     if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){
       
  2321       int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
       
  2322       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
       
  2323       if( !pWInfo->okOnePass && pTab->nCol<(sizeof(Bitmask)*8) ){
       
  2324         Bitmask b = pTabItem->colUsed;
       
  2325         int n = 0;
       
  2326         for(; b; b=b>>1, n++){}
       
  2327         sqlite3VdbeChangeP2(v, sqlite3VdbeCurrentAddr(v)-2, n);
       
  2328         assert( n<=pTab->nCol );
       
  2329       }
       
  2330     }else{
       
  2331       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
       
  2332     }
       
  2333     pLevel->iTabCur = pTabItem->iCursor;
       
  2334     if( (pIx = pLevel->pIdx)!=0 ){
       
  2335       KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
       
  2336       assert( pIx->pSchema==pTab->pSchema );
       
  2337       sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIx->nColumn+1);
       
  2338       sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb,
       
  2339                         (char*)pKey, P4_KEYINFO_HANDOFF);
       
  2340       VdbeComment((v, "%s", pIx->zName));
       
  2341     }
       
  2342     sqlite3CodeVerifySchema(pParse, iDb);
       
  2343   }
       
  2344   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
       
  2345 
       
  2346   /* Generate the code to do the search.  Each iteration of the for
       
  2347   ** loop below generates code for a single nested loop of the VM
       
  2348   ** program.
       
  2349   */
       
  2350   notReady = ~(Bitmask)0;
       
  2351   for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
       
  2352     int j, k;
       
  2353     int iCur = pTabItem->iCursor;  /* The VDBE cursor for the table */
       
  2354     Index *pIdx;       /* The index we will be using */
       
  2355     int nxt;           /* Where to jump to continue with the next IN case */
       
  2356     int iIdxCur;       /* The VDBE cursor for the index */
       
  2357     int omitTable;     /* True if we use the index only */
       
  2358     int bRev;          /* True if we need to scan in reverse order */
       
  2359 
       
  2360     pTabItem = &pTabList->a[pLevel->iFrom];
       
  2361     iCur = pTabItem->iCursor;
       
  2362     pIdx = pLevel->pIdx;
       
  2363     iIdxCur = pLevel->iIdxCur;
       
  2364     bRev = (pLevel->flags & WHERE_REVERSE)!=0;
       
  2365     omitTable = (pLevel->flags & WHERE_IDX_ONLY)!=0;
       
  2366 
       
  2367     /* Create labels for the "break" and "continue" instructions
       
  2368     ** for the current loop.  Jump to brk to break out of a loop.
       
  2369     ** Jump to cont to go immediately to the next iteration of the
       
  2370     ** loop.
       
  2371     **
       
  2372     ** When there is an IN operator, we also have a "nxt" label that
       
  2373     ** means to continue with the next IN value combination.  When
       
  2374     ** there are no IN operators in the constraints, the "nxt" label
       
  2375     ** is the same as "brk".
       
  2376     */
       
  2377     brk = pLevel->brk = pLevel->nxt = sqlite3VdbeMakeLabel(v);
       
  2378     cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
       
  2379 
       
  2380     /* If this is the right table of a LEFT OUTER JOIN, allocate and
       
  2381     ** initialize a memory cell that records if this table matches any
       
  2382     ** row of the left table of the join.
       
  2383     */
       
  2384     if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
       
  2385       pLevel->iLeftJoin = ++pParse->nMem;
       
  2386       sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
       
  2387       VdbeComment((v, "init LEFT JOIN no-match flag"));
       
  2388     }
       
  2389 
       
  2390 #ifndef SQLITE_OMIT_VIRTUALTABLE
       
  2391     if( pLevel->pBestIdx ){
       
  2392       /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
       
  2393       **          to access the data.
       
  2394       */
       
  2395       int j;
       
  2396       int iReg;   /* P3 Value for OP_VFilter */
       
  2397       sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
       
  2398       int nConstraint = pBestIdx->nConstraint;
       
  2399       struct sqlite3_index_constraint_usage *aUsage =
       
  2400                                                   pBestIdx->aConstraintUsage;
       
  2401       const struct sqlite3_index_constraint *aConstraint =
       
  2402                                                   pBestIdx->aConstraint;
       
  2403 
       
  2404       iReg = sqlite3GetTempRange(pParse, nConstraint+2);
       
  2405       pParse->disableColCache++;
       
  2406       for(j=1; j<=nConstraint; j++){
       
  2407         int k;
       
  2408         for(k=0; k<nConstraint; k++){
       
  2409           if( aUsage[k].argvIndex==j ){
       
  2410             int iTerm = aConstraint[k].iTermOffset;
       
  2411             assert( pParse->disableColCache );
       
  2412             sqlite3ExprCode(pParse, wc.a[iTerm].pExpr->pRight, iReg+j+1);
       
  2413             break;
       
  2414           }
       
  2415         }
       
  2416         if( k==nConstraint ) break;
       
  2417       }
       
  2418       assert( pParse->disableColCache );
       
  2419       pParse->disableColCache--;
       
  2420       sqlite3VdbeAddOp2(v, OP_Integer, pBestIdx->idxNum, iReg);
       
  2421       sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
       
  2422       sqlite3VdbeAddOp4(v, OP_VFilter, iCur, brk, iReg, pBestIdx->idxStr,
       
  2423                         pBestIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
       
  2424       sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
       
  2425       pBestIdx->needToFreeIdxStr = 0;
       
  2426       for(j=0; j<nConstraint; j++){
       
  2427         if( aUsage[j].omit ){
       
  2428           int iTerm = aConstraint[j].iTermOffset;
       
  2429           disableTerm(pLevel, &wc.a[iTerm]);
       
  2430         }
       
  2431       }
       
  2432       pLevel->op = OP_VNext;
       
  2433       pLevel->p1 = iCur;
       
  2434       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
       
  2435     }else
       
  2436 #endif /* SQLITE_OMIT_VIRTUALTABLE */
       
  2437 
       
  2438     if( pLevel->flags & WHERE_ROWID_EQ ){
       
  2439       /* Case 1:  We can directly reference a single row using an
       
  2440       **          equality comparison against the ROWID field.  Or
       
  2441       **          we reference multiple rows using a "rowid IN (...)"
       
  2442       **          construct.
       
  2443       */
       
  2444       int r1;
       
  2445       int rtmp = sqlite3GetTempReg(pParse);
       
  2446       pTerm = findTerm(&wc, iCur, -1, notReady, WO_EQ|WO_IN, 0);
       
  2447       assert( pTerm!=0 );
       
  2448       assert( pTerm->pExpr!=0 );
       
  2449       assert( pTerm->leftCursor==iCur );
       
  2450       assert( omitTable==0 );
       
  2451       r1 = codeEqualityTerm(pParse, pTerm, pLevel, rtmp);
       
  2452       nxt = pLevel->nxt;
       
  2453       sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, nxt);
       
  2454       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, nxt, r1);
       
  2455       sqlite3ReleaseTempReg(pParse, rtmp);
       
  2456       VdbeComment((v, "pk"));
       
  2457       pLevel->op = OP_Noop;
       
  2458     }else if( pLevel->flags & WHERE_ROWID_RANGE ){
       
  2459       /* Case 2:  We have an inequality comparison against the ROWID field.
       
  2460       */
       
  2461       int testOp = OP_Noop;
       
  2462       int start;
       
  2463       WhereTerm *pStart, *pEnd;
       
  2464 
       
  2465       assert( omitTable==0 );
       
  2466       pStart = findTerm(&wc, iCur, -1, notReady, WO_GT|WO_GE, 0);
       
  2467       pEnd = findTerm(&wc, iCur, -1, notReady, WO_LT|WO_LE, 0);
       
  2468       if( bRev ){
       
  2469         pTerm = pStart;
       
  2470         pStart = pEnd;
       
  2471         pEnd = pTerm;
       
  2472       }
       
  2473       if( pStart ){
       
  2474         Expr *pX;
       
  2475         int r1;
       
  2476         pX = pStart->pExpr;
       
  2477         assert( pX!=0 );
       
  2478         assert( pStart->leftCursor==iCur );
       
  2479 
       
  2480         /* The ForceInt instruction may modify the register that it operates
       
  2481         ** on. For example it may replace a real value with an integer one,
       
  2482         ** or if p3 is true it may increment the register value. For this
       
  2483         ** reason we need to make sure that register r1 is really a newly
       
  2484         ** allocated temporary register, and not part of the column-cache.
       
  2485         ** For this reason we cannot use sqlite3ExprCodeTemp() here.
       
  2486         */
       
  2487         r1 = sqlite3GetTempReg(pParse);
       
  2488         sqlite3ExprCode(pParse, pX->pRight, r1);
       
  2489 
       
  2490         sqlite3VdbeAddOp3(v, OP_ForceInt, r1, brk, 
       
  2491                              pX->op==TK_LE || pX->op==TK_GT);
       
  2492         sqlite3VdbeAddOp3(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk, r1);
       
  2493         VdbeComment((v, "pk"));
       
  2494         sqlite3ReleaseTempReg(pParse, r1);
       
  2495         disableTerm(pLevel, pStart);
       
  2496       }else{
       
  2497         sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, brk);
       
  2498       }
       
  2499       if( pEnd ){
       
  2500         Expr *pX;
       
  2501         pX = pEnd->pExpr;
       
  2502         assert( pX!=0 );
       
  2503         assert( pEnd->leftCursor==iCur );
       
  2504         pLevel->iMem = ++pParse->nMem;
       
  2505         sqlite3ExprCode(pParse, pX->pRight, pLevel->iMem);
       
  2506         if( pX->op==TK_LT || pX->op==TK_GT ){
       
  2507           testOp = bRev ? OP_Le : OP_Ge;
       
  2508         }else{
       
  2509           testOp = bRev ? OP_Lt : OP_Gt;
       
  2510         }
       
  2511         disableTerm(pLevel, pEnd);
       
  2512       }
       
  2513       start = sqlite3VdbeCurrentAddr(v);
       
  2514       pLevel->op = bRev ? OP_Prev : OP_Next;
       
  2515       pLevel->p1 = iCur;
       
  2516       pLevel->p2 = start;
       
  2517       if( testOp!=OP_Noop ){
       
  2518         int r1 = sqlite3GetTempReg(pParse);
       
  2519         sqlite3VdbeAddOp2(v, OP_Rowid, iCur, r1);
       
  2520         /* sqlite3VdbeAddOp2(v, OP_SCopy, pLevel->iMem, 0); */
       
  2521         sqlite3VdbeAddOp3(v, testOp, pLevel->iMem, brk, r1);
       
  2522         sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
       
  2523         sqlite3ReleaseTempReg(pParse, r1);
       
  2524       }
       
  2525     }else if( pLevel->flags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
       
  2526       /* Case 3: A scan using an index.
       
  2527       **
       
  2528       **         The WHERE clause may contain zero or more equality 
       
  2529       **         terms ("==" or "IN" operators) that refer to the N
       
  2530       **         left-most columns of the index. It may also contain
       
  2531       **         inequality constraints (>, <, >= or <=) on the indexed
       
  2532       **         column that immediately follows the N equalities. Only 
       
  2533       **         the right-most column can be an inequality - the rest must
       
  2534       **         use the "==" and "IN" operators. For example, if the 
       
  2535       **         index is on (x,y,z), then the following clauses are all 
       
  2536       **         optimized:
       
  2537       **
       
  2538       **            x=5
       
  2539       **            x=5 AND y=10
       
  2540       **            x=5 AND y<10
       
  2541       **            x=5 AND y>5 AND y<10
       
  2542       **            x=5 AND y=5 AND z<=10
       
  2543       **
       
  2544       **         The z<10 term of the following cannot be used, only
       
  2545       **         the x=5 term:
       
  2546       **
       
  2547       **            x=5 AND z<10
       
  2548       **
       
  2549       **         N may be zero if there are inequality constraints.
       
  2550       **         If there are no inequality constraints, then N is at
       
  2551       **         least one.
       
  2552       **
       
  2553       **         This case is also used when there are no WHERE clause
       
  2554       **         constraints but an index is selected anyway, in order
       
  2555       **         to force the output order to conform to an ORDER BY.
       
  2556       */  
       
  2557       int aStartOp[] = {
       
  2558         0,
       
  2559         0,
       
  2560         OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
       
  2561         OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
       
  2562         OP_MoveGt,           /* 4: (start_constraints  && !startEq && !bRev) */
       
  2563         OP_MoveLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
       
  2564         OP_MoveGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
       
  2565         OP_MoveLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
       
  2566       };
       
  2567       int aEndOp[] = {
       
  2568         OP_Noop,             /* 0: (!end_constraints) */
       
  2569         OP_IdxGE,            /* 1: (end_constraints && !bRev) */
       
  2570         OP_IdxLT             /* 2: (end_constraints && bRev) */
       
  2571       };
       
  2572       int nEq = pLevel->nEq;
       
  2573       int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
       
  2574       int regBase;                 /* Base register holding constraint values */
       
  2575       int r1;                      /* Temp register */
       
  2576       WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
       
  2577       WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
       
  2578       int startEq;                 /* True if range start uses ==, >= or <= */
       
  2579       int endEq;                   /* True if range end uses ==, >= or <= */
       
  2580       int start_constraints;       /* Start of range is constrained */
       
  2581       int k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */
       
  2582       int nConstraint;             /* Number of constraint terms */
       
  2583       int op;
       
  2584 
       
  2585       /* Generate code to evaluate all constraint terms using == or IN
       
  2586       ** and store the values of those terms in an array of registers
       
  2587       ** starting at regBase.
       
  2588       */
       
  2589       regBase = codeAllEqualityTerms(pParse, pLevel, &wc, notReady, 2);
       
  2590       nxt = pLevel->nxt;
       
  2591 
       
  2592       /* If this loop satisfies a sort order (pOrderBy) request that 
       
  2593       ** was passed to this function to implement a "SELECT min(x) ..." 
       
  2594       ** query, then the caller will only allow the loop to run for
       
  2595       ** a single iteration. This means that the first row returned
       
  2596       ** should not have a NULL value stored in 'x'. If column 'x' is
       
  2597       ** the first one after the nEq equality constraints in the index,
       
  2598       ** this requires some special handling.
       
  2599       */
       
  2600       if( (wflags&WHERE_ORDERBY_MIN)!=0
       
  2601        && (pLevel->flags&WHERE_ORDERBY)
       
  2602        && (pIdx->nColumn>nEq)
       
  2603       ){
       
  2604         assert( pOrderBy->nExpr==1 );
       
  2605         assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] );
       
  2606         isMinQuery = 1;
       
  2607       }
       
  2608 
       
  2609       /* Find any inequality constraint terms for the start and end 
       
  2610       ** of the range. 
       
  2611       */
       
  2612       if( pLevel->flags & WHERE_TOP_LIMIT ){
       
  2613         pRangeEnd = findTerm(&wc, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
       
  2614       }
       
  2615       if( pLevel->flags & WHERE_BTM_LIMIT ){
       
  2616         pRangeStart = findTerm(&wc, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
       
  2617       }
       
  2618 
       
  2619       /* If we are doing a reverse order scan on an ascending index, or
       
  2620       ** a forward order scan on a descending index, interchange the 
       
  2621       ** start and end terms (pRangeStart and pRangeEnd).
       
  2622       */
       
  2623       if( bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
       
  2624         SWAP(WhereTerm *, pRangeEnd, pRangeStart);
       
  2625       }
       
  2626 
       
  2627       testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
       
  2628       testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
       
  2629       testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
       
  2630       testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
       
  2631       startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
       
  2632       endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
       
  2633       start_constraints = pRangeStart || nEq>0;
       
  2634 
       
  2635       /* Seek the index cursor to the start of the range. */
       
  2636       nConstraint = nEq;
       
  2637       if( pRangeStart ){
       
  2638         int dcc = pParse->disableColCache;
       
  2639         if( pRangeEnd ){
       
  2640           pParse->disableColCache++;
       
  2641         }
       
  2642         sqlite3ExprCode(pParse, pRangeStart->pExpr->pRight, regBase+nEq);
       
  2643         pParse->disableColCache = dcc;
       
  2644         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, nxt);
       
  2645         nConstraint++;
       
  2646       }else if( isMinQuery ){
       
  2647         sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
       
  2648         nConstraint++;
       
  2649         startEq = 0;
       
  2650         start_constraints = 1;
       
  2651       }
       
  2652       codeApplyAffinity(pParse, regBase, nConstraint, pIdx);
       
  2653       op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
       
  2654       assert( op!=0 );
       
  2655       testcase( op==OP_Rewind );
       
  2656       testcase( op==OP_Last );
       
  2657       testcase( op==OP_MoveGt );
       
  2658       testcase( op==OP_MoveGe );
       
  2659       testcase( op==OP_MoveLe );
       
  2660       testcase( op==OP_MoveLt );
       
  2661       sqlite3VdbeAddOp4(v, op, iIdxCur, nxt, regBase, 
       
  2662                         SQLITE_INT_TO_PTR(nConstraint), P4_INT32);
       
  2663 
       
  2664       /* Load the value for the inequality constraint at the end of the
       
  2665       ** range (if any).
       
  2666       */
       
  2667       nConstraint = nEq;
       
  2668       if( pRangeEnd ){
       
  2669         sqlite3ExprCode(pParse, pRangeEnd->pExpr->pRight, regBase+nEq);
       
  2670         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, nxt);
       
  2671         codeApplyAffinity(pParse, regBase, nEq+1, pIdx);
       
  2672         nConstraint++;
       
  2673       }
       
  2674 
       
  2675       /* Top of the loop body */
       
  2676       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
       
  2677 
       
  2678       /* Check if the index cursor is past the end of the range. */
       
  2679       op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
       
  2680       testcase( op==OP_Noop );
       
  2681       testcase( op==OP_IdxGE );
       
  2682       testcase( op==OP_IdxLT );
       
  2683       sqlite3VdbeAddOp4(v, op, iIdxCur, nxt, regBase,
       
  2684                         SQLITE_INT_TO_PTR(nConstraint), P4_INT32);
       
  2685       sqlite3VdbeChangeP5(v, endEq!=bRev);
       
  2686 
       
  2687       /* If there are inequality constraints, check that the value
       
  2688       ** of the table column that the inequality contrains is not NULL.
       
  2689       ** If it is, jump to the next iteration of the loop.
       
  2690       */
       
  2691       r1 = sqlite3GetTempReg(pParse);
       
  2692       testcase( pLevel->flags & WHERE_BTM_LIMIT );
       
  2693       testcase( pLevel->flags & WHERE_TOP_LIMIT );
       
  2694       if( pLevel->flags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT) ){
       
  2695         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
       
  2696         sqlite3VdbeAddOp2(v, OP_IsNull, r1, cont);
       
  2697       }
       
  2698 
       
  2699       /* Seek the table cursor, if required */
       
  2700       if( !omitTable ){
       
  2701         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, r1);
       
  2702         sqlite3VdbeAddOp3(v, OP_MoveGe, iCur, 0, r1);  /* Deferred seek */
       
  2703       }
       
  2704       sqlite3ReleaseTempReg(pParse, r1);
       
  2705 
       
  2706       /* Record the instruction used to terminate the loop. Disable 
       
  2707       ** WHERE clause terms made redundant by the index range scan.
       
  2708       */
       
  2709       pLevel->op = bRev ? OP_Prev : OP_Next;
       
  2710       pLevel->p1 = iIdxCur;
       
  2711       disableTerm(pLevel, pRangeStart);
       
  2712       disableTerm(pLevel, pRangeEnd);
       
  2713     }else{
       
  2714       /* Case 4:  There is no usable index.  We must do a complete
       
  2715       **          scan of the entire table.
       
  2716       */
       
  2717       assert( omitTable==0 );
       
  2718       assert( bRev==0 );
       
  2719       pLevel->op = OP_Next;
       
  2720       pLevel->p1 = iCur;
       
  2721       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, OP_Rewind, iCur, brk);
       
  2722       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
       
  2723     }
       
  2724     notReady &= ~getMask(&maskSet, iCur);
       
  2725 
       
  2726     /* Insert code to test every subexpression that can be completely
       
  2727     ** computed using the current set of tables.
       
  2728     */
       
  2729     k = 0;
       
  2730     for(pTerm=wc.a, j=wc.nTerm; j>0; j--, pTerm++){
       
  2731       Expr *pE;
       
  2732       testcase( pTerm->flags & TERM_VIRTUAL );
       
  2733       testcase( pTerm->flags & TERM_CODED );
       
  2734       if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
       
  2735       if( (pTerm->prereqAll & notReady)!=0 ) continue;
       
  2736       pE = pTerm->pExpr;
       
  2737       assert( pE!=0 );
       
  2738       if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
       
  2739         continue;
       
  2740       }
       
  2741       pParse->disableColCache += k;
       
  2742       sqlite3ExprIfFalse(pParse, pE, cont, SQLITE_JUMPIFNULL);
       
  2743       pParse->disableColCache -= k;
       
  2744       k = 1;
       
  2745       pTerm->flags |= TERM_CODED;
       
  2746     }
       
  2747 
       
  2748     /* For a LEFT OUTER JOIN, generate code that will record the fact that
       
  2749     ** at least one row of the right table has matched the left table.  
       
  2750     */
       
  2751     if( pLevel->iLeftJoin ){
       
  2752       pLevel->top = sqlite3VdbeCurrentAddr(v);
       
  2753       sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
       
  2754       VdbeComment((v, "record LEFT JOIN hit"));
       
  2755       sqlite3ExprClearColumnCache(pParse, pLevel->iTabCur);
       
  2756       sqlite3ExprClearColumnCache(pParse, pLevel->iIdxCur);
       
  2757       for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
       
  2758         testcase( pTerm->flags & TERM_VIRTUAL );
       
  2759         testcase( pTerm->flags & TERM_CODED );
       
  2760         if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
       
  2761         if( (pTerm->prereqAll & notReady)!=0 ) continue;
       
  2762         assert( pTerm->pExpr );
       
  2763         sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, SQLITE_JUMPIFNULL);
       
  2764         pTerm->flags |= TERM_CODED;
       
  2765       }
       
  2766     }
       
  2767   }
       
  2768 
       
  2769 #ifdef SQLITE_TEST  /* For testing and debugging use only */
       
  2770   /* Record in the query plan information about the current table
       
  2771   ** and the index used to access it (if any).  If the table itself
       
  2772   ** is not used, its name is just '{}'.  If no index is used
       
  2773   ** the index is listed as "{}".  If the primary key is used the
       
  2774   ** index name is '*'.
       
  2775   */
       
  2776   for(i=0; i<pTabList->nSrc; i++){
       
  2777     char *z;
       
  2778     int n;
       
  2779     pLevel = &pWInfo->a[i];
       
  2780     pTabItem = &pTabList->a[pLevel->iFrom];
       
  2781     z = pTabItem->zAlias;
       
  2782     if( z==0 ) z = pTabItem->pTab->zName;
       
  2783     n = strlen(z);
       
  2784     if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
       
  2785       if( pLevel->flags & WHERE_IDX_ONLY ){
       
  2786         memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
       
  2787         nQPlan += 2;
       
  2788       }else{
       
  2789         memcpy(&sqlite3_query_plan[nQPlan], z, n);
       
  2790         nQPlan += n;
       
  2791       }
       
  2792       sqlite3_query_plan[nQPlan++] = ' ';
       
  2793     }
       
  2794     testcase( pLevel->flags & WHERE_ROWID_EQ );
       
  2795     testcase( pLevel->flags & WHERE_ROWID_RANGE );
       
  2796     if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
       
  2797       memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
       
  2798       nQPlan += 2;
       
  2799     }else if( pLevel->pIdx==0 ){
       
  2800       memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
       
  2801       nQPlan += 3;
       
  2802     }else{
       
  2803       n = strlen(pLevel->pIdx->zName);
       
  2804       if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
       
  2805         memcpy(&sqlite3_query_plan[nQPlan], pLevel->pIdx->zName, n);
       
  2806         nQPlan += n;
       
  2807         sqlite3_query_plan[nQPlan++] = ' ';
       
  2808       }
       
  2809     }
       
  2810   }
       
  2811   while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
       
  2812     sqlite3_query_plan[--nQPlan] = 0;
       
  2813   }
       
  2814   sqlite3_query_plan[nQPlan] = 0;
       
  2815   nQPlan = 0;
       
  2816 #endif /* SQLITE_TEST // Testing and debugging use only */
       
  2817 
       
  2818   /* Record the continuation address in the WhereInfo structure.  Then
       
  2819   ** clean up and return.
       
  2820   */
       
  2821   pWInfo->iContinue = cont;
       
  2822   whereClauseClear(&wc);
       
  2823   return pWInfo;
       
  2824 
       
  2825   /* Jump here if malloc fails */
       
  2826 whereBeginError:
       
  2827   whereClauseClear(&wc);
       
  2828   whereInfoFree(db, pWInfo);
       
  2829   return 0;
       
  2830 }
       
  2831 
       
  2832 /*
       
  2833 ** Generate the end of the WHERE loop.  See comments on 
       
  2834 ** sqlite3WhereBegin() for additional information.
       
  2835 */
       
  2836 void sqlite3WhereEnd(WhereInfo *pWInfo){
       
  2837   Parse *pParse = pWInfo->pParse;
       
  2838   Vdbe *v = pParse->pVdbe;
       
  2839   int i;
       
  2840   WhereLevel *pLevel;
       
  2841   SrcList *pTabList = pWInfo->pTabList;
       
  2842   sqlite3 *db = pParse->db;
       
  2843 
       
  2844   /* Generate loop termination code.
       
  2845   */
       
  2846   sqlite3ExprClearColumnCache(pParse, -1);
       
  2847   for(i=pTabList->nSrc-1; i>=0; i--){
       
  2848     pLevel = &pWInfo->a[i];
       
  2849     sqlite3VdbeResolveLabel(v, pLevel->cont);
       
  2850     if( pLevel->op!=OP_Noop ){
       
  2851       sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
       
  2852       sqlite3VdbeChangeP5(v, pLevel->p5);
       
  2853     }
       
  2854     if( pLevel->nIn ){
       
  2855       struct InLoop *pIn;
       
  2856       int j;
       
  2857       sqlite3VdbeResolveLabel(v, pLevel->nxt);
       
  2858       for(j=pLevel->nIn, pIn=&pLevel->aInLoop[j-1]; j>0; j--, pIn--){
       
  2859         sqlite3VdbeJumpHere(v, pIn->topAddr+1);
       
  2860         sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->topAddr);
       
  2861         sqlite3VdbeJumpHere(v, pIn->topAddr-1);
       
  2862       }
       
  2863       sqlite3DbFree(db, pLevel->aInLoop);
       
  2864     }
       
  2865     sqlite3VdbeResolveLabel(v, pLevel->brk);
       
  2866     if( pLevel->iLeftJoin ){
       
  2867       int addr;
       
  2868       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
       
  2869       sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
       
  2870       if( pLevel->iIdxCur>=0 ){
       
  2871         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
       
  2872       }
       
  2873       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->top);
       
  2874       sqlite3VdbeJumpHere(v, addr);
       
  2875     }
       
  2876   }
       
  2877 
       
  2878   /* The "break" point is here, just past the end of the outer loop.
       
  2879   ** Set it.
       
  2880   */
       
  2881   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
       
  2882 
       
  2883   /* Close all of the cursors that were opened by sqlite3WhereBegin.
       
  2884   */
       
  2885   for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
       
  2886     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
       
  2887     Table *pTab = pTabItem->pTab;
       
  2888     assert( pTab!=0 );
       
  2889     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ) continue;
       
  2890     if( !pWInfo->okOnePass && (pLevel->flags & WHERE_IDX_ONLY)==0 ){
       
  2891       sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
       
  2892     }
       
  2893     if( pLevel->pIdx!=0 ){
       
  2894       sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
       
  2895     }
       
  2896 
       
  2897     /* If this scan uses an index, make code substitutions to read data
       
  2898     ** from the index in preference to the table. Sometimes, this means
       
  2899     ** the table need never be read from. This is a performance boost,
       
  2900     ** as the vdbe level waits until the table is read before actually
       
  2901     ** seeking the table cursor to the record corresponding to the current
       
  2902     ** position in the index.
       
  2903     ** 
       
  2904     ** Calls to the code generator in between sqlite3WhereBegin and
       
  2905     ** sqlite3WhereEnd will have created code that references the table
       
  2906     ** directly.  This loop scans all that code looking for opcodes
       
  2907     ** that reference the table and converts them into opcodes that
       
  2908     ** reference the index.
       
  2909     */
       
  2910     if( pLevel->pIdx ){
       
  2911       int k, j, last;
       
  2912       VdbeOp *pOp;
       
  2913       Index *pIdx = pLevel->pIdx;
       
  2914       int useIndexOnly = pLevel->flags & WHERE_IDX_ONLY;
       
  2915 
       
  2916       assert( pIdx!=0 );
       
  2917       pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
       
  2918       last = sqlite3VdbeCurrentAddr(v);
       
  2919       for(k=pWInfo->iTop; k<last; k++, pOp++){
       
  2920         if( pOp->p1!=pLevel->iTabCur ) continue;
       
  2921         if( pOp->opcode==OP_Column ){
       
  2922           for(j=0; j<pIdx->nColumn; j++){
       
  2923             if( pOp->p2==pIdx->aiColumn[j] ){
       
  2924               pOp->p2 = j;
       
  2925               pOp->p1 = pLevel->iIdxCur;
       
  2926               break;
       
  2927             }
       
  2928           }
       
  2929           assert(!useIndexOnly || j<pIdx->nColumn);
       
  2930         }else if( pOp->opcode==OP_Rowid ){
       
  2931           pOp->p1 = pLevel->iIdxCur;
       
  2932           pOp->opcode = OP_IdxRowid;
       
  2933         }else if( pOp->opcode==OP_NullRow && useIndexOnly ){
       
  2934           pOp->opcode = OP_Noop;
       
  2935         }
       
  2936       }
       
  2937     }
       
  2938   }
       
  2939 
       
  2940   /* Final cleanup
       
  2941   */
       
  2942   whereInfoFree(db, pWInfo);
       
  2943   return;
       
  2944 }