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 | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame^] | 16 | ** $Id: where.c,v 1.32 2002/01/09 03:20:00 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 | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 170 | int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */ |
| 171 | int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */ |
| 172 | int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 173 | ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */ |
| 174 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 175 | /* Allocate space for aOrder[] and aiMem[]. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 176 | aOrder = sqliteMalloc( sizeof(int) * pTabList->nId ); |
| 177 | |
| 178 | /* Allocate and initialize the WhereInfo structure that will become the |
| 179 | ** return value. |
| 180 | */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 181 | pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nId*sizeof(WhereLevel) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 182 | if( sqlite_malloc_failed ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 183 | sqliteFree(aOrder); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 184 | sqliteFree(pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | pWInfo->pParse = pParse; |
| 188 | pWInfo->pTabList = pTabList; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 189 | base = pWInfo->base = pParse->nTab; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 190 | nCur = base + pTabList->nId; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 191 | |
| 192 | /* Split the WHERE clause into as many as 32 separate subexpressions |
| 193 | ** where each subexpression is separated by an AND operator. Any additional |
| 194 | ** subexpressions are attached in the aExpr[32] and will not enter |
| 195 | ** into the query optimizer computations. 32 is chosen as the cutoff |
| 196 | ** since that is the number of bits in an integer that we use for an |
| 197 | ** expression-used mask. |
| 198 | */ |
| 199 | memset(aExpr, 0, sizeof(aExpr)); |
| 200 | nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere); |
| 201 | |
| 202 | /* Analyze all of the subexpressions. |
| 203 | */ |
| 204 | for(i=0; i<nExpr; i++){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 205 | exprAnalyze(pParse->nTab, &aExpr[i]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* Figure out a good nesting order for the tables. aOrder[0] will |
| 209 | ** be the index in pTabList of the outermost table. aOrder[1] will |
| 210 | ** be the first nested loop and so on. aOrder[pTabList->nId-1] will |
| 211 | ** be the innermost loop. |
| 212 | ** |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 213 | ** Someday will put in a good algorithm here to reorder the loops |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 214 | ** for an effiecient query. But for now, just use whatever order the |
| 215 | ** tables appear in in the pTabList. |
| 216 | */ |
| 217 | for(i=0; i<pTabList->nId; i++){ |
| 218 | aOrder[i] = i; |
| 219 | } |
| 220 | |
| 221 | /* Figure out what index to use (if any) for each nested loop. |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 222 | ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested |
| 223 | ** loop where i==0 is the outer loop and i==pTabList->nId-1 is the inner |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 224 | ** loop. |
| 225 | ** |
| 226 | ** If terms exist that use the ROWID of any table, then set the |
| 227 | ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table |
| 228 | ** to the index of the term containing the ROWID. We always prefer |
| 229 | ** to use a ROWID which can directly access a table rather than an |
| 230 | ** index which requires two accesses. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 231 | ** |
| 232 | ** Actually, if there are more than 32 tables in the join, only the |
| 233 | ** first 32 tables are candidates for indices. |
| 234 | */ |
| 235 | loopMask = 0; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 236 | for(i=0; i<pTabList->nId && i<ARRAYSIZE(aDirect); i++){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 237 | int j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 238 | int idx = aOrder[i]; |
| 239 | Table *pTab = pTabList->a[idx].pTab; |
| 240 | Index *pIdx; |
| 241 | Index *pBestIdx = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 242 | int bestScore = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 243 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 244 | /* Check to see if there is an expression that uses only the |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 245 | ** ROWID field of this table. For terms of the form ROWID==expr |
| 246 | ** set iDirectEq[i] to the index of the term. For terms of the |
| 247 | ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index. |
| 248 | ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i]. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 249 | */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 250 | iDirectEq[i] = -1; |
| 251 | iDirectLt[i] = -1; |
| 252 | iDirectGt[i] = -1; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 253 | for(j=0; j<nExpr; j++){ |
| 254 | if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0 |
| 255 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 256 | switch( aExpr[j].p->op ){ |
| 257 | case TK_EQ: iDirectEq[i] = j; break; |
| 258 | case TK_LE: |
| 259 | case TK_LT: iDirectLt[i] = j; break; |
| 260 | case TK_GE: |
| 261 | case TK_GT: iDirectGt[i] = j; break; |
| 262 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 263 | } |
| 264 | if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0 |
| 265 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 266 | switch( aExpr[j].p->op ){ |
| 267 | case TK_EQ: iDirectEq[i] = j; break; |
| 268 | case TK_LE: |
| 269 | case TK_LT: iDirectGt[i] = j; break; |
| 270 | case TK_GE: |
| 271 | case TK_GT: iDirectLt[i] = j; break; |
| 272 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 275 | if( iDirectEq[i]>=0 ){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 276 | loopMask |= 1<<idx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 277 | pWInfo->a[i].pIdx = 0; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 278 | continue; |
| 279 | } |
| 280 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 281 | /* Do a search for usable indices. Leave pBestIdx pointing to |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 282 | ** the "best" index. pBestIdx is left set to NULL if no indices |
| 283 | ** are usable. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 284 | ** |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 285 | ** The best index is determined as follows. For each of the |
| 286 | ** left-most terms that is fixed by an equality operator, add |
| 287 | ** 4 to the score. The right-most term of the index may be |
| 288 | ** constrained by an inequality. Add 1 if for an "x<..." constraint |
| 289 | ** and add 2 for an "x>..." constraint. Chose the index that |
| 290 | ** gives the best score. |
| 291 | ** |
| 292 | ** This scoring system is designed so that the score can later be |
| 293 | ** used to determine how the index is used. If the score&3 is 0 |
| 294 | ** then all constraints are equalities. If score&1 is not 0 then |
| 295 | ** there is an inequality used as a termination key. (ex: "x<...") |
| 296 | ** If score&2 is not 0 then there is an inequality used as the |
| 297 | ** start key. (ex: "x>..."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 298 | */ |
| 299 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 300 | int eqMask = 0; /* Index columns covered by an x=... constraint */ |
| 301 | int ltMask = 0; /* Index columns covered by an x<... constraint */ |
| 302 | int gtMask = 0; /* Index columns covered by an x>... constraing */ |
| 303 | int nEq, m, score; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | |
drh | 74e24cd | 2002-01-09 03:19:59 +0000 | [diff] [blame^] | 305 | if( pIdx->isDropped ) continue; /* Ignore dropped indices */ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 306 | if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 307 | for(j=0; j<nExpr; j++){ |
| 308 | if( aExpr[j].idxLeft==idx |
| 309 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 310 | int iColumn = aExpr[j].p->pLeft->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 311 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 312 | for(k=0; k<pIdx->nColumn; k++){ |
| 313 | if( pIdx->aiColumn[k]==iColumn ){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 314 | switch( aExpr[j].p->op ){ |
| 315 | case TK_EQ: { |
| 316 | eqMask |= 1<<k; |
| 317 | break; |
| 318 | } |
| 319 | case TK_LE: |
| 320 | case TK_LT: { |
| 321 | ltMask |= 1<<k; |
| 322 | break; |
| 323 | } |
| 324 | case TK_GE: |
| 325 | case TK_GT: { |
| 326 | gtMask |= 1<<k; |
| 327 | break; |
| 328 | } |
| 329 | default: { |
| 330 | /* CANT_HAPPEN */ |
| 331 | assert( 0 ); |
| 332 | break; |
| 333 | } |
| 334 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | if( aExpr[j].idxRight==idx |
| 340 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 341 | int iColumn = aExpr[j].p->pRight->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 342 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 343 | for(k=0; k<pIdx->nColumn; k++){ |
| 344 | if( pIdx->aiColumn[k]==iColumn ){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 345 | switch( aExpr[j].p->op ){ |
| 346 | case TK_EQ: { |
| 347 | eqMask |= 1<<k; |
| 348 | break; |
| 349 | } |
| 350 | case TK_LE: |
| 351 | case TK_LT: { |
| 352 | gtMask |= 1<<k; |
| 353 | break; |
| 354 | } |
| 355 | case TK_GE: |
| 356 | case TK_GT: { |
| 357 | ltMask |= 1<<k; |
| 358 | break; |
| 359 | } |
| 360 | default: { |
| 361 | /* CANT_HAPPEN */ |
| 362 | assert( 0 ); |
| 363 | break; |
| 364 | } |
| 365 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 366 | break; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 371 | for(nEq=0; nEq<pIdx->nColumn; nEq++){ |
| 372 | m = (1<<(nEq+1))-1; |
| 373 | if( (m & eqMask)!=m ) break; |
| 374 | } |
| 375 | score = nEq*4; |
| 376 | m = 1<<nEq; |
| 377 | if( m & ltMask ) score++; |
| 378 | if( m & gtMask ) score+=2; |
| 379 | if( score>bestScore ){ |
| 380 | pBestIdx = pIdx; |
| 381 | bestScore = score; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 384 | pWInfo->a[i].pIdx = pBestIdx; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 385 | pWInfo->a[i].score = bestScore; |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 386 | loopMask |= 1<<idx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 387 | if( pBestIdx ){ |
| 388 | pWInfo->a[i].iCur = nCur++; |
| 389 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 390 | } |
| 391 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 392 | /* Open all tables in the pTabList and all indices used by those tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 393 | */ |
| 394 | for(i=0; i<pTabList->nId; i++){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 395 | int openOp; |
| 396 | Table *pTab; |
| 397 | |
| 398 | pTab = pTabList->a[i].pTab; |
| 399 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 400 | sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum); |
| 401 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 402 | if( i==0 && !pParse->schemaVerified && |
| 403 | (pParse->db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 404 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 405 | pParse->schemaVerified = 1; |
| 406 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 407 | if( pWInfo->a[i].pIdx!=0 ){ |
| 408 | sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum); |
| 409 | sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | /* Generate the code to do the search |
| 414 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 415 | loopMask = 0; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 416 | pWInfo->iBreak = sqliteVdbeMakeLabel(v); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 417 | for(i=0; i<pTabList->nId; i++){ |
| 418 | int j, k; |
| 419 | int idx = aOrder[i]; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 420 | Index *pIdx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 421 | WhereLevel *pLevel = &pWInfo->a[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 422 | |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 423 | pIdx = pLevel->pIdx; |
| 424 | if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){ |
| 425 | /* Case 1: We can directly reference a single row using an |
| 426 | ** equality comparison against the ROWID field. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 427 | */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 428 | k = iDirectEq[i]; |
| 429 | assert( k<nExpr ); |
| 430 | assert( aExpr[k].p!=0 ); |
| 431 | assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx ); |
| 432 | if( aExpr[k].idxLeft==idx ){ |
| 433 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 434 | }else{ |
| 435 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 436 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 437 | aExpr[k].p = 0; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 438 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 439 | cont = pLevel->cont = brk; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 440 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 441 | if( i==pTabList->nId-1 && pushKey ){ |
| 442 | haveKey = 1; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 443 | sqliteVdbeAddOp(v, OP_Distinct, base+idx, brk); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 444 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 445 | sqliteVdbeAddOp(v, OP_NotFound, base+idx, brk); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 446 | haveKey = 0; |
| 447 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 448 | pLevel->op = OP_Noop; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 449 | }else if( pIdx!=0 && pLevel->score%4==0 ){ |
| 450 | /* Case 2: All index constraints are equality operators. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 451 | */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 452 | int start; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 453 | int testOp; |
| 454 | int nColumn = pLevel->score/4; |
| 455 | for(j=0; j<nColumn; j++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 456 | for(k=0; k<nExpr; k++){ |
| 457 | if( aExpr[k].p==0 ) continue; |
| 458 | if( aExpr[k].idxLeft==idx |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 459 | && aExpr[k].p->op==TK_EQ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 460 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 461 | && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 462 | ){ |
| 463 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 464 | aExpr[k].p = 0; |
| 465 | break; |
| 466 | } |
| 467 | if( aExpr[k].idxRight==idx |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 468 | && aExpr[k].p->op==TK_EQ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 469 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 470 | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 471 | ){ |
| 472 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 473 | aExpr[k].p = 0; |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 478 | pLevel->iMem = pParse->nMem++; |
| 479 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 480 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 481 | sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0); |
| 482 | if( nColumn==pIdx->nColumn ){ |
| 483 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
| 484 | testOp = OP_IdxGT; |
| 485 | }else{ |
| 486 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 487 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 488 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 489 | testOp = OP_IdxGE; |
| 490 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 491 | sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 492 | start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 493 | sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 494 | sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 495 | if( i==pTabList->nId-1 && pushKey ){ |
| 496 | haveKey = 1; |
| 497 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 498 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 499 | haveKey = 0; |
| 500 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 501 | pLevel->op = OP_Next; |
| 502 | pLevel->p1 = pLevel->iCur; |
| 503 | pLevel->p2 = start; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 504 | }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){ |
| 505 | /* Case 3: We have an inequality comparison against the ROWID field. |
| 506 | */ |
| 507 | int testOp = OP_Noop; |
| 508 | int start; |
| 509 | |
| 510 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 511 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 512 | if( iDirectGt[i]>=0 ){ |
| 513 | k = iDirectGt[i]; |
| 514 | assert( k<nExpr ); |
| 515 | assert( aExpr[k].p!=0 ); |
| 516 | assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx ); |
| 517 | if( aExpr[k].idxLeft==idx ){ |
| 518 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 519 | }else{ |
| 520 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 521 | } |
| 522 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, brk); |
| 523 | if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){ |
| 524 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 525 | } |
| 526 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk); |
| 527 | aExpr[k].p = 0; |
| 528 | }else{ |
| 529 | sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk); |
| 530 | } |
| 531 | if( iDirectLt[i]>=0 ){ |
| 532 | k = iDirectLt[i]; |
| 533 | assert( k<nExpr ); |
| 534 | assert( aExpr[k].p!=0 ); |
| 535 | assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx ); |
| 536 | if( aExpr[k].idxLeft==idx ){ |
| 537 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 538 | }else{ |
| 539 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 540 | } |
| 541 | sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1); |
| 542 | pLevel->iMem = pParse->nMem++; |
| 543 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
| 544 | if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){ |
| 545 | testOp = OP_Ge; |
| 546 | }else{ |
| 547 | testOp = OP_Gt; |
| 548 | } |
| 549 | aExpr[k].p = 0; |
| 550 | } |
| 551 | start = sqliteVdbeCurrentAddr(v); |
| 552 | pLevel->op = OP_Next; |
| 553 | pLevel->p1 = base+idx; |
| 554 | pLevel->p2 = start; |
| 555 | if( testOp!=OP_Noop ){ |
| 556 | sqliteVdbeAddOp(v, OP_Recno, base+idx, 0); |
| 557 | sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 558 | sqliteVdbeAddOp(v, testOp, 0, brk); |
| 559 | } |
| 560 | haveKey = 0; |
| 561 | }else if( pIdx==0 ){ |
| 562 | /* Case 4: There was no usable index. We must do a complete |
| 563 | ** scan of the entire database table. |
| 564 | */ |
| 565 | int start; |
| 566 | |
| 567 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 568 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 569 | sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk); |
| 570 | start = sqliteVdbeCurrentAddr(v); |
| 571 | pLevel->op = OP_Next; |
| 572 | pLevel->p1 = base+idx; |
| 573 | pLevel->p2 = start; |
| 574 | haveKey = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 575 | }else{ |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 576 | /* Case 5: The contraint on the right-most index field is |
| 577 | ** an inequality. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 578 | */ |
| 579 | int score = pLevel->score; |
| 580 | int nEqColumn = score/4; |
| 581 | int start; |
| 582 | int leFlag, geFlag; |
| 583 | int testOp; |
| 584 | |
| 585 | /* Evaluate the equality constraints |
| 586 | */ |
| 587 | for(j=0; j<nEqColumn; j++){ |
| 588 | for(k=0; k<nExpr; k++){ |
| 589 | if( aExpr[k].p==0 ) continue; |
| 590 | if( aExpr[k].idxLeft==idx |
| 591 | && aExpr[k].p->op==TK_EQ |
| 592 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 593 | && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
| 594 | ){ |
| 595 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 596 | aExpr[k].p = 0; |
| 597 | break; |
| 598 | } |
| 599 | if( aExpr[k].idxRight==idx |
| 600 | && aExpr[k].p->op==TK_EQ |
| 601 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 602 | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
| 603 | ){ |
| 604 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 605 | aExpr[k].p = 0; |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* Duplicate the equality contraint values because they will all be |
| 612 | ** used twice: once to make the termination key and once to make the |
| 613 | ** start key. |
| 614 | */ |
| 615 | for(j=0; j<nEqColumn; j++){ |
| 616 | sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0); |
| 617 | } |
| 618 | |
| 619 | /* Generate the termination key. This is the key value that |
| 620 | ** will end the search. There is no termination key if there |
| 621 | ** are no equality contraints and no "X<..." constraint. |
| 622 | */ |
| 623 | if( (score & 1)!=0 ){ |
| 624 | for(k=0; k<nExpr; k++){ |
| 625 | Expr *pExpr = aExpr[k].p; |
| 626 | if( pExpr==0 ) continue; |
| 627 | if( aExpr[k].idxLeft==idx |
| 628 | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
| 629 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 630 | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
| 631 | ){ |
| 632 | sqliteExprCode(pParse, pExpr->pRight); |
| 633 | leFlag = pExpr->op==TK_LE; |
| 634 | aExpr[k].p = 0; |
| 635 | break; |
| 636 | } |
| 637 | if( aExpr[k].idxRight==idx |
| 638 | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
| 639 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 640 | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
| 641 | ){ |
| 642 | sqliteExprCode(pParse, pExpr->pLeft); |
| 643 | leFlag = pExpr->op==TK_GE; |
| 644 | aExpr[k].p = 0; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | testOp = OP_IdxGE; |
| 649 | }else{ |
| 650 | testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop; |
| 651 | leFlag = 1; |
| 652 | } |
| 653 | if( testOp!=OP_Noop ){ |
| 654 | pLevel->iMem = pParse->nMem++; |
| 655 | sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0); |
| 656 | if( leFlag ){ |
| 657 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 658 | } |
| 659 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 660 | } |
| 661 | |
| 662 | /* Generate the start key. This is the key that defines the lower |
| 663 | ** bound on the search. There is no start key if there are not |
| 664 | ** equality constraints and if there is no "X>..." constraint. In |
| 665 | ** that case, generate a "Rewind" instruction in place of the |
| 666 | ** start key search. |
| 667 | */ |
| 668 | if( (score & 2)!=0 ){ |
| 669 | for(k=0; k<nExpr; k++){ |
| 670 | Expr *pExpr = aExpr[k].p; |
| 671 | if( pExpr==0 ) continue; |
| 672 | if( aExpr[k].idxLeft==idx |
| 673 | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
| 674 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 675 | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
| 676 | ){ |
| 677 | sqliteExprCode(pParse, pExpr->pRight); |
| 678 | geFlag = pExpr->op==TK_GE; |
| 679 | aExpr[k].p = 0; |
| 680 | break; |
| 681 | } |
| 682 | if( aExpr[k].idxRight==idx |
| 683 | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
| 684 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 685 | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
| 686 | ){ |
| 687 | sqliteExprCode(pParse, pExpr->pLeft); |
| 688 | geFlag = pExpr->op==TK_LE; |
| 689 | aExpr[k].p = 0; |
| 690 | break; |
| 691 | } |
| 692 | } |
drh | 7900ead | 2001-11-12 13:51:43 +0000 | [diff] [blame] | 693 | }else{ |
| 694 | geFlag = 1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 695 | } |
| 696 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 697 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 698 | if( nEqColumn>0 || (score&2)!=0 ){ |
| 699 | sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0); |
| 700 | if( !geFlag ){ |
| 701 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 702 | } |
| 703 | sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 704 | }else{ |
| 705 | sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk); |
| 706 | } |
| 707 | |
| 708 | /* Generate the the top of the loop. If there is a termination |
| 709 | ** key we have to test for that key and abort at the top of the |
| 710 | ** loop. |
| 711 | */ |
| 712 | start = sqliteVdbeCurrentAddr(v); |
| 713 | if( testOp!=OP_Noop ){ |
| 714 | sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 715 | sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
| 716 | } |
| 717 | sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
| 718 | if( i==pTabList->nId-1 && pushKey ){ |
| 719 | haveKey = 1; |
| 720 | }else{ |
| 721 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
| 722 | haveKey = 0; |
| 723 | } |
| 724 | |
| 725 | /* Record the instruction used to terminate the loop. |
| 726 | */ |
| 727 | pLevel->op = OP_Next; |
| 728 | pLevel->p1 = pLevel->iCur; |
| 729 | pLevel->p2 = start; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 730 | } |
| 731 | loopMask |= 1<<idx; |
| 732 | |
| 733 | /* Insert code to test every subexpression that can be completely |
| 734 | ** computed using the current set of tables. |
| 735 | */ |
| 736 | for(j=0; j<nExpr; j++){ |
| 737 | if( aExpr[j].p==0 ) continue; |
| 738 | if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue; |
| 739 | if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue; |
| 740 | if( haveKey ){ |
drh | 573bd27 | 2001-02-19 23:23:38 +0000 | [diff] [blame] | 741 | haveKey = 0; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 742 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 743 | } |
| 744 | sqliteExprIfFalse(pParse, aExpr[j].p, cont); |
| 745 | aExpr[j].p = 0; |
| 746 | } |
| 747 | brk = cont; |
| 748 | } |
| 749 | pWInfo->iContinue = cont; |
| 750 | if( pushKey && !haveKey ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 751 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 752 | } |
| 753 | sqliteFree(aOrder); |
| 754 | return pWInfo; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | ** Generate the end of the WHERE loop. |
| 759 | */ |
| 760 | void sqliteWhereEnd(WhereInfo *pWInfo){ |
| 761 | Vdbe *v = pWInfo->pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 762 | int i; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 763 | int base = pWInfo->base; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 764 | WhereLevel *pLevel; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 765 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 766 | for(i=pWInfo->pTabList->nId-1; i>=0; i--){ |
| 767 | pLevel = &pWInfo->a[i]; |
| 768 | sqliteVdbeResolveLabel(v, pLevel->cont); |
| 769 | if( pLevel->op!=OP_Noop ){ |
| 770 | sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 771 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 772 | sqliteVdbeResolveLabel(v, pLevel->brk); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 773 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 774 | sqliteVdbeResolveLabel(v, pWInfo->iBreak); |
| 775 | for(i=0; i<pWInfo->pTabList->nId; i++){ |
| 776 | pLevel = &pWInfo->a[i]; |
| 777 | sqliteVdbeAddOp(v, OP_Close, base+i, 0); |
| 778 | if( pLevel->pIdx!=0 ){ |
| 779 | sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0); |
| 780 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 781 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 782 | sqliteFree(pWInfo); |
| 783 | return; |
| 784 | } |