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