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