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 | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 13 | ** the WHERE clause of SQL statements. This module is reponsible for |
| 14 | ** generating the code that loops through a table looking for applicable |
| 15 | ** rows. Indices are selected and used to speed the search when doing |
| 16 | ** so is applicable. Because this module is responsible for selecting |
| 17 | ** indices, you might also think of this module as the "query optimizer". |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | ** |
drh | ac931eb | 2005-01-11 18:13:56 +0000 | [diff] [blame] | 19 | ** $Id: where.c,v 1.128 2005/01/11 18:13:56 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | */ |
| 21 | #include "sqliteInt.h" |
| 22 | |
| 23 | /* |
| 24 | ** The query generator uses an array of instances of this structure to |
| 25 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 26 | ** clause subexpression is separated from the others by an AND operator. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 27 | ** |
| 28 | ** The idxLeft and idxRight fields are the VDBE cursor numbers for the |
| 29 | ** table that contains the column that appears on the left-hand and |
| 30 | ** right-hand side of ExprInfo.p. If either side of ExprInfo.p is |
| 31 | ** something other than a simple column reference, then idxLeft or |
| 32 | ** idxRight are -1. |
| 33 | ** |
| 34 | ** It is the VDBE cursor number is the value stored in Expr.iTable |
| 35 | ** when Expr.op==TK_COLUMN and the value stored in SrcList.a[].iCursor. |
| 36 | ** |
| 37 | ** prereqLeft, prereqRight, and prereqAll record sets of cursor numbers, |
| 38 | ** but they do so indirectly. A single ExprMaskSet structure translates |
| 39 | ** cursor number into bits and the translated bit is stored in the prereq |
| 40 | ** fields. The translation is used in order to maximize the number of |
| 41 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 42 | ** spread out over the non-negative integers. For example, the cursor |
| 43 | ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The ExprMaskSet |
| 44 | ** translates these sparse cursor numbers into consecutive integers |
| 45 | ** beginning with 0 in order to make the best possible use of the available |
| 46 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 47 | ** would be mapped into integers 0 through 7. |
| 48 | ** |
| 49 | ** prereqLeft tells us every VDBE cursor that is referenced on the |
| 50 | ** left-hand side of ExprInfo.p. prereqRight does the same for the |
| 51 | ** right-hand side of the expression. The following identity always |
| 52 | ** holds: |
| 53 | ** |
| 54 | ** prereqAll = prereqLeft | prereqRight |
| 55 | ** |
| 56 | ** The ExprInfo.indexable field is true if the ExprInfo.p expression |
| 57 | ** is of a form that might control an index. Indexable expressions |
| 58 | ** look like this: |
| 59 | ** |
| 60 | ** <column> <op> <expr> |
| 61 | ** |
| 62 | ** Where <column> is a simple column name and <op> is on of the operators |
| 63 | ** that allowedOp() recognizes. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 64 | */ |
| 65 | typedef struct ExprInfo ExprInfo; |
| 66 | struct ExprInfo { |
| 67 | Expr *p; /* Pointer to the subexpression */ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 68 | u8 indexable; /* True if this subexprssion is usable by an index */ |
| 69 | 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] | 70 | ** p->pLeft is not the column of any table */ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 71 | 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] | 72 | ** p->pRight is not the column of any table */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 73 | Bitmask prereqLeft; /* Bitmask of tables referenced by p->pLeft */ |
| 74 | Bitmask prereqRight; /* Bitmask of tables referenced by p->pRight */ |
| 75 | Bitmask prereqAll; /* Bitmask of tables referenced by p */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 79 | ** An instance of the following structure keeps track of a mapping |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 80 | ** between VDBE cursor numbers and bits of the bitmasks in ExprInfo. |
| 81 | ** |
| 82 | ** The VDBE cursor numbers are small integers contained in |
| 83 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 84 | ** clause, the cursor numbers might not begin with 0 and they might |
| 85 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 86 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 87 | ** from the sparse cursor numbers into consecutive integers beginning |
| 88 | ** with 0. |
| 89 | ** |
| 90 | ** If ExprMaskSet.ix[A]==B it means that The A-th bit of a Bitmask |
| 91 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 92 | ** |
| 93 | ** For example, if the WHERE clause expression used these VDBE |
| 94 | ** cursors: 4, 5, 8, 29, 57, 73. Then the ExprMaskSet structure |
| 95 | ** would map those cursor numbers into bits 0 through 5. |
| 96 | ** |
| 97 | ** Note that the mapping is not necessarily ordered. In the example |
| 98 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 99 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 100 | ** does not really matter. What is important is that sparse cursor |
| 101 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 102 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 103 | */ |
| 104 | typedef struct ExprMaskSet ExprMaskSet; |
| 105 | struct ExprMaskSet { |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 106 | int n; /* Number of assigned cursor values */ |
| 107 | int ix[sizeof(Bitmask)*8-1]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 111 | ** Determine the number of elements in an array. |
| 112 | */ |
| 113 | #define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0])) |
| 114 | |
| 115 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 116 | ** This routine identifies subexpressions in the WHERE clause where |
| 117 | ** each subexpression is separate by the AND operator. aSlot is |
| 118 | ** filled with pointers to the subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 119 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 120 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 121 | ** \________/ \_______________/ \________________/ |
| 122 | ** slot[0] slot[1] slot[2] |
| 123 | ** |
| 124 | ** The original WHERE clause in pExpr is unaltered. All this routine |
| 125 | ** does is make aSlot[] entries point to substructure within pExpr. |
| 126 | ** |
| 127 | ** aSlot[] is an array of subexpressions structures. There are nSlot |
| 128 | ** spaces left in this array. This routine finds as many AND-separated |
| 129 | ** subexpressions as it can and puts pointers to those subexpressions |
| 130 | ** into aSlot[] entries. The return value is the number of slots filled. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 131 | */ |
| 132 | static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){ |
| 133 | int cnt = 0; |
| 134 | if( pExpr==0 || nSlot<1 ) return 0; |
| 135 | if( nSlot==1 || pExpr->op!=TK_AND ){ |
| 136 | aSlot[0].p = pExpr; |
| 137 | return 1; |
| 138 | } |
| 139 | if( pExpr->pLeft->op!=TK_AND ){ |
| 140 | aSlot[0].p = pExpr->pLeft; |
| 141 | cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight); |
| 142 | }else{ |
drh | dcd997e | 2003-01-31 17:21:49 +0000 | [diff] [blame] | 143 | cnt = exprSplit(nSlot, aSlot, pExpr->pLeft); |
| 144 | cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 145 | } |
| 146 | return cnt; |
| 147 | } |
| 148 | |
| 149 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 150 | ** Initialize an expression mask set |
| 151 | */ |
| 152 | #define initMaskSet(P) memset(P, 0, sizeof(*P)) |
| 153 | |
| 154 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 155 | ** Return the bitmask for the given cursor number. Assign a new bitmask |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 156 | ** if this is the first time the cursor has been seen. |
| 157 | */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 158 | static Bitmask getMask(ExprMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 159 | int i; |
| 160 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 161 | if( pMaskSet->ix[i]==iCursor ){ |
| 162 | return ((Bitmask)1)<<i; |
| 163 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 164 | } |
| 165 | if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){ |
| 166 | pMaskSet->n++; |
| 167 | pMaskSet->ix[i] = iCursor; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 168 | return ((Bitmask)1)<<i; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | ** Destroy an expression mask set |
| 175 | */ |
| 176 | #define freeMaskSet(P) /* NO-OP */ |
| 177 | |
| 178 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 179 | ** This routine walks (recursively) an expression tree and generates |
| 180 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 181 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 182 | ** |
| 183 | ** In order for this routine to work, the calling function must have |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 184 | ** previously invoked sqlite3ExprResolveIds() on the expression. See |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 185 | ** the header comment on that routine for additional information. |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 186 | ** The sqlite3ExprResolveIds() routines looks for column names and |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 187 | ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to |
| 188 | ** the VDBE cursor number of the table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 189 | */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 190 | static Bitmask exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){ |
| 191 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 192 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 193 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 194 | mask = getMask(pMaskSet, p->iTable); |
| 195 | if( mask==0 ) mask = -1; |
| 196 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 197 | } |
| 198 | if( p->pRight ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 199 | mask = exprTableUsage(pMaskSet, p->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 200 | } |
| 201 | if( p->pLeft ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 202 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 203 | } |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 204 | if( p->pList ){ |
| 205 | int i; |
| 206 | for(i=0; i<p->pList->nExpr; i++){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 207 | mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 210 | return mask; |
| 211 | } |
| 212 | |
| 213 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 214 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 215 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 216 | ** "=", "<", ">", "<=", ">=", and "IN". |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 217 | */ |
| 218 | static int allowedOp(int op){ |
drh | 9a43267 | 2004-10-04 13:38:09 +0000 | [diff] [blame] | 219 | assert( TK_GT==TK_LE-1 && TK_LE==TK_LT-1 && TK_LT==TK_GE-1 && TK_EQ==TK_GT-1); |
| 220 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 224 | ** Swap two objects of type T. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 225 | */ |
| 226 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 227 | |
| 228 | /* |
| 229 | ** Return the index in the SrcList that uses cursor iCur. If iCur is |
| 230 | ** used by the first entry in SrcList return 0. If iCur is used by |
| 231 | ** the second entry return 1. And so forth. |
| 232 | ** |
| 233 | ** SrcList is the set of tables in the FROM clause in the order that |
| 234 | ** they will be processed. The value returned here gives us an index |
| 235 | ** of which tables will be processed first. |
| 236 | */ |
| 237 | static int tableOrder(SrcList *pList, int iCur){ |
| 238 | int i; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 239 | struct SrcList_item *pItem; |
| 240 | for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
| 241 | if( pItem->iCursor==iCur ) return i; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 242 | } |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 247 | ** The input to this routine is an ExprInfo structure with only the |
| 248 | ** "p" field filled in. The job of this routine is to analyze the |
| 249 | ** subexpression and populate all the other fields of the ExprInfo |
| 250 | ** structure. |
| 251 | */ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 252 | static void exprAnalyze(SrcList *pSrc, ExprMaskSet *pMaskSet, ExprInfo *pInfo){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 253 | Expr *pExpr = pInfo->p; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 254 | pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 255 | pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 256 | pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 257 | pInfo->indexable = 0; |
| 258 | pInfo->idxLeft = -1; |
| 259 | pInfo->idxRight = -1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 260 | if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 261 | if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 262 | pInfo->idxRight = pExpr->pRight->iTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 263 | pInfo->indexable = 1; |
| 264 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 265 | if( pExpr->pLeft->op==TK_COLUMN ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 266 | pInfo->idxLeft = pExpr->pLeft->iTable; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 267 | pInfo->indexable = 1; |
| 268 | } |
| 269 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 270 | if( pInfo->indexable ){ |
| 271 | assert( pInfo->idxLeft!=pInfo->idxRight ); |
| 272 | |
| 273 | /* We want the expression to be of the form "X = expr", not "expr = X". |
| 274 | ** So flip it over if necessary. If the expression is "X = Y", then |
| 275 | ** we want Y to come from an earlier table than X. |
| 276 | ** |
| 277 | ** The collating sequence rule is to always choose the left expression. |
| 278 | ** So if we do a flip, we also have to move the collating sequence. |
| 279 | */ |
| 280 | if( tableOrder(pSrc,pInfo->idxLeft)<tableOrder(pSrc,pInfo->idxRight) ){ |
| 281 | assert( pExpr->op!=TK_IN ); |
| 282 | SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl); |
| 283 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
drh | 9a43267 | 2004-10-04 13:38:09 +0000 | [diff] [blame] | 284 | if( pExpr->op>=TK_GT ){ |
| 285 | assert( TK_LT==TK_GT+2 ); |
| 286 | assert( TK_GE==TK_LE+2 ); |
| 287 | assert( TK_GT>TK_EQ ); |
| 288 | assert( TK_GT<TK_LE ); |
| 289 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 290 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 291 | } |
| 292 | SWAP(unsigned, pInfo->prereqLeft, pInfo->prereqRight); |
| 293 | SWAP(short int, pInfo->idxLeft, pInfo->idxRight); |
| 294 | } |
| 295 | } |
| 296 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 300 | ** This routine decides if pIdx can be used to satisfy the ORDER BY |
| 301 | ** clause. If it can, it returns 1. If pIdx cannot satisfy the |
| 302 | ** ORDER BY clause, this routine returns 0. |
| 303 | ** |
| 304 | ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the |
| 305 | ** left-most table in the FROM clause of that same SELECT statement and |
| 306 | ** the table has a cursor number of "base". pIdx is an index on pTab. |
| 307 | ** |
| 308 | ** nEqCol is the number of columns of pIdx that are used as equality |
| 309 | ** constraints. Any of these columns may be missing from the ORDER BY |
| 310 | ** clause and the match can still be a success. |
| 311 | ** |
| 312 | ** If the index is UNIQUE, then the ORDER BY clause is allowed to have |
| 313 | ** additional terms past the end of the index and the match will still |
| 314 | ** be a success. |
| 315 | ** |
| 316 | ** All terms of the ORDER BY that match against the index must be either |
| 317 | ** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE |
| 318 | ** index do not need to satisfy this constraint.) The *pbRev value is |
| 319 | ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if |
| 320 | ** the ORDER BY clause is all ASC. |
| 321 | */ |
| 322 | static int isSortingIndex( |
| 323 | Parse *pParse, /* Parsing context */ |
| 324 | Index *pIdx, /* The index we are testing */ |
| 325 | Table *pTab, /* The table to be sorted */ |
| 326 | int base, /* Cursor number for pTab */ |
| 327 | ExprList *pOrderBy, /* The ORDER BY clause */ |
| 328 | int nEqCol, /* Number of index columns with == constraints */ |
| 329 | int *pbRev /* Set to 1 if ORDER BY is DESC */ |
| 330 | ){ |
| 331 | int i, j; /* Loop counters */ |
| 332 | int sortOrder; /* Which direction we are sorting */ |
| 333 | int nTerm; /* Number of ORDER BY terms */ |
| 334 | struct ExprList_item *pTerm; /* A term of the ORDER BY clause */ |
| 335 | sqlite3 *db = pParse->db; |
| 336 | |
| 337 | assert( pOrderBy!=0 ); |
| 338 | nTerm = pOrderBy->nExpr; |
| 339 | assert( nTerm>0 ); |
| 340 | |
| 341 | /* Match terms of the ORDER BY clause against columns of |
| 342 | ** the index. |
| 343 | */ |
| 344 | for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<pIdx->nColumn; i++){ |
| 345 | Expr *pExpr; /* The expression of the ORDER BY pTerm */ |
| 346 | CollSeq *pColl; /* The collating sequence of pExpr */ |
| 347 | |
| 348 | pExpr = pTerm->pExpr; |
| 349 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){ |
| 350 | /* Can not use an index sort on anything that is not a column in the |
| 351 | ** left-most table of the FROM clause */ |
| 352 | return 0; |
| 353 | } |
| 354 | pColl = sqlite3ExprCollSeq(pParse, pExpr); |
| 355 | if( !pColl ) pColl = db->pDfltColl; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 356 | if( pExpr->iColumn!=pIdx->aiColumn[i] || pColl!=pIdx->keyInfo.aColl[i] ){ |
| 357 | /* Term j of the ORDER BY clause does not match column i of the index */ |
| 358 | if( i<nEqCol ){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 359 | /* If an index column that is constrained by == fails to match an |
| 360 | ** ORDER BY term, that is OK. Just ignore that column of the index |
| 361 | */ |
| 362 | continue; |
| 363 | }else{ |
| 364 | /* If an index column fails to match and is not constrained by == |
| 365 | ** then the index cannot satisfy the ORDER BY constraint. |
| 366 | */ |
| 367 | return 0; |
| 368 | } |
| 369 | } |
| 370 | if( i>nEqCol ){ |
| 371 | if( pTerm->sortOrder!=sortOrder ){ |
| 372 | /* Indices can only be used if all ORDER BY terms past the |
| 373 | ** equality constraints are all either DESC or ASC. */ |
| 374 | return 0; |
| 375 | } |
| 376 | }else{ |
| 377 | sortOrder = pTerm->sortOrder; |
| 378 | } |
| 379 | j++; |
| 380 | pTerm++; |
| 381 | } |
| 382 | |
| 383 | /* The index can be used for sorting if all terms of the ORDER BY clause |
| 384 | ** or covered or if we ran out of index columns and the it is a UNIQUE |
| 385 | ** index. |
| 386 | */ |
| 387 | if( j>=nTerm || (i>=pIdx->nColumn && pIdx->onError!=OE_None) ){ |
| 388 | *pbRev = sortOrder==SQLITE_SO_DESC; |
| 389 | return 1; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 395 | ** Check table to see if the ORDER BY clause in pOrderBy can be satisfied |
| 396 | ** by sorting in order of ROWID. Return true if so and set *pbRev to be |
| 397 | ** true for reverse ROWID and false for forward ROWID order. |
| 398 | */ |
| 399 | static int sortableByRowid( |
| 400 | int base, /* Cursor number for table to be sorted */ |
| 401 | ExprList *pOrderBy, /* The ORDER BY clause */ |
| 402 | int *pbRev /* Set to 1 if ORDER BY is DESC */ |
| 403 | ){ |
| 404 | Expr *p; |
| 405 | |
| 406 | assert( pOrderBy!=0 ); |
| 407 | assert( pOrderBy->nExpr>0 ); |
| 408 | p = pOrderBy->a[0].pExpr; |
| 409 | if( p->op==TK_COLUMN && p->iTable==base && p->iColumn==-1 ){ |
| 410 | *pbRev = pOrderBy->a[0].sortOrder; |
| 411 | return 1; |
| 412 | } |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 418 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 419 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 420 | ** or USING clause of that join. |
| 421 | ** |
| 422 | ** Consider the term t2.z='ok' in the following queries: |
| 423 | ** |
| 424 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 425 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 426 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 427 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 428 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 429 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 430 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 431 | ** |
| 432 | ** Disabling a term causes that term to not be tested in the inner loop |
| 433 | ** of the join. Disabling is an optimization. We would get the correct |
| 434 | ** results if nothing were ever disabled, but joins might run a little |
| 435 | ** slower. The trick is to disable as much as we can without disabling |
| 436 | ** too much. If we disabled in (1), we'd get the wrong answer. |
| 437 | ** See ticket #813. |
| 438 | */ |
| 439 | static void disableTerm(WhereLevel *pLevel, Expr **ppExpr){ |
| 440 | Expr *pExpr = *ppExpr; |
| 441 | if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 442 | *ppExpr = 0; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 447 | ** Generate code that builds a probe for an index. Details: |
| 448 | ** |
| 449 | ** * Check the top nColumn entries on the stack. If any |
| 450 | ** of those entries are NULL, jump immediately to brk, |
| 451 | ** which is the loop exit, since no index entry will match |
| 452 | ** if any part of the key is NULL. |
| 453 | ** |
| 454 | ** * Construct a probe entry from the top nColumn entries in |
| 455 | ** the stack with affinities appropriate for index pIdx. |
| 456 | */ |
| 457 | static void buildIndexProbe(Vdbe *v, int nColumn, int brk, Index *pIdx){ |
| 458 | sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3); |
| 459 | sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); |
| 460 | sqlite3VdbeAddOp(v, OP_Goto, 0, brk); |
| 461 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 462 | sqlite3IndexAffinityStr(v, pIdx); |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | ** Generate code for an equality term of the WHERE clause. An equality |
| 467 | ** term can be either X=expr or X IN (...). pTerm is the X. |
| 468 | */ |
| 469 | static void codeEqualityTerm( |
| 470 | Parse *pParse, /* The parsing context */ |
| 471 | ExprInfo *pTerm, /* The term of the WHERE clause to be coded */ |
| 472 | int brk, /* Jump here to abandon the loop */ |
| 473 | WhereLevel *pLevel /* When level of the FROM clause we are working on */ |
| 474 | ){ |
| 475 | Expr *pX = pTerm->p; |
| 476 | if( pX->op!=TK_IN ){ |
| 477 | assert( pX->op==TK_EQ ); |
| 478 | sqlite3ExprCode(pParse, pX->pRight); |
| 479 | }else{ |
| 480 | int iTab = pX->iTable; |
| 481 | Vdbe *v = pParse->pVdbe; |
| 482 | sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk); |
| 483 | sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 484 | pLevel->inP2 = sqlite3VdbeAddOp(v, OP_Column, iTab, 0); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 485 | pLevel->inOp = OP_Next; |
| 486 | pLevel->inP1 = iTab; |
| 487 | } |
| 488 | disableTerm(pLevel, &pTerm->p); |
| 489 | } |
| 490 | |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 491 | /* |
| 492 | ** The number of bits in a Bitmask |
| 493 | */ |
| 494 | #define BMS (sizeof(Bitmask)*8-1) |
| 495 | |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 496 | |
| 497 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 498 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 499 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 500 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 501 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 502 | ** in order to complete the WHERE clause processing. |
| 503 | ** |
| 504 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 505 | ** |
| 506 | ** The basic idea is to do a nested loop, one loop for each table in |
| 507 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 508 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 509 | ** example, if the SQL is this: |
| 510 | ** |
| 511 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 512 | ** |
| 513 | ** Then the code generated is conceptually like the following: |
| 514 | ** |
| 515 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 516 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 517 | ** foreach row3 in t3 do / |
| 518 | ** ... |
| 519 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 520 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 521 | ** end / |
| 522 | ** |
| 523 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 524 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 525 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 526 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 527 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 528 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 529 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | 7465a80 | 2005-01-03 01:28:51 +0000 | [diff] [blame] | 530 | ** can use OP_Column and OP_Recno opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 531 | ** data from the various tables of the loop. |
| 532 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 533 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 534 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 535 | ** the tables have indices and there are terms in the WHERE clause that |
| 536 | ** refer to those indices, a complete table scan can be avoided and the |
| 537 | ** code will run much faster. Most of the work of this routine is checking |
| 538 | ** to see if there are indices that can be used to speed up the loop. |
| 539 | ** |
| 540 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 541 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 542 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 543 | ** loops are evaluated and if false a jump is made around all subsequent |
| 544 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 545 | ** most loop) |
| 546 | ** |
| 547 | ** OUTER JOINS |
| 548 | ** |
| 549 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 550 | ** |
| 551 | ** foreach row1 in t1 do |
| 552 | ** flag = 0 |
| 553 | ** foreach row2 in t2 do |
| 554 | ** start: |
| 555 | ** ... |
| 556 | ** flag = 1 |
| 557 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 558 | ** if flag==0 then |
| 559 | ** move the row2 cursor to a null row |
| 560 | ** goto start |
| 561 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 562 | ** end |
| 563 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 564 | ** ORDER BY CLAUSE PROCESSING |
| 565 | ** |
| 566 | ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 567 | ** if there is one. If there is no ORDER BY clause or if this routine |
| 568 | ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL. |
| 569 | ** |
| 570 | ** If an index can be used so that the natural output order of the table |
| 571 | ** scan is correct for the ORDER BY clause, then that index is used and |
| 572 | ** *ppOrderBy is set to NULL. This is an optimization that prevents an |
| 573 | ** unnecessary sort of the result set if an index appropriate for the |
| 574 | ** ORDER BY clause already exists. |
| 575 | ** |
| 576 | ** If the where clause loops cannot be arranged to provide the correct |
| 577 | ** output order, then the *ppOrderBy is unchanged. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 578 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 579 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 580 | Parse *pParse, /* The parser context */ |
| 581 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 582 | Expr *pWhere, /* The WHERE clause */ |
drh | e4e7207 | 2004-11-23 01:47:30 +0000 | [diff] [blame] | 583 | ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */ |
| 584 | Fetch *pFetch /* Initial location of cursors. NULL otherwise */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 585 | ){ |
| 586 | int i; /* Loop counter */ |
| 587 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 588 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | d4f5ee2 | 2003-07-16 00:54:31 +0000 | [diff] [blame] | 589 | int brk, cont = 0; /* Addresses used during code generation */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 590 | int nExpr; /* Number of subexpressions in the WHERE clause */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 591 | Bitmask loopMask; /* One bit set for each outer loop */ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 592 | ExprInfo *pTerm; /* A single term in the WHERE clause; ptr to aExpr[] */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 593 | ExprMaskSet maskSet; /* The expression mask set */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 594 | int iDirectEq[BMS]; /* Term of the form ROWID==X for the N-th table */ |
| 595 | int iDirectLt[BMS]; /* Term of the form ROWID<X or ROWID<=X */ |
| 596 | int iDirectGt[BMS]; /* Term of the form ROWID>X or ROWID>=X */ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 597 | ExprInfo aExpr[101]; /* The WHERE clause is divided into these terms */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 598 | struct SrcList_item *pTabItem; /* A single entry from pTabList */ |
| 599 | WhereLevel *pLevel; /* A single level in the pWInfo list */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 600 | |
drh | 83dcb1a | 2002-06-28 01:02:38 +0000 | [diff] [blame] | 601 | /* Split the WHERE clause into separate subexpressions where each |
| 602 | ** subexpression is separated by an AND operator. If the aExpr[] |
| 603 | ** array fills up, the last entry might point to an expression which |
| 604 | ** contains additional unfactored AND operators. |
| 605 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 606 | initMaskSet(&maskSet); |
drh | 83dcb1a | 2002-06-28 01:02:38 +0000 | [diff] [blame] | 607 | memset(aExpr, 0, sizeof(aExpr)); |
| 608 | nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere); |
| 609 | if( nExpr==ARRAYSIZE(aExpr) ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 610 | sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more " |
drh | f7a9e1a | 2004-02-22 18:40:56 +0000 | [diff] [blame] | 611 | "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1); |
drh | 83dcb1a | 2002-06-28 01:02:38 +0000 | [diff] [blame] | 612 | return 0; |
| 613 | } |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 614 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 615 | /* Allocate and initialize the WhereInfo structure that will become the |
| 616 | ** return value. |
| 617 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 618 | pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel)); |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 619 | if( sqlite3_malloc_failed ){ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 620 | /* sqliteFree(pWInfo); // Leak memory when malloc fails */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | pWInfo->pParse = pParse; |
| 624 | pWInfo->pTabList = pTabList; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 625 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 626 | |
| 627 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 628 | ** expression and either jump over all of the code or fall thru. |
| 629 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 630 | if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){ |
| 631 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 632 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 633 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 634 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 635 | /* Analyze all of the subexpressions. |
| 636 | */ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 637 | for(pTerm=aExpr, i=0; i<nExpr; i++, pTerm++){ |
| 638 | TriggerStack *pStack; |
| 639 | exprAnalyze(pTabList, &maskSet, pTerm); |
drh | 1d1f305 | 2002-05-21 13:18:25 +0000 | [diff] [blame] | 640 | |
| 641 | /* If we are executing a trigger body, remove all references to |
| 642 | ** new.* and old.* tables from the prerequisite masks. |
| 643 | */ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 644 | if( (pStack = pParse->trigStack)!=0 ){ |
drh | 1d1f305 | 2002-05-21 13:18:25 +0000 | [diff] [blame] | 645 | int x; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 646 | if( (x=pStack->newIdx) >= 0 ){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 647 | Bitmask mask = ~getMask(&maskSet, x); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 648 | pTerm->prereqRight &= mask; |
| 649 | pTerm->prereqLeft &= mask; |
| 650 | pTerm->prereqAll &= mask; |
drh | 1d1f305 | 2002-05-21 13:18:25 +0000 | [diff] [blame] | 651 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 652 | if( (x=pStack->oldIdx) >= 0 ){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 653 | Bitmask mask = ~getMask(&maskSet, x); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 654 | pTerm->prereqRight &= mask; |
| 655 | pTerm->prereqLeft &= mask; |
| 656 | pTerm->prereqAll &= mask; |
drh | 1d1f305 | 2002-05-21 13:18:25 +0000 | [diff] [blame] | 657 | } |
danielk1977 | c3f9bad | 2002-05-15 08:30:12 +0000 | [diff] [blame] | 658 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 659 | } |
| 660 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 661 | /* Figure out what index to use (if any) for each nested loop. |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 662 | ** 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] | 663 | ** 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] | 664 | ** loop. |
| 665 | ** |
| 666 | ** If terms exist that use the ROWID of any table, then set the |
| 667 | ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table |
| 668 | ** to the index of the term containing the ROWID. We always prefer |
| 669 | ** to use a ROWID which can directly access a table rather than an |
drh | 0a36c57 | 2002-02-18 22:49:59 +0000 | [diff] [blame] | 670 | ** index which requires reading an index first to get the rowid then |
| 671 | ** doing a second read of the actual database table. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 672 | ** |
| 673 | ** Actually, if there are more than 32 tables in the join, only the |
drh | 0a36c57 | 2002-02-18 22:49:59 +0000 | [diff] [blame] | 674 | ** first 32 tables are candidates for indices. This is (again) due |
| 675 | ** to the limit of 32 bits in an integer bitmask. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 676 | */ |
| 677 | loopMask = 0; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 678 | pTabItem = pTabList->a; |
| 679 | pLevel = pWInfo->a; |
| 680 | for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++,pTabItem++,pLevel++){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 681 | int j; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 682 | int iCur = pTabItem->iCursor; /* The cursor for this table */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 683 | Bitmask mask = getMask(&maskSet, iCur); /* Cursor mask for this table */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 684 | Table *pTab = pTabItem->pTab; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 685 | Index *pIdx; |
| 686 | Index *pBestIdx = 0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 687 | int bestScore = 0; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 688 | int bestRev = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 689 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 690 | /* Check to see if there is an expression that uses only the |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 691 | ** ROWID field of this table. For terms of the form ROWID==expr |
| 692 | ** set iDirectEq[i] to the index of the term. For terms of the |
| 693 | ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index. |
| 694 | ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i]. |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 695 | ** |
| 696 | ** (Added:) Treat ROWID IN expr like ROWID=expr. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 697 | */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 698 | pLevel->iIdxCur = -1; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 699 | iDirectEq[i] = -1; |
| 700 | iDirectLt[i] = -1; |
| 701 | iDirectGt[i] = -1; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 702 | for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){ |
| 703 | Expr *pX = pTerm->p; |
| 704 | if( pTerm->idxLeft==iCur && pX->pLeft->iColumn<0 |
| 705 | && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){ |
| 706 | switch( pX->op ){ |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 707 | case TK_IN: |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 708 | case TK_EQ: iDirectEq[i] = j; break; |
| 709 | case TK_LE: |
| 710 | case TK_LT: iDirectLt[i] = j; break; |
| 711 | case TK_GE: |
| 712 | case TK_GT: iDirectGt[i] = j; break; |
| 713 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 714 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 715 | } |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 716 | |
| 717 | /* If we found a term that tests ROWID with == or IN, that term |
| 718 | ** will be used to locate the rows in the database table. There |
| 719 | ** is not need to continue into the code below that looks for |
| 720 | ** an index. We will always use the ROWID over an index. |
| 721 | */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 722 | if( iDirectEq[i]>=0 ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 723 | loopMask |= mask; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 724 | pLevel->pIdx = 0; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 725 | continue; |
| 726 | } |
| 727 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 728 | /* Do a search for usable indices. Leave pBestIdx pointing to |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 729 | ** the "best" index. pBestIdx is left set to NULL if no indices |
| 730 | ** are usable. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 731 | ** |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 732 | ** The best index is the one with the highest score. The score |
| 733 | ** for the index is determined as follows. For each of the |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 734 | ** left-most terms that is fixed by an equality operator, add |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 735 | ** 32 to the score. The right-most term of the index may be |
| 736 | ** constrained by an inequality. Add 4 if for an "x<..." constraint |
| 737 | ** and add 8 for an "x>..." constraint. If both constraints |
| 738 | ** are present, add 12. |
| 739 | ** |
| 740 | ** If the left-most term of the index uses an IN operator |
| 741 | ** (ex: "x IN (...)") then add 16 to the score. |
| 742 | ** |
| 743 | ** If an index can be used for sorting, add 2 to the score. |
| 744 | ** If an index contains all the terms of a table that are ever |
| 745 | ** used by any expression in the SQL statement, then add 1 to |
| 746 | ** the score. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 747 | ** |
| 748 | ** This scoring system is designed so that the score can later be |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 749 | ** used to determine how the index is used. If the score&0x1c is 0 |
| 750 | ** then all constraints are equalities. If score&0x4 is not 0 then |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 751 | ** there is an inequality used as a termination key. (ex: "x<...") |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 752 | ** If score&0x8 is not 0 then there is an inequality used as the |
| 753 | ** start key. (ex: "x>..."). A score or 0x10 is the special case |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 754 | ** of an IN operator constraint. (ex: "x IN ..."). |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 755 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 756 | ** The IN operator (as in "<expr> IN (...)") is treated the same as |
| 757 | ** an equality comparison except that it can only be used on the |
| 758 | ** left-most column of an index and other terms of the WHERE clause |
| 759 | ** cannot be used in conjunction with the IN operator to help satisfy |
| 760 | ** other columns of the index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 761 | */ |
| 762 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 763 | Bitmask eqMask = 0; /* Index columns covered by an x=... term */ |
| 764 | Bitmask ltMask = 0; /* Index columns covered by an x<... term */ |
| 765 | Bitmask gtMask = 0; /* Index columns covered by an x>... term */ |
| 766 | Bitmask inMask = 0; /* Index columns covered by an x IN .. term */ |
| 767 | Bitmask m; |
| 768 | int nEq, score, bRev = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 769 | |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 770 | if( pIdx->nColumn>sizeof(eqMask)*8 ){ |
| 771 | continue; /* Ignore indices with too many columns to analyze */ |
| 772 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 773 | for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 774 | Expr *pX = pTerm->p; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 775 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pX->pLeft); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 776 | if( !pColl && pX->pRight ){ |
| 777 | pColl = sqlite3ExprCollSeq(pParse, pX->pRight); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 778 | } |
| 779 | if( !pColl ){ |
| 780 | pColl = pParse->db->pDfltColl; |
| 781 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 782 | if( pTerm->idxLeft==iCur |
| 783 | && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){ |
| 784 | int iColumn = pX->pLeft->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 785 | int k; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 786 | char idxaff = pIdx->pTable->aCol[iColumn].affinity; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 787 | for(k=0; k<pIdx->nColumn; k++){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 788 | /* If the collating sequences or affinities don't match, |
| 789 | ** ignore this index. */ |
| 790 | if( pColl!=pIdx->keyInfo.aColl[k] ) continue; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 791 | if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 792 | if( pIdx->aiColumn[k]==iColumn ){ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 793 | switch( pX->op ){ |
drh | 48185c1 | 2002-06-09 01:55:20 +0000 | [diff] [blame] | 794 | case TK_IN: { |
| 795 | if( k==0 ) inMask |= 1; |
| 796 | break; |
| 797 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 798 | case TK_EQ: { |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 799 | eqMask |= ((Bitmask)1)<<k; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | case TK_LE: |
| 803 | case TK_LT: { |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 804 | ltMask |= ((Bitmask)1)<<k; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 805 | break; |
| 806 | } |
| 807 | case TK_GE: |
| 808 | case TK_GT: { |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 809 | gtMask |= ((Bitmask)1)<<k; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 810 | break; |
| 811 | } |
| 812 | default: { |
| 813 | /* CANT_HAPPEN */ |
| 814 | assert( 0 ); |
| 815 | break; |
| 816 | } |
| 817 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | } |
| 821 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 822 | } |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 823 | |
| 824 | /* The following loop ends with nEq set to the number of columns |
| 825 | ** on the left of the index with == constraints. |
| 826 | */ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 827 | for(nEq=0; nEq<pIdx->nColumn; nEq++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 828 | m = (((Bitmask)1)<<(nEq+1))-1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 829 | if( (m & eqMask)!=m ) break; |
| 830 | } |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 831 | |
| 832 | /* Begin assemblying the score |
| 833 | */ |
| 834 | score = nEq*32; /* Base score is 32 times number of == constraints */ |
| 835 | m = ((Bitmask)1)<<nEq; |
| 836 | if( m & ltMask ) score+=4; /* Increase score for a < constraint */ |
| 837 | if( m & gtMask ) score+=8; /* Increase score for a > constraint */ |
| 838 | if( score==0 && inMask ) score = 16; /* Default score for IN constraint */ |
| 839 | |
| 840 | /* Give bonus points if this index can be used for sorting |
| 841 | */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 842 | if( i==0 && score!=16 && ppOrderBy && *ppOrderBy ){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 843 | int base = pTabList->a[0].iCursor; |
| 844 | if( isSortingIndex(pParse, pIdx, pTab, base, *ppOrderBy, nEq, &bRev) ){ |
| 845 | score += 2; |
| 846 | } |
| 847 | } |
| 848 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 849 | /* Check to see if we can get away with using just the index without |
| 850 | ** ever reading the table. If that is the case, then add one bonus |
| 851 | ** point to the score. |
| 852 | */ |
| 853 | if( score && pTabItem->colUsed < (((Bitmask)1)<<(BMS-1)) ){ |
| 854 | for(m=0, j=0; j<pIdx->nColumn; j++){ |
| 855 | int x = pIdx->aiColumn[j]; |
| 856 | if( x<BMS-1 ){ |
| 857 | m |= ((Bitmask)1)<<x; |
| 858 | } |
| 859 | } |
| 860 | if( (pTabItem->colUsed & m)==pTabItem->colUsed ){ |
| 861 | score++; |
| 862 | } |
| 863 | } |
| 864 | |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 865 | /* If the score for this index is the best we have seen so far, then |
| 866 | ** save it |
| 867 | */ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 868 | if( score>bestScore ){ |
| 869 | pBestIdx = pIdx; |
| 870 | bestScore = score; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 871 | bestRev = bRev; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 874 | pLevel->pIdx = pBestIdx; |
| 875 | pLevel->score = bestScore; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 876 | pLevel->bRev = bestRev; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 877 | loopMask |= mask; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 878 | if( pBestIdx ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 879 | pLevel->iIdxCur = pParse->nTab++; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 880 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 881 | } |
| 882 | |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 883 | /* Check to see if the ORDER BY clause is or can be satisfied by the |
| 884 | ** use of an index on the first table. |
| 885 | */ |
| 886 | if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 887 | Index *pIdx; /* Index derived from the WHERE clause */ |
| 888 | Table *pTab; /* Left-most table in the FROM clause */ |
| 889 | int bRev = 0; /* True to reverse the output order */ |
| 890 | int iCur; /* Btree-cursor that will be used by pTab */ |
| 891 | WhereLevel *pLevel0 = &pWInfo->a[0]; |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 892 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 893 | pTab = pTabList->a[0].pTab; |
| 894 | pIdx = pLevel0->pIdx; |
| 895 | iCur = pTabList->a[0].iCursor; |
| 896 | if( pIdx==0 && sortableByRowid(iCur, *ppOrderBy, &bRev) ){ |
| 897 | /* The ORDER BY clause specifies ROWID order, which is what we |
| 898 | ** were going to be doing anyway... |
| 899 | */ |
| 900 | *ppOrderBy = 0; |
| 901 | pLevel0->bRev = bRev; |
| 902 | }else if( pLevel0->score==16 ){ |
| 903 | /* If there is already an IN index on the left-most table, |
| 904 | ** it will not give the correct sort order. |
| 905 | ** So, pretend that no suitable index is found. |
| 906 | */ |
| 907 | }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){ |
| 908 | /* If the left-most column is accessed using its ROWID, then do |
| 909 | ** not try to sort by index. But do delete the ORDER BY clause |
| 910 | ** if it is redundant. |
| 911 | */ |
| 912 | }else if( (pLevel0->score&2)!=0 ){ |
| 913 | /* The index that was selected for searching will cause rows to |
| 914 | ** appear in sorted order. |
| 915 | */ |
| 916 | *ppOrderBy = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 920 | /* Open all tables in the pTabList and any indices selected for |
| 921 | ** searching those tables. |
| 922 | */ |
| 923 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 924 | pLevel = pWInfo->a; |
| 925 | for(i=0, pTabItem=pTabList->a; i<pTabList->nSrc; i++, pTabItem++, pLevel++){ |
| 926 | Table *pTab; |
| 927 | Index *pIx; |
| 928 | int iIdxCur = pLevel->iIdxCur; |
| 929 | |
| 930 | pTab = pTabItem->pTab; |
| 931 | if( pTab->isTransient || pTab->pSelect ) continue; |
| 932 | if( (pLevel->score & 1)==0 ){ |
| 933 | sqlite3OpenTableForReading(v, pTabItem->iCursor, pTab); |
| 934 | } |
| 935 | pLevel->iTabCur = pTabItem->iCursor; |
| 936 | if( (pIx = pLevel->pIdx)!=0 ){ |
| 937 | sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0); |
| 938 | sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIx->tnum, |
| 939 | (char*)&pIx->keyInfo, P3_KEYINFO); |
| 940 | } |
| 941 | if( (pLevel->score & 1)!=0 ){ |
| 942 | sqlite3VdbeAddOp(v, OP_KeyAsData, iIdxCur, 1); |
| 943 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, pIx->nColumn+1); |
| 944 | } |
| 945 | sqlite3CodeVerifySchema(pParse, pTab->iDb); |
| 946 | } |
| 947 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 948 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 949 | /* Generate the code to do the search |
| 950 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 951 | loopMask = 0; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 952 | pLevel = pWInfo->a; |
| 953 | pTabItem = pTabList->a; |
| 954 | for(i=0; i<pTabList->nSrc; i++, pTabItem++, pLevel++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 955 | int j, k; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 956 | int iCur = pTabItem->iCursor; /* The VDBE cursor for the table */ |
| 957 | Index *pIdx; /* The index we will be using */ |
| 958 | int iIdxCur; /* The VDBE cursor for the index */ |
| 959 | int omitTable; /* True if we use the index only */ |
| 960 | |
| 961 | pIdx = pLevel->pIdx; |
| 962 | iIdxCur = pLevel->iIdxCur; |
| 963 | pLevel->inOp = OP_Noop; |
| 964 | |
| 965 | /* Check to see if it is appropriate to omit the use of the table |
| 966 | ** here and use its index instead. |
| 967 | */ |
| 968 | omitTable = (pLevel->score&1)!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 969 | |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 970 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 971 | ** initialize a memory cell that records if this table matches any |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 972 | ** row of the left table of the join. |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 973 | */ |
| 974 | if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){ |
| 975 | if( !pParse->nMem ) pParse->nMem++; |
| 976 | pLevel->iLeftJoin = pParse->nMem++; |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 977 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 978 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 979 | VdbeComment((v, "# init LEFT JOIN no-match flag")); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 980 | } |
| 981 | |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 982 | if( i<ARRAYSIZE(iDirectEq) && (k = iDirectEq[i])>=0 ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 983 | /* Case 1: We can directly reference a single row using an |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 984 | ** equality comparison against the ROWID field. Or |
| 985 | ** we reference multiple rows using a "rowid IN (...)" |
| 986 | ** construct. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 987 | */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 988 | assert( k<nExpr ); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 989 | pTerm = &aExpr[k]; |
| 990 | assert( pTerm->p!=0 ); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 991 | assert( pTerm->idxLeft==iCur ); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 992 | assert( omitTable==0 ); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 993 | brk = pLevel->brk = sqlite3VdbeMakeLabel(v); |
| 994 | codeEqualityTerm(pParse, pTerm, brk, pLevel); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 995 | cont = pLevel->cont = sqlite3VdbeMakeLabel(v); |
| 996 | sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 997 | sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk); |
tpoindex | 7a9b161 | 2005-01-03 18:13:18 +0000 | [diff] [blame] | 998 | VdbeComment((v, "pk")); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 999 | pLevel->op = OP_Noop; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1000 | }else if( pIdx!=0 && pLevel->score>3 && (pLevel->score&0x0c)==0 ){ |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1001 | /* Case 2: There is an index and all terms of the WHERE clause that |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1002 | ** refer to the index using the "==" or "IN" operators. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1003 | */ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1004 | int start; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1005 | int nColumn = (pLevel->score+16)/32; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1006 | brk = pLevel->brk = sqlite3VdbeMakeLabel(v); |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 1007 | |
| 1008 | /* For each column of the index, find the term of the WHERE clause that |
| 1009 | ** constraints that column. If the WHERE clause term is X=expr, then |
| 1010 | ** evaluation expr and leave the result on the stack */ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1011 | for(j=0; j<nColumn; j++){ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1012 | for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){ |
| 1013 | Expr *pX = pTerm->p; |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1014 | if( pX==0 ) continue; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1015 | if( pTerm->idxLeft==iCur |
| 1016 | && (pTerm->prereqRight & loopMask)==pTerm->prereqRight |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1017 | && pX->pLeft->iColumn==pIdx->aiColumn[j] |
drh | ac931eb | 2005-01-11 18:13:56 +0000 | [diff] [blame] | 1018 | && (pX->op==TK_EQ || pX->op==TK_IN) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1019 | ){ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1020 | char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1021 | if( sqlite3IndexAffinityOk(pX, idxaff) ){ |
| 1022 | codeEqualityTerm(pParse, pTerm, brk, pLevel); |
| 1023 | break; |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1024 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1025 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1028 | pLevel->iMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1029 | cont = pLevel->cont = sqlite3VdbeMakeLabel(v); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1030 | buildIndexProbe(v, nColumn, brk, pIdx); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1031 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 1032 | |
drh | 772ae62 | 2004-05-19 13:13:08 +0000 | [diff] [blame] | 1033 | /* Generate code (1) to move to the first matching element of the table. |
| 1034 | ** Then generate code (2) that jumps to "brk" after the cursor is past |
| 1035 | ** the last matching element of the table. The code (1) is executed |
| 1036 | ** once to initialize the search, the code (2) is executed before each |
| 1037 | ** iteration of the scan to see if the scan has finished. */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1038 | if( pLevel->bRev ){ |
| 1039 | /* Scan in reverse order */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1040 | sqlite3VdbeAddOp(v, OP_MoveLe, iIdxCur, brk); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1041 | start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1042 | sqlite3VdbeAddOp(v, OP_IdxLT, iIdxCur, brk); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1043 | pLevel->op = OP_Prev; |
| 1044 | }else{ |
| 1045 | /* Scan in the forward order */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1046 | sqlite3VdbeAddOp(v, OP_MoveGe, iIdxCur, brk); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1047 | start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1048 | sqlite3VdbeOp3(v, OP_IdxGE, iIdxCur, brk, "+", P3_STATIC); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1049 | pLevel->op = OP_Next; |
| 1050 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1051 | sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1052 | sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont); |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1053 | if( !omitTable ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1054 | sqlite3VdbeAddOp(v, OP_IdxRecno, iIdxCur, 0); |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1055 | sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1056 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1057 | pLevel->p1 = iIdxCur; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1058 | pLevel->p2 = start; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1059 | }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){ |
| 1060 | /* Case 3: We have an inequality comparison against the ROWID field. |
| 1061 | */ |
| 1062 | int testOp = OP_Noop; |
| 1063 | int start; |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1064 | int bRev = pLevel->bRev; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1065 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1066 | assert( omitTable==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1067 | brk = pLevel->brk = sqlite3VdbeMakeLabel(v); |
| 1068 | cont = pLevel->cont = sqlite3VdbeMakeLabel(v); |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1069 | if( bRev ){ |
| 1070 | int t = iDirectGt[i]; |
| 1071 | iDirectGt[i] = iDirectLt[i]; |
| 1072 | iDirectLt[i] = t; |
| 1073 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1074 | if( iDirectGt[i]>=0 ){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1075 | Expr *pX; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1076 | k = iDirectGt[i]; |
| 1077 | assert( k<nExpr ); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1078 | pTerm = &aExpr[k]; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1079 | pX = pTerm->p; |
| 1080 | assert( pX!=0 ); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1081 | assert( pTerm->idxLeft==iCur ); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1082 | sqlite3ExprCode(pParse, pX->pRight); |
| 1083 | sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LT || pX->op==TK_GT, brk); |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1084 | sqlite3VdbeAddOp(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk); |
tpoindex | 7a9b161 | 2005-01-03 18:13:18 +0000 | [diff] [blame] | 1085 | VdbeComment((v, "pk")); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1086 | disableTerm(pLevel, &pTerm->p); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1087 | }else{ |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1088 | sqlite3VdbeAddOp(v, bRev ? OP_Last : OP_Rewind, iCur, brk); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1089 | } |
| 1090 | if( iDirectLt[i]>=0 ){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1091 | Expr *pX; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1092 | k = iDirectLt[i]; |
| 1093 | assert( k<nExpr ); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1094 | pTerm = &aExpr[k]; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1095 | pX = pTerm->p; |
| 1096 | assert( pX!=0 ); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1097 | assert( pTerm->idxLeft==iCur ); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1098 | sqlite3ExprCode(pParse, pX->pRight); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1099 | pLevel->iMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1100 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1101 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1102 | testOp = bRev ? OP_Le : OP_Ge; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1103 | }else{ |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1104 | testOp = bRev ? OP_Lt : OP_Gt; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1105 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1106 | disableTerm(pLevel, &pTerm->p); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1107 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1108 | start = sqlite3VdbeCurrentAddr(v); |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1109 | pLevel->op = bRev ? OP_Prev : OP_Next; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1110 | pLevel->p1 = iCur; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1111 | pLevel->p2 = start; |
| 1112 | if( testOp!=OP_Noop ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1113 | sqlite3VdbeAddOp(v, OP_Recno, iCur, 0); |
| 1114 | sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 1115 | sqlite3VdbeAddOp(v, testOp, 0, brk); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1116 | } |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1117 | }else if( pIdx==0 ){ |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1118 | /* Case 4: There is no usable index. We must do a complete |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1119 | ** scan of the entire database table. |
| 1120 | */ |
| 1121 | int start; |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1122 | int opRewind; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1123 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1124 | assert( omitTable==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1125 | brk = pLevel->brk = sqlite3VdbeMakeLabel(v); |
| 1126 | cont = pLevel->cont = sqlite3VdbeMakeLabel(v); |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1127 | if( pLevel->bRev ){ |
| 1128 | opRewind = OP_Last; |
| 1129 | pLevel->op = OP_Prev; |
| 1130 | }else{ |
| 1131 | opRewind = OP_Rewind; |
| 1132 | pLevel->op = OP_Next; |
| 1133 | } |
| 1134 | sqlite3VdbeAddOp(v, opRewind, iCur, brk); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1135 | start = sqlite3VdbeCurrentAddr(v); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1136 | pLevel->p1 = iCur; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1137 | pLevel->p2 = start; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1138 | }else{ |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1139 | /* Case 5: The WHERE clause term that refers to the right-most |
| 1140 | ** column of the index is an inequality. For example, if |
| 1141 | ** the index is on (x,y,z) and the WHERE clause is of the |
| 1142 | ** form "x=5 AND y<10" then this case is used. Only the |
| 1143 | ** right-most column can be an inequality - the rest must |
| 1144 | ** use the "==" operator. |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 1145 | ** |
| 1146 | ** This case is also used when there are no WHERE clause |
| 1147 | ** constraints but an index is selected anyway, in order |
| 1148 | ** to force the output order to conform to an ORDER BY. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1149 | */ |
| 1150 | int score = pLevel->score; |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1151 | int nEqColumn = score/32; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1152 | int start; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 1153 | int leFlag=0, geFlag=0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1154 | int testOp; |
| 1155 | |
| 1156 | /* Evaluate the equality constraints |
| 1157 | */ |
| 1158 | for(j=0; j<nEqColumn; j++){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1159 | int iIdxCol = pIdx->aiColumn[j]; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1160 | for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1161 | Expr *pX = pTerm->p; |
| 1162 | if( pX==0 ) continue; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1163 | if( pTerm->idxLeft==iCur |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1164 | && pX->op==TK_EQ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1165 | && (pTerm->prereqRight & loopMask)==pTerm->prereqRight |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1166 | && pX->pLeft->iColumn==iIdxCol |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1167 | ){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1168 | sqlite3ExprCode(pParse, pX->pRight); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1169 | disableTerm(pLevel, &pTerm->p); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1170 | break; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1175 | /* Duplicate the equality term values because they will all be |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1176 | ** used twice: once to make the termination key and once to make the |
| 1177 | ** start key. |
| 1178 | */ |
| 1179 | for(j=0; j<nEqColumn; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1180 | sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1183 | /* Labels for the beginning and end of the loop |
| 1184 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1185 | cont = pLevel->cont = sqlite3VdbeMakeLabel(v); |
| 1186 | brk = pLevel->brk = sqlite3VdbeMakeLabel(v); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1187 | |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1188 | /* Generate the termination key. This is the key value that |
| 1189 | ** will end the search. There is no termination key if there |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1190 | ** are no equality terms and no "X<..." term. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1191 | ** |
| 1192 | ** 2002-Dec-04: On a reverse-order scan, the so-called "termination" |
| 1193 | ** key computed here really ends up being the start key. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1194 | */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1195 | if( (score & 4)!=0 ){ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1196 | for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1197 | Expr *pX = pTerm->p; |
| 1198 | if( pX==0 ) continue; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1199 | if( pTerm->idxLeft==iCur |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1200 | && (pX->op==TK_LT || pX->op==TK_LE) |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1201 | && (pTerm->prereqRight & loopMask)==pTerm->prereqRight |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1202 | && pX->pLeft->iColumn==pIdx->aiColumn[j] |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1203 | ){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1204 | sqlite3ExprCode(pParse, pX->pRight); |
| 1205 | leFlag = pX->op==TK_LE; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1206 | disableTerm(pLevel, &pTerm->p); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | } |
| 1209 | } |
| 1210 | testOp = OP_IdxGE; |
| 1211 | }else{ |
| 1212 | testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop; |
| 1213 | leFlag = 1; |
| 1214 | } |
| 1215 | if( testOp!=OP_Noop ){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1216 | int nCol = nEqColumn + ((score & 4)!=0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1217 | pLevel->iMem = pParse->nMem++; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1218 | buildIndexProbe(v, nCol, brk, pIdx); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1219 | if( pLevel->bRev ){ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1220 | int op = leFlag ? OP_MoveLe : OP_MoveLt; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1221 | sqlite3VdbeAddOp(v, op, iIdxCur, brk); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1222 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1223 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1224 | } |
| 1225 | }else if( pLevel->bRev ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1226 | sqlite3VdbeAddOp(v, OP_Last, iIdxCur, brk); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /* Generate the start key. This is the key that defines the lower |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1230 | ** bound on the search. There is no start key if there are no |
| 1231 | ** equality terms and if there is no "X>..." term. In |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1232 | ** that case, generate a "Rewind" instruction in place of the |
| 1233 | ** start key search. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1234 | ** |
| 1235 | ** 2002-Dec-04: In the case of a reverse-order search, the so-called |
| 1236 | ** "start" key really ends up being used as the termination key. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1237 | */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1238 | if( (score & 8)!=0 ){ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1239 | for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1240 | Expr *pX = pTerm->p; |
| 1241 | if( pX==0 ) continue; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1242 | if( pTerm->idxLeft==iCur |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1243 | && (pX->op==TK_GT || pX->op==TK_GE) |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1244 | && (pTerm->prereqRight & loopMask)==pTerm->prereqRight |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1245 | && pX->pLeft->iColumn==pIdx->aiColumn[j] |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1246 | ){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1247 | sqlite3ExprCode(pParse, pX->pRight); |
| 1248 | geFlag = pX->op==TK_GE; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1249 | disableTerm(pLevel, &pTerm->p); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1250 | break; |
| 1251 | } |
| 1252 | } |
drh | 7900ead | 2001-11-12 13:51:43 +0000 | [diff] [blame] | 1253 | }else{ |
| 1254 | geFlag = 1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1255 | } |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1256 | if( nEqColumn>0 || (score&8)!=0 ){ |
| 1257 | int nCol = nEqColumn + ((score&8)!=0); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1258 | buildIndexProbe(v, nCol, brk, pIdx); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1259 | if( pLevel->bRev ){ |
| 1260 | pLevel->iMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1261 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1262 | testOp = OP_IdxLT; |
| 1263 | }else{ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1264 | int op = geFlag ? OP_MoveGe : OP_MoveGt; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1265 | sqlite3VdbeAddOp(v, op, iIdxCur, brk); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1266 | } |
| 1267 | }else if( pLevel->bRev ){ |
| 1268 | testOp = OP_Noop; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1269 | }else{ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1270 | sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, brk); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | /* Generate the the top of the loop. If there is a termination |
| 1274 | ** key we have to test for that key and abort at the top of the |
| 1275 | ** loop. |
| 1276 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1277 | start = sqlite3VdbeCurrentAddr(v); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1278 | if( testOp!=OP_Noop ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1279 | sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1280 | sqlite3VdbeAddOp(v, testOp, iIdxCur, brk); |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1281 | if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){ |
| 1282 | sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC); |
| 1283 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1284 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1285 | sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 1286 | sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + ((score&4)!=0), cont); |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1287 | if( !omitTable ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1288 | sqlite3VdbeAddOp(v, OP_IdxRecno, iIdxCur, 0); |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1289 | sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /* Record the instruction used to terminate the loop. |
| 1293 | */ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1294 | pLevel->op = pLevel->bRev ? OP_Prev : OP_Next; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1295 | pLevel->p1 = iIdxCur; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1296 | pLevel->p2 = start; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1297 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1298 | loopMask |= getMask(&maskSet, iCur); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1299 | |
| 1300 | /* Insert code to test every subexpression that can be completely |
| 1301 | ** computed using the current set of tables. |
| 1302 | */ |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1303 | for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){ |
| 1304 | if( pTerm->p==0 ) continue; |
| 1305 | if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue; |
| 1306 | if( pLevel->iLeftJoin && !ExprHasProperty(pTerm->p,EP_FromJoin) ){ |
drh | 1f16230 | 2002-10-27 19:35:33 +0000 | [diff] [blame] | 1307 | continue; |
| 1308 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1309 | sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1); |
| 1310 | pTerm->p = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1311 | } |
| 1312 | brk = cont; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1313 | |
| 1314 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 1315 | ** at least one row of the right table has matched the left table. |
| 1316 | */ |
| 1317 | if( pLevel->iLeftJoin ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1318 | pLevel->top = sqlite3VdbeCurrentAddr(v); |
| 1319 | sqlite3VdbeAddOp(v, OP_Integer, 1, 0); |
| 1320 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 1321 | VdbeComment((v, "# record LEFT JOIN hit")); |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1322 | for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){ |
| 1323 | if( pTerm->p==0 ) continue; |
| 1324 | if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 1325 | sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1); |
| 1326 | pTerm->p = 0; |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 1327 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1328 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1329 | } |
| 1330 | pWInfo->iContinue = cont; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1331 | freeMaskSet(&maskSet); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1332 | return pWInfo; |
| 1333 | } |
| 1334 | |
| 1335 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1336 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1337 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1338 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1339 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1340 | Vdbe *v = pWInfo->pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1341 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1342 | WhereLevel *pLevel; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1343 | SrcList *pTabList = pWInfo->pTabList; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1344 | struct SrcList_item *pTabItem; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1345 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1346 | /* Generate loop termination code. |
| 1347 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1348 | for(i=pTabList->nSrc-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1349 | pLevel = &pWInfo->a[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1350 | sqlite3VdbeResolveLabel(v, pLevel->cont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1351 | if( pLevel->op!=OP_Noop ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1352 | sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1353 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1354 | sqlite3VdbeResolveLabel(v, pLevel->brk); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1355 | if( pLevel->inOp!=OP_Noop ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1356 | sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1357 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1358 | if( pLevel->iLeftJoin ){ |
| 1359 | int addr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1360 | addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1361 | sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iIdxCur>=0)); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1362 | sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1363 | if( pLevel->iIdxCur>=0 ){ |
| 1364 | sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iIdxCur, 0); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 1365 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1366 | sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1367 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1368 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1369 | |
| 1370 | /* The "break" point is here, just past the end of the outer loop. |
| 1371 | ** Set it. |
| 1372 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1373 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1374 | |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 1375 | /* Close all of the cursors that were opend by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1376 | */ |
| 1377 | pLevel = pWInfo->a; |
| 1378 | pTabItem = pTabList->a; |
| 1379 | for(i=0; i<pTabList->nSrc; i++, pTabItem++, pLevel++){ |
| 1380 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1381 | assert( pTab!=0 ); |
| 1382 | if( pTab->isTransient || pTab->pSelect ) continue; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1383 | if( (pLevel->score & 1)==0 ){ |
| 1384 | sqlite3VdbeAddOp(v, OP_Close, pTabItem->iCursor, 0); |
| 1385 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1386 | if( pLevel->pIdx!=0 ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1387 | sqlite3VdbeAddOp(v, OP_Close, pLevel->iIdxCur, 0); |
| 1388 | } |
| 1389 | |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 1390 | /* Make cursor substitutions for cases where we want to use |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1391 | ** just the index and never reference the table. |
| 1392 | ** |
| 1393 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 1394 | ** sqlite3WhereEnd will have created code that references the table |
| 1395 | ** directly. This loop scans all that code looking for opcodes |
| 1396 | ** that reference the table and converts them into opcodes that |
| 1397 | ** reference the index. |
| 1398 | */ |
| 1399 | if( pLevel->score & 1 ){ |
| 1400 | int i, j, last; |
| 1401 | VdbeOp *pOp; |
| 1402 | Index *pIdx = pLevel->pIdx; |
| 1403 | |
| 1404 | assert( pIdx!=0 ); |
| 1405 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 1406 | last = sqlite3VdbeCurrentAddr(v); |
| 1407 | for(i=pWInfo->iTop; i<last; i++, pOp++){ |
| 1408 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 1409 | if( pOp->opcode==OP_Column ){ |
| 1410 | pOp->p1 = pLevel->iIdxCur; |
| 1411 | for(j=0; j<pIdx->nColumn; j++){ |
| 1412 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 1413 | pOp->p2 = j; |
| 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | }else if( pOp->opcode==OP_Recno ){ |
| 1418 | pOp->p1 = pLevel->iIdxCur; |
| 1419 | pOp->opcode = OP_IdxRecno; |
| 1420 | } |
| 1421 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1422 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1423 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1424 | |
| 1425 | /* Final cleanup |
| 1426 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1427 | sqliteFree(pWInfo); |
| 1428 | return; |
| 1429 | } |