blob: 32077dc1570445762803351ba656f55ebb1dd83e [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**
drh832508b2002-03-02 17:04:07 +000016** $Id: where.c,v 1.38 2002/03/02 17:04:09 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 }
93 return mask;
94}
95
96/*
drh487ab3c2001-11-08 00:45:21 +000097** Return TRUE if the given operator is one of the operators that is
98** allowed for an indexable WHERE clause. The allowed operators are
99** "=", "<", ">", "<=", and ">=".
100*/
101static int allowedOp(int op){
102 switch( op ){
103 case TK_LT:
104 case TK_LE:
105 case TK_GT:
106 case TK_GE:
107 case TK_EQ:
108 return 1;
109 default:
110 return 0;
111 }
112}
113
114/*
drh75897232000-05-29 14:26:00 +0000115** The input to this routine is an ExprInfo structure with only the
116** "p" field filled in. The job of this routine is to analyze the
117** subexpression and populate all the other fields of the ExprInfo
118** structure.
drh19a775c2000-06-05 18:54:46 +0000119**
120** "base" is the cursor number (the value of the iTable field) that
drh832508b2002-03-02 17:04:07 +0000121** corresponds to the first entry in the table list.
drh75897232000-05-29 14:26:00 +0000122*/
drh19a775c2000-06-05 18:54:46 +0000123static void exprAnalyze(int base, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000124 Expr *pExpr = pInfo->p;
drh19a775c2000-06-05 18:54:46 +0000125 pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
126 pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
drh75897232000-05-29 14:26:00 +0000127 pInfo->indexable = 0;
128 pInfo->idxLeft = -1;
129 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000130 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drh967e8b72000-06-21 13:59:10 +0000131 if( pExpr->pRight->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000132 pInfo->idxRight = pExpr->pRight->iTable - base;
drh75897232000-05-29 14:26:00 +0000133 pInfo->indexable = 1;
134 }
drh967e8b72000-06-21 13:59:10 +0000135 if( pExpr->pLeft->op==TK_COLUMN ){
drh19a775c2000-06-05 18:54:46 +0000136 pInfo->idxLeft = pExpr->pLeft->iTable - base;
drh75897232000-05-29 14:26:00 +0000137 pInfo->indexable = 1;
138 }
139 }
140}
141
142/*
143** Generating the beginning of the loop used for WHERE clause processing.
144** The return value is a pointer to an (opaque) structure that contains
145** information needed to terminate the loop. Later, the calling routine
146** should invoke sqliteWhereEnd() with the return value of this function
147** in order to complete the WHERE clause processing.
148**
149** If an error occurs, this routine returns NULL.
150*/
151WhereInfo *sqliteWhereBegin(
152 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000153 int base, /* VDBE cursor index for left-most table in pTabList */
drh75897232000-05-29 14:26:00 +0000154 IdList *pTabList, /* A list of all tables */
155 Expr *pWhere, /* The WHERE clause */
156 int pushKey /* If TRUE, leave the table key on the stack */
157){
158 int i; /* Loop counter */
159 WhereInfo *pWInfo; /* Will become the return value of this function */
160 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
161 int brk, cont; /* Addresses used during code generation */
162 int *aOrder; /* Order in which pTabList entries are searched */
163 int nExpr; /* Number of subexpressions in the WHERE clause */
164 int loopMask; /* One bit set for each outer loop */
165 int haveKey; /* True if KEY is on the stack */
drhc4a3c772001-04-04 11:48:57 +0000166 int aDirect[32]; /* If TRUE, then index this table using ROWID */
drh8aff1012001-12-22 14:49:24 +0000167 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
168 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
169 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh75897232000-05-29 14:26:00 +0000170 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
171
drh6b563442001-11-07 16:48:26 +0000172 /* Allocate space for aOrder[] and aiMem[]. */
drh75897232000-05-29 14:26:00 +0000173 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
174
175 /* Allocate and initialize the WhereInfo structure that will become the
176 ** return value.
177 */
drh6b563442001-11-07 16:48:26 +0000178 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) );
drhdaffd0e2001-04-11 14:28:42 +0000179 if( sqlite_malloc_failed ){
drh75897232000-05-29 14:26:00 +0000180 sqliteFree(aOrder);
drhdaffd0e2001-04-11 14:28:42 +0000181 sqliteFree(pWInfo);
drh75897232000-05-29 14:26:00 +0000182 return 0;
183 }
184 pWInfo->pParse = pParse;
185 pWInfo->pTabList = pTabList;
drh832508b2002-03-02 17:04:07 +0000186 pWInfo->base = base;
187 pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
drh75897232000-05-29 14:26:00 +0000188
189 /* Split the WHERE clause into as many as 32 separate subexpressions
190 ** where each subexpression is separated by an AND operator. Any additional
191 ** subexpressions are attached in the aExpr[32] and will not enter
192 ** into the query optimizer computations. 32 is chosen as the cutoff
193 ** since that is the number of bits in an integer that we use for an
194 ** expression-used mask.
195 */
196 memset(aExpr, 0, sizeof(aExpr));
197 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
198
199 /* Analyze all of the subexpressions.
200 */
201 for(i=0; i<nExpr; i++){
drh22f70c32002-02-18 01:17:00 +0000202 exprAnalyze(base, &aExpr[i]);
drh75897232000-05-29 14:26:00 +0000203 }
204
205 /* Figure out a good nesting order for the tables. aOrder[0] will
206 ** be the index in pTabList of the outermost table. aOrder[1] will
207 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
208 ** be the innermost loop.
209 **
drh7e391e12000-05-30 20:17:49 +0000210 ** Someday will put in a good algorithm here to reorder the loops
drh75897232000-05-29 14:26:00 +0000211 ** for an effiecient query. But for now, just use whatever order the
212 ** tables appear in in the pTabList.
213 */
214 for(i=0; i<pTabList->nId; i++){
215 aOrder[i] = i;
216 }
217
218 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000219 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
220 ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000221 ** loop.
222 **
223 ** If terms exist that use the ROWID of any table, then set the
224 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
225 ** to the index of the term containing the ROWID. We always prefer
226 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000227 ** index which requires reading an index first to get the rowid then
228 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000229 **
230 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000231 ** first 32 tables are candidates for indices. This is (again) due
232 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000233 */
234 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000235 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){
drhc4a3c772001-04-04 11:48:57 +0000236 int j;
drh75897232000-05-29 14:26:00 +0000237 int idx = aOrder[i];
238 Table *pTab = pTabList->a[idx].pTab;
239 Index *pIdx;
240 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000241 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000242
drhc4a3c772001-04-04 11:48:57 +0000243 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000244 ** ROWID field of this table. For terms of the form ROWID==expr
245 ** set iDirectEq[i] to the index of the term. For terms of the
246 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
247 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drhc4a3c772001-04-04 11:48:57 +0000248 */
drh8aff1012001-12-22 14:49:24 +0000249 iDirectEq[i] = -1;
250 iDirectLt[i] = -1;
251 iDirectGt[i] = -1;
drhc4a3c772001-04-04 11:48:57 +0000252 for(j=0; j<nExpr; j++){
253 if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0
254 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh8aff1012001-12-22 14:49:24 +0000255 switch( aExpr[j].p->op ){
256 case TK_EQ: iDirectEq[i] = j; break;
257 case TK_LE:
258 case TK_LT: iDirectLt[i] = j; break;
259 case TK_GE:
260 case TK_GT: iDirectGt[i] = j; break;
261 }
drhc4a3c772001-04-04 11:48:57 +0000262 }
263 if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0
264 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh8aff1012001-12-22 14:49:24 +0000265 switch( aExpr[j].p->op ){
266 case TK_EQ: iDirectEq[i] = j; break;
267 case TK_LE:
268 case TK_LT: iDirectGt[i] = j; break;
269 case TK_GE:
270 case TK_GT: iDirectLt[i] = j; break;
271 }
drhc4a3c772001-04-04 11:48:57 +0000272 }
273 }
drh8aff1012001-12-22 14:49:24 +0000274 if( iDirectEq[i]>=0 ){
drhc4a3c772001-04-04 11:48:57 +0000275 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000276 pWInfo->a[i].pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000277 continue;
278 }
279
drh75897232000-05-29 14:26:00 +0000280 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000281 ** the "best" index. pBestIdx is left set to NULL if no indices
282 ** are usable.
drh75897232000-05-29 14:26:00 +0000283 **
drh487ab3c2001-11-08 00:45:21 +0000284 ** The best index is determined as follows. For each of the
285 ** left-most terms that is fixed by an equality operator, add
286 ** 4 to the score. The right-most term of the index may be
287 ** constrained by an inequality. Add 1 if for an "x<..." constraint
288 ** and add 2 for an "x>..." constraint. Chose the index that
289 ** gives the best score.
290 **
291 ** This scoring system is designed so that the score can later be
292 ** used to determine how the index is used. If the score&3 is 0
293 ** then all constraints are equalities. If score&1 is not 0 then
294 ** there is an inequality used as a termination key. (ex: "x<...")
295 ** If score&2 is not 0 then there is an inequality used as the
296 ** start key. (ex: "x>...");
drh75897232000-05-29 14:26:00 +0000297 */
298 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh487ab3c2001-11-08 00:45:21 +0000299 int eqMask = 0; /* Index columns covered by an x=... constraint */
300 int ltMask = 0; /* Index columns covered by an x<... constraint */
301 int gtMask = 0; /* Index columns covered by an x>... constraing */
302 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000303
drh74e24cd2002-01-09 03:19:59 +0000304 if( pIdx->isDropped ) continue; /* Ignore dropped indices */
drh487ab3c2001-11-08 00:45:21 +0000305 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh75897232000-05-29 14:26:00 +0000306 for(j=0; j<nExpr; j++){
307 if( aExpr[j].idxLeft==idx
308 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
drh967e8b72000-06-21 13:59:10 +0000309 int iColumn = aExpr[j].p->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000310 int k;
drh967e8b72000-06-21 13:59:10 +0000311 for(k=0; k<pIdx->nColumn; k++){
312 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000313 switch( aExpr[j].p->op ){
314 case TK_EQ: {
315 eqMask |= 1<<k;
316 break;
317 }
318 case TK_LE:
319 case TK_LT: {
320 ltMask |= 1<<k;
321 break;
322 }
323 case TK_GE:
324 case TK_GT: {
325 gtMask |= 1<<k;
326 break;
327 }
328 default: {
329 /* CANT_HAPPEN */
330 assert( 0 );
331 break;
332 }
333 }
drh75897232000-05-29 14:26:00 +0000334 break;
335 }
336 }
337 }
338 if( aExpr[j].idxRight==idx
339 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
drh967e8b72000-06-21 13:59:10 +0000340 int iColumn = aExpr[j].p->pRight->iColumn;
drh75897232000-05-29 14:26:00 +0000341 int k;
drh967e8b72000-06-21 13:59:10 +0000342 for(k=0; k<pIdx->nColumn; k++){
343 if( pIdx->aiColumn[k]==iColumn ){
drh487ab3c2001-11-08 00:45:21 +0000344 switch( aExpr[j].p->op ){
345 case TK_EQ: {
346 eqMask |= 1<<k;
347 break;
348 }
349 case TK_LE:
350 case TK_LT: {
351 gtMask |= 1<<k;
352 break;
353 }
354 case TK_GE:
355 case TK_GT: {
356 ltMask |= 1<<k;
357 break;
358 }
359 default: {
360 /* CANT_HAPPEN */
361 assert( 0 );
362 break;
363 }
364 }
drh75897232000-05-29 14:26:00 +0000365 break;
366 }
367 }
368 }
369 }
drh487ab3c2001-11-08 00:45:21 +0000370 for(nEq=0; nEq<pIdx->nColumn; nEq++){
371 m = (1<<(nEq+1))-1;
372 if( (m & eqMask)!=m ) break;
373 }
374 score = nEq*4;
375 m = 1<<nEq;
376 if( m & ltMask ) score++;
377 if( m & gtMask ) score+=2;
378 if( score>bestScore ){
379 pBestIdx = pIdx;
380 bestScore = score;
drh75897232000-05-29 14:26:00 +0000381 }
382 }
drh6b563442001-11-07 16:48:26 +0000383 pWInfo->a[i].pIdx = pBestIdx;
drh487ab3c2001-11-08 00:45:21 +0000384 pWInfo->a[i].score = bestScore;
drh7e391e12000-05-30 20:17:49 +0000385 loopMask |= 1<<idx;
drh6b563442001-11-07 16:48:26 +0000386 if( pBestIdx ){
drh832508b2002-03-02 17:04:07 +0000387 pWInfo->a[i].iCur = pParse->nTab++;
388 pWInfo->peakNTab = pParse->nTab;
drh6b563442001-11-07 16:48:26 +0000389 }
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;
drha76b5df2002-02-23 02:32:10 +0000399 if( pTab->isTransient || pTab->pSelect ) continue;
drhf57b3392001-10-08 13:22:32 +0000400 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh99fcd712001-10-13 01:06:47 +0000401 sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum);
402 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh50e5dad2001-09-15 00:57:28 +0000403 if( i==0 && !pParse->schemaVerified &&
404 (pParse->db->flags & SQLITE_InTrans)==0 ){
drh99fcd712001-10-13 01:06:47 +0000405 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
drh50e5dad2001-09-15 00:57:28 +0000406 pParse->schemaVerified = 1;
407 }
drh6b563442001-11-07 16:48:26 +0000408 if( pWInfo->a[i].pIdx!=0 ){
409 sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum);
410 sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC);
drh75897232000-05-29 14:26:00 +0000411 }
412 }
413
414 /* Generate the code to do the search
415 */
drh75897232000-05-29 14:26:00 +0000416 loopMask = 0;
drh6b563442001-11-07 16:48:26 +0000417 pWInfo->iBreak = sqliteVdbeMakeLabel(v);
drh75897232000-05-29 14:26:00 +0000418 for(i=0; i<pTabList->nId; i++){
419 int j, k;
420 int idx = aOrder[i];
drhc4a3c772001-04-04 11:48:57 +0000421 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000422 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000423
drh8aff1012001-12-22 14:49:24 +0000424 pIdx = pLevel->pIdx;
425 if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
426 /* Case 1: We can directly reference a single row using an
427 ** equality comparison against the ROWID field.
drhc4a3c772001-04-04 11:48:57 +0000428 */
drh8aff1012001-12-22 14:49:24 +0000429 k = iDirectEq[i];
430 assert( k<nExpr );
431 assert( aExpr[k].p!=0 );
432 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
433 if( aExpr[k].idxLeft==idx ){
434 sqliteExprCode(pParse, aExpr[k].p->pRight);
435 }else{
436 sqliteExprCode(pParse, aExpr[k].p->pLeft);
drhc4a3c772001-04-04 11:48:57 +0000437 }
drh8aff1012001-12-22 14:49:24 +0000438 aExpr[k].p = 0;
drh6b563442001-11-07 16:48:26 +0000439 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
440 cont = pLevel->cont = brk;
drh8aff1012001-12-22 14:49:24 +0000441 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
drhc4a3c772001-04-04 11:48:57 +0000442 if( i==pTabList->nId-1 && pushKey ){
drh97665872002-02-13 23:22:53 +0000443 /* Note: The OP_Dup below will cause the recno to be left on the
444 ** stack if the record does not exists and the OP_NotExists jump is
drh6b125452002-01-28 15:53:03 +0000445 ** taken. This violates a general rule of the VDBE that you should
446 ** never leave values on the stack in order to avoid a stack overflow.
447 ** But in this case, the OP_Dup will never happen inside of a loop,
drh97665872002-02-13 23:22:53 +0000448 ** because the pushKey flag is only true for UPDATE and DELETE, not
449 ** for SELECT, and nested loops only occur on a SELECT.
450 ** So it is safe to leave the recno on the stack.
drh6b125452002-01-28 15:53:03 +0000451 */
drhc4a3c772001-04-04 11:48:57 +0000452 haveKey = 1;
drh6b125452002-01-28 15:53:03 +0000453 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhc4a3c772001-04-04 11:48:57 +0000454 }else{
drhc4a3c772001-04-04 11:48:57 +0000455 haveKey = 0;
456 }
drh6b125452002-01-28 15:53:03 +0000457 sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk);
drh6b563442001-11-07 16:48:26 +0000458 pLevel->op = OP_Noop;
drh8aff1012001-12-22 14:49:24 +0000459 }else if( pIdx!=0 && pLevel->score%4==0 ){
460 /* Case 2: All index constraints are equality operators.
drh75897232000-05-29 14:26:00 +0000461 */
drh6b563442001-11-07 16:48:26 +0000462 int start;
drh487ab3c2001-11-08 00:45:21 +0000463 int testOp;
464 int nColumn = pLevel->score/4;
465 for(j=0; j<nColumn; j++){
drh75897232000-05-29 14:26:00 +0000466 for(k=0; k<nExpr; k++){
467 if( aExpr[k].p==0 ) continue;
468 if( aExpr[k].idxLeft==idx
drh487ab3c2001-11-08 00:45:21 +0000469 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000470 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
drh967e8b72000-06-21 13:59:10 +0000471 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000472 ){
473 sqliteExprCode(pParse, aExpr[k].p->pRight);
474 aExpr[k].p = 0;
475 break;
476 }
477 if( aExpr[k].idxRight==idx
drh487ab3c2001-11-08 00:45:21 +0000478 && aExpr[k].p->op==TK_EQ
drh75897232000-05-29 14:26:00 +0000479 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
drh967e8b72000-06-21 13:59:10 +0000480 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000481 ){
482 sqliteExprCode(pParse, aExpr[k].p->pLeft);
483 aExpr[k].p = 0;
484 break;
485 }
486 }
487 }
drh6b563442001-11-07 16:48:26 +0000488 pLevel->iMem = pParse->nMem++;
489 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
490 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
drh487ab3c2001-11-08 00:45:21 +0000491 sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
492 if( nColumn==pIdx->nColumn ){
493 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
494 testOp = OP_IdxGT;
495 }else{
496 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
497 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
498 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
499 testOp = OP_IdxGE;
500 }
drh6b563442001-11-07 16:48:26 +0000501 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
502 start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh487ab3c2001-11-08 00:45:21 +0000503 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
drh6b563442001-11-07 16:48:26 +0000504 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drh75897232000-05-29 14:26:00 +0000505 if( i==pTabList->nId-1 && pushKey ){
506 haveKey = 1;
507 }else{
drh99fcd712001-10-13 01:06:47 +0000508 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000509 haveKey = 0;
510 }
drh6b563442001-11-07 16:48:26 +0000511 pLevel->op = OP_Next;
512 pLevel->p1 = pLevel->iCur;
513 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000514 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
515 /* Case 3: We have an inequality comparison against the ROWID field.
516 */
517 int testOp = OP_Noop;
518 int start;
519
520 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
521 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
522 if( iDirectGt[i]>=0 ){
523 k = iDirectGt[i];
524 assert( k<nExpr );
525 assert( aExpr[k].p!=0 );
526 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
527 if( aExpr[k].idxLeft==idx ){
528 sqliteExprCode(pParse, aExpr[k].p->pRight);
529 }else{
530 sqliteExprCode(pParse, aExpr[k].p->pLeft);
531 }
532 sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk);
533 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
534 sqliteVdbeAddOp(v, OP_AddImm, 1, 0);
535 }
536 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk);
537 aExpr[k].p = 0;
538 }else{
539 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
540 }
541 if( iDirectLt[i]>=0 ){
542 k = iDirectLt[i];
543 assert( k<nExpr );
544 assert( aExpr[k].p!=0 );
545 assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx );
546 if( aExpr[k].idxLeft==idx ){
547 sqliteExprCode(pParse, aExpr[k].p->pRight);
548 }else{
549 sqliteExprCode(pParse, aExpr[k].p->pLeft);
550 }
551 sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1);
552 pLevel->iMem = pParse->nMem++;
553 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
554 if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
555 testOp = OP_Ge;
556 }else{
557 testOp = OP_Gt;
558 }
559 aExpr[k].p = 0;
560 }
561 start = sqliteVdbeCurrentAddr(v);
562 pLevel->op = OP_Next;
563 pLevel->p1 = base+idx;
564 pLevel->p2 = start;
565 if( testOp!=OP_Noop ){
566 sqliteVdbeAddOp(v, OP_Recno, base+idx, 0);
567 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
568 sqliteVdbeAddOp(v, testOp, 0, brk);
569 }
570 haveKey = 0;
571 }else if( pIdx==0 ){
572 /* Case 4: There was no usable index. We must do a complete
573 ** scan of the entire database table.
574 */
575 int start;
576
577 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
578 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
579 sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk);
580 start = sqliteVdbeCurrentAddr(v);
581 pLevel->op = OP_Next;
582 pLevel->p1 = base+idx;
583 pLevel->p2 = start;
584 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000585 }else{
drhaacc5432002-01-06 17:07:40 +0000586 /* Case 5: The contraint on the right-most index field is
587 ** an inequality.
drh487ab3c2001-11-08 00:45:21 +0000588 */
589 int score = pLevel->score;
590 int nEqColumn = score/4;
591 int start;
592 int leFlag, geFlag;
593 int testOp;
594
595 /* Evaluate the equality constraints
596 */
597 for(j=0; j<nEqColumn; j++){
598 for(k=0; k<nExpr; k++){
599 if( aExpr[k].p==0 ) continue;
600 if( aExpr[k].idxLeft==idx
601 && aExpr[k].p->op==TK_EQ
602 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
603 && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
604 ){
605 sqliteExprCode(pParse, aExpr[k].p->pRight);
606 aExpr[k].p = 0;
607 break;
608 }
609 if( aExpr[k].idxRight==idx
610 && aExpr[k].p->op==TK_EQ
611 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
612 && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
613 ){
614 sqliteExprCode(pParse, aExpr[k].p->pLeft);
615 aExpr[k].p = 0;
616 break;
617 }
618 }
619 }
620
621 /* Duplicate the equality contraint values because they will all be
622 ** used twice: once to make the termination key and once to make the
623 ** start key.
624 */
625 for(j=0; j<nEqColumn; j++){
626 sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
627 }
628
629 /* Generate the termination key. This is the key value that
630 ** will end the search. There is no termination key if there
631 ** are no equality contraints and no "X<..." constraint.
632 */
633 if( (score & 1)!=0 ){
634 for(k=0; k<nExpr; k++){
635 Expr *pExpr = aExpr[k].p;
636 if( pExpr==0 ) continue;
637 if( aExpr[k].idxLeft==idx
638 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
639 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
640 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
641 ){
642 sqliteExprCode(pParse, pExpr->pRight);
643 leFlag = pExpr->op==TK_LE;
644 aExpr[k].p = 0;
645 break;
646 }
647 if( aExpr[k].idxRight==idx
648 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
649 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
650 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
651 ){
652 sqliteExprCode(pParse, pExpr->pLeft);
653 leFlag = pExpr->op==TK_GE;
654 aExpr[k].p = 0;
655 break;
656 }
657 }
658 testOp = OP_IdxGE;
659 }else{
660 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
661 leFlag = 1;
662 }
663 if( testOp!=OP_Noop ){
664 pLevel->iMem = pParse->nMem++;
665 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0);
666 if( leFlag ){
667 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
668 }
669 sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
670 }
671
672 /* Generate the start key. This is the key that defines the lower
673 ** bound on the search. There is no start key if there are not
674 ** equality constraints and if there is no "X>..." constraint. In
675 ** that case, generate a "Rewind" instruction in place of the
676 ** start key search.
677 */
678 if( (score & 2)!=0 ){
679 for(k=0; k<nExpr; k++){
680 Expr *pExpr = aExpr[k].p;
681 if( pExpr==0 ) continue;
682 if( aExpr[k].idxLeft==idx
683 && (pExpr->op==TK_GT || pExpr->op==TK_GE)
684 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
685 && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
686 ){
687 sqliteExprCode(pParse, pExpr->pRight);
688 geFlag = pExpr->op==TK_GE;
689 aExpr[k].p = 0;
690 break;
691 }
692 if( aExpr[k].idxRight==idx
693 && (pExpr->op==TK_LT || pExpr->op==TK_LE)
694 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
695 && pExpr->pRight->iColumn==pIdx->aiColumn[j]
696 ){
697 sqliteExprCode(pParse, pExpr->pLeft);
698 geFlag = pExpr->op==TK_LE;
699 aExpr[k].p = 0;
700 break;
701 }
702 }
drh7900ead2001-11-12 13:51:43 +0000703 }else{
704 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +0000705 }
706 brk = pLevel->brk = sqliteVdbeMakeLabel(v);
707 cont = pLevel->cont = sqliteVdbeMakeLabel(v);
708 if( nEqColumn>0 || (score&2)!=0 ){
709 sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0);
710 if( !geFlag ){
711 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
712 }
713 sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
714 }else{
715 sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
716 }
717
718 /* Generate the the top of the loop. If there is a termination
719 ** key we have to test for that key and abort at the top of the
720 ** loop.
721 */
722 start = sqliteVdbeCurrentAddr(v);
723 if( testOp!=OP_Noop ){
724 sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
725 sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
726 }
727 sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
728 if( i==pTabList->nId-1 && pushKey ){
729 haveKey = 1;
730 }else{
731 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
732 haveKey = 0;
733 }
734
735 /* Record the instruction used to terminate the loop.
736 */
737 pLevel->op = OP_Next;
738 pLevel->p1 = pLevel->iCur;
739 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +0000740 }
741 loopMask |= 1<<idx;
742
743 /* Insert code to test every subexpression that can be completely
744 ** computed using the current set of tables.
745 */
746 for(j=0; j<nExpr; j++){
747 if( aExpr[j].p==0 ) continue;
748 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
749 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
750 if( haveKey ){
drh573bd272001-02-19 23:23:38 +0000751 haveKey = 0;
drh99fcd712001-10-13 01:06:47 +0000752 sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0);
drh75897232000-05-29 14:26:00 +0000753 }
754 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
755 aExpr[j].p = 0;
756 }
757 brk = cont;
758 }
759 pWInfo->iContinue = cont;
760 if( pushKey && !haveKey ){
drh99fcd712001-10-13 01:06:47 +0000761 sqliteVdbeAddOp(v, OP_Recno, base, 0);
drh75897232000-05-29 14:26:00 +0000762 }
763 sqliteFree(aOrder);
764 return pWInfo;
765}
766
767/*
768** Generate the end of the WHERE loop.
769*/
770void sqliteWhereEnd(WhereInfo *pWInfo){
771 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +0000772 int i;
drh19a775c2000-06-05 18:54:46 +0000773 int base = pWInfo->base;
drh6b563442001-11-07 16:48:26 +0000774 WhereLevel *pLevel;
drh22f70c32002-02-18 01:17:00 +0000775 IdList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +0000776
drh22f70c32002-02-18 01:17:00 +0000777 for(i=pTabList->nId-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +0000778 pLevel = &pWInfo->a[i];
779 sqliteVdbeResolveLabel(v, pLevel->cont);
780 if( pLevel->op!=OP_Noop ){
781 sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +0000782 }
drh6b563442001-11-07 16:48:26 +0000783 sqliteVdbeResolveLabel(v, pLevel->brk);
drh19a775c2000-06-05 18:54:46 +0000784 }
drh6b563442001-11-07 16:48:26 +0000785 sqliteVdbeResolveLabel(v, pWInfo->iBreak);
drh22f70c32002-02-18 01:17:00 +0000786 for(i=0; i<pTabList->nId; i++){
787 if( pTabList->a[i].pTab->isTransient ) continue;
drh6b563442001-11-07 16:48:26 +0000788 pLevel = &pWInfo->a[i];
789 sqliteVdbeAddOp(v, OP_Close, base+i, 0);
790 if( pLevel->pIdx!=0 ){
791 sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
792 }
drh19a775c2000-06-05 18:54:46 +0000793 }
drh832508b2002-03-02 17:04:07 +0000794 if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
795 pWInfo->pParse->nTab = pWInfo->savedNTab;
796 }
drh75897232000-05-29 14:26:00 +0000797 sqliteFree(pWInfo);
798 return;
799}