sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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 editability. 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 = sqlite3WhereMalloc(pWC->pWInfo, 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 | pWC->nSlot = pWC->nSlot*2; |
| 77 | } |
| 78 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
| 79 | if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm; |
| 80 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
| 81 | pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; |
| 82 | }else{ |
| 83 | pTerm->truthProb = 1; |
| 84 | } |
| 85 | pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p); |
| 86 | pTerm->wtFlags = wtFlags; |
| 87 | pTerm->pWC = pWC; |
| 88 | pTerm->iParent = -1; |
| 89 | memset(&pTerm->eOperator, 0, |
| 90 | sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)); |
| 91 | return idx; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | ** Return TRUE if the given operator is one of the operators that is |
| 96 | ** allowed for an indexable WHERE clause term. The allowed operators are |
| 97 | ** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL" |
| 98 | */ |
| 99 | static int allowedOp(int op){ |
| 100 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 101 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 102 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 103 | assert( TK_GE==TK_EQ+4 ); |
| 104 | assert( TK_IN<TK_EQ ); |
| 105 | assert( TK_IS<TK_EQ ); |
| 106 | assert( TK_ISNULL<TK_EQ ); |
| 107 | if( op>TK_GE ) return 0; |
| 108 | if( op>=TK_EQ ) return 1; |
| 109 | return op==TK_IN || op==TK_ISNULL || op==TK_IS; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | ** Commute a comparison operator. Expressions of the form "X op Y" |
| 114 | ** are converted into "Y op X". |
| 115 | */ |
| 116 | static u16 exprCommute(Parse *pParse, Expr *pExpr){ |
| 117 | if( pExpr->pLeft->op==TK_VECTOR |
| 118 | || pExpr->pRight->op==TK_VECTOR |
| 119 | || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) != |
| 120 | sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft) |
| 121 | ){ |
| 122 | pExpr->flags ^= EP_Commuted; |
| 123 | } |
| 124 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 125 | if( pExpr->op>=TK_GT ){ |
| 126 | assert( TK_LT==TK_GT+2 ); |
| 127 | assert( TK_GE==TK_LE+2 ); |
| 128 | assert( TK_GT>TK_EQ ); |
| 129 | assert( TK_GT<TK_LE ); |
| 130 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 131 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 138 | */ |
| 139 | static u16 operatorMask(int op){ |
| 140 | u16 c; |
| 141 | assert( allowedOp(op) ); |
| 142 | if( op>=TK_EQ ){ |
| 143 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 144 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
| 145 | }else if( op==TK_IN ){ |
| 146 | c = WO_IN; |
| 147 | }else if( op==TK_ISNULL ){ |
| 148 | c = WO_ISNULL; |
| 149 | }else{ |
| 150 | assert( op==TK_IS ); |
| 151 | c = WO_IS; |
| 152 | } |
| 153 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
| 154 | assert( op!=TK_IN || c==WO_IN ); |
| 155 | assert( op!=TK_EQ || c==WO_EQ ); |
| 156 | assert( op!=TK_LT || c==WO_LT ); |
| 157 | assert( op!=TK_LE || c==WO_LE ); |
| 158 | assert( op!=TK_GT || c==WO_GT ); |
| 159 | assert( op!=TK_GE || c==WO_GE ); |
| 160 | assert( op!=TK_IS || c==WO_IS ); |
| 161 | return c; |
| 162 | } |
| 163 | |
| 164 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 165 | /* |
| 166 | ** The input is an N-byte GLOB or LIKE pattern prefix |
| 167 | ** with the following guarantees: |
| 168 | ** |
| 169 | ** (1) It contains no wildcards. |
| 170 | ** (2) z[N]==0 |
| 171 | ** (3) N>0 |
| 172 | ** (4) bHasSpace is true if and only if the original complete LIKE/GLOB |
| 173 | ** pattern contained wildcards and/or escapes. (It is usually true, |
| 174 | ** therefore). bHasSpace also means that there is at least one byte |
| 175 | ** of overrun space at z[N+1] that this routine may use. |
| 176 | ** |
| 177 | ** This routine checks to see if the input will be a prefix to |
| 178 | ** any number (integer or floating-point) that SQLite might generate |
| 179 | ** from a MEM_Int or MEM_Real, including "Inf" and "-Inf". |
| 180 | ** |
| 181 | ** Return true if the input is a possible numeric prefix and |
| 182 | ** false if the input cannot possibly be a numeric prefix. |
| 183 | */ |
| 184 | static SQLITE_NOINLINE int isNumericPrefix(char *z, int N, int bHasSpace){ |
| 185 | int x; |
| 186 | double rDummy; |
| 187 | assert( N>0 ); |
| 188 | if( sqlite3AtoF(z, &rDummy)>0 ) return 1; /* Boundary is itself a number */ |
| 189 | if( bHasSpace ){ |
| 190 | z[N] = '0'; |
| 191 | z[N+1] = 0; |
| 192 | x = sqlite3AtoF(z, &rDummy); /* Prefix like "1.0e" or "-" */ |
| 193 | z[N] = 0; |
| 194 | if( x>0 ) return 1; |
| 195 | } |
| 196 | if( z[0]=='-' ){ z++; N--; } |
| 197 | if( N>0 && N<=3 && sqlite3StrNICmp(z,"inf",N)==0 ) return 1; |
| 198 | return 0; |
| 199 | } |
| 200 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 201 | |
| 202 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 203 | /* |
| 204 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 205 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 206 | ** so and false if not. |
| 207 | ** |
| 208 | ** In order for the operator to be optimizible, the RHS must be a string |
| 209 | ** literal that does not begin with a wildcard. The LHS must be a column |
| 210 | ** that may only be NULL, a string, or a BLOB, never a number. (This means |
| 211 | ** that virtual tables cannot participate in the LIKE optimization.) The |
| 212 | ** collating sequence for the column on the LHS must be appropriate for |
| 213 | ** the operator. |
| 214 | */ |
| 215 | static int isLikeOrGlob( |
| 216 | Parse *pParse, /* Parsing and code generating context */ |
| 217 | Expr *pExpr, /* Test this expression */ |
| 218 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
| 219 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 220 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
| 221 | ){ |
| 222 | const u8 *z = 0; /* String on RHS of LIKE operator */ |
| 223 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 224 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 225 | u8 c; /* One character in z[] */ |
| 226 | int cnt; /* Number of non-wildcard prefix characters */ |
| 227 | u8 wc[4]; /* Wildcard characters */ |
| 228 | sqlite3 *db = pParse->db; /* Database connection */ |
| 229 | sqlite3_value *pVal = 0; |
| 230 | int op; /* Opcode of pRight */ |
| 231 | int rc; /* Result code to return */ |
| 232 | |
| 233 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){ |
| 234 | return 0; |
| 235 | } |
| 236 | #ifdef SQLITE_EBCDIC |
| 237 | if( *pnoCase ) return 0; |
| 238 | #endif |
| 239 | assert( ExprUseXList(pExpr) ); |
| 240 | pList = pExpr->x.pList; |
| 241 | pLeft = pList->a[1].pExpr; |
| 242 | |
| 243 | pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
| 244 | op = pRight->op; |
| 245 | if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){ |
| 246 | Vdbe *pReprepare = pParse->pReprepare; |
| 247 | int iCol = pRight->iColumn; |
| 248 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); |
| 249 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 250 | z = sqlite3_value_text(pVal); |
| 251 | } |
| 252 | sqlite3VdbeReprepareOnBind(pParse->pVdbe, iCol, 0); |
| 253 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 254 | }else if( op==TK_STRING ){ |
| 255 | assert( !ExprHasProperty(pRight, EP_IntValue) ); |
| 256 | z = (u8*)pRight->u.zToken; |
| 257 | } |
| 258 | if( z ){ |
| 259 | /* Count the number of prefix bytes prior to the first wildcard, |
| 260 | ** U+fffd character, or malformed utf-8. If the underlying database |
| 261 | ** has a UTF16LE encoding, then only consider ASCII characters. Note that |
| 262 | ** the encoding of z[] is UTF8 - we are dealing with only UTF8 here in this |
| 263 | ** code, but the database engine itself might be processing content using a |
| 264 | ** different encoding. */ |
| 265 | cnt = 0; |
| 266 | while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ |
| 267 | cnt++; |
| 268 | if( c==wc[3] && z[cnt]>0 && z[cnt]<0x80 ){ |
| 269 | cnt++; |
| 270 | }else if( c>=0x80 ){ |
| 271 | const u8 *z2 = z+cnt-1; |
| 272 | u32 u; |
| 273 | if( c==0xff /* (1) */ |
| 274 | || (u = sqlite3Utf8Read(&z2))>=0xfffd /* (2) */ |
| 275 | || u==0x7ff /* (3) */ |
| 276 | || ENC(db)==SQLITE_UTF16LE /* (4) */ |
| 277 | ){ |
| 278 | /* Do not accept this character as part of the prefix because |
| 279 | ** (1) 0xff is not part of any valid UTF-8 character and the |
| 280 | ** 0xff causes difficult when incrementing to the upper bound |
| 281 | ** (2) 0xfffd is not a valid UTF-8 character and values greater |
| 282 | ** start getting into the surrogate pairs in UTF16BE. |
| 283 | ** (3) When incrementing this code point to the upper bound, |
| 284 | ** that would require adding an extra byte to the UTF-8 |
| 285 | ** (4) UTF16LE does not compare in sorted order. Only |
| 286 | ** UTF16BE and UTF8 have that property. |
| 287 | */ |
| 288 | cnt--; |
| 289 | break; |
| 290 | }else{ |
| 291 | cnt = (int)(z2-z); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* The optimization is possible only if either (1a) the pattern does not |
| 297 | ** begin with a wildcard and if (1b) the non-wildcard prefix does not end |
| 298 | ** with an (illegal) 0xff character, or (2) the pattern does not consist of |
| 299 | ** a single escape character. The (1b) condition is necessary so |
| 300 | ** that we can increment the prefix key to find an upper bound for the |
| 301 | ** range search. The (2) is because the caller assumes that the pattern |
| 302 | ** consists of at least one character after all escapes have been |
| 303 | ** removed. */ |
| 304 | if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && ALWAYS(255!=(u8)z[cnt-1]) ){ |
| 305 | Expr *pPrefix; |
| 306 | |
| 307 | /* A "complete" match if the pattern ends with "*" or "%" */ |
| 308 | *pisComplete = c==wc[0] && z[cnt+1]==0 && ENC(db)!=SQLITE_UTF16LE; |
| 309 | |
| 310 | /* Get the pattern prefix. Remove all escapes from the prefix. */ |
| 311 | pPrefix = sqlite3Expr(db, TK_STRING, (char*)z); |
| 312 | if( pPrefix ){ |
| 313 | int iFrom, iTo; |
| 314 | char *zNew; |
| 315 | assert( !ExprHasProperty(pPrefix, EP_IntValue) ); |
| 316 | zNew = pPrefix->u.zToken; |
| 317 | zNew[cnt] = 0; |
| 318 | for(iFrom=iTo=0; iFrom<cnt; iFrom++){ |
| 319 | if( zNew[iFrom]==wc[3] ) iFrom++; |
| 320 | zNew[iTo++] = zNew[iFrom]; |
| 321 | } |
| 322 | zNew[iTo] = 0; |
| 323 | assert( iTo>0 ); |
| 324 | |
| 325 | /* If the LHS is not an ordinary column with TEXT affinity, then the |
| 326 | ** pattern prefix boundaries (both the start and end boundaries) must |
| 327 | ** not look like a number or the prefix of a number. Otherwise the |
| 328 | ** pattern might might match against a number, which means this |
| 329 | ** optimization will not work. If either the start or end boundary |
| 330 | ** strings has any possibility of matching against a text rendering |
| 331 | ** of a MEM_Int or MEM_Real, then we must return 0. |
| 332 | ** |
| 333 | ** Getting this right has been a persistent source of bugs in the |
| 334 | ** LIKE optimization. See, for example: |
| 335 | ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1 |
| 336 | ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28 |
| 337 | ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07 |
| 338 | ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975 |
| 339 | ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a |
| 340 | ** 2026-10-28 https://sqlite.org/bugs/info/e85ab73aa1483325 |
| 341 | */ |
| 342 | if( pLeft->op!=TK_COLUMN |
| 343 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 344 | || (ALWAYS( ExprUseYTab(pLeft) ) |
| 345 | && ALWAYS(pLeft->y.pTab) |
| 346 | && IsVirtual(pLeft->y.pTab)) /* Might be numeric */ |
| 347 | ){ |
| 348 | int bHasSpace = iTo < (int)strlen((const char*)z); |
| 349 | int x; |
| 350 | assert( zNew[iTo]==0 ); |
| 351 | x = isNumericPrefix(zNew, iTo, bHasSpace); /* start boundary */ |
| 352 | if( x==0 ){ |
| 353 | zNew[iTo-1]++; |
| 354 | x = isNumericPrefix(zNew, iTo, bHasSpace); /* end boundary */ |
| 355 | zNew[iTo-1]--; |
| 356 | } |
| 357 | if( x!=0 ){ |
| 358 | sqlite3ExprDelete(db, pPrefix); |
| 359 | sqlite3ValueFree(pVal); |
| 360 | return 0; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | *ppPrefix = pPrefix; |
| 365 | |
| 366 | /* If the RHS pattern is a bound parameter, make arrangements to |
| 367 | ** reprepare the statement when that parameter is rebound */ |
| 368 | if( op==TK_VARIABLE ){ |
| 369 | Vdbe *v = pParse->pVdbe; |
| 370 | sqlite3VdbeReprepareOnBind(v, pRight->iColumn, 0); |
| 371 | assert( !ExprHasProperty(pRight, EP_IntValue) ); |
| 372 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 373 | /* If the rhs of the LIKE expression is a variable, and the current |
| 374 | ** value of the variable means there is no need to invoke the LIKE |
| 375 | ** function, then no OP_Variable will be added to the program. |
| 376 | ** This causes problems for the sqlite3_bind_parameter_name() |
| 377 | ** API. To work around them, add a dummy OP_Variable here. |
| 378 | */ |
| 379 | int r1 = sqlite3GetTempReg(pParse); |
| 380 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
| 381 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
| 382 | sqlite3ReleaseTempReg(pParse, r1); |
| 383 | } |
| 384 | } |
| 385 | }else{ |
| 386 | z = 0; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | rc = (z!=0); |
| 391 | sqlite3ValueFree(pVal); |
| 392 | return rc; |
| 393 | } |
| 394 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 395 | |
| 396 | /* |
| 397 | ** If pExpr is one of "like", "glob", "match", or "regexp", then |
| 398 | ** return the corresponding SQLITE_INDEX_CONSTRAINT_xxxx value. |
| 399 | ** If not, return 0. |
| 400 | ** |
| 401 | ** pExpr is guaranteed to be a TK_FUNCTION. |
| 402 | */ |
| 403 | int sqlite3ExprIsLikeOperator(const Expr *pExpr){ |
| 404 | static const struct { |
| 405 | const char *zOp; |
| 406 | unsigned char eOp; |
| 407 | } aOp[] = { |
| 408 | { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, |
| 409 | { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, |
| 410 | { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, |
| 411 | { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } |
| 412 | }; |
| 413 | int i; |
| 414 | assert( pExpr->op==TK_FUNCTION ); |
| 415 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 416 | for(i=0; i<ArraySize(aOp); i++){ |
| 417 | if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ |
| 418 | return aOp[i].eOp; |
| 419 | } |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 426 | /* |
| 427 | ** Check to see if the pExpr expression is a form that needs to be passed |
| 428 | ** to the xBestIndex method of virtual tables. Forms of interest include: |
| 429 | ** |
| 430 | ** Expression Virtual Table Operator |
| 431 | ** ----------------------- --------------------------------- |
| 432 | ** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH |
| 433 | ** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB |
| 434 | ** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE |
| 435 | ** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP |
| 436 | ** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE |
| 437 | ** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE |
| 438 | ** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT |
| 439 | ** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT |
| 440 | ** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL |
| 441 | ** |
| 442 | ** In every case, "column" must be a column of a virtual table. If there |
| 443 | ** is a match, set *ppLeft to the "column" expression, set *ppRight to the |
| 444 | ** "expr" expression (even though in forms (6) and (8) the column is on the |
| 445 | ** right and the expression is on the left). Also set *peOp2 to the |
| 446 | ** appropriate virtual table operator. The return value is 1 or 2 if there |
| 447 | ** is a match. The usual return is 1, but if the RHS is also a column |
| 448 | ** of virtual table in forms (5) or (7) then return 2. |
| 449 | ** |
| 450 | ** If the expression matches none of the patterns above, return 0. |
| 451 | */ |
| 452 | static int isAuxiliaryVtabOperator( |
| 453 | sqlite3 *db, /* Parsing context */ |
| 454 | Expr *pExpr, /* Test this expression */ |
| 455 | unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */ |
| 456 | Expr **ppLeft, /* Column expression to left of MATCH/op2 */ |
| 457 | Expr **ppRight /* Expression to left of MATCH/op2 */ |
| 458 | ){ |
| 459 | if( pExpr->op==TK_FUNCTION ){ |
| 460 | ExprList *pList; |
| 461 | Expr *pCol; /* Column reference */ |
| 462 | int i; |
| 463 | |
| 464 | assert( ExprUseXList(pExpr) ); |
| 465 | pList = pExpr->x.pList; |
| 466 | if( pList==0 || pList->nExpr!=2 ){ |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a |
| 471 | ** virtual table on their second argument, which is the same as |
| 472 | ** the left-hand side operand in their in-fix form. |
| 473 | ** |
| 474 | ** vtab_column MATCH expression |
| 475 | ** MATCH(expression,vtab_column) |
| 476 | */ |
| 477 | pCol = pList->a[1].pExpr; |
| 478 | assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) ); |
| 479 | if( ExprIsVtab(pCol) && (i = sqlite3ExprIsLikeOperator(pExpr))!=0 ){ |
| 480 | *peOp2 = i; |
| 481 | *ppRight = pList->a[0].pExpr; |
| 482 | *ppLeft = pCol; |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | /* We can also match against the first column of overloaded |
| 487 | ** functions where xFindFunction returns a value of at least |
| 488 | ** SQLITE_INDEX_CONSTRAINT_FUNCTION. |
| 489 | ** |
| 490 | ** OVERLOADED(vtab_column,expression) |
| 491 | ** |
| 492 | ** Historically, xFindFunction expected to see lower-case function |
| 493 | ** names. But for this use case, xFindFunction is expected to deal |
| 494 | ** with function names in an arbitrary case. |
| 495 | */ |
| 496 | pCol = pList->a[0].pExpr; |
| 497 | assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) ); |
| 498 | assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) ); |
| 499 | if( ExprIsVtab(pCol) ){ |
| 500 | sqlite3_vtab *pVtab; |
| 501 | sqlite3_module *pMod; |
| 502 | void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**); |
| 503 | void *pNotUsed; |
| 504 | pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab; |
| 505 | assert( pVtab!=0 ); |
| 506 | assert( pVtab->pModule!=0 ); |
| 507 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 508 | pMod = (sqlite3_module *)pVtab->pModule; |
| 509 | if( pMod->xFindFunction!=0 ){ |
| 510 | i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed); |
| 511 | if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){ |
| 512 | *peOp2 = i; |
| 513 | *ppRight = pList->a[1].pExpr; |
| 514 | *ppLeft = pCol; |
| 515 | return 1; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | }else if( pExpr->op>=TK_EQ ){ |
| 520 | /* Comparison operators are a common case. Save a few comparisons for |
| 521 | ** that common case by terminating early. */ |
| 522 | assert( TK_NE < TK_EQ ); |
| 523 | assert( TK_ISNOT < TK_EQ ); |
| 524 | assert( TK_NOTNULL < TK_EQ ); |
| 525 | return 0; |
| 526 | }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){ |
| 527 | int res = 0; |
| 528 | Expr *pLeft = pExpr->pLeft; |
| 529 | Expr *pRight = pExpr->pRight; |
| 530 | assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) ); |
| 531 | if( ExprIsVtab(pLeft) ){ |
| 532 | res++; |
| 533 | } |
| 534 | assert( pRight==0 || pRight->op!=TK_COLUMN |
| 535 | || (ExprUseYTab(pRight) && pRight->y.pTab!=0) ); |
| 536 | if( pRight && ExprIsVtab(pRight) ){ |
| 537 | res++; |
| 538 | SWAP(Expr*, pLeft, pRight); |
| 539 | } |
| 540 | *ppLeft = pLeft; |
| 541 | *ppRight = pRight; |
| 542 | if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE; |
| 543 | if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT; |
| 544 | if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL; |
| 545 | return res; |
| 546 | } |
| 547 | return 0; |
| 548 | } |
| 549 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 550 | |
| 551 | /* |
| 552 | ** If the pBase expression originated in the ON or USING clause of |
| 553 | ** a join, then transfer the appropriate markings over to derived. |
| 554 | */ |
| 555 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 556 | if( pDerived && ExprHasProperty(pBase, EP_OuterON|EP_InnerON) ){ |
| 557 | pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON); |
| 558 | pDerived->w.iJoin = pBase->w.iJoin; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | ** Mark term iChild as being a child of term iParent |
| 564 | */ |
| 565 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 566 | pWC->a[iChild].iParent = iParent; |
| 567 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 568 | assert( pWC->a[iParent].nChild < UMXV(pWC->a[0].nChild) ); |
| 569 | pWC->a[iParent].nChild++; |
| 570 | testcase( pWC->a[iParent].nChild == UMXV(pWC->a[0].nChild) ); |
| 571 | |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not |
| 576 | ** a conjunction, then return just pTerm when N==0. If N is exceeds |
| 577 | ** the number of available subterms, return NULL. |
| 578 | */ |
| 579 | static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ |
| 580 | if( pTerm->eOperator!=WO_AND ){ |
| 581 | return N==0 ? pTerm : 0; |
| 582 | } |
| 583 | if( N<pTerm->u.pAndInfo->wc.nTerm ){ |
| 584 | return &pTerm->u.pAndInfo->wc.a[N]; |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | ** Subterms pOne and pTwo are contained within WHERE clause pWC. The |
| 591 | ** two subterms are in disjunction - they are OR-ed together. |
| 592 | ** |
| 593 | ** If these two terms are both of the form: "A op B" with the same |
| 594 | ** A and B values but different operators and if the operators are |
| 595 | ** compatible (if one is = and the other is <, for example) then |
| 596 | ** add a new virtual AND term to pWC that is the combination of the |
| 597 | ** two. |
| 598 | ** |
| 599 | ** Some examples: |
| 600 | ** |
| 601 | ** x<y OR x=y --> x<=y |
| 602 | ** x=y OR x=y --> x=y |
| 603 | ** x<=y OR x<y --> x<=y |
| 604 | ** |
| 605 | ** The following is NOT generated: |
| 606 | ** |
| 607 | ** x<y OR x>y --> x!=y |
| 608 | */ |
| 609 | static void whereCombineDisjuncts( |
| 610 | SrcList *pSrc, /* the FROM clause */ |
| 611 | WhereClause *pWC, /* The complete WHERE clause */ |
| 612 | WhereTerm *pOne, /* First disjunct */ |
| 613 | WhereTerm *pTwo /* Second disjunct */ |
| 614 | ){ |
| 615 | u16 eOp = pOne->eOperator | pTwo->eOperator; |
| 616 | sqlite3 *db; /* Database connection (for malloc) */ |
| 617 | Expr *pNew; /* New virtual expression */ |
| 618 | int op; /* Operator for the combined expression */ |
| 619 | int idxNew; /* Index in pWC of the next virtual term */ |
| 620 | Expr *pA, *pB; /* Expressions associated with pOne and pTwo */ |
| 621 | |
| 622 | if( (pOne->wtFlags | pTwo->wtFlags) & (TERM_VNULL|TERM_LIKEOPT) ){ |
| 623 | testcase( pOne->wtFlags & TERM_VNULL ); |
| 624 | testcase( pOne->wtFlags & TERM_LIKEOPT ); |
| 625 | testcase( pTwo->wtFlags & TERM_VNULL ); |
| 626 | testcase( pTwo->wtFlags & TERM_LIKEOPT ); |
| 627 | return; |
| 628 | } |
| 629 | if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 630 | if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 631 | if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp |
| 632 | && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; |
| 633 | pA = pOne->pExpr; |
| 634 | pB = pTwo->pExpr; |
| 635 | assert( pA->pLeft!=0 && pA->pRight!=0 ); |
| 636 | assert( pB->pLeft!=0 && pB->pRight!=0 ); |
| 637 | if( sqlite3ExprCompare(0,pA->pLeft, pB->pLeft, -1) ) return; |
| 638 | if( sqlite3ExprCompare(0,pA->pRight, pB->pRight,-1) ) return; |
| 639 | if( ExprHasProperty(pA,EP_Commuted)!=ExprHasProperty(pB,EP_Commuted) ){ |
| 640 | return; |
| 641 | } |
| 642 | /* If we reach this point, it means the two subterms can be combined */ |
| 643 | if( (eOp & (eOp-1))!=0 ){ |
| 644 | if( eOp & (WO_LT|WO_LE) ){ |
| 645 | eOp = WO_LE; |
| 646 | }else{ |
| 647 | assert( eOp & (WO_GT|WO_GE) ); |
| 648 | eOp = WO_GE; |
| 649 | } |
| 650 | } |
| 651 | db = pWC->pWInfo->pParse->db; |
| 652 | pNew = sqlite3ExprDup(db, pA, 0); |
| 653 | if( pNew==0 ) return; |
| 654 | for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); } |
| 655 | pNew->op = op; |
| 656 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 657 | exprAnalyze(pSrc, pWC, idxNew); |
| 658 | } |
| 659 | |
| 660 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 661 | /* |
| 662 | ** Analyze a term that consists of two or more OR-connected |
| 663 | ** subterms. So in: |
| 664 | ** |
| 665 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 666 | ** ^^^^^^^^^^^^^^^^^^^^ |
| 667 | ** |
| 668 | ** This routine analyzes terms such as the middle term in the above example. |
| 669 | ** A WhereOrTerm object is computed and attached to the term under |
| 670 | ** analysis, regardless of the outcome of the analysis. Hence: |
| 671 | ** |
| 672 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 673 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
| 674 | ** |
| 675 | ** The term being analyzed must have two or more of OR-connected subterms. |
| 676 | ** A single subterm might be a set of AND-connected sub-subterms. |
| 677 | ** Examples of terms under analysis: |
| 678 | ** |
| 679 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 680 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 681 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 682 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 683 | ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) |
| 684 | ** (F) x>A OR (x=A AND y>=B) |
| 685 | ** |
| 686 | ** CASE 1: |
| 687 | ** |
| 688 | ** If all subterms are of the form T.C=expr for some single column of C and |
| 689 | ** a single table T (as shown in example B above) then create a new virtual |
| 690 | ** term that is an equivalent IN expression. In other words, if the term |
| 691 | ** being analyzed is: |
| 692 | ** |
| 693 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 694 | ** |
| 695 | ** then create a new virtual term like this: |
| 696 | ** |
| 697 | ** x IN (expr1,expr2,expr3) |
| 698 | ** |
| 699 | ** CASE 2: |
| 700 | ** |
| 701 | ** If there are exactly two disjuncts and one side has x>A and the other side |
| 702 | ** has x=A (for the same x and A) then add a new virtual conjunct term to the |
| 703 | ** WHERE clause of the form "x>=A". Example: |
| 704 | ** |
| 705 | ** x>A OR (x=A AND y>B) adds: x>=A |
| 706 | ** |
| 707 | ** The added conjunct can sometimes be helpful in query planning. |
| 708 | ** |
| 709 | ** CASE 3: |
| 710 | ** |
| 711 | ** If all subterms are indexable by a single table T, then set |
| 712 | ** |
| 713 | ** WhereTerm.eOperator = WO_OR |
| 714 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 715 | ** |
| 716 | ** A subterm is "indexable" if it is of the form |
| 717 | ** "T.C <op> <expr>" where C is any column of table T and |
| 718 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 719 | ** A subterm is also indexable if it is an AND of two or more |
| 720 | ** subsubterms at least one of which is indexable. Indexable AND |
| 721 | ** subterms have their eOperator set to WO_AND and they have |
| 722 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 723 | ** |
| 724 | ** From another point of view, "indexable" means that the subterm could |
| 725 | ** potentially be used with an index if an appropriate index exists. |
| 726 | ** This analysis does not consider whether or not the index exists; that |
| 727 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 728 | ** appropriate for indexing exist. |
| 729 | ** |
| 730 | ** All examples A through E above satisfy case 3. But if a term |
| 731 | ** also satisfies case 1 (such as B) we know that the optimizer will |
| 732 | ** always prefer case 1, so in that case we pretend that case 3 is not |
| 733 | ** satisfied. |
| 734 | ** |
| 735 | ** It might be the case that multiple tables are indexable. For example, |
| 736 | ** (E) above is indexable on tables P, Q, and R. |
| 737 | ** |
| 738 | ** Terms that satisfy case 3 are candidates for lookup by using |
| 739 | ** separate indices to find rowids for each subterm and composing |
| 740 | ** the union of all rowids using a RowSet object. This is similar |
| 741 | ** to "bitmap indices" in other database engines. |
| 742 | ** |
| 743 | ** OTHERWISE: |
| 744 | ** |
| 745 | ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to |
| 746 | ** zero. This term is not useful for search. |
| 747 | */ |
| 748 | static void exprAnalyzeOrTerm( |
| 749 | SrcList *pSrc, /* the FROM clause */ |
| 750 | WhereClause *pWC, /* the complete WHERE clause */ |
| 751 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 752 | ){ |
| 753 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 754 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
| 755 | sqlite3 *db = pParse->db; /* Database connection */ |
| 756 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 757 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
| 758 | int i; /* Loop counters */ |
| 759 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 760 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 761 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 762 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 763 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
| 764 | |
| 765 | /* |
| 766 | ** Break the OR clause into its separate subterms. The subterms are |
| 767 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 768 | ** object that is attached to the original OR clause term. |
| 769 | */ |
| 770 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 771 | assert( pExpr->op==TK_OR ); |
| 772 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 773 | if( pOrInfo==0 ) return; |
| 774 | pTerm->wtFlags |= TERM_ORINFO; |
| 775 | pOrWc = &pOrInfo->wc; |
| 776 | memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic)); |
| 777 | sqlite3WhereClauseInit(pOrWc, pWInfo); |
| 778 | sqlite3WhereSplit(pOrWc, pExpr, TK_OR); |
| 779 | sqlite3WhereExprAnalyze(pSrc, pOrWc); |
| 780 | if( db->mallocFailed ) return; |
| 781 | assert( pOrWc->nTerm>=2 ); |
| 782 | |
| 783 | /* |
| 784 | ** Compute the set of tables that might satisfy cases 1 or 3. |
| 785 | */ |
| 786 | indexable = ~(Bitmask)0; |
| 787 | chngToIN = ~(Bitmask)0; |
| 788 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 789 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
| 790 | WhereAndInfo *pAndInfo; |
| 791 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
| 792 | chngToIN = 0; |
| 793 | pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo)); |
| 794 | if( pAndInfo ){ |
| 795 | WhereClause *pAndWC; |
| 796 | WhereTerm *pAndTerm; |
| 797 | int j; |
| 798 | Bitmask b = 0; |
| 799 | pOrTerm->u.pAndInfo = pAndInfo; |
| 800 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 801 | pOrTerm->eOperator = WO_AND; |
| 802 | pOrTerm->leftCursor = -1; |
| 803 | pAndWC = &pAndInfo->wc; |
| 804 | memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic)); |
| 805 | sqlite3WhereClauseInit(pAndWC, pWC->pWInfo); |
| 806 | sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 807 | sqlite3WhereExprAnalyze(pSrc, pAndWC); |
| 808 | pAndWC->pOuter = pWC; |
| 809 | if( !db->mallocFailed ){ |
| 810 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 811 | assert( pAndTerm->pExpr ); |
| 812 | if( allowedOp(pAndTerm->pExpr->op) |
| 813 | || pAndTerm->eOperator==WO_AUX |
| 814 | ){ |
| 815 | b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | indexable &= b; |
| 820 | } |
| 821 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 822 | /* Skip this term for now. We revisit it when we process the |
| 823 | ** corresponding TERM_VIRTUAL term */ |
| 824 | }else{ |
| 825 | Bitmask b; |
| 826 | b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
| 827 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 828 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 829 | b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor); |
| 830 | } |
| 831 | indexable &= b; |
| 832 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
| 833 | chngToIN = 0; |
| 834 | }else{ |
| 835 | chngToIN &= b; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | ** Record the set of tables that satisfy case 3. The set might be |
| 842 | ** empty. |
| 843 | */ |
| 844 | pOrInfo->indexable = indexable; |
| 845 | pTerm->eOperator = WO_OR; |
| 846 | pTerm->leftCursor = -1; |
| 847 | if( indexable ){ |
| 848 | pWC->hasOr = 1; |
| 849 | } |
| 850 | |
| 851 | /* For a two-way OR, attempt to implementation case 2. |
| 852 | */ |
| 853 | if( indexable && pOrWc->nTerm==2 ){ |
| 854 | int iOne = 0; |
| 855 | WhereTerm *pOne; |
| 856 | while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){ |
| 857 | int iTwo = 0; |
| 858 | WhereTerm *pTwo; |
| 859 | while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){ |
| 860 | whereCombineDisjuncts(pSrc, pWC, pOne, pTwo); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 867 | ** we have to do some additional checking to see if case 1 really |
| 868 | ** is satisfied. |
| 869 | ** |
| 870 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 871 | ** that there is no possibility of transforming the OR clause into an |
| 872 | ** IN operator because one or more terms in the OR clause contain |
| 873 | ** something other than == on a column in the single table. The 1-bit |
| 874 | ** case means that every term of the OR clause is of the form |
| 875 | ** "table.column=expr" for some single table. The one bit that is set |
| 876 | ** will correspond to the common table. We still need to check to make |
| 877 | ** sure the same column is used on all terms. The 2-bit case is when |
| 878 | ** the all terms are of the form "table1.column=table2.column". It |
| 879 | ** might be possible to form an IN operator with either table1.column |
| 880 | ** or table2.column as the LHS if either is common to every term of |
| 881 | ** the OR clause. |
| 882 | ** |
| 883 | ** Note that terms of the form "table.column1=table.column2" (the |
| 884 | ** same table on both sizes of the ==) cannot be optimized. |
| 885 | */ |
| 886 | if( chngToIN ){ |
| 887 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 888 | int iColumn = -1; /* Column index on lhs of IN operator */ |
| 889 | int iCursor = -1; /* Table cursor common to all terms */ |
| 890 | int j = 0; /* Loop counter */ |
| 891 | |
| 892 | /* Search for a table and column that appears on one side or the |
| 893 | ** other of the == operator in every subterm. That table and column |
| 894 | ** will be recorded in iCursor and iColumn. There might not be any |
| 895 | ** such table and column. Set okToChngToIN if an appropriate table |
| 896 | ** and column is found but leave okToChngToIN false if not found. |
| 897 | */ |
| 898 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 899 | Expr *pLeft = 0; |
| 900 | pOrTerm = pOrWc->a; |
| 901 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
| 902 | assert( pOrTerm->eOperator & WO_EQ ); |
| 903 | pOrTerm->wtFlags &= ~TERM_OK; |
| 904 | if( pOrTerm->leftCursor==iCursor ){ |
| 905 | /* This is the 2-bit case and we are on the second iteration and |
| 906 | ** current term is from the first iteration. So skip this term. */ |
| 907 | assert( j==1 ); |
| 908 | continue; |
| 909 | } |
| 910 | if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet, |
| 911 | pOrTerm->leftCursor))==0 ){ |
| 912 | /* This term must be of the form t1.a==t2.b where t2 is in the |
| 913 | ** chngToIN set but t1 is not. This term will be either preceded |
| 914 | ** or followed by an inverted copy (t2.b==t1.a). Skip this term |
| 915 | ** and use its inversion. */ |
| 916 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 917 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 918 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 919 | continue; |
| 920 | } |
| 921 | assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 922 | iColumn = pOrTerm->u.x.leftColumn; |
| 923 | iCursor = pOrTerm->leftCursor; |
| 924 | pLeft = pOrTerm->pExpr->pLeft; |
| 925 | break; |
| 926 | } |
| 927 | if( i<0 ){ |
| 928 | /* No candidate table+column was found. This can only occur |
| 929 | ** on the second iteration */ |
| 930 | assert( j==1 ); |
| 931 | assert( IsPowerOfTwo(chngToIN) ); |
| 932 | assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) ); |
| 933 | break; |
| 934 | } |
| 935 | testcase( j==1 ); |
| 936 | |
| 937 | /* We have found a candidate table and column. Check to see if that |
| 938 | ** table and column is common to every term in the OR clause */ |
| 939 | okToChngToIN = 1; |
| 940 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
| 941 | assert( pOrTerm->eOperator & WO_EQ ); |
| 942 | assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 943 | if( pOrTerm->leftCursor!=iCursor ){ |
| 944 | pOrTerm->wtFlags &= ~TERM_OK; |
| 945 | }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR |
| 946 | && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1) |
| 947 | )){ |
| 948 | okToChngToIN = 0; |
| 949 | }else{ |
| 950 | int affLeft, affRight; |
| 951 | /* If the right-hand side is also a column, then the affinities |
| 952 | ** of both right and left sides must be such that no type |
| 953 | ** conversions are required on the right. (Ticket #2249) |
| 954 | */ |
| 955 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 956 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 957 | if( affRight!=0 && affRight!=affLeft ){ |
| 958 | okToChngToIN = 0; |
| 959 | }else{ |
| 960 | pOrTerm->wtFlags |= TERM_OK; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /* At this point, okToChngToIN is true if original pTerm is a |
| 967 | ** candidate to satisfy case 1, though we are not yet certain that |
| 968 | ** the collating sequences are all compatible. Try to construct a |
| 969 | ** new virtual term that is pTerm converted from an OR operator |
| 970 | ** into an IN operator. |
| 971 | ** |
| 972 | ** During construction, verify that the collating sequences on all |
| 973 | ** subterms of the OR are compatible. Omit the construction of the |
| 974 | ** new IN operator if there are any collating sequence mismatches. |
| 975 | */ |
| 976 | if( okToChngToIN ){ |
| 977 | Expr *pDup; /* A transient duplicate expression */ |
| 978 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 979 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 980 | CollSeq *pCollSeq = 0; /* Collating sequence to use */ |
| 981 | Expr *pNew; /* The complete IN operator */ |
| 982 | |
| 983 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 984 | Expr *pThis; |
| 985 | if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue; |
| 986 | assert( pOrTerm->eOperator & WO_EQ ); |
| 987 | assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 988 | assert( pOrTerm->leftCursor==iCursor ); |
| 989 | assert( pOrTerm->u.x.leftColumn==iColumn ); |
| 990 | pThis = pOrTerm->pExpr; |
| 991 | pDup = sqlite3ExprDup(db, pThis->pRight, 0); |
| 992 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
| 993 | if( pLeft==0 ){ |
| 994 | pLeft = pThis->pLeft; |
| 995 | pCollSeq = sqlite3ExprCompareCollSeq(pParse, pThis); |
| 996 | }else{ |
| 997 | assert( 0==sqlite3ExprCompare(pParse, |
| 998 | sqlite3ExprSkipCollate(pThis->pLeft), |
| 999 | sqlite3ExprSkipCollate(pLeft), -1) ); |
| 1000 | if( pCollSeq!=sqlite3ExprCompareCollSeq(pParse, pThis) ){ |
| 1001 | pLeft = 0; /* Collating sequence mismatch */ |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | if( pLeft==0 ){ |
| 1007 | pNew = 0; /* Collating sequence mismatch */ |
| 1008 | }else{ |
| 1009 | pDup = sqlite3ExprDup(db, pLeft, 0); |
| 1010 | if( sqlite3ExprCollSeq(pParse, pDup)!=pCollSeq |
| 1011 | && pCollSeq!=0 |
| 1012 | ){ |
| 1013 | assert( pCollSeq->zName!=0 ); |
| 1014 | pDup = sqlite3ExprAddCollateString(pParse, pDup, pCollSeq->zName); |
| 1015 | } |
| 1016 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0); |
| 1017 | } |
| 1018 | if( pNew ){ |
| 1019 | int idxNew; |
| 1020 | transferJoinMarkings(pNew, pExpr); |
| 1021 | assert( ExprUseXList(pNew) ); |
| 1022 | pNew->x.pList = pList; |
| 1023 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1024 | testcase( idxNew==0 ); |
| 1025 | exprAnalyze(pSrc, pWC, idxNew); |
| 1026 | /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */ |
| 1027 | markTermAsChild(pWC, idxNew, idxTerm); |
| 1028 | }else{ |
| 1029 | sqlite3ExprListDelete(db, pList); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
| 1035 | |
| 1036 | /* |
| 1037 | ** We already know that pExpr is a binary operator where both operands are |
| 1038 | ** column references. This routine checks to see if pExpr is an equivalence |
| 1039 | ** relation: |
| 1040 | ** 1. The SQLITE_Transitive optimization must be enabled |
| 1041 | ** 2. Must be either an == or an IS operator |
| 1042 | ** 3. Not originating in the ON clause of an OUTER JOIN |
| 1043 | ** 4. The operator is not IS or else the query does not contain RIGHT JOIN |
| 1044 | ** 5. The affinities of A and B must be compatible |
| 1045 | ** 6. Both operands use the same collating sequence, and they must not |
| 1046 | ** use explicit COLLATE clauses. |
| 1047 | ** If this routine returns TRUE, that means that the RHS can be substituted |
| 1048 | ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. |
| 1049 | ** This is an optimization. No harm comes from returning 0. But if 1 is |
| 1050 | ** returned when it should not be, then incorrect answers might result. |
| 1051 | */ |
| 1052 | static int termIsEquivalence(Parse *pParse, Expr *pExpr, SrcList *pSrc){ |
| 1053 | char aff1, aff2; |
| 1054 | if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; /* (1) */ |
| 1055 | if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; /* (2) */ |
| 1056 | if( ExprHasProperty(pExpr, EP_OuterON|EP_Collate) ) return 0; /* (3) */ |
| 1057 | assert( pSrc!=0 ); |
| 1058 | if( pExpr->op==TK_IS |
| 1059 | && pSrc->nSrc>=2 |
| 1060 | && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 |
| 1061 | ){ |
| 1062 | return 0; /* (4) */ |
| 1063 | } |
| 1064 | aff1 = sqlite3ExprAffinity(pExpr->pLeft); |
| 1065 | aff2 = sqlite3ExprAffinity(pExpr->pRight); |
| 1066 | if( aff1!=aff2 |
| 1067 | && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2)) |
| 1068 | ){ |
| 1069 | return 0; /* (5) */ |
| 1070 | } |
| 1071 | if( !sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight) ){ |
| 1072 | return 0; /* (6) */ |
| 1073 | } |
| 1074 | return 1; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | ** Recursively walk the expressions of a SELECT statement and generate |
| 1079 | ** a bitmask indicating which tables are used in that expression |
| 1080 | ** tree. |
| 1081 | */ |
| 1082 | static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){ |
| 1083 | Bitmask mask = 0; |
| 1084 | while( pS ){ |
| 1085 | SrcList *pSrc = pS->pSrc; |
| 1086 | mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList); |
| 1087 | mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy); |
| 1088 | mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy); |
| 1089 | mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere); |
| 1090 | mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving); |
| 1091 | if( ALWAYS(pSrc!=0) ){ |
| 1092 | int i; |
| 1093 | for(i=0; i<pSrc->nSrc; i++){ |
| 1094 | if( pSrc->a[i].fg.isSubquery ){ |
| 1095 | mask |= exprSelectUsage(pMaskSet, pSrc->a[i].u4.pSubq->pSelect); |
| 1096 | } |
| 1097 | if( pSrc->a[i].fg.isUsing==0 ){ |
| 1098 | mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn); |
| 1099 | } |
| 1100 | if( pSrc->a[i].fg.isTabFunc ){ |
| 1101 | mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg); |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | pS = pS->pPrior; |
| 1106 | } |
| 1107 | return mask; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | ** Expression pExpr is one operand of a comparison operator that might |
| 1112 | ** be useful for indexing. This routine checks to see if pExpr appears |
| 1113 | ** in any index. Return TRUE (1) if pExpr is an indexed term and return |
| 1114 | ** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor |
| 1115 | ** number of the table that is indexed and aiCurCol[1] to the column number |
| 1116 | ** of the column that is indexed, or XN_EXPR (-2) if an expression is being |
| 1117 | ** indexed. |
| 1118 | ** |
| 1119 | ** If pExpr is a TK_COLUMN column reference, then this routine always returns |
| 1120 | ** true even if that particular column is not indexed, because the column |
| 1121 | ** might be added to an automatic index later. |
| 1122 | */ |
| 1123 | static SQLITE_NOINLINE int exprMightBeIndexed2( |
| 1124 | SrcList *pFrom, /* The FROM clause */ |
| 1125 | int *aiCurCol, /* Write the referenced table cursor and column here */ |
| 1126 | Expr *pExpr, /* An operand of a comparison operator */ |
| 1127 | int j /* Start looking with the j-th pFrom entry */ |
| 1128 | ){ |
| 1129 | Index *pIdx; |
| 1130 | int i; |
| 1131 | int iCur; |
| 1132 | do{ |
| 1133 | iCur = pFrom->a[j].iCursor; |
| 1134 | for(pIdx=pFrom->a[j].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1135 | if( pIdx->aColExpr==0 ) continue; |
| 1136 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 1137 | if( pIdx->aiColumn[i]!=XN_EXPR ) continue; |
| 1138 | assert( pIdx->bHasExpr ); |
| 1139 | if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0 |
| 1140 | && !sqlite3ExprIsConstant(0,pIdx->aColExpr->a[i].pExpr) |
| 1141 | ){ |
| 1142 | aiCurCol[0] = iCur; |
| 1143 | aiCurCol[1] = XN_EXPR; |
| 1144 | return 1; |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | }while( ++j < pFrom->nSrc ); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | static int exprMightBeIndexed( |
| 1152 | SrcList *pFrom, /* The FROM clause */ |
| 1153 | int *aiCurCol, /* Write the referenced table cursor & column here */ |
| 1154 | Expr *pExpr, /* An operand of a comparison operator */ |
| 1155 | int op /* The specific comparison operator */ |
| 1156 | ){ |
| 1157 | int i; |
| 1158 | |
| 1159 | /* If this expression is a vector to the left or right of a |
| 1160 | ** inequality constraint (>, <, >= or <=), perform the processing |
| 1161 | ** on the first element of the vector. */ |
| 1162 | assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE ); |
| 1163 | assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE ); |
| 1164 | assert( op<=TK_GE ); |
| 1165 | if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){ |
| 1166 | assert( ExprUseXList(pExpr) ); |
| 1167 | pExpr = pExpr->x.pList->a[0].pExpr; |
| 1168 | } |
| 1169 | |
| 1170 | if( pExpr->op==TK_COLUMN ){ |
| 1171 | aiCurCol[0] = pExpr->iTable; |
| 1172 | aiCurCol[1] = pExpr->iColumn; |
| 1173 | return 1; |
| 1174 | } |
| 1175 | |
| 1176 | for(i=0; i<pFrom->nSrc; i++){ |
| 1177 | Index *pIdx; |
| 1178 | for(pIdx=pFrom->a[i].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1179 | if( pIdx->aColExpr ){ |
| 1180 | return exprMightBeIndexed2(pFrom,aiCurCol,pExpr,i); |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | /* |
| 1189 | ** The input to this routine is an WhereTerm structure with only the |
| 1190 | ** "pExpr" field filled in. The job of this routine is to analyze the |
| 1191 | ** subexpression and populate all the other fields of the WhereTerm |
| 1192 | ** structure. |
| 1193 | ** |
| 1194 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
| 1195 | ** to the standard form of "X <op> <expr>". |
| 1196 | ** |
| 1197 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1198 | ** columns, then the original expression is unchanged and a new virtual |
| 1199 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1200 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1201 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1202 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1203 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1204 | ** and the copy has idxParent set to the index of the original term. |
| 1205 | */ |
| 1206 | static void exprAnalyze( |
| 1207 | SrcList *pSrc, /* the FROM clause */ |
| 1208 | WhereClause *pWC, /* the WHERE clause */ |
| 1209 | int idxTerm /* Index of the term to be analyzed */ |
| 1210 | ){ |
| 1211 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 1212 | WhereTerm *pTerm; /* The term to be analyzed */ |
| 1213 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
| 1214 | Expr *pExpr; /* The expression to be analyzed */ |
| 1215 | Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */ |
| 1216 | Bitmask prereqAll; /* Prerequisites of pExpr */ |
| 1217 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
| 1218 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1219 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 1220 | int noCase = 0; /* uppercase equivalent to lowercase */ |
| 1221 | int op; /* Top-level operator. pExpr->op */ |
| 1222 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 1223 | sqlite3 *db = pParse->db; /* Database connection */ |
| 1224 | unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */ |
| 1225 | int nLeft; /* Number of elements on left side vector */ |
| 1226 | |
| 1227 | if( db->mallocFailed ){ |
| 1228 | return; |
| 1229 | } |
| 1230 | assert( pWC->nTerm > idxTerm ); |
| 1231 | pTerm = &pWC->a[idxTerm]; |
| 1232 | #ifdef SQLITE_DEBUG |
| 1233 | pTerm->iTerm = idxTerm; |
| 1234 | #endif |
| 1235 | pMaskSet = &pWInfo->sMaskSet; |
| 1236 | pExpr = pTerm->pExpr; |
| 1237 | assert( pExpr!=0 ); /* Because malloc() has not failed */ |
| 1238 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
| 1239 | exprAnalyze_restart: |
| 1240 | pMaskSet->bVarSelect = 0; |
| 1241 | prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft); |
| 1242 | op = pExpr->op; |
| 1243 | if( op==TK_IN ){ |
| 1244 | assert( pExpr->pRight==0 ); |
| 1245 | if( sqlite3ExprCheckIN(pParse, pExpr) ) return; |
| 1246 | if( ExprUseXSelect(pExpr) ){ |
| 1247 | pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect); |
| 1248 | }else{ |
| 1249 | pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList); |
| 1250 | } |
| 1251 | prereqAll = prereqLeft | pTerm->prereqRight; |
| 1252 | }else{ |
| 1253 | pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight); |
| 1254 | if( pExpr->pLeft==0 |
| 1255 | || ExprHasProperty(pExpr, EP_xIsSelect|EP_IfNullRow) |
| 1256 | || pExpr->x.pList!=0 |
| 1257 | ){ |
| 1258 | prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr); |
| 1259 | }else{ |
| 1260 | prereqAll = prereqLeft | pTerm->prereqRight; |
| 1261 | } |
| 1262 | } |
| 1263 | if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT; |
| 1264 | |
| 1265 | #ifdef SQLITE_DEBUG |
| 1266 | if( prereqAll!=sqlite3WhereExprUsageNN(pMaskSet, pExpr) ){ |
| 1267 | printf("\n*** Incorrect prereqAll computed for:\n"); |
| 1268 | sqlite3TreeViewExpr(0,pExpr,0); |
| 1269 | assert( 0 ); |
| 1270 | } |
| 1271 | #endif |
| 1272 | |
| 1273 | if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) ){ |
| 1274 | Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin); |
| 1275 | if( ExprHasProperty(pExpr, EP_OuterON) ){ |
| 1276 | prereqAll |= x; |
| 1277 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1278 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
| 1279 | }else if( (prereqAll>>1)>=x ){ |
| 1280 | ExprClearProperty(pExpr, EP_InnerON); |
| 1281 | } |
| 1282 | } |
| 1283 | pTerm->prereqAll = prereqAll; |
| 1284 | pTerm->leftCursor = -1; |
| 1285 | pTerm->iParent = -1; |
| 1286 | pTerm->eOperator = 0; |
| 1287 | if( allowedOp(op) ){ |
| 1288 | int aiCurCol[2]; |
| 1289 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1290 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
| 1291 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
| 1292 | |
| 1293 | if( pTerm->u.x.iField>0 ){ |
| 1294 | assert( op==TK_IN ); |
| 1295 | assert( pLeft->op==TK_VECTOR ); |
| 1296 | assert( ExprUseXList(pLeft) ); |
| 1297 | pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr; |
| 1298 | } |
| 1299 | |
| 1300 | if( exprMightBeIndexed(pSrc, aiCurCol, pLeft, op) ){ |
| 1301 | pTerm->leftCursor = aiCurCol[0]; |
| 1302 | assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 1303 | pTerm->u.x.leftColumn = aiCurCol[1]; |
| 1304 | pTerm->eOperator = operatorMask(op) & opMask; |
| 1305 | } |
| 1306 | if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; |
| 1307 | if( pRight |
| 1308 | && exprMightBeIndexed(pSrc, aiCurCol, pRight, op) |
| 1309 | && !ExprHasProperty(pRight, EP_FixedCol) |
| 1310 | ){ |
| 1311 | WhereTerm *pNew; |
| 1312 | Expr *pDup; |
| 1313 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
| 1314 | assert( pTerm->u.x.iField==0 ); |
| 1315 | if( pTerm->leftCursor>=0 ){ |
| 1316 | int idxNew; |
| 1317 | pDup = sqlite3ExprDup(db, pExpr, 0); |
| 1318 | if( db->mallocFailed ){ |
| 1319 | sqlite3ExprDelete(db, pDup); |
| 1320 | return; |
| 1321 | } |
| 1322 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1323 | if( idxNew==0 ) return; |
| 1324 | pNew = &pWC->a[idxNew]; |
| 1325 | markTermAsChild(pWC, idxNew, idxTerm); |
| 1326 | if( op==TK_IS ) pNew->wtFlags |= TERM_IS; |
| 1327 | pTerm = &pWC->a[idxTerm]; |
| 1328 | pTerm->wtFlags |= TERM_COPIED; |
| 1329 | assert( pWInfo->pTabList!=0 ); |
| 1330 | if( termIsEquivalence(pParse, pDup, pWInfo->pTabList) ){ |
| 1331 | pTerm->eOperator |= WO_EQUIV; |
| 1332 | eExtraOp = WO_EQUIV; |
| 1333 | } |
| 1334 | }else{ |
| 1335 | pDup = pExpr; |
| 1336 | pNew = pTerm; |
| 1337 | } |
| 1338 | pNew->wtFlags |= exprCommute(pParse, pDup); |
| 1339 | pNew->leftCursor = aiCurCol[0]; |
| 1340 | assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
| 1341 | pNew->u.x.leftColumn = aiCurCol[1]; |
| 1342 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1343 | pNew->prereqRight = prereqLeft | extraRight; |
| 1344 | pNew->prereqAll = prereqAll; |
| 1345 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
| 1346 | }else |
| 1347 | if( op==TK_ISNULL |
| 1348 | && !ExprHasProperty(pExpr,EP_OuterON) |
| 1349 | && 0==sqlite3ExprCanBeNull(pLeft) |
| 1350 | ){ |
| 1351 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 1352 | pExpr->op = TK_TRUEFALSE; /* See tag-20230504-1 */ |
| 1353 | pExpr->u.zToken = "false"; |
| 1354 | ExprSetProperty(pExpr, EP_IsFalse); |
| 1355 | pTerm->prereqAll = 0; |
| 1356 | pTerm->eOperator = 0; |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
| 1361 | /* If a term is the BETWEEN operator, create two new virtual terms |
| 1362 | ** that define the range that the BETWEEN implements. For example: |
| 1363 | ** |
| 1364 | ** a BETWEEN b AND c |
| 1365 | ** |
| 1366 | ** is converted into: |
| 1367 | ** |
| 1368 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1369 | ** |
| 1370 | ** The two new terms are added onto the end of the WhereClause object. |
| 1371 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1372 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1373 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1374 | ** BETWEEN term is skipped. |
| 1375 | */ |
| 1376 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
| 1377 | ExprList *pList; |
| 1378 | int i; |
| 1379 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1380 | assert( ExprUseXList(pExpr) ); |
| 1381 | pList = pExpr->x.pList; |
| 1382 | assert( pList!=0 ); |
| 1383 | assert( pList->nExpr==2 ); |
| 1384 | assert( pWC->a[idxTerm].nChild==0 ); |
| 1385 | for(i=0; i<2; i++){ |
| 1386 | Expr *pNewExpr; |
| 1387 | int idxNew; |
| 1388 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1389 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
| 1390 | sqlite3ExprDup(db, pList->a[i].pExpr, 0)); |
| 1391 | transferJoinMarkings(pNewExpr, pExpr); |
| 1392 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1393 | testcase( idxNew==0 ); |
| 1394 | exprAnalyze(pSrc, pWC, idxNew); |
| 1395 | pTerm = &pWC->a[idxTerm]; |
| 1396 | markTermAsChild(pWC, idxNew, idxTerm); |
| 1397 | } |
| 1398 | } |
| 1399 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
| 1400 | |
| 1401 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1402 | /* Analyze a term that is composed of two or more subterms connected by |
| 1403 | ** an OR operator. |
| 1404 | */ |
| 1405 | else if( pExpr->op==TK_OR && !ExprHasProperty(pExpr, EP_Collate) ){ |
| 1406 | Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); |
| 1407 | if( pAlt!=pExpr ){ |
| 1408 | pTerm->pExpr = pExpr = sqlite3ExprSkipCollateAndLikely(pAlt); |
| 1409 | goto exprAnalyze_restart; |
| 1410 | } |
| 1411 | assert( pWC->op==TK_AND ); |
| 1412 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
| 1413 | pTerm = &pWC->a[idxTerm]; |
| 1414 | } |
| 1415 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1416 | /* The form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1417 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1418 | ** virtual term of that form. |
| 1419 | ** |
| 1420 | ** The virtual term must be tagged with TERM_VNULL. |
| 1421 | */ |
| 1422 | else if( pExpr->op==TK_NOTNULL ){ |
| 1423 | if( pExpr->pLeft->op==TK_COLUMN |
| 1424 | && pExpr->pLeft->iColumn>=0 |
| 1425 | && !ExprHasProperty(pExpr, EP_OuterON) |
| 1426 | ){ |
| 1427 | Expr *pNewExpr; |
| 1428 | Expr *pLeft = pExpr->pLeft; |
| 1429 | int idxNew; |
| 1430 | WhereTerm *pNewTerm; |
| 1431 | |
| 1432 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1433 | sqlite3ExprDup(db, pLeft, 0), |
| 1434 | sqlite3ExprAlloc(db, TK_NULL, 0, 0)); |
| 1435 | |
| 1436 | idxNew = whereClauseInsert(pWC, pNewExpr, |
| 1437 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
| 1438 | if( idxNew ){ |
| 1439 | pNewTerm = &pWC->a[idxNew]; |
| 1440 | pNewTerm->prereqRight = 0; |
| 1441 | pNewTerm->leftCursor = pLeft->iTable; |
| 1442 | pNewTerm->u.x.leftColumn = pLeft->iColumn; |
| 1443 | pNewTerm->eOperator = WO_GT; |
| 1444 | markTermAsChild(pWC, idxNew, idxTerm); |
| 1445 | pTerm = &pWC->a[idxTerm]; |
| 1446 | pTerm->wtFlags |= TERM_COPIED; |
| 1447 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | |
| 1453 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1454 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1455 | ** operator. |
| 1456 | ** |
| 1457 | ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints |
| 1458 | ** |
| 1459 | ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%' |
| 1460 | ** |
| 1461 | ** The last character of the prefix "abc" is incremented to form the |
| 1462 | ** termination condition "abd". If case is not significant (the default |
| 1463 | ** for LIKE) then the lower-bound is made all uppercase and the upper- |
| 1464 | ** bound is made all lowercase so that the bounds also work when comparing |
| 1465 | ** BLOBs. |
| 1466 | */ |
| 1467 | else if( pExpr->op==TK_FUNCTION |
| 1468 | && pWC->op==TK_AND |
| 1469 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1470 | ){ |
| 1471 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1472 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1473 | Expr *pNewExpr1; |
| 1474 | Expr *pNewExpr2; |
| 1475 | int idxNew1; |
| 1476 | int idxNew2; |
| 1477 | const char *zCollSeqName; /* Name of collating sequence */ |
| 1478 | const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; |
| 1479 | |
| 1480 | assert( ExprUseXList(pExpr) ); |
| 1481 | pLeft = pExpr->x.pList->a[1].pExpr; |
| 1482 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
| 1483 | assert( pStr1==0 || !ExprHasProperty(pStr1, EP_IntValue) ); |
| 1484 | assert( pStr2==0 || !ExprHasProperty(pStr2, EP_IntValue) ); |
| 1485 | |
| 1486 | |
| 1487 | /* Convert the lower bound to upper-case and the upper bound to |
| 1488 | ** lower-case (upper-case is less than lower-case in ASCII) so that |
| 1489 | ** the range constraints also work for BLOBs |
| 1490 | */ |
| 1491 | if( noCase && !pParse->db->mallocFailed ){ |
| 1492 | int i; |
| 1493 | char c; |
| 1494 | pTerm->wtFlags |= TERM_LIKE; |
| 1495 | for(i=0; (c = pStr1->u.zToken[i])!=0; i++){ |
| 1496 | pStr1->u.zToken[i] = sqlite3Toupper(c); |
| 1497 | pStr2->u.zToken[i] = sqlite3Tolower(c); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | if( !db->mallocFailed ){ |
| 1502 | u8 *pC; /* Last character before the first wildcard */ |
| 1503 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
| 1504 | if( noCase ){ |
| 1505 | /* The point is to increment the last character before the first |
| 1506 | ** wildcard. But if we increment '@', that will push it into the |
| 1507 | ** alphabetic range where case conversions will mess up the |
| 1508 | ** inequality. To avoid this, make sure to also run the full |
| 1509 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1510 | */ |
| 1511 | if( *pC=='A'-1 ) isComplete = 0; |
| 1512 | *pC = sqlite3UpperToLower[*pC]; |
| 1513 | } |
| 1514 | |
| 1515 | /* Increment the value of the last utf8 character in the prefix. */ |
| 1516 | while( *pC==0xBF && pC>(u8*)pStr2->u.zToken ){ |
| 1517 | *pC = 0x80; |
| 1518 | pC--; |
| 1519 | } |
| 1520 | assert( *pC!=0xFF ); /* isLikeOrGlob() guarantees this */ |
| 1521 | (*pC)++; |
| 1522 | } |
| 1523 | zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY; |
| 1524 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
| 1525 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
| 1526 | sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), |
| 1527 | pStr1); |
| 1528 | transferJoinMarkings(pNewExpr1, pExpr); |
| 1529 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); |
| 1530 | testcase( idxNew1==0 ); |
| 1531 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
| 1532 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
| 1533 | sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), |
| 1534 | pStr2); |
| 1535 | transferJoinMarkings(pNewExpr2, pExpr); |
| 1536 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); |
| 1537 | testcase( idxNew2==0 ); |
| 1538 | exprAnalyze(pSrc, pWC, idxNew1); |
| 1539 | exprAnalyze(pSrc, pWC, idxNew2); |
| 1540 | pTerm = &pWC->a[idxTerm]; |
| 1541 | if( isComplete ){ |
| 1542 | markTermAsChild(pWC, idxNew1, idxTerm); |
| 1543 | markTermAsChild(pWC, idxNew2, idxTerm); |
| 1544 | } |
| 1545 | } |
| 1546 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 1547 | |
| 1548 | /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create |
| 1549 | ** new terms for each component comparison - "a = ?" and "b = ?". The |
| 1550 | ** new terms completely replace the original vector comparison, which is |
| 1551 | ** no longer used. |
| 1552 | ** |
| 1553 | ** This is only required if at least one side of the comparison operation |
| 1554 | ** is not a sub-select. |
| 1555 | ** |
| 1556 | ** tag-20220128a |
| 1557 | */ |
| 1558 | if( (pExpr->op==TK_EQ || pExpr->op==TK_IS) |
| 1559 | && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 |
| 1560 | && sqlite3ExprVectorSize(pExpr->pRight)==nLeft |
| 1561 | && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 |
| 1562 | || (pExpr->pRight->flags & EP_xIsSelect)==0) |
| 1563 | && pWC->op==TK_AND |
| 1564 | ){ |
| 1565 | int i; |
| 1566 | for(i=0; i<nLeft; i++){ |
| 1567 | int idxNew; |
| 1568 | Expr *pNew; |
| 1569 | Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft); |
| 1570 | Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft); |
| 1571 | |
| 1572 | pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight); |
| 1573 | transferJoinMarkings(pNew, pExpr); |
| 1574 | idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_SLICE); |
| 1575 | exprAnalyze(pSrc, pWC, idxNew); |
| 1576 | } |
| 1577 | pTerm = &pWC->a[idxTerm]; |
| 1578 | pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */ |
| 1579 | pTerm->eOperator = WO_ROWVAL; |
| 1580 | } |
| 1581 | |
| 1582 | /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create |
| 1583 | ** a virtual term for each vector component. The expression object |
| 1584 | ** used by each such virtual term is pExpr (the full vector IN(...) |
| 1585 | ** expression). The WhereTerm.u.x.iField variable identifies the index within |
| 1586 | ** the vector on the LHS that the virtual term represents. |
| 1587 | ** |
| 1588 | ** This only works if the RHS is a simple SELECT (not a compound) that does |
| 1589 | ** not use window functions or a LIMIT clause. |
| 1590 | */ |
| 1591 | else if( pExpr->op==TK_IN |
| 1592 | && pTerm->u.x.iField==0 |
| 1593 | && pExpr->pLeft->op==TK_VECTOR |
| 1594 | && ALWAYS( ExprUseXSelect(pExpr) ) |
| 1595 | && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values)) |
| 1596 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 1597 | && pExpr->x.pSelect->pWin==0 |
| 1598 | #endif |
| 1599 | && (pExpr->x.pSelect->selFlags & SF_MinMaxAgg)==0 |
| 1600 | && pExpr->x.pSelect->pLimit==0 |
| 1601 | /* ^-- See bug 2026-11-04T00:59:22Z */ |
| 1602 | && pWC->op==TK_AND |
| 1603 | && pExpr->x.pSelect->pEList->nExpr <= UMXV(pTerm->nChild) |
| 1604 | /* ^-- See bug 2026-06-04T10:00:49Z */ |
| 1605 | ){ |
| 1606 | int i; |
| 1607 | assert( pTerm->nChild==0 ); |
| 1608 | for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){ |
| 1609 | int idxNew; |
| 1610 | idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL|TERM_SLICE); |
| 1611 | pWC->a[idxNew].u.x.iField = i+1; |
| 1612 | exprAnalyze(pSrc, pWC, idxNew); |
| 1613 | markTermAsChild(pWC, idxNew, idxTerm); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1618 | /* Add a WO_AUX auxiliary term to the constraint set if the |
| 1619 | ** current expression is of the form "column OP expr" where OP |
| 1620 | ** is an operator that gets passed into virtual tables but which is |
| 1621 | ** not normally optimized for ordinary tables. In other words, OP |
| 1622 | ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL. |
| 1623 | ** This information is used by the xBestIndex methods of |
| 1624 | ** virtual tables. The native query optimizer does not attempt |
| 1625 | ** to do anything with MATCH functions. |
| 1626 | */ |
| 1627 | else if( pWC->op==TK_AND ){ |
| 1628 | Expr *pRight = 0, *pLeft = 0; |
| 1629 | int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight); |
| 1630 | while( res-- > 0 ){ |
| 1631 | int idxNew; |
| 1632 | WhereTerm *pNewTerm; |
| 1633 | Bitmask prereqColumn, prereqExpr; |
| 1634 | |
| 1635 | prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight); |
| 1636 | prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft); |
| 1637 | if( (prereqExpr & prereqColumn)==0 ){ |
| 1638 | Expr *pNewExpr; |
| 1639 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1640 | 0, sqlite3ExprDup(db, pRight, 0)); |
| 1641 | if( ExprHasProperty(pExpr, EP_OuterON) && pNewExpr ){ |
| 1642 | ExprSetProperty(pNewExpr, EP_OuterON); |
| 1643 | pNewExpr->w.iJoin = pExpr->w.iJoin; |
| 1644 | } |
| 1645 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1646 | testcase( idxNew==0 ); |
| 1647 | pNewTerm = &pWC->a[idxNew]; |
| 1648 | pNewTerm->prereqRight = prereqExpr | extraRight; |
| 1649 | pNewTerm->leftCursor = pLeft->iTable; |
| 1650 | pNewTerm->u.x.leftColumn = pLeft->iColumn; |
| 1651 | pNewTerm->eOperator = WO_AUX; |
| 1652 | pNewTerm->eMatchOp = eOp2; |
| 1653 | markTermAsChild(pWC, idxNew, idxTerm); |
| 1654 | pTerm = &pWC->a[idxTerm]; |
| 1655 | pTerm->wtFlags |= TERM_COPIED; |
| 1656 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1657 | } |
| 1658 | SWAP(Expr*, pLeft, pRight); |
| 1659 | } |
| 1660 | } |
| 1661 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1662 | |
| 1663 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1664 | ** an index for tables to the left of the join. |
| 1665 | */ |
| 1666 | testcase( pTerm!=&pWC->a[idxTerm] ); |
| 1667 | pTerm = &pWC->a[idxTerm]; |
| 1668 | pTerm->prereqRight |= extraRight; |
| 1669 | } |
| 1670 | |
| 1671 | /*************************************************************************** |
| 1672 | ** Routines with file scope above. Interface to the rest of the where.c |
| 1673 | ** subsystem follows. |
| 1674 | ***************************************************************************/ |
| 1675 | |
| 1676 | /* |
| 1677 | ** This routine identifies subexpressions in the WHERE clause where |
| 1678 | ** each subexpression is separated by the AND operator or some other |
| 1679 | ** operator specified in the op parameter. The WhereClause structure |
| 1680 | ** is filled with pointers to subexpressions. For example: |
| 1681 | ** |
| 1682 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 1683 | ** \________/ \_______________/ \________________/ |
| 1684 | ** slot[0] slot[1] slot[2] |
| 1685 | ** |
| 1686 | ** The original WHERE clause in pExpr is unaltered. All this routine |
| 1687 | ** does is make slot[] entries point to substructure within pExpr. |
| 1688 | ** |
| 1689 | ** In the previous sentence and in the diagram, "slot[]" refers to |
| 1690 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
| 1691 | ** all terms of the WHERE clause. |
| 1692 | */ |
| 1693 | void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 1694 | Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pExpr); |
| 1695 | pWC->op = op; |
| 1696 | assert( pE2!=0 || pExpr==0 ); |
| 1697 | if( pE2==0 ) return; |
| 1698 | if( pE2->op!=op ){ |
| 1699 | whereClauseInsert(pWC, pExpr, 0); |
| 1700 | }else{ |
| 1701 | sqlite3WhereSplit(pWC, pE2->pLeft, op); |
| 1702 | sqlite3WhereSplit(pWC, pE2->pRight, op); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | ** Add either a LIMIT (if eMatchOp==SQLITE_INDEX_CONSTRAINT_LIMIT) or |
| 1708 | ** OFFSET (if eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET) term to the |
| 1709 | ** where-clause passed as the first argument. The value for the term |
| 1710 | ** is found in register iReg. |
| 1711 | ** |
| 1712 | ** In the common case where the value is a simple integer |
| 1713 | ** (example: "LIMIT 5 OFFSET 10") then the expression codes as a |
| 1714 | ** TK_INTEGER so that it will be available to sqlite3_vtab_rhs_value(). |
| 1715 | ** If not, then it codes as a TK_REGISTER expression. |
| 1716 | */ |
| 1717 | static void whereAddLimitExpr( |
| 1718 | WhereClause *pWC, /* Add the constraint to this WHERE clause */ |
| 1719 | int iReg, /* Register that will hold value of the limit/offset */ |
| 1720 | Expr *pExpr, /* Expression that defines the limit/offset */ |
| 1721 | int iCsr, /* Cursor to which the constraint applies */ |
| 1722 | int eMatchOp /* SQLITE_INDEX_CONSTRAINT_LIMIT or _OFFSET */ |
| 1723 | ){ |
| 1724 | Parse *pParse = pWC->pWInfo->pParse; |
| 1725 | sqlite3 *db = pParse->db; |
| 1726 | Expr *pNew; |
| 1727 | int iVal = 0; |
| 1728 | |
| 1729 | if( sqlite3ExprIsInteger(pExpr, &iVal, pParse, 0) && iVal>=0 ){ |
| 1730 | Expr *pVal = sqlite3ExprInt32(db, iVal); |
| 1731 | if( pVal==0 ) return; |
| 1732 | pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 1733 | }else{ |
| 1734 | Expr *pVal = sqlite3ExprAlloc(db, TK_REGISTER, 0, 0); |
| 1735 | if( pVal==0 ) return; |
| 1736 | pVal->iTable = iReg; |
| 1737 | pNew = sqlite3PExpr(pParse, TK_MATCH, 0, pVal); |
| 1738 | } |
| 1739 | if( pNew ){ |
| 1740 | WhereTerm *pTerm; |
| 1741 | int idx; |
| 1742 | idx = whereClauseInsert(pWC, pNew, TERM_DYNAMIC|TERM_VIRTUAL); |
| 1743 | pTerm = &pWC->a[idx]; |
| 1744 | pTerm->leftCursor = iCsr; |
| 1745 | pTerm->eOperator = WO_AUX; |
| 1746 | pTerm->eMatchOp = eMatchOp; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | ** Possibly add terms corresponding to the LIMIT and OFFSET clauses of the |
| 1752 | ** SELECT statement passed as the second argument. These terms are only |
| 1753 | ** added if: |
| 1754 | ** |
| 1755 | ** 1. The SELECT statement has a LIMIT clause, and |
| 1756 | ** 2. The SELECT statement is not an aggregate or DISTINCT query, and |
| 1757 | ** 3. The SELECT statement has exactly one object in its FROM clause, and |
| 1758 | ** that object is a virtual table, and |
| 1759 | ** 4. There are no terms in the WHERE clause that will not be passed |
| 1760 | ** to the virtual table xBestIndex method. |
| 1761 | ** 5. The ORDER BY clause, if any, will be made available to the xBestIndex |
| 1762 | ** method. |
| 1763 | ** |
| 1764 | ** LIMIT and OFFSET terms are ignored by most of the planner code. They |
| 1765 | ** exist only so that they may be passed to the xBestIndex method of the |
| 1766 | ** single virtual table in the FROM clause of the SELECT. |
| 1767 | */ |
| 1768 | void SQLITE_NOINLINE sqlite3WhereAddLimit(WhereClause *pWC, Select *p){ |
| 1769 | assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */ |
| 1770 | if( p->pGroupBy==0 |
| 1771 | && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */ |
| 1772 | && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pSTab)) /* 3 */ |
| 1773 | ){ |
| 1774 | ExprList *pOrderBy = p->pOrderBy; |
| 1775 | int iCsr = p->pSrc->a[0].iCursor; |
| 1776 | int ii; |
| 1777 | |
| 1778 | /* Check condition (4). Return early if it is not met. */ |
| 1779 | for(ii=0; ii<pWC->nTerm; ii++){ |
| 1780 | if( pWC->a[ii].wtFlags & TERM_CODED ){ |
| 1781 | /* This term is a vector operation that has been decomposed into |
| 1782 | ** other, subsequent terms. It can be ignored. See tag-20220128a */ |
| 1783 | assert( pWC->a[ii].wtFlags & TERM_VIRTUAL ); |
| 1784 | assert( pWC->a[ii].eOperator==WO_ROWVAL ); |
| 1785 | continue; |
| 1786 | } |
| 1787 | if( pWC->a[ii].nChild ){ |
| 1788 | /* If this term has child terms, then they are also part of the |
| 1789 | ** pWC->a[] array. So this term can be ignored, as a LIMIT clause |
| 1790 | ** will only be added if each of the child terms passes the |
| 1791 | ** (leftCursor==iCsr) test below. */ |
| 1792 | continue; |
| 1793 | } |
| 1794 | if( pWC->a[ii].leftCursor==iCsr && pWC->a[ii].prereqRight==0 ) continue; |
| 1795 | |
| 1796 | /* If this term has a parent with exactly one child, and the parent will |
| 1797 | ** be passed through to xBestIndex, then this term can be ignored. */ |
| 1798 | if( pWC->a[ii].iParent>=0 ){ |
| 1799 | WhereTerm *pParent = &pWC->a[ pWC->a[ii].iParent ]; |
| 1800 | if( pParent->leftCursor==iCsr |
| 1801 | && pParent->prereqRight==0 |
| 1802 | && pParent->nChild==1 |
| 1803 | ){ |
| 1804 | continue; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | /* This term will not be passed through. Do not add a LIMIT clause. */ |
| 1809 | return; |
| 1810 | } |
| 1811 | |
| 1812 | /* Check condition (5). Return early if it is not met. */ |
| 1813 | if( pOrderBy ){ |
| 1814 | for(ii=0; ii<pOrderBy->nExpr; ii++){ |
| 1815 | Expr *pExpr = pOrderBy->a[ii].pExpr; |
| 1816 | if( pExpr->op!=TK_COLUMN ) return; |
| 1817 | if( pExpr->iTable!=iCsr ) return; |
| 1818 | if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return; |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | /* All conditions are met. Add the terms to the where-clause object. */ |
| 1823 | assert( p->pLimit->op==TK_LIMIT ); |
| 1824 | if( p->iOffset!=0 && (p->selFlags & SF_Compound)==0 ){ |
| 1825 | whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight, |
| 1826 | iCsr, SQLITE_INDEX_CONSTRAINT_OFFSET); |
| 1827 | } |
| 1828 | if( p->iOffset==0 || (p->selFlags & SF_Compound)==0 ){ |
| 1829 | whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft, |
| 1830 | iCsr, SQLITE_INDEX_CONSTRAINT_LIMIT); |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | ** Initialize a preallocated WhereClause structure. |
| 1837 | */ |
| 1838 | void sqlite3WhereClauseInit( |
| 1839 | WhereClause *pWC, /* The WhereClause to be initialized */ |
| 1840 | WhereInfo *pWInfo /* The WHERE processing context */ |
| 1841 | ){ |
| 1842 | pWC->pWInfo = pWInfo; |
| 1843 | pWC->hasOr = 0; |
| 1844 | pWC->pOuter = 0; |
| 1845 | pWC->nTerm = 0; |
| 1846 | pWC->nBase = 0; |
| 1847 | pWC->nSlot = ArraySize(pWC->aStatic); |
| 1848 | pWC->a = pWC->aStatic; |
| 1849 | } |
| 1850 | |
| 1851 | /* |
| 1852 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 1853 | ** itself is not freed. This routine is the inverse of |
| 1854 | ** sqlite3WhereClauseInit(). |
| 1855 | */ |
| 1856 | void sqlite3WhereClauseClear(WhereClause *pWC){ |
| 1857 | sqlite3 *db = pWC->pWInfo->pParse->db; |
| 1858 | assert( pWC->nTerm>=pWC->nBase ); |
| 1859 | if( pWC->nTerm>0 ){ |
| 1860 | WhereTerm *a = pWC->a; |
| 1861 | WhereTerm *aLast = &pWC->a[pWC->nTerm-1]; |
| 1862 | #ifdef SQLITE_DEBUG |
| 1863 | int i; |
| 1864 | /* Verify that every term past pWC->nBase is virtual */ |
| 1865 | for(i=pWC->nBase; i<pWC->nTerm; i++){ |
| 1866 | assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 ); |
| 1867 | } |
| 1868 | #endif |
| 1869 | while(1){ |
| 1870 | assert( a->eMatchOp==0 || a->eOperator==WO_AUX ); |
| 1871 | if( a->wtFlags & TERM_DYNAMIC ){ |
| 1872 | sqlite3ExprDelete(db, a->pExpr); |
| 1873 | } |
| 1874 | if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){ |
| 1875 | if( a->wtFlags & TERM_ORINFO ){ |
| 1876 | assert( (a->wtFlags & TERM_ANDINFO)==0 ); |
| 1877 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 1878 | }else{ |
| 1879 | assert( (a->wtFlags & TERM_ANDINFO)!=0 ); |
| 1880 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 1881 | } |
| 1882 | } |
| 1883 | if( a==aLast ) break; |
| 1884 | a++; |
| 1885 | } |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | /* |
| 1891 | ** These routines walk (recursively) an expression tree and generate |
| 1892 | ** a bitmask indicating which tables are used in that expression |
| 1893 | ** tree. |
| 1894 | ** |
| 1895 | ** sqlite3WhereExprUsage(MaskSet, Expr) -> |
| 1896 | ** |
| 1897 | ** Return a Bitmask of all tables referenced by Expr. Expr can be |
| 1898 | ** be NULL, in which case 0 is returned. |
| 1899 | ** |
| 1900 | ** sqlite3WhereExprUsageNN(MaskSet, Expr) -> |
| 1901 | ** |
| 1902 | ** Same as sqlite3WhereExprUsage() except that Expr must not be |
| 1903 | ** NULL. The "NN" suffix on the name stands for "Not Null". |
| 1904 | ** |
| 1905 | ** sqlite3WhereExprListUsage(MaskSet, ExprList) -> |
| 1906 | ** |
| 1907 | ** Return a Bitmask of all tables referenced by every expression |
| 1908 | ** in the expression list ExprList. ExprList can be NULL, in which |
| 1909 | ** case 0 is returned. |
| 1910 | ** |
| 1911 | ** sqlite3WhereExprUsageFull(MaskSet, ExprList) -> |
| 1912 | ** |
| 1913 | ** Internal use only. Called only by sqlite3WhereExprUsageNN() for |
| 1914 | ** complex expressions that require pushing register values onto |
| 1915 | ** the stack. Many calls to sqlite3WhereExprUsageNN() do not need |
| 1916 | ** the more complex analysis done by this routine. Hence, the |
| 1917 | ** computations done by this routine are broken out into a separate |
| 1918 | ** "no-inline" function to avoid the stack push overhead in the |
| 1919 | ** common case where it is not needed. |
| 1920 | */ |
| 1921 | static SQLITE_NOINLINE Bitmask sqlite3WhereExprUsageFull( |
| 1922 | WhereMaskSet *pMaskSet, |
| 1923 | Expr *p |
| 1924 | ){ |
| 1925 | Bitmask mask; |
| 1926 | mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0; |
| 1927 | if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft); |
| 1928 | if( p->pRight ){ |
| 1929 | mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight); |
| 1930 | assert( p->x.pList==0 ); |
| 1931 | }else if( ExprUseXSelect(p) ){ |
| 1932 | if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1; |
| 1933 | mask |= exprSelectUsage(pMaskSet, p->x.pSelect); |
| 1934 | }else if( p->x.pList ){ |
| 1935 | mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList); |
| 1936 | } |
| 1937 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 1938 | if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){ |
| 1939 | assert( p->y.pWin!=0 ); |
| 1940 | mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition); |
| 1941 | mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy); |
| 1942 | mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter); |
| 1943 | } |
| 1944 | #endif |
| 1945 | return mask; |
| 1946 | } |
| 1947 | Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){ |
| 1948 | if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){ |
| 1949 | return sqlite3WhereGetMask(pMaskSet, p->iTable); |
| 1950 | }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){ |
| 1951 | assert( p->op!=TK_IF_NULL_ROW ); |
| 1952 | return 0; |
| 1953 | } |
| 1954 | return sqlite3WhereExprUsageFull(pMaskSet, p); |
| 1955 | } |
| 1956 | Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 1957 | return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0; |
| 1958 | } |
| 1959 | Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
| 1960 | int i; |
| 1961 | Bitmask mask = 0; |
| 1962 | if( pList ){ |
| 1963 | for(i=0; i<pList->nExpr; i++){ |
| 1964 | mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr); |
| 1965 | } |
| 1966 | } |
| 1967 | return mask; |
| 1968 | } |
| 1969 | |
| 1970 | |
| 1971 | /* |
| 1972 | ** Call exprAnalyze on all terms in a WHERE clause. |
| 1973 | ** |
| 1974 | ** Note that exprAnalyze() might add new virtual terms onto the |
| 1975 | ** end of the WHERE clause. We do not want to analyze these new |
| 1976 | ** virtual terms, so start analyzing at the end and work forward |
| 1977 | ** so that the added virtual terms are never processed. |
| 1978 | */ |
| 1979 | void sqlite3WhereExprAnalyze( |
| 1980 | SrcList *pTabList, /* the FROM clause */ |
| 1981 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 1982 | ){ |
| 1983 | int i; |
| 1984 | for(i=pWC->nTerm-1; i>=0; i--){ |
| 1985 | exprAnalyze(pTabList, pWC, i); |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | /* |
| 1990 | ** For table-valued-functions, transform the function arguments into |
| 1991 | ** new WHERE clause terms. |
| 1992 | ** |
| 1993 | ** Each function argument translates into an equality constraint against |
| 1994 | ** a HIDDEN column in the table. |
| 1995 | */ |
| 1996 | void sqlite3WhereTabFuncArgs( |
| 1997 | Parse *pParse, /* Parsing context */ |
| 1998 | SrcItem *pItem, /* The FROM clause term to process */ |
| 1999 | WhereClause *pWC /* Xfer function arguments to here */ |
| 2000 | ){ |
| 2001 | Table *pTab; |
| 2002 | int j, k; |
| 2003 | ExprList *pArgs; |
| 2004 | Expr *pColRef; |
| 2005 | Expr *pTerm; |
| 2006 | if( pItem->fg.isTabFunc==0 ) return; |
| 2007 | pTab = pItem->pSTab; |
| 2008 | assert( pTab!=0 ); |
| 2009 | if( !IsVirtual(pTab) ){ |
| 2010 | sqlite3ErrorMsg(pParse, "'%s' is not a function", pItem->zName); |
| 2011 | return; |
| 2012 | } |
| 2013 | pArgs = pItem->u1.pFuncArg; |
| 2014 | if( pArgs==0 ) return; |
| 2015 | for(j=k=0; j<pArgs->nExpr; j++){ |
| 2016 | Expr *pRhs; |
| 2017 | u32 joinType; |
| 2018 | while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;} |
| 2019 | if( k>=pTab->nCol ){ |
| 2020 | sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d", |
| 2021 | pTab->zName, j); |
| 2022 | return; |
| 2023 | } |
| 2024 | pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0); |
| 2025 | if( pColRef==0 ) return; |
| 2026 | pColRef->iTable = pItem->iCursor; |
| 2027 | pColRef->iColumn = k++; |
| 2028 | assert( ExprUseYTab(pColRef) ); |
| 2029 | pColRef->y.pTab = pTab; |
| 2030 | pItem->colUsed |= sqlite3ExprColUsed(pColRef); |
| 2031 | pRhs = sqlite3PExpr(pParse, TK_UPLUS, |
| 2032 | sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0); |
| 2033 | pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, pRhs); |
| 2034 | if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){ |
| 2035 | testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */ |
| 2036 | testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */ |
| 2037 | joinType = EP_OuterON; |
| 2038 | }else{ |
| 2039 | testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */ |
| 2040 | joinType = EP_InnerON; |
| 2041 | } |
| 2042 | sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType); |
| 2043 | whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | ** Return true if the WhereLoop pLoop can be use a Bloom filter. |
| 2049 | ** tag-202607231411 |
| 2050 | */ |
| 2051 | int sqlite3WhereLoopBloomable(const WhereLoop *pLoop){ |
| 2052 | if( pLoop->wsFlags & WHERE_IPK ) return 1; |
| 2053 | if( NEVER((pLoop->wsFlags & WHERE_INDEXED)==0) ) return 0; |
| 2054 | return sqlite3IndexBloomable(pLoop->u.btree.pIndex, pLoop->u.btree.nEq); |
| 2055 | } |