blob: 89a7258c907f633f57637c469438921c2fea45d5 [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**
drh7900ead2001-11-12 13:51:43 +000016** $Id: where.c,v 1.28 2001/11/12 13:51:43 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 */
drh75897232000-05-29 14:26:00 +0000170 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
171
drh6b563442001-11-07 16:48:26 +0000172 /* Allocate space for aOrder[] and aiMem[]. */
drh75897232000-05-29 14:26:00 +0000173 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
174
175 /* Allocate and initialize the WhereInfo structure that will become the
176 ** return value.
177 */
drh6b563442001-11-07 16:48:26 +0000178 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) );
drhdaffd0e2001-04-11 14:28:42 +0000179 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000180 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000181 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000182 return 0;
183 }
184 pWInfo->pParse = pParse;
185 pWInfo->pTabList = pTabList;
drh19a775c2000-06-05 18:54:46 +0000186 base = pWInfo->base = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000187 nCur = base + pTabList->nId;
drh75897232000-05-29 14:26:00 +0000188
189 /* Split the WHERE clause into as many as 32 separate subexpressions
190 ** where each subexpression is separated by an AND operator. Any additional
191 ** subexpressions are attached in the aExpr[32] and will not enter
192 ** into the query optimizer computations. 32 is chosen as the cutoff
193 ** since that is the number of bits in an integer that we use for an
194 ** expression-used mask.
195 */
196 memset(aExpr, 0, sizeof(aExpr));
197 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
198
199 /* Analyze all of the subexpressions.
200 */
201 for(i=0; i<nExpr; i++){
drh19a775c2000-06-05 18:54:46 +0000202 exprAnalyze(pParse->nTab, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000203 }
204
205 /* Figure out a good nesting order for the tables. aOrder[0] will
206 ** be the index in pTabList of the outermost table. aOrder[1] will
207 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
208 ** be the innermost loop.
209 **
drh7e391e12000-05-30 20:17:49 +0000210 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000211 ** for an effiecient query. But for now, just use whatever order the
212 ** tables appear in in the pTabList.
213 */
214 for(i=0; i<pTabList->nId; i++){
215 aOrder[i] = i;
216 }
217
218 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000219 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
220 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drhc4a3c772001-04-04 11:48:57 +0000221 ** loop. If the expression uses only the ROWID field, then set
222 ** aDirect[i] to 1.
drh75897232000-05-29 14:26:00 +0000223 **
224 ** Actually, if there are more than 32 tables in the join, only the
225 ** first 32 tables are candidates for indices.
226 */
227 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000228 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000229 int j;
drh75897232000-05-29 14:26:00 +0000230 int idx = aOrder[i];
231 Table *pTab = pTabList->a[idx].pTab;
232 Index *pIdx;
233 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000234 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000235
drhc4a3c772001-04-04 11:48:57 +0000236 /* Check to see if there is an expression that uses only the
237 ** ROWID field of this table. If so, set aDirect[i] to 1.
238 ** If not, set aDirect[i] to 0.
239 */
240 aDirect[i] = 0;
241 for(j=0; j<nExpr; j++){
242 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
243 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
244 aDirect[i] = 1;
245 break;
246 }
247 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
248 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
249 aDirect[i] = 1;
250 break;
251 }
252 }
253 if( aDirect[i] ){
254 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000255 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000256 continue;
257 }
258
drh75897232000-05-29 14:26:00 +0000259 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000260 ** the "best" index. pBestIdx is left set to NULL if no indices
261 ** are usable.
drh75897232000-05-29 14:26:00 +0000262 **
drh487ab3c2001-11-08 00:45:21 +0000263 ** The best index is determined as follows. For each of the
264 ** left-most terms that is fixed by an equality operator, add
265 ** 4 to the score. The right-most term of the index may be
266 ** constrained by an inequality. Add 1 if for an "x<..." constraint
267 ** and add 2 for an "x>..." constraint. Chose the index that
268 ** gives the best score.
269 **
270 ** This scoring system is designed so that the score can later be
271 ** used to determine how the index is used. If the score&3 is 0
272 ** then all constraints are equalities. If score&1 is not 0 then
273 ** there is an inequality used as a termination key. (ex: "x<...")
274 ** If score&2 is not 0 then there is an inequality used as the
275 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000276 */
277 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000278 int eqMask = 0; /* Index columns covered by an x=... constraint */
279 int ltMask = 0; /* Index columns covered by an x<... constraint */
280 int gtMask = 0; /* Index columns covered by an x>... constraing */
281 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000282
drh487ab3c2001-11-08 00:45:21 +0000283 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000284 for(j=0; j<nExpr; j++){
285 if( aExpr[j].idxLeft==idx
286 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000287 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000288 int k;
drh967e8b72000-06-21 13:59:10 +0000289 for(k=0; k<pIdx->nColumn; k++){
290 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000291 switch( aExpr[j].p->op ){
292 case TK_EQ: {
293 eqMask |= 1<<k;
294 break;
295 }
296 case TK_LE:
297 case TK_LT: {
298 ltMask |= 1<<k;
299 break;
300 }
301 case TK_GE:
302 case TK_GT: {
303 gtMask |= 1<<k;
304 break;
305 }
306 default: {
307 /* CANT_HAPPEN */
308 assert( 0 );
309 break;
310 }
311 }
drh75897232000-05-29 14:26:00 +0000312 break;
313 }
314 }
315 }
316 if( aExpr[j].idxRight==idx
317 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000318 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000319 int k;
drh967e8b72000-06-21 13:59:10 +0000320 for(k=0; k<pIdx->nColumn; k++){
321 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000322 switch( aExpr[j].p->op ){
323 case TK_EQ: {
324 eqMask |= 1<<k;
325 break;
326 }
327 case TK_LE:
328 case TK_LT: {
329 gtMask |= 1<<k;
330 break;
331 }
332 case TK_GE:
333 case TK_GT: {
334 ltMask |= 1<<k;
335 break;
336 }
337 default: {
338 /* CANT_HAPPEN */
339 assert( 0 );
340 break;
341 }
342 }
drh75897232000-05-29 14:26:00 +0000343 break;
344 }
345 }
346 }
347 }
drh487ab3c2001-11-08 00:45:21 +0000348 for(nEq=0; nEq<pIdx->nColumn; nEq++){
349 m = (1<<(nEq+1))-1;
350 if( (m & eqMask)!=m ) break;
351 }
352 score = nEq*4;
353 m = 1<<nEq;
354 if( m & ltMask ) score++;
355 if( m & gtMask ) score+=2;
356 if( score>bestScore ){
357 pBestIdx = pIdx;
358 bestScore = score;
drh75897232000-05-29 14:26:00 +0000359 }
360 }
drh6b563442001-11-07 16:48:26 +0000361 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000362 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000363 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000364 if( pBestIdx ){
365 pWInfo->a[i].iCur = nCur++;
366 }
drh75897232000-05-29 14:26:00 +0000367 }
368
drh6b563442001-11-07 16:48:26 +0000369 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000370 */
371 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000372 int openOp;
373 Table *pTab;
374
375 pTab = pTabList->a[i].pTab;
376 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000377 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
378 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000379 if( i==0 && !pParse->schemaVerified &&
380 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000381 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000382 pParse->schemaVerified = 1;
383 }
drh6b563442001-11-07 16:48:26 +0000384 if( pWInfo->a[i].pIdx!=0 ){
385 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
386 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000387 }
388 }
389
390 /* Generate the code to do the search
391 */
drh75897232000-05-29 14:26:00 +0000392 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000393 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000394 for(i=0; i<pTabList->nId; i++){
395 int j, k;
396 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000397 int goDirect;
398 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000399 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000400
drh6b563442001-11-07 16:48:26 +0000401 if( i<ARRAYSIZE(aDirect) ){
402 pIdx = pLevel->pIdx;
drhc4a3c772001-04-04 11:48:57 +0000403 goDirect = aDirect[i];
404 }else{
405 pIdx = 0;
406 goDirect = 0;
407 }
408
409 if( goDirect ){
410 /* Case 1: We can directly reference a single row using the ROWID field.
411 */
drhc4a3c772001-04-04 11:48:57 +0000412 for(k=0; k<nExpr; k++){
413 if( aExpr[k].p==0 ) continue;
414 if( aExpr[k].idxLeft==idx
415 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
416 && aExpr[k].p->pLeft->iColumn<0
417 ){
418 sqliteExprCode(pParse, aExpr[k].p->pRight);
419 aExpr[k].p = 0;
420 break;
421 }
422 if( aExpr[k].idxRight==idx
423 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
424 && aExpr[k].p->pRight->iColumn<0
425 ){
426 sqliteExprCode(pParse, aExpr[k].p->pLeft);
427 aExpr[k].p = 0;
428 break;
429 }
430 }
drh99fcd712001-10-13 01:06:47 +0000431 sqliteVdbeAddOp(v, OP_AddImm, 0, 0);
drh6b563442001-11-07 16:48:26 +0000432 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
433 cont = pLevel->cont = brk;
drhc4a3c772001-04-04 11:48:57 +0000434 if( i==pTabList->nId-1 && pushKey ){
435 haveKey = 1;
436 }else{
drh99fcd712001-10-13 01:06:47 +0000437 sqliteVdbeAddOp(v, OP_NotFound, base+idx, brk);
drhc4a3c772001-04-04 11:48:57 +0000438 haveKey = 0;
439 }
drh6b563442001-11-07 16:48:26 +0000440 pLevel->op = OP_Noop;
drhc4a3c772001-04-04 11:48:57 +0000441 }else if( pIdx==0 ){
442 /* Case 2: There was no usable index. We must do a complete
drh487ab3c2001-11-08 00:45:21 +0000443 ** scan of the entire database table.
drh75897232000-05-29 14:26:00 +0000444 */
drh6b563442001-11-07 16:48:26 +0000445 int start;
446
447 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
448 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
449 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
450 start = sqliteVdbeCurrentAddr(v);
451 pLevel->op = OP_Next;
452 pLevel->p1 = base+idx;
453 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000454 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000455 }else if( pLevel->score%4==0 ){
456 /* Case 3: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000457 */
drh6b563442001-11-07 16:48:26 +0000458 int start;
drh487ab3c2001-11-08 00:45:21 +0000459 int testOp;
460 int nColumn = pLevel->score/4;
461 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000462 for(k=0; k<nExpr; k++){
463 if( aExpr[k].p==0 ) continue;
464 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000465 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000466 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000467 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000468 ){
469 sqliteExprCode(pParse, aExpr[k].p->pRight);
470 aExpr[k].p = 0;
471 break;
472 }
473 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000474 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000475 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000476 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000477 ){
478 sqliteExprCode(pParse, aExpr[k].p->pLeft);
479 aExpr[k].p = 0;
480 break;
481 }
482 }
483 }
drh6b563442001-11-07 16:48:26 +0000484 pLevel->iMem = pParse->nMem++;
485 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
486 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000487 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
488 if( nColumn==pIdx->nColumn ){
489 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
490 testOp = OP_IdxGT;
491 }else{
492 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
493 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
494 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
495 testOp = OP_IdxGE;
496 }
drh6b563442001-11-07 16:48:26 +0000497 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
498 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000499 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000500 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000501 if( i==pTabList->nId-1 && pushKey ){
502 haveKey = 1;
503 }else{
drh99fcd712001-10-13 01:06:47 +0000504 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000505 haveKey = 0;
506 }
drh6b563442001-11-07 16:48:26 +0000507 pLevel->op = OP_Next;
508 pLevel->p1 = pLevel->iCur;
509 pLevel->p2 = start;
drh487ab3c2001-11-08 00:45:21 +0000510 }else{
511 /* Case 4: The contraints on the right-most index field are
512 ** inequalities.
513 */
514 int score = pLevel->score;
515 int nEqColumn = score/4;
516 int start;
517 int leFlag, geFlag;
518 int testOp;
519
520 /* Evaluate the equality constraints
521 */
522 for(j=0; j<nEqColumn; j++){
523 for(k=0; k<nExpr; k++){
524 if( aExpr[k].p==0 ) continue;
525 if( aExpr[k].idxLeft==idx
526 && aExpr[k].p->op==TK_EQ
527 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
528 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
529 ){
530 sqliteExprCode(pParse, aExpr[k].p->pRight);
531 aExpr[k].p = 0;
532 break;
533 }
534 if( aExpr[k].idxRight==idx
535 && aExpr[k].p->op==TK_EQ
536 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
537 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
538 ){
539 sqliteExprCode(pParse, aExpr[k].p->pLeft);
540 aExpr[k].p = 0;
541 break;
542 }
543 }
544 }
545
546 /* Duplicate the equality contraint values because they will all be
547 ** used twice: once to make the termination key and once to make the
548 ** start key.
549 */
550 for(j=0; j<nEqColumn; j++){
551 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
552 }
553
554 /* Generate the termination key. This is the key value that
555 ** will end the search. There is no termination key if there
556 ** are no equality contraints and no "X<..." constraint.
557 */
558 if( (score & 1)!=0 ){
559 for(k=0; k<nExpr; k++){
560 Expr *pExpr = aExpr[k].p;
561 if( pExpr==0 ) continue;
562 if( aExpr[k].idxLeft==idx
563 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
564 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
565 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
566 ){
567 sqliteExprCode(pParse, pExpr->pRight);
568 leFlag = pExpr->op==TK_LE;
569 aExpr[k].p = 0;
570 break;
571 }
572 if( aExpr[k].idxRight==idx
573 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
574 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
575 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
576 ){
577 sqliteExprCode(pParse, pExpr->pLeft);
578 leFlag = pExpr->op==TK_GE;
579 aExpr[k].p = 0;
580 break;
581 }
582 }
583 testOp = OP_IdxGE;
584 }else{
585 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
586 leFlag = 1;
587 }
588 if( testOp!=OP_Noop ){
589 pLevel->iMem = pParse->nMem++;
590 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
591 if( leFlag ){
592 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
593 }
594 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
595 }
596
597 /* Generate the start key. This is the key that defines the lower
598 ** bound on the search. There is no start key if there are not
599 ** equality constraints and if there is no "X>..." constraint. In
600 ** that case, generate a "Rewind" instruction in place of the
601 ** start key search.
602 */
603 if( (score & 2)!=0 ){
604 for(k=0; k<nExpr; k++){
605 Expr *pExpr = aExpr[k].p;
606 if( pExpr==0 ) continue;
607 if( aExpr[k].idxLeft==idx
608 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
609 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
610 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
611 ){
612 sqliteExprCode(pParse, pExpr->pRight);
613 geFlag = pExpr->op==TK_GE;
614 aExpr[k].p = 0;
615 break;
616 }
617 if( aExpr[k].idxRight==idx
618 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
619 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
620 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
621 ){
622 sqliteExprCode(pParse, pExpr->pLeft);
623 geFlag = pExpr->op==TK_LE;
624 aExpr[k].p = 0;
625 break;
626 }
627 }
drh7900ead2001-11-12 13:51:43 +0000628 }else{
629 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000630 }
631 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
632 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
633 if( nEqColumn>0 || (score&2)!=0 ){
634 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
635 if( !geFlag ){
636 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
637 }
638 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
639 }else{
640 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
641 }
642
643 /* Generate the the top of the loop. If there is a termination
644 ** key we have to test for that key and abort at the top of the
645 ** loop.
646 */
647 start = sqliteVdbeCurrentAddr(v);
648 if( testOp!=OP_Noop ){
649 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
650 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
651 }
652 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
653 if( i==pTabList->nId-1 && pushKey ){
654 haveKey = 1;
655 }else{
656 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
657 haveKey = 0;
658 }
659
660 /* Record the instruction used to terminate the loop.
661 */
662 pLevel->op = OP_Next;
663 pLevel->p1 = pLevel->iCur;
664 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000665 }
666 loopMask |= 1<<idx;
667
668 /* Insert code to test every subexpression that can be completely
669 ** computed using the current set of tables.
670 */
671 for(j=0; j<nExpr; j++){
672 if( aExpr[j].p==0 ) continue;
673 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
674 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
675 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000676 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000677 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000678 }
679 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
680 aExpr[j].p = 0;
681 }
682 brk = cont;
683 }
684 pWInfo->iContinue = cont;
685 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000686 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000687 }
688 sqliteFree(aOrder);
689 return pWInfo;
690}
691
692/*
693** Generate the end of the WHERE loop.
694*/
695void sqliteWhereEnd(WhereInfo *pWInfo){
696 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000697 int i;
drh19a775c2000-06-05 18:54:46 +0000698 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000699 WhereLevel *pLevel;
drh19a775c2000-06-05 18:54:46 +0000700
drh6b563442001-11-07 16:48:26 +0000701 for(i=pWInfo->pTabList->nId-1; i>=0; i--){
702 pLevel = &pWInfo->a[i];
703 sqliteVdbeResolveLabel(v, pLevel->cont);
704 if( pLevel->op!=OP_Noop ){
705 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000706 }
drh6b563442001-11-07 16:48:26 +0000707 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000708 }
drh6b563442001-11-07 16:48:26 +0000709 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
710 for(i=0; i<pWInfo->pTabList->nId; i++){
711 pLevel = &pWInfo->a[i];
712 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
713 if( pLevel->pIdx!=0 ){
714 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
715 }
drh19a775c2000-06-05 18:54:46 +0000716 }
drh75897232000-05-29 14:26:00 +0000717 sqliteFree(pWInfo);
718 return;
719}