blob: 0f14df9deefb9d5cf373d490555db443f432b567 [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
13** the WHERE clause of SQL statements. Also found here are subroutines
14** to generate VDBE code to evaluate expressions.
15**
drhf5905aa2002-05-26 20:54:33 +000016** $Id: where.c,v 1.48 2002/05/26 20:54:34 drh Exp $
drh75897232000-05-29 14:26:00 +000017*/
18#include "sqliteInt.h"
19
20/*
21** The query generator uses an array of instances of this structure to
22** help it analyze the subexpressions of the WHERE clause. Each WHERE
23** clause subexpression is separated from the others by an AND operator.
24*/
25typedef struct ExprInfo ExprInfo;
26struct ExprInfo {
27 Expr *p; /* Pointer to the subexpression */
28 int indexable; /* True if this subexprssion is usable by an index */
drh967e8b72000-06-21 13:59:10 +000029 int idxLeft; /* p->pLeft is a column in this table number. -1 if
30 ** p->pLeft is not the column of any table */
31 int idxRight; /* p->pRight is a column in this table number. -1 if
32 ** p->pRight is not the column of any table */
drh75897232000-05-29 14:26:00 +000033 unsigned prereqLeft; /* Tables referenced by p->pLeft */
34 unsigned prereqRight; /* Tables referenced by p->pRight */
drh3f6b5482002-04-02 13:26:10 +000035 unsigned prereqAll; /* Tables referenced by this expression in any way */
drh75897232000-05-29 14:26:00 +000036};
37
38/*
39** Determine the number of elements in an array.
40*/
41#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
42
43/*
44** This routine is used to divide the WHERE expression into subexpressions
45** separated by the AND operator.
46**
47** aSlot[] is an array of subexpressions structures.
48** There are nSlot spaces left in this array. This routine attempts to
49** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
50** The return value is the number of slots filled.
51*/
52static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
53 int cnt = 0;
54 if( pExpr==0 || nSlot<1 ) return 0;
55 if( nSlot==1 || pExpr->op!=TK_AND ){
56 aSlot[0].p = pExpr;
57 return 1;
58 }
59 if( pExpr->pLeft->op!=TK_AND ){
60 aSlot[0].p = pExpr->pLeft;
61 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
62 }else{
63 cnt = exprSplit(nSlot, aSlot, pExpr->pRight);
64 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft);
65 }
66 return cnt;
67}
68
69/*
70** This routine walks (recursively) an expression tree and generates
71** a bitmask indicating which tables are used in that expression
72** tree. Bit 0 of the mask is set if table 0 is used. But 1 is set
73** if table 1 is used. And so forth.
74**
75** In order for this routine to work, the calling function must have
76** previously invoked sqliteExprResolveIds() on the expression. See
77** the header comment on that routine for additional information.
drh19a775c2000-06-05 18:54:46 +000078**
79** "base" is the cursor number (the value of the iTable field) that
drh832508b2002-03-02 17:04:07 +000080** corresponds to the first entry in the table list.
drh75897232000-05-29 14:26:00 +000081*/
drh19a775c2000-06-05 18:54:46 +000082static int exprTableUsage(int base, Expr *p){
drh75897232000-05-29 14:26:00 +000083 unsigned int mask = 0;
84 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +000085 if( p->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +000086 return 1<< (p->iTable - base);
drh75897232000-05-29 14:26:00 +000087 }
88 if( p->pRight ){
drh19a775c2000-06-05 18:54:46 +000089 mask = exprTableUsage(base, p->pRight);
drh75897232000-05-29 14:26:00 +000090 }
91 if( p->pLeft ){
drh19a775c2000-06-05 18:54:46 +000092 mask |= exprTableUsage(base, p->pLeft);
drh75897232000-05-29 14:26:00 +000093 }
drhdd579122002-04-02 01:58:57 +000094 if( p->pList ){
95 int i;
96 for(i=0; i<p->pList->nExpr; i++){
97 mask |= exprTableUsage(base, p->pList->a[i].pExpr);
98 }
99 }
drh75897232000-05-29 14:26:00 +0000100 return mask;
101}
102
103/*
drh487ab3c2001-11-08 00:45:21 +0000104** Return TRUE if the given operator is one of the operators that is
105** allowed for an indexable WHERE clause. The allowed operators are
106** "=", "<", ">", "<=", and ">=".
107*/
108static int allowedOp(int op){
109 switch( op ){
110 case TK_LT:
111 case TK_LE:
112 case TK_GT:
113 case TK_GE:
114 case TK_EQ:
115 return 1;
116 default:
117 return 0;
118 }
119}
120
121/*
drh75897232000-05-29 14:26:00 +0000122** The input to this routine is an ExprInfo structure with only the
123** "p" field filled in. The job of this routine is to analyze the
124** subexpression and populate all the other fields of the ExprInfo
125** structure.
drh19a775c2000-06-05 18:54:46 +0000126**
127** "base" is the cursor number (the value of the iTable field) that
drh832508b2002-03-02 17:04:07 +0000128** corresponds to the first entry in the table list.
drh75897232000-05-29 14:26:00 +0000129*/
drh19a775c2000-06-05 18:54:46 +0000130static void exprAnalyze(int base, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000131 Expr *pExpr = pInfo->p;
drh19a775c2000-06-05 18:54:46 +0000132 pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
133 pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
drh3f6b5482002-04-02 13:26:10 +0000134 pInfo->prereqAll = exprTableUsage(base, pExpr);
drh75897232000-05-29 14:26:00 +0000135 pInfo->indexable = 0;
136 pInfo->idxLeft = -1;
137 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000138 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drh967e8b72000-06-21 13:59:10 +0000139 if( pExpr->pRight->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000140 pInfo->idxRight = pExpr->pRight->iTable - base;
drh75897232000-05-29 14:26:00 +0000141 pInfo->indexable = 1;
142 }
drh967e8b72000-06-21 13:59:10 +0000143 if( pExpr->pLeft->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000144 pInfo->idxLeft = pExpr->pLeft->iTable - base;
drh75897232000-05-29 14:26:00 +0000145 pInfo->indexable = 1;
146 }
147 }
148}
149
150/*
151** Generating the beginning of the loop used for WHERE clause processing.
152** The return value is a pointer to an (opaque) structure that contains
153** information needed to terminate the loop. Later, the calling routine
154** should invoke sqliteWhereEnd() with the return value of this function
155** in order to complete the WHERE clause processing.
156**
157** If an error occurs, this routine returns NULL.
158*/
159WhereInfo *sqliteWhereBegin(
160 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000161 int base, /* VDBE cursor index for left-most table in pTabList */
drhad3cab52002-05-24 02:04:32 +0000162 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000163 Expr *pWhere, /* The WHERE clause */
164 int pushKey /* If TRUE, leave the table key on the stack */
165){
166 int i; /* Loop counter */
167 WhereInfo *pWInfo; /* Will become the return value of this function */
168 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
169 int brk, cont; /* Addresses used during code generation */
170 int *aOrder; /* Order in which pTabList entries are searched */
171 int nExpr; /* Number of subexpressions in the WHERE clause */
172 int loopMask; /* One bit set for each outer loop */
173 int haveKey; /* True if KEY is on the stack */
drhc4a3c772001-04-04 11:48:57 +0000174 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh8aff1012001-12-22 14:49:24 +0000175 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
176 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
177 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh75897232000-05-29 14:26:00 +0000178 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
179
drh6b563442001-11-07 16:48:26 +0000180 /* Allocate space for aOrder[] and aiMem[]. */
drhad3cab52002-05-24 02:04:32 +0000181 aOrder = sqliteMalloc( sizeof(int) * pTabList->nSrc );
drh75897232000-05-29 14:26:00 +0000182
183 /* Allocate and initialize the WhereInfo structure that will become the
184 ** return value.
185 */
drhad3cab52002-05-24 02:04:32 +0000186 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
drhdaffd0e2001-04-11 14:28:42 +0000187 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000188 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000189 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000190 return 0;
191 }
192 pWInfo->pParse = pParse;
193 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000194 pWInfo->base = base;
195 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
drh08192d52002-04-30 19:20:28 +0000196 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
197
198 /* Special case: a WHERE clause that is constant. Evaluate the
199 ** expression and either jump over all of the code or fall thru.
200 */
201 if( pWhere && sqliteExprIsConstant(pWhere) ){
drhf5905aa2002-05-26 20:54:33 +0000202 sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drh08192d52002-04-30 19:20:28 +0000203 }
drh75897232000-05-29 14:26:00 +0000204
205 /* Split the WHERE clause into as many as 32 separate subexpressions
206 ** where each subexpression is separated by an AND operator. Any additional
207 ** subexpressions are attached in the aExpr[32] and will not enter
208 ** into the query optimizer computations. 32 is chosen as the cutoff
209 ** since that is the number of bits in an integer that we use for an
210 ** expression-used mask.
211 */
212 memset(aExpr, 0, sizeof(aExpr));
213 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
214
215 /* Analyze all of the subexpressions.
216 */
217 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000218 exprAnalyze(base, &aExpr[i]);
drh1d1f3052002-05-21 13:18:25 +0000219
220 /* If we are executing a trigger body, remove all references to
221 ** new.* and old.* tables from the prerequisite masks.
222 */
223 if( pParse->trigStack ){
224 int x;
225 if( (x = pParse->trigStack->newIdx) >= 0 ){
226 int mask = ~(1 << (x - base));
227 aExpr[i].prereqRight &= mask;
228 aExpr[i].prereqLeft &= mask;
229 aExpr[i].prereqAll &= mask;
230 }
231 if( (x = pParse->trigStack->oldIdx) >= 0 ){
232 int mask = ~(1 << (x - base));
233 aExpr[i].prereqRight &= mask;
234 aExpr[i].prereqLeft &= mask;
235 aExpr[i].prereqAll &= mask;
236 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000237 }
drh75897232000-05-29 14:26:00 +0000238 }
239
240 /* Figure out a good nesting order for the tables. aOrder[0] will
241 ** be the index in pTabList of the outermost table. aOrder[1] will
drhad3cab52002-05-24 02:04:32 +0000242 ** be the first nested loop and so on. aOrder[pTabList->nSrc-1] will
drh75897232000-05-29 14:26:00 +0000243 ** be the innermost loop.
244 **
drh1d1f3052002-05-21 13:18:25 +0000245 ** Someday we will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000246 ** for an effiecient query. But for now, just use whatever order the
247 ** tables appear in in the pTabList.
248 */
drhad3cab52002-05-24 02:04:32 +0000249 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000250 aOrder[i] = i;
251 }
252
253 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000254 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000255 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000256 ** loop.
257 **
258 ** If terms exist that use the ROWID of any table, then set the
259 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
260 ** to the index of the term containing the ROWID. We always prefer
261 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000262 ** index which requires reading an index first to get the rowid then
263 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000264 **
265 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000266 ** first 32 tables are candidates for indices. This is (again) due
267 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000268 */
269 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000270 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000271 int j;
drh75897232000-05-29 14:26:00 +0000272 int idx = aOrder[i];
273 Table *pTab = pTabList->a[idx].pTab;
274 Index *pIdx;
275 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000276 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000277
drhc4a3c772001-04-04 11:48:57 +0000278 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000279 ** ROWID field of this table. For terms of the form ROWID==expr
280 ** set iDirectEq[i] to the index of the term. For terms of the
281 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
282 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000283 */
drh8aff1012001-12-22 14:49:24 +0000284 iDirectEq[i] = -1;
285 iDirectLt[i] = -1;
286 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000287 for(j=0; j<nExpr; j++){
288 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
289 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000290 switch( aExpr[j].p->op ){
291 case TK_EQ: iDirectEq[i] = j; break;
292 case TK_LE:
293 case TK_LT: iDirectLt[i] = j; break;
294 case TK_GE:
295 case TK_GT: iDirectGt[i] = j; break;
296 }
drhc4a3c772001-04-04 11:48:57 +0000297 }
298 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
299 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000300 switch( aExpr[j].p->op ){
301 case TK_EQ: iDirectEq[i] = j; break;
302 case TK_LE:
303 case TK_LT: iDirectGt[i] = j; break;
304 case TK_GE:
305 case TK_GT: iDirectLt[i] = j; break;
306 }
drhc4a3c772001-04-04 11:48:57 +0000307 }
308 }
drh8aff1012001-12-22 14:49:24 +0000309 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000310 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000311 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000312 continue;
313 }
314
drh75897232000-05-29 14:26:00 +0000315 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000316 ** the "best" index. pBestIdx is left set to NULL if no indices
317 ** are usable.
drh75897232000-05-29 14:26:00 +0000318 **
drh487ab3c2001-11-08 00:45:21 +0000319 ** The best index is determined as follows. For each of the
320 ** left-most terms that is fixed by an equality operator, add
321 ** 4 to the score. The right-most term of the index may be
322 ** constrained by an inequality. Add 1 if for an "x<..." constraint
323 ** and add 2 for an "x>..." constraint. Chose the index that
324 ** gives the best score.
325 **
326 ** This scoring system is designed so that the score can later be
327 ** used to determine how the index is used. If the score&3 is 0
328 ** then all constraints are equalities. If score&1 is not 0 then
329 ** there is an inequality used as a termination key. (ex: "x<...")
330 ** If score&2 is not 0 then there is an inequality used as the
331 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000332 */
333 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000334 int eqMask = 0; /* Index columns covered by an x=... constraint */
335 int ltMask = 0; /* Index columns covered by an x<... constraint */
336 int gtMask = 0; /* Index columns covered by an x>... constraing */
337 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000338
drh74e24cd2002-01-09 03:19:59 +0000339 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000340 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000341 for(j=0; j<nExpr; j++){
342 if( aExpr[j].idxLeft==idx
343 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000344 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000345 int k;
drh967e8b72000-06-21 13:59:10 +0000346 for(k=0; k<pIdx->nColumn; k++){
347 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000348 switch( aExpr[j].p->op ){
349 case TK_EQ: {
350 eqMask |= 1<<k;
351 break;
352 }
353 case TK_LE:
354 case TK_LT: {
355 ltMask |= 1<<k;
356 break;
357 }
358 case TK_GE:
359 case TK_GT: {
360 gtMask |= 1<<k;
361 break;
362 }
363 default: {
364 /* CANT_HAPPEN */
365 assert( 0 );
366 break;
367 }
368 }
drh75897232000-05-29 14:26:00 +0000369 break;
370 }
371 }
372 }
373 if( aExpr[j].idxRight==idx
374 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000375 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000376 int k;
drh967e8b72000-06-21 13:59:10 +0000377 for(k=0; k<pIdx->nColumn; k++){
378 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000379 switch( aExpr[j].p->op ){
380 case TK_EQ: {
381 eqMask |= 1<<k;
382 break;
383 }
384 case TK_LE:
385 case TK_LT: {
386 gtMask |= 1<<k;
387 break;
388 }
389 case TK_GE:
390 case TK_GT: {
391 ltMask |= 1<<k;
392 break;
393 }
394 default: {
395 /* CANT_HAPPEN */
396 assert( 0 );
397 break;
398 }
399 }
drh75897232000-05-29 14:26:00 +0000400 break;
401 }
402 }
403 }
404 }
drh487ab3c2001-11-08 00:45:21 +0000405 for(nEq=0; nEq<pIdx->nColumn; nEq++){
406 m = (1<<(nEq+1))-1;
407 if( (m & eqMask)!=m ) break;
408 }
409 score = nEq*4;
410 m = 1<<nEq;
411 if( m & ltMask ) score++;
412 if( m & gtMask ) score+=2;
413 if( score>bestScore ){
414 pBestIdx = pIdx;
415 bestScore = score;
drh75897232000-05-29 14:26:00 +0000416 }
417 }
drh6b563442001-11-07 16:48:26 +0000418 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000419 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000420 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000421 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000422 pWInfo->a[i].iCur = pParse->nTab++;
423 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000424 }
drh75897232000-05-29 14:26:00 +0000425 }
426
drh6b563442001-11-07 16:48:26 +0000427 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000428 */
drhad3cab52002-05-24 02:04:32 +0000429 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000430 int openOp;
431 Table *pTab;
432
433 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000434 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000435 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000436 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
437 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000438 if( i==0 && !pParse->schemaVerified &&
439 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000440 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000441 pParse->schemaVerified = 1;
442 }
drh6b563442001-11-07 16:48:26 +0000443 if( pWInfo->a[i].pIdx!=0 ){
444 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
445 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000446 }
447 }
448
449 /* Generate the code to do the search
450 */
drh75897232000-05-29 14:26:00 +0000451 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000452 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000453 int j, k;
454 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000455 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000456 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000457
drhad2d8302002-05-24 20:31:36 +0000458 /* If this is the right table of a LEFT OUTER JOIN, allocate and
459 ** initialize a memory cell that record if this table matches any
460 ** row of the left table in the join.
461 */
462 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
463 if( !pParse->nMem ) pParse->nMem++;
464 pLevel->iLeftJoin = pParse->nMem++;
465 sqliteVdbeAddOp(v, OP_String, 0, 0);
466 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
467 }
468
drh8aff1012001-12-22 14:49:24 +0000469 pIdx = pLevel->pIdx;
470 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
471 /* Case 1: We can directly reference a single row using an
472 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000473 */
drh8aff1012001-12-22 14:49:24 +0000474 k = iDirectEq[i];
475 assert( k<nExpr );
476 assert( aExpr[k].p!=0 );
477 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
478 if( aExpr[k].idxLeft==idx ){
479 sqliteExprCode(pParse, aExpr[k].p->pRight);
480 }else{
481 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000482 }
drh8aff1012001-12-22 14:49:24 +0000483 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000484 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
485 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000486 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhad3cab52002-05-24 02:04:32 +0000487 if( i==pTabList->nSrc-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000488 /* Note: The OP_Dup below will cause the recno to be left on the
489 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000490 ** taken. This violates a general rule of the VDBE that you should
491 ** never leave values on the stack in order to avoid a stack overflow.
492 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000493 ** because the pushKey flag is only true for UPDATE and DELETE, not
494 ** for SELECT, and nested loops only occur on a SELECT.
495 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000496 */
drhc4a3c772001-04-04 11:48:57 +0000497 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000498 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000499 }else{
drhc4a3c772001-04-04 11:48:57 +0000500 haveKey = 0;
501 }
drh6b125452002-01-28 15:53:03 +0000502 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000503 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000504 }else if( pIdx!=0 && pLevel->score%4==0 ){
505 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000506 */
drh6b563442001-11-07 16:48:26 +0000507 int start;
drh487ab3c2001-11-08 00:45:21 +0000508 int testOp;
509 int nColumn = pLevel->score/4;
510 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000511 for(k=0; k<nExpr; k++){
512 if( aExpr[k].p==0 ) continue;
513 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000514 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000515 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000516 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000517 ){
518 sqliteExprCode(pParse, aExpr[k].p->pRight);
519 aExpr[k].p = 0;
520 break;
521 }
522 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000523 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000524 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000525 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000526 ){
527 sqliteExprCode(pParse, aExpr[k].p->pLeft);
528 aExpr[k].p = 0;
529 break;
530 }
531 }
532 }
drh6b563442001-11-07 16:48:26 +0000533 pLevel->iMem = pParse->nMem++;
534 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
535 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000536 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
537 if( nColumn==pIdx->nColumn ){
538 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
539 testOp = OP_IdxGT;
540 }else{
541 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
542 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
543 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
544 testOp = OP_IdxGE;
545 }
drh6b563442001-11-07 16:48:26 +0000546 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
547 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000548 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000549 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000550 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000551 haveKey = 1;
552 }else{
drh99fcd712001-10-13 01:06:47 +0000553 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000554 haveKey = 0;
555 }
drh6b563442001-11-07 16:48:26 +0000556 pLevel->op = OP_Next;
557 pLevel->p1 = pLevel->iCur;
558 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000559 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
560 /* Case 3: We have an inequality comparison against the ROWID field.
561 */
562 int testOp = OP_Noop;
563 int start;
564
565 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
566 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
567 if( iDirectGt[i]>=0 ){
568 k = iDirectGt[i];
569 assert( k<nExpr );
570 assert( aExpr[k].p!=0 );
571 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
572 if( aExpr[k].idxLeft==idx ){
573 sqliteExprCode(pParse, aExpr[k].p->pRight);
574 }else{
575 sqliteExprCode(pParse, aExpr[k].p->pLeft);
576 }
577 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
578 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
579 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
580 }
581 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
582 aExpr[k].p = 0;
583 }else{
584 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
585 }
586 if( iDirectLt[i]>=0 ){
587 k = iDirectLt[i];
588 assert( k<nExpr );
589 assert( aExpr[k].p!=0 );
590 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
591 if( aExpr[k].idxLeft==idx ){
592 sqliteExprCode(pParse, aExpr[k].p->pRight);
593 }else{
594 sqliteExprCode(pParse, aExpr[k].p->pLeft);
595 }
596 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
597 pLevel->iMem = pParse->nMem++;
598 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
599 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
600 testOp = OP_Ge;
601 }else{
602 testOp = OP_Gt;
603 }
604 aExpr[k].p = 0;
605 }
606 start = sqliteVdbeCurrentAddr(v);
607 pLevel->op = OP_Next;
608 pLevel->p1 = base+idx;
609 pLevel->p2 = start;
610 if( testOp!=OP_Noop ){
611 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
612 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
613 sqliteVdbeAddOp(v, testOp, 0, brk);
614 }
615 haveKey = 0;
616 }else if( pIdx==0 ){
617 /* Case 4: There was no usable index. We must do a complete
618 ** scan of the entire database table.
619 */
620 int start;
621
622 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
623 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
624 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
625 start = sqliteVdbeCurrentAddr(v);
626 pLevel->op = OP_Next;
627 pLevel->p1 = base+idx;
628 pLevel->p2 = start;
629 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000630 }else{
drhaacc5432002-01-06 17:07:40 +0000631 /* Case 5: The contraint on the right-most index field is
632 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000633 */
634 int score = pLevel->score;
635 int nEqColumn = score/4;
636 int start;
637 int leFlag, geFlag;
638 int testOp;
639
640 /* Evaluate the equality constraints
641 */
642 for(j=0; j<nEqColumn; j++){
643 for(k=0; k<nExpr; k++){
644 if( aExpr[k].p==0 ) continue;
645 if( aExpr[k].idxLeft==idx
646 && aExpr[k].p->op==TK_EQ
647 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
648 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
649 ){
650 sqliteExprCode(pParse, aExpr[k].p->pRight);
651 aExpr[k].p = 0;
652 break;
653 }
654 if( aExpr[k].idxRight==idx
655 && aExpr[k].p->op==TK_EQ
656 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
657 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
658 ){
659 sqliteExprCode(pParse, aExpr[k].p->pLeft);
660 aExpr[k].p = 0;
661 break;
662 }
663 }
664 }
665
666 /* Duplicate the equality contraint values because they will all be
667 ** used twice: once to make the termination key and once to make the
668 ** start key.
669 */
670 for(j=0; j<nEqColumn; j++){
671 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
672 }
673
674 /* Generate the termination key. This is the key value that
675 ** will end the search. There is no termination key if there
676 ** are no equality contraints and no "X<..." constraint.
677 */
678 if( (score & 1)!=0 ){
679 for(k=0; k<nExpr; k++){
680 Expr *pExpr = aExpr[k].p;
681 if( pExpr==0 ) continue;
682 if( aExpr[k].idxLeft==idx
683 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
684 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
685 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
686 ){
687 sqliteExprCode(pParse, pExpr->pRight);
688 leFlag = pExpr->op==TK_LE;
689 aExpr[k].p = 0;
690 break;
691 }
692 if( aExpr[k].idxRight==idx
693 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
694 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
695 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
696 ){
697 sqliteExprCode(pParse, pExpr->pLeft);
698 leFlag = pExpr->op==TK_GE;
699 aExpr[k].p = 0;
700 break;
701 }
702 }
703 testOp = OP_IdxGE;
704 }else{
705 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
706 leFlag = 1;
707 }
708 if( testOp!=OP_Noop ){
709 pLevel->iMem = pParse->nMem++;
710 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
711 if( leFlag ){
712 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
713 }
714 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
715 }
716
717 /* Generate the start key. This is the key that defines the lower
718 ** bound on the search. There is no start key if there are not
719 ** equality constraints and if there is no "X>..." constraint. In
720 ** that case, generate a "Rewind" instruction in place of the
721 ** start key search.
722 */
723 if( (score & 2)!=0 ){
724 for(k=0; k<nExpr; k++){
725 Expr *pExpr = aExpr[k].p;
726 if( pExpr==0 ) continue;
727 if( aExpr[k].idxLeft==idx
728 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
729 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
730 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
731 ){
732 sqliteExprCode(pParse, pExpr->pRight);
733 geFlag = pExpr->op==TK_GE;
734 aExpr[k].p = 0;
735 break;
736 }
737 if( aExpr[k].idxRight==idx
738 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
739 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
740 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
741 ){
742 sqliteExprCode(pParse, pExpr->pLeft);
743 geFlag = pExpr->op==TK_LE;
744 aExpr[k].p = 0;
745 break;
746 }
747 }
drh7900ead2001-11-12 13:51:43 +0000748 }else{
749 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000750 }
751 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
752 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
753 if( nEqColumn>0 || (score&2)!=0 ){
754 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
755 if( !geFlag ){
756 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
757 }
758 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
759 }else{
760 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
761 }
762
763 /* Generate the the top of the loop. If there is a termination
764 ** key we have to test for that key and abort at the top of the
765 ** loop.
766 */
767 start = sqliteVdbeCurrentAddr(v);
768 if( testOp!=OP_Noop ){
769 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
770 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
771 }
772 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000773 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +0000774 haveKey = 1;
775 }else{
776 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
777 haveKey = 0;
778 }
779
780 /* Record the instruction used to terminate the loop.
781 */
782 pLevel->op = OP_Next;
783 pLevel->p1 = pLevel->iCur;
784 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000785 }
786 loopMask |= 1<<idx;
787
788 /* Insert code to test every subexpression that can be completely
789 ** computed using the current set of tables.
790 */
791 for(j=0; j<nExpr; j++){
792 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +0000793 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh75897232000-05-29 14:26:00 +0000794 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000795 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000796 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000797 }
drhf5905aa2002-05-26 20:54:33 +0000798 sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
drh75897232000-05-29 14:26:00 +0000799 aExpr[j].p = 0;
800 }
801 brk = cont;
drhad2d8302002-05-24 20:31:36 +0000802
803 /* For a LEFT OUTER JOIN, generate code that will record the fact that
804 ** at least one row of the right table has matched the left table.
805 */
806 if( pLevel->iLeftJoin ){
807 pLevel->top = sqliteVdbeCurrentAddr(v);
808 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
809 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
810 }
drh75897232000-05-29 14:26:00 +0000811 }
812 pWInfo->iContinue = cont;
813 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000814 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000815 }
816 sqliteFree(aOrder);
817 return pWInfo;
818}
819
820/*
821** Generate the end of the WHERE loop.
822*/
823void sqliteWhereEnd(WhereInfo *pWInfo){
824 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000825 int i;
drh19a775c2000-06-05 18:54:46 +0000826 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000827 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +0000828 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000829
drhad3cab52002-05-24 02:04:32 +0000830 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000831 pLevel = &pWInfo->a[i];
832 sqliteVdbeResolveLabel(v, pLevel->cont);
833 if( pLevel->op!=OP_Noop ){
834 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000835 }
drh6b563442001-11-07 16:48:26 +0000836 sqliteVdbeResolveLabel(v, pLevel->brk);
drhad2d8302002-05-24 20:31:36 +0000837 if( pLevel->iLeftJoin ){
838 int addr;
839 addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
840 sqliteVdbeAddOp(v, OP_NotNull, 0, addr+4);
841 sqliteVdbeAddOp(v, OP_NullRow, base+i, 0);
842 sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
843 }
drh19a775c2000-06-05 18:54:46 +0000844 }
drh6b563442001-11-07 16:48:26 +0000845 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +0000846 for(i=0; i<pTabList->nSrc; i++){
drh22f70c32002-02-18 01:17:00 +0000847 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000848 pLevel = &pWInfo->a[i];
849 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
850 if( pLevel->pIdx!=0 ){
851 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
852 }
drh19a775c2000-06-05 18:54:46 +0000853 }
drh832508b2002-03-02 17:04:07 +0000854 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
855 pWInfo->pParse->nTab = pWInfo->savedNTab;
856 }
drh75897232000-05-29 14:26:00 +0000857 sqliteFree(pWInfo);
858 return;
859}