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