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