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 | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 16 | ** $Id: where.c,v 1.59 2002/07/31 19:50:28 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 */ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 28 | u8 indexable; /* True if this subexprssion is usable by an index */ |
| 29 | short int idxLeft; /* p->pLeft is a column in this table number. -1 if |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 30 | ** p->pLeft is not the column of any table */ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 31 | short int idxRight; /* p->pRight is a column in this table number. -1 if |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 32 | ** p->pRight is not the column of any table */ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 33 | unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */ |
| 34 | unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */ |
| 35 | unsigned prereqAll; /* Bitmask of tables referenced by p */ |
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 |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 72 | ** tree. Bit 0 of the mask is set if table base+0 is used. Bit 1 |
| 73 | ** is set if table base+1 is used. And so forth. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 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 | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 80 | ** corresponds to the first entry in the list of tables that appear |
| 81 | ** in the FROM clause of a SELECT. For UPDATE and DELETE statements |
| 82 | ** there is just a single table with "base" as the cursor number. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 84 | static int exprTableUsage(int base, Expr *p){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 85 | unsigned int mask = 0; |
| 86 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 87 | if( p->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 88 | return 1<< (p->iTable - base); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 89 | } |
| 90 | if( p->pRight ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 91 | mask = exprTableUsage(base, p->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 92 | } |
| 93 | if( p->pLeft ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 94 | mask |= exprTableUsage(base, p->pLeft); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 95 | } |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 96 | if( p->pList ){ |
| 97 | int i; |
| 98 | for(i=0; i<p->pList->nExpr; i++){ |
| 99 | mask |= exprTableUsage(base, p->pList->a[i].pExpr); |
| 100 | } |
| 101 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 102 | return mask; |
| 103 | } |
| 104 | |
| 105 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 106 | ** Return TRUE if the given operator is one of the operators that is |
| 107 | ** allowed for an indexable WHERE clause. The allowed operators are |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 108 | ** "=", "<", ">", "<=", ">=", and "IN". |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 109 | */ |
| 110 | static int allowedOp(int op){ |
| 111 | switch( op ){ |
| 112 | case TK_LT: |
| 113 | case TK_LE: |
| 114 | case TK_GT: |
| 115 | case TK_GE: |
| 116 | case TK_EQ: |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 117 | case TK_IN: |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 118 | return 1; |
| 119 | default: |
| 120 | return 0; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 125 | ** The input to this routine is an ExprInfo structure with only the |
| 126 | ** "p" field filled in. The job of this routine is to analyze the |
| 127 | ** subexpression and populate all the other fields of the ExprInfo |
| 128 | ** structure. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 129 | ** |
| 130 | ** "base" is the cursor number (the value of the iTable field) that |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 131 | ** corresponds to the first entry in the table list. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 132 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 133 | static void exprAnalyze(int base, ExprInfo *pInfo){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 134 | Expr *pExpr = pInfo->p; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 135 | pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft); |
| 136 | pInfo->prereqRight = exprTableUsage(base, pExpr->pRight); |
drh | 3f6b548 | 2002-04-02 13:26:10 +0000 | [diff] [blame] | 137 | pInfo->prereqAll = exprTableUsage(base, pExpr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 138 | pInfo->indexable = 0; |
| 139 | pInfo->idxLeft = -1; |
| 140 | pInfo->idxRight = -1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 141 | if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 142 | if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 143 | pInfo->idxRight = pExpr->pRight->iTable - base; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 144 | pInfo->indexable = 1; |
| 145 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 146 | if( pExpr->pLeft->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 147 | pInfo->idxLeft = pExpr->pLeft->iTable - base; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 148 | pInfo->indexable = 1; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 154 | ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the |
| 155 | ** left-most table in the FROM clause of that same SELECT statement and |
| 156 | ** the table has a cursor number of "base". |
| 157 | ** |
| 158 | ** This routine attempts to find an index for pTab that generates the |
| 159 | ** correct record sequence for the given ORDER BY clause. The return value |
| 160 | ** is a pointer to an index that does the job. NULL is returned if the |
| 161 | ** table has no index that will generate the correct sort order. |
| 162 | ** |
| 163 | ** If there are two or more indices that generate the correct sort order |
| 164 | ** and pPreferredIdx is one of those indices, then return pPreferredIdx. |
| 165 | */ |
| 166 | static Index *findSortingIndex( |
| 167 | Table *pTab, /* The table to be sorted */ |
| 168 | int base, /* Cursor number for pTab */ |
| 169 | ExprList *pOrderBy, /* The ORDER BY clause */ |
| 170 | Index *pPreferredIdx /* Use this index, if possible and not NULL */ |
| 171 | ){ |
| 172 | int i; |
| 173 | Index *pMatch; |
| 174 | Index *pIdx; |
| 175 | |
| 176 | assert( pOrderBy!=0 ); |
| 177 | assert( pOrderBy->nExpr>0 ); |
| 178 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 179 | Expr *p; |
| 180 | if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=SQLITE_SO_ASC ){ |
| 181 | /* Indices can only be used for ascending sort order */ |
| 182 | return 0; |
| 183 | } |
| 184 | p = pOrderBy->a[i].pExpr; |
| 185 | if( p->op!=TK_COLUMN || p->iTable!=base ){ |
| 186 | /* Can not use an index sort on anything that is not a column in the |
| 187 | ** left-most table of the FROM clause */ |
| 188 | return 0; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* If we get this far, it means the ORDER BY clause consists only of |
| 193 | ** ascending columns in the left-most table of the FROM clause. Now |
| 194 | ** check for a matching index. |
| 195 | */ |
| 196 | pMatch = 0; |
| 197 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 198 | if( pIdx->nColumn<pOrderBy->nExpr ) continue; |
| 199 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 200 | if( pOrderBy->a[i].pExpr->iColumn!=pIdx->aiColumn[i] ) break; |
| 201 | } |
| 202 | if( i>=pOrderBy->nExpr ){ |
| 203 | pMatch = pIdx; |
| 204 | if( pIdx==pPreferredIdx ) break; |
| 205 | } |
| 206 | } |
| 207 | return pMatch; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 212 | ** The return value is a pointer to an (opaque) structure that contains |
| 213 | ** information needed to terminate the loop. Later, the calling routine |
| 214 | ** should invoke sqliteWhereEnd() with the return value of this function |
| 215 | ** in order to complete the WHERE clause processing. |
| 216 | ** |
| 217 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 218 | ** |
| 219 | ** The basic idea is to do a nested loop, one loop for each table in |
| 220 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 221 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 222 | ** example, if the SQL is this: |
| 223 | ** |
| 224 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 225 | ** |
| 226 | ** Then the code generated is conceptually like the following: |
| 227 | ** |
| 228 | ** foreach row1 in t1 do \ Code generated |
| 229 | ** foreach row2 in t2 do |-- by sqliteWhereBegin() |
| 230 | ** foreach row3 in t3 do / |
| 231 | ** ... |
| 232 | ** end \ Code generated |
| 233 | ** end |-- by sqliteWhereEnd() |
| 234 | ** end / |
| 235 | ** |
| 236 | ** There are Btree cursors associated with each table. t1 uses cursor |
| 237 | ** "base". t2 uses cursor "base+1". And so forth. This routine generates |
| 238 | ** the code to open those cursors. sqliteWhereEnd() generates the code |
| 239 | ** to close them. |
| 240 | ** |
| 241 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 242 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 243 | ** the tables have indices and there are terms in the WHERE clause that |
| 244 | ** refer to those indices, a complete table scan can be avoided and the |
| 245 | ** code will run much faster. Most of the work of this routine is checking |
| 246 | ** to see if there are indices that can be used to speed up the loop. |
| 247 | ** |
| 248 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 249 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 250 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 251 | ** loops are evaluated and if false a jump is made around all subsequent |
| 252 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 253 | ** most loop) |
| 254 | ** |
| 255 | ** OUTER JOINS |
| 256 | ** |
| 257 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 258 | ** |
| 259 | ** foreach row1 in t1 do |
| 260 | ** flag = 0 |
| 261 | ** foreach row2 in t2 do |
| 262 | ** start: |
| 263 | ** ... |
| 264 | ** flag = 1 |
| 265 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 266 | ** if flag==0 then |
| 267 | ** move the row2 cursor to a null row |
| 268 | ** goto start |
| 269 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 270 | ** end |
| 271 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 272 | ** ORDER BY CLAUSE PROCESSING |
| 273 | ** |
| 274 | ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 275 | ** if there is one. If there is no ORDER BY clause or if this routine |
| 276 | ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL. |
| 277 | ** |
| 278 | ** If an index can be used so that the natural output order of the table |
| 279 | ** scan is correct for the ORDER BY clause, then that index is used and |
| 280 | ** *ppOrderBy is set to NULL. This is an optimization that prevents an |
| 281 | ** unnecessary sort of the result set if an index appropriate for the |
| 282 | ** ORDER BY clause already exists. |
| 283 | ** |
| 284 | ** If the where clause loops cannot be arranged to provide the correct |
| 285 | ** output order, then the *ppOrderBy is unchanged. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 286 | */ |
| 287 | WhereInfo *sqliteWhereBegin( |
| 288 | Parse *pParse, /* The parser context */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 289 | int base, /* VDBE cursor index for left-most table in pTabList */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 290 | SrcList *pTabList, /* A list of all tables to be scanned */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 291 | Expr *pWhere, /* The WHERE clause */ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 292 | int pushKey, /* If TRUE, leave the table key on the stack */ |
| 293 | ExprList **ppOrderBy /* An ORDER BY clause, or NULL */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 294 | ){ |
| 295 | int i; /* Loop counter */ |
| 296 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 297 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 298 | int brk, cont; /* Addresses used during code generation */ |
| 299 | int *aOrder; /* Order in which pTabList entries are searched */ |
| 300 | int nExpr; /* Number of subexpressions in the WHERE clause */ |
| 301 | int loopMask; /* One bit set for each outer loop */ |
| 302 | int haveKey; /* True if KEY is on the stack */ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 303 | int aDirect[32]; /* If TRUE, then index this table using ROWID */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 304 | int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */ |
| 305 | int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */ |
| 306 | int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */ |
drh | 83dcb1a | 2002-06-28 01:02:38 +0000 | [diff] [blame] | 307 | ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 308 | |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 309 | /* pushKey is only allowed if there is a single table (as in an INSERT or |
| 310 | ** UPDATE statement) |
| 311 | */ |
| 312 | assert( pushKey==0 || pTabList->nSrc==1 ); |
drh | 83dcb1a | 2002-06-28 01:02:38 +0000 | [diff] [blame] | 313 | |
| 314 | /* Split the WHERE clause into separate subexpressions where each |
| 315 | ** subexpression is separated by an AND operator. If the aExpr[] |
| 316 | ** array fills up, the last entry might point to an expression which |
| 317 | ** contains additional unfactored AND operators. |
| 318 | */ |
| 319 | memset(aExpr, 0, sizeof(aExpr)); |
| 320 | nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere); |
| 321 | if( nExpr==ARRAYSIZE(aExpr) ){ |
| 322 | char zBuf[50]; |
| 323 | sprintf(zBuf, "%d", ARRAYSIZE(aExpr)-1); |
| 324 | sqliteSetString(&pParse->zErrMsg, "WHERE clause too complex - no more " |
| 325 | "than ", zBuf, " terms allowed", 0); |
| 326 | pParse->nErr++; |
| 327 | return 0; |
| 328 | } |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 329 | |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 330 | /* Allocate space for aOrder[] */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 331 | aOrder = sqliteMalloc( sizeof(int) * pTabList->nSrc ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 332 | |
| 333 | /* Allocate and initialize the WhereInfo structure that will become the |
| 334 | ** return value. |
| 335 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 336 | pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel)); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 337 | if( sqlite_malloc_failed ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 338 | sqliteFree(aOrder); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 339 | sqliteFree(pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | pWInfo->pParse = pParse; |
| 343 | pWInfo->pTabList = pTabList; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 344 | pWInfo->base = base; |
| 345 | pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 346 | pWInfo->iBreak = sqliteVdbeMakeLabel(v); |
| 347 | |
| 348 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 349 | ** expression and either jump over all of the code or fall thru. |
| 350 | */ |
| 351 | if( pWhere && sqliteExprIsConstant(pWhere) ){ |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 352 | sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 353 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 354 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 355 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 356 | /* Analyze all of the subexpressions. |
| 357 | */ |
| 358 | for(i=0; i<nExpr; i++){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 359 | exprAnalyze(base, &aExpr[i]); |
drh | 1d1f305 | 2002-05-21 13:18:25 +0000 | [diff] [blame] | 360 | |
| 361 | /* If we are executing a trigger body, remove all references to |
| 362 | ** new.* and old.* tables from the prerequisite masks. |
| 363 | */ |
| 364 | if( pParse->trigStack ){ |
| 365 | int x; |
| 366 | if( (x = pParse->trigStack->newIdx) >= 0 ){ |
| 367 | int mask = ~(1 << (x - base)); |
| 368 | aExpr[i].prereqRight &= mask; |
| 369 | aExpr[i].prereqLeft &= mask; |
| 370 | aExpr[i].prereqAll &= mask; |
| 371 | } |
| 372 | if( (x = pParse->trigStack->oldIdx) >= 0 ){ |
| 373 | int mask = ~(1 << (x - base)); |
| 374 | aExpr[i].prereqRight &= mask; |
| 375 | aExpr[i].prereqLeft &= mask; |
| 376 | aExpr[i].prereqAll &= mask; |
| 377 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 378 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* Figure out a good nesting order for the tables. aOrder[0] will |
| 382 | ** be the index in pTabList of the outermost table. aOrder[1] will |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 383 | ** be the first nested loop and so on. aOrder[pTabList->nSrc-1] will |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 384 | ** be the innermost loop. |
| 385 | ** |
drh | 1d1f305 | 2002-05-21 13:18:25 +0000 | [diff] [blame] | 386 | ** Someday we will put in a good algorithm here to reorder the loops |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 387 | ** for an effiecient query. But for now, just use whatever order the |
| 388 | ** tables appear in in the pTabList. |
| 389 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 390 | for(i=0; i<pTabList->nSrc; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 391 | aOrder[i] = i; |
| 392 | } |
| 393 | |
| 394 | /* Figure out what index to use (if any) for each nested loop. |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 395 | ** 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] | 396 | ** 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] | 397 | ** loop. |
| 398 | ** |
| 399 | ** If terms exist that use the ROWID of any table, then set the |
| 400 | ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table |
| 401 | ** to the index of the term containing the ROWID. We always prefer |
| 402 | ** to use a ROWID which can directly access a table rather than an |
drh | 0a36c57 | 2002-02-18 22:49:59 +0000 | [diff] [blame] | 403 | ** index which requires reading an index first to get the rowid then |
| 404 | ** doing a second read of the actual database table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 405 | ** |
| 406 | ** Actually, if there are more than 32 tables in the join, only the |
drh | 0a36c57 | 2002-02-18 22:49:59 +0000 | [diff] [blame] | 407 | ** first 32 tables are candidates for indices. This is (again) due |
| 408 | ** to the limit of 32 bits in an integer bitmask. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 409 | */ |
| 410 | loopMask = 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 411 | for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(aDirect); i++){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 412 | int j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 413 | int idx = aOrder[i]; |
| 414 | Table *pTab = pTabList->a[idx].pTab; |
| 415 | Index *pIdx; |
| 416 | Index *pBestIdx = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 417 | int bestScore = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 418 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 419 | /* Check to see if there is an expression that uses only the |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 420 | ** ROWID field of this table. For terms of the form ROWID==expr |
| 421 | ** set iDirectEq[i] to the index of the term. For terms of the |
| 422 | ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index. |
| 423 | ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i]. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 424 | */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 425 | iDirectEq[i] = -1; |
| 426 | iDirectLt[i] = -1; |
| 427 | iDirectGt[i] = -1; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 428 | for(j=0; j<nExpr; j++){ |
| 429 | if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0 |
| 430 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 431 | switch( aExpr[j].p->op ){ |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 432 | case TK_IN: |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 433 | case TK_EQ: iDirectEq[i] = j; break; |
| 434 | case TK_LE: |
| 435 | case TK_LT: iDirectLt[i] = j; break; |
| 436 | case TK_GE: |
| 437 | case TK_GT: iDirectGt[i] = j; break; |
| 438 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 439 | } |
| 440 | if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0 |
| 441 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 442 | switch( aExpr[j].p->op ){ |
| 443 | case TK_EQ: iDirectEq[i] = j; break; |
| 444 | case TK_LE: |
| 445 | case TK_LT: iDirectGt[i] = j; break; |
| 446 | case TK_GE: |
| 447 | case TK_GT: iDirectLt[i] = j; break; |
| 448 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 451 | if( iDirectEq[i]>=0 ){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 452 | loopMask |= 1<<idx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 453 | pWInfo->a[i].pIdx = 0; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 454 | continue; |
| 455 | } |
| 456 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 457 | /* Do a search for usable indices. Leave pBestIdx pointing to |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 458 | ** the "best" index. pBestIdx is left set to NULL if no indices |
| 459 | ** are usable. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 460 | ** |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 461 | ** The best index is determined as follows. For each of the |
| 462 | ** left-most terms that is fixed by an equality operator, add |
| 463 | ** 4 to the score. The right-most term of the index may be |
| 464 | ** constrained by an inequality. Add 1 if for an "x<..." constraint |
| 465 | ** and add 2 for an "x>..." constraint. Chose the index that |
| 466 | ** gives the best score. |
| 467 | ** |
| 468 | ** This scoring system is designed so that the score can later be |
| 469 | ** used to determine how the index is used. If the score&3 is 0 |
| 470 | ** then all constraints are equalities. If score&1 is not 0 then |
| 471 | ** there is an inequality used as a termination key. (ex: "x<...") |
| 472 | ** If score&2 is not 0 then there is an inequality used as the |
| 473 | ** start key. (ex: "x>..."); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 474 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 475 | ** The IN operator (as in "<expr> IN (...)") is treated the same as |
| 476 | ** an equality comparison except that it can only be used on the |
| 477 | ** left-most column of an index and other terms of the WHERE clause |
| 478 | ** cannot be used in conjunction with the IN operator to help satisfy |
| 479 | ** other columns of the index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 480 | */ |
| 481 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 482 | int eqMask = 0; /* Index columns covered by an x=... term */ |
| 483 | int ltMask = 0; /* Index columns covered by an x<... term */ |
| 484 | int gtMask = 0; /* Index columns covered by an x>... term */ |
| 485 | int inMask = 0; /* Index columns covered by an x IN .. term */ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 486 | int nEq, m, score; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 487 | |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 488 | if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 489 | for(j=0; j<nExpr; j++){ |
| 490 | if( aExpr[j].idxLeft==idx |
| 491 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 492 | int iColumn = aExpr[j].p->pLeft->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 493 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 494 | for(k=0; k<pIdx->nColumn; k++){ |
| 495 | if( pIdx->aiColumn[k]==iColumn ){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 496 | switch( aExpr[j].p->op ){ |
drh | 48185c1 | 2002-06-09 01:55:20 +0000 | [diff] [blame] | 497 | case TK_IN: { |
| 498 | if( k==0 ) inMask |= 1; |
| 499 | break; |
| 500 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 501 | case TK_EQ: { |
| 502 | eqMask |= 1<<k; |
| 503 | break; |
| 504 | } |
| 505 | case TK_LE: |
| 506 | case TK_LT: { |
| 507 | ltMask |= 1<<k; |
| 508 | break; |
| 509 | } |
| 510 | case TK_GE: |
| 511 | case TK_GT: { |
| 512 | gtMask |= 1<<k; |
| 513 | break; |
| 514 | } |
| 515 | default: { |
| 516 | /* CANT_HAPPEN */ |
| 517 | assert( 0 ); |
| 518 | break; |
| 519 | } |
| 520 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | if( aExpr[j].idxRight==idx |
| 526 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 527 | int iColumn = aExpr[j].p->pRight->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 528 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 529 | for(k=0; k<pIdx->nColumn; k++){ |
| 530 | if( pIdx->aiColumn[k]==iColumn ){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 531 | switch( aExpr[j].p->op ){ |
| 532 | case TK_EQ: { |
| 533 | eqMask |= 1<<k; |
| 534 | break; |
| 535 | } |
| 536 | case TK_LE: |
| 537 | case TK_LT: { |
| 538 | gtMask |= 1<<k; |
| 539 | break; |
| 540 | } |
| 541 | case TK_GE: |
| 542 | case TK_GT: { |
| 543 | ltMask |= 1<<k; |
| 544 | break; |
| 545 | } |
| 546 | default: { |
| 547 | /* CANT_HAPPEN */ |
| 548 | assert( 0 ); |
| 549 | break; |
| 550 | } |
| 551 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 557 | for(nEq=0; nEq<pIdx->nColumn; nEq++){ |
| 558 | m = (1<<(nEq+1))-1; |
| 559 | if( (m & eqMask)!=m ) break; |
| 560 | } |
| 561 | score = nEq*4; |
| 562 | m = 1<<nEq; |
| 563 | if( m & ltMask ) score++; |
| 564 | if( m & gtMask ) score+=2; |
drh | 48185c1 | 2002-06-09 01:55:20 +0000 | [diff] [blame] | 565 | if( score==0 && inMask ) score = 4; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 566 | if( score>bestScore ){ |
| 567 | pBestIdx = pIdx; |
| 568 | bestScore = score; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 571 | pWInfo->a[i].pIdx = pBestIdx; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 572 | pWInfo->a[i].score = bestScore; |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 573 | loopMask |= 1<<idx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 574 | if( pBestIdx ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 575 | pWInfo->a[i].iCur = pParse->nTab++; |
| 576 | pWInfo->peakNTab = pParse->nTab; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 577 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 578 | } |
| 579 | |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 580 | /* Check to see if the ORDER BY clause is or can be satisfied by the |
| 581 | ** use of an index on the first table. |
| 582 | */ |
| 583 | if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){ |
| 584 | Index *pSortIdx; |
| 585 | Index *pIdx; |
| 586 | Table *pTab; |
| 587 | |
| 588 | pTab = pTabList->a[0].pTab; |
| 589 | pIdx = pWInfo->a[0].pIdx; |
| 590 | if( pIdx && pWInfo->a[0].score==4 ){ |
| 591 | /* If there is already an index on the left-most column and it is |
| 592 | ** an equality index, then either sorting is not helpful, or the |
| 593 | ** index is an IN operator, in which case the index does not give |
| 594 | ** the correct sort order. Either way, pretend that no suitable |
| 595 | ** index is found. |
| 596 | */ |
| 597 | pSortIdx = 0; |
| 598 | }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){ |
| 599 | /* If the left-most column is accessed using its ROWID, then do |
| 600 | ** not try to sort by index. |
| 601 | */ |
| 602 | pSortIdx = 0; |
| 603 | }else{ |
| 604 | pSortIdx = findSortingIndex(pTab, base, *ppOrderBy, pIdx); |
| 605 | } |
| 606 | if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){ |
| 607 | if( pIdx==0 ){ |
| 608 | pWInfo->a[0].pIdx = pSortIdx; |
| 609 | pWInfo->a[0].iCur = pParse->nTab++; |
| 610 | pWInfo->peakNTab = pParse->nTab; |
| 611 | } |
| 612 | *ppOrderBy = 0; |
| 613 | } |
| 614 | } |
| 615 | |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 616 | /* Open all tables in the pTabList and all indices used by those tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 617 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 618 | for(i=0; i<pTabList->nSrc; i++){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 619 | int openOp; |
| 620 | Table *pTab; |
| 621 | |
| 622 | pTab = pTabList->a[i].pTab; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 623 | if( pTab->isTransient || pTab->pSelect ) continue; |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 624 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 625 | sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum); |
| 626 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 627 | if( i==0 && !pParse->schemaVerified && |
| 628 | (pParse->db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 629 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 630 | pParse->schemaVerified = 1; |
| 631 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 632 | if( pWInfo->a[i].pIdx!=0 ){ |
| 633 | sqliteVdbeAddOp(v, openOp, pWInfo->a[i].iCur, pWInfo->a[i].pIdx->tnum); |
| 634 | sqliteVdbeChangeP3(v, -1, pWInfo->a[i].pIdx->zName, P3_STATIC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | /* Generate the code to do the search |
| 639 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 640 | loopMask = 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 641 | for(i=0; i<pTabList->nSrc; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 642 | int j, k; |
| 643 | int idx = aOrder[i]; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 644 | Index *pIdx; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 645 | WhereLevel *pLevel = &pWInfo->a[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 646 | |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 647 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 648 | ** initialize a memory cell that record if this table matches any |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 649 | ** row of the left table of the join. |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 650 | */ |
| 651 | if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){ |
| 652 | if( !pParse->nMem ) pParse->nMem++; |
| 653 | pLevel->iLeftJoin = pParse->nMem++; |
| 654 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 655 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
| 656 | } |
| 657 | |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 658 | pIdx = pLevel->pIdx; |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 659 | pLevel->inOp = OP_Noop; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 660 | if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){ |
| 661 | /* Case 1: We can directly reference a single row using an |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 662 | ** equality comparison against the ROWID field. Or |
| 663 | ** we reference multiple rows using a "rowid IN (...)" |
| 664 | ** construct. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 665 | */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 666 | k = iDirectEq[i]; |
| 667 | assert( k<nExpr ); |
| 668 | assert( aExpr[k].p!=0 ); |
| 669 | assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx ); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 670 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 671 | if( aExpr[k].idxLeft==idx ){ |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 672 | Expr *pX = aExpr[k].p; |
| 673 | if( pX->op!=TK_IN ){ |
| 674 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 675 | }else if( pX->pList ){ |
| 676 | sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk); |
| 677 | pLevel->inOp = OP_SetNext; |
| 678 | pLevel->inP1 = pX->iTable; |
| 679 | pLevel->inP2 = sqliteVdbeCurrentAddr(v); |
| 680 | }else{ |
| 681 | assert( pX->pSelect ); |
| 682 | sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk); |
| 683 | sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1); |
| 684 | pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0); |
| 685 | pLevel->inOp = OP_Next; |
| 686 | pLevel->inP1 = pX->iTable; |
| 687 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 688 | }else{ |
| 689 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 690 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 691 | aExpr[k].p = 0; |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 692 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 693 | sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 694 | haveKey = 0; |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 695 | sqliteVdbeAddOp(v, OP_NotExists, base+idx, brk); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 696 | pLevel->op = OP_Noop; |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 697 | }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){ |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 698 | /* Case 2: There is an index and all terms of the WHERE clause that |
| 699 | ** refer to the index use the "==" or "IN" operators. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 700 | */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 701 | int start; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 702 | int testOp; |
| 703 | int nColumn = pLevel->score/4; |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 704 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 705 | for(j=0; j<nColumn; j++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 706 | for(k=0; k<nExpr; k++){ |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 707 | Expr *pX = aExpr[k].p; |
| 708 | if( pX==0 ) continue; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 709 | if( aExpr[k].idxLeft==idx |
| 710 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 711 | && pX->pLeft->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 712 | ){ |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 713 | if( pX->op==TK_EQ ){ |
| 714 | sqliteExprCode(pParse, pX->pRight); |
| 715 | aExpr[k].p = 0; |
| 716 | break; |
| 717 | } |
| 718 | if( pX->op==TK_IN && nColumn==1 ){ |
| 719 | if( pX->pList ){ |
| 720 | sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk); |
| 721 | pLevel->inOp = OP_SetNext; |
| 722 | pLevel->inP1 = pX->iTable; |
| 723 | pLevel->inP2 = sqliteVdbeCurrentAddr(v); |
| 724 | }else{ |
| 725 | assert( pX->pSelect ); |
| 726 | sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk); |
| 727 | sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1); |
| 728 | pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0); |
| 729 | pLevel->inOp = OP_Next; |
| 730 | pLevel->inP1 = pX->iTable; |
| 731 | } |
| 732 | aExpr[k].p = 0; |
| 733 | break; |
| 734 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 735 | } |
| 736 | if( aExpr[k].idxRight==idx |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 737 | && aExpr[k].p->op==TK_EQ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 738 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 739 | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 740 | ){ |
| 741 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 742 | aExpr[k].p = 0; |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 747 | pLevel->iMem = pParse->nMem++; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 748 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 749 | sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0); |
| 750 | if( nColumn==pIdx->nColumn ){ |
| 751 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
| 752 | testOp = OP_IdxGT; |
| 753 | }else{ |
| 754 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 755 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 756 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 757 | testOp = OP_IdxGE; |
| 758 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 759 | sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 760 | start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 761 | sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 762 | sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 763 | if( i==pTabList->nSrc-1 && pushKey ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 764 | haveKey = 1; |
| 765 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 766 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 767 | haveKey = 0; |
| 768 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 769 | pLevel->op = OP_Next; |
| 770 | pLevel->p1 = pLevel->iCur; |
| 771 | pLevel->p2 = start; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 772 | }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){ |
| 773 | /* Case 3: We have an inequality comparison against the ROWID field. |
| 774 | */ |
| 775 | int testOp = OP_Noop; |
| 776 | int start; |
| 777 | |
| 778 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 779 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 780 | if( iDirectGt[i]>=0 ){ |
| 781 | k = iDirectGt[i]; |
| 782 | assert( k<nExpr ); |
| 783 | assert( aExpr[k].p!=0 ); |
| 784 | assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx ); |
| 785 | if( aExpr[k].idxLeft==idx ){ |
| 786 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 787 | }else{ |
| 788 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 789 | } |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 790 | sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 791 | if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){ |
| 792 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
| 793 | } |
| 794 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, brk); |
| 795 | aExpr[k].p = 0; |
| 796 | }else{ |
| 797 | sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk); |
| 798 | } |
| 799 | if( iDirectLt[i]>=0 ){ |
| 800 | k = iDirectLt[i]; |
| 801 | assert( k<nExpr ); |
| 802 | assert( aExpr[k].p!=0 ); |
| 803 | assert( aExpr[k].idxLeft==idx || aExpr[k].idxRight==idx ); |
| 804 | if( aExpr[k].idxLeft==idx ){ |
| 805 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 806 | }else{ |
| 807 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 808 | } |
drh | f1351b6 | 2002-07-31 19:50:26 +0000 | [diff] [blame] | 809 | sqliteVdbeAddOp(v, OP_MustBeInt, 1, sqliteVdbeCurrentAddr(v)+1); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 810 | pLevel->iMem = pParse->nMem++; |
| 811 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
| 812 | if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){ |
| 813 | testOp = OP_Ge; |
| 814 | }else{ |
| 815 | testOp = OP_Gt; |
| 816 | } |
| 817 | aExpr[k].p = 0; |
| 818 | } |
| 819 | start = sqliteVdbeCurrentAddr(v); |
| 820 | pLevel->op = OP_Next; |
| 821 | pLevel->p1 = base+idx; |
| 822 | pLevel->p2 = start; |
| 823 | if( testOp!=OP_Noop ){ |
| 824 | sqliteVdbeAddOp(v, OP_Recno, base+idx, 0); |
| 825 | sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 826 | sqliteVdbeAddOp(v, testOp, 0, brk); |
| 827 | } |
| 828 | haveKey = 0; |
| 829 | }else if( pIdx==0 ){ |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 830 | /* Case 4: There is no usable index. We must do a complete |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 831 | ** scan of the entire database table. |
| 832 | */ |
| 833 | int start; |
| 834 | |
| 835 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 836 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 837 | sqliteVdbeAddOp(v, OP_Rewind, base+idx, brk); |
| 838 | start = sqliteVdbeCurrentAddr(v); |
| 839 | pLevel->op = OP_Next; |
| 840 | pLevel->p1 = base+idx; |
| 841 | pLevel->p2 = start; |
| 842 | haveKey = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 843 | }else{ |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 844 | /* Case 5: The WHERE clause term that refers to the right-most |
| 845 | ** column of the index is an inequality. For example, if |
| 846 | ** the index is on (x,y,z) and the WHERE clause is of the |
| 847 | ** form "x=5 AND y<10" then this case is used. Only the |
| 848 | ** right-most column can be an inequality - the rest must |
| 849 | ** use the "==" operator. |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 850 | ** |
| 851 | ** This case is also used when there are no WHERE clause |
| 852 | ** constraints but an index is selected anyway, in order |
| 853 | ** to force the output order to conform to an ORDER BY. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 854 | */ |
| 855 | int score = pLevel->score; |
| 856 | int nEqColumn = score/4; |
| 857 | int start; |
| 858 | int leFlag, geFlag; |
| 859 | int testOp; |
| 860 | |
| 861 | /* Evaluate the equality constraints |
| 862 | */ |
| 863 | for(j=0; j<nEqColumn; j++){ |
| 864 | for(k=0; k<nExpr; k++){ |
| 865 | if( aExpr[k].p==0 ) continue; |
| 866 | if( aExpr[k].idxLeft==idx |
| 867 | && aExpr[k].p->op==TK_EQ |
| 868 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 869 | && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
| 870 | ){ |
| 871 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 872 | aExpr[k].p = 0; |
| 873 | break; |
| 874 | } |
| 875 | if( aExpr[k].idxRight==idx |
| 876 | && aExpr[k].p->op==TK_EQ |
| 877 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 878 | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
| 879 | ){ |
| 880 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 881 | aExpr[k].p = 0; |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 887 | /* Duplicate the equality term values because they will all be |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 888 | ** used twice: once to make the termination key and once to make the |
| 889 | ** start key. |
| 890 | */ |
| 891 | for(j=0; j<nEqColumn; j++){ |
| 892 | sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0); |
| 893 | } |
| 894 | |
| 895 | /* Generate the termination key. This is the key value that |
| 896 | ** will end the search. There is no termination key if there |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 897 | ** are no equality terms and no "X<..." term. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 898 | */ |
| 899 | if( (score & 1)!=0 ){ |
| 900 | for(k=0; k<nExpr; k++){ |
| 901 | Expr *pExpr = aExpr[k].p; |
| 902 | if( pExpr==0 ) continue; |
| 903 | if( aExpr[k].idxLeft==idx |
| 904 | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
| 905 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 906 | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
| 907 | ){ |
| 908 | sqliteExprCode(pParse, pExpr->pRight); |
| 909 | leFlag = pExpr->op==TK_LE; |
| 910 | aExpr[k].p = 0; |
| 911 | break; |
| 912 | } |
| 913 | if( aExpr[k].idxRight==idx |
| 914 | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
| 915 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 916 | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
| 917 | ){ |
| 918 | sqliteExprCode(pParse, pExpr->pLeft); |
| 919 | leFlag = pExpr->op==TK_GE; |
| 920 | aExpr[k].p = 0; |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | testOp = OP_IdxGE; |
| 925 | }else{ |
| 926 | testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop; |
| 927 | leFlag = 1; |
| 928 | } |
| 929 | if( testOp!=OP_Noop ){ |
| 930 | pLevel->iMem = pParse->nMem++; |
| 931 | sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + (score & 1), 0); |
| 932 | if( leFlag ){ |
| 933 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 934 | } |
| 935 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 936 | } |
| 937 | |
| 938 | /* Generate the start key. This is the key that defines the lower |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 939 | ** bound on the search. There is no start key if there are no |
| 940 | ** equality terms and if there is no "X>..." term. In |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 941 | ** that case, generate a "Rewind" instruction in place of the |
| 942 | ** start key search. |
| 943 | */ |
| 944 | if( (score & 2)!=0 ){ |
| 945 | for(k=0; k<nExpr; k++){ |
| 946 | Expr *pExpr = aExpr[k].p; |
| 947 | if( pExpr==0 ) continue; |
| 948 | if( aExpr[k].idxLeft==idx |
| 949 | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
| 950 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 951 | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
| 952 | ){ |
| 953 | sqliteExprCode(pParse, pExpr->pRight); |
| 954 | geFlag = pExpr->op==TK_GE; |
| 955 | aExpr[k].p = 0; |
| 956 | break; |
| 957 | } |
| 958 | if( aExpr[k].idxRight==idx |
| 959 | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
| 960 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 961 | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
| 962 | ){ |
| 963 | sqliteExprCode(pParse, pExpr->pLeft); |
| 964 | geFlag = pExpr->op==TK_LE; |
| 965 | aExpr[k].p = 0; |
| 966 | break; |
| 967 | } |
| 968 | } |
drh | 7900ead | 2001-11-12 13:51:43 +0000 | [diff] [blame] | 969 | }else{ |
| 970 | geFlag = 1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 971 | } |
| 972 | brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 973 | cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 974 | if( nEqColumn>0 || (score&2)!=0 ){ |
| 975 | sqliteVdbeAddOp(v, OP_MakeKey, nEqColumn + ((score&2)!=0), 0); |
| 976 | if( !geFlag ){ |
| 977 | sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 978 | } |
| 979 | sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 980 | }else{ |
| 981 | sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk); |
| 982 | } |
| 983 | |
| 984 | /* Generate the the top of the loop. If there is a termination |
| 985 | ** key we have to test for that key and abort at the top of the |
| 986 | ** loop. |
| 987 | */ |
| 988 | start = sqliteVdbeCurrentAddr(v); |
| 989 | if( testOp!=OP_Noop ){ |
| 990 | sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 991 | sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
| 992 | } |
| 993 | sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 994 | if( i==pTabList->nSrc-1 && pushKey ){ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 995 | haveKey = 1; |
| 996 | }else{ |
| 997 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
| 998 | haveKey = 0; |
| 999 | } |
| 1000 | |
| 1001 | /* Record the instruction used to terminate the loop. |
| 1002 | */ |
| 1003 | pLevel->op = OP_Next; |
| 1004 | pLevel->p1 = pLevel->iCur; |
| 1005 | pLevel->p2 = start; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1006 | } |
| 1007 | loopMask |= 1<<idx; |
| 1008 | |
| 1009 | /* Insert code to test every subexpression that can be completely |
| 1010 | ** computed using the current set of tables. |
| 1011 | */ |
| 1012 | for(j=0; j<nExpr; j++){ |
| 1013 | if( aExpr[j].p==0 ) continue; |
drh | 3f6b548 | 2002-04-02 13:26:10 +0000 | [diff] [blame] | 1014 | if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue; |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 1015 | if( pLevel->iLeftJoin && aExpr[j].p->isJoinExpr==0 ) continue; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1016 | if( haveKey ){ |
drh | 573bd27 | 2001-02-19 23:23:38 +0000 | [diff] [blame] | 1017 | haveKey = 0; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1018 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1019 | } |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 1020 | sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1021 | aExpr[j].p = 0; |
| 1022 | } |
| 1023 | brk = cont; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1024 | |
| 1025 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 1026 | ** at least one row of the right table has matched the left table. |
| 1027 | */ |
| 1028 | if( pLevel->iLeftJoin ){ |
| 1029 | pLevel->top = sqliteVdbeCurrentAddr(v); |
| 1030 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
| 1031 | sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 1032 | for(j=0; j<nExpr; j++){ |
| 1033 | if( aExpr[j].p==0 ) continue; |
| 1034 | if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue; |
| 1035 | if( haveKey ){ |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 1036 | /* Cannot happen. "haveKey" can only be true if pushKey is true |
| 1037 | ** an pushKey can only be true for DELETE and UPDATE and there are |
| 1038 | ** no outer joins with DELETE and UPDATE. |
| 1039 | */ |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 1040 | haveKey = 0; |
| 1041 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
| 1042 | } |
| 1043 | sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1); |
| 1044 | aExpr[j].p = 0; |
| 1045 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1046 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1047 | } |
| 1048 | pWInfo->iContinue = cont; |
| 1049 | if( pushKey && !haveKey ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1050 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1051 | } |
| 1052 | sqliteFree(aOrder); |
| 1053 | return pWInfo; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1057 | ** Generate the end of the WHERE loop. See comments on |
| 1058 | ** sqliteWhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1059 | */ |
| 1060 | void sqliteWhereEnd(WhereInfo *pWInfo){ |
| 1061 | Vdbe *v = pWInfo->pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1062 | int i; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1063 | int base = pWInfo->base; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1064 | WhereLevel *pLevel; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1065 | SrcList *pTabList = pWInfo->pTabList; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1066 | |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1067 | for(i=pTabList->nSrc-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1068 | pLevel = &pWInfo->a[i]; |
| 1069 | sqliteVdbeResolveLabel(v, pLevel->cont); |
| 1070 | if( pLevel->op!=OP_Noop ){ |
| 1071 | sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1072 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1073 | sqliteVdbeResolveLabel(v, pLevel->brk); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1074 | if( pLevel->inOp!=OP_Noop ){ |
| 1075 | sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2); |
| 1076 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1077 | if( pLevel->iLeftJoin ){ |
| 1078 | int addr; |
| 1079 | addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0); |
drh | bf5cd97 | 2002-06-24 12:20:23 +0000 | [diff] [blame] | 1080 | sqliteVdbeAddOp(v, OP_NotNull, 1, addr+4); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1081 | sqliteVdbeAddOp(v, OP_NullRow, base+i, 0); |
| 1082 | sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top); |
| 1083 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1084 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1085 | sqliteVdbeResolveLabel(v, pWInfo->iBreak); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1086 | for(i=0; i<pTabList->nSrc; i++){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 1087 | if( pTabList->a[i].pTab->isTransient ) continue; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1088 | pLevel = &pWInfo->a[i]; |
| 1089 | sqliteVdbeAddOp(v, OP_Close, base+i, 0); |
| 1090 | if( pLevel->pIdx!=0 ){ |
| 1091 | sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0); |
| 1092 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1093 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1094 | if( pWInfo->pParse->nTab==pWInfo->peakNTab ){ |
| 1095 | pWInfo->pParse->nTab = pWInfo->savedNTab; |
| 1096 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1097 | sqliteFree(pWInfo); |
| 1098 | return; |
| 1099 | } |