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