2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2001 September 15
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This module contains C code that generates VDBE code used to process
2N/A** the WHERE clause of SQL statements.
2N/A**
2N/A** $Id: where.c,v 1.89.2.2 2004/07/19 19:30:50 drh Exp $
2N/A*/
2N/A#include "sqliteInt.h"
2N/A
2N/A/*
2N/A** The query generator uses an array of instances of this structure to
2N/A** help it analyze the subexpressions of the WHERE clause. Each WHERE
2N/A** clause subexpression is separated from the others by an AND operator.
2N/A*/
2N/Atypedef struct ExprInfo ExprInfo;
2N/Astruct ExprInfo {
2N/A Expr *p; /* Pointer to the subexpression */
2N/A u8 indexable; /* True if this subexprssion is usable by an index */
2N/A short int idxLeft; /* p->pLeft is a column in this table number. -1 if
2N/A ** p->pLeft is not the column of any table */
2N/A short int idxRight; /* p->pRight is a column in this table number. -1 if
2N/A ** p->pRight is not the column of any table */
2N/A unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */
2N/A unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */
2N/A unsigned prereqAll; /* Bitmask of tables referenced by p */
2N/A};
2N/A
2N/A/*
2N/A** An instance of the following structure keeps track of a mapping
2N/A** between VDBE cursor numbers and bitmasks. The VDBE cursor numbers
2N/A** are small integers contained in SrcList_item.iCursor and Expr.iTable
2N/A** fields. For any given WHERE clause, we want to track which cursors
2N/A** are being used, so we assign a single bit in a 32-bit word to track
2N/A** that cursor. Then a 32-bit integer is able to show the set of all
2N/A** cursors being used.
2N/A*/
2N/Atypedef struct ExprMaskSet ExprMaskSet;
2N/Astruct ExprMaskSet {
2N/A int n; /* Number of assigned cursor values */
2N/A int ix[31]; /* Cursor assigned to each bit */
2N/A};
2N/A
2N/A/*
2N/A** Determine the number of elements in an array.
2N/A*/
2N/A#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
2N/A
2N/A/*
2N/A** This routine is used to divide the WHERE expression into subexpressions
2N/A** separated by the AND operator.
2N/A**
2N/A** aSlot[] is an array of subexpressions structures.
2N/A** There are nSlot spaces left in this array. This routine attempts to
2N/A** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
2N/A** The return value is the number of slots filled.
2N/A*/
2N/Astatic int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
2N/A int cnt = 0;
2N/A if( pExpr==0 || nSlot<1 ) return 0;
2N/A if( nSlot==1 || pExpr->op!=TK_AND ){
2N/A aSlot[0].p = pExpr;
2N/A return 1;
2N/A }
2N/A if( pExpr->pLeft->op!=TK_AND ){
2N/A aSlot[0].p = pExpr->pLeft;
2N/A cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
2N/A }else{
2N/A cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
2N/A cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
2N/A }
2N/A return cnt;
2N/A}
2N/A
2N/A/*
2N/A** Initialize an expression mask set
2N/A*/
2N/A#define initMaskSet(P) memset(P, 0, sizeof(*P))
2N/A
2N/A/*
2N/A** Return the bitmask for the given cursor. Assign a new bitmask
2N/A** if this is the first time the cursor has been seen.
2N/A*/
2N/Astatic int getMask(ExprMaskSet *pMaskSet, int iCursor){
2N/A int i;
2N/A for(i=0; i<pMaskSet->n; i++){
2N/A if( pMaskSet->ix[i]==iCursor ) return 1<<i;
2N/A }
2N/A if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
2N/A pMaskSet->n++;
2N/A pMaskSet->ix[i] = iCursor;
2N/A return 1<<i;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A/*
2N/A** Destroy an expression mask set
2N/A*/
2N/A#define freeMaskSet(P) /* NO-OP */
2N/A
2N/A/*
2N/A** This routine walks (recursively) an expression tree and generates
2N/A** a bitmask indicating which tables are used in that expression
2N/A** tree.
2N/A**
2N/A** In order for this routine to work, the calling function must have
2N/A** previously invoked sqliteExprResolveIds() on the expression. See
2N/A** the header comment on that routine for additional information.
2N/A** The sqliteExprResolveIds() routines looks for column names and
2N/A** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
2N/A** the VDBE cursor number of the table.
2N/A*/
2N/Astatic int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
2N/A unsigned int mask = 0;
2N/A if( p==0 ) return 0;
2N/A if( p->op==TK_COLUMN ){
2N/A mask = getMask(pMaskSet, p->iTable);
2N/A if( mask==0 ) mask = -1;
2N/A return mask;
2N/A }
2N/A if( p->pRight ){
2N/A mask = exprTableUsage(pMaskSet, p->pRight);
2N/A }
2N/A if( p->pLeft ){
2N/A mask |= exprTableUsage(pMaskSet, p->pLeft);
2N/A }
2N/A if( p->pList ){
2N/A int i;
2N/A for(i=0; i<p->pList->nExpr; i++){
2N/A mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
2N/A }
2N/A }
2N/A return mask;
2N/A}
2N/A
2N/A/*
2N/A** Return TRUE if the given operator is one of the operators that is
2N/A** allowed for an indexable WHERE clause. The allowed operators are
2N/A** "=", "<", ">", "<=", ">=", and "IN".
2N/A*/
2N/Astatic int allowedOp(int op){
2N/A switch( op ){
2N/A case TK_LT:
2N/A case TK_LE:
2N/A case TK_GT:
2N/A case TK_GE:
2N/A case TK_EQ:
2N/A case TK_IN:
2N/A return 1;
2N/A default:
2N/A return 0;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A** The input to this routine is an ExprInfo structure with only the
2N/A** "p" field filled in. The job of this routine is to analyze the
2N/A** subexpression and populate all the other fields of the ExprInfo
2N/A** structure.
2N/A*/
2N/Astatic void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
2N/A Expr *pExpr = pInfo->p;
2N/A pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
2N/A pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
2N/A pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
2N/A pInfo->indexable = 0;
2N/A pInfo->idxLeft = -1;
2N/A pInfo->idxRight = -1;
2N/A if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
2N/A if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
2N/A pInfo->idxRight = pExpr->pRight->iTable;
2N/A pInfo->indexable = 1;
2N/A }
2N/A if( pExpr->pLeft->op==TK_COLUMN ){
2N/A pInfo->idxLeft = pExpr->pLeft->iTable;
2N/A pInfo->indexable = 1;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/*
2N/A** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
2N/A** left-most table in the FROM clause of that same SELECT statement and
2N/A** the table has a cursor number of "base".
2N/A**
2N/A** This routine attempts to find an index for pTab that generates the
2N/A** correct record sequence for the given ORDER BY clause. The return value
2N/A** is a pointer to an index that does the job. NULL is returned if the
2N/A** table has no index that will generate the correct sort order.
2N/A**
2N/A** If there are two or more indices that generate the correct sort order
2N/A** and pPreferredIdx is one of those indices, then return pPreferredIdx.
2N/A**
2N/A** nEqCol is the number of columns of pPreferredIdx that are used as
2N/A** equality constraints. Any index returned must have exactly this same
2N/A** set of columns. The ORDER BY clause only matches index columns beyond the
2N/A** the first nEqCol columns.
2N/A**
2N/A** All terms of the ORDER BY clause must be either ASC or DESC. The
2N/A** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
2N/A** set to 0 if the ORDER BY clause is all ASC.
2N/A*/
2N/Astatic Index *findSortingIndex(
2N/A Table *pTab, /* The table to be sorted */
2N/A int base, /* Cursor number for pTab */
2N/A ExprList *pOrderBy, /* The ORDER BY clause */
2N/A Index *pPreferredIdx, /* Use this index, if possible and not NULL */
2N/A int nEqCol, /* Number of index columns used with == constraints */
2N/A int *pbRev /* Set to 1 if ORDER BY is DESC */
2N/A){
2N/A int i, j;
2N/A Index *pMatch;
2N/A Index *pIdx;
2N/A int sortOrder;
2N/A
2N/A assert( pOrderBy!=0 );
2N/A assert( pOrderBy->nExpr>0 );
2N/A sortOrder = pOrderBy->a[0].sortOrder & SQLITE_SO_DIRMASK;
2N/A for(i=0; i<pOrderBy->nExpr; i++){
2N/A Expr *p;
2N/A if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){
2N/A /* Indices can only be used if all ORDER BY terms are either
2N/A ** DESC or ASC. Indices cannot be used on a mixture. */
2N/A return 0;
2N/A }
2N/A if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){
2N/A /* Do not sort by index if there is a COLLATE clause */
2N/A return 0;
2N/A }
2N/A p = pOrderBy->a[i].pExpr;
2N/A if( p->op!=TK_COLUMN || p->iTable!=base ){
2N/A /* Can not use an index sort on anything that is not a column in the
2N/A ** left-most table of the FROM clause */
2N/A return 0;
2N/A }
2N/A }
2N/A
2N/A /* If we get this far, it means the ORDER BY clause consists only of
2N/A ** ascending columns in the left-most table of the FROM clause. Now
2N/A ** check for a matching index.
2N/A */
2N/A pMatch = 0;
2N/A for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2N/A int nExpr = pOrderBy->nExpr;
2N/A if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
2N/A for(i=j=0; i<nEqCol; i++){
2N/A if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
2N/A if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; }
2N/A }
2N/A if( i<nEqCol ) continue;
2N/A for(i=0; i+j<nExpr; i++){
2N/A if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break;
2N/A }
2N/A if( i+j>=nExpr ){
2N/A pMatch = pIdx;
2N/A if( pIdx==pPreferredIdx ) break;
2N/A }
2N/A }
2N/A if( pMatch && pbRev ){
2N/A *pbRev = sortOrder==SQLITE_SO_DESC;
2N/A }
2N/A return pMatch;
2N/A}
2N/A
2N/A/*
2N/A** Disable a term in the WHERE clause. Except, do not disable the term
2N/A** if it controls a LEFT OUTER JOIN and it did not originate in the ON
2N/A** or USING clause of that join.
2N/A**
2N/A** Consider the term t2.z='ok' in the following queries:
2N/A**
2N/A** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
2N/A** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
2N/A** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
2N/A**
2N/A** The t2.z='ok' is disabled in the in (2) because it did not originate
2N/A** in the ON clause. The term is disabled in (3) because it is not part
2N/A** of a LEFT OUTER JOIN. In (1), the term is not disabled.
2N/A**
2N/A** Disabling a term causes that term to not be tested in the inner loop
2N/A** of the join. Disabling is an optimization. We would get the correct
2N/A** results if nothing were ever disabled, but joins might run a little
2N/A** slower. The trick is to disable as much as we can without disabling
2N/A** too much. If we disabled in (1), we'd get the wrong answer.
2N/A** See ticket #813.
2N/A*/
2N/Astatic void disableTerm(WhereLevel *pLevel, Expr **ppExpr){
2N/A Expr *pExpr = *ppExpr;
2N/A if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){
2N/A *ppExpr = 0;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A** Generate the beginning of the loop used for WHERE clause processing.
2N/A** The return value is a pointer to an (opaque) structure that contains
2N/A** information needed to terminate the loop. Later, the calling routine
2N/A** should invoke sqliteWhereEnd() with the return value of this function
2N/A** in order to complete the WHERE clause processing.
2N/A**
2N/A** If an error occurs, this routine returns NULL.
2N/A**
2N/A** The basic idea is to do a nested loop, one loop for each table in
2N/A** the FROM clause of a select. (INSERT and UPDATE statements are the
2N/A** same as a SELECT with only a single table in the FROM clause.) For
2N/A** example, if the SQL is this:
2N/A**
2N/A** SELECT * FROM t1, t2, t3 WHERE ...;
2N/A**
2N/A** Then the code generated is conceptually like the following:
2N/A**
2N/A** foreach row1 in t1 do \ Code generated
2N/A** foreach row2 in t2 do |-- by sqliteWhereBegin()
2N/A** foreach row3 in t3 do /
2N/A** ...
2N/A** end \ Code generated
2N/A** end |-- by sqliteWhereEnd()
2N/A** end /
2N/A**
2N/A** There are Btree cursors associated with each table. t1 uses cursor
2N/A** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
2N/A** And so forth. This routine generates code to open those VDBE cursors
2N/A** and sqliteWhereEnd() generates the code to close them.
2N/A**
2N/A** If the WHERE clause is empty, the foreach loops must each scan their
2N/A** entire tables. Thus a three-way join is an O(N^3) operation. But if
2N/A** the tables have indices and there are terms in the WHERE clause that
2N/A** refer to those indices, a complete table scan can be avoided and the
2N/A** code will run much faster. Most of the work of this routine is checking
2N/A** to see if there are indices that can be used to speed up the loop.
2N/A**
2N/A** Terms of the WHERE clause are also used to limit which rows actually
2N/A** make it to the "..." in the middle of the loop. After each "foreach",
2N/A** terms of the WHERE clause that use only terms in that loop and outer
2N/A** loops are evaluated and if false a jump is made around all subsequent
2N/A** inner loops (or around the "..." if the test occurs within the inner-
2N/A** most loop)
2N/A**
2N/A** OUTER JOINS
2N/A**
2N/A** An outer join of tables t1 and t2 is conceptally coded as follows:
2N/A**
2N/A** foreach row1 in t1 do
2N/A** flag = 0
2N/A** foreach row2 in t2 do
2N/A** start:
2N/A** ...
2N/A** flag = 1
2N/A** end
2N/A** if flag==0 then
2N/A** move the row2 cursor to a null row
2N/A** goto start
2N/A** fi
2N/A** end
2N/A**
2N/A** ORDER BY CLAUSE PROCESSING
2N/A**
2N/A** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
2N/A** if there is one. If there is no ORDER BY clause or if this routine
2N/A** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
2N/A**
2N/A** If an index can be used so that the natural output order of the table
2N/A** scan is correct for the ORDER BY clause, then that index is used and
2N/A** *ppOrderBy is set to NULL. This is an optimization that prevents an
2N/A** unnecessary sort of the result set if an index appropriate for the
2N/A** ORDER BY clause already exists.
2N/A**
2N/A** If the where clause loops cannot be arranged to provide the correct
2N/A** output order, then the *ppOrderBy is unchanged.
2N/A*/
2N/AWhereInfo *sqliteWhereBegin(
2N/A Parse *pParse, /* The parser context */
2N/A SrcList *pTabList, /* A list of all tables to be scanned */
2N/A Expr *pWhere, /* The WHERE clause */
2N/A int pushKey, /* If TRUE, leave the table key on the stack */
2N/A ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
2N/A){
2N/A int i; /* Loop counter */
2N/A WhereInfo *pWInfo; /* Will become the return value of this function */
2N/A Vdbe *v = pParse->pVdbe; /* The virtual database engine */
2N/A int brk, cont = 0; /* Addresses used during code generation */
2N/A int nExpr; /* Number of subexpressions in the WHERE clause */
2N/A int loopMask; /* One bit set for each outer loop */
2N/A int haveKey; /* True if KEY is on the stack */
2N/A ExprMaskSet maskSet; /* The expression mask set */
2N/A int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
2N/A int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
2N/A int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
2N/A ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
2N/A
2N/A /* pushKey is only allowed if there is a single table (as in an INSERT or
2N/A ** UPDATE statement)
2N/A */
2N/A assert( pushKey==0 || pTabList->nSrc==1 );
2N/A
2N/A /* Split the WHERE clause into separate subexpressions where each
2N/A ** subexpression is separated by an AND operator. If the aExpr[]
2N/A ** array fills up, the last entry might point to an expression which
2N/A ** contains additional unfactored AND operators.
2N/A */
2N/A initMaskSet(&maskSet);
2N/A memset(aExpr, 0, sizeof(aExpr));
2N/A nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
2N/A if( nExpr==ARRAYSIZE(aExpr) ){
2N/A sqliteErrorMsg(pParse, "WHERE clause too complex - no more "
2N/A "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
2N/A return 0;
2N/A }
2N/A
2N/A /* Allocate and initialize the WhereInfo structure that will become the
2N/A ** return value.
2N/A */
2N/A pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
2N/A if( sqlite_malloc_failed ){
2N/A sqliteFree(pWInfo);
2N/A return 0;
2N/A }
2N/A pWInfo->pParse = pParse;
2N/A pWInfo->pTabList = pTabList;
2N/A pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
2N/A pWInfo->iBreak = sqliteVdbeMakeLabel(v);
2N/A
2N/A /* Special case: a WHERE clause that is constant. Evaluate the
2N/A ** expression and either jump over all of the code or fall thru.
2N/A */
2N/A if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){
2N/A sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
2N/A pWhere = 0;
2N/A }
2N/A
2N/A /* Analyze all of the subexpressions.
2N/A */
2N/A for(i=0; i<nExpr; i++){
2N/A exprAnalyze(&maskSet, &aExpr[i]);
2N/A
2N/A /* If we are executing a trigger body, remove all references to
2N/A ** new.* and old.* tables from the prerequisite masks.
2N/A */
2N/A if( pParse->trigStack ){
2N/A int x;
2N/A if( (x = pParse->trigStack->newIdx) >= 0 ){
2N/A int mask = ~getMask(&maskSet, x);
2N/A aExpr[i].prereqRight &= mask;
2N/A aExpr[i].prereqLeft &= mask;
2N/A aExpr[i].prereqAll &= mask;
2N/A }
2N/A if( (x = pParse->trigStack->oldIdx) >= 0 ){
2N/A int mask = ~getMask(&maskSet, x);
2N/A aExpr[i].prereqRight &= mask;
2N/A aExpr[i].prereqLeft &= mask;
2N/A aExpr[i].prereqAll &= mask;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Figure out what index to use (if any) for each nested loop.
2N/A ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
2N/A ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
2N/A ** loop.
2N/A **
2N/A ** If terms exist that use the ROWID of any table, then set the
2N/A ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
2N/A ** to the index of the term containing the ROWID. We always prefer
2N/A ** to use a ROWID which can directly access a table rather than an
2N/A ** index which requires reading an index first to get the rowid then
2N/A ** doing a second read of the actual database table.
2N/A **
2N/A ** Actually, if there are more than 32 tables in the join, only the
2N/A ** first 32 tables are candidates for indices. This is (again) due
2N/A ** to the limit of 32 bits in an integer bitmask.
2N/A */
2N/A loopMask = 0;
2N/A for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
2N/A int j;
2N/A int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
2N/A int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
2N/A Table *pTab = pTabList->a[i].pTab;
2N/A Index *pIdx;
2N/A Index *pBestIdx = 0;
2N/A int bestScore = 0;
2N/A
2N/A /* Check to see if there is an expression that uses only the
2N/A ** ROWID field of this table. For terms of the form ROWID==expr
2N/A ** set iDirectEq[i] to the index of the term. For terms of the
2N/A ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
2N/A ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
2N/A **
2N/A ** (Added:) Treat ROWID IN expr like ROWID=expr.
2N/A */
2N/A pWInfo->a[i].iCur = -1;
2N/A iDirectEq[i] = -1;
2N/A iDirectLt[i] = -1;
2N/A iDirectGt[i] = -1;
2N/A for(j=0; j<nExpr; j++){
2N/A if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
2N/A && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
2N/A switch( aExpr[j].p->op ){
2N/A case TK_IN:
2N/A case TK_EQ: iDirectEq[i] = j; break;
2N/A case TK_LE:
2N/A case TK_LT: iDirectLt[i] = j; break;
2N/A case TK_GE:
2N/A case TK_GT: iDirectGt[i] = j; break;
2N/A }
2N/A }
2N/A if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
2N/A && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
2N/A switch( aExpr[j].p->op ){
2N/A case TK_EQ: iDirectEq[i] = j; break;
2N/A case TK_LE:
2N/A case TK_LT: iDirectGt[i] = j; break;
2N/A case TK_GE:
2N/A case TK_GT: iDirectLt[i] = j; break;
2N/A }
2N/A }
2N/A }
2N/A if( iDirectEq[i]>=0 ){
2N/A loopMask |= mask;
2N/A pWInfo->a[i].pIdx = 0;
2N/A continue;
2N/A }
2N/A
2N/A /* Do a search for usable indices. Leave pBestIdx pointing to
2N/A ** the "best" index. pBestIdx is left set to NULL if no indices
2N/A ** are usable.
2N/A **
2N/A ** The best index is determined as follows. For each of the
2N/A ** left-most terms that is fixed by an equality operator, add
2N/A ** 8 to the score. The right-most term of the index may be
2N/A ** constrained by an inequality. Add 1 if for an "x<..." constraint
2N/A ** and add 2 for an "x>..." constraint. Chose the index that
2N/A ** gives the best score.
2N/A **
2N/A ** This scoring system is designed so that the score can later be
2N/A ** used to determine how the index is used. If the score&7 is 0
2N/A ** then all constraints are equalities. If score&1 is not 0 then
2N/A ** there is an inequality used as a termination key. (ex: "x<...")
2N/A ** If score&2 is not 0 then there is an inequality used as the
2N/A ** start key. (ex: "x>..."). A score or 4 is the special case
2N/A ** of an IN operator constraint. (ex: "x IN ...").
2N/A **
2N/A ** The IN operator (as in "<expr> IN (...)") is treated the same as
2N/A ** an equality comparison except that it can only be used on the
2N/A ** left-most column of an index and other terms of the WHERE clause
2N/A ** cannot be used in conjunction with the IN operator to help satisfy
2N/A ** other columns of the index.
2N/A */
2N/A for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2N/A int eqMask = 0; /* Index columns covered by an x=... term */
2N/A int ltMask = 0; /* Index columns covered by an x<... term */
2N/A int gtMask = 0; /* Index columns covered by an x>... term */
2N/A int inMask = 0; /* Index columns covered by an x IN .. term */
2N/A int nEq, m, score;
2N/A
2N/A if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
2N/A for(j=0; j<nExpr; j++){
2N/A if( aExpr[j].idxLeft==iCur
2N/A && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
2N/A int iColumn = aExpr[j].p->pLeft->iColumn;
2N/A int k;
2N/A for(k=0; k<pIdx->nColumn; k++){
2N/A if( pIdx->aiColumn[k]==iColumn ){
2N/A switch( aExpr[j].p->op ){
2N/A case TK_IN: {
2N/A if( k==0 ) inMask |= 1;
2N/A break;
2N/A }
2N/A case TK_EQ: {
2N/A eqMask |= 1<<k;
2N/A break;
2N/A }
2N/A case TK_LE:
2N/A case TK_LT: {
2N/A ltMask |= 1<<k;
2N/A break;
2N/A }
2N/A case TK_GE:
2N/A case TK_GT: {
2N/A gtMask |= 1<<k;
2N/A break;
2N/A }
2N/A default: {
2N/A /* CANT_HAPPEN */
2N/A assert( 0 );
2N/A break;
2N/A }
2N/A }
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A if( aExpr[j].idxRight==iCur
2N/A && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
2N/A int iColumn = aExpr[j].p->pRight->iColumn;
2N/A int k;
2N/A for(k=0; k<pIdx->nColumn; k++){
2N/A if( pIdx->aiColumn[k]==iColumn ){
2N/A switch( aExpr[j].p->op ){
2N/A case TK_EQ: {
2N/A eqMask |= 1<<k;
2N/A break;
2N/A }
2N/A case TK_LE:
2N/A case TK_LT: {
2N/A gtMask |= 1<<k;
2N/A break;
2N/A }
2N/A case TK_GE:
2N/A case TK_GT: {
2N/A ltMask |= 1<<k;
2N/A break;
2N/A }
2N/A default: {
2N/A /* CANT_HAPPEN */
2N/A assert( 0 );
2N/A break;
2N/A }
2N/A }
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* The following loop ends with nEq set to the number of columns
2N/A ** on the left of the index with == constraints.
2N/A */
2N/A for(nEq=0; nEq<pIdx->nColumn; nEq++){
2N/A m = (1<<(nEq+1))-1;
2N/A if( (m & eqMask)!=m ) break;
2N/A }
2N/A score = nEq*8; /* Base score is 8 times number of == constraints */
2N/A m = 1<<nEq;
2N/A if( m & ltMask ) score++; /* Increase score for a < constraint */
2N/A if( m & gtMask ) score+=2; /* Increase score for a > constraint */
2N/A if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
2N/A if( score>bestScore ){
2N/A pBestIdx = pIdx;
2N/A bestScore = score;
2N/A }
2N/A }
2N/A pWInfo->a[i].pIdx = pBestIdx;
2N/A pWInfo->a[i].score = bestScore;
2N/A pWInfo->a[i].bRev = 0;
2N/A loopMask |= mask;
2N/A if( pBestIdx ){
2N/A pWInfo->a[i].iCur = pParse->nTab++;
2N/A pWInfo->peakNTab = pParse->nTab;
2N/A }
2N/A }
2N/A
2N/A /* Check to see if the ORDER BY clause is or can be satisfied by the
2N/A ** use of an index on the first table.
2N/A */
2N/A if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
2N/A Index *pSortIdx;
2N/A Index *pIdx;
2N/A Table *pTab;
2N/A int bRev = 0;
2N/A
2N/A pTab = pTabList->a[0].pTab;
2N/A pIdx = pWInfo->a[0].pIdx;
2N/A if( pIdx && pWInfo->a[0].score==4 ){
2N/A /* If there is already an IN index on the left-most table,
2N/A ** it will not give the correct sort order.
2N/A ** So, pretend that no suitable index is found.
2N/A */
2N/A pSortIdx = 0;
2N/A }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
2N/A /* If the left-most column is accessed using its ROWID, then do
2N/A ** not try to sort by index.
2N/A */
2N/A pSortIdx = 0;
2N/A }else{
2N/A int nEqCol = (pWInfo->a[0].score+4)/8;
2N/A pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
2N/A *ppOrderBy, pIdx, nEqCol, &bRev);
2N/A }
2N/A if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
2N/A if( pIdx==0 ){
2N/A pWInfo->a[0].pIdx = pSortIdx;
2N/A pWInfo->a[0].iCur = pParse->nTab++;
2N/A pWInfo->peakNTab = pParse->nTab;
2N/A }
2N/A pWInfo->a[0].bRev = bRev;
2N/A *ppOrderBy = 0;
2N/A }
2N/A }
2N/A
2N/A /* Open all tables in the pTabList and all indices used by those tables.
2N/A */
2N/A for(i=0; i<pTabList->nSrc; i++){
2N/A Table *pTab;
2N/A Index *pIx;
2N/A
2N/A pTab = pTabList->a[i].pTab;
2N/A if( pTab->isTransient || pTab->pSelect ) continue;
2N/A sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2N/A sqliteVdbeOp3(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum,
2N/A pTab->zName, P3_STATIC);
2N/A sqliteCodeVerifySchema(pParse, pTab->iDb);
2N/A if( (pIx = pWInfo->a[i].pIdx)!=0 ){
2N/A sqliteVdbeAddOp(v, OP_Integer, pIx->iDb, 0);
2N/A sqliteVdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum, pIx->zName,0);
2N/A }
2N/A }
2N/A
2N/A /* Generate the code to do the search
2N/A */
2N/A loopMask = 0;
2N/A for(i=0; i<pTabList->nSrc; i++){
2N/A int j, k;
2N/A int iCur = pTabList->a[i].iCursor;
2N/A Index *pIdx;
2N/A WhereLevel *pLevel = &pWInfo->a[i];
2N/A
2N/A /* If this is the right table of a LEFT OUTER JOIN, allocate and
2N/A ** initialize a memory cell that records if this table matches any
2N/A ** row of the left table of the join.
2N/A */
2N/A if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
2N/A if( !pParse->nMem ) pParse->nMem++;
2N/A pLevel->iLeftJoin = pParse->nMem++;
2N/A sqliteVdbeAddOp(v, OP_String, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
2N/A }
2N/A
2N/A pIdx = pLevel->pIdx;
2N/A pLevel->inOp = OP_Noop;
2N/A if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
2N/A /* Case 1: We can directly reference a single row using an
2N/A ** equality comparison against the ROWID field. Or
2N/A ** we reference multiple rows using a "rowid IN (...)"
2N/A ** construct.
2N/A */
2N/A k = iDirectEq[i];
2N/A assert( k<nExpr );
2N/A assert( aExpr[k].p!=0 );
2N/A assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
2N/A brk = pLevel->brk = sqliteVdbeMakeLabel(v);
2N/A if( aExpr[k].idxLeft==iCur ){
2N/A Expr *pX = aExpr[k].p;
2N/A if( pX->op!=TK_IN ){
2N/A sqliteExprCode(pParse, aExpr[k].p->pRight);
2N/A }else if( pX->pList ){
2N/A sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
2N/A pLevel->inOp = OP_SetNext;
2N/A pLevel->inP1 = pX->iTable;
2N/A pLevel->inP2 = sqliteVdbeCurrentAddr(v);
2N/A }else{
2N/A assert( pX->pSelect );
2N/A sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
2N/A sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
2N/A pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
2N/A pLevel->inOp = OP_Next;
2N/A pLevel->inP1 = pX->iTable;
2N/A }
2N/A }else{
2N/A sqliteExprCode(pParse, aExpr[k].p->pLeft);
2N/A }
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A cont = pLevel->cont = sqliteVdbeMakeLabel(v);
2N/A sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk);
2N/A haveKey = 0;
2N/A sqliteVdbeAddOp(v, OP_NotExists, iCur, brk);
2N/A pLevel->op = OP_Noop;
2N/A }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
2N/A /* Case 2: There is an index and all terms of the WHERE clause that
2N/A ** refer to the index use the "==" or "IN" operators.
2N/A */
2N/A int start;
2N/A int testOp;
2N/A int nColumn = (pLevel->score+4)/8;
2N/A brk = pLevel->brk = sqliteVdbeMakeLabel(v);
2N/A for(j=0; j<nColumn; j++){
2N/A for(k=0; k<nExpr; k++){
2N/A Expr *pX = aExpr[k].p;
2N/A if( pX==0 ) continue;
2N/A if( aExpr[k].idxLeft==iCur
2N/A && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
2N/A && pX->pLeft->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A if( pX->op==TK_EQ ){
2N/A sqliteExprCode(pParse, pX->pRight);
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A if( pX->op==TK_IN && nColumn==1 ){
2N/A if( pX->pList ){
2N/A sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
2N/A pLevel->inOp = OP_SetNext;
2N/A pLevel->inP1 = pX->iTable;
2N/A pLevel->inP2 = sqliteVdbeCurrentAddr(v);
2N/A }else{
2N/A assert( pX->pSelect );
2N/A sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
2N/A sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
2N/A pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
2N/A pLevel->inOp = OP_Next;
2N/A pLevel->inP1 = pX->iTable;
2N/A }
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A }
2N/A if( aExpr[k].idxRight==iCur
2N/A && aExpr[k].p->op==TK_EQ
2N/A && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
2N/A && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A sqliteExprCode(pParse, aExpr[k].p->pLeft);
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A pLevel->iMem = pParse->nMem++;
2N/A cont = pLevel->cont = sqliteVdbeMakeLabel(v);
2N/A sqliteVdbeAddOp(v, OP_NotNull, -nColumn, sqliteVdbeCurrentAddr(v)+3);
2N/A sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
2N/A sqliteVdbeAddOp(v, OP_Goto, 0, brk);
2N/A sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
2N/A sqliteAddIdxKeyType(v, pIdx);
2N/A if( nColumn==pIdx->nColumn || pLevel->bRev ){
2N/A sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
2N/A testOp = OP_IdxGT;
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
2N/A testOp = OP_IdxGE;
2N/A }
2N/A if( pLevel->bRev ){
2N/A /* Scan in reverse order */
2N/A sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
2N/A sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
2N/A start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2N/A sqliteVdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
2N/A pLevel->op = OP_Prev;
2N/A }else{
2N/A /* Scan in the forward order */
2N/A sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
2N/A start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2N/A sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
2N/A pLevel->op = OP_Next;
2N/A }
2N/A sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
2N/A sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
2N/A if( i==pTabList->nSrc-1 && pushKey ){
2N/A haveKey = 1;
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
2N/A haveKey = 0;
2N/A }
2N/A pLevel->p1 = pLevel->iCur;
2N/A pLevel->p2 = start;
2N/A }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
2N/A /* Case 3: We have an inequality comparison against the ROWID field.
2N/A */
2N/A int testOp = OP_Noop;
2N/A int start;
2N/A
2N/A brk = pLevel->brk = sqliteVdbeMakeLabel(v);
2N/A cont = pLevel->cont = sqliteVdbeMakeLabel(v);
2N/A if( iDirectGt[i]>=0 ){
2N/A k = iDirectGt[i];
2N/A assert( k<nExpr );
2N/A assert( aExpr[k].p!=0 );
2N/A assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
2N/A if( aExpr[k].idxLeft==iCur ){
2N/A sqliteExprCode(pParse, aExpr[k].p->pRight);
2N/A }else{
2N/A sqliteExprCode(pParse, aExpr[k].p->pLeft);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_ForceInt,
2N/A aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
2N/A sqliteVdbeAddOp(v, OP_MoveTo, iCur, brk);
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
2N/A }
2N/A if( iDirectLt[i]>=0 ){
2N/A k = iDirectLt[i];
2N/A assert( k<nExpr );
2N/A assert( aExpr[k].p!=0 );
2N/A assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
2N/A if( aExpr[k].idxLeft==iCur ){
2N/A sqliteExprCode(pParse, aExpr[k].p->pRight);
2N/A }else{
2N/A sqliteExprCode(pParse, aExpr[k].p->pLeft);
2N/A }
2N/A /* sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1); */
2N/A pLevel->iMem = pParse->nMem++;
2N/A sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
2N/A if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
2N/A testOp = OP_Ge;
2N/A }else{
2N/A testOp = OP_Gt;
2N/A }
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A }
2N/A start = sqliteVdbeCurrentAddr(v);
2N/A pLevel->op = OP_Next;
2N/A pLevel->p1 = iCur;
2N/A pLevel->p2 = start;
2N/A if( testOp!=OP_Noop ){
2N/A sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2N/A sqliteVdbeAddOp(v, testOp, 0, brk);
2N/A }
2N/A haveKey = 0;
2N/A }else if( pIdx==0 ){
2N/A /* Case 4: There is no usable index. We must do a complete
2N/A ** scan of the entire database table.
2N/A */
2N/A int start;
2N/A
2N/A brk = pLevel->brk = sqliteVdbeMakeLabel(v);
2N/A cont = pLevel->cont = sqliteVdbeMakeLabel(v);
2N/A sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
2N/A start = sqliteVdbeCurrentAddr(v);
2N/A pLevel->op = OP_Next;
2N/A pLevel->p1 = iCur;
2N/A pLevel->p2 = start;
2N/A haveKey = 0;
2N/A }else{
2N/A /* Case 5: The WHERE clause term that refers to the right-most
2N/A ** column of the index is an inequality. For example, if
2N/A ** the index is on (x,y,z) and the WHERE clause is of the
2N/A ** form "x=5 AND y<10" then this case is used. Only the
2N/A ** right-most column can be an inequality - the rest must
2N/A ** use the "==" operator.
2N/A **
2N/A ** This case is also used when there are no WHERE clause
2N/A ** constraints but an index is selected anyway, in order
2N/A ** to force the output order to conform to an ORDER BY.
2N/A */
2N/A int score = pLevel->score;
2N/A int nEqColumn = score/8;
2N/A int start;
2N/A int leFlag, geFlag;
2N/A int testOp;
2N/A
2N/A /* Evaluate the equality constraints
2N/A */
2N/A for(j=0; j<nEqColumn; j++){
2N/A for(k=0; k<nExpr; k++){
2N/A if( aExpr[k].p==0 ) continue;
2N/A if( aExpr[k].idxLeft==iCur
2N/A && aExpr[k].p->op==TK_EQ
2N/A && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
2N/A && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A sqliteExprCode(pParse, aExpr[k].p->pRight);
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A if( aExpr[k].idxRight==iCur
2N/A && aExpr[k].p->op==TK_EQ
2N/A && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
2N/A && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A sqliteExprCode(pParse, aExpr[k].p->pLeft);
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Duplicate the equality term values because they will all be
2N/A ** used twice: once to make the termination key and once to make the
2N/A ** start key.
2N/A */
2N/A for(j=0; j<nEqColumn; j++){
2N/A sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
2N/A }
2N/A
2N/A /* Labels for the beginning and end of the loop
2N/A */
2N/A cont = pLevel->cont = sqliteVdbeMakeLabel(v);
2N/A brk = pLevel->brk = sqliteVdbeMakeLabel(v);
2N/A
2N/A /* Generate the termination key. This is the key value that
2N/A ** will end the search. There is no termination key if there
2N/A ** are no equality terms and no "X<..." term.
2N/A **
2N/A ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
2N/A ** key computed here really ends up being the start key.
2N/A */
2N/A if( (score & 1)!=0 ){
2N/A for(k=0; k<nExpr; k++){
2N/A Expr *pExpr = aExpr[k].p;
2N/A if( pExpr==0 ) continue;
2N/A if( aExpr[k].idxLeft==iCur
2N/A && (pExpr->op==TK_LT || pExpr->op==TK_LE)
2N/A && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
2N/A && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A sqliteExprCode(pParse, pExpr->pRight);
2N/A leFlag = pExpr->op==TK_LE;
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A if( aExpr[k].idxRight==iCur
2N/A && (pExpr->op==TK_GT || pExpr->op==TK_GE)
2N/A && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
2N/A && pExpr->pRight->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A sqliteExprCode(pParse, pExpr->pLeft);
2N/A leFlag = pExpr->op==TK_GE;
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A }
2N/A testOp = OP_IdxGE;
2N/A }else{
2N/A testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
2N/A leFlag = 1;
2N/A }
2N/A if( testOp!=OP_Noop ){
2N/A int nCol = nEqColumn + (score & 1);
2N/A pLevel->iMem = pParse->nMem++;
2N/A sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
2N/A sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
2N/A sqliteVdbeAddOp(v, OP_Goto, 0, brk);
2N/A sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
2N/A sqliteAddIdxKeyType(v, pIdx);
2N/A if( leFlag ){
2N/A sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
2N/A }
2N/A if( pLevel->bRev ){
2N/A sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
2N/A }
2N/A }else if( pLevel->bRev ){
2N/A sqliteVdbeAddOp(v, OP_Last, pLevel->iCur, brk);
2N/A }
2N/A
2N/A /* Generate the start key. This is the key that defines the lower
2N/A ** bound on the search. There is no start key if there are no
2N/A ** equality terms and if there is no "X>..." term. In
2N/A ** that case, generate a "Rewind" instruction in place of the
2N/A ** start key search.
2N/A **
2N/A ** 2002-Dec-04: In the case of a reverse-order search, the so-called
2N/A ** "start" key really ends up being used as the termination key.
2N/A */
2N/A if( (score & 2)!=0 ){
2N/A for(k=0; k<nExpr; k++){
2N/A Expr *pExpr = aExpr[k].p;
2N/A if( pExpr==0 ) continue;
2N/A if( aExpr[k].idxLeft==iCur
2N/A && (pExpr->op==TK_GT || pExpr->op==TK_GE)
2N/A && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
2N/A && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A sqliteExprCode(pParse, pExpr->pRight);
2N/A geFlag = pExpr->op==TK_GE;
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A if( aExpr[k].idxRight==iCur
2N/A && (pExpr->op==TK_LT || pExpr->op==TK_LE)
2N/A && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
2N/A && pExpr->pRight->iColumn==pIdx->aiColumn[j]
2N/A ){
2N/A sqliteExprCode(pParse, pExpr->pLeft);
2N/A geFlag = pExpr->op==TK_LE;
2N/A disableTerm(pLevel, &aExpr[k].p);
2N/A break;
2N/A }
2N/A }
2N/A }else{
2N/A geFlag = 1;
2N/A }
2N/A if( nEqColumn>0 || (score&2)!=0 ){
2N/A int nCol = nEqColumn + ((score&2)!=0);
2N/A sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
2N/A sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
2N/A sqliteVdbeAddOp(v, OP_Goto, 0, brk);
2N/A sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
2N/A sqliteAddIdxKeyType(v, pIdx);
2N/A if( !geFlag ){
2N/A sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
2N/A }
2N/A if( pLevel->bRev ){
2N/A pLevel->iMem = pParse->nMem++;
2N/A sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
2N/A testOp = OP_IdxLT;
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
2N/A }
2N/A }else if( pLevel->bRev ){
2N/A testOp = OP_Noop;
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
2N/A }
2N/A
2N/A /* Generate the the top of the loop. If there is a termination
2N/A ** key we have to test for that key and abort at the top of the
2N/A ** loop.
2N/A */
2N/A start = sqliteVdbeCurrentAddr(v);
2N/A if( testOp!=OP_Noop ){
2N/A sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
2N/A sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
2N/A sqliteVdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
2N/A sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
2N/A if( i==pTabList->nSrc-1 && pushKey ){
2N/A haveKey = 1;
2N/A }else{
2N/A sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
2N/A haveKey = 0;
2N/A }
2N/A
2N/A /* Record the instruction used to terminate the loop.
2N/A */
2N/A pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
2N/A pLevel->p1 = pLevel->iCur;
2N/A pLevel->p2 = start;
2N/A }
2N/A loopMask |= getMask(&maskSet, iCur);
2N/A
2N/A /* Insert code to test every subexpression that can be completely
2N/A ** computed using the current set of tables.
2N/A */
2N/A for(j=0; j<nExpr; j++){
2N/A if( aExpr[j].p==0 ) continue;
2N/A if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
2N/A if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
2N/A continue;
2N/A }
2N/A if( haveKey ){
2N/A haveKey = 0;
2N/A sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
2N/A }
2N/A sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
2N/A aExpr[j].p = 0;
2N/A }
2N/A brk = cont;
2N/A
2N/A /* For a LEFT OUTER JOIN, generate code that will record the fact that
2N/A ** at least one row of the right table has matched the left table.
2N/A */
2N/A if( pLevel->iLeftJoin ){
2N/A pLevel->top = sqliteVdbeCurrentAddr(v);
2N/A sqliteVdbeAddOp(v, OP_Integer, 1, 0);
2N/A sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
2N/A for(j=0; j<nExpr; j++){
2N/A if( aExpr[j].p==0 ) continue;
2N/A if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
2N/A if( haveKey ){
2N/A /* Cannot happen. "haveKey" can only be true if pushKey is true
2N/A ** an pushKey can only be true for DELETE and UPDATE and there are
2N/A ** no outer joins with DELETE and UPDATE.
2N/A */
2N/A haveKey = 0;
2N/A sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
2N/A }
2N/A sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
2N/A aExpr[j].p = 0;
2N/A }
2N/A }
2N/A }
2N/A pWInfo->iContinue = cont;
2N/A if( pushKey && !haveKey ){
2N/A sqliteVdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
2N/A }
2N/A freeMaskSet(&maskSet);
2N/A return pWInfo;
2N/A}
2N/A
2N/A/*
2N/A** Generate the end of the WHERE loop. See comments on
2N/A** sqliteWhereBegin() for additional information.
2N/A*/
2N/Avoid sqliteWhereEnd(WhereInfo *pWInfo){
2N/A Vdbe *v = pWInfo->pParse->pVdbe;
2N/A int i;
2N/A WhereLevel *pLevel;
2N/A SrcList *pTabList = pWInfo->pTabList;
2N/A
2N/A for(i=pTabList->nSrc-1; i>=0; i--){
2N/A pLevel = &pWInfo->a[i];
2N/A sqliteVdbeResolveLabel(v, pLevel->cont);
2N/A if( pLevel->op!=OP_Noop ){
2N/A sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
2N/A }
2N/A sqliteVdbeResolveLabel(v, pLevel->brk);
2N/A if( pLevel->inOp!=OP_Noop ){
2N/A sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
2N/A }
2N/A if( pLevel->iLeftJoin ){
2N/A int addr;
2N/A addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
2N/A sqliteVdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
2N/A sqliteVdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
2N/A if( pLevel->iCur>=0 ){
2N/A sqliteVdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
2N/A }
2N/A sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
2N/A }
2N/A }
2N/A sqliteVdbeResolveLabel(v, pWInfo->iBreak);
2N/A for(i=0; i<pTabList->nSrc; i++){
2N/A Table *pTab = pTabList->a[i].pTab;
2N/A assert( pTab!=0 );
2N/A if( pTab->isTransient || pTab->pSelect ) continue;
2N/A pLevel = &pWInfo->a[i];
2N/A sqliteVdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
2N/A if( pLevel->pIdx!=0 ){
2N/A sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
2N/A }
2N/A }
2N/A#if 0 /* Never reuse a cursor */
2N/A if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
2N/A pWInfo->pParse->nTab = pWInfo->savedNTab;
2N/A }
2N/A#endif
2N/A sqliteFree(pWInfo);
2N/A return;
2N/A}