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