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