000001 /* 000002 ** 2001 September 15 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** This file contains routines used for analyzing expressions and 000013 ** for generating VDBE code that evaluates expressions in SQLite. 000014 */ 000015 #include "sqliteInt.h" 000016 000017 /* Forward declarations */ 000018 static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); 000019 static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree); 000020 000021 /* 000022 ** Return the affinity character for a single column of a table. 000023 */ 000024 char sqlite3TableColumnAffinity(const Table *pTab, int iCol){ 000025 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER; 000026 return pTab->aCol[iCol].affinity; 000027 } 000028 000029 /* 000030 ** Return the 'affinity' of the expression pExpr if any. 000031 ** 000032 ** If pExpr is a column, a reference to a column via an 'AS' alias, 000033 ** or a sub-select with a column as the return value, then the 000034 ** affinity of that column is returned. Otherwise, 0x00 is returned, 000035 ** indicating no affinity for the expression. 000036 ** 000037 ** i.e. the WHERE clause expressions in the following statements all 000038 ** have an affinity: 000039 ** 000040 ** CREATE TABLE t1(a); 000041 ** SELECT * FROM t1 WHERE a; 000042 ** SELECT a AS b FROM t1 WHERE b; 000043 ** SELECT * FROM t1 WHERE (select a from t1); 000044 */ 000045 char sqlite3ExprAffinity(const Expr *pExpr){ 000046 int op; 000047 op = pExpr->op; 000048 while( 1 /* exit-by-break */ ){ 000049 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){ 000050 assert( ExprUseYTab(pExpr) ); 000051 assert( pExpr->y.pTab!=0 ); 000052 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); 000053 } 000054 if( op==TK_SELECT ){ 000055 assert( ExprUseXSelect(pExpr) ); 000056 assert( pExpr->x.pSelect!=0 ); 000057 assert( pExpr->x.pSelect->pEList!=0 ); 000058 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 ); 000059 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); 000060 } 000061 #ifndef SQLITE_OMIT_CAST 000062 if( op==TK_CAST ){ 000063 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 000064 return sqlite3AffinityType(pExpr->u.zToken, 0); 000065 } 000066 #endif 000067 if( op==TK_SELECT_COLUMN ){ 000068 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) ); 000069 assert( pExpr->iColumn < pExpr->iTable ); 000070 assert( pExpr->iColumn >= 0 ); 000071 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr ); 000072 return sqlite3ExprAffinity( 000073 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr 000074 ); 000075 } 000076 if( op==TK_VECTOR ){ 000077 assert( ExprUseXList(pExpr) ); 000078 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr); 000079 } 000080 if( ExprHasProperty(pExpr, EP_Skip|EP_IfNullRow) ){ 000081 assert( pExpr->op==TK_COLLATE 000082 || pExpr->op==TK_IF_NULL_ROW 000083 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) ); 000084 pExpr = pExpr->pLeft; 000085 op = pExpr->op; 000086 continue; 000087 } 000088 if( op!=TK_REGISTER ) break; 000089 op = pExpr->op2; 000090 if( NEVER( op==TK_REGISTER ) ) break; 000091 } 000092 return pExpr->affExpr; 000093 } 000094 000095 /* 000096 ** Make a guess at all the possible datatypes of the result that could 000097 ** be returned by an expression. Return a bitmask indicating the answer: 000098 ** 000099 ** 0x01 Numeric 000100 ** 0x02 Text 000101 ** 0x04 Blob 000102 ** 000103 ** If the expression must return NULL, then 0x00 is returned. 000104 */ 000105 int sqlite3ExprDataType(const Expr *pExpr){ 000106 while( pExpr ){ 000107 switch( pExpr->op ){ 000108 case TK_COLLATE: 000109 case TK_IF_NULL_ROW: 000110 case TK_UPLUS: { 000111 pExpr = pExpr->pLeft; 000112 break; 000113 } 000114 case TK_NULL: { 000115 pExpr = 0; 000116 break; 000117 } 000118 case TK_STRING: { 000119 return 0x02; 000120 } 000121 case TK_BLOB: { 000122 return 0x04; 000123 } 000124 case TK_CONCAT: { 000125 return 0x06; 000126 } 000127 case TK_VARIABLE: 000128 case TK_AGG_FUNCTION: 000129 case TK_FUNCTION: { 000130 return 0x07; 000131 } 000132 case TK_COLUMN: 000133 case TK_AGG_COLUMN: 000134 case TK_SELECT: 000135 case TK_CAST: 000136 case TK_SELECT_COLUMN: 000137 case TK_VECTOR: { 000138 int aff = sqlite3ExprAffinity(pExpr); 000139 if( aff>=SQLITE_AFF_NUMERIC ) return 0x05; 000140 if( aff==SQLITE_AFF_TEXT ) return 0x06; 000141 return 0x07; 000142 } 000143 case TK_CASE: { 000144 int res = 0; 000145 int ii; 000146 ExprList *pList = pExpr->x.pList; 000147 assert( ExprUseXList(pExpr) && pList!=0 ); 000148 assert( pList->nExpr > 0); 000149 for(ii=1; ii<pList->nExpr; ii+=2){ 000150 res |= sqlite3ExprDataType(pList->a[ii].pExpr); 000151 } 000152 if( pList->nExpr % 2 ){ 000153 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr); 000154 } 000155 return res; 000156 } 000157 default: { 000158 return 0x01; 000159 } 000160 } /* End of switch(op) */ 000161 } /* End of while(pExpr) */ 000162 return 0x00; 000163 } 000164 000165 /* 000166 ** Set the collating sequence for expression pExpr to be the collating 000167 ** sequence named by pToken. Return a pointer to a new Expr node that 000168 ** implements the COLLATE operator. 000169 ** 000170 ** If a memory allocation error occurs, that fact is recorded in pParse->db 000171 ** and the pExpr parameter is returned unchanged. 000172 */ 000173 Expr *sqlite3ExprAddCollateToken( 000174 const Parse *pParse, /* Parsing context */ 000175 Expr *pExpr, /* Add the "COLLATE" clause to this expression */ 000176 const Token *pCollName, /* Name of collating sequence */ 000177 int dequote /* True to dequote pCollName */ 000178 ){ 000179 if( pCollName->n>0 ){ 000180 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); 000181 if( pNew ){ 000182 pNew->pLeft = pExpr; 000183 pNew->flags |= EP_Collate|EP_Skip; 000184 pExpr = pNew; 000185 } 000186 } 000187 return pExpr; 000188 } 000189 Expr *sqlite3ExprAddCollateString( 000190 const Parse *pParse, /* Parsing context */ 000191 Expr *pExpr, /* Add the "COLLATE" clause to this expression */ 000192 const char *zC /* The collating sequence name */ 000193 ){ 000194 Token s; 000195 assert( zC!=0 ); 000196 sqlite3TokenInit(&s, (char*)zC); 000197 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); 000198 } 000199 000200 /* 000201 ** Skip over any TK_COLLATE operators. 000202 */ 000203 Expr *sqlite3ExprSkipCollate(Expr *pExpr){ 000204 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ 000205 assert( pExpr->op==TK_COLLATE ); 000206 pExpr = pExpr->pLeft; 000207 } 000208 return pExpr; 000209 } 000210 000211 /* 000212 ** Skip over any TK_COLLATE operators and/or any unlikely() 000213 ** or likelihood() or likely() functions at the root of an 000214 ** expression. 000215 */ 000216 Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){ 000217 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){ 000218 if( ExprHasProperty(pExpr, EP_Unlikely) ){ 000219 assert( ExprUseXList(pExpr) ); 000220 assert( pExpr->x.pList->nExpr>0 ); 000221 assert( pExpr->op==TK_FUNCTION ); 000222 pExpr = pExpr->x.pList->a[0].pExpr; 000223 }else if( pExpr->op==TK_COLLATE ){ 000224 pExpr = pExpr->pLeft; 000225 }else{ 000226 break; 000227 } 000228 } 000229 return pExpr; 000230 } 000231 000232 /* 000233 ** Return the collation sequence for the expression pExpr. If 000234 ** there is no defined collating sequence, return NULL. 000235 ** 000236 ** See also: sqlite3ExprNNCollSeq() 000237 ** 000238 ** The sqlite3ExprNNCollSeq() works the same exact that it returns the 000239 ** default collation if pExpr has no defined collation. 000240 ** 000241 ** The collating sequence might be determined by a COLLATE operator 000242 ** or by the presence of a column with a defined collating sequence. 000243 ** COLLATE operators take first precedence. Left operands take 000244 ** precedence over right operands. 000245 */ 000246 CollSeq *sqlite3ExprCollSeq(Parse *pParse, const Expr *pExpr){ 000247 sqlite3 *db = pParse->db; 000248 CollSeq *pColl = 0; 000249 const Expr *p = pExpr; 000250 while( p ){ 000251 int op = p->op; 000252 if( op==TK_REGISTER ) op = p->op2; 000253 if( (op==TK_AGG_COLUMN && p->y.pTab!=0) 000254 || op==TK_COLUMN || op==TK_TRIGGER 000255 ){ 000256 int j; 000257 assert( ExprUseYTab(p) ); 000258 assert( p->y.pTab!=0 ); 000259 if( (j = p->iColumn)>=0 ){ 000260 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]); 000261 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); 000262 } 000263 break; 000264 } 000265 if( op==TK_CAST || op==TK_UPLUS ){ 000266 p = p->pLeft; 000267 continue; 000268 } 000269 if( op==TK_VECTOR ){ 000270 assert( ExprUseXList(p) ); 000271 p = p->x.pList->a[0].pExpr; 000272 continue; 000273 } 000274 if( op==TK_COLLATE ){ 000275 assert( !ExprHasProperty(p, EP_IntValue) ); 000276 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); 000277 break; 000278 } 000279 if( p->flags & EP_Collate ){ 000280 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ 000281 p = p->pLeft; 000282 }else{ 000283 Expr *pNext = p->pRight; 000284 /* The Expr.x union is never used at the same time as Expr.pRight */ 000285 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 ); 000286 if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){ 000287 int i; 000288 for(i=0; i<p->x.pList->nExpr; i++){ 000289 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ 000290 pNext = p->x.pList->a[i].pExpr; 000291 break; 000292 } 000293 } 000294 } 000295 p = pNext; 000296 } 000297 }else{ 000298 break; 000299 } 000300 } 000301 if( sqlite3CheckCollSeq(pParse, pColl) ){ 000302 pColl = 0; 000303 } 000304 return pColl; 000305 } 000306 000307 /* 000308 ** Return the collation sequence for the expression pExpr. If 000309 ** there is no defined collating sequence, return a pointer to the 000310 ** default collation sequence. 000311 ** 000312 ** See also: sqlite3ExprCollSeq() 000313 ** 000314 ** The sqlite3ExprCollSeq() routine works the same except that it 000315 ** returns NULL if there is no defined collation. 000316 */ 000317 CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, const Expr *pExpr){ 000318 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr); 000319 if( p==0 ) p = pParse->db->pDfltColl; 000320 assert( p!=0 ); 000321 return p; 000322 } 000323 000324 /* 000325 ** Return TRUE if the two expressions have equivalent collating sequences. 000326 */ 000327 int sqlite3ExprCollSeqMatch(Parse *pParse, const Expr *pE1, const Expr *pE2){ 000328 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1); 000329 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2); 000330 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0; 000331 } 000332 000333 /* 000334 ** pExpr is an operand of a comparison operator. aff2 is the 000335 ** type affinity of the other operand. This routine returns the 000336 ** type affinity that should be used for the comparison operator. 000337 */ 000338 char sqlite3CompareAffinity(const Expr *pExpr, char aff2){ 000339 char aff1 = sqlite3ExprAffinity(pExpr); 000340 if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){ 000341 /* Both sides of the comparison are columns. If one has numeric 000342 ** affinity, use that. Otherwise use no affinity. 000343 */ 000344 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ 000345 return SQLITE_AFF_NUMERIC; 000346 }else{ 000347 return SQLITE_AFF_BLOB; 000348 } 000349 }else{ 000350 /* One side is a column, the other is not. Use the columns affinity. */ 000351 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE ); 000352 return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE; 000353 } 000354 } 000355 000356 /* 000357 ** pExpr is a comparison operator. Return the type affinity that should 000358 ** be applied to both operands prior to doing the comparison. 000359 */ 000360 static char comparisonAffinity(const Expr *pExpr){ 000361 char aff; 000362 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 000363 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 000364 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT ); 000365 assert( pExpr->pLeft ); 000366 aff = sqlite3ExprAffinity(pExpr->pLeft); 000367 if( pExpr->pRight ){ 000368 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 000369 }else if( ExprUseXSelect(pExpr) ){ 000370 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); 000371 }else if( aff==0 ){ 000372 aff = SQLITE_AFF_BLOB; 000373 } 000374 return aff; 000375 } 000376 000377 /* 000378 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 000379 ** idx_affinity is the affinity of an indexed column. Return true 000380 ** if the index with affinity idx_affinity may be used to implement 000381 ** the comparison in pExpr. 000382 */ 000383 int sqlite3IndexAffinityOk(const Expr *pExpr, char idx_affinity){ 000384 char aff = comparisonAffinity(pExpr); 000385 if( aff<SQLITE_AFF_TEXT ){ 000386 return 1; 000387 } 000388 if( aff==SQLITE_AFF_TEXT ){ 000389 return idx_affinity==SQLITE_AFF_TEXT; 000390 } 000391 return sqlite3IsNumericAffinity(idx_affinity); 000392 } 000393 000394 /* 000395 ** Return the P5 value that should be used for a binary comparison 000396 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 000397 */ 000398 static u8 binaryCompareP5( 000399 const Expr *pExpr1, /* Left operand */ 000400 const Expr *pExpr2, /* Right operand */ 000401 int jumpIfNull /* Extra flags added to P5 */ 000402 ){ 000403 u8 aff = (char)sqlite3ExprAffinity(pExpr2); 000404 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull; 000405 return aff; 000406 } 000407 000408 /* 000409 ** Return a pointer to the collation sequence that should be used by 000410 ** a binary comparison operator comparing pLeft and pRight. 000411 ** 000412 ** If the left hand expression has a collating sequence type, then it is 000413 ** used. Otherwise the collation sequence for the right hand expression 000414 ** is used, or the default (BINARY) if neither expression has a collating 000415 ** type. 000416 ** 000417 ** Argument pRight (but not pLeft) may be a null pointer. In this case, 000418 ** it is not considered. 000419 */ 000420 CollSeq *sqlite3BinaryCompareCollSeq( 000421 Parse *pParse, 000422 const Expr *pLeft, 000423 const Expr *pRight 000424 ){ 000425 CollSeq *pColl; 000426 assert( pLeft ); 000427 if( pLeft->flags & EP_Collate ){ 000428 pColl = sqlite3ExprCollSeq(pParse, pLeft); 000429 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){ 000430 pColl = sqlite3ExprCollSeq(pParse, pRight); 000431 }else{ 000432 pColl = sqlite3ExprCollSeq(pParse, pLeft); 000433 if( !pColl ){ 000434 pColl = sqlite3ExprCollSeq(pParse, pRight); 000435 } 000436 } 000437 return pColl; 000438 } 000439 000440 /* Expression p is a comparison operator. Return a collation sequence 000441 ** appropriate for the comparison operator. 000442 ** 000443 ** This is normally just a wrapper around sqlite3BinaryCompareCollSeq(). 000444 ** However, if the OP_Commuted flag is set, then the order of the operands 000445 ** is reversed in the sqlite3BinaryCompareCollSeq() call so that the 000446 ** correct collating sequence is found. 000447 */ 000448 CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, const Expr *p){ 000449 if( ExprHasProperty(p, EP_Commuted) ){ 000450 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft); 000451 }else{ 000452 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight); 000453 } 000454 } 000455 000456 /* 000457 ** Generate code for a comparison operator. 000458 */ 000459 static int codeCompare( 000460 Parse *pParse, /* The parsing (and code generating) context */ 000461 Expr *pLeft, /* The left operand */ 000462 Expr *pRight, /* The right operand */ 000463 int opcode, /* The comparison opcode */ 000464 int in1, int in2, /* Register holding operands */ 000465 int dest, /* Jump here if true. */ 000466 int jumpIfNull, /* If true, jump if either operand is NULL */ 000467 int isCommuted /* The comparison has been commuted */ 000468 ){ 000469 int p5; 000470 int addr; 000471 CollSeq *p4; 000472 000473 if( pParse->nErr ) return 0; 000474 if( isCommuted ){ 000475 p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft); 000476 }else{ 000477 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); 000478 } 000479 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); 000480 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, 000481 (void*)p4, P4_COLLSEQ); 000482 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); 000483 return addr; 000484 } 000485 000486 /* 000487 ** Return true if expression pExpr is a vector, or false otherwise. 000488 ** 000489 ** A vector is defined as any expression that results in two or more 000490 ** columns of result. Every TK_VECTOR node is an vector because the 000491 ** parser will not generate a TK_VECTOR with fewer than two entries. 000492 ** But a TK_SELECT might be either a vector or a scalar. It is only 000493 ** considered a vector if it has two or more result columns. 000494 */ 000495 int sqlite3ExprIsVector(const Expr *pExpr){ 000496 return sqlite3ExprVectorSize(pExpr)>1; 000497 } 000498 000499 /* 000500 ** If the expression passed as the only argument is of type TK_VECTOR 000501 ** return the number of expressions in the vector. Or, if the expression 000502 ** is a sub-select, return the number of columns in the sub-select. For 000503 ** any other type of expression, return 1. 000504 */ 000505 int sqlite3ExprVectorSize(const Expr *pExpr){ 000506 u8 op = pExpr->op; 000507 if( op==TK_REGISTER ) op = pExpr->op2; 000508 if( op==TK_VECTOR ){ 000509 assert( ExprUseXList(pExpr) ); 000510 return pExpr->x.pList->nExpr; 000511 }else if( op==TK_SELECT ){ 000512 assert( ExprUseXSelect(pExpr) ); 000513 return pExpr->x.pSelect->pEList->nExpr; 000514 }else{ 000515 return 1; 000516 } 000517 } 000518 000519 /* 000520 ** Return a pointer to a subexpression of pVector that is the i-th 000521 ** column of the vector (numbered starting with 0). The caller must 000522 ** ensure that i is within range. 000523 ** 000524 ** If pVector is really a scalar (and "scalar" here includes subqueries 000525 ** that return a single column!) then return pVector unmodified. 000526 ** 000527 ** pVector retains ownership of the returned subexpression. 000528 ** 000529 ** If the vector is a (SELECT ...) then the expression returned is 000530 ** just the expression for the i-th term of the result set, and may 000531 ** not be ready for evaluation because the table cursor has not yet 000532 ** been positioned. 000533 */ 000534 Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){ 000535 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR ); 000536 if( sqlite3ExprIsVector(pVector) ){ 000537 assert( pVector->op2==0 || pVector->op==TK_REGISTER ); 000538 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){ 000539 assert( ExprUseXSelect(pVector) ); 000540 return pVector->x.pSelect->pEList->a[i].pExpr; 000541 }else{ 000542 assert( ExprUseXList(pVector) ); 000543 return pVector->x.pList->a[i].pExpr; 000544 } 000545 } 000546 return pVector; 000547 } 000548 000549 /* 000550 ** Compute and return a new Expr object which when passed to 000551 ** sqlite3ExprCode() will generate all necessary code to compute 000552 ** the iField-th column of the vector expression pVector. 000553 ** 000554 ** It is ok for pVector to be a scalar (as long as iField==0). 000555 ** In that case, this routine works like sqlite3ExprDup(). 000556 ** 000557 ** The caller owns the returned Expr object and is responsible for 000558 ** ensuring that the returned value eventually gets freed. 000559 ** 000560 ** The caller retains ownership of pVector. If pVector is a TK_SELECT, 000561 ** then the returned object will reference pVector and so pVector must remain 000562 ** valid for the life of the returned object. If pVector is a TK_VECTOR 000563 ** or a scalar expression, then it can be deleted as soon as this routine 000564 ** returns. 000565 ** 000566 ** A trick to cause a TK_SELECT pVector to be deleted together with 000567 ** the returned Expr object is to attach the pVector to the pRight field 000568 ** of the returned TK_SELECT_COLUMN Expr object. 000569 */ 000570 Expr *sqlite3ExprForVectorField( 000571 Parse *pParse, /* Parsing context */ 000572 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */ 000573 int iField, /* Which column of the vector to return */ 000574 int nField /* Total number of columns in the vector */ 000575 ){ 000576 Expr *pRet; 000577 if( pVector->op==TK_SELECT ){ 000578 assert( ExprUseXSelect(pVector) ); 000579 /* The TK_SELECT_COLUMN Expr node: 000580 ** 000581 ** pLeft: pVector containing TK_SELECT. Not deleted. 000582 ** pRight: not used. But recursively deleted. 000583 ** iColumn: Index of a column in pVector 000584 ** iTable: 0 or the number of columns on the LHS of an assignment 000585 ** pLeft->iTable: First in an array of register holding result, or 0 000586 ** if the result is not yet computed. 000587 ** 000588 ** sqlite3ExprDelete() specifically skips the recursive delete of 000589 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector 000590 ** can be attached to pRight to cause this node to take ownership of 000591 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes 000592 ** with the same pLeft pointer to the pVector, but only one of them 000593 ** will own the pVector. 000594 */ 000595 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0); 000596 if( pRet ){ 000597 ExprSetProperty(pRet, EP_FullSize); 000598 pRet->iTable = nField; 000599 pRet->iColumn = iField; 000600 pRet->pLeft = pVector; 000601 } 000602 }else{ 000603 if( pVector->op==TK_VECTOR ){ 000604 Expr **ppVector; 000605 assert( ExprUseXList(pVector) ); 000606 ppVector = &pVector->x.pList->a[iField].pExpr; 000607 pVector = *ppVector; 000608 if( IN_RENAME_OBJECT ){ 000609 /* This must be a vector UPDATE inside a trigger */ 000610 *ppVector = 0; 000611 return pVector; 000612 } 000613 } 000614 pRet = sqlite3ExprDup(pParse->db, pVector, 0); 000615 } 000616 return pRet; 000617 } 000618 000619 /* 000620 ** If expression pExpr is of type TK_SELECT, generate code to evaluate 000621 ** it. Return the register in which the result is stored (or, if the 000622 ** sub-select returns more than one column, the first in an array 000623 ** of registers in which the result is stored). 000624 ** 000625 ** If pExpr is not a TK_SELECT expression, return 0. 000626 */ 000627 static int exprCodeSubselect(Parse *pParse, Expr *pExpr){ 000628 int reg = 0; 000629 #ifndef SQLITE_OMIT_SUBQUERY 000630 if( pExpr->op==TK_SELECT ){ 000631 reg = sqlite3CodeSubselect(pParse, pExpr); 000632 } 000633 #endif 000634 return reg; 000635 } 000636 000637 /* 000638 ** Argument pVector points to a vector expression - either a TK_VECTOR 000639 ** or TK_SELECT that returns more than one column. This function returns 000640 ** the register number of a register that contains the value of 000641 ** element iField of the vector. 000642 ** 000643 ** If pVector is a TK_SELECT expression, then code for it must have 000644 ** already been generated using the exprCodeSubselect() routine. In this 000645 ** case parameter regSelect should be the first in an array of registers 000646 ** containing the results of the sub-select. 000647 ** 000648 ** If pVector is of type TK_VECTOR, then code for the requested field 000649 ** is generated. In this case (*pRegFree) may be set to the number of 000650 ** a temporary register to be freed by the caller before returning. 000651 ** 000652 ** Before returning, output parameter (*ppExpr) is set to point to the 000653 ** Expr object corresponding to element iElem of the vector. 000654 */ 000655 static int exprVectorRegister( 000656 Parse *pParse, /* Parse context */ 000657 Expr *pVector, /* Vector to extract element from */ 000658 int iField, /* Field to extract from pVector */ 000659 int regSelect, /* First in array of registers */ 000660 Expr **ppExpr, /* OUT: Expression element */ 000661 int *pRegFree /* OUT: Temp register to free */ 000662 ){ 000663 u8 op = pVector->op; 000664 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT || op==TK_ERROR ); 000665 if( op==TK_REGISTER ){ 000666 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField); 000667 return pVector->iTable+iField; 000668 } 000669 if( op==TK_SELECT ){ 000670 assert( ExprUseXSelect(pVector) ); 000671 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr; 000672 return regSelect+iField; 000673 } 000674 if( op==TK_VECTOR ){ 000675 assert( ExprUseXList(pVector) ); 000676 *ppExpr = pVector->x.pList->a[iField].pExpr; 000677 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree); 000678 } 000679 return 0; 000680 } 000681 000682 /* 000683 ** Expression pExpr is a comparison between two vector values. Compute 000684 ** the result of the comparison (1, 0, or NULL) and write that 000685 ** result into register dest. 000686 ** 000687 ** The caller must satisfy the following preconditions: 000688 ** 000689 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ 000690 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ 000691 ** otherwise: op==pExpr->op and p5==0 000692 */ 000693 static void codeVectorCompare( 000694 Parse *pParse, /* Code generator context */ 000695 Expr *pExpr, /* The comparison operation */ 000696 int dest, /* Write results into this register */ 000697 u8 op, /* Comparison operator */ 000698 u8 p5 /* SQLITE_NULLEQ or zero */ 000699 ){ 000700 Vdbe *v = pParse->pVdbe; 000701 Expr *pLeft = pExpr->pLeft; 000702 Expr *pRight = pExpr->pRight; 000703 int nLeft = sqlite3ExprVectorSize(pLeft); 000704 int i; 000705 int regLeft = 0; 000706 int regRight = 0; 000707 u8 opx = op; 000708 int addrCmp = 0; 000709 int addrDone = sqlite3VdbeMakeLabel(pParse); 000710 int isCommuted = ExprHasProperty(pExpr,EP_Commuted); 000711 000712 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); 000713 if( pParse->nErr ) return; 000714 if( nLeft!=sqlite3ExprVectorSize(pRight) ){ 000715 sqlite3ErrorMsg(pParse, "row value misused"); 000716 return; 000717 } 000718 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE 000719 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT 000720 || pExpr->op==TK_LT || pExpr->op==TK_GT 000721 || pExpr->op==TK_LE || pExpr->op==TK_GE 000722 ); 000723 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ) 000724 || (pExpr->op==TK_ISNOT && op==TK_NE) ); 000725 assert( p5==0 || pExpr->op!=op ); 000726 assert( p5==SQLITE_NULLEQ || pExpr->op==op ); 000727 000728 if( op==TK_LE ) opx = TK_LT; 000729 if( op==TK_GE ) opx = TK_GT; 000730 if( op==TK_NE ) opx = TK_EQ; 000731 000732 regLeft = exprCodeSubselect(pParse, pLeft); 000733 regRight = exprCodeSubselect(pParse, pRight); 000734 000735 sqlite3VdbeAddOp2(v, OP_Integer, 1, dest); 000736 for(i=0; 1 /*Loop exits by "break"*/; i++){ 000737 int regFree1 = 0, regFree2 = 0; 000738 Expr *pL = 0, *pR = 0; 000739 int r1, r2; 000740 assert( i>=0 && i<nLeft ); 000741 if( addrCmp ) sqlite3VdbeJumpHere(v, addrCmp); 000742 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, ®Free1); 000743 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, ®Free2); 000744 addrCmp = sqlite3VdbeCurrentAddr(v); 000745 codeCompare(pParse, pL, pR, opx, r1, r2, addrDone, p5, isCommuted); 000746 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 000747 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 000748 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 000749 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 000750 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 000751 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 000752 sqlite3ReleaseTempReg(pParse, regFree1); 000753 sqlite3ReleaseTempReg(pParse, regFree2); 000754 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){ 000755 addrCmp = sqlite3VdbeAddOp0(v, OP_ElseEq); 000756 testcase(opx==TK_LT); VdbeCoverageIf(v,opx==TK_LT); 000757 testcase(opx==TK_GT); VdbeCoverageIf(v,opx==TK_GT); 000758 } 000759 if( p5==SQLITE_NULLEQ ){ 000760 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest); 000761 }else{ 000762 sqlite3VdbeAddOp3(v, OP_ZeroOrNull, r1, dest, r2); 000763 } 000764 if( i==nLeft-1 ){ 000765 break; 000766 } 000767 if( opx==TK_EQ ){ 000768 sqlite3VdbeAddOp2(v, OP_NotNull, dest, addrDone); VdbeCoverage(v); 000769 }else{ 000770 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE ); 000771 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrDone); 000772 if( i==nLeft-2 ) opx = op; 000773 } 000774 } 000775 sqlite3VdbeJumpHere(v, addrCmp); 000776 sqlite3VdbeResolveLabel(v, addrDone); 000777 if( op==TK_NE ){ 000778 sqlite3VdbeAddOp2(v, OP_Not, dest, dest); 000779 } 000780 } 000781 000782 #if SQLITE_MAX_EXPR_DEPTH>0 000783 /* 000784 ** Check that argument nHeight is less than or equal to the maximum 000785 ** expression depth allowed. If it is not, leave an error message in 000786 ** pParse. 000787 */ 000788 int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ 000789 int rc = SQLITE_OK; 000790 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 000791 if( nHeight>mxHeight ){ 000792 sqlite3ErrorMsg(pParse, 000793 "Expression tree is too large (maximum depth %d)", mxHeight 000794 ); 000795 rc = SQLITE_ERROR; 000796 } 000797 return rc; 000798 } 000799 000800 /* The following three functions, heightOfExpr(), heightOfExprList() 000801 ** and heightOfSelect(), are used to determine the maximum height 000802 ** of any expression tree referenced by the structure passed as the 000803 ** first argument. 000804 ** 000805 ** If this maximum height is greater than the current value pointed 000806 ** to by pnHeight, the second parameter, then set *pnHeight to that 000807 ** value. 000808 */ 000809 static void heightOfExpr(const Expr *p, int *pnHeight){ 000810 if( p ){ 000811 if( p->nHeight>*pnHeight ){ 000812 *pnHeight = p->nHeight; 000813 } 000814 } 000815 } 000816 static void heightOfExprList(const ExprList *p, int *pnHeight){ 000817 if( p ){ 000818 int i; 000819 for(i=0; i<p->nExpr; i++){ 000820 heightOfExpr(p->a[i].pExpr, pnHeight); 000821 } 000822 } 000823 } 000824 static void heightOfSelect(const Select *pSelect, int *pnHeight){ 000825 const Select *p; 000826 for(p=pSelect; p; p=p->pPrior){ 000827 heightOfExpr(p->pWhere, pnHeight); 000828 heightOfExpr(p->pHaving, pnHeight); 000829 heightOfExpr(p->pLimit, pnHeight); 000830 heightOfExprList(p->pEList, pnHeight); 000831 heightOfExprList(p->pGroupBy, pnHeight); 000832 heightOfExprList(p->pOrderBy, pnHeight); 000833 } 000834 } 000835 000836 /* 000837 ** Set the Expr.nHeight variable in the structure passed as an 000838 ** argument. An expression with no children, Expr.pList or 000839 ** Expr.pSelect member has a height of 1. Any other expression 000840 ** has a height equal to the maximum height of any other 000841 ** referenced Expr plus one. 000842 ** 000843 ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, 000844 ** if appropriate. 000845 */ 000846 static void exprSetHeight(Expr *p){ 000847 int nHeight = p->pLeft ? p->pLeft->nHeight : 0; 000848 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){ 000849 nHeight = p->pRight->nHeight; 000850 } 000851 if( ExprUseXSelect(p) ){ 000852 heightOfSelect(p->x.pSelect, &nHeight); 000853 }else if( p->x.pList ){ 000854 heightOfExprList(p->x.pList, &nHeight); 000855 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 000856 } 000857 p->nHeight = nHeight + 1; 000858 } 000859 000860 /* 000861 ** Set the Expr.nHeight variable using the exprSetHeight() function. If 000862 ** the height is greater than the maximum allowed expression depth, 000863 ** leave an error in pParse. 000864 ** 000865 ** Also propagate all EP_Propagate flags from the Expr.x.pList into 000866 ** Expr.flags. 000867 */ 000868 void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 000869 if( pParse->nErr ) return; 000870 exprSetHeight(p); 000871 sqlite3ExprCheckHeight(pParse, p->nHeight); 000872 } 000873 000874 /* 000875 ** Return the maximum height of any expression tree referenced 000876 ** by the select statement passed as an argument. 000877 */ 000878 int sqlite3SelectExprHeight(const Select *p){ 000879 int nHeight = 0; 000880 heightOfSelect(p, &nHeight); 000881 return nHeight; 000882 } 000883 #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ 000884 /* 000885 ** Propagate all EP_Propagate flags from the Expr.x.pList into 000886 ** Expr.flags. 000887 */ 000888 void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 000889 if( pParse->nErr ) return; 000890 if( p && ExprUseXList(p) && p->x.pList ){ 000891 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 000892 } 000893 } 000894 #define exprSetHeight(y) 000895 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 000896 000897 /* 000898 ** Set the error offset for an Expr node, if possible. 000899 */ 000900 void sqlite3ExprSetErrorOffset(Expr *pExpr, int iOfst){ 000901 if( pExpr==0 ) return; 000902 if( NEVER(ExprUseWJoin(pExpr)) ) return; 000903 pExpr->w.iOfst = iOfst; 000904 } 000905 000906 /* 000907 ** This routine is the core allocator for Expr nodes. 000908 ** 000909 ** Construct a new expression node and return a pointer to it. Memory 000910 ** for this node and for the pToken argument is a single allocation 000911 ** obtained from sqlite3DbMalloc(). The calling function 000912 ** is responsible for making sure the node eventually gets freed. 000913 ** 000914 ** If dequote is true, then the token (if it exists) is dequoted. 000915 ** If dequote is false, no dequoting is performed. The deQuote 000916 ** parameter is ignored if pToken is NULL or if the token does not 000917 ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 000918 ** then the EP_DblQuoted flag is set on the expression node. 000919 ** 000920 ** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to 000921 ** a string that can be translated into a 32-bit integer, then the token is 000922 ** not stored in u.zToken. Instead, the integer values is written 000923 ** into u.iValue and the EP_IntValue flag is set. No extra storage 000924 ** is allocated to hold the integer text and the dequote flag is ignored. 000925 ** See also tag-20240227-b. 000926 */ 000927 Expr *sqlite3ExprAlloc( 000928 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */ 000929 int op, /* Expression opcode */ 000930 const Token *pToken, /* Token argument. Might be NULL */ 000931 int dequote /* True to dequote */ 000932 ){ 000933 Expr *pNew; 000934 int nExtra = 0; 000935 int iValue = 0; 000936 000937 assert( db!=0 ); 000938 if( pToken ){ 000939 if( op!=TK_INTEGER || pToken->z==0 000940 || sqlite3GetInt32(pToken->z, &iValue)==0 ){ 000941 nExtra = pToken->n+1; /* tag-20240227-a */ 000942 assert( iValue>=0 ); 000943 } 000944 } 000945 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); 000946 if( pNew ){ 000947 memset(pNew, 0, sizeof(Expr)); 000948 pNew->op = (u8)op; 000949 pNew->iAgg = -1; 000950 if( pToken ){ 000951 if( nExtra==0 ){ 000952 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse); 000953 pNew->u.iValue = iValue; 000954 }else{ 000955 pNew->u.zToken = (char*)&pNew[1]; 000956 assert( pToken->z!=0 || pToken->n==0 ); 000957 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); 000958 pNew->u.zToken[pToken->n] = 0; 000959 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ 000960 sqlite3DequoteExpr(pNew); 000961 } 000962 } 000963 } 000964 #if SQLITE_MAX_EXPR_DEPTH>0 000965 pNew->nHeight = 1; 000966 #endif 000967 } 000968 return pNew; 000969 } 000970 000971 /* 000972 ** Allocate a new expression node from a zero-terminated token that has 000973 ** already been dequoted. 000974 */ 000975 Expr *sqlite3Expr( 000976 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 000977 int op, /* Expression opcode */ 000978 const char *zToken /* Token argument. Might be NULL */ 000979 ){ 000980 Token x; 000981 x.z = zToken; 000982 x.n = sqlite3Strlen30(zToken); 000983 return sqlite3ExprAlloc(db, op, &x, 0); 000984 } 000985 000986 /* 000987 ** Attach subtrees pLeft and pRight to the Expr node pRoot. 000988 ** 000989 ** If pRoot==NULL that means that a memory allocation error has occurred. 000990 ** In that case, delete the subtrees pLeft and pRight. 000991 */ 000992 void sqlite3ExprAttachSubtrees( 000993 sqlite3 *db, 000994 Expr *pRoot, 000995 Expr *pLeft, 000996 Expr *pRight 000997 ){ 000998 if( pRoot==0 ){ 000999 assert( db->mallocFailed ); 001000 sqlite3ExprDelete(db, pLeft); 001001 sqlite3ExprDelete(db, pRight); 001002 }else{ 001003 assert( ExprUseXList(pRoot) ); 001004 assert( pRoot->x.pSelect==0 ); 001005 if( pRight ){ 001006 pRoot->pRight = pRight; 001007 pRoot->flags |= EP_Propagate & pRight->flags; 001008 #if SQLITE_MAX_EXPR_DEPTH>0 001009 pRoot->nHeight = pRight->nHeight+1; 001010 }else{ 001011 pRoot->nHeight = 1; 001012 #endif 001013 } 001014 if( pLeft ){ 001015 pRoot->pLeft = pLeft; 001016 pRoot->flags |= EP_Propagate & pLeft->flags; 001017 #if SQLITE_MAX_EXPR_DEPTH>0 001018 if( pLeft->nHeight>=pRoot->nHeight ){ 001019 pRoot->nHeight = pLeft->nHeight+1; 001020 } 001021 #endif 001022 } 001023 } 001024 } 001025 001026 /* 001027 ** Allocate an Expr node which joins as many as two subtrees. 001028 ** 001029 ** One or both of the subtrees can be NULL. Return a pointer to the new 001030 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 001031 ** free the subtrees and return NULL. 001032 */ 001033 Expr *sqlite3PExpr( 001034 Parse *pParse, /* Parsing context */ 001035 int op, /* Expression opcode */ 001036 Expr *pLeft, /* Left operand */ 001037 Expr *pRight /* Right operand */ 001038 ){ 001039 Expr *p; 001040 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)); 001041 if( p ){ 001042 memset(p, 0, sizeof(Expr)); 001043 p->op = op & 0xff; 001044 p->iAgg = -1; 001045 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 001046 sqlite3ExprCheckHeight(pParse, p->nHeight); 001047 }else{ 001048 sqlite3ExprDelete(pParse->db, pLeft); 001049 sqlite3ExprDelete(pParse->db, pRight); 001050 } 001051 return p; 001052 } 001053 001054 /* 001055 ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due 001056 ** do a memory allocation failure) then delete the pSelect object. 001057 */ 001058 void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ 001059 if( pExpr ){ 001060 pExpr->x.pSelect = pSelect; 001061 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); 001062 sqlite3ExprSetHeightAndFlags(pParse, pExpr); 001063 }else{ 001064 assert( pParse->db->mallocFailed ); 001065 sqlite3SelectDelete(pParse->db, pSelect); 001066 } 001067 } 001068 001069 /* 001070 ** Expression list pEList is a list of vector values. This function 001071 ** converts the contents of pEList to a VALUES(...) Select statement 001072 ** returning 1 row for each element of the list. For example, the 001073 ** expression list: 001074 ** 001075 ** ( (1,2), (3,4) (5,6) ) 001076 ** 001077 ** is translated to the equivalent of: 001078 ** 001079 ** VALUES(1,2), (3,4), (5,6) 001080 ** 001081 ** Each of the vector values in pEList must contain exactly nElem terms. 001082 ** If a list element that is not a vector or does not contain nElem terms, 001083 ** an error message is left in pParse. 001084 ** 001085 ** This is used as part of processing IN(...) expressions with a list 001086 ** of vectors on the RHS. e.g. "... IN ((1,2), (3,4), (5,6))". 001087 */ 001088 Select *sqlite3ExprListToValues(Parse *pParse, int nElem, ExprList *pEList){ 001089 int ii; 001090 Select *pRet = 0; 001091 assert( nElem>1 ); 001092 for(ii=0; ii<pEList->nExpr; ii++){ 001093 Select *pSel; 001094 Expr *pExpr = pEList->a[ii].pExpr; 001095 int nExprElem; 001096 if( pExpr->op==TK_VECTOR ){ 001097 assert( ExprUseXList(pExpr) ); 001098 nExprElem = pExpr->x.pList->nExpr; 001099 }else{ 001100 nExprElem = 1; 001101 } 001102 if( nExprElem!=nElem ){ 001103 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d", 001104 nExprElem, nExprElem>1?"s":"", nElem 001105 ); 001106 break; 001107 } 001108 assert( ExprUseXList(pExpr) ); 001109 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0); 001110 pExpr->x.pList = 0; 001111 if( pSel ){ 001112 if( pRet ){ 001113 pSel->op = TK_ALL; 001114 pSel->pPrior = pRet; 001115 } 001116 pRet = pSel; 001117 } 001118 } 001119 001120 if( pRet && pRet->pPrior ){ 001121 pRet->selFlags |= SF_MultiValue; 001122 } 001123 sqlite3ExprListDelete(pParse->db, pEList); 001124 return pRet; 001125 } 001126 001127 /* 001128 ** Join two expressions using an AND operator. If either expression is 001129 ** NULL, then just return the other expression. 001130 ** 001131 ** If one side or the other of the AND is known to be false, and neither side 001132 ** is part of an ON clause, then instead of returning an AND expression, 001133 ** just return a constant expression with a value of false. 001134 */ 001135 Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){ 001136 sqlite3 *db = pParse->db; 001137 if( pLeft==0 ){ 001138 return pRight; 001139 }else if( pRight==0 ){ 001140 return pLeft; 001141 }else{ 001142 u32 f = pLeft->flags | pRight->flags; 001143 if( (f&(EP_OuterON|EP_InnerON|EP_IsFalse))==EP_IsFalse 001144 && !IN_RENAME_OBJECT 001145 ){ 001146 sqlite3ExprDeferredDelete(pParse, pLeft); 001147 sqlite3ExprDeferredDelete(pParse, pRight); 001148 return sqlite3Expr(db, TK_INTEGER, "0"); 001149 }else{ 001150 return sqlite3PExpr(pParse, TK_AND, pLeft, pRight); 001151 } 001152 } 001153 } 001154 001155 /* 001156 ** Construct a new expression node for a function with multiple 001157 ** arguments. 001158 */ 001159 Expr *sqlite3ExprFunction( 001160 Parse *pParse, /* Parsing context */ 001161 ExprList *pList, /* Argument list */ 001162 const Token *pToken, /* Name of the function */ 001163 int eDistinct /* SF_Distinct or SF_ALL or 0 */ 001164 ){ 001165 Expr *pNew; 001166 sqlite3 *db = pParse->db; 001167 assert( pToken ); 001168 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 001169 if( pNew==0 ){ 001170 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 001171 return 0; 001172 } 001173 assert( !ExprHasProperty(pNew, EP_InnerON|EP_OuterON) ); 001174 pNew->w.iOfst = (int)(pToken->z - pParse->zTail); 001175 if( pList 001176 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] 001177 && !pParse->nested 001178 ){ 001179 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken); 001180 } 001181 pNew->x.pList = pList; 001182 ExprSetProperty(pNew, EP_HasFunc); 001183 assert( ExprUseXList(pNew) ); 001184 sqlite3ExprSetHeightAndFlags(pParse, pNew); 001185 if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct); 001186 return pNew; 001187 } 001188 001189 /* 001190 ** Report an error when attempting to use an ORDER BY clause within 001191 ** the arguments of a non-aggregate function. 001192 */ 001193 void sqlite3ExprOrderByAggregateError(Parse *pParse, Expr *p){ 001194 sqlite3ErrorMsg(pParse, 001195 "ORDER BY may not be used with non-aggregate %#T()", p 001196 ); 001197 } 001198 001199 /* 001200 ** Attach an ORDER BY clause to a function call. 001201 ** 001202 ** functionname( arguments ORDER BY sortlist ) 001203 ** \_____________________/ \______/ 001204 ** pExpr pOrderBy 001205 ** 001206 ** The ORDER BY clause is inserted into a new Expr node of type TK_ORDER 001207 ** and added to the Expr.pLeft field of the parent TK_FUNCTION node. 001208 */ 001209 void sqlite3ExprAddFunctionOrderBy( 001210 Parse *pParse, /* Parsing context */ 001211 Expr *pExpr, /* The function call to which ORDER BY is to be added */ 001212 ExprList *pOrderBy /* The ORDER BY clause to add */ 001213 ){ 001214 Expr *pOB; 001215 sqlite3 *db = pParse->db; 001216 if( NEVER(pOrderBy==0) ){ 001217 assert( db->mallocFailed ); 001218 return; 001219 } 001220 if( pExpr==0 ){ 001221 assert( db->mallocFailed ); 001222 sqlite3ExprListDelete(db, pOrderBy); 001223 return; 001224 } 001225 assert( pExpr->op==TK_FUNCTION ); 001226 assert( pExpr->pLeft==0 ); 001227 assert( ExprUseXList(pExpr) ); 001228 if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0) ){ 001229 /* Ignore ORDER BY on zero-argument aggregates */ 001230 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pOrderBy); 001231 return; 001232 } 001233 if( IsWindowFunc(pExpr) ){ 001234 sqlite3ExprOrderByAggregateError(pParse, pExpr); 001235 sqlite3ExprListDelete(db, pOrderBy); 001236 return; 001237 } 001238 001239 pOB = sqlite3ExprAlloc(db, TK_ORDER, 0, 0); 001240 if( pOB==0 ){ 001241 sqlite3ExprListDelete(db, pOrderBy); 001242 return; 001243 } 001244 pOB->x.pList = pOrderBy; 001245 assert( ExprUseXList(pOB) ); 001246 pExpr->pLeft = pOB; 001247 ExprSetProperty(pOB, EP_FullSize); 001248 } 001249 001250 /* 001251 ** Check to see if a function is usable according to current access 001252 ** rules: 001253 ** 001254 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL 001255 ** 001256 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from 001257 ** top-level SQL 001258 ** 001259 ** If the function is not usable, create an error. 001260 */ 001261 void sqlite3ExprFunctionUsable( 001262 Parse *pParse, /* Parsing and code generating context */ 001263 const Expr *pExpr, /* The function invocation */ 001264 const FuncDef *pDef /* The function being invoked */ 001265 ){ 001266 assert( !IN_RENAME_OBJECT ); 001267 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 ); 001268 if( ExprHasProperty(pExpr, EP_FromDDL) ){ 001269 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0 001270 || (pParse->db->flags & SQLITE_TrustedSchema)==0 001271 ){ 001272 /* Functions prohibited in triggers and views if: 001273 ** (1) tagged with SQLITE_DIRECTONLY 001274 ** (2) not tagged with SQLITE_INNOCUOUS (which means it 001275 ** is tagged with SQLITE_FUNC_UNSAFE) and 001276 ** SQLITE_DBCONFIG_TRUSTED_SCHEMA is off (meaning 001277 ** that the schema is possibly tainted). 001278 */ 001279 sqlite3ErrorMsg(pParse, "unsafe use of %#T()", pExpr); 001280 } 001281 } 001282 } 001283 001284 /* 001285 ** Assign a variable number to an expression that encodes a wildcard 001286 ** in the original SQL statement. 001287 ** 001288 ** Wildcards consisting of a single "?" are assigned the next sequential 001289 ** variable number. 001290 ** 001291 ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 001292 ** sure "nnn" is not too big to avoid a denial of service attack when 001293 ** the SQL statement comes from an external source. 001294 ** 001295 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number 001296 ** as the previous instance of the same wildcard. Or if this is the first 001297 ** instance of the wildcard, the next sequential variable number is 001298 ** assigned. 001299 */ 001300 void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){ 001301 sqlite3 *db = pParse->db; 001302 const char *z; 001303 ynVar x; 001304 001305 if( pExpr==0 ) return; 001306 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); 001307 z = pExpr->u.zToken; 001308 assert( z!=0 ); 001309 assert( z[0]!=0 ); 001310 assert( n==(u32)sqlite3Strlen30(z) ); 001311 if( z[1]==0 ){ 001312 /* Wildcard of the form "?". Assign the next variable number */ 001313 assert( z[0]=='?' ); 001314 x = (ynVar)(++pParse->nVar); 001315 }else{ 001316 int doAdd = 0; 001317 if( z[0]=='?' ){ 001318 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 001319 ** use it as the variable number */ 001320 i64 i; 001321 int bOk; 001322 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/ 001323 i = z[1]-'0'; /* The common case of ?N for a single digit N */ 001324 bOk = 1; 001325 }else{ 001326 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); 001327 } 001328 testcase( i==0 ); 001329 testcase( i==1 ); 001330 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 001331 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 001332 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 001333 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 001334 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 001335 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); 001336 return; 001337 } 001338 x = (ynVar)i; 001339 if( x>pParse->nVar ){ 001340 pParse->nVar = (int)x; 001341 doAdd = 1; 001342 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){ 001343 doAdd = 1; 001344 } 001345 }else{ 001346 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable 001347 ** number as the prior appearance of the same name, or if the name 001348 ** has never appeared before, reuse the same variable number 001349 */ 001350 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n); 001351 if( x==0 ){ 001352 x = (ynVar)(++pParse->nVar); 001353 doAdd = 1; 001354 } 001355 } 001356 if( doAdd ){ 001357 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x); 001358 } 001359 } 001360 pExpr->iColumn = x; 001361 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 001362 sqlite3ErrorMsg(pParse, "too many SQL variables"); 001363 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr); 001364 } 001365 } 001366 001367 /* 001368 ** Recursively delete an expression tree. 001369 */ 001370 static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ 001371 assert( p!=0 ); 001372 assert( db!=0 ); 001373 exprDeleteRestart: 001374 assert( !ExprUseUValue(p) || p->u.iValue>=0 ); 001375 assert( !ExprUseYWin(p) || !ExprUseYSub(p) ); 001376 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed ); 001377 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) ); 001378 #ifdef SQLITE_DEBUG 001379 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ 001380 assert( p->pLeft==0 ); 001381 assert( p->pRight==0 ); 001382 assert( !ExprUseXSelect(p) || p->x.pSelect==0 ); 001383 assert( !ExprUseXList(p) || p->x.pList==0 ); 001384 } 001385 #endif 001386 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){ 001387 /* The Expr.x union is never used at the same time as Expr.pRight */ 001388 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 ); 001389 if( p->pRight ){ 001390 assert( !ExprHasProperty(p, EP_WinFunc) ); 001391 sqlite3ExprDeleteNN(db, p->pRight); 001392 }else if( ExprUseXSelect(p) ){ 001393 assert( !ExprHasProperty(p, EP_WinFunc) ); 001394 sqlite3SelectDelete(db, p->x.pSelect); 001395 }else{ 001396 sqlite3ExprListDelete(db, p->x.pList); 001397 #ifndef SQLITE_OMIT_WINDOWFUNC 001398 if( ExprHasProperty(p, EP_WinFunc) ){ 001399 sqlite3WindowDelete(db, p->y.pWin); 001400 } 001401 #endif 001402 } 001403 if( p->pLeft && p->op!=TK_SELECT_COLUMN ){ 001404 Expr *pLeft = p->pLeft; 001405 if( !ExprHasProperty(p, EP_Static) 001406 && !ExprHasProperty(pLeft, EP_Static) 001407 ){ 001408 /* Avoid unnecessary recursion on unary operators */ 001409 sqlite3DbNNFreeNN(db, p); 001410 p = pLeft; 001411 goto exprDeleteRestart; 001412 }else{ 001413 sqlite3ExprDeleteNN(db, pLeft); 001414 } 001415 } 001416 } 001417 if( !ExprHasProperty(p, EP_Static) ){ 001418 sqlite3DbNNFreeNN(db, p); 001419 } 001420 } 001421 void sqlite3ExprDelete(sqlite3 *db, Expr *p){ 001422 if( p ) sqlite3ExprDeleteNN(db, p); 001423 } 001424 void sqlite3ExprDeleteGeneric(sqlite3 *db, void *p){ 001425 if( ALWAYS(p) ) sqlite3ExprDeleteNN(db, (Expr*)p); 001426 } 001427 001428 /* 001429 ** Clear both elements of an OnOrUsing object 001430 */ 001431 void sqlite3ClearOnOrUsing(sqlite3 *db, OnOrUsing *p){ 001432 if( p==0 ){ 001433 /* Nothing to clear */ 001434 }else if( p->pOn ){ 001435 sqlite3ExprDeleteNN(db, p->pOn); 001436 }else if( p->pUsing ){ 001437 sqlite3IdListDelete(db, p->pUsing); 001438 } 001439 } 001440 001441 /* 001442 ** Arrange to cause pExpr to be deleted when the pParse is deleted. 001443 ** This is similar to sqlite3ExprDelete() except that the delete is 001444 ** deferred until the pParse is deleted. 001445 ** 001446 ** The pExpr might be deleted immediately on an OOM error. 001447 ** 001448 ** Return 0 if the delete was successfully deferred. Return non-zero 001449 ** if the delete happened immediately because of an OOM. 001450 */ 001451 int sqlite3ExprDeferredDelete(Parse *pParse, Expr *pExpr){ 001452 return 0==sqlite3ParserAddCleanup(pParse, sqlite3ExprDeleteGeneric, pExpr); 001453 } 001454 001455 /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the 001456 ** expression. 001457 */ 001458 void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){ 001459 if( p ){ 001460 if( IN_RENAME_OBJECT ){ 001461 sqlite3RenameExprUnmap(pParse, p); 001462 } 001463 sqlite3ExprDeleteNN(pParse->db, p); 001464 } 001465 } 001466 001467 /* 001468 ** Return the number of bytes allocated for the expression structure 001469 ** passed as the first argument. This is always one of EXPR_FULLSIZE, 001470 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. 001471 */ 001472 static int exprStructSize(const Expr *p){ 001473 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; 001474 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; 001475 return EXPR_FULLSIZE; 001476 } 001477 001478 /* 001479 ** The dupedExpr*Size() routines each return the number of bytes required 001480 ** to store a copy of an expression or expression tree. They differ in 001481 ** how much of the tree is measured. 001482 ** 001483 ** dupedExprStructSize() Size of only the Expr structure 001484 ** dupedExprNodeSize() Size of Expr + space for token 001485 ** dupedExprSize() Expr + token + subtree components 001486 ** 001487 *************************************************************************** 001488 ** 001489 ** The dupedExprStructSize() function returns two values OR-ed together: 001490 ** (1) the space required for a copy of the Expr structure only and 001491 ** (2) the EP_xxx flags that indicate what the structure size should be. 001492 ** The return values is always one of: 001493 ** 001494 ** EXPR_FULLSIZE 001495 ** EXPR_REDUCEDSIZE | EP_Reduced 001496 ** EXPR_TOKENONLYSIZE | EP_TokenOnly 001497 ** 001498 ** The size of the structure can be found by masking the return value 001499 ** of this routine with 0xfff. The flags can be found by masking the 001500 ** return value with EP_Reduced|EP_TokenOnly. 001501 ** 001502 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size 001503 ** (unreduced) Expr objects as they or originally constructed by the parser. 001504 ** During expression analysis, extra information is computed and moved into 001505 ** later parts of the Expr object and that extra information might get chopped 001506 ** off if the expression is reduced. Note also that it does not work to 001507 ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal 001508 ** to reduce a pristine expression tree from the parser. The implementation 001509 ** of dupedExprStructSize() contain multiple assert() statements that attempt 001510 ** to enforce this constraint. 001511 */ 001512 static int dupedExprStructSize(const Expr *p, int flags){ 001513 int nSize; 001514 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ 001515 assert( EXPR_FULLSIZE<=0xfff ); 001516 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); 001517 if( 0==flags || ExprHasProperty(p, EP_FullSize) ){ 001518 nSize = EXPR_FULLSIZE; 001519 }else{ 001520 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); 001521 assert( !ExprHasProperty(p, EP_OuterON) ); 001522 assert( !ExprHasVVAProperty(p, EP_NoReduce) ); 001523 if( p->pLeft || p->x.pList ){ 001524 nSize = EXPR_REDUCEDSIZE | EP_Reduced; 001525 }else{ 001526 assert( p->pRight==0 ); 001527 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; 001528 } 001529 } 001530 return nSize; 001531 } 001532 001533 /* 001534 ** This function returns the space in bytes required to store the copy 001535 ** of the Expr structure and a copy of the Expr.u.zToken string (if that 001536 ** string is defined.) 001537 */ 001538 static int dupedExprNodeSize(const Expr *p, int flags){ 001539 int nByte = dupedExprStructSize(p, flags) & 0xfff; 001540 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 001541 nByte += sqlite3Strlen30NN(p->u.zToken)+1; 001542 } 001543 return ROUND8(nByte); 001544 } 001545 001546 /* 001547 ** Return the number of bytes required to create a duplicate of the 001548 ** expression passed as the first argument. 001549 ** 001550 ** The value returned includes space to create a copy of the Expr struct 001551 ** itself and the buffer referred to by Expr.u.zToken, if any. 001552 ** 001553 ** The return value includes space to duplicate all Expr nodes in the 001554 ** tree formed by Expr.pLeft and Expr.pRight, but not any other 001555 ** substructure such as Expr.x.pList, Expr.x.pSelect, and Expr.y.pWin. 001556 */ 001557 static int dupedExprSize(const Expr *p){ 001558 int nByte; 001559 assert( p!=0 ); 001560 nByte = dupedExprNodeSize(p, EXPRDUP_REDUCE); 001561 if( p->pLeft ) nByte += dupedExprSize(p->pLeft); 001562 if( p->pRight ) nByte += dupedExprSize(p->pRight); 001563 assert( nByte==ROUND8(nByte) ); 001564 return nByte; 001565 } 001566 001567 /* 001568 ** An EdupBuf is a memory allocation used to stored multiple Expr objects 001569 ** together with their Expr.zToken content. This is used to help implement 001570 ** compression while doing sqlite3ExprDup(). The top-level Expr does the 001571 ** allocation for itself and many of its decendents, then passes an instance 001572 ** of the structure down into exprDup() so that they decendents can have 001573 ** access to that memory. 001574 */ 001575 typedef struct EdupBuf EdupBuf; 001576 struct EdupBuf { 001577 u8 *zAlloc; /* Memory space available for storage */ 001578 #ifdef SQLITE_DEBUG 001579 u8 *zEnd; /* First byte past the end of memory */ 001580 #endif 001581 }; 001582 001583 /* 001584 ** This function is similar to sqlite3ExprDup(), except that if pEdupBuf 001585 ** is not NULL then it points to memory that can be used to store a copy 001586 ** of the input Expr p together with its p->u.zToken (if any). pEdupBuf 001587 ** is updated with the new buffer tail prior to returning. 001588 */ 001589 static Expr *exprDup( 001590 sqlite3 *db, /* Database connection (for memory allocation) */ 001591 const Expr *p, /* Expr tree to be duplicated */ 001592 int dupFlags, /* EXPRDUP_REDUCE for compression. 0 if not */ 001593 EdupBuf *pEdupBuf /* Preallocated storage space, or NULL */ 001594 ){ 001595 Expr *pNew; /* Value to return */ 001596 EdupBuf sEdupBuf; /* Memory space from which to build Expr object */ 001597 u32 staticFlag; /* EP_Static if space not obtained from malloc */ 001598 int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */ 001599 001600 assert( db!=0 ); 001601 assert( p ); 001602 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); 001603 assert( pEdupBuf==0 || dupFlags==EXPRDUP_REDUCE ); 001604 001605 /* Figure out where to write the new Expr structure. */ 001606 if( pEdupBuf ){ 001607 sEdupBuf.zAlloc = pEdupBuf->zAlloc; 001608 #ifdef SQLITE_DEBUG 001609 sEdupBuf.zEnd = pEdupBuf->zEnd; 001610 #endif 001611 staticFlag = EP_Static; 001612 assert( sEdupBuf.zAlloc!=0 ); 001613 assert( dupFlags==EXPRDUP_REDUCE ); 001614 }else{ 001615 int nAlloc; 001616 if( dupFlags ){ 001617 nAlloc = dupedExprSize(p); 001618 }else if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 001619 nToken = sqlite3Strlen30NN(p->u.zToken)+1; 001620 nAlloc = ROUND8(EXPR_FULLSIZE + nToken); 001621 }else{ 001622 nToken = 0; 001623 nAlloc = ROUND8(EXPR_FULLSIZE); 001624 } 001625 assert( nAlloc==ROUND8(nAlloc) ); 001626 sEdupBuf.zAlloc = sqlite3DbMallocRawNN(db, nAlloc); 001627 #ifdef SQLITE_DEBUG 001628 sEdupBuf.zEnd = sEdupBuf.zAlloc ? sEdupBuf.zAlloc+nAlloc : 0; 001629 #endif 001630 001631 staticFlag = 0; 001632 } 001633 pNew = (Expr *)sEdupBuf.zAlloc; 001634 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); 001635 001636 if( pNew ){ 001637 /* Set nNewSize to the size allocated for the structure pointed to 001638 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or 001639 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed 001640 ** by the copy of the p->u.zToken string (if any). 001641 */ 001642 const unsigned nStructSize = dupedExprStructSize(p, dupFlags); 001643 int nNewSize = nStructSize & 0xfff; 001644 if( nToken<0 ){ 001645 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 001646 nToken = sqlite3Strlen30(p->u.zToken) + 1; 001647 }else{ 001648 nToken = 0; 001649 } 001650 } 001651 if( dupFlags ){ 001652 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= nNewSize+nToken ); 001653 assert( ExprHasProperty(p, EP_Reduced)==0 ); 001654 memcpy(sEdupBuf.zAlloc, p, nNewSize); 001655 }else{ 001656 u32 nSize = (u32)exprStructSize(p); 001657 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= 001658 (int)EXPR_FULLSIZE+nToken ); 001659 memcpy(sEdupBuf.zAlloc, p, nSize); 001660 if( nSize<EXPR_FULLSIZE ){ 001661 memset(&sEdupBuf.zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 001662 } 001663 nNewSize = EXPR_FULLSIZE; 001664 } 001665 001666 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 001667 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static); 001668 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 001669 pNew->flags |= staticFlag; 001670 ExprClearVVAProperties(pNew); 001671 if( dupFlags ){ 001672 ExprSetVVAProperty(pNew, EP_Immutable); 001673 } 001674 001675 /* Copy the p->u.zToken string, if any. */ 001676 assert( nToken>=0 ); 001677 if( nToken>0 ){ 001678 char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize]; 001679 memcpy(zToken, p->u.zToken, nToken); 001680 nNewSize += nToken; 001681 } 001682 sEdupBuf.zAlloc += ROUND8(nNewSize); 001683 001684 if( ((p->flags|pNew->flags)&(EP_TokenOnly|EP_Leaf))==0 ){ 001685 001686 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ 001687 if( ExprUseXSelect(p) ){ 001688 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); 001689 }else{ 001690 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, 001691 p->op!=TK_ORDER ? dupFlags : 0); 001692 } 001693 001694 #ifndef SQLITE_OMIT_WINDOWFUNC 001695 if( ExprHasProperty(p, EP_WinFunc) ){ 001696 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin); 001697 assert( ExprHasProperty(pNew, EP_WinFunc) ); 001698 } 001699 #endif /* SQLITE_OMIT_WINDOWFUNC */ 001700 001701 /* Fill in pNew->pLeft and pNew->pRight. */ 001702 if( dupFlags ){ 001703 if( p->op==TK_SELECT_COLUMN ){ 001704 pNew->pLeft = p->pLeft; 001705 assert( p->pRight==0 001706 || p->pRight==p->pLeft 001707 || ExprHasProperty(p->pLeft, EP_Subquery) ); 001708 }else{ 001709 pNew->pLeft = p->pLeft ? 001710 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &sEdupBuf) : 0; 001711 } 001712 pNew->pRight = p->pRight ? 001713 exprDup(db, p->pRight, EXPRDUP_REDUCE, &sEdupBuf) : 0; 001714 }else{ 001715 if( p->op==TK_SELECT_COLUMN ){ 001716 pNew->pLeft = p->pLeft; 001717 assert( p->pRight==0 001718 || p->pRight==p->pLeft 001719 || ExprHasProperty(p->pLeft, EP_Subquery) ); 001720 }else{ 001721 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); 001722 } 001723 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); 001724 } 001725 } 001726 } 001727 if( pEdupBuf ) memcpy(pEdupBuf, &sEdupBuf, sizeof(sEdupBuf)); 001728 assert( sEdupBuf.zAlloc <= sEdupBuf.zEnd ); 001729 return pNew; 001730 } 001731 001732 /* 001733 ** Create and return a deep copy of the object passed as the second 001734 ** argument. If an OOM condition is encountered, NULL is returned 001735 ** and the db->mallocFailed flag set. 001736 */ 001737 #ifndef SQLITE_OMIT_CTE 001738 With *sqlite3WithDup(sqlite3 *db, With *p){ 001739 With *pRet = 0; 001740 if( p ){ 001741 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); 001742 pRet = sqlite3DbMallocZero(db, nByte); 001743 if( pRet ){ 001744 int i; 001745 pRet->nCte = p->nCte; 001746 for(i=0; i<p->nCte; i++){ 001747 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); 001748 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); 001749 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); 001750 pRet->a[i].eM10d = p->a[i].eM10d; 001751 } 001752 } 001753 } 001754 return pRet; 001755 } 001756 #else 001757 # define sqlite3WithDup(x,y) 0 001758 #endif 001759 001760 #ifndef SQLITE_OMIT_WINDOWFUNC 001761 /* 001762 ** The gatherSelectWindows() procedure and its helper routine 001763 ** gatherSelectWindowsCallback() are used to scan all the expressions 001764 ** an a newly duplicated SELECT statement and gather all of the Window 001765 ** objects found there, assembling them onto the linked list at Select->pWin. 001766 */ 001767 static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){ 001768 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){ 001769 Select *pSelect = pWalker->u.pSelect; 001770 Window *pWin = pExpr->y.pWin; 001771 assert( pWin ); 001772 assert( IsWindowFunc(pExpr) ); 001773 assert( pWin->ppThis==0 ); 001774 sqlite3WindowLink(pSelect, pWin); 001775 } 001776 return WRC_Continue; 001777 } 001778 static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){ 001779 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune; 001780 } 001781 static void gatherSelectWindows(Select *p){ 001782 Walker w; 001783 w.xExprCallback = gatherSelectWindowsCallback; 001784 w.xSelectCallback = gatherSelectWindowsSelectCallback; 001785 w.xSelectCallback2 = 0; 001786 w.pParse = 0; 001787 w.u.pSelect = p; 001788 sqlite3WalkSelect(&w, p); 001789 } 001790 #endif 001791 001792 001793 /* 001794 ** The following group of routines make deep copies of expressions, 001795 ** expression lists, ID lists, and select statements. The copies can 001796 ** be deleted (by being passed to their respective ...Delete() routines) 001797 ** without effecting the originals. 001798 ** 001799 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 001800 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 001801 ** by subsequent calls to sqlite*ListAppend() routines. 001802 ** 001803 ** Any tables that the SrcList might point to are not duplicated. 001804 ** 001805 ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 001806 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 001807 ** truncated version of the usual Expr structure that will be stored as 001808 ** part of the in-memory representation of the database schema. 001809 */ 001810 Expr *sqlite3ExprDup(sqlite3 *db, const Expr *p, int flags){ 001811 assert( flags==0 || flags==EXPRDUP_REDUCE ); 001812 return p ? exprDup(db, p, flags, 0) : 0; 001813 } 001814 ExprList *sqlite3ExprListDup(sqlite3 *db, const ExprList *p, int flags){ 001815 ExprList *pNew; 001816 struct ExprList_item *pItem; 001817 const struct ExprList_item *pOldItem; 001818 int i; 001819 Expr *pPriorSelectColOld = 0; 001820 Expr *pPriorSelectColNew = 0; 001821 assert( db!=0 ); 001822 if( p==0 ) return 0; 001823 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p)); 001824 if( pNew==0 ) return 0; 001825 pNew->nExpr = p->nExpr; 001826 pNew->nAlloc = p->nAlloc; 001827 pItem = pNew->a; 001828 pOldItem = p->a; 001829 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 001830 Expr *pOldExpr = pOldItem->pExpr; 001831 Expr *pNewExpr; 001832 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); 001833 if( pOldExpr 001834 && pOldExpr->op==TK_SELECT_COLUMN 001835 && (pNewExpr = pItem->pExpr)!=0 001836 ){ 001837 if( pNewExpr->pRight ){ 001838 pPriorSelectColOld = pOldExpr->pRight; 001839 pPriorSelectColNew = pNewExpr->pRight; 001840 pNewExpr->pLeft = pNewExpr->pRight; 001841 }else{ 001842 if( pOldExpr->pLeft!=pPriorSelectColOld ){ 001843 pPriorSelectColOld = pOldExpr->pLeft; 001844 pPriorSelectColNew = sqlite3ExprDup(db, pPriorSelectColOld, flags); 001845 pNewExpr->pRight = pPriorSelectColNew; 001846 } 001847 pNewExpr->pLeft = pPriorSelectColNew; 001848 } 001849 } 001850 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName); 001851 pItem->fg = pOldItem->fg; 001852 pItem->fg.done = 0; 001853 pItem->u = pOldItem->u; 001854 } 001855 return pNew; 001856 } 001857 001858 /* 001859 ** If cursors, triggers, views and subqueries are all omitted from 001860 ** the build, then none of the following routines, except for 001861 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 001862 ** called with a NULL argument. 001863 */ 001864 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 001865 || !defined(SQLITE_OMIT_SUBQUERY) 001866 SrcList *sqlite3SrcListDup(sqlite3 *db, const SrcList *p, int flags){ 001867 SrcList *pNew; 001868 int i; 001869 int nByte; 001870 assert( db!=0 ); 001871 if( p==0 ) return 0; 001872 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 001873 pNew = sqlite3DbMallocRawNN(db, nByte ); 001874 if( pNew==0 ) return 0; 001875 pNew->nSrc = pNew->nAlloc = p->nSrc; 001876 for(i=0; i<p->nSrc; i++){ 001877 SrcItem *pNewItem = &pNew->a[i]; 001878 const SrcItem *pOldItem = &p->a[i]; 001879 Table *pTab; 001880 pNewItem->fg = pOldItem->fg; 001881 if( pOldItem->fg.isSubquery ){ 001882 Subquery *pNewSubq = sqlite3DbMallocRaw(db, sizeof(Subquery)); 001883 if( pNewSubq==0 ){ 001884 assert( db->mallocFailed ); 001885 pNewItem->fg.isSubquery = 0; 001886 }else{ 001887 memcpy(pNewSubq, pOldItem->u4.pSubq, sizeof(*pNewSubq)); 001888 pNewSubq->pSelect = sqlite3SelectDup(db, pNewSubq->pSelect, flags); 001889 if( pNewSubq->pSelect==0 ){ 001890 sqlite3DbFree(db, pNewSubq); 001891 pNewSubq = 0; 001892 pNewItem->fg.isSubquery = 0; 001893 } 001894 } 001895 pNewItem->u4.pSubq = pNewSubq; 001896 }else if( pOldItem->fg.fixedSchema ){ 001897 pNewItem->u4.pSchema = pOldItem->u4.pSchema; 001898 }else{ 001899 pNewItem->u4.zDatabase = sqlite3DbStrDup(db, pOldItem->u4.zDatabase); 001900 } 001901 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 001902 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 001903 pNewItem->iCursor = pOldItem->iCursor; 001904 if( pNewItem->fg.isIndexedBy ){ 001905 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); 001906 }else if( pNewItem->fg.isTabFunc ){ 001907 pNewItem->u1.pFuncArg = 001908 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); 001909 }else{ 001910 pNewItem->u1.nRow = pOldItem->u1.nRow; 001911 } 001912 pNewItem->u2 = pOldItem->u2; 001913 if( pNewItem->fg.isCte ){ 001914 pNewItem->u2.pCteUse->nUse++; 001915 } 001916 pTab = pNewItem->pSTab = pOldItem->pSTab; 001917 if( pTab ){ 001918 pTab->nTabRef++; 001919 } 001920 if( pOldItem->fg.isUsing ){ 001921 assert( pNewItem->fg.isUsing ); 001922 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing); 001923 }else{ 001924 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags); 001925 } 001926 pNewItem->colUsed = pOldItem->colUsed; 001927 } 001928 return pNew; 001929 } 001930 IdList *sqlite3IdListDup(sqlite3 *db, const IdList *p){ 001931 IdList *pNew; 001932 int i; 001933 assert( db!=0 ); 001934 if( p==0 ) return 0; 001935 assert( p->eU4!=EU4_EXPR ); 001936 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) ); 001937 if( pNew==0 ) return 0; 001938 pNew->nId = p->nId; 001939 pNew->eU4 = p->eU4; 001940 for(i=0; i<p->nId; i++){ 001941 struct IdList_item *pNewItem = &pNew->a[i]; 001942 const struct IdList_item *pOldItem = &p->a[i]; 001943 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 001944 pNewItem->u4 = pOldItem->u4; 001945 } 001946 return pNew; 001947 } 001948 Select *sqlite3SelectDup(sqlite3 *db, const Select *pDup, int flags){ 001949 Select *pRet = 0; 001950 Select *pNext = 0; 001951 Select **pp = &pRet; 001952 const Select *p; 001953 001954 assert( db!=0 ); 001955 for(p=pDup; p; p=p->pPrior){ 001956 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); 001957 if( pNew==0 ) break; 001958 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); 001959 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); 001960 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); 001961 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); 001962 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); 001963 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); 001964 pNew->op = p->op; 001965 pNew->pNext = pNext; 001966 pNew->pPrior = 0; 001967 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); 001968 pNew->iLimit = 0; 001969 pNew->iOffset = 0; 001970 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; 001971 pNew->addrOpenEphm[0] = -1; 001972 pNew->addrOpenEphm[1] = -1; 001973 pNew->nSelectRow = p->nSelectRow; 001974 pNew->pWith = sqlite3WithDup(db, p->pWith); 001975 #ifndef SQLITE_OMIT_WINDOWFUNC 001976 pNew->pWin = 0; 001977 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn); 001978 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew); 001979 #endif 001980 pNew->selId = p->selId; 001981 if( db->mallocFailed ){ 001982 /* Any prior OOM might have left the Select object incomplete. 001983 ** Delete the whole thing rather than allow an incomplete Select 001984 ** to be used by the code generator. */ 001985 pNew->pNext = 0; 001986 sqlite3SelectDelete(db, pNew); 001987 break; 001988 } 001989 *pp = pNew; 001990 pp = &pNew->pPrior; 001991 pNext = pNew; 001992 } 001993 return pRet; 001994 } 001995 #else 001996 Select *sqlite3SelectDup(sqlite3 *db, const Select *p, int flags){ 001997 assert( p==0 ); 001998 return 0; 001999 } 002000 #endif 002001 002002 002003 /* 002004 ** Add a new element to the end of an expression list. If pList is 002005 ** initially NULL, then create a new expression list. 002006 ** 002007 ** The pList argument must be either NULL or a pointer to an ExprList 002008 ** obtained from a prior call to sqlite3ExprListAppend(). 002009 ** 002010 ** If a memory allocation error occurs, the entire list is freed and 002011 ** NULL is returned. If non-NULL is returned, then it is guaranteed 002012 ** that the new entry was successfully appended. 002013 */ 002014 static const struct ExprList_item zeroItem = {0}; 002015 SQLITE_NOINLINE ExprList *sqlite3ExprListAppendNew( 002016 sqlite3 *db, /* Database handle. Used for memory allocation */ 002017 Expr *pExpr /* Expression to be appended. Might be NULL */ 002018 ){ 002019 struct ExprList_item *pItem; 002020 ExprList *pList; 002021 002022 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 ); 002023 if( pList==0 ){ 002024 sqlite3ExprDelete(db, pExpr); 002025 return 0; 002026 } 002027 pList->nAlloc = 4; 002028 pList->nExpr = 1; 002029 pItem = &pList->a[0]; 002030 *pItem = zeroItem; 002031 pItem->pExpr = pExpr; 002032 return pList; 002033 } 002034 SQLITE_NOINLINE ExprList *sqlite3ExprListAppendGrow( 002035 sqlite3 *db, /* Database handle. Used for memory allocation */ 002036 ExprList *pList, /* List to which to append. Might be NULL */ 002037 Expr *pExpr /* Expression to be appended. Might be NULL */ 002038 ){ 002039 struct ExprList_item *pItem; 002040 ExprList *pNew; 002041 pList->nAlloc *= 2; 002042 pNew = sqlite3DbRealloc(db, pList, 002043 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0])); 002044 if( pNew==0 ){ 002045 sqlite3ExprListDelete(db, pList); 002046 sqlite3ExprDelete(db, pExpr); 002047 return 0; 002048 }else{ 002049 pList = pNew; 002050 } 002051 pItem = &pList->a[pList->nExpr++]; 002052 *pItem = zeroItem; 002053 pItem->pExpr = pExpr; 002054 return pList; 002055 } 002056 ExprList *sqlite3ExprListAppend( 002057 Parse *pParse, /* Parsing context */ 002058 ExprList *pList, /* List to which to append. Might be NULL */ 002059 Expr *pExpr /* Expression to be appended. Might be NULL */ 002060 ){ 002061 struct ExprList_item *pItem; 002062 if( pList==0 ){ 002063 return sqlite3ExprListAppendNew(pParse->db,pExpr); 002064 } 002065 if( pList->nAlloc<pList->nExpr+1 ){ 002066 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr); 002067 } 002068 pItem = &pList->a[pList->nExpr++]; 002069 *pItem = zeroItem; 002070 pItem->pExpr = pExpr; 002071 return pList; 002072 } 002073 002074 /* 002075 ** pColumns and pExpr form a vector assignment which is part of the SET 002076 ** clause of an UPDATE statement. Like this: 002077 ** 002078 ** (a,b,c) = (expr1,expr2,expr3) 002079 ** Or: (a,b,c) = (SELECT x,y,z FROM ....) 002080 ** 002081 ** For each term of the vector assignment, append new entries to the 002082 ** expression list pList. In the case of a subquery on the RHS, append 002083 ** TK_SELECT_COLUMN expressions. 002084 */ 002085 ExprList *sqlite3ExprListAppendVector( 002086 Parse *pParse, /* Parsing context */ 002087 ExprList *pList, /* List to which to append. Might be NULL */ 002088 IdList *pColumns, /* List of names of LHS of the assignment */ 002089 Expr *pExpr /* Vector expression to be appended. Might be NULL */ 002090 ){ 002091 sqlite3 *db = pParse->db; 002092 int n; 002093 int i; 002094 int iFirst = pList ? pList->nExpr : 0; 002095 /* pColumns can only be NULL due to an OOM but an OOM will cause an 002096 ** exit prior to this routine being invoked */ 002097 if( NEVER(pColumns==0) ) goto vector_append_error; 002098 if( pExpr==0 ) goto vector_append_error; 002099 002100 /* If the RHS is a vector, then we can immediately check to see that 002101 ** the size of the RHS and LHS match. But if the RHS is a SELECT, 002102 ** wildcards ("*") in the result set of the SELECT must be expanded before 002103 ** we can do the size check, so defer the size check until code generation. 002104 */ 002105 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){ 002106 sqlite3ErrorMsg(pParse, "%d columns assigned %d values", 002107 pColumns->nId, n); 002108 goto vector_append_error; 002109 } 002110 002111 for(i=0; i<pColumns->nId; i++){ 002112 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId); 002113 assert( pSubExpr!=0 || db->mallocFailed ); 002114 if( pSubExpr==0 ) continue; 002115 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr); 002116 if( pList ){ 002117 assert( pList->nExpr==iFirst+i+1 ); 002118 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName; 002119 pColumns->a[i].zName = 0; 002120 } 002121 } 002122 002123 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){ 002124 Expr *pFirst = pList->a[iFirst].pExpr; 002125 assert( pFirst!=0 ); 002126 assert( pFirst->op==TK_SELECT_COLUMN ); 002127 002128 /* Store the SELECT statement in pRight so it will be deleted when 002129 ** sqlite3ExprListDelete() is called */ 002130 pFirst->pRight = pExpr; 002131 pExpr = 0; 002132 002133 /* Remember the size of the LHS in iTable so that we can check that 002134 ** the RHS and LHS sizes match during code generation. */ 002135 pFirst->iTable = pColumns->nId; 002136 } 002137 002138 vector_append_error: 002139 sqlite3ExprUnmapAndDelete(pParse, pExpr); 002140 sqlite3IdListDelete(db, pColumns); 002141 return pList; 002142 } 002143 002144 /* 002145 ** Set the sort order for the last element on the given ExprList. 002146 */ 002147 void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){ 002148 struct ExprList_item *pItem; 002149 if( p==0 ) return; 002150 assert( p->nExpr>0 ); 002151 002152 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 ); 002153 assert( iSortOrder==SQLITE_SO_UNDEFINED 002154 || iSortOrder==SQLITE_SO_ASC 002155 || iSortOrder==SQLITE_SO_DESC 002156 ); 002157 assert( eNulls==SQLITE_SO_UNDEFINED 002158 || eNulls==SQLITE_SO_ASC 002159 || eNulls==SQLITE_SO_DESC 002160 ); 002161 002162 pItem = &p->a[p->nExpr-1]; 002163 assert( pItem->fg.bNulls==0 ); 002164 if( iSortOrder==SQLITE_SO_UNDEFINED ){ 002165 iSortOrder = SQLITE_SO_ASC; 002166 } 002167 pItem->fg.sortFlags = (u8)iSortOrder; 002168 002169 if( eNulls!=SQLITE_SO_UNDEFINED ){ 002170 pItem->fg.bNulls = 1; 002171 if( iSortOrder!=eNulls ){ 002172 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL; 002173 } 002174 } 002175 } 002176 002177 /* 002178 ** Set the ExprList.a[].zEName element of the most recently added item 002179 ** on the expression list. 002180 ** 002181 ** pList might be NULL following an OOM error. But pName should never be 002182 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 002183 ** is set. 002184 */ 002185 void sqlite3ExprListSetName( 002186 Parse *pParse, /* Parsing context */ 002187 ExprList *pList, /* List to which to add the span. */ 002188 const Token *pName, /* Name to be added */ 002189 int dequote /* True to cause the name to be dequoted */ 002190 ){ 002191 assert( pList!=0 || pParse->db->mallocFailed!=0 ); 002192 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 ); 002193 if( pList ){ 002194 struct ExprList_item *pItem; 002195 assert( pList->nExpr>0 ); 002196 pItem = &pList->a[pList->nExpr-1]; 002197 assert( pItem->zEName==0 ); 002198 assert( pItem->fg.eEName==ENAME_NAME ); 002199 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); 002200 if( dequote ){ 002201 /* If dequote==0, then pName->z does not point to part of a DDL 002202 ** statement handled by the parser. And so no token need be added 002203 ** to the token-map. */ 002204 sqlite3Dequote(pItem->zEName); 002205 if( IN_RENAME_OBJECT ){ 002206 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName); 002207 } 002208 } 002209 } 002210 } 002211 002212 /* 002213 ** Set the ExprList.a[].zSpan element of the most recently added item 002214 ** on the expression list. 002215 ** 002216 ** pList might be NULL following an OOM error. But pSpan should never be 002217 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 002218 ** is set. 002219 */ 002220 void sqlite3ExprListSetSpan( 002221 Parse *pParse, /* Parsing context */ 002222 ExprList *pList, /* List to which to add the span. */ 002223 const char *zStart, /* Start of the span */ 002224 const char *zEnd /* End of the span */ 002225 ){ 002226 sqlite3 *db = pParse->db; 002227 assert( pList!=0 || db->mallocFailed!=0 ); 002228 if( pList ){ 002229 struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; 002230 assert( pList->nExpr>0 ); 002231 if( pItem->zEName==0 ){ 002232 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd); 002233 pItem->fg.eEName = ENAME_SPAN; 002234 } 002235 } 002236 } 002237 002238 /* 002239 ** If the expression list pEList contains more than iLimit elements, 002240 ** leave an error message in pParse. 002241 */ 002242 void sqlite3ExprListCheckLength( 002243 Parse *pParse, 002244 ExprList *pEList, 002245 const char *zObject 002246 ){ 002247 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 002248 testcase( pEList && pEList->nExpr==mx ); 002249 testcase( pEList && pEList->nExpr==mx+1 ); 002250 if( pEList && pEList->nExpr>mx ){ 002251 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 002252 } 002253 } 002254 002255 /* 002256 ** Delete an entire expression list. 002257 */ 002258 static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ 002259 int i = pList->nExpr; 002260 struct ExprList_item *pItem = pList->a; 002261 assert( pList->nExpr>0 ); 002262 assert( db!=0 ); 002263 do{ 002264 sqlite3ExprDelete(db, pItem->pExpr); 002265 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName); 002266 pItem++; 002267 }while( --i>0 ); 002268 sqlite3DbNNFreeNN(db, pList); 002269 } 002270 void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ 002271 if( pList ) exprListDeleteNN(db, pList); 002272 } 002273 void sqlite3ExprListDeleteGeneric(sqlite3 *db, void *pList){ 002274 if( ALWAYS(pList) ) exprListDeleteNN(db, (ExprList*)pList); 002275 } 002276 002277 /* 002278 ** Return the bitwise-OR of all Expr.flags fields in the given 002279 ** ExprList. 002280 */ 002281 u32 sqlite3ExprListFlags(const ExprList *pList){ 002282 int i; 002283 u32 m = 0; 002284 assert( pList!=0 ); 002285 for(i=0; i<pList->nExpr; i++){ 002286 Expr *pExpr = pList->a[i].pExpr; 002287 assert( pExpr!=0 ); 002288 m |= pExpr->flags; 002289 } 002290 return m; 002291 } 002292 002293 /* 002294 ** This is a SELECT-node callback for the expression walker that 002295 ** always "fails". By "fail" in this case, we mean set 002296 ** pWalker->eCode to zero and abort. 002297 ** 002298 ** This callback is used by multiple expression walkers. 002299 */ 002300 int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){ 002301 UNUSED_PARAMETER(NotUsed); 002302 pWalker->eCode = 0; 002303 return WRC_Abort; 002304 } 002305 002306 /* 002307 ** Check the input string to see if it is "true" or "false" (in any case). 002308 ** 002309 ** If the string is.... Return 002310 ** "true" EP_IsTrue 002311 ** "false" EP_IsFalse 002312 ** anything else 0 002313 */ 002314 u32 sqlite3IsTrueOrFalse(const char *zIn){ 002315 if( sqlite3StrICmp(zIn, "true")==0 ) return EP_IsTrue; 002316 if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse; 002317 return 0; 002318 } 002319 002320 002321 /* 002322 ** If the input expression is an ID with the name "true" or "false" 002323 ** then convert it into an TK_TRUEFALSE term. Return non-zero if 002324 ** the conversion happened, and zero if the expression is unaltered. 002325 */ 002326 int sqlite3ExprIdToTrueFalse(Expr *pExpr){ 002327 u32 v; 002328 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING ); 002329 if( !ExprHasProperty(pExpr, EP_Quoted|EP_IntValue) 002330 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0 002331 ){ 002332 pExpr->op = TK_TRUEFALSE; 002333 ExprSetProperty(pExpr, v); 002334 return 1; 002335 } 002336 return 0; 002337 } 002338 002339 /* 002340 ** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE 002341 ** and 0 if it is FALSE. 002342 */ 002343 int sqlite3ExprTruthValue(const Expr *pExpr){ 002344 pExpr = sqlite3ExprSkipCollateAndLikely((Expr*)pExpr); 002345 assert( pExpr->op==TK_TRUEFALSE ); 002346 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 002347 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0 002348 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 ); 002349 return pExpr->u.zToken[4]==0; 002350 } 002351 002352 /* 002353 ** If pExpr is an AND or OR expression, try to simplify it by eliminating 002354 ** terms that are always true or false. Return the simplified expression. 002355 ** Or return the original expression if no simplification is possible. 002356 ** 002357 ** Examples: 002358 ** 002359 ** (x<10) AND true => (x<10) 002360 ** (x<10) AND false => false 002361 ** (x<10) AND (y=22 OR false) => (x<10) AND (y=22) 002362 ** (x<10) AND (y=22 OR true) => (x<10) 002363 ** (y=22) OR true => true 002364 */ 002365 Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){ 002366 assert( pExpr!=0 ); 002367 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){ 002368 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight); 002369 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft); 002370 if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){ 002371 pExpr = pExpr->op==TK_AND ? pRight : pLeft; 002372 }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){ 002373 pExpr = pExpr->op==TK_AND ? pLeft : pRight; 002374 } 002375 } 002376 return pExpr; 002377 } 002378 002379 /* 002380 ** pExpr is a TK_FUNCTION node. Try to determine whether or not the 002381 ** function is a constant function. A function is constant if all of 002382 ** the following are true: 002383 ** 002384 ** (1) It is a scalar function (not an aggregate or window function) 002385 ** (2) It has either the SQLITE_FUNC_CONSTANT or SQLITE_FUNC_SLOCHNG 002386 ** property. 002387 ** (3) All of its arguments are constants 002388 ** 002389 ** This routine sets pWalker->eCode to 0 if pExpr is not a constant. 002390 ** It makes no changes to pWalker->eCode if pExpr is constant. In 002391 ** every case, it returns WRC_Abort. 002392 ** 002393 ** Called as a service subroutine from exprNodeIsConstant(). 002394 */ 002395 static SQLITE_NOINLINE int exprNodeIsConstantFunction( 002396 Walker *pWalker, 002397 Expr *pExpr 002398 ){ 002399 int n; /* Number of arguments */ 002400 ExprList *pList; /* List of arguments */ 002401 FuncDef *pDef; /* The function */ 002402 sqlite3 *db; /* The database */ 002403 002404 assert( pExpr->op==TK_FUNCTION ); 002405 if( ExprHasProperty(pExpr, EP_TokenOnly) 002406 || (pList = pExpr->x.pList)==0 002407 ){; 002408 n = 0; 002409 }else{ 002410 n = pList->nExpr; 002411 sqlite3WalkExprList(pWalker, pList); 002412 if( pWalker->eCode==0 ) return WRC_Abort; 002413 } 002414 db = pWalker->pParse->db; 002415 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0); 002416 if( pDef==0 002417 || pDef->xFinalize!=0 002418 || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 002419 || ExprHasProperty(pExpr, EP_WinFunc) 002420 ){ 002421 pWalker->eCode = 0; 002422 return WRC_Abort; 002423 } 002424 return WRC_Prune; 002425 } 002426 002427 002428 /* 002429 ** These routines are Walker callbacks used to check expressions to 002430 ** see if they are "constant" for some definition of constant. The 002431 ** Walker.eCode value determines the type of "constant" we are looking 002432 ** for. 002433 ** 002434 ** These callback routines are used to implement the following: 002435 ** 002436 ** sqlite3ExprIsConstant() pWalker->eCode==1 002437 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 002438 ** sqlite3ExprIsTableConstant() pWalker->eCode==3 002439 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 002440 ** 002441 ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression 002442 ** is found to not be a constant. 002443 ** 002444 ** The sqlite3ExprIsConstantOrFunction() is used for evaluating DEFAULT 002445 ** expressions in a CREATE TABLE statement. The Walker.eCode value is 5 002446 ** when parsing an existing schema out of the sqlite_schema table and 4 002447 ** when processing a new CREATE TABLE statement. A bound parameter raises 002448 ** an error for new statements, but is silently converted 002449 ** to NULL for existing schemas. This allows sqlite_schema tables that 002450 ** contain a bound parameter because they were generated by older versions 002451 ** of SQLite to be parsed by newer versions of SQLite without raising a 002452 ** malformed schema error. 002453 */ 002454 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 002455 assert( pWalker->eCode>0 ); 002456 002457 /* If pWalker->eCode is 2 then any term of the expression that comes from 002458 ** the ON or USING clauses of an outer join disqualifies the expression 002459 ** from being considered constant. */ 002460 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){ 002461 pWalker->eCode = 0; 002462 return WRC_Abort; 002463 } 002464 002465 switch( pExpr->op ){ 002466 /* Consider functions to be constant if all their arguments are constant 002467 ** and either pWalker->eCode==4 or 5 or the function has the 002468 ** SQLITE_FUNC_CONST flag. */ 002469 case TK_FUNCTION: 002470 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc)) 002471 && !ExprHasProperty(pExpr, EP_WinFunc) 002472 ){ 002473 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL); 002474 return WRC_Continue; 002475 }else if( pWalker->pParse ){ 002476 return exprNodeIsConstantFunction(pWalker, pExpr); 002477 }else{ 002478 pWalker->eCode = 0; 002479 return WRC_Abort; 002480 } 002481 case TK_ID: 002482 /* Convert "true" or "false" in a DEFAULT clause into the 002483 ** appropriate TK_TRUEFALSE operator */ 002484 if( sqlite3ExprIdToTrueFalse(pExpr) ){ 002485 return WRC_Prune; 002486 } 002487 /* no break */ deliberate_fall_through 002488 case TK_COLUMN: 002489 case TK_AGG_FUNCTION: 002490 case TK_AGG_COLUMN: 002491 testcase( pExpr->op==TK_ID ); 002492 testcase( pExpr->op==TK_COLUMN ); 002493 testcase( pExpr->op==TK_AGG_FUNCTION ); 002494 testcase( pExpr->op==TK_AGG_COLUMN ); 002495 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){ 002496 return WRC_Continue; 002497 } 002498 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ 002499 return WRC_Continue; 002500 } 002501 /* no break */ deliberate_fall_through 002502 case TK_IF_NULL_ROW: 002503 case TK_REGISTER: 002504 case TK_DOT: 002505 case TK_RAISE: 002506 testcase( pExpr->op==TK_REGISTER ); 002507 testcase( pExpr->op==TK_IF_NULL_ROW ); 002508 testcase( pExpr->op==TK_DOT ); 002509 testcase( pExpr->op==TK_RAISE ); 002510 pWalker->eCode = 0; 002511 return WRC_Abort; 002512 case TK_VARIABLE: 002513 if( pWalker->eCode==5 ){ 002514 /* Silently convert bound parameters that appear inside of CREATE 002515 ** statements into a NULL when parsing the CREATE statement text out 002516 ** of the sqlite_schema table */ 002517 pExpr->op = TK_NULL; 002518 }else if( pWalker->eCode==4 ){ 002519 /* A bound parameter in a CREATE statement that originates from 002520 ** sqlite3_prepare() causes an error */ 002521 pWalker->eCode = 0; 002522 return WRC_Abort; 002523 } 002524 /* no break */ deliberate_fall_through 002525 default: 002526 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */ 002527 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */ 002528 return WRC_Continue; 002529 } 002530 } 002531 static int exprIsConst(Parse *pParse, Expr *p, int initFlag){ 002532 Walker w; 002533 w.eCode = initFlag; 002534 w.pParse = pParse; 002535 w.xExprCallback = exprNodeIsConstant; 002536 w.xSelectCallback = sqlite3SelectWalkFail; 002537 #ifdef SQLITE_DEBUG 002538 w.xSelectCallback2 = sqlite3SelectWalkAssert2; 002539 #endif 002540 sqlite3WalkExpr(&w, p); 002541 return w.eCode; 002542 } 002543 002544 /* 002545 ** Walk an expression tree. Return non-zero if the expression is constant 002546 ** and 0 if it involves variables or function calls. 002547 ** 002548 ** For the purposes of this function, a double-quoted string (ex: "abc") 002549 ** is considered a variable but a single-quoted string (ex: 'abc') is 002550 ** a constant. 002551 ** 002552 ** The pParse parameter may be NULL. But if it is NULL, there is no way 002553 ** to determine if function calls are constant or not, and hence all 002554 ** function calls will be considered to be non-constant. If pParse is 002555 ** not NULL, then a function call might be constant, depending on the 002556 ** function and on its parameters. 002557 */ 002558 int sqlite3ExprIsConstant(Parse *pParse, Expr *p){ 002559 return exprIsConst(pParse, p, 1); 002560 } 002561 002562 /* 002563 ** Walk an expression tree. Return non-zero if 002564 ** 002565 ** (1) the expression is constant, and 002566 ** (2) the expression does originate in the ON or USING clause 002567 ** of a LEFT JOIN, and 002568 ** (3) the expression does not contain any EP_FixedCol TK_COLUMN 002569 ** operands created by the constant propagation optimization. 002570 ** 002571 ** When this routine returns true, it indicates that the expression 002572 ** can be added to the pParse->pConstExpr list and evaluated once when 002573 ** the prepared statement starts up. See sqlite3ExprCodeRunJustOnce(). 002574 */ 002575 static int sqlite3ExprIsConstantNotJoin(Parse *pParse, Expr *p){ 002576 return exprIsConst(pParse, p, 2); 002577 } 002578 002579 /* 002580 ** This routine examines sub-SELECT statements as an expression is being 002581 ** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered 002582 ** constant as long as they are uncorrelated - meaning that they do not 002583 ** contain any terms from outer contexts. 002584 */ 002585 static int exprSelectWalkTableConstant(Walker *pWalker, Select *pSelect){ 002586 assert( pSelect!=0 ); 002587 assert( pWalker->eCode==3 || pWalker->eCode==0 ); 002588 if( (pSelect->selFlags & SF_Correlated)!=0 ){ 002589 pWalker->eCode = 0; 002590 return WRC_Abort; 002591 } 002592 return WRC_Prune; 002593 } 002594 002595 /* 002596 ** Walk an expression tree. Return non-zero if the expression is constant 002597 ** for any single row of the table with cursor iCur. In other words, the 002598 ** expression must not refer to any non-deterministic function nor any 002599 ** table other than iCur. 002600 ** 002601 ** Consider uncorrelated subqueries to be constants if the bAllowSubq 002602 ** parameter is true. 002603 */ 002604 static int sqlite3ExprIsTableConstant(Expr *p, int iCur, int bAllowSubq){ 002605 Walker w; 002606 w.eCode = 3; 002607 w.pParse = 0; 002608 w.xExprCallback = exprNodeIsConstant; 002609 if( bAllowSubq ){ 002610 w.xSelectCallback = exprSelectWalkTableConstant; 002611 }else{ 002612 w.xSelectCallback = sqlite3SelectWalkFail; 002613 #ifdef SQLITE_DEBUG 002614 w.xSelectCallback2 = sqlite3SelectWalkAssert2; 002615 #endif 002616 } 002617 w.u.iCur = iCur; 002618 sqlite3WalkExpr(&w, p); 002619 return w.eCode; 002620 } 002621 002622 /* 002623 ** Check pExpr to see if it is an constraint on the single data source 002624 ** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr 002625 ** constrains pSrc but does not depend on any other tables or data 002626 ** sources anywhere else in the query. Return true (non-zero) if pExpr 002627 ** is a constraint on pSrc only. 002628 ** 002629 ** This is an optimization. False negatives will perhaps cause slower 002630 ** queries, but false positives will yield incorrect answers. So when in 002631 ** doubt, return 0. 002632 ** 002633 ** To be an single-source constraint, the following must be true: 002634 ** 002635 ** (1) pExpr cannot refer to any table other than pSrc->iCursor. 002636 ** 002637 ** (2a) pExpr cannot use subqueries unless the bAllowSubq parameter is 002638 ** true and the subquery is non-correlated 002639 ** 002640 ** (2b) pExpr cannot use non-deterministic functions. 002641 ** 002642 ** (3) pSrc cannot be part of the left operand for a RIGHT JOIN. 002643 ** (Is there some way to relax this constraint?) 002644 ** 002645 ** (4) If pSrc is the right operand of a LEFT JOIN, then... 002646 ** (4a) pExpr must come from an ON clause.. 002647 ** (4b) and specifically the ON clause associated with the LEFT JOIN. 002648 ** 002649 ** (5) If pSrc is not the right operand of a LEFT JOIN or the left 002650 ** operand of a RIGHT JOIN, then pExpr must be from the WHERE 002651 ** clause, not an ON clause. 002652 ** 002653 ** (6) Either: 002654 ** 002655 ** (6a) pExpr does not originate in an ON or USING clause, or 002656 ** 002657 ** (6b) The ON or USING clause from which pExpr is derived is 002658 ** not to the left of a RIGHT JOIN (or FULL JOIN). 002659 ** 002660 ** Without this restriction, accepting pExpr as a single-table 002661 ** constraint might move the the ON/USING filter expression 002662 ** from the left side of a RIGHT JOIN over to the right side, 002663 ** which leads to incorrect answers. See also restriction (9) 002664 ** on push-down. 002665 */ 002666 int sqlite3ExprIsSingleTableConstraint( 002667 Expr *pExpr, /* The constraint */ 002668 const SrcList *pSrcList, /* Complete FROM clause */ 002669 int iSrc, /* Which element of pSrcList to use */ 002670 int bAllowSubq /* Allow non-correlated subqueries */ 002671 ){ 002672 const SrcItem *pSrc = &pSrcList->a[iSrc]; 002673 if( pSrc->fg.jointype & JT_LTORJ ){ 002674 return 0; /* rule (3) */ 002675 } 002676 if( pSrc->fg.jointype & JT_LEFT ){ 002677 if( !ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* rule (4a) */ 002678 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */ 002679 }else{ 002680 if( ExprHasProperty(pExpr, EP_OuterON) ) return 0; /* rule (5) */ 002681 } 002682 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) /* (6a) */ 002683 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */ 002684 ){ 002685 int jj; 002686 for(jj=0; jj<iSrc; jj++){ 002687 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){ 002688 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){ 002689 return 0; /* restriction (6) */ 002690 } 002691 break; 002692 } 002693 } 002694 } 002695 /* Rules (1), (2a), and (2b) handled by the following: */ 002696 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq); 002697 } 002698 002699 002700 /* 002701 ** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy(). 002702 */ 002703 static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){ 002704 ExprList *pGroupBy = pWalker->u.pGroupBy; 002705 int i; 002706 002707 /* Check if pExpr is identical to any GROUP BY term. If so, consider 002708 ** it constant. */ 002709 for(i=0; i<pGroupBy->nExpr; i++){ 002710 Expr *p = pGroupBy->a[i].pExpr; 002711 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){ 002712 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p); 002713 if( sqlite3IsBinary(pColl) ){ 002714 return WRC_Prune; 002715 } 002716 } 002717 } 002718 002719 /* Check if pExpr is a sub-select. If so, consider it variable. */ 002720 if( ExprUseXSelect(pExpr) ){ 002721 pWalker->eCode = 0; 002722 return WRC_Abort; 002723 } 002724 002725 return exprNodeIsConstant(pWalker, pExpr); 002726 } 002727 002728 /* 002729 ** Walk the expression tree passed as the first argument. Return non-zero 002730 ** if the expression consists entirely of constants or copies of terms 002731 ** in pGroupBy that sort with the BINARY collation sequence. 002732 ** 002733 ** This routine is used to determine if a term of the HAVING clause can 002734 ** be promoted into the WHERE clause. In order for such a promotion to work, 002735 ** the value of the HAVING clause term must be the same for all members of 002736 ** a "group". The requirement that the GROUP BY term must be BINARY 002737 ** assumes that no other collating sequence will have a finer-grained 002738 ** grouping than binary. In other words (A=B COLLATE binary) implies 002739 ** A=B in every other collating sequence. The requirement that the 002740 ** GROUP BY be BINARY is stricter than necessary. It would also work 002741 ** to promote HAVING clauses that use the same alternative collating 002742 ** sequence as the GROUP BY term, but that is much harder to check, 002743 ** alternative collating sequences are uncommon, and this is only an 002744 ** optimization, so we take the easy way out and simply require the 002745 ** GROUP BY to use the BINARY collating sequence. 002746 */ 002747 int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){ 002748 Walker w; 002749 w.eCode = 1; 002750 w.xExprCallback = exprNodeIsConstantOrGroupBy; 002751 w.xSelectCallback = 0; 002752 w.u.pGroupBy = pGroupBy; 002753 w.pParse = pParse; 002754 sqlite3WalkExpr(&w, p); 002755 return w.eCode; 002756 } 002757 002758 /* 002759 ** Walk an expression tree for the DEFAULT field of a column definition 002760 ** in a CREATE TABLE statement. Return non-zero if the expression is 002761 ** acceptable for use as a DEFAULT. That is to say, return non-zero if 002762 ** the expression is constant or a function call with constant arguments. 002763 ** Return and 0 if there are any variables. 002764 ** 002765 ** isInit is true when parsing from sqlite_schema. isInit is false when 002766 ** processing a new CREATE TABLE statement. When isInit is true, parameters 002767 ** (such as ? or $abc) in the expression are converted into NULL. When 002768 ** isInit is false, parameters raise an error. Parameters should not be 002769 ** allowed in a CREATE TABLE statement, but some legacy versions of SQLite 002770 ** allowed it, so we need to support it when reading sqlite_schema for 002771 ** backwards compatibility. 002772 ** 002773 ** If isInit is true, set EP_FromDDL on every TK_FUNCTION node. 002774 ** 002775 ** For the purposes of this function, a double-quoted string (ex: "abc") 002776 ** is considered a variable but a single-quoted string (ex: 'abc') is 002777 ** a constant. 002778 */ 002779 int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 002780 assert( isInit==0 || isInit==1 ); 002781 return exprIsConst(0, p, 4+isInit); 002782 } 002783 002784 #ifdef SQLITE_ENABLE_CURSOR_HINTS 002785 /* 002786 ** Walk an expression tree. Return 1 if the expression contains a 002787 ** subquery of some kind. Return 0 if there are no subqueries. 002788 */ 002789 int sqlite3ExprContainsSubquery(Expr *p){ 002790 Walker w; 002791 w.eCode = 1; 002792 w.xExprCallback = sqlite3ExprWalkNoop; 002793 w.xSelectCallback = sqlite3SelectWalkFail; 002794 #ifdef SQLITE_DEBUG 002795 w.xSelectCallback2 = sqlite3SelectWalkAssert2; 002796 #endif 002797 sqlite3WalkExpr(&w, p); 002798 return w.eCode==0; 002799 } 002800 #endif 002801 002802 /* 002803 ** If the expression p codes a constant integer that is small enough 002804 ** to fit in a 32-bit integer, return 1 and put the value of the integer 002805 ** in *pValue. If the expression is not an integer or if it is too big 002806 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 002807 ** 002808 ** If the pParse pointer is provided, then allow the expression p to be 002809 ** a parameter (TK_VARIABLE) that is bound to an integer. 002810 ** But if pParse is NULL, then p must be a pure integer literal. 002811 */ 002812 int sqlite3ExprIsInteger(const Expr *p, int *pValue, Parse *pParse){ 002813 int rc = 0; 002814 if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */ 002815 002816 /* If an expression is an integer literal that fits in a signed 32-bit 002817 ** integer, then the EP_IntValue flag will have already been set */ 002818 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 002819 || sqlite3GetInt32(p->u.zToken, &rc)==0 ); 002820 002821 if( p->flags & EP_IntValue ){ 002822 *pValue = p->u.iValue; 002823 return 1; 002824 } 002825 switch( p->op ){ 002826 case TK_UPLUS: { 002827 rc = sqlite3ExprIsInteger(p->pLeft, pValue, 0); 002828 break; 002829 } 002830 case TK_UMINUS: { 002831 int v = 0; 002832 if( sqlite3ExprIsInteger(p->pLeft, &v, 0) ){ 002833 assert( ((unsigned int)v)!=0x80000000 ); 002834 *pValue = -v; 002835 rc = 1; 002836 } 002837 break; 002838 } 002839 case TK_VARIABLE: { 002840 sqlite3_value *pVal; 002841 if( pParse==0 ) break; 002842 if( NEVER(pParse->pVdbe==0) ) break; 002843 if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) break; 002844 sqlite3VdbeSetVarmask(pParse->pVdbe, p->iColumn); 002845 pVal = sqlite3VdbeGetBoundValue(pParse->pReprepare, p->iColumn, 002846 SQLITE_AFF_BLOB); 002847 if( pVal ){ 002848 if( sqlite3_value_type(pVal)==SQLITE_INTEGER ){ 002849 sqlite3_int64 vv = sqlite3_value_int64(pVal); 002850 if( vv == (vv & 0x7fffffff) ){ /* non-negative numbers only */ 002851 *pValue = (int)vv; 002852 rc = 1; 002853 } 002854 } 002855 sqlite3ValueFree(pVal); 002856 } 002857 break; 002858 } 002859 default: break; 002860 } 002861 return rc; 002862 } 002863 002864 /* 002865 ** Return FALSE if there is no chance that the expression can be NULL. 002866 ** 002867 ** If the expression might be NULL or if the expression is too complex 002868 ** to tell return TRUE. 002869 ** 002870 ** This routine is used as an optimization, to skip OP_IsNull opcodes 002871 ** when we know that a value cannot be NULL. Hence, a false positive 002872 ** (returning TRUE when in fact the expression can never be NULL) might 002873 ** be a small performance hit but is otherwise harmless. On the other 002874 ** hand, a false negative (returning FALSE when the result could be NULL) 002875 ** will likely result in an incorrect answer. So when in doubt, return 002876 ** TRUE. 002877 */ 002878 int sqlite3ExprCanBeNull(const Expr *p){ 002879 u8 op; 002880 assert( p!=0 ); 002881 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ 002882 p = p->pLeft; 002883 assert( p!=0 ); 002884 } 002885 op = p->op; 002886 if( op==TK_REGISTER ) op = p->op2; 002887 switch( op ){ 002888 case TK_INTEGER: 002889 case TK_STRING: 002890 case TK_FLOAT: 002891 case TK_BLOB: 002892 return 0; 002893 case TK_COLUMN: 002894 assert( ExprUseYTab(p) ); 002895 return ExprHasProperty(p, EP_CanBeNull) 002896 || NEVER(p->y.pTab==0) /* Reference to column of index on expr */ 002897 #ifdef SQLITE_ALLOW_ROWID_IN_VIEW 002898 || (p->iColumn==XN_ROWID && IsView(p->y.pTab)) 002899 #endif 002900 || (p->iColumn>=0 002901 && p->y.pTab->aCol!=0 /* Possible due to prior error */ 002902 && ALWAYS(p->iColumn<p->y.pTab->nCol) 002903 && p->y.pTab->aCol[p->iColumn].notNull==0); 002904 default: 002905 return 1; 002906 } 002907 } 002908 002909 /* 002910 ** Return TRUE if the given expression is a constant which would be 002911 ** unchanged by OP_Affinity with the affinity given in the second 002912 ** argument. 002913 ** 002914 ** This routine is used to determine if the OP_Affinity operation 002915 ** can be omitted. When in doubt return FALSE. A false negative 002916 ** is harmless. A false positive, however, can result in the wrong 002917 ** answer. 002918 */ 002919 int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 002920 u8 op; 002921 int unaryMinus = 0; 002922 if( aff==SQLITE_AFF_BLOB ) return 1; 002923 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ 002924 if( p->op==TK_UMINUS ) unaryMinus = 1; 002925 p = p->pLeft; 002926 } 002927 op = p->op; 002928 if( op==TK_REGISTER ) op = p->op2; 002929 switch( op ){ 002930 case TK_INTEGER: { 002931 return aff>=SQLITE_AFF_NUMERIC; 002932 } 002933 case TK_FLOAT: { 002934 return aff>=SQLITE_AFF_NUMERIC; 002935 } 002936 case TK_STRING: { 002937 return !unaryMinus && aff==SQLITE_AFF_TEXT; 002938 } 002939 case TK_BLOB: { 002940 return !unaryMinus; 002941 } 002942 case TK_COLUMN: { 002943 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ 002944 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0; 002945 } 002946 default: { 002947 return 0; 002948 } 002949 } 002950 } 002951 002952 /* 002953 ** Return TRUE if the given string is a row-id column name. 002954 */ 002955 int sqlite3IsRowid(const char *z){ 002956 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 002957 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 002958 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 002959 return 0; 002960 } 002961 002962 /* 002963 ** Return a pointer to a buffer containing a usable rowid alias for table 002964 ** pTab. An alias is usable if there is not an explicit user-defined column 002965 ** of the same name. 002966 */ 002967 const char *sqlite3RowidAlias(Table *pTab){ 002968 const char *azOpt[] = {"_ROWID_", "ROWID", "OID"}; 002969 int ii; 002970 assert( VisibleRowid(pTab) ); 002971 for(ii=0; ii<ArraySize(azOpt); ii++){ 002972 int iCol; 002973 for(iCol=0; iCol<pTab->nCol; iCol++){ 002974 if( sqlite3_stricmp(azOpt[ii], pTab->aCol[iCol].zCnName)==0 ) break; 002975 } 002976 if( iCol==pTab->nCol ){ 002977 return azOpt[ii]; 002978 } 002979 } 002980 return 0; 002981 } 002982 002983 /* 002984 ** pX is the RHS of an IN operator. If pX is a SELECT statement 002985 ** that can be simplified to a direct table access, then return 002986 ** a pointer to the SELECT statement. If pX is not a SELECT statement, 002987 ** or if the SELECT statement needs to be materialized into a transient 002988 ** table, then return NULL. 002989 */ 002990 #ifndef SQLITE_OMIT_SUBQUERY 002991 static Select *isCandidateForInOpt(const Expr *pX){ 002992 Select *p; 002993 SrcList *pSrc; 002994 ExprList *pEList; 002995 Table *pTab; 002996 int i; 002997 if( !ExprUseXSelect(pX) ) return 0; /* Not a subquery */ 002998 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ 002999 p = pX->x.pSelect; 003000 if( p->pPrior ) return 0; /* Not a compound SELECT */ 003001 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ 003002 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); 003003 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); 003004 return 0; /* No DISTINCT keyword and no aggregate functions */ 003005 } 003006 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ 003007 if( p->pLimit ) return 0; /* Has no LIMIT clause */ 003008 if( p->pWhere ) return 0; /* Has no WHERE clause */ 003009 pSrc = p->pSrc; 003010 assert( pSrc!=0 ); 003011 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ 003012 if( pSrc->a[0].fg.isSubquery) return 0;/* FROM is not a subquery or view */ 003013 pTab = pSrc->a[0].pSTab; 003014 assert( pTab!=0 ); 003015 assert( !IsView(pTab) ); /* FROM clause is not a view */ 003016 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ 003017 pEList = p->pEList; 003018 assert( pEList!=0 ); 003019 /* All SELECT results must be columns. */ 003020 for(i=0; i<pEList->nExpr; i++){ 003021 Expr *pRes = pEList->a[i].pExpr; 003022 if( pRes->op!=TK_COLUMN ) return 0; 003023 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ 003024 } 003025 return p; 003026 } 003027 #endif /* SQLITE_OMIT_SUBQUERY */ 003028 003029 #ifndef SQLITE_OMIT_SUBQUERY 003030 /* 003031 ** Generate code that checks the left-most column of index table iCur to see if 003032 ** it contains any NULL entries. Cause the register at regHasNull to be set 003033 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 003034 ** to be set to NULL if iCur contains one or more NULL values. 003035 */ 003036 static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 003037 int addr1; 003038 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 003039 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 003040 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 003041 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 003042 VdbeComment((v, "first_entry_in(%d)", iCur)); 003043 sqlite3VdbeJumpHere(v, addr1); 003044 } 003045 #endif 003046 003047 003048 #ifndef SQLITE_OMIT_SUBQUERY 003049 /* 003050 ** The argument is an IN operator with a list (not a subquery) on the 003051 ** right-hand side. Return TRUE if that list is constant. 003052 */ 003053 static int sqlite3InRhsIsConstant(Parse *pParse, Expr *pIn){ 003054 Expr *pLHS; 003055 int res; 003056 assert( !ExprHasProperty(pIn, EP_xIsSelect) ); 003057 pLHS = pIn->pLeft; 003058 pIn->pLeft = 0; 003059 res = sqlite3ExprIsConstant(pParse, pIn); 003060 pIn->pLeft = pLHS; 003061 return res; 003062 } 003063 #endif 003064 003065 /* 003066 ** This function is used by the implementation of the IN (...) operator. 003067 ** The pX parameter is the expression on the RHS of the IN operator, which 003068 ** might be either a list of expressions or a subquery. 003069 ** 003070 ** The job of this routine is to find or create a b-tree object that can 003071 ** be used either to test for membership in the RHS set or to iterate through 003072 ** all members of the RHS set, skipping duplicates. 003073 ** 003074 ** A cursor is opened on the b-tree object that is the RHS of the IN operator 003075 ** and the *piTab parameter is set to the index of that cursor. 003076 ** 003077 ** The returned value of this function indicates the b-tree type, as follows: 003078 ** 003079 ** IN_INDEX_ROWID - The cursor was opened on a database table. 003080 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. 003081 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. 003082 ** IN_INDEX_EPH - The cursor was opened on a specially created and 003083 ** populated ephemeral table. 003084 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be 003085 ** implemented as a sequence of comparisons. 003086 ** 003087 ** An existing b-tree might be used if the RHS expression pX is a simple 003088 ** subquery such as: 003089 ** 003090 ** SELECT <column1>, <column2>... FROM <table> 003091 ** 003092 ** If the RHS of the IN operator is a list or a more complex subquery, then 003093 ** an ephemeral table might need to be generated from the RHS and then 003094 ** pX->iTable made to point to the ephemeral table instead of an 003095 ** existing table. In this case, the creation and initialization of the 003096 ** ephemeral table might be put inside of a subroutine, the EP_Subrtn flag 003097 ** will be set on pX and the pX->y.sub fields will be set to show where 003098 ** the subroutine is coded. 003099 ** 003100 ** The inFlags parameter must contain, at a minimum, one of the bits 003101 ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains 003102 ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast 003103 ** membership test. When the IN_INDEX_LOOP bit is set, the IN index will 003104 ** be used to loop over all values of the RHS of the IN operator. 003105 ** 003106 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate 003107 ** through the set members) then the b-tree must not contain duplicates. 003108 ** An ephemeral table will be created unless the selected columns are guaranteed 003109 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to 003110 ** a UNIQUE constraint or index. 003111 ** 003112 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 003113 ** for fast set membership tests) then an ephemeral table must 003114 ** be used unless <columns> is a single INTEGER PRIMARY KEY column or an 003115 ** index can be found with the specified <columns> as its left-most. 003116 ** 003117 ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and 003118 ** if the RHS of the IN operator is a list (not a subquery) then this 003119 ** routine might decide that creating an ephemeral b-tree for membership 003120 ** testing is too expensive and return IN_INDEX_NOOP. In that case, the 003121 ** calling routine should implement the IN operator using a sequence 003122 ** of Eq or Ne comparison operations. 003123 ** 003124 ** When the b-tree is being used for membership tests, the calling function 003125 ** might need to know whether or not the RHS side of the IN operator 003126 ** contains a NULL. If prRhsHasNull is not a NULL pointer and 003127 ** if there is any chance that the (...) might contain a NULL value at 003128 ** runtime, then a register is allocated and the register number written 003129 ** to *prRhsHasNull. If there is no chance that the (...) contains a 003130 ** NULL value, then *prRhsHasNull is left unchanged. 003131 ** 003132 ** If a register is allocated and its location stored in *prRhsHasNull, then 003133 ** the value in that register will be NULL if the b-tree contains one or more 003134 ** NULL values, and it will be some non-NULL value if the b-tree contains no 003135 ** NULL values. 003136 ** 003137 ** If the aiMap parameter is not NULL, it must point to an array containing 003138 ** one element for each column returned by the SELECT statement on the RHS 003139 ** of the IN(...) operator. The i'th entry of the array is populated with the 003140 ** offset of the index column that matches the i'th column returned by the 003141 ** SELECT. For example, if the expression and selected index are: 003142 ** 003143 ** (?,?,?) IN (SELECT a, b, c FROM t1) 003144 ** CREATE INDEX i1 ON t1(b, c, a); 003145 ** 003146 ** then aiMap[] is populated with {2, 0, 1}. 003147 */ 003148 #ifndef SQLITE_OMIT_SUBQUERY 003149 int sqlite3FindInIndex( 003150 Parse *pParse, /* Parsing context */ 003151 Expr *pX, /* The IN expression */ 003152 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */ 003153 int *prRhsHasNull, /* Register holding NULL status. See notes */ 003154 int *aiMap, /* Mapping from Index fields to RHS fields */ 003155 int *piTab /* OUT: index to use */ 003156 ){ 003157 Select *p; /* SELECT to the right of IN operator */ 003158 int eType = 0; /* Type of RHS table. IN_INDEX_* */ 003159 int iTab; /* Cursor of the RHS table */ 003160 int mustBeUnique; /* True if RHS must be unique */ 003161 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 003162 003163 assert( pX->op==TK_IN ); 003164 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 003165 iTab = pParse->nTab++; 003166 003167 /* If the RHS of this IN(...) operator is a SELECT, and if it matters 003168 ** whether or not the SELECT result contains NULL values, check whether 003169 ** or not NULL is actually possible (it may not be, for example, due 003170 ** to NOT NULL constraints in the schema). If no NULL values are possible, 003171 ** set prRhsHasNull to 0 before continuing. */ 003172 if( prRhsHasNull && ExprUseXSelect(pX) ){ 003173 int i; 003174 ExprList *pEList = pX->x.pSelect->pEList; 003175 for(i=0; i<pEList->nExpr; i++){ 003176 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break; 003177 } 003178 if( i==pEList->nExpr ){ 003179 prRhsHasNull = 0; 003180 } 003181 } 003182 003183 /* Check to see if an existing table or index can be used to 003184 ** satisfy the query. This is preferable to generating a new 003185 ** ephemeral table. */ 003186 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ 003187 sqlite3 *db = pParse->db; /* Database connection */ 003188 Table *pTab; /* Table <table>. */ 003189 int iDb; /* Database idx for pTab */ 003190 ExprList *pEList = p->pEList; 003191 int nExpr = pEList->nExpr; 003192 003193 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 003194 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 003195 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 003196 pTab = p->pSrc->a[0].pSTab; 003197 003198 /* Code an OP_Transaction and OP_TableLock for <table>. */ 003199 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 003200 assert( iDb>=0 && iDb<SQLITE_MAX_DB ); 003201 sqlite3CodeVerifySchema(pParse, iDb); 003202 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 003203 003204 assert(v); /* sqlite3GetVdbe() has always been previously called */ 003205 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ 003206 /* The "x IN (SELECT rowid FROM table)" case */ 003207 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); 003208 VdbeCoverage(v); 003209 003210 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 003211 eType = IN_INDEX_ROWID; 003212 ExplainQueryPlan((pParse, 0, 003213 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName)); 003214 sqlite3VdbeJumpHere(v, iAddr); 003215 }else{ 003216 Index *pIdx; /* Iterator variable */ 003217 int affinity_ok = 1; 003218 int i; 003219 003220 /* Check that the affinity that will be used to perform each 003221 ** comparison is the same as the affinity of each column in table 003222 ** on the RHS of the IN operator. If it not, it is not possible to 003223 ** use any index of the RHS table. */ 003224 for(i=0; i<nExpr && affinity_ok; i++){ 003225 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); 003226 int iCol = pEList->a[i].pExpr->iColumn; 003227 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */ 003228 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); 003229 testcase( cmpaff==SQLITE_AFF_BLOB ); 003230 testcase( cmpaff==SQLITE_AFF_TEXT ); 003231 switch( cmpaff ){ 003232 case SQLITE_AFF_BLOB: 003233 break; 003234 case SQLITE_AFF_TEXT: 003235 /* sqlite3CompareAffinity() only returns TEXT if one side or the 003236 ** other has no affinity and the other side is TEXT. Hence, 003237 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT 003238 ** and for the term on the LHS of the IN to have no affinity. */ 003239 assert( idxaff==SQLITE_AFF_TEXT ); 003240 break; 003241 default: 003242 affinity_ok = sqlite3IsNumericAffinity(idxaff); 003243 } 003244 } 003245 003246 if( affinity_ok ){ 003247 /* Search for an existing index that will work for this IN operator */ 003248 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){ 003249 Bitmask colUsed; /* Columns of the index used */ 003250 Bitmask mCol; /* Mask for the current column */ 003251 if( pIdx->nColumn<nExpr ) continue; 003252 if( pIdx->pPartIdxWhere!=0 ) continue; 003253 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute 003254 ** BITMASK(nExpr) without overflowing */ 003255 testcase( pIdx->nColumn==BMS-2 ); 003256 testcase( pIdx->nColumn==BMS-1 ); 003257 if( pIdx->nColumn>=BMS-1 ) continue; 003258 if( mustBeUnique ){ 003259 if( pIdx->nKeyCol>nExpr 003260 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)) 003261 ){ 003262 continue; /* This index is not unique over the IN RHS columns */ 003263 } 003264 } 003265 003266 colUsed = 0; /* Columns of index used so far */ 003267 for(i=0; i<nExpr; i++){ 003268 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); 003269 Expr *pRhs = pEList->a[i].pExpr; 003270 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); 003271 int j; 003272 003273 for(j=0; j<nExpr; j++){ 003274 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; 003275 assert( pIdx->azColl[j] ); 003276 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ 003277 continue; 003278 } 003279 break; 003280 } 003281 if( j==nExpr ) break; 003282 mCol = MASKBIT(j); 003283 if( mCol & colUsed ) break; /* Each column used only once */ 003284 colUsed |= mCol; 003285 if( aiMap ) aiMap[i] = j; 003286 } 003287 003288 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) ); 003289 if( colUsed==(MASKBIT(nExpr)-1) ){ 003290 /* If we reach this point, that means the index pIdx is usable */ 003291 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); 003292 ExplainQueryPlan((pParse, 0, 003293 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName)); 003294 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); 003295 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 003296 VdbeComment((v, "%s", pIdx->zName)); 003297 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); 003298 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; 003299 003300 if( prRhsHasNull ){ 003301 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 003302 i64 mask = (1<<nExpr)-1; 003303 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 003304 iTab, 0, 0, (u8*)&mask, P4_INT64); 003305 #endif 003306 *prRhsHasNull = ++pParse->nMem; 003307 if( nExpr==1 ){ 003308 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); 003309 } 003310 } 003311 sqlite3VdbeJumpHere(v, iAddr); 003312 } 003313 } /* End loop over indexes */ 003314 } /* End if( affinity_ok ) */ 003315 } /* End if not an rowid index */ 003316 } /* End attempt to optimize using an index */ 003317 003318 /* If no preexisting index is available for the IN clause 003319 ** and IN_INDEX_NOOP is an allowed reply 003320 ** and the RHS of the IN operator is a list, not a subquery 003321 ** and the RHS is not constant or has two or fewer terms, 003322 ** then it is not worth creating an ephemeral table to evaluate 003323 ** the IN operator so return IN_INDEX_NOOP. 003324 */ 003325 if( eType==0 003326 && (inFlags & IN_INDEX_NOOP_OK) 003327 && ExprUseXList(pX) 003328 && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2) 003329 ){ 003330 pParse->nTab--; /* Back out the allocation of the unused cursor */ 003331 iTab = -1; /* Cursor is not allocated */ 003332 eType = IN_INDEX_NOOP; 003333 } 003334 003335 if( eType==0 ){ 003336 /* Could not find an existing table or index to use as the RHS b-tree. 003337 ** We will have to generate an ephemeral table to do the job. 003338 */ 003339 u32 savedNQueryLoop = pParse->nQueryLoop; 003340 int rMayHaveNull = 0; 003341 eType = IN_INDEX_EPH; 003342 if( inFlags & IN_INDEX_LOOP ){ 003343 pParse->nQueryLoop = 0; 003344 }else if( prRhsHasNull ){ 003345 *prRhsHasNull = rMayHaveNull = ++pParse->nMem; 003346 } 003347 assert( pX->op==TK_IN ); 003348 sqlite3CodeRhsOfIN(pParse, pX, iTab); 003349 if( rMayHaveNull ){ 003350 sqlite3SetHasNullFlag(v, iTab, rMayHaveNull); 003351 } 003352 pParse->nQueryLoop = savedNQueryLoop; 003353 } 003354 003355 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ 003356 int i, n; 003357 n = sqlite3ExprVectorSize(pX->pLeft); 003358 for(i=0; i<n; i++) aiMap[i] = i; 003359 } 003360 *piTab = iTab; 003361 return eType; 003362 } 003363 #endif 003364 003365 #ifndef SQLITE_OMIT_SUBQUERY 003366 /* 003367 ** Argument pExpr is an (?, ?...) IN(...) expression. This 003368 ** function allocates and returns a nul-terminated string containing 003369 ** the affinities to be used for each column of the comparison. 003370 ** 003371 ** It is the responsibility of the caller to ensure that the returned 003372 ** string is eventually freed using sqlite3DbFree(). 003373 */ 003374 static char *exprINAffinity(Parse *pParse, const Expr *pExpr){ 003375 Expr *pLeft = pExpr->pLeft; 003376 int nVal = sqlite3ExprVectorSize(pLeft); 003377 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0; 003378 char *zRet; 003379 003380 assert( pExpr->op==TK_IN ); 003381 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1); 003382 if( zRet ){ 003383 int i; 003384 for(i=0; i<nVal; i++){ 003385 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i); 003386 char a = sqlite3ExprAffinity(pA); 003387 if( pSelect ){ 003388 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a); 003389 }else{ 003390 zRet[i] = a; 003391 } 003392 } 003393 zRet[nVal] = '\0'; 003394 } 003395 return zRet; 003396 } 003397 #endif 003398 003399 #ifndef SQLITE_OMIT_SUBQUERY 003400 /* 003401 ** Load the Parse object passed as the first argument with an error 003402 ** message of the form: 003403 ** 003404 ** "sub-select returns N columns - expected M" 003405 */ 003406 void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){ 003407 if( pParse->nErr==0 ){ 003408 const char *zFmt = "sub-select returns %d columns - expected %d"; 003409 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect); 003410 } 003411 } 003412 #endif 003413 003414 /* 003415 ** Expression pExpr is a vector that has been used in a context where 003416 ** it is not permitted. If pExpr is a sub-select vector, this routine 003417 ** loads the Parse object with a message of the form: 003418 ** 003419 ** "sub-select returns N columns - expected 1" 003420 ** 003421 ** Or, if it is a regular scalar vector: 003422 ** 003423 ** "row value misused" 003424 */ 003425 void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){ 003426 #ifndef SQLITE_OMIT_SUBQUERY 003427 if( ExprUseXSelect(pExpr) ){ 003428 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1); 003429 }else 003430 #endif 003431 { 003432 sqlite3ErrorMsg(pParse, "row value misused"); 003433 } 003434 } 003435 003436 #ifndef SQLITE_OMIT_SUBQUERY 003437 /* 003438 ** Scan all previously generated bytecode looking for an OP_BeginSubrtn 003439 ** that is compatible with pExpr. If found, add the y.sub values 003440 ** to pExpr and return true. If not found, return false. 003441 */ 003442 static int findCompatibleInRhsSubrtn( 003443 Parse *pParse, /* Parsing context */ 003444 Expr *pExpr, /* IN operator with RHS that we want to reuse */ 003445 SubrtnSig *pNewSig /* Signature for the IN operator */ 003446 ){ 003447 VdbeOp *pOp, *pEnd; 003448 SubrtnSig *pSig; 003449 Vdbe *v; 003450 003451 if( pNewSig==0 ) return 0; 003452 if( (pParse->mSubrtnSig & (1<<(pNewSig->selId&7)))==0 ) return 0; 003453 assert( pExpr->op==TK_IN ); 003454 assert( !ExprUseYSub(pExpr) ); 003455 assert( ExprUseXSelect(pExpr) ); 003456 assert( pExpr->x.pSelect!=0 ); 003457 assert( (pExpr->x.pSelect->selFlags & SF_All)==0 ); 003458 v = pParse->pVdbe; 003459 assert( v!=0 ); 003460 pOp = sqlite3VdbeGetOp(v, 1); 003461 pEnd = sqlite3VdbeGetLastOp(v); 003462 for(; pOp<pEnd; pOp++){ 003463 if( pOp->p4type!=P4_SUBRTNSIG ) continue; 003464 assert( pOp->opcode==OP_BeginSubrtn ); 003465 pSig = pOp->p4.pSubrtnSig; 003466 assert( pSig!=0 ); 003467 if( pNewSig->selId!=pSig->selId ) continue; 003468 if( strcmp(pNewSig->zAff,pSig->zAff)!=0 ) continue; 003469 pExpr->y.sub.iAddr = pSig->iAddr; 003470 pExpr->y.sub.regReturn = pSig->regReturn; 003471 pExpr->iTable = pSig->iTable; 003472 ExprSetProperty(pExpr, EP_Subrtn); 003473 return 1; 003474 } 003475 return 0; 003476 } 003477 #endif /* SQLITE_OMIT_SUBQUERY */ 003478 003479 #ifndef SQLITE_OMIT_SUBQUERY 003480 /* 003481 ** Generate code that will construct an ephemeral table containing all terms 003482 ** in the RHS of an IN operator. The IN operator can be in either of two 003483 ** forms: 003484 ** 003485 ** x IN (4,5,11) -- IN operator with list on right-hand side 003486 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 003487 ** 003488 ** The pExpr parameter is the IN operator. The cursor number for the 003489 ** constructed ephemeral table is returned. The first time the ephemeral 003490 ** table is computed, the cursor number is also stored in pExpr->iTable, 003491 ** however the cursor number returned might not be the same, as it might 003492 ** have been duplicated using OP_OpenDup. 003493 ** 003494 ** If the LHS expression ("x" in the examples) is a column value, or 003495 ** the SELECT statement returns a column value, then the affinity of that 003496 ** column is used to build the index keys. If both 'x' and the 003497 ** SELECT... statement are columns, then numeric affinity is used 003498 ** if either column has NUMERIC or INTEGER affinity. If neither 003499 ** 'x' nor the SELECT... statement are columns, then numeric affinity 003500 ** is used. 003501 */ 003502 void sqlite3CodeRhsOfIN( 003503 Parse *pParse, /* Parsing context */ 003504 Expr *pExpr, /* The IN operator */ 003505 int iTab /* Use this cursor number */ 003506 ){ 003507 int addrOnce = 0; /* Address of the OP_Once instruction at top */ 003508 int addr; /* Address of OP_OpenEphemeral instruction */ 003509 Expr *pLeft; /* the LHS of the IN operator */ 003510 KeyInfo *pKeyInfo = 0; /* Key information */ 003511 int nVal; /* Size of vector pLeft */ 003512 Vdbe *v; /* The prepared statement under construction */ 003513 003514 v = pParse->pVdbe; 003515 assert( v!=0 ); 003516 003517 /* The evaluation of the IN must be repeated every time it 003518 ** is encountered if any of the following is true: 003519 ** 003520 ** * The right-hand side is a correlated subquery 003521 ** * The right-hand side is an expression list containing variables 003522 ** * We are inside a trigger 003523 ** 003524 ** If all of the above are false, then we can compute the RHS just once 003525 ** and reuse it many names. 003526 */ 003527 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){ 003528 /* Reuse of the RHS is allowed 003529 ** 003530 ** Compute a signature for the RHS of the IN operator to facility 003531 ** finding and reusing prior instances of the same IN operator. 003532 */ 003533 SubrtnSig *pSig = 0; 003534 assert( !ExprUseXSelect(pExpr) || pExpr->x.pSelect!=0 ); 003535 if( ExprUseXSelect(pExpr) && (pExpr->x.pSelect->selFlags & SF_All)==0 ){ 003536 pSig = sqlite3DbMallocRawNN(pParse->db, sizeof(pSig[0])); 003537 if( pSig ){ 003538 pSig->selId = pExpr->x.pSelect->selId; 003539 pSig->zAff = exprINAffinity(pParse, pExpr); 003540 } 003541 } 003542 003543 /* Check to see if there is a prior materialization of the RHS of 003544 ** this IN operator. If there is, then make use of that prior 003545 ** materialization rather than recomputing it. 003546 */ 003547 if( ExprHasProperty(pExpr, EP_Subrtn) 003548 || findCompatibleInRhsSubrtn(pParse, pExpr, pSig) 003549 ){ 003550 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); 003551 if( ExprUseXSelect(pExpr) ){ 003552 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d", 003553 pExpr->x.pSelect->selId)); 003554 } 003555 assert( ExprUseYSub(pExpr) ); 003556 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn, 003557 pExpr->y.sub.iAddr); 003558 assert( iTab!=pExpr->iTable ); 003559 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable); 003560 sqlite3VdbeJumpHere(v, addrOnce); 003561 if( pSig ){ 003562 sqlite3DbFree(pParse->db, pSig->zAff); 003563 sqlite3DbFree(pParse->db, pSig); 003564 } 003565 return; 003566 } 003567 003568 /* Begin coding the subroutine */ 003569 assert( !ExprUseYWin(pExpr) ); 003570 ExprSetProperty(pExpr, EP_Subrtn); 003571 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 003572 pExpr->y.sub.regReturn = ++pParse->nMem; 003573 pExpr->y.sub.iAddr = 003574 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1; 003575 if( pSig ){ 003576 pSig->iAddr = pExpr->y.sub.iAddr; 003577 pSig->regReturn = pExpr->y.sub.regReturn; 003578 pSig->iTable = iTab; 003579 pParse->mSubrtnSig = 1 << (pSig->selId&7); 003580 sqlite3VdbeChangeP4(v, -1, (const char*)pSig, P4_SUBRTNSIG); 003581 } 003582 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); 003583 } 003584 003585 /* Check to see if this is a vector IN operator */ 003586 pLeft = pExpr->pLeft; 003587 nVal = sqlite3ExprVectorSize(pLeft); 003588 003589 /* Construct the ephemeral table that will contain the content of 003590 ** RHS of the IN operator. 003591 */ 003592 pExpr->iTable = iTab; 003593 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal); 003594 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 003595 if( ExprUseXSelect(pExpr) ){ 003596 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId)); 003597 }else{ 003598 VdbeComment((v, "RHS of IN operator")); 003599 } 003600 #endif 003601 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1); 003602 003603 if( ExprUseXSelect(pExpr) ){ 003604 /* Case 1: expr IN (SELECT ...) 003605 ** 003606 ** Generate code to write the results of the select into the temporary 003607 ** table allocated and opened above. 003608 */ 003609 Select *pSelect = pExpr->x.pSelect; 003610 ExprList *pEList = pSelect->pEList; 003611 003612 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d", 003613 addrOnce?"":"CORRELATED ", pSelect->selId 003614 )); 003615 /* If the LHS and RHS of the IN operator do not match, that 003616 ** error will have been caught long before we reach this point. */ 003617 if( ALWAYS(pEList->nExpr==nVal) ){ 003618 Select *pCopy; 003619 SelectDest dest; 003620 int i; 003621 int rc; 003622 int addrBloom = 0; 003623 sqlite3SelectDestInit(&dest, SRT_Set, iTab); 003624 dest.zAffSdst = exprINAffinity(pParse, pExpr); 003625 pSelect->iLimit = 0; 003626 if( addrOnce && OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){ 003627 int regBloom = ++pParse->nMem; 003628 addrBloom = sqlite3VdbeAddOp2(v, OP_Blob, 10000, regBloom); 003629 VdbeComment((v, "Bloom filter")); 003630 dest.iSDParm2 = regBloom; 003631 } 003632 testcase( pSelect->selFlags & SF_Distinct ); 003633 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ 003634 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0); 003635 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest); 003636 sqlite3SelectDelete(pParse->db, pCopy); 003637 sqlite3DbFree(pParse->db, dest.zAffSdst); 003638 if( addrBloom ){ 003639 sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2; 003640 if( dest.iSDParm2==0 ){ 003641 sqlite3VdbeChangeToNoop(v, addrBloom); 003642 }else{ 003643 sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2; 003644 } 003645 } 003646 if( rc ){ 003647 sqlite3KeyInfoUnref(pKeyInfo); 003648 return; 003649 } 003650 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ 003651 assert( pEList!=0 ); 003652 assert( pEList->nExpr>0 ); 003653 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 003654 for(i=0; i<nVal; i++){ 003655 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i); 003656 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( 003657 pParse, p, pEList->a[i].pExpr 003658 ); 003659 } 003660 } 003661 }else if( ALWAYS(pExpr->x.pList!=0) ){ 003662 /* Case 2: expr IN (exprlist) 003663 ** 003664 ** For each expression, build an index key from the evaluation and 003665 ** store it in the temporary table. If <expr> is a column, then use 003666 ** that columns affinity when building index keys. If <expr> is not 003667 ** a column, use numeric affinity. 003668 */ 003669 char affinity; /* Affinity of the LHS of the IN */ 003670 int i; 003671 ExprList *pList = pExpr->x.pList; 003672 struct ExprList_item *pItem; 003673 int r1, r2; 003674 affinity = sqlite3ExprAffinity(pLeft); 003675 if( affinity<=SQLITE_AFF_NONE ){ 003676 affinity = SQLITE_AFF_BLOB; 003677 }else if( affinity==SQLITE_AFF_REAL ){ 003678 affinity = SQLITE_AFF_NUMERIC; 003679 } 003680 if( pKeyInfo ){ 003681 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 003682 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 003683 } 003684 003685 /* Loop through each expression in <exprlist>. */ 003686 r1 = sqlite3GetTempReg(pParse); 003687 r2 = sqlite3GetTempReg(pParse); 003688 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 003689 Expr *pE2 = pItem->pExpr; 003690 003691 /* If the expression is not constant then we will need to 003692 ** disable the test that was generated above that makes sure 003693 ** this code only executes once. Because for a non-constant 003694 ** expression we need to rerun this code each time. 003695 */ 003696 if( addrOnce && !sqlite3ExprIsConstant(pParse, pE2) ){ 003697 sqlite3VdbeChangeToNoop(v, addrOnce-1); 003698 sqlite3VdbeChangeToNoop(v, addrOnce); 003699 ExprClearProperty(pExpr, EP_Subrtn); 003700 addrOnce = 0; 003701 } 003702 003703 /* Evaluate the expression and insert it into the temp table */ 003704 sqlite3ExprCode(pParse, pE2, r1); 003705 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); 003706 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r1, 1); 003707 } 003708 sqlite3ReleaseTempReg(pParse, r1); 003709 sqlite3ReleaseTempReg(pParse, r2); 003710 } 003711 if( pKeyInfo ){ 003712 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); 003713 } 003714 if( addrOnce ){ 003715 sqlite3VdbeAddOp1(v, OP_NullRow, iTab); 003716 sqlite3VdbeJumpHere(v, addrOnce); 003717 /* Subroutine return */ 003718 assert( ExprUseYSub(pExpr) ); 003719 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn 003720 || pParse->nErr ); 003721 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn, 003722 pExpr->y.sub.iAddr, 1); 003723 VdbeCoverage(v); 003724 sqlite3ClearTempRegCache(pParse); 003725 } 003726 } 003727 #endif /* SQLITE_OMIT_SUBQUERY */ 003728 003729 /* 003730 ** Generate code for scalar subqueries used as a subquery expression 003731 ** or EXISTS operator: 003732 ** 003733 ** (SELECT a FROM b) -- subquery 003734 ** EXISTS (SELECT a FROM b) -- EXISTS subquery 003735 ** 003736 ** The pExpr parameter is the SELECT or EXISTS operator to be coded. 003737 ** 003738 ** Return the register that holds the result. For a multi-column SELECT, 003739 ** the result is stored in a contiguous array of registers and the 003740 ** return value is the register of the left-most result column. 003741 ** Return 0 if an error occurs. 003742 */ 003743 #ifndef SQLITE_OMIT_SUBQUERY 003744 int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 003745 int addrOnce = 0; /* Address of OP_Once at top of subroutine */ 003746 int rReg = 0; /* Register storing resulting */ 003747 Select *pSel; /* SELECT statement to encode */ 003748 SelectDest dest; /* How to deal with SELECT result */ 003749 int nReg; /* Registers to allocate */ 003750 Expr *pLimit; /* New limit expression */ 003751 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 003752 int addrExplain; /* Address of OP_Explain instruction */ 003753 #endif 003754 003755 Vdbe *v = pParse->pVdbe; 003756 assert( v!=0 ); 003757 if( pParse->nErr ) return 0; 003758 testcase( pExpr->op==TK_EXISTS ); 003759 testcase( pExpr->op==TK_SELECT ); 003760 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); 003761 assert( ExprUseXSelect(pExpr) ); 003762 pSel = pExpr->x.pSelect; 003763 003764 /* If this routine has already been coded, then invoke it as a 003765 ** subroutine. */ 003766 if( ExprHasProperty(pExpr, EP_Subrtn) ){ 003767 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId)); 003768 assert( ExprUseYSub(pExpr) ); 003769 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn, 003770 pExpr->y.sub.iAddr); 003771 return pExpr->iTable; 003772 } 003773 003774 /* Begin coding the subroutine */ 003775 assert( !ExprUseYWin(pExpr) ); 003776 assert( !ExprHasProperty(pExpr, EP_Reduced|EP_TokenOnly) ); 003777 ExprSetProperty(pExpr, EP_Subrtn); 003778 pExpr->y.sub.regReturn = ++pParse->nMem; 003779 pExpr->y.sub.iAddr = 003780 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1; 003781 003782 /* The evaluation of the EXISTS/SELECT must be repeated every time it 003783 ** is encountered if any of the following is true: 003784 ** 003785 ** * The right-hand side is a correlated subquery 003786 ** * The right-hand side is an expression list containing variables 003787 ** * We are inside a trigger 003788 ** 003789 ** If all of the above are false, then we can run this code just once 003790 ** save the results, and reuse the same result on subsequent invocations. 003791 */ 003792 if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 003793 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); 003794 } 003795 003796 /* For a SELECT, generate code to put the values for all columns of 003797 ** the first row into an array of registers and return the index of 003798 ** the first register. 003799 ** 003800 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists) 003801 ** into a register and return that register number. 003802 ** 003803 ** In both cases, the query is augmented with "LIMIT 1". Any 003804 ** preexisting limit is discarded in place of the new LIMIT 1. 003805 */ 003806 ExplainQueryPlan2(addrExplain, (pParse, 1, "%sSCALAR SUBQUERY %d", 003807 addrOnce?"":"CORRELATED ", pSel->selId)); 003808 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1); 003809 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; 003810 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); 003811 pParse->nMem += nReg; 003812 if( pExpr->op==TK_SELECT ){ 003813 dest.eDest = SRT_Mem; 003814 dest.iSdst = dest.iSDParm; 003815 dest.nSdst = nReg; 003816 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1); 003817 VdbeComment((v, "Init subquery result")); 003818 }else{ 003819 dest.eDest = SRT_Exists; 003820 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 003821 VdbeComment((v, "Init EXISTS result")); 003822 } 003823 if( pSel->pLimit ){ 003824 /* The subquery already has a limit. If the pre-existing limit is X 003825 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */ 003826 sqlite3 *db = pParse->db; 003827 pLimit = sqlite3Expr(db, TK_INTEGER, "0"); 003828 if( pLimit ){ 003829 pLimit->affExpr = SQLITE_AFF_NUMERIC; 003830 pLimit = sqlite3PExpr(pParse, TK_NE, 003831 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit); 003832 } 003833 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft); 003834 pSel->pLimit->pLeft = pLimit; 003835 }else{ 003836 /* If there is no pre-existing limit add a limit of 1 */ 003837 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1"); 003838 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0); 003839 } 003840 pSel->iLimit = 0; 003841 if( sqlite3Select(pParse, pSel, &dest) ){ 003842 pExpr->op2 = pExpr->op; 003843 pExpr->op = TK_ERROR; 003844 return 0; 003845 } 003846 pExpr->iTable = rReg = dest.iSDParm; 003847 ExprSetVVAProperty(pExpr, EP_NoReduce); 003848 if( addrOnce ){ 003849 sqlite3VdbeJumpHere(v, addrOnce); 003850 } 003851 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1); 003852 003853 /* Subroutine return */ 003854 assert( ExprUseYSub(pExpr) ); 003855 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn 003856 || pParse->nErr ); 003857 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn, 003858 pExpr->y.sub.iAddr, 1); 003859 VdbeCoverage(v); 003860 sqlite3ClearTempRegCache(pParse); 003861 return rReg; 003862 } 003863 #endif /* SQLITE_OMIT_SUBQUERY */ 003864 003865 #ifndef SQLITE_OMIT_SUBQUERY 003866 /* 003867 ** Expr pIn is an IN(...) expression. This function checks that the 003868 ** sub-select on the RHS of the IN() operator has the same number of 003869 ** columns as the vector on the LHS. Or, if the RHS of the IN() is not 003870 ** a sub-query, that the LHS is a vector of size 1. 003871 */ 003872 int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){ 003873 int nVector = sqlite3ExprVectorSize(pIn->pLeft); 003874 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){ 003875 if( nVector!=pIn->x.pSelect->pEList->nExpr ){ 003876 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector); 003877 return 1; 003878 } 003879 }else if( nVector!=1 ){ 003880 sqlite3VectorErrorMsg(pParse, pIn->pLeft); 003881 return 1; 003882 } 003883 return 0; 003884 } 003885 #endif 003886 003887 #ifndef SQLITE_OMIT_SUBQUERY 003888 /* 003889 ** Generate code for an IN expression. 003890 ** 003891 ** x IN (SELECT ...) 003892 ** x IN (value, value, ...) 003893 ** 003894 ** The left-hand side (LHS) is a scalar or vector expression. The 003895 ** right-hand side (RHS) is an array of zero or more scalar values, or a 003896 ** subquery. If the RHS is a subquery, the number of result columns must 003897 ** match the number of columns in the vector on the LHS. If the RHS is 003898 ** a list of values, the LHS must be a scalar. 003899 ** 003900 ** The IN operator is true if the LHS value is contained within the RHS. 003901 ** The result is false if the LHS is definitely not in the RHS. The 003902 ** result is NULL if the presence of the LHS in the RHS cannot be 003903 ** determined due to NULLs. 003904 ** 003905 ** This routine generates code that jumps to destIfFalse if the LHS is not 003906 ** contained within the RHS. If due to NULLs we cannot determine if the LHS 003907 ** is contained in the RHS then jump to destIfNull. If the LHS is contained 003908 ** within the RHS then fall through. 003909 ** 003910 ** See the separate in-operator.md documentation file in the canonical 003911 ** SQLite source tree for additional information. 003912 */ 003913 static void sqlite3ExprCodeIN( 003914 Parse *pParse, /* Parsing and code generating context */ 003915 Expr *pExpr, /* The IN expression */ 003916 int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 003917 int destIfNull /* Jump here if the results are unknown due to NULLs */ 003918 ){ 003919 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ 003920 int eType; /* Type of the RHS */ 003921 int rLhs; /* Register(s) holding the LHS values */ 003922 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */ 003923 Vdbe *v; /* Statement under construction */ 003924 int *aiMap = 0; /* Map from vector field to index column */ 003925 char *zAff = 0; /* Affinity string for comparisons */ 003926 int nVector; /* Size of vectors for this IN operator */ 003927 int iDummy; /* Dummy parameter to exprCodeVector() */ 003928 Expr *pLeft; /* The LHS of the IN operator */ 003929 int i; /* loop counter */ 003930 int destStep2; /* Where to jump when NULLs seen in step 2 */ 003931 int destStep6 = 0; /* Start of code for Step 6 */ 003932 int addrTruthOp; /* Address of opcode that determines the IN is true */ 003933 int destNotNull; /* Jump here if a comparison is not true in step 6 */ 003934 int addrTop; /* Top of the step-6 loop */ 003935 int iTab = 0; /* Index to use */ 003936 u8 okConstFactor = pParse->okConstFactor; 003937 003938 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); 003939 pLeft = pExpr->pLeft; 003940 if( sqlite3ExprCheckIN(pParse, pExpr) ) return; 003941 zAff = exprINAffinity(pParse, pExpr); 003942 nVector = sqlite3ExprVectorSize(pExpr->pLeft); 003943 aiMap = (int*)sqlite3DbMallocZero(pParse->db, nVector*sizeof(int)); 003944 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error; 003945 003946 /* Attempt to compute the RHS. After this step, if anything other than 003947 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab 003948 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, 003949 ** the RHS has not yet been coded. */ 003950 v = pParse->pVdbe; 003951 assert( v!=0 ); /* OOM detected prior to this routine */ 003952 VdbeNoopComment((v, "begin IN expr")); 003953 eType = sqlite3FindInIndex(pParse, pExpr, 003954 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, 003955 destIfFalse==destIfNull ? 0 : &rRhsHasNull, 003956 aiMap, &iTab); 003957 003958 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH 003959 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC 003960 ); 003961 #ifdef SQLITE_DEBUG 003962 /* Confirm that aiMap[] contains nVector integer values between 0 and 003963 ** nVector-1. */ 003964 for(i=0; i<nVector; i++){ 003965 int j, cnt; 003966 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++; 003967 assert( cnt==1 ); 003968 } 003969 #endif 003970 003971 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a 003972 ** vector, then it is stored in an array of nVector registers starting 003973 ** at r1. 003974 ** 003975 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector 003976 ** so that the fields are in the same order as an existing index. The 003977 ** aiMap[] array contains a mapping from the original LHS field order to 003978 ** the field order that matches the RHS index. 003979 ** 003980 ** Avoid factoring the LHS of the IN(...) expression out of the loop, 003981 ** even if it is constant, as OP_Affinity may be used on the register 003982 ** by code generated below. */ 003983 assert( pParse->okConstFactor==okConstFactor ); 003984 pParse->okConstFactor = 0; 003985 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy); 003986 pParse->okConstFactor = okConstFactor; 003987 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */ 003988 if( i==nVector ){ 003989 /* LHS fields are not reordered */ 003990 rLhs = rLhsOrig; 003991 }else{ 003992 /* Need to reorder the LHS fields according to aiMap */ 003993 rLhs = sqlite3GetTempRange(pParse, nVector); 003994 for(i=0; i<nVector; i++){ 003995 sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0); 003996 } 003997 } 003998 003999 /* If sqlite3FindInIndex() did not find or create an index that is 004000 ** suitable for evaluating the IN operator, then evaluate using a 004001 ** sequence of comparisons. 004002 ** 004003 ** This is step (1) in the in-operator.md optimized algorithm. 004004 */ 004005 if( eType==IN_INDEX_NOOP ){ 004006 ExprList *pList; 004007 CollSeq *pColl; 004008 int labelOk = sqlite3VdbeMakeLabel(pParse); 004009 int r2, regToFree; 004010 int regCkNull = 0; 004011 int ii; 004012 assert( ExprUseXList(pExpr) ); 004013 pList = pExpr->x.pList; 004014 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 004015 if( destIfNull!=destIfFalse ){ 004016 regCkNull = sqlite3GetTempReg(pParse); 004017 sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull); 004018 } 004019 for(ii=0; ii<pList->nExpr; ii++){ 004020 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); 004021 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ 004022 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); 004023 } 004024 sqlite3ReleaseTempReg(pParse, regToFree); 004025 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ 004026 int op = rLhs!=r2 ? OP_Eq : OP_NotNull; 004027 sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2, 004028 (void*)pColl, P4_COLLSEQ); 004029 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq); 004030 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq); 004031 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull); 004032 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull); 004033 sqlite3VdbeChangeP5(v, zAff[0]); 004034 }else{ 004035 int op = rLhs!=r2 ? OP_Ne : OP_IsNull; 004036 assert( destIfNull==destIfFalse ); 004037 sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2, 004038 (void*)pColl, P4_COLLSEQ); 004039 VdbeCoverageIf(v, op==OP_Ne); 004040 VdbeCoverageIf(v, op==OP_IsNull); 004041 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL); 004042 } 004043 } 004044 if( regCkNull ){ 004045 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 004046 sqlite3VdbeGoto(v, destIfFalse); 004047 } 004048 sqlite3VdbeResolveLabel(v, labelOk); 004049 sqlite3ReleaseTempReg(pParse, regCkNull); 004050 goto sqlite3ExprCodeIN_finished; 004051 } 004052 004053 /* Step 2: Check to see if the LHS contains any NULL columns. If the 004054 ** LHS does contain NULLs then the result must be either FALSE or NULL. 004055 ** We will then skip the binary search of the RHS. 004056 */ 004057 if( destIfNull==destIfFalse ){ 004058 destStep2 = destIfFalse; 004059 }else{ 004060 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse); 004061 } 004062 for(i=0; i<nVector; i++){ 004063 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i); 004064 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error; 004065 if( sqlite3ExprCanBeNull(p) ){ 004066 sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); 004067 VdbeCoverage(v); 004068 } 004069 } 004070 004071 /* Step 3. The LHS is now known to be non-NULL. Do the binary search 004072 ** of the RHS using the LHS as a probe. If found, the result is 004073 ** true. 004074 */ 004075 if( eType==IN_INDEX_ROWID ){ 004076 /* In this case, the RHS is the ROWID of table b-tree and so we also 004077 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4 004078 ** into a single opcode. */ 004079 sqlite3VdbeAddOp3(v, OP_SeekRowid, iTab, destIfFalse, rLhs); 004080 VdbeCoverage(v); 004081 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */ 004082 }else{ 004083 sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector); 004084 if( destIfFalse==destIfNull ){ 004085 /* Combine Step 3 and Step 5 into a single opcode */ 004086 if( ExprHasProperty(pExpr, EP_Subrtn) ){ 004087 const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr); 004088 assert( pOp->opcode==OP_Once || pParse->nErr ); 004089 if( pOp->opcode==OP_Once && pOp->p3>0 ){ 004090 assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ); 004091 sqlite3VdbeAddOp4Int(v, OP_Filter, pOp->p3, destIfFalse, 004092 rLhs, nVector); VdbeCoverage(v); 004093 } 004094 } 004095 sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse, 004096 rLhs, nVector); VdbeCoverage(v); 004097 goto sqlite3ExprCodeIN_finished; 004098 } 004099 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */ 004100 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, iTab, 0, 004101 rLhs, nVector); VdbeCoverage(v); 004102 } 004103 004104 /* Step 4. If the RHS is known to be non-NULL and we did not find 004105 ** an match on the search above, then the result must be FALSE. 004106 */ 004107 if( rRhsHasNull && nVector==1 ){ 004108 sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse); 004109 VdbeCoverage(v); 004110 } 004111 004112 /* Step 5. If we do not care about the difference between NULL and 004113 ** FALSE, then just return false. 004114 */ 004115 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse); 004116 004117 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS. 004118 ** If any comparison is NULL, then the result is NULL. If all 004119 ** comparisons are FALSE then the final result is FALSE. 004120 ** 004121 ** For a scalar LHS, it is sufficient to check just the first row 004122 ** of the RHS. 004123 */ 004124 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6); 004125 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse); 004126 VdbeCoverage(v); 004127 if( nVector>1 ){ 004128 destNotNull = sqlite3VdbeMakeLabel(pParse); 004129 }else{ 004130 /* For nVector==1, combine steps 6 and 7 by immediately returning 004131 ** FALSE if the first comparison is not NULL */ 004132 destNotNull = destIfFalse; 004133 } 004134 for(i=0; i<nVector; i++){ 004135 Expr *p; 004136 CollSeq *pColl; 004137 int r3 = sqlite3GetTempReg(pParse); 004138 p = sqlite3VectorFieldSubexpr(pLeft, i); 004139 pColl = sqlite3ExprCollSeq(pParse, p); 004140 sqlite3VdbeAddOp3(v, OP_Column, iTab, i, r3); 004141 sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3, 004142 (void*)pColl, P4_COLLSEQ); 004143 VdbeCoverage(v); 004144 sqlite3ReleaseTempReg(pParse, r3); 004145 } 004146 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); 004147 if( nVector>1 ){ 004148 sqlite3VdbeResolveLabel(v, destNotNull); 004149 sqlite3VdbeAddOp2(v, OP_Next, iTab, addrTop+1); 004150 VdbeCoverage(v); 004151 004152 /* Step 7: If we reach this point, we know that the result must 004153 ** be false. */ 004154 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 004155 } 004156 004157 /* Jumps here in order to return true. */ 004158 sqlite3VdbeJumpHere(v, addrTruthOp); 004159 004160 sqlite3ExprCodeIN_finished: 004161 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs); 004162 VdbeComment((v, "end IN expr")); 004163 sqlite3ExprCodeIN_oom_error: 004164 sqlite3DbFree(pParse->db, aiMap); 004165 sqlite3DbFree(pParse->db, zAff); 004166 } 004167 #endif /* SQLITE_OMIT_SUBQUERY */ 004168 004169 #ifndef SQLITE_OMIT_FLOATING_POINT 004170 /* 004171 ** Generate an instruction that will put the floating point 004172 ** value described by z[0..n-1] into register iMem. 004173 ** 004174 ** The z[] string will probably not be zero-terminated. But the 004175 ** z[n] character is guaranteed to be something that does not look 004176 ** like the continuation of the number. 004177 */ 004178 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 004179 if( ALWAYS(z!=0) ){ 004180 double value; 004181 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 004182 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 004183 if( negateFlag ) value = -value; 004184 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); 004185 } 004186 } 004187 #endif 004188 004189 004190 /* 004191 ** Generate an instruction that will put the integer describe by 004192 ** text z[0..n-1] into register iMem. 004193 ** 004194 ** Expr.u.zToken is always UTF8 and zero-terminated. 004195 */ 004196 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 004197 Vdbe *v = pParse->pVdbe; 004198 if( pExpr->flags & EP_IntValue ){ 004199 int i = pExpr->u.iValue; 004200 assert( i>=0 ); 004201 if( negFlag ) i = -i; 004202 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 004203 }else{ 004204 int c; 004205 i64 value; 004206 const char *z = pExpr->u.zToken; 004207 assert( z!=0 ); 004208 c = sqlite3DecOrHexToI64(z, &value); 004209 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){ 004210 #ifdef SQLITE_OMIT_FLOATING_POINT 004211 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr); 004212 #else 004213 #ifndef SQLITE_OMIT_HEX_INTEGER 004214 if( sqlite3_strnicmp(z,"0x",2)==0 ){ 004215 sqlite3ErrorMsg(pParse, "hex literal too big: %s%#T", 004216 negFlag?"-":"",pExpr); 004217 }else 004218 #endif 004219 { 004220 codeReal(v, z, negFlag, iMem); 004221 } 004222 #endif 004223 }else{ 004224 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; } 004225 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); 004226 } 004227 } 004228 } 004229 004230 004231 /* Generate code that will load into register regOut a value that is 004232 ** appropriate for the iIdxCol-th column of index pIdx. 004233 */ 004234 void sqlite3ExprCodeLoadIndexColumn( 004235 Parse *pParse, /* The parsing context */ 004236 Index *pIdx, /* The index whose column is to be loaded */ 004237 int iTabCur, /* Cursor pointing to a table row */ 004238 int iIdxCol, /* The column of the index to be loaded */ 004239 int regOut /* Store the index column value in this register */ 004240 ){ 004241 i16 iTabCol = pIdx->aiColumn[iIdxCol]; 004242 if( iTabCol==XN_EXPR ){ 004243 assert( pIdx->aColExpr ); 004244 assert( pIdx->aColExpr->nExpr>iIdxCol ); 004245 pParse->iSelfTab = iTabCur + 1; 004246 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); 004247 pParse->iSelfTab = 0; 004248 }else{ 004249 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, 004250 iTabCol, regOut); 004251 } 004252 } 004253 004254 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 004255 /* 004256 ** Generate code that will compute the value of generated column pCol 004257 ** and store the result in register regOut 004258 */ 004259 void sqlite3ExprCodeGeneratedColumn( 004260 Parse *pParse, /* Parsing context */ 004261 Table *pTab, /* Table containing the generated column */ 004262 Column *pCol, /* The generated column */ 004263 int regOut /* Put the result in this register */ 004264 ){ 004265 int iAddr; 004266 Vdbe *v = pParse->pVdbe; 004267 int nErr = pParse->nErr; 004268 assert( v!=0 ); 004269 assert( pParse->iSelfTab!=0 ); 004270 if( pParse->iSelfTab>0 ){ 004271 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut); 004272 }else{ 004273 iAddr = 0; 004274 } 004275 sqlite3ExprCodeCopy(pParse, sqlite3ColumnExpr(pTab,pCol), regOut); 004276 if( pCol->affinity>=SQLITE_AFF_TEXT ){ 004277 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1); 004278 } 004279 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr); 004280 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1; 004281 } 004282 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */ 004283 004284 /* 004285 ** Generate code to extract the value of the iCol-th column of a table. 004286 */ 004287 void sqlite3ExprCodeGetColumnOfTable( 004288 Vdbe *v, /* Parsing context */ 004289 Table *pTab, /* The table containing the value */ 004290 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 004291 int iCol, /* Index of the column to extract */ 004292 int regOut /* Extract the value into this register */ 004293 ){ 004294 Column *pCol; 004295 assert( v!=0 ); 004296 assert( pTab!=0 ); 004297 assert( iCol!=XN_EXPR ); 004298 if( iCol<0 || iCol==pTab->iPKey ){ 004299 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 004300 VdbeComment((v, "%s.rowid", pTab->zName)); 004301 }else{ 004302 int op; 004303 int x; 004304 if( IsVirtual(pTab) ){ 004305 op = OP_VColumn; 004306 x = iCol; 004307 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 004308 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){ 004309 Parse *pParse = sqlite3VdbeParser(v); 004310 if( pCol->colFlags & COLFLAG_BUSY ){ 004311 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", 004312 pCol->zCnName); 004313 }else{ 004314 int savedSelfTab = pParse->iSelfTab; 004315 pCol->colFlags |= COLFLAG_BUSY; 004316 pParse->iSelfTab = iTabCur+1; 004317 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, regOut); 004318 pParse->iSelfTab = savedSelfTab; 004319 pCol->colFlags &= ~COLFLAG_BUSY; 004320 } 004321 return; 004322 #endif 004323 }else if( !HasRowid(pTab) ){ 004324 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) ); 004325 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 004326 op = OP_Column; 004327 }else{ 004328 x = sqlite3TableColumnToStorage(pTab,iCol); 004329 testcase( x!=iCol ); 004330 op = OP_Column; 004331 } 004332 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 004333 sqlite3ColumnDefault(v, pTab, iCol, regOut); 004334 } 004335 } 004336 004337 /* 004338 ** Generate code that will extract the iColumn-th column from 004339 ** table pTab and store the column value in register iReg. 004340 ** 004341 ** There must be an open cursor to pTab in iTable when this routine 004342 ** is called. If iColumn<0 then code is generated that extracts the rowid. 004343 */ 004344 int sqlite3ExprCodeGetColumn( 004345 Parse *pParse, /* Parsing and code generating context */ 004346 Table *pTab, /* Description of the table we are reading from */ 004347 int iColumn, /* Index of the table column */ 004348 int iTable, /* The cursor pointing to the table */ 004349 int iReg, /* Store results here */ 004350 u8 p5 /* P5 value for OP_Column + FLAGS */ 004351 ){ 004352 assert( pParse->pVdbe!=0 ); 004353 assert( (p5 & (OPFLAG_NOCHNG|OPFLAG_TYPEOFARG|OPFLAG_LENGTHARG))==p5 ); 004354 assert( IsVirtual(pTab) || (p5 & OPFLAG_NOCHNG)==0 ); 004355 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg); 004356 if( p5 ){ 004357 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe); 004358 if( pOp->opcode==OP_Column ) pOp->p5 = p5; 004359 if( pOp->opcode==OP_VColumn ) pOp->p5 = (p5 & OPFLAG_NOCHNG); 004360 } 004361 return iReg; 004362 } 004363 004364 /* 004365 ** Generate code to move content from registers iFrom...iFrom+nReg-1 004366 ** over to iTo..iTo+nReg-1. 004367 */ 004368 void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ 004369 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); 004370 } 004371 004372 /* 004373 ** Convert a scalar expression node to a TK_REGISTER referencing 004374 ** register iReg. The caller must ensure that iReg already contains 004375 ** the correct value for the expression. 004376 */ 004377 void sqlite3ExprToRegister(Expr *pExpr, int iReg){ 004378 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr); 004379 if( NEVER(p==0) ) return; 004380 if( p->op==TK_REGISTER ){ 004381 assert( p->iTable==iReg ); 004382 }else{ 004383 p->op2 = p->op; 004384 p->op = TK_REGISTER; 004385 p->iTable = iReg; 004386 ExprClearProperty(p, EP_Skip); 004387 } 004388 } 004389 004390 /* 004391 ** Evaluate an expression (either a vector or a scalar expression) and store 004392 ** the result in contiguous temporary registers. Return the index of 004393 ** the first register used to store the result. 004394 ** 004395 ** If the returned result register is a temporary scalar, then also write 004396 ** that register number into *piFreeable. If the returned result register 004397 ** is not a temporary or if the expression is a vector set *piFreeable 004398 ** to 0. 004399 */ 004400 static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){ 004401 int iResult; 004402 int nResult = sqlite3ExprVectorSize(p); 004403 if( nResult==1 ){ 004404 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable); 004405 }else{ 004406 *piFreeable = 0; 004407 if( p->op==TK_SELECT ){ 004408 #if SQLITE_OMIT_SUBQUERY 004409 iResult = 0; 004410 #else 004411 iResult = sqlite3CodeSubselect(pParse, p); 004412 #endif 004413 }else{ 004414 int i; 004415 iResult = pParse->nMem+1; 004416 pParse->nMem += nResult; 004417 assert( ExprUseXList(p) ); 004418 for(i=0; i<nResult; i++){ 004419 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult); 004420 } 004421 } 004422 } 004423 return iResult; 004424 } 004425 004426 /* 004427 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5) 004428 ** so that a subsequent copy will not be merged into this one. 004429 */ 004430 static void setDoNotMergeFlagOnCopy(Vdbe *v){ 004431 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){ 004432 sqlite3VdbeChangeP5(v, 1); /* Tag trailing OP_Copy as not mergeable */ 004433 } 004434 } 004435 004436 /* 004437 ** Generate code to implement special SQL functions that are implemented 004438 ** in-line rather than by using the usual callbacks. 004439 */ 004440 static int exprCodeInlineFunction( 004441 Parse *pParse, /* Parsing context */ 004442 ExprList *pFarg, /* List of function arguments */ 004443 int iFuncId, /* Function ID. One of the INTFUNC_... values */ 004444 int target /* Store function result in this register */ 004445 ){ 004446 int nFarg; 004447 Vdbe *v = pParse->pVdbe; 004448 assert( v!=0 ); 004449 assert( pFarg!=0 ); 004450 nFarg = pFarg->nExpr; 004451 assert( nFarg>0 ); /* All in-line functions have at least one argument */ 004452 switch( iFuncId ){ 004453 case INLINEFUNC_coalesce: { 004454 /* Attempt a direct implementation of the built-in COALESCE() and 004455 ** IFNULL() functions. This avoids unnecessary evaluation of 004456 ** arguments past the first non-NULL argument. 004457 */ 004458 int endCoalesce = sqlite3VdbeMakeLabel(pParse); 004459 int i; 004460 assert( nFarg>=2 ); 004461 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 004462 for(i=1; i<nFarg; i++){ 004463 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); 004464 VdbeCoverage(v); 004465 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); 004466 } 004467 setDoNotMergeFlagOnCopy(v); 004468 sqlite3VdbeResolveLabel(v, endCoalesce); 004469 break; 004470 } 004471 case INLINEFUNC_iif: { 004472 Expr caseExpr; 004473 memset(&caseExpr, 0, sizeof(caseExpr)); 004474 caseExpr.op = TK_CASE; 004475 caseExpr.x.pList = pFarg; 004476 return sqlite3ExprCodeTarget(pParse, &caseExpr, target); 004477 } 004478 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 004479 case INLINEFUNC_sqlite_offset: { 004480 Expr *pArg = pFarg->a[0].pExpr; 004481 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){ 004482 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target); 004483 }else{ 004484 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 004485 } 004486 break; 004487 } 004488 #endif 004489 default: { 004490 /* The UNLIKELY() function is a no-op. The result is the value 004491 ** of the first argument. 004492 */ 004493 assert( nFarg==1 || nFarg==2 ); 004494 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); 004495 break; 004496 } 004497 004498 /*********************************************************************** 004499 ** Test-only SQL functions that are only usable if enabled 004500 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS 004501 */ 004502 #if !defined(SQLITE_UNTESTABLE) 004503 case INLINEFUNC_expr_compare: { 004504 /* Compare two expressions using sqlite3ExprCompare() */ 004505 assert( nFarg==2 ); 004506 sqlite3VdbeAddOp2(v, OP_Integer, 004507 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1), 004508 target); 004509 break; 004510 } 004511 004512 case INLINEFUNC_expr_implies_expr: { 004513 /* Compare two expressions using sqlite3ExprImpliesExpr() */ 004514 assert( nFarg==2 ); 004515 sqlite3VdbeAddOp2(v, OP_Integer, 004516 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1), 004517 target); 004518 break; 004519 } 004520 004521 case INLINEFUNC_implies_nonnull_row: { 004522 /* Result of sqlite3ExprImpliesNonNullRow() */ 004523 Expr *pA1; 004524 assert( nFarg==2 ); 004525 pA1 = pFarg->a[1].pExpr; 004526 if( pA1->op==TK_COLUMN ){ 004527 sqlite3VdbeAddOp2(v, OP_Integer, 004528 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1), 004529 target); 004530 }else{ 004531 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 004532 } 004533 break; 004534 } 004535 004536 case INLINEFUNC_affinity: { 004537 /* The AFFINITY() function evaluates to a string that describes 004538 ** the type affinity of the argument. This is used for testing of 004539 ** the SQLite type logic. 004540 */ 004541 const char *azAff[] = { "blob", "text", "numeric", "integer", 004542 "real", "flexnum" }; 004543 char aff; 004544 assert( nFarg==1 ); 004545 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr); 004546 assert( aff<=SQLITE_AFF_NONE 004547 || (aff>=SQLITE_AFF_BLOB && aff<=SQLITE_AFF_FLEXNUM) ); 004548 sqlite3VdbeLoadString(v, target, 004549 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]); 004550 break; 004551 } 004552 #endif /* !defined(SQLITE_UNTESTABLE) */ 004553 } 004554 return target; 004555 } 004556 004557 /* 004558 ** Expression Node callback for sqlite3ExprCanReturnSubtype(). 004559 ** 004560 ** Only a function call is able to return a subtype. So if the node 004561 ** is not a function call, return WRC_Prune immediately. 004562 ** 004563 ** A function call is able to return a subtype if it has the 004564 ** SQLITE_RESULT_SUBTYPE property. 004565 ** 004566 ** Assume that every function is able to pass-through a subtype from 004567 ** one of its argument (using sqlite3_result_value()). Most functions 004568 ** are not this way, but we don't have a mechanism to distinguish those 004569 ** that are from those that are not, so assume they all work this way. 004570 ** That means that if one of its arguments is another function and that 004571 ** other function is able to return a subtype, then this function is 004572 ** able to return a subtype. 004573 */ 004574 static int exprNodeCanReturnSubtype(Walker *pWalker, Expr *pExpr){ 004575 int n; 004576 FuncDef *pDef; 004577 sqlite3 *db; 004578 if( pExpr->op!=TK_FUNCTION ){ 004579 return WRC_Prune; 004580 } 004581 assert( ExprUseXList(pExpr) ); 004582 db = pWalker->pParse->db; 004583 n = ALWAYS(pExpr->x.pList) ? pExpr->x.pList->nExpr : 0; 004584 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0); 004585 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){ 004586 pWalker->eCode = 1; 004587 return WRC_Prune; 004588 } 004589 return WRC_Continue; 004590 } 004591 004592 /* 004593 ** Return TRUE if expression pExpr is able to return a subtype. 004594 ** 004595 ** A TRUE return does not guarantee that a subtype will be returned. 004596 ** It only indicates that a subtype return is possible. False positives 004597 ** are acceptable as they only disable an optimization. False negatives, 004598 ** on the other hand, can lead to incorrect answers. 004599 */ 004600 static int sqlite3ExprCanReturnSubtype(Parse *pParse, Expr *pExpr){ 004601 Walker w; 004602 memset(&w, 0, sizeof(w)); 004603 w.pParse = pParse; 004604 w.xExprCallback = exprNodeCanReturnSubtype; 004605 sqlite3WalkExpr(&w, pExpr); 004606 return w.eCode; 004607 } 004608 004609 004610 /* 004611 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr. 004612 ** If it is, then resolve the expression by reading from the index and 004613 ** return the register into which the value has been read. If pExpr is 004614 ** not an indexed expression, then return negative. 004615 */ 004616 static SQLITE_NOINLINE int sqlite3IndexedExprLookup( 004617 Parse *pParse, /* The parsing context */ 004618 Expr *pExpr, /* The expression to potentially bypass */ 004619 int target /* Where to store the result of the expression */ 004620 ){ 004621 IndexedExpr *p; 004622 Vdbe *v; 004623 for(p=pParse->pIdxEpr; p; p=p->pIENext){ 004624 u8 exprAff; 004625 int iDataCur = p->iDataCur; 004626 if( iDataCur<0 ) continue; 004627 if( pParse->iSelfTab ){ 004628 if( p->iDataCur!=pParse->iSelfTab-1 ) continue; 004629 iDataCur = -1; 004630 } 004631 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue; 004632 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC ); 004633 exprAff = sqlite3ExprAffinity(pExpr); 004634 if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB) 004635 || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT) 004636 || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC) 004637 ){ 004638 /* Affinity mismatch on a generated column */ 004639 continue; 004640 } 004641 004642 004643 /* Functions that might set a subtype should not be replaced by the 004644 ** value taken from an expression index if they are themselves an 004645 ** argument to another scalar function or aggregate. 004646 ** https://sqlite.org/forum/forumpost/68d284c86b082c3e */ 004647 if( ExprHasProperty(pExpr, EP_SubtArg) 004648 && sqlite3ExprCanReturnSubtype(pParse, pExpr) 004649 ){ 004650 continue; 004651 } 004652 004653 v = pParse->pVdbe; 004654 assert( v!=0 ); 004655 if( p->bMaybeNullRow ){ 004656 /* If the index is on a NULL row due to an outer join, then we 004657 ** cannot extract the value from the index. The value must be 004658 ** computed using the original expression. */ 004659 int addr = sqlite3VdbeCurrentAddr(v); 004660 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target); 004661 VdbeCoverage(v); 004662 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target); 004663 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol)); 004664 sqlite3VdbeGoto(v, 0); 004665 p = pParse->pIdxEpr; 004666 pParse->pIdxEpr = 0; 004667 sqlite3ExprCode(pParse, pExpr, target); 004668 pParse->pIdxEpr = p; 004669 sqlite3VdbeJumpHere(v, addr+2); 004670 }else{ 004671 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target); 004672 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol)); 004673 } 004674 return target; 004675 } 004676 return -1; /* Not found */ 004677 } 004678 004679 004680 /* 004681 ** Expresion pExpr is guaranteed to be a TK_COLUMN or equivalent. This 004682 ** function checks the Parse.pIdxPartExpr list to see if this column 004683 ** can be replaced with a constant value. If so, it generates code to 004684 ** put the constant value in a register (ideally, but not necessarily, 004685 ** register iTarget) and returns the register number. 004686 ** 004687 ** Or, if the TK_COLUMN cannot be replaced by a constant, zero is 004688 ** returned. 004689 */ 004690 static int exprPartidxExprLookup(Parse *pParse, Expr *pExpr, int iTarget){ 004691 IndexedExpr *p; 004692 for(p=pParse->pIdxPartExpr; p; p=p->pIENext){ 004693 if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){ 004694 Vdbe *v = pParse->pVdbe; 004695 int addr = 0; 004696 int ret; 004697 004698 if( p->bMaybeNullRow ){ 004699 addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur); 004700 } 004701 ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget); 004702 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0, 004703 (const char*)&p->aff, 1); 004704 if( addr ){ 004705 sqlite3VdbeJumpHere(v, addr); 004706 sqlite3VdbeChangeP3(v, addr, ret); 004707 } 004708 return ret; 004709 } 004710 } 004711 return 0; 004712 } 004713 004714 004715 /* 004716 ** Generate code into the current Vdbe to evaluate the given 004717 ** expression. Attempt to store the results in register "target". 004718 ** Return the register where results are stored. 004719 ** 004720 ** With this routine, there is no guarantee that results will 004721 ** be stored in target. The result might be stored in some other 004722 ** register if it is convenient to do so. The calling function 004723 ** must check the return code and move the results to the desired 004724 ** register. 004725 */ 004726 int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 004727 Vdbe *v = pParse->pVdbe; /* The VM under construction */ 004728 int op; /* The opcode being coded */ 004729 int inReg = target; /* Results stored in register inReg */ 004730 int regFree1 = 0; /* If non-zero free this temporary register */ 004731 int regFree2 = 0; /* If non-zero free this temporary register */ 004732 int r1, r2; /* Various register numbers */ 004733 Expr tempX; /* Temporary expression node */ 004734 int p5 = 0; 004735 004736 assert( target>0 && target<=pParse->nMem ); 004737 assert( v!=0 ); 004738 004739 expr_code_doover: 004740 if( pExpr==0 ){ 004741 op = TK_NULL; 004742 }else if( pParse->pIdxEpr!=0 004743 && !ExprHasProperty(pExpr, EP_Leaf) 004744 && (r1 = sqlite3IndexedExprLookup(pParse, pExpr, target))>=0 004745 ){ 004746 return r1; 004747 }else{ 004748 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); 004749 op = pExpr->op; 004750 } 004751 assert( op!=TK_ORDER ); 004752 switch( op ){ 004753 case TK_AGG_COLUMN: { 004754 AggInfo *pAggInfo = pExpr->pAggInfo; 004755 struct AggInfo_col *pCol; 004756 assert( pAggInfo!=0 ); 004757 assert( pExpr->iAgg>=0 ); 004758 if( pExpr->iAgg>=pAggInfo->nColumn ){ 004759 /* Happens when the left table of a RIGHT JOIN is null and 004760 ** is using an expression index */ 004761 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 004762 #ifdef SQLITE_VDBE_COVERAGE 004763 /* Verify that the OP_Null above is exercised by tests 004764 ** tag-20230325-2 */ 004765 sqlite3VdbeAddOp3(v, OP_NotNull, target, 1, 20230325); 004766 VdbeCoverageNeverTaken(v); 004767 #endif 004768 break; 004769 } 004770 pCol = &pAggInfo->aCol[pExpr->iAgg]; 004771 if( !pAggInfo->directMode ){ 004772 return AggInfoColumnReg(pAggInfo, pExpr->iAgg); 004773 }else if( pAggInfo->useSortingIdx ){ 004774 Table *pTab = pCol->pTab; 004775 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 004776 pCol->iSorterColumn, target); 004777 if( pTab==0 ){ 004778 /* No comment added */ 004779 }else if( pCol->iColumn<0 ){ 004780 VdbeComment((v,"%s.rowid",pTab->zName)); 004781 }else{ 004782 VdbeComment((v,"%s.%s", 004783 pTab->zName, pTab->aCol[pCol->iColumn].zCnName)); 004784 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){ 004785 sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 004786 } 004787 } 004788 return target; 004789 }else if( pExpr->y.pTab==0 ){ 004790 /* This case happens when the argument to an aggregate function 004791 ** is rewritten by aggregateConvertIndexedExprRefToColumn() */ 004792 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target); 004793 return target; 004794 } 004795 /* Otherwise, fall thru into the TK_COLUMN case */ 004796 /* no break */ deliberate_fall_through 004797 } 004798 case TK_COLUMN: { 004799 int iTab = pExpr->iTable; 004800 int iReg; 004801 if( ExprHasProperty(pExpr, EP_FixedCol) ){ 004802 /* This COLUMN expression is really a constant due to WHERE clause 004803 ** constraints, and that constant is coded by the pExpr->pLeft 004804 ** expression. However, make sure the constant has the correct 004805 ** datatype by applying the Affinity of the table column to the 004806 ** constant. 004807 */ 004808 int aff; 004809 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target); 004810 assert( ExprUseYTab(pExpr) ); 004811 assert( pExpr->y.pTab!=0 ); 004812 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); 004813 if( aff>SQLITE_AFF_BLOB ){ 004814 static const char zAff[] = "B\000C\000D\000E\000F"; 004815 assert( SQLITE_AFF_BLOB=='A' ); 004816 assert( SQLITE_AFF_TEXT=='B' ); 004817 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0, 004818 &zAff[(aff-'B')*2], P4_STATIC); 004819 } 004820 return iReg; 004821 } 004822 if( iTab<0 ){ 004823 if( pParse->iSelfTab<0 ){ 004824 /* Other columns in the same row for CHECK constraints or 004825 ** generated columns or for inserting into partial index. 004826 ** The row is unpacked into registers beginning at 004827 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register 004828 ** immediately prior to the first column. 004829 */ 004830 Column *pCol; 004831 Table *pTab; 004832 int iSrc; 004833 int iCol = pExpr->iColumn; 004834 assert( ExprUseYTab(pExpr) ); 004835 pTab = pExpr->y.pTab; 004836 assert( pTab!=0 ); 004837 assert( iCol>=XN_ROWID ); 004838 assert( iCol<pTab->nCol ); 004839 if( iCol<0 ){ 004840 return -1-pParse->iSelfTab; 004841 } 004842 pCol = pTab->aCol + iCol; 004843 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) ); 004844 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab; 004845 #ifndef SQLITE_OMIT_GENERATED_COLUMNS 004846 if( pCol->colFlags & COLFLAG_GENERATED ){ 004847 if( pCol->colFlags & COLFLAG_BUSY ){ 004848 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", 004849 pCol->zCnName); 004850 return 0; 004851 } 004852 pCol->colFlags |= COLFLAG_BUSY; 004853 if( pCol->colFlags & COLFLAG_NOTAVAIL ){ 004854 sqlite3ExprCodeGeneratedColumn(pParse, pTab, pCol, iSrc); 004855 } 004856 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL); 004857 return iSrc; 004858 }else 004859 #endif /* SQLITE_OMIT_GENERATED_COLUMNS */ 004860 if( pCol->affinity==SQLITE_AFF_REAL ){ 004861 sqlite3VdbeAddOp2(v, OP_SCopy, iSrc, target); 004862 sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 004863 return target; 004864 }else{ 004865 return iSrc; 004866 } 004867 }else{ 004868 /* Coding an expression that is part of an index where column names 004869 ** in the index refer to the table to which the index belongs */ 004870 iTab = pParse->iSelfTab - 1; 004871 } 004872 } 004873 else if( pParse->pIdxPartExpr 004874 && 0!=(r1 = exprPartidxExprLookup(pParse, pExpr, target)) 004875 ){ 004876 return r1; 004877 } 004878 assert( ExprUseYTab(pExpr) ); 004879 assert( pExpr->y.pTab!=0 ); 004880 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab, 004881 pExpr->iColumn, iTab, target, 004882 pExpr->op2); 004883 return iReg; 004884 } 004885 case TK_INTEGER: { 004886 codeInteger(pParse, pExpr, 0, target); 004887 return target; 004888 } 004889 case TK_TRUEFALSE: { 004890 sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target); 004891 return target; 004892 } 004893 #ifndef SQLITE_OMIT_FLOATING_POINT 004894 case TK_FLOAT: { 004895 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 004896 codeReal(v, pExpr->u.zToken, 0, target); 004897 return target; 004898 } 004899 #endif 004900 case TK_STRING: { 004901 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 004902 sqlite3VdbeLoadString(v, target, pExpr->u.zToken); 004903 return target; 004904 } 004905 default: { 004906 /* Make NULL the default case so that if a bug causes an illegal 004907 ** Expr node to be passed into this function, it will be handled 004908 ** sanely and not crash. But keep the assert() to bring the problem 004909 ** to the attention of the developers. */ 004910 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed ); 004911 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 004912 return target; 004913 } 004914 #ifndef SQLITE_OMIT_BLOB_LITERAL 004915 case TK_BLOB: { 004916 int n; 004917 const char *z; 004918 char *zBlob; 004919 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 004920 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 004921 assert( pExpr->u.zToken[1]=='\'' ); 004922 z = &pExpr->u.zToken[2]; 004923 n = sqlite3Strlen30(z) - 1; 004924 assert( z[n]=='\'' ); 004925 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 004926 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 004927 return target; 004928 } 004929 #endif 004930 case TK_VARIABLE: { 004931 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 004932 assert( pExpr->u.zToken!=0 ); 004933 assert( pExpr->u.zToken[0]!=0 ); 004934 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); 004935 return target; 004936 } 004937 case TK_REGISTER: { 004938 return pExpr->iTable; 004939 } 004940 #ifndef SQLITE_OMIT_CAST 004941 case TK_CAST: { 004942 /* Expressions of the form: CAST(pLeft AS token) */ 004943 sqlite3ExprCode(pParse, pExpr->pLeft, target); 004944 assert( inReg==target ); 004945 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 004946 sqlite3VdbeAddOp2(v, OP_Cast, target, 004947 sqlite3AffinityType(pExpr->u.zToken, 0)); 004948 return inReg; 004949 } 004950 #endif /* SQLITE_OMIT_CAST */ 004951 case TK_IS: 004952 case TK_ISNOT: 004953 op = (op==TK_IS) ? TK_EQ : TK_NE; 004954 p5 = SQLITE_NULLEQ; 004955 /* fall-through */ 004956 case TK_LT: 004957 case TK_LE: 004958 case TK_GT: 004959 case TK_GE: 004960 case TK_NE: 004961 case TK_EQ: { 004962 Expr *pLeft = pExpr->pLeft; 004963 if( sqlite3ExprIsVector(pLeft) ){ 004964 codeVectorCompare(pParse, pExpr, target, op, p5); 004965 }else{ 004966 r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 004967 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 004968 sqlite3VdbeAddOp2(v, OP_Integer, 1, inReg); 004969 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2, 004970 sqlite3VdbeCurrentAddr(v)+2, p5, 004971 ExprHasProperty(pExpr,EP_Commuted)); 004972 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 004973 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 004974 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 004975 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 004976 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 004977 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 004978 if( p5==SQLITE_NULLEQ ){ 004979 sqlite3VdbeAddOp2(v, OP_Integer, 0, inReg); 004980 }else{ 004981 sqlite3VdbeAddOp3(v, OP_ZeroOrNull, r1, inReg, r2); 004982 } 004983 testcase( regFree1==0 ); 004984 testcase( regFree2==0 ); 004985 } 004986 break; 004987 } 004988 case TK_AND: 004989 case TK_OR: 004990 case TK_PLUS: 004991 case TK_STAR: 004992 case TK_MINUS: 004993 case TK_REM: 004994 case TK_BITAND: 004995 case TK_BITOR: 004996 case TK_SLASH: 004997 case TK_LSHIFT: 004998 case TK_RSHIFT: 004999 case TK_CONCAT: { 005000 assert( TK_AND==OP_And ); testcase( op==TK_AND ); 005001 assert( TK_OR==OP_Or ); testcase( op==TK_OR ); 005002 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); 005003 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); 005004 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); 005005 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); 005006 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); 005007 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); 005008 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); 005009 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); 005010 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); 005011 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 005012 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 005013 sqlite3VdbeAddOp3(v, op, r2, r1, target); 005014 testcase( regFree1==0 ); 005015 testcase( regFree2==0 ); 005016 break; 005017 } 005018 case TK_UMINUS: { 005019 Expr *pLeft = pExpr->pLeft; 005020 assert( pLeft ); 005021 if( pLeft->op==TK_INTEGER ){ 005022 codeInteger(pParse, pLeft, 1, target); 005023 return target; 005024 #ifndef SQLITE_OMIT_FLOATING_POINT 005025 }else if( pLeft->op==TK_FLOAT ){ 005026 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 005027 codeReal(v, pLeft->u.zToken, 1, target); 005028 return target; 005029 #endif 005030 }else{ 005031 tempX.op = TK_INTEGER; 005032 tempX.flags = EP_IntValue|EP_TokenOnly; 005033 tempX.u.iValue = 0; 005034 ExprClearVVAProperties(&tempX); 005035 r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); 005036 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 005037 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 005038 testcase( regFree2==0 ); 005039 } 005040 break; 005041 } 005042 case TK_BITNOT: 005043 case TK_NOT: { 005044 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); 005045 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); 005046 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 005047 testcase( regFree1==0 ); 005048 sqlite3VdbeAddOp2(v, op, r1, inReg); 005049 break; 005050 } 005051 case TK_TRUTH: { 005052 int isTrue; /* IS TRUE or IS NOT TRUE */ 005053 int bNormal; /* IS TRUE or IS FALSE */ 005054 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 005055 testcase( regFree1==0 ); 005056 isTrue = sqlite3ExprTruthValue(pExpr->pRight); 005057 bNormal = pExpr->op2==TK_IS; 005058 testcase( isTrue && bNormal); 005059 testcase( !isTrue && bNormal); 005060 sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal); 005061 break; 005062 } 005063 case TK_ISNULL: 005064 case TK_NOTNULL: { 005065 int addr; 005066 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 005067 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 005068 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 005069 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 005070 testcase( regFree1==0 ); 005071 addr = sqlite3VdbeAddOp1(v, op, r1); 005072 VdbeCoverageIf(v, op==TK_ISNULL); 005073 VdbeCoverageIf(v, op==TK_NOTNULL); 005074 sqlite3VdbeAddOp2(v, OP_Integer, 0, target); 005075 sqlite3VdbeJumpHere(v, addr); 005076 break; 005077 } 005078 case TK_AGG_FUNCTION: { 005079 AggInfo *pInfo = pExpr->pAggInfo; 005080 if( pInfo==0 005081 || NEVER(pExpr->iAgg<0) 005082 || NEVER(pExpr->iAgg>=pInfo->nFunc) 005083 ){ 005084 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 005085 sqlite3ErrorMsg(pParse, "misuse of aggregate: %#T()", pExpr); 005086 }else{ 005087 return AggInfoFuncReg(pInfo, pExpr->iAgg); 005088 } 005089 break; 005090 } 005091 case TK_FUNCTION: { 005092 ExprList *pFarg; /* List of function arguments */ 005093 int nFarg; /* Number of function arguments */ 005094 FuncDef *pDef; /* The function definition object */ 005095 const char *zId; /* The function name */ 005096 u32 constMask = 0; /* Mask of function arguments that are constant */ 005097 int i; /* Loop counter */ 005098 sqlite3 *db = pParse->db; /* The database connection */ 005099 u8 enc = ENC(db); /* The text encoding used by this database */ 005100 CollSeq *pColl = 0; /* A collating sequence */ 005101 005102 #ifndef SQLITE_OMIT_WINDOWFUNC 005103 if( ExprHasProperty(pExpr, EP_WinFunc) ){ 005104 return pExpr->y.pWin->regResult; 005105 } 005106 #endif 005107 005108 if( ConstFactorOk(pParse) 005109 && sqlite3ExprIsConstantNotJoin(pParse,pExpr) 005110 ){ 005111 /* SQL functions can be expensive. So try to avoid running them 005112 ** multiple times if we know they always give the same result */ 005113 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1); 005114 } 005115 assert( !ExprHasProperty(pExpr, EP_TokenOnly) ); 005116 assert( ExprUseXList(pExpr) ); 005117 pFarg = pExpr->x.pList; 005118 nFarg = pFarg ? pFarg->nExpr : 0; 005119 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 005120 zId = pExpr->u.zToken; 005121 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); 005122 #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION 005123 if( pDef==0 && pParse->explain ){ 005124 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0); 005125 } 005126 #endif 005127 if( pDef==0 || pDef->xFinalize!=0 ){ 005128 sqlite3ErrorMsg(pParse, "unknown function: %#T()", pExpr); 005129 break; 005130 } 005131 if( (pDef->funcFlags & SQLITE_FUNC_INLINE)!=0 && ALWAYS(pFarg!=0) ){ 005132 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 ); 005133 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 ); 005134 return exprCodeInlineFunction(pParse, pFarg, 005135 SQLITE_PTR_TO_INT(pDef->pUserData), target); 005136 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){ 005137 sqlite3ExprFunctionUsable(pParse, pExpr, pDef); 005138 } 005139 005140 for(i=0; i<nFarg; i++){ 005141 if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){ 005142 testcase( i==31 ); 005143 constMask |= MASKBIT32(i); 005144 } 005145 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 005146 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 005147 } 005148 } 005149 if( pFarg ){ 005150 if( constMask ){ 005151 r1 = pParse->nMem+1; 005152 pParse->nMem += nFarg; 005153 }else{ 005154 r1 = sqlite3GetTempRange(pParse, nFarg); 005155 } 005156 005157 /* For length() and typeof() and octet_length() functions, 005158 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG 005159 ** or OPFLAG_TYPEOFARG or OPFLAG_BYTELENARG respectively, to avoid 005160 ** unnecessary data loading. 005161 */ 005162 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ 005163 u8 exprOp; 005164 assert( nFarg==1 ); 005165 assert( pFarg->a[0].pExpr!=0 ); 005166 exprOp = pFarg->a[0].pExpr->op; 005167 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 005168 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 005169 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 005170 assert( SQLITE_FUNC_BYTELEN==OPFLAG_BYTELENARG ); 005171 assert( (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG)==OPFLAG_BYTELENARG ); 005172 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG ); 005173 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG ); 005174 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG); 005175 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG; 005176 } 005177 } 005178 005179 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, SQLITE_ECEL_FACTOR); 005180 }else{ 005181 r1 = 0; 005182 } 005183 #ifndef SQLITE_OMIT_VIRTUALTABLE 005184 /* Possibly overload the function if the first argument is 005185 ** a virtual table column. 005186 ** 005187 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 005188 ** second argument, not the first, as the argument to test to 005189 ** see if it is a column in a virtual table. This is done because 005190 ** the left operand of infix functions (the operand we want to 005191 ** control overloading) ends up as the second argument to the 005192 ** function. The expression "A glob B" is equivalent to 005193 ** "glob(B,A). We want to use the A in "A glob B" to test 005194 ** for function overloading. But we use the B term in "glob(B,A)". 005195 */ 005196 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){ 005197 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 005198 }else if( nFarg>0 ){ 005199 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 005200 } 005201 #endif 005202 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 005203 if( !pColl ) pColl = db->pDfltColl; 005204 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 005205 } 005206 sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg, 005207 pDef, pExpr->op2); 005208 if( nFarg ){ 005209 if( constMask==0 ){ 005210 sqlite3ReleaseTempRange(pParse, r1, nFarg); 005211 }else{ 005212 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1); 005213 } 005214 } 005215 return target; 005216 } 005217 #ifndef SQLITE_OMIT_SUBQUERY 005218 case TK_EXISTS: 005219 case TK_SELECT: { 005220 int nCol; 005221 testcase( op==TK_EXISTS ); 005222 testcase( op==TK_SELECT ); 005223 if( pParse->db->mallocFailed ){ 005224 return 0; 005225 }else if( op==TK_SELECT 005226 && ALWAYS( ExprUseXSelect(pExpr) ) 005227 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 005228 ){ 005229 sqlite3SubselectError(pParse, nCol, 1); 005230 }else{ 005231 return sqlite3CodeSubselect(pParse, pExpr); 005232 } 005233 break; 005234 } 005235 case TK_SELECT_COLUMN: { 005236 int n; 005237 Expr *pLeft = pExpr->pLeft; 005238 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){ 005239 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft); 005240 pLeft->op2 = pParse->withinRJSubrtn; 005241 } 005242 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR ); 005243 n = sqlite3ExprVectorSize(pLeft); 005244 if( pExpr->iTable!=n ){ 005245 sqlite3ErrorMsg(pParse, "%d columns assigned %d values", 005246 pExpr->iTable, n); 005247 } 005248 return pLeft->iTable + pExpr->iColumn; 005249 } 005250 case TK_IN: { 005251 int destIfFalse = sqlite3VdbeMakeLabel(pParse); 005252 int destIfNull = sqlite3VdbeMakeLabel(pParse); 005253 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 005254 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 005255 sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 005256 sqlite3VdbeResolveLabel(v, destIfFalse); 005257 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); 005258 sqlite3VdbeResolveLabel(v, destIfNull); 005259 return target; 005260 } 005261 #endif /* SQLITE_OMIT_SUBQUERY */ 005262 005263 005264 /* 005265 ** x BETWEEN y AND z 005266 ** 005267 ** This is equivalent to 005268 ** 005269 ** x>=y AND x<=z 005270 ** 005271 ** X is stored in pExpr->pLeft. 005272 ** Y is stored in pExpr->pList->a[0].pExpr. 005273 ** Z is stored in pExpr->pList->a[1].pExpr. 005274 */ 005275 case TK_BETWEEN: { 005276 exprCodeBetween(pParse, pExpr, target, 0, 0); 005277 return target; 005278 } 005279 case TK_COLLATE: { 005280 if( !ExprHasProperty(pExpr, EP_Collate) ){ 005281 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called 005282 ** "SOFT-COLLATE" that is added to constraints that are pushed down 005283 ** from outer queries into sub-queries by the WHERE-clause push-down 005284 ** optimization. Clear subtypes as subtypes may not cross a subquery 005285 ** boundary. 005286 */ 005287 assert( pExpr->pLeft ); 005288 sqlite3ExprCode(pParse, pExpr->pLeft, target); 005289 sqlite3VdbeAddOp1(v, OP_ClrSubtype, target); 005290 return target; 005291 }else{ 005292 pExpr = pExpr->pLeft; 005293 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */ 005294 } 005295 } 005296 case TK_SPAN: 005297 case TK_UPLUS: { 005298 pExpr = pExpr->pLeft; 005299 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */ 005300 } 005301 005302 case TK_TRIGGER: { 005303 /* If the opcode is TK_TRIGGER, then the expression is a reference 005304 ** to a column in the new.* or old.* pseudo-tables available to 005305 ** trigger programs. In this case Expr.iTable is set to 1 for the 005306 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 005307 ** is set to the column of the pseudo-table to read, or to -1 to 005308 ** read the rowid field. 005309 ** 005310 ** The expression is implemented using an OP_Param opcode. The p1 005311 ** parameter is set to 0 for an old.rowid reference, or to (i+1) 005312 ** to reference another column of the old.* pseudo-table, where 005313 ** i is the index of the column. For a new.rowid reference, p1 is 005314 ** set to (n+1), where n is the number of columns in each pseudo-table. 005315 ** For a reference to any other column in the new.* pseudo-table, p1 005316 ** is set to (n+2+i), where n and i are as defined previously. For 005317 ** example, if the table on which triggers are being fired is 005318 ** declared as: 005319 ** 005320 ** CREATE TABLE t1(a, b); 005321 ** 005322 ** Then p1 is interpreted as follows: 005323 ** 005324 ** p1==0 -> old.rowid p1==3 -> new.rowid 005325 ** p1==1 -> old.a p1==4 -> new.a 005326 ** p1==2 -> old.b p1==5 -> new.b 005327 */ 005328 Table *pTab; 005329 int iCol; 005330 int p1; 005331 005332 assert( ExprUseYTab(pExpr) ); 005333 pTab = pExpr->y.pTab; 005334 iCol = pExpr->iColumn; 005335 p1 = pExpr->iTable * (pTab->nCol+1) + 1 005336 + sqlite3TableColumnToStorage(pTab, iCol); 005337 005338 assert( pExpr->iTable==0 || pExpr->iTable==1 ); 005339 assert( iCol>=-1 && iCol<pTab->nCol ); 005340 assert( pTab->iPKey<0 || iCol!=pTab->iPKey ); 005341 assert( p1>=0 && p1<(pTab->nCol*2+2) ); 005342 005343 sqlite3VdbeAddOp2(v, OP_Param, p1, target); 005344 VdbeComment((v, "r[%d]=%s.%s", target, 005345 (pExpr->iTable ? "new" : "old"), 005346 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName) 005347 )); 005348 005349 #ifndef SQLITE_OMIT_FLOATING_POINT 005350 /* If the column has REAL affinity, it may currently be stored as an 005351 ** integer. Use OP_RealAffinity to make sure it is really real. 005352 ** 005353 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to 005354 ** floating point when extracting it from the record. */ 005355 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){ 005356 sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 005357 } 005358 #endif 005359 break; 005360 } 005361 005362 case TK_VECTOR: { 005363 sqlite3ErrorMsg(pParse, "row value misused"); 005364 break; 005365 } 005366 005367 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions 005368 ** that derive from the right-hand table of a LEFT JOIN. The 005369 ** Expr.iTable value is the table number for the right-hand table. 005370 ** The expression is only evaluated if that table is not currently 005371 ** on a LEFT JOIN NULL row. 005372 */ 005373 case TK_IF_NULL_ROW: { 005374 int addrINR; 005375 u8 okConstFactor = pParse->okConstFactor; 005376 AggInfo *pAggInfo = pExpr->pAggInfo; 005377 if( pAggInfo ){ 005378 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); 005379 if( !pAggInfo->directMode ){ 005380 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg); 005381 break; 005382 } 005383 if( pExpr->pAggInfo->useSortingIdx ){ 005384 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 005385 pAggInfo->aCol[pExpr->iAgg].iSorterColumn, 005386 target); 005387 inReg = target; 005388 break; 005389 } 005390 } 005391 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow, pExpr->iTable, 0, target); 005392 /* The OP_IfNullRow opcode above can overwrite the result register with 005393 ** NULL. So we have to ensure that the result register is not a value 005394 ** that is suppose to be a constant. Two defenses are needed: 005395 ** (1) Temporarily disable factoring of constant expressions 005396 ** (2) Make sure the computed value really is stored in register 005397 ** "target" and not someplace else. 005398 */ 005399 pParse->okConstFactor = 0; /* note (1) above */ 005400 sqlite3ExprCode(pParse, pExpr->pLeft, target); 005401 assert( target==inReg ); 005402 pParse->okConstFactor = okConstFactor; 005403 sqlite3VdbeJumpHere(v, addrINR); 005404 break; 005405 } 005406 005407 /* 005408 ** Form A: 005409 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 005410 ** 005411 ** Form B: 005412 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 005413 ** 005414 ** Form A is can be transformed into the equivalent form B as follows: 005415 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 005416 ** WHEN x=eN THEN rN ELSE y END 005417 ** 005418 ** X (if it exists) is in pExpr->pLeft. 005419 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is 005420 ** odd. The Y is also optional. If the number of elements in x.pList 005421 ** is even, then Y is omitted and the "otherwise" result is NULL. 005422 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 005423 ** 005424 ** The result of the expression is the Ri for the first matching Ei, 005425 ** or if there is no matching Ei, the ELSE term Y, or if there is 005426 ** no ELSE term, NULL. 005427 */ 005428 case TK_CASE: { 005429 int endLabel; /* GOTO label for end of CASE stmt */ 005430 int nextCase; /* GOTO label for next WHEN clause */ 005431 int nExpr; /* 2x number of WHEN terms */ 005432 int i; /* Loop counter */ 005433 ExprList *pEList; /* List of WHEN terms */ 005434 struct ExprList_item *aListelem; /* Array of WHEN terms */ 005435 Expr opCompare; /* The X==Ei expression */ 005436 Expr *pX; /* The X expression */ 005437 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ 005438 Expr *pDel = 0; 005439 sqlite3 *db = pParse->db; 005440 005441 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 ); 005442 assert(pExpr->x.pList->nExpr > 0); 005443 pEList = pExpr->x.pList; 005444 aListelem = pEList->a; 005445 nExpr = pEList->nExpr; 005446 endLabel = sqlite3VdbeMakeLabel(pParse); 005447 if( (pX = pExpr->pLeft)!=0 ){ 005448 pDel = sqlite3ExprDup(db, pX, 0); 005449 if( db->mallocFailed ){ 005450 sqlite3ExprDelete(db, pDel); 005451 break; 005452 } 005453 testcase( pX->op==TK_COLUMN ); 005454 sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, ®Free1)); 005455 testcase( regFree1==0 ); 005456 memset(&opCompare, 0, sizeof(opCompare)); 005457 opCompare.op = TK_EQ; 005458 opCompare.pLeft = pDel; 005459 pTest = &opCompare; 005460 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: 005461 ** The value in regFree1 might get SCopy-ed into the file result. 005462 ** So make sure that the regFree1 register is not reused for other 005463 ** purposes and possibly overwritten. */ 005464 regFree1 = 0; 005465 } 005466 for(i=0; i<nExpr-1; i=i+2){ 005467 if( pX ){ 005468 assert( pTest!=0 ); 005469 opCompare.pRight = aListelem[i].pExpr; 005470 }else{ 005471 pTest = aListelem[i].pExpr; 005472 } 005473 nextCase = sqlite3VdbeMakeLabel(pParse); 005474 testcase( pTest->op==TK_COLUMN ); 005475 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 005476 testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 005477 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 005478 sqlite3VdbeGoto(v, endLabel); 005479 sqlite3VdbeResolveLabel(v, nextCase); 005480 } 005481 if( (nExpr&1)!=0 ){ 005482 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 005483 }else{ 005484 sqlite3VdbeAddOp2(v, OP_Null, 0, target); 005485 } 005486 sqlite3ExprDelete(db, pDel); 005487 setDoNotMergeFlagOnCopy(v); 005488 sqlite3VdbeResolveLabel(v, endLabel); 005489 break; 005490 } 005491 #ifndef SQLITE_OMIT_TRIGGER 005492 case TK_RAISE: { 005493 assert( pExpr->affExpr==OE_Rollback 005494 || pExpr->affExpr==OE_Abort 005495 || pExpr->affExpr==OE_Fail 005496 || pExpr->affExpr==OE_Ignore 005497 ); 005498 if( !pParse->pTriggerTab && !pParse->nested ){ 005499 sqlite3ErrorMsg(pParse, 005500 "RAISE() may only be used within a trigger-program"); 005501 return 0; 005502 } 005503 if( pExpr->affExpr==OE_Abort ){ 005504 sqlite3MayAbort(pParse); 005505 } 005506 assert( !ExprHasProperty(pExpr, EP_IntValue) ); 005507 if( pExpr->affExpr==OE_Ignore ){ 005508 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, OE_Ignore); 005509 VdbeCoverage(v); 005510 }else{ 005511 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 005512 sqlite3VdbeAddOp3(v, OP_Halt, 005513 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR, 005514 pExpr->affExpr, r1); 005515 } 005516 break; 005517 } 005518 #endif 005519 } 005520 sqlite3ReleaseTempReg(pParse, regFree1); 005521 sqlite3ReleaseTempReg(pParse, regFree2); 005522 return inReg; 005523 } 005524 005525 /* 005526 ** Generate code that will evaluate expression pExpr just one time 005527 ** per prepared statement execution. 005528 ** 005529 ** If the expression uses functions (that might throw an exception) then 005530 ** guard them with an OP_Once opcode to ensure that the code is only executed 005531 ** once. If no functions are involved, then factor the code out and put it at 005532 ** the end of the prepared statement in the initialization section. 005533 ** 005534 ** If regDest>0 then the result is always stored in that register and the 005535 ** result is not reusable. If regDest<0 then this routine is free to 005536 ** store the value wherever it wants. The register where the expression 005537 ** is stored is returned. When regDest<0, two identical expressions might 005538 ** code to the same register, if they do not contain function calls and hence 005539 ** are factored out into the initialization section at the end of the 005540 ** prepared statement. 005541 */ 005542 int sqlite3ExprCodeRunJustOnce( 005543 Parse *pParse, /* Parsing context */ 005544 Expr *pExpr, /* The expression to code when the VDBE initializes */ 005545 int regDest /* Store the value in this register */ 005546 ){ 005547 ExprList *p; 005548 assert( ConstFactorOk(pParse) ); 005549 assert( regDest!=0 ); 005550 p = pParse->pConstExpr; 005551 if( regDest<0 && p ){ 005552 struct ExprList_item *pItem; 005553 int i; 005554 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ 005555 if( pItem->fg.reusable 005556 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 005557 ){ 005558 return pItem->u.iConstExprReg; 005559 } 005560 } 005561 } 005562 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); 005563 if( pExpr!=0 && ExprHasProperty(pExpr, EP_HasFunc) ){ 005564 Vdbe *v = pParse->pVdbe; 005565 int addr; 005566 assert( v ); 005567 addr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); 005568 pParse->okConstFactor = 0; 005569 if( !pParse->db->mallocFailed ){ 005570 if( regDest<0 ) regDest = ++pParse->nMem; 005571 sqlite3ExprCode(pParse, pExpr, regDest); 005572 } 005573 pParse->okConstFactor = 1; 005574 sqlite3ExprDelete(pParse->db, pExpr); 005575 sqlite3VdbeJumpHere(v, addr); 005576 }else{ 005577 p = sqlite3ExprListAppend(pParse, p, pExpr); 005578 if( p ){ 005579 struct ExprList_item *pItem = &p->a[p->nExpr-1]; 005580 pItem->fg.reusable = regDest<0; 005581 if( regDest<0 ) regDest = ++pParse->nMem; 005582 pItem->u.iConstExprReg = regDest; 005583 } 005584 pParse->pConstExpr = p; 005585 } 005586 return regDest; 005587 } 005588 005589 /* 005590 ** Generate code to evaluate an expression and store the results 005591 ** into a register. Return the register number where the results 005592 ** are stored. 005593 ** 005594 ** If the register is a temporary register that can be deallocated, 005595 ** then write its number into *pReg. If the result register is not 005596 ** a temporary, then set *pReg to zero. 005597 ** 005598 ** If pExpr is a constant, then this routine might generate this 005599 ** code to fill the register in the initialization section of the 005600 ** VDBE program, in order to factor it out of the evaluation loop. 005601 */ 005602 int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 005603 int r2; 005604 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr); 005605 if( ConstFactorOk(pParse) 005606 && ALWAYS(pExpr!=0) 005607 && pExpr->op!=TK_REGISTER 005608 && sqlite3ExprIsConstantNotJoin(pParse, pExpr) 005609 ){ 005610 *pReg = 0; 005611 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1); 005612 }else{ 005613 int r1 = sqlite3GetTempReg(pParse); 005614 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 005615 if( r2==r1 ){ 005616 *pReg = r1; 005617 }else{ 005618 sqlite3ReleaseTempReg(pParse, r1); 005619 *pReg = 0; 005620 } 005621 } 005622 return r2; 005623 } 005624 005625 /* 005626 ** Generate code that will evaluate expression pExpr and store the 005627 ** results in register target. The results are guaranteed to appear 005628 ** in register target. 005629 */ 005630 void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 005631 int inReg; 005632 005633 assert( pExpr==0 || !ExprHasVVAProperty(pExpr,EP_Immutable) ); 005634 assert( target>0 && target<=pParse->nMem ); 005635 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); 005636 if( pParse->pVdbe==0 ) return; 005637 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 005638 if( inReg!=target ){ 005639 u8 op; 005640 Expr *pX = sqlite3ExprSkipCollateAndLikely(pExpr); 005641 testcase( pX!=pExpr ); 005642 if( ALWAYS(pX) 005643 && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER) 005644 ){ 005645 op = OP_Copy; 005646 }else{ 005647 op = OP_SCopy; 005648 } 005649 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target); 005650 } 005651 } 005652 005653 /* 005654 ** Make a transient copy of expression pExpr and then code it using 005655 ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() 005656 ** except that the input expression is guaranteed to be unchanged. 005657 */ 005658 void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ 005659 sqlite3 *db = pParse->db; 005660 pExpr = sqlite3ExprDup(db, pExpr, 0); 005661 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); 005662 sqlite3ExprDelete(db, pExpr); 005663 } 005664 005665 /* 005666 ** Generate code that will evaluate expression pExpr and store the 005667 ** results in register target. The results are guaranteed to appear 005668 ** in register target. If the expression is constant, then this routine 005669 ** might choose to code the expression at initialization time. 005670 */ 005671 void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 005672 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){ 005673 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target); 005674 }else{ 005675 sqlite3ExprCodeCopy(pParse, pExpr, target); 005676 } 005677 } 005678 005679 /* 005680 ** Generate code that pushes the value of every element of the given 005681 ** expression list into a sequence of registers beginning at target. 005682 ** 005683 ** Return the number of elements evaluated. The number returned will 005684 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF 005685 ** is defined. 005686 ** 005687 ** The SQLITE_ECEL_DUP flag prevents the arguments from being 005688 ** filled using OP_SCopy. OP_Copy must be used instead. 005689 ** 005690 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 005691 ** factored out into initialization code. 005692 ** 005693 ** The SQLITE_ECEL_REF flag means that expressions in the list with 005694 ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored 005695 ** in registers at srcReg, and so the value can be copied from there. 005696 ** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0 005697 ** are simply omitted rather than being copied from srcReg. 005698 */ 005699 int sqlite3ExprCodeExprList( 005700 Parse *pParse, /* Parsing context */ 005701 ExprList *pList, /* The expression list to be coded */ 005702 int target, /* Where to write results */ 005703 int srcReg, /* Source registers if SQLITE_ECEL_REF */ 005704 u8 flags /* SQLITE_ECEL_* flags */ 005705 ){ 005706 struct ExprList_item *pItem; 005707 int i, j, n; 005708 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 005709 Vdbe *v = pParse->pVdbe; 005710 assert( pList!=0 ); 005711 assert( target>0 ); 005712 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 005713 n = pList->nExpr; 005714 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 005715 for(pItem=pList->a, i=0; i<n; i++, pItem++){ 005716 Expr *pExpr = pItem->pExpr; 005717 #ifdef SQLITE_ENABLE_SORTER_REFERENCES 005718 if( pItem->fg.bSorterRef ){ 005719 i--; 005720 n--; 005721 }else 005722 #endif 005723 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){ 005724 if( flags & SQLITE_ECEL_OMITREF ){ 005725 i--; 005726 n--; 005727 }else{ 005728 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); 005729 } 005730 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 005731 && sqlite3ExprIsConstantNotJoin(pParse,pExpr) 005732 ){ 005733 sqlite3ExprCodeRunJustOnce(pParse, pExpr, target+i); 005734 }else{ 005735 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 005736 if( inReg!=target+i ){ 005737 VdbeOp *pOp; 005738 if( copyOp==OP_Copy 005739 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy 005740 && pOp->p1+pOp->p3+1==inReg 005741 && pOp->p2+pOp->p3+1==target+i 005742 && pOp->p5==0 /* The do-not-merge flag must be clear */ 005743 ){ 005744 pOp->p3++; 005745 }else{ 005746 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 005747 } 005748 } 005749 } 005750 } 005751 return n; 005752 } 005753 005754 /* 005755 ** Generate code for a BETWEEN operator. 005756 ** 005757 ** x BETWEEN y AND z 005758 ** 005759 ** The above is equivalent to 005760 ** 005761 ** x>=y AND x<=z 005762 ** 005763 ** Code it as such, taking care to do the common subexpression 005764 ** elimination of x. 005765 ** 005766 ** The xJumpIf parameter determines details: 005767 ** 005768 ** NULL: Store the boolean result in reg[dest] 005769 ** sqlite3ExprIfTrue: Jump to dest if true 005770 ** sqlite3ExprIfFalse: Jump to dest if false 005771 ** 005772 ** The jumpIfNull parameter is ignored if xJumpIf is NULL. 005773 */ 005774 static void exprCodeBetween( 005775 Parse *pParse, /* Parsing and code generating context */ 005776 Expr *pExpr, /* The BETWEEN expression */ 005777 int dest, /* Jump destination or storage location */ 005778 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */ 005779 int jumpIfNull /* Take the jump if the BETWEEN is NULL */ 005780 ){ 005781 Expr exprAnd; /* The AND operator in x>=y AND x<=z */ 005782 Expr compLeft; /* The x>=y term */ 005783 Expr compRight; /* The x<=z term */ 005784 int regFree1 = 0; /* Temporary use register */ 005785 Expr *pDel = 0; 005786 sqlite3 *db = pParse->db; 005787 005788 memset(&compLeft, 0, sizeof(Expr)); 005789 memset(&compRight, 0, sizeof(Expr)); 005790 memset(&exprAnd, 0, sizeof(Expr)); 005791 005792 assert( ExprUseXList(pExpr) ); 005793 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0); 005794 if( db->mallocFailed==0 ){ 005795 exprAnd.op = TK_AND; 005796 exprAnd.pLeft = &compLeft; 005797 exprAnd.pRight = &compRight; 005798 compLeft.op = TK_GE; 005799 compLeft.pLeft = pDel; 005800 compLeft.pRight = pExpr->x.pList->a[0].pExpr; 005801 compRight.op = TK_LE; 005802 compRight.pLeft = pDel; 005803 compRight.pRight = pExpr->x.pList->a[1].pExpr; 005804 sqlite3ExprToRegister(pDel, exprCodeVector(pParse, pDel, ®Free1)); 005805 if( xJump ){ 005806 xJump(pParse, &exprAnd, dest, jumpIfNull); 005807 }else{ 005808 /* Mark the expression is being from the ON or USING clause of a join 005809 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move 005810 ** it into the Parse.pConstExpr list. We should use a new bit for this, 005811 ** for clarity, but we are out of bits in the Expr.flags field so we 005812 ** have to reuse the EP_OuterON bit. Bummer. */ 005813 pDel->flags |= EP_OuterON; 005814 sqlite3ExprCodeTarget(pParse, &exprAnd, dest); 005815 } 005816 sqlite3ReleaseTempReg(pParse, regFree1); 005817 } 005818 sqlite3ExprDelete(db, pDel); 005819 005820 /* Ensure adequate test coverage */ 005821 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 ); 005822 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 ); 005823 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 ); 005824 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 ); 005825 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 ); 005826 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 ); 005827 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 ); 005828 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 ); 005829 testcase( xJump==0 ); 005830 } 005831 005832 /* 005833 ** Generate code for a boolean expression such that a jump is made 005834 ** to the label "dest" if the expression is true but execution 005835 ** continues straight thru if the expression is false. 005836 ** 005837 ** If the expression evaluates to NULL (neither true nor false), then 005838 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 005839 ** 005840 ** This code depends on the fact that certain token values (ex: TK_EQ) 005841 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 005842 ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 005843 ** the make process cause these values to align. Assert()s in the code 005844 ** below verify that the numbers are aligned correctly. 005845 */ 005846 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 005847 Vdbe *v = pParse->pVdbe; 005848 int op = 0; 005849 int regFree1 = 0; 005850 int regFree2 = 0; 005851 int r1, r2; 005852 005853 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 005854 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 005855 if( NEVER(pExpr==0) ) return; /* No way this can happen */ 005856 assert( !ExprHasVVAProperty(pExpr, EP_Immutable) ); 005857 op = pExpr->op; 005858 switch( op ){ 005859 case TK_AND: 005860 case TK_OR: { 005861 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); 005862 if( pAlt!=pExpr ){ 005863 sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull); 005864 }else if( op==TK_AND ){ 005865 int d2 = sqlite3VdbeMakeLabel(pParse); 005866 testcase( jumpIfNull==0 ); 005867 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, 005868 jumpIfNull^SQLITE_JUMPIFNULL); 005869 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 005870 sqlite3VdbeResolveLabel(v, d2); 005871 }else{ 005872 testcase( jumpIfNull==0 ); 005873 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 005874 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 005875 } 005876 break; 005877 } 005878 case TK_NOT: { 005879 testcase( jumpIfNull==0 ); 005880 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 005881 break; 005882 } 005883 case TK_TRUTH: { 005884 int isNot; /* IS NOT TRUE or IS NOT FALSE */ 005885 int isTrue; /* IS TRUE or IS NOT TRUE */ 005886 testcase( jumpIfNull==0 ); 005887 isNot = pExpr->op2==TK_ISNOT; 005888 isTrue = sqlite3ExprTruthValue(pExpr->pRight); 005889 testcase( isTrue && isNot ); 005890 testcase( !isTrue && isNot ); 005891 if( isTrue ^ isNot ){ 005892 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, 005893 isNot ? SQLITE_JUMPIFNULL : 0); 005894 }else{ 005895 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, 005896 isNot ? SQLITE_JUMPIFNULL : 0); 005897 } 005898 break; 005899 } 005900 case TK_IS: 005901 case TK_ISNOT: 005902 testcase( op==TK_IS ); 005903 testcase( op==TK_ISNOT ); 005904 op = (op==TK_IS) ? TK_EQ : TK_NE; 005905 jumpIfNull = SQLITE_NULLEQ; 005906 /* no break */ deliberate_fall_through 005907 case TK_LT: 005908 case TK_LE: 005909 case TK_GT: 005910 case TK_GE: 005911 case TK_NE: 005912 case TK_EQ: { 005913 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; 005914 testcase( jumpIfNull==0 ); 005915 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 005916 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 005917 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 005918 r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted)); 005919 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 005920 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 005921 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 005922 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 005923 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 005924 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 005925 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 005926 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 005927 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 005928 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 005929 testcase( regFree1==0 ); 005930 testcase( regFree2==0 ); 005931 break; 005932 } 005933 case TK_ISNULL: 005934 case TK_NOTNULL: { 005935 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 005936 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 005937 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 005938 sqlite3VdbeTypeofColumn(v, r1); 005939 sqlite3VdbeAddOp2(v, op, r1, dest); 005940 VdbeCoverageIf(v, op==TK_ISNULL); 005941 VdbeCoverageIf(v, op==TK_NOTNULL); 005942 testcase( regFree1==0 ); 005943 break; 005944 } 005945 case TK_BETWEEN: { 005946 testcase( jumpIfNull==0 ); 005947 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); 005948 break; 005949 } 005950 #ifndef SQLITE_OMIT_SUBQUERY 005951 case TK_IN: { 005952 int destIfFalse = sqlite3VdbeMakeLabel(pParse); 005953 int destIfNull = jumpIfNull ? dest : destIfFalse; 005954 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 005955 sqlite3VdbeGoto(v, dest); 005956 sqlite3VdbeResolveLabel(v, destIfFalse); 005957 break; 005958 } 005959 #endif 005960 default: { 005961 default_expr: 005962 if( ExprAlwaysTrue(pExpr) ){ 005963 sqlite3VdbeGoto(v, dest); 005964 }else if( ExprAlwaysFalse(pExpr) ){ 005965 /* No-op */ 005966 }else{ 005967 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 005968 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 005969 VdbeCoverage(v); 005970 testcase( regFree1==0 ); 005971 testcase( jumpIfNull==0 ); 005972 } 005973 break; 005974 } 005975 } 005976 sqlite3ReleaseTempReg(pParse, regFree1); 005977 sqlite3ReleaseTempReg(pParse, regFree2); 005978 } 005979 005980 /* 005981 ** Generate code for a boolean expression such that a jump is made 005982 ** to the label "dest" if the expression is false but execution 005983 ** continues straight thru if the expression is true. 005984 ** 005985 ** If the expression evaluates to NULL (neither true nor false) then 005986 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 005987 ** is 0. 005988 */ 005989 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 005990 Vdbe *v = pParse->pVdbe; 005991 int op = 0; 005992 int regFree1 = 0; 005993 int regFree2 = 0; 005994 int r1, r2; 005995 005996 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 005997 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 005998 if( pExpr==0 ) return; 005999 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) ); 006000 006001 /* The value of pExpr->op and op are related as follows: 006002 ** 006003 ** pExpr->op op 006004 ** --------- ---------- 006005 ** TK_ISNULL OP_NotNull 006006 ** TK_NOTNULL OP_IsNull 006007 ** TK_NE OP_Eq 006008 ** TK_EQ OP_Ne 006009 ** TK_GT OP_Le 006010 ** TK_LE OP_Gt 006011 ** TK_GE OP_Lt 006012 ** TK_LT OP_Ge 006013 ** 006014 ** For other values of pExpr->op, op is undefined and unused. 006015 ** The value of TK_ and OP_ constants are arranged such that we 006016 ** can compute the mapping above using the following expression. 006017 ** Assert()s verify that the computation is correct. 006018 */ 006019 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 006020 006021 /* Verify correct alignment of TK_ and OP_ constants 006022 */ 006023 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 006024 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 006025 assert( pExpr->op!=TK_NE || op==OP_Eq ); 006026 assert( pExpr->op!=TK_EQ || op==OP_Ne ); 006027 assert( pExpr->op!=TK_LT || op==OP_Ge ); 006028 assert( pExpr->op!=TK_LE || op==OP_Gt ); 006029 assert( pExpr->op!=TK_GT || op==OP_Le ); 006030 assert( pExpr->op!=TK_GE || op==OP_Lt ); 006031 006032 switch( pExpr->op ){ 006033 case TK_AND: 006034 case TK_OR: { 006035 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); 006036 if( pAlt!=pExpr ){ 006037 sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull); 006038 }else if( pExpr->op==TK_AND ){ 006039 testcase( jumpIfNull==0 ); 006040 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 006041 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 006042 }else{ 006043 int d2 = sqlite3VdbeMakeLabel(pParse); 006044 testcase( jumpIfNull==0 ); 006045 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, 006046 jumpIfNull^SQLITE_JUMPIFNULL); 006047 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 006048 sqlite3VdbeResolveLabel(v, d2); 006049 } 006050 break; 006051 } 006052 case TK_NOT: { 006053 testcase( jumpIfNull==0 ); 006054 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 006055 break; 006056 } 006057 case TK_TRUTH: { 006058 int isNot; /* IS NOT TRUE or IS NOT FALSE */ 006059 int isTrue; /* IS TRUE or IS NOT TRUE */ 006060 testcase( jumpIfNull==0 ); 006061 isNot = pExpr->op2==TK_ISNOT; 006062 isTrue = sqlite3ExprTruthValue(pExpr->pRight); 006063 testcase( isTrue && isNot ); 006064 testcase( !isTrue && isNot ); 006065 if( isTrue ^ isNot ){ 006066 /* IS TRUE and IS NOT FALSE */ 006067 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, 006068 isNot ? 0 : SQLITE_JUMPIFNULL); 006069 006070 }else{ 006071 /* IS FALSE and IS NOT TRUE */ 006072 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, 006073 isNot ? 0 : SQLITE_JUMPIFNULL); 006074 } 006075 break; 006076 } 006077 case TK_IS: 006078 case TK_ISNOT: 006079 testcase( pExpr->op==TK_IS ); 006080 testcase( pExpr->op==TK_ISNOT ); 006081 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; 006082 jumpIfNull = SQLITE_NULLEQ; 006083 /* no break */ deliberate_fall_through 006084 case TK_LT: 006085 case TK_LE: 006086 case TK_GT: 006087 case TK_GE: 006088 case TK_NE: 006089 case TK_EQ: { 006090 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; 006091 testcase( jumpIfNull==0 ); 006092 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 006093 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 006094 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 006095 r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted)); 006096 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 006097 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 006098 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 006099 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 006100 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 006101 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 006102 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 006103 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 006104 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 006105 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 006106 testcase( regFree1==0 ); 006107 testcase( regFree2==0 ); 006108 break; 006109 } 006110 case TK_ISNULL: 006111 case TK_NOTNULL: { 006112 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 006113 sqlite3VdbeTypeofColumn(v, r1); 006114 sqlite3VdbeAddOp2(v, op, r1, dest); 006115 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); 006116 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); 006117 testcase( regFree1==0 ); 006118 break; 006119 } 006120 case TK_BETWEEN: { 006121 testcase( jumpIfNull==0 ); 006122 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); 006123 break; 006124 } 006125 #ifndef SQLITE_OMIT_SUBQUERY 006126 case TK_IN: { 006127 if( jumpIfNull ){ 006128 sqlite3ExprCodeIN(pParse, pExpr, dest, dest); 006129 }else{ 006130 int destIfNull = sqlite3VdbeMakeLabel(pParse); 006131 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 006132 sqlite3VdbeResolveLabel(v, destIfNull); 006133 } 006134 break; 006135 } 006136 #endif 006137 default: { 006138 default_expr: 006139 if( ExprAlwaysFalse(pExpr) ){ 006140 sqlite3VdbeGoto(v, dest); 006141 }else if( ExprAlwaysTrue(pExpr) ){ 006142 /* no-op */ 006143 }else{ 006144 r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 006145 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 006146 VdbeCoverage(v); 006147 testcase( regFree1==0 ); 006148 testcase( jumpIfNull==0 ); 006149 } 006150 break; 006151 } 006152 } 006153 sqlite3ReleaseTempReg(pParse, regFree1); 006154 sqlite3ReleaseTempReg(pParse, regFree2); 006155 } 006156 006157 /* 006158 ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before 006159 ** code generation, and that copy is deleted after code generation. This 006160 ** ensures that the original pExpr is unchanged. 006161 */ 006162 void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ 006163 sqlite3 *db = pParse->db; 006164 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); 006165 if( db->mallocFailed==0 ){ 006166 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); 006167 } 006168 sqlite3ExprDelete(db, pCopy); 006169 } 006170 006171 /* 006172 ** Expression pVar is guaranteed to be an SQL variable. pExpr may be any 006173 ** type of expression. 006174 ** 006175 ** If pExpr is a simple SQL value - an integer, real, string, blob 006176 ** or NULL value - then the VDBE currently being prepared is configured 006177 ** to re-prepare each time a new value is bound to variable pVar. 006178 ** 006179 ** Additionally, if pExpr is a simple SQL value and the value is the 006180 ** same as that currently bound to variable pVar, non-zero is returned. 006181 ** Otherwise, if the values are not the same or if pExpr is not a simple 006182 ** SQL value, zero is returned. 006183 */ 006184 static int exprCompareVariable( 006185 const Parse *pParse, 006186 const Expr *pVar, 006187 const Expr *pExpr 006188 ){ 006189 int res = 0; 006190 int iVar; 006191 sqlite3_value *pL, *pR = 0; 006192 006193 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR); 006194 if( pR ){ 006195 iVar = pVar->iColumn; 006196 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); 006197 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB); 006198 if( pL ){ 006199 if( sqlite3_value_type(pL)==SQLITE_TEXT ){ 006200 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */ 006201 } 006202 res = 0==sqlite3MemCompare(pL, pR, 0); 006203 } 006204 sqlite3ValueFree(pR); 006205 sqlite3ValueFree(pL); 006206 } 006207 006208 return res; 006209 } 006210 006211 /* 006212 ** Do a deep comparison of two expression trees. Return 0 if the two 006213 ** expressions are completely identical. Return 1 if they differ only 006214 ** by a COLLATE operator at the top level. Return 2 if there are differences 006215 ** other than the top-level COLLATE operator. 006216 ** 006217 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 006218 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 006219 ** 006220 ** The pA side might be using TK_REGISTER. If that is the case and pB is 006221 ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 006222 ** 006223 ** Sometimes this routine will return 2 even if the two expressions 006224 ** really are equivalent. If we cannot prove that the expressions are 006225 ** identical, we return 2 just to be safe. So if this routine 006226 ** returns 2, then you do not really know for certain if the two 006227 ** expressions are the same. But if you get a 0 or 1 return, then you 006228 ** can be sure the expressions are the same. In the places where 006229 ** this routine is used, it does not hurt to get an extra 2 - that 006230 ** just might result in some slightly slower code. But returning 006231 ** an incorrect 0 or 1 could lead to a malfunction. 006232 ** 006233 ** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in 006234 ** pParse->pReprepare can be matched against literals in pB. The 006235 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced. 006236 ** If pParse is NULL (the normal case) then any TK_VARIABLE term in 006237 ** Argument pParse should normally be NULL. If it is not NULL and pA or 006238 ** pB causes a return value of 2. 006239 */ 006240 int sqlite3ExprCompare( 006241 const Parse *pParse, 006242 const Expr *pA, 006243 const Expr *pB, 006244 int iTab 006245 ){ 006246 u32 combinedFlags; 006247 if( pA==0 || pB==0 ){ 006248 return pB==pA ? 0 : 2; 006249 } 006250 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){ 006251 return 0; 006252 } 006253 combinedFlags = pA->flags | pB->flags; 006254 if( combinedFlags & EP_IntValue ){ 006255 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ 006256 return 0; 006257 } 006258 return 2; 006259 } 006260 if( pA->op!=pB->op || pA->op==TK_RAISE ){ 006261 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){ 006262 return 1; 006263 } 006264 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){ 006265 return 1; 006266 } 006267 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN 006268 && pB->iTable<0 && pA->iTable==iTab 006269 ){ 006270 /* fall through */ 006271 }else{ 006272 return 2; 006273 } 006274 } 006275 assert( !ExprHasProperty(pA, EP_IntValue) ); 006276 assert( !ExprHasProperty(pB, EP_IntValue) ); 006277 if( pA->u.zToken ){ 006278 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){ 006279 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 006280 #ifndef SQLITE_OMIT_WINDOWFUNC 006281 assert( pA->op==pB->op ); 006282 if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){ 006283 return 2; 006284 } 006285 if( ExprHasProperty(pA,EP_WinFunc) ){ 006286 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){ 006287 return 2; 006288 } 006289 } 006290 #endif 006291 }else if( pA->op==TK_NULL ){ 006292 return 0; 006293 }else if( pA->op==TK_COLLATE ){ 006294 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 006295 }else 006296 if( pB->u.zToken!=0 006297 && pA->op!=TK_COLUMN 006298 && pA->op!=TK_AGG_COLUMN 006299 && strcmp(pA->u.zToken,pB->u.zToken)!=0 006300 ){ 006301 return 2; 006302 } 006303 } 006304 if( (pA->flags & (EP_Distinct|EP_Commuted)) 006305 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2; 006306 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 006307 if( combinedFlags & EP_xIsSelect ) return 2; 006308 if( (combinedFlags & EP_FixedCol)==0 006309 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2; 006310 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2; 006311 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 006312 if( pA->op!=TK_STRING 006313 && pA->op!=TK_TRUEFALSE 006314 && ALWAYS((combinedFlags & EP_Reduced)==0) 006315 ){ 006316 if( pA->iColumn!=pB->iColumn ) return 2; 006317 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2; 006318 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){ 006319 return 2; 006320 } 006321 } 006322 } 006323 return 0; 006324 } 006325 006326 /* 006327 ** Compare two ExprList objects. Return 0 if they are identical, 1 006328 ** if they are certainly different, or 2 if it is not possible to 006329 ** determine if they are identical or not. 006330 ** 006331 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 006332 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 006333 ** 006334 ** This routine might return non-zero for equivalent ExprLists. The 006335 ** only consequence will be disabled optimizations. But this routine 006336 ** must never return 0 if the two ExprList objects are different, or 006337 ** a malfunction will result. 006338 ** 006339 ** Two NULL pointers are considered to be the same. But a NULL pointer 006340 ** always differs from a non-NULL pointer. 006341 */ 006342 int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB, int iTab){ 006343 int i; 006344 if( pA==0 && pB==0 ) return 0; 006345 if( pA==0 || pB==0 ) return 1; 006346 if( pA->nExpr!=pB->nExpr ) return 1; 006347 for(i=0; i<pA->nExpr; i++){ 006348 int res; 006349 Expr *pExprA = pA->a[i].pExpr; 006350 Expr *pExprB = pB->a[i].pExpr; 006351 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1; 006352 if( (res = sqlite3ExprCompare(0, pExprA, pExprB, iTab)) ) return res; 006353 } 006354 return 0; 006355 } 006356 006357 /* 006358 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level 006359 ** are ignored. 006360 */ 006361 int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){ 006362 return sqlite3ExprCompare(0, 006363 sqlite3ExprSkipCollate(pA), 006364 sqlite3ExprSkipCollate(pB), 006365 iTab); 006366 } 006367 006368 /* 006369 ** Return non-zero if Expr p can only be true if pNN is not NULL. 006370 ** 006371 ** Or if seenNot is true, return non-zero if Expr p can only be 006372 ** non-NULL if pNN is not NULL 006373 */ 006374 static int exprImpliesNotNull( 006375 const Parse *pParse,/* Parsing context */ 006376 const Expr *p, /* The expression to be checked */ 006377 const Expr *pNN, /* The expression that is NOT NULL */ 006378 int iTab, /* Table being evaluated */ 006379 int seenNot /* Return true only if p can be any non-NULL value */ 006380 ){ 006381 assert( p ); 006382 assert( pNN ); 006383 if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){ 006384 return pNN->op!=TK_NULL; 006385 } 006386 switch( p->op ){ 006387 case TK_IN: { 006388 if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0; 006389 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) ); 006390 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); 006391 } 006392 case TK_BETWEEN: { 006393 ExprList *pList; 006394 assert( ExprUseXList(p) ); 006395 pList = p->x.pList; 006396 assert( pList!=0 ); 006397 assert( pList->nExpr==2 ); 006398 if( seenNot ) return 0; 006399 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1) 006400 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1) 006401 ){ 006402 return 1; 006403 } 006404 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); 006405 } 006406 case TK_EQ: 006407 case TK_NE: 006408 case TK_LT: 006409 case TK_LE: 006410 case TK_GT: 006411 case TK_GE: 006412 case TK_PLUS: 006413 case TK_MINUS: 006414 case TK_BITOR: 006415 case TK_LSHIFT: 006416 case TK_RSHIFT: 006417 case TK_CONCAT: 006418 seenNot = 1; 006419 /* no break */ deliberate_fall_through 006420 case TK_STAR: 006421 case TK_REM: 006422 case TK_BITAND: 006423 case TK_SLASH: { 006424 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1; 006425 /* no break */ deliberate_fall_through 006426 } 006427 case TK_SPAN: 006428 case TK_COLLATE: 006429 case TK_UPLUS: 006430 case TK_UMINUS: { 006431 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot); 006432 } 006433 case TK_TRUTH: { 006434 if( seenNot ) return 0; 006435 if( p->op2!=TK_IS ) return 0; 006436 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); 006437 } 006438 case TK_BITNOT: 006439 case TK_NOT: { 006440 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); 006441 } 006442 } 006443 return 0; 006444 } 006445 006446 /* 006447 ** Return true if we can prove the pE2 will always be true if pE1 is 006448 ** true. Return false if we cannot complete the proof or if pE2 might 006449 ** be false. Examples: 006450 ** 006451 ** pE1: x==5 pE2: x==5 Result: true 006452 ** pE1: x>0 pE2: x==5 Result: false 006453 ** pE1: x=21 pE2: x=21 OR y=43 Result: true 006454 ** pE1: x!=123 pE2: x IS NOT NULL Result: true 006455 ** pE1: x!=?1 pE2: x IS NOT NULL Result: true 006456 ** pE1: x IS NULL pE2: x IS NOT NULL Result: false 006457 ** pE1: x IS ?2 pE2: x IS NOT NULL Result: false 006458 ** 006459 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has 006460 ** Expr.iTable<0 then assume a table number given by iTab. 006461 ** 006462 ** If pParse is not NULL, then the values of bound variables in pE1 are 006463 ** compared against literal values in pE2 and pParse->pVdbe->expmask is 006464 ** modified to record which bound variables are referenced. If pParse 006465 ** is NULL, then false will be returned if pE1 contains any bound variables. 006466 ** 006467 ** When in doubt, return false. Returning true might give a performance 006468 ** improvement. Returning false might cause a performance reduction, but 006469 ** it will always give the correct answer and is hence always safe. 006470 */ 006471 int sqlite3ExprImpliesExpr( 006472 const Parse *pParse, 006473 const Expr *pE1, 006474 const Expr *pE2, 006475 int iTab 006476 ){ 006477 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){ 006478 return 1; 006479 } 006480 if( pE2->op==TK_OR 006481 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab) 006482 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) ) 006483 ){ 006484 return 1; 006485 } 006486 if( pE2->op==TK_NOTNULL 006487 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0) 006488 ){ 006489 return 1; 006490 } 006491 return 0; 006492 } 006493 006494 /* This is a helper function to impliesNotNullRow(). In this routine, 006495 ** set pWalker->eCode to one only if *both* of the input expressions 006496 ** separately have the implies-not-null-row property. 006497 */ 006498 static void bothImplyNotNullRow(Walker *pWalker, Expr *pE1, Expr *pE2){ 006499 if( pWalker->eCode==0 ){ 006500 sqlite3WalkExpr(pWalker, pE1); 006501 if( pWalker->eCode ){ 006502 pWalker->eCode = 0; 006503 sqlite3WalkExpr(pWalker, pE2); 006504 } 006505 } 006506 } 006507 006508 /* 006509 ** This is the Expr node callback for sqlite3ExprImpliesNonNullRow(). 006510 ** If the expression node requires that the table at pWalker->iCur 006511 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort. 006512 ** 006513 ** pWalker->mWFlags is non-zero if this inquiry is being undertaking on 006514 ** behalf of a RIGHT JOIN (or FULL JOIN). That makes a difference when 006515 ** evaluating terms in the ON clause of an inner join. 006516 ** 006517 ** This routine controls an optimization. False positives (setting 006518 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives 006519 ** (never setting pWalker->eCode) is a harmless missed optimization. 006520 */ 006521 static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){ 006522 testcase( pExpr->op==TK_AGG_COLUMN ); 006523 testcase( pExpr->op==TK_AGG_FUNCTION ); 006524 if( ExprHasProperty(pExpr, EP_OuterON) ) return WRC_Prune; 006525 if( ExprHasProperty(pExpr, EP_InnerON) && pWalker->mWFlags ){ 006526 /* If iCur is used in an inner-join ON clause to the left of a 006527 ** RIGHT JOIN, that does *not* mean that the table must be non-null. 006528 ** But it is difficult to check for that condition precisely. 006529 ** To keep things simple, any use of iCur from any inner-join is 006530 ** ignored while attempting to simplify a RIGHT JOIN. */ 006531 return WRC_Prune; 006532 } 006533 switch( pExpr->op ){ 006534 case TK_ISNOT: 006535 case TK_ISNULL: 006536 case TK_NOTNULL: 006537 case TK_IS: 006538 case TK_VECTOR: 006539 case TK_FUNCTION: 006540 case TK_TRUTH: 006541 case TK_CASE: 006542 testcase( pExpr->op==TK_ISNOT ); 006543 testcase( pExpr->op==TK_ISNULL ); 006544 testcase( pExpr->op==TK_NOTNULL ); 006545 testcase( pExpr->op==TK_IS ); 006546 testcase( pExpr->op==TK_VECTOR ); 006547 testcase( pExpr->op==TK_FUNCTION ); 006548 testcase( pExpr->op==TK_TRUTH ); 006549 testcase( pExpr->op==TK_CASE ); 006550 return WRC_Prune; 006551 006552 case TK_COLUMN: 006553 if( pWalker->u.iCur==pExpr->iTable ){ 006554 pWalker->eCode = 1; 006555 return WRC_Abort; 006556 } 006557 return WRC_Prune; 006558 006559 case TK_OR: 006560 case TK_AND: 006561 /* Both sides of an AND or OR must separately imply non-null-row. 006562 ** Consider these cases: 006563 ** 1. NOT (x AND y) 006564 ** 2. x OR y 006565 ** If only one of x or y is non-null-row, then the overall expression 006566 ** can be true if the other arm is false (case 1) or true (case 2). 006567 */ 006568 testcase( pExpr->op==TK_OR ); 006569 testcase( pExpr->op==TK_AND ); 006570 bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight); 006571 return WRC_Prune; 006572 006573 case TK_IN: 006574 /* Beware of "x NOT IN ()" and "x NOT IN (SELECT 1 WHERE false)", 006575 ** both of which can be true. But apart from these cases, if 006576 ** the left-hand side of the IN is NULL then the IN itself will be 006577 ** NULL. */ 006578 if( ExprUseXList(pExpr) && ALWAYS(pExpr->x.pList->nExpr>0) ){ 006579 sqlite3WalkExpr(pWalker, pExpr->pLeft); 006580 } 006581 return WRC_Prune; 006582 006583 case TK_BETWEEN: 006584 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else 006585 ** both y and z must be non-null row */ 006586 assert( ExprUseXList(pExpr) ); 006587 assert( pExpr->x.pList->nExpr==2 ); 006588 sqlite3WalkExpr(pWalker, pExpr->pLeft); 006589 bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr, 006590 pExpr->x.pList->a[1].pExpr); 006591 return WRC_Prune; 006592 006593 /* Virtual tables are allowed to use constraints like x=NULL. So 006594 ** a term of the form x=y does not prove that y is not null if x 006595 ** is the column of a virtual table */ 006596 case TK_EQ: 006597 case TK_NE: 006598 case TK_LT: 006599 case TK_LE: 006600 case TK_GT: 006601 case TK_GE: { 006602 Expr *pLeft = pExpr->pLeft; 006603 Expr *pRight = pExpr->pRight; 006604 testcase( pExpr->op==TK_EQ ); 006605 testcase( pExpr->op==TK_NE ); 006606 testcase( pExpr->op==TK_LT ); 006607 testcase( pExpr->op==TK_LE ); 006608 testcase( pExpr->op==TK_GT ); 006609 testcase( pExpr->op==TK_GE ); 006610 /* The y.pTab=0 assignment in wherecode.c always happens after the 006611 ** impliesNotNullRow() test */ 006612 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) ); 006613 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) ); 006614 if( (pLeft->op==TK_COLUMN 006615 && ALWAYS(pLeft->y.pTab!=0) 006616 && IsVirtual(pLeft->y.pTab)) 006617 || (pRight->op==TK_COLUMN 006618 && ALWAYS(pRight->y.pTab!=0) 006619 && IsVirtual(pRight->y.pTab)) 006620 ){ 006621 return WRC_Prune; 006622 } 006623 /* no break */ deliberate_fall_through 006624 } 006625 default: 006626 return WRC_Continue; 006627 } 006628 } 006629 006630 /* 006631 ** Return true (non-zero) if expression p can only be true if at least 006632 ** one column of table iTab is non-null. In other words, return true 006633 ** if expression p will always be NULL or false if every column of iTab 006634 ** is NULL. 006635 ** 006636 ** False negatives are acceptable. In other words, it is ok to return 006637 ** zero even if expression p will never be true of every column of iTab 006638 ** is NULL. A false negative is merely a missed optimization opportunity. 006639 ** 006640 ** False positives are not allowed, however. A false positive may result 006641 ** in an incorrect answer. 006642 ** 006643 ** Terms of p that are marked with EP_OuterON (and hence that come from 006644 ** the ON or USING clauses of OUTER JOINS) are excluded from the analysis. 006645 ** 006646 ** This routine is used to check if a LEFT JOIN can be converted into 006647 ** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE 006648 ** clause requires that some column of the right table of the LEFT JOIN 006649 ** be non-NULL, then the LEFT JOIN can be safely converted into an 006650 ** ordinary join. 006651 */ 006652 int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab, int isRJ){ 006653 Walker w; 006654 p = sqlite3ExprSkipCollateAndLikely(p); 006655 if( p==0 ) return 0; 006656 if( p->op==TK_NOTNULL ){ 006657 p = p->pLeft; 006658 }else{ 006659 while( p->op==TK_AND ){ 006660 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1; 006661 p = p->pRight; 006662 } 006663 } 006664 w.xExprCallback = impliesNotNullRow; 006665 w.xSelectCallback = 0; 006666 w.xSelectCallback2 = 0; 006667 w.eCode = 0; 006668 w.mWFlags = isRJ!=0; 006669 w.u.iCur = iTab; 006670 sqlite3WalkExpr(&w, p); 006671 return w.eCode; 006672 } 006673 006674 /* 006675 ** An instance of the following structure is used by the tree walker 006676 ** to determine if an expression can be evaluated by reference to the 006677 ** index only, without having to do a search for the corresponding 006678 ** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur 006679 ** is the cursor for the table. 006680 */ 006681 struct IdxCover { 006682 Index *pIdx; /* The index to be tested for coverage */ 006683 int iCur; /* Cursor number for the table corresponding to the index */ 006684 }; 006685 006686 /* 006687 ** Check to see if there are references to columns in table 006688 ** pWalker->u.pIdxCover->iCur can be satisfied using the index 006689 ** pWalker->u.pIdxCover->pIdx. 006690 */ 006691 static int exprIdxCover(Walker *pWalker, Expr *pExpr){ 006692 if( pExpr->op==TK_COLUMN 006693 && pExpr->iTable==pWalker->u.pIdxCover->iCur 006694 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0 006695 ){ 006696 pWalker->eCode = 1; 006697 return WRC_Abort; 006698 } 006699 return WRC_Continue; 006700 } 006701 006702 /* 006703 ** Determine if an index pIdx on table with cursor iCur contains will 006704 ** the expression pExpr. Return true if the index does cover the 006705 ** expression and false if the pExpr expression references table columns 006706 ** that are not found in the index pIdx. 006707 ** 006708 ** An index covering an expression means that the expression can be 006709 ** evaluated using only the index and without having to lookup the 006710 ** corresponding table entry. 006711 */ 006712 int sqlite3ExprCoveredByIndex( 006713 Expr *pExpr, /* The index to be tested */ 006714 int iCur, /* The cursor number for the corresponding table */ 006715 Index *pIdx /* The index that might be used for coverage */ 006716 ){ 006717 Walker w; 006718 struct IdxCover xcov; 006719 memset(&w, 0, sizeof(w)); 006720 xcov.iCur = iCur; 006721 xcov.pIdx = pIdx; 006722 w.xExprCallback = exprIdxCover; 006723 w.u.pIdxCover = &xcov; 006724 sqlite3WalkExpr(&w, pExpr); 006725 return !w.eCode; 006726 } 006727 006728 006729 /* Structure used to pass information throughout the Walker in order to 006730 ** implement sqlite3ReferencesSrcList(). 006731 */ 006732 struct RefSrcList { 006733 sqlite3 *db; /* Database connection used for sqlite3DbRealloc() */ 006734 SrcList *pRef; /* Looking for references to these tables */ 006735 i64 nExclude; /* Number of tables to exclude from the search */ 006736 int *aiExclude; /* Cursor IDs for tables to exclude from the search */ 006737 }; 006738 006739 /* 006740 ** Walker SELECT callbacks for sqlite3ReferencesSrcList(). 006741 ** 006742 ** When entering a new subquery on the pExpr argument, add all FROM clause 006743 ** entries for that subquery to the exclude list. 006744 ** 006745 ** When leaving the subquery, remove those entries from the exclude list. 006746 */ 006747 static int selectRefEnter(Walker *pWalker, Select *pSelect){ 006748 struct RefSrcList *p = pWalker->u.pRefSrcList; 006749 SrcList *pSrc = pSelect->pSrc; 006750 i64 i, j; 006751 int *piNew; 006752 if( pSrc->nSrc==0 ) return WRC_Continue; 006753 j = p->nExclude; 006754 p->nExclude += pSrc->nSrc; 006755 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int)); 006756 if( piNew==0 ){ 006757 p->nExclude = 0; 006758 return WRC_Abort; 006759 }else{ 006760 p->aiExclude = piNew; 006761 } 006762 for(i=0; i<pSrc->nSrc; i++, j++){ 006763 p->aiExclude[j] = pSrc->a[i].iCursor; 006764 } 006765 return WRC_Continue; 006766 } 006767 static void selectRefLeave(Walker *pWalker, Select *pSelect){ 006768 struct RefSrcList *p = pWalker->u.pRefSrcList; 006769 SrcList *pSrc = pSelect->pSrc; 006770 if( p->nExclude ){ 006771 assert( p->nExclude>=pSrc->nSrc ); 006772 p->nExclude -= pSrc->nSrc; 006773 } 006774 } 006775 006776 /* This is the Walker EXPR callback for sqlite3ReferencesSrcList(). 006777 ** 006778 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any 006779 ** of the tables shown in RefSrcList.pRef. 006780 ** 006781 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a 006782 ** table is in neither RefSrcList.pRef nor RefSrcList.aiExclude. 006783 */ 006784 static int exprRefToSrcList(Walker *pWalker, Expr *pExpr){ 006785 if( pExpr->op==TK_COLUMN 006786 || pExpr->op==TK_AGG_COLUMN 006787 ){ 006788 int i; 006789 struct RefSrcList *p = pWalker->u.pRefSrcList; 006790 SrcList *pSrc = p->pRef; 006791 int nSrc = pSrc ? pSrc->nSrc : 0; 006792 for(i=0; i<nSrc; i++){ 006793 if( pExpr->iTable==pSrc->a[i].iCursor ){ 006794 pWalker->eCode |= 1; 006795 return WRC_Continue; 006796 } 006797 } 006798 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){} 006799 if( i>=p->nExclude ){ 006800 pWalker->eCode |= 2; 006801 } 006802 } 006803 return WRC_Continue; 006804 } 006805 006806 /* 006807 ** Check to see if pExpr references any tables in pSrcList. 006808 ** Possible return values: 006809 ** 006810 ** 1 pExpr does references a table in pSrcList. 006811 ** 006812 ** 0 pExpr references some table that is not defined in either 006813 ** pSrcList or in subqueries of pExpr itself. 006814 ** 006815 ** -1 pExpr only references no tables at all, or it only 006816 ** references tables defined in subqueries of pExpr itself. 006817 ** 006818 ** As currently used, pExpr is always an aggregate function call. That 006819 ** fact is exploited for efficiency. 006820 */ 006821 int sqlite3ReferencesSrcList(Parse *pParse, Expr *pExpr, SrcList *pSrcList){ 006822 Walker w; 006823 struct RefSrcList x; 006824 assert( pParse->db!=0 ); 006825 memset(&w, 0, sizeof(w)); 006826 memset(&x, 0, sizeof(x)); 006827 w.xExprCallback = exprRefToSrcList; 006828 w.xSelectCallback = selectRefEnter; 006829 w.xSelectCallback2 = selectRefLeave; 006830 w.u.pRefSrcList = &x; 006831 x.db = pParse->db; 006832 x.pRef = pSrcList; 006833 assert( pExpr->op==TK_AGG_FUNCTION ); 006834 assert( ExprUseXList(pExpr) ); 006835 sqlite3WalkExprList(&w, pExpr->x.pList); 006836 if( pExpr->pLeft ){ 006837 assert( pExpr->pLeft->op==TK_ORDER ); 006838 assert( ExprUseXList(pExpr->pLeft) ); 006839 assert( pExpr->pLeft->x.pList!=0 ); 006840 sqlite3WalkExprList(&w, pExpr->pLeft->x.pList); 006841 } 006842 #ifndef SQLITE_OMIT_WINDOWFUNC 006843 if( ExprHasProperty(pExpr, EP_WinFunc) ){ 006844 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter); 006845 } 006846 #endif 006847 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude); 006848 if( w.eCode & 0x01 ){ 006849 return 1; 006850 }else if( w.eCode ){ 006851 return 0; 006852 }else{ 006853 return -1; 006854 } 006855 } 006856 006857 /* 006858 ** This is a Walker expression node callback. 006859 ** 006860 ** For Expr nodes that contain pAggInfo pointers, make sure the AggInfo 006861 ** object that is referenced does not refer directly to the Expr. If 006862 ** it does, make a copy. This is done because the pExpr argument is 006863 ** subject to change. 006864 ** 006865 ** The copy is scheduled for deletion using the sqlite3ExprDeferredDelete() 006866 ** which builds on the sqlite3ParserAddCleanup() mechanism. 006867 */ 006868 static int agginfoPersistExprCb(Walker *pWalker, Expr *pExpr){ 006869 if( ALWAYS(!ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced)) 006870 && pExpr->pAggInfo!=0 006871 ){ 006872 AggInfo *pAggInfo = pExpr->pAggInfo; 006873 int iAgg = pExpr->iAgg; 006874 Parse *pParse = pWalker->pParse; 006875 sqlite3 *db = pParse->db; 006876 assert( iAgg>=0 ); 006877 if( pExpr->op!=TK_AGG_FUNCTION ){ 006878 if( iAgg<pAggInfo->nColumn 006879 && pAggInfo->aCol[iAgg].pCExpr==pExpr 006880 ){ 006881 pExpr = sqlite3ExprDup(db, pExpr, 0); 006882 if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){ 006883 pAggInfo->aCol[iAgg].pCExpr = pExpr; 006884 } 006885 } 006886 }else{ 006887 assert( pExpr->op==TK_AGG_FUNCTION ); 006888 if( ALWAYS(iAgg<pAggInfo->nFunc) 006889 && pAggInfo->aFunc[iAgg].pFExpr==pExpr 006890 ){ 006891 pExpr = sqlite3ExprDup(db, pExpr, 0); 006892 if( pExpr && !sqlite3ExprDeferredDelete(pParse, pExpr) ){ 006893 pAggInfo->aFunc[iAgg].pFExpr = pExpr; 006894 } 006895 } 006896 } 006897 } 006898 return WRC_Continue; 006899 } 006900 006901 /* 006902 ** Initialize a Walker object so that will persist AggInfo entries referenced 006903 ** by the tree that is walked. 006904 */ 006905 void sqlite3AggInfoPersistWalkerInit(Walker *pWalker, Parse *pParse){ 006906 memset(pWalker, 0, sizeof(*pWalker)); 006907 pWalker->pParse = pParse; 006908 pWalker->xExprCallback = agginfoPersistExprCb; 006909 pWalker->xSelectCallback = sqlite3SelectWalkNoop; 006910 } 006911 006912 /* 006913 ** Add a new element to the pAggInfo->aCol[] array. Return the index of 006914 ** the new element. Return a negative number if malloc fails. 006915 */ 006916 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 006917 int i; 006918 pInfo->aCol = sqlite3ArrayAllocate( 006919 db, 006920 pInfo->aCol, 006921 sizeof(pInfo->aCol[0]), 006922 &pInfo->nColumn, 006923 &i 006924 ); 006925 return i; 006926 } 006927 006928 /* 006929 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 006930 ** the new element. Return a negative number if malloc fails. 006931 */ 006932 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 006933 int i; 006934 pInfo->aFunc = sqlite3ArrayAllocate( 006935 db, 006936 pInfo->aFunc, 006937 sizeof(pInfo->aFunc[0]), 006938 &pInfo->nFunc, 006939 &i 006940 ); 006941 return i; 006942 } 006943 006944 /* 006945 ** Search the AggInfo object for an aCol[] entry that has iTable and iColumn. 006946 ** Return the index in aCol[] of the entry that describes that column. 006947 ** 006948 ** If no prior entry is found, create a new one and return -1. The 006949 ** new column will have an index of pAggInfo->nColumn-1. 006950 */ 006951 static void findOrCreateAggInfoColumn( 006952 Parse *pParse, /* Parsing context */ 006953 AggInfo *pAggInfo, /* The AggInfo object to search and/or modify */ 006954 Expr *pExpr /* Expr describing the column to find or insert */ 006955 ){ 006956 struct AggInfo_col *pCol; 006957 int k; 006958 006959 assert( pAggInfo->iFirstReg==0 ); 006960 pCol = pAggInfo->aCol; 006961 for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 006962 if( pCol->pCExpr==pExpr ) return; 006963 if( pCol->iTable==pExpr->iTable 006964 && pCol->iColumn==pExpr->iColumn 006965 && pExpr->op!=TK_IF_NULL_ROW 006966 ){ 006967 goto fix_up_expr; 006968 } 006969 } 006970 k = addAggInfoColumn(pParse->db, pAggInfo); 006971 if( k<0 ){ 006972 /* OOM on resize */ 006973 assert( pParse->db->mallocFailed ); 006974 return; 006975 } 006976 pCol = &pAggInfo->aCol[k]; 006977 assert( ExprUseYTab(pExpr) ); 006978 pCol->pTab = pExpr->y.pTab; 006979 pCol->iTable = pExpr->iTable; 006980 pCol->iColumn = pExpr->iColumn; 006981 pCol->iSorterColumn = -1; 006982 pCol->pCExpr = pExpr; 006983 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){ 006984 int j, n; 006985 ExprList *pGB = pAggInfo->pGroupBy; 006986 struct ExprList_item *pTerm = pGB->a; 006987 n = pGB->nExpr; 006988 for(j=0; j<n; j++, pTerm++){ 006989 Expr *pE = pTerm->pExpr; 006990 if( pE->op==TK_COLUMN 006991 && pE->iTable==pExpr->iTable 006992 && pE->iColumn==pExpr->iColumn 006993 ){ 006994 pCol->iSorterColumn = j; 006995 break; 006996 } 006997 } 006998 } 006999 if( pCol->iSorterColumn<0 ){ 007000 pCol->iSorterColumn = pAggInfo->nSortingColumn++; 007001 } 007002 fix_up_expr: 007003 ExprSetVVAProperty(pExpr, EP_NoReduce); 007004 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo ); 007005 pExpr->pAggInfo = pAggInfo; 007006 if( pExpr->op==TK_COLUMN ){ 007007 pExpr->op = TK_AGG_COLUMN; 007008 } 007009 pExpr->iAgg = (i16)k; 007010 } 007011 007012 /* 007013 ** This is the xExprCallback for a tree walker. It is used to 007014 ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 007015 ** for additional information. 007016 */ 007017 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ 007018 int i; 007019 NameContext *pNC = pWalker->u.pNC; 007020 Parse *pParse = pNC->pParse; 007021 SrcList *pSrcList = pNC->pSrcList; 007022 AggInfo *pAggInfo = pNC->uNC.pAggInfo; 007023 007024 assert( pNC->ncFlags & NC_UAggInfo ); 007025 assert( pAggInfo->iFirstReg==0 ); 007026 switch( pExpr->op ){ 007027 default: { 007028 IndexedExpr *pIEpr; 007029 Expr tmp; 007030 assert( pParse->iSelfTab==0 ); 007031 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break; 007032 if( pParse->pIdxEpr==0 ) break; 007033 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){ 007034 int iDataCur = pIEpr->iDataCur; 007035 if( iDataCur<0 ) continue; 007036 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break; 007037 } 007038 if( pIEpr==0 ) break; 007039 if( NEVER(!ExprUseYTab(pExpr)) ) break; 007040 for(i=0; i<pSrcList->nSrc; i++){ 007041 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break; 007042 } 007043 if( i>=pSrcList->nSrc ) break; 007044 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */ 007045 if( pParse->nErr ){ return WRC_Abort; } 007046 007047 /* If we reach this point, it means that expression pExpr can be 007048 ** translated into a reference to an index column as described by 007049 ** pIEpr. 007050 */ 007051 memset(&tmp, 0, sizeof(tmp)); 007052 tmp.op = TK_AGG_COLUMN; 007053 tmp.iTable = pIEpr->iIdxCur; 007054 tmp.iColumn = pIEpr->iIdxCol; 007055 findOrCreateAggInfoColumn(pParse, pAggInfo, &tmp); 007056 if( pParse->nErr ){ return WRC_Abort; } 007057 assert( pAggInfo->aCol!=0 ); 007058 assert( tmp.iAgg<pAggInfo->nColumn ); 007059 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr; 007060 pExpr->pAggInfo = pAggInfo; 007061 pExpr->iAgg = tmp.iAgg; 007062 return WRC_Prune; 007063 } 007064 case TK_IF_NULL_ROW: 007065 case TK_AGG_COLUMN: 007066 case TK_COLUMN: { 007067 testcase( pExpr->op==TK_AGG_COLUMN ); 007068 testcase( pExpr->op==TK_COLUMN ); 007069 testcase( pExpr->op==TK_IF_NULL_ROW ); 007070 /* Check to see if the column is in one of the tables in the FROM 007071 ** clause of the aggregate query */ 007072 if( ALWAYS(pSrcList!=0) ){ 007073 SrcItem *pItem = pSrcList->a; 007074 for(i=0; i<pSrcList->nSrc; i++, pItem++){ 007075 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 007076 if( pExpr->iTable==pItem->iCursor ){ 007077 findOrCreateAggInfoColumn(pParse, pAggInfo, pExpr); 007078 break; 007079 } /* endif pExpr->iTable==pItem->iCursor */ 007080 } /* end loop over pSrcList */ 007081 } 007082 return WRC_Continue; 007083 } 007084 case TK_AGG_FUNCTION: { 007085 if( (pNC->ncFlags & NC_InAggFunc)==0 007086 && pWalker->walkerDepth==pExpr->op2 007087 && pExpr->pAggInfo==0 007088 ){ 007089 /* Check to see if pExpr is a duplicate of another aggregate 007090 ** function that is already in the pAggInfo structure 007091 */ 007092 struct AggInfo_func *pItem = pAggInfo->aFunc; 007093 for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 007094 if( NEVER(pItem->pFExpr==pExpr) ) break; 007095 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){ 007096 break; 007097 } 007098 } 007099 if( i>=pAggInfo->nFunc ){ 007100 /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 007101 */ 007102 u8 enc = ENC(pParse->db); 007103 i = addAggInfoFunc(pParse->db, pAggInfo); 007104 if( i>=0 ){ 007105 int nArg; 007106 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 007107 pItem = &pAggInfo->aFunc[i]; 007108 pItem->pFExpr = pExpr; 007109 assert( ExprUseUToken(pExpr) ); 007110 nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0; 007111 pItem->pFunc = sqlite3FindFunction(pParse->db, 007112 pExpr->u.zToken, nArg, enc, 0); 007113 assert( pItem->bOBUnique==0 ); 007114 if( pExpr->pLeft 007115 && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)==0 007116 ){ 007117 /* The NEEDCOLL test above causes any ORDER BY clause on 007118 ** aggregate min() or max() to be ignored. */ 007119 ExprList *pOBList; 007120 assert( nArg>0 ); 007121 assert( pExpr->pLeft->op==TK_ORDER ); 007122 assert( ExprUseXList(pExpr->pLeft) ); 007123 pItem->iOBTab = pParse->nTab++; 007124 pOBList = pExpr->pLeft->x.pList; 007125 assert( pOBList->nExpr>0 ); 007126 assert( pItem->bOBUnique==0 ); 007127 if( pOBList->nExpr==1 007128 && nArg==1 007129 && sqlite3ExprCompare(0,pOBList->a[0].pExpr, 007130 pExpr->x.pList->a[0].pExpr,0)==0 007131 ){ 007132 pItem->bOBPayload = 0; 007133 pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct); 007134 }else{ 007135 pItem->bOBPayload = 1; 007136 } 007137 pItem->bUseSubtype = 007138 (pItem->pFunc->funcFlags & SQLITE_SUBTYPE)!=0; 007139 }else{ 007140 pItem->iOBTab = -1; 007141 } 007142 if( ExprHasProperty(pExpr, EP_Distinct) && !pItem->bOBUnique ){ 007143 pItem->iDistinct = pParse->nTab++; 007144 }else{ 007145 pItem->iDistinct = -1; 007146 } 007147 } 007148 } 007149 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 007150 */ 007151 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 007152 ExprSetVVAProperty(pExpr, EP_NoReduce); 007153 pExpr->iAgg = (i16)i; 007154 pExpr->pAggInfo = pAggInfo; 007155 return WRC_Prune; 007156 }else{ 007157 return WRC_Continue; 007158 } 007159 } 007160 } 007161 return WRC_Continue; 007162 } 007163 007164 /* 007165 ** Analyze the pExpr expression looking for aggregate functions and 007166 ** for variables that need to be added to AggInfo object that pNC->pAggInfo 007167 ** points to. Additional entries are made on the AggInfo object as 007168 ** necessary. 007169 ** 007170 ** This routine should only be called after the expression has been 007171 ** analyzed by sqlite3ResolveExprNames(). 007172 */ 007173 void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 007174 Walker w; 007175 w.xExprCallback = analyzeAggregate; 007176 w.xSelectCallback = sqlite3WalkerDepthIncrease; 007177 w.xSelectCallback2 = sqlite3WalkerDepthDecrease; 007178 w.walkerDepth = 0; 007179 w.u.pNC = pNC; 007180 w.pParse = 0; 007181 assert( pNC->pSrcList!=0 ); 007182 sqlite3WalkExpr(&w, pExpr); 007183 } 007184 007185 /* 007186 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 007187 ** expression list. Return the number of errors. 007188 ** 007189 ** If an error is found, the analysis is cut short. 007190 */ 007191 void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 007192 struct ExprList_item *pItem; 007193 int i; 007194 if( pList ){ 007195 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 007196 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 007197 } 007198 } 007199 } 007200 007201 /* 007202 ** Allocate a single new register for use to hold some intermediate result. 007203 */ 007204 int sqlite3GetTempReg(Parse *pParse){ 007205 if( pParse->nTempReg==0 ){ 007206 return ++pParse->nMem; 007207 } 007208 return pParse->aTempReg[--pParse->nTempReg]; 007209 } 007210 007211 /* 007212 ** Deallocate a register, making available for reuse for some other 007213 ** purpose. 007214 */ 007215 void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 007216 if( iReg ){ 007217 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0); 007218 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 007219 pParse->aTempReg[pParse->nTempReg++] = iReg; 007220 } 007221 } 007222 } 007223 007224 /* 007225 ** Allocate or deallocate a block of nReg consecutive registers. 007226 */ 007227 int sqlite3GetTempRange(Parse *pParse, int nReg){ 007228 int i, n; 007229 if( nReg==1 ) return sqlite3GetTempReg(pParse); 007230 i = pParse->iRangeReg; 007231 n = pParse->nRangeReg; 007232 if( nReg<=n ){ 007233 pParse->iRangeReg += nReg; 007234 pParse->nRangeReg -= nReg; 007235 }else{ 007236 i = pParse->nMem+1; 007237 pParse->nMem += nReg; 007238 } 007239 return i; 007240 } 007241 void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 007242 if( nReg==1 ){ 007243 sqlite3ReleaseTempReg(pParse, iReg); 007244 return; 007245 } 007246 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0); 007247 if( nReg>pParse->nRangeReg ){ 007248 pParse->nRangeReg = nReg; 007249 pParse->iRangeReg = iReg; 007250 } 007251 } 007252 007253 /* 007254 ** Mark all temporary registers as being unavailable for reuse. 007255 ** 007256 ** Always invoke this procedure after coding a subroutine or co-routine 007257 ** that might be invoked from other parts of the code, to ensure that 007258 ** the sub/co-routine does not use registers in common with the code that 007259 ** invokes the sub/co-routine. 007260 */ 007261 void sqlite3ClearTempRegCache(Parse *pParse){ 007262 pParse->nTempReg = 0; 007263 pParse->nRangeReg = 0; 007264 } 007265 007266 /* 007267 ** Make sure sufficient registers have been allocated so that 007268 ** iReg is a valid register number. 007269 */ 007270 void sqlite3TouchRegister(Parse *pParse, int iReg){ 007271 if( pParse->nMem<iReg ) pParse->nMem = iReg; 007272 } 007273 007274 #if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_DEBUG) 007275 /* 007276 ** Return the latest reusable register in the set of all registers. 007277 ** The value returned is no less than iMin. If any register iMin or 007278 ** greater is in permanent use, then return one more than that last 007279 ** permanent register. 007280 */ 007281 int sqlite3FirstAvailableRegister(Parse *pParse, int iMin){ 007282 const ExprList *pList = pParse->pConstExpr; 007283 if( pList ){ 007284 int i; 007285 for(i=0; i<pList->nExpr; i++){ 007286 if( pList->a[i].u.iConstExprReg>=iMin ){ 007287 iMin = pList->a[i].u.iConstExprReg + 1; 007288 } 007289 } 007290 } 007291 pParse->nTempReg = 0; 007292 pParse->nRangeReg = 0; 007293 return iMin; 007294 } 007295 #endif /* SQLITE_ENABLE_STAT4 || SQLITE_DEBUG */ 007296 007297 /* 007298 ** Validate that no temporary register falls within the range of 007299 ** iFirst..iLast, inclusive. This routine is only call from within assert() 007300 ** statements. 007301 */ 007302 #ifdef SQLITE_DEBUG 007303 int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ 007304 int i; 007305 if( pParse->nRangeReg>0 007306 && pParse->iRangeReg+pParse->nRangeReg > iFirst 007307 && pParse->iRangeReg <= iLast 007308 ){ 007309 return 0; 007310 } 007311 for(i=0; i<pParse->nTempReg; i++){ 007312 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ 007313 return 0; 007314 } 007315 } 007316 if( pParse->pConstExpr ){ 007317 ExprList *pList = pParse->pConstExpr; 007318 for(i=0; i<pList->nExpr; i++){ 007319 int iReg = pList->a[i].u.iConstExprReg; 007320 if( iReg==0 ) continue; 007321 if( iReg>=iFirst && iReg<=iLast ) return 0; 007322 } 007323 } 007324 return 1; 007325 } 007326 #endif /* SQLITE_DEBUG */