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