blob: fda6b299d8be314140396e0af964aed9eb1f487f [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** This module contains C code that generates VDBE code used to process
drhb2fe7d82003-04-20 17:29:23 +000013** the WHERE clause of SQL statements.
drh75897232000-05-29 14:26:00 +000014**
drh6a3ea0e2003-05-02 14:32:12 +000015** $Id: where.c,v 1.78 2003/05/02 14:32:14 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include "sqliteInt.h"
18
19/*
20** The query generator uses an array of instances of this structure to
21** help it analyze the subexpressions of the WHERE clause. Each WHERE
22** clause subexpression is separated from the others by an AND operator.
23*/
24typedef struct ExprInfo ExprInfo;
25struct ExprInfo {
26 Expr *p; /* Pointer to the subexpression */
drhe3184742002-06-19 14:27:05 +000027 u8 indexable; /* True if this subexprssion is usable by an index */
drh1f162302002-10-27 19:35:33 +000028 u8 oracle8join; /* -1 if left side contains "(+)". +1 if right side
29 ** contains "(+)". 0 if neither contains "(+)" */
drhe3184742002-06-19 14:27:05 +000030 short int idxLeft; /* p->pLeft is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000031 ** p->pLeft is not the column of any table */
drhe3184742002-06-19 14:27:05 +000032 short int idxRight; /* p->pRight is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000033 ** p->pRight is not the column of any table */
drhe3184742002-06-19 14:27:05 +000034 unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */
35 unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */
36 unsigned prereqAll; /* Bitmask of tables referenced by p */
drh75897232000-05-29 14:26:00 +000037};
38
39/*
drh6a3ea0e2003-05-02 14:32:12 +000040** An instance of the following structure keeps track of a mapping
41** between VDBE cursor numbers and bitmasks. The VDBE cursor numbers
42** are small integers contained in SrcList_item.iCursor and Expr.iTable
43** fields. For any given WHERE clause, we want to track which cursors
44** are being used, so we assign a single bit in a 32-bit word to track
45** that cursor. Then a 32-bit integer is able to show the set of all
46** cursors being used.
47*/
48typedef struct ExprMaskSet ExprMaskSet;
49struct ExprMaskSet {
50 int n; /* Number of assigned cursor values */
51 int ix[32]; /* Cursor assigned to each bit */
52};
53
54/*
drh75897232000-05-29 14:26:00 +000055** Determine the number of elements in an array.
56*/
57#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
58
59/*
60** This routine is used to divide the WHERE expression into subexpressions
61** separated by the AND operator.
62**
63** aSlot[] is an array of subexpressions structures.
64** There are nSlot spaces left in this array. This routine attempts to
65** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
66** The return value is the number of slots filled.
67*/
68static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
69 int cnt = 0;
70 if( pExpr==0 || nSlot<1 ) return 0;
71 if( nSlot==1 || pExpr->op!=TK_AND ){
72 aSlot[0].p = pExpr;
73 return 1;
74 }
75 if( pExpr->pLeft->op!=TK_AND ){
76 aSlot[0].p = pExpr->pLeft;
77 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
78 }else{
drhdcd997e2003-01-31 17:21:49 +000079 cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
80 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
drh75897232000-05-29 14:26:00 +000081 }
82 return cnt;
83}
84
85/*
drh6a3ea0e2003-05-02 14:32:12 +000086** Initialize an expression mask set
87*/
88#define initMaskSet(P) memset(P, 0, sizeof(*P))
89
90/*
91** Return the bitmask for the given cursor. Assign a new bitmask
92** if this is the first time the cursor has been seen.
93*/
94static int getMask(ExprMaskSet *pMaskSet, int iCursor){
95 int i;
96 for(i=0; i<pMaskSet->n; i++){
97 if( pMaskSet->ix[i]==iCursor ) return 1<<i;
98 }
99 if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
100 pMaskSet->n++;
101 pMaskSet->ix[i] = iCursor;
102 return 1<<i;
103 }
104 return 0;
105}
106
107/*
108** Destroy an expression mask set
109*/
110#define freeMaskSet(P) /* NO-OP */
111
112/*
drh75897232000-05-29 14:26:00 +0000113** This routine walks (recursively) an expression tree and generates
114** a bitmask indicating which tables are used in that expression
drh6a3ea0e2003-05-02 14:32:12 +0000115** tree.
drh75897232000-05-29 14:26:00 +0000116**
117** In order for this routine to work, the calling function must have
118** previously invoked sqliteExprResolveIds() on the expression. See
119** the header comment on that routine for additional information.
drh6a3ea0e2003-05-02 14:32:12 +0000120** The sqliteExprResolveIds() routines looks for column names and
121** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
122** the VDBE cursor number of the table.
drh75897232000-05-29 14:26:00 +0000123*/
drh6a3ea0e2003-05-02 14:32:12 +0000124static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
drh75897232000-05-29 14:26:00 +0000125 unsigned int mask = 0;
126 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +0000127 if( p->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000128 return getMask(pMaskSet, p->iTable);
drh75897232000-05-29 14:26:00 +0000129 }
130 if( p->pRight ){
drh6a3ea0e2003-05-02 14:32:12 +0000131 mask = exprTableUsage(pMaskSet, p->pRight);
drh75897232000-05-29 14:26:00 +0000132 }
133 if( p->pLeft ){
drh6a3ea0e2003-05-02 14:32:12 +0000134 mask |= exprTableUsage(pMaskSet, p->pLeft);
drh75897232000-05-29 14:26:00 +0000135 }
drhdd579122002-04-02 01:58:57 +0000136 if( p->pList ){
137 int i;
138 for(i=0; i<p->pList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000139 mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
drhdd579122002-04-02 01:58:57 +0000140 }
141 }
drh75897232000-05-29 14:26:00 +0000142 return mask;
143}
144
145/*
drh487ab3c2001-11-08 00:45:21 +0000146** Return TRUE if the given operator is one of the operators that is
147** allowed for an indexable WHERE clause. The allowed operators are
drhc27a1ce2002-06-14 20:58:45 +0000148** "=", "<", ">", "<=", ">=", and "IN".
drh487ab3c2001-11-08 00:45:21 +0000149*/
150static int allowedOp(int op){
151 switch( op ){
152 case TK_LT:
153 case TK_LE:
154 case TK_GT:
155 case TK_GE:
156 case TK_EQ:
drhd99f7062002-06-08 23:25:08 +0000157 case TK_IN:
drh487ab3c2001-11-08 00:45:21 +0000158 return 1;
159 default:
160 return 0;
161 }
162}
163
164/*
drh75897232000-05-29 14:26:00 +0000165** The input to this routine is an ExprInfo structure with only the
166** "p" field filled in. The job of this routine is to analyze the
167** subexpression and populate all the other fields of the ExprInfo
168** structure.
169*/
drh6a3ea0e2003-05-02 14:32:12 +0000170static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000171 Expr *pExpr = pInfo->p;
drh6a3ea0e2003-05-02 14:32:12 +0000172 pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
173 pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
174 pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
drh75897232000-05-29 14:26:00 +0000175 pInfo->indexable = 0;
176 pInfo->idxLeft = -1;
177 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000178 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drhd99f7062002-06-08 23:25:08 +0000179 if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000180 pInfo->idxRight = pExpr->pRight->iTable;
drh75897232000-05-29 14:26:00 +0000181 pInfo->indexable = 1;
182 }
drh967e8b72000-06-21 13:59:10 +0000183 if( pExpr->pLeft->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000184 pInfo->idxLeft = pExpr->pLeft->iTable;
drh75897232000-05-29 14:26:00 +0000185 pInfo->indexable = 1;
186 }
187 }
188}
189
190/*
drhe3184742002-06-19 14:27:05 +0000191** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
192** left-most table in the FROM clause of that same SELECT statement and
193** the table has a cursor number of "base".
194**
195** This routine attempts to find an index for pTab that generates the
196** correct record sequence for the given ORDER BY clause. The return value
197** is a pointer to an index that does the job. NULL is returned if the
198** table has no index that will generate the correct sort order.
199**
200** If there are two or more indices that generate the correct sort order
201** and pPreferredIdx is one of those indices, then return pPreferredIdx.
drhdd4852c2002-12-04 21:50:16 +0000202**
203** nEqCol is the number of columns of pPreferredIdx that are used as
204** equality constraints. Any index returned must have exactly this same
205** set of columns. The ORDER BY clause only matches index columns beyond the
206** the first nEqCol columns.
207**
208** All terms of the ORDER BY clause must be either ASC or DESC. The
209** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
210** set to 0 if the ORDER BY clause is all ASC.
drhe3184742002-06-19 14:27:05 +0000211*/
212static Index *findSortingIndex(
213 Table *pTab, /* The table to be sorted */
214 int base, /* Cursor number for pTab */
215 ExprList *pOrderBy, /* The ORDER BY clause */
drhc045ec52002-12-04 20:01:06 +0000216 Index *pPreferredIdx, /* Use this index, if possible and not NULL */
drhdd4852c2002-12-04 21:50:16 +0000217 int nEqCol, /* Number of index columns used with == constraints */
drhc045ec52002-12-04 20:01:06 +0000218 int *pbRev /* Set to 1 if ORDER BY is DESC */
drhe3184742002-06-19 14:27:05 +0000219){
drhdd4852c2002-12-04 21:50:16 +0000220 int i, j;
drhe3184742002-06-19 14:27:05 +0000221 Index *pMatch;
222 Index *pIdx;
drhc045ec52002-12-04 20:01:06 +0000223 int sortOrder;
drhe3184742002-06-19 14:27:05 +0000224
225 assert( pOrderBy!=0 );
226 assert( pOrderBy->nExpr>0 );
drhc045ec52002-12-04 20:01:06 +0000227 sortOrder = pOrderBy->a[0].sortOrder & SQLITE_SO_DIRMASK;
drhe3184742002-06-19 14:27:05 +0000228 for(i=0; i<pOrderBy->nExpr; i++){
229 Expr *p;
drhc045ec52002-12-04 20:01:06 +0000230 if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){
231 /* Indices can only be used if all ORDER BY terms are either
232 ** DESC or ASC. Indices cannot be used on a mixture. */
drhe3184742002-06-19 14:27:05 +0000233 return 0;
234 }
drhc330af12002-08-14 03:03:57 +0000235 if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){
236 /* Do not sort by index if there is a COLLATE clause */
237 return 0;
238 }
drhe3184742002-06-19 14:27:05 +0000239 p = pOrderBy->a[i].pExpr;
240 if( p->op!=TK_COLUMN || p->iTable!=base ){
241 /* Can not use an index sort on anything that is not a column in the
242 ** left-most table of the FROM clause */
243 return 0;
244 }
245 }
drhc045ec52002-12-04 20:01:06 +0000246
drhe3184742002-06-19 14:27:05 +0000247 /* If we get this far, it means the ORDER BY clause consists only of
248 ** ascending columns in the left-most table of the FROM clause. Now
249 ** check for a matching index.
250 */
251 pMatch = 0;
252 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhdd4852c2002-12-04 21:50:16 +0000253 int nExpr = pOrderBy->nExpr;
254 if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
255 for(i=j=0; i<nEqCol; i++){
256 if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
257 if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; }
drhe3184742002-06-19 14:27:05 +0000258 }
drhdd4852c2002-12-04 21:50:16 +0000259 if( i<nEqCol ) continue;
260 for(i=0; i+j<nExpr; i++){
261 if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break;
262 }
263 if( i+j>=nExpr ){
drhe3184742002-06-19 14:27:05 +0000264 pMatch = pIdx;
265 if( pIdx==pPreferredIdx ) break;
266 }
267 }
drhc045ec52002-12-04 20:01:06 +0000268 if( pMatch && pbRev ){
269 *pbRev = sortOrder==SQLITE_SO_DESC;
270 }
drhe3184742002-06-19 14:27:05 +0000271 return pMatch;
272}
273
274/*
275** Generate the beginning of the loop used for WHERE clause processing.
drh75897232000-05-29 14:26:00 +0000276** The return value is a pointer to an (opaque) structure that contains
277** information needed to terminate the loop. Later, the calling routine
278** should invoke sqliteWhereEnd() with the return value of this function
279** in order to complete the WHERE clause processing.
280**
281** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000282**
283** The basic idea is to do a nested loop, one loop for each table in
284** the FROM clause of a select. (INSERT and UPDATE statements are the
285** same as a SELECT with only a single table in the FROM clause.) For
286** example, if the SQL is this:
287**
288** SELECT * FROM t1, t2, t3 WHERE ...;
289**
290** Then the code generated is conceptually like the following:
291**
292** foreach row1 in t1 do \ Code generated
293** foreach row2 in t2 do |-- by sqliteWhereBegin()
294** foreach row3 in t3 do /
295** ...
296** end \ Code generated
297** end |-- by sqliteWhereEnd()
298** end /
299**
300** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000301** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
302** And so forth. This routine generates code to open those VDBE cursors
303** and sqliteWhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000304**
305** If the WHERE clause is empty, the foreach loops must each scan their
306** entire tables. Thus a three-way join is an O(N^3) operation. But if
307** the tables have indices and there are terms in the WHERE clause that
308** refer to those indices, a complete table scan can be avoided and the
309** code will run much faster. Most of the work of this routine is checking
310** to see if there are indices that can be used to speed up the loop.
311**
312** Terms of the WHERE clause are also used to limit which rows actually
313** make it to the "..." in the middle of the loop. After each "foreach",
314** terms of the WHERE clause that use only terms in that loop and outer
315** loops are evaluated and if false a jump is made around all subsequent
316** inner loops (or around the "..." if the test occurs within the inner-
317** most loop)
318**
319** OUTER JOINS
320**
321** An outer join of tables t1 and t2 is conceptally coded as follows:
322**
323** foreach row1 in t1 do
324** flag = 0
325** foreach row2 in t2 do
326** start:
327** ...
328** flag = 1
329** end
drhe3184742002-06-19 14:27:05 +0000330** if flag==0 then
331** move the row2 cursor to a null row
332** goto start
333** fi
drhc27a1ce2002-06-14 20:58:45 +0000334** end
335**
drhe3184742002-06-19 14:27:05 +0000336** ORDER BY CLAUSE PROCESSING
337**
338** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
339** if there is one. If there is no ORDER BY clause or if this routine
340** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
341**
342** If an index can be used so that the natural output order of the table
343** scan is correct for the ORDER BY clause, then that index is used and
344** *ppOrderBy is set to NULL. This is an optimization that prevents an
345** unnecessary sort of the result set if an index appropriate for the
346** ORDER BY clause already exists.
347**
348** If the where clause loops cannot be arranged to provide the correct
349** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000350*/
351WhereInfo *sqliteWhereBegin(
352 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000353 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000354 Expr *pWhere, /* The WHERE clause */
drhe3184742002-06-19 14:27:05 +0000355 int pushKey, /* If TRUE, leave the table key on the stack */
356 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000357){
358 int i; /* Loop counter */
359 WhereInfo *pWInfo; /* Will become the return value of this function */
360 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
361 int brk, cont; /* Addresses used during code generation */
drh75897232000-05-29 14:26:00 +0000362 int nExpr; /* Number of subexpressions in the WHERE clause */
363 int loopMask; /* One bit set for each outer loop */
364 int haveKey; /* True if KEY is on the stack */
drh6a3ea0e2003-05-02 14:32:12 +0000365 ExprMaskSet maskSet; /* The expression mask set */
drh8aff1012001-12-22 14:49:24 +0000366 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
367 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
368 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh83dcb1a2002-06-28 01:02:38 +0000369 ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
drh75897232000-05-29 14:26:00 +0000370
drhc27a1ce2002-06-14 20:58:45 +0000371 /* pushKey is only allowed if there is a single table (as in an INSERT or
372 ** UPDATE statement)
373 */
374 assert( pushKey==0 || pTabList->nSrc==1 );
drh83dcb1a2002-06-28 01:02:38 +0000375
376 /* Split the WHERE clause into separate subexpressions where each
377 ** subexpression is separated by an AND operator. If the aExpr[]
378 ** array fills up, the last entry might point to an expression which
379 ** contains additional unfactored AND operators.
380 */
drh6a3ea0e2003-05-02 14:32:12 +0000381 initMaskSet(&maskSet);
drh83dcb1a2002-06-28 01:02:38 +0000382 memset(aExpr, 0, sizeof(aExpr));
383 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
384 if( nExpr==ARRAYSIZE(aExpr) ){
385 char zBuf[50];
drhfd159812003-01-11 14:25:39 +0000386 sprintf(zBuf, "%d", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000387 sqliteSetString(&pParse->zErrMsg, "WHERE clause too complex - no more "
388 "than ", zBuf, " terms allowed", 0);
389 pParse->nErr++;
390 return 0;
391 }
drhc27a1ce2002-06-14 20:58:45 +0000392
drh75897232000-05-29 14:26:00 +0000393 /* Allocate and initialize the WhereInfo structure that will become the
394 ** return value.
395 */
drhad3cab52002-05-24 02:04:32 +0000396 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
drhdaffd0e2001-04-11 14:28:42 +0000397 if( sqlite_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +0000398 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000399 return 0;
400 }
401 pWInfo->pParse = pParse;
402 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000403 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
drh08192d52002-04-30 19:20:28 +0000404 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
405
406 /* Special case: a WHERE clause that is constant. Evaluate the
407 ** expression and either jump over all of the code or fall thru.
408 */
drh37ea94b2003-04-19 16:34:04 +0000409 if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){
drhf5905aa2002-05-26 20:54:33 +0000410 sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000411 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000412 }
drh75897232000-05-29 14:26:00 +0000413
drh75897232000-05-29 14:26:00 +0000414 /* Analyze all of the subexpressions.
415 */
416 for(i=0; i<nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000417 exprAnalyze(&maskSet, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000418
419 /* If we are executing a trigger body, remove all references to
420 ** new.* and old.* tables from the prerequisite masks.
421 */
422 if( pParse->trigStack ){
423 int x;
424 if( (x = pParse->trigStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000425 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000426 aExpr[i].prereqRight &= mask;
427 aExpr[i].prereqLeft &= mask;
428 aExpr[i].prereqAll &= mask;
429 }
430 if( (x = pParse->trigStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000431 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000432 aExpr[i].prereqRight &= mask;
433 aExpr[i].prereqLeft &= mask;
434 aExpr[i].prereqAll &= mask;
435 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000436 }
drh75897232000-05-29 14:26:00 +0000437 }
438
drh75897232000-05-29 14:26:00 +0000439 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000440 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000441 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000442 ** loop.
443 **
444 ** If terms exist that use the ROWID of any table, then set the
445 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
446 ** to the index of the term containing the ROWID. We always prefer
447 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000448 ** index which requires reading an index first to get the rowid then
449 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000450 **
451 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000452 ** first 32 tables are candidates for indices. This is (again) due
453 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000454 */
455 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000456 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000457 int j;
drh6a3ea0e2003-05-02 14:32:12 +0000458 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
459 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
460 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000461 Index *pIdx;
462 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000463 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000464
drhc4a3c772001-04-04 11:48:57 +0000465 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000466 ** ROWID field of this table. For terms of the form ROWID==expr
467 ** set iDirectEq[i] to the index of the term. For terms of the
468 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
469 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000470 **
471 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000472 */
drhc8f8b632002-09-30 12:36:26 +0000473 pWInfo->a[i].iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000474 iDirectEq[i] = -1;
475 iDirectLt[i] = -1;
476 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000477 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000478 if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000479 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000480 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000481 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000482 case TK_EQ: iDirectEq[i] = j; break;
483 case TK_LE:
484 case TK_LT: iDirectLt[i] = j; break;
485 case TK_GE:
486 case TK_GT: iDirectGt[i] = j; break;
487 }
drhc4a3c772001-04-04 11:48:57 +0000488 }
drh6a3ea0e2003-05-02 14:32:12 +0000489 if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000490 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000491 switch( aExpr[j].p->op ){
492 case TK_EQ: iDirectEq[i] = j; break;
493 case TK_LE:
494 case TK_LT: iDirectGt[i] = j; break;
495 case TK_GE:
496 case TK_GT: iDirectLt[i] = j; break;
497 }
drhc4a3c772001-04-04 11:48:57 +0000498 }
499 }
drh8aff1012001-12-22 14:49:24 +0000500 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000501 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000502 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000503 continue;
504 }
505
drh75897232000-05-29 14:26:00 +0000506 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000507 ** the "best" index. pBestIdx is left set to NULL if no indices
508 ** are usable.
drh75897232000-05-29 14:26:00 +0000509 **
drh487ab3c2001-11-08 00:45:21 +0000510 ** The best index is determined as follows. For each of the
511 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000512 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000513 ** constrained by an inequality. Add 1 if for an "x<..." constraint
514 ** and add 2 for an "x>..." constraint. Chose the index that
515 ** gives the best score.
516 **
517 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000518 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000519 ** then all constraints are equalities. If score&1 is not 0 then
520 ** there is an inequality used as a termination key. (ex: "x<...")
521 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000522 ** start key. (ex: "x>..."). A score or 4 is the special case
523 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000524 **
drhc27a1ce2002-06-14 20:58:45 +0000525 ** The IN operator (as in "<expr> IN (...)") is treated the same as
526 ** an equality comparison except that it can only be used on the
527 ** left-most column of an index and other terms of the WHERE clause
528 ** cannot be used in conjunction with the IN operator to help satisfy
529 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000530 */
531 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000532 int eqMask = 0; /* Index columns covered by an x=... term */
533 int ltMask = 0; /* Index columns covered by an x<... term */
534 int gtMask = 0; /* Index columns covered by an x>... term */
535 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000536 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000537
drh487ab3c2001-11-08 00:45:21 +0000538 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000539 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000540 if( aExpr[j].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000541 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000542 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000543 int k;
drh967e8b72000-06-21 13:59:10 +0000544 for(k=0; k<pIdx->nColumn; k++){
545 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000546 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000547 case TK_IN: {
548 if( k==0 ) inMask |= 1;
549 break;
550 }
drh487ab3c2001-11-08 00:45:21 +0000551 case TK_EQ: {
552 eqMask |= 1<<k;
553 break;
554 }
555 case TK_LE:
556 case TK_LT: {
557 ltMask |= 1<<k;
558 break;
559 }
560 case TK_GE:
561 case TK_GT: {
562 gtMask |= 1<<k;
563 break;
564 }
565 default: {
566 /* CANT_HAPPEN */
567 assert( 0 );
568 break;
569 }
570 }
drh75897232000-05-29 14:26:00 +0000571 break;
572 }
573 }
574 }
drh6a3ea0e2003-05-02 14:32:12 +0000575 if( aExpr[j].idxRight==iCur
drh75897232000-05-29 14:26:00 +0000576 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000577 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000578 int k;
drh967e8b72000-06-21 13:59:10 +0000579 for(k=0; k<pIdx->nColumn; k++){
580 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000581 switch( aExpr[j].p->op ){
582 case TK_EQ: {
583 eqMask |= 1<<k;
584 break;
585 }
586 case TK_LE:
587 case TK_LT: {
588 gtMask |= 1<<k;
589 break;
590 }
591 case TK_GE:
592 case TK_GT: {
593 ltMask |= 1<<k;
594 break;
595 }
596 default: {
597 /* CANT_HAPPEN */
598 assert( 0 );
599 break;
600 }
601 }
drh75897232000-05-29 14:26:00 +0000602 break;
603 }
604 }
605 }
606 }
drhc045ec52002-12-04 20:01:06 +0000607
608 /* The following loop ends with nEq set to the number of columns
609 ** on the left of the index with == constraints.
610 */
drh487ab3c2001-11-08 00:45:21 +0000611 for(nEq=0; nEq<pIdx->nColumn; nEq++){
612 m = (1<<(nEq+1))-1;
613 if( (m & eqMask)!=m ) break;
614 }
drhc045ec52002-12-04 20:01:06 +0000615 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000616 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000617 if( m & ltMask ) score++; /* Increase score for a < constraint */
618 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
619 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000620 if( score>bestScore ){
621 pBestIdx = pIdx;
622 bestScore = score;
drh75897232000-05-29 14:26:00 +0000623 }
624 }
drh6b563442001-11-07 16:48:26 +0000625 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000626 pWInfo->a[i].score = bestScore;
drhc045ec52002-12-04 20:01:06 +0000627 pWInfo->a[i].bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000628 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000629 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000630 pWInfo->a[i].iCur = pParse->nTab++;
631 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000632 }
drh75897232000-05-29 14:26:00 +0000633 }
634
drhe3184742002-06-19 14:27:05 +0000635 /* Check to see if the ORDER BY clause is or can be satisfied by the
636 ** use of an index on the first table.
637 */
638 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
639 Index *pSortIdx;
640 Index *pIdx;
641 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000642 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000643
644 pTab = pTabList->a[0].pTab;
645 pIdx = pWInfo->a[0].pIdx;
646 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000647 /* If there is already an IN index on the left-most table,
648 ** it will not give the correct sort order.
649 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000650 */
651 pSortIdx = 0;
652 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
653 /* If the left-most column is accessed using its ROWID, then do
654 ** not try to sort by index.
655 */
656 pSortIdx = 0;
657 }else{
drhdd4852c2002-12-04 21:50:16 +0000658 int nEqCol = (pWInfo->a[0].score+4)/8;
drh6a3ea0e2003-05-02 14:32:12 +0000659 pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
660 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000661 }
662 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
663 if( pIdx==0 ){
664 pWInfo->a[0].pIdx = pSortIdx;
665 pWInfo->a[0].iCur = pParse->nTab++;
666 pWInfo->peakNTab = pParse->nTab;
667 }
drhc045ec52002-12-04 20:01:06 +0000668 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000669 *ppOrderBy = 0;
670 }
671 }
672
drh6b563442001-11-07 16:48:26 +0000673 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000674 */
drhad3cab52002-05-24 02:04:32 +0000675 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000676 Table *pTab;
677
678 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000679 if( pTab->isTransient || pTab->pSelect ) continue;
drhd24cc422003-03-27 12:51:24 +0000680 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh6a3ea0e2003-05-02 14:32:12 +0000681 sqliteVdbeAddOp(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +0000682 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000683 if( i==0 && !pParse->schemaVerified &&
684 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh001bbcb2003-03-19 03:14:00 +0000685 sqliteCodeVerifySchema(pParse);
drh50e5dad2001-09-15 00:57:28 +0000686 }
drh6b563442001-11-07 16:48:26 +0000687 if( pWInfo->a[i].pIdx!=0 ){
drhd24cc422003-03-27 12:51:24 +0000688 sqliteVdbeAddOp(v, OP_Integer, pWInfo->a[i].pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000689 sqliteVdbeAddOp(v, OP_OpenRead,
690 pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
drh6b563442001-11-07 16:48:26 +0000691 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000692 }
693 }
694
695 /* Generate the code to do the search
696 */
drh75897232000-05-29 14:26:00 +0000697 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000698 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000699 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000700 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000701 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000702 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000703
drhad2d8302002-05-24 20:31:36 +0000704 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000705 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000706 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000707 */
708 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
709 if( !pParse->nMem ) pParse->nMem++;
710 pLevel->iLeftJoin = pParse->nMem++;
711 sqliteVdbeAddOp(v, OP_String, 0, 0);
712 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
713 }
714
drh8aff1012001-12-22 14:49:24 +0000715 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000716 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000717 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
718 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000719 ** equality comparison against the ROWID field. Or
720 ** we reference multiple rows using a "rowid IN (...)"
721 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000722 */
drh8aff1012001-12-22 14:49:24 +0000723 k = iDirectEq[i];
724 assert( k<nExpr );
725 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000726 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
drhd99f7062002-06-08 23:25:08 +0000727 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000728 if( aExpr[k].idxLeft==iCur ){
drhd99f7062002-06-08 23:25:08 +0000729 Expr *pX = aExpr[k].p;
730 if( pX->op!=TK_IN ){
731 sqliteExprCode(pParse, aExpr[k].p->pRight);
732 }else if( pX->pList ){
733 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
734 pLevel->inOp = OP_SetNext;
735 pLevel->inP1 = pX->iTable;
736 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
737 }else{
738 assert( pX->pSelect );
739 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
740 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
741 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
742 pLevel->inOp = OP_Next;
743 pLevel->inP1 = pX->iTable;
744 }
drh8aff1012001-12-22 14:49:24 +0000745 }else{
746 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000747 }
drh8aff1012001-12-22 14:49:24 +0000748 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000749 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drhf1351b62002-07-31 19:50:26 +0000750 sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000751 haveKey = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000752 sqliteVdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000753 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000754 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000755 /* Case 2: There is an index and all terms of the WHERE clause that
756 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000757 */
drh6b563442001-11-07 16:48:26 +0000758 int start;
drh487ab3c2001-11-08 00:45:21 +0000759 int testOp;
drhc045ec52002-12-04 20:01:06 +0000760 int nColumn = (pLevel->score+4)/8;
drhd99f7062002-06-08 23:25:08 +0000761 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000762 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000763 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000764 Expr *pX = aExpr[k].p;
765 if( pX==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000766 if( aExpr[k].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000767 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000768 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000769 ){
drhd99f7062002-06-08 23:25:08 +0000770 if( pX->op==TK_EQ ){
771 sqliteExprCode(pParse, pX->pRight);
772 aExpr[k].p = 0;
773 break;
774 }
775 if( pX->op==TK_IN && nColumn==1 ){
776 if( pX->pList ){
777 sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
778 pLevel->inOp = OP_SetNext;
779 pLevel->inP1 = pX->iTable;
780 pLevel->inP2 = sqliteVdbeCurrentAddr(v);
781 }else{
782 assert( pX->pSelect );
783 sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
784 sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
785 pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
786 pLevel->inOp = OP_Next;
787 pLevel->inP1 = pX->iTable;
788 }
789 aExpr[k].p = 0;
790 break;
791 }
drh75897232000-05-29 14:26:00 +0000792 }
drh6a3ea0e2003-05-02 14:32:12 +0000793 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000794 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000795 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000796 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000797 ){
798 sqliteExprCode(pParse, aExpr[k].p->pLeft);
799 aExpr[k].p = 0;
800 break;
801 }
802 }
803 }
drh6b563442001-11-07 16:48:26 +0000804 pLevel->iMem = pParse->nMem++;
drh6b563442001-11-07 16:48:26 +0000805 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000806 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
drha9e99ae2002-08-13 23:02:57 +0000807 sqliteAddIdxKeyType(v, pIdx);
drhc045ec52002-12-04 20:01:06 +0000808 if( nColumn==pIdx->nColumn || pLevel->bRev ){
drh487ab3c2001-11-08 00:45:21 +0000809 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
810 testOp = OP_IdxGT;
811 }else{
812 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
813 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
814 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
815 testOp = OP_IdxGE;
816 }
drhc045ec52002-12-04 20:01:06 +0000817 if( pLevel->bRev ){
818 /* Scan in reverse order */
819 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
820 sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
821 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
822 sqliteVdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
823 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
824 pLevel->op = OP_Prev;
825 }else{
826 /* Scan in the forward order */
827 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
828 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
829 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
830 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
831 pLevel->op = OP_Next;
832 }
drhad3cab52002-05-24 02:04:32 +0000833 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000834 haveKey = 1;
835 }else{
drh6a3ea0e2003-05-02 14:32:12 +0000836 sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
drh75897232000-05-29 14:26:00 +0000837 haveKey = 0;
838 }
drh6b563442001-11-07 16:48:26 +0000839 pLevel->p1 = pLevel->iCur;
840 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000841 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
842 /* Case 3: We have an inequality comparison against the ROWID field.
843 */
844 int testOp = OP_Noop;
845 int start;
846
847 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
848 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
849 if( iDirectGt[i]>=0 ){
850 k = iDirectGt[i];
851 assert( k<nExpr );
852 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000853 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
854 if( aExpr[k].idxLeft==iCur ){
drh8aff1012001-12-22 14:49:24 +0000855 sqliteExprCode(pParse, aExpr[k].p->pRight);
856 }else{
857 sqliteExprCode(pParse, aExpr[k].p->pLeft);
858 }
drhf1351b62002-07-31 19:50:26 +0000859 sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk);
drh8aff1012001-12-22 14:49:24 +0000860 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
861 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
862 }
drh6a3ea0e2003-05-02 14:32:12 +0000863 sqliteVdbeAddOp(v, OP_MoveTo, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000864 aExpr[k].p = 0;
865 }else{
drh6a3ea0e2003-05-02 14:32:12 +0000866 sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000867 }
868 if( iDirectLt[i]>=0 ){
869 k = iDirectLt[i];
870 assert( k<nExpr );
871 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000872 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
873 if( aExpr[k].idxLeft==iCur ){
drh8aff1012001-12-22 14:49:24 +0000874 sqliteExprCode(pParse, aExpr[k].p->pRight);
875 }else{
876 sqliteExprCode(pParse, aExpr[k].p->pLeft);
877 }
drhf1351b62002-07-31 19:50:26 +0000878 sqliteVdbeAddOp(v, OP_MustBeInt, 1, sqliteVdbeCurrentAddr(v)+1);
drh8aff1012001-12-22 14:49:24 +0000879 pLevel->iMem = pParse->nMem++;
880 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
881 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
882 testOp = OP_Ge;
883 }else{
884 testOp = OP_Gt;
885 }
886 aExpr[k].p = 0;
887 }
888 start = sqliteVdbeCurrentAddr(v);
889 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000890 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000891 pLevel->p2 = start;
892 if( testOp!=OP_Noop ){
drh6a3ea0e2003-05-02 14:32:12 +0000893 sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
drh8aff1012001-12-22 14:49:24 +0000894 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
895 sqliteVdbeAddOp(v, testOp, 0, brk);
896 }
897 haveKey = 0;
898 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000899 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000900 ** scan of the entire database table.
901 */
902 int start;
903
904 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
905 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000906 sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000907 start = sqliteVdbeCurrentAddr(v);
908 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000909 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000910 pLevel->p2 = start;
911 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000912 }else{
drhc27a1ce2002-06-14 20:58:45 +0000913 /* Case 5: The WHERE clause term that refers to the right-most
914 ** column of the index is an inequality. For example, if
915 ** the index is on (x,y,z) and the WHERE clause is of the
916 ** form "x=5 AND y<10" then this case is used. Only the
917 ** right-most column can be an inequality - the rest must
918 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000919 **
920 ** This case is also used when there are no WHERE clause
921 ** constraints but an index is selected anyway, in order
922 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000923 */
924 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000925 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000926 int start;
927 int leFlag, geFlag;
928 int testOp;
929
930 /* Evaluate the equality constraints
931 */
932 for(j=0; j<nEqColumn; j++){
933 for(k=0; k<nExpr; k++){
934 if( aExpr[k].p==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000935 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000936 && aExpr[k].p->op==TK_EQ
937 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
938 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
939 ){
940 sqliteExprCode(pParse, aExpr[k].p->pRight);
941 aExpr[k].p = 0;
942 break;
943 }
drh6a3ea0e2003-05-02 14:32:12 +0000944 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000945 && aExpr[k].p->op==TK_EQ
946 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
947 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
948 ){
949 sqliteExprCode(pParse, aExpr[k].p->pLeft);
950 aExpr[k].p = 0;
951 break;
952 }
953 }
954 }
955
drhc27a1ce2002-06-14 20:58:45 +0000956 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000957 ** used twice: once to make the termination key and once to make the
958 ** start key.
959 */
960 for(j=0; j<nEqColumn; j++){
961 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
962 }
963
drhc045ec52002-12-04 20:01:06 +0000964 /* Labels for the beginning and end of the loop
965 */
966 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
967 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
968
drh487ab3c2001-11-08 00:45:21 +0000969 /* Generate the termination key. This is the key value that
970 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000971 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +0000972 **
973 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
974 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +0000975 */
976 if( (score & 1)!=0 ){
977 for(k=0; k<nExpr; k++){
978 Expr *pExpr = aExpr[k].p;
979 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000980 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000981 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
982 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
983 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
984 ){
985 sqliteExprCode(pParse, pExpr->pRight);
986 leFlag = pExpr->op==TK_LE;
987 aExpr[k].p = 0;
988 break;
989 }
drh6a3ea0e2003-05-02 14:32:12 +0000990 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000991 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
992 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
993 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
994 ){
995 sqliteExprCode(pParse, pExpr->pLeft);
996 leFlag = pExpr->op==TK_GE;
997 aExpr[k].p = 0;
998 break;
999 }
1000 }
1001 testOp = OP_IdxGE;
1002 }else{
1003 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1004 leFlag = 1;
1005 }
1006 if( testOp!=OP_Noop ){
1007 pLevel->iMem = pParse->nMem++;
1008 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
drha9e99ae2002-08-13 23:02:57 +00001009 sqliteAddIdxKeyType(v, pIdx);
drh487ab3c2001-11-08 00:45:21 +00001010 if( leFlag ){
1011 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
1012 }
drhc045ec52002-12-04 20:01:06 +00001013 if( pLevel->bRev ){
1014 sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
1015 }else{
1016 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1017 }
1018 }else if( pLevel->bRev ){
1019 sqliteVdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001020 }
1021
1022 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001023 ** bound on the search. There is no start key if there are no
1024 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001025 ** that case, generate a "Rewind" instruction in place of the
1026 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001027 **
1028 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1029 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001030 */
1031 if( (score & 2)!=0 ){
1032 for(k=0; k<nExpr; k++){
1033 Expr *pExpr = aExpr[k].p;
1034 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001035 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001036 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1037 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1038 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1039 ){
1040 sqliteExprCode(pParse, pExpr->pRight);
1041 geFlag = pExpr->op==TK_GE;
1042 aExpr[k].p = 0;
1043 break;
1044 }
drh6a3ea0e2003-05-02 14:32:12 +00001045 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001046 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1047 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1048 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1049 ){
1050 sqliteExprCode(pParse, pExpr->pLeft);
1051 geFlag = pExpr->op==TK_LE;
1052 aExpr[k].p = 0;
1053 break;
1054 }
1055 }
drh7900ead2001-11-12 13:51:43 +00001056 }else{
1057 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001058 }
drh487ab3c2001-11-08 00:45:21 +00001059 if( nEqColumn>0 || (score&2)!=0 ){
1060 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
drha9e99ae2002-08-13 23:02:57 +00001061 sqliteAddIdxKeyType(v, pIdx);
drh487ab3c2001-11-08 00:45:21 +00001062 if( !geFlag ){
1063 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
1064 }
drhc045ec52002-12-04 20:01:06 +00001065 if( pLevel->bRev ){
1066 pLevel->iMem = pParse->nMem++;
1067 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1068 testOp = OP_IdxLT;
1069 }else{
1070 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
1071 }
1072 }else if( pLevel->bRev ){
1073 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001074 }else{
1075 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
1076 }
1077
1078 /* Generate the the top of the loop. If there is a termination
1079 ** key we have to test for that key and abort at the top of the
1080 ** loop.
1081 */
1082 start = sqliteVdbeCurrentAddr(v);
1083 if( testOp!=OP_Noop ){
1084 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1085 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
1086 }
1087 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001088 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001089 haveKey = 1;
1090 }else{
drh6a3ea0e2003-05-02 14:32:12 +00001091 sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001092 haveKey = 0;
1093 }
1094
1095 /* Record the instruction used to terminate the loop.
1096 */
drhc045ec52002-12-04 20:01:06 +00001097 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001098 pLevel->p1 = pLevel->iCur;
1099 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001100 }
drh6a3ea0e2003-05-02 14:32:12 +00001101 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001102
1103 /* Insert code to test every subexpression that can be completely
1104 ** computed using the current set of tables.
1105 */
1106 for(j=0; j<nExpr; j++){
1107 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +00001108 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh1f162302002-10-27 19:35:33 +00001109 if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1110 continue;
1111 }
drh75897232000-05-29 14:26:00 +00001112 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001113 haveKey = 0;
drh6a3ea0e2003-05-02 14:32:12 +00001114 sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
drh75897232000-05-29 14:26:00 +00001115 }
drhf5905aa2002-05-26 20:54:33 +00001116 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +00001117 aExpr[j].p = 0;
1118 }
1119 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001120
1121 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1122 ** at least one row of the right table has matched the left table.
1123 */
1124 if( pLevel->iLeftJoin ){
1125 pLevel->top = sqliteVdbeCurrentAddr(v);
1126 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
1127 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drh1cc093c2002-06-24 22:01:57 +00001128 for(j=0; j<nExpr; j++){
1129 if( aExpr[j].p==0 ) continue;
1130 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1131 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001132 /* Cannot happen. "haveKey" can only be true if pushKey is true
1133 ** an pushKey can only be true for DELETE and UPDATE and there are
1134 ** no outer joins with DELETE and UPDATE.
1135 */
drh1cc093c2002-06-24 22:01:57 +00001136 haveKey = 0;
drh6a3ea0e2003-05-02 14:32:12 +00001137 sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001138 }
1139 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
1140 aExpr[j].p = 0;
1141 }
drhad2d8302002-05-24 20:31:36 +00001142 }
drh75897232000-05-29 14:26:00 +00001143 }
1144 pWInfo->iContinue = cont;
1145 if( pushKey && !haveKey ){
drh6a3ea0e2003-05-02 14:32:12 +00001146 sqliteVdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001147 }
drh6a3ea0e2003-05-02 14:32:12 +00001148 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001149 return pWInfo;
1150}
1151
1152/*
drhc27a1ce2002-06-14 20:58:45 +00001153** Generate the end of the WHERE loop. See comments on
1154** sqliteWhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001155*/
1156void sqliteWhereEnd(WhereInfo *pWInfo){
1157 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001158 int i;
drh6b563442001-11-07 16:48:26 +00001159 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001160 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001161
drhad3cab52002-05-24 02:04:32 +00001162 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001163 pLevel = &pWInfo->a[i];
1164 sqliteVdbeResolveLabel(v, pLevel->cont);
1165 if( pLevel->op!=OP_Noop ){
1166 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001167 }
drh6b563442001-11-07 16:48:26 +00001168 sqliteVdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001169 if( pLevel->inOp!=OP_Noop ){
1170 sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
1171 }
drhad2d8302002-05-24 20:31:36 +00001172 if( pLevel->iLeftJoin ){
1173 int addr;
1174 addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
drh7f09b3e2002-08-13 13:15:49 +00001175 sqliteVdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
drh6a3ea0e2003-05-02 14:32:12 +00001176 sqliteVdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001177 if( pLevel->iCur>=0 ){
1178 sqliteVdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
1179 }
drhad2d8302002-05-24 20:31:36 +00001180 sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
1181 }
drh19a775c2000-06-05 18:54:46 +00001182 }
drh6b563442001-11-07 16:48:26 +00001183 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001184 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001185 Table *pTab = pTabList->a[i].pTab;
1186 assert( pTab!=0 );
1187 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001188 pLevel = &pWInfo->a[i];
drh6a3ea0e2003-05-02 14:32:12 +00001189 sqliteVdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001190 if( pLevel->pIdx!=0 ){
1191 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
1192 }
drh19a775c2000-06-05 18:54:46 +00001193 }
drh142e30d2002-08-28 03:00:58 +00001194#if 0 /* Never reuse a cursor */
drh832508b2002-03-02 17:04:07 +00001195 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1196 pWInfo->pParse->nTab = pWInfo->savedNTab;
1197 }
drh142e30d2002-08-28 03:00:58 +00001198#endif
drh75897232000-05-29 14:26:00 +00001199 sqliteFree(pWInfo);
1200 return;
1201}