drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 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 | ** |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 16 | ** $Id: where.c,v 1.26 2001/11/08 00:45:22 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 17 | */ |
| 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 | */ |
| 25 | typedef struct ExprInfo ExprInfo; |
| 26 | struct ExprInfo { |
| 27 | Expr *p; /* Pointer to the subexpression */ |
| 28 | int indexable; /* True if this subexprssion is usable by an index */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 29 | 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 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 33 | 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 | */ |
| 51 | static 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. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 77 | ** |
| 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 81 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 82 | static int exprTableUsage(int base, Expr *p){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | unsigned int mask = 0; |
| 84 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 85 | if( p->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 86 | return 1<< (p->iTable - base); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 87 | } |
| 88 | if( p->pRight ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 89 | mask = exprTableUsage(base, p->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 90 | } |
| 91 | if( p->pLeft ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 92 | mask |= exprTableUsage(base, p->pLeft); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 93 | } |
| 94 | return mask; |
| 95 | } |
| 96 | |
| 97 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 98 | ** 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 | */ |
| 102 | static 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 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 116 | ** 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. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 120 | ** |
| 121 | ** "base" is the cursor number (the value of the iTable field) that |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 122 | ** corresponds to the first entry in the table list. This is the |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 123 | ** same as pParse->nTab. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 124 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 125 | static void exprAnalyze(int base, ExprInfo *pInfo){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 126 | Expr *pExpr = pInfo->p; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 127 | pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft); |
| 128 | pInfo->prereqRight = exprTableUsage(base, pExpr->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 129 | pInfo->indexable = 0; |
| 130 | pInfo->idxLeft = -1; |
| 131 | pInfo->idxRight = -1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 132 | if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 133 | if( pExpr->pRight->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 134 | pInfo->idxRight = pExpr->pRight->iTable - base; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 135 | pInfo->indexable = 1; |
| 136 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 137 | if( pExpr->pLeft->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 138 | pInfo->idxLeft = pExpr->pLeft->iTable - base; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 139 | 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 | */ |
| 153 | WhereInfo *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 */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 167 | int base; /* First available index for OP_Open opcodes */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 168 | int nCur; /* Next unused cursor number */ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 169 | int aDirect[32]; /* If TRUE, then index this table using ROWID */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 170 | ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */ |
| 171 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 172 | /* Allocate space for aOrder[] and aiMem[]. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 173 | aOrder = sqliteMalloc( sizeof(int) * pTabList->nId ); |
| 174 | |
| 175 | /* Allocate and initialize the WhereInfo structure that will become the |
| 176 | ** return value. |
| 177 | */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 178 | pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 179 | if( sqlite_malloc_failed ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 180 | sqliteFree(aOrder); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 181 | sqliteFree(pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | pWInfo->pParse = pParse; |
| 185 | pWInfo->pTabList = pTabList; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 186 | base = pWInfo->base = pParse->nTab; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 187 | nCur = base + pTabList->nId; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 188 | |
| 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++){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 202 | exprAnalyze(pParse->nTab, &aExpr[i]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 203 | } |
| 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 | ** |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 210 | ** Someday will put in a good algorithm here to reorder the loops |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | ** 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. |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 219 | ** 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 |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 221 | ** loop. If the expression uses only the ROWID field, then set |
| 222 | ** aDirect[i] to 1. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 223 | ** |
| 224 | ** Actually, if there are more than 32 tables in the join, only the |
| 225 | ** first 32 tables are candidates for indices. |
| 226 | */ |
| 227 | loopMask = 0; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 228 | for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 229 | int j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 230 | int idx = aOrder[i]; |
| 231 | Table *pTab = pTabList->a[idx].pTab; |
| 232 | Index *pIdx; |
| 233 | Index *pBestIdx = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 234 | int bestScore = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 235 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 236 | /* Check to see if there is an expression that uses only the |
| 237 | ** ROWID field of this table. If so, set aDirect[i] to 1. |
| 238 | ** If not, set aDirect[i] to 0. |
| 239 | */ |
| 240 | aDirect[i] = 0; |
| 241 | for(j=0; j<nExpr; j++){ |
| 242 | if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0 |
| 243 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
| 244 | aDirect[i] = 1; |
| 245 | break; |
| 246 | } |
| 247 | if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0 |
| 248 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
| 249 | aDirect[i] = 1; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | if( aDirect[i] ){ |
| 254 | loopMask |= 1<<idx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 255 | pWInfo->a[i].pIdx = 0; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 256 | continue; |
| 257 | } |
| 258 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 259 | /* Do a search for usable indices. Leave pBestIdx pointing to |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 260 | ** the "best" index. pBestIdx is left set to NULL if no indices |
| 261 | ** are usable. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 262 | ** |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 263 | ** The best index is determined as follows. For each of the |
| 264 | ** left-most terms that is fixed by an equality operator, add |
| 265 | ** 4 to the score. The right-most term of the index may be |
| 266 | ** constrained by an inequality. Add 1 if for an "x<..." constraint |
| 267 | ** and add 2 for an "x>..." constraint. Chose the index that |
| 268 | ** gives the best score. |
| 269 | ** |
| 270 | ** This scoring system is designed so that the score can later be |
| 271 | ** used to determine how the index is used. If the score&3 is 0 |
| 272 | ** then all constraints are equalities. If score&1 is not 0 then |
| 273 | ** there is an inequality used as a termination key. (ex: "x<...") |
| 274 | ** If score&2 is not 0 then there is an inequality used as the |
| 275 | ** start key. (ex: "x>..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 276 | */ |
| 277 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 278 | int eqMask = 0; /* Index columns covered by an x=... constraint */ |
| 279 | int ltMask = 0; /* Index columns covered by an x<... constraint */ |
| 280 | int gtMask = 0; /* Index columns covered by an x>... constraing */ |
| 281 | int nEq, m, score; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 282 | |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 283 | if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 284 | for(j=0; j<nExpr; j++){ |
| 285 | if( aExpr[j].idxLeft==idx |
| 286 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 287 | int iColumn = aExpr[j].p->pLeft->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 288 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 289 | for(k=0; k<pIdx->nColumn; k++){ |
| 290 | if( pIdx->aiColumn[k]==iColumn ){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 291 | switch( aExpr[j].p->op ){ |
| 292 | case TK_EQ: { |
| 293 | eqMask |= 1<<k; |
| 294 | break; |
| 295 | } |
| 296 | case TK_LE: |
| 297 | case TK_LT: { |
| 298 | ltMask |= 1<<k; |
| 299 | break; |
| 300 | } |
| 301 | case TK_GE: |
| 302 | case TK_GT: { |
| 303 | gtMask |= 1<<k; |
| 304 | break; |
| 305 | } |
| 306 | default: { |
| 307 | /* CANT_HAPPEN */ |
| 308 | assert( 0 ); |
| 309 | break; |
| 310 | } |
| 311 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | if( aExpr[j].idxRight==idx |
| 317 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 318 | int iColumn = aExpr[j].p->pRight->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 319 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 320 | for(k=0; k<pIdx->nColumn; k++){ |
| 321 | if( pIdx->aiColumn[k]==iColumn ){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 322 | switch( aExpr[j].p->op ){ |
| 323 | case TK_EQ: { |
| 324 | eqMask |= 1<<k; |
| 325 | break; |
| 326 | } |
| 327 | case TK_LE: |
| 328 | case TK_LT: { |
| 329 | gtMask |= 1<<k; |
| 330 | break; |
| 331 | } |
| 332 | case TK_GE: |
| 333 | case TK_GT: { |
| 334 | ltMask |= 1<<k; |
| 335 | break; |
| 336 | } |
| 337 | default: { |
| 338 | /* CANT_HAPPEN */ |
| 339 | assert( 0 ); |
| 340 | break; |
| 341 | } |
| 342 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 343 | break; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 348 | for(nEq=0; nEq<pIdx->nColumn; nEq++){ |
| 349 | m = (1<<(nEq+1))-1; |
| 350 | if( (m & eqMask)!=m ) break; |
| 351 | } |
| 352 | score = nEq*4; |
| 353 | m = 1<<nEq; |
| 354 | if( m & ltMask ) score++; |
| 355 | if( m & gtMask ) score+=2; |
| 356 | if( score>bestScore ){ |
| 357 | pBestIdx = pIdx; |
| 358 | bestScore = score; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 361 | pWInfo->a[i].pIdx = pBestIdx; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 362 | pWInfo->a[i].score = bestScore; |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 363 | loopMask |= 1<<idx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 364 | if( pBestIdx ){ |
| 365 | pWInfo->a[i].iCur = nCur++; |
| 366 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 367 | } |
| 368 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 369 | /* Open all tables in the pTabList and all indices used by those tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 370 | */ |
| 371 | for(i=0; i<pTabList->nId; i++){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 372 | int openOp; |
| 373 | Table *pTab; |
| 374 | |
| 375 | pTab = pTabList->a[i].pTab; |
| 376 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 377 | sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum); |
| 378 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 379 | if( i==0 && !pParse->schemaVerified && |
| 380 | (pParse->db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 381 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 382 | pParse->schemaVerified = 1; |
| 383 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 384 | if( pWInfo->a[i].pIdx!=0 ){ |
| 385 | sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum); |
| 386 | sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | /* Generate the code to do the search |
| 391 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 392 | loopMask = 0; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 393 | pWInfo->iBreak = sqliteVdbeMakeLabel(v); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 394 | for(i=0; i<pTabList->nId; i++){ |
| 395 | int j, k; |
| 396 | int idx = aOrder[i]; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 397 | int goDirect; |
| 398 | Index *pIdx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 399 | WhereLevel *pLevel = &pWInfo->a[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 400 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 401 | if( i<ARRAYSIZE(aDirect) ){ |
| 402 | pIdx = pLevel->pIdx; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 403 | goDirect = aDirect[i]; |
| 404 | }else{ |
| 405 | pIdx = 0; |
| 406 | goDirect = 0; |
| 407 | } |
| 408 | |
| 409 | if( goDirect ){ |
| 410 | /* Case 1: We can directly reference a single row using the ROWID field. |
| 411 | */ |
| 412 | cont = brk; |
| 413 | for(k=0; k<nExpr; k++){ |
| 414 | if( aExpr[k].p==0 ) continue; |
| 415 | if( aExpr[k].idxLeft==idx |
| 416 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 417 | && aExpr[k].p->pLeft->iColumn<0 |
| 418 | ){ |
| 419 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 420 | aExpr[k].p = 0; |
| 421 | break; |
| 422 | } |
| 423 | if( aExpr[k].idxRight==idx |
| 424 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 425 | && aExpr[k].p->pRight->iColumn<0 |
| 426 | ){ |
| 427 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 428 | aExpr[k].p = 0; |
| 429 | break; |
| 430 | } |
| 431 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 432 | sqliteVdbeAddOp(v, OP_AddImm, 0, 0); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 433 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 434 | cont = pLevel->cont = brk; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 435 | if( i==pTabList->nId-1 && pushKey ){ |
| 436 | haveKey = 1; |
| 437 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 438 | sqliteVdbeAddOp(v, OP_NotFound, base+idx, brk); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 439 | haveKey = 0; |
| 440 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 441 | pLevel->op = OP_Noop; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 442 | }else if( pIdx==0 ){ |
| 443 | /* Case 2: There was no usable index. We must do a complete |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 444 | ** scan of the entire database table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 445 | */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 446 | int start; |
| 447 | |
| 448 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 449 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 450 | sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk); |
| 451 | start = sqliteVdbeCurrentAddr(v); |
| 452 | pLevel->op = OP_Next; |
| 453 | pLevel->p1 = base+idx; |
| 454 | pLevel->p2 = start; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 455 | haveKey = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 456 | }else if( pLevel->score%4==0 ){ |
| 457 | /* Case 3: All index constraints are equality operators. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 458 | */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 459 | int start; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 460 | int testOp; |
| 461 | int nColumn = pLevel->score/4; |
| 462 | for(j=0; j<nColumn; j++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 463 | for(k=0; k<nExpr; k++){ |
| 464 | if( aExpr[k].p==0 ) continue; |
| 465 | if( aExpr[k].idxLeft==idx |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 466 | && aExpr[k].p->op==TK_EQ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 467 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 468 | && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 469 | ){ |
| 470 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 471 | aExpr[k].p = 0; |
| 472 | break; |
| 473 | } |
| 474 | if( aExpr[k].idxRight==idx |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 475 | && aExpr[k].p->op==TK_EQ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 476 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 477 | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 478 | ){ |
| 479 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 480 | aExpr[k].p = 0; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 485 | pLevel->iMem = pParse->nMem++; |
| 486 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 487 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 488 | sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0); |
| 489 | if( nColumn==pIdx->nColumn ){ |
| 490 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
| 491 | testOp = OP_IdxGT; |
| 492 | }else{ |
| 493 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 494 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 495 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 496 | testOp = OP_IdxGE; |
| 497 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 498 | sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 499 | start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 500 | sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 501 | sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 502 | if( i==pTabList->nId-1 && pushKey ){ |
| 503 | haveKey = 1; |
| 504 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 505 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 506 | haveKey = 0; |
| 507 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 508 | pLevel->op = OP_Next; |
| 509 | pLevel->p1 = pLevel->iCur; |
| 510 | pLevel->p2 = start; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame^] | 511 | }else{ |
| 512 | /* Case 4: The contraints on the right-most index field are |
| 513 | ** inequalities. |
| 514 | */ |
| 515 | int score = pLevel->score; |
| 516 | int nEqColumn = score/4; |
| 517 | int start; |
| 518 | int leFlag, geFlag; |
| 519 | int testOp; |
| 520 | |
| 521 | /* Evaluate the equality constraints |
| 522 | */ |
| 523 | for(j=0; j<nEqColumn; j++){ |
| 524 | for(k=0; k<nExpr; k++){ |
| 525 | if( aExpr[k].p==0 ) continue; |
| 526 | if( aExpr[k].idxLeft==idx |
| 527 | && aExpr[k].p->op==TK_EQ |
| 528 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 529 | && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
| 530 | ){ |
| 531 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 532 | aExpr[k].p = 0; |
| 533 | break; |
| 534 | } |
| 535 | if( aExpr[k].idxRight==idx |
| 536 | && aExpr[k].p->op==TK_EQ |
| 537 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 538 | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
| 539 | ){ |
| 540 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 541 | aExpr[k].p = 0; |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* Duplicate the equality contraint values because they will all be |
| 548 | ** used twice: once to make the termination key and once to make the |
| 549 | ** start key. |
| 550 | */ |
| 551 | for(j=0; j<nEqColumn; j++){ |
| 552 | sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0); |
| 553 | } |
| 554 | |
| 555 | /* Generate the termination key. This is the key value that |
| 556 | ** will end the search. There is no termination key if there |
| 557 | ** are no equality contraints and no "X<..." constraint. |
| 558 | */ |
| 559 | if( (score & 1)!=0 ){ |
| 560 | for(k=0; k<nExpr; k++){ |
| 561 | Expr *pExpr = aExpr[k].p; |
| 562 | if( pExpr==0 ) continue; |
| 563 | if( aExpr[k].idxLeft==idx |
| 564 | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
| 565 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 566 | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
| 567 | ){ |
| 568 | sqliteExprCode(pParse, pExpr->pRight); |
| 569 | leFlag = pExpr->op==TK_LE; |
| 570 | aExpr[k].p = 0; |
| 571 | break; |
| 572 | } |
| 573 | if( aExpr[k].idxRight==idx |
| 574 | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
| 575 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 576 | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
| 577 | ){ |
| 578 | sqliteExprCode(pParse, pExpr->pLeft); |
| 579 | leFlag = pExpr->op==TK_GE; |
| 580 | aExpr[k].p = 0; |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | testOp = OP_IdxGE; |
| 585 | }else{ |
| 586 | testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop; |
| 587 | leFlag = 1; |
| 588 | } |
| 589 | if( testOp!=OP_Noop ){ |
| 590 | pLevel->iMem = pParse->nMem++; |
| 591 | sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0); |
| 592 | if( leFlag ){ |
| 593 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 594 | } |
| 595 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 596 | } |
| 597 | |
| 598 | /* Generate the start key. This is the key that defines the lower |
| 599 | ** bound on the search. There is no start key if there are not |
| 600 | ** equality constraints and if there is no "X>..." constraint. In |
| 601 | ** that case, generate a "Rewind" instruction in place of the |
| 602 | ** start key search. |
| 603 | */ |
| 604 | if( (score & 2)!=0 ){ |
| 605 | for(k=0; k<nExpr; k++){ |
| 606 | Expr *pExpr = aExpr[k].p; |
| 607 | if( pExpr==0 ) continue; |
| 608 | if( aExpr[k].idxLeft==idx |
| 609 | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
| 610 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 611 | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
| 612 | ){ |
| 613 | sqliteExprCode(pParse, pExpr->pRight); |
| 614 | geFlag = pExpr->op==TK_GE; |
| 615 | aExpr[k].p = 0; |
| 616 | break; |
| 617 | } |
| 618 | if( aExpr[k].idxRight==idx |
| 619 | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
| 620 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 621 | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
| 622 | ){ |
| 623 | sqliteExprCode(pParse, pExpr->pLeft); |
| 624 | geFlag = pExpr->op==TK_LE; |
| 625 | aExpr[k].p = 0; |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 631 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 632 | if( nEqColumn>0 || (score&2)!=0 ){ |
| 633 | sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0); |
| 634 | if( !geFlag ){ |
| 635 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 636 | } |
| 637 | sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 638 | }else{ |
| 639 | sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk); |
| 640 | } |
| 641 | |
| 642 | /* Generate the the top of the loop. If there is a termination |
| 643 | ** key we have to test for that key and abort at the top of the |
| 644 | ** loop. |
| 645 | */ |
| 646 | start = sqliteVdbeCurrentAddr(v); |
| 647 | if( testOp!=OP_Noop ){ |
| 648 | sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 649 | sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
| 650 | } |
| 651 | sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
| 652 | if( i==pTabList->nId-1 && pushKey ){ |
| 653 | haveKey = 1; |
| 654 | }else{ |
| 655 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
| 656 | haveKey = 0; |
| 657 | } |
| 658 | |
| 659 | /* Record the instruction used to terminate the loop. |
| 660 | */ |
| 661 | pLevel->op = OP_Next; |
| 662 | pLevel->p1 = pLevel->iCur; |
| 663 | pLevel->p2 = start; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 664 | } |
| 665 | loopMask |= 1<<idx; |
| 666 | |
| 667 | /* Insert code to test every subexpression that can be completely |
| 668 | ** computed using the current set of tables. |
| 669 | */ |
| 670 | for(j=0; j<nExpr; j++){ |
| 671 | if( aExpr[j].p==0 ) continue; |
| 672 | if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue; |
| 673 | if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue; |
| 674 | if( haveKey ){ |
drh | 573bd27 | 2001-02-19 23:23:38 +0000 | [diff] [blame] | 675 | haveKey = 0; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 676 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 677 | } |
| 678 | sqliteExprIfFalse(pParse, aExpr[j].p, cont); |
| 679 | aExpr[j].p = 0; |
| 680 | } |
| 681 | brk = cont; |
| 682 | } |
| 683 | pWInfo->iContinue = cont; |
| 684 | if( pushKey && !haveKey ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 685 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 686 | } |
| 687 | sqliteFree(aOrder); |
| 688 | return pWInfo; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | ** Generate the end of the WHERE loop. |
| 693 | */ |
| 694 | void sqliteWhereEnd(WhereInfo *pWInfo){ |
| 695 | Vdbe *v = pWInfo->pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 696 | int i; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 697 | int base = pWInfo->base; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 698 | WhereLevel *pLevel; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 699 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 700 | for(i=pWInfo->pTabList->nId-1; i>=0; i--){ |
| 701 | pLevel = &pWInfo->a[i]; |
| 702 | sqliteVdbeResolveLabel(v, pLevel->cont); |
| 703 | if( pLevel->op!=OP_Noop ){ |
| 704 | sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 705 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 706 | sqliteVdbeResolveLabel(v, pLevel->brk); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 707 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 708 | sqliteVdbeResolveLabel(v, pWInfo->iBreak); |
| 709 | for(i=0; i<pWInfo->pTabList->nId; i++){ |
| 710 | pLevel = &pWInfo->a[i]; |
| 711 | sqliteVdbeAddOp(v, OP_Close, base+i, 0); |
| 712 | if( pLevel->pIdx!=0 ){ |
| 713 | sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0); |
| 714 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 715 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 716 | sqliteFree(pWInfo); |
| 717 | return; |
| 718 | } |