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