blob: b93e2ceb21ca1c7bcff4bc812a6738809c1e74c4 [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**
drh143f3c42004-01-07 20:37:52 +000015** $Id: where.c,v 1.86 2004/01/07 20:37:52 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
116** previously invoked sqliteExprResolveIds() on the expression. See
117** the header comment on that routine for additional information.
drh6a3ea0e2003-05-02 14:32:12 +0000118** The sqliteExprResolveIds() routines looks for column names and
119** 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
276** should invoke sqliteWhereEnd() with the return value of this function
277** 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
291** foreach row2 in t2 do |-- by sqliteWhereBegin()
292** foreach row3 in t3 do /
293** ...
294** end \ Code generated
295** end |-- by sqliteWhereEnd()
296** 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
301** and sqliteWhereEnd() 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*/
349WhereInfo *sqliteWhereBegin(
350 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) ){
383 char zBuf[50];
drhfd159812003-01-11 14:25:39 +0000384 sprintf(zBuf, "%d", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000385 sqliteSetString(&pParse->zErrMsg, "WHERE clause too complex - no more "
drh41743982003-12-06 21:43:55 +0000386 "than ", zBuf, " terms allowed", (char*)0);
drh83dcb1a2002-06-28 01:02:38 +0000387 pParse->nErr++;
388 return 0;
389 }
drhc27a1ce2002-06-14 20:58:45 +0000390
drh75897232000-05-29 14:26:00 +0000391 /* Allocate and initialize the WhereInfo structure that will become the
392 ** return value.
393 */
drhad3cab52002-05-24 02:04:32 +0000394 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
drhdaffd0e2001-04-11 14:28:42 +0000395 if( sqlite_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +0000396 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000397 return 0;
398 }
399 pWInfo->pParse = pParse;
400 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000401 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
drh08192d52002-04-30 19:20:28 +0000402 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
403
404 /* Special case: a WHERE clause that is constant. Evaluate the
405 ** expression and either jump over all of the code or fall thru.
406 */
drh37ea94b2003-04-19 16:34:04 +0000407 if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){
drhf5905aa2002-05-26 20:54:33 +0000408 sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000409 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000410 }
drh75897232000-05-29 14:26:00 +0000411
drh75897232000-05-29 14:26:00 +0000412 /* Analyze all of the subexpressions.
413 */
414 for(i=0; i<nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000415 exprAnalyze(&maskSet, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000416
417 /* If we are executing a trigger body, remove all references to
418 ** new.* and old.* tables from the prerequisite masks.
419 */
420 if( pParse->trigStack ){
421 int x;
422 if( (x = pParse->trigStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000423 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000424 aExpr[i].prereqRight &= mask;
425 aExpr[i].prereqLeft &= mask;
426 aExpr[i].prereqAll &= mask;
427 }
428 if( (x = pParse->trigStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000429 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000430 aExpr[i].prereqRight &= mask;
431 aExpr[i].prereqLeft &= mask;
432 aExpr[i].prereqAll &= mask;
433 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000434 }
drh75897232000-05-29 14:26:00 +0000435 }
436
drh75897232000-05-29 14:26:00 +0000437 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000438 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000439 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000440 ** loop.
441 **
442 ** If terms exist that use the ROWID of any table, then set the
443 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
444 ** to the index of the term containing the ROWID. We always prefer
445 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000446 ** index which requires reading an index first to get the rowid then
447 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000448 **
449 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000450 ** first 32 tables are candidates for indices. This is (again) due
451 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000452 */
453 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000454 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000455 int j;
drh6a3ea0e2003-05-02 14:32:12 +0000456 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
457 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
458 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000459 Index *pIdx;
460 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000461 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000462
drhc4a3c772001-04-04 11:48:57 +0000463 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000464 ** ROWID field of this table. For terms of the form ROWID==expr
465 ** set iDirectEq[i] to the index of the term. For terms of the
466 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
467 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000468 **
469 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000470 */
drhc8f8b632002-09-30 12:36:26 +0000471 pWInfo->a[i].iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000472 iDirectEq[i] = -1;
473 iDirectLt[i] = -1;
474 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000475 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000476 if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000477 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000478 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000479 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000480 case TK_EQ: iDirectEq[i] = j; break;
481 case TK_LE:
482 case TK_LT: iDirectLt[i] = j; break;
483 case TK_GE:
484 case TK_GT: iDirectGt[i] = j; break;
485 }
drhc4a3c772001-04-04 11:48:57 +0000486 }
drh6a3ea0e2003-05-02 14:32:12 +0000487 if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000488 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000489 switch( aExpr[j].p->op ){
490 case TK_EQ: iDirectEq[i] = j; break;
491 case TK_LE:
492 case TK_LT: iDirectGt[i] = j; break;
493 case TK_GE:
494 case TK_GT: iDirectLt[i] = j; break;
495 }
drhc4a3c772001-04-04 11:48:57 +0000496 }
497 }
drh8aff1012001-12-22 14:49:24 +0000498 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000499 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000500 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000501 continue;
502 }
503
drh75897232000-05-29 14:26:00 +0000504 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000505 ** the "best" index. pBestIdx is left set to NULL if no indices
506 ** are usable.
drh75897232000-05-29 14:26:00 +0000507 **
drh487ab3c2001-11-08 00:45:21 +0000508 ** The best index is determined as follows. For each of the
509 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000510 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000511 ** constrained by an inequality. Add 1 if for an "x<..." constraint
512 ** and add 2 for an "x>..." constraint. Chose the index that
513 ** gives the best score.
514 **
515 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000516 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000517 ** then all constraints are equalities. If score&1 is not 0 then
518 ** there is an inequality used as a termination key. (ex: "x<...")
519 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000520 ** start key. (ex: "x>..."). A score or 4 is the special case
521 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000522 **
drhc27a1ce2002-06-14 20:58:45 +0000523 ** The IN operator (as in "<expr> IN (...)") is treated the same as
524 ** an equality comparison except that it can only be used on the
525 ** left-most column of an index and other terms of the WHERE clause
526 ** cannot be used in conjunction with the IN operator to help satisfy
527 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000528 */
529 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000530 int eqMask = 0; /* Index columns covered by an x=... term */
531 int ltMask = 0; /* Index columns covered by an x<... term */
532 int gtMask = 0; /* Index columns covered by an x>... term */
533 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000534 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000535
drh487ab3c2001-11-08 00:45:21 +0000536 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000537 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000538 if( aExpr[j].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000539 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000540 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000541 int k;
drh967e8b72000-06-21 13:59:10 +0000542 for(k=0; k<pIdx->nColumn; k++){
543 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000544 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000545 case TK_IN: {
546 if( k==0 ) inMask |= 1;
547 break;
548 }
drh487ab3c2001-11-08 00:45:21 +0000549 case TK_EQ: {
550 eqMask |= 1<<k;
551 break;
552 }
553 case TK_LE:
554 case TK_LT: {
555 ltMask |= 1<<k;
556 break;
557 }
558 case TK_GE:
559 case TK_GT: {
560 gtMask |= 1<<k;
561 break;
562 }
563 default: {
564 /* CANT_HAPPEN */
565 assert( 0 );
566 break;
567 }
568 }
drh75897232000-05-29 14:26:00 +0000569 break;
570 }
571 }
572 }
drh6a3ea0e2003-05-02 14:32:12 +0000573 if( aExpr[j].idxRight==iCur
drh75897232000-05-29 14:26:00 +0000574 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000575 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000576 int k;
drh967e8b72000-06-21 13:59:10 +0000577 for(k=0; k<pIdx->nColumn; k++){
578 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000579 switch( aExpr[j].p->op ){
580 case TK_EQ: {
581 eqMask |= 1<<k;
582 break;
583 }
584 case TK_LE:
585 case TK_LT: {
586 gtMask |= 1<<k;
587 break;
588 }
589 case TK_GE:
590 case TK_GT: {
591 ltMask |= 1<<k;
592 break;
593 }
594 default: {
595 /* CANT_HAPPEN */
596 assert( 0 );
597 break;
598 }
599 }
drh75897232000-05-29 14:26:00 +0000600 break;
601 }
602 }
603 }
604 }
drhc045ec52002-12-04 20:01:06 +0000605
606 /* The following loop ends with nEq set to the number of columns
607 ** on the left of the index with == constraints.
608 */
drh487ab3c2001-11-08 00:45:21 +0000609 for(nEq=0; nEq<pIdx->nColumn; nEq++){
610 m = (1<<(nEq+1))-1;
611 if( (m & eqMask)!=m ) break;
612 }
drhc045ec52002-12-04 20:01:06 +0000613 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000614 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000615 if( m & ltMask ) score++; /* Increase score for a < constraint */
616 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
617 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000618 if( score>bestScore ){
619 pBestIdx = pIdx;
620 bestScore = score;
drh75897232000-05-29 14:26:00 +0000621 }
622 }
drh6b563442001-11-07 16:48:26 +0000623 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000624 pWInfo->a[i].score = bestScore;
drhc045ec52002-12-04 20:01:06 +0000625 pWInfo->a[i].bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000626 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000627 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000628 pWInfo->a[i].iCur = pParse->nTab++;
629 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000630 }
drh75897232000-05-29 14:26:00 +0000631 }
632
drhe3184742002-06-19 14:27:05 +0000633 /* Check to see if the ORDER BY clause is or can be satisfied by the
634 ** use of an index on the first table.
635 */
636 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
637 Index *pSortIdx;
638 Index *pIdx;
639 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000640 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000641
642 pTab = pTabList->a[0].pTab;
643 pIdx = pWInfo->a[0].pIdx;
644 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000645 /* If there is already an IN index on the left-most table,
646 ** it will not give the correct sort order.
647 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000648 */
649 pSortIdx = 0;
650 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
651 /* If the left-most column is accessed using its ROWID, then do
652 ** not try to sort by index.
653 */
654 pSortIdx = 0;
655 }else{
drhdd4852c2002-12-04 21:50:16 +0000656 int nEqCol = (pWInfo->a[0].score+4)/8;
drh6a3ea0e2003-05-02 14:32:12 +0000657 pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
658 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000659 }
660 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
661 if( pIdx==0 ){
662 pWInfo->a[0].pIdx = pSortIdx;
663 pWInfo->a[0].iCur = pParse->nTab++;
664 pWInfo->peakNTab = pParse->nTab;
665 }
drhc045ec52002-12-04 20:01:06 +0000666 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000667 *ppOrderBy = 0;
668 }
669 }
670
drh6b563442001-11-07 16:48:26 +0000671 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000672 */
drhad3cab52002-05-24 02:04:32 +0000673 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000674 Table *pTab;
675
676 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000677 if( pTab->isTransient || pTab->pSelect ) continue;
drhd24cc422003-03-27 12:51:24 +0000678 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh6a3ea0e2003-05-02 14:32:12 +0000679 sqliteVdbeAddOp(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +0000680 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh8bf8dc92003-05-17 17:35:10 +0000681 sqliteCodeVerifySchema(pParse, pTab->iDb);
drh6b563442001-11-07 16:48:26 +0000682 if( pWInfo->a[i].pIdx!=0 ){
drhd24cc422003-03-27 12:51:24 +0000683 sqliteVdbeAddOp(v, OP_Integer, pWInfo->a[i].pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000684 sqliteVdbeAddOp(v, OP_OpenRead,
685 pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
drh6b563442001-11-07 16:48:26 +0000686 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000687 }
688 }
689
690 /* Generate the code to do the search
691 */
drh75897232000-05-29 14:26:00 +0000692 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000693 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000694 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000695 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000696 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000697 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000698
drhad2d8302002-05-24 20:31:36 +0000699 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000700 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000701 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000702 */
703 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
704 if( !pParse->nMem ) pParse->nMem++;
705 pLevel->iLeftJoin = pParse->nMem++;
706 sqliteVdbeAddOp(v, OP_String, 0, 0);
707 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
708 }
709
drh8aff1012001-12-22 14:49:24 +0000710 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000711 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000712 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
713 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000714 ** equality comparison against the ROWID field. Or
715 ** we reference multiple rows using a "rowid IN (...)"
716 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000717 */
drh8aff1012001-12-22 14:49:24 +0000718 k = iDirectEq[i];
719 assert( k<nExpr );
720 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000721 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
drhd99f7062002-06-08 23:25:08 +0000722 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000723 if( aExpr[k].idxLeft==iCur ){
drhd99f7062002-06-08 23:25:08 +0000724 Expr *pX = aExpr[k].p;
725 if( pX->op!=TK_IN ){
726 sqliteExprCode(pParse, aExpr[k].p->pRight);
727 }else if( pX->pList ){
728 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
729 pLevel->inOp = OP_SetNext;
730 pLevel->inP1 = pX->iTable;
731 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
732 }else{
733 assert( pX->pSelect );
734 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
735 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
736 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
737 pLevel->inOp = OP_Next;
738 pLevel->inP1 = pX->iTable;
739 }
drh8aff1012001-12-22 14:49:24 +0000740 }else{
741 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000742 }
drh8aff1012001-12-22 14:49:24 +0000743 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000744 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drhf1351b62002-07-31 19:50:26 +0000745 sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000746 haveKey = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000747 sqliteVdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000748 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000749 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000750 /* Case 2: There is an index and all terms of the WHERE clause that
751 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000752 */
drh6b563442001-11-07 16:48:26 +0000753 int start;
drh487ab3c2001-11-08 00:45:21 +0000754 int testOp;
drhc045ec52002-12-04 20:01:06 +0000755 int nColumn = (pLevel->score+4)/8;
drhd99f7062002-06-08 23:25:08 +0000756 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000757 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000758 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000759 Expr *pX = aExpr[k].p;
760 if( pX==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000761 if( aExpr[k].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000762 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000763 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000764 ){
drhd99f7062002-06-08 23:25:08 +0000765 if( pX->op==TK_EQ ){
766 sqliteExprCode(pParse, pX->pRight);
drh143f3c42004-01-07 20:37:52 +0000767 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000768 break;
769 }
770 if( pX->op==TK_IN && nColumn==1 ){
771 if( pX->pList ){
772 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
773 pLevel->inOp = OP_SetNext;
774 pLevel->inP1 = pX->iTable;
775 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
776 }else{
777 assert( pX->pSelect );
778 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
779 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
780 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
781 pLevel->inOp = OP_Next;
782 pLevel->inP1 = pX->iTable;
783 }
drh143f3c42004-01-07 20:37:52 +0000784 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000785 break;
786 }
drh75897232000-05-29 14:26:00 +0000787 }
drh6a3ea0e2003-05-02 14:32:12 +0000788 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000789 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000790 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000791 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000792 ){
793 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000794 aExpr[k].p = 0;
drh75897232000-05-29 14:26:00 +0000795 break;
796 }
797 }
798 }
drh6b563442001-11-07 16:48:26 +0000799 pLevel->iMem = pParse->nMem++;
drh6b563442001-11-07 16:48:26 +0000800 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh143f3c42004-01-07 20:37:52 +0000801 sqliteVdbeAddOp(v, OP_NotNull, -nColumn, sqliteVdbeCurrentAddr(v)+3);
802 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
803 sqliteVdbeAddOp(v, OP_Goto, 0, brk);
drh487ab3c2001-11-08 00:45:21 +0000804 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
drha9e99ae2002-08-13 23:02:57 +0000805 sqliteAddIdxKeyType(v, pIdx);
drhc045ec52002-12-04 20:01:06 +0000806 if( nColumn==pIdx->nColumn || pLevel->bRev ){
drh487ab3c2001-11-08 00:45:21 +0000807 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
808 testOp = OP_IdxGT;
809 }else{
810 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
811 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
812 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
813 testOp = OP_IdxGE;
814 }
drhc045ec52002-12-04 20:01:06 +0000815 if( pLevel->bRev ){
816 /* Scan in reverse order */
817 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
818 sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
819 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
820 sqliteVdbeAddOp(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 */
824 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
825 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
826 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000827 pLevel->op = OP_Next;
828 }
drh143f3c42004-01-07 20:37:52 +0000829 sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
830 sqliteVdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
831 sqliteVdbeAddOp(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{
drh6a3ea0e2003-05-02 14:32:12 +0000835 sqliteVdbeAddOp(v, OP_MoveTo, 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
846 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
847 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
848 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 ){
drh8aff1012001-12-22 14:49:24 +0000854 sqliteExprCode(pParse, aExpr[k].p->pRight);
855 }else{
856 sqliteExprCode(pParse, aExpr[k].p->pLeft);
857 }
drh1dd59e02003-07-06 17:22:25 +0000858 sqliteVdbeAddOp(v, OP_IsNumeric, 1, brk);
drh8aff1012001-12-22 14:49:24 +0000859 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
860 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
861 }
drh6a3ea0e2003-05-02 14:32:12 +0000862 sqliteVdbeAddOp(v, OP_MoveTo, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000863 aExpr[k].p = 0;
864 }else{
drh6a3ea0e2003-05-02 14:32:12 +0000865 sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000866 }
867 if( iDirectLt[i]>=0 ){
868 k = iDirectLt[i];
869 assert( k<nExpr );
870 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000871 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
872 if( aExpr[k].idxLeft==iCur ){
drh8aff1012001-12-22 14:49:24 +0000873 sqliteExprCode(pParse, aExpr[k].p->pRight);
874 }else{
875 sqliteExprCode(pParse, aExpr[k].p->pLeft);
876 }
drh1dd59e02003-07-06 17:22:25 +0000877 /* sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1); */
drh8aff1012001-12-22 14:49:24 +0000878 pLevel->iMem = pParse->nMem++;
drh0f18bfa2003-12-10 01:31:21 +0000879 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh8aff1012001-12-22 14:49:24 +0000880 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
881 testOp = OP_Ge;
882 }else{
883 testOp = OP_Gt;
884 }
885 aExpr[k].p = 0;
886 }
887 start = sqliteVdbeCurrentAddr(v);
888 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000889 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000890 pLevel->p2 = start;
891 if( testOp!=OP_Noop ){
drh6a3ea0e2003-05-02 14:32:12 +0000892 sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
drh8aff1012001-12-22 14:49:24 +0000893 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
894 sqliteVdbeAddOp(v, testOp, 0, brk);
895 }
896 haveKey = 0;
897 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000898 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000899 ** scan of the entire database table.
900 */
901 int start;
902
903 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
904 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000905 sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000906 start = sqliteVdbeCurrentAddr(v);
907 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000908 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000909 pLevel->p2 = start;
910 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000911 }else{
drhc27a1ce2002-06-14 20:58:45 +0000912 /* Case 5: The WHERE clause term that refers to the right-most
913 ** column of the index is an inequality. For example, if
914 ** the index is on (x,y,z) and the WHERE clause is of the
915 ** form "x=5 AND y<10" then this case is used. Only the
916 ** right-most column can be an inequality - the rest must
917 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000918 **
919 ** This case is also used when there are no WHERE clause
920 ** constraints but an index is selected anyway, in order
921 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000922 */
923 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000924 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000925 int start;
926 int leFlag, geFlag;
927 int testOp;
928
929 /* Evaluate the equality constraints
930 */
931 for(j=0; j<nEqColumn; j++){
932 for(k=0; k<nExpr; k++){
933 if( aExpr[k].p==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000934 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000935 && aExpr[k].p->op==TK_EQ
936 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
937 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
938 ){
939 sqliteExprCode(pParse, aExpr[k].p->pRight);
drh143f3c42004-01-07 20:37:52 +0000940 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000941 break;
942 }
drh6a3ea0e2003-05-02 14:32:12 +0000943 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000944 && aExpr[k].p->op==TK_EQ
945 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
946 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
947 ){
948 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000949 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000950 break;
951 }
952 }
953 }
954
drhc27a1ce2002-06-14 20:58:45 +0000955 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000956 ** used twice: once to make the termination key and once to make the
957 ** start key.
958 */
959 for(j=0; j<nEqColumn; j++){
960 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
961 }
962
drhc045ec52002-12-04 20:01:06 +0000963 /* Labels for the beginning and end of the loop
964 */
965 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
966 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
967
drh487ab3c2001-11-08 00:45:21 +0000968 /* Generate the termination key. This is the key value that
969 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000970 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +0000971 **
972 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
973 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +0000974 */
975 if( (score & 1)!=0 ){
976 for(k=0; k<nExpr; k++){
977 Expr *pExpr = aExpr[k].p;
978 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000979 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000980 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
981 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
982 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
983 ){
984 sqliteExprCode(pParse, pExpr->pRight);
985 leFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +0000986 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000987 break;
988 }
drh6a3ea0e2003-05-02 14:32:12 +0000989 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000990 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
991 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
992 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
993 ){
994 sqliteExprCode(pParse, pExpr->pLeft);
995 leFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +0000996 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000997 break;
998 }
999 }
1000 testOp = OP_IdxGE;
1001 }else{
1002 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1003 leFlag = 1;
1004 }
1005 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +00001006 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +00001007 pLevel->iMem = pParse->nMem++;
drh143f3c42004-01-07 20:37:52 +00001008 sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
1009 sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
1010 sqliteVdbeAddOp(v, OP_Goto, 0, brk);
1011 sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
drha9e99ae2002-08-13 23:02:57 +00001012 sqliteAddIdxKeyType(v, pIdx);
drh487ab3c2001-11-08 00:45:21 +00001013 if( leFlag ){
1014 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
1015 }
drhc045ec52002-12-04 20:01:06 +00001016 if( pLevel->bRev ){
1017 sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
1018 }else{
1019 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1020 }
1021 }else if( pLevel->bRev ){
1022 sqliteVdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001023 }
1024
1025 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001026 ** bound on the search. There is no start key if there are no
1027 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001028 ** that case, generate a "Rewind" instruction in place of the
1029 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001030 **
1031 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1032 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001033 */
1034 if( (score & 2)!=0 ){
1035 for(k=0; k<nExpr; k++){
1036 Expr *pExpr = aExpr[k].p;
1037 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001038 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001039 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1040 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1041 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1042 ){
1043 sqliteExprCode(pParse, pExpr->pRight);
1044 geFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001045 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001046 break;
1047 }
drh6a3ea0e2003-05-02 14:32:12 +00001048 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001049 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1050 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1051 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1052 ){
1053 sqliteExprCode(pParse, pExpr->pLeft);
1054 geFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001055 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001056 break;
1057 }
1058 }
drh7900ead2001-11-12 13:51:43 +00001059 }else{
1060 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001061 }
drh487ab3c2001-11-08 00:45:21 +00001062 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001063 int nCol = nEqColumn + ((score&2)!=0);
1064 sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
1065 sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
1066 sqliteVdbeAddOp(v, OP_Goto, 0, brk);
1067 sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
drha9e99ae2002-08-13 23:02:57 +00001068 sqliteAddIdxKeyType(v, pIdx);
drh487ab3c2001-11-08 00:45:21 +00001069 if( !geFlag ){
1070 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
1071 }
drhc045ec52002-12-04 20:01:06 +00001072 if( pLevel->bRev ){
1073 pLevel->iMem = pParse->nMem++;
1074 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1075 testOp = OP_IdxLT;
1076 }else{
1077 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
1078 }
1079 }else if( pLevel->bRev ){
1080 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001081 }else{
1082 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
1083 }
1084
1085 /* Generate the the top of the loop. If there is a termination
1086 ** key we have to test for that key and abort at the top of the
1087 ** loop.
1088 */
1089 start = sqliteVdbeCurrentAddr(v);
1090 if( testOp!=OP_Noop ){
1091 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1092 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
1093 }
drh143f3c42004-01-07 20:37:52 +00001094 sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1095 sqliteVdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
drh487ab3c2001-11-08 00:45:21 +00001096 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001097 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001098 haveKey = 1;
1099 }else{
drh6a3ea0e2003-05-02 14:32:12 +00001100 sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001101 haveKey = 0;
1102 }
1103
1104 /* Record the instruction used to terminate the loop.
1105 */
drhc045ec52002-12-04 20:01:06 +00001106 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001107 pLevel->p1 = pLevel->iCur;
1108 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001109 }
drh6a3ea0e2003-05-02 14:32:12 +00001110 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001111
1112 /* Insert code to test every subexpression that can be completely
1113 ** computed using the current set of tables.
1114 */
1115 for(j=0; j<nExpr; j++){
1116 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +00001117 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh1f162302002-10-27 19:35:33 +00001118 if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1119 continue;
1120 }
drh75897232000-05-29 14:26:00 +00001121 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001122 haveKey = 0;
drh6a3ea0e2003-05-02 14:32:12 +00001123 sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
drh75897232000-05-29 14:26:00 +00001124 }
drhf5905aa2002-05-26 20:54:33 +00001125 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +00001126 aExpr[j].p = 0;
1127 }
1128 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001129
1130 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1131 ** at least one row of the right table has matched the left table.
1132 */
1133 if( pLevel->iLeftJoin ){
1134 pLevel->top = sqliteVdbeCurrentAddr(v);
1135 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
1136 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drh1cc093c2002-06-24 22:01:57 +00001137 for(j=0; j<nExpr; j++){
1138 if( aExpr[j].p==0 ) continue;
1139 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1140 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001141 /* Cannot happen. "haveKey" can only be true if pushKey is true
1142 ** an pushKey can only be true for DELETE and UPDATE and there are
1143 ** no outer joins with DELETE and UPDATE.
1144 */
drh1cc093c2002-06-24 22:01:57 +00001145 haveKey = 0;
drh6a3ea0e2003-05-02 14:32:12 +00001146 sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001147 }
1148 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
1149 aExpr[j].p = 0;
1150 }
drhad2d8302002-05-24 20:31:36 +00001151 }
drh75897232000-05-29 14:26:00 +00001152 }
1153 pWInfo->iContinue = cont;
1154 if( pushKey && !haveKey ){
drh6a3ea0e2003-05-02 14:32:12 +00001155 sqliteVdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001156 }
drh6a3ea0e2003-05-02 14:32:12 +00001157 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001158 return pWInfo;
1159}
1160
1161/*
drhc27a1ce2002-06-14 20:58:45 +00001162** Generate the end of the WHERE loop. See comments on
1163** sqliteWhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001164*/
1165void sqliteWhereEnd(WhereInfo *pWInfo){
1166 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001167 int i;
drh6b563442001-11-07 16:48:26 +00001168 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001169 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001170
drhad3cab52002-05-24 02:04:32 +00001171 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001172 pLevel = &pWInfo->a[i];
1173 sqliteVdbeResolveLabel(v, pLevel->cont);
1174 if( pLevel->op!=OP_Noop ){
1175 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001176 }
drh6b563442001-11-07 16:48:26 +00001177 sqliteVdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001178 if( pLevel->inOp!=OP_Noop ){
1179 sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
1180 }
drhad2d8302002-05-24 20:31:36 +00001181 if( pLevel->iLeftJoin ){
1182 int addr;
1183 addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
drh7f09b3e2002-08-13 13:15:49 +00001184 sqliteVdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
drh6a3ea0e2003-05-02 14:32:12 +00001185 sqliteVdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001186 if( pLevel->iCur>=0 ){
1187 sqliteVdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
1188 }
drhad2d8302002-05-24 20:31:36 +00001189 sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
1190 }
drh19a775c2000-06-05 18:54:46 +00001191 }
drh6b563442001-11-07 16:48:26 +00001192 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001193 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001194 Table *pTab = pTabList->a[i].pTab;
1195 assert( pTab!=0 );
1196 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001197 pLevel = &pWInfo->a[i];
drh6a3ea0e2003-05-02 14:32:12 +00001198 sqliteVdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001199 if( pLevel->pIdx!=0 ){
1200 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
1201 }
drh19a775c2000-06-05 18:54:46 +00001202 }
drh142e30d2002-08-28 03:00:58 +00001203#if 0 /* Never reuse a cursor */
drh832508b2002-03-02 17:04:07 +00001204 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1205 pWInfo->pParse->nTab = pWInfo->savedNTab;
1206 }
drh142e30d2002-08-28 03:00:58 +00001207#endif
drh75897232000-05-29 14:26:00 +00001208 sqliteFree(pWInfo);
1209 return;
1210}