blob: 1bf6f25ee158c2729a8ced774b607a08bc799147 [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**
danielk1977c3f9bad2002-05-15 08:30:12 +000016** $Id: where.c,v 1.42 2002/05/15 08:30:14 danielk1977 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 */
drh75897232000-05-29 14:26:00 +0000162 IdList *pTabList, /* A list of all tables */
163 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[]. */
drh75897232000-05-29 14:26:00 +0000181 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
182
183 /* Allocate and initialize the WhereInfo structure that will become the
184 ** return value.
185 */
drh6b563442001-11-07 16:48:26 +0000186 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*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) ){
202 sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak);
203 }
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]);
danielk1977c3f9bad2002-05-15 08:30:12 +0000219 if (pParse->trigStack && pParse->trigStack->newIdx >= 0) {
220 aExpr[i].prereqRight =
221 aExpr[i].prereqRight & ~(1 << pParse->trigStack->newIdx - base);
222 aExpr[i].prereqLeft =
223 aExpr[i].prereqLeft & ~(1 << pParse->trigStack->newIdx - base);
224 aExpr[i].prereqAll =
225 aExpr[i].prereqAll & ~(1 << pParse->trigStack->newIdx - base);
226 }
227 if (pParse->trigStack && pParse->trigStack->oldIdx >= 0) {
228 aExpr[i].prereqRight =
229 aExpr[i].prereqRight & ~(1 << pParse->trigStack->oldIdx - base);
230 aExpr[i].prereqLeft =
231 aExpr[i].prereqLeft & ~(1 << pParse->trigStack->oldIdx - base);
232 aExpr[i].prereqAll =
233 aExpr[i].prereqAll & ~(1 << pParse->trigStack->oldIdx - base);
234 }
drh75897232000-05-29 14:26:00 +0000235 }
236
237 /* Figure out a good nesting order for the tables. aOrder[0] will
238 ** be the index in pTabList of the outermost table. aOrder[1] will
239 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
240 ** be the innermost loop.
241 **
drh7e391e12000-05-30 20:17:49 +0000242 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000243 ** for an effiecient query. But for now, just use whatever order the
244 ** tables appear in in the pTabList.
245 */
246 for(i=0; i<pTabList->nId; i++){
247 aOrder[i] = i;
248 }
249
250 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000251 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
252 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000253 ** loop.
254 **
255 ** If terms exist that use the ROWID of any table, then set the
256 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
257 ** to the index of the term containing the ROWID. We always prefer
258 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000259 ** index which requires reading an index first to get the rowid then
260 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000261 **
262 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000263 ** first 32 tables are candidates for indices. This is (again) due
264 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000265 */
266 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000267 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000268 int j;
drh75897232000-05-29 14:26:00 +0000269 int idx = aOrder[i];
270 Table *pTab = pTabList->a[idx].pTab;
271 Index *pIdx;
272 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000273 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000274
drhc4a3c772001-04-04 11:48:57 +0000275 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000276 ** ROWID field of this table. For terms of the form ROWID==expr
277 ** set iDirectEq[i] to the index of the term. For terms of the
278 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
279 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000280 */
drh8aff1012001-12-22 14:49:24 +0000281 iDirectEq[i] = -1;
282 iDirectLt[i] = -1;
283 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000284 for(j=0; j<nExpr; j++){
285 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
286 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000287 switch( aExpr[j].p->op ){
288 case TK_EQ: iDirectEq[i] = j; break;
289 case TK_LE:
290 case TK_LT: iDirectLt[i] = j; break;
291 case TK_GE:
292 case TK_GT: iDirectGt[i] = j; break;
293 }
drhc4a3c772001-04-04 11:48:57 +0000294 }
295 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
296 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000297 switch( aExpr[j].p->op ){
298 case TK_EQ: iDirectEq[i] = j; break;
299 case TK_LE:
300 case TK_LT: iDirectGt[i] = j; break;
301 case TK_GE:
302 case TK_GT: iDirectLt[i] = j; break;
303 }
drhc4a3c772001-04-04 11:48:57 +0000304 }
305 }
drh8aff1012001-12-22 14:49:24 +0000306 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000307 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000308 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000309 continue;
310 }
311
drh75897232000-05-29 14:26:00 +0000312 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000313 ** the "best" index. pBestIdx is left set to NULL if no indices
314 ** are usable.
drh75897232000-05-29 14:26:00 +0000315 **
drh487ab3c2001-11-08 00:45:21 +0000316 ** The best index is determined as follows. For each of the
317 ** left-most terms that is fixed by an equality operator, add
318 ** 4 to the score. The right-most term of the index may be
319 ** constrained by an inequality. Add 1 if for an "x<..." constraint
320 ** and add 2 for an "x>..." constraint. Chose the index that
321 ** gives the best score.
322 **
323 ** This scoring system is designed so that the score can later be
324 ** used to determine how the index is used. If the score&3 is 0
325 ** then all constraints are equalities. If score&1 is not 0 then
326 ** there is an inequality used as a termination key. (ex: "x<...")
327 ** If score&2 is not 0 then there is an inequality used as the
328 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000329 */
330 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000331 int eqMask = 0; /* Index columns covered by an x=... constraint */
332 int ltMask = 0; /* Index columns covered by an x<... constraint */
333 int gtMask = 0; /* Index columns covered by an x>... constraing */
334 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000335
drh74e24cd2002-01-09 03:19:59 +0000336 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000337 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000338 for(j=0; j<nExpr; j++){
339 if( aExpr[j].idxLeft==idx
340 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000341 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000342 int k;
drh967e8b72000-06-21 13:59:10 +0000343 for(k=0; k<pIdx->nColumn; k++){
344 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000345 switch( aExpr[j].p->op ){
346 case TK_EQ: {
347 eqMask |= 1<<k;
348 break;
349 }
350 case TK_LE:
351 case TK_LT: {
352 ltMask |= 1<<k;
353 break;
354 }
355 case TK_GE:
356 case TK_GT: {
357 gtMask |= 1<<k;
358 break;
359 }
360 default: {
361 /* CANT_HAPPEN */
362 assert( 0 );
363 break;
364 }
365 }
drh75897232000-05-29 14:26:00 +0000366 break;
367 }
368 }
369 }
370 if( aExpr[j].idxRight==idx
371 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000372 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000373 int k;
drh967e8b72000-06-21 13:59:10 +0000374 for(k=0; k<pIdx->nColumn; k++){
375 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000376 switch( aExpr[j].p->op ){
377 case TK_EQ: {
378 eqMask |= 1<<k;
379 break;
380 }
381 case TK_LE:
382 case TK_LT: {
383 gtMask |= 1<<k;
384 break;
385 }
386 case TK_GE:
387 case TK_GT: {
388 ltMask |= 1<<k;
389 break;
390 }
391 default: {
392 /* CANT_HAPPEN */
393 assert( 0 );
394 break;
395 }
396 }
drh75897232000-05-29 14:26:00 +0000397 break;
398 }
399 }
400 }
401 }
drh487ab3c2001-11-08 00:45:21 +0000402 for(nEq=0; nEq<pIdx->nColumn; nEq++){
403 m = (1<<(nEq+1))-1;
404 if( (m & eqMask)!=m ) break;
405 }
406 score = nEq*4;
407 m = 1<<nEq;
408 if( m & ltMask ) score++;
409 if( m & gtMask ) score+=2;
410 if( score>bestScore ){
411 pBestIdx = pIdx;
412 bestScore = score;
drh75897232000-05-29 14:26:00 +0000413 }
414 }
drh6b563442001-11-07 16:48:26 +0000415 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000416 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000417 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000418 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000419 pWInfo->a[i].iCur = pParse->nTab++;
420 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000421 }
drh75897232000-05-29 14:26:00 +0000422 }
423
drh6b563442001-11-07 16:48:26 +0000424 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000425 */
426 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000427 int openOp;
428 Table *pTab;
429
430 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000431 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000432 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000433 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
434 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000435 if( i==0 && !pParse->schemaVerified &&
436 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000437 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000438 pParse->schemaVerified = 1;
439 }
drh6b563442001-11-07 16:48:26 +0000440 if( pWInfo->a[i].pIdx!=0 ){
441 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
442 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000443 }
444 }
445
446 /* Generate the code to do the search
447 */
drh75897232000-05-29 14:26:00 +0000448 loopMask = 0;
449 for(i=0; i<pTabList->nId; i++){
450 int j, k;
451 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000452 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000453 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000454
drh8aff1012001-12-22 14:49:24 +0000455 pIdx = pLevel->pIdx;
456 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
457 /* Case 1: We can directly reference a single row using an
458 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000459 */
drh8aff1012001-12-22 14:49:24 +0000460 k = iDirectEq[i];
461 assert( k<nExpr );
462 assert( aExpr[k].p!=0 );
463 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
464 if( aExpr[k].idxLeft==idx ){
465 sqliteExprCode(pParse, aExpr[k].p->pRight);
466 }else{
467 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000468 }
drh8aff1012001-12-22 14:49:24 +0000469 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000470 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
471 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000472 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhc4a3c772001-04-04 11:48:57 +0000473 if( i==pTabList->nId-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000474 /* Note: The OP_Dup below will cause the recno to be left on the
475 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000476 ** taken. This violates a general rule of the VDBE that you should
477 ** never leave values on the stack in order to avoid a stack overflow.
478 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000479 ** because the pushKey flag is only true for UPDATE and DELETE, not
480 ** for SELECT, and nested loops only occur on a SELECT.
481 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000482 */
drhc4a3c772001-04-04 11:48:57 +0000483 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000484 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000485 }else{
drhc4a3c772001-04-04 11:48:57 +0000486 haveKey = 0;
487 }
drh6b125452002-01-28 15:53:03 +0000488 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000489 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000490 }else if( pIdx!=0 && pLevel->score%4==0 ){
491 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000492 */
drh6b563442001-11-07 16:48:26 +0000493 int start;
drh487ab3c2001-11-08 00:45:21 +0000494 int testOp;
495 int nColumn = pLevel->score/4;
496 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000497 for(k=0; k<nExpr; k++){
498 if( aExpr[k].p==0 ) continue;
499 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000500 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000501 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000502 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000503 ){
504 sqliteExprCode(pParse, aExpr[k].p->pRight);
505 aExpr[k].p = 0;
506 break;
507 }
508 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000509 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000510 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000511 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000512 ){
513 sqliteExprCode(pParse, aExpr[k].p->pLeft);
514 aExpr[k].p = 0;
515 break;
516 }
517 }
518 }
drh6b563442001-11-07 16:48:26 +0000519 pLevel->iMem = pParse->nMem++;
520 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
521 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000522 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
523 if( nColumn==pIdx->nColumn ){
524 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
525 testOp = OP_IdxGT;
526 }else{
527 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
528 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
529 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
530 testOp = OP_IdxGE;
531 }
drh6b563442001-11-07 16:48:26 +0000532 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
533 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000534 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000535 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000536 if( i==pTabList->nId-1 && pushKey ){
537 haveKey = 1;
538 }else{
drh99fcd712001-10-13 01:06:47 +0000539 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000540 haveKey = 0;
541 }
drh6b563442001-11-07 16:48:26 +0000542 pLevel->op = OP_Next;
543 pLevel->p1 = pLevel->iCur;
544 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000545 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
546 /* Case 3: We have an inequality comparison against the ROWID field.
547 */
548 int testOp = OP_Noop;
549 int start;
550
551 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
552 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
553 if( iDirectGt[i]>=0 ){
554 k = iDirectGt[i];
555 assert( k<nExpr );
556 assert( aExpr[k].p!=0 );
557 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
558 if( aExpr[k].idxLeft==idx ){
559 sqliteExprCode(pParse, aExpr[k].p->pRight);
560 }else{
561 sqliteExprCode(pParse, aExpr[k].p->pLeft);
562 }
563 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
564 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
565 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
566 }
567 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
568 aExpr[k].p = 0;
569 }else{
570 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
571 }
572 if( iDirectLt[i]>=0 ){
573 k = iDirectLt[i];
574 assert( k<nExpr );
575 assert( aExpr[k].p!=0 );
576 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
577 if( aExpr[k].idxLeft==idx ){
578 sqliteExprCode(pParse, aExpr[k].p->pRight);
579 }else{
580 sqliteExprCode(pParse, aExpr[k].p->pLeft);
581 }
582 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
583 pLevel->iMem = pParse->nMem++;
584 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
585 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
586 testOp = OP_Ge;
587 }else{
588 testOp = OP_Gt;
589 }
590 aExpr[k].p = 0;
591 }
592 start = sqliteVdbeCurrentAddr(v);
593 pLevel->op = OP_Next;
594 pLevel->p1 = base+idx;
595 pLevel->p2 = start;
596 if( testOp!=OP_Noop ){
597 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
598 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
599 sqliteVdbeAddOp(v, testOp, 0, brk);
600 }
601 haveKey = 0;
602 }else if( pIdx==0 ){
603 /* Case 4: There was no usable index. We must do a complete
604 ** scan of the entire database table.
605 */
606 int start;
607
608 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
609 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
610 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
611 start = sqliteVdbeCurrentAddr(v);
612 pLevel->op = OP_Next;
613 pLevel->p1 = base+idx;
614 pLevel->p2 = start;
615 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000616 }else{
drhaacc5432002-01-06 17:07:40 +0000617 /* Case 5: The contraint on the right-most index field is
618 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000619 */
620 int score = pLevel->score;
621 int nEqColumn = score/4;
622 int start;
623 int leFlag, geFlag;
624 int testOp;
625
626 /* Evaluate the equality constraints
627 */
628 for(j=0; j<nEqColumn; j++){
629 for(k=0; k<nExpr; k++){
630 if( aExpr[k].p==0 ) continue;
631 if( aExpr[k].idxLeft==idx
632 && aExpr[k].p->op==TK_EQ
633 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
634 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
635 ){
636 sqliteExprCode(pParse, aExpr[k].p->pRight);
637 aExpr[k].p = 0;
638 break;
639 }
640 if( aExpr[k].idxRight==idx
641 && aExpr[k].p->op==TK_EQ
642 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
643 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
644 ){
645 sqliteExprCode(pParse, aExpr[k].p->pLeft);
646 aExpr[k].p = 0;
647 break;
648 }
649 }
650 }
651
652 /* Duplicate the equality contraint values because they will all be
653 ** used twice: once to make the termination key and once to make the
654 ** start key.
655 */
656 for(j=0; j<nEqColumn; j++){
657 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
658 }
659
660 /* Generate the termination key. This is the key value that
661 ** will end the search. There is no termination key if there
662 ** are no equality contraints and no "X<..." constraint.
663 */
664 if( (score & 1)!=0 ){
665 for(k=0; k<nExpr; k++){
666 Expr *pExpr = aExpr[k].p;
667 if( pExpr==0 ) continue;
668 if( aExpr[k].idxLeft==idx
669 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
670 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
671 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
672 ){
673 sqliteExprCode(pParse, pExpr->pRight);
674 leFlag = pExpr->op==TK_LE;
675 aExpr[k].p = 0;
676 break;
677 }
678 if( aExpr[k].idxRight==idx
679 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
680 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
681 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
682 ){
683 sqliteExprCode(pParse, pExpr->pLeft);
684 leFlag = pExpr->op==TK_GE;
685 aExpr[k].p = 0;
686 break;
687 }
688 }
689 testOp = OP_IdxGE;
690 }else{
691 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
692 leFlag = 1;
693 }
694 if( testOp!=OP_Noop ){
695 pLevel->iMem = pParse->nMem++;
696 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
697 if( leFlag ){
698 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
699 }
700 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
701 }
702
703 /* Generate the start key. This is the key that defines the lower
704 ** bound on the search. There is no start key if there are not
705 ** equality constraints and if there is no "X>..." constraint. In
706 ** that case, generate a "Rewind" instruction in place of the
707 ** start key search.
708 */
709 if( (score & 2)!=0 ){
710 for(k=0; k<nExpr; k++){
711 Expr *pExpr = aExpr[k].p;
712 if( pExpr==0 ) continue;
713 if( aExpr[k].idxLeft==idx
714 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
715 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
716 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
717 ){
718 sqliteExprCode(pParse, pExpr->pRight);
719 geFlag = pExpr->op==TK_GE;
720 aExpr[k].p = 0;
721 break;
722 }
723 if( aExpr[k].idxRight==idx
724 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
725 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
726 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
727 ){
728 sqliteExprCode(pParse, pExpr->pLeft);
729 geFlag = pExpr->op==TK_LE;
730 aExpr[k].p = 0;
731 break;
732 }
733 }
drh7900ead2001-11-12 13:51:43 +0000734 }else{
735 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000736 }
737 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
738 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
739 if( nEqColumn>0 || (score&2)!=0 ){
740 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
741 if( !geFlag ){
742 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
743 }
744 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
745 }else{
746 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
747 }
748
749 /* Generate the the top of the loop. If there is a termination
750 ** key we have to test for that key and abort at the top of the
751 ** loop.
752 */
753 start = sqliteVdbeCurrentAddr(v);
754 if( testOp!=OP_Noop ){
755 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
756 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
757 }
758 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
759 if( i==pTabList->nId-1 && pushKey ){
760 haveKey = 1;
761 }else{
762 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
763 haveKey = 0;
764 }
765
766 /* Record the instruction used to terminate the loop.
767 */
768 pLevel->op = OP_Next;
769 pLevel->p1 = pLevel->iCur;
770 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000771 }
772 loopMask |= 1<<idx;
773
774 /* Insert code to test every subexpression that can be completely
775 ** computed using the current set of tables.
776 */
777 for(j=0; j<nExpr; j++){
778 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +0000779 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh75897232000-05-29 14:26:00 +0000780 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000781 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000782 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000783 }
784 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
785 aExpr[j].p = 0;
786 }
787 brk = cont;
788 }
789 pWInfo->iContinue = cont;
790 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000791 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000792 }
793 sqliteFree(aOrder);
794 return pWInfo;
795}
796
797/*
798** Generate the end of the WHERE loop.
799*/
800void sqliteWhereEnd(WhereInfo *pWInfo){
801 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000802 int i;
drh19a775c2000-06-05 18:54:46 +0000803 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000804 WhereLevel *pLevel;
drh22f70c32002-02-18 01:17:00 +0000805 IdList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000806
drh22f70c32002-02-18 01:17:00 +0000807 for(i=pTabList->nId-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000808 pLevel = &pWInfo->a[i];
809 sqliteVdbeResolveLabel(v, pLevel->cont);
810 if( pLevel->op!=OP_Noop ){
811 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000812 }
drh6b563442001-11-07 16:48:26 +0000813 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000814 }
drh6b563442001-11-07 16:48:26 +0000815 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drh22f70c32002-02-18 01:17:00 +0000816 for(i=0; i<pTabList->nId; i++){
817 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000818 pLevel = &pWInfo->a[i];
819 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
820 if( pLevel->pIdx!=0 ){
821 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
822 }
drh19a775c2000-06-05 18:54:46 +0000823 }
drh832508b2002-03-02 17:04:07 +0000824 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
825 pWInfo->pParse->nTab = pWInfo->savedNTab;
826 }
drh75897232000-05-29 14:26:00 +0000827 sqliteFree(pWInfo);
828 return;
829}