blob: c658ab59e7c1cc44d86a910283657943bfffb176 [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
13** the WHERE clause of SQL statements. Also found here are subroutines
14** to generate VDBE code to evaluate expressions.
15**
drhc27a1ce2002-06-14 20:58:45 +000016** $Id: where.c,v 1.51 2002/06/14 20:58:45 drh Exp $
drh75897232000-05-29 14:26:00 +000017*/
18#include "sqliteInt.h"
19
20/*
21** The query generator uses an array of instances of this structure to
22** help it analyze the subexpressions of the WHERE clause. Each WHERE
23** clause subexpression is separated from the others by an AND operator.
24*/
25typedef struct ExprInfo ExprInfo;
26struct ExprInfo {
27 Expr *p; /* Pointer to the subexpression */
28 int indexable; /* True if this subexprssion is usable by an index */
drh967e8b72000-06-21 13:59:10 +000029 int idxLeft; /* p->pLeft is a column in this table number. -1 if
30 ** p->pLeft is not the column of any table */
31 int idxRight; /* p->pRight is a column in this table number. -1 if
32 ** p->pRight is not the column of any table */
drh75897232000-05-29 14:26:00 +000033 unsigned prereqLeft; /* Tables referenced by p->pLeft */
34 unsigned prereqRight; /* Tables referenced by p->pRight */
drh3f6b5482002-04-02 13:26:10 +000035 unsigned prereqAll; /* Tables referenced by this expression in any way */
drh75897232000-05-29 14:26:00 +000036};
37
38/*
39** Determine the number of elements in an array.
40*/
41#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
42
43/*
44** This routine is used to divide the WHERE expression into subexpressions
45** separated by the AND operator.
46**
47** aSlot[] is an array of subexpressions structures.
48** There are nSlot spaces left in this array. This routine attempts to
49** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
50** The return value is the number of slots filled.
51*/
52static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
53 int cnt = 0;
54 if( pExpr==0 || nSlot<1 ) return 0;
55 if( nSlot==1 || pExpr->op!=TK_AND ){
56 aSlot[0].p = pExpr;
57 return 1;
58 }
59 if( pExpr->pLeft->op!=TK_AND ){
60 aSlot[0].p = pExpr->pLeft;
61 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
62 }else{
63 cnt = exprSplit(nSlot, aSlot, pExpr->pRight);
64 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft);
65 }
66 return cnt;
67}
68
69/*
70** This routine walks (recursively) an expression tree and generates
71** a bitmask indicating which tables are used in that expression
72** tree. Bit 0 of the mask is set if table 0 is used. But 1 is set
73** if table 1 is used. And so forth.
74**
75** In order for this routine to work, the calling function must have
76** previously invoked sqliteExprResolveIds() on the expression. See
77** the header comment on that routine for additional information.
drh19a775c2000-06-05 18:54:46 +000078**
79** "base" is the cursor number (the value of the iTable field) that
drh832508b2002-03-02 17:04:07 +000080** corresponds to the first entry in the table list.
drh75897232000-05-29 14:26:00 +000081*/
drh19a775c2000-06-05 18:54:46 +000082static int exprTableUsage(int base, Expr *p){
drh75897232000-05-29 14:26:00 +000083 unsigned int mask = 0;
84 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +000085 if( p->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +000086 return 1<< (p->iTable - base);
drh75897232000-05-29 14:26:00 +000087 }
88 if( p->pRight ){
drh19a775c2000-06-05 18:54:46 +000089 mask = exprTableUsage(base, p->pRight);
drh75897232000-05-29 14:26:00 +000090 }
91 if( p->pLeft ){
drh19a775c2000-06-05 18:54:46 +000092 mask |= exprTableUsage(base, p->pLeft);
drh75897232000-05-29 14:26:00 +000093 }
drhdd579122002-04-02 01:58:57 +000094 if( p->pList ){
95 int i;
96 for(i=0; i<p->pList->nExpr; i++){
97 mask |= exprTableUsage(base, p->pList->a[i].pExpr);
98 }
99 }
drh75897232000-05-29 14:26:00 +0000100 return mask;
101}
102
103/*
drh487ab3c2001-11-08 00:45:21 +0000104** Return TRUE if the given operator is one of the operators that is
105** allowed for an indexable WHERE clause. The allowed operators are
drhc27a1ce2002-06-14 20:58:45 +0000106** "=", "<", ">", "<=", ">=", and "IN".
drh487ab3c2001-11-08 00:45:21 +0000107*/
108static int allowedOp(int op){
109 switch( op ){
110 case TK_LT:
111 case TK_LE:
112 case TK_GT:
113 case TK_GE:
114 case TK_EQ:
drhd99f7062002-06-08 23:25:08 +0000115 case TK_IN:
drh487ab3c2001-11-08 00:45:21 +0000116 return 1;
117 default:
118 return 0;
119 }
120}
121
122/*
drh75897232000-05-29 14:26:00 +0000123** The input to this routine is an ExprInfo structure with only the
124** "p" field filled in. The job of this routine is to analyze the
125** subexpression and populate all the other fields of the ExprInfo
126** structure.
drh19a775c2000-06-05 18:54:46 +0000127**
128** "base" is the cursor number (the value of the iTable field) that
drh832508b2002-03-02 17:04:07 +0000129** corresponds to the first entry in the table list.
drh75897232000-05-29 14:26:00 +0000130*/
drh19a775c2000-06-05 18:54:46 +0000131static void exprAnalyze(int base, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000132 Expr *pExpr = pInfo->p;
drh19a775c2000-06-05 18:54:46 +0000133 pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
134 pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
drh3f6b5482002-04-02 13:26:10 +0000135 pInfo->prereqAll = exprTableUsage(base, pExpr);
drh75897232000-05-29 14:26:00 +0000136 pInfo->indexable = 0;
137 pInfo->idxLeft = -1;
138 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000139 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drhd99f7062002-06-08 23:25:08 +0000140 if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000141 pInfo->idxRight = pExpr->pRight->iTable - base;
drh75897232000-05-29 14:26:00 +0000142 pInfo->indexable = 1;
143 }
drh967e8b72000-06-21 13:59:10 +0000144 if( pExpr->pLeft->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000145 pInfo->idxLeft = pExpr->pLeft->iTable - base;
drh75897232000-05-29 14:26:00 +0000146 pInfo->indexable = 1;
147 }
148 }
149}
150
151/*
152** Generating the beginning of the loop used for WHERE clause processing.
153** The return value is a pointer to an (opaque) structure that contains
154** information needed to terminate the loop. Later, the calling routine
155** should invoke sqliteWhereEnd() with the return value of this function
156** in order to complete the WHERE clause processing.
157**
158** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000159**
160** The basic idea is to do a nested loop, one loop for each table in
161** the FROM clause of a select. (INSERT and UPDATE statements are the
162** same as a SELECT with only a single table in the FROM clause.) For
163** example, if the SQL is this:
164**
165** SELECT * FROM t1, t2, t3 WHERE ...;
166**
167** Then the code generated is conceptually like the following:
168**
169** foreach row1 in t1 do \ Code generated
170** foreach row2 in t2 do |-- by sqliteWhereBegin()
171** foreach row3 in t3 do /
172** ...
173** end \ Code generated
174** end |-- by sqliteWhereEnd()
175** end /
176**
177** There are Btree cursors associated with each table. t1 uses cursor
178** "base". t2 uses cursor "base+1". And so forth. This routine generates
179** the code to open those cursors. sqliteWhereEnd() generates the code
180** to close them.
181**
182** If the WHERE clause is empty, the foreach loops must each scan their
183** entire tables. Thus a three-way join is an O(N^3) operation. But if
184** the tables have indices and there are terms in the WHERE clause that
185** refer to those indices, a complete table scan can be avoided and the
186** code will run much faster. Most of the work of this routine is checking
187** to see if there are indices that can be used to speed up the loop.
188**
189** Terms of the WHERE clause are also used to limit which rows actually
190** make it to the "..." in the middle of the loop. After each "foreach",
191** terms of the WHERE clause that use only terms in that loop and outer
192** loops are evaluated and if false a jump is made around all subsequent
193** inner loops (or around the "..." if the test occurs within the inner-
194** most loop)
195**
196** OUTER JOINS
197**
198** An outer join of tables t1 and t2 is conceptally coded as follows:
199**
200** foreach row1 in t1 do
201** flag = 0
202** foreach row2 in t2 do
203** start:
204** ...
205** flag = 1
206** end
207** if flag==0 goto start
208** end
209**
210** In words, if the right
drh75897232000-05-29 14:26:00 +0000211*/
212WhereInfo *sqliteWhereBegin(
213 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000214 int base, /* VDBE cursor index for left-most table in pTabList */
drhad3cab52002-05-24 02:04:32 +0000215 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000216 Expr *pWhere, /* The WHERE clause */
217 int pushKey /* If TRUE, leave the table key on the stack */
218){
219 int i; /* Loop counter */
220 WhereInfo *pWInfo; /* Will become the return value of this function */
221 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
222 int brk, cont; /* Addresses used during code generation */
223 int *aOrder; /* Order in which pTabList entries are searched */
224 int nExpr; /* Number of subexpressions in the WHERE clause */
225 int loopMask; /* One bit set for each outer loop */
226 int haveKey; /* True if KEY is on the stack */
drhc4a3c772001-04-04 11:48:57 +0000227 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh8aff1012001-12-22 14:49:24 +0000228 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
229 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
230 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh75897232000-05-29 14:26:00 +0000231 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
232
drhc27a1ce2002-06-14 20:58:45 +0000233 /* pushKey is only allowed if there is a single table (as in an INSERT or
234 ** UPDATE statement)
235 */
236 assert( pushKey==0 || pTabList->nSrc==1 );
237
drh6b563442001-11-07 16:48:26 +0000238 /* Allocate space for aOrder[] and aiMem[]. */
drhad3cab52002-05-24 02:04:32 +0000239 aOrder = sqliteMalloc( sizeof(int) * pTabList->nSrc );
drh75897232000-05-29 14:26:00 +0000240
241 /* Allocate and initialize the WhereInfo structure that will become the
242 ** return value.
243 */
drhad3cab52002-05-24 02:04:32 +0000244 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
drhdaffd0e2001-04-11 14:28:42 +0000245 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000246 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000247 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000248 return 0;
249 }
250 pWInfo->pParse = pParse;
251 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000252 pWInfo->base = base;
253 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
drh08192d52002-04-30 19:20:28 +0000254 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
255
256 /* Special case: a WHERE clause that is constant. Evaluate the
257 ** expression and either jump over all of the code or fall thru.
258 */
259 if( pWhere && sqliteExprIsConstant(pWhere) ){
drhf5905aa2002-05-26 20:54:33 +0000260 sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drh08192d52002-04-30 19:20:28 +0000261 }
drh75897232000-05-29 14:26:00 +0000262
263 /* Split the WHERE clause into as many as 32 separate subexpressions
264 ** where each subexpression is separated by an AND operator. Any additional
265 ** subexpressions are attached in the aExpr[32] and will not enter
266 ** into the query optimizer computations. 32 is chosen as the cutoff
267 ** since that is the number of bits in an integer that we use for an
268 ** expression-used mask.
269 */
270 memset(aExpr, 0, sizeof(aExpr));
271 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
272
273 /* Analyze all of the subexpressions.
274 */
275 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000276 exprAnalyze(base, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000277
278 /* If we are executing a trigger body, remove all references to
279 ** new.* and old.* tables from the prerequisite masks.
280 */
281 if( pParse->trigStack ){
282 int x;
283 if( (x = pParse->trigStack->newIdx) >= 0 ){
284 int mask = ~(1 << (x - base));
285 aExpr[i].prereqRight &= mask;
286 aExpr[i].prereqLeft &= mask;
287 aExpr[i].prereqAll &= mask;
288 }
289 if( (x = pParse->trigStack->oldIdx) >= 0 ){
290 int mask = ~(1 << (x - base));
291 aExpr[i].prereqRight &= mask;
292 aExpr[i].prereqLeft &= mask;
293 aExpr[i].prereqAll &= mask;
294 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000295 }
drh75897232000-05-29 14:26:00 +0000296 }
297
298 /* Figure out a good nesting order for the tables. aOrder[0] will
299 ** be the index in pTabList of the outermost table. aOrder[1] will
drhad3cab52002-05-24 02:04:32 +0000300 ** be the first nested loop and so on. aOrder[pTabList->nSrc-1] will
drh75897232000-05-29 14:26:00 +0000301 ** be the innermost loop.
302 **
drh1d1f3052002-05-21 13:18:25 +0000303 ** Someday we will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000304 ** for an effiecient query. But for now, just use whatever order the
305 ** tables appear in in the pTabList.
306 */
drhad3cab52002-05-24 02:04:32 +0000307 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000308 aOrder[i] = i;
309 }
310
311 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000312 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000313 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000314 ** loop.
315 **
316 ** If terms exist that use the ROWID of any table, then set the
317 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
318 ** to the index of the term containing the ROWID. We always prefer
319 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000320 ** index which requires reading an index first to get the rowid then
321 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000322 **
323 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000324 ** first 32 tables are candidates for indices. This is (again) due
325 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000326 */
327 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000328 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000329 int j;
drh75897232000-05-29 14:26:00 +0000330 int idx = aOrder[i];
331 Table *pTab = pTabList->a[idx].pTab;
332 Index *pIdx;
333 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000334 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000335
drhc4a3c772001-04-04 11:48:57 +0000336 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000337 ** ROWID field of this table. For terms of the form ROWID==expr
338 ** set iDirectEq[i] to the index of the term. For terms of the
339 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
340 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000341 */
drh8aff1012001-12-22 14:49:24 +0000342 iDirectEq[i] = -1;
343 iDirectLt[i] = -1;
344 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000345 for(j=0; j<nExpr; j++){
346 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
347 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000348 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000349 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000350 case TK_EQ: iDirectEq[i] = j; break;
351 case TK_LE:
352 case TK_LT: iDirectLt[i] = j; break;
353 case TK_GE:
354 case TK_GT: iDirectGt[i] = j; break;
355 }
drhc4a3c772001-04-04 11:48:57 +0000356 }
357 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
358 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000359 switch( aExpr[j].p->op ){
360 case TK_EQ: iDirectEq[i] = j; break;
361 case TK_LE:
362 case TK_LT: iDirectGt[i] = j; break;
363 case TK_GE:
364 case TK_GT: iDirectLt[i] = j; break;
365 }
drhc4a3c772001-04-04 11:48:57 +0000366 }
367 }
drh8aff1012001-12-22 14:49:24 +0000368 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000369 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000370 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000371 continue;
372 }
373
drh75897232000-05-29 14:26:00 +0000374 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000375 ** the "best" index. pBestIdx is left set to NULL if no indices
376 ** are usable.
drh75897232000-05-29 14:26:00 +0000377 **
drh487ab3c2001-11-08 00:45:21 +0000378 ** The best index is determined as follows. For each of the
379 ** left-most terms that is fixed by an equality operator, add
380 ** 4 to the score. The right-most term of the index may be
381 ** constrained by an inequality. Add 1 if for an "x<..." constraint
382 ** and add 2 for an "x>..." constraint. Chose the index that
383 ** gives the best score.
384 **
385 ** This scoring system is designed so that the score can later be
386 ** used to determine how the index is used. If the score&3 is 0
387 ** then all constraints are equalities. If score&1 is not 0 then
388 ** there is an inequality used as a termination key. (ex: "x<...")
389 ** If score&2 is not 0 then there is an inequality used as the
390 ** start key. (ex: "x>...");
drhd99f7062002-06-08 23:25:08 +0000391 **
drhc27a1ce2002-06-14 20:58:45 +0000392 ** The IN operator (as in "<expr> IN (...)") is treated the same as
393 ** an equality comparison except that it can only be used on the
394 ** left-most column of an index and other terms of the WHERE clause
395 ** cannot be used in conjunction with the IN operator to help satisfy
396 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000397 */
398 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000399 int eqMask = 0; /* Index columns covered by an x=... term */
400 int ltMask = 0; /* Index columns covered by an x<... term */
401 int gtMask = 0; /* Index columns covered by an x>... term */
402 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000403 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000404
drh74e24cd2002-01-09 03:19:59 +0000405 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000406 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000407 for(j=0; j<nExpr; j++){
408 if( aExpr[j].idxLeft==idx
409 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000410 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000411 int k;
drh967e8b72000-06-21 13:59:10 +0000412 for(k=0; k<pIdx->nColumn; k++){
413 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000414 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000415 case TK_IN: {
416 if( k==0 ) inMask |= 1;
417 break;
418 }
drh487ab3c2001-11-08 00:45:21 +0000419 case TK_EQ: {
420 eqMask |= 1<<k;
421 break;
422 }
423 case TK_LE:
424 case TK_LT: {
425 ltMask |= 1<<k;
426 break;
427 }
428 case TK_GE:
429 case TK_GT: {
430 gtMask |= 1<<k;
431 break;
432 }
433 default: {
434 /* CANT_HAPPEN */
435 assert( 0 );
436 break;
437 }
438 }
drh75897232000-05-29 14:26:00 +0000439 break;
440 }
441 }
442 }
443 if( aExpr[j].idxRight==idx
444 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000445 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000446 int k;
drh967e8b72000-06-21 13:59:10 +0000447 for(k=0; k<pIdx->nColumn; k++){
448 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000449 switch( aExpr[j].p->op ){
450 case TK_EQ: {
451 eqMask |= 1<<k;
452 break;
453 }
454 case TK_LE:
455 case TK_LT: {
456 gtMask |= 1<<k;
457 break;
458 }
459 case TK_GE:
460 case TK_GT: {
461 ltMask |= 1<<k;
462 break;
463 }
464 default: {
465 /* CANT_HAPPEN */
466 assert( 0 );
467 break;
468 }
469 }
drh75897232000-05-29 14:26:00 +0000470 break;
471 }
472 }
473 }
474 }
drh487ab3c2001-11-08 00:45:21 +0000475 for(nEq=0; nEq<pIdx->nColumn; nEq++){
476 m = (1<<(nEq+1))-1;
477 if( (m & eqMask)!=m ) break;
478 }
479 score = nEq*4;
480 m = 1<<nEq;
481 if( m & ltMask ) score++;
482 if( m & gtMask ) score+=2;
drh48185c12002-06-09 01:55:20 +0000483 if( score==0 && inMask ) score = 4;
drh487ab3c2001-11-08 00:45:21 +0000484 if( score>bestScore ){
485 pBestIdx = pIdx;
486 bestScore = score;
drh75897232000-05-29 14:26:00 +0000487 }
488 }
drh6b563442001-11-07 16:48:26 +0000489 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000490 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000491 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000492 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000493 pWInfo->a[i].iCur = pParse->nTab++;
494 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000495 }
drh75897232000-05-29 14:26:00 +0000496 }
497
drh6b563442001-11-07 16:48:26 +0000498 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000499 */
drhad3cab52002-05-24 02:04:32 +0000500 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000501 int openOp;
502 Table *pTab;
503
504 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000505 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000506 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000507 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
508 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000509 if( i==0 && !pParse->schemaVerified &&
510 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000511 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000512 pParse->schemaVerified = 1;
513 }
drh6b563442001-11-07 16:48:26 +0000514 if( pWInfo->a[i].pIdx!=0 ){
515 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
516 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000517 }
518 }
519
520 /* Generate the code to do the search
521 */
drh75897232000-05-29 14:26:00 +0000522 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000523 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000524 int j, k;
525 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000526 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000527 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000528
drhad2d8302002-05-24 20:31:36 +0000529 /* If this is the right table of a LEFT OUTER JOIN, allocate and
530 ** initialize a memory cell that record if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000531 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000532 */
533 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
534 if( !pParse->nMem ) pParse->nMem++;
535 pLevel->iLeftJoin = pParse->nMem++;
536 sqliteVdbeAddOp(v, OP_String, 0, 0);
537 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
538 }
539
drh8aff1012001-12-22 14:49:24 +0000540 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000541 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000542 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
543 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000544 ** equality comparison against the ROWID field. Or
545 ** we reference multiple rows using a "rowid IN (...)"
546 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000547 */
drh8aff1012001-12-22 14:49:24 +0000548 k = iDirectEq[i];
549 assert( k<nExpr );
550 assert( aExpr[k].p!=0 );
551 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
drhd99f7062002-06-08 23:25:08 +0000552 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000553 if( aExpr[k].idxLeft==idx ){
drhd99f7062002-06-08 23:25:08 +0000554 Expr *pX = aExpr[k].p;
555 if( pX->op!=TK_IN ){
556 sqliteExprCode(pParse, aExpr[k].p->pRight);
557 }else if( pX->pList ){
558 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
559 pLevel->inOp = OP_SetNext;
560 pLevel->inP1 = pX->iTable;
561 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
562 }else{
563 assert( pX->pSelect );
564 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
565 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
566 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
567 pLevel->inOp = OP_Next;
568 pLevel->inP1 = pX->iTable;
569 }
drh8aff1012001-12-22 14:49:24 +0000570 }else{
571 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000572 }
drh8aff1012001-12-22 14:49:24 +0000573 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000574 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000575 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhd99f7062002-06-08 23:25:08 +0000576 haveKey = 0;
drh6b125452002-01-28 15:53:03 +0000577 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000578 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000579 }else if( pIdx!=0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000580 /* Case 2: There is an index and all terms of the WHERE clause that
581 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000582 */
drh6b563442001-11-07 16:48:26 +0000583 int start;
drh487ab3c2001-11-08 00:45:21 +0000584 int testOp;
585 int nColumn = pLevel->score/4;
drhd99f7062002-06-08 23:25:08 +0000586 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000587 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000588 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000589 Expr *pX = aExpr[k].p;
590 if( pX==0 ) continue;
drh75897232000-05-29 14:26:00 +0000591 if( aExpr[k].idxLeft==idx
592 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000593 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000594 ){
drhd99f7062002-06-08 23:25:08 +0000595 if( pX->op==TK_EQ ){
596 sqliteExprCode(pParse, pX->pRight);
597 aExpr[k].p = 0;
598 break;
599 }
600 if( pX->op==TK_IN && nColumn==1 ){
601 if( pX->pList ){
602 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
603 pLevel->inOp = OP_SetNext;
604 pLevel->inP1 = pX->iTable;
605 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
606 }else{
607 assert( pX->pSelect );
608 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
609 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
610 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
611 pLevel->inOp = OP_Next;
612 pLevel->inP1 = pX->iTable;
613 }
614 aExpr[k].p = 0;
615 break;
616 }
drh75897232000-05-29 14:26:00 +0000617 }
618 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000619 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000620 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000621 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000622 ){
623 sqliteExprCode(pParse, aExpr[k].p->pLeft);
624 aExpr[k].p = 0;
625 break;
626 }
627 }
628 }
drh6b563442001-11-07 16:48:26 +0000629 pLevel->iMem = pParse->nMem++;
drh6b563442001-11-07 16:48:26 +0000630 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000631 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
632 if( nColumn==pIdx->nColumn ){
633 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
634 testOp = OP_IdxGT;
635 }else{
636 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
637 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
638 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
639 testOp = OP_IdxGE;
640 }
drh6b563442001-11-07 16:48:26 +0000641 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
642 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000643 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000644 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000645 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000646 haveKey = 1;
647 }else{
drh99fcd712001-10-13 01:06:47 +0000648 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000649 haveKey = 0;
650 }
drh6b563442001-11-07 16:48:26 +0000651 pLevel->op = OP_Next;
652 pLevel->p1 = pLevel->iCur;
653 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000654 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
655 /* Case 3: We have an inequality comparison against the ROWID field.
656 */
657 int testOp = OP_Noop;
658 int start;
659
660 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
661 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
662 if( iDirectGt[i]>=0 ){
663 k = iDirectGt[i];
664 assert( k<nExpr );
665 assert( aExpr[k].p!=0 );
666 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
667 if( aExpr[k].idxLeft==idx ){
668 sqliteExprCode(pParse, aExpr[k].p->pRight);
669 }else{
670 sqliteExprCode(pParse, aExpr[k].p->pLeft);
671 }
672 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
673 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
674 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
675 }
676 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
677 aExpr[k].p = 0;
678 }else{
679 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
680 }
681 if( iDirectLt[i]>=0 ){
682 k = iDirectLt[i];
683 assert( k<nExpr );
684 assert( aExpr[k].p!=0 );
685 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
686 if( aExpr[k].idxLeft==idx ){
687 sqliteExprCode(pParse, aExpr[k].p->pRight);
688 }else{
689 sqliteExprCode(pParse, aExpr[k].p->pLeft);
690 }
691 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
692 pLevel->iMem = pParse->nMem++;
693 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
694 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
695 testOp = OP_Ge;
696 }else{
697 testOp = OP_Gt;
698 }
699 aExpr[k].p = 0;
700 }
701 start = sqliteVdbeCurrentAddr(v);
702 pLevel->op = OP_Next;
703 pLevel->p1 = base+idx;
704 pLevel->p2 = start;
705 if( testOp!=OP_Noop ){
706 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
707 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
708 sqliteVdbeAddOp(v, testOp, 0, brk);
709 }
710 haveKey = 0;
711 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000712 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000713 ** scan of the entire database table.
714 */
715 int start;
716
717 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
718 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
719 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
720 start = sqliteVdbeCurrentAddr(v);
721 pLevel->op = OP_Next;
722 pLevel->p1 = base+idx;
723 pLevel->p2 = start;
724 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000725 }else{
drhc27a1ce2002-06-14 20:58:45 +0000726 /* Case 5: The WHERE clause term that refers to the right-most
727 ** column of the index is an inequality. For example, if
728 ** the index is on (x,y,z) and the WHERE clause is of the
729 ** form "x=5 AND y<10" then this case is used. Only the
730 ** right-most column can be an inequality - the rest must
731 ** use the "==" operator.
drh487ab3c2001-11-08 00:45:21 +0000732 */
733 int score = pLevel->score;
734 int nEqColumn = score/4;
735 int start;
736 int leFlag, geFlag;
737 int testOp;
738
739 /* Evaluate the equality constraints
740 */
741 for(j=0; j<nEqColumn; j++){
742 for(k=0; k<nExpr; k++){
743 if( aExpr[k].p==0 ) continue;
744 if( aExpr[k].idxLeft==idx
745 && aExpr[k].p->op==TK_EQ
746 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
747 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
748 ){
749 sqliteExprCode(pParse, aExpr[k].p->pRight);
750 aExpr[k].p = 0;
751 break;
752 }
753 if( aExpr[k].idxRight==idx
754 && aExpr[k].p->op==TK_EQ
755 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
756 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
757 ){
758 sqliteExprCode(pParse, aExpr[k].p->pLeft);
759 aExpr[k].p = 0;
760 break;
761 }
762 }
763 }
764
drhc27a1ce2002-06-14 20:58:45 +0000765 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000766 ** used twice: once to make the termination key and once to make the
767 ** start key.
768 */
769 for(j=0; j<nEqColumn; j++){
770 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
771 }
772
773 /* Generate the termination key. This is the key value that
774 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000775 ** are no equality terms and no "X<..." term.
drh487ab3c2001-11-08 00:45:21 +0000776 */
777 if( (score & 1)!=0 ){
778 for(k=0; k<nExpr; k++){
779 Expr *pExpr = aExpr[k].p;
780 if( pExpr==0 ) continue;
781 if( aExpr[k].idxLeft==idx
782 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
783 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
784 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
785 ){
786 sqliteExprCode(pParse, pExpr->pRight);
787 leFlag = pExpr->op==TK_LE;
788 aExpr[k].p = 0;
789 break;
790 }
791 if( aExpr[k].idxRight==idx
792 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
793 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
794 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
795 ){
796 sqliteExprCode(pParse, pExpr->pLeft);
797 leFlag = pExpr->op==TK_GE;
798 aExpr[k].p = 0;
799 break;
800 }
801 }
802 testOp = OP_IdxGE;
803 }else{
804 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
805 leFlag = 1;
806 }
807 if( testOp!=OP_Noop ){
808 pLevel->iMem = pParse->nMem++;
809 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
810 if( leFlag ){
811 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
812 }
813 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
814 }
815
816 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +0000817 ** bound on the search. There is no start key if there are no
818 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +0000819 ** that case, generate a "Rewind" instruction in place of the
820 ** start key search.
821 */
822 if( (score & 2)!=0 ){
823 for(k=0; k<nExpr; k++){
824 Expr *pExpr = aExpr[k].p;
825 if( pExpr==0 ) continue;
826 if( aExpr[k].idxLeft==idx
827 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
828 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
829 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
830 ){
831 sqliteExprCode(pParse, pExpr->pRight);
832 geFlag = pExpr->op==TK_GE;
833 aExpr[k].p = 0;
834 break;
835 }
836 if( aExpr[k].idxRight==idx
837 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
838 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
839 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
840 ){
841 sqliteExprCode(pParse, pExpr->pLeft);
842 geFlag = pExpr->op==TK_LE;
843 aExpr[k].p = 0;
844 break;
845 }
846 }
drh7900ead2001-11-12 13:51:43 +0000847 }else{
848 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000849 }
850 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
851 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
852 if( nEqColumn>0 || (score&2)!=0 ){
853 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
854 if( !geFlag ){
855 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
856 }
857 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
858 }else{
859 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
860 }
861
862 /* Generate the the top of the loop. If there is a termination
863 ** key we have to test for that key and abort at the top of the
864 ** loop.
865 */
866 start = sqliteVdbeCurrentAddr(v);
867 if( testOp!=OP_Noop ){
868 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
869 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
870 }
871 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000872 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +0000873 haveKey = 1;
874 }else{
875 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
876 haveKey = 0;
877 }
878
879 /* Record the instruction used to terminate the loop.
880 */
881 pLevel->op = OP_Next;
882 pLevel->p1 = pLevel->iCur;
883 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000884 }
885 loopMask |= 1<<idx;
886
887 /* Insert code to test every subexpression that can be completely
888 ** computed using the current set of tables.
889 */
890 for(j=0; j<nExpr; j++){
891 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +0000892 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh75897232000-05-29 14:26:00 +0000893 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000894 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000895 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000896 }
drhf5905aa2002-05-26 20:54:33 +0000897 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +0000898 aExpr[j].p = 0;
899 }
900 brk = cont;
drhad2d8302002-05-24 20:31:36 +0000901
902 /* For a LEFT OUTER JOIN, generate code that will record the fact that
903 ** at least one row of the right table has matched the left table.
904 */
905 if( pLevel->iLeftJoin ){
906 pLevel->top = sqliteVdbeCurrentAddr(v);
907 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
908 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
909 }
drh75897232000-05-29 14:26:00 +0000910 }
911 pWInfo->iContinue = cont;
912 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000913 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000914 }
915 sqliteFree(aOrder);
916 return pWInfo;
917}
918
919/*
drhc27a1ce2002-06-14 20:58:45 +0000920** Generate the end of the WHERE loop. See comments on
921** sqliteWhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +0000922*/
923void sqliteWhereEnd(WhereInfo *pWInfo){
924 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000925 int i;
drh19a775c2000-06-05 18:54:46 +0000926 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000927 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +0000928 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000929
drhad3cab52002-05-24 02:04:32 +0000930 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000931 pLevel = &pWInfo->a[i];
932 sqliteVdbeResolveLabel(v, pLevel->cont);
933 if( pLevel->op!=OP_Noop ){
934 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000935 }
drh6b563442001-11-07 16:48:26 +0000936 sqliteVdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +0000937 if( pLevel->inOp!=OP_Noop ){
938 sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
939 }
drhad2d8302002-05-24 20:31:36 +0000940 if( pLevel->iLeftJoin ){
941 int addr;
942 addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
943 sqliteVdbeAddOp(v, OP_NotNull, 0, addr+4);
944 sqliteVdbeAddOp(v, OP_NullRow, base+i, 0);
945 sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
946 }
drh19a775c2000-06-05 18:54:46 +0000947 }
drh6b563442001-11-07 16:48:26 +0000948 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +0000949 for(i=0; i<pTabList->nSrc; i++){
drh22f70c32002-02-18 01:17:00 +0000950 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000951 pLevel = &pWInfo->a[i];
952 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
953 if( pLevel->pIdx!=0 ){
954 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
955 }
drh19a775c2000-06-05 18:54:46 +0000956 }
drh832508b2002-03-02 17:04:07 +0000957 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
958 pWInfo->pParse->nTab = pWInfo->savedNTab;
959 }
drh75897232000-05-29 14:26:00 +0000960 sqliteFree(pWInfo);
961 return;
962}