blob: 41a37e287309c2082a61409fabdcefc038a872e4 [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**
danielk1977b4964b72004-05-18 01:23:38 +000015** $Id: where.c,v 1.96 2004/05/18 01:23:39 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include "sqliteInt.h"
18
19/*
20** The query generator uses an array of instances of this structure to
21** help it analyze the subexpressions of the WHERE clause. Each WHERE
22** clause subexpression is separated from the others by an AND operator.
23*/
24typedef struct ExprInfo ExprInfo;
25struct ExprInfo {
26 Expr *p; /* Pointer to the subexpression */
drhe3184742002-06-19 14:27:05 +000027 u8 indexable; /* True if this subexprssion is usable by an index */
28 short int idxLeft; /* p->pLeft is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000029 ** p->pLeft is not the column of any table */
drhe3184742002-06-19 14:27:05 +000030 short int idxRight; /* p->pRight is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000031 ** p->pRight is not the column of any table */
drhe3184742002-06-19 14:27:05 +000032 unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */
33 unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */
34 unsigned prereqAll; /* Bitmask of tables referenced by p */
drh75897232000-05-29 14:26:00 +000035};
36
37/*
drh6a3ea0e2003-05-02 14:32:12 +000038** An instance of the following structure keeps track of a mapping
39** between VDBE cursor numbers and bitmasks. The VDBE cursor numbers
40** are small integers contained in SrcList_item.iCursor and Expr.iTable
41** fields. For any given WHERE clause, we want to track which cursors
42** are being used, so we assign a single bit in a 32-bit word to track
43** that cursor. Then a 32-bit integer is able to show the set of all
44** cursors being used.
45*/
46typedef struct ExprMaskSet ExprMaskSet;
47struct ExprMaskSet {
48 int n; /* Number of assigned cursor values */
49 int ix[32]; /* Cursor assigned to each bit */
50};
51
52/*
drh75897232000-05-29 14:26:00 +000053** Determine the number of elements in an array.
54*/
55#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
56
57/*
58** This routine is used to divide the WHERE expression into subexpressions
59** separated by the AND operator.
60**
61** aSlot[] is an array of subexpressions structures.
62** There are nSlot spaces left in this array. This routine attempts to
63** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
64** The return value is the number of slots filled.
65*/
66static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
67 int cnt = 0;
68 if( pExpr==0 || nSlot<1 ) return 0;
69 if( nSlot==1 || pExpr->op!=TK_AND ){
70 aSlot[0].p = pExpr;
71 return 1;
72 }
73 if( pExpr->pLeft->op!=TK_AND ){
74 aSlot[0].p = pExpr->pLeft;
75 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
76 }else{
drhdcd997e2003-01-31 17:21:49 +000077 cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
78 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
drh75897232000-05-29 14:26:00 +000079 }
80 return cnt;
81}
82
83/*
drh6a3ea0e2003-05-02 14:32:12 +000084** Initialize an expression mask set
85*/
86#define initMaskSet(P) memset(P, 0, sizeof(*P))
87
88/*
89** Return the bitmask for the given cursor. Assign a new bitmask
90** if this is the first time the cursor has been seen.
91*/
92static int getMask(ExprMaskSet *pMaskSet, int iCursor){
93 int i;
94 for(i=0; i<pMaskSet->n; i++){
95 if( pMaskSet->ix[i]==iCursor ) return 1<<i;
96 }
97 if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
98 pMaskSet->n++;
99 pMaskSet->ix[i] = iCursor;
100 return 1<<i;
101 }
102 return 0;
103}
104
105/*
106** Destroy an expression mask set
107*/
108#define freeMaskSet(P) /* NO-OP */
109
110/*
drh75897232000-05-29 14:26:00 +0000111** This routine walks (recursively) an expression tree and generates
112** a bitmask indicating which tables are used in that expression
drh6a3ea0e2003-05-02 14:32:12 +0000113** tree.
drh75897232000-05-29 14:26:00 +0000114**
115** In order for this routine to work, the calling function must have
danielk19774adee202004-05-08 08:23:19 +0000116** previously invoked sqlite3ExprResolveIds() on the expression. See
drh75897232000-05-29 14:26:00 +0000117** the header comment on that routine for additional information.
danielk19774adee202004-05-08 08:23:19 +0000118** The sqlite3ExprResolveIds() routines looks for column names and
drh6a3ea0e2003-05-02 14:32:12 +0000119** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
120** the VDBE cursor number of the table.
drh75897232000-05-29 14:26:00 +0000121*/
drh6a3ea0e2003-05-02 14:32:12 +0000122static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
drh75897232000-05-29 14:26:00 +0000123 unsigned int mask = 0;
124 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +0000125 if( p->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000126 return getMask(pMaskSet, p->iTable);
drh75897232000-05-29 14:26:00 +0000127 }
128 if( p->pRight ){
drh6a3ea0e2003-05-02 14:32:12 +0000129 mask = exprTableUsage(pMaskSet, p->pRight);
drh75897232000-05-29 14:26:00 +0000130 }
131 if( p->pLeft ){
drh6a3ea0e2003-05-02 14:32:12 +0000132 mask |= exprTableUsage(pMaskSet, p->pLeft);
drh75897232000-05-29 14:26:00 +0000133 }
drhdd579122002-04-02 01:58:57 +0000134 if( p->pList ){
135 int i;
136 for(i=0; i<p->pList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000137 mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
drhdd579122002-04-02 01:58:57 +0000138 }
139 }
drh75897232000-05-29 14:26:00 +0000140 return mask;
141}
142
143/*
drh487ab3c2001-11-08 00:45:21 +0000144** Return TRUE if the given operator is one of the operators that is
145** allowed for an indexable WHERE clause. The allowed operators are
drhc27a1ce2002-06-14 20:58:45 +0000146** "=", "<", ">", "<=", ">=", and "IN".
drh487ab3c2001-11-08 00:45:21 +0000147*/
148static int allowedOp(int op){
149 switch( op ){
150 case TK_LT:
151 case TK_LE:
152 case TK_GT:
153 case TK_GE:
154 case TK_EQ:
drhd99f7062002-06-08 23:25:08 +0000155 case TK_IN:
drh487ab3c2001-11-08 00:45:21 +0000156 return 1;
157 default:
158 return 0;
159 }
160}
161
162/*
drh75897232000-05-29 14:26:00 +0000163** The input to this routine is an ExprInfo structure with only the
164** "p" field filled in. The job of this routine is to analyze the
165** subexpression and populate all the other fields of the ExprInfo
166** structure.
167*/
drh6a3ea0e2003-05-02 14:32:12 +0000168static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000169 Expr *pExpr = pInfo->p;
drh6a3ea0e2003-05-02 14:32:12 +0000170 pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
171 pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
172 pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
drh75897232000-05-29 14:26:00 +0000173 pInfo->indexable = 0;
174 pInfo->idxLeft = -1;
175 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000176 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drhd99f7062002-06-08 23:25:08 +0000177 if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000178 pInfo->idxRight = pExpr->pRight->iTable;
drh75897232000-05-29 14:26:00 +0000179 pInfo->indexable = 1;
180 }
drh967e8b72000-06-21 13:59:10 +0000181 if( pExpr->pLeft->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000182 pInfo->idxLeft = pExpr->pLeft->iTable;
drh75897232000-05-29 14:26:00 +0000183 pInfo->indexable = 1;
184 }
185 }
186}
187
188/*
drhe3184742002-06-19 14:27:05 +0000189** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
190** left-most table in the FROM clause of that same SELECT statement and
191** the table has a cursor number of "base".
192**
193** This routine attempts to find an index for pTab that generates the
194** correct record sequence for the given ORDER BY clause. The return value
195** is a pointer to an index that does the job. NULL is returned if the
196** table has no index that will generate the correct sort order.
197**
198** If there are two or more indices that generate the correct sort order
199** and pPreferredIdx is one of those indices, then return pPreferredIdx.
drhdd4852c2002-12-04 21:50:16 +0000200**
201** nEqCol is the number of columns of pPreferredIdx that are used as
202** equality constraints. Any index returned must have exactly this same
203** set of columns. The ORDER BY clause only matches index columns beyond the
204** the first nEqCol columns.
205**
206** All terms of the ORDER BY clause must be either ASC or DESC. The
207** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
208** set to 0 if the ORDER BY clause is all ASC.
drhe3184742002-06-19 14:27:05 +0000209*/
210static Index *findSortingIndex(
211 Table *pTab, /* The table to be sorted */
212 int base, /* Cursor number for pTab */
213 ExprList *pOrderBy, /* The ORDER BY clause */
drhc045ec52002-12-04 20:01:06 +0000214 Index *pPreferredIdx, /* Use this index, if possible and not NULL */
drhdd4852c2002-12-04 21:50:16 +0000215 int nEqCol, /* Number of index columns used with == constraints */
drhc045ec52002-12-04 20:01:06 +0000216 int *pbRev /* Set to 1 if ORDER BY is DESC */
drhe3184742002-06-19 14:27:05 +0000217){
drhdd4852c2002-12-04 21:50:16 +0000218 int i, j;
drhe3184742002-06-19 14:27:05 +0000219 Index *pMatch;
220 Index *pIdx;
drhc045ec52002-12-04 20:01:06 +0000221 int sortOrder;
drhe3184742002-06-19 14:27:05 +0000222
223 assert( pOrderBy!=0 );
224 assert( pOrderBy->nExpr>0 );
drhc045ec52002-12-04 20:01:06 +0000225 sortOrder = pOrderBy->a[0].sortOrder & SQLITE_SO_DIRMASK;
drhe3184742002-06-19 14:27:05 +0000226 for(i=0; i<pOrderBy->nExpr; i++){
227 Expr *p;
drhc045ec52002-12-04 20:01:06 +0000228 if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){
229 /* Indices can only be used if all ORDER BY terms are either
230 ** DESC or ASC. Indices cannot be used on a mixture. */
drhe3184742002-06-19 14:27:05 +0000231 return 0;
232 }
drhc330af12002-08-14 03:03:57 +0000233 if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){
234 /* Do not sort by index if there is a COLLATE clause */
235 return 0;
236 }
drhe3184742002-06-19 14:27:05 +0000237 p = pOrderBy->a[i].pExpr;
238 if( p->op!=TK_COLUMN || p->iTable!=base ){
239 /* Can not use an index sort on anything that is not a column in the
240 ** left-most table of the FROM clause */
241 return 0;
242 }
243 }
drhc045ec52002-12-04 20:01:06 +0000244
drhe3184742002-06-19 14:27:05 +0000245 /* If we get this far, it means the ORDER BY clause consists only of
246 ** ascending columns in the left-most table of the FROM clause. Now
247 ** check for a matching index.
248 */
249 pMatch = 0;
250 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhdd4852c2002-12-04 21:50:16 +0000251 int nExpr = pOrderBy->nExpr;
252 if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
253 for(i=j=0; i<nEqCol; i++){
254 if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
255 if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; }
drhe3184742002-06-19 14:27:05 +0000256 }
drhdd4852c2002-12-04 21:50:16 +0000257 if( i<nEqCol ) continue;
258 for(i=0; i+j<nExpr; i++){
259 if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break;
260 }
261 if( i+j>=nExpr ){
drhe3184742002-06-19 14:27:05 +0000262 pMatch = pIdx;
263 if( pIdx==pPreferredIdx ) break;
264 }
265 }
drhc045ec52002-12-04 20:01:06 +0000266 if( pMatch && pbRev ){
267 *pbRev = sortOrder==SQLITE_SO_DESC;
268 }
drhe3184742002-06-19 14:27:05 +0000269 return pMatch;
270}
271
272/*
273** Generate the beginning of the loop used for WHERE clause processing.
drh75897232000-05-29 14:26:00 +0000274** The return value is a pointer to an (opaque) structure that contains
275** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000276** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000277** in order to complete the WHERE clause processing.
278**
279** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000280**
281** The basic idea is to do a nested loop, one loop for each table in
282** the FROM clause of a select. (INSERT and UPDATE statements are the
283** same as a SELECT with only a single table in the FROM clause.) For
284** example, if the SQL is this:
285**
286** SELECT * FROM t1, t2, t3 WHERE ...;
287**
288** Then the code generated is conceptually like the following:
289**
290** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000291** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000292** foreach row3 in t3 do /
293** ...
294** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000295** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000296** end /
297**
298** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000299** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
300** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000301** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000302**
303** If the WHERE clause is empty, the foreach loops must each scan their
304** entire tables. Thus a three-way join is an O(N^3) operation. But if
305** the tables have indices and there are terms in the WHERE clause that
306** refer to those indices, a complete table scan can be avoided and the
307** code will run much faster. Most of the work of this routine is checking
308** to see if there are indices that can be used to speed up the loop.
309**
310** Terms of the WHERE clause are also used to limit which rows actually
311** make it to the "..." in the middle of the loop. After each "foreach",
312** terms of the WHERE clause that use only terms in that loop and outer
313** loops are evaluated and if false a jump is made around all subsequent
314** inner loops (or around the "..." if the test occurs within the inner-
315** most loop)
316**
317** OUTER JOINS
318**
319** An outer join of tables t1 and t2 is conceptally coded as follows:
320**
321** foreach row1 in t1 do
322** flag = 0
323** foreach row2 in t2 do
324** start:
325** ...
326** flag = 1
327** end
drhe3184742002-06-19 14:27:05 +0000328** if flag==0 then
329** move the row2 cursor to a null row
330** goto start
331** fi
drhc27a1ce2002-06-14 20:58:45 +0000332** end
333**
drhe3184742002-06-19 14:27:05 +0000334** ORDER BY CLAUSE PROCESSING
335**
336** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
337** if there is one. If there is no ORDER BY clause or if this routine
338** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
339**
340** If an index can be used so that the natural output order of the table
341** scan is correct for the ORDER BY clause, then that index is used and
342** *ppOrderBy is set to NULL. This is an optimization that prevents an
343** unnecessary sort of the result set if an index appropriate for the
344** ORDER BY clause already exists.
345**
346** If the where clause loops cannot be arranged to provide the correct
347** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000348*/
danielk19774adee202004-05-08 08:23:19 +0000349WhereInfo *sqlite3WhereBegin(
drh75897232000-05-29 14:26:00 +0000350 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000351 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000352 Expr *pWhere, /* The WHERE clause */
drhe3184742002-06-19 14:27:05 +0000353 int pushKey, /* If TRUE, leave the table key on the stack */
354 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000355){
356 int i; /* Loop counter */
357 WhereInfo *pWInfo; /* Will become the return value of this function */
358 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000359 int brk, cont = 0; /* Addresses used during code generation */
drh75897232000-05-29 14:26:00 +0000360 int nExpr; /* Number of subexpressions in the WHERE clause */
361 int loopMask; /* One bit set for each outer loop */
362 int haveKey; /* True if KEY is on the stack */
drh6a3ea0e2003-05-02 14:32:12 +0000363 ExprMaskSet maskSet; /* The expression mask set */
drh8aff1012001-12-22 14:49:24 +0000364 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
365 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
366 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh83dcb1a2002-06-28 01:02:38 +0000367 ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
drh75897232000-05-29 14:26:00 +0000368
drhc27a1ce2002-06-14 20:58:45 +0000369 /* pushKey is only allowed if there is a single table (as in an INSERT or
370 ** UPDATE statement)
371 */
372 assert( pushKey==0 || pTabList->nSrc==1 );
drh83dcb1a2002-06-28 01:02:38 +0000373
374 /* Split the WHERE clause into separate subexpressions where each
375 ** subexpression is separated by an AND operator. If the aExpr[]
376 ** array fills up, the last entry might point to an expression which
377 ** contains additional unfactored AND operators.
378 */
drh6a3ea0e2003-05-02 14:32:12 +0000379 initMaskSet(&maskSet);
drh83dcb1a2002-06-28 01:02:38 +0000380 memset(aExpr, 0, sizeof(aExpr));
381 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
382 if( nExpr==ARRAYSIZE(aExpr) ){
danielk19774adee202004-05-08 08:23:19 +0000383 sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more "
drhf7a9e1a2004-02-22 18:40:56 +0000384 "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000385 return 0;
386 }
drhc27a1ce2002-06-14 20:58:45 +0000387
drh75897232000-05-29 14:26:00 +0000388 /* Allocate and initialize the WhereInfo structure that will become the
389 ** return value.
390 */
drhad3cab52002-05-24 02:04:32 +0000391 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000392 if( sqlite3_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +0000393 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000394 return 0;
395 }
396 pWInfo->pParse = pParse;
397 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000398 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000399 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000400
401 /* Special case: a WHERE clause that is constant. Evaluate the
402 ** expression and either jump over all of the code or fall thru.
403 */
danielk19774adee202004-05-08 08:23:19 +0000404 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
405 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000406 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000407 }
drh75897232000-05-29 14:26:00 +0000408
drh75897232000-05-29 14:26:00 +0000409 /* Analyze all of the subexpressions.
410 */
411 for(i=0; i<nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000412 exprAnalyze(&maskSet, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000413
414 /* If we are executing a trigger body, remove all references to
415 ** new.* and old.* tables from the prerequisite masks.
416 */
417 if( pParse->trigStack ){
418 int x;
419 if( (x = pParse->trigStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000420 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000421 aExpr[i].prereqRight &= mask;
422 aExpr[i].prereqLeft &= mask;
423 aExpr[i].prereqAll &= mask;
424 }
425 if( (x = pParse->trigStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000426 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000427 aExpr[i].prereqRight &= mask;
428 aExpr[i].prereqLeft &= mask;
429 aExpr[i].prereqAll &= mask;
430 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000431 }
drh75897232000-05-29 14:26:00 +0000432 }
433
drh75897232000-05-29 14:26:00 +0000434 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000435 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000436 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000437 ** loop.
438 **
439 ** If terms exist that use the ROWID of any table, then set the
440 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
441 ** to the index of the term containing the ROWID. We always prefer
442 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000443 ** index which requires reading an index first to get the rowid then
444 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000445 **
446 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000447 ** first 32 tables are candidates for indices. This is (again) due
448 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000449 */
450 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000451 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000452 int j;
drh6a3ea0e2003-05-02 14:32:12 +0000453 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
454 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
455 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000456 Index *pIdx;
457 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000458 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000459
drhc4a3c772001-04-04 11:48:57 +0000460 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000461 ** ROWID field of this table. For terms of the form ROWID==expr
462 ** set iDirectEq[i] to the index of the term. For terms of the
463 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
464 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000465 **
466 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000467 */
drhc8f8b632002-09-30 12:36:26 +0000468 pWInfo->a[i].iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000469 iDirectEq[i] = -1;
470 iDirectLt[i] = -1;
471 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000472 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000473 if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000474 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000475 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000476 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000477 case TK_EQ: iDirectEq[i] = j; break;
478 case TK_LE:
479 case TK_LT: iDirectLt[i] = j; break;
480 case TK_GE:
481 case TK_GT: iDirectGt[i] = j; break;
482 }
drhc4a3c772001-04-04 11:48:57 +0000483 }
drh6a3ea0e2003-05-02 14:32:12 +0000484 if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000485 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000486 switch( aExpr[j].p->op ){
487 case TK_EQ: iDirectEq[i] = j; break;
488 case TK_LE:
489 case TK_LT: iDirectGt[i] = j; break;
490 case TK_GE:
491 case TK_GT: iDirectLt[i] = j; break;
492 }
drhc4a3c772001-04-04 11:48:57 +0000493 }
494 }
drh8aff1012001-12-22 14:49:24 +0000495 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000496 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000497 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000498 continue;
499 }
500
drh75897232000-05-29 14:26:00 +0000501 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000502 ** the "best" index. pBestIdx is left set to NULL if no indices
503 ** are usable.
drh75897232000-05-29 14:26:00 +0000504 **
drh487ab3c2001-11-08 00:45:21 +0000505 ** The best index is determined as follows. For each of the
506 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000507 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000508 ** constrained by an inequality. Add 1 if for an "x<..." constraint
509 ** and add 2 for an "x>..." constraint. Chose the index that
510 ** gives the best score.
511 **
512 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000513 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000514 ** then all constraints are equalities. If score&1 is not 0 then
515 ** there is an inequality used as a termination key. (ex: "x<...")
516 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000517 ** start key. (ex: "x>..."). A score or 4 is the special case
518 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000519 **
drhc27a1ce2002-06-14 20:58:45 +0000520 ** The IN operator (as in "<expr> IN (...)") is treated the same as
521 ** an equality comparison except that it can only be used on the
522 ** left-most column of an index and other terms of the WHERE clause
523 ** cannot be used in conjunction with the IN operator to help satisfy
524 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000525 */
526 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000527 int eqMask = 0; /* Index columns covered by an x=... term */
528 int ltMask = 0; /* Index columns covered by an x<... term */
529 int gtMask = 0; /* Index columns covered by an x>... term */
530 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000531 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000532
drh487ab3c2001-11-08 00:45:21 +0000533 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000534 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000535 if( aExpr[j].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000536 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000537 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000538 int k;
danielk1977e014a832004-05-17 10:48:57 +0000539 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000540 for(k=0; k<pIdx->nColumn; k++){
danielk1977e014a832004-05-17 10:48:57 +0000541 if( pIdx->aiColumn[k]==iColumn
542 && sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ){
drh487ab3c2001-11-08 00:45:21 +0000543 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000544 case TK_IN: {
545 if( k==0 ) inMask |= 1;
546 break;
547 }
drh487ab3c2001-11-08 00:45:21 +0000548 case TK_EQ: {
549 eqMask |= 1<<k;
550 break;
551 }
552 case TK_LE:
553 case TK_LT: {
554 ltMask |= 1<<k;
555 break;
556 }
557 case TK_GE:
558 case TK_GT: {
559 gtMask |= 1<<k;
560 break;
561 }
562 default: {
563 /* CANT_HAPPEN */
564 assert( 0 );
565 break;
566 }
567 }
drh75897232000-05-29 14:26:00 +0000568 break;
569 }
570 }
571 }
drh6a3ea0e2003-05-02 14:32:12 +0000572 if( aExpr[j].idxRight==iCur
drh75897232000-05-29 14:26:00 +0000573 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000574 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000575 int k;
danielk1977e014a832004-05-17 10:48:57 +0000576 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000577 for(k=0; k<pIdx->nColumn; k++){
danielk1977e014a832004-05-17 10:48:57 +0000578 if( pIdx->aiColumn[k]==iColumn
579 && sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ){
drh487ab3c2001-11-08 00:45:21 +0000580 switch( aExpr[j].p->op ){
581 case TK_EQ: {
582 eqMask |= 1<<k;
583 break;
584 }
585 case TK_LE:
586 case TK_LT: {
587 gtMask |= 1<<k;
588 break;
589 }
590 case TK_GE:
591 case TK_GT: {
592 ltMask |= 1<<k;
593 break;
594 }
595 default: {
596 /* CANT_HAPPEN */
597 assert( 0 );
598 break;
599 }
600 }
drh75897232000-05-29 14:26:00 +0000601 break;
602 }
603 }
604 }
605 }
drhc045ec52002-12-04 20:01:06 +0000606
607 /* The following loop ends with nEq set to the number of columns
608 ** on the left of the index with == constraints.
609 */
drh487ab3c2001-11-08 00:45:21 +0000610 for(nEq=0; nEq<pIdx->nColumn; nEq++){
611 m = (1<<(nEq+1))-1;
612 if( (m & eqMask)!=m ) break;
613 }
drhc045ec52002-12-04 20:01:06 +0000614 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000615 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000616 if( m & ltMask ) score++; /* Increase score for a < constraint */
617 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
618 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000619 if( score>bestScore ){
620 pBestIdx = pIdx;
621 bestScore = score;
drh75897232000-05-29 14:26:00 +0000622 }
623 }
drh6b563442001-11-07 16:48:26 +0000624 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000625 pWInfo->a[i].score = bestScore;
drhc045ec52002-12-04 20:01:06 +0000626 pWInfo->a[i].bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000627 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000628 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000629 pWInfo->a[i].iCur = pParse->nTab++;
630 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000631 }
drh75897232000-05-29 14:26:00 +0000632 }
633
drhe3184742002-06-19 14:27:05 +0000634 /* Check to see if the ORDER BY clause is or can be satisfied by the
635 ** use of an index on the first table.
636 */
637 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
638 Index *pSortIdx;
639 Index *pIdx;
640 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000641 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000642
643 pTab = pTabList->a[0].pTab;
644 pIdx = pWInfo->a[0].pIdx;
645 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000646 /* If there is already an IN index on the left-most table,
647 ** it will not give the correct sort order.
648 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000649 */
650 pSortIdx = 0;
651 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
652 /* If the left-most column is accessed using its ROWID, then do
653 ** not try to sort by index.
654 */
655 pSortIdx = 0;
656 }else{
drhdd4852c2002-12-04 21:50:16 +0000657 int nEqCol = (pWInfo->a[0].score+4)/8;
drh6a3ea0e2003-05-02 14:32:12 +0000658 pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
659 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000660 }
661 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
662 if( pIdx==0 ){
663 pWInfo->a[0].pIdx = pSortIdx;
664 pWInfo->a[0].iCur = pParse->nTab++;
665 pWInfo->peakNTab = pParse->nTab;
666 }
drhc045ec52002-12-04 20:01:06 +0000667 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000668 *ppOrderBy = 0;
669 }
670 }
671
drh6b563442001-11-07 16:48:26 +0000672 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000673 */
drhad3cab52002-05-24 02:04:32 +0000674 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000675 Table *pTab;
drh701a0ae2004-02-22 20:05:00 +0000676 Index *pIx;
drhf57b3392001-10-08 13:22:32 +0000677
678 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000679 if( pTab->isTransient || pTab->pSelect ) continue;
danielk19774adee202004-05-08 08:23:19 +0000680 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
681 sqlite3VdbeOp3(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum,
drh701a0ae2004-02-22 20:05:00 +0000682 pTab->zName, P3_STATIC);
danielk1977b4964b72004-05-18 01:23:38 +0000683 sqlite3VdbeAddOp(v, OP_SetNumColumns, pTabList->a[i].iCursor, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +0000684 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh701a0ae2004-02-22 20:05:00 +0000685 if( (pIx = pWInfo->a[i].pIdx)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000686 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
687 sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum, pIx->zName,0);
drh75897232000-05-29 14:26:00 +0000688 }
689 }
690
691 /* Generate the code to do the search
692 */
drh75897232000-05-29 14:26:00 +0000693 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000694 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000695 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000696 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000697 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000698 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000699
drhad2d8302002-05-24 20:31:36 +0000700 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000701 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000702 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000703 */
704 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
705 if( !pParse->nMem ) pParse->nMem++;
706 pLevel->iLeftJoin = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000707 sqlite3VdbeAddOp(v, OP_String, 0, 0);
708 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad2d8302002-05-24 20:31:36 +0000709 }
710
drh8aff1012001-12-22 14:49:24 +0000711 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000712 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000713 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
714 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000715 ** equality comparison against the ROWID field. Or
716 ** we reference multiple rows using a "rowid IN (...)"
717 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000718 */
drh8aff1012001-12-22 14:49:24 +0000719 k = iDirectEq[i];
720 assert( k<nExpr );
721 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000722 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
danielk19774adee202004-05-08 08:23:19 +0000723 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000724 if( aExpr[k].idxLeft==iCur ){
drhd99f7062002-06-08 23:25:08 +0000725 Expr *pX = aExpr[k].p;
726 if( pX->op!=TK_IN ){
danielk19774adee202004-05-08 08:23:19 +0000727 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drhd99f7062002-06-08 23:25:08 +0000728 }else{
danielk19774adee202004-05-08 08:23:19 +0000729 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
730 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000731 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000732 pLevel->inOp = OP_Next;
733 pLevel->inP1 = pX->iTable;
734 }
drh8aff1012001-12-22 14:49:24 +0000735 }else{
danielk19774adee202004-05-08 08:23:19 +0000736 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000737 }
drh8aff1012001-12-22 14:49:24 +0000738 aExpr[k].p = 0;
danielk19774adee202004-05-08 08:23:19 +0000739 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
740 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000741 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +0000742 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000743 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000744 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000745 /* Case 2: There is an index and all terms of the WHERE clause that
746 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000747 */
drh6b563442001-11-07 16:48:26 +0000748 int start;
drh487ab3c2001-11-08 00:45:21 +0000749 int testOp;
drhc045ec52002-12-04 20:01:06 +0000750 int nColumn = (pLevel->score+4)/8;
danielk19774adee202004-05-08 08:23:19 +0000751 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000752 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000753 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000754 Expr *pX = aExpr[k].p;
755 if( pX==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000756 if( aExpr[k].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000757 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000758 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000759 ){
danielk1977e014a832004-05-17 10:48:57 +0000760 char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
761 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
762 if( pX->op==TK_EQ ){
763 sqlite3ExprCode(pParse, pX->pRight);
764 aExpr[k].p = 0;
765 break;
766 }
767 if( pX->op==TK_IN && nColumn==1 ){
danielk19774adee202004-05-08 08:23:19 +0000768 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
769 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000770 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000771 pLevel->inOp = OP_Next;
772 pLevel->inP1 = pX->iTable;
danielk1977e014a832004-05-17 10:48:57 +0000773 aExpr[k].p = 0;
774 break;
drhd99f7062002-06-08 23:25:08 +0000775 }
drhd99f7062002-06-08 23:25:08 +0000776 }
drh75897232000-05-29 14:26:00 +0000777 }
drh6a3ea0e2003-05-02 14:32:12 +0000778 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000779 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000780 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000781 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000782 ){
danielk1977e014a832004-05-17 10:48:57 +0000783 char idxaff = pIdx->pTable->aCol[pX->pRight->iColumn].affinity;
784 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
785 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
786 aExpr[k].p = 0;
787 break;
788 }
drh75897232000-05-29 14:26:00 +0000789 }
790 }
791 }
drh6b563442001-11-07 16:48:26 +0000792 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000793 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
794 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
795 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
796 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
797 sqlite3VdbeAddOp(v, OP_MakeKey, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000798 sqlite3IndexAffinityStr(v, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +0000799 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drhc045ec52002-12-04 20:01:06 +0000800 if( nColumn==pIdx->nColumn || pLevel->bRev ){
drh487ab3c2001-11-08 00:45:21 +0000801 testOp = OP_IdxGT;
802 }else{
drh487ab3c2001-11-08 00:45:21 +0000803 testOp = OP_IdxGE;
804 }
drhc045ec52002-12-04 20:01:06 +0000805 if( pLevel->bRev ){
806 /* Scan in reverse order */
danielk19774adee202004-05-08 08:23:19 +0000807 sqlite3VdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +0000808 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000809 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
810 sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000811 pLevel->op = OP_Prev;
812 }else{
813 /* Scan in the forward order */
danielk19774adee202004-05-08 08:23:19 +0000814 sqlite3VdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
815 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
816 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +0000817 if( testOp==OP_IdxGE ){
818 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
819 }
drhc045ec52002-12-04 20:01:06 +0000820 pLevel->op = OP_Next;
821 }
danielk19774adee202004-05-08 08:23:19 +0000822 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
823 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
824 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000825 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000826 haveKey = 1;
827 }else{
danielk19774adee202004-05-08 08:23:19 +0000828 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh75897232000-05-29 14:26:00 +0000829 haveKey = 0;
830 }
drh6b563442001-11-07 16:48:26 +0000831 pLevel->p1 = pLevel->iCur;
832 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000833 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
834 /* Case 3: We have an inequality comparison against the ROWID field.
835 */
836 int testOp = OP_Noop;
837 int start;
838
danielk19774adee202004-05-08 08:23:19 +0000839 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
840 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000841 if( iDirectGt[i]>=0 ){
842 k = iDirectGt[i];
843 assert( k<nExpr );
844 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000845 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
846 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000847 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000848 }else{
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000850 }
danielk19774adee202004-05-08 08:23:19 +0000851 sqlite3VdbeAddOp(v, OP_ForceInt,
drh751f4122004-01-14 21:59:22 +0000852 aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
danielk19774adee202004-05-08 08:23:19 +0000853 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000854 aExpr[k].p = 0;
855 }else{
danielk19774adee202004-05-08 08:23:19 +0000856 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000857 }
858 if( iDirectLt[i]>=0 ){
859 k = iDirectLt[i];
860 assert( k<nExpr );
861 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000862 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
863 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000864 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000865 }else{
danielk19774adee202004-05-08 08:23:19 +0000866 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000867 }
danielk19774adee202004-05-08 08:23:19 +0000868 /* sqlite3VdbeAddOp(v, OP_MustBeInt, 0, sqlite3VdbeCurrentAddr(v)+1); */
drh8aff1012001-12-22 14:49:24 +0000869 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000870 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh8aff1012001-12-22 14:49:24 +0000871 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
872 testOp = OP_Ge;
873 }else{
874 testOp = OP_Gt;
875 }
876 aExpr[k].p = 0;
877 }
danielk19774adee202004-05-08 08:23:19 +0000878 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000879 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000880 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000881 pLevel->p2 = start;
882 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +0000883 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
884 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
885 sqlite3VdbeAddOp(v, testOp, 0, brk);
drh8aff1012001-12-22 14:49:24 +0000886 }
887 haveKey = 0;
888 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000889 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000890 ** scan of the entire database table.
891 */
892 int start;
893
danielk19774adee202004-05-08 08:23:19 +0000894 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
895 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
896 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
897 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000898 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000899 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000900 pLevel->p2 = start;
901 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000902 }else{
drhc27a1ce2002-06-14 20:58:45 +0000903 /* Case 5: The WHERE clause term that refers to the right-most
904 ** column of the index is an inequality. For example, if
905 ** the index is on (x,y,z) and the WHERE clause is of the
906 ** form "x=5 AND y<10" then this case is used. Only the
907 ** right-most column can be an inequality - the rest must
908 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000909 **
910 ** This case is also used when there are no WHERE clause
911 ** constraints but an index is selected anyway, in order
912 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000913 */
914 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000915 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000916 int start;
917 int leFlag, geFlag;
918 int testOp;
919
920 /* Evaluate the equality constraints
921 */
922 for(j=0; j<nEqColumn; j++){
923 for(k=0; k<nExpr; k++){
924 if( aExpr[k].p==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000925 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000926 && aExpr[k].p->op==TK_EQ
927 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
928 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
929 ){
danielk19774adee202004-05-08 08:23:19 +0000930 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh143f3c42004-01-07 20:37:52 +0000931 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000932 break;
933 }
drh6a3ea0e2003-05-02 14:32:12 +0000934 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000935 && aExpr[k].p->op==TK_EQ
936 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
937 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
938 ){
danielk19774adee202004-05-08 08:23:19 +0000939 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000940 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000941 break;
942 }
943 }
944 }
945
drhc27a1ce2002-06-14 20:58:45 +0000946 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000947 ** used twice: once to make the termination key and once to make the
948 ** start key.
949 */
950 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +0000951 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +0000952 }
953
drhc045ec52002-12-04 20:01:06 +0000954 /* Labels for the beginning and end of the loop
955 */
danielk19774adee202004-05-08 08:23:19 +0000956 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
957 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +0000958
drh487ab3c2001-11-08 00:45:21 +0000959 /* Generate the termination key. This is the key value that
960 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000961 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +0000962 **
963 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
964 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +0000965 */
966 if( (score & 1)!=0 ){
967 for(k=0; k<nExpr; k++){
968 Expr *pExpr = aExpr[k].p;
969 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000970 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000971 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
972 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
973 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
974 ){
danielk19774adee202004-05-08 08:23:19 +0000975 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +0000976 leFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +0000977 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000978 break;
979 }
drh6a3ea0e2003-05-02 14:32:12 +0000980 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000981 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
982 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
983 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
984 ){
danielk19774adee202004-05-08 08:23:19 +0000985 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +0000986 leFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +0000987 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000988 break;
989 }
990 }
991 testOp = OP_IdxGE;
992 }else{
993 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
994 leFlag = 1;
995 }
996 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +0000997 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +0000998 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000999 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1000 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1001 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1002 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001003 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001004 if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001005 sqlite3VdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001006 if( !geFlag ){
1007 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1008 }
drhc045ec52002-12-04 20:01:06 +00001009 }else{
danielk19774adee202004-05-08 08:23:19 +00001010 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001011 }
1012 }else if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001013 sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001014 }
1015
1016 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001017 ** bound on the search. There is no start key if there are no
1018 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001019 ** that case, generate a "Rewind" instruction in place of the
1020 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001021 **
1022 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1023 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001024 */
1025 if( (score & 2)!=0 ){
1026 for(k=0; k<nExpr; k++){
1027 Expr *pExpr = aExpr[k].p;
1028 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001029 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001030 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1031 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1032 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1033 ){
danielk19774adee202004-05-08 08:23:19 +00001034 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +00001035 geFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001036 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001037 break;
1038 }
drh6a3ea0e2003-05-02 14:32:12 +00001039 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001040 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1041 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1042 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1043 ){
danielk19774adee202004-05-08 08:23:19 +00001044 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +00001045 geFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001046 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001047 break;
1048 }
1049 }
drh7900ead2001-11-12 13:51:43 +00001050 }else{
1051 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001052 }
drh487ab3c2001-11-08 00:45:21 +00001053 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001054 int nCol = nEqColumn + ((score&2)!=0);
danielk19774adee202004-05-08 08:23:19 +00001055 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1056 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1057 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1058 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001059 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001060 if( pLevel->bRev ){
1061 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001062 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001063 testOp = OP_IdxLT;
1064 }else{
danielk19774adee202004-05-08 08:23:19 +00001065 sqlite3VdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001066 if( !geFlag ){
1067 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1068 }
drhc045ec52002-12-04 20:01:06 +00001069 }
1070 }else if( pLevel->bRev ){
1071 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001072 }else{
danielk19774adee202004-05-08 08:23:19 +00001073 sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001074 }
1075
1076 /* Generate the the top of the loop. If there is a termination
1077 ** key we have to test for that key and abort at the top of the
1078 ** loop.
1079 */
danielk19774adee202004-05-08 08:23:19 +00001080 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001081 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001082 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1083 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001084 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1085 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1086 }
drh487ab3c2001-11-08 00:45:21 +00001087 }
danielk19774adee202004-05-08 08:23:19 +00001088 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1089 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1090 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001091 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001092 haveKey = 1;
1093 }else{
danielk19774adee202004-05-08 08:23:19 +00001094 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001095 haveKey = 0;
1096 }
1097
1098 /* Record the instruction used to terminate the loop.
1099 */
drhc045ec52002-12-04 20:01:06 +00001100 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001101 pLevel->p1 = pLevel->iCur;
1102 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001103 }
drh6a3ea0e2003-05-02 14:32:12 +00001104 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001105
1106 /* Insert code to test every subexpression that can be completely
1107 ** computed using the current set of tables.
1108 */
1109 for(j=0; j<nExpr; j++){
1110 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +00001111 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh1f162302002-10-27 19:35:33 +00001112 if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1113 continue;
1114 }
drh75897232000-05-29 14:26:00 +00001115 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001116 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +00001117 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh75897232000-05-29 14:26:00 +00001118 }
danielk19774adee202004-05-08 08:23:19 +00001119 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +00001120 aExpr[j].p = 0;
1121 }
1122 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001123
1124 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1125 ** at least one row of the right table has matched the left table.
1126 */
1127 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001128 pLevel->top = sqlite3VdbeCurrentAddr(v);
1129 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1130 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drh1cc093c2002-06-24 22:01:57 +00001131 for(j=0; j<nExpr; j++){
1132 if( aExpr[j].p==0 ) continue;
1133 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1134 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001135 /* Cannot happen. "haveKey" can only be true if pushKey is true
1136 ** an pushKey can only be true for DELETE and UPDATE and there are
1137 ** no outer joins with DELETE and UPDATE.
1138 */
drh1cc093c2002-06-24 22:01:57 +00001139 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +00001140 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001141 }
danielk19774adee202004-05-08 08:23:19 +00001142 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh1cc093c2002-06-24 22:01:57 +00001143 aExpr[j].p = 0;
1144 }
drhad2d8302002-05-24 20:31:36 +00001145 }
drh75897232000-05-29 14:26:00 +00001146 }
1147 pWInfo->iContinue = cont;
1148 if( pushKey && !haveKey ){
danielk19774adee202004-05-08 08:23:19 +00001149 sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001150 }
drh6a3ea0e2003-05-02 14:32:12 +00001151 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001152 return pWInfo;
1153}
1154
1155/*
drhc27a1ce2002-06-14 20:58:45 +00001156** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001157** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001158*/
danielk19774adee202004-05-08 08:23:19 +00001159void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001160 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001161 int i;
drh6b563442001-11-07 16:48:26 +00001162 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001163 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001164
drhad3cab52002-05-24 02:04:32 +00001165 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001166 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001167 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001168 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001169 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001170 }
danielk19774adee202004-05-08 08:23:19 +00001171 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001172 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001173 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001174 }
drhad2d8302002-05-24 20:31:36 +00001175 if( pLevel->iLeftJoin ){
1176 int addr;
danielk19774adee202004-05-08 08:23:19 +00001177 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1178 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1179 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001180 if( pLevel->iCur>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001181 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001182 }
danielk19774adee202004-05-08 08:23:19 +00001183 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001184 }
drh19a775c2000-06-05 18:54:46 +00001185 }
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001187 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001188 Table *pTab = pTabList->a[i].pTab;
1189 assert( pTab!=0 );
1190 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001191 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001193 if( pLevel->pIdx!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001194 sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
drh6b563442001-11-07 16:48:26 +00001195 }
drh19a775c2000-06-05 18:54:46 +00001196 }
drh142e30d2002-08-28 03:00:58 +00001197#if 0 /* Never reuse a cursor */
drh832508b2002-03-02 17:04:07 +00001198 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1199 pWInfo->pParse->nTab = pWInfo->savedNTab;
1200 }
drh142e30d2002-08-28 03:00:58 +00001201#endif
drh75897232000-05-29 14:26:00 +00001202 sqliteFree(pWInfo);
1203 return;
1204}
danielk19774adee202004-05-08 08:23:19 +00001205
1206
1207