blob: 0d685137a6d1a71671b3632d0fff238bf21af1d2 [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**
drh3f6b5482002-04-02 13:26:10 +000016** $Id: where.c,v 1.40 2002/04/02 13:26:11 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 */
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;
drh75897232000-05-29 14:26:00 +0000196
197 /* Split the WHERE clause into as many as 32 separate subexpressions
198 ** where each subexpression is separated by an AND operator. Any additional
199 ** subexpressions are attached in the aExpr[32] and will not enter
200 ** into the query optimizer computations. 32 is chosen as the cutoff
201 ** since that is the number of bits in an integer that we use for an
202 ** expression-used mask.
203 */
204 memset(aExpr, 0, sizeof(aExpr));
205 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
206
207 /* Analyze all of the subexpressions.
208 */
209 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000210 exprAnalyze(base, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000211 }
212
213 /* Figure out a good nesting order for the tables. aOrder[0] will
214 ** be the index in pTabList of the outermost table. aOrder[1] will
215 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
216 ** be the innermost loop.
217 **
drh7e391e12000-05-30 20:17:49 +0000218 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000219 ** for an effiecient query. But for now, just use whatever order the
220 ** tables appear in in the pTabList.
221 */
222 for(i=0; i<pTabList->nId; i++){
223 aOrder[i] = i;
224 }
225
226 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000227 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
228 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000229 ** loop.
230 **
231 ** If terms exist that use the ROWID of any table, then set the
232 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
233 ** to the index of the term containing the ROWID. We always prefer
234 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000235 ** index which requires reading an index first to get the rowid then
236 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000237 **
238 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000239 ** first 32 tables are candidates for indices. This is (again) due
240 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000241 */
242 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000243 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000244 int j;
drh75897232000-05-29 14:26:00 +0000245 int idx = aOrder[i];
246 Table *pTab = pTabList->a[idx].pTab;
247 Index *pIdx;
248 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000249 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000250
drhc4a3c772001-04-04 11:48:57 +0000251 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000252 ** ROWID field of this table. For terms of the form ROWID==expr
253 ** set iDirectEq[i] to the index of the term. For terms of the
254 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
255 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000256 */
drh8aff1012001-12-22 14:49:24 +0000257 iDirectEq[i] = -1;
258 iDirectLt[i] = -1;
259 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000260 for(j=0; j<nExpr; j++){
261 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
262 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000263 switch( aExpr[j].p->op ){
264 case TK_EQ: iDirectEq[i] = j; break;
265 case TK_LE:
266 case TK_LT: iDirectLt[i] = j; break;
267 case TK_GE:
268 case TK_GT: iDirectGt[i] = j; break;
269 }
drhc4a3c772001-04-04 11:48:57 +0000270 }
271 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
272 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000273 switch( aExpr[j].p->op ){
274 case TK_EQ: iDirectEq[i] = j; break;
275 case TK_LE:
276 case TK_LT: iDirectGt[i] = j; break;
277 case TK_GE:
278 case TK_GT: iDirectLt[i] = j; break;
279 }
drhc4a3c772001-04-04 11:48:57 +0000280 }
281 }
drh8aff1012001-12-22 14:49:24 +0000282 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000283 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000284 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000285 continue;
286 }
287
drh75897232000-05-29 14:26:00 +0000288 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000289 ** the "best" index. pBestIdx is left set to NULL if no indices
290 ** are usable.
drh75897232000-05-29 14:26:00 +0000291 **
drh487ab3c2001-11-08 00:45:21 +0000292 ** The best index is determined as follows. For each of the
293 ** left-most terms that is fixed by an equality operator, add
294 ** 4 to the score. The right-most term of the index may be
295 ** constrained by an inequality. Add 1 if for an "x<..." constraint
296 ** and add 2 for an "x>..." constraint. Chose the index that
297 ** gives the best score.
298 **
299 ** This scoring system is designed so that the score can later be
300 ** used to determine how the index is used. If the score&3 is 0
301 ** then all constraints are equalities. If score&1 is not 0 then
302 ** there is an inequality used as a termination key. (ex: "x<...")
303 ** If score&2 is not 0 then there is an inequality used as the
304 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000305 */
306 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000307 int eqMask = 0; /* Index columns covered by an x=... constraint */
308 int ltMask = 0; /* Index columns covered by an x<... constraint */
309 int gtMask = 0; /* Index columns covered by an x>... constraing */
310 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000311
drh74e24cd2002-01-09 03:19:59 +0000312 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000313 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000314 for(j=0; j<nExpr; j++){
315 if( aExpr[j].idxLeft==idx
316 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000317 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000318 int k;
drh967e8b72000-06-21 13:59:10 +0000319 for(k=0; k<pIdx->nColumn; k++){
320 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000321 switch( aExpr[j].p->op ){
322 case TK_EQ: {
323 eqMask |= 1<<k;
324 break;
325 }
326 case TK_LE:
327 case TK_LT: {
328 ltMask |= 1<<k;
329 break;
330 }
331 case TK_GE:
332 case TK_GT: {
333 gtMask |= 1<<k;
334 break;
335 }
336 default: {
337 /* CANT_HAPPEN */
338 assert( 0 );
339 break;
340 }
341 }
drh75897232000-05-29 14:26:00 +0000342 break;
343 }
344 }
345 }
346 if( aExpr[j].idxRight==idx
347 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000348 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000349 int k;
drh967e8b72000-06-21 13:59:10 +0000350 for(k=0; k<pIdx->nColumn; k++){
351 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000352 switch( aExpr[j].p->op ){
353 case TK_EQ: {
354 eqMask |= 1<<k;
355 break;
356 }
357 case TK_LE:
358 case TK_LT: {
359 gtMask |= 1<<k;
360 break;
361 }
362 case TK_GE:
363 case TK_GT: {
364 ltMask |= 1<<k;
365 break;
366 }
367 default: {
368 /* CANT_HAPPEN */
369 assert( 0 );
370 break;
371 }
372 }
drh75897232000-05-29 14:26:00 +0000373 break;
374 }
375 }
376 }
377 }
drh487ab3c2001-11-08 00:45:21 +0000378 for(nEq=0; nEq<pIdx->nColumn; nEq++){
379 m = (1<<(nEq+1))-1;
380 if( (m & eqMask)!=m ) break;
381 }
382 score = nEq*4;
383 m = 1<<nEq;
384 if( m & ltMask ) score++;
385 if( m & gtMask ) score+=2;
386 if( score>bestScore ){
387 pBestIdx = pIdx;
388 bestScore = score;
drh75897232000-05-29 14:26:00 +0000389 }
390 }
drh6b563442001-11-07 16:48:26 +0000391 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000392 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000393 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000394 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000395 pWInfo->a[i].iCur = pParse->nTab++;
396 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000397 }
drh75897232000-05-29 14:26:00 +0000398 }
399
drh6b563442001-11-07 16:48:26 +0000400 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000401 */
402 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000403 int openOp;
404 Table *pTab;
405
406 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000407 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000408 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000409 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
410 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000411 if( i==0 && !pParse->schemaVerified &&
412 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000413 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000414 pParse->schemaVerified = 1;
415 }
drh6b563442001-11-07 16:48:26 +0000416 if( pWInfo->a[i].pIdx!=0 ){
417 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
418 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000419 }
420 }
421
422 /* Generate the code to do the search
423 */
drh75897232000-05-29 14:26:00 +0000424 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000425 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000426 for(i=0; i<pTabList->nId; i++){
427 int j, k;
428 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000429 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000430 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000431
drh8aff1012001-12-22 14:49:24 +0000432 pIdx = pLevel->pIdx;
433 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
434 /* Case 1: We can directly reference a single row using an
435 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000436 */
drh8aff1012001-12-22 14:49:24 +0000437 k = iDirectEq[i];
438 assert( k<nExpr );
439 assert( aExpr[k].p!=0 );
440 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
441 if( aExpr[k].idxLeft==idx ){
442 sqliteExprCode(pParse, aExpr[k].p->pRight);
443 }else{
444 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000445 }
drh8aff1012001-12-22 14:49:24 +0000446 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000447 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
448 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000449 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhc4a3c772001-04-04 11:48:57 +0000450 if( i==pTabList->nId-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000451 /* Note: The OP_Dup below will cause the recno to be left on the
452 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000453 ** taken. This violates a general rule of the VDBE that you should
454 ** never leave values on the stack in order to avoid a stack overflow.
455 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000456 ** because the pushKey flag is only true for UPDATE and DELETE, not
457 ** for SELECT, and nested loops only occur on a SELECT.
458 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000459 */
drhc4a3c772001-04-04 11:48:57 +0000460 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000461 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000462 }else{
drhc4a3c772001-04-04 11:48:57 +0000463 haveKey = 0;
464 }
drh6b125452002-01-28 15:53:03 +0000465 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000466 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000467 }else if( pIdx!=0 && pLevel->score%4==0 ){
468 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000469 */
drh6b563442001-11-07 16:48:26 +0000470 int start;
drh487ab3c2001-11-08 00:45:21 +0000471 int testOp;
472 int nColumn = pLevel->score/4;
473 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000474 for(k=0; k<nExpr; k++){
475 if( aExpr[k].p==0 ) continue;
476 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000477 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000478 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000479 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000480 ){
481 sqliteExprCode(pParse, aExpr[k].p->pRight);
482 aExpr[k].p = 0;
483 break;
484 }
485 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000486 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000487 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000488 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000489 ){
490 sqliteExprCode(pParse, aExpr[k].p->pLeft);
491 aExpr[k].p = 0;
492 break;
493 }
494 }
495 }
drh6b563442001-11-07 16:48:26 +0000496 pLevel->iMem = pParse->nMem++;
497 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
498 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000499 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
500 if( nColumn==pIdx->nColumn ){
501 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
502 testOp = OP_IdxGT;
503 }else{
504 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
505 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
506 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
507 testOp = OP_IdxGE;
508 }
drh6b563442001-11-07 16:48:26 +0000509 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
510 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000511 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000512 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000513 if( i==pTabList->nId-1 && pushKey ){
514 haveKey = 1;
515 }else{
drh99fcd712001-10-13 01:06:47 +0000516 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000517 haveKey = 0;
518 }
drh6b563442001-11-07 16:48:26 +0000519 pLevel->op = OP_Next;
520 pLevel->p1 = pLevel->iCur;
521 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000522 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
523 /* Case 3: We have an inequality comparison against the ROWID field.
524 */
525 int testOp = OP_Noop;
526 int start;
527
528 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
529 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
530 if( iDirectGt[i]>=0 ){
531 k = iDirectGt[i];
532 assert( k<nExpr );
533 assert( aExpr[k].p!=0 );
534 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
535 if( aExpr[k].idxLeft==idx ){
536 sqliteExprCode(pParse, aExpr[k].p->pRight);
537 }else{
538 sqliteExprCode(pParse, aExpr[k].p->pLeft);
539 }
540 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
541 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
542 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
543 }
544 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
545 aExpr[k].p = 0;
546 }else{
547 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
548 }
549 if( iDirectLt[i]>=0 ){
550 k = iDirectLt[i];
551 assert( k<nExpr );
552 assert( aExpr[k].p!=0 );
553 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
554 if( aExpr[k].idxLeft==idx ){
555 sqliteExprCode(pParse, aExpr[k].p->pRight);
556 }else{
557 sqliteExprCode(pParse, aExpr[k].p->pLeft);
558 }
559 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
560 pLevel->iMem = pParse->nMem++;
561 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
562 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
563 testOp = OP_Ge;
564 }else{
565 testOp = OP_Gt;
566 }
567 aExpr[k].p = 0;
568 }
569 start = sqliteVdbeCurrentAddr(v);
570 pLevel->op = OP_Next;
571 pLevel->p1 = base+idx;
572 pLevel->p2 = start;
573 if( testOp!=OP_Noop ){
574 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
575 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
576 sqliteVdbeAddOp(v, testOp, 0, brk);
577 }
578 haveKey = 0;
579 }else if( pIdx==0 ){
580 /* Case 4: There was no usable index. We must do a complete
581 ** scan of the entire database table.
582 */
583 int start;
584
585 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
586 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
587 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
588 start = sqliteVdbeCurrentAddr(v);
589 pLevel->op = OP_Next;
590 pLevel->p1 = base+idx;
591 pLevel->p2 = start;
592 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000593 }else{
drhaacc5432002-01-06 17:07:40 +0000594 /* Case 5: The contraint on the right-most index field is
595 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000596 */
597 int score = pLevel->score;
598 int nEqColumn = score/4;
599 int start;
600 int leFlag, geFlag;
601 int testOp;
602
603 /* Evaluate the equality constraints
604 */
605 for(j=0; j<nEqColumn; j++){
606 for(k=0; k<nExpr; k++){
607 if( aExpr[k].p==0 ) continue;
608 if( aExpr[k].idxLeft==idx
609 && aExpr[k].p->op==TK_EQ
610 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
611 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
612 ){
613 sqliteExprCode(pParse, aExpr[k].p->pRight);
614 aExpr[k].p = 0;
615 break;
616 }
617 if( aExpr[k].idxRight==idx
618 && aExpr[k].p->op==TK_EQ
619 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
620 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
621 ){
622 sqliteExprCode(pParse, aExpr[k].p->pLeft);
623 aExpr[k].p = 0;
624 break;
625 }
626 }
627 }
628
629 /* Duplicate the equality contraint values because they will all be
630 ** used twice: once to make the termination key and once to make the
631 ** start key.
632 */
633 for(j=0; j<nEqColumn; j++){
634 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
635 }
636
637 /* Generate the termination key. This is the key value that
638 ** will end the search. There is no termination key if there
639 ** are no equality contraints and no "X<..." constraint.
640 */
641 if( (score & 1)!=0 ){
642 for(k=0; k<nExpr; k++){
643 Expr *pExpr = aExpr[k].p;
644 if( pExpr==0 ) continue;
645 if( aExpr[k].idxLeft==idx
646 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
647 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
648 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
649 ){
650 sqliteExprCode(pParse, pExpr->pRight);
651 leFlag = pExpr->op==TK_LE;
652 aExpr[k].p = 0;
653 break;
654 }
655 if( aExpr[k].idxRight==idx
656 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
657 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
658 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
659 ){
660 sqliteExprCode(pParse, pExpr->pLeft);
661 leFlag = pExpr->op==TK_GE;
662 aExpr[k].p = 0;
663 break;
664 }
665 }
666 testOp = OP_IdxGE;
667 }else{
668 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
669 leFlag = 1;
670 }
671 if( testOp!=OP_Noop ){
672 pLevel->iMem = pParse->nMem++;
673 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
674 if( leFlag ){
675 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
676 }
677 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
678 }
679
680 /* Generate the start key. This is the key that defines the lower
681 ** bound on the search. There is no start key if there are not
682 ** equality constraints and if there is no "X>..." constraint. In
683 ** that case, generate a "Rewind" instruction in place of the
684 ** start key search.
685 */
686 if( (score & 2)!=0 ){
687 for(k=0; k<nExpr; k++){
688 Expr *pExpr = aExpr[k].p;
689 if( pExpr==0 ) continue;
690 if( aExpr[k].idxLeft==idx
691 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
692 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
693 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
694 ){
695 sqliteExprCode(pParse, pExpr->pRight);
696 geFlag = pExpr->op==TK_GE;
697 aExpr[k].p = 0;
698 break;
699 }
700 if( aExpr[k].idxRight==idx
701 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
702 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
703 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
704 ){
705 sqliteExprCode(pParse, pExpr->pLeft);
706 geFlag = pExpr->op==TK_LE;
707 aExpr[k].p = 0;
708 break;
709 }
710 }
drh7900ead2001-11-12 13:51:43 +0000711 }else{
712 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000713 }
714 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
715 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
716 if( nEqColumn>0 || (score&2)!=0 ){
717 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
718 if( !geFlag ){
719 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
720 }
721 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
722 }else{
723 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
724 }
725
726 /* Generate the the top of the loop. If there is a termination
727 ** key we have to test for that key and abort at the top of the
728 ** loop.
729 */
730 start = sqliteVdbeCurrentAddr(v);
731 if( testOp!=OP_Noop ){
732 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
733 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
734 }
735 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
736 if( i==pTabList->nId-1 && pushKey ){
737 haveKey = 1;
738 }else{
739 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
740 haveKey = 0;
741 }
742
743 /* Record the instruction used to terminate the loop.
744 */
745 pLevel->op = OP_Next;
746 pLevel->p1 = pLevel->iCur;
747 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000748 }
749 loopMask |= 1<<idx;
750
751 /* Insert code to test every subexpression that can be completely
752 ** computed using the current set of tables.
753 */
754 for(j=0; j<nExpr; j++){
755 if( aExpr[j].p==0 ) continue;
drh3f6b5482002-04-02 13:26:10 +0000756 if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
drh75897232000-05-29 14:26:00 +0000757 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000758 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000759 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000760 }
761 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
762 aExpr[j].p = 0;
763 }
764 brk = cont;
765 }
766 pWInfo->iContinue = cont;
767 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000768 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000769 }
770 sqliteFree(aOrder);
771 return pWInfo;
772}
773
774/*
775** Generate the end of the WHERE loop.
776*/
777void sqliteWhereEnd(WhereInfo *pWInfo){
778 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000779 int i;
drh19a775c2000-06-05 18:54:46 +0000780 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000781 WhereLevel *pLevel;
drh22f70c32002-02-18 01:17:00 +0000782 IdList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000783
drh22f70c32002-02-18 01:17:00 +0000784 for(i=pTabList->nId-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000785 pLevel = &pWInfo->a[i];
786 sqliteVdbeResolveLabel(v, pLevel->cont);
787 if( pLevel->op!=OP_Noop ){
788 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000789 }
drh6b563442001-11-07 16:48:26 +0000790 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000791 }
drh6b563442001-11-07 16:48:26 +0000792 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drh22f70c32002-02-18 01:17:00 +0000793 for(i=0; i<pTabList->nId; i++){
794 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000795 pLevel = &pWInfo->a[i];
796 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
797 if( pLevel->pIdx!=0 ){
798 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
799 }
drh19a775c2000-06-05 18:54:46 +0000800 }
drh832508b2002-03-02 17:04:07 +0000801 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
802 pWInfo->pParse->nTab = pWInfo->savedNTab;
803 }
drh75897232000-05-29 14:26:00 +0000804 sqliteFree(pWInfo);
805 return;
806}