blob: 9d28c876eac2788247371d8ba3df0d638d47c980 [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**
danielk19773d1bfea2004-05-14 11:00:53 +000015** $Id: where.c,v 1.92 2004/05/14 11:00:53 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;
drh967e8b72000-06-21 13:59:10 +0000539 for(k=0; k<pIdx->nColumn; k++){
540 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000541 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000542 case TK_IN: {
543 if( k==0 ) inMask |= 1;
544 break;
545 }
drh487ab3c2001-11-08 00:45:21 +0000546 case TK_EQ: {
547 eqMask |= 1<<k;
548 break;
549 }
550 case TK_LE:
551 case TK_LT: {
552 ltMask |= 1<<k;
553 break;
554 }
555 case TK_GE:
556 case TK_GT: {
557 gtMask |= 1<<k;
558 break;
559 }
560 default: {
561 /* CANT_HAPPEN */
562 assert( 0 );
563 break;
564 }
565 }
drh75897232000-05-29 14:26:00 +0000566 break;
567 }
568 }
569 }
drh6a3ea0e2003-05-02 14:32:12 +0000570 if( aExpr[j].idxRight==iCur
drh75897232000-05-29 14:26:00 +0000571 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000572 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000573 int k;
drh967e8b72000-06-21 13:59:10 +0000574 for(k=0; k<pIdx->nColumn; k++){
575 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000576 switch( aExpr[j].p->op ){
577 case TK_EQ: {
578 eqMask |= 1<<k;
579 break;
580 }
581 case TK_LE:
582 case TK_LT: {
583 gtMask |= 1<<k;
584 break;
585 }
586 case TK_GE:
587 case TK_GT: {
588 ltMask |= 1<<k;
589 break;
590 }
591 default: {
592 /* CANT_HAPPEN */
593 assert( 0 );
594 break;
595 }
596 }
drh75897232000-05-29 14:26:00 +0000597 break;
598 }
599 }
600 }
601 }
drhc045ec52002-12-04 20:01:06 +0000602
603 /* The following loop ends with nEq set to the number of columns
604 ** on the left of the index with == constraints.
605 */
drh487ab3c2001-11-08 00:45:21 +0000606 for(nEq=0; nEq<pIdx->nColumn; nEq++){
607 m = (1<<(nEq+1))-1;
608 if( (m & eqMask)!=m ) break;
609 }
drhc045ec52002-12-04 20:01:06 +0000610 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000611 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000612 if( m & ltMask ) score++; /* Increase score for a < constraint */
613 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
614 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000615 if( score>bestScore ){
616 pBestIdx = pIdx;
617 bestScore = score;
drh75897232000-05-29 14:26:00 +0000618 }
619 }
drh6b563442001-11-07 16:48:26 +0000620 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000621 pWInfo->a[i].score = bestScore;
drhc045ec52002-12-04 20:01:06 +0000622 pWInfo->a[i].bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000623 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000624 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000625 pWInfo->a[i].iCur = pParse->nTab++;
626 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000627 }
drh75897232000-05-29 14:26:00 +0000628 }
629
drhe3184742002-06-19 14:27:05 +0000630 /* Check to see if the ORDER BY clause is or can be satisfied by the
631 ** use of an index on the first table.
632 */
633 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
634 Index *pSortIdx;
635 Index *pIdx;
636 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000637 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000638
639 pTab = pTabList->a[0].pTab;
640 pIdx = pWInfo->a[0].pIdx;
641 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000642 /* If there is already an IN index on the left-most table,
643 ** it will not give the correct sort order.
644 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000645 */
646 pSortIdx = 0;
647 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
648 /* If the left-most column is accessed using its ROWID, then do
649 ** not try to sort by index.
650 */
651 pSortIdx = 0;
652 }else{
drhdd4852c2002-12-04 21:50:16 +0000653 int nEqCol = (pWInfo->a[0].score+4)/8;
drh6a3ea0e2003-05-02 14:32:12 +0000654 pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
655 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000656 }
657 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
658 if( pIdx==0 ){
659 pWInfo->a[0].pIdx = pSortIdx;
660 pWInfo->a[0].iCur = pParse->nTab++;
661 pWInfo->peakNTab = pParse->nTab;
662 }
drhc045ec52002-12-04 20:01:06 +0000663 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000664 *ppOrderBy = 0;
665 }
666 }
667
drh6b563442001-11-07 16:48:26 +0000668 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000669 */
drhad3cab52002-05-24 02:04:32 +0000670 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000671 Table *pTab;
drh701a0ae2004-02-22 20:05:00 +0000672 Index *pIx;
drhf57b3392001-10-08 13:22:32 +0000673
674 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000675 if( pTab->isTransient || pTab->pSelect ) continue;
danielk19774adee202004-05-08 08:23:19 +0000676 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
677 sqlite3VdbeOp3(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum,
drh701a0ae2004-02-22 20:05:00 +0000678 pTab->zName, P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000679 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh701a0ae2004-02-22 20:05:00 +0000680 if( (pIx = pWInfo->a[i].pIdx)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000681 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
682 sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum, pIx->zName,0);
drh75897232000-05-29 14:26:00 +0000683 }
684 }
685
686 /* Generate the code to do the search
687 */
drh75897232000-05-29 14:26:00 +0000688 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000689 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000690 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000691 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000692 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000693 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000694
drhad2d8302002-05-24 20:31:36 +0000695 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000696 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000697 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000698 */
699 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
700 if( !pParse->nMem ) pParse->nMem++;
701 pLevel->iLeftJoin = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000702 sqlite3VdbeAddOp(v, OP_String, 0, 0);
703 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad2d8302002-05-24 20:31:36 +0000704 }
705
drh8aff1012001-12-22 14:49:24 +0000706 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000707 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000708 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
709 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000710 ** equality comparison against the ROWID field. Or
711 ** we reference multiple rows using a "rowid IN (...)"
712 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000713 */
drh8aff1012001-12-22 14:49:24 +0000714 k = iDirectEq[i];
715 assert( k<nExpr );
716 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000717 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
danielk19774adee202004-05-08 08:23:19 +0000718 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000719 if( aExpr[k].idxLeft==iCur ){
drhd99f7062002-06-08 23:25:08 +0000720 Expr *pX = aExpr[k].p;
721 if( pX->op!=TK_IN ){
danielk19774adee202004-05-08 08:23:19 +0000722 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drhd99f7062002-06-08 23:25:08 +0000723 }else if( pX->pList ){
danielk19774adee202004-05-08 08:23:19 +0000724 sqlite3VdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
drhd99f7062002-06-08 23:25:08 +0000725 pLevel->inOp = OP_SetNext;
726 pLevel->inP1 = pX->iTable;
danielk19774adee202004-05-08 08:23:19 +0000727 pLevel->inP2 = sqlite3VdbeCurrentAddr(v);
drhd99f7062002-06-08 23:25:08 +0000728 }else{
729 assert( pX->pSelect );
danielk19774adee202004-05-08 08:23:19 +0000730 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
731 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
732 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_FullKey, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000733 pLevel->inOp = OP_Next;
734 pLevel->inP1 = pX->iTable;
735 }
drh8aff1012001-12-22 14:49:24 +0000736 }else{
danielk19774adee202004-05-08 08:23:19 +0000737 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000738 }
drh8aff1012001-12-22 14:49:24 +0000739 aExpr[k].p = 0;
danielk19774adee202004-05-08 08:23:19 +0000740 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
741 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000742 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +0000743 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000744 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000745 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000746 /* Case 2: There is an index and all terms of the WHERE clause that
747 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000748 */
drh6b563442001-11-07 16:48:26 +0000749 int start;
drh487ab3c2001-11-08 00:45:21 +0000750 int testOp;
drhc045ec52002-12-04 20:01:06 +0000751 int nColumn = (pLevel->score+4)/8;
danielk19774adee202004-05-08 08:23:19 +0000752 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000753 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000754 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000755 Expr *pX = aExpr[k].p;
756 if( pX==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000757 if( aExpr[k].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000758 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000759 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000760 ){
drhd99f7062002-06-08 23:25:08 +0000761 if( pX->op==TK_EQ ){
danielk19774adee202004-05-08 08:23:19 +0000762 sqlite3ExprCode(pParse, pX->pRight);
drh143f3c42004-01-07 20:37:52 +0000763 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000764 break;
765 }
766 if( pX->op==TK_IN && nColumn==1 ){
767 if( pX->pList ){
danielk19774adee202004-05-08 08:23:19 +0000768 sqlite3VdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
drhd99f7062002-06-08 23:25:08 +0000769 pLevel->inOp = OP_SetNext;
770 pLevel->inP1 = pX->iTable;
danielk19774adee202004-05-08 08:23:19 +0000771 pLevel->inP2 = sqlite3VdbeCurrentAddr(v);
drhd99f7062002-06-08 23:25:08 +0000772 }else{
773 assert( pX->pSelect );
danielk19774adee202004-05-08 08:23:19 +0000774 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
775 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
776 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_FullKey, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000777 pLevel->inOp = OP_Next;
778 pLevel->inP1 = pX->iTable;
779 }
drh143f3c42004-01-07 20:37:52 +0000780 aExpr[k].p = 0;
drhd99f7062002-06-08 23:25:08 +0000781 break;
782 }
drh75897232000-05-29 14:26:00 +0000783 }
drh6a3ea0e2003-05-02 14:32:12 +0000784 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000785 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000786 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000787 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000788 ){
danielk19774adee202004-05-08 08:23:19 +0000789 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000790 aExpr[k].p = 0;
drh75897232000-05-29 14:26:00 +0000791 break;
792 }
793 }
794 }
drh6b563442001-11-07 16:48:26 +0000795 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000796 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
797 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
798 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
799 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
800 sqlite3VdbeAddOp(v, OP_MakeKey, nColumn, 0);
801 sqlite3AddIdxKeyType(v, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +0000802 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drhc045ec52002-12-04 20:01:06 +0000803 if( nColumn==pIdx->nColumn || pLevel->bRev ){
drh487ab3c2001-11-08 00:45:21 +0000804 testOp = OP_IdxGT;
805 }else{
danielk19773d1bfea2004-05-14 11:00:53 +0000806/*
danielk19774adee202004-05-08 08:23:19 +0000807 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
808 sqlite3VdbeAddOp(v, OP_IncrKey, 0, 0);
809 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
danielk19773d1bfea2004-05-14 11:00:53 +0000810*/
drh487ab3c2001-11-08 00:45:21 +0000811 testOp = OP_IdxGE;
812 }
drhc045ec52002-12-04 20:01:06 +0000813 if( pLevel->bRev ){
814 /* Scan in reverse order */
danielk19773d1bfea2004-05-14 11:00:53 +0000815 /* sqlite3VdbeAddOp(v, OP_IncrKey, 0, 0); */
danielk19774adee202004-05-08 08:23:19 +0000816 sqlite3VdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +0000817 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000818 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
819 sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000820 pLevel->op = OP_Prev;
821 }else{
822 /* Scan in the forward order */
danielk19774adee202004-05-08 08:23:19 +0000823 sqlite3VdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
824 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
825 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +0000826 if( testOp==OP_IdxGE ){
827 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
828 }
drhc045ec52002-12-04 20:01:06 +0000829 pLevel->op = OP_Next;
830 }
danielk19774adee202004-05-08 08:23:19 +0000831 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
832 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
833 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000834 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000835 haveKey = 1;
836 }else{
danielk19774adee202004-05-08 08:23:19 +0000837 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh75897232000-05-29 14:26:00 +0000838 haveKey = 0;
839 }
drh6b563442001-11-07 16:48:26 +0000840 pLevel->p1 = pLevel->iCur;
841 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000842 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
843 /* Case 3: We have an inequality comparison against the ROWID field.
844 */
845 int testOp = OP_Noop;
846 int start;
847
danielk19774adee202004-05-08 08:23:19 +0000848 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
849 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000850 if( iDirectGt[i]>=0 ){
851 k = iDirectGt[i];
852 assert( k<nExpr );
853 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000854 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
855 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000856 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000857 }else{
danielk19774adee202004-05-08 08:23:19 +0000858 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000859 }
danielk19774adee202004-05-08 08:23:19 +0000860 sqlite3VdbeAddOp(v, OP_ForceInt,
drh751f4122004-01-14 21:59:22 +0000861 aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
danielk19774adee202004-05-08 08:23:19 +0000862 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000863 aExpr[k].p = 0;
864 }else{
danielk19774adee202004-05-08 08:23:19 +0000865 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000866 }
867 if( iDirectLt[i]>=0 ){
868 k = iDirectLt[i];
869 assert( k<nExpr );
870 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000871 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
872 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000873 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000874 }else{
danielk19774adee202004-05-08 08:23:19 +0000875 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000876 }
danielk19774adee202004-05-08 08:23:19 +0000877 /* sqlite3VdbeAddOp(v, OP_MustBeInt, 0, sqlite3VdbeCurrentAddr(v)+1); */
drh8aff1012001-12-22 14:49:24 +0000878 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000879 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh8aff1012001-12-22 14:49:24 +0000880 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
881 testOp = OP_Ge;
882 }else{
883 testOp = OP_Gt;
884 }
885 aExpr[k].p = 0;
886 }
danielk19774adee202004-05-08 08:23:19 +0000887 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000888 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000889 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000890 pLevel->p2 = start;
891 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +0000892 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
893 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
894 sqlite3VdbeAddOp(v, testOp, 0, brk);
drh8aff1012001-12-22 14:49:24 +0000895 }
896 haveKey = 0;
897 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000898 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000899 ** scan of the entire database table.
900 */
901 int start;
902
danielk19774adee202004-05-08 08:23:19 +0000903 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
904 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
905 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
906 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000907 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000908 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000909 pLevel->p2 = start;
910 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000911 }else{
drhc27a1ce2002-06-14 20:58:45 +0000912 /* Case 5: The WHERE clause term that refers to the right-most
913 ** column of the index is an inequality. For example, if
914 ** the index is on (x,y,z) and the WHERE clause is of the
915 ** form "x=5 AND y<10" then this case is used. Only the
916 ** right-most column can be an inequality - the rest must
917 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000918 **
919 ** This case is also used when there are no WHERE clause
920 ** constraints but an index is selected anyway, in order
921 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000922 */
923 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000924 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000925 int start;
926 int leFlag, geFlag;
927 int testOp;
928
929 /* Evaluate the equality constraints
930 */
931 for(j=0; j<nEqColumn; j++){
932 for(k=0; k<nExpr; k++){
933 if( aExpr[k].p==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000934 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000935 && aExpr[k].p->op==TK_EQ
936 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
937 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
938 ){
danielk19774adee202004-05-08 08:23:19 +0000939 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh143f3c42004-01-07 20:37:52 +0000940 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000941 break;
942 }
drh6a3ea0e2003-05-02 14:32:12 +0000943 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000944 && aExpr[k].p->op==TK_EQ
945 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
946 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
947 ){
danielk19774adee202004-05-08 08:23:19 +0000948 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000949 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000950 break;
951 }
952 }
953 }
954
drhc27a1ce2002-06-14 20:58:45 +0000955 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000956 ** used twice: once to make the termination key and once to make the
957 ** start key.
958 */
959 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +0000960 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +0000961 }
962
drhc045ec52002-12-04 20:01:06 +0000963 /* Labels for the beginning and end of the loop
964 */
danielk19774adee202004-05-08 08:23:19 +0000965 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
966 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +0000967
drh487ab3c2001-11-08 00:45:21 +0000968 /* Generate the termination key. This is the key value that
969 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000970 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +0000971 **
972 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
973 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +0000974 */
975 if( (score & 1)!=0 ){
976 for(k=0; k<nExpr; k++){
977 Expr *pExpr = aExpr[k].p;
978 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000979 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000980 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
981 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
982 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
983 ){
danielk19774adee202004-05-08 08:23:19 +0000984 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +0000985 leFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +0000986 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000987 break;
988 }
drh6a3ea0e2003-05-02 14:32:12 +0000989 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000990 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
991 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
992 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
993 ){
danielk19774adee202004-05-08 08:23:19 +0000994 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +0000995 leFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +0000996 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000997 break;
998 }
999 }
1000 testOp = OP_IdxGE;
1001 }else{
1002 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1003 leFlag = 1;
1004 }
1005 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +00001006 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +00001007 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001008 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1009 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1010 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1011 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
1012 sqlite3AddIdxKeyType(v, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +00001013/*
drh487ab3c2001-11-08 00:45:21 +00001014 if( leFlag ){
danielk19774adee202004-05-08 08:23:19 +00001015 sqlite3VdbeAddOp(v, OP_IncrKey, 0, 0);
drh487ab3c2001-11-08 00:45:21 +00001016 }
danielk19773d1bfea2004-05-14 11:00:53 +00001017*/
drhc045ec52002-12-04 20:01:06 +00001018 if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001019 sqlite3VdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001020 if( !geFlag ){
1021 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1022 }
drhc045ec52002-12-04 20:01:06 +00001023 }else{
danielk19774adee202004-05-08 08:23:19 +00001024 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001025 }
1026 }else if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001027 sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001028 }
1029
1030 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001031 ** bound on the search. There is no start key if there are no
1032 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001033 ** that case, generate a "Rewind" instruction in place of the
1034 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001035 **
1036 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1037 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001038 */
1039 if( (score & 2)!=0 ){
1040 for(k=0; k<nExpr; k++){
1041 Expr *pExpr = aExpr[k].p;
1042 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001043 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001044 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1045 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1046 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1047 ){
danielk19774adee202004-05-08 08:23:19 +00001048 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +00001049 geFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001050 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001051 break;
1052 }
drh6a3ea0e2003-05-02 14:32:12 +00001053 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001054 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1055 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1056 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1057 ){
danielk19774adee202004-05-08 08:23:19 +00001058 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +00001059 geFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001060 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001061 break;
1062 }
1063 }
drh7900ead2001-11-12 13:51:43 +00001064 }else{
1065 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001066 }
drh487ab3c2001-11-08 00:45:21 +00001067 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001068 int nCol = nEqColumn + ((score&2)!=0);
danielk19774adee202004-05-08 08:23:19 +00001069 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1070 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1071 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1072 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
1073 sqlite3AddIdxKeyType(v, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +00001074/*
drh487ab3c2001-11-08 00:45:21 +00001075 if( !geFlag ){
danielk19774adee202004-05-08 08:23:19 +00001076 sqlite3VdbeAddOp(v, OP_IncrKey, 0, 0);
drh487ab3c2001-11-08 00:45:21 +00001077 }
danielk19773d1bfea2004-05-14 11:00:53 +00001078*/
drhc045ec52002-12-04 20:01:06 +00001079 if( pLevel->bRev ){
1080 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001081 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001082 testOp = OP_IdxLT;
1083 }else{
danielk19774adee202004-05-08 08:23:19 +00001084 sqlite3VdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001085 if( !geFlag ){
1086 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1087 }
drhc045ec52002-12-04 20:01:06 +00001088 }
1089 }else if( pLevel->bRev ){
1090 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001091 }else{
danielk19774adee202004-05-08 08:23:19 +00001092 sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001093 }
1094
1095 /* Generate the the top of the loop. If there is a termination
1096 ** key we have to test for that key and abort at the top of the
1097 ** loop.
1098 */
danielk19774adee202004-05-08 08:23:19 +00001099 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001100 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001101 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1102 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001103 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1104 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1105 }
drh487ab3c2001-11-08 00:45:21 +00001106 }
danielk19774adee202004-05-08 08:23:19 +00001107 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1108 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1109 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001110 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001111 haveKey = 1;
1112 }else{
danielk19774adee202004-05-08 08:23:19 +00001113 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001114 haveKey = 0;
1115 }
1116
1117 /* Record the instruction used to terminate the loop.
1118 */
drhc045ec52002-12-04 20:01:06 +00001119 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001120 pLevel->p1 = pLevel->iCur;
1121 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001122 }
drh6a3ea0e2003-05-02 14:32:12 +00001123 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001124
1125 /* Insert code to test every subexpression that can be completely
1126 ** computed using the current set of tables.
1127 */
1128 for(j=0; j<nExpr; j++){
1129 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +00001130 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh1f162302002-10-27 19:35:33 +00001131 if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1132 continue;
1133 }
drh75897232000-05-29 14:26:00 +00001134 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001135 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +00001136 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh75897232000-05-29 14:26:00 +00001137 }
danielk19774adee202004-05-08 08:23:19 +00001138 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +00001139 aExpr[j].p = 0;
1140 }
1141 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001142
1143 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1144 ** at least one row of the right table has matched the left table.
1145 */
1146 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001147 pLevel->top = sqlite3VdbeCurrentAddr(v);
1148 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1149 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drh1cc093c2002-06-24 22:01:57 +00001150 for(j=0; j<nExpr; j++){
1151 if( aExpr[j].p==0 ) continue;
1152 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1153 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001154 /* Cannot happen. "haveKey" can only be true if pushKey is true
1155 ** an pushKey can only be true for DELETE and UPDATE and there are
1156 ** no outer joins with DELETE and UPDATE.
1157 */
drh1cc093c2002-06-24 22:01:57 +00001158 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +00001159 sqlite3VdbeAddOp(v, OP_MoveTo, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001160 }
danielk19774adee202004-05-08 08:23:19 +00001161 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh1cc093c2002-06-24 22:01:57 +00001162 aExpr[j].p = 0;
1163 }
drhad2d8302002-05-24 20:31:36 +00001164 }
drh75897232000-05-29 14:26:00 +00001165 }
1166 pWInfo->iContinue = cont;
1167 if( pushKey && !haveKey ){
danielk19774adee202004-05-08 08:23:19 +00001168 sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001169 }
drh6a3ea0e2003-05-02 14:32:12 +00001170 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001171 return pWInfo;
1172}
1173
1174/*
drhc27a1ce2002-06-14 20:58:45 +00001175** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001176** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001177*/
danielk19774adee202004-05-08 08:23:19 +00001178void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001179 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001180 int i;
drh6b563442001-11-07 16:48:26 +00001181 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001182 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001183
drhad3cab52002-05-24 02:04:32 +00001184 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001185 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001187 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001188 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001189 }
danielk19774adee202004-05-08 08:23:19 +00001190 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001191 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001193 }
drhad2d8302002-05-24 20:31:36 +00001194 if( pLevel->iLeftJoin ){
1195 int addr;
danielk19774adee202004-05-08 08:23:19 +00001196 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1197 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1198 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001199 if( pLevel->iCur>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001201 }
danielk19774adee202004-05-08 08:23:19 +00001202 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001203 }
drh19a775c2000-06-05 18:54:46 +00001204 }
danielk19774adee202004-05-08 08:23:19 +00001205 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001206 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001207 Table *pTab = pTabList->a[i].pTab;
1208 assert( pTab!=0 );
1209 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001210 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001211 sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001212 if( pLevel->pIdx!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001213 sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
drh6b563442001-11-07 16:48:26 +00001214 }
drh19a775c2000-06-05 18:54:46 +00001215 }
drh142e30d2002-08-28 03:00:58 +00001216#if 0 /* Never reuse a cursor */
drh832508b2002-03-02 17:04:07 +00001217 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1218 pWInfo->pParse->nTab = pWInfo->savedNTab;
1219 }
drh142e30d2002-08-28 03:00:58 +00001220#endif
drh75897232000-05-29 14:26:00 +00001221 sqliteFree(pWInfo);
1222 return;
1223}
danielk19774adee202004-05-08 08:23:19 +00001224
1225
1226