blob: 7d33af48a4e29a3e5f122696d1c5eb404bd9c2c8 [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**
danielk19770202b292004-06-09 09:55:16 +000015** $Id: where.c,v 1.103 2004/06/09 09:55:20 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(
danielk19770202b292004-06-09 09:55:16 +0000211 sqlite *db,
drhe3184742002-06-19 14:27:05 +0000212 Table *pTab, /* The table to be sorted */
213 int base, /* Cursor number for pTab */
214 ExprList *pOrderBy, /* The ORDER BY clause */
drhc045ec52002-12-04 20:01:06 +0000215 Index *pPreferredIdx, /* Use this index, if possible and not NULL */
drhdd4852c2002-12-04 21:50:16 +0000216 int nEqCol, /* Number of index columns used with == constraints */
drhc045ec52002-12-04 20:01:06 +0000217 int *pbRev /* Set to 1 if ORDER BY is DESC */
drhe3184742002-06-19 14:27:05 +0000218){
drhdd4852c2002-12-04 21:50:16 +0000219 int i, j;
drhe3184742002-06-19 14:27:05 +0000220 Index *pMatch;
221 Index *pIdx;
drhc045ec52002-12-04 20:01:06 +0000222 int sortOrder;
drhe3184742002-06-19 14:27:05 +0000223
224 assert( pOrderBy!=0 );
225 assert( pOrderBy->nExpr>0 );
drhd3d39e92004-05-20 22:16:29 +0000226 sortOrder = pOrderBy->a[0].sortOrder;
drhe3184742002-06-19 14:27:05 +0000227 for(i=0; i<pOrderBy->nExpr; i++){
228 Expr *p;
drhd3d39e92004-05-20 22:16:29 +0000229 if( pOrderBy->a[i].sortOrder!=sortOrder ){
drhc045ec52002-12-04 20:01:06 +0000230 /* Indices can only be used if all ORDER BY terms are either
231 ** DESC or ASC. Indices cannot be used on a mixture. */
drhe3184742002-06-19 14:27:05 +0000232 return 0;
233 }
234 p = pOrderBy->a[i].pExpr;
235 if( p->op!=TK_COLUMN || p->iTable!=base ){
236 /* Can not use an index sort on anything that is not a column in the
237 ** left-most table of the FROM clause */
238 return 0;
239 }
240 }
danielk19770202b292004-06-09 09:55:16 +0000241
drhe3184742002-06-19 14:27:05 +0000242 /* If we get this far, it means the ORDER BY clause consists only of
243 ** ascending columns in the left-most table of the FROM clause. Now
244 ** check for a matching index.
245 */
246 pMatch = 0;
247 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhdd4852c2002-12-04 21:50:16 +0000248 int nExpr = pOrderBy->nExpr;
249 if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
250 for(i=j=0; i<nEqCol; i++){
danielk19770202b292004-06-09 09:55:16 +0000251 CollSeq *pColl = sqlite3ExprCollSeq(pOrderBy->a[j].pExpr);
252 if( !pColl ) pColl = db->pDfltColl;
drhdd4852c2002-12-04 21:50:16 +0000253 if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
danielk19770202b292004-06-09 09:55:16 +0000254 if( pPreferredIdx->keyInfo.aColl[i]!=pIdx->keyInfo.aColl[i] ) break;
255 if( j<nExpr &&
256 pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] &&
257 pColl==pIdx->keyInfo.aColl[i]
258 ){
259 j++;
260 }
drhe3184742002-06-19 14:27:05 +0000261 }
drhdd4852c2002-12-04 21:50:16 +0000262 if( i<nEqCol ) continue;
263 for(i=0; i+j<nExpr; i++){
danielk19770202b292004-06-09 09:55:16 +0000264 CollSeq *pColl = sqlite3ExprCollSeq(pOrderBy->a[i+j].pExpr);
265 if( !pColl ) pColl = db->pDfltColl;
266 if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ||
267 pColl!=pIdx->keyInfo.aColl[i+nEqCol] ) break;
drhdd4852c2002-12-04 21:50:16 +0000268 }
269 if( i+j>=nExpr ){
drhe3184742002-06-19 14:27:05 +0000270 pMatch = pIdx;
271 if( pIdx==pPreferredIdx ) break;
272 }
273 }
drhc045ec52002-12-04 20:01:06 +0000274 if( pMatch && pbRev ){
275 *pbRev = sortOrder==SQLITE_SO_DESC;
276 }
drhe3184742002-06-19 14:27:05 +0000277 return pMatch;
278}
279
280/*
281** Generate the beginning of the loop used for WHERE clause processing.
drh75897232000-05-29 14:26:00 +0000282** The return value is a pointer to an (opaque) structure that contains
283** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000284** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000285** in order to complete the WHERE clause processing.
286**
287** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000288**
289** The basic idea is to do a nested loop, one loop for each table in
290** the FROM clause of a select. (INSERT and UPDATE statements are the
291** same as a SELECT with only a single table in the FROM clause.) For
292** example, if the SQL is this:
293**
294** SELECT * FROM t1, t2, t3 WHERE ...;
295**
296** Then the code generated is conceptually like the following:
297**
298** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000299** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000300** foreach row3 in t3 do /
301** ...
302** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000303** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000304** end /
305**
306** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000307** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
308** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000309** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000310**
311** If the WHERE clause is empty, the foreach loops must each scan their
312** entire tables. Thus a three-way join is an O(N^3) operation. But if
313** the tables have indices and there are terms in the WHERE clause that
314** refer to those indices, a complete table scan can be avoided and the
315** code will run much faster. Most of the work of this routine is checking
316** to see if there are indices that can be used to speed up the loop.
317**
318** Terms of the WHERE clause are also used to limit which rows actually
319** make it to the "..." in the middle of the loop. After each "foreach",
320** terms of the WHERE clause that use only terms in that loop and outer
321** loops are evaluated and if false a jump is made around all subsequent
322** inner loops (or around the "..." if the test occurs within the inner-
323** most loop)
324**
325** OUTER JOINS
326**
327** An outer join of tables t1 and t2 is conceptally coded as follows:
328**
329** foreach row1 in t1 do
330** flag = 0
331** foreach row2 in t2 do
332** start:
333** ...
334** flag = 1
335** end
drhe3184742002-06-19 14:27:05 +0000336** if flag==0 then
337** move the row2 cursor to a null row
338** goto start
339** fi
drhc27a1ce2002-06-14 20:58:45 +0000340** end
341**
drhe3184742002-06-19 14:27:05 +0000342** ORDER BY CLAUSE PROCESSING
343**
344** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
345** if there is one. If there is no ORDER BY clause or if this routine
346** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
347**
348** If an index can be used so that the natural output order of the table
349** scan is correct for the ORDER BY clause, then that index is used and
350** *ppOrderBy is set to NULL. This is an optimization that prevents an
351** unnecessary sort of the result set if an index appropriate for the
352** ORDER BY clause already exists.
353**
354** If the where clause loops cannot be arranged to provide the correct
355** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000356*/
danielk19774adee202004-05-08 08:23:19 +0000357WhereInfo *sqlite3WhereBegin(
drh75897232000-05-29 14:26:00 +0000358 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000359 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000360 Expr *pWhere, /* The WHERE clause */
drhe3184742002-06-19 14:27:05 +0000361 int pushKey, /* If TRUE, leave the table key on the stack */
362 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000363){
364 int i; /* Loop counter */
365 WhereInfo *pWInfo; /* Will become the return value of this function */
366 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000367 int brk, cont = 0; /* Addresses used during code generation */
drh75897232000-05-29 14:26:00 +0000368 int nExpr; /* Number of subexpressions in the WHERE clause */
369 int loopMask; /* One bit set for each outer loop */
370 int haveKey; /* True if KEY is on the stack */
drh6a3ea0e2003-05-02 14:32:12 +0000371 ExprMaskSet maskSet; /* The expression mask set */
drh8aff1012001-12-22 14:49:24 +0000372 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
373 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
374 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh83dcb1a2002-06-28 01:02:38 +0000375 ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
drh75897232000-05-29 14:26:00 +0000376
drhc27a1ce2002-06-14 20:58:45 +0000377 /* pushKey is only allowed if there is a single table (as in an INSERT or
378 ** UPDATE statement)
379 */
380 assert( pushKey==0 || pTabList->nSrc==1 );
drh83dcb1a2002-06-28 01:02:38 +0000381
382 /* Split the WHERE clause into separate subexpressions where each
383 ** subexpression is separated by an AND operator. If the aExpr[]
384 ** array fills up, the last entry might point to an expression which
385 ** contains additional unfactored AND operators.
386 */
drh6a3ea0e2003-05-02 14:32:12 +0000387 initMaskSet(&maskSet);
drh83dcb1a2002-06-28 01:02:38 +0000388 memset(aExpr, 0, sizeof(aExpr));
389 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
390 if( nExpr==ARRAYSIZE(aExpr) ){
danielk19774adee202004-05-08 08:23:19 +0000391 sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more "
drhf7a9e1a2004-02-22 18:40:56 +0000392 "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000393 return 0;
394 }
drhc27a1ce2002-06-14 20:58:45 +0000395
drh75897232000-05-29 14:26:00 +0000396 /* Allocate and initialize the WhereInfo structure that will become the
397 ** return value.
398 */
drhad3cab52002-05-24 02:04:32 +0000399 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000400 if( sqlite3_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +0000401 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000402 return 0;
403 }
404 pWInfo->pParse = pParse;
405 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000406 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000407 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000408
409 /* Special case: a WHERE clause that is constant. Evaluate the
410 ** expression and either jump over all of the code or fall thru.
411 */
danielk19774adee202004-05-08 08:23:19 +0000412 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
413 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000414 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000415 }
drh75897232000-05-29 14:26:00 +0000416
drh75897232000-05-29 14:26:00 +0000417 /* Analyze all of the subexpressions.
418 */
419 for(i=0; i<nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000420 exprAnalyze(&maskSet, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000421
422 /* If we are executing a trigger body, remove all references to
423 ** new.* and old.* tables from the prerequisite masks.
424 */
425 if( pParse->trigStack ){
426 int x;
427 if( (x = pParse->trigStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000428 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000429 aExpr[i].prereqRight &= mask;
430 aExpr[i].prereqLeft &= mask;
431 aExpr[i].prereqAll &= mask;
432 }
433 if( (x = pParse->trigStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000434 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000435 aExpr[i].prereqRight &= mask;
436 aExpr[i].prereqLeft &= mask;
437 aExpr[i].prereqAll &= mask;
438 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000439 }
drh75897232000-05-29 14:26:00 +0000440 }
441
drh75897232000-05-29 14:26:00 +0000442 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000443 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000444 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000445 ** loop.
446 **
447 ** If terms exist that use the ROWID of any table, then set the
448 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
449 ** to the index of the term containing the ROWID. We always prefer
450 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000451 ** index which requires reading an index first to get the rowid then
452 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000453 **
454 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000455 ** first 32 tables are candidates for indices. This is (again) due
456 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000457 */
458 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000459 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000460 int j;
drh6a3ea0e2003-05-02 14:32:12 +0000461 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
462 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
463 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000464 Index *pIdx;
465 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000466 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000467
drhc4a3c772001-04-04 11:48:57 +0000468 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000469 ** ROWID field of this table. For terms of the form ROWID==expr
470 ** set iDirectEq[i] to the index of the term. For terms of the
471 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
472 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000473 **
474 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000475 */
drhc8f8b632002-09-30 12:36:26 +0000476 pWInfo->a[i].iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000477 iDirectEq[i] = -1;
478 iDirectLt[i] = -1;
479 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000480 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000481 if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000482 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000483 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000484 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000485 case TK_EQ: iDirectEq[i] = j; break;
486 case TK_LE:
487 case TK_LT: iDirectLt[i] = j; break;
488 case TK_GE:
489 case TK_GT: iDirectGt[i] = j; break;
490 }
drhc4a3c772001-04-04 11:48:57 +0000491 }
drh6a3ea0e2003-05-02 14:32:12 +0000492 if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000493 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000494 switch( aExpr[j].p->op ){
495 case TK_EQ: iDirectEq[i] = j; break;
496 case TK_LE:
497 case TK_LT: iDirectGt[i] = j; break;
498 case TK_GE:
499 case TK_GT: iDirectLt[i] = j; break;
500 }
drhc4a3c772001-04-04 11:48:57 +0000501 }
502 }
drh8aff1012001-12-22 14:49:24 +0000503 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000504 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000505 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000506 continue;
507 }
508
drh75897232000-05-29 14:26:00 +0000509 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000510 ** the "best" index. pBestIdx is left set to NULL if no indices
511 ** are usable.
drh75897232000-05-29 14:26:00 +0000512 **
drh487ab3c2001-11-08 00:45:21 +0000513 ** The best index is determined as follows. For each of the
514 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000515 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000516 ** constrained by an inequality. Add 1 if for an "x<..." constraint
517 ** and add 2 for an "x>..." constraint. Chose the index that
518 ** gives the best score.
519 **
520 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000521 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000522 ** then all constraints are equalities. If score&1 is not 0 then
523 ** there is an inequality used as a termination key. (ex: "x<...")
524 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000525 ** start key. (ex: "x>..."). A score or 4 is the special case
526 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000527 **
drhc27a1ce2002-06-14 20:58:45 +0000528 ** The IN operator (as in "<expr> IN (...)") is treated the same as
529 ** an equality comparison except that it can only be used on the
530 ** left-most column of an index and other terms of the WHERE clause
531 ** cannot be used in conjunction with the IN operator to help satisfy
532 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000533 */
534 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000535 int eqMask = 0; /* Index columns covered by an x=... term */
536 int ltMask = 0; /* Index columns covered by an x<... term */
537 int gtMask = 0; /* Index columns covered by an x>... term */
538 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000539 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000540
drh487ab3c2001-11-08 00:45:21 +0000541 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000542 for(j=0; j<nExpr; j++){
danielk19770202b292004-06-09 09:55:16 +0000543 CollSeq *pColl = sqlite3ExprCollSeq(aExpr[j].p->pLeft);
544 if( !pColl && aExpr[j].p->pRight ){
545 pColl = sqlite3ExprCollSeq(aExpr[j].p->pRight);
546 }
547 if( !pColl ){
548 pColl = pParse->db->pDfltColl;
549 }
drh6a3ea0e2003-05-02 14:32:12 +0000550 if( aExpr[j].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000551 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000552 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000553 int k;
danielk1977e014a832004-05-17 10:48:57 +0000554 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000555 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000556 /* If the collating sequences or affinities don't match,
557 ** ignore this index. */
558 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
559 if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
560 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000561 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000562 case TK_IN: {
563 if( k==0 ) inMask |= 1;
564 break;
565 }
drh487ab3c2001-11-08 00:45:21 +0000566 case TK_EQ: {
567 eqMask |= 1<<k;
568 break;
569 }
570 case TK_LE:
571 case TK_LT: {
572 ltMask |= 1<<k;
573 break;
574 }
575 case TK_GE:
576 case TK_GT: {
577 gtMask |= 1<<k;
578 break;
579 }
580 default: {
581 /* CANT_HAPPEN */
582 assert( 0 );
583 break;
584 }
585 }
drh75897232000-05-29 14:26:00 +0000586 break;
587 }
588 }
589 }
drh6a3ea0e2003-05-02 14:32:12 +0000590 if( aExpr[j].idxRight==iCur
drh75897232000-05-29 14:26:00 +0000591 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000592 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000593 int k;
danielk1977e014a832004-05-17 10:48:57 +0000594 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000595 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000596 /* If the collating sequences or affinities don't match,
597 ** ignore this index. */
598 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
599 if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
600 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000601 switch( aExpr[j].p->op ){
602 case TK_EQ: {
603 eqMask |= 1<<k;
604 break;
605 }
606 case TK_LE:
607 case TK_LT: {
608 gtMask |= 1<<k;
609 break;
610 }
611 case TK_GE:
612 case TK_GT: {
613 ltMask |= 1<<k;
614 break;
615 }
616 default: {
617 /* CANT_HAPPEN */
618 assert( 0 );
619 break;
620 }
621 }
drh75897232000-05-29 14:26:00 +0000622 break;
623 }
624 }
625 }
626 }
drhc045ec52002-12-04 20:01:06 +0000627
628 /* The following loop ends with nEq set to the number of columns
629 ** on the left of the index with == constraints.
630 */
drh487ab3c2001-11-08 00:45:21 +0000631 for(nEq=0; nEq<pIdx->nColumn; nEq++){
632 m = (1<<(nEq+1))-1;
633 if( (m & eqMask)!=m ) break;
634 }
drhc045ec52002-12-04 20:01:06 +0000635 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000636 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000637 if( m & ltMask ) score++; /* Increase score for a < constraint */
638 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
639 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000640 if( score>bestScore ){
641 pBestIdx = pIdx;
642 bestScore = score;
drh75897232000-05-29 14:26:00 +0000643 }
644 }
drh6b563442001-11-07 16:48:26 +0000645 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000646 pWInfo->a[i].score = bestScore;
drhc045ec52002-12-04 20:01:06 +0000647 pWInfo->a[i].bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000648 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000649 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000650 pWInfo->a[i].iCur = pParse->nTab++;
651 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000652 }
drh75897232000-05-29 14:26:00 +0000653 }
654
drhe3184742002-06-19 14:27:05 +0000655 /* Check to see if the ORDER BY clause is or can be satisfied by the
656 ** use of an index on the first table.
657 */
658 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
659 Index *pSortIdx;
660 Index *pIdx;
661 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000662 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000663
664 pTab = pTabList->a[0].pTab;
665 pIdx = pWInfo->a[0].pIdx;
666 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000667 /* If there is already an IN index on the left-most table,
668 ** it will not give the correct sort order.
669 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000670 */
671 pSortIdx = 0;
672 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
673 /* If the left-most column is accessed using its ROWID, then do
674 ** not try to sort by index.
675 */
676 pSortIdx = 0;
677 }else{
drhdd4852c2002-12-04 21:50:16 +0000678 int nEqCol = (pWInfo->a[0].score+4)/8;
danielk19770202b292004-06-09 09:55:16 +0000679 pSortIdx = findSortingIndex(pParse->db, pTab, pTabList->a[0].iCursor,
drh6a3ea0e2003-05-02 14:32:12 +0000680 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000681 }
682 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
683 if( pIdx==0 ){
684 pWInfo->a[0].pIdx = pSortIdx;
685 pWInfo->a[0].iCur = pParse->nTab++;
686 pWInfo->peakNTab = pParse->nTab;
687 }
drhc045ec52002-12-04 20:01:06 +0000688 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000689 *ppOrderBy = 0;
690 }
691 }
692
drh6b563442001-11-07 16:48:26 +0000693 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000694 */
drh80242052004-06-09 00:48:12 +0000695 sqlite3CodeVerifySchema(pParse, 1); /* Inserts the cookie verifier Goto */
drhad3cab52002-05-24 02:04:32 +0000696 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000697 Table *pTab;
drh701a0ae2004-02-22 20:05:00 +0000698 Index *pIx;
drhf57b3392001-10-08 13:22:32 +0000699
700 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000701 if( pTab->isTransient || pTab->pSelect ) continue;
danielk19774adee202004-05-08 08:23:19 +0000702 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000703 sqlite3VdbeAddOp(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +0000704 sqlite3VdbeAddOp(v, OP_SetNumColumns, pTabList->a[i].iCursor, pTab->nCol);
drh80242052004-06-09 00:48:12 +0000705 if( pTab->tnum>1 ){
706 sqlite3CodeVerifySchema(pParse, pTab->iDb);
707 }
drh701a0ae2004-02-22 20:05:00 +0000708 if( (pIx = pWInfo->a[i].pIdx)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000709 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000710 sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum,
711 (char*)&pIx->keyInfo, P3_KEYINFO);
drh75897232000-05-29 14:26:00 +0000712 }
713 }
714
715 /* Generate the code to do the search
716 */
drh75897232000-05-29 14:26:00 +0000717 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000718 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000719 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000720 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000721 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000722 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000723
drhad2d8302002-05-24 20:31:36 +0000724 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000725 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000726 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000727 */
728 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
729 if( !pParse->nMem ) pParse->nMem++;
730 pLevel->iLeftJoin = pParse->nMem++;
danielk19770f69c1e2004-05-29 11:24:50 +0000731 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000732 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad2d8302002-05-24 20:31:36 +0000733 }
734
drh8aff1012001-12-22 14:49:24 +0000735 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000736 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000737 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
738 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000739 ** equality comparison against the ROWID field. Or
740 ** we reference multiple rows using a "rowid IN (...)"
741 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000742 */
drh8aff1012001-12-22 14:49:24 +0000743 k = iDirectEq[i];
744 assert( k<nExpr );
745 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000746 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
danielk19774adee202004-05-08 08:23:19 +0000747 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000748 if( aExpr[k].idxLeft==iCur ){
drhd99f7062002-06-08 23:25:08 +0000749 Expr *pX = aExpr[k].p;
750 if( pX->op!=TK_IN ){
danielk19774adee202004-05-08 08:23:19 +0000751 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drhd99f7062002-06-08 23:25:08 +0000752 }else{
danielk19774adee202004-05-08 08:23:19 +0000753 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
754 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000755 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000756 pLevel->inOp = OP_Next;
757 pLevel->inP1 = pX->iTable;
758 }
drh8aff1012001-12-22 14:49:24 +0000759 }else{
danielk19774adee202004-05-08 08:23:19 +0000760 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000761 }
drh8aff1012001-12-22 14:49:24 +0000762 aExpr[k].p = 0;
danielk19774adee202004-05-08 08:23:19 +0000763 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
764 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000765 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +0000766 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000767 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000768 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000769 /* Case 2: There is an index and all terms of the WHERE clause that
770 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000771 */
drh6b563442001-11-07 16:48:26 +0000772 int start;
drhc045ec52002-12-04 20:01:06 +0000773 int nColumn = (pLevel->score+4)/8;
danielk19774adee202004-05-08 08:23:19 +0000774 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000775
776 /* For each column of the index, find the term of the WHERE clause that
777 ** constraints that column. If the WHERE clause term is X=expr, then
778 ** evaluation expr and leave the result on the stack */
drh487ab3c2001-11-08 00:45:21 +0000779 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000780 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000781 Expr *pX = aExpr[k].p;
782 if( pX==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000783 if( aExpr[k].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000784 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000785 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000786 ){
danielk1977e014a832004-05-17 10:48:57 +0000787 char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
788 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
789 if( pX->op==TK_EQ ){
790 sqlite3ExprCode(pParse, pX->pRight);
791 aExpr[k].p = 0;
792 break;
793 }
794 if( pX->op==TK_IN && nColumn==1 ){
danielk19774adee202004-05-08 08:23:19 +0000795 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
796 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000797 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000798 pLevel->inOp = OP_Next;
799 pLevel->inP1 = pX->iTable;
danielk1977e014a832004-05-17 10:48:57 +0000800 aExpr[k].p = 0;
801 break;
drhd99f7062002-06-08 23:25:08 +0000802 }
drhd99f7062002-06-08 23:25:08 +0000803 }
drh75897232000-05-29 14:26:00 +0000804 }
drh6a3ea0e2003-05-02 14:32:12 +0000805 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000806 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000807 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000808 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000809 ){
danielk1977e014a832004-05-17 10:48:57 +0000810 char idxaff = pIdx->pTable->aCol[pX->pRight->iColumn].affinity;
811 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
812 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
813 aExpr[k].p = 0;
814 break;
815 }
drh75897232000-05-29 14:26:00 +0000816 }
817 }
818 }
drh6b563442001-11-07 16:48:26 +0000819 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000820 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000821
822 /* At this point, the top nColumn elements of the stack are the
823 ** values of columns in the index we are using. Check to see if
824 ** any of these values are NULL. If they are, they will not match any
825 ** index entries, so skip immediately to the next iteration of the loop */
danielk19774adee202004-05-08 08:23:19 +0000826 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
827 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
828 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
drh772ae622004-05-19 13:13:08 +0000829
830 /* Generate an index key from the top nColumn elements of the stack */
danielk19774adee202004-05-08 08:23:19 +0000831 sqlite3VdbeAddOp(v, OP_MakeKey, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000832 sqlite3IndexAffinityStr(v, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +0000833 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drh772ae622004-05-19 13:13:08 +0000834
drh772ae622004-05-19 13:13:08 +0000835 /* Generate code (1) to move to the first matching element of the table.
836 ** Then generate code (2) that jumps to "brk" after the cursor is past
837 ** the last matching element of the table. The code (1) is executed
838 ** once to initialize the search, the code (2) is executed before each
839 ** iteration of the scan to see if the scan has finished. */
drhc045ec52002-12-04 20:01:06 +0000840 if( pLevel->bRev ){
841 /* Scan in reverse order */
drh7cf6e4d2004-05-19 14:56:55 +0000842 sqlite3VdbeAddOp(v, OP_MoveLe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000843 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
844 sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000845 pLevel->op = OP_Prev;
846 }else{
847 /* Scan in the forward order */
drh7cf6e4d2004-05-19 14:56:55 +0000848 sqlite3VdbeAddOp(v, OP_MoveGe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000849 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drhfec19aa2004-05-19 20:41:03 +0000850 sqlite3VdbeOp3(v, OP_IdxGE, pLevel->iCur, brk, "+", P3_STATIC);
drhc045ec52002-12-04 20:01:06 +0000851 pLevel->op = OP_Next;
852 }
danielk19774adee202004-05-08 08:23:19 +0000853 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
854 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
855 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000856 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000857 haveKey = 1;
858 }else{
drh7cf6e4d2004-05-19 14:56:55 +0000859 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +0000860 haveKey = 0;
861 }
drh6b563442001-11-07 16:48:26 +0000862 pLevel->p1 = pLevel->iCur;
863 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000864 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
865 /* Case 3: We have an inequality comparison against the ROWID field.
866 */
867 int testOp = OP_Noop;
868 int start;
869
danielk19774adee202004-05-08 08:23:19 +0000870 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
871 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000872 if( iDirectGt[i]>=0 ){
873 k = iDirectGt[i];
874 assert( k<nExpr );
875 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000876 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
877 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000878 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000879 }else{
danielk19774adee202004-05-08 08:23:19 +0000880 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000881 }
danielk19774adee202004-05-08 08:23:19 +0000882 sqlite3VdbeAddOp(v, OP_ForceInt,
drh751f4122004-01-14 21:59:22 +0000883 aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
drh7cf6e4d2004-05-19 14:56:55 +0000884 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000885 aExpr[k].p = 0;
886 }else{
danielk19774adee202004-05-08 08:23:19 +0000887 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000888 }
889 if( iDirectLt[i]>=0 ){
890 k = iDirectLt[i];
891 assert( k<nExpr );
892 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000893 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
894 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000895 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000896 }else{
danielk19774adee202004-05-08 08:23:19 +0000897 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000898 }
danielk19774adee202004-05-08 08:23:19 +0000899 /* sqlite3VdbeAddOp(v, OP_MustBeInt, 0, sqlite3VdbeCurrentAddr(v)+1); */
drh8aff1012001-12-22 14:49:24 +0000900 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000901 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh8aff1012001-12-22 14:49:24 +0000902 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
903 testOp = OP_Ge;
904 }else{
905 testOp = OP_Gt;
906 }
907 aExpr[k].p = 0;
908 }
danielk19774adee202004-05-08 08:23:19 +0000909 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000910 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000911 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000912 pLevel->p2 = start;
913 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +0000914 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
915 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
916 sqlite3VdbeAddOp(v, testOp, 0, brk);
drh8aff1012001-12-22 14:49:24 +0000917 }
918 haveKey = 0;
919 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000920 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000921 ** scan of the entire database table.
922 */
923 int start;
924
danielk19774adee202004-05-08 08:23:19 +0000925 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
926 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
927 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
928 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000929 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000930 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000931 pLevel->p2 = start;
932 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000933 }else{
drhc27a1ce2002-06-14 20:58:45 +0000934 /* Case 5: The WHERE clause term that refers to the right-most
935 ** column of the index is an inequality. For example, if
936 ** the index is on (x,y,z) and the WHERE clause is of the
937 ** form "x=5 AND y<10" then this case is used. Only the
938 ** right-most column can be an inequality - the rest must
939 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000940 **
941 ** This case is also used when there are no WHERE clause
942 ** constraints but an index is selected anyway, in order
943 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000944 */
945 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000946 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000947 int start;
948 int leFlag, geFlag;
949 int testOp;
950
951 /* Evaluate the equality constraints
952 */
953 for(j=0; j<nEqColumn; j++){
954 for(k=0; k<nExpr; k++){
955 if( aExpr[k].p==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000956 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000957 && aExpr[k].p->op==TK_EQ
958 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
959 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
960 ){
danielk19774adee202004-05-08 08:23:19 +0000961 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh143f3c42004-01-07 20:37:52 +0000962 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000963 break;
964 }
drh6a3ea0e2003-05-02 14:32:12 +0000965 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000966 && aExpr[k].p->op==TK_EQ
967 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
968 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
969 ){
danielk19774adee202004-05-08 08:23:19 +0000970 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000971 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000972 break;
973 }
974 }
975 }
976
drhc27a1ce2002-06-14 20:58:45 +0000977 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000978 ** used twice: once to make the termination key and once to make the
979 ** start key.
980 */
981 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +0000982 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +0000983 }
984
drhc045ec52002-12-04 20:01:06 +0000985 /* Labels for the beginning and end of the loop
986 */
danielk19774adee202004-05-08 08:23:19 +0000987 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
988 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +0000989
drh487ab3c2001-11-08 00:45:21 +0000990 /* Generate the termination key. This is the key value that
991 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000992 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +0000993 **
994 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
995 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +0000996 */
997 if( (score & 1)!=0 ){
998 for(k=0; k<nExpr; k++){
999 Expr *pExpr = aExpr[k].p;
1000 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001001 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001002 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1003 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1004 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1005 ){
danielk19774adee202004-05-08 08:23:19 +00001006 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +00001007 leFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001008 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001009 break;
1010 }
drh6a3ea0e2003-05-02 14:32:12 +00001011 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001012 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1013 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1014 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1015 ){
danielk19774adee202004-05-08 08:23:19 +00001016 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +00001017 leFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001018 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001019 break;
1020 }
1021 }
1022 testOp = OP_IdxGE;
1023 }else{
1024 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1025 leFlag = 1;
1026 }
1027 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +00001028 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +00001029 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001030 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1031 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1032 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1033 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001034 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001035 if( pLevel->bRev ){
drh7cf6e4d2004-05-19 14:56:55 +00001036 int op = leFlag ? OP_MoveLe : OP_MoveLt;
1037 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001038 }else{
danielk19774adee202004-05-08 08:23:19 +00001039 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001040 }
1041 }else if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001042 sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001043 }
1044
1045 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001046 ** bound on the search. There is no start key if there are no
1047 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001048 ** that case, generate a "Rewind" instruction in place of the
1049 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001050 **
1051 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1052 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001053 */
1054 if( (score & 2)!=0 ){
1055 for(k=0; k<nExpr; k++){
1056 Expr *pExpr = aExpr[k].p;
1057 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001058 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001059 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1060 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1061 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1062 ){
danielk19774adee202004-05-08 08:23:19 +00001063 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +00001064 geFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001065 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001066 break;
1067 }
drh6a3ea0e2003-05-02 14:32:12 +00001068 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001069 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1070 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1071 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1072 ){
danielk19774adee202004-05-08 08:23:19 +00001073 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +00001074 geFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001075 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001076 break;
1077 }
1078 }
drh7900ead2001-11-12 13:51:43 +00001079 }else{
1080 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001081 }
drh487ab3c2001-11-08 00:45:21 +00001082 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001083 int nCol = nEqColumn + ((score&2)!=0);
danielk19774adee202004-05-08 08:23:19 +00001084 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1085 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1086 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
1087 sqlite3VdbeAddOp(v, OP_MakeKey, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001088 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001089 if( pLevel->bRev ){
1090 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001091 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001092 testOp = OP_IdxLT;
1093 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001094 int op = geFlag ? OP_MoveGe : OP_MoveGt;
1095 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001096 }
1097 }else if( pLevel->bRev ){
1098 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001099 }else{
danielk19774adee202004-05-08 08:23:19 +00001100 sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001101 }
1102
1103 /* Generate the the top of the loop. If there is a termination
1104 ** key we have to test for that key and abort at the top of the
1105 ** loop.
1106 */
danielk19774adee202004-05-08 08:23:19 +00001107 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001108 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001109 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1110 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001111 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1112 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1113 }
drh487ab3c2001-11-08 00:45:21 +00001114 }
danielk19774adee202004-05-08 08:23:19 +00001115 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1116 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1117 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001118 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001119 haveKey = 1;
1120 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001121 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001122 haveKey = 0;
1123 }
1124
1125 /* Record the instruction used to terminate the loop.
1126 */
drhc045ec52002-12-04 20:01:06 +00001127 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001128 pLevel->p1 = pLevel->iCur;
1129 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001130 }
drh6a3ea0e2003-05-02 14:32:12 +00001131 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001132
1133 /* Insert code to test every subexpression that can be completely
1134 ** computed using the current set of tables.
1135 */
1136 for(j=0; j<nExpr; j++){
1137 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +00001138 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh1f162302002-10-27 19:35:33 +00001139 if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1140 continue;
1141 }
drh75897232000-05-29 14:26:00 +00001142 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001143 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001144 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +00001145 }
danielk19774adee202004-05-08 08:23:19 +00001146 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +00001147 aExpr[j].p = 0;
1148 }
1149 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001150
1151 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1152 ** at least one row of the right table has matched the left table.
1153 */
1154 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001155 pLevel->top = sqlite3VdbeCurrentAddr(v);
1156 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1157 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drh1cc093c2002-06-24 22:01:57 +00001158 for(j=0; j<nExpr; j++){
1159 if( aExpr[j].p==0 ) continue;
1160 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1161 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001162 /* Cannot happen. "haveKey" can only be true if pushKey is true
1163 ** an pushKey can only be true for DELETE and UPDATE and there are
1164 ** no outer joins with DELETE and UPDATE.
1165 */
drh1cc093c2002-06-24 22:01:57 +00001166 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001167 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001168 }
danielk19774adee202004-05-08 08:23:19 +00001169 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh1cc093c2002-06-24 22:01:57 +00001170 aExpr[j].p = 0;
1171 }
drhad2d8302002-05-24 20:31:36 +00001172 }
drh75897232000-05-29 14:26:00 +00001173 }
1174 pWInfo->iContinue = cont;
1175 if( pushKey && !haveKey ){
danielk19774adee202004-05-08 08:23:19 +00001176 sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001177 }
drh6a3ea0e2003-05-02 14:32:12 +00001178 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001179 return pWInfo;
1180}
1181
1182/*
drhc27a1ce2002-06-14 20:58:45 +00001183** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001184** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001185*/
danielk19774adee202004-05-08 08:23:19 +00001186void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001187 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001188 int i;
drh6b563442001-11-07 16:48:26 +00001189 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001190 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001191
drhad3cab52002-05-24 02:04:32 +00001192 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001193 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001194 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001195 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001196 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001197 }
danielk19774adee202004-05-08 08:23:19 +00001198 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001199 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001201 }
drhad2d8302002-05-24 20:31:36 +00001202 if( pLevel->iLeftJoin ){
1203 int addr;
danielk19774adee202004-05-08 08:23:19 +00001204 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1205 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1206 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001207 if( pLevel->iCur>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001208 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001209 }
danielk19774adee202004-05-08 08:23:19 +00001210 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001211 }
drh19a775c2000-06-05 18:54:46 +00001212 }
danielk19774adee202004-05-08 08:23:19 +00001213 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001214 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001215 Table *pTab = pTabList->a[i].pTab;
1216 assert( pTab!=0 );
1217 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001218 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001219 sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001220 if( pLevel->pIdx!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001221 sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
drh6b563442001-11-07 16:48:26 +00001222 }
drh19a775c2000-06-05 18:54:46 +00001223 }
drh142e30d2002-08-28 03:00:58 +00001224#if 0 /* Never reuse a cursor */
drh832508b2002-03-02 17:04:07 +00001225 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1226 pWInfo->pParse->nTab = pWInfo->savedNTab;
1227 }
drh142e30d2002-08-28 03:00:58 +00001228#endif
drh75897232000-05-29 14:26:00 +00001229 sqliteFree(pWInfo);
1230 return;
1231}