blob: 5d676d7c1023786fb49d56e6371df73924398959 [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**
drhc275b4e2004-07-19 17:25:24 +000015** $Id: where.c,v 1.109 2004/07/19 17:25:25 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include "sqliteInt.h"
18
19/*
20** The query generator uses an array of instances of this structure to
21** help it analyze the subexpressions of the WHERE clause. Each WHERE
22** clause subexpression is separated from the others by an AND operator.
23*/
24typedef struct ExprInfo ExprInfo;
25struct ExprInfo {
26 Expr *p; /* Pointer to the subexpression */
drhe3184742002-06-19 14:27:05 +000027 u8 indexable; /* True if this subexprssion is usable by an index */
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 */
drh8feb4b12004-07-19 02:12:14 +000049 int ix[31]; /* Cursor assigned to each bit */
drh6a3ea0e2003-05-02 14:32:12 +000050};
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 ){
drh8feb4b12004-07-19 02:12:14 +0000126 mask = getMask(pMaskSet, p->iTable);
127 if( mask==0 ) mask = -1;
128 return mask;
drh75897232000-05-29 14:26:00 +0000129 }
130 if( p->pRight ){
drh6a3ea0e2003-05-02 14:32:12 +0000131 mask = exprTableUsage(pMaskSet, p->pRight);
drh75897232000-05-29 14:26:00 +0000132 }
133 if( p->pLeft ){
drh6a3ea0e2003-05-02 14:32:12 +0000134 mask |= exprTableUsage(pMaskSet, p->pLeft);
drh75897232000-05-29 14:26:00 +0000135 }
drhdd579122002-04-02 01:58:57 +0000136 if( p->pList ){
137 int i;
138 for(i=0; i<p->pList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000139 mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
drhdd579122002-04-02 01:58:57 +0000140 }
141 }
drh75897232000-05-29 14:26:00 +0000142 return mask;
143}
144
145/*
drh487ab3c2001-11-08 00:45:21 +0000146** Return TRUE if the given operator is one of the operators that is
147** allowed for an indexable WHERE clause. The allowed operators are
drhc27a1ce2002-06-14 20:58:45 +0000148** "=", "<", ">", "<=", ">=", and "IN".
drh487ab3c2001-11-08 00:45:21 +0000149*/
150static int allowedOp(int op){
151 switch( op ){
152 case TK_LT:
153 case TK_LE:
154 case TK_GT:
155 case TK_GE:
156 case TK_EQ:
drhd99f7062002-06-08 23:25:08 +0000157 case TK_IN:
drh487ab3c2001-11-08 00:45:21 +0000158 return 1;
159 default:
160 return 0;
161 }
162}
163
164/*
drh75897232000-05-29 14:26:00 +0000165** The input to this routine is an ExprInfo structure with only the
166** "p" field filled in. The job of this routine is to analyze the
167** subexpression and populate all the other fields of the ExprInfo
168** structure.
169*/
drh6a3ea0e2003-05-02 14:32:12 +0000170static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000171 Expr *pExpr = pInfo->p;
drh6a3ea0e2003-05-02 14:32:12 +0000172 pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
173 pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
174 pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
drh75897232000-05-29 14:26:00 +0000175 pInfo->indexable = 0;
176 pInfo->idxLeft = -1;
177 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000178 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drhd99f7062002-06-08 23:25:08 +0000179 if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000180 pInfo->idxRight = pExpr->pRight->iTable;
drh75897232000-05-29 14:26:00 +0000181 pInfo->indexable = 1;
182 }
drh967e8b72000-06-21 13:59:10 +0000183 if( pExpr->pLeft->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000184 pInfo->idxLeft = pExpr->pLeft->iTable;
drh75897232000-05-29 14:26:00 +0000185 pInfo->indexable = 1;
186 }
187 }
188}
189
190/*
drhe3184742002-06-19 14:27:05 +0000191** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
192** left-most table in the FROM clause of that same SELECT statement and
193** the table has a cursor number of "base".
194**
195** This routine attempts to find an index for pTab that generates the
196** correct record sequence for the given ORDER BY clause. The return value
197** is a pointer to an index that does the job. NULL is returned if the
198** table has no index that will generate the correct sort order.
199**
200** If there are two or more indices that generate the correct sort order
201** and pPreferredIdx is one of those indices, then return pPreferredIdx.
drhdd4852c2002-12-04 21:50:16 +0000202**
203** nEqCol is the number of columns of pPreferredIdx that are used as
204** equality constraints. Any index returned must have exactly this same
205** set of columns. The ORDER BY clause only matches index columns beyond the
206** the first nEqCol columns.
207**
208** All terms of the ORDER BY clause must be either ASC or DESC. The
209** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
210** set to 0 if the ORDER BY clause is all ASC.
drhe3184742002-06-19 14:27:05 +0000211*/
212static Index *findSortingIndex(
danielk1977d2b65b92004-06-10 10:51:47 +0000213 Parse *pParse,
drhe3184742002-06-19 14:27:05 +0000214 Table *pTab, /* The table to be sorted */
215 int base, /* Cursor number for pTab */
216 ExprList *pOrderBy, /* The ORDER BY clause */
drhc045ec52002-12-04 20:01:06 +0000217 Index *pPreferredIdx, /* Use this index, if possible and not NULL */
drhdd4852c2002-12-04 21:50:16 +0000218 int nEqCol, /* Number of index columns used with == constraints */
drhc045ec52002-12-04 20:01:06 +0000219 int *pbRev /* Set to 1 if ORDER BY is DESC */
drhe3184742002-06-19 14:27:05 +0000220){
drhdd4852c2002-12-04 21:50:16 +0000221 int i, j;
drhe3184742002-06-19 14:27:05 +0000222 Index *pMatch;
223 Index *pIdx;
drhc045ec52002-12-04 20:01:06 +0000224 int sortOrder;
danielk1977d2b65b92004-06-10 10:51:47 +0000225 sqlite *db = pParse->db;
drhe3184742002-06-19 14:27:05 +0000226
227 assert( pOrderBy!=0 );
228 assert( pOrderBy->nExpr>0 );
drhd3d39e92004-05-20 22:16:29 +0000229 sortOrder = pOrderBy->a[0].sortOrder;
drhe3184742002-06-19 14:27:05 +0000230 for(i=0; i<pOrderBy->nExpr; i++){
231 Expr *p;
drhd3d39e92004-05-20 22:16:29 +0000232 if( pOrderBy->a[i].sortOrder!=sortOrder ){
drhc045ec52002-12-04 20:01:06 +0000233 /* Indices can only be used if all ORDER BY terms are either
234 ** DESC or ASC. Indices cannot be used on a mixture. */
drhe3184742002-06-19 14:27:05 +0000235 return 0;
236 }
237 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 }
danielk19770202b292004-06-09 09:55:16 +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++){
danielk1977d2b65b92004-06-10 10:51:47 +0000254 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[j].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000255 if( !pColl ) pColl = db->pDfltColl;
drhdd4852c2002-12-04 21:50:16 +0000256 if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
danielk19770202b292004-06-09 09:55:16 +0000257 if( pPreferredIdx->keyInfo.aColl[i]!=pIdx->keyInfo.aColl[i] ) break;
258 if( j<nExpr &&
259 pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] &&
260 pColl==pIdx->keyInfo.aColl[i]
261 ){
262 j++;
263 }
drhe3184742002-06-19 14:27:05 +0000264 }
drhdd4852c2002-12-04 21:50:16 +0000265 if( i<nEqCol ) continue;
266 for(i=0; i+j<nExpr; i++){
danielk1977d2b65b92004-06-10 10:51:47 +0000267 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[i+j].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000268 if( !pColl ) pColl = db->pDfltColl;
269 if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ||
270 pColl!=pIdx->keyInfo.aColl[i+nEqCol] ) break;
drhdd4852c2002-12-04 21:50:16 +0000271 }
272 if( i+j>=nExpr ){
drhe3184742002-06-19 14:27:05 +0000273 pMatch = pIdx;
274 if( pIdx==pPreferredIdx ) break;
275 }
276 }
drhc045ec52002-12-04 20:01:06 +0000277 if( pMatch && pbRev ){
278 *pbRev = sortOrder==SQLITE_SO_DESC;
279 }
drhe3184742002-06-19 14:27:05 +0000280 return pMatch;
281}
282
283/*
284** Generate the beginning of the loop used for WHERE clause processing.
drh75897232000-05-29 14:26:00 +0000285** The return value is a pointer to an (opaque) structure that contains
286** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000287** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000288** in order to complete the WHERE clause processing.
289**
290** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000291**
292** The basic idea is to do a nested loop, one loop for each table in
293** the FROM clause of a select. (INSERT and UPDATE statements are the
294** same as a SELECT with only a single table in the FROM clause.) For
295** example, if the SQL is this:
296**
297** SELECT * FROM t1, t2, t3 WHERE ...;
298**
299** Then the code generated is conceptually like the following:
300**
301** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000302** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000303** foreach row3 in t3 do /
304** ...
305** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000306** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000307** end /
308**
309** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000310** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
311** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000312** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000313**
314** If the WHERE clause is empty, the foreach loops must each scan their
315** entire tables. Thus a three-way join is an O(N^3) operation. But if
316** the tables have indices and there are terms in the WHERE clause that
317** refer to those indices, a complete table scan can be avoided and the
318** code will run much faster. Most of the work of this routine is checking
319** to see if there are indices that can be used to speed up the loop.
320**
321** Terms of the WHERE clause are also used to limit which rows actually
322** make it to the "..." in the middle of the loop. After each "foreach",
323** terms of the WHERE clause that use only terms in that loop and outer
324** loops are evaluated and if false a jump is made around all subsequent
325** inner loops (or around the "..." if the test occurs within the inner-
326** most loop)
327**
328** OUTER JOINS
329**
330** An outer join of tables t1 and t2 is conceptally coded as follows:
331**
332** foreach row1 in t1 do
333** flag = 0
334** foreach row2 in t2 do
335** start:
336** ...
337** flag = 1
338** end
drhe3184742002-06-19 14:27:05 +0000339** if flag==0 then
340** move the row2 cursor to a null row
341** goto start
342** fi
drhc27a1ce2002-06-14 20:58:45 +0000343** end
344**
drhe3184742002-06-19 14:27:05 +0000345** ORDER BY CLAUSE PROCESSING
346**
347** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
348** if there is one. If there is no ORDER BY clause or if this routine
349** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
350**
351** If an index can be used so that the natural output order of the table
352** scan is correct for the ORDER BY clause, then that index is used and
353** *ppOrderBy is set to NULL. This is an optimization that prevents an
354** unnecessary sort of the result set if an index appropriate for the
355** ORDER BY clause already exists.
356**
357** If the where clause loops cannot be arranged to provide the correct
358** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000359*/
danielk19774adee202004-05-08 08:23:19 +0000360WhereInfo *sqlite3WhereBegin(
drh75897232000-05-29 14:26:00 +0000361 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000362 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000363 Expr *pWhere, /* The WHERE clause */
drhe3184742002-06-19 14:27:05 +0000364 int pushKey, /* If TRUE, leave the table key on the stack */
365 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000366){
367 int i; /* Loop counter */
368 WhereInfo *pWInfo; /* Will become the return value of this function */
369 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000370 int brk, cont = 0; /* Addresses used during code generation */
drh75897232000-05-29 14:26:00 +0000371 int nExpr; /* Number of subexpressions in the WHERE clause */
372 int loopMask; /* One bit set for each outer loop */
danielk1977f7df9cc2004-06-16 12:02:47 +0000373 int haveKey = 0; /* True if KEY is on the stack */
drh6a3ea0e2003-05-02 14:32:12 +0000374 ExprMaskSet maskSet; /* The expression mask set */
drh8aff1012001-12-22 14:49:24 +0000375 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
376 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
377 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh83dcb1a2002-06-28 01:02:38 +0000378 ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
drh75897232000-05-29 14:26:00 +0000379
drhc27a1ce2002-06-14 20:58:45 +0000380 /* pushKey is only allowed if there is a single table (as in an INSERT or
381 ** UPDATE statement)
382 */
383 assert( pushKey==0 || pTabList->nSrc==1 );
drh83dcb1a2002-06-28 01:02:38 +0000384
385 /* Split the WHERE clause into separate subexpressions where each
386 ** subexpression is separated by an AND operator. If the aExpr[]
387 ** array fills up, the last entry might point to an expression which
388 ** contains additional unfactored AND operators.
389 */
drh6a3ea0e2003-05-02 14:32:12 +0000390 initMaskSet(&maskSet);
drh83dcb1a2002-06-28 01:02:38 +0000391 memset(aExpr, 0, sizeof(aExpr));
392 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
393 if( nExpr==ARRAYSIZE(aExpr) ){
danielk19774adee202004-05-08 08:23:19 +0000394 sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more "
drhf7a9e1a2004-02-22 18:40:56 +0000395 "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000396 return 0;
397 }
drhc27a1ce2002-06-14 20:58:45 +0000398
drh75897232000-05-29 14:26:00 +0000399 /* Allocate and initialize the WhereInfo structure that will become the
400 ** return value.
401 */
drhad3cab52002-05-24 02:04:32 +0000402 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000403 if( sqlite3_malloc_failed ){
drhdaffd0e2001-04-11 14:28:42 +0000404 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000405 return 0;
406 }
407 pWInfo->pParse = pParse;
408 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000409 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
danielk19774adee202004-05-08 08:23:19 +0000410 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000411
412 /* Special case: a WHERE clause that is constant. Evaluate the
413 ** expression and either jump over all of the code or fall thru.
414 */
danielk19774adee202004-05-08 08:23:19 +0000415 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
416 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000417 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000418 }
drh75897232000-05-29 14:26:00 +0000419
drh75897232000-05-29 14:26:00 +0000420 /* Analyze all of the subexpressions.
421 */
422 for(i=0; i<nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000423 exprAnalyze(&maskSet, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000424
425 /* If we are executing a trigger body, remove all references to
426 ** new.* and old.* tables from the prerequisite masks.
427 */
428 if( pParse->trigStack ){
429 int x;
430 if( (x = pParse->trigStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000431 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000432 aExpr[i].prereqRight &= mask;
433 aExpr[i].prereqLeft &= mask;
434 aExpr[i].prereqAll &= mask;
435 }
436 if( (x = pParse->trigStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000437 int mask = ~getMask(&maskSet, x);
drh1d1f3052002-05-21 13:18:25 +0000438 aExpr[i].prereqRight &= mask;
439 aExpr[i].prereqLeft &= mask;
440 aExpr[i].prereqAll &= mask;
441 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000442 }
drh75897232000-05-29 14:26:00 +0000443 }
444
drh75897232000-05-29 14:26:00 +0000445 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000446 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000447 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000448 ** loop.
449 **
450 ** If terms exist that use the ROWID of any table, then set the
451 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
452 ** to the index of the term containing the ROWID. We always prefer
453 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000454 ** index which requires reading an index first to get the rowid then
455 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000456 **
457 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000458 ** first 32 tables are candidates for indices. This is (again) due
459 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000460 */
461 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000462 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000463 int j;
drh6a3ea0e2003-05-02 14:32:12 +0000464 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
465 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
466 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000467 Index *pIdx;
468 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000469 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000470
drhc4a3c772001-04-04 11:48:57 +0000471 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000472 ** ROWID field of this table. For terms of the form ROWID==expr
473 ** set iDirectEq[i] to the index of the term. For terms of the
474 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
475 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000476 **
477 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000478 */
drhc8f8b632002-09-30 12:36:26 +0000479 pWInfo->a[i].iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000480 iDirectEq[i] = -1;
481 iDirectLt[i] = -1;
482 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000483 for(j=0; j<nExpr; j++){
drh6a3ea0e2003-05-02 14:32:12 +0000484 if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000485 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000486 switch( aExpr[j].p->op ){
drhd99f7062002-06-08 23:25:08 +0000487 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000488 case TK_EQ: iDirectEq[i] = j; break;
489 case TK_LE:
490 case TK_LT: iDirectLt[i] = j; break;
491 case TK_GE:
492 case TK_GT: iDirectGt[i] = j; break;
493 }
drhc4a3c772001-04-04 11:48:57 +0000494 }
drh6a3ea0e2003-05-02 14:32:12 +0000495 if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
drhc4a3c772001-04-04 11:48:57 +0000496 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000497 switch( aExpr[j].p->op ){
498 case TK_EQ: iDirectEq[i] = j; break;
499 case TK_LE:
500 case TK_LT: iDirectGt[i] = j; break;
501 case TK_GE:
502 case TK_GT: iDirectLt[i] = j; break;
503 }
drhc4a3c772001-04-04 11:48:57 +0000504 }
505 }
drh8aff1012001-12-22 14:49:24 +0000506 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000507 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000508 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000509 continue;
510 }
511
drh75897232000-05-29 14:26:00 +0000512 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000513 ** the "best" index. pBestIdx is left set to NULL if no indices
514 ** are usable.
drh75897232000-05-29 14:26:00 +0000515 **
drh487ab3c2001-11-08 00:45:21 +0000516 ** The best index is determined as follows. For each of the
517 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000518 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000519 ** constrained by an inequality. Add 1 if for an "x<..." constraint
520 ** and add 2 for an "x>..." constraint. Chose the index that
521 ** gives the best score.
522 **
523 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000524 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000525 ** then all constraints are equalities. If score&1 is not 0 then
526 ** there is an inequality used as a termination key. (ex: "x<...")
527 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000528 ** start key. (ex: "x>..."). A score or 4 is the special case
529 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000530 **
drhc27a1ce2002-06-14 20:58:45 +0000531 ** The IN operator (as in "<expr> IN (...)") is treated the same as
532 ** an equality comparison except that it can only be used on the
533 ** left-most column of an index and other terms of the WHERE clause
534 ** cannot be used in conjunction with the IN operator to help satisfy
535 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000536 */
537 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000538 int eqMask = 0; /* Index columns covered by an x=... term */
539 int ltMask = 0; /* Index columns covered by an x<... term */
540 int gtMask = 0; /* Index columns covered by an x>... term */
541 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000542 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000543
drh487ab3c2001-11-08 00:45:21 +0000544 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000545 for(j=0; j<nExpr; j++){
danielk1977d2b65b92004-06-10 10:51:47 +0000546 CollSeq *pColl = sqlite3ExprCollSeq(pParse, aExpr[j].p->pLeft);
danielk19770202b292004-06-09 09:55:16 +0000547 if( !pColl && aExpr[j].p->pRight ){
danielk1977d2b65b92004-06-10 10:51:47 +0000548 pColl = sqlite3ExprCollSeq(pParse, aExpr[j].p->pRight);
danielk19770202b292004-06-09 09:55:16 +0000549 }
550 if( !pColl ){
551 pColl = pParse->db->pDfltColl;
552 }
drh6a3ea0e2003-05-02 14:32:12 +0000553 if( aExpr[j].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000554 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000555 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000556 int k;
danielk1977e014a832004-05-17 10:48:57 +0000557 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000558 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000559 /* If the collating sequences or affinities don't match,
560 ** ignore this index. */
561 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
562 if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
563 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000564 switch( aExpr[j].p->op ){
drh48185c12002-06-09 01:55:20 +0000565 case TK_IN: {
566 if( k==0 ) inMask |= 1;
567 break;
568 }
drh487ab3c2001-11-08 00:45:21 +0000569 case TK_EQ: {
570 eqMask |= 1<<k;
571 break;
572 }
573 case TK_LE:
574 case TK_LT: {
575 ltMask |= 1<<k;
576 break;
577 }
578 case TK_GE:
579 case TK_GT: {
580 gtMask |= 1<<k;
581 break;
582 }
583 default: {
584 /* CANT_HAPPEN */
585 assert( 0 );
586 break;
587 }
588 }
drh75897232000-05-29 14:26:00 +0000589 break;
590 }
591 }
592 }
drh6a3ea0e2003-05-02 14:32:12 +0000593 if( aExpr[j].idxRight==iCur
drh75897232000-05-29 14:26:00 +0000594 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000595 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000596 int k;
danielk1977e014a832004-05-17 10:48:57 +0000597 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000598 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000599 /* If the collating sequences or affinities don't match,
600 ** ignore this index. */
601 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
602 if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
603 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000604 switch( aExpr[j].p->op ){
605 case TK_EQ: {
606 eqMask |= 1<<k;
607 break;
608 }
609 case TK_LE:
610 case TK_LT: {
611 gtMask |= 1<<k;
612 break;
613 }
614 case TK_GE:
615 case TK_GT: {
616 ltMask |= 1<<k;
617 break;
618 }
619 default: {
620 /* CANT_HAPPEN */
621 assert( 0 );
622 break;
623 }
624 }
drh75897232000-05-29 14:26:00 +0000625 break;
626 }
627 }
628 }
629 }
drhc045ec52002-12-04 20:01:06 +0000630
631 /* The following loop ends with nEq set to the number of columns
632 ** on the left of the index with == constraints.
633 */
drh487ab3c2001-11-08 00:45:21 +0000634 for(nEq=0; nEq<pIdx->nColumn; nEq++){
635 m = (1<<(nEq+1))-1;
636 if( (m & eqMask)!=m ) break;
637 }
drhc045ec52002-12-04 20:01:06 +0000638 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000639 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000640 if( m & ltMask ) score++; /* Increase score for a < constraint */
641 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
642 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000643 if( score>bestScore ){
644 pBestIdx = pIdx;
645 bestScore = score;
drh75897232000-05-29 14:26:00 +0000646 }
647 }
drh6b563442001-11-07 16:48:26 +0000648 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000649 pWInfo->a[i].score = bestScore;
drhc045ec52002-12-04 20:01:06 +0000650 pWInfo->a[i].bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000651 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000652 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000653 pWInfo->a[i].iCur = pParse->nTab++;
654 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000655 }
drh75897232000-05-29 14:26:00 +0000656 }
657
drhe3184742002-06-19 14:27:05 +0000658 /* Check to see if the ORDER BY clause is or can be satisfied by the
659 ** use of an index on the first table.
660 */
661 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
662 Index *pSortIdx;
663 Index *pIdx;
664 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000665 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000666
667 pTab = pTabList->a[0].pTab;
668 pIdx = pWInfo->a[0].pIdx;
669 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000670 /* If there is already an IN index on the left-most table,
671 ** it will not give the correct sort order.
672 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000673 */
674 pSortIdx = 0;
675 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
676 /* If the left-most column is accessed using its ROWID, then do
677 ** not try to sort by index.
678 */
679 pSortIdx = 0;
680 }else{
drhdd4852c2002-12-04 21:50:16 +0000681 int nEqCol = (pWInfo->a[0].score+4)/8;
danielk1977d2b65b92004-06-10 10:51:47 +0000682 pSortIdx = findSortingIndex(pParse, pTab, pTabList->a[0].iCursor,
drh6a3ea0e2003-05-02 14:32:12 +0000683 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000684 }
685 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
686 if( pIdx==0 ){
687 pWInfo->a[0].pIdx = pSortIdx;
688 pWInfo->a[0].iCur = pParse->nTab++;
689 pWInfo->peakNTab = pParse->nTab;
690 }
drhc045ec52002-12-04 20:01:06 +0000691 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000692 *ppOrderBy = 0;
693 }
694 }
695
drh6b563442001-11-07 16:48:26 +0000696 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000697 */
drhc275b4e2004-07-19 17:25:24 +0000698 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
drhad3cab52002-05-24 02:04:32 +0000699 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000700 Table *pTab;
drh701a0ae2004-02-22 20:05:00 +0000701 Index *pIx;
drhf57b3392001-10-08 13:22:32 +0000702
703 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000704 if( pTab->isTransient || pTab->pSelect ) continue;
danielk19774adee202004-05-08 08:23:19 +0000705 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000706 sqlite3VdbeAddOp(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum);
danielk1977b4964b72004-05-18 01:23:38 +0000707 sqlite3VdbeAddOp(v, OP_SetNumColumns, pTabList->a[i].iCursor, pTab->nCol);
danielk1977f9d19a62004-06-14 08:26:35 +0000708 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh701a0ae2004-02-22 20:05:00 +0000709 if( (pIx = pWInfo->a[i].pIdx)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000710 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000711 sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum,
712 (char*)&pIx->keyInfo, P3_KEYINFO);
drh75897232000-05-29 14:26:00 +0000713 }
714 }
715
716 /* Generate the code to do the search
717 */
drh75897232000-05-29 14:26:00 +0000718 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000719 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000720 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000721 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000722 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000723 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000724
drhad2d8302002-05-24 20:31:36 +0000725 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000726 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000727 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000728 */
729 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
730 if( !pParse->nMem ) pParse->nMem++;
731 pLevel->iLeftJoin = pParse->nMem++;
danielk19770f69c1e2004-05-29 11:24:50 +0000732 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad2d8302002-05-24 20:31:36 +0000734 }
735
drh8aff1012001-12-22 14:49:24 +0000736 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000737 pLevel->inOp = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000738 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
739 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000740 ** equality comparison against the ROWID field. Or
741 ** we reference multiple rows using a "rowid IN (...)"
742 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000743 */
drh8aff1012001-12-22 14:49:24 +0000744 k = iDirectEq[i];
745 assert( k<nExpr );
746 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000747 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
danielk19774adee202004-05-08 08:23:19 +0000748 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh6a3ea0e2003-05-02 14:32:12 +0000749 if( aExpr[k].idxLeft==iCur ){
drhd99f7062002-06-08 23:25:08 +0000750 Expr *pX = aExpr[k].p;
751 if( pX->op!=TK_IN ){
danielk19774adee202004-05-08 08:23:19 +0000752 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drhd99f7062002-06-08 23:25:08 +0000753 }else{
danielk19774adee202004-05-08 08:23:19 +0000754 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
755 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000756 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000757 pLevel->inOp = OP_Next;
758 pLevel->inP1 = pX->iTable;
759 }
drh8aff1012001-12-22 14:49:24 +0000760 }else{
danielk19774adee202004-05-08 08:23:19 +0000761 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000762 }
drh8aff1012001-12-22 14:49:24 +0000763 aExpr[k].p = 0;
danielk19774adee202004-05-08 08:23:19 +0000764 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
765 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000766 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +0000767 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000768 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000769 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000770 /* Case 2: There is an index and all terms of the WHERE clause that
771 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000772 */
drh6b563442001-11-07 16:48:26 +0000773 int start;
drhc045ec52002-12-04 20:01:06 +0000774 int nColumn = (pLevel->score+4)/8;
danielk19774adee202004-05-08 08:23:19 +0000775 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000776
777 /* For each column of the index, find the term of the WHERE clause that
778 ** constraints that column. If the WHERE clause term is X=expr, then
779 ** evaluation expr and leave the result on the stack */
drh487ab3c2001-11-08 00:45:21 +0000780 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000781 for(k=0; k<nExpr; k++){
drhd99f7062002-06-08 23:25:08 +0000782 Expr *pX = aExpr[k].p;
783 if( pX==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000784 if( aExpr[k].idxLeft==iCur
drh75897232000-05-29 14:26:00 +0000785 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drhd99f7062002-06-08 23:25:08 +0000786 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000787 ){
danielk1977e014a832004-05-17 10:48:57 +0000788 char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
789 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
790 if( pX->op==TK_EQ ){
791 sqlite3ExprCode(pParse, pX->pRight);
792 aExpr[k].p = 0;
793 break;
794 }
795 if( pX->op==TK_IN && nColumn==1 ){
danielk19774adee202004-05-08 08:23:19 +0000796 sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
797 sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000798 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
drhd99f7062002-06-08 23:25:08 +0000799 pLevel->inOp = OP_Next;
800 pLevel->inP1 = pX->iTable;
danielk1977e014a832004-05-17 10:48:57 +0000801 aExpr[k].p = 0;
802 break;
drhd99f7062002-06-08 23:25:08 +0000803 }
drhd99f7062002-06-08 23:25:08 +0000804 }
drh75897232000-05-29 14:26:00 +0000805 }
drh6a3ea0e2003-05-02 14:32:12 +0000806 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000807 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000808 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000809 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000810 ){
danielk1977e014a832004-05-17 10:48:57 +0000811 char idxaff = pIdx->pTable->aCol[pX->pRight->iColumn].affinity;
812 if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
813 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
814 aExpr[k].p = 0;
815 break;
816 }
drh75897232000-05-29 14:26:00 +0000817 }
818 }
819 }
drh6b563442001-11-07 16:48:26 +0000820 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000821 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000822
823 /* At this point, the top nColumn elements of the stack are the
824 ** values of columns in the index we are using. Check to see if
825 ** any of these values are NULL. If they are, they will not match any
826 ** index entries, so skip immediately to the next iteration of the loop */
danielk19774adee202004-05-08 08:23:19 +0000827 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
828 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
829 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
drh772ae622004-05-19 13:13:08 +0000830
831 /* Generate an index key from the top nColumn elements of the stack */
danielk1977ededfd52004-06-17 07:53:01 +0000832 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000833 sqlite3IndexAffinityStr(v, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +0000834 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drh772ae622004-05-19 13:13:08 +0000835
drh772ae622004-05-19 13:13:08 +0000836 /* Generate code (1) to move to the first matching element of the table.
837 ** Then generate code (2) that jumps to "brk" after the cursor is past
838 ** the last matching element of the table. The code (1) is executed
839 ** once to initialize the search, the code (2) is executed before each
840 ** iteration of the scan to see if the scan has finished. */
drhc045ec52002-12-04 20:01:06 +0000841 if( pLevel->bRev ){
842 /* Scan in reverse order */
drh7cf6e4d2004-05-19 14:56:55 +0000843 sqlite3VdbeAddOp(v, OP_MoveLe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000844 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
845 sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000846 pLevel->op = OP_Prev;
847 }else{
848 /* Scan in the forward order */
drh7cf6e4d2004-05-19 14:56:55 +0000849 sqlite3VdbeAddOp(v, OP_MoveGe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000850 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drhfec19aa2004-05-19 20:41:03 +0000851 sqlite3VdbeOp3(v, OP_IdxGE, pLevel->iCur, brk, "+", P3_STATIC);
drhc045ec52002-12-04 20:01:06 +0000852 pLevel->op = OP_Next;
853 }
danielk19774adee202004-05-08 08:23:19 +0000854 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
855 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
856 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000857 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000858 haveKey = 1;
859 }else{
drh7cf6e4d2004-05-19 14:56:55 +0000860 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +0000861 haveKey = 0;
862 }
drh6b563442001-11-07 16:48:26 +0000863 pLevel->p1 = pLevel->iCur;
864 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000865 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
866 /* Case 3: We have an inequality comparison against the ROWID field.
867 */
868 int testOp = OP_Noop;
869 int start;
870
danielk19774adee202004-05-08 08:23:19 +0000871 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
872 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000873 if( iDirectGt[i]>=0 ){
874 k = iDirectGt[i];
875 assert( k<nExpr );
876 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000877 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
878 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000879 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000880 }else{
danielk19774adee202004-05-08 08:23:19 +0000881 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000882 }
danielk19774adee202004-05-08 08:23:19 +0000883 sqlite3VdbeAddOp(v, OP_ForceInt,
drh751f4122004-01-14 21:59:22 +0000884 aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
drh7cf6e4d2004-05-19 14:56:55 +0000885 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000886 aExpr[k].p = 0;
887 }else{
danielk19774adee202004-05-08 08:23:19 +0000888 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000889 }
890 if( iDirectLt[i]>=0 ){
891 k = iDirectLt[i];
892 assert( k<nExpr );
893 assert( aExpr[k].p!=0 );
drh6a3ea0e2003-05-02 14:32:12 +0000894 assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
895 if( aExpr[k].idxLeft==iCur ){
danielk19774adee202004-05-08 08:23:19 +0000896 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh8aff1012001-12-22 14:49:24 +0000897 }else{
danielk19774adee202004-05-08 08:23:19 +0000898 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh8aff1012001-12-22 14:49:24 +0000899 }
danielk19774adee202004-05-08 08:23:19 +0000900 /* sqlite3VdbeAddOp(v, OP_MustBeInt, 0, sqlite3VdbeCurrentAddr(v)+1); */
drh8aff1012001-12-22 14:49:24 +0000901 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000902 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh8aff1012001-12-22 14:49:24 +0000903 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
904 testOp = OP_Ge;
905 }else{
906 testOp = OP_Gt;
907 }
908 aExpr[k].p = 0;
909 }
danielk19774adee202004-05-08 08:23:19 +0000910 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000911 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000912 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000913 pLevel->p2 = start;
914 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +0000915 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
916 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
917 sqlite3VdbeAddOp(v, testOp, 0, brk);
drh8aff1012001-12-22 14:49:24 +0000918 }
919 haveKey = 0;
920 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000921 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000922 ** scan of the entire database table.
923 */
924 int start;
925
danielk19774adee202004-05-08 08:23:19 +0000926 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
927 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
928 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
929 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000930 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000931 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000932 pLevel->p2 = start;
933 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000934 }else{
drhc27a1ce2002-06-14 20:58:45 +0000935 /* Case 5: The WHERE clause term that refers to the right-most
936 ** column of the index is an inequality. For example, if
937 ** the index is on (x,y,z) and the WHERE clause is of the
938 ** form "x=5 AND y<10" then this case is used. Only the
939 ** right-most column can be an inequality - the rest must
940 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000941 **
942 ** This case is also used when there are no WHERE clause
943 ** constraints but an index is selected anyway, in order
944 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000945 */
946 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000947 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000948 int start;
danielk1977f7df9cc2004-06-16 12:02:47 +0000949 int leFlag=0, geFlag=0;
drh487ab3c2001-11-08 00:45:21 +0000950 int testOp;
951
952 /* Evaluate the equality constraints
953 */
954 for(j=0; j<nEqColumn; j++){
955 for(k=0; k<nExpr; k++){
956 if( aExpr[k].p==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +0000957 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +0000958 && aExpr[k].p->op==TK_EQ
959 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
960 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
961 ){
danielk19774adee202004-05-08 08:23:19 +0000962 sqlite3ExprCode(pParse, aExpr[k].p->pRight);
drh143f3c42004-01-07 20:37:52 +0000963 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000964 break;
965 }
drh6a3ea0e2003-05-02 14:32:12 +0000966 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +0000967 && aExpr[k].p->op==TK_EQ
968 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
969 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
970 ){
danielk19774adee202004-05-08 08:23:19 +0000971 sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
drh143f3c42004-01-07 20:37:52 +0000972 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +0000973 break;
974 }
975 }
976 }
977
drhc27a1ce2002-06-14 20:58:45 +0000978 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000979 ** used twice: once to make the termination key and once to make the
980 ** start key.
981 */
982 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +0000983 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +0000984 }
985
drhc045ec52002-12-04 20:01:06 +0000986 /* Labels for the beginning and end of the loop
987 */
danielk19774adee202004-05-08 08:23:19 +0000988 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
989 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +0000990
drh487ab3c2001-11-08 00:45:21 +0000991 /* Generate the termination key. This is the key value that
992 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +0000993 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +0000994 **
995 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
996 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +0000997 */
998 if( (score & 1)!=0 ){
999 for(k=0; k<nExpr; k++){
1000 Expr *pExpr = aExpr[k].p;
1001 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001002 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001003 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1004 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1005 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1006 ){
danielk19774adee202004-05-08 08:23:19 +00001007 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +00001008 leFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001009 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001010 break;
1011 }
drh6a3ea0e2003-05-02 14:32:12 +00001012 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001013 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1014 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1015 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1016 ){
danielk19774adee202004-05-08 08:23:19 +00001017 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +00001018 leFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001019 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001020 break;
1021 }
1022 }
1023 testOp = OP_IdxGE;
1024 }else{
1025 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1026 leFlag = 1;
1027 }
1028 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +00001029 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +00001030 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001031 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1032 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1033 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
danielk1977ededfd52004-06-17 07:53:01 +00001034 sqlite3VdbeAddOp(v, OP_MakeRecord, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001035 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001036 if( pLevel->bRev ){
drh7cf6e4d2004-05-19 14:56:55 +00001037 int op = leFlag ? OP_MoveLe : OP_MoveLt;
1038 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001039 }else{
danielk19774adee202004-05-08 08:23:19 +00001040 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001041 }
1042 }else if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001043 sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001044 }
1045
1046 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001047 ** bound on the search. There is no start key if there are no
1048 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001049 ** that case, generate a "Rewind" instruction in place of the
1050 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001051 **
1052 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1053 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001054 */
1055 if( (score & 2)!=0 ){
1056 for(k=0; k<nExpr; k++){
1057 Expr *pExpr = aExpr[k].p;
1058 if( pExpr==0 ) continue;
drh6a3ea0e2003-05-02 14:32:12 +00001059 if( aExpr[k].idxLeft==iCur
drh487ab3c2001-11-08 00:45:21 +00001060 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1061 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1062 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1063 ){
danielk19774adee202004-05-08 08:23:19 +00001064 sqlite3ExprCode(pParse, pExpr->pRight);
drh487ab3c2001-11-08 00:45:21 +00001065 geFlag = pExpr->op==TK_GE;
drh143f3c42004-01-07 20:37:52 +00001066 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001067 break;
1068 }
drh6a3ea0e2003-05-02 14:32:12 +00001069 if( aExpr[k].idxRight==iCur
drh487ab3c2001-11-08 00:45:21 +00001070 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1071 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1072 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1073 ){
danielk19774adee202004-05-08 08:23:19 +00001074 sqlite3ExprCode(pParse, pExpr->pLeft);
drh487ab3c2001-11-08 00:45:21 +00001075 geFlag = pExpr->op==TK_LE;
drh143f3c42004-01-07 20:37:52 +00001076 aExpr[k].p = 0;
drh487ab3c2001-11-08 00:45:21 +00001077 break;
1078 }
1079 }
drh7900ead2001-11-12 13:51:43 +00001080 }else{
1081 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001082 }
drh487ab3c2001-11-08 00:45:21 +00001083 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001084 int nCol = nEqColumn + ((score&2)!=0);
danielk19774adee202004-05-08 08:23:19 +00001085 sqlite3VdbeAddOp(v, OP_NotNull, -nCol, sqlite3VdbeCurrentAddr(v)+3);
1086 sqlite3VdbeAddOp(v, OP_Pop, nCol, 0);
1087 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
danielk1977ededfd52004-06-17 07:53:01 +00001088 sqlite3VdbeAddOp(v, OP_MakeRecord, nCol, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001089 sqlite3IndexAffinityStr(v, pIdx);
drhc045ec52002-12-04 20:01:06 +00001090 if( pLevel->bRev ){
1091 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001092 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001093 testOp = OP_IdxLT;
1094 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001095 int op = geFlag ? OP_MoveGe : OP_MoveGt;
1096 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001097 }
1098 }else if( pLevel->bRev ){
1099 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001100 }else{
danielk19774adee202004-05-08 08:23:19 +00001101 sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001102 }
1103
1104 /* Generate the the top of the loop. If there is a termination
1105 ** key we have to test for that key and abort at the top of the
1106 ** loop.
1107 */
danielk19774adee202004-05-08 08:23:19 +00001108 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001109 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001110 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1111 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001112 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1113 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1114 }
drh487ab3c2001-11-08 00:45:21 +00001115 }
danielk19774adee202004-05-08 08:23:19 +00001116 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1117 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1118 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001119 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001120 haveKey = 1;
1121 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001122 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001123 haveKey = 0;
1124 }
1125
1126 /* Record the instruction used to terminate the loop.
1127 */
drhc045ec52002-12-04 20:01:06 +00001128 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001129 pLevel->p1 = pLevel->iCur;
1130 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001131 }
drh6a3ea0e2003-05-02 14:32:12 +00001132 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001133
1134 /* Insert code to test every subexpression that can be completely
1135 ** computed using the current set of tables.
1136 */
1137 for(j=0; j<nExpr; j++){
1138 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +00001139 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh1f162302002-10-27 19:35:33 +00001140 if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1141 continue;
1142 }
drh75897232000-05-29 14:26:00 +00001143 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001144 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001145 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +00001146 }
danielk19774adee202004-05-08 08:23:19 +00001147 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +00001148 aExpr[j].p = 0;
1149 }
1150 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001151
1152 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1153 ** at least one row of the right table has matched the left table.
1154 */
1155 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001156 pLevel->top = sqlite3VdbeCurrentAddr(v);
1157 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1158 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drh1cc093c2002-06-24 22:01:57 +00001159 for(j=0; j<nExpr; j++){
1160 if( aExpr[j].p==0 ) continue;
1161 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1162 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001163 /* Cannot happen. "haveKey" can only be true if pushKey is true
1164 ** an pushKey can only be true for DELETE and UPDATE and there are
1165 ** no outer joins with DELETE and UPDATE.
1166 */
drh1cc093c2002-06-24 22:01:57 +00001167 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001168 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001169 }
danielk19774adee202004-05-08 08:23:19 +00001170 sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh1cc093c2002-06-24 22:01:57 +00001171 aExpr[j].p = 0;
1172 }
drhad2d8302002-05-24 20:31:36 +00001173 }
drh75897232000-05-29 14:26:00 +00001174 }
1175 pWInfo->iContinue = cont;
1176 if( pushKey && !haveKey ){
danielk19774adee202004-05-08 08:23:19 +00001177 sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001178 }
drh6a3ea0e2003-05-02 14:32:12 +00001179 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001180 return pWInfo;
1181}
1182
1183/*
drhc27a1ce2002-06-14 20:58:45 +00001184** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001185** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001186*/
danielk19774adee202004-05-08 08:23:19 +00001187void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001188 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001189 int i;
drh6b563442001-11-07 16:48:26 +00001190 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001191 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001192
drhad3cab52002-05-24 02:04:32 +00001193 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001194 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001195 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001196 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001197 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001198 }
danielk19774adee202004-05-08 08:23:19 +00001199 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001200 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001201 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001202 }
drhad2d8302002-05-24 20:31:36 +00001203 if( pLevel->iLeftJoin ){
1204 int addr;
danielk19774adee202004-05-08 08:23:19 +00001205 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1206 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1207 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001208 if( pLevel->iCur>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001209 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001210 }
danielk19774adee202004-05-08 08:23:19 +00001211 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001212 }
drh19a775c2000-06-05 18:54:46 +00001213 }
danielk19774adee202004-05-08 08:23:19 +00001214 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001215 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001216 Table *pTab = pTabList->a[i].pTab;
1217 assert( pTab!=0 );
1218 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001219 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001220 sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001221 if( pLevel->pIdx!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001222 sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
drh6b563442001-11-07 16:48:26 +00001223 }
drh19a775c2000-06-05 18:54:46 +00001224 }
drh142e30d2002-08-28 03:00:58 +00001225#if 0 /* Never reuse a cursor */
drh832508b2002-03-02 17:04:07 +00001226 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1227 pWInfo->pParse->nTab = pWInfo->savedNTab;
1228 }
drh142e30d2002-08-28 03:00:58 +00001229#endif
drh75897232000-05-29 14:26:00 +00001230 sqliteFree(pWInfo);
1231 return;
1232}