1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains routines used for analyzing expressions and 13 ** for generating VDBE code that evaluates expressions in SQLite. 14 */ 15 #include "sqliteInt.h" 16 17 /* Forward declarations */ 18 static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); 19 static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree); 20 21 /* 22 ** Return the affinity character for a single column of a table. 23 */ 24 char sqlite3TableColumnAffinity(Table *pTab, int iCol){ 25 assert( iCol<pTab->nCol ); 26 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER; 27 } 28 29 /* 30 ** Return the 'affinity' of the expression pExpr if any. 31 ** 32 ** If pExpr is a column, a reference to a column via an 'AS' alias, 33 ** or a sub-select with a column as the return value, then the 34 ** affinity of that column is returned. Otherwise, 0x00 is returned, 35 ** indicating no affinity for the expression. 36 ** 37 ** i.e. the WHERE clause expressions in the following statements all 38 ** have an affinity: 39 ** 40 ** CREATE TABLE t1(a); 41 ** SELECT * FROM t1 WHERE a; 42 ** SELECT a AS b FROM t1 WHERE b; 43 ** SELECT * FROM t1 WHERE (select a from t1); 44 */ 45 char sqlite3ExprAffinity(Expr *pExpr){ 46 int op; 47 pExpr = sqlite3ExprSkipCollate(pExpr); 48 if( pExpr->flags & EP_Generic ) return 0; 49 op = pExpr->op; 50 if( op==TK_SELECT ){ 51 assert( pExpr->flags&EP_xIsSelect ); 52 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); 53 } 54 if( op==TK_REGISTER ) op = pExpr->op2; 55 #ifndef SQLITE_OMIT_CAST 56 if( op==TK_CAST ){ 57 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 58 return sqlite3AffinityType(pExpr->u.zToken, 0); 59 } 60 #endif 61 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){ 62 return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn); 63 } 64 if( op==TK_SELECT_COLUMN ){ 65 assert( pExpr->pLeft->flags&EP_xIsSelect ); 66 return sqlite3ExprAffinity( 67 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr 68 ); 69 } 70 return pExpr->affinity; 71 } 72 73 /* 74 ** Set the collating sequence for expression pExpr to be the collating 75 ** sequence named by pToken. Return a pointer to a new Expr node that 76 ** implements the COLLATE operator. 77 ** 78 ** If a memory allocation error occurs, that fact is recorded in pParse->db 79 ** and the pExpr parameter is returned unchanged. 80 */ 81 Expr *sqlite3ExprAddCollateToken( 82 Parse *pParse, /* Parsing context */ 83 Expr *pExpr, /* Add the "COLLATE" clause to this expression */ 84 const Token *pCollName, /* Name of collating sequence */ 85 int dequote /* True to dequote pCollName */ 86 ){ 87 if( pCollName->n>0 ){ 88 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); 89 if( pNew ){ 90 pNew->pLeft = pExpr; 91 pNew->flags |= EP_Collate|EP_Skip; 92 pExpr = pNew; 93 } 94 } 95 return pExpr; 96 } 97 Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ 98 Token s; 99 assert( zC!=0 ); 100 sqlite3TokenInit(&s, (char*)zC); 101 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); 102 } 103 104 /* 105 ** Skip over any TK_COLLATE operators and any unlikely() 106 ** or likelihood() function at the root of an expression. 107 */ 108 Expr *sqlite3ExprSkipCollate(Expr *pExpr){ 109 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ 110 if( ExprHasProperty(pExpr, EP_Unlikely) ){ 111 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 112 assert( pExpr->x.pList->nExpr>0 ); 113 assert( pExpr->op==TK_FUNCTION ); 114 pExpr = pExpr->x.pList->a[0].pExpr; 115 }else{ 116 assert( pExpr->op==TK_COLLATE ); 117 pExpr = pExpr->pLeft; 118 } 119 } 120 return pExpr; 121 } 122 123 /* 124 ** Return the collation sequence for the expression pExpr. If 125 ** there is no defined collating sequence, return NULL. 126 ** 127 ** See also: sqlite3ExprNNCollSeq() 128 ** 129 ** The sqlite3ExprNNCollSeq() works the same exact that it returns the 130 ** default collation if pExpr has no defined collation. 131 ** 132 ** The collating sequence might be determined by a COLLATE operator 133 ** or by the presence of a column with a defined collating sequence. 134 ** COLLATE operators take first precedence. Left operands take 135 ** precedence over right operands. 136 */ 137 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 138 sqlite3 *db = pParse->db; 139 CollSeq *pColl = 0; 140 Expr *p = pExpr; 141 while( p ){ 142 int op = p->op; 143 if( p->flags & EP_Generic ) break; 144 if( op==TK_CAST || op==TK_UPLUS ){ 145 p = p->pLeft; 146 continue; 147 } 148 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){ 149 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); 150 break; 151 } 152 if( (op==TK_AGG_COLUMN || op==TK_COLUMN 153 || op==TK_REGISTER || op==TK_TRIGGER) 154 && p->pTab!=0 155 ){ 156 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally 157 ** a TK_COLUMN but was previously evaluated and cached in a register */ 158 int j = p->iColumn; 159 if( j>=0 ){ 160 const char *zColl = p->pTab->aCol[j].zColl; 161 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); 162 } 163 break; 164 } 165 if( p->flags & EP_Collate ){ 166 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ 167 p = p->pLeft; 168 }else{ 169 Expr *pNext = p->pRight; 170 /* The Expr.x union is never used at the same time as Expr.pRight */ 171 assert( p->x.pList==0 || p->pRight==0 ); 172 /* p->flags holds EP_Collate and p->pLeft->flags does not. And 173 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at 174 ** least one EP_Collate. Thus the following two ALWAYS. */ 175 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){ 176 int i; 177 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){ 178 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ 179 pNext = p->x.pList->a[i].pExpr; 180 break; 181 } 182 } 183 } 184 p = pNext; 185 } 186 }else{ 187 break; 188 } 189 } 190 if( sqlite3CheckCollSeq(pParse, pColl) ){ 191 pColl = 0; 192 } 193 return pColl; 194 } 195 196 /* 197 ** Return the collation sequence for the expression pExpr. If 198 ** there is no defined collating sequence, return a pointer to the 199 ** defautl collation sequence. 200 ** 201 ** See also: sqlite3ExprCollSeq() 202 ** 203 ** The sqlite3ExprCollSeq() routine works the same except that it 204 ** returns NULL if there is no defined collation. 205 */ 206 CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){ 207 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr); 208 if( p==0 ) p = pParse->db->pDfltColl; 209 assert( p!=0 ); 210 return p; 211 } 212 213 /* 214 ** Return TRUE if the two expressions have equivalent collating sequences. 215 */ 216 int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){ 217 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1); 218 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2); 219 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0; 220 } 221 222 /* 223 ** pExpr is an operand of a comparison operator. aff2 is the 224 ** type affinity of the other operand. This routine returns the 225 ** type affinity that should be used for the comparison operator. 226 */ 227 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 228 char aff1 = sqlite3ExprAffinity(pExpr); 229 if( aff1 && aff2 ){ 230 /* Both sides of the comparison are columns. If one has numeric 231 ** affinity, use that. Otherwise use no affinity. 232 */ 233 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ 234 return SQLITE_AFF_NUMERIC; 235 }else{ 236 return SQLITE_AFF_BLOB; 237 } 238 }else if( !aff1 && !aff2 ){ 239 /* Neither side of the comparison is a column. Compare the 240 ** results directly. 241 */ 242 return SQLITE_AFF_BLOB; 243 }else{ 244 /* One side is a column, the other is not. Use the columns affinity. */ 245 assert( aff1==0 || aff2==0 ); 246 return (aff1 + aff2); 247 } 248 } 249 250 /* 251 ** pExpr is a comparison operator. Return the type affinity that should 252 ** be applied to both operands prior to doing the comparison. 253 */ 254 static char comparisonAffinity(Expr *pExpr){ 255 char aff; 256 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 257 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 258 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT ); 259 assert( pExpr->pLeft ); 260 aff = sqlite3ExprAffinity(pExpr->pLeft); 261 if( pExpr->pRight ){ 262 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 263 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 264 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); 265 }else if( aff==0 ){ 266 aff = SQLITE_AFF_BLOB; 267 } 268 return aff; 269 } 270 271 /* 272 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 273 ** idx_affinity is the affinity of an indexed column. Return true 274 ** if the index with affinity idx_affinity may be used to implement 275 ** the comparison in pExpr. 276 */ 277 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 278 char aff = comparisonAffinity(pExpr); 279 switch( aff ){ 280 case SQLITE_AFF_BLOB: 281 return 1; 282 case SQLITE_AFF_TEXT: 283 return idx_affinity==SQLITE_AFF_TEXT; 284 default: 285 return sqlite3IsNumericAffinity(idx_affinity); 286 } 287 } 288 289 /* 290 ** Return the P5 value that should be used for a binary comparison 291 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 292 */ 293 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 294 u8 aff = (char)sqlite3ExprAffinity(pExpr2); 295 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull; 296 return aff; 297 } 298 299 /* 300 ** Return a pointer to the collation sequence that should be used by 301 ** a binary comparison operator comparing pLeft and pRight. 302 ** 303 ** If the left hand expression has a collating sequence type, then it is 304 ** used. Otherwise the collation sequence for the right hand expression 305 ** is used, or the default (BINARY) if neither expression has a collating 306 ** type. 307 ** 308 ** Argument pRight (but not pLeft) may be a null pointer. In this case, 309 ** it is not considered. 310 */ 311 CollSeq *sqlite3BinaryCompareCollSeq( 312 Parse *pParse, 313 Expr *pLeft, 314 Expr *pRight 315 ){ 316 CollSeq *pColl; 317 assert( pLeft ); 318 if( pLeft->flags & EP_Collate ){ 319 pColl = sqlite3ExprCollSeq(pParse, pLeft); 320 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){ 321 pColl = sqlite3ExprCollSeq(pParse, pRight); 322 }else{ 323 pColl = sqlite3ExprCollSeq(pParse, pLeft); 324 if( !pColl ){ 325 pColl = sqlite3ExprCollSeq(pParse, pRight); 326 } 327 } 328 return pColl; 329 } 330 331 /* 332 ** Generate code for a comparison operator. 333 */ 334 static int codeCompare( 335 Parse *pParse, /* The parsing (and code generating) context */ 336 Expr *pLeft, /* The left operand */ 337 Expr *pRight, /* The right operand */ 338 int opcode, /* The comparison opcode */ 339 int in1, int in2, /* Register holding operands */ 340 int dest, /* Jump here if true. */ 341 int jumpIfNull /* If true, jump if either operand is NULL */ 342 ){ 343 int p5; 344 int addr; 345 CollSeq *p4; 346 347 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); 348 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); 349 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, 350 (void*)p4, P4_COLLSEQ); 351 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); 352 return addr; 353 } 354 355 /* 356 ** Return true if expression pExpr is a vector, or false otherwise. 357 ** 358 ** A vector is defined as any expression that results in two or more 359 ** columns of result. Every TK_VECTOR node is an vector because the 360 ** parser will not generate a TK_VECTOR with fewer than two entries. 361 ** But a TK_SELECT might be either a vector or a scalar. It is only 362 ** considered a vector if it has two or more result columns. 363 */ 364 int sqlite3ExprIsVector(Expr *pExpr){ 365 return sqlite3ExprVectorSize(pExpr)>1; 366 } 367 368 /* 369 ** If the expression passed as the only argument is of type TK_VECTOR 370 ** return the number of expressions in the vector. Or, if the expression 371 ** is a sub-select, return the number of columns in the sub-select. For 372 ** any other type of expression, return 1. 373 */ 374 int sqlite3ExprVectorSize(Expr *pExpr){ 375 u8 op = pExpr->op; 376 if( op==TK_REGISTER ) op = pExpr->op2; 377 if( op==TK_VECTOR ){ 378 return pExpr->x.pList->nExpr; 379 }else if( op==TK_SELECT ){ 380 return pExpr->x.pSelect->pEList->nExpr; 381 }else{ 382 return 1; 383 } 384 } 385 386 /* 387 ** Return a pointer to a subexpression of pVector that is the i-th 388 ** column of the vector (numbered starting with 0). The caller must 389 ** ensure that i is within range. 390 ** 391 ** If pVector is really a scalar (and "scalar" here includes subqueries 392 ** that return a single column!) then return pVector unmodified. 393 ** 394 ** pVector retains ownership of the returned subexpression. 395 ** 396 ** If the vector is a (SELECT ...) then the expression returned is 397 ** just the expression for the i-th term of the result set, and may 398 ** not be ready for evaluation because the table cursor has not yet 399 ** been positioned. 400 */ 401 Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){ 402 assert( i<sqlite3ExprVectorSize(pVector) ); 403 if( sqlite3ExprIsVector(pVector) ){ 404 assert( pVector->op2==0 || pVector->op==TK_REGISTER ); 405 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){ 406 return pVector->x.pSelect->pEList->a[i].pExpr; 407 }else{ 408 return pVector->x.pList->a[i].pExpr; 409 } 410 } 411 return pVector; 412 } 413 414 /* 415 ** Compute and return a new Expr object which when passed to 416 ** sqlite3ExprCode() will generate all necessary code to compute 417 ** the iField-th column of the vector expression pVector. 418 ** 419 ** It is ok for pVector to be a scalar (as long as iField==0). 420 ** In that case, this routine works like sqlite3ExprDup(). 421 ** 422 ** The caller owns the returned Expr object and is responsible for 423 ** ensuring that the returned value eventually gets freed. 424 ** 425 ** The caller retains ownership of pVector. If pVector is a TK_SELECT, 426 ** then the returned object will reference pVector and so pVector must remain 427 ** valid for the life of the returned object. If pVector is a TK_VECTOR 428 ** or a scalar expression, then it can be deleted as soon as this routine 429 ** returns. 430 ** 431 ** A trick to cause a TK_SELECT pVector to be deleted together with 432 ** the returned Expr object is to attach the pVector to the pRight field 433 ** of the returned TK_SELECT_COLUMN Expr object. 434 */ 435 Expr *sqlite3ExprForVectorField( 436 Parse *pParse, /* Parsing context */ 437 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */ 438 int iField /* Which column of the vector to return */ 439 ){ 440 Expr *pRet; 441 if( pVector->op==TK_SELECT ){ 442 assert( pVector->flags & EP_xIsSelect ); 443 /* The TK_SELECT_COLUMN Expr node: 444 ** 445 ** pLeft: pVector containing TK_SELECT. Not deleted. 446 ** pRight: not used. But recursively deleted. 447 ** iColumn: Index of a column in pVector 448 ** iTable: 0 or the number of columns on the LHS of an assignment 449 ** pLeft->iTable: First in an array of register holding result, or 0 450 ** if the result is not yet computed. 451 ** 452 ** sqlite3ExprDelete() specifically skips the recursive delete of 453 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector 454 ** can be attached to pRight to cause this node to take ownership of 455 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes 456 ** with the same pLeft pointer to the pVector, but only one of them 457 ** will own the pVector. 458 */ 459 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0); 460 if( pRet ){ 461 pRet->iColumn = iField; 462 pRet->pLeft = pVector; 463 } 464 assert( pRet==0 || pRet->iTable==0 ); 465 }else{ 466 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr; 467 pRet = sqlite3ExprDup(pParse->db, pVector, 0); 468 } 469 return pRet; 470 } 471 472 /* 473 ** If expression pExpr is of type TK_SELECT, generate code to evaluate 474 ** it. Return the register in which the result is stored (or, if the 475 ** sub-select returns more than one column, the first in an array 476 ** of registers in which the result is stored). 477 ** 478 ** If pExpr is not a TK_SELECT expression, return 0. 479 */ 480 static int exprCodeSubselect(Parse *pParse, Expr *pExpr){ 481 int reg = 0; 482 #ifndef SQLITE_OMIT_SUBQUERY 483 if( pExpr->op==TK_SELECT ){ 484 reg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 485 } 486 #endif 487 return reg; 488 } 489 490 /* 491 ** Argument pVector points to a vector expression - either a TK_VECTOR 492 ** or TK_SELECT that returns more than one column. This function returns 493 ** the register number of a register that contains the value of 494 ** element iField of the vector. 495 ** 496 ** If pVector is a TK_SELECT expression, then code for it must have 497 ** already been generated using the exprCodeSubselect() routine. In this 498 ** case parameter regSelect should be the first in an array of registers 499 ** containing the results of the sub-select. 500 ** 501 ** If pVector is of type TK_VECTOR, then code for the requested field 502 ** is generated. In this case (*pRegFree) may be set to the number of 503 ** a temporary register to be freed by the caller before returning. 504 ** 505 ** Before returning, output parameter (*ppExpr) is set to point to the 506 ** Expr object corresponding to element iElem of the vector. 507 */ 508 static int exprVectorRegister( 509 Parse *pParse, /* Parse context */ 510 Expr *pVector, /* Vector to extract element from */ 511 int iField, /* Field to extract from pVector */ 512 int regSelect, /* First in array of registers */ 513 Expr **ppExpr, /* OUT: Expression element */ 514 int *pRegFree /* OUT: Temp register to free */ 515 ){ 516 u8 op = pVector->op; 517 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT ); 518 if( op==TK_REGISTER ){ 519 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField); 520 return pVector->iTable+iField; 521 } 522 if( op==TK_SELECT ){ 523 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr; 524 return regSelect+iField; 525 } 526 *ppExpr = pVector->x.pList->a[iField].pExpr; 527 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree); 528 } 529 530 /* 531 ** Expression pExpr is a comparison between two vector values. Compute 532 ** the result of the comparison (1, 0, or NULL) and write that 533 ** result into register dest. 534 ** 535 ** The caller must satisfy the following preconditions: 536 ** 537 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ 538 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ 539 ** otherwise: op==pExpr->op and p5==0 540 */ 541 static void codeVectorCompare( 542 Parse *pParse, /* Code generator context */ 543 Expr *pExpr, /* The comparison operation */ 544 int dest, /* Write results into this register */ 545 u8 op, /* Comparison operator */ 546 u8 p5 /* SQLITE_NULLEQ or zero */ 547 ){ 548 Vdbe *v = pParse->pVdbe; 549 Expr *pLeft = pExpr->pLeft; 550 Expr *pRight = pExpr->pRight; 551 int nLeft = sqlite3ExprVectorSize(pLeft); 552 int i; 553 int regLeft = 0; 554 int regRight = 0; 555 u8 opx = op; 556 int addrDone = sqlite3VdbeMakeLabel(v); 557 558 if( nLeft!=sqlite3ExprVectorSize(pRight) ){ 559 sqlite3ErrorMsg(pParse, "row value misused"); 560 return; 561 } 562 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE 563 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT 564 || pExpr->op==TK_LT || pExpr->op==TK_GT 565 || pExpr->op==TK_LE || pExpr->op==TK_GE 566 ); 567 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ) 568 || (pExpr->op==TK_ISNOT && op==TK_NE) ); 569 assert( p5==0 || pExpr->op!=op ); 570 assert( p5==SQLITE_NULLEQ || pExpr->op==op ); 571 572 p5 |= SQLITE_STOREP2; 573 if( opx==TK_LE ) opx = TK_LT; 574 if( opx==TK_GE ) opx = TK_GT; 575 576 regLeft = exprCodeSubselect(pParse, pLeft); 577 regRight = exprCodeSubselect(pParse, pRight); 578 579 for(i=0; 1 /*Loop exits by "break"*/; i++){ 580 int regFree1 = 0, regFree2 = 0; 581 Expr *pL, *pR; 582 int r1, r2; 583 assert( i>=0 && i<nLeft ); 584 if( i>0 ) sqlite3ExprCachePush(pParse); 585 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, ®Free1); 586 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, ®Free2); 587 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5); 588 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 589 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 590 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 591 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 592 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 593 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 594 sqlite3ReleaseTempReg(pParse, regFree1); 595 sqlite3ReleaseTempReg(pParse, regFree2); 596 if( i>0 ) sqlite3ExprCachePop(pParse); 597 if( i==nLeft-1 ){ 598 break; 599 } 600 if( opx==TK_EQ ){ 601 sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v); 602 p5 |= SQLITE_KEEPNULL; 603 }else if( opx==TK_NE ){ 604 sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v); 605 p5 |= SQLITE_KEEPNULL; 606 }else{ 607 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE ); 608 sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone); 609 VdbeCoverageIf(v, op==TK_LT); 610 VdbeCoverageIf(v, op==TK_GT); 611 VdbeCoverageIf(v, op==TK_LE); 612 VdbeCoverageIf(v, op==TK_GE); 613 if( i==nLeft-2 ) opx = op; 614 } 615 } 616 sqlite3VdbeResolveLabel(v, addrDone); 617 } 618 619 #if SQLITE_MAX_EXPR_DEPTH>0 620 /* 621 ** Check that argument nHeight is less than or equal to the maximum 622 ** expression depth allowed. If it is not, leave an error message in 623 ** pParse. 624 */ 625 int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ 626 int rc = SQLITE_OK; 627 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 628 if( nHeight>mxHeight ){ 629 sqlite3ErrorMsg(pParse, 630 "Expression tree is too large (maximum depth %d)", mxHeight 631 ); 632 rc = SQLITE_ERROR; 633 } 634 return rc; 635 } 636 637 /* The following three functions, heightOfExpr(), heightOfExprList() 638 ** and heightOfSelect(), are used to determine the maximum height 639 ** of any expression tree referenced by the structure passed as the 640 ** first argument. 641 ** 642 ** If this maximum height is greater than the current value pointed 643 ** to by pnHeight, the second parameter, then set *pnHeight to that 644 ** value. 645 */ 646 static void heightOfExpr(Expr *p, int *pnHeight){ 647 if( p ){ 648 if( p->nHeight>*pnHeight ){ 649 *pnHeight = p->nHeight; 650 } 651 } 652 } 653 static void heightOfExprList(ExprList *p, int *pnHeight){ 654 if( p ){ 655 int i; 656 for(i=0; i<p->nExpr; i++){ 657 heightOfExpr(p->a[i].pExpr, pnHeight); 658 } 659 } 660 } 661 static void heightOfSelect(Select *pSelect, int *pnHeight){ 662 Select *p; 663 for(p=pSelect; p; p=p->pPrior){ 664 heightOfExpr(p->pWhere, pnHeight); 665 heightOfExpr(p->pHaving, pnHeight); 666 heightOfExpr(p->pLimit, pnHeight); 667 heightOfExprList(p->pEList, pnHeight); 668 heightOfExprList(p->pGroupBy, pnHeight); 669 heightOfExprList(p->pOrderBy, pnHeight); 670 } 671 } 672 673 /* 674 ** Set the Expr.nHeight variable in the structure passed as an 675 ** argument. An expression with no children, Expr.pList or 676 ** Expr.pSelect member has a height of 1. Any other expression 677 ** has a height equal to the maximum height of any other 678 ** referenced Expr plus one. 679 ** 680 ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, 681 ** if appropriate. 682 */ 683 static void exprSetHeight(Expr *p){ 684 int nHeight = 0; 685 heightOfExpr(p->pLeft, &nHeight); 686 heightOfExpr(p->pRight, &nHeight); 687 if( ExprHasProperty(p, EP_xIsSelect) ){ 688 heightOfSelect(p->x.pSelect, &nHeight); 689 }else if( p->x.pList ){ 690 heightOfExprList(p->x.pList, &nHeight); 691 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 692 } 693 p->nHeight = nHeight + 1; 694 } 695 696 /* 697 ** Set the Expr.nHeight variable using the exprSetHeight() function. If 698 ** the height is greater than the maximum allowed expression depth, 699 ** leave an error in pParse. 700 ** 701 ** Also propagate all EP_Propagate flags from the Expr.x.pList into 702 ** Expr.flags. 703 */ 704 void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 705 if( pParse->nErr ) return; 706 exprSetHeight(p); 707 sqlite3ExprCheckHeight(pParse, p->nHeight); 708 } 709 710 /* 711 ** Return the maximum height of any expression tree referenced 712 ** by the select statement passed as an argument. 713 */ 714 int sqlite3SelectExprHeight(Select *p){ 715 int nHeight = 0; 716 heightOfSelect(p, &nHeight); 717 return nHeight; 718 } 719 #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ 720 /* 721 ** Propagate all EP_Propagate flags from the Expr.x.pList into 722 ** Expr.flags. 723 */ 724 void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 725 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ 726 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 727 } 728 } 729 #define exprSetHeight(y) 730 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 731 732 /* 733 ** This routine is the core allocator for Expr nodes. 734 ** 735 ** Construct a new expression node and return a pointer to it. Memory 736 ** for this node and for the pToken argument is a single allocation 737 ** obtained from sqlite3DbMalloc(). The calling function 738 ** is responsible for making sure the node eventually gets freed. 739 ** 740 ** If dequote is true, then the token (if it exists) is dequoted. 741 ** If dequote is false, no dequoting is performed. The deQuote 742 ** parameter is ignored if pToken is NULL or if the token does not 743 ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 744 ** then the EP_DblQuoted flag is set on the expression node. 745 ** 746 ** Special case: If op==TK_INTEGER and pToken points to a string that 747 ** can be translated into a 32-bit integer, then the token is not 748 ** stored in u.zToken. Instead, the integer values is written 749 ** into u.iValue and the EP_IntValue flag is set. No extra storage 750 ** is allocated to hold the integer text and the dequote flag is ignored. 751 */ 752 Expr *sqlite3ExprAlloc( 753 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */ 754 int op, /* Expression opcode */ 755 const Token *pToken, /* Token argument. Might be NULL */ 756 int dequote /* True to dequote */ 757 ){ 758 Expr *pNew; 759 int nExtra = 0; 760 int iValue = 0; 761 762 assert( db!=0 ); 763 if( pToken ){ 764 if( op!=TK_INTEGER || pToken->z==0 765 || sqlite3GetInt32(pToken->z, &iValue)==0 ){ 766 nExtra = pToken->n+1; 767 assert( iValue>=0 ); 768 } 769 } 770 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); 771 if( pNew ){ 772 memset(pNew, 0, sizeof(Expr)); 773 pNew->op = (u8)op; 774 pNew->iAgg = -1; 775 if( pToken ){ 776 if( nExtra==0 ){ 777 pNew->flags |= EP_IntValue|EP_Leaf; 778 pNew->u.iValue = iValue; 779 }else{ 780 pNew->u.zToken = (char*)&pNew[1]; 781 assert( pToken->z!=0 || pToken->n==0 ); 782 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); 783 pNew->u.zToken[pToken->n] = 0; 784 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ 785 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted; 786 sqlite3Dequote(pNew->u.zToken); 787 } 788 } 789 } 790 #if SQLITE_MAX_EXPR_DEPTH>0 791 pNew->nHeight = 1; 792 #endif 793 } 794 return pNew; 795 } 796 797 /* 798 ** Allocate a new expression node from a zero-terminated token that has 799 ** already been dequoted. 800 */ 801 Expr *sqlite3Expr( 802 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 803 int op, /* Expression opcode */ 804 const char *zToken /* Token argument. Might be NULL */ 805 ){ 806 Token x; 807 x.z = zToken; 808 x.n = sqlite3Strlen30(zToken); 809 return sqlite3ExprAlloc(db, op, &x, 0); 810 } 811 812 /* 813 ** Attach subtrees pLeft and pRight to the Expr node pRoot. 814 ** 815 ** If pRoot==NULL that means that a memory allocation error has occurred. 816 ** In that case, delete the subtrees pLeft and pRight. 817 */ 818 void sqlite3ExprAttachSubtrees( 819 sqlite3 *db, 820 Expr *pRoot, 821 Expr *pLeft, 822 Expr *pRight 823 ){ 824 if( pRoot==0 ){ 825 assert( db->mallocFailed ); 826 sqlite3ExprDelete(db, pLeft); 827 sqlite3ExprDelete(db, pRight); 828 }else{ 829 if( pRight ){ 830 pRoot->pRight = pRight; 831 pRoot->flags |= EP_Propagate & pRight->flags; 832 } 833 if( pLeft ){ 834 pRoot->pLeft = pLeft; 835 pRoot->flags |= EP_Propagate & pLeft->flags; 836 } 837 exprSetHeight(pRoot); 838 } 839 } 840 841 /* 842 ** Allocate an Expr node which joins as many as two subtrees. 843 ** 844 ** One or both of the subtrees can be NULL. Return a pointer to the new 845 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 846 ** free the subtrees and return NULL. 847 */ 848 Expr *sqlite3PExpr( 849 Parse *pParse, /* Parsing context */ 850 int op, /* Expression opcode */ 851 Expr *pLeft, /* Left operand */ 852 Expr *pRight /* Right operand */ 853 ){ 854 Expr *p; 855 if( op==TK_AND && pParse->nErr==0 ){ 856 /* Take advantage of short-circuit false optimization for AND */ 857 p = sqlite3ExprAnd(pParse->db, pLeft, pRight); 858 }else{ 859 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)); 860 if( p ){ 861 memset(p, 0, sizeof(Expr)); 862 p->op = op & TKFLG_MASK; 863 p->iAgg = -1; 864 } 865 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 866 } 867 if( p ) { 868 sqlite3ExprCheckHeight(pParse, p->nHeight); 869 } 870 return p; 871 } 872 873 /* 874 ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due 875 ** do a memory allocation failure) then delete the pSelect object. 876 */ 877 void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ 878 if( pExpr ){ 879 pExpr->x.pSelect = pSelect; 880 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); 881 sqlite3ExprSetHeightAndFlags(pParse, pExpr); 882 }else{ 883 assert( pParse->db->mallocFailed ); 884 sqlite3SelectDelete(pParse->db, pSelect); 885 } 886 } 887 888 889 /* 890 ** If the expression is always either TRUE or FALSE (respectively), 891 ** then return 1. If one cannot determine the truth value of the 892 ** expression at compile-time return 0. 893 ** 894 ** This is an optimization. If is OK to return 0 here even if 895 ** the expression really is always false or false (a false negative). 896 ** But it is a bug to return 1 if the expression might have different 897 ** boolean values in different circumstances (a false positive.) 898 ** 899 ** Note that if the expression is part of conditional for a 900 ** LEFT JOIN, then we cannot determine at compile-time whether or not 901 ** is it true or false, so always return 0. 902 */ 903 static int exprAlwaysTrue(Expr *p){ 904 int v = 0; 905 if( ExprHasProperty(p, EP_FromJoin) ) return 0; 906 if( !sqlite3ExprIsInteger(p, &v) ) return 0; 907 return v!=0; 908 } 909 static int exprAlwaysFalse(Expr *p){ 910 int v = 0; 911 if( ExprHasProperty(p, EP_FromJoin) ) return 0; 912 if( !sqlite3ExprIsInteger(p, &v) ) return 0; 913 return v==0; 914 } 915 916 /* 917 ** Join two expressions using an AND operator. If either expression is 918 ** NULL, then just return the other expression. 919 ** 920 ** If one side or the other of the AND is known to be false, then instead 921 ** of returning an AND expression, just return a constant expression with 922 ** a value of false. 923 */ 924 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 925 if( pLeft==0 ){ 926 return pRight; 927 }else if( pRight==0 ){ 928 return pLeft; 929 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ 930 sqlite3ExprDelete(db, pLeft); 931 sqlite3ExprDelete(db, pRight); 932 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); 933 }else{ 934 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); 935 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); 936 return pNew; 937 } 938 } 939 940 /* 941 ** Construct a new expression node for a function with multiple 942 ** arguments. 943 */ 944 Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 945 Expr *pNew; 946 sqlite3 *db = pParse->db; 947 assert( pToken ); 948 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 949 if( pNew==0 ){ 950 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 951 return 0; 952 } 953 pNew->x.pList = pList; 954 ExprSetProperty(pNew, EP_HasFunc); 955 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 956 sqlite3ExprSetHeightAndFlags(pParse, pNew); 957 return pNew; 958 } 959 960 /* 961 ** Assign a variable number to an expression that encodes a wildcard 962 ** in the original SQL statement. 963 ** 964 ** Wildcards consisting of a single "?" are assigned the next sequential 965 ** variable number. 966 ** 967 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 968 ** sure "nnn" is not too big to avoid a denial of service attack when 969 ** the SQL statement comes from an external source. 970 ** 971 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number 972 ** as the previous instance of the same wildcard. Or if this is the first 973 ** instance of the wildcard, the next sequential variable number is 974 ** assigned. 975 */ 976 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){ 977 sqlite3 *db = pParse->db; 978 const char *z; 979 ynVar x; 980 981 if( pExpr==0 ) return; 982 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); 983 z = pExpr->u.zToken; 984 assert( z!=0 ); 985 assert( z[0]!=0 ); 986 assert( n==(u32)sqlite3Strlen30(z) ); 987 if( z[1]==0 ){ 988 /* Wildcard of the form "?". Assign the next variable number */ 989 assert( z[0]=='?' ); 990 x = (ynVar)(++pParse->nVar); 991 }else{ 992 int doAdd = 0; 993 if( z[0]=='?' ){ 994 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 995 ** use it as the variable number */ 996 i64 i; 997 int bOk; 998 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/ 999 i = z[1]-'0'; /* The common case of ?N for a single digit N */ 1000 bOk = 1; 1001 }else{ 1002 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); 1003 } 1004 testcase( i==0 ); 1005 testcase( i==1 ); 1006 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 1007 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 1008 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 1009 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 1010 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 1011 return; 1012 } 1013 x = (ynVar)i; 1014 if( x>pParse->nVar ){ 1015 pParse->nVar = (int)x; 1016 doAdd = 1; 1017 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){ 1018 doAdd = 1; 1019 } 1020 }else{ 1021 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable 1022 ** number as the prior appearance of the same name, or if the name 1023 ** has never appeared before, reuse the same variable number 1024 */ 1025 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n); 1026 if( x==0 ){ 1027 x = (ynVar)(++pParse->nVar); 1028 doAdd = 1; 1029 } 1030 } 1031 if( doAdd ){ 1032 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x); 1033 } 1034 } 1035 pExpr->iColumn = x; 1036 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 1037 sqlite3ErrorMsg(pParse, "too many SQL variables"); 1038 } 1039 } 1040 1041 /* 1042 ** Recursively delete an expression tree. 1043 */ 1044 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ 1045 assert( p!=0 ); 1046 /* Sanity check: Assert that the IntValue is non-negative if it exists */ 1047 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); 1048 #ifdef SQLITE_DEBUG 1049 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ 1050 assert( p->pLeft==0 ); 1051 assert( p->pRight==0 ); 1052 assert( p->x.pSelect==0 ); 1053 } 1054 #endif 1055 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){ 1056 /* The Expr.x union is never used at the same time as Expr.pRight */ 1057 assert( p->x.pList==0 || p->pRight==0 ); 1058 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft); 1059 if( p->pRight ){ 1060 sqlite3ExprDeleteNN(db, p->pRight); 1061 }else if( ExprHasProperty(p, EP_xIsSelect) ){ 1062 sqlite3SelectDelete(db, p->x.pSelect); 1063 }else{ 1064 sqlite3ExprListDelete(db, p->x.pList); 1065 } 1066 if( !ExprHasProperty(p, EP_Reduced) ){ 1067 sqlite3WindowDelete(db, p->pWin); 1068 } 1069 } 1070 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); 1071 if( !ExprHasProperty(p, EP_Static) ){ 1072 sqlite3DbFreeNN(db, p); 1073 } 1074 } 1075 void sqlite3ExprDelete(sqlite3 *db, Expr *p){ 1076 if( p ) sqlite3ExprDeleteNN(db, p); 1077 } 1078 1079 /* 1080 ** Return the number of bytes allocated for the expression structure 1081 ** passed as the first argument. This is always one of EXPR_FULLSIZE, 1082 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. 1083 */ 1084 static int exprStructSize(Expr *p){ 1085 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; 1086 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; 1087 return EXPR_FULLSIZE; 1088 } 1089 1090 /* 1091 ** The dupedExpr*Size() routines each return the number of bytes required 1092 ** to store a copy of an expression or expression tree. They differ in 1093 ** how much of the tree is measured. 1094 ** 1095 ** dupedExprStructSize() Size of only the Expr structure 1096 ** dupedExprNodeSize() Size of Expr + space for token 1097 ** dupedExprSize() Expr + token + subtree components 1098 ** 1099 *************************************************************************** 1100 ** 1101 ** The dupedExprStructSize() function returns two values OR-ed together: 1102 ** (1) the space required for a copy of the Expr structure only and 1103 ** (2) the EP_xxx flags that indicate what the structure size should be. 1104 ** The return values is always one of: 1105 ** 1106 ** EXPR_FULLSIZE 1107 ** EXPR_REDUCEDSIZE | EP_Reduced 1108 ** EXPR_TOKENONLYSIZE | EP_TokenOnly 1109 ** 1110 ** The size of the structure can be found by masking the return value 1111 ** of this routine with 0xfff. The flags can be found by masking the 1112 ** return value with EP_Reduced|EP_TokenOnly. 1113 ** 1114 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size 1115 ** (unreduced) Expr objects as they or originally constructed by the parser. 1116 ** During expression analysis, extra information is computed and moved into 1117 ** later parts of the Expr object and that extra information might get chopped 1118 ** off if the expression is reduced. Note also that it does not work to 1119 ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal 1120 ** to reduce a pristine expression tree from the parser. The implementation 1121 ** of dupedExprStructSize() contain multiple assert() statements that attempt 1122 ** to enforce this constraint. 1123 */ 1124 static int dupedExprStructSize(Expr *p, int flags){ 1125 int nSize; 1126 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ 1127 assert( EXPR_FULLSIZE<=0xfff ); 1128 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); 1129 if( 0==flags || p->op==TK_SELECT_COLUMN 1130 #ifndef SQLITE_OMIT_WINDOWFUNC 1131 || p->pWin 1132 #endif 1133 ){ 1134 nSize = EXPR_FULLSIZE; 1135 }else{ 1136 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); 1137 assert( !ExprHasProperty(p, EP_FromJoin) ); 1138 assert( !ExprHasProperty(p, EP_MemToken) ); 1139 assert( !ExprHasProperty(p, EP_NoReduce) ); 1140 if( p->pLeft || p->x.pList ){ 1141 nSize = EXPR_REDUCEDSIZE | EP_Reduced; 1142 }else{ 1143 assert( p->pRight==0 ); 1144 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; 1145 } 1146 } 1147 return nSize; 1148 } 1149 1150 /* 1151 ** This function returns the space in bytes required to store the copy 1152 ** of the Expr structure and a copy of the Expr.u.zToken string (if that 1153 ** string is defined.) 1154 */ 1155 static int dupedExprNodeSize(Expr *p, int flags){ 1156 int nByte = dupedExprStructSize(p, flags) & 0xfff; 1157 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 1158 nByte += sqlite3Strlen30(p->u.zToken)+1; 1159 } 1160 return ROUND8(nByte); 1161 } 1162 1163 /* 1164 ** Return the number of bytes required to create a duplicate of the 1165 ** expression passed as the first argument. The second argument is a 1166 ** mask containing EXPRDUP_XXX flags. 1167 ** 1168 ** The value returned includes space to create a copy of the Expr struct 1169 ** itself and the buffer referred to by Expr.u.zToken, if any. 1170 ** 1171 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 1172 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 1173 ** and Expr.pRight variables (but not for any structures pointed to or 1174 ** descended from the Expr.x.pList or Expr.x.pSelect variables). 1175 */ 1176 static int dupedExprSize(Expr *p, int flags){ 1177 int nByte = 0; 1178 if( p ){ 1179 nByte = dupedExprNodeSize(p, flags); 1180 if( flags&EXPRDUP_REDUCE ){ 1181 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); 1182 } 1183 } 1184 return nByte; 1185 } 1186 1187 /* 1188 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 1189 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 1190 ** to store the copy of expression p, the copies of p->u.zToken 1191 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, 1192 ** if any. Before returning, *pzBuffer is set to the first byte past the 1193 ** portion of the buffer copied into by this function. 1194 */ 1195 static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ 1196 Expr *pNew; /* Value to return */ 1197 u8 *zAlloc; /* Memory space from which to build Expr object */ 1198 u32 staticFlag; /* EP_Static if space not obtained from malloc */ 1199 1200 assert( db!=0 ); 1201 assert( p ); 1202 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); 1203 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE ); 1204 1205 /* Figure out where to write the new Expr structure. */ 1206 if( pzBuffer ){ 1207 zAlloc = *pzBuffer; 1208 staticFlag = EP_Static; 1209 }else{ 1210 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)); 1211 staticFlag = 0; 1212 } 1213 pNew = (Expr *)zAlloc; 1214 1215 if( pNew ){ 1216 /* Set nNewSize to the size allocated for the structure pointed to 1217 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or 1218 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed 1219 ** by the copy of the p->u.zToken string (if any). 1220 */ 1221 const unsigned nStructSize = dupedExprStructSize(p, dupFlags); 1222 const int nNewSize = nStructSize & 0xfff; 1223 int nToken; 1224 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 1225 nToken = sqlite3Strlen30(p->u.zToken) + 1; 1226 }else{ 1227 nToken = 0; 1228 } 1229 if( dupFlags ){ 1230 assert( ExprHasProperty(p, EP_Reduced)==0 ); 1231 memcpy(zAlloc, p, nNewSize); 1232 }else{ 1233 u32 nSize = (u32)exprStructSize(p); 1234 memcpy(zAlloc, p, nSize); 1235 if( nSize<EXPR_FULLSIZE ){ 1236 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 1237 } 1238 } 1239 1240 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 1241 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); 1242 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 1243 pNew->flags |= staticFlag; 1244 1245 /* Copy the p->u.zToken string, if any. */ 1246 if( nToken ){ 1247 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; 1248 memcpy(zToken, p->u.zToken, nToken); 1249 } 1250 1251 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){ 1252 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ 1253 if( ExprHasProperty(p, EP_xIsSelect) ){ 1254 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); 1255 }else{ 1256 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); 1257 } 1258 } 1259 1260 /* Fill in pNew->pLeft and pNew->pRight. */ 1261 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ 1262 zAlloc += dupedExprNodeSize(p, dupFlags); 1263 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){ 1264 pNew->pLeft = p->pLeft ? 1265 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; 1266 pNew->pRight = p->pRight ? 1267 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; 1268 } 1269 if( pzBuffer ){ 1270 *pzBuffer = zAlloc; 1271 } 1272 }else{ 1273 #ifndef SQLITE_OMIT_WINDOWFUNC 1274 if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){ 1275 pNew->pWin = 0; 1276 }else{ 1277 pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin); 1278 } 1279 #endif /* SQLITE_OMIT_WINDOWFUNC */ 1280 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){ 1281 if( pNew->op==TK_SELECT_COLUMN ){ 1282 pNew->pLeft = p->pLeft; 1283 assert( p->iColumn==0 || p->pRight==0 ); 1284 assert( p->pRight==0 || p->pRight==p->pLeft ); 1285 }else{ 1286 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); 1287 } 1288 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); 1289 } 1290 } 1291 } 1292 return pNew; 1293 } 1294 1295 /* 1296 ** Create and return a deep copy of the object passed as the second 1297 ** argument. If an OOM condition is encountered, NULL is returned 1298 ** and the db->mallocFailed flag set. 1299 */ 1300 #ifndef SQLITE_OMIT_CTE 1301 static With *withDup(sqlite3 *db, With *p){ 1302 With *pRet = 0; 1303 if( p ){ 1304 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); 1305 pRet = sqlite3DbMallocZero(db, nByte); 1306 if( pRet ){ 1307 int i; 1308 pRet->nCte = p->nCte; 1309 for(i=0; i<p->nCte; i++){ 1310 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); 1311 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); 1312 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); 1313 } 1314 } 1315 } 1316 return pRet; 1317 } 1318 #else 1319 # define withDup(x,y) 0 1320 #endif 1321 1322 /* 1323 ** The following group of routines make deep copies of expressions, 1324 ** expression lists, ID lists, and select statements. The copies can 1325 ** be deleted (by being passed to their respective ...Delete() routines) 1326 ** without effecting the originals. 1327 ** 1328 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 1329 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 1330 ** by subsequent calls to sqlite*ListAppend() routines. 1331 ** 1332 ** Any tables that the SrcList might point to are not duplicated. 1333 ** 1334 ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 1335 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 1336 ** truncated version of the usual Expr structure that will be stored as 1337 ** part of the in-memory representation of the database schema. 1338 */ 1339 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ 1340 assert( flags==0 || flags==EXPRDUP_REDUCE ); 1341 return p ? exprDup(db, p, flags, 0) : 0; 1342 } 1343 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ 1344 ExprList *pNew; 1345 struct ExprList_item *pItem, *pOldItem; 1346 int i; 1347 Expr *pPriorSelectCol = 0; 1348 assert( db!=0 ); 1349 if( p==0 ) return 0; 1350 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p)); 1351 if( pNew==0 ) return 0; 1352 pNew->nExpr = p->nExpr; 1353 pItem = pNew->a; 1354 pOldItem = p->a; 1355 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 1356 Expr *pOldExpr = pOldItem->pExpr; 1357 Expr *pNewExpr; 1358 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); 1359 if( pOldExpr 1360 && pOldExpr->op==TK_SELECT_COLUMN 1361 && (pNewExpr = pItem->pExpr)!=0 1362 ){ 1363 assert( pNewExpr->iColumn==0 || i>0 ); 1364 if( pNewExpr->iColumn==0 ){ 1365 assert( pOldExpr->pLeft==pOldExpr->pRight ); 1366 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight; 1367 }else{ 1368 assert( i>0 ); 1369 assert( pItem[-1].pExpr!=0 ); 1370 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 ); 1371 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft ); 1372 pNewExpr->pLeft = pPriorSelectCol; 1373 } 1374 } 1375 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1376 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); 1377 pItem->sortOrder = pOldItem->sortOrder; 1378 pItem->done = 0; 1379 pItem->bSpanIsTab = pOldItem->bSpanIsTab; 1380 pItem->bSorterRef = pOldItem->bSorterRef; 1381 pItem->u = pOldItem->u; 1382 } 1383 return pNew; 1384 } 1385 1386 /* 1387 ** If cursors, triggers, views and subqueries are all omitted from 1388 ** the build, then none of the following routines, except for 1389 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 1390 ** called with a NULL argument. 1391 */ 1392 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 1393 || !defined(SQLITE_OMIT_SUBQUERY) 1394 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ 1395 SrcList *pNew; 1396 int i; 1397 int nByte; 1398 assert( db!=0 ); 1399 if( p==0 ) return 0; 1400 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 1401 pNew = sqlite3DbMallocRawNN(db, nByte ); 1402 if( pNew==0 ) return 0; 1403 pNew->nSrc = pNew->nAlloc = p->nSrc; 1404 for(i=0; i<p->nSrc; i++){ 1405 struct SrcList_item *pNewItem = &pNew->a[i]; 1406 struct SrcList_item *pOldItem = &p->a[i]; 1407 Table *pTab; 1408 pNewItem->pSchema = pOldItem->pSchema; 1409 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 1410 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1411 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 1412 pNewItem->fg = pOldItem->fg; 1413 pNewItem->iCursor = pOldItem->iCursor; 1414 pNewItem->addrFillSub = pOldItem->addrFillSub; 1415 pNewItem->regReturn = pOldItem->regReturn; 1416 if( pNewItem->fg.isIndexedBy ){ 1417 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); 1418 } 1419 pNewItem->pIBIndex = pOldItem->pIBIndex; 1420 if( pNewItem->fg.isTabFunc ){ 1421 pNewItem->u1.pFuncArg = 1422 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); 1423 } 1424 pTab = pNewItem->pTab = pOldItem->pTab; 1425 if( pTab ){ 1426 pTab->nTabRef++; 1427 } 1428 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); 1429 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); 1430 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 1431 pNewItem->colUsed = pOldItem->colUsed; 1432 } 1433 return pNew; 1434 } 1435 IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 1436 IdList *pNew; 1437 int i; 1438 assert( db!=0 ); 1439 if( p==0 ) return 0; 1440 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1441 if( pNew==0 ) return 0; 1442 pNew->nId = p->nId; 1443 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); 1444 if( pNew->a==0 ){ 1445 sqlite3DbFreeNN(db, pNew); 1446 return 0; 1447 } 1448 /* Note that because the size of the allocation for p->a[] is not 1449 ** necessarily a power of two, sqlite3IdListAppend() may not be called 1450 ** on the duplicate created by this function. */ 1451 for(i=0; i<p->nId; i++){ 1452 struct IdList_item *pNewItem = &pNew->a[i]; 1453 struct IdList_item *pOldItem = &p->a[i]; 1454 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1455 pNewItem->idx = pOldItem->idx; 1456 } 1457 return pNew; 1458 } 1459 Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){ 1460 Select *pRet = 0; 1461 Select *pNext = 0; 1462 Select **pp = &pRet; 1463 Select *p; 1464 1465 assert( db!=0 ); 1466 for(p=pDup; p; p=p->pPrior){ 1467 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); 1468 if( pNew==0 ) break; 1469 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); 1470 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); 1471 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); 1472 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); 1473 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); 1474 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); 1475 pNew->op = p->op; 1476 pNew->pNext = pNext; 1477 pNew->pPrior = 0; 1478 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); 1479 pNew->iLimit = 0; 1480 pNew->iOffset = 0; 1481 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; 1482 pNew->addrOpenEphm[0] = -1; 1483 pNew->addrOpenEphm[1] = -1; 1484 pNew->nSelectRow = p->nSelectRow; 1485 pNew->pWith = withDup(db, p->pWith); 1486 #ifndef SQLITE_OMIT_WINDOWFUNC 1487 pNew->pWin = 0; 1488 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn); 1489 #endif 1490 pNew->selId = p->selId; 1491 *pp = pNew; 1492 pp = &pNew->pPrior; 1493 pNext = pNew; 1494 } 1495 1496 return pRet; 1497 } 1498 #else 1499 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 1500 assert( p==0 ); 1501 return 0; 1502 } 1503 #endif 1504 1505 1506 /* 1507 ** Add a new element to the end of an expression list. If pList is 1508 ** initially NULL, then create a new expression list. 1509 ** 1510 ** The pList argument must be either NULL or a pointer to an ExprList 1511 ** obtained from a prior call to sqlite3ExprListAppend(). This routine 1512 ** may not be used with an ExprList obtained from sqlite3ExprListDup(). 1513 ** Reason: This routine assumes that the number of slots in pList->a[] 1514 ** is a power of two. That is true for sqlite3ExprListAppend() returns 1515 ** but is not necessarily true from the return value of sqlite3ExprListDup(). 1516 ** 1517 ** If a memory allocation error occurs, the entire list is freed and 1518 ** NULL is returned. If non-NULL is returned, then it is guaranteed 1519 ** that the new entry was successfully appended. 1520 */ 1521 ExprList *sqlite3ExprListAppend( 1522 Parse *pParse, /* Parsing context */ 1523 ExprList *pList, /* List to which to append. Might be NULL */ 1524 Expr *pExpr /* Expression to be appended. Might be NULL */ 1525 ){ 1526 struct ExprList_item *pItem; 1527 sqlite3 *db = pParse->db; 1528 assert( db!=0 ); 1529 if( pList==0 ){ 1530 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); 1531 if( pList==0 ){ 1532 goto no_mem; 1533 } 1534 pList->nExpr = 0; 1535 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ 1536 ExprList *pNew; 1537 pNew = sqlite3DbRealloc(db, pList, 1538 sizeof(*pList)+(2*pList->nExpr - 1)*sizeof(pList->a[0])); 1539 if( pNew==0 ){ 1540 goto no_mem; 1541 } 1542 pList = pNew; 1543 } 1544 pItem = &pList->a[pList->nExpr++]; 1545 assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) ); 1546 assert( offsetof(struct ExprList_item,pExpr)==0 ); 1547 memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName)); 1548 pItem->pExpr = pExpr; 1549 return pList; 1550 1551 no_mem: 1552 /* Avoid leaking memory if malloc has failed. */ 1553 sqlite3ExprDelete(db, pExpr); 1554 sqlite3ExprListDelete(db, pList); 1555 return 0; 1556 } 1557 1558 /* 1559 ** pColumns and pExpr form a vector assignment which is part of the SET 1560 ** clause of an UPDATE statement. Like this: 1561 ** 1562 ** (a,b,c) = (expr1,expr2,expr3) 1563 ** Or: (a,b,c) = (SELECT x,y,z FROM ....) 1564 ** 1565 ** For each term of the vector assignment, append new entries to the 1566 ** expression list pList. In the case of a subquery on the RHS, append 1567 ** TK_SELECT_COLUMN expressions. 1568 */ 1569 ExprList *sqlite3ExprListAppendVector( 1570 Parse *pParse, /* Parsing context */ 1571 ExprList *pList, /* List to which to append. Might be NULL */ 1572 IdList *pColumns, /* List of names of LHS of the assignment */ 1573 Expr *pExpr /* Vector expression to be appended. Might be NULL */ 1574 ){ 1575 sqlite3 *db = pParse->db; 1576 int n; 1577 int i; 1578 int iFirst = pList ? pList->nExpr : 0; 1579 /* pColumns can only be NULL due to an OOM but an OOM will cause an 1580 ** exit prior to this routine being invoked */ 1581 if( NEVER(pColumns==0) ) goto vector_append_error; 1582 if( pExpr==0 ) goto vector_append_error; 1583 1584 /* If the RHS is a vector, then we can immediately check to see that 1585 ** the size of the RHS and LHS match. But if the RHS is a SELECT, 1586 ** wildcards ("*") in the result set of the SELECT must be expanded before 1587 ** we can do the size check, so defer the size check until code generation. 1588 */ 1589 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){ 1590 sqlite3ErrorMsg(pParse, "%d columns assigned %d values", 1591 pColumns->nId, n); 1592 goto vector_append_error; 1593 } 1594 1595 for(i=0; i<pColumns->nId; i++){ 1596 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i); 1597 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr); 1598 if( pList ){ 1599 assert( pList->nExpr==iFirst+i+1 ); 1600 pList->a[pList->nExpr-1].zName = pColumns->a[i].zName; 1601 pColumns->a[i].zName = 0; 1602 } 1603 } 1604 1605 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){ 1606 Expr *pFirst = pList->a[iFirst].pExpr; 1607 assert( pFirst!=0 ); 1608 assert( pFirst->op==TK_SELECT_COLUMN ); 1609 1610 /* Store the SELECT statement in pRight so it will be deleted when 1611 ** sqlite3ExprListDelete() is called */ 1612 pFirst->pRight = pExpr; 1613 pExpr = 0; 1614 1615 /* Remember the size of the LHS in iTable so that we can check that 1616 ** the RHS and LHS sizes match during code generation. */ 1617 pFirst->iTable = pColumns->nId; 1618 } 1619 1620 vector_append_error: 1621 sqlite3ExprDelete(db, pExpr); 1622 sqlite3IdListDelete(db, pColumns); 1623 return pList; 1624 } 1625 1626 /* 1627 ** Set the sort order for the last element on the given ExprList. 1628 */ 1629 void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ 1630 if( p==0 ) return; 1631 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); 1632 assert( p->nExpr>0 ); 1633 if( iSortOrder<0 ){ 1634 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); 1635 return; 1636 } 1637 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; 1638 } 1639 1640 /* 1641 ** Set the ExprList.a[].zName element of the most recently added item 1642 ** on the expression list. 1643 ** 1644 ** pList might be NULL following an OOM error. But pName should never be 1645 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1646 ** is set. 1647 */ 1648 void sqlite3ExprListSetName( 1649 Parse *pParse, /* Parsing context */ 1650 ExprList *pList, /* List to which to add the span. */ 1651 Token *pName, /* Name to be added */ 1652 int dequote /* True to cause the name to be dequoted */ 1653 ){ 1654 assert( pList!=0 || pParse->db->mallocFailed!=0 ); 1655 if( pList ){ 1656 struct ExprList_item *pItem; 1657 assert( pList->nExpr>0 ); 1658 pItem = &pList->a[pList->nExpr-1]; 1659 assert( pItem->zName==0 ); 1660 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); 1661 if( dequote ) sqlite3Dequote(pItem->zName); 1662 } 1663 } 1664 1665 /* 1666 ** Set the ExprList.a[].zSpan element of the most recently added item 1667 ** on the expression list. 1668 ** 1669 ** pList might be NULL following an OOM error. But pSpan should never be 1670 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1671 ** is set. 1672 */ 1673 void sqlite3ExprListSetSpan( 1674 Parse *pParse, /* Parsing context */ 1675 ExprList *pList, /* List to which to add the span. */ 1676 const char *zStart, /* Start of the span */ 1677 const char *zEnd /* End of the span */ 1678 ){ 1679 sqlite3 *db = pParse->db; 1680 assert( pList!=0 || db->mallocFailed!=0 ); 1681 if( pList ){ 1682 struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; 1683 assert( pList->nExpr>0 ); 1684 sqlite3DbFree(db, pItem->zSpan); 1685 pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd); 1686 } 1687 } 1688 1689 /* 1690 ** If the expression list pEList contains more than iLimit elements, 1691 ** leave an error message in pParse. 1692 */ 1693 void sqlite3ExprListCheckLength( 1694 Parse *pParse, 1695 ExprList *pEList, 1696 const char *zObject 1697 ){ 1698 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 1699 testcase( pEList && pEList->nExpr==mx ); 1700 testcase( pEList && pEList->nExpr==mx+1 ); 1701 if( pEList && pEList->nExpr>mx ){ 1702 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 1703 } 1704 } 1705 1706 /* 1707 ** Delete an entire expression list. 1708 */ 1709 static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ 1710 int i = pList->nExpr; 1711 struct ExprList_item *pItem = pList->a; 1712 assert( pList->nExpr>0 ); 1713 do{ 1714 sqlite3ExprDelete(db, pItem->pExpr); 1715 sqlite3DbFree(db, pItem->zName); 1716 sqlite3DbFree(db, pItem->zSpan); 1717 pItem++; 1718 }while( --i>0 ); 1719 sqlite3DbFreeNN(db, pList); 1720 } 1721 void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ 1722 if( pList ) exprListDeleteNN(db, pList); 1723 } 1724 1725 /* 1726 ** Return the bitwise-OR of all Expr.flags fields in the given 1727 ** ExprList. 1728 */ 1729 u32 sqlite3ExprListFlags(const ExprList *pList){ 1730 int i; 1731 u32 m = 0; 1732 assert( pList!=0 ); 1733 for(i=0; i<pList->nExpr; i++){ 1734 Expr *pExpr = pList->a[i].pExpr; 1735 assert( pExpr!=0 ); 1736 m |= pExpr->flags; 1737 } 1738 return m; 1739 } 1740 1741 /* 1742 ** This is a SELECT-node callback for the expression walker that 1743 ** always "fails". By "fail" in this case, we mean set 1744 ** pWalker->eCode to zero and abort. 1745 ** 1746 ** This callback is used by multiple expression walkers. 1747 */ 1748 int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){ 1749 UNUSED_PARAMETER(NotUsed); 1750 pWalker->eCode = 0; 1751 return WRC_Abort; 1752 } 1753 1754 /* 1755 ** If the input expression is an ID with the name "true" or "false" 1756 ** then convert it into an TK_TRUEFALSE term. Return non-zero if 1757 ** the conversion happened, and zero if the expression is unaltered. 1758 */ 1759 int sqlite3ExprIdToTrueFalse(Expr *pExpr){ 1760 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING ); 1761 if( sqlite3StrICmp(pExpr->u.zToken, "true")==0 1762 || sqlite3StrICmp(pExpr->u.zToken, "false")==0 1763 ){ 1764 pExpr->op = TK_TRUEFALSE; 1765 return 1; 1766 } 1767 return 0; 1768 } 1769 1770 /* 1771 ** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE 1772 ** and 0 if it is FALSE. 1773 */ 1774 int sqlite3ExprTruthValue(const Expr *pExpr){ 1775 assert( pExpr->op==TK_TRUEFALSE ); 1776 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0 1777 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 ); 1778 return pExpr->u.zToken[4]==0; 1779 } 1780 1781 1782 /* 1783 ** These routines are Walker callbacks used to check expressions to 1784 ** see if they are "constant" for some definition of constant. The 1785 ** Walker.eCode value determines the type of "constant" we are looking 1786 ** for. 1787 ** 1788 ** These callback routines are used to implement the following: 1789 ** 1790 ** sqlite3ExprIsConstant() pWalker->eCode==1 1791 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 1792 ** sqlite3ExprIsTableConstant() pWalker->eCode==3 1793 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 1794 ** 1795 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression 1796 ** is found to not be a constant. 1797 ** 1798 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions 1799 ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing 1800 ** an existing schema and 4 when processing a new statement. A bound 1801 ** parameter raises an error for new statements, but is silently converted 1802 ** to NULL for existing schemas. This allows sqlite_master tables that 1803 ** contain a bound parameter because they were generated by older versions 1804 ** of SQLite to be parsed by newer versions of SQLite without raising a 1805 ** malformed schema error. 1806 */ 1807 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 1808 1809 /* If pWalker->eCode is 2 then any term of the expression that comes from 1810 ** the ON or USING clauses of a left join disqualifies the expression 1811 ** from being considered constant. */ 1812 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ 1813 pWalker->eCode = 0; 1814 return WRC_Abort; 1815 } 1816 1817 switch( pExpr->op ){ 1818 /* Consider functions to be constant if all their arguments are constant 1819 ** and either pWalker->eCode==4 or 5 or the function has the 1820 ** SQLITE_FUNC_CONST flag. */ 1821 case TK_FUNCTION: 1822 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ 1823 return WRC_Continue; 1824 }else{ 1825 pWalker->eCode = 0; 1826 return WRC_Abort; 1827 } 1828 case TK_ID: 1829 /* Convert "true" or "false" in a DEFAULT clause into the 1830 ** appropriate TK_TRUEFALSE operator */ 1831 if( sqlite3ExprIdToTrueFalse(pExpr) ){ 1832 return WRC_Prune; 1833 } 1834 /* Fall thru */ 1835 case TK_COLUMN: 1836 case TK_AGG_FUNCTION: 1837 case TK_AGG_COLUMN: 1838 testcase( pExpr->op==TK_ID ); 1839 testcase( pExpr->op==TK_COLUMN ); 1840 testcase( pExpr->op==TK_AGG_FUNCTION ); 1841 testcase( pExpr->op==TK_AGG_COLUMN ); 1842 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ 1843 return WRC_Continue; 1844 } 1845 /* Fall through */ 1846 case TK_IF_NULL_ROW: 1847 case TK_REGISTER: 1848 testcase( pExpr->op==TK_REGISTER ); 1849 testcase( pExpr->op==TK_IF_NULL_ROW ); 1850 pWalker->eCode = 0; 1851 return WRC_Abort; 1852 case TK_VARIABLE: 1853 if( pWalker->eCode==5 ){ 1854 /* Silently convert bound parameters that appear inside of CREATE 1855 ** statements into a NULL when parsing the CREATE statement text out 1856 ** of the sqlite_master table */ 1857 pExpr->op = TK_NULL; 1858 }else if( pWalker->eCode==4 ){ 1859 /* A bound parameter in a CREATE statement that originates from 1860 ** sqlite3_prepare() causes an error */ 1861 pWalker->eCode = 0; 1862 return WRC_Abort; 1863 } 1864 /* Fall through */ 1865 default: 1866 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */ 1867 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */ 1868 return WRC_Continue; 1869 } 1870 } 1871 static int exprIsConst(Expr *p, int initFlag, int iCur){ 1872 Walker w; 1873 w.eCode = initFlag; 1874 w.xExprCallback = exprNodeIsConstant; 1875 w.xSelectCallback = sqlite3SelectWalkFail; 1876 #ifdef SQLITE_DEBUG 1877 w.xSelectCallback2 = sqlite3SelectWalkAssert2; 1878 #endif 1879 w.u.iCur = iCur; 1880 sqlite3WalkExpr(&w, p); 1881 return w.eCode; 1882 } 1883 1884 /* 1885 ** Walk an expression tree. Return non-zero if the expression is constant 1886 ** and 0 if it involves variables or function calls. 1887 ** 1888 ** For the purposes of this function, a double-quoted string (ex: "abc") 1889 ** is considered a variable but a single-quoted string (ex: 'abc') is 1890 ** a constant. 1891 */ 1892 int sqlite3ExprIsConstant(Expr *p){ 1893 return exprIsConst(p, 1, 0); 1894 } 1895 1896 /* 1897 ** Walk an expression tree. Return non-zero if the expression is constant 1898 ** that does no originate from the ON or USING clauses of a join. 1899 ** Return 0 if it involves variables or function calls or terms from 1900 ** an ON or USING clause. 1901 */ 1902 int sqlite3ExprIsConstantNotJoin(Expr *p){ 1903 return exprIsConst(p, 2, 0); 1904 } 1905 1906 /* 1907 ** Walk an expression tree. Return non-zero if the expression is constant 1908 ** for any single row of the table with cursor iCur. In other words, the 1909 ** expression must not refer to any non-deterministic function nor any 1910 ** table other than iCur. 1911 */ 1912 int sqlite3ExprIsTableConstant(Expr *p, int iCur){ 1913 return exprIsConst(p, 3, iCur); 1914 } 1915 1916 1917 /* 1918 ** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy(). 1919 */ 1920 static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){ 1921 ExprList *pGroupBy = pWalker->u.pGroupBy; 1922 int i; 1923 1924 /* Check if pExpr is identical to any GROUP BY term. If so, consider 1925 ** it constant. */ 1926 for(i=0; i<pGroupBy->nExpr; i++){ 1927 Expr *p = pGroupBy->a[i].pExpr; 1928 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){ 1929 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p); 1930 if( sqlite3_stricmp("BINARY", pColl->zName)==0 ){ 1931 return WRC_Prune; 1932 } 1933 } 1934 } 1935 1936 /* Check if pExpr is a sub-select. If so, consider it variable. */ 1937 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 1938 pWalker->eCode = 0; 1939 return WRC_Abort; 1940 } 1941 1942 return exprNodeIsConstant(pWalker, pExpr); 1943 } 1944 1945 /* 1946 ** Walk the expression tree passed as the first argument. Return non-zero 1947 ** if the expression consists entirely of constants or copies of terms 1948 ** in pGroupBy that sort with the BINARY collation sequence. 1949 ** 1950 ** This routine is used to determine if a term of the HAVING clause can 1951 ** be promoted into the WHERE clause. In order for such a promotion to work, 1952 ** the value of the HAVING clause term must be the same for all members of 1953 ** a "group". The requirement that the GROUP BY term must be BINARY 1954 ** assumes that no other collating sequence will have a finer-grained 1955 ** grouping than binary. In other words (A=B COLLATE binary) implies 1956 ** A=B in every other collating sequence. The requirement that the 1957 ** GROUP BY be BINARY is stricter than necessary. It would also work 1958 ** to promote HAVING clauses that use the same alternative collating 1959 ** sequence as the GROUP BY term, but that is much harder to check, 1960 ** alternative collating sequences are uncommon, and this is only an 1961 ** optimization, so we take the easy way out and simply require the 1962 ** GROUP BY to use the BINARY collating sequence. 1963 */ 1964 int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){ 1965 Walker w; 1966 w.eCode = 1; 1967 w.xExprCallback = exprNodeIsConstantOrGroupBy; 1968 w.xSelectCallback = 0; 1969 w.u.pGroupBy = pGroupBy; 1970 w.pParse = pParse; 1971 sqlite3WalkExpr(&w, p); 1972 return w.eCode; 1973 } 1974 1975 /* 1976 ** Walk an expression tree. Return non-zero if the expression is constant 1977 ** or a function call with constant arguments. Return and 0 if there 1978 ** are any variables. 1979 ** 1980 ** For the purposes of this function, a double-quoted string (ex: "abc") 1981 ** is considered a variable but a single-quoted string (ex: 'abc') is 1982 ** a constant. 1983 */ 1984 int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 1985 assert( isInit==0 || isInit==1 ); 1986 return exprIsConst(p, 4+isInit, 0); 1987 } 1988 1989 #ifdef SQLITE_ENABLE_CURSOR_HINTS 1990 /* 1991 ** Walk an expression tree. Return 1 if the expression contains a 1992 ** subquery of some kind. Return 0 if there are no subqueries. 1993 */ 1994 int sqlite3ExprContainsSubquery(Expr *p){ 1995 Walker w; 1996 w.eCode = 1; 1997 w.xExprCallback = sqlite3ExprWalkNoop; 1998 w.xSelectCallback = sqlite3SelectWalkFail; 1999 #ifdef SQLITE_DEBUG 2000 w.xSelectCallback2 = sqlite3SelectWalkAssert2; 2001 #endif 2002 sqlite3WalkExpr(&w, p); 2003 return w.eCode==0; 2004 } 2005 #endif 2006 2007 /* 2008 ** If the expression p codes a constant integer that is small enough 2009 ** to fit in a 32-bit integer, return 1 and put the value of the integer 2010 ** in *pValue. If the expression is not an integer or if it is too big 2011 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 2012 */ 2013 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 2014 int rc = 0; 2015 if( p==0 ) return 0; /* Can only happen following on OOM */ 2016 2017 /* If an expression is an integer literal that fits in a signed 32-bit 2018 ** integer, then the EP_IntValue flag will have already been set */ 2019 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 2020 || sqlite3GetInt32(p->u.zToken, &rc)==0 ); 2021 2022 if( p->flags & EP_IntValue ){ 2023 *pValue = p->u.iValue; 2024 return 1; 2025 } 2026 switch( p->op ){ 2027 case TK_UPLUS: { 2028 rc = sqlite3ExprIsInteger(p->pLeft, pValue); 2029 break; 2030 } 2031 case TK_UMINUS: { 2032 int v; 2033 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 2034 assert( v!=(-2147483647-1) ); 2035 *pValue = -v; 2036 rc = 1; 2037 } 2038 break; 2039 } 2040 default: break; 2041 } 2042 return rc; 2043 } 2044 2045 /* 2046 ** Return FALSE if there is no chance that the expression can be NULL. 2047 ** 2048 ** If the expression might be NULL or if the expression is too complex 2049 ** to tell return TRUE. 2050 ** 2051 ** This routine is used as an optimization, to skip OP_IsNull opcodes 2052 ** when we know that a value cannot be NULL. Hence, a false positive 2053 ** (returning TRUE when in fact the expression can never be NULL) might 2054 ** be a small performance hit but is otherwise harmless. On the other 2055 ** hand, a false negative (returning FALSE when the result could be NULL) 2056 ** will likely result in an incorrect answer. So when in doubt, return 2057 ** TRUE. 2058 */ 2059 int sqlite3ExprCanBeNull(const Expr *p){ 2060 u8 op; 2061 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 2062 op = p->op; 2063 if( op==TK_REGISTER ) op = p->op2; 2064 switch( op ){ 2065 case TK_INTEGER: 2066 case TK_STRING: 2067 case TK_FLOAT: 2068 case TK_BLOB: 2069 return 0; 2070 case TK_COLUMN: 2071 return ExprHasProperty(p, EP_CanBeNull) || 2072 p->pTab==0 || /* Reference to column of index on expression */ 2073 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); 2074 default: 2075 return 1; 2076 } 2077 } 2078 2079 /* 2080 ** Return TRUE if the given expression is a constant which would be 2081 ** unchanged by OP_Affinity with the affinity given in the second 2082 ** argument. 2083 ** 2084 ** This routine is used to determine if the OP_Affinity operation 2085 ** can be omitted. When in doubt return FALSE. A false negative 2086 ** is harmless. A false positive, however, can result in the wrong 2087 ** answer. 2088 */ 2089 int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 2090 u8 op; 2091 if( aff==SQLITE_AFF_BLOB ) return 1; 2092 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 2093 op = p->op; 2094 if( op==TK_REGISTER ) op = p->op2; 2095 switch( op ){ 2096 case TK_INTEGER: { 2097 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; 2098 } 2099 case TK_FLOAT: { 2100 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; 2101 } 2102 case TK_STRING: { 2103 return aff==SQLITE_AFF_TEXT; 2104 } 2105 case TK_BLOB: { 2106 return 1; 2107 } 2108 case TK_COLUMN: { 2109 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ 2110 return p->iColumn<0 2111 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); 2112 } 2113 default: { 2114 return 0; 2115 } 2116 } 2117 } 2118 2119 /* 2120 ** Return TRUE if the given string is a row-id column name. 2121 */ 2122 int sqlite3IsRowid(const char *z){ 2123 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 2124 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 2125 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 2126 return 0; 2127 } 2128 2129 /* 2130 ** pX is the RHS of an IN operator. If pX is a SELECT statement 2131 ** that can be simplified to a direct table access, then return 2132 ** a pointer to the SELECT statement. If pX is not a SELECT statement, 2133 ** or if the SELECT statement needs to be manifested into a transient 2134 ** table, then return NULL. 2135 */ 2136 #ifndef SQLITE_OMIT_SUBQUERY 2137 static Select *isCandidateForInOpt(Expr *pX){ 2138 Select *p; 2139 SrcList *pSrc; 2140 ExprList *pEList; 2141 Table *pTab; 2142 int i; 2143 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ 2144 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ 2145 p = pX->x.pSelect; 2146 if( p->pPrior ) return 0; /* Not a compound SELECT */ 2147 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ 2148 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); 2149 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); 2150 return 0; /* No DISTINCT keyword and no aggregate functions */ 2151 } 2152 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ 2153 if( p->pLimit ) return 0; /* Has no LIMIT clause */ 2154 if( p->pWhere ) return 0; /* Has no WHERE clause */ 2155 pSrc = p->pSrc; 2156 assert( pSrc!=0 ); 2157 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ 2158 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ 2159 pTab = pSrc->a[0].pTab; 2160 assert( pTab!=0 ); 2161 assert( pTab->pSelect==0 ); /* FROM clause is not a view */ 2162 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ 2163 pEList = p->pEList; 2164 assert( pEList!=0 ); 2165 /* All SELECT results must be columns. */ 2166 for(i=0; i<pEList->nExpr; i++){ 2167 Expr *pRes = pEList->a[i].pExpr; 2168 if( pRes->op!=TK_COLUMN ) return 0; 2169 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ 2170 } 2171 return p; 2172 } 2173 #endif /* SQLITE_OMIT_SUBQUERY */ 2174 2175 #ifndef SQLITE_OMIT_SUBQUERY 2176 /* 2177 ** Generate code that checks the left-most column of index table iCur to see if 2178 ** it contains any NULL entries. Cause the register at regHasNull to be set 2179 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 2180 ** to be set to NULL if iCur contains one or more NULL values. 2181 */ 2182 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 2183 int addr1; 2184 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 2185 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 2186 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 2187 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 2188 VdbeComment((v, "first_entry_in(%d)", iCur)); 2189 sqlite3VdbeJumpHere(v, addr1); 2190 } 2191 #endif 2192 2193 2194 #ifndef SQLITE_OMIT_SUBQUERY 2195 /* 2196 ** The argument is an IN operator with a list (not a subquery) on the 2197 ** right-hand side. Return TRUE if that list is constant. 2198 */ 2199 static int sqlite3InRhsIsConstant(Expr *pIn){ 2200 Expr *pLHS; 2201 int res; 2202 assert( !ExprHasProperty(pIn, EP_xIsSelect) ); 2203 pLHS = pIn->pLeft; 2204 pIn->pLeft = 0; 2205 res = sqlite3ExprIsConstant(pIn); 2206 pIn->pLeft = pLHS; 2207 return res; 2208 } 2209 #endif 2210 2211 /* 2212 ** This function is used by the implementation of the IN (...) operator. 2213 ** The pX parameter is the expression on the RHS of the IN operator, which 2214 ** might be either a list of expressions or a subquery. 2215 ** 2216 ** The job of this routine is to find or create a b-tree object that can 2217 ** be used either to test for membership in the RHS set or to iterate through 2218 ** all members of the RHS set, skipping duplicates. 2219 ** 2220 ** A cursor is opened on the b-tree object that is the RHS of the IN operator 2221 ** and pX->iTable is set to the index of that cursor. 2222 ** 2223 ** The returned value of this function indicates the b-tree type, as follows: 2224 ** 2225 ** IN_INDEX_ROWID - The cursor was opened on a database table. 2226 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. 2227 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. 2228 ** IN_INDEX_EPH - The cursor was opened on a specially created and 2229 ** populated epheremal table. 2230 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be 2231 ** implemented as a sequence of comparisons. 2232 ** 2233 ** An existing b-tree might be used if the RHS expression pX is a simple 2234 ** subquery such as: 2235 ** 2236 ** SELECT <column1>, <column2>... FROM <table> 2237 ** 2238 ** If the RHS of the IN operator is a list or a more complex subquery, then 2239 ** an ephemeral table might need to be generated from the RHS and then 2240 ** pX->iTable made to point to the ephemeral table instead of an 2241 ** existing table. 2242 ** 2243 ** The inFlags parameter must contain, at a minimum, one of the bits 2244 ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains 2245 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast 2246 ** membership test. When the IN_INDEX_LOOP bit is set, the IN index will 2247 ** be used to loop over all values of the RHS of the IN operator. 2248 ** 2249 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate 2250 ** through the set members) then the b-tree must not contain duplicates. 2251 ** An epheremal table will be created unless the selected columns are guaranteed 2252 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to 2253 ** a UNIQUE constraint or index. 2254 ** 2255 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 2256 ** for fast set membership tests) then an epheremal table must 2257 ** be used unless <columns> is a single INTEGER PRIMARY KEY column or an 2258 ** index can be found with the specified <columns> as its left-most. 2259 ** 2260 ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and 2261 ** if the RHS of the IN operator is a list (not a subquery) then this 2262 ** routine might decide that creating an ephemeral b-tree for membership 2263 ** testing is too expensive and return IN_INDEX_NOOP. In that case, the 2264 ** calling routine should implement the IN operator using a sequence 2265 ** of Eq or Ne comparison operations. 2266 ** 2267 ** When the b-tree is being used for membership tests, the calling function 2268 ** might need to know whether or not the RHS side of the IN operator 2269 ** contains a NULL. If prRhsHasNull is not a NULL pointer and 2270 ** if there is any chance that the (...) might contain a NULL value at 2271 ** runtime, then a register is allocated and the register number written 2272 ** to *prRhsHasNull. If there is no chance that the (...) contains a 2273 ** NULL value, then *prRhsHasNull is left unchanged. 2274 ** 2275 ** If a register is allocated and its location stored in *prRhsHasNull, then 2276 ** the value in that register will be NULL if the b-tree contains one or more 2277 ** NULL values, and it will be some non-NULL value if the b-tree contains no 2278 ** NULL values. 2279 ** 2280 ** If the aiMap parameter is not NULL, it must point to an array containing 2281 ** one element for each column returned by the SELECT statement on the RHS 2282 ** of the IN(...) operator. The i'th entry of the array is populated with the 2283 ** offset of the index column that matches the i'th column returned by the 2284 ** SELECT. For example, if the expression and selected index are: 2285 ** 2286 ** (?,?,?) IN (SELECT a, b, c FROM t1) 2287 ** CREATE INDEX i1 ON t1(b, c, a); 2288 ** 2289 ** then aiMap[] is populated with {2, 0, 1}. 2290 */ 2291 #ifndef SQLITE_OMIT_SUBQUERY 2292 int sqlite3FindInIndex( 2293 Parse *pParse, /* Parsing context */ 2294 Expr *pX, /* The right-hand side (RHS) of the IN operator */ 2295 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */ 2296 int *prRhsHasNull, /* Register holding NULL status. See notes */ 2297 int *aiMap /* Mapping from Index fields to RHS fields */ 2298 ){ 2299 Select *p; /* SELECT to the right of IN operator */ 2300 int eType = 0; /* Type of RHS table. IN_INDEX_* */ 2301 int iTab = pParse->nTab++; /* Cursor of the RHS table */ 2302 int mustBeUnique; /* True if RHS must be unique */ 2303 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 2304 2305 assert( pX->op==TK_IN ); 2306 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 2307 2308 /* If the RHS of this IN(...) operator is a SELECT, and if it matters 2309 ** whether or not the SELECT result contains NULL values, check whether 2310 ** or not NULL is actually possible (it may not be, for example, due 2311 ** to NOT NULL constraints in the schema). If no NULL values are possible, 2312 ** set prRhsHasNull to 0 before continuing. */ 2313 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){ 2314 int i; 2315 ExprList *pEList = pX->x.pSelect->pEList; 2316 for(i=0; i<pEList->nExpr; i++){ 2317 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break; 2318 } 2319 if( i==pEList->nExpr ){ 2320 prRhsHasNull = 0; 2321 } 2322 } 2323 2324 /* Check to see if an existing table or index can be used to 2325 ** satisfy the query. This is preferable to generating a new 2326 ** ephemeral table. */ 2327 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ 2328 sqlite3 *db = pParse->db; /* Database connection */ 2329 Table *pTab; /* Table <table>. */ 2330 i16 iDb; /* Database idx for pTab */ 2331 ExprList *pEList = p->pEList; 2332 int nExpr = pEList->nExpr; 2333 2334 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 2335 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 2336 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 2337 pTab = p->pSrc->a[0].pTab; 2338 2339 /* Code an OP_Transaction and OP_TableLock for <table>. */ 2340 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 2341 sqlite3CodeVerifySchema(pParse, iDb); 2342 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 2343 2344 assert(v); /* sqlite3GetVdbe() has always been previously called */ 2345 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ 2346 /* The "x IN (SELECT rowid FROM table)" case */ 2347 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); 2348 VdbeCoverage(v); 2349 2350 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 2351 eType = IN_INDEX_ROWID; 2352 2353 sqlite3VdbeJumpHere(v, iAddr); 2354 }else{ 2355 Index *pIdx; /* Iterator variable */ 2356 int affinity_ok = 1; 2357 int i; 2358 2359 /* Check that the affinity that will be used to perform each 2360 ** comparison is the same as the affinity of each column in table 2361 ** on the RHS of the IN operator. If it not, it is not possible to 2362 ** use any index of the RHS table. */ 2363 for(i=0; i<nExpr && affinity_ok; i++){ 2364 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); 2365 int iCol = pEList->a[i].pExpr->iColumn; 2366 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */ 2367 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); 2368 testcase( cmpaff==SQLITE_AFF_BLOB ); 2369 testcase( cmpaff==SQLITE_AFF_TEXT ); 2370 switch( cmpaff ){ 2371 case SQLITE_AFF_BLOB: 2372 break; 2373 case SQLITE_AFF_TEXT: 2374 /* sqlite3CompareAffinity() only returns TEXT if one side or the 2375 ** other has no affinity and the other side is TEXT. Hence, 2376 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT 2377 ** and for the term on the LHS of the IN to have no affinity. */ 2378 assert( idxaff==SQLITE_AFF_TEXT ); 2379 break; 2380 default: 2381 affinity_ok = sqlite3IsNumericAffinity(idxaff); 2382 } 2383 } 2384 2385 if( affinity_ok ){ 2386 /* Search for an existing index that will work for this IN operator */ 2387 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){ 2388 Bitmask colUsed; /* Columns of the index used */ 2389 Bitmask mCol; /* Mask for the current column */ 2390 if( pIdx->nColumn<nExpr ) continue; 2391 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute 2392 ** BITMASK(nExpr) without overflowing */ 2393 testcase( pIdx->nColumn==BMS-2 ); 2394 testcase( pIdx->nColumn==BMS-1 ); 2395 if( pIdx->nColumn>=BMS-1 ) continue; 2396 if( mustBeUnique ){ 2397 if( pIdx->nKeyCol>nExpr 2398 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)) 2399 ){ 2400 continue; /* This index is not unique over the IN RHS columns */ 2401 } 2402 } 2403 2404 colUsed = 0; /* Columns of index used so far */ 2405 for(i=0; i<nExpr; i++){ 2406 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); 2407 Expr *pRhs = pEList->a[i].pExpr; 2408 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); 2409 int j; 2410 2411 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr ); 2412 for(j=0; j<nExpr; j++){ 2413 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; 2414 assert( pIdx->azColl[j] ); 2415 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ 2416 continue; 2417 } 2418 break; 2419 } 2420 if( j==nExpr ) break; 2421 mCol = MASKBIT(j); 2422 if( mCol & colUsed ) break; /* Each column used only once */ 2423 colUsed |= mCol; 2424 if( aiMap ) aiMap[i] = j; 2425 } 2426 2427 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) ); 2428 if( colUsed==(MASKBIT(nExpr)-1) ){ 2429 /* If we reach this point, that means the index pIdx is usable */ 2430 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); 2431 ExplainQueryPlan((pParse, 0, 2432 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName)); 2433 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); 2434 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 2435 VdbeComment((v, "%s", pIdx->zName)); 2436 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); 2437 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; 2438 2439 if( prRhsHasNull ){ 2440 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 2441 i64 mask = (1<<nExpr)-1; 2442 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 2443 iTab, 0, 0, (u8*)&mask, P4_INT64); 2444 #endif 2445 *prRhsHasNull = ++pParse->nMem; 2446 if( nExpr==1 ){ 2447 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); 2448 } 2449 } 2450 sqlite3VdbeJumpHere(v, iAddr); 2451 } 2452 } /* End loop over indexes */ 2453 } /* End if( affinity_ok ) */ 2454 } /* End if not an rowid index */ 2455 } /* End attempt to optimize using an index */ 2456 2457 /* If no preexisting index is available for the IN clause 2458 ** and IN_INDEX_NOOP is an allowed reply 2459 ** and the RHS of the IN operator is a list, not a subquery 2460 ** and the RHS is not constant or has two or fewer terms, 2461 ** then it is not worth creating an ephemeral table to evaluate 2462 ** the IN operator so return IN_INDEX_NOOP. 2463 */ 2464 if( eType==0 2465 && (inFlags & IN_INDEX_NOOP_OK) 2466 && !ExprHasProperty(pX, EP_xIsSelect) 2467 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) 2468 ){ 2469 eType = IN_INDEX_NOOP; 2470 } 2471 2472 if( eType==0 ){ 2473 /* Could not find an existing table or index to use as the RHS b-tree. 2474 ** We will have to generate an ephemeral table to do the job. 2475 */ 2476 u32 savedNQueryLoop = pParse->nQueryLoop; 2477 int rMayHaveNull = 0; 2478 eType = IN_INDEX_EPH; 2479 if( inFlags & IN_INDEX_LOOP ){ 2480 pParse->nQueryLoop = 0; 2481 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ 2482 eType = IN_INDEX_ROWID; 2483 } 2484 }else if( prRhsHasNull ){ 2485 *prRhsHasNull = rMayHaveNull = ++pParse->nMem; 2486 } 2487 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); 2488 pParse->nQueryLoop = savedNQueryLoop; 2489 }else{ 2490 pX->iTable = iTab; 2491 } 2492 2493 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ 2494 int i, n; 2495 n = sqlite3ExprVectorSize(pX->pLeft); 2496 for(i=0; i<n; i++) aiMap[i] = i; 2497 } 2498 return eType; 2499 } 2500 #endif 2501 2502 #ifndef SQLITE_OMIT_SUBQUERY 2503 /* 2504 ** Argument pExpr is an (?, ?...) IN(...) expression. This 2505 ** function allocates and returns a nul-terminated string containing 2506 ** the affinities to be used for each column of the comparison. 2507 ** 2508 ** It is the responsibility of the caller to ensure that the returned 2509 ** string is eventually freed using sqlite3DbFree(). 2510 */ 2511 static char *exprINAffinity(Parse *pParse, Expr *pExpr){ 2512 Expr *pLeft = pExpr->pLeft; 2513 int nVal = sqlite3ExprVectorSize(pLeft); 2514 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0; 2515 char *zRet; 2516 2517 assert( pExpr->op==TK_IN ); 2518 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1); 2519 if( zRet ){ 2520 int i; 2521 for(i=0; i<nVal; i++){ 2522 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i); 2523 char a = sqlite3ExprAffinity(pA); 2524 if( pSelect ){ 2525 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a); 2526 }else{ 2527 zRet[i] = a; 2528 } 2529 } 2530 zRet[nVal] = '\0'; 2531 } 2532 return zRet; 2533 } 2534 #endif 2535 2536 #ifndef SQLITE_OMIT_SUBQUERY 2537 /* 2538 ** Load the Parse object passed as the first argument with an error 2539 ** message of the form: 2540 ** 2541 ** "sub-select returns N columns - expected M" 2542 */ 2543 void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){ 2544 const char *zFmt = "sub-select returns %d columns - expected %d"; 2545 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect); 2546 } 2547 #endif 2548 2549 /* 2550 ** Expression pExpr is a vector that has been used in a context where 2551 ** it is not permitted. If pExpr is a sub-select vector, this routine 2552 ** loads the Parse object with a message of the form: 2553 ** 2554 ** "sub-select returns N columns - expected 1" 2555 ** 2556 ** Or, if it is a regular scalar vector: 2557 ** 2558 ** "row value misused" 2559 */ 2560 void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){ 2561 #ifndef SQLITE_OMIT_SUBQUERY 2562 if( pExpr->flags & EP_xIsSelect ){ 2563 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1); 2564 }else 2565 #endif 2566 { 2567 sqlite3ErrorMsg(pParse, "row value misused"); 2568 } 2569 } 2570 2571 /* 2572 ** Generate code for scalar subqueries used as a subquery expression, EXISTS, 2573 ** or IN operators. Examples: 2574 ** 2575 ** (SELECT a FROM b) -- subquery 2576 ** EXISTS (SELECT a FROM b) -- EXISTS subquery 2577 ** x IN (4,5,11) -- IN operator with list on right-hand side 2578 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 2579 ** 2580 ** The pExpr parameter describes the expression that contains the IN 2581 ** operator or subquery. 2582 ** 2583 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed 2584 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference 2585 ** to some integer key column of a table B-Tree. In this case, use an 2586 ** intkey B-Tree to store the set of IN(...) values instead of the usual 2587 ** (slower) variable length keys B-Tree. 2588 ** 2589 ** If rMayHaveNull is non-zero, that means that the operation is an IN 2590 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. 2591 ** All this routine does is initialize the register given by rMayHaveNull 2592 ** to NULL. Calling routines will take care of changing this register 2593 ** value to non-NULL if the RHS is NULL-free. 2594 ** 2595 ** For a SELECT or EXISTS operator, return the register that holds the 2596 ** result. For a multi-column SELECT, the result is stored in a contiguous 2597 ** array of registers and the return value is the register of the left-most 2598 ** result column. Return 0 for IN operators or if an error occurs. 2599 */ 2600 #ifndef SQLITE_OMIT_SUBQUERY 2601 int sqlite3CodeSubselect( 2602 Parse *pParse, /* Parsing context */ 2603 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ 2604 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ 2605 int isRowid /* If true, LHS of IN operator is a rowid */ 2606 ){ 2607 int jmpIfDynamic = -1; /* One-time test address */ 2608 int rReg = 0; /* Register storing resulting */ 2609 Vdbe *v = sqlite3GetVdbe(pParse); 2610 if( NEVER(v==0) ) return 0; 2611 sqlite3ExprCachePush(pParse); 2612 2613 /* The evaluation of the IN/EXISTS/SELECT must be repeated every time it 2614 ** is encountered if any of the following is true: 2615 ** 2616 ** * The right-hand side is a correlated subquery 2617 ** * The right-hand side is an expression list containing variables 2618 ** * We are inside a trigger 2619 ** 2620 ** If all of the above are false, then we can run this code just once 2621 ** save the results, and reuse the same result on subsequent invocations. 2622 */ 2623 if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 2624 jmpIfDynamic = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); 2625 } 2626 2627 switch( pExpr->op ){ 2628 case TK_IN: { 2629 int addr; /* Address of OP_OpenEphemeral instruction */ 2630 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ 2631 KeyInfo *pKeyInfo = 0; /* Key information */ 2632 int nVal; /* Size of vector pLeft */ 2633 2634 nVal = sqlite3ExprVectorSize(pLeft); 2635 assert( !isRowid || nVal==1 ); 2636 2637 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 2638 ** expression it is handled the same way. An ephemeral table is 2639 ** filled with index keys representing the results from the 2640 ** SELECT or the <exprlist>. 2641 ** 2642 ** If the 'x' expression is a column value, or the SELECT... 2643 ** statement returns a column value, then the affinity of that 2644 ** column is used to build the index keys. If both 'x' and the 2645 ** SELECT... statement are columns, then numeric affinity is used 2646 ** if either column has NUMERIC or INTEGER affinity. If neither 2647 ** 'x' nor the SELECT... statement are columns, then numeric affinity 2648 ** is used. 2649 */ 2650 pExpr->iTable = pParse->nTab++; 2651 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 2652 pExpr->iTable, (isRowid?0:nVal)); 2653 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1); 2654 2655 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 2656 /* Case 1: expr IN (SELECT ...) 2657 ** 2658 ** Generate code to write the results of the select into the temporary 2659 ** table allocated and opened above. 2660 */ 2661 Select *pSelect = pExpr->x.pSelect; 2662 ExprList *pEList = pSelect->pEList; 2663 2664 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY", 2665 jmpIfDynamic>=0?"":"CORRELATED " 2666 )); 2667 assert( !isRowid ); 2668 /* If the LHS and RHS of the IN operator do not match, that 2669 ** error will have been caught long before we reach this point. */ 2670 if( ALWAYS(pEList->nExpr==nVal) ){ 2671 SelectDest dest; 2672 int i; 2673 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 2674 dest.zAffSdst = exprINAffinity(pParse, pExpr); 2675 pSelect->iLimit = 0; 2676 testcase( pSelect->selFlags & SF_Distinct ); 2677 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ 2678 if( sqlite3Select(pParse, pSelect, &dest) ){ 2679 sqlite3DbFree(pParse->db, dest.zAffSdst); 2680 sqlite3KeyInfoUnref(pKeyInfo); 2681 return 0; 2682 } 2683 sqlite3DbFree(pParse->db, dest.zAffSdst); 2684 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ 2685 assert( pEList!=0 ); 2686 assert( pEList->nExpr>0 ); 2687 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 2688 for(i=0; i<nVal; i++){ 2689 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i); 2690 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( 2691 pParse, p, pEList->a[i].pExpr 2692 ); 2693 } 2694 } 2695 }else if( ALWAYS(pExpr->x.pList!=0) ){ 2696 /* Case 2: expr IN (exprlist) 2697 ** 2698 ** For each expression, build an index key from the evaluation and 2699 ** store it in the temporary table. If <expr> is a column, then use 2700 ** that columns affinity when building index keys. If <expr> is not 2701 ** a column, use numeric affinity. 2702 */ 2703 char affinity; /* Affinity of the LHS of the IN */ 2704 int i; 2705 ExprList *pList = pExpr->x.pList; 2706 struct ExprList_item *pItem; 2707 int r1, r2, r3; 2708 affinity = sqlite3ExprAffinity(pLeft); 2709 if( !affinity ){ 2710 affinity = SQLITE_AFF_BLOB; 2711 } 2712 if( pKeyInfo ){ 2713 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 2714 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2715 } 2716 2717 /* Loop through each expression in <exprlist>. */ 2718 r1 = sqlite3GetTempReg(pParse); 2719 r2 = sqlite3GetTempReg(pParse); 2720 if( isRowid ) sqlite3VdbeAddOp4(v, OP_Blob, 0, r2, 0, "", P4_STATIC); 2721 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 2722 Expr *pE2 = pItem->pExpr; 2723 int iValToIns; 2724 2725 /* If the expression is not constant then we will need to 2726 ** disable the test that was generated above that makes sure 2727 ** this code only executes once. Because for a non-constant 2728 ** expression we need to rerun this code each time. 2729 */ 2730 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ 2731 sqlite3VdbeChangeToNoop(v, jmpIfDynamic); 2732 jmpIfDynamic = -1; 2733 } 2734 2735 /* Evaluate the expression and insert it into the temp table */ 2736 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ 2737 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); 2738 }else{ 2739 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); 2740 if( isRowid ){ 2741 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, 2742 sqlite3VdbeCurrentAddr(v)+2); 2743 VdbeCoverage(v); 2744 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); 2745 }else{ 2746 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); 2747 sqlite3ExprCacheAffinityChange(pParse, r3, 1); 2748 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pExpr->iTable, r2, r3, 1); 2749 } 2750 } 2751 } 2752 sqlite3ReleaseTempReg(pParse, r1); 2753 sqlite3ReleaseTempReg(pParse, r2); 2754 } 2755 if( pKeyInfo ){ 2756 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); 2757 } 2758 break; 2759 } 2760 2761 case TK_EXISTS: 2762 case TK_SELECT: 2763 default: { 2764 /* Case 3: (SELECT ... FROM ...) 2765 ** or: EXISTS(SELECT ... FROM ...) 2766 ** 2767 ** For a SELECT, generate code to put the values for all columns of 2768 ** the first row into an array of registers and return the index of 2769 ** the first register. 2770 ** 2771 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists) 2772 ** into a register and return that register number. 2773 ** 2774 ** In both cases, the query is augmented with "LIMIT 1". Any 2775 ** preexisting limit is discarded in place of the new LIMIT 1. 2776 */ 2777 Select *pSel; /* SELECT statement to encode */ 2778 SelectDest dest; /* How to deal with SELECT result */ 2779 int nReg; /* Registers to allocate */ 2780 Expr *pLimit; /* New limit expression */ 2781 2782 testcase( pExpr->op==TK_EXISTS ); 2783 testcase( pExpr->op==TK_SELECT ); 2784 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); 2785 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); 2786 2787 pSel = pExpr->x.pSelect; 2788 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY", 2789 jmpIfDynamic>=0?"":"CORRELATED ")); 2790 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; 2791 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); 2792 pParse->nMem += nReg; 2793 if( pExpr->op==TK_SELECT ){ 2794 dest.eDest = SRT_Mem; 2795 dest.iSdst = dest.iSDParm; 2796 dest.nSdst = nReg; 2797 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1); 2798 VdbeComment((v, "Init subquery result")); 2799 }else{ 2800 dest.eDest = SRT_Exists; 2801 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 2802 VdbeComment((v, "Init EXISTS result")); 2803 } 2804 pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0); 2805 if( pSel->pLimit ){ 2806 sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft); 2807 pSel->pLimit->pLeft = pLimit; 2808 }else{ 2809 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0); 2810 } 2811 pSel->iLimit = 0; 2812 if( sqlite3Select(pParse, pSel, &dest) ){ 2813 return 0; 2814 } 2815 rReg = dest.iSDParm; 2816 ExprSetVVAProperty(pExpr, EP_NoReduce); 2817 break; 2818 } 2819 } 2820 2821 if( rHasNullFlag ){ 2822 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); 2823 } 2824 2825 if( jmpIfDynamic>=0 ){ 2826 sqlite3VdbeJumpHere(v, jmpIfDynamic); 2827 } 2828 sqlite3ExprCachePop(pParse); 2829 2830 return rReg; 2831 } 2832 #endif /* SQLITE_OMIT_SUBQUERY */ 2833 2834 #ifndef SQLITE_OMIT_SUBQUERY 2835 /* 2836 ** Expr pIn is an IN(...) expression. This function checks that the 2837 ** sub-select on the RHS of the IN() operator has the same number of 2838 ** columns as the vector on the LHS. Or, if the RHS of the IN() is not 2839 ** a sub-query, that the LHS is a vector of size 1. 2840 */ 2841 int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){ 2842 int nVector = sqlite3ExprVectorSize(pIn->pLeft); 2843 if( (pIn->flags & EP_xIsSelect) ){ 2844 if( nVector!=pIn->x.pSelect->pEList->nExpr ){ 2845 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector); 2846 return 1; 2847 } 2848 }else if( nVector!=1 ){ 2849 sqlite3VectorErrorMsg(pParse, pIn->pLeft); 2850 return 1; 2851 } 2852 return 0; 2853 } 2854 #endif 2855 2856 #ifndef SQLITE_OMIT_SUBQUERY 2857 /* 2858 ** Generate code for an IN expression. 2859 ** 2860 ** x IN (SELECT ...) 2861 ** x IN (value, value, ...) 2862 ** 2863 ** The left-hand side (LHS) is a scalar or vector expression. The 2864 ** right-hand side (RHS) is an array of zero or more scalar values, or a 2865 ** subquery. If the RHS is a subquery, the number of result columns must 2866 ** match the number of columns in the vector on the LHS. If the RHS is 2867 ** a list of values, the LHS must be a scalar. 2868 ** 2869 ** The IN operator is true if the LHS value is contained within the RHS. 2870 ** The result is false if the LHS is definitely not in the RHS. The 2871 ** result is NULL if the presence of the LHS in the RHS cannot be 2872 ** determined due to NULLs. 2873 ** 2874 ** This routine generates code that jumps to destIfFalse if the LHS is not 2875 ** contained within the RHS. If due to NULLs we cannot determine if the LHS 2876 ** is contained in the RHS then jump to destIfNull. If the LHS is contained 2877 ** within the RHS then fall through. 2878 ** 2879 ** See the separate in-operator.md documentation file in the canonical 2880 ** SQLite source tree for additional information. 2881 */ 2882 static void sqlite3ExprCodeIN( 2883 Parse *pParse, /* Parsing and code generating context */ 2884 Expr *pExpr, /* The IN expression */ 2885 int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 2886 int destIfNull /* Jump here if the results are unknown due to NULLs */ 2887 ){ 2888 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ 2889 int eType; /* Type of the RHS */ 2890 int rLhs; /* Register(s) holding the LHS values */ 2891 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */ 2892 Vdbe *v; /* Statement under construction */ 2893 int *aiMap = 0; /* Map from vector field to index column */ 2894 char *zAff = 0; /* Affinity string for comparisons */ 2895 int nVector; /* Size of vectors for this IN operator */ 2896 int iDummy; /* Dummy parameter to exprCodeVector() */ 2897 Expr *pLeft; /* The LHS of the IN operator */ 2898 int i; /* loop counter */ 2899 int destStep2; /* Where to jump when NULLs seen in step 2 */ 2900 int destStep6 = 0; /* Start of code for Step 6 */ 2901 int addrTruthOp; /* Address of opcode that determines the IN is true */ 2902 int destNotNull; /* Jump here if a comparison is not true in step 6 */ 2903 int addrTop; /* Top of the step-6 loop */ 2904 2905 pLeft = pExpr->pLeft; 2906 if( sqlite3ExprCheckIN(pParse, pExpr) ) return; 2907 zAff = exprINAffinity(pParse, pExpr); 2908 nVector = sqlite3ExprVectorSize(pExpr->pLeft); 2909 aiMap = (int*)sqlite3DbMallocZero( 2910 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1 2911 ); 2912 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error; 2913 2914 /* Attempt to compute the RHS. After this step, if anything other than 2915 ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable 2916 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, 2917 ** the RHS has not yet been coded. */ 2918 v = pParse->pVdbe; 2919 assert( v!=0 ); /* OOM detected prior to this routine */ 2920 VdbeNoopComment((v, "begin IN expr")); 2921 eType = sqlite3FindInIndex(pParse, pExpr, 2922 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, 2923 destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap); 2924 2925 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH 2926 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC 2927 ); 2928 #ifdef SQLITE_DEBUG 2929 /* Confirm that aiMap[] contains nVector integer values between 0 and 2930 ** nVector-1. */ 2931 for(i=0; i<nVector; i++){ 2932 int j, cnt; 2933 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++; 2934 assert( cnt==1 ); 2935 } 2936 #endif 2937 2938 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a 2939 ** vector, then it is stored in an array of nVector registers starting 2940 ** at r1. 2941 ** 2942 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector 2943 ** so that the fields are in the same order as an existing index. The 2944 ** aiMap[] array contains a mapping from the original LHS field order to 2945 ** the field order that matches the RHS index. 2946 */ 2947 sqlite3ExprCachePush(pParse); 2948 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy); 2949 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */ 2950 if( i==nVector ){ 2951 /* LHS fields are not reordered */ 2952 rLhs = rLhsOrig; 2953 }else{ 2954 /* Need to reorder the LHS fields according to aiMap */ 2955 rLhs = sqlite3GetTempRange(pParse, nVector); 2956 for(i=0; i<nVector; i++){ 2957 sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0); 2958 } 2959 } 2960 2961 /* If sqlite3FindInIndex() did not find or create an index that is 2962 ** suitable for evaluating the IN operator, then evaluate using a 2963 ** sequence of comparisons. 2964 ** 2965 ** This is step (1) in the in-operator.md optimized algorithm. 2966 */ 2967 if( eType==IN_INDEX_NOOP ){ 2968 ExprList *pList = pExpr->x.pList; 2969 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2970 int labelOk = sqlite3VdbeMakeLabel(v); 2971 int r2, regToFree; 2972 int regCkNull = 0; 2973 int ii; 2974 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2975 if( destIfNull!=destIfFalse ){ 2976 regCkNull = sqlite3GetTempReg(pParse); 2977 sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull); 2978 } 2979 for(ii=0; ii<pList->nExpr; ii++){ 2980 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); 2981 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ 2982 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); 2983 } 2984 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ 2985 sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2, 2986 (void*)pColl, P4_COLLSEQ); 2987 VdbeCoverageIf(v, ii<pList->nExpr-1); 2988 VdbeCoverageIf(v, ii==pList->nExpr-1); 2989 sqlite3VdbeChangeP5(v, zAff[0]); 2990 }else{ 2991 assert( destIfNull==destIfFalse ); 2992 sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2, 2993 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); 2994 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL); 2995 } 2996 sqlite3ReleaseTempReg(pParse, regToFree); 2997 } 2998 if( regCkNull ){ 2999 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 3000 sqlite3VdbeGoto(v, destIfFalse); 3001 } 3002 sqlite3VdbeResolveLabel(v, labelOk); 3003 sqlite3ReleaseTempReg(pParse, regCkNull); 3004 goto sqlite3ExprCodeIN_finished; 3005 } 3006 3007 /* Step 2: Check to see if the LHS contains any NULL columns. If the 3008 ** LHS does contain NULLs then the result must be either FALSE or NULL. 3009 ** We will then skip the binary search of the RHS. 3010 */ 3011 if( destIfNull==destIfFalse ){ 3012 destStep2 = destIfFalse; 3013 }else{ 3014 destStep2 = destStep6 = sqlite3VdbeMakeLabel(v); 3015 } 3016 for(i=0; i<nVector; i++){ 3017 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i); 3018 if( sqlite3ExprCanBeNull(p) ){ 3019 sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); 3020 VdbeCoverage(v); 3021 } 3022 } 3023 3024 /* Step 3. The LHS is now known to be non-NULL. Do the binary search 3025 ** of the RHS using the LHS as a probe. If found, the result is 3026 ** true. 3027 */ 3028 if( eType==IN_INDEX_ROWID ){ 3029 /* In this case, the RHS is the ROWID of table b-tree and so we also 3030 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4 3031 ** into a single opcode. */ 3032 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, rLhs); 3033 VdbeCoverage(v); 3034 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */ 3035 }else{ 3036 sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector); 3037 if( destIfFalse==destIfNull ){ 3038 /* Combine Step 3 and Step 5 into a single opcode */ 3039 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, 3040 rLhs, nVector); VdbeCoverage(v); 3041 goto sqlite3ExprCodeIN_finished; 3042 } 3043 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */ 3044 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, 3045 rLhs, nVector); VdbeCoverage(v); 3046 } 3047 3048 /* Step 4. If the RHS is known to be non-NULL and we did not find 3049 ** an match on the search above, then the result must be FALSE. 3050 */ 3051 if( rRhsHasNull && nVector==1 ){ 3052 sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse); 3053 VdbeCoverage(v); 3054 } 3055 3056 /* Step 5. If we do not care about the difference between NULL and 3057 ** FALSE, then just return false. 3058 */ 3059 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse); 3060 3061 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS. 3062 ** If any comparison is NULL, then the result is NULL. If all 3063 ** comparisons are FALSE then the final result is FALSE. 3064 ** 3065 ** For a scalar LHS, it is sufficient to check just the first row 3066 ** of the RHS. 3067 */ 3068 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6); 3069 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 3070 VdbeCoverage(v); 3071 if( nVector>1 ){ 3072 destNotNull = sqlite3VdbeMakeLabel(v); 3073 }else{ 3074 /* For nVector==1, combine steps 6 and 7 by immediately returning 3075 ** FALSE if the first comparison is not NULL */ 3076 destNotNull = destIfFalse; 3077 } 3078 for(i=0; i<nVector; i++){ 3079 Expr *p; 3080 CollSeq *pColl; 3081 int r3 = sqlite3GetTempReg(pParse); 3082 p = sqlite3VectorFieldSubexpr(pLeft, i); 3083 pColl = sqlite3ExprCollSeq(pParse, p); 3084 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r3); 3085 sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3, 3086 (void*)pColl, P4_COLLSEQ); 3087 VdbeCoverage(v); 3088 sqlite3ReleaseTempReg(pParse, r3); 3089 } 3090 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); 3091 if( nVector>1 ){ 3092 sqlite3VdbeResolveLabel(v, destNotNull); 3093 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrTop+1); 3094 VdbeCoverage(v); 3095 3096 /* Step 7: If we reach this point, we know that the result must 3097 ** be false. */ 3098 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 3099 } 3100 3101 /* Jumps here in order to return true. */ 3102 sqlite3VdbeJumpHere(v, addrTruthOp); 3103 3104 sqlite3ExprCodeIN_finished: 3105 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs); 3106 sqlite3ExprCachePop(pParse); 3107 VdbeComment((v, "end IN expr")); 3108 sqlite3ExprCodeIN_oom_error: 3109 sqlite3DbFree(pParse->db, aiMap); 3110 sqlite3DbFree(pParse->db, zAff); 3111 } 3112 #endif /* SQLITE_OMIT_SUBQUERY */ 3113 3114 #ifndef SQLITE_OMIT_FLOATING_POINT 3115 /* 3116 ** Generate an instruction that will put the floating point 3117 ** value described by z[0..n-1] into register iMem. 3118 ** 3119 ** The z[] string will probably not be zero-terminated. But the 3120 ** z[n] character is guaranteed to be something that does not look 3121 ** like the continuation of the number. 3122 */ 3123 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 3124 if( ALWAYS(z!=0) ){ 3125 double value; 3126 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 3127 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 3128 if( negateFlag ) value = -value; 3129 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); 3130 } 3131 } 3132 #endif 3133 3134 3135 /* 3136 ** Generate an instruction that will put the integer describe by 3137 ** text z[0..n-1] into register iMem. 3138 ** 3139 ** Expr.u.zToken is always UTF8 and zero-terminated. 3140 */ 3141 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 3142 Vdbe *v = pParse->pVdbe; 3143 if( pExpr->flags & EP_IntValue ){ 3144 int i = pExpr->u.iValue; 3145 assert( i>=0 ); 3146 if( negFlag ) i = -i; 3147 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 3148 }else{ 3149 int c; 3150 i64 value; 3151 const char *z = pExpr->u.zToken; 3152 assert( z!=0 ); 3153 c = sqlite3DecOrHexToI64(z, &value); 3154 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){ 3155 #ifdef SQLITE_OMIT_FLOATING_POINT 3156 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); 3157 #else 3158 #ifndef SQLITE_OMIT_HEX_INTEGER 3159 if( sqlite3_strnicmp(z,"0x",2)==0 ){ 3160 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z); 3161 }else 3162 #endif 3163 { 3164 codeReal(v, z, negFlag, iMem); 3165 } 3166 #endif 3167 }else{ 3168 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; } 3169 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); 3170 } 3171 } 3172 } 3173 3174 /* 3175 ** Erase column-cache entry number i 3176 */ 3177 static void cacheEntryClear(Parse *pParse, int i){ 3178 if( pParse->aColCache[i].tempReg ){ 3179 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 3180 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg; 3181 } 3182 } 3183 pParse->nColCache--; 3184 if( i<pParse->nColCache ){ 3185 pParse->aColCache[i] = pParse->aColCache[pParse->nColCache]; 3186 } 3187 } 3188 3189 3190 /* 3191 ** Record in the column cache that a particular column from a 3192 ** particular table is stored in a particular register. 3193 */ 3194 void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ 3195 int i; 3196 int minLru; 3197 int idxLru; 3198 struct yColCache *p; 3199 3200 /* Unless an error has occurred, register numbers are always positive. */ 3201 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); 3202 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ 3203 3204 /* The SQLITE_ColumnCache flag disables the column cache. This is used 3205 ** for testing only - to verify that SQLite always gets the same answer 3206 ** with and without the column cache. 3207 */ 3208 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; 3209 3210 /* First replace any existing entry. 3211 ** 3212 ** Actually, the way the column cache is currently used, we are guaranteed 3213 ** that the object will never already be in cache. Verify this guarantee. 3214 */ 3215 #ifndef NDEBUG 3216 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ 3217 assert( p->iTable!=iTab || p->iColumn!=iCol ); 3218 } 3219 #endif 3220 3221 #ifdef SQLITE_DEBUG_COLUMNCACHE 3222 /* Add a SetTabCol opcode for run-time verification that the column 3223 ** cache is working correctly. 3224 */ 3225 sqlite3VdbeAddOp3(pParse->pVdbe, OP_SetTabCol, iTab, iCol, iReg); 3226 #endif 3227 3228 /* If the cache is already full, delete the least recently used entry */ 3229 if( pParse->nColCache>=SQLITE_N_COLCACHE ){ 3230 minLru = 0x7fffffff; 3231 idxLru = -1; 3232 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 3233 if( p->lru<minLru ){ 3234 idxLru = i; 3235 minLru = p->lru; 3236 } 3237 } 3238 p = &pParse->aColCache[idxLru]; 3239 }else{ 3240 p = &pParse->aColCache[pParse->nColCache++]; 3241 } 3242 3243 /* Add the new entry to the end of the cache */ 3244 p->iLevel = pParse->iCacheLevel; 3245 p->iTable = iTab; 3246 p->iColumn = iCol; 3247 p->iReg = iReg; 3248 p->tempReg = 0; 3249 p->lru = pParse->iCacheCnt++; 3250 } 3251 3252 /* 3253 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. 3254 ** Purge the range of registers from the column cache. 3255 */ 3256 void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ 3257 int i = 0; 3258 while( i<pParse->nColCache ){ 3259 struct yColCache *p = &pParse->aColCache[i]; 3260 if( p->iReg >= iReg && p->iReg < iReg+nReg ){ 3261 cacheEntryClear(pParse, i); 3262 }else{ 3263 i++; 3264 } 3265 } 3266 } 3267 3268 /* 3269 ** Remember the current column cache context. Any new entries added 3270 ** added to the column cache after this call are removed when the 3271 ** corresponding pop occurs. 3272 */ 3273 void sqlite3ExprCachePush(Parse *pParse){ 3274 pParse->iCacheLevel++; 3275 #ifdef SQLITE_DEBUG 3276 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 3277 printf("PUSH to %d\n", pParse->iCacheLevel); 3278 } 3279 #endif 3280 } 3281 3282 /* 3283 ** Remove from the column cache any entries that were added since the 3284 ** the previous sqlite3ExprCachePush operation. In other words, restore 3285 ** the cache to the state it was in prior the most recent Push. 3286 */ 3287 void sqlite3ExprCachePop(Parse *pParse){ 3288 int i = 0; 3289 assert( pParse->iCacheLevel>=1 ); 3290 pParse->iCacheLevel--; 3291 #ifdef SQLITE_DEBUG 3292 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 3293 printf("POP to %d\n", pParse->iCacheLevel); 3294 } 3295 #endif 3296 while( i<pParse->nColCache ){ 3297 if( pParse->aColCache[i].iLevel>pParse->iCacheLevel ){ 3298 cacheEntryClear(pParse, i); 3299 }else{ 3300 i++; 3301 } 3302 } 3303 } 3304 3305 /* 3306 ** When a cached column is reused, make sure that its register is 3307 ** no longer available as a temp register. ticket #3879: that same 3308 ** register might be in the cache in multiple places, so be sure to 3309 ** get them all. 3310 */ 3311 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ 3312 int i; 3313 struct yColCache *p; 3314 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ 3315 if( p->iReg==iReg ){ 3316 p->tempReg = 0; 3317 } 3318 } 3319 } 3320 3321 /* Generate code that will load into register regOut a value that is 3322 ** appropriate for the iIdxCol-th column of index pIdx. 3323 */ 3324 void sqlite3ExprCodeLoadIndexColumn( 3325 Parse *pParse, /* The parsing context */ 3326 Index *pIdx, /* The index whose column is to be loaded */ 3327 int iTabCur, /* Cursor pointing to a table row */ 3328 int iIdxCol, /* The column of the index to be loaded */ 3329 int regOut /* Store the index column value in this register */ 3330 ){ 3331 i16 iTabCol = pIdx->aiColumn[iIdxCol]; 3332 if( iTabCol==XN_EXPR ){ 3333 assert( pIdx->aColExpr ); 3334 assert( pIdx->aColExpr->nExpr>iIdxCol ); 3335 pParse->iSelfTab = iTabCur + 1; 3336 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); 3337 pParse->iSelfTab = 0; 3338 }else{ 3339 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, 3340 iTabCol, regOut); 3341 } 3342 } 3343 3344 /* 3345 ** Generate code to extract the value of the iCol-th column of a table. 3346 */ 3347 void sqlite3ExprCodeGetColumnOfTable( 3348 Vdbe *v, /* The VDBE under construction */ 3349 Table *pTab, /* The table containing the value */ 3350 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 3351 int iCol, /* Index of the column to extract */ 3352 int regOut /* Extract the value into this register */ 3353 ){ 3354 if( pTab==0 ){ 3355 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut); 3356 return; 3357 } 3358 if( iCol<0 || iCol==pTab->iPKey ){ 3359 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 3360 }else{ 3361 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 3362 int x = iCol; 3363 if( !HasRowid(pTab) && !IsVirtual(pTab) ){ 3364 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 3365 } 3366 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 3367 } 3368 if( iCol>=0 ){ 3369 sqlite3ColumnDefault(v, pTab, iCol, regOut); 3370 } 3371 } 3372 3373 /* 3374 ** Generate code that will extract the iColumn-th column from 3375 ** table pTab and store the column value in a register. 3376 ** 3377 ** An effort is made to store the column value in register iReg. This 3378 ** is not garanteeed for GetColumn() - the result can be stored in 3379 ** any register. But the result is guaranteed to land in register iReg 3380 ** for GetColumnToReg(). 3381 ** 3382 ** There must be an open cursor to pTab in iTable when this routine 3383 ** is called. If iColumn<0 then code is generated that extracts the rowid. 3384 */ 3385 int sqlite3ExprCodeGetColumn( 3386 Parse *pParse, /* Parsing and code generating context */ 3387 Table *pTab, /* Description of the table we are reading from */ 3388 int iColumn, /* Index of the table column */ 3389 int iTable, /* The cursor pointing to the table */ 3390 int iReg, /* Store results here */ 3391 u8 p5 /* P5 value for OP_Column + FLAGS */ 3392 ){ 3393 Vdbe *v = pParse->pVdbe; 3394 int i; 3395 struct yColCache *p; 3396 3397 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ 3398 if( p->iTable==iTable && p->iColumn==iColumn ){ 3399 p->lru = pParse->iCacheCnt++; 3400 sqlite3ExprCachePinRegister(pParse, p->iReg); 3401 #ifdef SQLITE_DEBUG_COLUMNCACHE 3402 sqlite3VdbeAddOp3(v, OP_VerifyTabCol, iTable, iColumn, p->iReg); 3403 #endif 3404 return p->iReg; 3405 } 3406 } 3407 assert( v!=0 ); 3408 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); 3409 if( p5 ){ 3410 sqlite3VdbeChangeP5(v, p5); 3411 }else{ 3412 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); 3413 } 3414 return iReg; 3415 } 3416 void sqlite3ExprCodeGetColumnToReg( 3417 Parse *pParse, /* Parsing and code generating context */ 3418 Table *pTab, /* Description of the table we are reading from */ 3419 int iColumn, /* Index of the table column */ 3420 int iTable, /* The cursor pointing to the table */ 3421 int iReg /* Store results here */ 3422 ){ 3423 int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); 3424 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); 3425 } 3426 3427 3428 /* 3429 ** Clear all column cache entries. 3430 */ 3431 void sqlite3ExprCacheClear(Parse *pParse){ 3432 int i; 3433 3434 #ifdef SQLITE_DEBUG 3435 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 3436 printf("CLEAR\n"); 3437 } 3438 #endif 3439 for(i=0; i<pParse->nColCache; i++){ 3440 if( pParse->aColCache[i].tempReg 3441 && pParse->nTempReg<ArraySize(pParse->aTempReg) 3442 ){ 3443 pParse->aTempReg[pParse->nTempReg++] = pParse->aColCache[i].iReg; 3444 } 3445 } 3446 pParse->nColCache = 0; 3447 } 3448 3449 /* 3450 ** Record the fact that an affinity change has occurred on iCount 3451 ** registers starting with iStart. 3452 */ 3453 void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 3454 sqlite3ExprCacheRemove(pParse, iStart, iCount); 3455 } 3456 3457 /* 3458 ** Generate code to move content from registers iFrom...iFrom+nReg-1 3459 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. 3460 */ 3461 void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ 3462 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); 3463 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); 3464 sqlite3ExprCacheRemove(pParse, iFrom, nReg); 3465 } 3466 3467 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) 3468 /* 3469 ** Return true if any register in the range iFrom..iTo (inclusive) 3470 ** is used as part of the column cache. 3471 ** 3472 ** This routine is used within assert() and testcase() macros only 3473 ** and does not appear in a normal build. 3474 */ 3475 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 3476 int i; 3477 struct yColCache *p; 3478 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ 3479 int r = p->iReg; 3480 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ 3481 } 3482 return 0; 3483 } 3484 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ 3485 3486 3487 /* 3488 ** Convert a scalar expression node to a TK_REGISTER referencing 3489 ** register iReg. The caller must ensure that iReg already contains 3490 ** the correct value for the expression. 3491 */ 3492 static void exprToRegister(Expr *p, int iReg){ 3493 p->op2 = p->op; 3494 p->op = TK_REGISTER; 3495 p->iTable = iReg; 3496 ExprClearProperty(p, EP_Skip); 3497 } 3498 3499 /* 3500 ** Evaluate an expression (either a vector or a scalar expression) and store 3501 ** the result in continguous temporary registers. Return the index of 3502 ** the first register used to store the result. 3503 ** 3504 ** If the returned result register is a temporary scalar, then also write 3505 ** that register number into *piFreeable. If the returned result register 3506 ** is not a temporary or if the expression is a vector set *piFreeable 3507 ** to 0. 3508 */ 3509 static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){ 3510 int iResult; 3511 int nResult = sqlite3ExprVectorSize(p); 3512 if( nResult==1 ){ 3513 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable); 3514 }else{ 3515 *piFreeable = 0; 3516 if( p->op==TK_SELECT ){ 3517 #if SQLITE_OMIT_SUBQUERY 3518 iResult = 0; 3519 #else 3520 iResult = sqlite3CodeSubselect(pParse, p, 0, 0); 3521 #endif 3522 }else{ 3523 int i; 3524 iResult = pParse->nMem+1; 3525 pParse->nMem += nResult; 3526 for(i=0; i<nResult; i++){ 3527 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult); 3528 } 3529 } 3530 } 3531 return iResult; 3532 } 3533 3534 3535 /* 3536 ** Generate code into the current Vdbe to evaluate the given 3537 ** expression. Attempt to store the results in register "target". 3538 ** Return the register where results are stored. 3539 ** 3540 ** With this routine, there is no guarantee that results will 3541 ** be stored in target. The result might be stored in some other 3542 ** register if it is convenient to do so. The calling function 3543 ** must check the return code and move the results to the desired 3544 ** register. 3545 */ 3546 int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 3547 Vdbe *v = pParse->pVdbe; /* The VM under construction */ 3548 int op; /* The opcode being coded */ 3549 int inReg = target; /* Results stored in register inReg */ 3550 int regFree1 = 0; /* If non-zero free this temporary register */ 3551 int regFree2 = 0; /* If non-zero free this temporary register */ 3552 int r1, r2; /* Various register numbers */ 3553 Expr tempX; /* Temporary expression node */ 3554 int p5 = 0; 3555 3556 assert( target>0 && target<=pParse->nMem ); 3557 if( v==0 ){ 3558 assert( pParse->db->mallocFailed ); 3559 return 0; 3560 } 3561 3562 expr_code_doover: 3563 if( pExpr==0 ){ 3564 op = TK_NULL; 3565 }else{ 3566 op = pExpr->op; 3567 } 3568 switch( op ){ 3569 case TK_AGG_COLUMN: { 3570 AggInfo *pAggInfo = pExpr->pAggInfo; 3571 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 3572 if( !pAggInfo->directMode ){ 3573 assert( pCol->iMem>0 ); 3574 return pCol->iMem; 3575 }else if( pAggInfo->useSortingIdx ){ 3576 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 3577 pCol->iSorterColumn, target); 3578 return target; 3579 } 3580 /* Otherwise, fall thru into the TK_COLUMN case */ 3581 } 3582 case TK_COLUMN: { 3583 int iTab = pExpr->iTable; 3584 if( iTab<0 ){ 3585 if( pParse->iSelfTab<0 ){ 3586 /* Generating CHECK constraints or inserting into partial index */ 3587 return pExpr->iColumn - pParse->iSelfTab; 3588 }else{ 3589 /* Coding an expression that is part of an index where column names 3590 ** in the index refer to the table to which the index belongs */ 3591 iTab = pParse->iSelfTab - 1; 3592 } 3593 } 3594 return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 3595 pExpr->iColumn, iTab, target, 3596 pExpr->op2); 3597 } 3598 case TK_INTEGER: { 3599 codeInteger(pParse, pExpr, 0, target); 3600 return target; 3601 } 3602 case TK_TRUEFALSE: { 3603 sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target); 3604 return target; 3605 } 3606 #ifndef SQLITE_OMIT_FLOATING_POINT 3607 case TK_FLOAT: { 3608 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3609 codeReal(v, pExpr->u.zToken, 0, target); 3610 return target; 3611 } 3612 #endif 3613 case TK_STRING: { 3614 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3615 sqlite3VdbeLoadString(v, target, pExpr->u.zToken); 3616 return target; 3617 } 3618 case TK_NULL: { 3619 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3620 return target; 3621 } 3622 #ifndef SQLITE_OMIT_BLOB_LITERAL 3623 case TK_BLOB: { 3624 int n; 3625 const char *z; 3626 char *zBlob; 3627 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3628 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 3629 assert( pExpr->u.zToken[1]=='\'' ); 3630 z = &pExpr->u.zToken[2]; 3631 n = sqlite3Strlen30(z) - 1; 3632 assert( z[n]=='\'' ); 3633 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 3634 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 3635 return target; 3636 } 3637 #endif 3638 case TK_VARIABLE: { 3639 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3640 assert( pExpr->u.zToken!=0 ); 3641 assert( pExpr->u.zToken[0]!=0 ); 3642 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); 3643 if( pExpr->u.zToken[1]!=0 ){ 3644 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn); 3645 assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 ); 3646 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */ 3647 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC); 3648 } 3649 return target; 3650 } 3651 case TK_REGISTER: { 3652 return pExpr->iTable; 3653 } 3654 #ifndef SQLITE_OMIT_CAST 3655 case TK_CAST: { 3656 /* Expressions of the form: CAST(pLeft AS token) */ 3657 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 3658 if( inReg!=target ){ 3659 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); 3660 inReg = target; 3661 } 3662 sqlite3VdbeAddOp2(v, OP_Cast, target, 3663 sqlite3AffinityType(pExpr->u.zToken, 0)); 3664 testcase( usedAsColumnCache(pParse, inReg, inReg) ); 3665 sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 3666 return inReg; 3667 } 3668 #endif /* SQLITE_OMIT_CAST */ 3669 case TK_IS: 3670 case TK_ISNOT: 3671 op = (op==TK_IS) ? TK_EQ : TK_NE; 3672 p5 = SQLITE_NULLEQ; 3673 /* fall-through */ 3674 case TK_LT: 3675 case TK_LE: 3676 case TK_GT: 3677 case TK_GE: 3678 case TK_NE: 3679 case TK_EQ: { 3680 Expr *pLeft = pExpr->pLeft; 3681 if( sqlite3ExprIsVector(pLeft) ){ 3682 codeVectorCompare(pParse, pExpr, target, op, p5); 3683 }else{ 3684 r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 3685 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 3686 codeCompare(pParse, pLeft, pExpr->pRight, op, 3687 r1, r2, inReg, SQLITE_STOREP2 | p5); 3688 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 3689 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 3690 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 3691 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 3692 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 3693 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 3694 testcase( regFree1==0 ); 3695 testcase( regFree2==0 ); 3696 } 3697 break; 3698 } 3699 case TK_AND: 3700 case TK_OR: 3701 case TK_PLUS: 3702 case TK_STAR: 3703 case TK_MINUS: 3704 case TK_REM: 3705 case TK_BITAND: 3706 case TK_BITOR: 3707 case TK_SLASH: 3708 case TK_LSHIFT: 3709 case TK_RSHIFT: 3710 case TK_CONCAT: { 3711 assert( TK_AND==OP_And ); testcase( op==TK_AND ); 3712 assert( TK_OR==OP_Or ); testcase( op==TK_OR ); 3713 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); 3714 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); 3715 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); 3716 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); 3717 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); 3718 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); 3719 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); 3720 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); 3721 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); 3722 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3723 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 3724 sqlite3VdbeAddOp3(v, op, r2, r1, target); 3725 testcase( regFree1==0 ); 3726 testcase( regFree2==0 ); 3727 break; 3728 } 3729 case TK_UMINUS: { 3730 Expr *pLeft = pExpr->pLeft; 3731 assert( pLeft ); 3732 if( pLeft->op==TK_INTEGER ){ 3733 codeInteger(pParse, pLeft, 1, target); 3734 return target; 3735 #ifndef SQLITE_OMIT_FLOATING_POINT 3736 }else if( pLeft->op==TK_FLOAT ){ 3737 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3738 codeReal(v, pLeft->u.zToken, 1, target); 3739 return target; 3740 #endif 3741 }else{ 3742 tempX.op = TK_INTEGER; 3743 tempX.flags = EP_IntValue|EP_TokenOnly; 3744 tempX.u.iValue = 0; 3745 r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); 3746 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 3747 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 3748 testcase( regFree2==0 ); 3749 } 3750 break; 3751 } 3752 case TK_BITNOT: 3753 case TK_NOT: { 3754 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); 3755 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); 3756 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3757 testcase( regFree1==0 ); 3758 sqlite3VdbeAddOp2(v, op, r1, inReg); 3759 break; 3760 } 3761 case TK_TRUTH: { 3762 int isTrue; /* IS TRUE or IS NOT TRUE */ 3763 int bNormal; /* IS TRUE or IS FALSE */ 3764 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3765 testcase( regFree1==0 ); 3766 isTrue = sqlite3ExprTruthValue(pExpr->pRight); 3767 bNormal = pExpr->op2==TK_IS; 3768 testcase( isTrue && bNormal); 3769 testcase( !isTrue && bNormal); 3770 sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal); 3771 break; 3772 } 3773 case TK_ISNULL: 3774 case TK_NOTNULL: { 3775 int addr; 3776 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 3777 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 3778 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 3779 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3780 testcase( regFree1==0 ); 3781 addr = sqlite3VdbeAddOp1(v, op, r1); 3782 VdbeCoverageIf(v, op==TK_ISNULL); 3783 VdbeCoverageIf(v, op==TK_NOTNULL); 3784 sqlite3VdbeAddOp2(v, OP_Integer, 0, target); 3785 sqlite3VdbeJumpHere(v, addr); 3786 break; 3787 } 3788 case TK_AGG_FUNCTION: { 3789 AggInfo *pInfo = pExpr->pAggInfo; 3790 if( pInfo==0 ){ 3791 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3792 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); 3793 }else{ 3794 return pInfo->aFunc[pExpr->iAgg].iMem; 3795 } 3796 break; 3797 } 3798 case TK_FUNCTION: { 3799 ExprList *pFarg; /* List of function arguments */ 3800 int nFarg; /* Number of function arguments */ 3801 FuncDef *pDef; /* The function definition object */ 3802 const char *zId; /* The function name */ 3803 u32 constMask = 0; /* Mask of function arguments that are constant */ 3804 int i; /* Loop counter */ 3805 sqlite3 *db = pParse->db; /* The database connection */ 3806 u8 enc = ENC(db); /* The text encoding used by this database */ 3807 CollSeq *pColl = 0; /* A collating sequence */ 3808 3809 #ifndef SQLITE_OMIT_WINDOWFUNC 3810 if( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) && pExpr->pWin ){ 3811 return pExpr->pWin->regResult; 3812 } 3813 #endif 3814 3815 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){ 3816 /* SQL functions can be expensive. So try to move constant functions 3817 ** out of the inner loop, even if that means an extra OP_Copy. */ 3818 return sqlite3ExprCodeAtInit(pParse, pExpr, -1); 3819 } 3820 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 3821 if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 3822 pFarg = 0; 3823 }else{ 3824 pFarg = pExpr->x.pList; 3825 } 3826 nFarg = pFarg ? pFarg->nExpr : 0; 3827 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3828 zId = pExpr->u.zToken; 3829 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); 3830 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 3831 if( pDef==0 && pParse->explain ){ 3832 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0); 3833 } 3834 #endif 3835 if( pDef==0 || pDef->xFinalize!=0 ){ 3836 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); 3837 break; 3838 } 3839 3840 /* Attempt a direct implementation of the built-in COALESCE() and 3841 ** IFNULL() functions. This avoids unnecessary evaluation of 3842 ** arguments past the first non-NULL argument. 3843 */ 3844 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ 3845 int endCoalesce = sqlite3VdbeMakeLabel(v); 3846 assert( nFarg>=2 ); 3847 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 3848 for(i=1; i<nFarg; i++){ 3849 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); 3850 VdbeCoverage(v); 3851 sqlite3ExprCacheRemove(pParse, target, 1); 3852 sqlite3ExprCachePush(pParse); 3853 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); 3854 sqlite3ExprCachePop(pParse); 3855 } 3856 sqlite3VdbeResolveLabel(v, endCoalesce); 3857 break; 3858 } 3859 3860 /* The UNLIKELY() function is a no-op. The result is the value 3861 ** of the first argument. 3862 */ 3863 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 3864 assert( nFarg>=1 ); 3865 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); 3866 } 3867 3868 #ifdef SQLITE_DEBUG 3869 /* The AFFINITY() function evaluates to a string that describes 3870 ** the type affinity of the argument. This is used for testing of 3871 ** the SQLite type logic. 3872 */ 3873 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){ 3874 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" }; 3875 char aff; 3876 assert( nFarg==1 ); 3877 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr); 3878 sqlite3VdbeLoadString(v, target, 3879 aff ? azAff[aff-SQLITE_AFF_BLOB] : "none"); 3880 return target; 3881 } 3882 #endif 3883 3884 for(i=0; i<nFarg; i++){ 3885 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ 3886 testcase( i==31 ); 3887 constMask |= MASKBIT32(i); 3888 } 3889 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 3890 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 3891 } 3892 } 3893 if( pFarg ){ 3894 if( constMask ){ 3895 r1 = pParse->nMem+1; 3896 pParse->nMem += nFarg; 3897 }else{ 3898 r1 = sqlite3GetTempRange(pParse, nFarg); 3899 } 3900 3901 /* For length() and typeof() functions with a column argument, 3902 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG 3903 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data 3904 ** loading. 3905 */ 3906 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ 3907 u8 exprOp; 3908 assert( nFarg==1 ); 3909 assert( pFarg->a[0].pExpr!=0 ); 3910 exprOp = pFarg->a[0].pExpr->op; 3911 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 3912 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 3913 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 3914 testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); 3915 pFarg->a[0].pExpr->op2 = 3916 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); 3917 } 3918 } 3919 3920 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ 3921 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, 3922 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); 3923 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ 3924 }else{ 3925 r1 = 0; 3926 } 3927 #ifndef SQLITE_OMIT_VIRTUALTABLE 3928 /* Possibly overload the function if the first argument is 3929 ** a virtual table column. 3930 ** 3931 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 3932 ** second argument, not the first, as the argument to test to 3933 ** see if it is a column in a virtual table. This is done because 3934 ** the left operand of infix functions (the operand we want to 3935 ** control overloading) ends up as the second argument to the 3936 ** function. The expression "A glob B" is equivalent to 3937 ** "glob(B,A). We want to use the A in "A glob B" to test 3938 ** for function overloading. But we use the B term in "glob(B,A)". 3939 */ 3940 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ 3941 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 3942 }else if( nFarg>0 ){ 3943 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 3944 } 3945 #endif 3946 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 3947 if( !pColl ) pColl = db->pDfltColl; 3948 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 3949 } 3950 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 3951 if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){ 3952 Expr *pArg = pFarg->a[0].pExpr; 3953 if( pArg->op==TK_COLUMN ){ 3954 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target); 3955 }else{ 3956 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3957 } 3958 }else 3959 #endif 3960 { 3961 sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0, 3962 constMask, r1, target, (char*)pDef, P4_FUNCDEF); 3963 sqlite3VdbeChangeP5(v, (u8)nFarg); 3964 } 3965 if( nFarg && constMask==0 ){ 3966 sqlite3ReleaseTempRange(pParse, r1, nFarg); 3967 } 3968 return target; 3969 } 3970 #ifndef SQLITE_OMIT_SUBQUERY 3971 case TK_EXISTS: 3972 case TK_SELECT: { 3973 int nCol; 3974 testcase( op==TK_EXISTS ); 3975 testcase( op==TK_SELECT ); 3976 if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){ 3977 sqlite3SubselectError(pParse, nCol, 1); 3978 }else{ 3979 return sqlite3CodeSubselect(pParse, pExpr, 0, 0); 3980 } 3981 break; 3982 } 3983 case TK_SELECT_COLUMN: { 3984 int n; 3985 if( pExpr->pLeft->iTable==0 ){ 3986 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft, 0, 0); 3987 } 3988 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT ); 3989 if( pExpr->iTable 3990 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft)) 3991 ){ 3992 sqlite3ErrorMsg(pParse, "%d columns assigned %d values", 3993 pExpr->iTable, n); 3994 } 3995 return pExpr->pLeft->iTable + pExpr->iColumn; 3996 } 3997 case TK_IN: { 3998 int destIfFalse = sqlite3VdbeMakeLabel(v); 3999 int destIfNull = sqlite3VdbeMakeLabel(v); 4000 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 4001 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 4002 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 4003 sqlite3VdbeResolveLabel(v, destIfFalse); 4004 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); 4005 sqlite3VdbeResolveLabel(v, destIfNull); 4006 return target; 4007 } 4008 #endif /* SQLITE_OMIT_SUBQUERY */ 4009 4010 4011 /* 4012 ** x BETWEEN y AND z 4013 ** 4014 ** This is equivalent to 4015 ** 4016 ** x>=y AND x<=z 4017 ** 4018 ** X is stored in pExpr->pLeft. 4019 ** Y is stored in pExpr->pList->a[0].pExpr. 4020 ** Z is stored in pExpr->pList->a[1].pExpr. 4021 */ 4022 case TK_BETWEEN: { 4023 exprCodeBetween(pParse, pExpr, target, 0, 0); 4024 return target; 4025 } 4026 case TK_SPAN: 4027 case TK_COLLATE: 4028 case TK_UPLUS: { 4029 pExpr = pExpr->pLeft; 4030 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */ 4031 } 4032 4033 case TK_TRIGGER: { 4034 /* If the opcode is TK_TRIGGER, then the expression is a reference 4035 ** to a column in the new.* or old.* pseudo-tables available to 4036 ** trigger programs. In this case Expr.iTable is set to 1 for the 4037 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 4038 ** is set to the column of the pseudo-table to read, or to -1 to 4039 ** read the rowid field. 4040 ** 4041 ** The expression is implemented using an OP_Param opcode. The p1 4042 ** parameter is set to 0 for an old.rowid reference, or to (i+1) 4043 ** to reference another column of the old.* pseudo-table, where 4044 ** i is the index of the column. For a new.rowid reference, p1 is 4045 ** set to (n+1), where n is the number of columns in each pseudo-table. 4046 ** For a reference to any other column in the new.* pseudo-table, p1 4047 ** is set to (n+2+i), where n and i are as defined previously. For 4048 ** example, if the table on which triggers are being fired is 4049 ** declared as: 4050 ** 4051 ** CREATE TABLE t1(a, b); 4052 ** 4053 ** Then p1 is interpreted as follows: 4054 ** 4055 ** p1==0 -> old.rowid p1==3 -> new.rowid 4056 ** p1==1 -> old.a p1==4 -> new.a 4057 ** p1==2 -> old.b p1==5 -> new.b 4058 */ 4059 Table *pTab = pExpr->pTab; 4060 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; 4061 4062 assert( pExpr->iTable==0 || pExpr->iTable==1 ); 4063 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); 4064 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); 4065 assert( p1>=0 && p1<(pTab->nCol*2+2) ); 4066 4067 sqlite3VdbeAddOp2(v, OP_Param, p1, target); 4068 VdbeComment((v, "r[%d]=%s.%s", target, 4069 (pExpr->iTable ? "new" : "old"), 4070 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName) 4071 )); 4072 4073 #ifndef SQLITE_OMIT_FLOATING_POINT 4074 /* If the column has REAL affinity, it may currently be stored as an 4075 ** integer. Use OP_RealAffinity to make sure it is really real. 4076 ** 4077 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to 4078 ** floating point when extracting it from the record. */ 4079 if( pExpr->iColumn>=0 4080 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL 4081 ){ 4082 sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 4083 } 4084 #endif 4085 break; 4086 } 4087 4088 case TK_VECTOR: { 4089 sqlite3ErrorMsg(pParse, "row value misused"); 4090 break; 4091 } 4092 4093 case TK_IF_NULL_ROW: { 4094 int addrINR; 4095 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable); 4096 sqlite3ExprCachePush(pParse); 4097 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 4098 sqlite3ExprCachePop(pParse); 4099 sqlite3VdbeJumpHere(v, addrINR); 4100 sqlite3VdbeChangeP3(v, addrINR, inReg); 4101 break; 4102 } 4103 4104 /* 4105 ** Form A: 4106 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 4107 ** 4108 ** Form B: 4109 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 4110 ** 4111 ** Form A is can be transformed into the equivalent form B as follows: 4112 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 4113 ** WHEN x=eN THEN rN ELSE y END 4114 ** 4115 ** X (if it exists) is in pExpr->pLeft. 4116 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is 4117 ** odd. The Y is also optional. If the number of elements in x.pList 4118 ** is even, then Y is omitted and the "otherwise" result is NULL. 4119 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 4120 ** 4121 ** The result of the expression is the Ri for the first matching Ei, 4122 ** or if there is no matching Ei, the ELSE term Y, or if there is 4123 ** no ELSE term, NULL. 4124 */ 4125 default: assert( op==TK_CASE ); { 4126 int endLabel; /* GOTO label for end of CASE stmt */ 4127 int nextCase; /* GOTO label for next WHEN clause */ 4128 int nExpr; /* 2x number of WHEN terms */ 4129 int i; /* Loop counter */ 4130 ExprList *pEList; /* List of WHEN terms */ 4131 struct ExprList_item *aListelem; /* Array of WHEN terms */ 4132 Expr opCompare; /* The X==Ei expression */ 4133 Expr *pX; /* The X expression */ 4134 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ 4135 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) 4136 4137 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); 4138 assert(pExpr->x.pList->nExpr > 0); 4139 pEList = pExpr->x.pList; 4140 aListelem = pEList->a; 4141 nExpr = pEList->nExpr; 4142 endLabel = sqlite3VdbeMakeLabel(v); 4143 if( (pX = pExpr->pLeft)!=0 ){ 4144 tempX = *pX; 4145 testcase( pX->op==TK_COLUMN ); 4146 exprToRegister(&tempX, exprCodeVector(pParse, &tempX, ®Free1)); 4147 testcase( regFree1==0 ); 4148 memset(&opCompare, 0, sizeof(opCompare)); 4149 opCompare.op = TK_EQ; 4150 opCompare.pLeft = &tempX; 4151 pTest = &opCompare; 4152 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: 4153 ** The value in regFree1 might get SCopy-ed into the file result. 4154 ** So make sure that the regFree1 register is not reused for other 4155 ** purposes and possibly overwritten. */ 4156 regFree1 = 0; 4157 } 4158 for(i=0; i<nExpr-1; i=i+2){ 4159 sqlite3ExprCachePush(pParse); 4160 if( pX ){ 4161 assert( pTest!=0 ); 4162 opCompare.pRight = aListelem[i].pExpr; 4163 }else{ 4164 pTest = aListelem[i].pExpr; 4165 } 4166 nextCase = sqlite3VdbeMakeLabel(v); 4167 testcase( pTest->op==TK_COLUMN ); 4168 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 4169 testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 4170 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 4171 sqlite3VdbeGoto(v, endLabel); 4172 sqlite3ExprCachePop(pParse); 4173 sqlite3VdbeResolveLabel(v, nextCase); 4174 } 4175 if( (nExpr&1)!=0 ){ 4176 sqlite3ExprCachePush(pParse); 4177 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 4178 sqlite3ExprCachePop(pParse); 4179 }else{ 4180 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 4181 } 4182 assert( pParse->db->mallocFailed || pParse->nErr>0 4183 || pParse->iCacheLevel==iCacheLevel ); 4184 sqlite3VdbeResolveLabel(v, endLabel); 4185 break; 4186 } 4187 #ifndef SQLITE_OMIT_TRIGGER 4188 case TK_RAISE: { 4189 assert( pExpr->affinity==OE_Rollback 4190 || pExpr->affinity==OE_Abort 4191 || pExpr->affinity==OE_Fail 4192 || pExpr->affinity==OE_Ignore 4193 ); 4194 if( !pParse->pTriggerTab ){ 4195 sqlite3ErrorMsg(pParse, 4196 "RAISE() may only be used within a trigger-program"); 4197 return 0; 4198 } 4199 if( pExpr->affinity==OE_Abort ){ 4200 sqlite3MayAbort(pParse); 4201 } 4202 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 4203 if( pExpr->affinity==OE_Ignore ){ 4204 sqlite3VdbeAddOp4( 4205 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); 4206 VdbeCoverage(v); 4207 }else{ 4208 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, 4209 pExpr->affinity, pExpr->u.zToken, 0, 0); 4210 } 4211 4212 break; 4213 } 4214 #endif 4215 } 4216 sqlite3ReleaseTempReg(pParse, regFree1); 4217 sqlite3ReleaseTempReg(pParse, regFree2); 4218 return inReg; 4219 } 4220 4221 /* 4222 ** Factor out the code of the given expression to initialization time. 4223 ** 4224 ** If regDest>=0 then the result is always stored in that register and the 4225 ** result is not reusable. If regDest<0 then this routine is free to 4226 ** store the value whereever it wants. The register where the expression 4227 ** is stored is returned. When regDest<0, two identical expressions will 4228 ** code to the same register. 4229 */ 4230 int sqlite3ExprCodeAtInit( 4231 Parse *pParse, /* Parsing context */ 4232 Expr *pExpr, /* The expression to code when the VDBE initializes */ 4233 int regDest /* Store the value in this register */ 4234 ){ 4235 ExprList *p; 4236 assert( ConstFactorOk(pParse) ); 4237 p = pParse->pConstExpr; 4238 if( regDest<0 && p ){ 4239 struct ExprList_item *pItem; 4240 int i; 4241 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ 4242 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){ 4243 return pItem->u.iConstExprReg; 4244 } 4245 } 4246 } 4247 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); 4248 p = sqlite3ExprListAppend(pParse, p, pExpr); 4249 if( p ){ 4250 struct ExprList_item *pItem = &p->a[p->nExpr-1]; 4251 pItem->reusable = regDest<0; 4252 if( regDest<0 ) regDest = ++pParse->nMem; 4253 pItem->u.iConstExprReg = regDest; 4254 } 4255 pParse->pConstExpr = p; 4256 return regDest; 4257 } 4258 4259 /* 4260 ** Generate code to evaluate an expression and store the results 4261 ** into a register. Return the register number where the results 4262 ** are stored. 4263 ** 4264 ** If the register is a temporary register that can be deallocated, 4265 ** then write its number into *pReg. If the result register is not 4266 ** a temporary, then set *pReg to zero. 4267 ** 4268 ** If pExpr is a constant, then this routine might generate this 4269 ** code to fill the register in the initialization section of the 4270 ** VDBE program, in order to factor it out of the evaluation loop. 4271 */ 4272 int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 4273 int r2; 4274 pExpr = sqlite3ExprSkipCollate(pExpr); 4275 if( ConstFactorOk(pParse) 4276 && pExpr->op!=TK_REGISTER 4277 && sqlite3ExprIsConstantNotJoin(pExpr) 4278 ){ 4279 *pReg = 0; 4280 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1); 4281 }else{ 4282 int r1 = sqlite3GetTempReg(pParse); 4283 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 4284 if( r2==r1 ){ 4285 *pReg = r1; 4286 }else{ 4287 sqlite3ReleaseTempReg(pParse, r1); 4288 *pReg = 0; 4289 } 4290 } 4291 return r2; 4292 } 4293 4294 /* 4295 ** Generate code that will evaluate expression pExpr and store the 4296 ** results in register target. The results are guaranteed to appear 4297 ** in register target. 4298 */ 4299 void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 4300 int inReg; 4301 4302 assert( target>0 && target<=pParse->nMem ); 4303 if( pExpr && pExpr->op==TK_REGISTER ){ 4304 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); 4305 }else{ 4306 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 4307 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); 4308 if( inReg!=target && pParse->pVdbe ){ 4309 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 4310 } 4311 } 4312 } 4313 4314 /* 4315 ** Make a transient copy of expression pExpr and then code it using 4316 ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() 4317 ** except that the input expression is guaranteed to be unchanged. 4318 */ 4319 void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ 4320 sqlite3 *db = pParse->db; 4321 pExpr = sqlite3ExprDup(db, pExpr, 0); 4322 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); 4323 sqlite3ExprDelete(db, pExpr); 4324 } 4325 4326 /* 4327 ** Generate code that will evaluate expression pExpr and store the 4328 ** results in register target. The results are guaranteed to appear 4329 ** in register target. If the expression is constant, then this routine 4330 ** might choose to code the expression at initialization time. 4331 */ 4332 void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 4333 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ 4334 sqlite3ExprCodeAtInit(pParse, pExpr, target); 4335 }else{ 4336 sqlite3ExprCode(pParse, pExpr, target); 4337 } 4338 } 4339 4340 /* 4341 ** Generate code that evaluates the given expression and puts the result 4342 ** in register target. 4343 ** 4344 ** Also make a copy of the expression results into another "cache" register 4345 ** and modify the expression so that the next time it is evaluated, 4346 ** the result is a copy of the cache register. 4347 ** 4348 ** This routine is used for expressions that are used multiple 4349 ** times. They are evaluated once and the results of the expression 4350 ** are reused. 4351 */ 4352 void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 4353 Vdbe *v = pParse->pVdbe; 4354 int iMem; 4355 4356 assert( target>0 ); 4357 assert( pExpr->op!=TK_REGISTER ); 4358 sqlite3ExprCode(pParse, pExpr, target); 4359 iMem = ++pParse->nMem; 4360 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); 4361 exprToRegister(pExpr, iMem); 4362 } 4363 4364 /* 4365 ** Generate code that pushes the value of every element of the given 4366 ** expression list into a sequence of registers beginning at target. 4367 ** 4368 ** Return the number of elements evaluated. The number returned will 4369 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF 4370 ** is defined. 4371 ** 4372 ** The SQLITE_ECEL_DUP flag prevents the arguments from being 4373 ** filled using OP_SCopy. OP_Copy must be used instead. 4374 ** 4375 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 4376 ** factored out into initialization code. 4377 ** 4378 ** The SQLITE_ECEL_REF flag means that expressions in the list with 4379 ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored 4380 ** in registers at srcReg, and so the value can be copied from there. 4381 ** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0 4382 ** are simply omitted rather than being copied from srcReg. 4383 */ 4384 int sqlite3ExprCodeExprList( 4385 Parse *pParse, /* Parsing context */ 4386 ExprList *pList, /* The expression list to be coded */ 4387 int target, /* Where to write results */ 4388 int srcReg, /* Source registers if SQLITE_ECEL_REF */ 4389 u8 flags /* SQLITE_ECEL_* flags */ 4390 ){ 4391 struct ExprList_item *pItem; 4392 int i, j, n; 4393 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 4394 Vdbe *v = pParse->pVdbe; 4395 assert( pList!=0 ); 4396 assert( target>0 ); 4397 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 4398 n = pList->nExpr; 4399 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 4400 for(pItem=pList->a, i=0; i<n; i++, pItem++){ 4401 Expr *pExpr = pItem->pExpr; 4402 #ifdef SQLITE_ENABLE_SORTER_REFERENCES 4403 if( pItem->bSorterRef ){ 4404 i--; 4405 n--; 4406 }else 4407 #endif 4408 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){ 4409 if( flags & SQLITE_ECEL_OMITREF ){ 4410 i--; 4411 n--; 4412 }else{ 4413 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); 4414 } 4415 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ 4416 sqlite3ExprCodeAtInit(pParse, pExpr, target+i); 4417 }else{ 4418 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 4419 if( inReg!=target+i ){ 4420 VdbeOp *pOp; 4421 if( copyOp==OP_Copy 4422 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy 4423 && pOp->p1+pOp->p3+1==inReg 4424 && pOp->p2+pOp->p3+1==target+i 4425 ){ 4426 pOp->p3++; 4427 }else{ 4428 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 4429 } 4430 } 4431 } 4432 } 4433 return n; 4434 } 4435 4436 /* 4437 ** Generate code for a BETWEEN operator. 4438 ** 4439 ** x BETWEEN y AND z 4440 ** 4441 ** The above is equivalent to 4442 ** 4443 ** x>=y AND x<=z 4444 ** 4445 ** Code it as such, taking care to do the common subexpression 4446 ** elimination of x. 4447 ** 4448 ** The xJumpIf parameter determines details: 4449 ** 4450 ** NULL: Store the boolean result in reg[dest] 4451 ** sqlite3ExprIfTrue: Jump to dest if true 4452 ** sqlite3ExprIfFalse: Jump to dest if false 4453 ** 4454 ** The jumpIfNull parameter is ignored if xJumpIf is NULL. 4455 */ 4456 static void exprCodeBetween( 4457 Parse *pParse, /* Parsing and code generating context */ 4458 Expr *pExpr, /* The BETWEEN expression */ 4459 int dest, /* Jump destination or storage location */ 4460 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */ 4461 int jumpIfNull /* Take the jump if the BETWEEN is NULL */ 4462 ){ 4463 Expr exprAnd; /* The AND operator in x>=y AND x<=z */ 4464 Expr compLeft; /* The x>=y term */ 4465 Expr compRight; /* The x<=z term */ 4466 Expr exprX; /* The x subexpression */ 4467 int regFree1 = 0; /* Temporary use register */ 4468 4469 4470 memset(&compLeft, 0, sizeof(Expr)); 4471 memset(&compRight, 0, sizeof(Expr)); 4472 memset(&exprAnd, 0, sizeof(Expr)); 4473 4474 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 4475 exprX = *pExpr->pLeft; 4476 exprAnd.op = TK_AND; 4477 exprAnd.pLeft = &compLeft; 4478 exprAnd.pRight = &compRight; 4479 compLeft.op = TK_GE; 4480 compLeft.pLeft = &exprX; 4481 compLeft.pRight = pExpr->x.pList->a[0].pExpr; 4482 compRight.op = TK_LE; 4483 compRight.pLeft = &exprX; 4484 compRight.pRight = pExpr->x.pList->a[1].pExpr; 4485 exprToRegister(&exprX, exprCodeVector(pParse, &exprX, ®Free1)); 4486 if( xJump ){ 4487 xJump(pParse, &exprAnd, dest, jumpIfNull); 4488 }else{ 4489 /* Mark the expression is being from the ON or USING clause of a join 4490 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move 4491 ** it into the Parse.pConstExpr list. We should use a new bit for this, 4492 ** for clarity, but we are out of bits in the Expr.flags field so we 4493 ** have to reuse the EP_FromJoin bit. Bummer. */ 4494 exprX.flags |= EP_FromJoin; 4495 sqlite3ExprCodeTarget(pParse, &exprAnd, dest); 4496 } 4497 sqlite3ReleaseTempReg(pParse, regFree1); 4498 4499 /* Ensure adequate test coverage */ 4500 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 ); 4501 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 ); 4502 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 ); 4503 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 ); 4504 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 ); 4505 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 ); 4506 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 ); 4507 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 ); 4508 testcase( xJump==0 ); 4509 } 4510 4511 /* 4512 ** Generate code for a boolean expression such that a jump is made 4513 ** to the label "dest" if the expression is true but execution 4514 ** continues straight thru if the expression is false. 4515 ** 4516 ** If the expression evaluates to NULL (neither true nor false), then 4517 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 4518 ** 4519 ** This code depends on the fact that certain token values (ex: TK_EQ) 4520 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 4521 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 4522 ** the make process cause these values to align. Assert()s in the code 4523 ** below verify that the numbers are aligned correctly. 4524 */ 4525 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 4526 Vdbe *v = pParse->pVdbe; 4527 int op = 0; 4528 int regFree1 = 0; 4529 int regFree2 = 0; 4530 int r1, r2; 4531 4532 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 4533 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 4534 if( NEVER(pExpr==0) ) return; /* No way this can happen */ 4535 op = pExpr->op; 4536 switch( op ){ 4537 case TK_AND: { 4538 int d2 = sqlite3VdbeMakeLabel(v); 4539 testcase( jumpIfNull==0 ); 4540 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 4541 sqlite3ExprCachePush(pParse); 4542 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 4543 sqlite3VdbeResolveLabel(v, d2); 4544 sqlite3ExprCachePop(pParse); 4545 break; 4546 } 4547 case TK_OR: { 4548 testcase( jumpIfNull==0 ); 4549 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 4550 sqlite3ExprCachePush(pParse); 4551 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 4552 sqlite3ExprCachePop(pParse); 4553 break; 4554 } 4555 case TK_NOT: { 4556 testcase( jumpIfNull==0 ); 4557 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 4558 break; 4559 } 4560 case TK_TRUTH: { 4561 int isNot; /* IS NOT TRUE or IS NOT FALSE */ 4562 int isTrue; /* IS TRUE or IS NOT TRUE */ 4563 testcase( jumpIfNull==0 ); 4564 isNot = pExpr->op2==TK_ISNOT; 4565 isTrue = sqlite3ExprTruthValue(pExpr->pRight); 4566 testcase( isTrue && isNot ); 4567 testcase( !isTrue && isNot ); 4568 if( isTrue ^ isNot ){ 4569 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, 4570 isNot ? SQLITE_JUMPIFNULL : 0); 4571 }else{ 4572 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, 4573 isNot ? SQLITE_JUMPIFNULL : 0); 4574 } 4575 break; 4576 } 4577 case TK_IS: 4578 case TK_ISNOT: 4579 testcase( op==TK_IS ); 4580 testcase( op==TK_ISNOT ); 4581 op = (op==TK_IS) ? TK_EQ : TK_NE; 4582 jumpIfNull = SQLITE_NULLEQ; 4583 /* Fall thru */ 4584 case TK_LT: 4585 case TK_LE: 4586 case TK_GT: 4587 case TK_GE: 4588 case TK_NE: 4589 case TK_EQ: { 4590 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; 4591 testcase( jumpIfNull==0 ); 4592 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 4593 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 4594 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 4595 r1, r2, dest, jumpIfNull); 4596 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 4597 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 4598 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 4599 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 4600 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 4601 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 4602 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 4603 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 4604 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 4605 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 4606 testcase( regFree1==0 ); 4607 testcase( regFree2==0 ); 4608 break; 4609 } 4610 case TK_ISNULL: 4611 case TK_NOTNULL: { 4612 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 4613 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 4614 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 4615 sqlite3VdbeAddOp2(v, op, r1, dest); 4616 VdbeCoverageIf(v, op==TK_ISNULL); 4617 VdbeCoverageIf(v, op==TK_NOTNULL); 4618 testcase( regFree1==0 ); 4619 break; 4620 } 4621 case TK_BETWEEN: { 4622 testcase( jumpIfNull==0 ); 4623 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); 4624 break; 4625 } 4626 #ifndef SQLITE_OMIT_SUBQUERY 4627 case TK_IN: { 4628 int destIfFalse = sqlite3VdbeMakeLabel(v); 4629 int destIfNull = jumpIfNull ? dest : destIfFalse; 4630 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 4631 sqlite3VdbeGoto(v, dest); 4632 sqlite3VdbeResolveLabel(v, destIfFalse); 4633 break; 4634 } 4635 #endif 4636 default: { 4637 default_expr: 4638 if( exprAlwaysTrue(pExpr) ){ 4639 sqlite3VdbeGoto(v, dest); 4640 }else if( exprAlwaysFalse(pExpr) ){ 4641 /* No-op */ 4642 }else{ 4643 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 4644 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 4645 VdbeCoverage(v); 4646 testcase( regFree1==0 ); 4647 testcase( jumpIfNull==0 ); 4648 } 4649 break; 4650 } 4651 } 4652 sqlite3ReleaseTempReg(pParse, regFree1); 4653 sqlite3ReleaseTempReg(pParse, regFree2); 4654 } 4655 4656 /* 4657 ** Generate code for a boolean expression such that a jump is made 4658 ** to the label "dest" if the expression is false but execution 4659 ** continues straight thru if the expression is true. 4660 ** 4661 ** If the expression evaluates to NULL (neither true nor false) then 4662 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 4663 ** is 0. 4664 */ 4665 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 4666 Vdbe *v = pParse->pVdbe; 4667 int op = 0; 4668 int regFree1 = 0; 4669 int regFree2 = 0; 4670 int r1, r2; 4671 4672 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 4673 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 4674 if( pExpr==0 ) return; 4675 4676 /* The value of pExpr->op and op are related as follows: 4677 ** 4678 ** pExpr->op op 4679 ** --------- ---------- 4680 ** TK_ISNULL OP_NotNull 4681 ** TK_NOTNULL OP_IsNull 4682 ** TK_NE OP_Eq 4683 ** TK_EQ OP_Ne 4684 ** TK_GT OP_Le 4685 ** TK_LE OP_Gt 4686 ** TK_GE OP_Lt 4687 ** TK_LT OP_Ge 4688 ** 4689 ** For other values of pExpr->op, op is undefined and unused. 4690 ** The value of TK_ and OP_ constants are arranged such that we 4691 ** can compute the mapping above using the following expression. 4692 ** Assert()s verify that the computation is correct. 4693 */ 4694 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 4695 4696 /* Verify correct alignment of TK_ and OP_ constants 4697 */ 4698 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 4699 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 4700 assert( pExpr->op!=TK_NE || op==OP_Eq ); 4701 assert( pExpr->op!=TK_EQ || op==OP_Ne ); 4702 assert( pExpr->op!=TK_LT || op==OP_Ge ); 4703 assert( pExpr->op!=TK_LE || op==OP_Gt ); 4704 assert( pExpr->op!=TK_GT || op==OP_Le ); 4705 assert( pExpr->op!=TK_GE || op==OP_Lt ); 4706 4707 switch( pExpr->op ){ 4708 case TK_AND: { 4709 testcase( jumpIfNull==0 ); 4710 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 4711 sqlite3ExprCachePush(pParse); 4712 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 4713 sqlite3ExprCachePop(pParse); 4714 break; 4715 } 4716 case TK_OR: { 4717 int d2 = sqlite3VdbeMakeLabel(v); 4718 testcase( jumpIfNull==0 ); 4719 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 4720 sqlite3ExprCachePush(pParse); 4721 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 4722 sqlite3VdbeResolveLabel(v, d2); 4723 sqlite3ExprCachePop(pParse); 4724 break; 4725 } 4726 case TK_NOT: { 4727 testcase( jumpIfNull==0 ); 4728 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 4729 break; 4730 } 4731 case TK_TRUTH: { 4732 int isNot; /* IS NOT TRUE or IS NOT FALSE */ 4733 int isTrue; /* IS TRUE or IS NOT TRUE */ 4734 testcase( jumpIfNull==0 ); 4735 isNot = pExpr->op2==TK_ISNOT; 4736 isTrue = sqlite3ExprTruthValue(pExpr->pRight); 4737 testcase( isTrue && isNot ); 4738 testcase( !isTrue && isNot ); 4739 if( isTrue ^ isNot ){ 4740 /* IS TRUE and IS NOT FALSE */ 4741 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, 4742 isNot ? 0 : SQLITE_JUMPIFNULL); 4743 4744 }else{ 4745 /* IS FALSE and IS NOT TRUE */ 4746 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, 4747 isNot ? 0 : SQLITE_JUMPIFNULL); 4748 } 4749 break; 4750 } 4751 case TK_IS: 4752 case TK_ISNOT: 4753 testcase( pExpr->op==TK_IS ); 4754 testcase( pExpr->op==TK_ISNOT ); 4755 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; 4756 jumpIfNull = SQLITE_NULLEQ; 4757 /* Fall thru */ 4758 case TK_LT: 4759 case TK_LE: 4760 case TK_GT: 4761 case TK_GE: 4762 case TK_NE: 4763 case TK_EQ: { 4764 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; 4765 testcase( jumpIfNull==0 ); 4766 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 4767 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 4768 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 4769 r1, r2, dest, jumpIfNull); 4770 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 4771 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 4772 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 4773 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 4774 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 4775 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 4776 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 4777 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 4778 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 4779 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 4780 testcase( regFree1==0 ); 4781 testcase( regFree2==0 ); 4782 break; 4783 } 4784 case TK_ISNULL: 4785 case TK_NOTNULL: { 4786 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 4787 sqlite3VdbeAddOp2(v, op, r1, dest); 4788 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); 4789 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); 4790 testcase( regFree1==0 ); 4791 break; 4792 } 4793 case TK_BETWEEN: { 4794 testcase( jumpIfNull==0 ); 4795 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); 4796 break; 4797 } 4798 #ifndef SQLITE_OMIT_SUBQUERY 4799 case TK_IN: { 4800 if( jumpIfNull ){ 4801 sqlite3ExprCodeIN(pParse, pExpr, dest, dest); 4802 }else{ 4803 int destIfNull = sqlite3VdbeMakeLabel(v); 4804 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 4805 sqlite3VdbeResolveLabel(v, destIfNull); 4806 } 4807 break; 4808 } 4809 #endif 4810 default: { 4811 default_expr: 4812 if( exprAlwaysFalse(pExpr) ){ 4813 sqlite3VdbeGoto(v, dest); 4814 }else if( exprAlwaysTrue(pExpr) ){ 4815 /* no-op */ 4816 }else{ 4817 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 4818 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 4819 VdbeCoverage(v); 4820 testcase( regFree1==0 ); 4821 testcase( jumpIfNull==0 ); 4822 } 4823 break; 4824 } 4825 } 4826 sqlite3ReleaseTempReg(pParse, regFree1); 4827 sqlite3ReleaseTempReg(pParse, regFree2); 4828 } 4829 4830 /* 4831 ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before 4832 ** code generation, and that copy is deleted after code generation. This 4833 ** ensures that the original pExpr is unchanged. 4834 */ 4835 void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ 4836 sqlite3 *db = pParse->db; 4837 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); 4838 if( db->mallocFailed==0 ){ 4839 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); 4840 } 4841 sqlite3ExprDelete(db, pCopy); 4842 } 4843 4844 /* 4845 ** Expression pVar is guaranteed to be an SQL variable. pExpr may be any 4846 ** type of expression. 4847 ** 4848 ** If pExpr is a simple SQL value - an integer, real, string, blob 4849 ** or NULL value - then the VDBE currently being prepared is configured 4850 ** to re-prepare each time a new value is bound to variable pVar. 4851 ** 4852 ** Additionally, if pExpr is a simple SQL value and the value is the 4853 ** same as that currently bound to variable pVar, non-zero is returned. 4854 ** Otherwise, if the values are not the same or if pExpr is not a simple 4855 ** SQL value, zero is returned. 4856 */ 4857 static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){ 4858 int res = 0; 4859 int iVar; 4860 sqlite3_value *pL, *pR = 0; 4861 4862 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR); 4863 if( pR ){ 4864 iVar = pVar->iColumn; 4865 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); 4866 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB); 4867 if( pL ){ 4868 if( sqlite3_value_type(pL)==SQLITE_TEXT ){ 4869 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */ 4870 } 4871 res = 0==sqlite3MemCompare(pL, pR, 0); 4872 } 4873 sqlite3ValueFree(pR); 4874 sqlite3ValueFree(pL); 4875 } 4876 4877 return res; 4878 } 4879 4880 /* 4881 ** Do a deep comparison of two expression trees. Return 0 if the two 4882 ** expressions are completely identical. Return 1 if they differ only 4883 ** by a COLLATE operator at the top level. Return 2 if there are differences 4884 ** other than the top-level COLLATE operator. 4885 ** 4886 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4887 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4888 ** 4889 ** The pA side might be using TK_REGISTER. If that is the case and pB is 4890 ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 4891 ** 4892 ** Sometimes this routine will return 2 even if the two expressions 4893 ** really are equivalent. If we cannot prove that the expressions are 4894 ** identical, we return 2 just to be safe. So if this routine 4895 ** returns 2, then you do not really know for certain if the two 4896 ** expressions are the same. But if you get a 0 or 1 return, then you 4897 ** can be sure the expressions are the same. In the places where 4898 ** this routine is used, it does not hurt to get an extra 2 - that 4899 ** just might result in some slightly slower code. But returning 4900 ** an incorrect 0 or 1 could lead to a malfunction. 4901 ** 4902 ** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in 4903 ** pParse->pReprepare can be matched against literals in pB. The 4904 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced. 4905 ** If pParse is NULL (the normal case) then any TK_VARIABLE term in 4906 ** Argument pParse should normally be NULL. If it is not NULL and pA or 4907 ** pB causes a return value of 2. 4908 */ 4909 int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){ 4910 u32 combinedFlags; 4911 if( pA==0 || pB==0 ){ 4912 return pB==pA ? 0 : 2; 4913 } 4914 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){ 4915 return 0; 4916 } 4917 combinedFlags = pA->flags | pB->flags; 4918 if( combinedFlags & EP_IntValue ){ 4919 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ 4920 return 0; 4921 } 4922 return 2; 4923 } 4924 if( pA->op!=pB->op ){ 4925 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){ 4926 return 1; 4927 } 4928 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){ 4929 return 1; 4930 } 4931 return 2; 4932 } 4933 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ 4934 if( pA->op==TK_FUNCTION ){ 4935 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 4936 }else if( pA->op==TK_COLLATE ){ 4937 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 4938 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ 4939 return 2; 4940 } 4941 } 4942 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; 4943 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 4944 if( combinedFlags & EP_xIsSelect ) return 2; 4945 if( sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2; 4946 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2; 4947 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 4948 assert( (combinedFlags & EP_Reduced)==0 ); 4949 if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){ 4950 if( pA->iColumn!=pB->iColumn ) return 2; 4951 if( pA->iTable!=pB->iTable 4952 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; 4953 } 4954 #ifndef SQLITE_OMIT_WINDOWFUNC 4955 /* Justification for the assert(): 4956 ** window functions have p->op==TK_FUNCTION but aggregate functions 4957 ** have p->op==TK_AGG_FUNCTION. So any comparison between an aggregate 4958 ** function and a window function should have failed before reaching 4959 ** this point. And, it is not possible to have a window function and 4960 ** a scalar function with the same name and number of arguments. So 4961 ** if we reach this point, either A and B both window functions or 4962 ** neither are a window functions. */ 4963 assert( (pA->pWin==0)==(pB->pWin==0) ); 4964 4965 if( pA->pWin!=0 ){ 4966 if( sqlite3WindowCompare(pParse,pA->pWin,pB->pWin)!=0 ) return 2; 4967 } 4968 #endif 4969 } 4970 return 0; 4971 } 4972 4973 /* 4974 ** Compare two ExprList objects. Return 0 if they are identical and 4975 ** non-zero if they differ in any way. 4976 ** 4977 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4978 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4979 ** 4980 ** This routine might return non-zero for equivalent ExprLists. The 4981 ** only consequence will be disabled optimizations. But this routine 4982 ** must never return 0 if the two ExprList objects are different, or 4983 ** a malfunction will result. 4984 ** 4985 ** Two NULL pointers are considered to be the same. But a NULL pointer 4986 ** always differs from a non-NULL pointer. 4987 */ 4988 int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ 4989 int i; 4990 if( pA==0 && pB==0 ) return 0; 4991 if( pA==0 || pB==0 ) return 1; 4992 if( pA->nExpr!=pB->nExpr ) return 1; 4993 for(i=0; i<pA->nExpr; i++){ 4994 Expr *pExprA = pA->a[i].pExpr; 4995 Expr *pExprB = pB->a[i].pExpr; 4996 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; 4997 if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1; 4998 } 4999 return 0; 5000 } 5001 5002 /* 5003 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level 5004 ** are ignored. 5005 */ 5006 int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){ 5007 return sqlite3ExprCompare(0, 5008 sqlite3ExprSkipCollate(pA), 5009 sqlite3ExprSkipCollate(pB), 5010 iTab); 5011 } 5012 5013 /* 5014 ** Return true if we can prove the pE2 will always be true if pE1 is 5015 ** true. Return false if we cannot complete the proof or if pE2 might 5016 ** be false. Examples: 5017 ** 5018 ** pE1: x==5 pE2: x==5 Result: true 5019 ** pE1: x>0 pE2: x==5 Result: false 5020 ** pE1: x=21 pE2: x=21 OR y=43 Result: true 5021 ** pE1: x!=123 pE2: x IS NOT NULL Result: true 5022 ** pE1: x!=?1 pE2: x IS NOT NULL Result: true 5023 ** pE1: x IS NULL pE2: x IS NOT NULL Result: false 5024 ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false 5025 ** 5026 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has 5027 ** Expr.iTable<0 then assume a table number given by iTab. 5028 ** 5029 ** If pParse is not NULL, then the values of bound variables in pE1 are 5030 ** compared against literal values in pE2 and pParse->pVdbe->expmask is 5031 ** modified to record which bound variables are referenced. If pParse 5032 ** is NULL, then false will be returned if pE1 contains any bound variables. 5033 ** 5034 ** When in doubt, return false. Returning true might give a performance 5035 ** improvement. Returning false might cause a performance reduction, but 5036 ** it will always give the correct answer and is hence always safe. 5037 */ 5038 int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){ 5039 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){ 5040 return 1; 5041 } 5042 if( pE2->op==TK_OR 5043 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab) 5044 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) ) 5045 ){ 5046 return 1; 5047 } 5048 if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){ 5049 Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft); 5050 testcase( pX!=pE1->pLeft ); 5051 if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1; 5052 } 5053 return 0; 5054 } 5055 5056 /* 5057 ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow(). 5058 ** If the expression node requires that the table at pWalker->iCur 5059 ** have a non-NULL column, then set pWalker->eCode to 1 and abort. 5060 */ 5061 static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){ 5062 /* This routine is only called for WHERE clause expressions and so it 5063 ** cannot have any TK_AGG_COLUMN entries because those are only found 5064 ** in HAVING clauses. We can get a TK_AGG_FUNCTION in a WHERE clause, 5065 ** but that is an illegal construct and the query will be rejected at 5066 ** a later stage of processing, so the TK_AGG_FUNCTION case does not 5067 ** need to be considered here. */ 5068 assert( pExpr->op!=TK_AGG_COLUMN ); 5069 testcase( pExpr->op==TK_AGG_FUNCTION ); 5070 5071 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune; 5072 switch( pExpr->op ){ 5073 case TK_ISNOT: 5074 case TK_NOT: 5075 case TK_ISNULL: 5076 case TK_IS: 5077 case TK_OR: 5078 case TK_CASE: 5079 case TK_IN: 5080 case TK_FUNCTION: 5081 testcase( pExpr->op==TK_ISNOT ); 5082 testcase( pExpr->op==TK_NOT ); 5083 testcase( pExpr->op==TK_ISNULL ); 5084 testcase( pExpr->op==TK_IS ); 5085 testcase( pExpr->op==TK_OR ); 5086 testcase( pExpr->op==TK_CASE ); 5087 testcase( pExpr->op==TK_IN ); 5088 testcase( pExpr->op==TK_FUNCTION ); 5089 return WRC_Prune; 5090 case TK_COLUMN: 5091 if( pWalker->u.iCur==pExpr->iTable ){ 5092 pWalker->eCode = 1; 5093 return WRC_Abort; 5094 } 5095 return WRC_Prune; 5096 5097 /* Virtual tables are allowed to use constraints like x=NULL. So 5098 ** a term of the form x=y does not prove that y is not null if x 5099 ** is the column of a virtual table */ 5100 case TK_EQ: 5101 case TK_NE: 5102 case TK_LT: 5103 case TK_LE: 5104 case TK_GT: 5105 case TK_GE: 5106 testcase( pExpr->op==TK_EQ ); 5107 testcase( pExpr->op==TK_NE ); 5108 testcase( pExpr->op==TK_LT ); 5109 testcase( pExpr->op==TK_LE ); 5110 testcase( pExpr->op==TK_GT ); 5111 testcase( pExpr->op==TK_GE ); 5112 if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab)) 5113 || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab)) 5114 ){ 5115 return WRC_Prune; 5116 } 5117 default: 5118 return WRC_Continue; 5119 } 5120 } 5121 5122 /* 5123 ** Return true (non-zero) if expression p can only be true if at least 5124 ** one column of table iTab is non-null. In other words, return true 5125 ** if expression p will always be NULL or false if every column of iTab 5126 ** is NULL. 5127 ** 5128 ** False negatives are acceptable. In other words, it is ok to return 5129 ** zero even if expression p will never be true of every column of iTab 5130 ** is NULL. A false negative is merely a missed optimization opportunity. 5131 ** 5132 ** False positives are not allowed, however. A false positive may result 5133 ** in an incorrect answer. 5134 ** 5135 ** Terms of p that are marked with EP_FromJoin (and hence that come from 5136 ** the ON or USING clauses of LEFT JOINS) are excluded from the analysis. 5137 ** 5138 ** This routine is used to check if a LEFT JOIN can be converted into 5139 ** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE 5140 ** clause requires that some column of the right table of the LEFT JOIN 5141 ** be non-NULL, then the LEFT JOIN can be safely converted into an 5142 ** ordinary join. 5143 */ 5144 int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){ 5145 Walker w; 5146 w.xExprCallback = impliesNotNullRow; 5147 w.xSelectCallback = 0; 5148 w.xSelectCallback2 = 0; 5149 w.eCode = 0; 5150 w.u.iCur = iTab; 5151 sqlite3WalkExpr(&w, p); 5152 return w.eCode; 5153 } 5154 5155 /* 5156 ** An instance of the following structure is used by the tree walker 5157 ** to determine if an expression can be evaluated by reference to the 5158 ** index only, without having to do a search for the corresponding 5159 ** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur 5160 ** is the cursor for the table. 5161 */ 5162 struct IdxCover { 5163 Index *pIdx; /* The index to be tested for coverage */ 5164 int iCur; /* Cursor number for the table corresponding to the index */ 5165 }; 5166 5167 /* 5168 ** Check to see if there are references to columns in table 5169 ** pWalker->u.pIdxCover->iCur can be satisfied using the index 5170 ** pWalker->u.pIdxCover->pIdx. 5171 */ 5172 static int exprIdxCover(Walker *pWalker, Expr *pExpr){ 5173 if( pExpr->op==TK_COLUMN 5174 && pExpr->iTable==pWalker->u.pIdxCover->iCur 5175 && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0 5176 ){ 5177 pWalker->eCode = 1; 5178 return WRC_Abort; 5179 } 5180 return WRC_Continue; 5181 } 5182 5183 /* 5184 ** Determine if an index pIdx on table with cursor iCur contains will 5185 ** the expression pExpr. Return true if the index does cover the 5186 ** expression and false if the pExpr expression references table columns 5187 ** that are not found in the index pIdx. 5188 ** 5189 ** An index covering an expression means that the expression can be 5190 ** evaluated using only the index and without having to lookup the 5191 ** corresponding table entry. 5192 */ 5193 int sqlite3ExprCoveredByIndex( 5194 Expr *pExpr, /* The index to be tested */ 5195 int iCur, /* The cursor number for the corresponding table */ 5196 Index *pIdx /* The index that might be used for coverage */ 5197 ){ 5198 Walker w; 5199 struct IdxCover xcov; 5200 memset(&w, 0, sizeof(w)); 5201 xcov.iCur = iCur; 5202 xcov.pIdx = pIdx; 5203 w.xExprCallback = exprIdxCover; 5204 w.u.pIdxCover = &xcov; 5205 sqlite3WalkExpr(&w, pExpr); 5206 return !w.eCode; 5207 } 5208 5209 5210 /* 5211 ** An instance of the following structure is used by the tree walker 5212 ** to count references to table columns in the arguments of an 5213 ** aggregate function, in order to implement the 5214 ** sqlite3FunctionThisSrc() routine. 5215 */ 5216 struct SrcCount { 5217 SrcList *pSrc; /* One particular FROM clause in a nested query */ 5218 int nThis; /* Number of references to columns in pSrcList */ 5219 int nOther; /* Number of references to columns in other FROM clauses */ 5220 }; 5221 5222 /* 5223 ** Count the number of references to columns. 5224 */ 5225 static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 5226 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() 5227 ** is always called before sqlite3ExprAnalyzeAggregates() and so the 5228 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If 5229 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the 5230 ** NEVER() will need to be removed. */ 5231 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 5232 int i; 5233 struct SrcCount *p = pWalker->u.pSrcCount; 5234 SrcList *pSrc = p->pSrc; 5235 int nSrc = pSrc ? pSrc->nSrc : 0; 5236 for(i=0; i<nSrc; i++){ 5237 if( pExpr->iTable==pSrc->a[i].iCursor ) break; 5238 } 5239 if( i<nSrc ){ 5240 p->nThis++; 5241 }else{ 5242 p->nOther++; 5243 } 5244 } 5245 return WRC_Continue; 5246 } 5247 5248 /* 5249 ** Determine if any of the arguments to the pExpr Function reference 5250 ** pSrcList. Return true if they do. Also return true if the function 5251 ** has no arguments or has only constant arguments. Return false if pExpr 5252 ** references columns but not columns of tables found in pSrcList. 5253 */ 5254 int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ 5255 Walker w; 5256 struct SrcCount cnt; 5257 assert( pExpr->op==TK_AGG_FUNCTION ); 5258 w.xExprCallback = exprSrcCount; 5259 w.xSelectCallback = 0; 5260 w.u.pSrcCount = &cnt; 5261 cnt.pSrc = pSrcList; 5262 cnt.nThis = 0; 5263 cnt.nOther = 0; 5264 sqlite3WalkExprList(&w, pExpr->x.pList); 5265 return cnt.nThis>0 || cnt.nOther==0; 5266 } 5267 5268 /* 5269 ** Add a new element to the pAggInfo->aCol[] array. Return the index of 5270 ** the new element. Return a negative number if malloc fails. 5271 */ 5272 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 5273 int i; 5274 pInfo->aCol = sqlite3ArrayAllocate( 5275 db, 5276 pInfo->aCol, 5277 sizeof(pInfo->aCol[0]), 5278 &pInfo->nColumn, 5279 &i 5280 ); 5281 return i; 5282 } 5283 5284 /* 5285 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 5286 ** the new element. Return a negative number if malloc fails. 5287 */ 5288 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 5289 int i; 5290 pInfo->aFunc = sqlite3ArrayAllocate( 5291 db, 5292 pInfo->aFunc, 5293 sizeof(pInfo->aFunc[0]), 5294 &pInfo->nFunc, 5295 &i 5296 ); 5297 return i; 5298 } 5299 5300 /* 5301 ** This is the xExprCallback for a tree walker. It is used to 5302 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 5303 ** for additional information. 5304 */ 5305 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ 5306 int i; 5307 NameContext *pNC = pWalker->u.pNC; 5308 Parse *pParse = pNC->pParse; 5309 SrcList *pSrcList = pNC->pSrcList; 5310 AggInfo *pAggInfo = pNC->uNC.pAggInfo; 5311 5312 assert( pNC->ncFlags & NC_UAggInfo ); 5313 switch( pExpr->op ){ 5314 case TK_AGG_COLUMN: 5315 case TK_COLUMN: { 5316 testcase( pExpr->op==TK_AGG_COLUMN ); 5317 testcase( pExpr->op==TK_COLUMN ); 5318 /* Check to see if the column is in one of the tables in the FROM 5319 ** clause of the aggregate query */ 5320 if( ALWAYS(pSrcList!=0) ){ 5321 struct SrcList_item *pItem = pSrcList->a; 5322 for(i=0; i<pSrcList->nSrc; i++, pItem++){ 5323 struct AggInfo_col *pCol; 5324 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 5325 if( pExpr->iTable==pItem->iCursor ){ 5326 /* If we reach this point, it means that pExpr refers to a table 5327 ** that is in the FROM clause of the aggregate query. 5328 ** 5329 ** Make an entry for the column in pAggInfo->aCol[] if there 5330 ** is not an entry there already. 5331 */ 5332 int k; 5333 pCol = pAggInfo->aCol; 5334 for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 5335 if( pCol->iTable==pExpr->iTable && 5336 pCol->iColumn==pExpr->iColumn ){ 5337 break; 5338 } 5339 } 5340 if( (k>=pAggInfo->nColumn) 5341 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 5342 ){ 5343 pCol = &pAggInfo->aCol[k]; 5344 pCol->pTab = pExpr->pTab; 5345 pCol->iTable = pExpr->iTable; 5346 pCol->iColumn = pExpr->iColumn; 5347 pCol->iMem = ++pParse->nMem; 5348 pCol->iSorterColumn = -1; 5349 pCol->pExpr = pExpr; 5350 if( pAggInfo->pGroupBy ){ 5351 int j, n; 5352 ExprList *pGB = pAggInfo->pGroupBy; 5353 struct ExprList_item *pTerm = pGB->a; 5354 n = pGB->nExpr; 5355 for(j=0; j<n; j++, pTerm++){ 5356 Expr *pE = pTerm->pExpr; 5357 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 5358 pE->iColumn==pExpr->iColumn ){ 5359 pCol->iSorterColumn = j; 5360 break; 5361 } 5362 } 5363 } 5364 if( pCol->iSorterColumn<0 ){ 5365 pCol->iSorterColumn = pAggInfo->nSortingColumn++; 5366 } 5367 } 5368 /* There is now an entry for pExpr in pAggInfo->aCol[] (either 5369 ** because it was there before or because we just created it). 5370 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 5371 ** pAggInfo->aCol[] entry. 5372 */ 5373 ExprSetVVAProperty(pExpr, EP_NoReduce); 5374 pExpr->pAggInfo = pAggInfo; 5375 pExpr->op = TK_AGG_COLUMN; 5376 pExpr->iAgg = (i16)k; 5377 break; 5378 } /* endif pExpr->iTable==pItem->iCursor */ 5379 } /* end loop over pSrcList */ 5380 } 5381 return WRC_Prune; 5382 } 5383 case TK_AGG_FUNCTION: { 5384 if( (pNC->ncFlags & NC_InAggFunc)==0 5385 && pWalker->walkerDepth==pExpr->op2 5386 ){ 5387 /* Check to see if pExpr is a duplicate of another aggregate 5388 ** function that is already in the pAggInfo structure 5389 */ 5390 struct AggInfo_func *pItem = pAggInfo->aFunc; 5391 for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 5392 if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){ 5393 break; 5394 } 5395 } 5396 if( i>=pAggInfo->nFunc ){ 5397 /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 5398 */ 5399 u8 enc = ENC(pParse->db); 5400 i = addAggInfoFunc(pParse->db, pAggInfo); 5401 if( i>=0 ){ 5402 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 5403 pItem = &pAggInfo->aFunc[i]; 5404 pItem->pExpr = pExpr; 5405 pItem->iMem = ++pParse->nMem; 5406 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 5407 pItem->pFunc = sqlite3FindFunction(pParse->db, 5408 pExpr->u.zToken, 5409 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); 5410 if( pExpr->flags & EP_Distinct ){ 5411 pItem->iDistinct = pParse->nTab++; 5412 }else{ 5413 pItem->iDistinct = -1; 5414 } 5415 } 5416 } 5417 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 5418 */ 5419 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 5420 ExprSetVVAProperty(pExpr, EP_NoReduce); 5421 pExpr->iAgg = (i16)i; 5422 pExpr->pAggInfo = pAggInfo; 5423 return WRC_Prune; 5424 }else{ 5425 return WRC_Continue; 5426 } 5427 } 5428 } 5429 return WRC_Continue; 5430 } 5431 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ 5432 UNUSED_PARAMETER(pSelect); 5433 pWalker->walkerDepth++; 5434 return WRC_Continue; 5435 } 5436 static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){ 5437 UNUSED_PARAMETER(pSelect); 5438 pWalker->walkerDepth--; 5439 } 5440 5441 /* 5442 ** Analyze the pExpr expression looking for aggregate functions and 5443 ** for variables that need to be added to AggInfo object that pNC->pAggInfo 5444 ** points to. Additional entries are made on the AggInfo object as 5445 ** necessary. 5446 ** 5447 ** This routine should only be called after the expression has been 5448 ** analyzed by sqlite3ResolveExprNames(). 5449 */ 5450 void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 5451 Walker w; 5452 w.xExprCallback = analyzeAggregate; 5453 w.xSelectCallback = analyzeAggregatesInSelect; 5454 w.xSelectCallback2 = analyzeAggregatesInSelectEnd; 5455 w.walkerDepth = 0; 5456 w.u.pNC = pNC; 5457 assert( pNC->pSrcList!=0 ); 5458 sqlite3WalkExpr(&w, pExpr); 5459 } 5460 5461 /* 5462 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 5463 ** expression list. Return the number of errors. 5464 ** 5465 ** If an error is found, the analysis is cut short. 5466 */ 5467 void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 5468 struct ExprList_item *pItem; 5469 int i; 5470 if( pList ){ 5471 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 5472 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 5473 } 5474 } 5475 } 5476 5477 /* 5478 ** Allocate a single new register for use to hold some intermediate result. 5479 */ 5480 int sqlite3GetTempReg(Parse *pParse){ 5481 if( pParse->nTempReg==0 ){ 5482 return ++pParse->nMem; 5483 } 5484 return pParse->aTempReg[--pParse->nTempReg]; 5485 } 5486 5487 /* 5488 ** Deallocate a register, making available for reuse for some other 5489 ** purpose. 5490 ** 5491 ** If a register is currently being used by the column cache, then 5492 ** the deallocation is deferred until the column cache line that uses 5493 ** the register becomes stale. 5494 */ 5495 void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 5496 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 5497 int i; 5498 struct yColCache *p; 5499 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ 5500 if( p->iReg==iReg ){ 5501 p->tempReg = 1; 5502 return; 5503 } 5504 } 5505 pParse->aTempReg[pParse->nTempReg++] = iReg; 5506 } 5507 } 5508 5509 /* 5510 ** Allocate or deallocate a block of nReg consecutive registers. 5511 */ 5512 int sqlite3GetTempRange(Parse *pParse, int nReg){ 5513 int i, n; 5514 if( nReg==1 ) return sqlite3GetTempReg(pParse); 5515 i = pParse->iRangeReg; 5516 n = pParse->nRangeReg; 5517 if( nReg<=n ){ 5518 assert( !usedAsColumnCache(pParse, i, i+n-1) ); 5519 pParse->iRangeReg += nReg; 5520 pParse->nRangeReg -= nReg; 5521 }else{ 5522 i = pParse->nMem+1; 5523 pParse->nMem += nReg; 5524 } 5525 return i; 5526 } 5527 void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 5528 if( nReg==1 ){ 5529 sqlite3ReleaseTempReg(pParse, iReg); 5530 return; 5531 } 5532 sqlite3ExprCacheRemove(pParse, iReg, nReg); 5533 if( nReg>pParse->nRangeReg ){ 5534 pParse->nRangeReg = nReg; 5535 pParse->iRangeReg = iReg; 5536 } 5537 } 5538 5539 /* 5540 ** Mark all temporary registers as being unavailable for reuse. 5541 */ 5542 void sqlite3ClearTempRegCache(Parse *pParse){ 5543 pParse->nTempReg = 0; 5544 pParse->nRangeReg = 0; 5545 } 5546 5547 /* 5548 ** Validate that no temporary register falls within the range of 5549 ** iFirst..iLast, inclusive. This routine is only call from within assert() 5550 ** statements. 5551 */ 5552 #ifdef SQLITE_DEBUG 5553 int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ 5554 int i; 5555 if( pParse->nRangeReg>0 5556 && pParse->iRangeReg+pParse->nRangeReg > iFirst 5557 && pParse->iRangeReg <= iLast 5558 ){ 5559 return 0; 5560 } 5561 for(i=0; i<pParse->nTempReg; i++){ 5562 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ 5563 return 0; 5564 } 5565 } 5566 return 1; 5567 } 5568 #endif /* SQLITE_DEBUG */ 5569