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