blob: af4fa53a90c719df03fae6452f843fac863e6f78 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** This module contains C code that generates VDBE code used to process
drhb2fe7d82003-04-20 17:29:23 +000013** the WHERE clause of SQL statements.
drh75897232000-05-29 14:26:00 +000014**
drhfec19aa2004-05-19 20:41:03 +000015** $Id: where.c,v 1.99 2004/05/19 20:41:04 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include "sqliteInt.h"
18
19/*
20** The query generator uses an array of instances of this structure to
21** help it analyze the subexpressions of the WHERE clause. Each WHERE
22** clause subexpression is separated from the others by an AND operator.
23*/
24typedef struct ExprInfo ExprInfo;
25struct ExprInfo {
26 Expr *p; /* Pointer to the subexpression */
drhe3184742002-06-19 14:27:05 +000027 u8 indexable; /* True if this subexprssion is usable by an index */
28 short int idxLeft; /* p->pLeft is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000029 ** p->pLeft is not the column of any table */
drhe3184742002-06-19 14:27:05 +000030 short int idxRight; /* p->pRight is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000031 ** p->pRight is not the column of any table */
drhe3184742002-06-19 14:27:05 +000032 unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */
33 unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */
34 unsigned prereqAll; /* Bitmask of tables referenced by p */
drh75897232000-05-29 14:26:00 +000035};
36
37/*
drh6a3ea0e2003-05-02 14:32:12 +000038** An instance of the following structure keeps track of a mapping
39** between VDBE cursor numbers and bitmasks. The VDBE cursor numbers
40** are small integers contained in SrcList_item.iCursor and Expr.iTable
41** fields. For any given WHERE clause, we want to track which cursors
42** are being used, so we assign a single bit in a 32-bit word to track
43** that cursor. Then a 32-bit integer is able to show the set of all
44** cursors being used.
45*/
46typedef struct ExprMaskSet ExprMaskSet;
47struct ExprMaskSet {
48 int n; /* Number of assigned cursor values */
49 int ix[32]; /* Cursor assigned to each bit */
50};
51
52/*
drh75897232000-05-29 14:26:00 +000053** Determine the number of elements in an array.
54*/
55#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
56
57/*
58** This routine is used to divide the WHERE expression into subexpressions
59** separated by the AND operator.
60**
61** aSlot[] is an array of subexpressions structures.
62** There are nSlot spaces left in this array. This routine attempts to
63** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
64** The return value is the number of slots filled.
65*/
66static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
67 int cnt = 0;
68 if( pExpr==0 || nSlot<1 ) return 0;
69 if( nSlot==1 || pExpr->op!=TK_AND ){
70 aSlot[0].p = pExpr;
71 return 1;
72 }
73 if( pExpr->pLeft->op!=TK_AND ){
74 aSlot[0].p = pExpr->pLeft;
75 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
76 }else{
drhdcd997e2003-01-31 17:21:49 +000077 cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
78 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
drh75897232000-05-29 14:26:00 +000079 }
80 return cnt;
81}
82
83/*
drh6a3ea0e2003-05-02 14:32:12 +000084** Initialize an expression mask set
85*/
86#define initMaskSet(P) memset(P, 0, sizeof(*P))
87
88/*
89** Return the bitmask for the given cursor. Assign a new bitmask
90** if this is the first time the cursor has been seen.
91*/
92static int getMask(ExprMaskSet *pMaskSet, int iCursor){
93 int i;
94 for(i=0; i<pMaskSet->n; i++){
95 if( pMaskSet->ix[i]==iCursor ) return 1<<i;
96 }
97 if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
98 pMaskSet->n++;
99 pMaskSet->ix[i] = iCursor;
100 return 1<<i;
101 }
102 return 0;
103}
104
105/*
106** Destroy an expression mask set
107*/
108#define freeMaskSet(P) /* NO-OP */
109
110/*
drh75897232000-05-29 14:26:00 +0000111** This routine walks (recursively) an expression tree and generates
112** a bitmask indicating which tables are used in that expression
drh6a3ea0e2003-05-02 14:32:12 +0000113** tree.
drh75897232000-05-29 14:26:00 +0000114**
115** In order for this routine to work, the calling function must have
danielk19774adee202004-05-08 08:23:19 +0000116** previously invoked sqlite3ExprResolveIds() on the expression. See
drh75897232000-05-29 14:26:00 +0000117** the header comment on that routine for additional information.
danielk19774adee202004-05-08 08:23:19 +0000118** The sqlite3ExprResolveIds() routines looks for column names and
drh6a3ea0e2003-05-02 14:32:12 +0000119** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
120** the VDBE cursor number of the table.
drh75897232000-05-29 14:26:00 +0000121*/
drh6a3ea0e2003-05-02 14:32:12 +0000122static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
drh75897232000-05-29 14:26:00 +0000123 unsigned int mask = 0;
124 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +0000125 if( p->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000126 return getMask(pMaskSet, p->iTable);
drh75897232000-05-29 14:26:00 +0000127 }
128 if( p->pRight ){
drh6a3ea0e2003-05-02 14:32:12 +0000129 mask = exprTableUsage(pMaskSet, p->pRight);
drh75897232000-05-29 14:26:00 +0000130 }
131 if( p->pLeft ){
drh6a3ea0e2003-05-02 14:32:12 +0000132 mask |= exprTableUsage(pMaskSet, p->pLeft);
drh75897232000-05-29 14:26:00 +0000133 }
drhdd579122002-04-02 01:58:57 +0000134 if( p->pList ){
135 int i;
136 for(i=0; i<p->pList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000137 mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
drhdd579122002-04-02 01:58:57 +0000138 }
139 }
drh75897232000-05-29 14:26:00 +0000140 return mask;
141}
142
143/*
drh487ab3c2001-11-08 00:45:21 +0000144** Return TRUE if the given operator is one of the operators that is
145** allowed for an indexable WHERE clause. The allowed operators are
drhc27a1ce2002-06-14 20:58:45 +0000146** "=", "<", ">", "<=", ">=", and "IN".
drh487ab3c2001-11-08 00:45:21 +0000147*/
148static int allowedOp(int op){
149 switch( op ){
150 case TK_LT:
151 case TK_LE:
152 case TK_GT:
153 case TK_GE:
154 case TK_EQ:
drhd99f7062002-06-08 23:25:08 +0000155 case TK_IN:
drh487ab3c2001-11-08 00:45:21 +0000156 return 1;
157 default:
158 return 0;
159 }
160}
161
162/*
drh75897232000-05-29 14:26:00 +0000163** The input to this routine is an ExprInfo structure with only the
164** "p" field filled in. The job of this routine is to analyze the
165** subexpression and populate all the other fields of the ExprInfo
166** structure.
167*/
drh6a3ea0e2003-05-02 14:32:12 +0000168static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000169 Expr *pExpr = pInfo->p;
drh6a3ea0e2003-05-02 14:32:12 +0000170 pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
171 pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
172 pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
drh75897232000-05-29 14:26:00 +0000173 pInfo->indexable = 0;
174 pInfo->idxLeft = -1;
175 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000176 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drhd99f7062002-06-08 23:25:08 +0000177 if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000178 pInfo->idxRight = pExpr->pRight->iTable;
drh75897232000-05-29 14:26:00 +0000179 pInfo->indexable = 1;
180 }
drh967e8b72000-06-21 13:59:10 +0000181 if( pExpr->pLeft->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000182 pInfo->idxLeft = pExpr->pLeft->iTable;
drh75897232000-05-29 14:26:00 +0000183 pInfo->indexable = 1;
184 }
185 }
186}
187
188/*
drhe3184742002-06-19 14:27:05 +0000189** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
190** left-most table in the FROM clause of that same SELECT statement and
191** the table has a cursor number of "base".
192**
193** This routine attempts to find an index for pTab that generates the
194** correct record sequence for the given ORDER BY clause. The return value
195** is a pointer to an index that does the job. NULL is returned if the
196** table has no index that will generate the correct sort order.
197**
198** If there are two or more indices that generate the correct sort order
199** and pPreferredIdx is one of those indices, then return pPreferredIdx.
drhdd4852c2002-12-04 21:50:16 +0000200**
201** nEqCol is the number of columns of pPreferredIdx that are used as
202** equality constraints. Any index returned must have exactly this same
203** set of columns. The ORDER BY clause only matches index columns beyond the
204** the first nEqCol columns.
205**
206** All terms of the ORDER BY clause must be either ASC or DESC. The
207** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
208** set to 0 if the ORDER BY clause is all ASC.
drhe3184742002-06-19 14:27:05 +0000209*/
210static Index *findSortingIndex(
211 Table *pTab, /* The table to be sorted */
212 int base, /* Cursor number for pTab */
213 ExprList *pOrderBy, /* The ORDER BY clause */
drhc045ec52002-12-04 20:01:06 +0000214 Index *pPreferredIdx, /* Use this index, if possible and not NULL */
drhdd4852c2002-12-04 21:50:16 +0000215 int nEqCol, /* Number of index columns used with == constraints */
drhc045ec52002-12-04 20:01:06 +0000216 int *pbRev /* Set to 1 if ORDER BY is DESC */
drhe3184742002-06-19 14:27:05 +0000217){
drhdd4852c2002-12-04 21:50:16 +0000218 int i, j;
drhe3184742002-06-19 14:27:05 +0000219 Index *pMatch;
220 Index *pIdx;
drhc045ec52002-12-04 20:01:06 +0000221 int sortOrder;
drhe3184742002-06-19 14:27:05 +0000222
223 assert( pOrderBy!=0 );
224 assert( pOrderBy->nExpr>0 );
drhc045ec52002-12-04 20:01:06 +0000225 sortOrder = pOrderBy->a[0].sortOrder & SQLITE_SO_DIRMASK;
drhe3184742002-06-19 14:27:05 +0000226 for(i=0; i<pOrderBy->nExpr; i++){
227 Expr *p;
drhc045ec52002-12-04 20:01:06 +0000228 if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){
229 /* Indices can only be used if all ORDER BY terms are either
230 ** DESC or ASC. Indices cannot be used on a mixture. */
drhe3184742002-06-19 14:27:05 +0000231 return 0;
232 }
drhc330af12002-08-14 03:03:57 +0000233 if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){
234 /* Do not sort by index if there is a COLLATE clause */
235 return 0;
236 }
drhe3184742002-06-19 14:27:05 +0000237 p = pOrderBy->a[i].pExpr;
238 if( p->op!=TK_COLUMN || p->iTable!=base ){
239 /* Can not use an index sort on anything that is not a column in the
240 ** left-most table of the FROM clause */
241 return 0;
242 }
243 }
drhc045ec52002-12-04 20:01:06 +0000244
drhe3184742002-06-19 14:27:05 +0000245 /* If we get this far, it means the ORDER BY clause consists only of
246 ** ascending columns in the left-most table of the FROM clause. Now
247 ** check for a matching index.
248 */
249 pMatch = 0;
250 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhdd4852c2002-12-04 21:50:16 +0000251 int nExpr = pOrderBy->nExpr;
252 if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
253 for(i=j=0; i<nEqCol; i++){
254 if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
255 if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; }
drhe3184742002-06-19 14:27:05 +0000256 }
drhdd4852c2002-12-04 21:50:16 +0000257 if( i<nEqCol ) continue;
258 for(i=0; i+j<nExpr; i++){
259 if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break;
260 }
261 if( i+j>=nExpr ){
drhe3184742002-06-19 14:27:05 +0000262 pMatch = pIdx;
263 if( pIdx==pPreferredIdx ) break;
264 }
265 }
drhc045ec52002-12-04 20:01:06 +0000266 if( pMatch && pbRev ){
267 *pbRev = sortOrder==SQLITE_SO_DESC;
268 }
drhe3184742002-06-19 14:27:05 +0000269 return pMatch;
270}
271
272/*
273** Generate the beginning of the loop used for WHERE clause processing.
drh75897232000-05-29 14:26:00 +0000274** The return value is a pointer to an (opaque) structure that contains
275** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000276** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000277** in order to complete the WHERE clause processing.
278**
279** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000280**
281** The basic idea is to do a nested loop, one loop for each table in
282** the FROM clause of a select. (INSERT and UPDATE statements are the
283** same as a SELECT with only a single table in the FROM clause.) For
284** example, if the SQL is this:
285**
286** SELECT * FROM t1, t2, t3 WHERE ...;
287**
288** Then the code generated is conceptually like the following:
289**
290** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000291** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000292** foreach row3 in t3 do /
293** ...
294** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000295** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000296** end /
297**
298** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000299** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
300** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000301** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000302**
303** If the WHERE clause is empty, the foreach loops must each scan their
304** entire tables. Thus a three-way join is an O(N^3) operation. But if
305** the tables have indices and there are terms in the WHERE clause that
306** refer to those indices, a complete table scan can be avoided and the
307** code will run much faster. Most of the work of this routine is checking
308** to see if there are indices that can be used to speed up the loop.
309**
310** Terms of the WHERE clause are also used to limit which rows actually
311** make it to the "..." in the middle of the loop. After each "foreach",
312** terms of the WHERE clause that use only terms in that loop and outer
313** loops are evaluated and if false a jump is made around all subsequent
314** inner loops (or around the "..." if the test occurs within the inner-
315** most loop)
316**
317** OUTER JOINS
318**
319** An outer join of tables t1 and t2 is conceptally coded as follows:
320**
321** foreach row1 in t1 do
322** flag = 0
323** foreach row2 in t2 do
324** start:
325** ...
326** flag = 1
327** end
drhe3184742002-06-19 14:27:05 +0000328** if flag==0 then
329** move the row2 cursor to a null row
330** goto start
331** fi
drhc27a1ce2002-06-14 20:58:45 +0000332** end
333**
drhe3184742002-06-19 14:27:05 +0000334** ORDER BY CLAUSE PROCESSING
335**
336** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
337** if there is one. If there is no ORDER BY clause or if this routine
338** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
339**
340** If an index can be used so that the natural output order of the table
341** scan is correct for the ORDER BY clause, then that index is used and
342** *ppOrderBy is set to NULL. This is an optimization that prevents an
343** unnecessary sort of the result set if an index appropriate for the
344** ORDER BY clause already exists.
345**
346** If the where clause loops cannot be arranged to provide the correct
347** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000348*/
danielk19774adee202004-05-08 08:23:19 +0000349WhereInfo *sqlite3WhereBegin(
drh75897232000-05-29 14:26:00 +0000350 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000351 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000352 Expr *pWhere, /* The WHERE clause */
drhe3184742002-06-19 14:27:05 +0000353 int pushKey, /* If TRUE, leave the table key on the stack */
354 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000355){
356 int i; /* Loop counter */
357 WhereInfo *pWInfo; /* Will become the return value of this function */
358 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000359 int brk, cont = 0; /* Addresses used during code generation */
drh75897232000-05-29 14:26:00 +0000360 int nExpr; /* Number of subexpressions in the WHERE clause */
361 int loopMask; /* One bit set for each outer loop */
362 int haveKey; /* True if KEY is on the stack */
drh6a3ea0e2003-05-02 14:32:12 +0000363 ExprMaskSet maskSet; /* The expression mask set */
drh8aff1012001-12-22 14:49:24 +0000364 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
365 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
366 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh83dcb1a2002-06-28 01:02:38 +0000367 ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
drh75897232000-05-29 14:26:00 +0000368
drhc27a1ce2002-06-14 20:58:45 +0000369 /* pushKey is only allowed if there is a single table (as in an INSERT or
370 ** UPDATE statement)
371 */
372 assert( pushKey==0 || pTabList->nSrc==1 );
drh83dcb1a2002-06-28 01:02:38 +0000373
374 /* Split the WHERE clause into separate subexpressions where each
375 ** subexpression is separated by an AND operator. If the aExpr[]
376 ** array fills up, the last entry might point to an expression which
377 ** contains additional unfactored AND operators.
378 */
drh6a3ea0e2003-05-02 14:32:12 +0000379 initMaskSet(&maskSet);
drh83dcb1a2002-06-28 01:02:38 +0000380 memset(aExpr, 0, sizeof(aExpr));
381 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
382 if( nExpr==ARRAYSIZE(aExpr) ){
danielk19774adee202004-05-08 08:23:19 +0000383 sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more "
drhf7a9e1a2004-02-22 18:40:56 +0000384 "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000385 return 0;
386 }
drhc27a1ce2002-06-14 20:58:45 +0000387
drh75897232000-05-29 14:26:00 +0000388 /* Allocate and initialize the WhereInfo structure that will become the
389 ** return value.
390 */
drhad3cab52002-05-24 02:04:32 +0000391 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000392 if( sqlite3_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +0000393 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000394 return 0;
395 }
396 pWInfo->pParse = pParse;
397 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000398 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000399 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000400
401 /* Special case: a WHERE clause that is constant. Evaluate the
402 ** expression and either jump over all of the code or fall thru.
403 */
danielk19774adee202004-05-08 08:23:19 +0000404 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
405 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000406 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000407 }
drh75897232000-05-29 14:26:00 +0000408
drh75897232000-05-29 14:26:00 +0000409 /* Analyze all of the subexpressions.
410 */
411 for(i=0; i<nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000412 exprAnalyze(&maskSet, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000413
414 /* If we are executing a trigger body, remove all references to
415 ** new.* and old.* tables from the prerequisite masks.
416 */
417 if( pParse->trigStack ){
418 int x;
419 if( (x = pParse->trigStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000420 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000421 aExpr[i].prereqRight &= mask;
422 aExpr[i].prereqLeft &= mask;
423 aExpr[i].prereqAll &= mask;
424 }
425 if( (x = pParse->trigStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000426 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000427 aExpr[i].prereqRight &= mask;
428 aExpr[i].prereqLeft &= mask;
429 aExpr[i].prereqAll &= mask;
430 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000431 }
drh75897232000-05-29 14:26:00 +0000432 }
433
drh75897232000-05-29 14:26:00 +0000434 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000435 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000436 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000437 ** loop.
438 **
439 ** If terms exist that use the ROWID of any table, then set the
440 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
441 ** to the index of the term containing the ROWID. We always prefer
442 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000443 ** index which requires reading an index first to get the rowid then
444 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000445 **
446 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000447 ** first 32 tables are candidates for indices. This is (again) due
448 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000449 */
450 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000451 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000452 int j;
drh6a3ea0e2003-05-02 14:32:12 +0000453 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
454 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
455 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000456 Index *pIdx;
457 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000458 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000459
drhc4a3c772001-04-04 11:48:57 +0000460 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000461 ** ROWID field of this table. For terms of the form ROWID==expr
462 ** set iDirectEq[i] to the index of the term. For terms of the
463 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
464 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000465 **
466 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000467 */
drhc8f8b632002-09-30 12:36:26 +0000468 pWInfo->a[i].iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000469 iDirectEq[i] = -1;
470 iDirectLt[i] = -1;
471 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000472 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000473 if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000474 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000475 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000476 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000477 case TK_EQ: iDirectEq[i] = j; break;
478 case TK_LE:
479 case TK_LT: iDirectLt[i] = j; break;
480 case TK_GE:
481 case TK_GT: iDirectGt[i] = j; break;
482 }
drhc4a3c772001-04-04 11:48:57 +0000483 }
drh6a3ea0e2003-05-02 14:32:12 +0000484 if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000485 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000486 switch( aExpr[j].p->op ){
487 case TK_EQ: iDirectEq[i] = j; break;
488 case TK_LE:
489 case TK_LT: iDirectGt[i] = j; break;
490 case TK_GE:
491 case TK_GT: iDirectLt[i] = j; break;
492 }
drhc4a3c772001-04-04 11:48:57 +0000493 }
494 }
drh8aff1012001-12-22 14:49:24 +0000495 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000496 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000497 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000498 continue;
499 }
500
drh75897232000-05-29 14:26:00 +0000501 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000502 ** the "best" index. pBestIdx is left set to NULL if no indices
503 ** are usable.
drh75897232000-05-29 14:26:00 +0000504 **
drh487ab3c2001-11-08 00:45:21 +0000505 ** The best index is determined as follows. For each of the
506 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000507 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000508 ** constrained by an inequality. Add 1 if for an "x<..." constraint
509 ** and add 2 for an "x>..." constraint. Chose the index that
510 ** gives the best score.
511 **
512 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000513 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000514 ** then all constraints are equalities. If score&1 is not 0 then
515 ** there is an inequality used as a termination key. (ex: "x<...")
516 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000517 ** start key. (ex: "x>..."). A score or 4 is the special case
518 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000519 **
drhc27a1ce2002-06-14 20:58:45 +0000520 ** The IN operator (as in "<expr> IN (...)") is treated the same as
521 ** an equality comparison except that it can only be used on the
522 ** left-most column of an index and other terms of the WHERE clause
523 ** cannot be used in conjunction with the IN operator to help satisfy
524 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000525 */
526 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000527 int eqMask = 0; /* Index columns covered by an x=... term */
528 int ltMask = 0; /* Index columns covered by an x<... term */
529 int gtMask = 0; /* Index columns covered by an x>... term */
530 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000531 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000532
drh487ab3c2001-11-08 00:45:21 +0000533 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000534 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000535 if( aExpr[j].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000536 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000537 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000538 int k;
danielk1977e014a832004-05-17 10:48:57 +0000539 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000540 for(k=0; k<pIdx->nColumn; k++){
danielk1977e014a832004-05-17 10:48:57 +0000541 if( pIdx->aiColumn[k]==iColumn
542 && sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ){
drh487ab3c2001-11-08 00:45:21 +0000543 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000544 case TK_IN: {
545 if( k==0 ) inMask |= 1;
546 break;
547 }
drh487ab3c2001-11-08 00:45:21 +0000548 case TK_EQ: {
549 eqMask |= 1<<k;
550 break;
551 }
552 case TK_LE:
553 case TK_LT: {
554 ltMask |= 1<<k;
555 break;
556 }
557 case TK_GE:
558 case TK_GT: {
559 gtMask |= 1<<k;
560 break;
561 }
562 default: {
563 /* CANT_HAPPEN */
564 assert( 0 );
565 break;
566 }
567 }
drh75897232000-05-29 14:26:00 +0000568 break;
569 }
570 }
571 }
drh6a3ea0e2003-05-02 14:32:12 +0000572 if( aExpr[j].idxRight==iCur
drh75897232000-05-29 14:26:00 +0000573 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000574 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000575 int k;
danielk1977e014a832004-05-17 10:48:57 +0000576 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000577 for(k=0; k<pIdx->nColumn; k++){
danielk1977e014a832004-05-17 10:48:57 +0000578 if( pIdx->aiColumn[k]==iColumn
579 && sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ){
drh487ab3c2001-11-08 00:45:21 +0000580 switch( aExpr[j].p->op ){
581 case TK_EQ: {
582 eqMask |= 1<<k;
583 break;
584 }
585 case TK_LE:
586 case TK_LT: {
587 gtMask |= 1<<k;
588 break;
589 }
590 case TK_GE:
591 case TK_GT: {
592 ltMask |= 1<<k;
593 break;
594 }
595 default: {
596 /* CANT_HAPPEN */
597 assert( 0 );
598 break;
599 }
600 }
drh75897232000-05-29 14:26:00 +0000601 break;
602 }
603 }
604 }
605 }
drhc045ec52002-12-04 20:01:06 +0000606
607 /* The following loop ends with nEq set to the number of columns
608 ** on the left of the index with == constraints.
609 */
drh487ab3c2001-11-08 00:45:21 +0000610 for(nEq=0; nEq<pIdx->nColumn; nEq++){
611 m = (1<<(nEq+1))-1;
612 if( (m & eqMask)!=m ) break;
613 }
drhc045ec52002-12-04 20:01:06 +0000614 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000615 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000616 if( m & ltMask ) score++; /* Increase score for a < constraint */
617 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
618 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000619 if( score>bestScore ){
620 pBestIdx = pIdx;
621 bestScore = score;
drh75897232000-05-29 14:26:00 +0000622 }
623 }
drh6b563442001-11-07 16:48:26 +0000624 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000625 pWInfo->a[i].score = bestScore;
drhc045ec52002-12-04 20:01:06 +0000626 pWInfo->a[i].bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000627 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000628 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000629 pWInfo->a[i].iCur = pParse->nTab++;
630 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000631 }
drh75897232000-05-29 14:26:00 +0000632 }
633
drhe3184742002-06-19 14:27:05 +0000634 /* Check to see if the ORDER BY clause is or can be satisfied by the
635 ** use of an index on the first table.
636 */
637 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
638 Index *pSortIdx;
639 Index *pIdx;
640 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000641 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000642
643 pTab = pTabList->a[0].pTab;
644 pIdx = pWInfo->a[0].pIdx;
645 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000646 /* If there is already an IN index on the left-most table,
647 ** it will not give the correct sort order.
648 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000649 */
650 pSortIdx = 0;
651 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
652 /* If the left-most column is accessed using its ROWID, then do
653 ** not try to sort by index.
654 */
655 pSortIdx = 0;
656 }else{
drhdd4852c2002-12-04 21:50:16 +0000657 int nEqCol = (pWInfo->a[0].score+4)/8;
drh6a3ea0e2003-05-02 14:32:12 +0000658 pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
659 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000660 }
661 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
662 if( pIdx==0 ){
663 pWInfo->a[0].pIdx = pSortIdx;
664 pWInfo->a[0].iCur = pParse->nTab++;
665 pWInfo->peakNTab = pParse->nTab;
666 }
drhc045ec52002-12-04 20:01:06 +0000667 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000668 *ppOrderBy = 0;
669 }
670 }
671
drh6b563442001-11-07 16:48:26 +0000672 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000673 */
drhad3cab52002-05-24 02:04:32 +0000674 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000675 Table *pTab;
drh701a0ae2004-02-22 20:05:00 +0000676 Index *pIx;
drhf57b3392001-10-08 13:22:32 +0000677
678 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000679 if( pTab->isTransient || pTab->pSelect ) continue;
danielk19774adee202004-05-08 08:23:19 +0000680 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
681 sqlite3VdbeOp3(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum,
drh701a0ae2004-02-22 20:05:00 +0000682 pTab->zName, P3_STATIC);
danielk1977b4964b72004-05-18 01:23:38 +0000683 sqlite3VdbeAddOp(v, OP_SetNumColumns, pTabList->a[i].iCursor, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +0000684 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh701a0ae2004-02-22 20:05:00 +0000685 if( (pIx = pWInfo->a[i].pIdx)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000686 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
drh772ae622004-05-19 13:13:08 +0000687 sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum,pIx->zName,0);
drh75897232000-05-29 14:26:00 +0000688 }
689 }
690
691 /* Generate the code to do the search
692 */
drh75897232000-05-29 14:26:00 +0000693 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000694 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000695 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000696 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000697 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000698 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000699
drhad2d8302002-05-24 20:31:36 +0000700 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000701 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000702 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000703 */
704 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
705 if( !pParse->nMem ) pParse->nMem++;
706 pLevel->iLeftJoin = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000707 sqlite3VdbeAddOp(v, OP_String, 0, 0);
708 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad2d8302002-05-24 20:31:36 +0000709 }
710
drh8aff1012001-12-22 14:49:24 +0000711 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000712 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000713 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
714 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000715 ** equality comparison against the ROWID field. Or
716 ** we reference multiple rows using a "rowid IN (...)"
717 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000718 */
drh8aff1012001-12-22 14:49:24 +0000719 k = iDirectEq[i];
720 assert( k<nExpr );
721 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000722 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
danielk19774adee202004-05-08 08:23:19 +0000723 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000724 if( aExpr[k].idxLeft==iCur ){
drhd99f7062002-06-08 23:25:08 +0000725 Expr *pX = aExpr[k].p;
726 if( pX->op!=TK_IN ){
danielk19774adee202004-05-08 08:23:19 +0000727 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drhd99f7062002-06-08 23:25:08 +0000728 }else{
danielk19774adee202004-05-08 08:23:19 +0000729 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
730 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000731 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000732 pLevel->inOp = OP_Next;
733 pLevel->inP1 = pX->iTable;
734 }
drh8aff1012001-12-22 14:49:24 +0000735 }else{
danielk19774adee202004-05-08 08:23:19 +0000736 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000737 }
drh8aff1012001-12-22 14:49:24 +0000738 aExpr[k].p = 0;
danielk19774adee202004-05-08 08:23:19 +0000739 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
740 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000741 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +0000742 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000743 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000744 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000745 /* Case 2: There is an index and all terms of the WHERE clause that
746 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000747 */
drh6b563442001-11-07 16:48:26 +0000748 int start;
drhc045ec52002-12-04 20:01:06 +0000749 int nColumn = (pLevel->score+4)/8;
danielk19774adee202004-05-08 08:23:19 +0000750 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000751
752 /* For each column of the index, find the term of the WHERE clause that
753 ** constraints that column. If the WHERE clause term is X=expr, then
754 ** evaluation expr and leave the result on the stack */
drh487ab3c2001-11-08 00:45:21 +0000755 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000756 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000757 Expr *pX = aExpr[k].p;
758 if( pX==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000759 if( aExpr[k].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000760 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000761 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000762 ){
danielk1977e014a832004-05-17 10:48:57 +0000763 char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
764 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
765 if( pX->op==TK_EQ ){
766 sqlite3ExprCode(pParse, pX->pRight);
767 aExpr[k].p = 0;
768 break;
769 }
770 if( pX->op==TK_IN && nColumn==1 ){
danielk19774adee202004-05-08 08:23:19 +0000771 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
772 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000773 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000774 pLevel->inOp = OP_Next;
775 pLevel->inP1 = pX->iTable;
danielk1977e014a832004-05-17 10:48:57 +0000776 aExpr[k].p = 0;
777 break;
drhd99f7062002-06-08 23:25:08 +0000778 }
drhd99f7062002-06-08 23:25:08 +0000779 }
drh75897232000-05-29 14:26:00 +0000780 }
drh6a3ea0e2003-05-02 14:32:12 +0000781 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000782 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000783 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000784 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000785 ){
danielk1977e014a832004-05-17 10:48:57 +0000786 char idxaff = pIdx->pTable->aCol[pX->pRight->iColumn].affinity;
787 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
788 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
789 aExpr[k].p = 0;
790 break;
791 }
drh75897232000-05-29 14:26:00 +0000792 }
793 }
794 }
drh6b563442001-11-07 16:48:26 +0000795 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000796 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000797
798 /* At this point, the top nColumn elements of the stack are the
799 ** values of columns in the index we are using. Check to see if
800 ** any of these values are NULL. If they are, they will not match any
801 ** index entries, so skip immediately to the next iteration of the loop */
danielk19774adee202004-05-08 08:23:19 +0000802 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
803 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
804 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
drh772ae622004-05-19 13:13:08 +0000805
806 /* Generate an index key from the top nColumn elements of the stack */
danielk19774adee202004-05-08 08:23:19 +0000807 sqlite3VdbeAddOp(v, OP_MakeKey, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000808 sqlite3IndexAffinityStr(v, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +0000809 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drh772ae622004-05-19 13:13:08 +0000810
drh772ae622004-05-19 13:13:08 +0000811 /* Generate code (1) to move to the first matching element of the table.
812 ** Then generate code (2) that jumps to "brk" after the cursor is past
813 ** the last matching element of the table. The code (1) is executed
814 ** once to initialize the search, the code (2) is executed before each
815 ** iteration of the scan to see if the scan has finished. */
drhc045ec52002-12-04 20:01:06 +0000816 if( pLevel->bRev ){
817 /* Scan in reverse order */
drh7cf6e4d2004-05-19 14:56:55 +0000818 sqlite3VdbeAddOp(v, OP_MoveLe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000819 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
820 sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000821 pLevel->op = OP_Prev;
822 }else{
823 /* Scan in the forward order */
drh7cf6e4d2004-05-19 14:56:55 +0000824 sqlite3VdbeAddOp(v, OP_MoveGe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000825 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drhfec19aa2004-05-19 20:41:03 +0000826 sqlite3VdbeOp3(v, OP_IdxGE, pLevel->iCur, brk, "+", P3_STATIC);
drhc045ec52002-12-04 20:01:06 +0000827 pLevel->op = OP_Next;
828 }
danielk19774adee202004-05-08 08:23:19 +0000829 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
830 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
831 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000832 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000833 haveKey = 1;
834 }else{
drh7cf6e4d2004-05-19 14:56:55 +0000835 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +0000836 haveKey = 0;
837 }
drh6b563442001-11-07 16:48:26 +0000838 pLevel->p1 = pLevel->iCur;
839 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000840 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
841 /* Case 3: We have an inequality comparison against the ROWID field.
842 */
843 int testOp = OP_Noop;
844 int start;
845
danielk19774adee202004-05-08 08:23:19 +0000846 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
847 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000848 if( iDirectGt[i]>=0 ){
849 k = iDirectGt[i];
850 assert( k<nExpr );
851 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000852 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
853 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000854 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000855 }else{
danielk19774adee202004-05-08 08:23:19 +0000856 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000857 }
danielk19774adee202004-05-08 08:23:19 +0000858 sqlite3VdbeAddOp(v, OP_ForceInt,
drh751f4122004-01-14 21:59:22 +0000859 aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
drh7cf6e4d2004-05-19 14:56:55 +0000860 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000861 aExpr[k].p = 0;
862 }else{
danielk19774adee202004-05-08 08:23:19 +0000863 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000864 }
865 if( iDirectLt[i]>=0 ){
866 k = iDirectLt[i];
867 assert( k<nExpr );
868 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000869 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
870 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000871 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000872 }else{
danielk19774adee202004-05-08 08:23:19 +0000873 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000874 }
danielk19774adee202004-05-08 08:23:19 +0000875 /* sqlite3VdbeAddOp(v, OP_MustBeInt, 0, sqlite3VdbeCurrentAddr(v)+1); */
drh8aff1012001-12-22 14:49:24 +0000876 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000877 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh8aff1012001-12-22 14:49:24 +0000878 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
879 testOp = OP_Ge;
880 }else{
881 testOp = OP_Gt;
882 }
883 aExpr[k].p = 0;
884 }
danielk19774adee202004-05-08 08:23:19 +0000885 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000886 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000887 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000888 pLevel->p2 = start;
889 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +0000890 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
891 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
892 sqlite3VdbeAddOp(v, testOp, 0, brk);
drh8aff1012001-12-22 14:49:24 +0000893 }
894 haveKey = 0;
895 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000896 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000897 ** scan of the entire database table.
898 */
899 int start;
900
danielk19774adee202004-05-08 08:23:19 +0000901 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
902 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
903 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
904 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000905 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000906 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000907 pLevel->p2 = start;
908 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000909 }else{
drhc27a1ce2002-06-14 20:58:45 +0000910 /* Case 5: The WHERE clause term that refers to the right-most
911 ** column of the index is an inequality. For example, if
912 ** the index is on (x,y,z) and the WHERE clause is of the
913 ** form "x=5 AND y<10" then this case is used. Only the
914 ** right-most column can be an inequality - the rest must
915 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000916 **
917 ** This case is also used when there are no WHERE clause
918 ** constraints but an index is selected anyway, in order
919 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000920 */
921 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000922 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000923 int start;
924 int leFlag, geFlag;
925 int testOp;
926
927 /* Evaluate the equality constraints
928 */
929 for(j=0; j<nEqColumn; j++){
930 for(k=0; k<nExpr; k++){
931 if( aExpr[k].p==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000932 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000933 && aExpr[k].p->op==TK_EQ
934 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
935 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
936 ){
danielk19774adee202004-05-08 08:23:19 +0000937 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh143f3c42004-01-07 20:37:52 +0000938 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000939 break;
940 }
drh6a3ea0e2003-05-02 14:32:12 +0000941 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000942 && aExpr[k].p->op==TK_EQ
943 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
944 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
945 ){
danielk19774adee202004-05-08 08:23:19 +0000946 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000947 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000948 break;
949 }
950 }
951 }
952
drhc27a1ce2002-06-14 20:58:45 +0000953 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000954 ** used twice: once to make the termination key and once to make the
955 ** start key.
956 */
957 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +0000958 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +0000959 }
960
drhc045ec52002-12-04 20:01:06 +0000961 /* Labels for the beginning and end of the loop
962 */
danielk19774adee202004-05-08 08:23:19 +0000963 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
964 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +0000965
drh487ab3c2001-11-08 00:45:21 +0000966 /* Generate the termination key. This is the key value that
967 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000968 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +0000969 **
970 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
971 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +0000972 */
973 if( (score & 1)!=0 ){
974 for(k=0; k<nExpr; k++){
975 Expr *pExpr = aExpr[k].p;
976 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000977 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000978 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
979 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
980 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
981 ){
danielk19774adee202004-05-08 08:23:19 +0000982 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +0000983 leFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +0000984 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000985 break;
986 }
drh6a3ea0e2003-05-02 14:32:12 +0000987 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000988 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
989 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
990 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
991 ){
danielk19774adee202004-05-08 08:23:19 +0000992 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +0000993 leFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +0000994 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000995 break;
996 }
997 }
998 testOp = OP_IdxGE;
999 }else{
1000 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1001 leFlag = 1;
1002 }
1003 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +00001004 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +00001005 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001006 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1007 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1008 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1009 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001010 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001011 if( pLevel->bRev ){
drh7cf6e4d2004-05-19 14:56:55 +00001012 int op = leFlag ? OP_MoveLe : OP_MoveLt;
1013 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001014 }else{
danielk19774adee202004-05-08 08:23:19 +00001015 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001016 }
1017 }else if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001018 sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001019 }
1020
1021 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001022 ** bound on the search. There is no start key if there are no
1023 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001024 ** that case, generate a "Rewind" instruction in place of the
1025 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001026 **
1027 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1028 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001029 */
1030 if( (score & 2)!=0 ){
1031 for(k=0; k<nExpr; k++){
1032 Expr *pExpr = aExpr[k].p;
1033 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001034 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001035 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1036 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1037 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1038 ){
danielk19774adee202004-05-08 08:23:19 +00001039 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +00001040 geFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001041 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001042 break;
1043 }
drh6a3ea0e2003-05-02 14:32:12 +00001044 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001045 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1046 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1047 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1048 ){
danielk19774adee202004-05-08 08:23:19 +00001049 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +00001050 geFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001051 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001052 break;
1053 }
1054 }
drh7900ead2001-11-12 13:51:43 +00001055 }else{
1056 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001057 }
drh487ab3c2001-11-08 00:45:21 +00001058 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001059 int nCol = nEqColumn + ((score&2)!=0);
danielk19774adee202004-05-08 08:23:19 +00001060 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1061 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1062 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1063 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001064 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001065 if( pLevel->bRev ){
1066 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001067 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001068 testOp = OP_IdxLT;
1069 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001070 int op = geFlag ? OP_MoveGe : OP_MoveGt;
1071 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001072 }
1073 }else if( pLevel->bRev ){
1074 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001075 }else{
danielk19774adee202004-05-08 08:23:19 +00001076 sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001077 }
1078
1079 /* Generate the the top of the loop. If there is a termination
1080 ** key we have to test for that key and abort at the top of the
1081 ** loop.
1082 */
danielk19774adee202004-05-08 08:23:19 +00001083 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001084 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001085 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1086 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001087 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1088 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1089 }
drh487ab3c2001-11-08 00:45:21 +00001090 }
danielk19774adee202004-05-08 08:23:19 +00001091 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1092 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1093 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001094 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001095 haveKey = 1;
1096 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001097 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001098 haveKey = 0;
1099 }
1100
1101 /* Record the instruction used to terminate the loop.
1102 */
drhc045ec52002-12-04 20:01:06 +00001103 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001104 pLevel->p1 = pLevel->iCur;
1105 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001106 }
drh6a3ea0e2003-05-02 14:32:12 +00001107 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001108
1109 /* Insert code to test every subexpression that can be completely
1110 ** computed using the current set of tables.
1111 */
1112 for(j=0; j<nExpr; j++){
1113 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +00001114 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh1f162302002-10-27 19:35:33 +00001115 if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1116 continue;
1117 }
drh75897232000-05-29 14:26:00 +00001118 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001119 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001120 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +00001121 }
danielk19774adee202004-05-08 08:23:19 +00001122 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +00001123 aExpr[j].p = 0;
1124 }
1125 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001126
1127 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1128 ** at least one row of the right table has matched the left table.
1129 */
1130 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001131 pLevel->top = sqlite3VdbeCurrentAddr(v);
1132 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1133 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drh1cc093c2002-06-24 22:01:57 +00001134 for(j=0; j<nExpr; j++){
1135 if( aExpr[j].p==0 ) continue;
1136 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1137 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001138 /* Cannot happen. "haveKey" can only be true if pushKey is true
1139 ** an pushKey can only be true for DELETE and UPDATE and there are
1140 ** no outer joins with DELETE and UPDATE.
1141 */
drh1cc093c2002-06-24 22:01:57 +00001142 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001143 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001144 }
danielk19774adee202004-05-08 08:23:19 +00001145 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh1cc093c2002-06-24 22:01:57 +00001146 aExpr[j].p = 0;
1147 }
drhad2d8302002-05-24 20:31:36 +00001148 }
drh75897232000-05-29 14:26:00 +00001149 }
1150 pWInfo->iContinue = cont;
1151 if( pushKey && !haveKey ){
danielk19774adee202004-05-08 08:23:19 +00001152 sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001153 }
drh6a3ea0e2003-05-02 14:32:12 +00001154 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001155 return pWInfo;
1156}
1157
1158/*
drhc27a1ce2002-06-14 20:58:45 +00001159** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001160** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001161*/
danielk19774adee202004-05-08 08:23:19 +00001162void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001163 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001164 int i;
drh6b563442001-11-07 16:48:26 +00001165 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001166 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001167
drhad3cab52002-05-24 02:04:32 +00001168 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001169 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001170 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001171 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001172 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001173 }
danielk19774adee202004-05-08 08:23:19 +00001174 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001175 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001176 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001177 }
drhad2d8302002-05-24 20:31:36 +00001178 if( pLevel->iLeftJoin ){
1179 int addr;
danielk19774adee202004-05-08 08:23:19 +00001180 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1181 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1182 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001183 if( pLevel->iCur>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001184 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001185 }
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001187 }
drh19a775c2000-06-05 18:54:46 +00001188 }
danielk19774adee202004-05-08 08:23:19 +00001189 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001190 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001191 Table *pTab = pTabList->a[i].pTab;
1192 assert( pTab!=0 );
1193 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001194 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001195 sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001196 if( pLevel->pIdx!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001197 sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
drh6b563442001-11-07 16:48:26 +00001198 }
drh19a775c2000-06-05 18:54:46 +00001199 }
drh142e30d2002-08-28 03:00:58 +00001200#if 0 /* Never reuse a cursor */
drh832508b2002-03-02 17:04:07 +00001201 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1202 pWInfo->pParse->nTab = pWInfo->savedNTab;
1203 }
drh142e30d2002-08-28 03:00:58 +00001204#endif
drh75897232000-05-29 14:26:00 +00001205 sqliteFree(pWInfo);
1206 return;
1207}