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