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