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