blob: dd112023914988805b5faee914da18c085ad5fc3 [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**
drh97665872002-02-13 23:22:53 +000016** $Id: where.c,v 1.34 2002/02/13 23:22:54 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;
drh75897232000-05-29 14:26:00 +0000191
192 /* Split the WHERE clause into as many as 32 separate subexpressions
193 ** where each subexpression is separated by an AND operator. Any additional
194 ** subexpressions are attached in the aExpr[32] and will not enter
195 ** into the query optimizer computations. 32 is chosen as the cutoff
196 ** since that is the number of bits in an integer that we use for an
197 ** expression-used mask.
198 */
199 memset(aExpr, 0, sizeof(aExpr));
200 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
201
202 /* Analyze all of the subexpressions.
203 */
204 for(i=0; i<nExpr; i++){
drh19a775c2000-06-05 18:54:46 +0000205 exprAnalyze(pParse->nTab, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000206 }
207
208 /* Figure out a good nesting order for the tables. aOrder[0] will
209 ** be the index in pTabList of the outermost table. aOrder[1] will
210 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
211 ** be the innermost loop.
212 **
drh7e391e12000-05-30 20:17:49 +0000213 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000214 ** for an effiecient query. But for now, just use whatever order the
215 ** tables appear in in the pTabList.
216 */
217 for(i=0; i<pTabList->nId; i++){
218 aOrder[i] = i;
219 }
220
221 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000222 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
223 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000224 ** loop.
225 **
226 ** If terms exist that use the ROWID of any table, then set the
227 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
228 ** to the index of the term containing the ROWID. We always prefer
229 ** to use a ROWID which can directly access a table rather than an
230 ** index which requires two accesses.
drh75897232000-05-29 14:26:00 +0000231 **
232 ** Actually, if there are more than 32 tables in the join, only the
233 ** first 32 tables are candidates for indices.
234 */
235 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000236 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000237 int j;
drh75897232000-05-29 14:26:00 +0000238 int idx = aOrder[i];
239 Table *pTab = pTabList->a[idx].pTab;
240 Index *pIdx;
241 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000242 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000243
drhc4a3c772001-04-04 11:48:57 +0000244 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000245 ** ROWID field of this table. For terms of the form ROWID==expr
246 ** set iDirectEq[i] to the index of the term. For terms of the
247 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
248 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000249 */
drh8aff1012001-12-22 14:49:24 +0000250 iDirectEq[i] = -1;
251 iDirectLt[i] = -1;
252 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000253 for(j=0; j<nExpr; j++){
254 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
255 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000256 switch( aExpr[j].p->op ){
257 case TK_EQ: iDirectEq[i] = j; break;
258 case TK_LE:
259 case TK_LT: iDirectLt[i] = j; break;
260 case TK_GE:
261 case TK_GT: iDirectGt[i] = j; break;
262 }
drhc4a3c772001-04-04 11:48:57 +0000263 }
264 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
265 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000266 switch( aExpr[j].p->op ){
267 case TK_EQ: iDirectEq[i] = j; break;
268 case TK_LE:
269 case TK_LT: iDirectGt[i] = j; break;
270 case TK_GE:
271 case TK_GT: iDirectLt[i] = j; break;
272 }
drhc4a3c772001-04-04 11:48:57 +0000273 }
274 }
drh8aff1012001-12-22 14:49:24 +0000275 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000276 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000277 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000278 continue;
279 }
280
drh75897232000-05-29 14:26:00 +0000281 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000282 ** the "best" index. pBestIdx is left set to NULL if no indices
283 ** are usable.
drh75897232000-05-29 14:26:00 +0000284 **
drh487ab3c2001-11-08 00:45:21 +0000285 ** The best index is determined as follows. For each of the
286 ** left-most terms that is fixed by an equality operator, add
287 ** 4 to the score. The right-most term of the index may be
288 ** constrained by an inequality. Add 1 if for an "x<..." constraint
289 ** and add 2 for an "x>..." constraint. Chose the index that
290 ** gives the best score.
291 **
292 ** This scoring system is designed so that the score can later be
293 ** used to determine how the index is used. If the score&3 is 0
294 ** then all constraints are equalities. If score&1 is not 0 then
295 ** there is an inequality used as a termination key. (ex: "x<...")
296 ** If score&2 is not 0 then there is an inequality used as the
297 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000298 */
299 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000300 int eqMask = 0; /* Index columns covered by an x=... constraint */
301 int ltMask = 0; /* Index columns covered by an x<... constraint */
302 int gtMask = 0; /* Index columns covered by an x>... constraing */
303 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000304
drh74e24cd2002-01-09 03:19:59 +0000305 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000306 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000307 for(j=0; j<nExpr; j++){
308 if( aExpr[j].idxLeft==idx
309 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000310 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000311 int k;
drh967e8b72000-06-21 13:59:10 +0000312 for(k=0; k<pIdx->nColumn; k++){
313 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000314 switch( aExpr[j].p->op ){
315 case TK_EQ: {
316 eqMask |= 1<<k;
317 break;
318 }
319 case TK_LE:
320 case TK_LT: {
321 ltMask |= 1<<k;
322 break;
323 }
324 case TK_GE:
325 case TK_GT: {
326 gtMask |= 1<<k;
327 break;
328 }
329 default: {
330 /* CANT_HAPPEN */
331 assert( 0 );
332 break;
333 }
334 }
drh75897232000-05-29 14:26:00 +0000335 break;
336 }
337 }
338 }
339 if( aExpr[j].idxRight==idx
340 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000341 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000342 int k;
drh967e8b72000-06-21 13:59:10 +0000343 for(k=0; k<pIdx->nColumn; k++){
344 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000345 switch( aExpr[j].p->op ){
346 case TK_EQ: {
347 eqMask |= 1<<k;
348 break;
349 }
350 case TK_LE:
351 case TK_LT: {
352 gtMask |= 1<<k;
353 break;
354 }
355 case TK_GE:
356 case TK_GT: {
357 ltMask |= 1<<k;
358 break;
359 }
360 default: {
361 /* CANT_HAPPEN */
362 assert( 0 );
363 break;
364 }
365 }
drh75897232000-05-29 14:26:00 +0000366 break;
367 }
368 }
369 }
370 }
drh487ab3c2001-11-08 00:45:21 +0000371 for(nEq=0; nEq<pIdx->nColumn; nEq++){
372 m = (1<<(nEq+1))-1;
373 if( (m & eqMask)!=m ) break;
374 }
375 score = nEq*4;
376 m = 1<<nEq;
377 if( m & ltMask ) score++;
378 if( m & gtMask ) score+=2;
379 if( score>bestScore ){
380 pBestIdx = pIdx;
381 bestScore = score;
drh75897232000-05-29 14:26:00 +0000382 }
383 }
drh6b563442001-11-07 16:48:26 +0000384 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000385 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000386 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000387 if( pBestIdx ){
388 pWInfo->a[i].iCur = nCur++;
389 }
drh75897232000-05-29 14:26:00 +0000390 }
391
drh6b563442001-11-07 16:48:26 +0000392 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000393 */
394 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000395 int openOp;
396 Table *pTab;
397
398 pTab = pTabList->a[i].pTab;
399 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000400 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
401 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000402 if( i==0 && !pParse->schemaVerified &&
403 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000404 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000405 pParse->schemaVerified = 1;
406 }
drh6b563442001-11-07 16:48:26 +0000407 if( pWInfo->a[i].pIdx!=0 ){
408 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
409 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000410 }
411 }
412
413 /* Generate the code to do the search
414 */
drh75897232000-05-29 14:26:00 +0000415 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000416 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000417 for(i=0; i<pTabList->nId; i++){
418 int j, k;
419 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000420 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000421 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000422
drh8aff1012001-12-22 14:49:24 +0000423 pIdx = pLevel->pIdx;
424 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
425 /* Case 1: We can directly reference a single row using an
426 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000427 */
drh8aff1012001-12-22 14:49:24 +0000428 k = iDirectEq[i];
429 assert( k<nExpr );
430 assert( aExpr[k].p!=0 );
431 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
432 if( aExpr[k].idxLeft==idx ){
433 sqliteExprCode(pParse, aExpr[k].p->pRight);
434 }else{
435 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000436 }
drh8aff1012001-12-22 14:49:24 +0000437 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000438 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
439 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000440 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhc4a3c772001-04-04 11:48:57 +0000441 if( i==pTabList->nId-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000442 /* Note: The OP_Dup below will cause the recno to be left on the
443 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000444 ** taken. This violates a general rule of the VDBE that you should
445 ** never leave values on the stack in order to avoid a stack overflow.
446 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000447 ** because the pushKey flag is only true for UPDATE and DELETE, not
448 ** for SELECT, and nested loops only occur on a SELECT.
449 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000450 */
drhc4a3c772001-04-04 11:48:57 +0000451 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000452 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000453 }else{
drhc4a3c772001-04-04 11:48:57 +0000454 haveKey = 0;
455 }
drh6b125452002-01-28 15:53:03 +0000456 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000457 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000458 }else if( pIdx!=0 && pLevel->score%4==0 ){
459 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000460 */
drh6b563442001-11-07 16:48:26 +0000461 int start;
drh487ab3c2001-11-08 00:45:21 +0000462 int testOp;
463 int nColumn = pLevel->score/4;
464 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000465 for(k=0; k<nExpr; k++){
466 if( aExpr[k].p==0 ) continue;
467 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000468 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000469 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000470 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000471 ){
472 sqliteExprCode(pParse, aExpr[k].p->pRight);
473 aExpr[k].p = 0;
474 break;
475 }
476 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000477 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000478 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000479 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000480 ){
481 sqliteExprCode(pParse, aExpr[k].p->pLeft);
482 aExpr[k].p = 0;
483 break;
484 }
485 }
486 }
drh6b563442001-11-07 16:48:26 +0000487 pLevel->iMem = pParse->nMem++;
488 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
489 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000490 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
491 if( nColumn==pIdx->nColumn ){
492 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
493 testOp = OP_IdxGT;
494 }else{
495 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
496 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
497 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
498 testOp = OP_IdxGE;
499 }
drh6b563442001-11-07 16:48:26 +0000500 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
501 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000502 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000503 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000504 if( i==pTabList->nId-1 && pushKey ){
505 haveKey = 1;
506 }else{
drh99fcd712001-10-13 01:06:47 +0000507 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000508 haveKey = 0;
509 }
drh6b563442001-11-07 16:48:26 +0000510 pLevel->op = OP_Next;
511 pLevel->p1 = pLevel->iCur;
512 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000513 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
514 /* Case 3: We have an inequality comparison against the ROWID field.
515 */
516 int testOp = OP_Noop;
517 int start;
518
519 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
520 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
521 if( iDirectGt[i]>=0 ){
522 k = iDirectGt[i];
523 assert( k<nExpr );
524 assert( aExpr[k].p!=0 );
525 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
526 if( aExpr[k].idxLeft==idx ){
527 sqliteExprCode(pParse, aExpr[k].p->pRight);
528 }else{
529 sqliteExprCode(pParse, aExpr[k].p->pLeft);
530 }
531 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
532 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
533 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
534 }
535 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
536 aExpr[k].p = 0;
537 }else{
538 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
539 }
540 if( iDirectLt[i]>=0 ){
541 k = iDirectLt[i];
542 assert( k<nExpr );
543 assert( aExpr[k].p!=0 );
544 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
545 if( aExpr[k].idxLeft==idx ){
546 sqliteExprCode(pParse, aExpr[k].p->pRight);
547 }else{
548 sqliteExprCode(pParse, aExpr[k].p->pLeft);
549 }
550 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
551 pLevel->iMem = pParse->nMem++;
552 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
553 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
554 testOp = OP_Ge;
555 }else{
556 testOp = OP_Gt;
557 }
558 aExpr[k].p = 0;
559 }
560 start = sqliteVdbeCurrentAddr(v);
561 pLevel->op = OP_Next;
562 pLevel->p1 = base+idx;
563 pLevel->p2 = start;
564 if( testOp!=OP_Noop ){
565 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
566 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
567 sqliteVdbeAddOp(v, testOp, 0, brk);
568 }
569 haveKey = 0;
570 }else if( pIdx==0 ){
571 /* Case 4: There was no usable index. We must do a complete
572 ** scan of the entire database table.
573 */
574 int start;
575
576 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
577 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
578 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
579 start = sqliteVdbeCurrentAddr(v);
580 pLevel->op = OP_Next;
581 pLevel->p1 = base+idx;
582 pLevel->p2 = start;
583 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000584 }else{
drhaacc5432002-01-06 17:07:40 +0000585 /* Case 5: The contraint on the right-most index field is
586 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000587 */
588 int score = pLevel->score;
589 int nEqColumn = score/4;
590 int start;
591 int leFlag, geFlag;
592 int testOp;
593
594 /* Evaluate the equality constraints
595 */
596 for(j=0; j<nEqColumn; j++){
597 for(k=0; k<nExpr; k++){
598 if( aExpr[k].p==0 ) continue;
599 if( aExpr[k].idxLeft==idx
600 && aExpr[k].p->op==TK_EQ
601 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
602 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
603 ){
604 sqliteExprCode(pParse, aExpr[k].p->pRight);
605 aExpr[k].p = 0;
606 break;
607 }
608 if( aExpr[k].idxRight==idx
609 && aExpr[k].p->op==TK_EQ
610 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
611 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
612 ){
613 sqliteExprCode(pParse, aExpr[k].p->pLeft);
614 aExpr[k].p = 0;
615 break;
616 }
617 }
618 }
619
620 /* Duplicate the equality contraint values because they will all be
621 ** used twice: once to make the termination key and once to make the
622 ** start key.
623 */
624 for(j=0; j<nEqColumn; j++){
625 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
626 }
627
628 /* Generate the termination key. This is the key value that
629 ** will end the search. There is no termination key if there
630 ** are no equality contraints and no "X<..." constraint.
631 */
632 if( (score & 1)!=0 ){
633 for(k=0; k<nExpr; k++){
634 Expr *pExpr = aExpr[k].p;
635 if( pExpr==0 ) continue;
636 if( aExpr[k].idxLeft==idx
637 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
638 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
639 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
640 ){
641 sqliteExprCode(pParse, pExpr->pRight);
642 leFlag = pExpr->op==TK_LE;
643 aExpr[k].p = 0;
644 break;
645 }
646 if( aExpr[k].idxRight==idx
647 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
648 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
649 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
650 ){
651 sqliteExprCode(pParse, pExpr->pLeft);
652 leFlag = pExpr->op==TK_GE;
653 aExpr[k].p = 0;
654 break;
655 }
656 }
657 testOp = OP_IdxGE;
658 }else{
659 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
660 leFlag = 1;
661 }
662 if( testOp!=OP_Noop ){
663 pLevel->iMem = pParse->nMem++;
664 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
665 if( leFlag ){
666 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
667 }
668 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
669 }
670
671 /* Generate the start key. This is the key that defines the lower
672 ** bound on the search. There is no start key if there are not
673 ** equality constraints and if there is no "X>..." constraint. In
674 ** that case, generate a "Rewind" instruction in place of the
675 ** start key search.
676 */
677 if( (score & 2)!=0 ){
678 for(k=0; k<nExpr; k++){
679 Expr *pExpr = aExpr[k].p;
680 if( pExpr==0 ) continue;
681 if( aExpr[k].idxLeft==idx
682 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
683 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
684 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
685 ){
686 sqliteExprCode(pParse, pExpr->pRight);
687 geFlag = pExpr->op==TK_GE;
688 aExpr[k].p = 0;
689 break;
690 }
691 if( aExpr[k].idxRight==idx
692 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
693 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
694 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
695 ){
696 sqliteExprCode(pParse, pExpr->pLeft);
697 geFlag = pExpr->op==TK_LE;
698 aExpr[k].p = 0;
699 break;
700 }
701 }
drh7900ead2001-11-12 13:51:43 +0000702 }else{
703 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000704 }
705 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
706 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
707 if( nEqColumn>0 || (score&2)!=0 ){
708 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
709 if( !geFlag ){
710 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
711 }
712 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
713 }else{
714 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
715 }
716
717 /* Generate the the top of the loop. If there is a termination
718 ** key we have to test for that key and abort at the top of the
719 ** loop.
720 */
721 start = sqliteVdbeCurrentAddr(v);
722 if( testOp!=OP_Noop ){
723 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
724 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
725 }
726 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
727 if( i==pTabList->nId-1 && pushKey ){
728 haveKey = 1;
729 }else{
730 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
731 haveKey = 0;
732 }
733
734 /* Record the instruction used to terminate the loop.
735 */
736 pLevel->op = OP_Next;
737 pLevel->p1 = pLevel->iCur;
738 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000739 }
740 loopMask |= 1<<idx;
741
742 /* Insert code to test every subexpression that can be completely
743 ** computed using the current set of tables.
744 */
745 for(j=0; j<nExpr; j++){
746 if( aExpr[j].p==0 ) continue;
747 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
748 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
749 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000750 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000751 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000752 }
753 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
754 aExpr[j].p = 0;
755 }
756 brk = cont;
757 }
758 pWInfo->iContinue = cont;
759 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000760 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000761 }
762 sqliteFree(aOrder);
763 return pWInfo;
764}
765
766/*
767** Generate the end of the WHERE loop.
768*/
769void sqliteWhereEnd(WhereInfo *pWInfo){
770 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000771 int i;
drh19a775c2000-06-05 18:54:46 +0000772 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000773 WhereLevel *pLevel;
drh19a775c2000-06-05 18:54:46 +0000774
drh6b563442001-11-07 16:48:26 +0000775 for(i=pWInfo->pTabList->nId-1; i>=0; i--){
776 pLevel = &pWInfo->a[i];
777 sqliteVdbeResolveLabel(v, pLevel->cont);
778 if( pLevel->op!=OP_Noop ){
779 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000780 }
drh6b563442001-11-07 16:48:26 +0000781 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000782 }
drh6b563442001-11-07 16:48:26 +0000783 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
784 for(i=0; i<pWInfo->pTabList->nId; i++){
785 pLevel = &pWInfo->a[i];
786 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
787 if( pLevel->pIdx!=0 ){
788 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
789 }
drh19a775c2000-06-05 18:54:46 +0000790 }
drh75897232000-05-29 14:26:00 +0000791 sqliteFree(pWInfo);
792 return;
793}