blob: d71121ea9e6bd8ee7c1367339021286ec1ee58a6 [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**
drh48185c12002-06-09 01:55:20 +000016** $Id: where.c,v 1.50 2002/06/09 01:55:20 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
106** "=", "<", ">", "<=", and ">=".
107*/
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.
159*/
160WhereInfo *sqliteWhereBegin(
161 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000162 int base, /* VDBE cursor index for left-most table in pTabList */
drhad3cab52002-05-24 02:04:32 +0000163 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000164 Expr *pWhere, /* The WHERE clause */
165 int pushKey /* If TRUE, leave the table key on the stack */
166){
167 int i; /* Loop counter */
168 WhereInfo *pWInfo; /* Will become the return value of this function */
169 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
170 int brk, cont; /* Addresses used during code generation */
171 int *aOrder; /* Order in which pTabList entries are searched */
172 int nExpr; /* Number of subexpressions in the WHERE clause */
173 int loopMask; /* One bit set for each outer loop */
174 int haveKey; /* True if KEY is on the stack */
drhc4a3c772001-04-04 11:48:57 +0000175 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh8aff1012001-12-22 14:49:24 +0000176 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
177 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
178 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh75897232000-05-29 14:26:00 +0000179 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
180
drh6b563442001-11-07 16:48:26 +0000181 /* Allocate space for aOrder[] and aiMem[]. */
drhad3cab52002-05-24 02:04:32 +0000182 aOrder = sqliteMalloc( sizeof(int) * pTabList->nSrc );
drh75897232000-05-29 14:26:00 +0000183
184 /* Allocate and initialize the WhereInfo structure that will become the
185 ** return value.
186 */
drhad3cab52002-05-24 02:04:32 +0000187 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
drhdaffd0e2001-04-11 14:28:42 +0000188 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000189 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000190 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000191 return 0;
192 }
193 pWInfo->pParse = pParse;
194 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000195 pWInfo->base = base;
196 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
drh08192d52002-04-30 19:20:28 +0000197 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
198
199 /* Special case: a WHERE clause that is constant. Evaluate the
200 ** expression and either jump over all of the code or fall thru.
201 */
202 if( pWhere && sqliteExprIsConstant(pWhere) ){
drhf5905aa2002-05-26 20:54:33 +0000203 sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drh08192d52002-04-30 19:20:28 +0000204 }
drh75897232000-05-29 14:26:00 +0000205
206 /* Split the WHERE clause into as many as 32 separate subexpressions
207 ** where each subexpression is separated by an AND operator. Any additional
208 ** subexpressions are attached in the aExpr[32] and will not enter
209 ** into the query optimizer computations. 32 is chosen as the cutoff
210 ** since that is the number of bits in an integer that we use for an
211 ** expression-used mask.
212 */
213 memset(aExpr, 0, sizeof(aExpr));
214 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
215
216 /* Analyze all of the subexpressions.
217 */
218 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000219 exprAnalyze(base, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000220
221 /* If we are executing a trigger body, remove all references to
222 ** new.* and old.* tables from the prerequisite masks.
223 */
224 if( pParse->trigStack ){
225 int x;
226 if( (x = pParse->trigStack->newIdx) >= 0 ){
227 int mask = ~(1 << (x - base));
228 aExpr[i].prereqRight &= mask;
229 aExpr[i].prereqLeft &= mask;
230 aExpr[i].prereqAll &= mask;
231 }
232 if( (x = pParse->trigStack->oldIdx) >= 0 ){
233 int mask = ~(1 << (x - base));
234 aExpr[i].prereqRight &= mask;
235 aExpr[i].prereqLeft &= mask;
236 aExpr[i].prereqAll &= mask;
237 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000238 }
drh75897232000-05-29 14:26:00 +0000239 }
240
241 /* Figure out a good nesting order for the tables. aOrder[0] will
242 ** be the index in pTabList of the outermost table. aOrder[1] will
drhad3cab52002-05-24 02:04:32 +0000243 ** be the first nested loop and so on. aOrder[pTabList->nSrc-1] will
drh75897232000-05-29 14:26:00 +0000244 ** be the innermost loop.
245 **
drh1d1f3052002-05-21 13:18:25 +0000246 ** Someday we will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000247 ** for an effiecient query. But for now, just use whatever order the
248 ** tables appear in in the pTabList.
249 */
drhad3cab52002-05-24 02:04:32 +0000250 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000251 aOrder[i] = i;
252 }
253
254 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000255 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000256 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000257 ** loop.
258 **
259 ** If terms exist that use the ROWID of any table, then set the
260 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
261 ** to the index of the term containing the ROWID. We always prefer
262 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000263 ** index which requires reading an index first to get the rowid then
264 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000265 **
266 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000267 ** first 32 tables are candidates for indices. This is (again) due
268 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000269 */
270 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000271 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000272 int j;
drh75897232000-05-29 14:26:00 +0000273 int idx = aOrder[i];
274 Table *pTab = pTabList->a[idx].pTab;
275 Index *pIdx;
276 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000277 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000278
drhc4a3c772001-04-04 11:48:57 +0000279 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000280 ** ROWID field of this table. For terms of the form ROWID==expr
281 ** set iDirectEq[i] to the index of the term. For terms of the
282 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
283 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000284 */
drh8aff1012001-12-22 14:49:24 +0000285 iDirectEq[i] = -1;
286 iDirectLt[i] = -1;
287 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000288 for(j=0; j<nExpr; j++){
289 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
290 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000291 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000292 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000293 case TK_EQ: iDirectEq[i] = j; break;
294 case TK_LE:
295 case TK_LT: iDirectLt[i] = j; break;
296 case TK_GE:
297 case TK_GT: iDirectGt[i] = j; break;
298 }
drhc4a3c772001-04-04 11:48:57 +0000299 }
300 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
301 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000302 switch( aExpr[j].p->op ){
303 case TK_EQ: iDirectEq[i] = j; break;
304 case TK_LE:
305 case TK_LT: iDirectGt[i] = j; break;
306 case TK_GE:
307 case TK_GT: iDirectLt[i] = j; break;
308 }
drhc4a3c772001-04-04 11:48:57 +0000309 }
310 }
drh8aff1012001-12-22 14:49:24 +0000311 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000312 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000313 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000314 continue;
315 }
316
drh75897232000-05-29 14:26:00 +0000317 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000318 ** the "best" index. pBestIdx is left set to NULL if no indices
319 ** are usable.
drh75897232000-05-29 14:26:00 +0000320 **
drh487ab3c2001-11-08 00:45:21 +0000321 ** The best index is determined as follows. For each of the
322 ** left-most terms that is fixed by an equality operator, add
323 ** 4 to the score. The right-most term of the index may be
324 ** constrained by an inequality. Add 1 if for an "x<..." constraint
325 ** and add 2 for an "x>..." constraint. Chose the index that
326 ** gives the best score.
327 **
328 ** This scoring system is designed so that the score can later be
329 ** used to determine how the index is used. If the score&3 is 0
330 ** then all constraints are equalities. If score&1 is not 0 then
331 ** there is an inequality used as a termination key. (ex: "x<...")
332 ** If score&2 is not 0 then there is an inequality used as the
333 ** start key. (ex: "x>...");
drhd99f7062002-06-08 23:25:08 +0000334 **
335 ** The IN operator as in "<expr> IN (...)" is treated the same as
336 ** an equality comparison.
drh75897232000-05-29 14:26:00 +0000337 */
338 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000339 int eqMask = 0; /* Index columns covered by an x=... constraint */
340 int ltMask = 0; /* Index columns covered by an x<... constraint */
drh48185c12002-06-09 01:55:20 +0000341 int gtMask = 0; /* Index columns covered by an x>... constraint */
342 int inMask = 0; /* Index columns covered by an x IN .. constraint */
drh487ab3c2001-11-08 00:45:21 +0000343 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000344
drh74e24cd2002-01-09 03:19:59 +0000345 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000346 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000347 for(j=0; j<nExpr; j++){
348 if( aExpr[j].idxLeft==idx
349 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000350 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000351 int k;
drh967e8b72000-06-21 13:59:10 +0000352 for(k=0; k<pIdx->nColumn; k++){
353 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000354 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000355 case TK_IN: {
356 if( k==0 ) inMask |= 1;
357 break;
358 }
drh487ab3c2001-11-08 00:45:21 +0000359 case TK_EQ: {
360 eqMask |= 1<<k;
361 break;
362 }
363 case TK_LE:
364 case TK_LT: {
365 ltMask |= 1<<k;
366 break;
367 }
368 case TK_GE:
369 case TK_GT: {
370 gtMask |= 1<<k;
371 break;
372 }
373 default: {
374 /* CANT_HAPPEN */
375 assert( 0 );
376 break;
377 }
378 }
drh75897232000-05-29 14:26:00 +0000379 break;
380 }
381 }
382 }
383 if( aExpr[j].idxRight==idx
384 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000385 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000386 int k;
drh967e8b72000-06-21 13:59:10 +0000387 for(k=0; k<pIdx->nColumn; k++){
388 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000389 switch( aExpr[j].p->op ){
390 case TK_EQ: {
391 eqMask |= 1<<k;
392 break;
393 }
394 case TK_LE:
395 case TK_LT: {
396 gtMask |= 1<<k;
397 break;
398 }
399 case TK_GE:
400 case TK_GT: {
401 ltMask |= 1<<k;
402 break;
403 }
404 default: {
405 /* CANT_HAPPEN */
406 assert( 0 );
407 break;
408 }
409 }
drh75897232000-05-29 14:26:00 +0000410 break;
411 }
412 }
413 }
414 }
drh487ab3c2001-11-08 00:45:21 +0000415 for(nEq=0; nEq<pIdx->nColumn; nEq++){
416 m = (1<<(nEq+1))-1;
417 if( (m & eqMask)!=m ) break;
418 }
419 score = nEq*4;
420 m = 1<<nEq;
421 if( m & ltMask ) score++;
422 if( m & gtMask ) score+=2;
drh48185c12002-06-09 01:55:20 +0000423 if( score==0 && inMask ) score = 4;
drh487ab3c2001-11-08 00:45:21 +0000424 if( score>bestScore ){
425 pBestIdx = pIdx;
426 bestScore = score;
drh75897232000-05-29 14:26:00 +0000427 }
428 }
drh6b563442001-11-07 16:48:26 +0000429 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000430 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000431 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000432 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000433 pWInfo->a[i].iCur = pParse->nTab++;
434 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000435 }
drh75897232000-05-29 14:26:00 +0000436 }
437
drh6b563442001-11-07 16:48:26 +0000438 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000439 */
drhad3cab52002-05-24 02:04:32 +0000440 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000441 int openOp;
442 Table *pTab;
443
444 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000445 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000446 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000447 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
448 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000449 if( i==0 && !pParse->schemaVerified &&
450 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000451 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000452 pParse->schemaVerified = 1;
453 }
drh6b563442001-11-07 16:48:26 +0000454 if( pWInfo->a[i].pIdx!=0 ){
455 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
456 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000457 }
458 }
459
460 /* Generate the code to do the search
461 */
drh75897232000-05-29 14:26:00 +0000462 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000463 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000464 int j, k;
465 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000466 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000467 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000468
drhad2d8302002-05-24 20:31:36 +0000469 /* If this is the right table of a LEFT OUTER JOIN, allocate and
470 ** initialize a memory cell that record if this table matches any
471 ** row of the left table in the join.
472 */
473 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
474 if( !pParse->nMem ) pParse->nMem++;
475 pLevel->iLeftJoin = pParse->nMem++;
476 sqliteVdbeAddOp(v, OP_String, 0, 0);
477 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
478 }
479
drh8aff1012001-12-22 14:49:24 +0000480 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000481 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000482 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
483 /* Case 1: We can directly reference a single row using an
484 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000485 */
drh8aff1012001-12-22 14:49:24 +0000486 k = iDirectEq[i];
487 assert( k<nExpr );
488 assert( aExpr[k].p!=0 );
489 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
drhd99f7062002-06-08 23:25:08 +0000490 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000491 if( aExpr[k].idxLeft==idx ){
drhd99f7062002-06-08 23:25:08 +0000492 Expr *pX = aExpr[k].p;
493 if( pX->op!=TK_IN ){
494 sqliteExprCode(pParse, aExpr[k].p->pRight);
495 }else if( pX->pList ){
496 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
497 pLevel->inOp = OP_SetNext;
498 pLevel->inP1 = pX->iTable;
499 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
500 }else{
501 assert( pX->pSelect );
502 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
503 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
504 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
505 pLevel->inOp = OP_Next;
506 pLevel->inP1 = pX->iTable;
507 }
drh8aff1012001-12-22 14:49:24 +0000508 }else{
509 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000510 }
drh8aff1012001-12-22 14:49:24 +0000511 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000512 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000513 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhd99f7062002-06-08 23:25:08 +0000514 haveKey = 0;
drh6b125452002-01-28 15:53:03 +0000515 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000516 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000517 }else if( pIdx!=0 && pLevel->score%4==0 ){
518 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000519 */
drh6b563442001-11-07 16:48:26 +0000520 int start;
drh487ab3c2001-11-08 00:45:21 +0000521 int testOp;
522 int nColumn = pLevel->score/4;
drhd99f7062002-06-08 23:25:08 +0000523 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000524 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000525 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000526 Expr *pX = aExpr[k].p;
527 if( pX==0 ) continue;
drh75897232000-05-29 14:26:00 +0000528 if( aExpr[k].idxLeft==idx
529 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000530 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000531 ){
drhd99f7062002-06-08 23:25:08 +0000532 if( pX->op==TK_EQ ){
533 sqliteExprCode(pParse, pX->pRight);
534 aExpr[k].p = 0;
535 break;
536 }
537 if( pX->op==TK_IN && nColumn==1 ){
538 if( pX->pList ){
539 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
540 pLevel->inOp = OP_SetNext;
541 pLevel->inP1 = pX->iTable;
542 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
543 }else{
544 assert( pX->pSelect );
545 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
546 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
547 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
548 pLevel->inOp = OP_Next;
549 pLevel->inP1 = pX->iTable;
550 }
551 aExpr[k].p = 0;
552 break;
553 }
drh75897232000-05-29 14:26:00 +0000554 }
555 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000556 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000557 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000558 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000559 ){
560 sqliteExprCode(pParse, aExpr[k].p->pLeft);
561 aExpr[k].p = 0;
562 break;
563 }
564 }
565 }
drh6b563442001-11-07 16:48:26 +0000566 pLevel->iMem = pParse->nMem++;
drh6b563442001-11-07 16:48:26 +0000567 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000568 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
569 if( nColumn==pIdx->nColumn ){
570 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
571 testOp = OP_IdxGT;
572 }else{
573 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
574 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
575 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
576 testOp = OP_IdxGE;
577 }
drh6b563442001-11-07 16:48:26 +0000578 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
579 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000580 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000581 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000582 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000583 haveKey = 1;
584 }else{
drh99fcd712001-10-13 01:06:47 +0000585 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000586 haveKey = 0;
587 }
drh6b563442001-11-07 16:48:26 +0000588 pLevel->op = OP_Next;
589 pLevel->p1 = pLevel->iCur;
590 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000591 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
592 /* Case 3: We have an inequality comparison against the ROWID field.
593 */
594 int testOp = OP_Noop;
595 int start;
596
597 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
598 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
599 if( iDirectGt[i]>=0 ){
600 k = iDirectGt[i];
601 assert( k<nExpr );
602 assert( aExpr[k].p!=0 );
603 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
604 if( aExpr[k].idxLeft==idx ){
605 sqliteExprCode(pParse, aExpr[k].p->pRight);
606 }else{
607 sqliteExprCode(pParse, aExpr[k].p->pLeft);
608 }
609 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
610 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
611 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
612 }
613 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
614 aExpr[k].p = 0;
615 }else{
616 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
617 }
618 if( iDirectLt[i]>=0 ){
619 k = iDirectLt[i];
620 assert( k<nExpr );
621 assert( aExpr[k].p!=0 );
622 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
623 if( aExpr[k].idxLeft==idx ){
624 sqliteExprCode(pParse, aExpr[k].p->pRight);
625 }else{
626 sqliteExprCode(pParse, aExpr[k].p->pLeft);
627 }
628 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
629 pLevel->iMem = pParse->nMem++;
630 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
631 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
632 testOp = OP_Ge;
633 }else{
634 testOp = OP_Gt;
635 }
636 aExpr[k].p = 0;
637 }
638 start = sqliteVdbeCurrentAddr(v);
639 pLevel->op = OP_Next;
640 pLevel->p1 = base+idx;
641 pLevel->p2 = start;
642 if( testOp!=OP_Noop ){
643 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
644 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
645 sqliteVdbeAddOp(v, testOp, 0, brk);
646 }
647 haveKey = 0;
648 }else if( pIdx==0 ){
649 /* Case 4: There was no usable index. We must do a complete
650 ** scan of the entire database table.
651 */
652 int start;
653
654 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
655 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
656 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
657 start = sqliteVdbeCurrentAddr(v);
658 pLevel->op = OP_Next;
659 pLevel->p1 = base+idx;
660 pLevel->p2 = start;
661 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000662 }else{
drhaacc5432002-01-06 17:07:40 +0000663 /* Case 5: The contraint on the right-most index field is
664 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000665 */
666 int score = pLevel->score;
667 int nEqColumn = score/4;
668 int start;
669 int leFlag, geFlag;
670 int testOp;
671
672 /* Evaluate the equality constraints
673 */
674 for(j=0; j<nEqColumn; j++){
675 for(k=0; k<nExpr; k++){
676 if( aExpr[k].p==0 ) continue;
677 if( aExpr[k].idxLeft==idx
678 && aExpr[k].p->op==TK_EQ
679 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
680 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
681 ){
682 sqliteExprCode(pParse, aExpr[k].p->pRight);
683 aExpr[k].p = 0;
684 break;
685 }
686 if( aExpr[k].idxRight==idx
687 && aExpr[k].p->op==TK_EQ
688 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
689 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
690 ){
691 sqliteExprCode(pParse, aExpr[k].p->pLeft);
692 aExpr[k].p = 0;
693 break;
694 }
695 }
696 }
697
698 /* Duplicate the equality contraint values because they will all be
699 ** used twice: once to make the termination key and once to make the
700 ** start key.
701 */
702 for(j=0; j<nEqColumn; j++){
703 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
704 }
705
706 /* Generate the termination key. This is the key value that
707 ** will end the search. There is no termination key if there
708 ** are no equality contraints and no "X<..." constraint.
709 */
710 if( (score & 1)!=0 ){
711 for(k=0; k<nExpr; k++){
712 Expr *pExpr = aExpr[k].p;
713 if( pExpr==0 ) continue;
714 if( aExpr[k].idxLeft==idx
715 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
716 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
717 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
718 ){
719 sqliteExprCode(pParse, pExpr->pRight);
720 leFlag = pExpr->op==TK_LE;
721 aExpr[k].p = 0;
722 break;
723 }
724 if( aExpr[k].idxRight==idx
725 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
726 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
727 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
728 ){
729 sqliteExprCode(pParse, pExpr->pLeft);
730 leFlag = pExpr->op==TK_GE;
731 aExpr[k].p = 0;
732 break;
733 }
734 }
735 testOp = OP_IdxGE;
736 }else{
737 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
738 leFlag = 1;
739 }
740 if( testOp!=OP_Noop ){
741 pLevel->iMem = pParse->nMem++;
742 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
743 if( leFlag ){
744 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
745 }
746 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
747 }
748
749 /* Generate the start key. This is the key that defines the lower
750 ** bound on the search. There is no start key if there are not
751 ** equality constraints and if there is no "X>..." constraint. In
752 ** that case, generate a "Rewind" instruction in place of the
753 ** start key search.
754 */
755 if( (score & 2)!=0 ){
756 for(k=0; k<nExpr; k++){
757 Expr *pExpr = aExpr[k].p;
758 if( pExpr==0 ) continue;
759 if( aExpr[k].idxLeft==idx
760 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
761 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
762 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
763 ){
764 sqliteExprCode(pParse, pExpr->pRight);
765 geFlag = pExpr->op==TK_GE;
766 aExpr[k].p = 0;
767 break;
768 }
769 if( aExpr[k].idxRight==idx
770 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
771 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
772 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
773 ){
774 sqliteExprCode(pParse, pExpr->pLeft);
775 geFlag = pExpr->op==TK_LE;
776 aExpr[k].p = 0;
777 break;
778 }
779 }
drh7900ead2001-11-12 13:51:43 +0000780 }else{
781 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000782 }
783 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
784 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
785 if( nEqColumn>0 || (score&2)!=0 ){
786 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
787 if( !geFlag ){
788 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
789 }
790 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
791 }else{
792 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
793 }
794
795 /* Generate the the top of the loop. If there is a termination
796 ** key we have to test for that key and abort at the top of the
797 ** loop.
798 */
799 start = sqliteVdbeCurrentAddr(v);
800 if( testOp!=OP_Noop ){
801 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
802 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
803 }
804 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000805 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +0000806 haveKey = 1;
807 }else{
808 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
809 haveKey = 0;
810 }
811
812 /* Record the instruction used to terminate the loop.
813 */
814 pLevel->op = OP_Next;
815 pLevel->p1 = pLevel->iCur;
816 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000817 }
818 loopMask |= 1<<idx;
819
820 /* Insert code to test every subexpression that can be completely
821 ** computed using the current set of tables.
822 */
823 for(j=0; j<nExpr; j++){
824 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +0000825 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh75897232000-05-29 14:26:00 +0000826 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000827 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000828 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000829 }
drhf5905aa2002-05-26 20:54:33 +0000830 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +0000831 aExpr[j].p = 0;
832 }
833 brk = cont;
drhad2d8302002-05-24 20:31:36 +0000834
835 /* For a LEFT OUTER JOIN, generate code that will record the fact that
836 ** at least one row of the right table has matched the left table.
837 */
838 if( pLevel->iLeftJoin ){
839 pLevel->top = sqliteVdbeCurrentAddr(v);
840 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
841 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
842 }
drh75897232000-05-29 14:26:00 +0000843 }
844 pWInfo->iContinue = cont;
845 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000846 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000847 }
848 sqliteFree(aOrder);
849 return pWInfo;
850}
851
852/*
853** Generate the end of the WHERE loop.
854*/
855void sqliteWhereEnd(WhereInfo *pWInfo){
856 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000857 int i;
drh19a775c2000-06-05 18:54:46 +0000858 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000859 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +0000860 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000861
drhad3cab52002-05-24 02:04:32 +0000862 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000863 pLevel = &pWInfo->a[i];
864 sqliteVdbeResolveLabel(v, pLevel->cont);
865 if( pLevel->op!=OP_Noop ){
866 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000867 }
drh6b563442001-11-07 16:48:26 +0000868 sqliteVdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +0000869 if( pLevel->inOp!=OP_Noop ){
870 sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
871 }
drhad2d8302002-05-24 20:31:36 +0000872 if( pLevel->iLeftJoin ){
873 int addr;
874 addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
875 sqliteVdbeAddOp(v, OP_NotNull, 0, addr+4);
876 sqliteVdbeAddOp(v, OP_NullRow, base+i, 0);
877 sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
878 }
drh19a775c2000-06-05 18:54:46 +0000879 }
drh6b563442001-11-07 16:48:26 +0000880 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +0000881 for(i=0; i<pTabList->nSrc; i++){
drh22f70c32002-02-18 01:17:00 +0000882 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000883 pLevel = &pWInfo->a[i];
884 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
885 if( pLevel->pIdx!=0 ){
886 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
887 }
drh19a775c2000-06-05 18:54:46 +0000888 }
drh832508b2002-03-02 17:04:07 +0000889 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
890 pWInfo->pParse->nTab = pWInfo->savedNTab;
891 }
drh75897232000-05-29 14:26:00 +0000892 sqliteFree(pWInfo);
893 return;
894}