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 |
| 13 | ** the WHERE clause of SQL statements. Also found here are subroutines |
| 14 | ** to generate VDBE code to evaluate expressions. |
| 15 | ** |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 16 | ** $Id: where.c,v 1.24 2001/11/04 18:32:48 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | |
| 20 | /* |
| 21 | ** The query generator uses an array of instances of this structure to |
| 22 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 23 | ** clause subexpression is separated from the others by an AND operator. |
| 24 | */ |
| 25 | typedef struct ExprInfo ExprInfo; |
| 26 | struct ExprInfo { |
| 27 | Expr *p; /* Pointer to the subexpression */ |
| 28 | int indexable; /* True if this subexprssion is usable by an index */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 29 | int idxLeft; /* p->pLeft is a column in this table number. -1 if |
| 30 | ** p->pLeft is not the column of any table */ |
| 31 | int idxRight; /* p->pRight is a column in this table number. -1 if |
| 32 | ** p->pRight is not the column of any table */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 33 | unsigned prereqLeft; /* Tables referenced by p->pLeft */ |
| 34 | unsigned prereqRight; /* Tables referenced by p->pRight */ |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | ** Determine the number of elements in an array. |
| 39 | */ |
| 40 | #define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0])) |
| 41 | |
| 42 | /* |
| 43 | ** This routine is used to divide the WHERE expression into subexpressions |
| 44 | ** separated by the AND operator. |
| 45 | ** |
| 46 | ** aSlot[] is an array of subexpressions structures. |
| 47 | ** There are nSlot spaces left in this array. This routine attempts to |
| 48 | ** split pExpr into subexpressions and fills aSlot[] with those subexpressions. |
| 49 | ** The return value is the number of slots filled. |
| 50 | */ |
| 51 | static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){ |
| 52 | int cnt = 0; |
| 53 | if( pExpr==0 || nSlot<1 ) return 0; |
| 54 | if( nSlot==1 || pExpr->op!=TK_AND ){ |
| 55 | aSlot[0].p = pExpr; |
| 56 | return 1; |
| 57 | } |
| 58 | if( pExpr->pLeft->op!=TK_AND ){ |
| 59 | aSlot[0].p = pExpr->pLeft; |
| 60 | cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight); |
| 61 | }else{ |
| 62 | cnt = exprSplit(nSlot, aSlot, pExpr->pRight); |
| 63 | cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft); |
| 64 | } |
| 65 | return cnt; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | ** This routine walks (recursively) an expression tree and generates |
| 70 | ** a bitmask indicating which tables are used in that expression |
| 71 | ** tree. Bit 0 of the mask is set if table 0 is used. But 1 is set |
| 72 | ** if table 1 is used. And so forth. |
| 73 | ** |
| 74 | ** In order for this routine to work, the calling function must have |
| 75 | ** previously invoked sqliteExprResolveIds() on the expression. See |
| 76 | ** the header comment on that routine for additional information. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 77 | ** |
| 78 | ** "base" is the cursor number (the value of the iTable field) that |
| 79 | ** corresponds to the first entry in the table list. This is the |
| 80 | ** same as pParse->nTab. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 81 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 82 | static int exprTableUsage(int base, Expr *p){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 83 | unsigned int mask = 0; |
| 84 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 85 | if( p->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 86 | return 1<< (p->iTable - base); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 87 | } |
| 88 | if( p->pRight ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 89 | mask = exprTableUsage(base, p->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 90 | } |
| 91 | if( p->pLeft ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 92 | mask |= exprTableUsage(base, p->pLeft); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 93 | } |
| 94 | return mask; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | ** The input to this routine is an ExprInfo structure with only the |
| 99 | ** "p" field filled in. The job of this routine is to analyze the |
| 100 | ** subexpression and populate all the other fields of the ExprInfo |
| 101 | ** structure. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 102 | ** |
| 103 | ** "base" is the cursor number (the value of the iTable field) that |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 104 | ** corresponds to the first entry in the table list. This is the |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 105 | ** same as pParse->nTab. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 106 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 107 | static void exprAnalyze(int base, ExprInfo *pInfo){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 108 | Expr *pExpr = pInfo->p; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 109 | pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft); |
| 110 | pInfo->prereqRight = exprTableUsage(base, pExpr->pRight); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 111 | pInfo->indexable = 0; |
| 112 | pInfo->idxLeft = -1; |
| 113 | pInfo->idxRight = -1; |
| 114 | if( pExpr->op==TK_EQ && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 115 | if( pExpr->pRight->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 116 | pInfo->idxRight = pExpr->pRight->iTable - base; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 117 | pInfo->indexable = 1; |
| 118 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 119 | if( pExpr->pLeft->op==TK_COLUMN ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 120 | pInfo->idxLeft = pExpr->pLeft->iTable - base; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 121 | pInfo->indexable = 1; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | ** Generating the beginning of the loop used for WHERE clause processing. |
| 128 | ** The return value is a pointer to an (opaque) structure that contains |
| 129 | ** information needed to terminate the loop. Later, the calling routine |
| 130 | ** should invoke sqliteWhereEnd() with the return value of this function |
| 131 | ** in order to complete the WHERE clause processing. |
| 132 | ** |
| 133 | ** If an error occurs, this routine returns NULL. |
| 134 | */ |
| 135 | WhereInfo *sqliteWhereBegin( |
| 136 | Parse *pParse, /* The parser context */ |
| 137 | IdList *pTabList, /* A list of all tables */ |
| 138 | Expr *pWhere, /* The WHERE clause */ |
| 139 | int pushKey /* If TRUE, leave the table key on the stack */ |
| 140 | ){ |
| 141 | int i; /* Loop counter */ |
| 142 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 143 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 144 | int brk, cont; /* Addresses used during code generation */ |
| 145 | int *aOrder; /* Order in which pTabList entries are searched */ |
| 146 | int nExpr; /* Number of subexpressions in the WHERE clause */ |
| 147 | int loopMask; /* One bit set for each outer loop */ |
| 148 | int haveKey; /* True if KEY is on the stack */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 149 | int base; /* First available index for OP_Open opcodes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 150 | Index *aIdx[32]; /* Index to use on each nested loop. */ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 151 | int aDirect[32]; /* If TRUE, then index this table using ROWID */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 152 | ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */ |
| 153 | |
| 154 | /* Allocate space for aOrder[]. */ |
| 155 | aOrder = sqliteMalloc( sizeof(int) * pTabList->nId ); |
| 156 | |
| 157 | /* Allocate and initialize the WhereInfo structure that will become the |
| 158 | ** return value. |
| 159 | */ |
| 160 | pWInfo = sqliteMalloc( sizeof(WhereInfo) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 161 | if( sqlite_malloc_failed ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 162 | sqliteFree(aOrder); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 163 | sqliteFree(pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | pWInfo->pParse = pParse; |
| 167 | pWInfo->pTabList = pTabList; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 168 | base = pWInfo->base = pParse->nTab; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 169 | |
| 170 | /* Split the WHERE clause into as many as 32 separate subexpressions |
| 171 | ** where each subexpression is separated by an AND operator. Any additional |
| 172 | ** subexpressions are attached in the aExpr[32] and will not enter |
| 173 | ** into the query optimizer computations. 32 is chosen as the cutoff |
| 174 | ** since that is the number of bits in an integer that we use for an |
| 175 | ** expression-used mask. |
| 176 | */ |
| 177 | memset(aExpr, 0, sizeof(aExpr)); |
| 178 | nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere); |
| 179 | |
| 180 | /* Analyze all of the subexpressions. |
| 181 | */ |
| 182 | for(i=0; i<nExpr; i++){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 183 | exprAnalyze(pParse->nTab, &aExpr[i]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* Figure out a good nesting order for the tables. aOrder[0] will |
| 187 | ** be the index in pTabList of the outermost table. aOrder[1] will |
| 188 | ** be the first nested loop and so on. aOrder[pTabList->nId-1] will |
| 189 | ** be the innermost loop. |
| 190 | ** |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 191 | ** Someday will put in a good algorithm here to reorder the loops |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 192 | ** for an effiecient query. But for now, just use whatever order the |
| 193 | ** tables appear in in the pTabList. |
| 194 | */ |
| 195 | for(i=0; i<pTabList->nId; i++){ |
| 196 | aOrder[i] = i; |
| 197 | } |
| 198 | |
| 199 | /* Figure out what index to use (if any) for each nested loop. |
| 200 | ** Make aIdx[i] point to the index to use for the i-th nested loop |
| 201 | ** where i==0 is the outer loop and i==pTabList->nId-1 is the inner |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 202 | ** loop. If the expression uses only the ROWID field, then set |
| 203 | ** aDirect[i] to 1. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 204 | ** |
| 205 | ** Actually, if there are more than 32 tables in the join, only the |
| 206 | ** first 32 tables are candidates for indices. |
| 207 | */ |
| 208 | loopMask = 0; |
| 209 | for(i=0; i<pTabList->nId && i<ARRAYSIZE(aIdx); i++){ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 210 | int j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | int idx = aOrder[i]; |
| 212 | Table *pTab = pTabList->a[idx].pTab; |
| 213 | Index *pIdx; |
| 214 | Index *pBestIdx = 0; |
| 215 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 216 | /* Check to see if there is an expression that uses only the |
| 217 | ** ROWID field of this table. If so, set aDirect[i] to 1. |
| 218 | ** If not, set aDirect[i] to 0. |
| 219 | */ |
| 220 | aDirect[i] = 0; |
| 221 | for(j=0; j<nExpr; j++){ |
| 222 | if( aExpr[j].idxLeft==idx && aExpr[j].p->pLeft->iColumn<0 |
| 223 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
| 224 | aDirect[i] = 1; |
| 225 | break; |
| 226 | } |
| 227 | if( aExpr[j].idxRight==idx && aExpr[j].p->pRight->iColumn<0 |
| 228 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
| 229 | aDirect[i] = 1; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | if( aDirect[i] ){ |
| 234 | loopMask |= 1<<idx; |
| 235 | aIdx[i] = 0; |
| 236 | continue; |
| 237 | } |
| 238 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 239 | /* Do a search for usable indices. Leave pBestIdx pointing to |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 240 | ** the most specific usable index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 241 | ** |
| 242 | ** "Most specific" means that pBestIdx is the usable index that |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 243 | ** has the largest value for nColumn. A usable index is one for |
| 244 | ** which there are subexpressions to compute every column of the |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 245 | ** index. |
| 246 | */ |
| 247 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 248 | int columnMask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 249 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 250 | if( pIdx->nColumn>32 ) continue; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 251 | for(j=0; j<nExpr; j++){ |
| 252 | if( aExpr[j].idxLeft==idx |
| 253 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 254 | int iColumn = aExpr[j].p->pLeft->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 255 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 256 | for(k=0; k<pIdx->nColumn; k++){ |
| 257 | if( pIdx->aiColumn[k]==iColumn ){ |
| 258 | columnMask |= 1<<k; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | if( aExpr[j].idxRight==idx |
| 264 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 265 | int iColumn = aExpr[j].p->pRight->iColumn; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 266 | int k; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 267 | for(k=0; k<pIdx->nColumn; k++){ |
| 268 | if( pIdx->aiColumn[k]==iColumn ){ |
| 269 | columnMask |= 1<<k; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 275 | if( columnMask + 1 == (1<<pIdx->nColumn) ){ |
| 276 | if( pBestIdx==0 || pBestIdx->nColumn<pIdx->nColumn ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 277 | pBestIdx = pIdx; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | aIdx[i] = pBestIdx; |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 282 | loopMask |= 1<<idx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* Open all tables in the pTabList and all indices in aIdx[]. |
| 286 | */ |
| 287 | for(i=0; i<pTabList->nId; i++){ |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 288 | int openOp; |
| 289 | Table *pTab; |
| 290 | |
| 291 | pTab = pTabList->a[i].pTab; |
| 292 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 293 | sqliteVdbeAddOp(v, openOp, base+i, pTab->tnum); |
| 294 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 295 | if( i==0 && !pParse->schemaVerified && |
| 296 | (pParse->db->flags & SQLITE_InTrans)==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 297 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 298 | pParse->schemaVerified = 1; |
| 299 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 300 | if( i<ARRAYSIZE(aIdx) && aIdx[i]!=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 301 | sqliteVdbeAddOp(v, openOp, base+pTabList->nId+i, aIdx[i]->tnum); |
| 302 | sqliteVdbeChangeP3(v, -1, aIdx[i]->zName, P3_STATIC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 305 | memcpy(pWInfo->aIdx, aIdx, sizeof(aIdx)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 306 | |
| 307 | /* Generate the code to do the search |
| 308 | */ |
| 309 | pWInfo->iBreak = brk = sqliteVdbeMakeLabel(v); |
| 310 | loopMask = 0; |
| 311 | for(i=0; i<pTabList->nId; i++){ |
| 312 | int j, k; |
| 313 | int idx = aOrder[i]; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 314 | int goDirect; |
| 315 | Index *pIdx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 316 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 317 | if( i<ARRAYSIZE(aIdx) ){ |
| 318 | pIdx = aIdx[i]; |
| 319 | goDirect = aDirect[i]; |
| 320 | }else{ |
| 321 | pIdx = 0; |
| 322 | goDirect = 0; |
| 323 | } |
| 324 | |
| 325 | if( goDirect ){ |
| 326 | /* Case 1: We can directly reference a single row using the ROWID field. |
| 327 | */ |
| 328 | cont = brk; |
| 329 | for(k=0; k<nExpr; k++){ |
| 330 | if( aExpr[k].p==0 ) continue; |
| 331 | if( aExpr[k].idxLeft==idx |
| 332 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 333 | && aExpr[k].p->pLeft->iColumn<0 |
| 334 | ){ |
| 335 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 336 | aExpr[k].p = 0; |
| 337 | break; |
| 338 | } |
| 339 | if( aExpr[k].idxRight==idx |
| 340 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 341 | && aExpr[k].p->pRight->iColumn<0 |
| 342 | ){ |
| 343 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 344 | aExpr[k].p = 0; |
| 345 | break; |
| 346 | } |
| 347 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 348 | sqliteVdbeAddOp(v, OP_AddImm, 0, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 349 | if( i==pTabList->nId-1 && pushKey ){ |
| 350 | haveKey = 1; |
| 351 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 352 | sqliteVdbeAddOp(v, OP_NotFound, base+idx, brk); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 353 | haveKey = 0; |
| 354 | } |
| 355 | }else if( pIdx==0 ){ |
| 356 | /* Case 2: There was no usable index. We must do a complete |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 357 | ** scan of the table. |
| 358 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 359 | sqliteVdbeAddOp(v, OP_Rewind, base+idx, 0); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 360 | cont = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 361 | sqliteVdbeResolveLabel(v, cont); |
| 362 | sqliteVdbeAddOp(v, OP_Next, base+idx, brk); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 363 | haveKey = 0; |
| 364 | }else{ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 365 | /* Case 3: We do have a usable index in pIdx. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 366 | */ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 367 | cont = sqliteVdbeMakeLabel(v); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 368 | for(j=0; j<pIdx->nColumn; j++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 369 | for(k=0; k<nExpr; k++){ |
| 370 | if( aExpr[k].p==0 ) continue; |
| 371 | if( aExpr[k].idxLeft==idx |
| 372 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 373 | && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 374 | ){ |
| 375 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 376 | aExpr[k].p = 0; |
| 377 | break; |
| 378 | } |
| 379 | if( aExpr[k].idxRight==idx |
| 380 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 381 | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 382 | ){ |
| 383 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 384 | aExpr[k].p = 0; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 389 | sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nColumn, 0); |
| 390 | sqliteVdbeAddOp(v, OP_BeginIdx, base+pTabList->nId+i, 0); |
| 391 | sqliteVdbeResolveLabel(v, cont); |
| 392 | sqliteVdbeAddOp(v, OP_NextIdx, base+pTabList->nId+i, brk); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 393 | if( i==pTabList->nId-1 && pushKey ){ |
| 394 | haveKey = 1; |
| 395 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 396 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 397 | haveKey = 0; |
| 398 | } |
| 399 | } |
| 400 | loopMask |= 1<<idx; |
| 401 | |
| 402 | /* Insert code to test every subexpression that can be completely |
| 403 | ** computed using the current set of tables. |
| 404 | */ |
| 405 | for(j=0; j<nExpr; j++){ |
| 406 | if( aExpr[j].p==0 ) continue; |
| 407 | if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue; |
| 408 | if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue; |
| 409 | if( haveKey ){ |
drh | 573bd27 | 2001-02-19 23:23:38 +0000 | [diff] [blame] | 410 | haveKey = 0; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 411 | sqliteVdbeAddOp(v, OP_MoveTo, base+idx, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 412 | } |
| 413 | sqliteExprIfFalse(pParse, aExpr[j].p, cont); |
| 414 | aExpr[j].p = 0; |
| 415 | } |
| 416 | brk = cont; |
| 417 | } |
| 418 | pWInfo->iContinue = cont; |
| 419 | if( pushKey && !haveKey ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 420 | sqliteVdbeAddOp(v, OP_Recno, base, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 421 | } |
| 422 | sqliteFree(aOrder); |
| 423 | return pWInfo; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | ** Generate the end of the WHERE loop. |
| 428 | */ |
| 429 | void sqliteWhereEnd(WhereInfo *pWInfo){ |
| 430 | Vdbe *v = pWInfo->pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 431 | int i; |
| 432 | int brk = pWInfo->iBreak; |
| 433 | int base = pWInfo->base; |
| 434 | |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 435 | sqliteVdbeAddOp(v, OP_Goto, 0, pWInfo->iContinue); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 436 | for(i=0; i<pWInfo->pTabList->nId; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 437 | sqliteVdbeResolveLabel(v, brk); |
| 438 | sqliteVdbeAddOp(v, OP_Close, base+i, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 439 | brk = 0; |
| 440 | if( i<ARRAYSIZE(pWInfo->aIdx) && pWInfo->aIdx[i]!=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 441 | sqliteVdbeAddOp(v, OP_Close, base+pWInfo->pTabList->nId+i, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | if( brk!=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 445 | sqliteVdbeResolveLabel(v, brk); |
| 446 | sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 447 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 448 | sqliteFree(pWInfo); |
| 449 | return; |
| 450 | } |