blob: d7b24dfa697ae385b2644eff6c2add2d0a4f5264 [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**
drhdf199a22002-06-14 22:38:41 +000016** $Id: where.c,v 1.52 2002/06/14 22:38:43 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);
drhdf199a22002-06-14 22:38:41 +0000261 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000262 }
drh75897232000-05-29 14:26:00 +0000263
264 /* Split the WHERE clause into as many as 32 separate subexpressions
265 ** where each subexpression is separated by an AND operator. Any additional
266 ** subexpressions are attached in the aExpr[32] and will not enter
267 ** into the query optimizer computations. 32 is chosen as the cutoff
268 ** since that is the number of bits in an integer that we use for an
269 ** expression-used mask.
270 */
271 memset(aExpr, 0, sizeof(aExpr));
272 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
273
274 /* Analyze all of the subexpressions.
275 */
276 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000277 exprAnalyze(base, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000278
279 /* If we are executing a trigger body, remove all references to
280 ** new.* and old.* tables from the prerequisite masks.
281 */
282 if( pParse->trigStack ){
283 int x;
284 if( (x = pParse->trigStack->newIdx) >= 0 ){
285 int mask = ~(1 << (x - base));
286 aExpr[i].prereqRight &= mask;
287 aExpr[i].prereqLeft &= mask;
288 aExpr[i].prereqAll &= mask;
289 }
290 if( (x = pParse->trigStack->oldIdx) >= 0 ){
291 int mask = ~(1 << (x - base));
292 aExpr[i].prereqRight &= mask;
293 aExpr[i].prereqLeft &= mask;
294 aExpr[i].prereqAll &= mask;
295 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000296 }
drh75897232000-05-29 14:26:00 +0000297 }
298
299 /* Figure out a good nesting order for the tables. aOrder[0] will
300 ** be the index in pTabList of the outermost table. aOrder[1] will
drhad3cab52002-05-24 02:04:32 +0000301 ** be the first nested loop and so on. aOrder[pTabList->nSrc-1] will
drh75897232000-05-29 14:26:00 +0000302 ** be the innermost loop.
303 **
drh1d1f3052002-05-21 13:18:25 +0000304 ** Someday we will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000305 ** for an effiecient query. But for now, just use whatever order the
306 ** tables appear in in the pTabList.
307 */
drhad3cab52002-05-24 02:04:32 +0000308 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000309 aOrder[i] = i;
310 }
311
312 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000313 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000314 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000315 ** loop.
316 **
317 ** If terms exist that use the ROWID of any table, then set the
318 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
319 ** to the index of the term containing the ROWID. We always prefer
320 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000321 ** index which requires reading an index first to get the rowid then
322 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000323 **
324 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000325 ** first 32 tables are candidates for indices. This is (again) due
326 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000327 */
328 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000329 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000330 int j;
drh75897232000-05-29 14:26:00 +0000331 int idx = aOrder[i];
332 Table *pTab = pTabList->a[idx].pTab;
333 Index *pIdx;
334 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000335 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000336
drhc4a3c772001-04-04 11:48:57 +0000337 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000338 ** ROWID field of this table. For terms of the form ROWID==expr
339 ** set iDirectEq[i] to the index of the term. For terms of the
340 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
341 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000342 */
drh8aff1012001-12-22 14:49:24 +0000343 iDirectEq[i] = -1;
344 iDirectLt[i] = -1;
345 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000346 for(j=0; j<nExpr; j++){
347 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
348 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000349 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000350 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000351 case TK_EQ: iDirectEq[i] = j; break;
352 case TK_LE:
353 case TK_LT: iDirectLt[i] = j; break;
354 case TK_GE:
355 case TK_GT: iDirectGt[i] = j; break;
356 }
drhc4a3c772001-04-04 11:48:57 +0000357 }
358 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
359 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000360 switch( aExpr[j].p->op ){
361 case TK_EQ: iDirectEq[i] = j; break;
362 case TK_LE:
363 case TK_LT: iDirectGt[i] = j; break;
364 case TK_GE:
365 case TK_GT: iDirectLt[i] = j; break;
366 }
drhc4a3c772001-04-04 11:48:57 +0000367 }
368 }
drh8aff1012001-12-22 14:49:24 +0000369 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000370 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000371 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000372 continue;
373 }
374
drh75897232000-05-29 14:26:00 +0000375 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000376 ** the "best" index. pBestIdx is left set to NULL if no indices
377 ** are usable.
drh75897232000-05-29 14:26:00 +0000378 **
drh487ab3c2001-11-08 00:45:21 +0000379 ** The best index is determined as follows. For each of the
380 ** left-most terms that is fixed by an equality operator, add
381 ** 4 to the score. The right-most term of the index may be
382 ** constrained by an inequality. Add 1 if for an "x<..." constraint
383 ** and add 2 for an "x>..." constraint. Chose the index that
384 ** gives the best score.
385 **
386 ** This scoring system is designed so that the score can later be
387 ** used to determine how the index is used. If the score&3 is 0
388 ** then all constraints are equalities. If score&1 is not 0 then
389 ** there is an inequality used as a termination key. (ex: "x<...")
390 ** If score&2 is not 0 then there is an inequality used as the
391 ** start key. (ex: "x>...");
drhd99f7062002-06-08 23:25:08 +0000392 **
drhc27a1ce2002-06-14 20:58:45 +0000393 ** The IN operator (as in "<expr> IN (...)") is treated the same as
394 ** an equality comparison except that it can only be used on the
395 ** left-most column of an index and other terms of the WHERE clause
396 ** cannot be used in conjunction with the IN operator to help satisfy
397 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000398 */
399 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000400 int eqMask = 0; /* Index columns covered by an x=... term */
401 int ltMask = 0; /* Index columns covered by an x<... term */
402 int gtMask = 0; /* Index columns covered by an x>... term */
403 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000404 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000405
drh74e24cd2002-01-09 03:19:59 +0000406 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000407 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000408 for(j=0; j<nExpr; j++){
409 if( aExpr[j].idxLeft==idx
410 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000411 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000412 int k;
drh967e8b72000-06-21 13:59:10 +0000413 for(k=0; k<pIdx->nColumn; k++){
414 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000415 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000416 case TK_IN: {
417 if( k==0 ) inMask |= 1;
418 break;
419 }
drh487ab3c2001-11-08 00:45:21 +0000420 case TK_EQ: {
421 eqMask |= 1<<k;
422 break;
423 }
424 case TK_LE:
425 case TK_LT: {
426 ltMask |= 1<<k;
427 break;
428 }
429 case TK_GE:
430 case TK_GT: {
431 gtMask |= 1<<k;
432 break;
433 }
434 default: {
435 /* CANT_HAPPEN */
436 assert( 0 );
437 break;
438 }
439 }
drh75897232000-05-29 14:26:00 +0000440 break;
441 }
442 }
443 }
444 if( aExpr[j].idxRight==idx
445 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000446 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000447 int k;
drh967e8b72000-06-21 13:59:10 +0000448 for(k=0; k<pIdx->nColumn; k++){
449 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000450 switch( aExpr[j].p->op ){
451 case TK_EQ: {
452 eqMask |= 1<<k;
453 break;
454 }
455 case TK_LE:
456 case TK_LT: {
457 gtMask |= 1<<k;
458 break;
459 }
460 case TK_GE:
461 case TK_GT: {
462 ltMask |= 1<<k;
463 break;
464 }
465 default: {
466 /* CANT_HAPPEN */
467 assert( 0 );
468 break;
469 }
470 }
drh75897232000-05-29 14:26:00 +0000471 break;
472 }
473 }
474 }
475 }
drh487ab3c2001-11-08 00:45:21 +0000476 for(nEq=0; nEq<pIdx->nColumn; nEq++){
477 m = (1<<(nEq+1))-1;
478 if( (m & eqMask)!=m ) break;
479 }
480 score = nEq*4;
481 m = 1<<nEq;
482 if( m & ltMask ) score++;
483 if( m & gtMask ) score+=2;
drh48185c12002-06-09 01:55:20 +0000484 if( score==0 && inMask ) score = 4;
drh487ab3c2001-11-08 00:45:21 +0000485 if( score>bestScore ){
486 pBestIdx = pIdx;
487 bestScore = score;
drh75897232000-05-29 14:26:00 +0000488 }
489 }
drh6b563442001-11-07 16:48:26 +0000490 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000491 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000492 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000493 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000494 pWInfo->a[i].iCur = pParse->nTab++;
495 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000496 }
drh75897232000-05-29 14:26:00 +0000497 }
498
drh6b563442001-11-07 16:48:26 +0000499 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000500 */
drhad3cab52002-05-24 02:04:32 +0000501 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000502 int openOp;
503 Table *pTab;
504
505 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000506 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000507 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000508 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
509 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000510 if( i==0 && !pParse->schemaVerified &&
511 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000512 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000513 pParse->schemaVerified = 1;
514 }
drh6b563442001-11-07 16:48:26 +0000515 if( pWInfo->a[i].pIdx!=0 ){
516 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
517 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000518 }
519 }
520
521 /* Generate the code to do the search
522 */
drh75897232000-05-29 14:26:00 +0000523 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000524 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000525 int j, k;
526 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000527 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000528 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000529
drhad2d8302002-05-24 20:31:36 +0000530 /* If this is the right table of a LEFT OUTER JOIN, allocate and
531 ** initialize a memory cell that record if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000532 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000533 */
534 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
535 if( !pParse->nMem ) pParse->nMem++;
536 pLevel->iLeftJoin = pParse->nMem++;
537 sqliteVdbeAddOp(v, OP_String, 0, 0);
538 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
539 }
540
drh8aff1012001-12-22 14:49:24 +0000541 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000542 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000543 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
544 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000545 ** equality comparison against the ROWID field. Or
546 ** we reference multiple rows using a "rowid IN (...)"
547 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000548 */
drh8aff1012001-12-22 14:49:24 +0000549 k = iDirectEq[i];
550 assert( k<nExpr );
551 assert( aExpr[k].p!=0 );
552 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
drhd99f7062002-06-08 23:25:08 +0000553 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000554 if( aExpr[k].idxLeft==idx ){
drhd99f7062002-06-08 23:25:08 +0000555 Expr *pX = aExpr[k].p;
556 if( pX->op!=TK_IN ){
557 sqliteExprCode(pParse, aExpr[k].p->pRight);
558 }else if( pX->pList ){
559 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
560 pLevel->inOp = OP_SetNext;
561 pLevel->inP1 = pX->iTable;
562 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
563 }else{
564 assert( pX->pSelect );
565 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
566 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
567 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
568 pLevel->inOp = OP_Next;
569 pLevel->inP1 = pX->iTable;
570 }
drh8aff1012001-12-22 14:49:24 +0000571 }else{
572 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000573 }
drh8aff1012001-12-22 14:49:24 +0000574 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000575 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000576 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhd99f7062002-06-08 23:25:08 +0000577 haveKey = 0;
drh6b125452002-01-28 15:53:03 +0000578 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000579 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000580 }else if( pIdx!=0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000581 /* Case 2: There is an index and all terms of the WHERE clause that
582 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000583 */
drh6b563442001-11-07 16:48:26 +0000584 int start;
drh487ab3c2001-11-08 00:45:21 +0000585 int testOp;
586 int nColumn = pLevel->score/4;
drhd99f7062002-06-08 23:25:08 +0000587 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000588 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000589 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000590 Expr *pX = aExpr[k].p;
591 if( pX==0 ) continue;
drh75897232000-05-29 14:26:00 +0000592 if( aExpr[k].idxLeft==idx
593 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000594 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000595 ){
drhd99f7062002-06-08 23:25:08 +0000596 if( pX->op==TK_EQ ){
597 sqliteExprCode(pParse, pX->pRight);
598 aExpr[k].p = 0;
599 break;
600 }
601 if( pX->op==TK_IN && nColumn==1 ){
602 if( pX->pList ){
603 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
604 pLevel->inOp = OP_SetNext;
605 pLevel->inP1 = pX->iTable;
606 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
607 }else{
608 assert( pX->pSelect );
609 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
610 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
611 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
612 pLevel->inOp = OP_Next;
613 pLevel->inP1 = pX->iTable;
614 }
615 aExpr[k].p = 0;
616 break;
617 }
drh75897232000-05-29 14:26:00 +0000618 }
619 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000620 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000621 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000622 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000623 ){
624 sqliteExprCode(pParse, aExpr[k].p->pLeft);
625 aExpr[k].p = 0;
626 break;
627 }
628 }
629 }
drh6b563442001-11-07 16:48:26 +0000630 pLevel->iMem = pParse->nMem++;
drh6b563442001-11-07 16:48:26 +0000631 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000632 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
633 if( nColumn==pIdx->nColumn ){
634 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
635 testOp = OP_IdxGT;
636 }else{
637 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
638 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
639 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
640 testOp = OP_IdxGE;
641 }
drh6b563442001-11-07 16:48:26 +0000642 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
643 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000644 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000645 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000646 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000647 haveKey = 1;
648 }else{
drh99fcd712001-10-13 01:06:47 +0000649 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000650 haveKey = 0;
651 }
drh6b563442001-11-07 16:48:26 +0000652 pLevel->op = OP_Next;
653 pLevel->p1 = pLevel->iCur;
654 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000655 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
656 /* Case 3: We have an inequality comparison against the ROWID field.
657 */
658 int testOp = OP_Noop;
659 int start;
660
661 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
662 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
663 if( iDirectGt[i]>=0 ){
664 k = iDirectGt[i];
665 assert( k<nExpr );
666 assert( aExpr[k].p!=0 );
667 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
668 if( aExpr[k].idxLeft==idx ){
669 sqliteExprCode(pParse, aExpr[k].p->pRight);
670 }else{
671 sqliteExprCode(pParse, aExpr[k].p->pLeft);
672 }
673 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
674 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
675 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
676 }
677 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
678 aExpr[k].p = 0;
679 }else{
680 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
681 }
682 if( iDirectLt[i]>=0 ){
683 k = iDirectLt[i];
684 assert( k<nExpr );
685 assert( aExpr[k].p!=0 );
686 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
687 if( aExpr[k].idxLeft==idx ){
688 sqliteExprCode(pParse, aExpr[k].p->pRight);
689 }else{
690 sqliteExprCode(pParse, aExpr[k].p->pLeft);
691 }
692 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
693 pLevel->iMem = pParse->nMem++;
694 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
695 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
696 testOp = OP_Ge;
697 }else{
698 testOp = OP_Gt;
699 }
700 aExpr[k].p = 0;
701 }
702 start = sqliteVdbeCurrentAddr(v);
703 pLevel->op = OP_Next;
704 pLevel->p1 = base+idx;
705 pLevel->p2 = start;
706 if( testOp!=OP_Noop ){
707 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
708 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
709 sqliteVdbeAddOp(v, testOp, 0, brk);
710 }
711 haveKey = 0;
712 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000713 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000714 ** scan of the entire database table.
715 */
716 int start;
717
718 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
719 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
720 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
721 start = sqliteVdbeCurrentAddr(v);
722 pLevel->op = OP_Next;
723 pLevel->p1 = base+idx;
724 pLevel->p2 = start;
725 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000726 }else{
drhc27a1ce2002-06-14 20:58:45 +0000727 /* Case 5: The WHERE clause term that refers to the right-most
728 ** column of the index is an inequality. For example, if
729 ** the index is on (x,y,z) and the WHERE clause is of the
730 ** form "x=5 AND y<10" then this case is used. Only the
731 ** right-most column can be an inequality - the rest must
732 ** use the "==" operator.
drh487ab3c2001-11-08 00:45:21 +0000733 */
734 int score = pLevel->score;
735 int nEqColumn = score/4;
736 int start;
737 int leFlag, geFlag;
738 int testOp;
739
740 /* Evaluate the equality constraints
741 */
742 for(j=0; j<nEqColumn; j++){
743 for(k=0; k<nExpr; k++){
744 if( aExpr[k].p==0 ) continue;
745 if( aExpr[k].idxLeft==idx
746 && aExpr[k].p->op==TK_EQ
747 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
748 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
749 ){
750 sqliteExprCode(pParse, aExpr[k].p->pRight);
751 aExpr[k].p = 0;
752 break;
753 }
754 if( aExpr[k].idxRight==idx
755 && aExpr[k].p->op==TK_EQ
756 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
757 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
758 ){
759 sqliteExprCode(pParse, aExpr[k].p->pLeft);
760 aExpr[k].p = 0;
761 break;
762 }
763 }
764 }
765
drhc27a1ce2002-06-14 20:58:45 +0000766 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000767 ** used twice: once to make the termination key and once to make the
768 ** start key.
769 */
770 for(j=0; j<nEqColumn; j++){
771 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
772 }
773
774 /* Generate the termination key. This is the key value that
775 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000776 ** are no equality terms and no "X<..." term.
drh487ab3c2001-11-08 00:45:21 +0000777 */
778 if( (score & 1)!=0 ){
779 for(k=0; k<nExpr; k++){
780 Expr *pExpr = aExpr[k].p;
781 if( pExpr==0 ) continue;
782 if( aExpr[k].idxLeft==idx
783 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
784 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
785 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
786 ){
787 sqliteExprCode(pParse, pExpr->pRight);
788 leFlag = pExpr->op==TK_LE;
789 aExpr[k].p = 0;
790 break;
791 }
792 if( aExpr[k].idxRight==idx
793 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
794 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
795 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
796 ){
797 sqliteExprCode(pParse, pExpr->pLeft);
798 leFlag = pExpr->op==TK_GE;
799 aExpr[k].p = 0;
800 break;
801 }
802 }
803 testOp = OP_IdxGE;
804 }else{
805 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
806 leFlag = 1;
807 }
808 if( testOp!=OP_Noop ){
809 pLevel->iMem = pParse->nMem++;
810 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
811 if( leFlag ){
812 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
813 }
814 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
815 }
816
817 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +0000818 ** bound on the search. There is no start key if there are no
819 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +0000820 ** that case, generate a "Rewind" instruction in place of the
821 ** start key search.
822 */
823 if( (score & 2)!=0 ){
824 for(k=0; k<nExpr; k++){
825 Expr *pExpr = aExpr[k].p;
826 if( pExpr==0 ) continue;
827 if( aExpr[k].idxLeft==idx
828 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
829 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
830 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
831 ){
832 sqliteExprCode(pParse, pExpr->pRight);
833 geFlag = pExpr->op==TK_GE;
834 aExpr[k].p = 0;
835 break;
836 }
837 if( aExpr[k].idxRight==idx
838 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
839 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
840 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
841 ){
842 sqliteExprCode(pParse, pExpr->pLeft);
843 geFlag = pExpr->op==TK_LE;
844 aExpr[k].p = 0;
845 break;
846 }
847 }
drh7900ead2001-11-12 13:51:43 +0000848 }else{
849 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000850 }
851 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
852 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
853 if( nEqColumn>0 || (score&2)!=0 ){
854 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
855 if( !geFlag ){
856 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
857 }
858 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
859 }else{
860 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
861 }
862
863 /* Generate the the top of the loop. If there is a termination
864 ** key we have to test for that key and abort at the top of the
865 ** loop.
866 */
867 start = sqliteVdbeCurrentAddr(v);
868 if( testOp!=OP_Noop ){
869 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
870 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
871 }
872 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000873 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +0000874 haveKey = 1;
875 }else{
876 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
877 haveKey = 0;
878 }
879
880 /* Record the instruction used to terminate the loop.
881 */
882 pLevel->op = OP_Next;
883 pLevel->p1 = pLevel->iCur;
884 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000885 }
886 loopMask |= 1<<idx;
887
888 /* Insert code to test every subexpression that can be completely
889 ** computed using the current set of tables.
890 */
891 for(j=0; j<nExpr; j++){
892 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +0000893 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh75897232000-05-29 14:26:00 +0000894 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000895 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000896 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000897 }
drhf5905aa2002-05-26 20:54:33 +0000898 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +0000899 aExpr[j].p = 0;
900 }
901 brk = cont;
drhad2d8302002-05-24 20:31:36 +0000902
903 /* For a LEFT OUTER JOIN, generate code that will record the fact that
904 ** at least one row of the right table has matched the left table.
905 */
906 if( pLevel->iLeftJoin ){
907 pLevel->top = sqliteVdbeCurrentAddr(v);
908 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
909 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
910 }
drh75897232000-05-29 14:26:00 +0000911 }
912 pWInfo->iContinue = cont;
913 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000914 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000915 }
916 sqliteFree(aOrder);
917 return pWInfo;
918}
919
920/*
drhc27a1ce2002-06-14 20:58:45 +0000921** Generate the end of the WHERE loop. See comments on
922** sqliteWhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +0000923*/
924void sqliteWhereEnd(WhereInfo *pWInfo){
925 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000926 int i;
drh19a775c2000-06-05 18:54:46 +0000927 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000928 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +0000929 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000930
drhad3cab52002-05-24 02:04:32 +0000931 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000932 pLevel = &pWInfo->a[i];
933 sqliteVdbeResolveLabel(v, pLevel->cont);
934 if( pLevel->op!=OP_Noop ){
935 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000936 }
drh6b563442001-11-07 16:48:26 +0000937 sqliteVdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +0000938 if( pLevel->inOp!=OP_Noop ){
939 sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
940 }
drhad2d8302002-05-24 20:31:36 +0000941 if( pLevel->iLeftJoin ){
942 int addr;
943 addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
944 sqliteVdbeAddOp(v, OP_NotNull, 0, addr+4);
945 sqliteVdbeAddOp(v, OP_NullRow, base+i, 0);
946 sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
947 }
drh19a775c2000-06-05 18:54:46 +0000948 }
drh6b563442001-11-07 16:48:26 +0000949 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +0000950 for(i=0; i<pTabList->nSrc; i++){
drh22f70c32002-02-18 01:17:00 +0000951 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000952 pLevel = &pWInfo->a[i];
953 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
954 if( pLevel->pIdx!=0 ){
955 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
956 }
drh19a775c2000-06-05 18:54:46 +0000957 }
drh832508b2002-03-02 17:04:07 +0000958 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
959 pWInfo->pParse->nTab = pWInfo->savedNTab;
960 }
drh75897232000-05-29 14:26:00 +0000961 sqliteFree(pWInfo);
962 return;
963}