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 | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 19 | ** $Id: where.c,v 1.152 2005/07/23 22:59:56 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | */ |
| 21 | #include "sqliteInt.h" |
| 22 | |
| 23 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 24 | ** The number of bits in a Bitmask. "BMS" means "BitMask Size". |
| 25 | */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 26 | #define BMS (sizeof(Bitmask)*8) |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | ** Determine the number of elements in an array. |
| 30 | */ |
| 31 | #define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0])) |
| 32 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 33 | /* |
| 34 | ** Trace output macros |
| 35 | */ |
| 36 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 37 | int sqlite3_where_trace = 0; |
| 38 | # define TRACE(X) if(sqlite3_where_trace) sqlite3DebugPrintf X |
| 39 | #else |
| 40 | # define TRACE(X) |
| 41 | #endif |
| 42 | |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 43 | /* Forward reference |
| 44 | */ |
| 45 | typedef struct WhereClause WhereClause; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 46 | |
| 47 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 48 | ** The query generator uses an array of instances of this structure to |
| 49 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 50 | ** clause subexpression is separated from the others by an AND operator. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 51 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 52 | ** All WhereTerms are collected into a single WhereClause structure. |
| 53 | ** The following identity holds: |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 54 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 55 | ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 56 | ** |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 57 | ** When a term is of the form: |
| 58 | ** |
| 59 | ** X <op> <expr> |
| 60 | ** |
| 61 | ** where X is a column name and <op> is one of certain operators, |
| 62 | ** then WhereTerm.leftCursor and WhereTerm.leftColumn record the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 63 | ** cursor number and column number for X. WhereTerm.operator records |
| 64 | ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 65 | ** use of a bitmask encoding for the operator allows us to search |
| 66 | ** quickly for terms that match any of several different operators. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 67 | ** |
| 68 | ** prereqRight and prereqAll record sets of cursor numbers, |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 69 | ** but they do so indirectly. A single ExprMaskSet structure translates |
| 70 | ** cursor number into bits and the translated bit is stored in the prereq |
| 71 | ** fields. The translation is used in order to maximize the number of |
| 72 | ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 73 | ** spread out over the non-negative integers. For example, the cursor |
| 74 | ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The ExprMaskSet |
| 75 | ** translates these sparse cursor numbers into consecutive integers |
| 76 | ** beginning with 0 in order to make the best possible use of the available |
| 77 | ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 78 | ** would be mapped into integers 0 through 7. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 79 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 80 | typedef struct WhereTerm WhereTerm; |
| 81 | struct WhereTerm { |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 82 | Expr *pExpr; /* Pointer to the subexpression */ |
| 83 | u16 idx; /* Index of this term in pWC->a[] */ |
| 84 | i16 iPartner; /* Disable pWC->a[iPartner] when this term disabled */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 85 | u16 flags; /* Bit flags. See below */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 86 | i16 leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
| 87 | i16 leftColumn; /* Column number of X in "X <op> <expr>" */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 88 | u16 operator; /* A WO_xx value describing <op> */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 89 | WhereClause *pWC; /* The clause this term is part of */ |
| 90 | Bitmask prereqRight; /* Bitmask of tables used by pRight */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 91 | Bitmask prereqAll; /* Bitmask of tables referenced by p */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 95 | ** Allowed values of WhereTerm.flags |
| 96 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 97 | #define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(pExpr) */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 98 | #define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 99 | #define TERM_CODED 0x0004 /* This term is already coded */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | ** An instance of the following structure holds all information about a |
| 103 | ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 104 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 105 | struct WhereClause { |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 106 | Parse *pParse; /* The parser context */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 107 | int nTerm; /* Number of terms */ |
| 108 | int nSlot; /* Number of entries in a[] */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 109 | WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 110 | WhereTerm aStatic[10]; /* Initial static space for a[] */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 114 | ** An instance of the following structure keeps track of a mapping |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 115 | ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 116 | ** |
| 117 | ** The VDBE cursor numbers are small integers contained in |
| 118 | ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 119 | ** clause, the cursor numbers might not begin with 0 and they might |
| 120 | ** contain gaps in the numbering sequence. But we want to make maximum |
| 121 | ** use of the bits in our bitmasks. This structure provides a mapping |
| 122 | ** from the sparse cursor numbers into consecutive integers beginning |
| 123 | ** with 0. |
| 124 | ** |
| 125 | ** If ExprMaskSet.ix[A]==B it means that The A-th bit of a Bitmask |
| 126 | ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 127 | ** |
| 128 | ** For example, if the WHERE clause expression used these VDBE |
| 129 | ** cursors: 4, 5, 8, 29, 57, 73. Then the ExprMaskSet structure |
| 130 | ** would map those cursor numbers into bits 0 through 5. |
| 131 | ** |
| 132 | ** Note that the mapping is not necessarily ordered. In the example |
| 133 | ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 134 | ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 135 | ** does not really matter. What is important is that sparse cursor |
| 136 | ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 137 | ** no gaps. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 138 | */ |
| 139 | typedef struct ExprMaskSet ExprMaskSet; |
| 140 | struct ExprMaskSet { |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 141 | int n; /* Number of assigned cursor values */ |
| 142 | int ix[sizeof(Bitmask)*8]; /* Cursor assigned to each bit */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 145 | |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 146 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 147 | ** Bitmasks for the operators that indices are able to exploit. An |
| 148 | ** OR-ed combination of these values can be used when searching for |
| 149 | ** terms in the where clause. |
| 150 | */ |
| 151 | #define WO_IN 1 |
| 152 | #define WO_LIST 2 |
| 153 | #define WO_SELECT 4 |
| 154 | #define WO_EQ 8 |
| 155 | #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 156 | #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 157 | #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 158 | #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
| 159 | |
| 160 | /* |
| 161 | ** Value for flags returned by bestIndex() |
| 162 | */ |
| 163 | #define WHERE_ROWID_EQ 0x0001 /* rowid=EXPR or rowid IN (...) */ |
| 164 | #define WHERE_ROWID_RANGE 0x0002 /* rowid<EXPR and/or rowid>EXPR */ |
| 165 | #define WHERE_COLUMN_EQ 0x0010 /* x=EXPR or x IN (...) */ |
| 166 | #define WHERE_COLUMN_RANGE 0x0020 /* x<EXPR and/or x>EXPR */ |
| 167 | #define WHERE_COLUMN_IN 0x0040 /* x IN (...) */ |
| 168 | #define WHERE_TOP_LIMIT 0x0100 /* x<EXPR or x<=EXPR constraint */ |
| 169 | #define WHERE_BTM_LIMIT 0x0200 /* x>EXPR or x>=EXPR constraint */ |
| 170 | #define WHERE_IDX_ONLY 0x0800 /* Use index only - omit table */ |
| 171 | #define WHERE_ORDERBY 0x1000 /* Output will appear in correct order */ |
| 172 | #define WHERE_REVERSE 0x2000 /* Scan in reverse order */ |
| 173 | |
| 174 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 175 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 176 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 177 | static void whereClauseInit(WhereClause *pWC, Parse *pParse){ |
| 178 | pWC->pParse = pParse; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 179 | pWC->nTerm = 0; |
| 180 | pWC->nSlot = ARRAYSIZE(pWC->aStatic); |
| 181 | pWC->a = pWC->aStatic; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 186 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 187 | */ |
| 188 | static void whereClauseClear(WhereClause *pWC){ |
| 189 | int i; |
| 190 | WhereTerm *a; |
| 191 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 192 | if( a->flags & TERM_DYNAMIC ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 193 | sqlite3ExprDelete(a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | if( pWC->a!=pWC->aStatic ){ |
| 197 | sqliteFree(pWC->a); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | ** Add a new entries to the WhereClause structure. Increase the allocated |
| 203 | ** space as necessary. |
| 204 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 205 | static WhereTerm *whereClauseInsert(WhereClause *pWC, Expr *p, int flags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 206 | WhereTerm *pTerm; |
| 207 | if( pWC->nTerm>=pWC->nSlot ){ |
| 208 | WhereTerm *pOld = pWC->a; |
| 209 | pWC->a = sqliteMalloc( sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 210 | if( pWC->a==0 ) return 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 211 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 212 | if( pOld!=pWC->aStatic ){ |
| 213 | sqliteFree(pOld); |
| 214 | } |
| 215 | pWC->nSlot *= 2; |
| 216 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 217 | pTerm = &pWC->a[pWC->nTerm]; |
| 218 | pTerm->idx = pWC->nTerm; |
| 219 | pWC->nTerm++; |
| 220 | pTerm->pExpr = p; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 221 | pTerm->flags = flags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 222 | pTerm->pWC = pWC; |
| 223 | pTerm->iPartner = -1; |
| 224 | return pTerm; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 225 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 226 | |
| 227 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 228 | ** This routine identifies subexpressions in the WHERE clause where |
| 229 | ** each subexpression is separate by the AND operator. aSlot is |
| 230 | ** filled with pointers to the subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 231 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 232 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 233 | ** \________/ \_______________/ \________________/ |
| 234 | ** slot[0] slot[1] slot[2] |
| 235 | ** |
| 236 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 237 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 238 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 239 | ** In the previous sentence and in the diagram, "slot[]" refers to |
| 240 | ** the WhereClause.a[] array. This array grows as needed to contain |
| 241 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 242 | */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 243 | static void whereSplit(WhereClause *pWC, Expr *pExpr){ |
| 244 | if( pExpr==0 ) return; |
| 245 | if( pExpr->op!=TK_AND ){ |
| 246 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 247 | }else{ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 248 | whereSplit(pWC, pExpr->pLeft); |
| 249 | whereSplit(pWC, pExpr->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 250 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 254 | ** Initialize an expression mask set |
| 255 | */ |
| 256 | #define initMaskSet(P) memset(P, 0, sizeof(*P)) |
| 257 | |
| 258 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 259 | ** Return the bitmask for the given cursor number. Return 0 if |
| 260 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 261 | */ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 262 | static Bitmask getMask(ExprMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 263 | int i; |
| 264 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 265 | if( pMaskSet->ix[i]==iCursor ){ |
| 266 | return ((Bitmask)1)<<i; |
| 267 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 268 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 273 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 274 | ** |
| 275 | ** There is one cursor per table in the FROM clause. The number of |
| 276 | ** tables in the FROM clause is limited by a test early in the |
| 277 | ** sqlite3WhereBegin() routien. So we know that the pMaskSet->ix[] |
| 278 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 279 | */ |
| 280 | static void createMask(ExprMaskSet *pMaskSet, int iCursor){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 281 | assert( pMaskSet->n < ARRAYSIZE(pMaskSet->ix) ); |
| 282 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 286 | ** This routine walks (recursively) an expression tree and generates |
| 287 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 288 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 289 | ** |
| 290 | ** In order for this routine to work, the calling function must have |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 291 | ** previously invoked sqlite3ExprResolveNames() on the expression. See |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 292 | ** the header comment on that routine for additional information. |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 293 | ** The sqlite3ExprResolveNames() routines looks for column names and |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 294 | ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 295 | ** the VDBE cursor number of the table. This routine just has to |
| 296 | ** translate the cursor numbers into bitmask values and OR all |
| 297 | ** the bitmasks together. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 298 | */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 299 | static Bitmask exprListTableUsage(ExprMaskSet *, ExprList *); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 300 | static Bitmask exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){ |
| 301 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 302 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 303 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 304 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 305 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 306 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 307 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 308 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
| 309 | mask |= exprListTableUsage(pMaskSet, p->pList); |
| 310 | if( p->pSelect ){ |
| 311 | Select *pS = p->pSelect; |
| 312 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
| 313 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 314 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 315 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 316 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 317 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 318 | return mask; |
| 319 | } |
| 320 | static Bitmask exprListTableUsage(ExprMaskSet *pMaskSet, ExprList *pList){ |
| 321 | int i; |
| 322 | Bitmask mask = 0; |
| 323 | if( pList ){ |
| 324 | for(i=0; i<pList->nExpr; i++){ |
| 325 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 328 | return mask; |
| 329 | } |
| 330 | |
| 331 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 332 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 333 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 334 | ** "=", "<", ">", "<=", ">=", and "IN". |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 335 | */ |
| 336 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 337 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 338 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 339 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 340 | assert( TK_GE==TK_EQ+4 ); |
drh | 9a43267 | 2004-10-04 13:38:09 +0000 | [diff] [blame] | 341 | return op==TK_IN || (op>=TK_EQ && op<=TK_GE); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 345 | ** Swap two objects of type T. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 346 | */ |
| 347 | #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} |
| 348 | |
| 349 | /* |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 350 | ** Commute a comparision operator. Expressions of the form "X op Y" |
| 351 | ** are converted into "Y op X". |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 352 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 353 | static void exprCommute(Expr *pExpr){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 354 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 355 | SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl); |
| 356 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 357 | if( pExpr->op>=TK_GT ){ |
| 358 | assert( TK_LT==TK_GT+2 ); |
| 359 | assert( TK_GE==TK_LE+2 ); |
| 360 | assert( TK_GT>TK_EQ ); |
| 361 | assert( TK_GT<TK_LE ); |
| 362 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 363 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 364 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 368 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 369 | */ |
| 370 | static int operatorMask(int op){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 371 | int c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 372 | assert( allowedOp(op) ); |
| 373 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 374 | c = WO_IN; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 375 | }else{ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 376 | c = WO_EQ<<(op-TK_EQ); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 377 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 378 | assert( op!=TK_IN || c==WO_IN ); |
| 379 | assert( op!=TK_EQ || c==WO_EQ ); |
| 380 | assert( op!=TK_LT || c==WO_LT ); |
| 381 | assert( op!=TK_LE || c==WO_LE ); |
| 382 | assert( op!=TK_GT || c==WO_GT ); |
| 383 | assert( op!=TK_GE || c==WO_GE ); |
| 384 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
| 388 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 389 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 390 | ** the WO_xx operator codes specified by the op parameter. |
| 391 | ** Return a pointer to the term. Return 0 if not found. |
| 392 | */ |
| 393 | static WhereTerm *findTerm( |
| 394 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 395 | int iCur, /* Cursor number of LHS */ |
| 396 | int iColumn, /* Column number of LHS */ |
| 397 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 398 | u16 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 399 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 400 | ){ |
| 401 | WhereTerm *pTerm; |
| 402 | int k; |
| 403 | for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){ |
| 404 | if( pTerm->leftCursor==iCur |
| 405 | && (pTerm->prereqRight & notReady)==0 |
| 406 | && pTerm->leftColumn==iColumn |
| 407 | && (pTerm->operator & op)!=0 |
| 408 | ){ |
| 409 | if( iCur>=0 && pIdx ){ |
| 410 | Expr *pX = pTerm->pExpr; |
| 411 | CollSeq *pColl; |
| 412 | char idxaff; |
| 413 | int k; |
| 414 | Parse *pParse = pWC->pParse; |
| 415 | |
| 416 | idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 417 | if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue; |
| 418 | pColl = sqlite3ExprCollSeq(pParse, pX->pLeft); |
| 419 | if( !pColl ){ |
| 420 | if( pX->pRight ){ |
| 421 | pColl = sqlite3ExprCollSeq(pParse, pX->pRight); |
| 422 | } |
| 423 | if( !pColl ){ |
| 424 | pColl = pParse->db->pDfltColl; |
| 425 | } |
| 426 | } |
| 427 | for(k=0; k<pIdx->nColumn && pIdx->aiColumn[k]!=iColumn; k++){} |
| 428 | assert( k<pIdx->nColumn ); |
| 429 | if( pColl!=pIdx->keyInfo.aColl[k] ) continue; |
| 430 | } |
| 431 | return pTerm; |
| 432 | } |
| 433 | } |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 438 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 439 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 440 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 441 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 442 | ** |
| 443 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
| 444 | ** to the standard form of "X <op> <expr>". If the expression is of |
| 445 | ** the form "X <op> Y" where both X and Y are columns, then the original |
| 446 | ** expression is unchanged and a new virtual expression of the form |
| 447 | ** "Y <op> X" is added to the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 448 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 449 | static void exprAnalyze( |
| 450 | SrcList *pSrc, /* the FROM clause */ |
| 451 | ExprMaskSet *pMaskSet, /* table masks */ |
| 452 | WhereTerm *pTerm /* the WHERE clause term to be analyzed */ |
| 453 | ){ |
| 454 | Expr *pExpr = pTerm->pExpr; |
| 455 | Bitmask prereqLeft; |
| 456 | Bitmask prereqAll; |
| 457 | int idxRight; |
| 458 | |
| 459 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 460 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 461 | pTerm->prereqAll = prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 462 | pTerm->leftCursor = -1; |
| 463 | pTerm->iPartner = -1; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 464 | pTerm->operator = 0; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 465 | idxRight = -1; |
| 466 | if( allowedOp(pExpr->op) && (pTerm->prereqRight & prereqLeft)==0 ){ |
| 467 | Expr *pLeft = pExpr->pLeft; |
| 468 | Expr *pRight = pExpr->pRight; |
| 469 | if( pLeft->op==TK_COLUMN ){ |
| 470 | pTerm->leftCursor = pLeft->iTable; |
| 471 | pTerm->leftColumn = pLeft->iColumn; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 472 | pTerm->operator = operatorMask(pExpr->op); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 473 | if( pTerm->operator==WO_IN ){ |
| 474 | if( pExpr->pSelect ){ |
| 475 | pTerm->operator |= WO_SELECT; |
| 476 | }else if( pExpr->pList ){ |
| 477 | pTerm->operator |= WO_LIST; |
| 478 | } |
| 479 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 480 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 481 | if( pRight && pRight->op==TK_COLUMN ){ |
| 482 | WhereTerm *pNew; |
| 483 | Expr *pDup; |
| 484 | if( pTerm->leftCursor>=0 ){ |
| 485 | pDup = sqlite3ExprDup(pExpr); |
| 486 | pNew = whereClauseInsert(pTerm->pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 487 | if( pNew==0 ) return; |
| 488 | pNew->iPartner = pTerm->idx; |
| 489 | }else{ |
| 490 | pDup = pExpr; |
| 491 | pNew = pTerm; |
| 492 | } |
| 493 | exprCommute(pDup); |
| 494 | pLeft = pDup->pLeft; |
| 495 | pNew->leftCursor = pLeft->iTable; |
| 496 | pNew->leftColumn = pLeft->iColumn; |
| 497 | pNew->prereqRight = prereqLeft; |
| 498 | pNew->prereqAll = prereqAll; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 499 | pNew->operator = operatorMask(pDup->op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 504 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 505 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 506 | ** This routine decides if pIdx can be used to satisfy the ORDER BY |
| 507 | ** clause. If it can, it returns 1. If pIdx cannot satisfy the |
| 508 | ** ORDER BY clause, this routine returns 0. |
| 509 | ** |
| 510 | ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the |
| 511 | ** left-most table in the FROM clause of that same SELECT statement and |
| 512 | ** the table has a cursor number of "base". pIdx is an index on pTab. |
| 513 | ** |
| 514 | ** nEqCol is the number of columns of pIdx that are used as equality |
| 515 | ** constraints. Any of these columns may be missing from the ORDER BY |
| 516 | ** clause and the match can still be a success. |
| 517 | ** |
| 518 | ** If the index is UNIQUE, then the ORDER BY clause is allowed to have |
| 519 | ** additional terms past the end of the index and the match will still |
| 520 | ** be a success. |
| 521 | ** |
| 522 | ** All terms of the ORDER BY that match against the index must be either |
| 523 | ** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE |
| 524 | ** index do not need to satisfy this constraint.) The *pbRev value is |
| 525 | ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if |
| 526 | ** the ORDER BY clause is all ASC. |
| 527 | */ |
| 528 | static int isSortingIndex( |
| 529 | Parse *pParse, /* Parsing context */ |
| 530 | Index *pIdx, /* The index we are testing */ |
| 531 | Table *pTab, /* The table to be sorted */ |
| 532 | int base, /* Cursor number for pTab */ |
| 533 | ExprList *pOrderBy, /* The ORDER BY clause */ |
| 534 | int nEqCol, /* Number of index columns with == constraints */ |
| 535 | int *pbRev /* Set to 1 if ORDER BY is DESC */ |
| 536 | ){ |
| 537 | int i, j; /* Loop counters */ |
| 538 | int sortOrder; /* Which direction we are sorting */ |
| 539 | int nTerm; /* Number of ORDER BY terms */ |
| 540 | struct ExprList_item *pTerm; /* A term of the ORDER BY clause */ |
| 541 | sqlite3 *db = pParse->db; |
| 542 | |
| 543 | assert( pOrderBy!=0 ); |
| 544 | nTerm = pOrderBy->nExpr; |
| 545 | assert( nTerm>0 ); |
| 546 | |
| 547 | /* Match terms of the ORDER BY clause against columns of |
| 548 | ** the index. |
| 549 | */ |
| 550 | for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<pIdx->nColumn; i++){ |
| 551 | Expr *pExpr; /* The expression of the ORDER BY pTerm */ |
| 552 | CollSeq *pColl; /* The collating sequence of pExpr */ |
| 553 | |
| 554 | pExpr = pTerm->pExpr; |
| 555 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){ |
| 556 | /* Can not use an index sort on anything that is not a column in the |
| 557 | ** left-most table of the FROM clause */ |
| 558 | return 0; |
| 559 | } |
| 560 | pColl = sqlite3ExprCollSeq(pParse, pExpr); |
| 561 | if( !pColl ) pColl = db->pDfltColl; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 562 | if( pExpr->iColumn!=pIdx->aiColumn[i] || pColl!=pIdx->keyInfo.aColl[i] ){ |
| 563 | /* Term j of the ORDER BY clause does not match column i of the index */ |
| 564 | if( i<nEqCol ){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 565 | /* If an index column that is constrained by == fails to match an |
| 566 | ** ORDER BY term, that is OK. Just ignore that column of the index |
| 567 | */ |
| 568 | continue; |
| 569 | }else{ |
| 570 | /* If an index column fails to match and is not constrained by == |
| 571 | ** then the index cannot satisfy the ORDER BY constraint. |
| 572 | */ |
| 573 | return 0; |
| 574 | } |
| 575 | } |
| 576 | if( i>nEqCol ){ |
| 577 | if( pTerm->sortOrder!=sortOrder ){ |
| 578 | /* Indices can only be used if all ORDER BY terms past the |
| 579 | ** equality constraints are all either DESC or ASC. */ |
| 580 | return 0; |
| 581 | } |
| 582 | }else{ |
| 583 | sortOrder = pTerm->sortOrder; |
| 584 | } |
| 585 | j++; |
| 586 | pTerm++; |
| 587 | } |
| 588 | |
| 589 | /* The index can be used for sorting if all terms of the ORDER BY clause |
| 590 | ** or covered or if we ran out of index columns and the it is a UNIQUE |
| 591 | ** index. |
| 592 | */ |
| 593 | if( j>=nTerm || (i>=pIdx->nColumn && pIdx->onError!=OE_None) ){ |
| 594 | *pbRev = sortOrder==SQLITE_SO_DESC; |
| 595 | return 1; |
| 596 | } |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | /* |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 601 | ** Check table to see if the ORDER BY clause in pOrderBy can be satisfied |
| 602 | ** by sorting in order of ROWID. Return true if so and set *pbRev to be |
| 603 | ** true for reverse ROWID and false for forward ROWID order. |
| 604 | */ |
| 605 | static int sortableByRowid( |
| 606 | int base, /* Cursor number for table to be sorted */ |
| 607 | ExprList *pOrderBy, /* The ORDER BY clause */ |
| 608 | int *pbRev /* Set to 1 if ORDER BY is DESC */ |
| 609 | ){ |
| 610 | Expr *p; |
| 611 | |
| 612 | assert( pOrderBy!=0 ); |
| 613 | assert( pOrderBy->nExpr>0 ); |
| 614 | p = pOrderBy->a[0].pExpr; |
| 615 | if( p->op==TK_COLUMN && p->iTable==base && p->iColumn==-1 ){ |
| 616 | *pbRev = pOrderBy->a[0].sortOrder; |
| 617 | return 1; |
| 618 | } |
| 619 | return 0; |
| 620 | } |
| 621 | |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 622 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 623 | ** Find the best index for accessing a particular table. Return a pointer |
| 624 | ** to the index, flags that describe how the index should be used, the |
| 625 | ** number of equality constraints and the "cost" for this index. |
| 626 | ** |
| 627 | ** The lowest cost index wins. The cost is an estimate of the amount of |
| 628 | ** CPU and disk I/O need to process the request using the selected index. |
| 629 | ** Factors that influence cost include: |
| 630 | ** |
| 631 | ** * The estimated number of rows that will be retrieved. (The |
| 632 | ** fewer the better.) |
| 633 | ** |
| 634 | ** * Whether or not sorting must occur. |
| 635 | ** |
| 636 | ** * Whether or not there must be separate lookups in the |
| 637 | ** index and in the main table. |
| 638 | ** |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 639 | */ |
| 640 | static double bestIndex( |
| 641 | Parse *pParse, /* The parsing context */ |
| 642 | WhereClause *pWC, /* The WHERE clause */ |
| 643 | struct SrcList_item *pSrc, /* The FROM clause term to search */ |
| 644 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 645 | ExprList *pOrderBy, /* The order by clause */ |
| 646 | Index **ppIndex, /* Make *ppIndex point to the best index */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 647 | int *pFlags, /* Put flags describing this choice in *pFlags */ |
| 648 | int *pnEq /* Put the number of == or IN constraints here */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 649 | ){ |
| 650 | WhereTerm *pTerm; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 651 | Index *bestIdx = 0; /* Index that gives the lowest cost */ |
| 652 | double lowestCost = 1.0e99; /* The cost of using bestIdx */ |
| 653 | int bestFlags = 0; /* Flags associated with bestIdx */ |
| 654 | int bestNEq = 0; /* Best value for nEq */ |
| 655 | int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */ |
| 656 | Index *pProbe; /* An index we are evaluating */ |
| 657 | int rev; /* True to scan in reverse order */ |
| 658 | int flags; /* Flags associated with pProbe */ |
| 659 | int nEq; /* Number of == or IN constraints */ |
| 660 | double cost; /* Cost of using pProbe */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 661 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 662 | TRACE(("bestIndex: tbl=%s notReady=%x\n", pSrc->pTab->zName, notReady)); |
| 663 | |
| 664 | /* Check for a rowid=EXPR or rowid IN (...) constraints |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 665 | */ |
| 666 | pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0); |
| 667 | if( pTerm ){ |
| 668 | *ppIndex = 0; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 669 | bestFlags = WHERE_ROWID_EQ; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 670 | if( pTerm->operator & WO_EQ ){ |
| 671 | *pFlags = WHERE_ROWID_EQ; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 672 | *pnEq = 1; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 673 | if( pOrderBy ) *pFlags |= WHERE_ORDERBY; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 674 | TRACE(("... best is rowid\n")); |
| 675 | return 0.0; |
| 676 | }else if( pTerm->operator & WO_LIST ){ |
| 677 | lowestCost = pTerm->pExpr->pList->nExpr; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 678 | }else{ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 679 | lowestCost = 100.0; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 680 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 681 | TRACE(("... rowid IN cost: %g\n", lowestCost)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 682 | } |
| 683 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 684 | /* Check for constraints on a range of rowids or a full table scan. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 685 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 686 | pProbe = pSrc->pTab->pIndex; |
| 687 | cost = pProbe ? pProbe->aiRowEst[0] : 100000.0; |
| 688 | TRACE(("... base cost: %g\n", cost)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 689 | pTerm = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE|WO_GT|WO_GE, 0); |
| 690 | if( pTerm ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 691 | flags = WHERE_ROWID_RANGE; |
| 692 | if( findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0) ){ |
| 693 | flags |= WHERE_TOP_LIMIT; |
| 694 | cost *= 0.25; /* Guess that rowid<EXPR eliminates 75% of the search */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 695 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 696 | if( findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0) ){ |
| 697 | flags |= WHERE_BTM_LIMIT; |
| 698 | cost *= 0.25; /* Guess that rowid>EXPR eliminates 75% of the search */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 699 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 700 | TRACE(("... rowid range cost: %g\n", cost)); |
| 701 | }else{ |
| 702 | flags = 0; |
| 703 | } |
| 704 | if( pOrderBy && sortableByRowid(iCur, pOrderBy, &rev) ){ |
| 705 | flags |= WHERE_ORDERBY|WHERE_ROWID_RANGE; |
| 706 | cost *= 0.5; |
| 707 | if( rev ){ |
| 708 | flags |= WHERE_REVERSE; |
| 709 | } |
| 710 | TRACE(("... order by reduces cost to %g\n", cost)); |
| 711 | } |
| 712 | if( cost<lowestCost ){ |
| 713 | lowestCost = cost; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 714 | bestFlags = flags; |
| 715 | } |
| 716 | |
| 717 | /* Look at each index. |
| 718 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 719 | for(; pProbe; pProbe=pProbe->pNext){ |
| 720 | int i; /* Loop counter */ |
| 721 | double inMultiplier = 2.0; /* Includes built-in index lookup penalty */ |
| 722 | |
| 723 | TRACE(("... index %s:\n", pProbe->zName)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 724 | |
| 725 | /* Count the number of columns in the index that are satisfied |
| 726 | ** by x=EXPR constraints or x IN (...) constraints. |
| 727 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 728 | flags = 0; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 729 | for(i=0; i<pProbe->nColumn; i++){ |
| 730 | int j = pProbe->aiColumn[i]; |
| 731 | pTerm = findTerm(pWC, iCur, j, notReady, WO_EQ|WO_IN, pProbe); |
| 732 | if( pTerm==0 ) break; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 733 | flags |= WHERE_COLUMN_EQ; |
| 734 | if( pTerm->operator & WO_IN ){ |
| 735 | flags |= WHERE_COLUMN_IN; |
| 736 | if( pTerm->operator & WO_SELECT ){ |
| 737 | inMultiplier *= 100.0; |
| 738 | }else if( pTerm->operator & WO_LIST ){ |
| 739 | inMultiplier *= pTerm->pExpr->pList->nExpr + 1.0; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 743 | cost = pProbe->aiRowEst[i] * inMultiplier; |
| 744 | nEq = i; |
| 745 | TRACE(("...... nEq=%d inMult=%g cost=%g\n", nEq, inMultiplier, cost)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 746 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 747 | /* Look for range constraints |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 748 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 749 | if( nEq<pProbe->nColumn ){ |
| 750 | int j = pProbe->aiColumn[nEq]; |
| 751 | pTerm = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pProbe); |
| 752 | if( pTerm ){ |
| 753 | flags = WHERE_COLUMN_RANGE; |
| 754 | if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pProbe) ){ |
| 755 | flags |= WHERE_TOP_LIMIT; |
| 756 | cost *= 0.5; |
| 757 | } |
| 758 | if( findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pProbe) ){ |
| 759 | flags |= WHERE_BTM_LIMIT; |
| 760 | cost *= 0.5; |
| 761 | } |
| 762 | TRACE(("...... range reduces cost to %g\n", cost)); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* Reduce the cost substantially if this index can be used to satisfy |
| 767 | ** the ORDER BY clause |
| 768 | */ |
| 769 | if( pOrderBy && (flags & WHERE_COLUMN_IN)==0 && |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 770 | isSortingIndex(pParse, pProbe, pSrc->pTab, iCur, pOrderBy, nEq, &rev) ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 771 | if( flags==0 ){ |
| 772 | flags = WHERE_COLUMN_RANGE; |
| 773 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 774 | flags |= WHERE_ORDERBY; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 775 | cost *= 0.5; |
| 776 | if( rev ){ |
| 777 | flags |= WHERE_REVERSE; |
| 778 | } |
| 779 | TRACE(("...... orderby reduces cost to %g\n", cost)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* Check to see if we can get away with using just the index without |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 783 | ** ever reading the table. If that is the case, then halve the |
| 784 | ** cost of this index. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 785 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 786 | if( flags && pSrc->colUsed < (((Bitmask)1)<<(BMS-1)) ){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 787 | Bitmask m = pSrc->colUsed; |
| 788 | int j; |
| 789 | for(j=0; j<pProbe->nColumn; j++){ |
| 790 | int x = pProbe->aiColumn[j]; |
| 791 | if( x<BMS-1 ){ |
| 792 | m &= ~(((Bitmask)1)<<x); |
| 793 | } |
| 794 | } |
| 795 | if( m==0 ){ |
| 796 | flags |= WHERE_IDX_ONLY; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 797 | cost *= 0.5; |
| 798 | TRACE(("...... idx-only reduces cost to %g\n", cost)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 802 | /* If this index has achieved the lowest cost so far, then use it. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 803 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 804 | if( cost < lowestCost ){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 805 | bestIdx = pProbe; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 806 | lowestCost = cost; |
| 807 | if( flags==0 ){ |
| 808 | flags = WHERE_COLUMN_RANGE; |
| 809 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 810 | bestFlags = flags; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 811 | bestNEq = nEq; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 815 | /* Report the best result |
| 816 | */ |
| 817 | *ppIndex = bestIdx; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 818 | TRACE(("best index is %s, cost=%g, flags=%x, nEq=%d\n", |
| 819 | bestIdx ? bestIdx->zName : "(none)", lowestCost, bestFlags, bestNEq)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 820 | *pFlags = bestFlags; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 821 | *pnEq = bestNEq; |
| 822 | return lowestCost; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 823 | } |
| 824 | |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 825 | |
| 826 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 827 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 828 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 829 | ** or USING clause of that join. |
| 830 | ** |
| 831 | ** Consider the term t2.z='ok' in the following queries: |
| 832 | ** |
| 833 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 834 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 835 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 836 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 837 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 838 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 839 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 840 | ** |
| 841 | ** Disabling a term causes that term to not be tested in the inner loop |
| 842 | ** of the join. Disabling is an optimization. We would get the correct |
| 843 | ** results if nothing were ever disabled, but joins might run a little |
| 844 | ** slower. The trick is to disable as much as we can without disabling |
| 845 | ** too much. If we disabled in (1), we'd get the wrong answer. |
| 846 | ** See ticket #813. |
| 847 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 848 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 849 | if( pTerm |
| 850 | && (pTerm->flags & TERM_CODED)==0 |
| 851 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 852 | ){ |
| 853 | pTerm->flags |= TERM_CODED; |
| 854 | if( pTerm->iPartner>=0 ){ |
| 855 | disableTerm(pLevel, &pTerm->pWC->a[pTerm->iPartner]); |
| 856 | } |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
| 860 | /* |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 861 | ** Generate code that builds a probe for an index. Details: |
| 862 | ** |
| 863 | ** * Check the top nColumn entries on the stack. If any |
| 864 | ** of those entries are NULL, jump immediately to brk, |
| 865 | ** which is the loop exit, since no index entry will match |
| 866 | ** if any part of the key is NULL. |
| 867 | ** |
| 868 | ** * Construct a probe entry from the top nColumn entries in |
| 869 | ** the stack with affinities appropriate for index pIdx. |
| 870 | */ |
| 871 | static void buildIndexProbe(Vdbe *v, int nColumn, int brk, Index *pIdx){ |
| 872 | sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3); |
| 873 | sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); |
| 874 | sqlite3VdbeAddOp(v, OP_Goto, 0, brk); |
| 875 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 876 | sqlite3IndexAffinityStr(v, pIdx); |
| 877 | } |
| 878 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 879 | |
| 880 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 881 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 882 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 883 | ** coded. |
| 884 | ** |
| 885 | ** The current value for the constraint is left on the top of the stack. |
| 886 | ** |
| 887 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 888 | ** result is left on the stack. For constraints of the form X IN (...) |
| 889 | ** this routine sets up a loop that will iterate over all values of X. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 890 | */ |
| 891 | static void codeEqualityTerm( |
| 892 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 893 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 894 | int brk, /* Jump here to abandon the loop */ |
| 895 | WhereLevel *pLevel /* When level of the FROM clause we are working on */ |
| 896 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 897 | Expr *pX = pTerm->pExpr; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 898 | if( pX->op!=TK_IN ){ |
| 899 | assert( pX->op==TK_EQ ); |
| 900 | sqlite3ExprCode(pParse, pX->pRight); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 901 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 902 | }else{ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 903 | int iTab; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 904 | int *aIn; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 905 | Vdbe *v = pParse->pVdbe; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 906 | |
| 907 | sqlite3CodeSubselect(pParse, pX); |
| 908 | iTab = pX->iTable; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 909 | sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 910 | VdbeComment((v, "# %.*s", pX->span.n, pX->span.z)); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 911 | pLevel->nIn++; |
| 912 | pLevel->aInLoop = aIn = sqliteRealloc(pLevel->aInLoop, |
| 913 | sizeof(pLevel->aInLoop[0])*3*pLevel->nIn); |
| 914 | if( aIn ){ |
| 915 | aIn += pLevel->nIn*3 - 3; |
| 916 | aIn[0] = OP_Next; |
| 917 | aIn[1] = iTab; |
| 918 | aIn[2] = sqlite3VdbeAddOp(v, OP_Column, iTab, 0); |
| 919 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 920 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 921 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 922 | disableTerm(pLevel, pTerm); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 923 | } |
| 924 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 925 | /* |
| 926 | ** Generate code that will evaluate all == and IN constraints for an |
| 927 | ** index. The values for all constraints are left on the stack. |
| 928 | ** |
| 929 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 930 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 931 | ** The index has as many as three equality constraints, but in this |
| 932 | ** example, the third "c" value is an inequality. So only two |
| 933 | ** constraints are coded. This routine will generate code to evaluate |
| 934 | ** a==5 and b IN (1,2,3). The current values for a and b will be left |
| 935 | ** on the stack - a is the deepest and b the shallowest. |
| 936 | ** |
| 937 | ** In the example above nEq==2. But this subroutine works for any value |
| 938 | ** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
| 939 | ** The only thing it does is allocate the pLevel->iMem memory cell. |
| 940 | ** |
| 941 | ** This routine always allocates at least one memory cell and puts |
| 942 | ** the address of that memory cell in pLevel->iMem. The code that |
| 943 | ** calls this routine will use pLevel->iMem to store the termination |
| 944 | ** key value of the loop. If one or more IN operators appear, then |
| 945 | ** this routine allocates an additional nEq memory cells for internal |
| 946 | ** use. |
| 947 | */ |
| 948 | static void codeAllEqualityTerms( |
| 949 | Parse *pParse, /* Parsing context */ |
| 950 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 951 | WhereClause *pWC, /* The WHERE clause */ |
| 952 | Bitmask notReady, /* Which parts of FROM have not yet been coded */ |
| 953 | int brk /* Jump here to end the loop */ |
| 954 | ){ |
| 955 | int nEq = pLevel->nEq; /* The number of == or IN constraints to code */ |
| 956 | int termsInMem = 0; /* If true, store value in mem[] cells */ |
| 957 | Vdbe *v = pParse->pVdbe; /* The virtual machine under construction */ |
| 958 | Index *pIdx = pLevel->pIdx; /* The index being used for this loop */ |
| 959 | int iCur = pLevel->iTabCur; /* The cursor of the table */ |
| 960 | WhereTerm *pTerm; /* A single constraint term */ |
| 961 | int j; /* Loop counter */ |
| 962 | |
| 963 | /* Figure out how many memory cells we will need then allocate them. |
| 964 | ** We always need at least one used to store the loop terminator |
| 965 | ** value. If there are IN operators we'll need one for each == or |
| 966 | ** IN constraint. |
| 967 | */ |
| 968 | pLevel->iMem = pParse->nMem++; |
| 969 | if( pLevel->flags & WHERE_COLUMN_IN ){ |
| 970 | pParse->nMem += pLevel->nEq; |
| 971 | termsInMem = 1; |
| 972 | } |
| 973 | |
| 974 | /* Evaluate the equality constraints |
| 975 | */ |
| 976 | for(j=0; 1; j++){ |
| 977 | int k = pIdx->aiColumn[j]; |
| 978 | pTerm = findTerm(pWC, iCur, k, notReady, WO_EQ|WO_IN, pIdx); |
| 979 | if( pTerm==0 ) break; |
| 980 | assert( (pTerm->flags & TERM_CODED)==0 ); |
| 981 | codeEqualityTerm(pParse, pTerm, brk, pLevel); |
| 982 | if( termsInMem ){ |
| 983 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem+j+1, 1); |
| 984 | } |
| 985 | } |
| 986 | assert( j==nEq ); |
| 987 | |
| 988 | /* Make sure all the constraint values are on the top of the stack |
| 989 | */ |
| 990 | if( termsInMem ){ |
| 991 | for(j=0; j<nEq; j++){ |
| 992 | sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem+j+1, 0); |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 997 | #ifdef SQLITE_TEST |
| 998 | /* |
| 999 | ** The following variable holds a text description of query plan generated |
| 1000 | ** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin |
| 1001 | ** overwrites the previous. This information is used for testing and |
| 1002 | ** analysis only. |
| 1003 | */ |
| 1004 | char sqlite3_query_plan[BMS*2*40]; /* Text of the join */ |
| 1005 | static int nQPlan = 0; /* Next free slow in _query_plan[] */ |
| 1006 | |
| 1007 | #endif /* SQLITE_TEST */ |
| 1008 | |
| 1009 | |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1010 | |
| 1011 | /* |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 1012 | ** Generate the beginning of the loop used for WHERE clause processing. |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 1013 | ** The return value is a pointer to an opaque structure that contains |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1014 | ** information needed to terminate the loop. Later, the calling routine |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1015 | ** should invoke sqlite3WhereEnd() with the return value of this function |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1016 | ** in order to complete the WHERE clause processing. |
| 1017 | ** |
| 1018 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1019 | ** |
| 1020 | ** The basic idea is to do a nested loop, one loop for each table in |
| 1021 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 1022 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 1023 | ** example, if the SQL is this: |
| 1024 | ** |
| 1025 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 1026 | ** |
| 1027 | ** Then the code generated is conceptually like the following: |
| 1028 | ** |
| 1029 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1030 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1031 | ** foreach row3 in t3 do / |
| 1032 | ** ... |
| 1033 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1034 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1035 | ** end / |
| 1036 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1037 | ** Note that the loops might not be nested in the order in which they |
| 1038 | ** appear in the FROM clause if a different order is better able to make |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1039 | ** use of indices. Note also that when the IN operator appears in |
| 1040 | ** the WHERE clause, it might result in additional nested loops for |
| 1041 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1042 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1043 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1044 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 1045 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1046 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1047 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1048 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 1049 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1050 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1051 | ** data from the various tables of the loop. |
| 1052 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1053 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 1054 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 1055 | ** the tables have indices and there are terms in the WHERE clause that |
| 1056 | ** refer to those indices, a complete table scan can be avoided and the |
| 1057 | ** code will run much faster. Most of the work of this routine is checking |
| 1058 | ** to see if there are indices that can be used to speed up the loop. |
| 1059 | ** |
| 1060 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 1061 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 1062 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 1063 | ** loops are evaluated and if false a jump is made around all subsequent |
| 1064 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 1065 | ** most loop) |
| 1066 | ** |
| 1067 | ** OUTER JOINS |
| 1068 | ** |
| 1069 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 1070 | ** |
| 1071 | ** foreach row1 in t1 do |
| 1072 | ** flag = 0 |
| 1073 | ** foreach row2 in t2 do |
| 1074 | ** start: |
| 1075 | ** ... |
| 1076 | ** flag = 1 |
| 1077 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 1078 | ** if flag==0 then |
| 1079 | ** move the row2 cursor to a null row |
| 1080 | ** goto start |
| 1081 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1082 | ** end |
| 1083 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 1084 | ** ORDER BY CLAUSE PROCESSING |
| 1085 | ** |
| 1086 | ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 1087 | ** if there is one. If there is no ORDER BY clause or if this routine |
| 1088 | ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL. |
| 1089 | ** |
| 1090 | ** If an index can be used so that the natural output order of the table |
| 1091 | ** scan is correct for the ORDER BY clause, then that index is used and |
| 1092 | ** *ppOrderBy is set to NULL. This is an optimization that prevents an |
| 1093 | ** unnecessary sort of the result set if an index appropriate for the |
| 1094 | ** ORDER BY clause already exists. |
| 1095 | ** |
| 1096 | ** If the where clause loops cannot be arranged to provide the correct |
| 1097 | ** output order, then the *ppOrderBy is unchanged. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1098 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1099 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 1100 | Parse *pParse, /* The parser context */ |
| 1101 | SrcList *pTabList, /* A list of all tables to be scanned */ |
| 1102 | Expr *pWhere, /* The WHERE clause */ |
drh | f8db1bc | 2005-04-22 02:38:37 +0000 | [diff] [blame] | 1103 | ExprList **ppOrderBy /* An ORDER BY clause, or NULL */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1104 | ){ |
| 1105 | int i; /* Loop counter */ |
| 1106 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 1107 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | d4f5ee2 | 2003-07-16 00:54:31 +0000 | [diff] [blame] | 1108 | int brk, cont = 0; /* Addresses used during code generation */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1109 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1110 | WhereTerm *pTerm; /* A single term in the WHERE clause */ |
| 1111 | ExprMaskSet maskSet; /* The expression mask set */ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1112 | WhereClause wc; /* The WHERE clause is divided into these terms */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1113 | struct SrcList_item *pTabItem; /* A single entry from pTabList */ |
| 1114 | WhereLevel *pLevel; /* A single level in the pWInfo list */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1115 | int iFrom; /* First unused FROM clause element */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1116 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1117 | /* The number of tables in the FROM clause is limited by the number of |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1118 | ** bits in a Bitmask |
| 1119 | */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1120 | if( pTabList->nSrc>BMS ){ |
| 1121 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
drh | 83dcb1a | 2002-06-28 01:02:38 +0000 | [diff] [blame] | 1125 | /* Split the WHERE clause into separate subexpressions where each |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1126 | ** subexpression is separated by an AND operator. |
drh | 83dcb1a | 2002-06-28 01:02:38 +0000 | [diff] [blame] | 1127 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1128 | initMaskSet(&maskSet); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1129 | whereClauseInit(&wc, pParse); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1130 | whereSplit(&wc, pWhere); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1131 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1132 | /* Allocate and initialize the WhereInfo structure that will become the |
| 1133 | ** return value. |
| 1134 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1135 | pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel)); |
danielk1977 | 132872b | 2004-05-10 10:37:18 +0000 | [diff] [blame] | 1136 | if( sqlite3_malloc_failed ){ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 1137 | goto whereBeginNoMem; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1138 | } |
| 1139 | pWInfo->pParse = pParse; |
| 1140 | pWInfo->pTabList = pTabList; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1141 | pWInfo->iBreak = sqlite3VdbeMakeLabel(v); |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 1142 | |
| 1143 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 1144 | ** expression and either jump over all of the code or fall thru. |
| 1145 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1146 | if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){ |
| 1147 | sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 1148 | pWhere = 0; |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 1149 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1150 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1151 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 1152 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 1153 | ** want to analyze these virtual terms, so start analyzing at the end |
| 1154 | ** and work forward so that they added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1155 | */ |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1156 | for(i=0; i<pTabList->nSrc; i++){ |
| 1157 | createMask(&maskSet, pTabList->a[i].iCursor); |
| 1158 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1159 | for(i=wc.nTerm-1; i>=0; i--){ |
| 1160 | exprAnalyze(pTabList, &maskSet, &wc.a[i]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1163 | /* Chose the best index to use for each table in the FROM clause. |
| 1164 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1165 | ** This loop fills in the following fields: |
| 1166 | ** |
| 1167 | ** pWInfo->a[].pIdx The index to use for this level of the loop. |
| 1168 | ** pWInfo->a[].flags WHERE_xxx flags associated with pIdx |
| 1169 | ** pWInfo->a[].nEq The number of == and IN constraints |
| 1170 | ** pWInfo->a[].iFrom When term of the FROM clause is being coded |
| 1171 | ** pWInfo->a[].iTabCur The VDBE cursor for the database table |
| 1172 | ** pWInfo->a[].iIdxCur The VDBE cursor for the index |
| 1173 | ** |
| 1174 | ** This loop also figures out the nesting order of tables in the FROM |
| 1175 | ** clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1176 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1177 | notReady = ~(Bitmask)0; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1178 | pTabItem = pTabList->a; |
| 1179 | pLevel = pWInfo->a; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1180 | for(i=iFrom=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){ |
| 1181 | Index *pIdx; /* Index for FROM table at pTabItem */ |
| 1182 | int flags; /* Flags asssociated with pIdx */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1183 | int nEq; /* Number of == or IN constraints */ |
| 1184 | double cost; /* The cost for pIdx */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1185 | int j; /* For looping over FROM tables */ |
| 1186 | Index *pBest = 0; /* The best index seen so far */ |
| 1187 | int bestFlags = 0; /* Flags associated with pBest */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1188 | int bestNEq = 0; /* nEq associated with pBest */ |
| 1189 | double lowestCost = 1.0e99; /* Cost of the pBest */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1190 | int bestJ; /* The value of j */ |
| 1191 | Bitmask m; /* Bitmask value for j or bestJ */ |
| 1192 | |
| 1193 | for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){ |
| 1194 | m = getMask(&maskSet, pTabItem->iCursor); |
| 1195 | if( (m & notReady)==0 ){ |
| 1196 | if( j==iFrom ) iFrom++; |
| 1197 | continue; |
| 1198 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1199 | cost = bestIndex(pParse, &wc, pTabItem, notReady, |
| 1200 | (j==0 && ppOrderBy) ? *ppOrderBy : 0, |
| 1201 | &pIdx, &flags, &nEq); |
| 1202 | if( cost<lowestCost ){ |
| 1203 | lowestCost = cost; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1204 | pBest = pIdx; |
| 1205 | bestFlags = flags; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1206 | bestNEq = nEq; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1207 | bestJ = j; |
| 1208 | } |
| 1209 | if( (pTabItem->jointype & JT_LEFT)!=0 |
| 1210 | || (j>0 && (pTabItem[-1].jointype & JT_LEFT)!=0) |
| 1211 | ){ |
| 1212 | break; |
| 1213 | } |
| 1214 | } |
| 1215 | if( bestFlags & WHERE_ORDERBY ){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1216 | *ppOrderBy = 0; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1217 | } |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1218 | pLevel->flags = bestFlags; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1219 | pLevel->pIdx = pBest; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1220 | pLevel->nEq = bestNEq; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 1221 | pLevel->aInLoop = 0; |
| 1222 | pLevel->nIn = 0; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1223 | if( pBest ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1224 | pLevel->iIdxCur = pParse->nTab++; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1225 | }else{ |
| 1226 | pLevel->iIdxCur = -1; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1227 | } |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1228 | notReady &= ~getMask(&maskSet, pTabList->a[bestJ].iCursor); |
| 1229 | pLevel->iFrom = bestJ; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1232 | /* Open all tables in the pTabList and any indices selected for |
| 1233 | ** searching those tables. |
| 1234 | */ |
| 1235 | sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ |
| 1236 | pLevel = pWInfo->a; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1237 | for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1238 | Table *pTab; |
| 1239 | Index *pIx; |
| 1240 | int iIdxCur = pLevel->iIdxCur; |
| 1241 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1242 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1243 | pTab = pTabItem->pTab; |
| 1244 | if( pTab->isTransient || pTab->pSelect ) continue; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1245 | if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1246 | sqlite3OpenTableForReading(v, pTabItem->iCursor, pTab); |
| 1247 | } |
| 1248 | pLevel->iTabCur = pTabItem->iCursor; |
| 1249 | if( (pIx = pLevel->pIdx)!=0 ){ |
| 1250 | sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1251 | VdbeComment((v, "# %s", pIx->zName)); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1252 | sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIx->tnum, |
| 1253 | (char*)&pIx->keyInfo, P3_KEYINFO); |
| 1254 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1255 | if( (pLevel->flags & WHERE_IDX_ONLY)!=0 ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1256 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, pIx->nColumn+1); |
| 1257 | } |
| 1258 | sqlite3CodeVerifySchema(pParse, pTab->iDb); |
| 1259 | } |
| 1260 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 1261 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1262 | /* Generate the code to do the search. Each iteration of the for |
| 1263 | ** loop below generates code for a single nested loop of the VM |
| 1264 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1265 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1266 | notReady = ~(Bitmask)0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1267 | for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1268 | int j; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1269 | int iCur = pTabItem->iCursor; /* The VDBE cursor for the table */ |
| 1270 | Index *pIdx; /* The index we will be using */ |
| 1271 | int iIdxCur; /* The VDBE cursor for the index */ |
| 1272 | int omitTable; /* True if we use the index only */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1273 | int bRev; /* True if we need to scan in reverse order */ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1274 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1275 | pTabItem = &pTabList->a[pLevel->iFrom]; |
| 1276 | iCur = pTabItem->iCursor; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1277 | pIdx = pLevel->pIdx; |
| 1278 | iIdxCur = pLevel->iIdxCur; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1279 | bRev = (pLevel->flags & WHERE_REVERSE)!=0; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1280 | omitTable = (pLevel->flags & WHERE_IDX_ONLY)!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1281 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1282 | /* Create labels for the "break" and "continue" instructions |
| 1283 | ** for the current loop. Jump to brk to break out of a loop. |
| 1284 | ** Jump to cont to go immediately to the next iteration of the |
| 1285 | ** loop. |
| 1286 | */ |
| 1287 | brk = pLevel->brk = sqlite3VdbeMakeLabel(v); |
| 1288 | cont = pLevel->cont = sqlite3VdbeMakeLabel(v); |
| 1289 | |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1290 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1291 | ** initialize a memory cell that records if this table matches any |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1292 | ** row of the left table of the join. |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1293 | */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1294 | if( pLevel->iFrom>0 && (pTabItem[-1].jointype & JT_LEFT)!=0 ){ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1295 | if( !pParse->nMem ) pParse->nMem++; |
| 1296 | pLevel->iLeftJoin = pParse->nMem++; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1297 | sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1298 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 1299 | VdbeComment((v, "# init LEFT JOIN no-match flag")); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1302 | if( pLevel->flags & WHERE_ROWID_EQ ){ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1303 | /* Case 1: We can directly reference a single row using an |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1304 | ** equality comparison against the ROWID field. Or |
| 1305 | ** we reference multiple rows using a "rowid IN (...)" |
| 1306 | ** construct. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1307 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1308 | pTerm = findTerm(&wc, iCur, -1, notReady, WO_EQ|WO_IN, 0); |
| 1309 | assert( pTerm!=0 ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1310 | assert( pTerm->pExpr!=0 ); |
| 1311 | assert( pTerm->leftCursor==iCur ); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1312 | assert( omitTable==0 ); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1313 | codeEqualityTerm(pParse, pTerm, brk, pLevel); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1314 | sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1315 | sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk); |
tpoindex | 7a9b161 | 2005-01-03 18:13:18 +0000 | [diff] [blame] | 1316 | VdbeComment((v, "pk")); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1317 | pLevel->op = OP_Noop; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1318 | }else if( pLevel->flags & WHERE_ROWID_RANGE ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1319 | /* Case 2: We have an inequality comparison against the ROWID field. |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1320 | */ |
| 1321 | int testOp = OP_Noop; |
| 1322 | int start; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1323 | WhereTerm *pStart, *pEnd; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1324 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1325 | assert( omitTable==0 ); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1326 | if( pLevel->flags & WHERE_BTM_LIMIT ){ |
| 1327 | pStart = findTerm(&wc, iCur, -1, notReady, WO_GT|WO_GE, 0); |
| 1328 | assert( pStart!=0 ); |
| 1329 | }else{ |
| 1330 | pStart = 0; |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1331 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1332 | if( pLevel->flags & WHERE_TOP_LIMIT ){ |
| 1333 | pEnd = findTerm(&wc, iCur, -1, notReady, WO_LT|WO_LE, 0); |
| 1334 | assert( pEnd!=0 ); |
| 1335 | }else{ |
| 1336 | pEnd = 0; |
| 1337 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1338 | if( bRev ){ |
| 1339 | pTerm = pStart; |
| 1340 | pStart = pEnd; |
| 1341 | pEnd = pTerm; |
| 1342 | } |
| 1343 | if( pStart ){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1344 | Expr *pX; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1345 | pX = pStart->pExpr; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1346 | assert( pX!=0 ); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1347 | assert( pStart->leftCursor==iCur ); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1348 | sqlite3ExprCode(pParse, pX->pRight); |
danielk1977 | d0a6932 | 2005-02-02 01:10:44 +0000 | [diff] [blame] | 1349 | sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LE || pX->op==TK_GT, brk); |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1350 | sqlite3VdbeAddOp(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk); |
tpoindex | 7a9b161 | 2005-01-03 18:13:18 +0000 | [diff] [blame] | 1351 | VdbeComment((v, "pk")); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1352 | disableTerm(pLevel, pStart); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1353 | }else{ |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1354 | sqlite3VdbeAddOp(v, bRev ? OP_Last : OP_Rewind, iCur, brk); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1355 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1356 | if( pEnd ){ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1357 | Expr *pX; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1358 | pX = pEnd->pExpr; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1359 | assert( pX!=0 ); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1360 | assert( pEnd->leftCursor==iCur ); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1361 | sqlite3ExprCode(pParse, pX->pRight); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1362 | pLevel->iMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1363 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1364 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1365 | testOp = bRev ? OP_Le : OP_Ge; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1366 | }else{ |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1367 | testOp = bRev ? OP_Lt : OP_Gt; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1368 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1369 | disableTerm(pLevel, pEnd); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1370 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1371 | start = sqlite3VdbeCurrentAddr(v); |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1372 | pLevel->op = bRev ? OP_Prev : OP_Next; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1373 | pLevel->p1 = iCur; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1374 | pLevel->p2 = start; |
| 1375 | if( testOp!=OP_Noop ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1376 | sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1377 | sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1378 | sqlite3VdbeAddOp(v, testOp, 'n', brk); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 1379 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1380 | }else if( pLevel->flags & WHERE_COLUMN_RANGE ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1381 | /* Case 3: The WHERE clause term that refers to the right-most |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1382 | ** column of the index is an inequality. For example, if |
| 1383 | ** the index is on (x,y,z) and the WHERE clause is of the |
| 1384 | ** form "x=5 AND y<10" then this case is used. Only the |
| 1385 | ** right-most column can be an inequality - the rest must |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1386 | ** use the "==" and "IN" operators. |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 1387 | ** |
| 1388 | ** This case is also used when there are no WHERE clause |
| 1389 | ** constraints but an index is selected anyway, in order |
| 1390 | ** to force the output order to conform to an ORDER BY. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1391 | */ |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1392 | int start; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1393 | int nEq = pLevel->nEq; |
danielk1977 | f7df9cc | 2004-06-16 12:02:47 +0000 | [diff] [blame] | 1394 | int leFlag=0, geFlag=0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1395 | int testOp; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1396 | int topLimit = (pLevel->flags & WHERE_TOP_LIMIT)!=0; |
| 1397 | int btmLimit = (pLevel->flags & WHERE_BTM_LIMIT)!=0; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1398 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1399 | /* Generate code to evaluate all constraint terms using == or IN |
| 1400 | ** and level the values of those terms on the stack. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1401 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1402 | codeAllEqualityTerms(pParse, pLevel, &wc, notReady, brk); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1403 | |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1404 | /* Duplicate the equality term values because they will all be |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1405 | ** used twice: once to make the termination key and once to make the |
| 1406 | ** start key. |
| 1407 | */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1408 | for(j=0; j<nEq; j++){ |
| 1409 | sqlite3VdbeAddOp(v, OP_Dup, nEq-1, 0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | /* Generate the termination key. This is the key value that |
| 1413 | ** will end the search. There is no termination key if there |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1414 | ** are no equality terms and no "X<..." term. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1415 | ** |
| 1416 | ** 2002-Dec-04: On a reverse-order scan, the so-called "termination" |
| 1417 | ** key computed here really ends up being the start key. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1418 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1419 | if( topLimit ){ |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 1420 | Expr *pX; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1421 | int k = pIdx->aiColumn[j]; |
| 1422 | pTerm = findTerm(&wc, iCur, k, notReady, WO_LT|WO_LE, pIdx); |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 1423 | assert( pTerm!=0 ); |
| 1424 | pX = pTerm->pExpr; |
| 1425 | assert( (pTerm->flags & TERM_CODED)==0 ); |
| 1426 | sqlite3ExprCode(pParse, pX->pRight); |
| 1427 | leFlag = pX->op==TK_LE; |
| 1428 | disableTerm(pLevel, pTerm); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1429 | testOp = OP_IdxGE; |
| 1430 | }else{ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1431 | testOp = nEq>0 ? OP_IdxGE : OP_Noop; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1432 | leFlag = 1; |
| 1433 | } |
| 1434 | if( testOp!=OP_Noop ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1435 | int nCol = nEq + topLimit; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1436 | pLevel->iMem = pParse->nMem++; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1437 | buildIndexProbe(v, nCol, brk, pIdx); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1438 | if( bRev ){ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1439 | int op = leFlag ? OP_MoveLe : OP_MoveLt; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1440 | sqlite3VdbeAddOp(v, op, iIdxCur, brk); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1441 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1442 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1443 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1444 | }else if( bRev ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1445 | sqlite3VdbeAddOp(v, OP_Last, iIdxCur, brk); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | /* Generate the start key. This is the key that defines the lower |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1449 | ** bound on the search. There is no start key if there are no |
| 1450 | ** equality terms and if there is no "X>..." term. In |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1451 | ** that case, generate a "Rewind" instruction in place of the |
| 1452 | ** start key search. |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1453 | ** |
| 1454 | ** 2002-Dec-04: In the case of a reverse-order search, the so-called |
| 1455 | ** "start" key really ends up being used as the termination key. |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1456 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1457 | if( btmLimit ){ |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 1458 | Expr *pX; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1459 | int k = pIdx->aiColumn[j]; |
| 1460 | pTerm = findTerm(&wc, iCur, k, notReady, WO_GT|WO_GE, pIdx); |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 1461 | assert( pTerm!=0 ); |
| 1462 | pX = pTerm->pExpr; |
| 1463 | assert( (pTerm->flags & TERM_CODED)==0 ); |
| 1464 | sqlite3ExprCode(pParse, pX->pRight); |
| 1465 | geFlag = pX->op==TK_GE; |
| 1466 | disableTerm(pLevel, pTerm); |
drh | 7900ead | 2001-11-12 13:51:43 +0000 | [diff] [blame] | 1467 | }else{ |
| 1468 | geFlag = 1; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1469 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1470 | if( nEq>0 || btmLimit ){ |
| 1471 | int nCol = nEq + btmLimit; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 1472 | buildIndexProbe(v, nCol, brk, pIdx); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1473 | if( bRev ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1474 | pLevel->iMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1475 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1476 | testOp = OP_IdxLT; |
| 1477 | }else{ |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 1478 | int op = geFlag ? OP_MoveGe : OP_MoveGt; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1479 | sqlite3VdbeAddOp(v, op, iIdxCur, brk); |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1480 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1481 | }else if( bRev ){ |
drh | c045ec5 | 2002-12-04 20:01:06 +0000 | [diff] [blame] | 1482 | testOp = OP_Noop; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1483 | }else{ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1484 | sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, brk); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* Generate the the top of the loop. If there is a termination |
| 1488 | ** key we have to test for that key and abort at the top of the |
| 1489 | ** loop. |
| 1490 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1491 | start = sqlite3VdbeCurrentAddr(v); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1492 | if( testOp!=OP_Noop ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1493 | sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1494 | sqlite3VdbeAddOp(v, testOp, iIdxCur, brk); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1495 | if( (leFlag && !bRev) || (!geFlag && bRev) ){ |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 1496 | sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC); |
| 1497 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1498 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1499 | sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1500 | sqlite3VdbeAddOp(v, OP_IdxIsNull, nEq + topLimit, cont); |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1501 | if( !omitTable ){ |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1502 | sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0); |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 1503 | sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /* Record the instruction used to terminate the loop. |
| 1507 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1508 | pLevel->op = bRev ? OP_Prev : OP_Next; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1509 | pLevel->p1 = iIdxCur; |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 1510 | pLevel->p2 = start; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame^] | 1511 | }else if( pLevel->flags & WHERE_COLUMN_EQ ){ |
| 1512 | /* Case 4: There is an index and all terms of the WHERE clause that |
| 1513 | ** refer to the index using the "==" or "IN" operators. |
| 1514 | */ |
| 1515 | int start; |
| 1516 | int nEq = pLevel->nEq; |
| 1517 | |
| 1518 | /* Generate code to evaluate all constraint terms using == or IN |
| 1519 | ** and level the values of those terms on the stack. |
| 1520 | */ |
| 1521 | codeAllEqualityTerms(pParse, pLevel, &wc, notReady, brk); |
| 1522 | |
| 1523 | /* Generate a single key that will be used to both start and terminate |
| 1524 | ** the search |
| 1525 | */ |
| 1526 | buildIndexProbe(v, nEq, brk, pIdx); |
| 1527 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
| 1528 | |
| 1529 | /* Generate code (1) to move to the first matching element of the table. |
| 1530 | ** Then generate code (2) that jumps to "brk" after the cursor is past |
| 1531 | ** the last matching element of the table. The code (1) is executed |
| 1532 | ** once to initialize the search, the code (2) is executed before each |
| 1533 | ** iteration of the scan to see if the scan has finished. */ |
| 1534 | if( bRev ){ |
| 1535 | /* Scan in reverse order */ |
| 1536 | sqlite3VdbeAddOp(v, OP_MoveLe, iIdxCur, brk); |
| 1537 | start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 1538 | sqlite3VdbeAddOp(v, OP_IdxLT, iIdxCur, brk); |
| 1539 | pLevel->op = OP_Prev; |
| 1540 | }else{ |
| 1541 | /* Scan in the forward order */ |
| 1542 | sqlite3VdbeAddOp(v, OP_MoveGe, iIdxCur, brk); |
| 1543 | start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 1544 | sqlite3VdbeOp3(v, OP_IdxGE, iIdxCur, brk, "+", P3_STATIC); |
| 1545 | pLevel->op = OP_Next; |
| 1546 | } |
| 1547 | sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0); |
| 1548 | sqlite3VdbeAddOp(v, OP_IdxIsNull, nEq, cont); |
| 1549 | if( !omitTable ){ |
| 1550 | sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0); |
| 1551 | sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); |
| 1552 | } |
| 1553 | pLevel->p1 = iIdxCur; |
| 1554 | pLevel->p2 = start; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1555 | }else{ |
| 1556 | /* Case 5: There is no usable index. We must do a complete |
| 1557 | ** scan of the entire table. |
| 1558 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1559 | int opRewind; |
| 1560 | |
| 1561 | assert( omitTable==0 ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1562 | if( bRev ){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1563 | opRewind = OP_Last; |
| 1564 | pLevel->op = OP_Prev; |
| 1565 | }else{ |
| 1566 | opRewind = OP_Rewind; |
| 1567 | pLevel->op = OP_Next; |
| 1568 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1569 | pLevel->p1 = iCur; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1570 | pLevel->p2 = 1 + sqlite3VdbeAddOp(v, opRewind, iCur, brk); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1571 | } |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1572 | notReady &= ~getMask(&maskSet, iCur); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1573 | |
| 1574 | /* Insert code to test every subexpression that can be completely |
| 1575 | ** computed using the current set of tables. |
| 1576 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1577 | for(pTerm=wc.a, j=wc.nTerm; j>0; j--, pTerm++){ |
| 1578 | Expr *pE; |
| 1579 | if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1580 | if( (pTerm->prereqAll & notReady)!=0 ) continue; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1581 | pE = pTerm->pExpr; |
| 1582 | assert( pE!=0 ); |
drh | 392e597 | 2005-07-08 14:14:22 +0000 | [diff] [blame] | 1583 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
drh | 1f16230 | 2002-10-27 19:35:33 +0000 | [diff] [blame] | 1584 | continue; |
| 1585 | } |
drh | 392e597 | 2005-07-08 14:14:22 +0000 | [diff] [blame] | 1586 | sqlite3ExprIfFalse(pParse, pE, cont, 1); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1587 | pTerm->flags |= TERM_CODED; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1588 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1589 | |
| 1590 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 1591 | ** at least one row of the right table has matched the left table. |
| 1592 | */ |
| 1593 | if( pLevel->iLeftJoin ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1594 | pLevel->top = sqlite3VdbeCurrentAddr(v); |
| 1595 | sqlite3VdbeAddOp(v, OP_Integer, 1, 0); |
| 1596 | sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 1597 | VdbeComment((v, "# record LEFT JOIN hit")); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1598 | for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1599 | if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1600 | if( (pTerm->prereqAll & notReady)!=0 ) continue; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1601 | assert( pTerm->pExpr ); |
| 1602 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, 1); |
| 1603 | pTerm->flags |= TERM_CODED; |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 1604 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1605 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1606 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 1607 | |
| 1608 | #ifdef SQLITE_TEST /* For testing and debugging use only */ |
| 1609 | /* Record in the query plan information about the current table |
| 1610 | ** and the index used to access it (if any). If the table itself |
| 1611 | ** is not used, its name is just '{}'. If no index is used |
| 1612 | ** the index is listed as "{}". If the primary key is used the |
| 1613 | ** index name is '*'. |
| 1614 | */ |
| 1615 | for(i=0; i<pTabList->nSrc; i++){ |
| 1616 | char *z; |
| 1617 | int n; |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 1618 | pLevel = &pWInfo->a[i]; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1619 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 1620 | z = pTabItem->zAlias; |
| 1621 | if( z==0 ) z = pTabItem->pTab->zName; |
| 1622 | n = strlen(z); |
| 1623 | if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){ |
| 1624 | if( pLevel->flags & WHERE_IDX_ONLY ){ |
| 1625 | strcpy(&sqlite3_query_plan[nQPlan], "{}"); |
| 1626 | nQPlan += 2; |
| 1627 | }else{ |
| 1628 | strcpy(&sqlite3_query_plan[nQPlan], z); |
| 1629 | nQPlan += n; |
| 1630 | } |
| 1631 | sqlite3_query_plan[nQPlan++] = ' '; |
| 1632 | } |
| 1633 | if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ |
| 1634 | strcpy(&sqlite3_query_plan[nQPlan], "* "); |
| 1635 | nQPlan += 2; |
| 1636 | }else if( pLevel->pIdx==0 ){ |
| 1637 | strcpy(&sqlite3_query_plan[nQPlan], "{} "); |
| 1638 | nQPlan += 3; |
| 1639 | }else{ |
| 1640 | n = strlen(pLevel->pIdx->zName); |
| 1641 | if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){ |
| 1642 | strcpy(&sqlite3_query_plan[nQPlan], pLevel->pIdx->zName); |
| 1643 | nQPlan += n; |
| 1644 | sqlite3_query_plan[nQPlan++] = ' '; |
| 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){ |
| 1649 | sqlite3_query_plan[--nQPlan] = 0; |
| 1650 | } |
| 1651 | sqlite3_query_plan[nQPlan] = 0; |
| 1652 | nQPlan = 0; |
| 1653 | #endif /* SQLITE_TEST // Testing and debugging use only */ |
| 1654 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1655 | /* Record the continuation address in the WhereInfo structure. Then |
| 1656 | ** clean up and return. |
| 1657 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1658 | pWInfo->iContinue = cont; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1659 | whereClauseClear(&wc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1660 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 1661 | |
| 1662 | /* Jump here if malloc fails */ |
| 1663 | whereBeginNoMem: |
| 1664 | whereClauseClear(&wc); |
| 1665 | sqliteFree(pWInfo); |
| 1666 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 1670 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1671 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1672 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1673 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1674 | Vdbe *v = pWInfo->pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1675 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1676 | WhereLevel *pLevel; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1677 | SrcList *pTabList = pWInfo->pTabList; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1678 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1679 | /* Generate loop termination code. |
| 1680 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1681 | for(i=pTabList->nSrc-1; i>=0; i--){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1682 | pLevel = &pWInfo->a[i]; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1683 | sqlite3VdbeResolveLabel(v, pLevel->cont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1684 | if( pLevel->op!=OP_Noop ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1685 | sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1686 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1687 | sqlite3VdbeResolveLabel(v, pLevel->brk); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 1688 | if( pLevel->nIn ){ |
| 1689 | int *a; |
| 1690 | int j; |
| 1691 | for(j=pLevel->nIn, a=&pLevel->aInLoop[j*3-3]; j>0; j--, a-=3){ |
| 1692 | sqlite3VdbeAddOp(v, a[0], a[1], a[2]); |
| 1693 | } |
| 1694 | sqliteFree(pLevel->aInLoop); |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 1695 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1696 | if( pLevel->iLeftJoin ){ |
| 1697 | int addr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1698 | addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1699 | sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iIdxCur>=0)); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1700 | sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1701 | if( pLevel->iIdxCur>=0 ){ |
| 1702 | sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iIdxCur, 0); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 1703 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1704 | sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1705 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1706 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1707 | |
| 1708 | /* The "break" point is here, just past the end of the outer loop. |
| 1709 | ** Set it. |
| 1710 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1711 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1712 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1713 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1714 | */ |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 1715 | for(i=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){ |
| 1716 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1717 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1718 | assert( pTab!=0 ); |
| 1719 | if( pTab->isTransient || pTab->pSelect ) continue; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1720 | if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1721 | sqlite3VdbeAddOp(v, OP_Close, pTabItem->iCursor, 0); |
| 1722 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1723 | if( pLevel->pIdx!=0 ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1724 | sqlite3VdbeAddOp(v, OP_Close, pLevel->iIdxCur, 0); |
| 1725 | } |
| 1726 | |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 1727 | /* Make cursor substitutions for cases where we want to use |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1728 | ** just the index and never reference the table. |
| 1729 | ** |
| 1730 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 1731 | ** sqlite3WhereEnd will have created code that references the table |
| 1732 | ** directly. This loop scans all that code looking for opcodes |
| 1733 | ** that reference the table and converts them into opcodes that |
| 1734 | ** reference the index. |
| 1735 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 1736 | if( pLevel->flags & WHERE_IDX_ONLY ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1737 | int i, j, last; |
| 1738 | VdbeOp *pOp; |
| 1739 | Index *pIdx = pLevel->pIdx; |
| 1740 | |
| 1741 | assert( pIdx!=0 ); |
| 1742 | pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); |
| 1743 | last = sqlite3VdbeCurrentAddr(v); |
| 1744 | for(i=pWInfo->iTop; i<last; i++, pOp++){ |
| 1745 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 1746 | if( pOp->opcode==OP_Column ){ |
| 1747 | pOp->p1 = pLevel->iIdxCur; |
| 1748 | for(j=0; j<pIdx->nColumn; j++){ |
| 1749 | if( pOp->p2==pIdx->aiColumn[j] ){ |
| 1750 | pOp->p2 = j; |
| 1751 | break; |
| 1752 | } |
| 1753 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1754 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1755 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1756 | pOp->opcode = OP_IdxRowid; |
danielk1977 | 6c18b6e | 2005-01-30 09:17:58 +0000 | [diff] [blame] | 1757 | }else if( pOp->opcode==OP_NullRow ){ |
| 1758 | pOp->opcode = OP_Noop; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1761 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1762 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 1763 | |
| 1764 | /* Final cleanup |
| 1765 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1766 | sqliteFree(pWInfo); |
| 1767 | return; |
| 1768 | } |