blob: f1736c9c7b7d5bbe6817dbd4586171ea276ebaa4 [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**
drhdd579122002-04-02 01:58:57 +000016** $Id: where.c,v 1.39 2002/04/02 01:58:58 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
drh832508b2002-03-02 17:04:07 +000079** corresponds to the first entry in the table list.
drh75897232000-05-29 14:26:00 +000080*/
drh19a775c2000-06-05 18:54:46 +000081static int exprTableUsage(int base, Expr *p){
drh75897232000-05-29 14:26:00 +000082 unsigned int mask = 0;
83 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +000084 if( p->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +000085 return 1<< (p->iTable - base);
drh75897232000-05-29 14:26:00 +000086 }
87 if( p->pRight ){
drh19a775c2000-06-05 18:54:46 +000088 mask = exprTableUsage(base, p->pRight);
drh75897232000-05-29 14:26:00 +000089 }
90 if( p->pLeft ){
drh19a775c2000-06-05 18:54:46 +000091 mask |= exprTableUsage(base, p->pLeft);
drh75897232000-05-29 14:26:00 +000092 }
drhdd579122002-04-02 01:58:57 +000093 if( p->pList ){
94 int i;
95 for(i=0; i<p->pList->nExpr; i++){
96 mask |= exprTableUsage(base, p->pList->a[i].pExpr);
97 }
98 }
drh75897232000-05-29 14:26:00 +000099 return mask;
100}
101
102/*
drh487ab3c2001-11-08 00:45:21 +0000103** Return TRUE if the given operator is one of the operators that is
104** allowed for an indexable WHERE clause. The allowed operators are
105** "=", "<", ">", "<=", and ">=".
106*/
107static int allowedOp(int op){
108 switch( op ){
109 case TK_LT:
110 case TK_LE:
111 case TK_GT:
112 case TK_GE:
113 case TK_EQ:
114 return 1;
115 default:
116 return 0;
117 }
118}
119
120/*
drh75897232000-05-29 14:26:00 +0000121** The input to this routine is an ExprInfo structure with only the
122** "p" field filled in. The job of this routine is to analyze the
123** subexpression and populate all the other fields of the ExprInfo
124** structure.
drh19a775c2000-06-05 18:54:46 +0000125**
126** "base" is the cursor number (the value of the iTable field) that
drh832508b2002-03-02 17:04:07 +0000127** corresponds to the first entry in the table list.
drh75897232000-05-29 14:26:00 +0000128*/
drh19a775c2000-06-05 18:54:46 +0000129static void exprAnalyze(int base, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000130 Expr *pExpr = pInfo->p;
drh19a775c2000-06-05 18:54:46 +0000131 pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
132 pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
drh75897232000-05-29 14:26:00 +0000133 pInfo->indexable = 0;
134 pInfo->idxLeft = -1;
135 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000136 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drh967e8b72000-06-21 13:59:10 +0000137 if( pExpr->pRight->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000138 pInfo->idxRight = pExpr->pRight->iTable - base;
drh75897232000-05-29 14:26:00 +0000139 pInfo->indexable = 1;
140 }
drh967e8b72000-06-21 13:59:10 +0000141 if( pExpr->pLeft->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000142 pInfo->idxLeft = pExpr->pLeft->iTable - base;
drh75897232000-05-29 14:26:00 +0000143 pInfo->indexable = 1;
144 }
145 }
146}
147
148/*
149** Generating the beginning of the loop used for WHERE clause processing.
150** The return value is a pointer to an (opaque) structure that contains
151** information needed to terminate the loop. Later, the calling routine
152** should invoke sqliteWhereEnd() with the return value of this function
153** in order to complete the WHERE clause processing.
154**
155** If an error occurs, this routine returns NULL.
156*/
157WhereInfo *sqliteWhereBegin(
158 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000159 int base, /* VDBE cursor index for left-most table in pTabList */
drh75897232000-05-29 14:26:00 +0000160 IdList *pTabList, /* A list of all tables */
161 Expr *pWhere, /* The WHERE clause */
162 int pushKey /* If TRUE, leave the table key on the stack */
163){
164 int i; /* Loop counter */
165 WhereInfo *pWInfo; /* Will become the return value of this function */
166 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
167 int brk, cont; /* Addresses used during code generation */
168 int *aOrder; /* Order in which pTabList entries are searched */
169 int nExpr; /* Number of subexpressions in the WHERE clause */
170 int loopMask; /* One bit set for each outer loop */
171 int haveKey; /* True if KEY is on the stack */
drhc4a3c772001-04-04 11:48:57 +0000172 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh8aff1012001-12-22 14:49:24 +0000173 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
174 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
175 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh75897232000-05-29 14:26:00 +0000176 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
177
drh6b563442001-11-07 16:48:26 +0000178 /* Allocate space for aOrder[] and aiMem[]. */
drh75897232000-05-29 14:26:00 +0000179 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
180
181 /* Allocate and initialize the WhereInfo structure that will become the
182 ** return value.
183 */
drh6b563442001-11-07 16:48:26 +0000184 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) );
drhdaffd0e2001-04-11 14:28:42 +0000185 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000186 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000187 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000188 return 0;
189 }
190 pWInfo->pParse = pParse;
191 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000192 pWInfo->base = base;
193 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
drh75897232000-05-29 14:26:00 +0000194
195 /* Split the WHERE clause into as many as 32 separate subexpressions
196 ** where each subexpression is separated by an AND operator. Any additional
197 ** subexpressions are attached in the aExpr[32] and will not enter
198 ** into the query optimizer computations. 32 is chosen as the cutoff
199 ** since that is the number of bits in an integer that we use for an
200 ** expression-used mask.
201 */
202 memset(aExpr, 0, sizeof(aExpr));
203 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
204
205 /* Analyze all of the subexpressions.
206 */
207 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000208 exprAnalyze(base, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000209 }
210
211 /* Figure out a good nesting order for the tables. aOrder[0] will
212 ** be the index in pTabList of the outermost table. aOrder[1] will
213 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
214 ** be the innermost loop.
215 **
drh7e391e12000-05-30 20:17:49 +0000216 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000217 ** for an effiecient query. But for now, just use whatever order the
218 ** tables appear in in the pTabList.
219 */
220 for(i=0; i<pTabList->nId; i++){
221 aOrder[i] = i;
222 }
223
224 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000225 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
226 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000227 ** loop.
228 **
229 ** If terms exist that use the ROWID of any table, then set the
230 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
231 ** to the index of the term containing the ROWID. We always prefer
232 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000233 ** index which requires reading an index first to get the rowid then
234 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000235 **
236 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000237 ** first 32 tables are candidates for indices. This is (again) due
238 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000239 */
240 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000241 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000242 int j;
drh75897232000-05-29 14:26:00 +0000243 int idx = aOrder[i];
244 Table *pTab = pTabList->a[idx].pTab;
245 Index *pIdx;
246 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000247 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000248
drhc4a3c772001-04-04 11:48:57 +0000249 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000250 ** ROWID field of this table. For terms of the form ROWID==expr
251 ** set iDirectEq[i] to the index of the term. For terms of the
252 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
253 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000254 */
drh8aff1012001-12-22 14:49:24 +0000255 iDirectEq[i] = -1;
256 iDirectLt[i] = -1;
257 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000258 for(j=0; j<nExpr; j++){
259 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
260 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000261 switch( aExpr[j].p->op ){
262 case TK_EQ: iDirectEq[i] = j; break;
263 case TK_LE:
264 case TK_LT: iDirectLt[i] = j; break;
265 case TK_GE:
266 case TK_GT: iDirectGt[i] = j; break;
267 }
drhc4a3c772001-04-04 11:48:57 +0000268 }
269 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
270 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000271 switch( aExpr[j].p->op ){
272 case TK_EQ: iDirectEq[i] = j; break;
273 case TK_LE:
274 case TK_LT: iDirectGt[i] = j; break;
275 case TK_GE:
276 case TK_GT: iDirectLt[i] = j; break;
277 }
drhc4a3c772001-04-04 11:48:57 +0000278 }
279 }
drh8aff1012001-12-22 14:49:24 +0000280 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000281 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000282 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000283 continue;
284 }
285
drh75897232000-05-29 14:26:00 +0000286 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000287 ** the "best" index. pBestIdx is left set to NULL if no indices
288 ** are usable.
drh75897232000-05-29 14:26:00 +0000289 **
drh487ab3c2001-11-08 00:45:21 +0000290 ** The best index is determined as follows. For each of the
291 ** left-most terms that is fixed by an equality operator, add
292 ** 4 to the score. The right-most term of the index may be
293 ** constrained by an inequality. Add 1 if for an "x<..." constraint
294 ** and add 2 for an "x>..." constraint. Chose the index that
295 ** gives the best score.
296 **
297 ** This scoring system is designed so that the score can later be
298 ** used to determine how the index is used. If the score&3 is 0
299 ** then all constraints are equalities. If score&1 is not 0 then
300 ** there is an inequality used as a termination key. (ex: "x<...")
301 ** If score&2 is not 0 then there is an inequality used as the
302 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000303 */
304 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000305 int eqMask = 0; /* Index columns covered by an x=... constraint */
306 int ltMask = 0; /* Index columns covered by an x<... constraint */
307 int gtMask = 0; /* Index columns covered by an x>... constraing */
308 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000309
drh74e24cd2002-01-09 03:19:59 +0000310 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000311 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000312 for(j=0; j<nExpr; j++){
313 if( aExpr[j].idxLeft==idx
314 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000315 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000316 int k;
drh967e8b72000-06-21 13:59:10 +0000317 for(k=0; k<pIdx->nColumn; k++){
318 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000319 switch( aExpr[j].p->op ){
320 case TK_EQ: {
321 eqMask |= 1<<k;
322 break;
323 }
324 case TK_LE:
325 case TK_LT: {
326 ltMask |= 1<<k;
327 break;
328 }
329 case TK_GE:
330 case TK_GT: {
331 gtMask |= 1<<k;
332 break;
333 }
334 default: {
335 /* CANT_HAPPEN */
336 assert( 0 );
337 break;
338 }
339 }
drh75897232000-05-29 14:26:00 +0000340 break;
341 }
342 }
343 }
344 if( aExpr[j].idxRight==idx
345 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000346 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000347 int k;
drh967e8b72000-06-21 13:59:10 +0000348 for(k=0; k<pIdx->nColumn; k++){
349 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000350 switch( aExpr[j].p->op ){
351 case TK_EQ: {
352 eqMask |= 1<<k;
353 break;
354 }
355 case TK_LE:
356 case TK_LT: {
357 gtMask |= 1<<k;
358 break;
359 }
360 case TK_GE:
361 case TK_GT: {
362 ltMask |= 1<<k;
363 break;
364 }
365 default: {
366 /* CANT_HAPPEN */
367 assert( 0 );
368 break;
369 }
370 }
drh75897232000-05-29 14:26:00 +0000371 break;
372 }
373 }
374 }
375 }
drh487ab3c2001-11-08 00:45:21 +0000376 for(nEq=0; nEq<pIdx->nColumn; nEq++){
377 m = (1<<(nEq+1))-1;
378 if( (m & eqMask)!=m ) break;
379 }
380 score = nEq*4;
381 m = 1<<nEq;
382 if( m & ltMask ) score++;
383 if( m & gtMask ) score+=2;
384 if( score>bestScore ){
385 pBestIdx = pIdx;
386 bestScore = score;
drh75897232000-05-29 14:26:00 +0000387 }
388 }
drh6b563442001-11-07 16:48:26 +0000389 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000390 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000391 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000392 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000393 pWInfo->a[i].iCur = pParse->nTab++;
394 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000395 }
drh75897232000-05-29 14:26:00 +0000396 }
397
drh6b563442001-11-07 16:48:26 +0000398 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000399 */
400 for(i=0; i<pTabList->nId; i++){
drhf57b3392001-10-08 13:22:32 +0000401 int openOp;
402 Table *pTab;
403
404 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000405 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000406 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000407 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
408 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000409 if( i==0 && !pParse->schemaVerified &&
410 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000411 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000412 pParse->schemaVerified = 1;
413 }
drh6b563442001-11-07 16:48:26 +0000414 if( pWInfo->a[i].pIdx!=0 ){
415 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
416 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000417 }
418 }
419
420 /* Generate the code to do the search
421 */
drh75897232000-05-29 14:26:00 +0000422 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000423 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000424 for(i=0; i<pTabList->nId; i++){
425 int j, k;
426 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000427 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000428 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000429
drh8aff1012001-12-22 14:49:24 +0000430 pIdx = pLevel->pIdx;
431 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
432 /* Case 1: We can directly reference a single row using an
433 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000434 */
drh8aff1012001-12-22 14:49:24 +0000435 k = iDirectEq[i];
436 assert( k<nExpr );
437 assert( aExpr[k].p!=0 );
438 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
439 if( aExpr[k].idxLeft==idx ){
440 sqliteExprCode(pParse, aExpr[k].p->pRight);
441 }else{
442 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000443 }
drh8aff1012001-12-22 14:49:24 +0000444 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000445 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
446 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000447 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhc4a3c772001-04-04 11:48:57 +0000448 if( i==pTabList->nId-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000449 /* Note: The OP_Dup below will cause the recno to be left on the
450 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000451 ** taken. This violates a general rule of the VDBE that you should
452 ** never leave values on the stack in order to avoid a stack overflow.
453 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000454 ** because the pushKey flag is only true for UPDATE and DELETE, not
455 ** for SELECT, and nested loops only occur on a SELECT.
456 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000457 */
drhc4a3c772001-04-04 11:48:57 +0000458 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000459 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000460 }else{
drhc4a3c772001-04-04 11:48:57 +0000461 haveKey = 0;
462 }
drh6b125452002-01-28 15:53:03 +0000463 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000464 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000465 }else if( pIdx!=0 && pLevel->score%4==0 ){
466 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000467 */
drh6b563442001-11-07 16:48:26 +0000468 int start;
drh487ab3c2001-11-08 00:45:21 +0000469 int testOp;
470 int nColumn = pLevel->score/4;
471 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000472 for(k=0; k<nExpr; k++){
473 if( aExpr[k].p==0 ) continue;
474 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000475 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000476 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000477 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000478 ){
479 sqliteExprCode(pParse, aExpr[k].p->pRight);
480 aExpr[k].p = 0;
481 break;
482 }
483 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000484 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000485 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000486 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000487 ){
488 sqliteExprCode(pParse, aExpr[k].p->pLeft);
489 aExpr[k].p = 0;
490 break;
491 }
492 }
493 }
drh6b563442001-11-07 16:48:26 +0000494 pLevel->iMem = pParse->nMem++;
495 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
496 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000497 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
498 if( nColumn==pIdx->nColumn ){
499 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
500 testOp = OP_IdxGT;
501 }else{
502 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
503 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
504 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
505 testOp = OP_IdxGE;
506 }
drh6b563442001-11-07 16:48:26 +0000507 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
508 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000509 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000510 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000511 if( i==pTabList->nId-1 && pushKey ){
512 haveKey = 1;
513 }else{
drh99fcd712001-10-13 01:06:47 +0000514 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000515 haveKey = 0;
516 }
drh6b563442001-11-07 16:48:26 +0000517 pLevel->op = OP_Next;
518 pLevel->p1 = pLevel->iCur;
519 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000520 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
521 /* Case 3: We have an inequality comparison against the ROWID field.
522 */
523 int testOp = OP_Noop;
524 int start;
525
526 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
527 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
528 if( iDirectGt[i]>=0 ){
529 k = iDirectGt[i];
530 assert( k<nExpr );
531 assert( aExpr[k].p!=0 );
532 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
533 if( aExpr[k].idxLeft==idx ){
534 sqliteExprCode(pParse, aExpr[k].p->pRight);
535 }else{
536 sqliteExprCode(pParse, aExpr[k].p->pLeft);
537 }
538 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
539 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
540 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
541 }
542 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
543 aExpr[k].p = 0;
544 }else{
545 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
546 }
547 if( iDirectLt[i]>=0 ){
548 k = iDirectLt[i];
549 assert( k<nExpr );
550 assert( aExpr[k].p!=0 );
551 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
552 if( aExpr[k].idxLeft==idx ){
553 sqliteExprCode(pParse, aExpr[k].p->pRight);
554 }else{
555 sqliteExprCode(pParse, aExpr[k].p->pLeft);
556 }
557 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
558 pLevel->iMem = pParse->nMem++;
559 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
560 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
561 testOp = OP_Ge;
562 }else{
563 testOp = OP_Gt;
564 }
565 aExpr[k].p = 0;
566 }
567 start = sqliteVdbeCurrentAddr(v);
568 pLevel->op = OP_Next;
569 pLevel->p1 = base+idx;
570 pLevel->p2 = start;
571 if( testOp!=OP_Noop ){
572 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
573 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
574 sqliteVdbeAddOp(v, testOp, 0, brk);
575 }
576 haveKey = 0;
577 }else if( pIdx==0 ){
578 /* Case 4: There was no usable index. We must do a complete
579 ** scan of the entire database table.
580 */
581 int start;
582
583 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
584 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
585 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
586 start = sqliteVdbeCurrentAddr(v);
587 pLevel->op = OP_Next;
588 pLevel->p1 = base+idx;
589 pLevel->p2 = start;
590 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000591 }else{
drhaacc5432002-01-06 17:07:40 +0000592 /* Case 5: The contraint on the right-most index field is
593 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000594 */
595 int score = pLevel->score;
596 int nEqColumn = score/4;
597 int start;
598 int leFlag, geFlag;
599 int testOp;
600
601 /* Evaluate the equality constraints
602 */
603 for(j=0; j<nEqColumn; j++){
604 for(k=0; k<nExpr; k++){
605 if( aExpr[k].p==0 ) continue;
606 if( aExpr[k].idxLeft==idx
607 && aExpr[k].p->op==TK_EQ
608 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
609 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
610 ){
611 sqliteExprCode(pParse, aExpr[k].p->pRight);
612 aExpr[k].p = 0;
613 break;
614 }
615 if( aExpr[k].idxRight==idx
616 && aExpr[k].p->op==TK_EQ
617 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
618 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
619 ){
620 sqliteExprCode(pParse, aExpr[k].p->pLeft);
621 aExpr[k].p = 0;
622 break;
623 }
624 }
625 }
626
627 /* Duplicate the equality contraint values because they will all be
628 ** used twice: once to make the termination key and once to make the
629 ** start key.
630 */
631 for(j=0; j<nEqColumn; j++){
632 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
633 }
634
635 /* Generate the termination key. This is the key value that
636 ** will end the search. There is no termination key if there
637 ** are no equality contraints and no "X<..." constraint.
638 */
639 if( (score & 1)!=0 ){
640 for(k=0; k<nExpr; k++){
641 Expr *pExpr = aExpr[k].p;
642 if( pExpr==0 ) continue;
643 if( aExpr[k].idxLeft==idx
644 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
645 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
646 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
647 ){
648 sqliteExprCode(pParse, pExpr->pRight);
649 leFlag = pExpr->op==TK_LE;
650 aExpr[k].p = 0;
651 break;
652 }
653 if( aExpr[k].idxRight==idx
654 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
655 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
656 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
657 ){
658 sqliteExprCode(pParse, pExpr->pLeft);
659 leFlag = pExpr->op==TK_GE;
660 aExpr[k].p = 0;
661 break;
662 }
663 }
664 testOp = OP_IdxGE;
665 }else{
666 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
667 leFlag = 1;
668 }
669 if( testOp!=OP_Noop ){
670 pLevel->iMem = pParse->nMem++;
671 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
672 if( leFlag ){
673 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
674 }
675 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
676 }
677
678 /* Generate the start key. This is the key that defines the lower
679 ** bound on the search. There is no start key if there are not
680 ** equality constraints and if there is no "X>..." constraint. In
681 ** that case, generate a "Rewind" instruction in place of the
682 ** start key search.
683 */
684 if( (score & 2)!=0 ){
685 for(k=0; k<nExpr; k++){
686 Expr *pExpr = aExpr[k].p;
687 if( pExpr==0 ) continue;
688 if( aExpr[k].idxLeft==idx
689 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
690 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
691 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
692 ){
693 sqliteExprCode(pParse, pExpr->pRight);
694 geFlag = pExpr->op==TK_GE;
695 aExpr[k].p = 0;
696 break;
697 }
698 if( aExpr[k].idxRight==idx
699 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
700 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
701 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
702 ){
703 sqliteExprCode(pParse, pExpr->pLeft);
704 geFlag = pExpr->op==TK_LE;
705 aExpr[k].p = 0;
706 break;
707 }
708 }
drh7900ead2001-11-12 13:51:43 +0000709 }else{
710 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000711 }
712 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
713 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
714 if( nEqColumn>0 || (score&2)!=0 ){
715 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
716 if( !geFlag ){
717 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
718 }
719 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
720 }else{
721 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
722 }
723
724 /* Generate the the top of the loop. If there is a termination
725 ** key we have to test for that key and abort at the top of the
726 ** loop.
727 */
728 start = sqliteVdbeCurrentAddr(v);
729 if( testOp!=OP_Noop ){
730 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
731 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
732 }
733 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
734 if( i==pTabList->nId-1 && pushKey ){
735 haveKey = 1;
736 }else{
737 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
738 haveKey = 0;
739 }
740
741 /* Record the instruction used to terminate the loop.
742 */
743 pLevel->op = OP_Next;
744 pLevel->p1 = pLevel->iCur;
745 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000746 }
747 loopMask |= 1<<idx;
748
749 /* Insert code to test every subexpression that can be completely
750 ** computed using the current set of tables.
751 */
752 for(j=0; j<nExpr; j++){
753 if( aExpr[j].p==0 ) continue;
754 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
755 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
756 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000757 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000758 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000759 }
760 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
761 aExpr[j].p = 0;
762 }
763 brk = cont;
764 }
765 pWInfo->iContinue = cont;
766 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000767 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000768 }
769 sqliteFree(aOrder);
770 return pWInfo;
771}
772
773/*
774** Generate the end of the WHERE loop.
775*/
776void sqliteWhereEnd(WhereInfo *pWInfo){
777 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000778 int i;
drh19a775c2000-06-05 18:54:46 +0000779 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000780 WhereLevel *pLevel;
drh22f70c32002-02-18 01:17:00 +0000781 IdList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000782
drh22f70c32002-02-18 01:17:00 +0000783 for(i=pTabList->nId-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000784 pLevel = &pWInfo->a[i];
785 sqliteVdbeResolveLabel(v, pLevel->cont);
786 if( pLevel->op!=OP_Noop ){
787 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000788 }
drh6b563442001-11-07 16:48:26 +0000789 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000790 }
drh6b563442001-11-07 16:48:26 +0000791 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drh22f70c32002-02-18 01:17:00 +0000792 for(i=0; i<pTabList->nId; i++){
793 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000794 pLevel = &pWInfo->a[i];
795 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
796 if( pLevel->pIdx!=0 ){
797 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
798 }
drh19a775c2000-06-05 18:54:46 +0000799 }
drh832508b2002-03-02 17:04:07 +0000800 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
801 pWInfo->pParse->nTab = pWInfo->savedNTab;
802 }
drh75897232000-05-29 14:26:00 +0000803 sqliteFree(pWInfo);
804 return;
805}