blob: 48cf4b302665351f984d4a47d53ccd748ef3c78f [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**
drh22f70c32002-02-18 01:17:00 +000016** $Id: where.c,v 1.35 2002/02/18 01:17:00 drh Exp $
drh75897232000-05-29 14:26:00 +000017*/
18#include "sqliteInt.h"
19
20/*
21** The query generator uses an array of instances of this structure to
22** help it analyze the subexpressions of the WHERE clause. Each WHERE
23** clause subexpression is separated from the others by an AND operator.
24*/
25typedef struct ExprInfo ExprInfo;
26struct ExprInfo {
27 Expr *p; /* Pointer to the subexpression */
28 int indexable; /* True if this subexprssion is usable by an index */
drh967e8b72000-06-21 13:59:10 +000029 int idxLeft; /* p->pLeft is a column in this table number. -1 if
30 ** p->pLeft is not the column of any table */
31 int idxRight; /* p->pRight is a column in this table number. -1 if
32 ** p->pRight is not the column of any table */
drh75897232000-05-29 14:26:00 +000033 unsigned prereqLeft; /* Tables referenced by p->pLeft */
34 unsigned prereqRight; /* Tables referenced by p->pRight */
35};
36
37/*
38** Determine the number of elements in an array.
39*/
40#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
41
42/*
43** This routine is used to divide the WHERE expression into subexpressions
44** separated by the AND operator.
45**
46** aSlot[] is an array of subexpressions structures.
47** There are nSlot spaces left in this array. This routine attempts to
48** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
49** The return value is the number of slots filled.
50*/
51static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
52 int cnt = 0;
53 if( pExpr==0 || nSlot<1 ) return 0;
54 if( nSlot==1 || pExpr->op!=TK_AND ){
55 aSlot[0].p = pExpr;
56 return 1;
57 }
58 if( pExpr->pLeft->op!=TK_AND ){
59 aSlot[0].p = pExpr->pLeft;
60 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
61 }else{
62 cnt = exprSplit(nSlot, aSlot, pExpr->pRight);
63 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft);
64 }
65 return cnt;
66}
67
68/*
69** This routine walks (recursively) an expression tree and generates
70** a bitmask indicating which tables are used in that expression
71** tree. Bit 0 of the mask is set if table 0 is used. But 1 is set
72** if table 1 is used. And so forth.
73**
74** In order for this routine to work, the calling function must have
75** previously invoked sqliteExprResolveIds() on the expression. See
76** the header comment on that routine for additional information.
drh19a775c2000-06-05 18:54:46 +000077**
78** "base" is the cursor number (the value of the iTable field) that
79** corresponds to the first entry in the table list. This is the
80** same as pParse->nTab.
drh75897232000-05-29 14:26:00 +000081*/
drh19a775c2000-06-05 18:54:46 +000082static int exprTableUsage(int base, Expr *p){
drh75897232000-05-29 14:26:00 +000083 unsigned int mask = 0;
84 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +000085 if( p->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +000086 return 1<< (p->iTable - base);
drh75897232000-05-29 14:26:00 +000087 }
88 if( p->pRight ){
drh19a775c2000-06-05 18:54:46 +000089 mask = exprTableUsage(base, p->pRight);
drh75897232000-05-29 14:26:00 +000090 }
91 if( p->pLeft ){
drh19a775c2000-06-05 18:54:46 +000092 mask |= exprTableUsage(base, p->pLeft);
drh75897232000-05-29 14:26:00 +000093 }
94 return mask;
95}
96
97/*
drh487ab3c2001-11-08 00:45:21 +000098** Return TRUE if the given operator is one of the operators that is
99** allowed for an indexable WHERE clause. The allowed operators are
100** "=", "<", ">", "<=", and ">=".
101*/
102static int allowedOp(int op){
103 switch( op ){
104 case TK_LT:
105 case TK_LE:
106 case TK_GT:
107 case TK_GE:
108 case TK_EQ:
109 return 1;
110 default:
111 return 0;
112 }
113}
114
115/*
drh75897232000-05-29 14:26:00 +0000116** The input to this routine is an ExprInfo structure with only the
117** "p" field filled in. The job of this routine is to analyze the
118** subexpression and populate all the other fields of the ExprInfo
119** structure.
drh19a775c2000-06-05 18:54:46 +0000120**
121** "base" is the cursor number (the value of the iTable field) that
drh80ff32f2001-11-04 18:32:46 +0000122** corresponds to the first entry in the table list. This is the
drh19a775c2000-06-05 18:54:46 +0000123** same as pParse->nTab.
drh75897232000-05-29 14:26:00 +0000124*/
drh19a775c2000-06-05 18:54:46 +0000125static void exprAnalyze(int base, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000126 Expr *pExpr = pInfo->p;
drh19a775c2000-06-05 18:54:46 +0000127 pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
128 pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
drh75897232000-05-29 14:26:00 +0000129 pInfo->indexable = 0;
130 pInfo->idxLeft = -1;
131 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000132 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drh967e8b72000-06-21 13:59:10 +0000133 if( pExpr->pRight->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000134 pInfo->idxRight = pExpr->pRight->iTable - base;
drh75897232000-05-29 14:26:00 +0000135 pInfo->indexable = 1;
136 }
drh967e8b72000-06-21 13:59:10 +0000137 if( pExpr->pLeft->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000138 pInfo->idxLeft = pExpr->pLeft->iTable - base;
drh75897232000-05-29 14:26:00 +0000139 pInfo->indexable = 1;
140 }
141 }
142}
143
144/*
145** Generating the beginning of the loop used for WHERE clause processing.
146** The return value is a pointer to an (opaque) structure that contains
147** information needed to terminate the loop. Later, the calling routine
148** should invoke sqliteWhereEnd() with the return value of this function
149** in order to complete the WHERE clause processing.
150**
151** If an error occurs, this routine returns NULL.
152*/
153WhereInfo *sqliteWhereBegin(
154 Parse *pParse, /* The parser context */
155 IdList *pTabList, /* A list of all tables */
156 Expr *pWhere, /* The WHERE clause */
157 int pushKey /* If TRUE, leave the table key on the stack */
158){
159 int i; /* Loop counter */
160 WhereInfo *pWInfo; /* Will become the return value of this function */
161 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
162 int brk, cont; /* Addresses used during code generation */
163 int *aOrder; /* Order in which pTabList entries are searched */
164 int nExpr; /* Number of subexpressions in the WHERE clause */
165 int loopMask; /* One bit set for each outer loop */
166 int haveKey; /* True if KEY is on the stack */
drh19a775c2000-06-05 18:54:46 +0000167 int base; /* First available index for OP_Open opcodes */
drh6b563442001-11-07 16:48:26 +0000168 int nCur; /* Next unused cursor number */
drhc4a3c772001-04-04 11:48:57 +0000169 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh8aff1012001-12-22 14:49:24 +0000170 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
171 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
172 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh75897232000-05-29 14:26:00 +0000173 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
174
drh6b563442001-11-07 16:48:26 +0000175 /* Allocate space for aOrder[] and aiMem[]. */
drh75897232000-05-29 14:26:00 +0000176 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
177
178 /* Allocate and initialize the WhereInfo structure that will become the
179 ** return value.
180 */
drh6b563442001-11-07 16:48:26 +0000181 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) );
drhdaffd0e2001-04-11 14:28:42 +0000182 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000183 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000184 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000185 return 0;
186 }
187 pWInfo->pParse = pParse;
188 pWInfo->pTabList = pTabList;
drh19a775c2000-06-05 18:54:46 +0000189 base = pWInfo->base = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000190 nCur = base + pTabList->nId;
drh22f70c32002-02-18 01:17:00 +0000191 pParse->nTab += nCur*2;
drh75897232000-05-29 14:26:00 +0000192
193 /* Split the WHERE clause into as many as 32 separate subexpressions
194 ** where each subexpression is separated by an AND operator. Any additional
195 ** subexpressions are attached in the aExpr[32] and will not enter
196 ** into the query optimizer computations. 32 is chosen as the cutoff
197 ** since that is the number of bits in an integer that we use for an
198 ** expression-used mask.
199 */
200 memset(aExpr, 0, sizeof(aExpr));
201 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
202
203 /* Analyze all of the subexpressions.
204 */
205 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000206 exprAnalyze(base, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000207 }
208
209 /* Figure out a good nesting order for the tables. aOrder[0] will
210 ** be the index in pTabList of the outermost table. aOrder[1] will
211 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
212 ** be the innermost loop.
213 **
drh7e391e12000-05-30 20:17:49 +0000214 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000215 ** for an effiecient query. But for now, just use whatever order the
216 ** tables appear in in the pTabList.
217 */
218 for(i=0; i<pTabList->nId; i++){
219 aOrder[i] = i;
220 }
221
222 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000223 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
224 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000225 ** loop.
226 **
227 ** If terms exist that use the ROWID of any table, then set the
228 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
229 ** to the index of the term containing the ROWID. We always prefer
230 ** to use a ROWID which can directly access a table rather than an
231 ** index which requires two accesses.
drh75897232000-05-29 14:26:00 +0000232 **
233 ** Actually, if there are more than 32 tables in the join, only the
234 ** first 32 tables are candidates for indices.
235 */
236 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000237 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000238 int j;
drh75897232000-05-29 14:26:00 +0000239 int idx = aOrder[i];
240 Table *pTab = pTabList->a[idx].pTab;
241 Index *pIdx;
242 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000243 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000244
drhc4a3c772001-04-04 11:48:57 +0000245 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000246 ** ROWID field of this table. For terms of the form ROWID==expr
247 ** set iDirectEq[i] to the index of the term. For terms of the
248 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
249 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000250 */
drh8aff1012001-12-22 14:49:24 +0000251 iDirectEq[i] = -1;
252 iDirectLt[i] = -1;
253 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000254 for(j=0; j<nExpr; j++){
255 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
256 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000257 switch( aExpr[j].p->op ){
258 case TK_EQ: iDirectEq[i] = j; break;
259 case TK_LE:
260 case TK_LT: iDirectLt[i] = j; break;
261 case TK_GE:
262 case TK_GT: iDirectGt[i] = j; break;
263 }
drhc4a3c772001-04-04 11:48:57 +0000264 }
265 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
266 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000267 switch( aExpr[j].p->op ){
268 case TK_EQ: iDirectEq[i] = j; break;
269 case TK_LE:
270 case TK_LT: iDirectGt[i] = j; break;
271 case TK_GE:
272 case TK_GT: iDirectLt[i] = j; break;
273 }
drhc4a3c772001-04-04 11:48:57 +0000274 }
275 }
drh8aff1012001-12-22 14:49:24 +0000276 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000277 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000278 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000279 continue;
280 }
281
drh75897232000-05-29 14:26:00 +0000282 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000283 ** the "best" index. pBestIdx is left set to NULL if no indices
284 ** are usable.
drh75897232000-05-29 14:26:00 +0000285 **
drh487ab3c2001-11-08 00:45:21 +0000286 ** The best index is determined as follows. For each of the
287 ** left-most terms that is fixed by an equality operator, add
288 ** 4 to the score. The right-most term of the index may be
289 ** constrained by an inequality. Add 1 if for an "x<..." constraint
290 ** and add 2 for an "x>..." constraint. Chose the index that
291 ** gives the best score.
292 **
293 ** This scoring system is designed so that the score can later be
294 ** used to determine how the index is used. If the score&3 is 0
295 ** then all constraints are equalities. If score&1 is not 0 then
296 ** there is an inequality used as a termination key. (ex: "x<...")
297 ** If score&2 is not 0 then there is an inequality used as the
298 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000299 */
300 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000301 int eqMask = 0; /* Index columns covered by an x=... constraint */
302 int ltMask = 0; /* Index columns covered by an x<... constraint */
303 int gtMask = 0; /* Index columns covered by an x>... constraing */
304 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000305
drh74e24cd2002-01-09 03:19:59 +0000306 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000307 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000308 for(j=0; j<nExpr; j++){
309 if( aExpr[j].idxLeft==idx
310 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000311 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000312 int k;
drh967e8b72000-06-21 13:59:10 +0000313 for(k=0; k<pIdx->nColumn; k++){
314 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000315 switch( aExpr[j].p->op ){
316 case TK_EQ: {
317 eqMask |= 1<<k;
318 break;
319 }
320 case TK_LE:
321 case TK_LT: {
322 ltMask |= 1<<k;
323 break;
324 }
325 case TK_GE:
326 case TK_GT: {
327 gtMask |= 1<<k;
328 break;
329 }
330 default: {
331 /* CANT_HAPPEN */
332 assert( 0 );
333 break;
334 }
335 }
drh75897232000-05-29 14:26:00 +0000336 break;
337 }
338 }
339 }
340 if( aExpr[j].idxRight==idx
341 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000342 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000343 int k;
drh967e8b72000-06-21 13:59:10 +0000344 for(k=0; k<pIdx->nColumn; k++){
345 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000346 switch( aExpr[j].p->op ){
347 case TK_EQ: {
348 eqMask |= 1<<k;
349 break;
350 }
351 case TK_LE:
352 case TK_LT: {
353 gtMask |= 1<<k;
354 break;
355 }
356 case TK_GE:
357 case TK_GT: {
358 ltMask |= 1<<k;
359 break;
360 }
361 default: {
362 /* CANT_HAPPEN */
363 assert( 0 );
364 break;
365 }
366 }
drh75897232000-05-29 14:26:00 +0000367 break;
368 }
369 }
370 }
371 }
drh487ab3c2001-11-08 00:45:21 +0000372 for(nEq=0; nEq<pIdx->nColumn; nEq++){
373 m = (1<<(nEq+1))-1;
374 if( (m & eqMask)!=m ) break;
375 }
376 score = nEq*4;
377 m = 1<<nEq;
378 if( m & ltMask ) score++;
379 if( m & gtMask ) score+=2;
380 if( score>bestScore ){
381 pBestIdx = pIdx;
382 bestScore = score;
drh75897232000-05-29 14:26:00 +0000383 }
384 }
drh6b563442001-11-07 16:48:26 +0000385 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000386 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000387 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000388 if( pBestIdx ){
389 pWInfo->a[i].iCur = nCur++;
390 }
drh75897232000-05-29 14:26:00 +0000391 }
392
drh6b563442001-11-07 16:48:26 +0000393 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000394 */
395 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000396 int openOp;
397 Table *pTab;
398
399 pTab = pTabList->a[i].pTab;
drh22f70c32002-02-18 01:17:00 +0000400 if( pTab->isTransient ) continue;
drhf57b3392001-10-08 13:22:32 +0000401 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000402 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
403 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000404 if( i==0 && !pParse->schemaVerified &&
405 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000406 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000407 pParse->schemaVerified = 1;
408 }
drh6b563442001-11-07 16:48:26 +0000409 if( pWInfo->a[i].pIdx!=0 ){
410 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
411 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000412 }
413 }
414
415 /* Generate the code to do the search
416 */
drh75897232000-05-29 14:26:00 +0000417 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000418 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000419 for(i=0; i<pTabList->nId; i++){
420 int j, k;
421 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000422 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000423 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000424
drh8aff1012001-12-22 14:49:24 +0000425 pIdx = pLevel->pIdx;
426 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
427 /* Case 1: We can directly reference a single row using an
428 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000429 */
drh8aff1012001-12-22 14:49:24 +0000430 k = iDirectEq[i];
431 assert( k<nExpr );
432 assert( aExpr[k].p!=0 );
433 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
434 if( aExpr[k].idxLeft==idx ){
435 sqliteExprCode(pParse, aExpr[k].p->pRight);
436 }else{
437 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000438 }
drh8aff1012001-12-22 14:49:24 +0000439 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000440 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
441 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000442 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhc4a3c772001-04-04 11:48:57 +0000443 if( i==pTabList->nId-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000444 /* Note: The OP_Dup below will cause the recno to be left on the
445 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000446 ** taken. This violates a general rule of the VDBE that you should
447 ** never leave values on the stack in order to avoid a stack overflow.
448 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000449 ** because the pushKey flag is only true for UPDATE and DELETE, not
450 ** for SELECT, and nested loops only occur on a SELECT.
451 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000452 */
drhc4a3c772001-04-04 11:48:57 +0000453 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000454 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000455 }else{
drhc4a3c772001-04-04 11:48:57 +0000456 haveKey = 0;
457 }
drh6b125452002-01-28 15:53:03 +0000458 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000459 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000460 }else if( pIdx!=0 && pLevel->score%4==0 ){
461 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000462 */
drh6b563442001-11-07 16:48:26 +0000463 int start;
drh487ab3c2001-11-08 00:45:21 +0000464 int testOp;
465 int nColumn = pLevel->score/4;
466 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000467 for(k=0; k<nExpr; k++){
468 if( aExpr[k].p==0 ) continue;
469 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000470 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000471 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000472 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000473 ){
474 sqliteExprCode(pParse, aExpr[k].p->pRight);
475 aExpr[k].p = 0;
476 break;
477 }
478 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000479 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000480 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000481 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000482 ){
483 sqliteExprCode(pParse, aExpr[k].p->pLeft);
484 aExpr[k].p = 0;
485 break;
486 }
487 }
488 }
drh6b563442001-11-07 16:48:26 +0000489 pLevel->iMem = pParse->nMem++;
490 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
491 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000492 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
493 if( nColumn==pIdx->nColumn ){
494 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
495 testOp = OP_IdxGT;
496 }else{
497 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
498 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
499 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
500 testOp = OP_IdxGE;
501 }
drh6b563442001-11-07 16:48:26 +0000502 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
503 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000504 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000505 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000506 if( i==pTabList->nId-1 && pushKey ){
507 haveKey = 1;
508 }else{
drh99fcd712001-10-13 01:06:47 +0000509 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000510 haveKey = 0;
511 }
drh6b563442001-11-07 16:48:26 +0000512 pLevel->op = OP_Next;
513 pLevel->p1 = pLevel->iCur;
514 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000515 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
516 /* Case 3: We have an inequality comparison against the ROWID field.
517 */
518 int testOp = OP_Noop;
519 int start;
520
521 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
522 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
523 if( iDirectGt[i]>=0 ){
524 k = iDirectGt[i];
525 assert( k<nExpr );
526 assert( aExpr[k].p!=0 );
527 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
528 if( aExpr[k].idxLeft==idx ){
529 sqliteExprCode(pParse, aExpr[k].p->pRight);
530 }else{
531 sqliteExprCode(pParse, aExpr[k].p->pLeft);
532 }
533 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
534 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
535 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
536 }
537 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
538 aExpr[k].p = 0;
539 }else{
540 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
541 }
542 if( iDirectLt[i]>=0 ){
543 k = iDirectLt[i];
544 assert( k<nExpr );
545 assert( aExpr[k].p!=0 );
546 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
547 if( aExpr[k].idxLeft==idx ){
548 sqliteExprCode(pParse, aExpr[k].p->pRight);
549 }else{
550 sqliteExprCode(pParse, aExpr[k].p->pLeft);
551 }
552 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
553 pLevel->iMem = pParse->nMem++;
554 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
555 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
556 testOp = OP_Ge;
557 }else{
558 testOp = OP_Gt;
559 }
560 aExpr[k].p = 0;
561 }
562 start = sqliteVdbeCurrentAddr(v);
563 pLevel->op = OP_Next;
564 pLevel->p1 = base+idx;
565 pLevel->p2 = start;
566 if( testOp!=OP_Noop ){
567 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
568 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
569 sqliteVdbeAddOp(v, testOp, 0, brk);
570 }
571 haveKey = 0;
572 }else if( pIdx==0 ){
573 /* Case 4: There was no usable index. We must do a complete
574 ** scan of the entire database table.
575 */
576 int start;
577
578 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
579 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
580 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
581 start = sqliteVdbeCurrentAddr(v);
582 pLevel->op = OP_Next;
583 pLevel->p1 = base+idx;
584 pLevel->p2 = start;
585 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000586 }else{
drhaacc5432002-01-06 17:07:40 +0000587 /* Case 5: The contraint on the right-most index field is
588 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000589 */
590 int score = pLevel->score;
591 int nEqColumn = score/4;
592 int start;
593 int leFlag, geFlag;
594 int testOp;
595
596 /* Evaluate the equality constraints
597 */
598 for(j=0; j<nEqColumn; j++){
599 for(k=0; k<nExpr; k++){
600 if( aExpr[k].p==0 ) continue;
601 if( aExpr[k].idxLeft==idx
602 && aExpr[k].p->op==TK_EQ
603 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
604 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
605 ){
606 sqliteExprCode(pParse, aExpr[k].p->pRight);
607 aExpr[k].p = 0;
608 break;
609 }
610 if( aExpr[k].idxRight==idx
611 && aExpr[k].p->op==TK_EQ
612 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
613 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
614 ){
615 sqliteExprCode(pParse, aExpr[k].p->pLeft);
616 aExpr[k].p = 0;
617 break;
618 }
619 }
620 }
621
622 /* Duplicate the equality contraint values because they will all be
623 ** used twice: once to make the termination key and once to make the
624 ** start key.
625 */
626 for(j=0; j<nEqColumn; j++){
627 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
628 }
629
630 /* Generate the termination key. This is the key value that
631 ** will end the search. There is no termination key if there
632 ** are no equality contraints and no "X<..." constraint.
633 */
634 if( (score & 1)!=0 ){
635 for(k=0; k<nExpr; k++){
636 Expr *pExpr = aExpr[k].p;
637 if( pExpr==0 ) continue;
638 if( aExpr[k].idxLeft==idx
639 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
640 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
641 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
642 ){
643 sqliteExprCode(pParse, pExpr->pRight);
644 leFlag = pExpr->op==TK_LE;
645 aExpr[k].p = 0;
646 break;
647 }
648 if( aExpr[k].idxRight==idx
649 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
650 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
651 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
652 ){
653 sqliteExprCode(pParse, pExpr->pLeft);
654 leFlag = pExpr->op==TK_GE;
655 aExpr[k].p = 0;
656 break;
657 }
658 }
659 testOp = OP_IdxGE;
660 }else{
661 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
662 leFlag = 1;
663 }
664 if( testOp!=OP_Noop ){
665 pLevel->iMem = pParse->nMem++;
666 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
667 if( leFlag ){
668 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
669 }
670 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
671 }
672
673 /* Generate the start key. This is the key that defines the lower
674 ** bound on the search. There is no start key if there are not
675 ** equality constraints and if there is no "X>..." constraint. In
676 ** that case, generate a "Rewind" instruction in place of the
677 ** start key search.
678 */
679 if( (score & 2)!=0 ){
680 for(k=0; k<nExpr; k++){
681 Expr *pExpr = aExpr[k].p;
682 if( pExpr==0 ) continue;
683 if( aExpr[k].idxLeft==idx
684 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
685 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
686 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
687 ){
688 sqliteExprCode(pParse, pExpr->pRight);
689 geFlag = pExpr->op==TK_GE;
690 aExpr[k].p = 0;
691 break;
692 }
693 if( aExpr[k].idxRight==idx
694 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
695 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
696 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
697 ){
698 sqliteExprCode(pParse, pExpr->pLeft);
699 geFlag = pExpr->op==TK_LE;
700 aExpr[k].p = 0;
701 break;
702 }
703 }
drh7900ead2001-11-12 13:51:43 +0000704 }else{
705 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000706 }
707 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
708 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
709 if( nEqColumn>0 || (score&2)!=0 ){
710 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
711 if( !geFlag ){
712 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
713 }
714 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
715 }else{
716 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
717 }
718
719 /* Generate the the top of the loop. If there is a termination
720 ** key we have to test for that key and abort at the top of the
721 ** loop.
722 */
723 start = sqliteVdbeCurrentAddr(v);
724 if( testOp!=OP_Noop ){
725 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
726 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
727 }
728 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
729 if( i==pTabList->nId-1 && pushKey ){
730 haveKey = 1;
731 }else{
732 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
733 haveKey = 0;
734 }
735
736 /* Record the instruction used to terminate the loop.
737 */
738 pLevel->op = OP_Next;
739 pLevel->p1 = pLevel->iCur;
740 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000741 }
742 loopMask |= 1<<idx;
743
744 /* Insert code to test every subexpression that can be completely
745 ** computed using the current set of tables.
746 */
747 for(j=0; j<nExpr; j++){
748 if( aExpr[j].p==0 ) continue;
749 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
750 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
751 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000752 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000753 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000754 }
755 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
756 aExpr[j].p = 0;
757 }
758 brk = cont;
759 }
760 pWInfo->iContinue = cont;
761 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000762 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000763 }
764 sqliteFree(aOrder);
765 return pWInfo;
766}
767
768/*
769** Generate the end of the WHERE loop.
770*/
771void sqliteWhereEnd(WhereInfo *pWInfo){
772 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000773 int i;
drh19a775c2000-06-05 18:54:46 +0000774 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000775 WhereLevel *pLevel;
drh22f70c32002-02-18 01:17:00 +0000776 IdList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000777
drh22f70c32002-02-18 01:17:00 +0000778 for(i=pTabList->nId-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000779 pLevel = &pWInfo->a[i];
780 sqliteVdbeResolveLabel(v, pLevel->cont);
781 if( pLevel->op!=OP_Noop ){
782 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000783 }
drh6b563442001-11-07 16:48:26 +0000784 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000785 }
drh6b563442001-11-07 16:48:26 +0000786 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drh22f70c32002-02-18 01:17:00 +0000787 for(i=0; i<pTabList->nId; i++){
788 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000789 pLevel = &pWInfo->a[i];
790 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
791 if( pLevel->pIdx!=0 ){
792 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
793 }
drh19a775c2000-06-05 18:54:46 +0000794 }
drh22f70c32002-02-18 01:17:00 +0000795 pWInfo->pParse->nTab = base;
drh75897232000-05-29 14:26:00 +0000796 sqliteFree(pWInfo);
797 return;
798}