1 /* 2 ** 2015-06-08 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 ** This file was originally part of where.c but was split out to improve 16 ** readability and editabiliity. This file contains utility routines for 17 ** analyzing Expr objects in the WHERE clause. 18 */ 19 #include "sqliteInt.h" 20 #include "whereInt.h" 21 22 /* Forward declarations */ 23 static void exprAnalyze(SrcList*, WhereClause*, int); 24 25 /* 26 ** Deallocate all memory associated with a WhereOrInfo object. 27 */ 28 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ 29 sqlite3WhereClauseClear(&p->wc); 30 sqlite3DbFree(db, p); 31 } 32 33 /* 34 ** Deallocate all memory associated with a WhereAndInfo object. 35 */ 36 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ 37 sqlite3WhereClauseClear(&p->wc); 38 sqlite3DbFree(db, p); 39 } 40 41 /* 42 ** Add a single new WhereTerm entry to the WhereClause object pWC. 43 ** The new WhereTerm object is constructed from Expr p and with wtFlags. 44 ** The index in pWC->a[] of the new WhereTerm is returned on success. 45 ** 0 is returned if the new WhereTerm could not be added due to a memory 46 ** allocation error. The memory allocation failure will be recorded in 47 ** the db->mallocFailed flag so that higher-level functions can detect it. 48 ** 49 ** This routine will increase the size of the pWC->a[] array as necessary. 50 ** 51 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility 52 ** for freeing the expression p is assumed by the WhereClause object pWC. 53 ** This is true even if this routine fails to allocate a new WhereTerm. 54 ** 55 ** WARNING: This routine might reallocate the space used to store 56 ** WhereTerms. All pointers to WhereTerms should be invalidated after 57 ** calling this routine. Such pointers may be reinitialized by referencing 58 ** the pWC->a[] array. 59 */ 60 static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ 61 WhereTerm *pTerm; 62 int idx; 63 testcase( wtFlags & TERM_VIRTUAL ); 64 if( pWC->nTerm>=pWC->nSlot ){ 65 WhereTerm *pOld = pWC->a; 66 sqlite3 *db = pWC->pWInfo->pParse->db; 67 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); 68 if( pWC->a==0 ){ 69 if( wtFlags & TERM_DYNAMIC ){ 70 sqlite3ExprDelete(db, p); 71 } 72 pWC->a = pOld; 73 return 0; 74 } 75 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); 76 if( pOld!=pWC->aStatic ){ 77 sqlite3DbFree(db, pOld); 78 } 79 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); 80 } 81 pTerm = &pWC->a[idx = pWC->nTerm++]; 82 if( p && ExprHasProperty(p, EP_Unlikely) ){ 83 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; 84 }else{ 85 pTerm->truthProb = 1; 86 } 87 pTerm->pExpr = sqlite3ExprSkipCollate(p); 88 pTerm->wtFlags = wtFlags; 89 pTerm->pWC = pWC; 90 pTerm->iParent = -1; 91 memset(&pTerm->eOperator, 0, 92 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)); 93 return idx; 94 } 95 96 /* 97 ** Return TRUE if the given operator is one of the operators that is 98 ** allowed for an indexable WHERE clause term. The allowed operators are 99 ** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL" 100 */ 101 static int allowedOp(int op){ 102 assert( TK_GT>TK_EQ && TK_GT<TK_GE ); 103 assert( TK_LT>TK_EQ && TK_LT<TK_GE ); 104 assert( TK_LE>TK_EQ && TK_LE<TK_GE ); 105 assert( TK_GE==TK_EQ+4 ); 106 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS; 107 } 108 109 /* 110 ** Commute a comparison operator. Expressions of the form "X op Y" 111 ** are converted into "Y op X". 112 ** 113 ** If left/right precedence rules come into play when determining the 114 ** collating sequence, then COLLATE operators are adjusted to ensure 115 ** that the collating sequence does not change. For example: 116 ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on 117 ** the left hand side of a comparison overrides any collation sequence 118 ** attached to the right. For the same reason the EP_Collate flag 119 ** is not commuted. 120 */ 121 static void exprCommute(Parse *pParse, Expr *pExpr){ 122 u16 expRight = (pExpr->pRight->flags & EP_Collate); 123 u16 expLeft = (pExpr->pLeft->flags & EP_Collate); 124 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); 125 if( expRight==expLeft ){ 126 /* Either X and Y both have COLLATE operator or neither do */ 127 if( expRight ){ 128 /* Both X and Y have COLLATE operators. Make sure X is always 129 ** used by clearing the EP_Collate flag from Y. */ 130 pExpr->pRight->flags &= ~EP_Collate; 131 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ 132 /* Neither X nor Y have COLLATE operators, but X has a non-default 133 ** collating sequence. So add the EP_Collate marker on X to cause 134 ** it to be searched first. */ 135 pExpr->pLeft->flags |= EP_Collate; 136 } 137 } 138 SWAP(Expr*,pExpr->pRight,pExpr->pLeft); 139 if( pExpr->op>=TK_GT ){ 140 assert( TK_LT==TK_GT+2 ); 141 assert( TK_GE==TK_LE+2 ); 142 assert( TK_GT>TK_EQ ); 143 assert( TK_GT<TK_LE ); 144 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); 145 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; 146 } 147 } 148 149 /* 150 ** Translate from TK_xx operator to WO_xx bitmask. 151 */ 152 static u16 operatorMask(int op){ 153 u16 c; 154 assert( allowedOp(op) ); 155 if( op==TK_IN ){ 156 c = WO_IN; 157 }else if( op==TK_ISNULL ){ 158 c = WO_ISNULL; 159 }else if( op==TK_IS ){ 160 c = WO_IS; 161 }else{ 162 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); 163 c = (u16)(WO_EQ<<(op-TK_EQ)); 164 } 165 assert( op!=TK_ISNULL || c==WO_ISNULL ); 166 assert( op!=TK_IN || c==WO_IN ); 167 assert( op!=TK_EQ || c==WO_EQ ); 168 assert( op!=TK_LT || c==WO_LT ); 169 assert( op!=TK_LE || c==WO_LE ); 170 assert( op!=TK_GT || c==WO_GT ); 171 assert( op!=TK_GE || c==WO_GE ); 172 assert( op!=TK_IS || c==WO_IS ); 173 return c; 174 } 175 176 177 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION 178 /* 179 ** Check to see if the given expression is a LIKE or GLOB operator that 180 ** can be optimized using inequality constraints. Return TRUE if it is 181 ** so and false if not. 182 ** 183 ** In order for the operator to be optimizible, the RHS must be a string 184 ** literal that does not begin with a wildcard. The LHS must be a column 185 ** that may only be NULL, a string, or a BLOB, never a number. (This means 186 ** that virtual tables cannot participate in the LIKE optimization.) The 187 ** collating sequence for the column on the LHS must be appropriate for 188 ** the operator. 189 */ 190 static int isLikeOrGlob( 191 Parse *pParse, /* Parsing and code generating context */ 192 Expr *pExpr, /* Test this expression */ 193 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ 194 int *pisComplete, /* True if the only wildcard is % in the last character */ 195 int *pnoCase /* True if uppercase is equivalent to lowercase */ 196 ){ 197 const u8 *z = 0; /* String on RHS of LIKE operator */ 198 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ 199 ExprList *pList; /* List of operands to the LIKE operator */ 200 int c; /* One character in z[] */ 201 int cnt; /* Number of non-wildcard prefix characters */ 202 char wc[4]; /* Wildcard characters */ 203 sqlite3 *db = pParse->db; /* Database connection */ 204 sqlite3_value *pVal = 0; 205 int op; /* Opcode of pRight */ 206 int rc; /* Result code to return */ 207 208 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ 209 return 0; 210 } 211 #ifdef SQLITE_EBCDIC 212 if( *pnoCase ) return 0; 213 #endif 214 pList = pExpr->x.pList; 215 pLeft = pList->a[1].pExpr; 216 217 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); 218 op = pRight->op; 219 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){ 220 Vdbe *pReprepare = pParse->pReprepare; 221 int iCol = pRight->iColumn; 222 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); 223 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ 224 z = sqlite3_value_text(pVal); 225 } 226 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); 227 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); 228 }else if( op==TK_STRING ){ 229 z = (u8*)pRight->u.zToken; 230 } 231 if( z ){ 232 233 /* If the RHS begins with a digit or a minus sign, then the LHS must 234 ** be an ordinary column (not a virtual table column) with TEXT affinity. 235 ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false 236 ** even though "lhs LIKE rhs" is true. But if the RHS does not start 237 ** with a digit or '-', then "lhs LIKE rhs" will always be false if 238 ** the LHS is numeric and so the optimization still works. 239 */ 240 if( sqlite3Isdigit(z[0]) || z[0]=='-' ){ 241 if( pLeft->op!=TK_COLUMN 242 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 243 || IsVirtual(pLeft->pTab) /* Value might be numeric */ 244 ){ 245 sqlite3ValueFree(pVal); 246 return 0; 247 } 248 } 249 250 /* Count the number of prefix characters prior to the first wildcard */ 251 cnt = 0; 252 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ 253 cnt++; 254 if( c==wc[3] && z[cnt]!=0 ) cnt++; 255 } 256 257 /* The optimization is possible only if (1) the pattern does not begin 258 ** with a wildcard and if (2) the non-wildcard prefix does not end with 259 ** an (illegal 0xff) character. The second condition is necessary so 260 ** that we can increment the prefix key to find an upper bound for the 261 ** range search. 262 */ 263 if( cnt!=0 && 255!=(u8)z[cnt-1] ){ 264 Expr *pPrefix; 265 266 /* A "complete" match if the pattern ends with "*" or "%" */ 267 *pisComplete = c==wc[0] && z[cnt+1]==0; 268 269 /* Get the pattern prefix. Remove all escapes from the prefix. */ 270 pPrefix = sqlite3Expr(db, TK_STRING, (char*)z); 271 if( pPrefix ){ 272 int iFrom, iTo; 273 char *zNew = pPrefix->u.zToken; 274 zNew[cnt] = 0; 275 for(iFrom=iTo=0; iFrom<cnt; iFrom++){ 276 if( zNew[iFrom]==wc[3] ) iFrom++; 277 zNew[iTo++] = zNew[iFrom]; 278 } 279 zNew[iTo] = 0; 280 } 281 *ppPrefix = pPrefix; 282 283 /* If the RHS pattern is a bound parameter, make arrangements to 284 ** reprepare the statement when that parameter is rebound */ 285 if( op==TK_VARIABLE ){ 286 Vdbe *v = pParse->pVdbe; 287 sqlite3VdbeSetVarmask(v, pRight->iColumn); 288 if( *pisComplete && pRight->u.zToken[1] ){ 289 /* If the rhs of the LIKE expression is a variable, and the current 290 ** value of the variable means there is no need to invoke the LIKE 291 ** function, then no OP_Variable will be added to the program. 292 ** This causes problems for the sqlite3_bind_parameter_name() 293 ** API. To work around them, add a dummy OP_Variable here. 294 */ 295 int r1 = sqlite3GetTempReg(pParse); 296 sqlite3ExprCodeTarget(pParse, pRight, r1); 297 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); 298 sqlite3ReleaseTempReg(pParse, r1); 299 } 300 } 301 }else{ 302 z = 0; 303 } 304 } 305 306 rc = (z!=0); 307 sqlite3ValueFree(pVal); 308 return rc; 309 } 310 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ 311 312 313 #ifndef SQLITE_OMIT_VIRTUALTABLE 314 /* 315 ** Check to see if the given expression is of the form 316 ** 317 ** column OP expr 318 ** 319 ** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a 320 ** column of a virtual table. 321 ** 322 ** If it is then return TRUE. If not, return FALSE. 323 */ 324 static int isMatchOfColumn( 325 Expr *pExpr, /* Test this expression */ 326 unsigned char *peOp2 /* OUT: 0 for MATCH, or else an op2 value */ 327 ){ 328 static const struct Op2 { 329 const char *zOp; 330 unsigned char eOp2; 331 } aOp[] = { 332 { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, 333 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, 334 { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, 335 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } 336 }; 337 ExprList *pList; 338 Expr *pCol; /* Column reference */ 339 int i; 340 341 if( pExpr->op!=TK_FUNCTION ){ 342 return 0; 343 } 344 pList = pExpr->x.pList; 345 if( pList==0 || pList->nExpr!=2 ){ 346 return 0; 347 } 348 pCol = pList->a[1].pExpr; 349 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){ 350 return 0; 351 } 352 for(i=0; i<ArraySize(aOp); i++){ 353 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ 354 *peOp2 = aOp[i].eOp2; 355 return 1; 356 } 357 } 358 return 0; 359 } 360 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 361 362 /* 363 ** If the pBase expression originated in the ON or USING clause of 364 ** a join, then transfer the appropriate markings over to derived. 365 */ 366 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ 367 if( pDerived ){ 368 pDerived->flags |= pBase->flags & EP_FromJoin; 369 pDerived->iRightJoinTable = pBase->iRightJoinTable; 370 } 371 } 372 373 /* 374 ** Mark term iChild as being a child of term iParent 375 */ 376 static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ 377 pWC->a[iChild].iParent = iParent; 378 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; 379 pWC->a[iParent].nChild++; 380 } 381 382 /* 383 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not 384 ** a conjunction, then return just pTerm when N==0. If N is exceeds 385 ** the number of available subterms, return NULL. 386 */ 387 static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ 388 if( pTerm->eOperator!=WO_AND ){ 389 return N==0 ? pTerm : 0; 390 } 391 if( N<pTerm->u.pAndInfo->wc.nTerm ){ 392 return &pTerm->u.pAndInfo->wc.a[N]; 393 } 394 return 0; 395 } 396 397 /* 398 ** Subterms pOne and pTwo are contained within WHERE clause pWC. The 399 ** two subterms are in disjunction - they are OR-ed together. 400 ** 401 ** If these two terms are both of the form: "A op B" with the same 402 ** A and B values but different operators and if the operators are 403 ** compatible (if one is = and the other is <, for example) then 404 ** add a new virtual AND term to pWC that is the combination of the 405 ** two. 406 ** 407 ** Some examples: 408 ** 409 ** x<y OR x=y --> x<=y 410 ** x=y OR x=y --> x=y 411 ** x<=y OR x<y --> x<=y 412 ** 413 ** The following is NOT generated: 414 ** 415 ** x<y OR x>y --> x!=y 416 */ 417 static void whereCombineDisjuncts( 418 SrcList *pSrc, /* the FROM clause */ 419 WhereClause *pWC, /* The complete WHERE clause */ 420 WhereTerm *pOne, /* First disjunct */ 421 WhereTerm *pTwo /* Second disjunct */ 422 ){ 423 u16 eOp = pOne->eOperator | pTwo->eOperator; 424 sqlite3 *db; /* Database connection (for malloc) */ 425 Expr *pNew; /* New virtual expression */ 426 int op; /* Operator for the combined expression */ 427 int idxNew; /* Index in pWC of the next virtual term */ 428 429 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; 430 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; 431 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp 432 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; 433 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 ); 434 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 ); 435 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; 436 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return; 437 /* If we reach this point, it means the two subterms can be combined */ 438 if( (eOp & (eOp-1))!=0 ){ 439 if( eOp & (WO_LT|WO_LE) ){ 440 eOp = WO_LE; 441 }else{ 442 assert( eOp & (WO_GT|WO_GE) ); 443 eOp = WO_GE; 444 } 445 } 446 db = pWC->pWInfo->pParse->db; 447 pNew = sqlite3ExprDup(db, pOne->pExpr, 0); 448 if( pNew==0 ) return; 449 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); } 450 pNew->op = op; 451 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); 452 exprAnalyze(pSrc, pWC, idxNew); 453 } 454 455 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) 456 /* 457 ** Analyze a term that consists of two or more OR-connected 458 ** subterms. So in: 459 ** 460 ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) 461 ** ^^^^^^^^^^^^^^^^^^^^ 462 ** 463 ** This routine analyzes terms such as the middle term in the above example. 464 ** A WhereOrTerm object is computed and attached to the term under 465 ** analysis, regardless of the outcome of the analysis. Hence: 466 ** 467 ** WhereTerm.wtFlags |= TERM_ORINFO 468 ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object 469 ** 470 ** The term being analyzed must have two or more of OR-connected subterms. 471 ** A single subterm might be a set of AND-connected sub-subterms. 472 ** Examples of terms under analysis: 473 ** 474 ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 475 ** (B) x=expr1 OR expr2=x OR x=expr3 476 ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) 477 ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') 478 ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) 479 ** (F) x>A OR (x=A AND y>=B) 480 ** 481 ** CASE 1: 482 ** 483 ** If all subterms are of the form T.C=expr for some single column of C and 484 ** a single table T (as shown in example B above) then create a new virtual 485 ** term that is an equivalent IN expression. In other words, if the term 486 ** being analyzed is: 487 ** 488 ** x = expr1 OR expr2 = x OR x = expr3 489 ** 490 ** then create a new virtual term like this: 491 ** 492 ** x IN (expr1,expr2,expr3) 493 ** 494 ** CASE 2: 495 ** 496 ** If there are exactly two disjuncts and one side has x>A and the other side 497 ** has x=A (for the same x and A) then add a new virtual conjunct term to the 498 ** WHERE clause of the form "x>=A". Example: 499 ** 500 ** x>A OR (x=A AND y>B) adds: x>=A 501 ** 502 ** The added conjunct can sometimes be helpful in query planning. 503 ** 504 ** CASE 3: 505 ** 506 ** If all subterms are indexable by a single table T, then set 507 ** 508 ** WhereTerm.eOperator = WO_OR 509 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T 510 ** 511 ** A subterm is "indexable" if it is of the form 512 ** "T.C <op> <expr>" where C is any column of table T and 513 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". 514 ** A subterm is also indexable if it is an AND of two or more 515 ** subsubterms at least one of which is indexable. Indexable AND 516 ** subterms have their eOperator set to WO_AND and they have 517 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. 518 ** 519 ** From another point of view, "indexable" means that the subterm could 520 ** potentially be used with an index if an appropriate index exists. 521 ** This analysis does not consider whether or not the index exists; that 522 ** is decided elsewhere. This analysis only looks at whether subterms 523 ** appropriate for indexing exist. 524 ** 525 ** All examples A through E above satisfy case 3. But if a term 526 ** also satisfies case 1 (such as B) we know that the optimizer will 527 ** always prefer case 1, so in that case we pretend that case 3 is not 528 ** satisfied. 529 ** 530 ** It might be the case that multiple tables are indexable. For example, 531 ** (E) above is indexable on tables P, Q, and R. 532 ** 533 ** Terms that satisfy case 3 are candidates for lookup by using 534 ** separate indices to find rowids for each subterm and composing 535 ** the union of all rowids using a RowSet object. This is similar 536 ** to "bitmap indices" in other database engines. 537 ** 538 ** OTHERWISE: 539 ** 540 ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to 541 ** zero. This term is not useful for search. 542 */ 543 static void exprAnalyzeOrTerm( 544 SrcList *pSrc, /* the FROM clause */ 545 WhereClause *pWC, /* the complete WHERE clause */ 546 int idxTerm /* Index of the OR-term to be analyzed */ 547 ){ 548 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ 549 Parse *pParse = pWInfo->pParse; /* Parser context */ 550 sqlite3 *db = pParse->db; /* Database connection */ 551 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ 552 Expr *pExpr = pTerm->pExpr; /* The expression of the term */ 553 int i; /* Loop counters */ 554 WhereClause *pOrWc; /* Breakup of pTerm into subterms */ 555 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ 556 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ 557 Bitmask chngToIN; /* Tables that might satisfy case 1 */ 558 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ 559 560 /* 561 ** Break the OR clause into its separate subterms. The subterms are 562 ** stored in a WhereClause structure containing within the WhereOrInfo 563 ** object that is attached to the original OR clause term. 564 */ 565 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); 566 assert( pExpr->op==TK_OR ); 567 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); 568 if( pOrInfo==0 ) return; 569 pTerm->wtFlags |= TERM_ORINFO; 570 pOrWc = &pOrInfo->wc; 571 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic)); 572 sqlite3WhereClauseInit(pOrWc, pWInfo); 573 sqlite3WhereSplit(pOrWc, pExpr, TK_OR); 574 sqlite3WhereExprAnalyze(pSrc, pOrWc); 575 if( db->mallocFailed ) return; 576 assert( pOrWc->nTerm>=2 ); 577 578 /* 579 ** Compute the set of tables that might satisfy cases 1 or 3. 580 */ 581 indexable = ~(Bitmask)0; 582 chngToIN = ~(Bitmask)0; 583 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ 584 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ 585 WhereAndInfo *pAndInfo; 586 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); 587 chngToIN = 0; 588 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo)); 589 if( pAndInfo ){ 590 WhereClause *pAndWC; 591 WhereTerm *pAndTerm; 592 int j; 593 Bitmask b = 0; 594 pOrTerm->u.pAndInfo = pAndInfo; 595 pOrTerm->wtFlags |= TERM_ANDINFO; 596 pOrTerm->eOperator = WO_AND; 597 pAndWC = &pAndInfo->wc; 598 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic)); 599 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo); 600 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND); 601 sqlite3WhereExprAnalyze(pSrc, pAndWC); 602 pAndWC->pOuter = pWC; 603 if( !db->mallocFailed ){ 604 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ 605 assert( pAndTerm->pExpr ); 606 if( allowedOp(pAndTerm->pExpr->op) 607 || pAndTerm->eOperator==WO_MATCH 608 ){ 609 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); 610 } 611 } 612 } 613 indexable &= b; 614 } 615 }else if( pOrTerm->wtFlags & TERM_COPIED ){ 616 /* Skip this term for now. We revisit it when we process the 617 ** corresponding TERM_VIRTUAL term */ 618 }else{ 619 Bitmask b; 620 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); 621 if( pOrTerm->wtFlags & TERM_VIRTUAL ){ 622 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; 623 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor); 624 } 625 indexable &= b; 626 if( (pOrTerm->eOperator & WO_EQ)==0 ){ 627 chngToIN = 0; 628 }else{ 629 chngToIN &= b; 630 } 631 } 632 } 633 634 /* 635 ** Record the set of tables that satisfy case 3. The set might be 636 ** empty. 637 */ 638 pOrInfo->indexable = indexable; 639 pTerm->eOperator = indexable==0 ? 0 : WO_OR; 640 641 /* For a two-way OR, attempt to implementation case 2. 642 */ 643 if( indexable && pOrWc->nTerm==2 ){ 644 int iOne = 0; 645 WhereTerm *pOne; 646 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){ 647 int iTwo = 0; 648 WhereTerm *pTwo; 649 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){ 650 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo); 651 } 652 } 653 } 654 655 /* 656 ** chngToIN holds a set of tables that *might* satisfy case 1. But 657 ** we have to do some additional checking to see if case 1 really 658 ** is satisfied. 659 ** 660 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means 661 ** that there is no possibility of transforming the OR clause into an 662 ** IN operator because one or more terms in the OR clause contain 663 ** something other than == on a column in the single table. The 1-bit 664 ** case means that every term of the OR clause is of the form 665 ** "table.column=expr" for some single table. The one bit that is set 666 ** will correspond to the common table. We still need to check to make 667 ** sure the same column is used on all terms. The 2-bit case is when 668 ** the all terms are of the form "table1.column=table2.column". It 669 ** might be possible to form an IN operator with either table1.column 670 ** or table2.column as the LHS if either is common to every term of 671 ** the OR clause. 672 ** 673 ** Note that terms of the form "table.column1=table.column2" (the 674 ** same table on both sizes of the ==) cannot be optimized. 675 */ 676 if( chngToIN ){ 677 int okToChngToIN = 0; /* True if the conversion to IN is valid */ 678 int iColumn = -1; /* Column index on lhs of IN operator */ 679 int iCursor = -1; /* Table cursor common to all terms */ 680 int j = 0; /* Loop counter */ 681 682 /* Search for a table and column that appears on one side or the 683 ** other of the == operator in every subterm. That table and column 684 ** will be recorded in iCursor and iColumn. There might not be any 685 ** such table and column. Set okToChngToIN if an appropriate table 686 ** and column is found but leave okToChngToIN false if not found. 687 */ 688 for(j=0; j<2 && !okToChngToIN; j++){ 689 pOrTerm = pOrWc->a; 690 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ 691 assert( pOrTerm->eOperator & WO_EQ ); 692 pOrTerm->wtFlags &= ~TERM_OR_OK; 693 if( pOrTerm->leftCursor==iCursor ){ 694 /* This is the 2-bit case and we are on the second iteration and 695 ** current term is from the first iteration. So skip this term. */ 696 assert( j==1 ); 697 continue; 698 } 699 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet, 700 pOrTerm->leftCursor))==0 ){ 701 /* This term must be of the form t1.a==t2.b where t2 is in the 702 ** chngToIN set but t1 is not. This term will be either preceded 703 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term 704 ** and use its inversion. */ 705 testcase( pOrTerm->wtFlags & TERM_COPIED ); 706 testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); 707 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); 708 continue; 709 } 710 iColumn = pOrTerm->u.leftColumn; 711 iCursor = pOrTerm->leftCursor; 712 break; 713 } 714 if( i<0 ){ 715 /* No candidate table+column was found. This can only occur 716 ** on the second iteration */ 717 assert( j==1 ); 718 assert( IsPowerOfTwo(chngToIN) ); 719 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) ); 720 break; 721 } 722 testcase( j==1 ); 723 724 /* We have found a candidate table and column. Check to see if that 725 ** table and column is common to every term in the OR clause */ 726 okToChngToIN = 1; 727 for(; i>=0 && okToChngToIN; i--, pOrTerm++){ 728 assert( pOrTerm->eOperator & WO_EQ ); 729 if( pOrTerm->leftCursor!=iCursor ){ 730 pOrTerm->wtFlags &= ~TERM_OR_OK; 731 }else if( pOrTerm->u.leftColumn!=iColumn ){ 732 okToChngToIN = 0; 733 }else{ 734 int affLeft, affRight; 735 /* If the right-hand side is also a column, then the affinities 736 ** of both right and left sides must be such that no type 737 ** conversions are required on the right. (Ticket #2249) 738 */ 739 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); 740 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); 741 if( affRight!=0 && affRight!=affLeft ){ 742 okToChngToIN = 0; 743 }else{ 744 pOrTerm->wtFlags |= TERM_OR_OK; 745 } 746 } 747 } 748 } 749 750 /* At this point, okToChngToIN is true if original pTerm satisfies 751 ** case 1. In that case, construct a new virtual term that is 752 ** pTerm converted into an IN operator. 753 */ 754 if( okToChngToIN ){ 755 Expr *pDup; /* A transient duplicate expression */ 756 ExprList *pList = 0; /* The RHS of the IN operator */ 757 Expr *pLeft = 0; /* The LHS of the IN operator */ 758 Expr *pNew; /* The complete IN operator */ 759 760 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ 761 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; 762 assert( pOrTerm->eOperator & WO_EQ ); 763 assert( pOrTerm->leftCursor==iCursor ); 764 assert( pOrTerm->u.leftColumn==iColumn ); 765 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); 766 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); 767 pLeft = pOrTerm->pExpr->pLeft; 768 } 769 assert( pLeft!=0 ); 770 pDup = sqlite3ExprDup(db, pLeft, 0); 771 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0); 772 if( pNew ){ 773 int idxNew; 774 transferJoinMarkings(pNew, pExpr); 775 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 776 pNew->x.pList = pList; 777 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); 778 testcase( idxNew==0 ); 779 exprAnalyze(pSrc, pWC, idxNew); 780 pTerm = &pWC->a[idxTerm]; 781 markTermAsChild(pWC, idxNew, idxTerm); 782 }else{ 783 sqlite3ExprListDelete(db, pList); 784 } 785 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ 786 } 787 } 788 } 789 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ 790 791 /* 792 ** We already know that pExpr is a binary operator where both operands are 793 ** column references. This routine checks to see if pExpr is an equivalence 794 ** relation: 795 ** 1. The SQLITE_Transitive optimization must be enabled 796 ** 2. Must be either an == or an IS operator 797 ** 3. Not originating in the ON clause of an OUTER JOIN 798 ** 4. The affinities of A and B must be compatible 799 ** 5a. Both operands use the same collating sequence OR 800 ** 5b. The overall collating sequence is BINARY 801 ** If this routine returns TRUE, that means that the RHS can be substituted 802 ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. 803 ** This is an optimization. No harm comes from returning 0. But if 1 is 804 ** returned when it should not be, then incorrect answers might result. 805 */ 806 static int termIsEquivalence(Parse *pParse, Expr *pExpr){ 807 char aff1, aff2; 808 CollSeq *pColl; 809 const char *zColl1, *zColl2; 810 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; 811 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; 812 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0; 813 aff1 = sqlite3ExprAffinity(pExpr->pLeft); 814 aff2 = sqlite3ExprAffinity(pExpr->pRight); 815 if( aff1!=aff2 816 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2)) 817 ){ 818 return 0; 819 } 820 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); 821 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1; 822 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 823 zColl1 = pColl ? pColl->zName : 0; 824 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); 825 zColl2 = pColl ? pColl->zName : 0; 826 return sqlite3_stricmp(zColl1, zColl2)==0; 827 } 828 829 /* 830 ** Recursively walk the expressions of a SELECT statement and generate 831 ** a bitmask indicating which tables are used in that expression 832 ** tree. 833 */ 834 static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){ 835 Bitmask mask = 0; 836 while( pS ){ 837 SrcList *pSrc = pS->pSrc; 838 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList); 839 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy); 840 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy); 841 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere); 842 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving); 843 if( ALWAYS(pSrc!=0) ){ 844 int i; 845 for(i=0; i<pSrc->nSrc; i++){ 846 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect); 847 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn); 848 } 849 } 850 pS = pS->pPrior; 851 } 852 return mask; 853 } 854 855 /* 856 ** Expression pExpr is one operand of a comparison operator that might 857 ** be useful for indexing. This routine checks to see if pExpr appears 858 ** in any index. Return TRUE (1) if pExpr is an indexed term and return 859 ** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor 860 ** number of the table that is indexed and aiCurCol[1] to the column number 861 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being 862 ** indexed. 863 ** 864 ** If pExpr is a TK_COLUMN column reference, then this routine always returns 865 ** true even if that particular column is not indexed, because the column 866 ** might be added to an automatic index later. 867 */ 868 static SQLITE_NOINLINE int exprMightBeIndexed2( 869 SrcList *pFrom, /* The FROM clause */ 870 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */ 871 int *aiCurCol, /* Write the referenced table cursor and column here */ 872 Expr *pExpr /* An operand of a comparison operator */ 873 ){ 874 Index *pIdx; 875 int i; 876 int iCur; 877 for(i=0; mPrereq>1; i++, mPrereq>>=1){} 878 iCur = pFrom->a[i].iCursor; 879 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 880 if( pIdx->aColExpr==0 ) continue; 881 for(i=0; i<pIdx->nKeyCol; i++){ 882 if( pIdx->aiColumn[i]!=XN_EXPR ) continue; 883 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){ 884 aiCurCol[0] = iCur; 885 aiCurCol[1] = XN_EXPR; 886 return 1; 887 } 888 } 889 } 890 return 0; 891 } 892 static int exprMightBeIndexed( 893 SrcList *pFrom, /* The FROM clause */ 894 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */ 895 int *aiCurCol, /* Write the referenced table cursor & column here */ 896 Expr *pExpr, /* An operand of a comparison operator */ 897 int op /* The specific comparison operator */ 898 ){ 899 /* If this expression is a vector to the left or right of a 900 ** inequality constraint (>, <, >= or <=), perform the processing 901 ** on the first element of the vector. */ 902 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE ); 903 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE ); 904 assert( op<=TK_GE ); 905 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){ 906 pExpr = pExpr->x.pList->a[0].pExpr; 907 } 908 909 if( pExpr->op==TK_COLUMN ){ 910 aiCurCol[0] = pExpr->iTable; 911 aiCurCol[1] = pExpr->iColumn; 912 return 1; 913 } 914 if( mPrereq==0 ) return 0; /* No table references */ 915 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */ 916 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr); 917 } 918 919 /* 920 ** The input to this routine is an WhereTerm structure with only the 921 ** "pExpr" field filled in. The job of this routine is to analyze the 922 ** subexpression and populate all the other fields of the WhereTerm 923 ** structure. 924 ** 925 ** If the expression is of the form "<expr> <op> X" it gets commuted 926 ** to the standard form of "X <op> <expr>". 927 ** 928 ** If the expression is of the form "X <op> Y" where both X and Y are 929 ** columns, then the original expression is unchanged and a new virtual 930 ** term of the form "Y <op> X" is added to the WHERE clause and 931 ** analyzed separately. The original term is marked with TERM_COPIED 932 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr 933 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it 934 ** is a commuted copy of a prior term.) The original term has nChild=1 935 ** and the copy has idxParent set to the index of the original term. 936 */ 937 static void exprAnalyze( 938 SrcList *pSrc, /* the FROM clause */ 939 WhereClause *pWC, /* the WHERE clause */ 940 int idxTerm /* Index of the term to be analyzed */ 941 ){ 942 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ 943 WhereTerm *pTerm; /* The term to be analyzed */ 944 WhereMaskSet *pMaskSet; /* Set of table index masks */ 945 Expr *pExpr; /* The expression to be analyzed */ 946 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ 947 Bitmask prereqAll; /* Prerequesites of pExpr */ 948 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ 949 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ 950 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ 951 int noCase = 0; /* uppercase equivalent to lowercase */ 952 int op; /* Top-level operator. pExpr->op */ 953 Parse *pParse = pWInfo->pParse; /* Parsing context */ 954 sqlite3 *db = pParse->db; /* Database connection */ 955 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */ 956 int nLeft; /* Number of elements on left side vector */ 957 958 if( db->mallocFailed ){ 959 return; 960 } 961 pTerm = &pWC->a[idxTerm]; 962 pMaskSet = &pWInfo->sMaskSet; 963 pExpr = pTerm->pExpr; 964 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); 965 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft); 966 op = pExpr->op; 967 if( op==TK_IN ){ 968 assert( pExpr->pRight==0 ); 969 if( sqlite3ExprCheckIN(pParse, pExpr) ) return; 970 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 971 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect); 972 }else{ 973 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList); 974 } 975 }else if( op==TK_ISNULL ){ 976 pTerm->prereqRight = 0; 977 }else{ 978 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight); 979 } 980 pMaskSet->bVarSelect = 0; 981 prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr); 982 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT; 983 if( ExprHasProperty(pExpr, EP_FromJoin) ){ 984 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); 985 prereqAll |= x; 986 extraRight = x-1; /* ON clause terms may not be used with an index 987 ** on left table of a LEFT JOIN. Ticket #3015 */ 988 if( (prereqAll>>1)>=x ){ 989 sqlite3ErrorMsg(pParse, "ON clause references tables to its right"); 990 return; 991 } 992 } 993 pTerm->prereqAll = prereqAll; 994 pTerm->leftCursor = -1; 995 pTerm->iParent = -1; 996 pTerm->eOperator = 0; 997 if( allowedOp(op) ){ 998 int aiCurCol[2]; 999 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); 1000 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); 1001 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; 1002 1003 if( pTerm->iField>0 ){ 1004 assert( op==TK_IN ); 1005 assert( pLeft->op==TK_VECTOR ); 1006 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr; 1007 } 1008 1009 if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){ 1010 pTerm->leftCursor = aiCurCol[0]; 1011 pTerm->u.leftColumn = aiCurCol[1]; 1012 pTerm->eOperator = operatorMask(op) & opMask; 1013 } 1014 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; 1015 if( pRight 1016 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op) 1017 ){ 1018 WhereTerm *pNew; 1019 Expr *pDup; 1020 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ 1021 assert( pTerm->iField==0 ); 1022 if( pTerm->leftCursor>=0 ){ 1023 int idxNew; 1024 pDup = sqlite3ExprDup(db, pExpr, 0); 1025 if( db->mallocFailed ){ 1026 sqlite3ExprDelete(db, pDup); 1027 return; 1028 } 1029 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); 1030 if( idxNew==0 ) return; 1031 pNew = &pWC->a[idxNew]; 1032 markTermAsChild(pWC, idxNew, idxTerm); 1033 if( op==TK_IS ) pNew->wtFlags |= TERM_IS; 1034 pTerm = &pWC->a[idxTerm]; 1035 pTerm->wtFlags |= TERM_COPIED; 1036 1037 if( termIsEquivalence(pParse, pDup) ){ 1038 pTerm->eOperator |= WO_EQUIV; 1039 eExtraOp = WO_EQUIV; 1040 } 1041 }else{ 1042 pDup = pExpr; 1043 pNew = pTerm; 1044 } 1045 exprCommute(pParse, pDup); 1046 pNew->leftCursor = aiCurCol[0]; 1047 pNew->u.leftColumn = aiCurCol[1]; 1048 testcase( (prereqLeft | extraRight) != prereqLeft ); 1049 pNew->prereqRight = prereqLeft | extraRight; 1050 pNew->prereqAll = prereqAll; 1051 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; 1052 } 1053 } 1054 1055 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION 1056 /* If a term is the BETWEEN operator, create two new virtual terms 1057 ** that define the range that the BETWEEN implements. For example: 1058 ** 1059 ** a BETWEEN b AND c 1060 ** 1061 ** is converted into: 1062 ** 1063 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) 1064 ** 1065 ** The two new terms are added onto the end of the WhereClause object. 1066 ** The new terms are "dynamic" and are children of the original BETWEEN 1067 ** term. That means that if the BETWEEN term is coded, the children are 1068 ** skipped. Or, if the children are satisfied by an index, the original 1069 ** BETWEEN term is skipped. 1070 */ 1071 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ 1072 ExprList *pList = pExpr->x.pList; 1073 int i; 1074 static const u8 ops[] = {TK_GE, TK_LE}; 1075 assert( pList!=0 ); 1076 assert( pList->nExpr==2 ); 1077 for(i=0; i<2; i++){ 1078 Expr *pNewExpr; 1079 int idxNew; 1080 pNewExpr = sqlite3PExpr(pParse, ops[i], 1081 sqlite3ExprDup(db, pExpr->pLeft, 0), 1082 sqlite3ExprDup(db, pList->a[i].pExpr, 0)); 1083 transferJoinMarkings(pNewExpr, pExpr); 1084 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); 1085 testcase( idxNew==0 ); 1086 exprAnalyze(pSrc, pWC, idxNew); 1087 pTerm = &pWC->a[idxTerm]; 1088 markTermAsChild(pWC, idxNew, idxTerm); 1089 } 1090 } 1091 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ 1092 1093 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) 1094 /* Analyze a term that is composed of two or more subterms connected by 1095 ** an OR operator. 1096 */ 1097 else if( pExpr->op==TK_OR ){ 1098 assert( pWC->op==TK_AND ); 1099 exprAnalyzeOrTerm(pSrc, pWC, idxTerm); 1100 pTerm = &pWC->a[idxTerm]; 1101 } 1102 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ 1103 1104 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION 1105 /* Add constraints to reduce the search space on a LIKE or GLOB 1106 ** operator. 1107 ** 1108 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints 1109 ** 1110 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%' 1111 ** 1112 ** The last character of the prefix "abc" is incremented to form the 1113 ** termination condition "abd". If case is not significant (the default 1114 ** for LIKE) then the lower-bound is made all uppercase and the upper- 1115 ** bound is made all lowercase so that the bounds also work when comparing 1116 ** BLOBs. 1117 */ 1118 if( pWC->op==TK_AND 1119 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) 1120 ){ 1121 Expr *pLeft; /* LHS of LIKE/GLOB operator */ 1122 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ 1123 Expr *pNewExpr1; 1124 Expr *pNewExpr2; 1125 int idxNew1; 1126 int idxNew2; 1127 const char *zCollSeqName; /* Name of collating sequence */ 1128 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; 1129 1130 pLeft = pExpr->x.pList->a[1].pExpr; 1131 pStr2 = sqlite3ExprDup(db, pStr1, 0); 1132 1133 /* Convert the lower bound to upper-case and the upper bound to 1134 ** lower-case (upper-case is less than lower-case in ASCII) so that 1135 ** the range constraints also work for BLOBs 1136 */ 1137 if( noCase && !pParse->db->mallocFailed ){ 1138 int i; 1139 char c; 1140 pTerm->wtFlags |= TERM_LIKE; 1141 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){ 1142 pStr1->u.zToken[i] = sqlite3Toupper(c); 1143 pStr2->u.zToken[i] = sqlite3Tolower(c); 1144 } 1145 } 1146 1147 if( !db->mallocFailed ){ 1148 u8 c, *pC; /* Last character before the first wildcard */ 1149 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; 1150 c = *pC; 1151 if( noCase ){ 1152 /* The point is to increment the last character before the first 1153 ** wildcard. But if we increment '@', that will push it into the 1154 ** alphabetic range where case conversions will mess up the 1155 ** inequality. To avoid this, make sure to also run the full 1156 ** LIKE on all candidate expressions by clearing the isComplete flag 1157 */ 1158 if( c=='A'-1 ) isComplete = 0; 1159 c = sqlite3UpperToLower[c]; 1160 } 1161 *pC = c + 1; 1162 } 1163 zCollSeqName = noCase ? "NOCASE" : "BINARY"; 1164 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); 1165 pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 1166 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), 1167 pStr1); 1168 transferJoinMarkings(pNewExpr1, pExpr); 1169 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); 1170 testcase( idxNew1==0 ); 1171 exprAnalyze(pSrc, pWC, idxNew1); 1172 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); 1173 pNewExpr2 = sqlite3PExpr(pParse, TK_LT, 1174 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), 1175 pStr2); 1176 transferJoinMarkings(pNewExpr2, pExpr); 1177 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); 1178 testcase( idxNew2==0 ); 1179 exprAnalyze(pSrc, pWC, idxNew2); 1180 pTerm = &pWC->a[idxTerm]; 1181 if( isComplete ){ 1182 markTermAsChild(pWC, idxNew1, idxTerm); 1183 markTermAsChild(pWC, idxNew2, idxTerm); 1184 } 1185 } 1186 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ 1187 1188 #ifndef SQLITE_OMIT_VIRTUALTABLE 1189 /* Add a WO_MATCH auxiliary term to the constraint set if the 1190 ** current expression is of the form: column MATCH expr. 1191 ** This information is used by the xBestIndex methods of 1192 ** virtual tables. The native query optimizer does not attempt 1193 ** to do anything with MATCH functions. 1194 */ 1195 if( pWC->op==TK_AND && isMatchOfColumn(pExpr, &eOp2) ){ 1196 int idxNew; 1197 Expr *pRight, *pLeft; 1198 WhereTerm *pNewTerm; 1199 Bitmask prereqColumn, prereqExpr; 1200 1201 pRight = pExpr->x.pList->a[0].pExpr; 1202 pLeft = pExpr->x.pList->a[1].pExpr; 1203 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight); 1204 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft); 1205 if( (prereqExpr & prereqColumn)==0 ){ 1206 Expr *pNewExpr; 1207 pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 1208 0, sqlite3ExprDup(db, pRight, 0)); 1209 if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){ 1210 ExprSetProperty(pNewExpr, EP_FromJoin); 1211 } 1212 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); 1213 testcase( idxNew==0 ); 1214 pNewTerm = &pWC->a[idxNew]; 1215 pNewTerm->prereqRight = prereqExpr; 1216 pNewTerm->leftCursor = pLeft->iTable; 1217 pNewTerm->u.leftColumn = pLeft->iColumn; 1218 pNewTerm->eOperator = WO_MATCH; 1219 pNewTerm->eMatchOp = eOp2; 1220 markTermAsChild(pWC, idxNew, idxTerm); 1221 pTerm = &pWC->a[idxTerm]; 1222 pTerm->wtFlags |= TERM_COPIED; 1223 pNewTerm->prereqAll = pTerm->prereqAll; 1224 } 1225 } 1226 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 1227 1228 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create 1229 ** new terms for each component comparison - "a = ?" and "b = ?". The 1230 ** new terms completely replace the original vector comparison, which is 1231 ** no longer used. 1232 ** 1233 ** This is only required if at least one side of the comparison operation 1234 ** is not a sub-select. */ 1235 if( pWC->op==TK_AND 1236 && (pExpr->op==TK_EQ || pExpr->op==TK_IS) 1237 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 1238 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft 1239 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 1240 || (pExpr->pRight->flags & EP_xIsSelect)==0) 1241 ){ 1242 int i; 1243 for(i=0; i<nLeft; i++){ 1244 int idxNew; 1245 Expr *pNew; 1246 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i); 1247 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i); 1248 1249 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight); 1250 transferJoinMarkings(pNew, pExpr); 1251 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC); 1252 exprAnalyze(pSrc, pWC, idxNew); 1253 } 1254 pTerm = &pWC->a[idxTerm]; 1255 pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL; /* Disable the original */ 1256 pTerm->eOperator = 0; 1257 } 1258 1259 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create 1260 ** a virtual term for each vector component. The expression object 1261 ** used by each such virtual term is pExpr (the full vector IN(...) 1262 ** expression). The WhereTerm.iField variable identifies the index within 1263 ** the vector on the LHS that the virtual term represents. 1264 ** 1265 ** This only works if the RHS is a simple SELECT, not a compound 1266 */ 1267 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0 1268 && pExpr->pLeft->op==TK_VECTOR 1269 && pExpr->x.pSelect->pPrior==0 1270 ){ 1271 int i; 1272 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){ 1273 int idxNew; 1274 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL); 1275 pWC->a[idxNew].iField = i+1; 1276 exprAnalyze(pSrc, pWC, idxNew); 1277 markTermAsChild(pWC, idxNew, idxTerm); 1278 } 1279 } 1280 1281 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1282 /* When sqlite_stat3 histogram data is available an operator of the 1283 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently 1284 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a 1285 ** virtual term of that form. 1286 ** 1287 ** Note that the virtual term must be tagged with TERM_VNULL. 1288 */ 1289 if( pExpr->op==TK_NOTNULL 1290 && pExpr->pLeft->op==TK_COLUMN 1291 && pExpr->pLeft->iColumn>=0 1292 && OptimizationEnabled(db, SQLITE_Stat34) 1293 ){ 1294 Expr *pNewExpr; 1295 Expr *pLeft = pExpr->pLeft; 1296 int idxNew; 1297 WhereTerm *pNewTerm; 1298 1299 pNewExpr = sqlite3PExpr(pParse, TK_GT, 1300 sqlite3ExprDup(db, pLeft, 0), 1301 sqlite3ExprAlloc(db, TK_NULL, 0, 0)); 1302 1303 idxNew = whereClauseInsert(pWC, pNewExpr, 1304 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); 1305 if( idxNew ){ 1306 pNewTerm = &pWC->a[idxNew]; 1307 pNewTerm->prereqRight = 0; 1308 pNewTerm->leftCursor = pLeft->iTable; 1309 pNewTerm->u.leftColumn = pLeft->iColumn; 1310 pNewTerm->eOperator = WO_GT; 1311 markTermAsChild(pWC, idxNew, idxTerm); 1312 pTerm = &pWC->a[idxTerm]; 1313 pTerm->wtFlags |= TERM_COPIED; 1314 pNewTerm->prereqAll = pTerm->prereqAll; 1315 } 1316 } 1317 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ 1318 1319 /* Prevent ON clause terms of a LEFT JOIN from being used to drive 1320 ** an index for tables to the left of the join. 1321 */ 1322 testcase( pTerm!=&pWC->a[idxTerm] ); 1323 pTerm = &pWC->a[idxTerm]; 1324 pTerm->prereqRight |= extraRight; 1325 } 1326 1327 /*************************************************************************** 1328 ** Routines with file scope above. Interface to the rest of the where.c 1329 ** subsystem follows. 1330 ***************************************************************************/ 1331 1332 /* 1333 ** This routine identifies subexpressions in the WHERE clause where 1334 ** each subexpression is separated by the AND operator or some other 1335 ** operator specified in the op parameter. The WhereClause structure 1336 ** is filled with pointers to subexpressions. For example: 1337 ** 1338 ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) 1339 ** \________/ \_______________/ \________________/ 1340 ** slot[0] slot[1] slot[2] 1341 ** 1342 ** The original WHERE clause in pExpr is unaltered. All this routine 1343 ** does is make slot[] entries point to substructure within pExpr. 1344 ** 1345 ** In the previous sentence and in the diagram, "slot[]" refers to 1346 ** the WhereClause.a[] array. The slot[] array grows as needed to contain 1347 ** all terms of the WHERE clause. 1348 */ 1349 void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ 1350 Expr *pE2 = sqlite3ExprSkipCollate(pExpr); 1351 pWC->op = op; 1352 if( pE2==0 ) return; 1353 if( pE2->op!=op ){ 1354 whereClauseInsert(pWC, pExpr, 0); 1355 }else{ 1356 sqlite3WhereSplit(pWC, pE2->pLeft, op); 1357 sqlite3WhereSplit(pWC, pE2->pRight, op); 1358 } 1359 } 1360 1361 /* 1362 ** Initialize a preallocated WhereClause structure. 1363 */ 1364 void sqlite3WhereClauseInit( 1365 WhereClause *pWC, /* The WhereClause to be initialized */ 1366 WhereInfo *pWInfo /* The WHERE processing context */ 1367 ){ 1368 pWC->pWInfo = pWInfo; 1369 pWC->pOuter = 0; 1370 pWC->nTerm = 0; 1371 pWC->nSlot = ArraySize(pWC->aStatic); 1372 pWC->a = pWC->aStatic; 1373 } 1374 1375 /* 1376 ** Deallocate a WhereClause structure. The WhereClause structure 1377 ** itself is not freed. This routine is the inverse of 1378 ** sqlite3WhereClauseInit(). 1379 */ 1380 void sqlite3WhereClauseClear(WhereClause *pWC){ 1381 int i; 1382 WhereTerm *a; 1383 sqlite3 *db = pWC->pWInfo->pParse->db; 1384 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ 1385 if( a->wtFlags & TERM_DYNAMIC ){ 1386 sqlite3ExprDelete(db, a->pExpr); 1387 } 1388 if( a->wtFlags & TERM_ORINFO ){ 1389 whereOrInfoDelete(db, a->u.pOrInfo); 1390 }else if( a->wtFlags & TERM_ANDINFO ){ 1391 whereAndInfoDelete(db, a->u.pAndInfo); 1392 } 1393 } 1394 if( pWC->a!=pWC->aStatic ){ 1395 sqlite3DbFree(db, pWC->a); 1396 } 1397 } 1398 1399 1400 /* 1401 ** These routines walk (recursively) an expression tree and generate 1402 ** a bitmask indicating which tables are used in that expression 1403 ** tree. 1404 */ 1405 Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){ 1406 Bitmask mask; 1407 if( p==0 ) return 0; 1408 if( p->op==TK_COLUMN ){ 1409 return sqlite3WhereGetMask(pMaskSet, p->iTable); 1410 } 1411 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0; 1412 assert( !ExprHasProperty(p, EP_TokenOnly) ); 1413 if( p->pLeft ) mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft); 1414 if( p->pRight ){ 1415 mask |= sqlite3WhereExprUsage(pMaskSet, p->pRight); 1416 assert( p->x.pList==0 ); 1417 }else if( ExprHasProperty(p, EP_xIsSelect) ){ 1418 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1; 1419 mask |= exprSelectUsage(pMaskSet, p->x.pSelect); 1420 }else if( p->x.pList ){ 1421 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList); 1422 } 1423 return mask; 1424 } 1425 Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){ 1426 int i; 1427 Bitmask mask = 0; 1428 if( pList ){ 1429 for(i=0; i<pList->nExpr; i++){ 1430 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr); 1431 } 1432 } 1433 return mask; 1434 } 1435 1436 1437 /* 1438 ** Call exprAnalyze on all terms in a WHERE clause. 1439 ** 1440 ** Note that exprAnalyze() might add new virtual terms onto the 1441 ** end of the WHERE clause. We do not want to analyze these new 1442 ** virtual terms, so start analyzing at the end and work forward 1443 ** so that the added virtual terms are never processed. 1444 */ 1445 void sqlite3WhereExprAnalyze( 1446 SrcList *pTabList, /* the FROM clause */ 1447 WhereClause *pWC /* the WHERE clause to be analyzed */ 1448 ){ 1449 int i; 1450 for(i=pWC->nTerm-1; i>=0; i--){ 1451 exprAnalyze(pTabList, pWC, i); 1452 } 1453 } 1454 1455 /* 1456 ** For table-valued-functions, transform the function arguments into 1457 ** new WHERE clause terms. 1458 ** 1459 ** Each function argument translates into an equality constraint against 1460 ** a HIDDEN column in the table. 1461 */ 1462 void sqlite3WhereTabFuncArgs( 1463 Parse *pParse, /* Parsing context */ 1464 struct SrcList_item *pItem, /* The FROM clause term to process */ 1465 WhereClause *pWC /* Xfer function arguments to here */ 1466 ){ 1467 Table *pTab; 1468 int j, k; 1469 ExprList *pArgs; 1470 Expr *pColRef; 1471 Expr *pTerm; 1472 if( pItem->fg.isTabFunc==0 ) return; 1473 pTab = pItem->pTab; 1474 assert( pTab!=0 ); 1475 pArgs = pItem->u1.pFuncArg; 1476 if( pArgs==0 ) return; 1477 for(j=k=0; j<pArgs->nExpr; j++){ 1478 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;} 1479 if( k>=pTab->nCol ){ 1480 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d", 1481 pTab->zName, j); 1482 return; 1483 } 1484 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0); 1485 if( pColRef==0 ) return; 1486 pColRef->iTable = pItem->iCursor; 1487 pColRef->iColumn = k++; 1488 pColRef->pTab = pTab; 1489 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, 1490 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0)); 1491 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); 1492 } 1493 } 1494