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