blob: e325af807ac194bfda646d3100df897b6abe4ccb [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**
drha76b5df2002-02-23 02:32:10 +000016** $Id: where.c,v 1.37 2002/02/23 02:32:10 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 */
35};
36
37/*
38** Determine the number of elements in an array.
39*/
40#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
41
42/*
43** This routine is used to divide the WHERE expression into subexpressions
44** separated by the AND operator.
45**
46** aSlot[] is an array of subexpressions structures.
47** There are nSlot spaces left in this array. This routine attempts to
48** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
49** The return value is the number of slots filled.
50*/
51static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
52 int cnt = 0;
53 if( pExpr==0 || nSlot<1 ) return 0;
54 if( nSlot==1 || pExpr->op!=TK_AND ){
55 aSlot[0].p = pExpr;
56 return 1;
57 }
58 if( pExpr->pLeft->op!=TK_AND ){
59 aSlot[0].p = pExpr->pLeft;
60 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
61 }else{
62 cnt = exprSplit(nSlot, aSlot, pExpr->pRight);
63 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft);
64 }
65 return cnt;
66}
67
68/*
69** This routine walks (recursively) an expression tree and generates
70** a bitmask indicating which tables are used in that expression
71** tree. Bit 0 of the mask is set if table 0 is used. But 1 is set
72** if table 1 is used. And so forth.
73**
74** In order for this routine to work, the calling function must have
75** previously invoked sqliteExprResolveIds() on the expression. See
76** the header comment on that routine for additional information.
drh19a775c2000-06-05 18:54:46 +000077**
78** "base" is the cursor number (the value of the iTable field) that
79** corresponds to the first entry in the table list. This is the
80** same as pParse->nTab.
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 }
94 return mask;
95}
96
97/*
drh487ab3c2001-11-08 00:45:21 +000098** Return TRUE if the given operator is one of the operators that is
99** allowed for an indexable WHERE clause. The allowed operators are
100** "=", "<", ">", "<=", and ">=".
101*/
102static int allowedOp(int op){
103 switch( op ){
104 case TK_LT:
105 case TK_LE:
106 case TK_GT:
107 case TK_GE:
108 case TK_EQ:
109 return 1;
110 default:
111 return 0;
112 }
113}
114
115/*
drh75897232000-05-29 14:26:00 +0000116** The input to this routine is an ExprInfo structure with only the
117** "p" field filled in. The job of this routine is to analyze the
118** subexpression and populate all the other fields of the ExprInfo
119** structure.
drh19a775c2000-06-05 18:54:46 +0000120**
121** "base" is the cursor number (the value of the iTable field) that
drh80ff32f2001-11-04 18:32:46 +0000122** corresponds to the first entry in the table list. This is the
drh19a775c2000-06-05 18:54:46 +0000123** same as pParse->nTab.
drh75897232000-05-29 14:26:00 +0000124*/
drh19a775c2000-06-05 18:54:46 +0000125static void exprAnalyze(int base, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000126 Expr *pExpr = pInfo->p;
drh19a775c2000-06-05 18:54:46 +0000127 pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
128 pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
drh75897232000-05-29 14:26:00 +0000129 pInfo->indexable = 0;
130 pInfo->idxLeft = -1;
131 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000132 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drh967e8b72000-06-21 13:59:10 +0000133 if( pExpr->pRight->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000134 pInfo->idxRight = pExpr->pRight->iTable - base;
drh75897232000-05-29 14:26:00 +0000135 pInfo->indexable = 1;
136 }
drh967e8b72000-06-21 13:59:10 +0000137 if( pExpr->pLeft->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000138 pInfo->idxLeft = pExpr->pLeft->iTable - base;
drh75897232000-05-29 14:26:00 +0000139 pInfo->indexable = 1;
140 }
141 }
142}
143
144/*
145** Generating the beginning of the loop used for WHERE clause processing.
146** The return value is a pointer to an (opaque) structure that contains
147** information needed to terminate the loop. Later, the calling routine
148** should invoke sqliteWhereEnd() with the return value of this function
149** in order to complete the WHERE clause processing.
150**
151** If an error occurs, this routine returns NULL.
152*/
153WhereInfo *sqliteWhereBegin(
154 Parse *pParse, /* The parser context */
155 IdList *pTabList, /* A list of all tables */
156 Expr *pWhere, /* The WHERE clause */
157 int pushKey /* If TRUE, leave the table key on the stack */
158){
159 int i; /* Loop counter */
160 WhereInfo *pWInfo; /* Will become the return value of this function */
161 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
162 int brk, cont; /* Addresses used during code generation */
163 int *aOrder; /* Order in which pTabList entries are searched */
164 int nExpr; /* Number of subexpressions in the WHERE clause */
165 int loopMask; /* One bit set for each outer loop */
166 int haveKey; /* True if KEY is on the stack */
drh19a775c2000-06-05 18:54:46 +0000167 int base; /* First available index for OP_Open opcodes */
drh6b563442001-11-07 16:48:26 +0000168 int nCur; /* Next unused cursor number */
drhc4a3c772001-04-04 11:48:57 +0000169 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh8aff1012001-12-22 14:49:24 +0000170 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
171 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
172 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh75897232000-05-29 14:26:00 +0000173 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
174
drh6b563442001-11-07 16:48:26 +0000175 /* Allocate space for aOrder[] and aiMem[]. */
drh75897232000-05-29 14:26:00 +0000176 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
177
178 /* Allocate and initialize the WhereInfo structure that will become the
179 ** return value.
180 */
drh6b563442001-11-07 16:48:26 +0000181 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) );
drhdaffd0e2001-04-11 14:28:42 +0000182 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000183 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000184 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000185 return 0;
186 }
187 pWInfo->pParse = pParse;
188 pWInfo->pTabList = pTabList;
drh19a775c2000-06-05 18:54:46 +0000189 base = pWInfo->base = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000190 nCur = base + pTabList->nId;
drh22f70c32002-02-18 01:17:00 +0000191 pParse->nTab += nCur*2;
drh75897232000-05-29 14:26:00 +0000192
193 /* Split the WHERE clause into as many as 32 separate subexpressions
194 ** where each subexpression is separated by an AND operator. Any additional
195 ** subexpressions are attached in the aExpr[32] and will not enter
196 ** into the query optimizer computations. 32 is chosen as the cutoff
197 ** since that is the number of bits in an integer that we use for an
198 ** expression-used mask.
199 */
200 memset(aExpr, 0, sizeof(aExpr));
201 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
202
203 /* Analyze all of the subexpressions.
204 */
205 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000206 exprAnalyze(base, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000207 }
208
209 /* Figure out a good nesting order for the tables. aOrder[0] will
210 ** be the index in pTabList of the outermost table. aOrder[1] will
211 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
212 ** be the innermost loop.
213 **
drh7e391e12000-05-30 20:17:49 +0000214 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000215 ** for an effiecient query. But for now, just use whatever order the
216 ** tables appear in in the pTabList.
217 */
218 for(i=0; i<pTabList->nId; i++){
219 aOrder[i] = i;
220 }
221
222 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000223 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
224 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000225 ** loop.
226 **
227 ** If terms exist that use the ROWID of any table, then set the
228 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
229 ** to the index of the term containing the ROWID. We always prefer
230 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000231 ** index which requires reading an index first to get the rowid then
232 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000233 **
234 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000235 ** first 32 tables are candidates for indices. This is (again) due
236 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000237 */
238 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000239 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000240 int j;
drh75897232000-05-29 14:26:00 +0000241 int idx = aOrder[i];
242 Table *pTab = pTabList->a[idx].pTab;
243 Index *pIdx;
244 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000245 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000246
drhc4a3c772001-04-04 11:48:57 +0000247 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000248 ** ROWID field of this table. For terms of the form ROWID==expr
249 ** set iDirectEq[i] to the index of the term. For terms of the
250 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
251 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000252 */
drh8aff1012001-12-22 14:49:24 +0000253 iDirectEq[i] = -1;
254 iDirectLt[i] = -1;
255 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000256 for(j=0; j<nExpr; j++){
257 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
258 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000259 switch( aExpr[j].p->op ){
260 case TK_EQ: iDirectEq[i] = j; break;
261 case TK_LE:
262 case TK_LT: iDirectLt[i] = j; break;
263 case TK_GE:
264 case TK_GT: iDirectGt[i] = j; break;
265 }
drhc4a3c772001-04-04 11:48:57 +0000266 }
267 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
268 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000269 switch( aExpr[j].p->op ){
270 case TK_EQ: iDirectEq[i] = j; break;
271 case TK_LE:
272 case TK_LT: iDirectGt[i] = j; break;
273 case TK_GE:
274 case TK_GT: iDirectLt[i] = j; break;
275 }
drhc4a3c772001-04-04 11:48:57 +0000276 }
277 }
drh8aff1012001-12-22 14:49:24 +0000278 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000279 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000280 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000281 continue;
282 }
283
drh75897232000-05-29 14:26:00 +0000284 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000285 ** the "best" index. pBestIdx is left set to NULL if no indices
286 ** are usable.
drh75897232000-05-29 14:26:00 +0000287 **
drh487ab3c2001-11-08 00:45:21 +0000288 ** The best index is determined as follows. For each of the
289 ** left-most terms that is fixed by an equality operator, add
290 ** 4 to the score. The right-most term of the index may be
291 ** constrained by an inequality. Add 1 if for an "x<..." constraint
292 ** and add 2 for an "x>..." constraint. Chose the index that
293 ** gives the best score.
294 **
295 ** This scoring system is designed so that the score can later be
296 ** used to determine how the index is used. If the score&3 is 0
297 ** then all constraints are equalities. If score&1 is not 0 then
298 ** there is an inequality used as a termination key. (ex: "x<...")
299 ** If score&2 is not 0 then there is an inequality used as the
300 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000301 */
302 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000303 int eqMask = 0; /* Index columns covered by an x=... constraint */
304 int ltMask = 0; /* Index columns covered by an x<... constraint */
305 int gtMask = 0; /* Index columns covered by an x>... constraing */
306 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000307
drh74e24cd2002-01-09 03:19:59 +0000308 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000309 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000310 for(j=0; j<nExpr; j++){
311 if( aExpr[j].idxLeft==idx
312 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000313 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000314 int k;
drh967e8b72000-06-21 13:59:10 +0000315 for(k=0; k<pIdx->nColumn; k++){
316 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000317 switch( aExpr[j].p->op ){
318 case TK_EQ: {
319 eqMask |= 1<<k;
320 break;
321 }
322 case TK_LE:
323 case TK_LT: {
324 ltMask |= 1<<k;
325 break;
326 }
327 case TK_GE:
328 case TK_GT: {
329 gtMask |= 1<<k;
330 break;
331 }
332 default: {
333 /* CANT_HAPPEN */
334 assert( 0 );
335 break;
336 }
337 }
drh75897232000-05-29 14:26:00 +0000338 break;
339 }
340 }
341 }
342 if( aExpr[j].idxRight==idx
343 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000344 int iColumn = aExpr[j].p->pRight->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 gtMask |= 1<<k;
356 break;
357 }
358 case TK_GE:
359 case TK_GT: {
360 ltMask |= 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 }
drh487ab3c2001-11-08 00:45:21 +0000374 for(nEq=0; nEq<pIdx->nColumn; nEq++){
375 m = (1<<(nEq+1))-1;
376 if( (m & eqMask)!=m ) break;
377 }
378 score = nEq*4;
379 m = 1<<nEq;
380 if( m & ltMask ) score++;
381 if( m & gtMask ) score+=2;
382 if( score>bestScore ){
383 pBestIdx = pIdx;
384 bestScore = score;
drh75897232000-05-29 14:26:00 +0000385 }
386 }
drh6b563442001-11-07 16:48:26 +0000387 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000388 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000389 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000390 if( pBestIdx ){
391 pWInfo->a[i].iCur = nCur++;
392 }
drh75897232000-05-29 14:26:00 +0000393 }
394
drh6b563442001-11-07 16:48:26 +0000395 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000396 */
397 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000398 int openOp;
399 Table *pTab;
400
401 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000402 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000403 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000404 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
405 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000406 if( i==0 && !pParse->schemaVerified &&
407 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000408 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000409 pParse->schemaVerified = 1;
410 }
drh6b563442001-11-07 16:48:26 +0000411 if( pWInfo->a[i].pIdx!=0 ){
412 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
413 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000414 }
415 }
416
417 /* Generate the code to do the search
418 */
drh75897232000-05-29 14:26:00 +0000419 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000420 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000421 for(i=0; i<pTabList->nId; i++){
422 int j, k;
423 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000424 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000425 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000426
drh8aff1012001-12-22 14:49:24 +0000427 pIdx = pLevel->pIdx;
428 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
429 /* Case 1: We can directly reference a single row using an
430 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000431 */
drh8aff1012001-12-22 14:49:24 +0000432 k = iDirectEq[i];
433 assert( k<nExpr );
434 assert( aExpr[k].p!=0 );
435 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
436 if( aExpr[k].idxLeft==idx ){
437 sqliteExprCode(pParse, aExpr[k].p->pRight);
438 }else{
439 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000440 }
drh8aff1012001-12-22 14:49:24 +0000441 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000442 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
443 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000444 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhc4a3c772001-04-04 11:48:57 +0000445 if( i==pTabList->nId-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000446 /* Note: The OP_Dup below will cause the recno to be left on the
447 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000448 ** taken. This violates a general rule of the VDBE that you should
449 ** never leave values on the stack in order to avoid a stack overflow.
450 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000451 ** because the pushKey flag is only true for UPDATE and DELETE, not
452 ** for SELECT, and nested loops only occur on a SELECT.
453 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000454 */
drhc4a3c772001-04-04 11:48:57 +0000455 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000456 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000457 }else{
drhc4a3c772001-04-04 11:48:57 +0000458 haveKey = 0;
459 }
drh6b125452002-01-28 15:53:03 +0000460 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000461 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000462 }else if( pIdx!=0 && pLevel->score%4==0 ){
463 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000464 */
drh6b563442001-11-07 16:48:26 +0000465 int start;
drh487ab3c2001-11-08 00:45:21 +0000466 int testOp;
467 int nColumn = pLevel->score/4;
468 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000469 for(k=0; k<nExpr; k++){
470 if( aExpr[k].p==0 ) continue;
471 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000472 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000473 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000474 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000475 ){
476 sqliteExprCode(pParse, aExpr[k].p->pRight);
477 aExpr[k].p = 0;
478 break;
479 }
480 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000481 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000482 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000483 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000484 ){
485 sqliteExprCode(pParse, aExpr[k].p->pLeft);
486 aExpr[k].p = 0;
487 break;
488 }
489 }
490 }
drh6b563442001-11-07 16:48:26 +0000491 pLevel->iMem = pParse->nMem++;
492 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
493 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000494 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
495 if( nColumn==pIdx->nColumn ){
496 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
497 testOp = OP_IdxGT;
498 }else{
499 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
500 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
501 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
502 testOp = OP_IdxGE;
503 }
drh6b563442001-11-07 16:48:26 +0000504 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
505 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000506 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000507 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000508 if( i==pTabList->nId-1 && pushKey ){
509 haveKey = 1;
510 }else{
drh99fcd712001-10-13 01:06:47 +0000511 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000512 haveKey = 0;
513 }
drh6b563442001-11-07 16:48:26 +0000514 pLevel->op = OP_Next;
515 pLevel->p1 = pLevel->iCur;
516 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000517 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
518 /* Case 3: We have an inequality comparison against the ROWID field.
519 */
520 int testOp = OP_Noop;
521 int start;
522
523 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
524 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
525 if( iDirectGt[i]>=0 ){
526 k = iDirectGt[i];
527 assert( k<nExpr );
528 assert( aExpr[k].p!=0 );
529 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
530 if( aExpr[k].idxLeft==idx ){
531 sqliteExprCode(pParse, aExpr[k].p->pRight);
532 }else{
533 sqliteExprCode(pParse, aExpr[k].p->pLeft);
534 }
535 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
536 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
537 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
538 }
539 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
540 aExpr[k].p = 0;
541 }else{
542 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
543 }
544 if( iDirectLt[i]>=0 ){
545 k = iDirectLt[i];
546 assert( k<nExpr );
547 assert( aExpr[k].p!=0 );
548 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
549 if( aExpr[k].idxLeft==idx ){
550 sqliteExprCode(pParse, aExpr[k].p->pRight);
551 }else{
552 sqliteExprCode(pParse, aExpr[k].p->pLeft);
553 }
554 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
555 pLevel->iMem = pParse->nMem++;
556 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
557 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
558 testOp = OP_Ge;
559 }else{
560 testOp = OP_Gt;
561 }
562 aExpr[k].p = 0;
563 }
564 start = sqliteVdbeCurrentAddr(v);
565 pLevel->op = OP_Next;
566 pLevel->p1 = base+idx;
567 pLevel->p2 = start;
568 if( testOp!=OP_Noop ){
569 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
570 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
571 sqliteVdbeAddOp(v, testOp, 0, brk);
572 }
573 haveKey = 0;
574 }else if( pIdx==0 ){
575 /* Case 4: There was no usable index. We must do a complete
576 ** scan of the entire database table.
577 */
578 int start;
579
580 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
581 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
582 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
583 start = sqliteVdbeCurrentAddr(v);
584 pLevel->op = OP_Next;
585 pLevel->p1 = base+idx;
586 pLevel->p2 = start;
587 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000588 }else{
drhaacc5432002-01-06 17:07:40 +0000589 /* Case 5: The contraint on the right-most index field is
590 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000591 */
592 int score = pLevel->score;
593 int nEqColumn = score/4;
594 int start;
595 int leFlag, geFlag;
596 int testOp;
597
598 /* Evaluate the equality constraints
599 */
600 for(j=0; j<nEqColumn; j++){
601 for(k=0; k<nExpr; k++){
602 if( aExpr[k].p==0 ) continue;
603 if( aExpr[k].idxLeft==idx
604 && aExpr[k].p->op==TK_EQ
605 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
606 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
607 ){
608 sqliteExprCode(pParse, aExpr[k].p->pRight);
609 aExpr[k].p = 0;
610 break;
611 }
612 if( aExpr[k].idxRight==idx
613 && aExpr[k].p->op==TK_EQ
614 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
615 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
616 ){
617 sqliteExprCode(pParse, aExpr[k].p->pLeft);
618 aExpr[k].p = 0;
619 break;
620 }
621 }
622 }
623
624 /* Duplicate the equality contraint values because they will all be
625 ** used twice: once to make the termination key and once to make the
626 ** start key.
627 */
628 for(j=0; j<nEqColumn; j++){
629 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
630 }
631
632 /* Generate the termination key. This is the key value that
633 ** will end the search. There is no termination key if there
634 ** are no equality contraints and no "X<..." constraint.
635 */
636 if( (score & 1)!=0 ){
637 for(k=0; k<nExpr; k++){
638 Expr *pExpr = aExpr[k].p;
639 if( pExpr==0 ) continue;
640 if( aExpr[k].idxLeft==idx
641 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
642 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
643 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
644 ){
645 sqliteExprCode(pParse, pExpr->pRight);
646 leFlag = pExpr->op==TK_LE;
647 aExpr[k].p = 0;
648 break;
649 }
650 if( aExpr[k].idxRight==idx
651 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
652 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
653 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
654 ){
655 sqliteExprCode(pParse, pExpr->pLeft);
656 leFlag = pExpr->op==TK_GE;
657 aExpr[k].p = 0;
658 break;
659 }
660 }
661 testOp = OP_IdxGE;
662 }else{
663 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
664 leFlag = 1;
665 }
666 if( testOp!=OP_Noop ){
667 pLevel->iMem = pParse->nMem++;
668 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
669 if( leFlag ){
670 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
671 }
672 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
673 }
674
675 /* Generate the start key. This is the key that defines the lower
676 ** bound on the search. There is no start key if there are not
677 ** equality constraints and if there is no "X>..." constraint. In
678 ** that case, generate a "Rewind" instruction in place of the
679 ** start key search.
680 */
681 if( (score & 2)!=0 ){
682 for(k=0; k<nExpr; k++){
683 Expr *pExpr = aExpr[k].p;
684 if( pExpr==0 ) continue;
685 if( aExpr[k].idxLeft==idx
686 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
687 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
688 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
689 ){
690 sqliteExprCode(pParse, pExpr->pRight);
691 geFlag = pExpr->op==TK_GE;
692 aExpr[k].p = 0;
693 break;
694 }
695 if( aExpr[k].idxRight==idx
696 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
697 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
698 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
699 ){
700 sqliteExprCode(pParse, pExpr->pLeft);
701 geFlag = pExpr->op==TK_LE;
702 aExpr[k].p = 0;
703 break;
704 }
705 }
drh7900ead2001-11-12 13:51:43 +0000706 }else{
707 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000708 }
709 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
710 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
711 if( nEqColumn>0 || (score&2)!=0 ){
712 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
713 if( !geFlag ){
714 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
715 }
716 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
717 }else{
718 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
719 }
720
721 /* Generate the the top of the loop. If there is a termination
722 ** key we have to test for that key and abort at the top of the
723 ** loop.
724 */
725 start = sqliteVdbeCurrentAddr(v);
726 if( testOp!=OP_Noop ){
727 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
728 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
729 }
730 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
731 if( i==pTabList->nId-1 && pushKey ){
732 haveKey = 1;
733 }else{
734 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
735 haveKey = 0;
736 }
737
738 /* Record the instruction used to terminate the loop.
739 */
740 pLevel->op = OP_Next;
741 pLevel->p1 = pLevel->iCur;
742 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000743 }
744 loopMask |= 1<<idx;
745
746 /* Insert code to test every subexpression that can be completely
747 ** computed using the current set of tables.
748 */
749 for(j=0; j<nExpr; j++){
750 if( aExpr[j].p==0 ) continue;
751 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
752 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
753 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000754 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000755 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000756 }
757 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
758 aExpr[j].p = 0;
759 }
760 brk = cont;
761 }
762 pWInfo->iContinue = cont;
763 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000764 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000765 }
766 sqliteFree(aOrder);
767 return pWInfo;
768}
769
770/*
771** Generate the end of the WHERE loop.
772*/
773void sqliteWhereEnd(WhereInfo *pWInfo){
774 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000775 int i;
drh19a775c2000-06-05 18:54:46 +0000776 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000777 WhereLevel *pLevel;
drh22f70c32002-02-18 01:17:00 +0000778 IdList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000779
drh22f70c32002-02-18 01:17:00 +0000780 for(i=pTabList->nId-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000781 pLevel = &pWInfo->a[i];
782 sqliteVdbeResolveLabel(v, pLevel->cont);
783 if( pLevel->op!=OP_Noop ){
784 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000785 }
drh6b563442001-11-07 16:48:26 +0000786 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000787 }
drh6b563442001-11-07 16:48:26 +0000788 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drh22f70c32002-02-18 01:17:00 +0000789 for(i=0; i<pTabList->nId; i++){
790 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000791 pLevel = &pWInfo->a[i];
792 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
793 if( pLevel->pIdx!=0 ){
794 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
795 }
drh19a775c2000-06-05 18:54:46 +0000796 }
drh22f70c32002-02-18 01:17:00 +0000797 pWInfo->pParse->nTab = base;
drh75897232000-05-29 14:26:00 +0000798 sqliteFree(pWInfo);
799 return;
800}