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