1cce7d176Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 3cce7d176Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 6cce7d176Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 10cce7d176Sdrh ** 11cce7d176Sdrh ************************************************************************* 121ccde15dSdrh ** This file contains routines used for analyzing expressions and 13b19a2bc6Sdrh ** for generating VDBE code that evaluates expressions in SQLite. 14cce7d176Sdrh ** 15*d7eb2ed5Sdanielk1977 ** $Id: expr.c,v 1.368 2008/04/24 12:36:35 danielk1977 Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20e014a838Sdanielk1977 /* 21e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any. 22e014a838Sdanielk1977 ** 23e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias, 24e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the 25e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned, 26e014a838Sdanielk1977 ** indicating no affinity for the expression. 27e014a838Sdanielk1977 ** 28e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all 29e014a838Sdanielk1977 ** have an affinity: 30e014a838Sdanielk1977 ** 31e014a838Sdanielk1977 ** CREATE TABLE t1(a); 32e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a; 33e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b; 34e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1); 35e014a838Sdanielk1977 */ 36bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){ 37487e262fSdrh int op = pExpr->op; 38487e262fSdrh if( op==TK_SELECT ){ 39bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 40a37cdde0Sdanielk1977 } 41487e262fSdrh #ifndef SQLITE_OMIT_CAST 42487e262fSdrh if( op==TK_CAST ){ 438a51256cSdrh return sqlite3AffinityType(&pExpr->token); 44487e262fSdrh } 45487e262fSdrh #endif 46a37cdde0Sdanielk1977 return pExpr->affinity; 47a37cdde0Sdanielk1977 } 48a37cdde0Sdanielk1977 4953db1458Sdrh /* 508b4c40d8Sdrh ** Set the collating sequence for expression pExpr to be the collating 518b4c40d8Sdrh ** sequence named by pToken. Return a pointer to the revised expression. 52a34001c9Sdrh ** The collating sequence is marked as "explicit" using the EP_ExpCollate 53a34001c9Sdrh ** flag. An explicit collating sequence will override implicit 54a34001c9Sdrh ** collating sequences. 558b4c40d8Sdrh */ 568b4c40d8Sdrh Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){ 5739002505Sdanielk1977 char *zColl = 0; /* Dequoted name of collation sequence */ 588b4c40d8Sdrh CollSeq *pColl; 5939002505Sdanielk1977 zColl = sqlite3NameFromToken(pParse->db, pName); 6039002505Sdanielk1977 if( pExpr && zColl ){ 6139002505Sdanielk1977 pColl = sqlite3LocateCollSeq(pParse, zColl, -1); 628b4c40d8Sdrh if( pColl ){ 638b4c40d8Sdrh pExpr->pColl = pColl; 648b4c40d8Sdrh pExpr->flags |= EP_ExpCollate; 658b4c40d8Sdrh } 6639002505Sdanielk1977 } 6739002505Sdanielk1977 sqlite3_free(zColl); 688b4c40d8Sdrh return pExpr; 698b4c40d8Sdrh } 708b4c40d8Sdrh 718b4c40d8Sdrh /* 720202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 730202b29eSdanielk1977 ** there is no default collation type, return 0. 740202b29eSdanielk1977 */ 757cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 767cedc8d4Sdanielk1977 CollSeq *pColl = 0; 770202b29eSdanielk1977 if( pExpr ){ 787e09fe0bSdrh int op; 797cedc8d4Sdanielk1977 pColl = pExpr->pColl; 807e09fe0bSdrh op = pExpr->op; 817e09fe0bSdrh if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){ 827cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 830202b29eSdanielk1977 } 840202b29eSdanielk1977 } 857cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 867cedc8d4Sdanielk1977 pColl = 0; 877cedc8d4Sdanielk1977 } 887cedc8d4Sdanielk1977 return pColl; 890202b29eSdanielk1977 } 900202b29eSdanielk1977 910202b29eSdanielk1977 /* 92626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 93626a879aSdrh ** type affinity of the other operand. This routine returns the 9453db1458Sdrh ** type affinity that should be used for the comparison operator. 9553db1458Sdrh */ 96e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 97bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 98e014a838Sdanielk1977 if( aff1 && aff2 ){ 998df447f0Sdrh /* Both sides of the comparison are columns. If one has numeric 1008df447f0Sdrh ** affinity, use that. Otherwise use no affinity. 101e014a838Sdanielk1977 */ 1028a51256cSdrh if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ 103e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 104e014a838Sdanielk1977 }else{ 105e014a838Sdanielk1977 return SQLITE_AFF_NONE; 106e014a838Sdanielk1977 } 107e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 1085f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 1095f6a87b3Sdrh ** results directly. 110e014a838Sdanielk1977 */ 1115f6a87b3Sdrh return SQLITE_AFF_NONE; 112e014a838Sdanielk1977 }else{ 113e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 114fe05af87Sdrh assert( aff1==0 || aff2==0 ); 115e014a838Sdanielk1977 return (aff1 + aff2); 116e014a838Sdanielk1977 } 117e014a838Sdanielk1977 } 118e014a838Sdanielk1977 11953db1458Sdrh /* 12053db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 12153db1458Sdrh ** be applied to both operands prior to doing the comparison. 12253db1458Sdrh */ 123e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 124e014a838Sdanielk1977 char aff; 125e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 126e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 127e014a838Sdanielk1977 pExpr->op==TK_NE ); 128e014a838Sdanielk1977 assert( pExpr->pLeft ); 129bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 130e014a838Sdanielk1977 if( pExpr->pRight ){ 131e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 132e014a838Sdanielk1977 } 133e014a838Sdanielk1977 else if( pExpr->pSelect ){ 134e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 135e014a838Sdanielk1977 } 136e014a838Sdanielk1977 else if( !aff ){ 137de087bd5Sdrh aff = SQLITE_AFF_NONE; 138e014a838Sdanielk1977 } 139e014a838Sdanielk1977 return aff; 140e014a838Sdanielk1977 } 141e014a838Sdanielk1977 142e014a838Sdanielk1977 /* 143e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 144e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 145e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 146e014a838Sdanielk1977 ** the comparison in pExpr. 147e014a838Sdanielk1977 */ 148e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 149e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 1508a51256cSdrh switch( aff ){ 1518a51256cSdrh case SQLITE_AFF_NONE: 1528a51256cSdrh return 1; 1538a51256cSdrh case SQLITE_AFF_TEXT: 1548a51256cSdrh return idx_affinity==SQLITE_AFF_TEXT; 1558a51256cSdrh default: 1568a51256cSdrh return sqlite3IsNumericAffinity(idx_affinity); 1578a51256cSdrh } 158e014a838Sdanielk1977 } 159e014a838Sdanielk1977 160a37cdde0Sdanielk1977 /* 16135573356Sdrh ** Return the P5 value that should be used for a binary comparison 162a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 163a37cdde0Sdanielk1977 */ 16435573356Sdrh static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 16535573356Sdrh u8 aff = (char)sqlite3ExprAffinity(pExpr2); 16635573356Sdrh aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull; 16735573356Sdrh return aff; 168a37cdde0Sdanielk1977 } 169a37cdde0Sdanielk1977 170a2e00042Sdrh /* 1710202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1720202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1730202b29eSdanielk1977 ** 1740202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1750202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1760202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1770202b29eSdanielk1977 ** type. 178bcbb04e5Sdanielk1977 ** 179bcbb04e5Sdanielk1977 ** Argument pRight (but not pLeft) may be a null pointer. In this case, 180bcbb04e5Sdanielk1977 ** it is not considered. 1810202b29eSdanielk1977 */ 182bcbb04e5Sdanielk1977 CollSeq *sqlite3BinaryCompareCollSeq( 183bcbb04e5Sdanielk1977 Parse *pParse, 184bcbb04e5Sdanielk1977 Expr *pLeft, 185bcbb04e5Sdanielk1977 Expr *pRight 186bcbb04e5Sdanielk1977 ){ 187ec41ddacSdrh CollSeq *pColl; 188ec41ddacSdrh assert( pLeft ); 189ec41ddacSdrh if( pLeft->flags & EP_ExpCollate ){ 190ec41ddacSdrh assert( pLeft->pColl ); 191ec41ddacSdrh pColl = pLeft->pColl; 192bcbb04e5Sdanielk1977 }else if( pRight && pRight->flags & EP_ExpCollate ){ 193ec41ddacSdrh assert( pRight->pColl ); 194ec41ddacSdrh pColl = pRight->pColl; 195ec41ddacSdrh }else{ 196ec41ddacSdrh pColl = sqlite3ExprCollSeq(pParse, pLeft); 1970202b29eSdanielk1977 if( !pColl ){ 1987cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 1990202b29eSdanielk1977 } 200ec41ddacSdrh } 2010202b29eSdanielk1977 return pColl; 2020202b29eSdanielk1977 } 2030202b29eSdanielk1977 2040202b29eSdanielk1977 /* 205da250ea5Sdrh ** Generate the operands for a comparison operation. Before 206da250ea5Sdrh ** generating the code for each operand, set the EP_AnyAff 207da250ea5Sdrh ** flag on the expression so that it will be able to used a 208da250ea5Sdrh ** cached column value that has previously undergone an 209da250ea5Sdrh ** affinity change. 210da250ea5Sdrh */ 211da250ea5Sdrh static void codeCompareOperands( 212da250ea5Sdrh Parse *pParse, /* Parsing and code generating context */ 213da250ea5Sdrh Expr *pLeft, /* The left operand */ 214da250ea5Sdrh int *pRegLeft, /* Register where left operand is stored */ 215da250ea5Sdrh int *pFreeLeft, /* Free this register when done */ 216da250ea5Sdrh Expr *pRight, /* The right operand */ 217da250ea5Sdrh int *pRegRight, /* Register where right operand is stored */ 218da250ea5Sdrh int *pFreeRight /* Write temp register for right operand there */ 219da250ea5Sdrh ){ 220da250ea5Sdrh while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft; 221da250ea5Sdrh pLeft->flags |= EP_AnyAff; 222da250ea5Sdrh *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft); 223da250ea5Sdrh while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft; 224da250ea5Sdrh pRight->flags |= EP_AnyAff; 225da250ea5Sdrh *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight); 226da250ea5Sdrh } 227da250ea5Sdrh 228da250ea5Sdrh /* 229be5c89acSdrh ** Generate code for a comparison operator. 230be5c89acSdrh */ 231be5c89acSdrh static int codeCompare( 232be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 233be5c89acSdrh Expr *pLeft, /* The left operand */ 234be5c89acSdrh Expr *pRight, /* The right operand */ 235be5c89acSdrh int opcode, /* The comparison opcode */ 23635573356Sdrh int in1, int in2, /* Register holding operands */ 237be5c89acSdrh int dest, /* Jump here if true. */ 238be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 239be5c89acSdrh ){ 24035573356Sdrh int p5; 24135573356Sdrh int addr; 24235573356Sdrh CollSeq *p4; 24335573356Sdrh 24435573356Sdrh p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); 24535573356Sdrh p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); 24635573356Sdrh addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, 24735573356Sdrh (void*)p4, P4_COLLSEQ); 24835573356Sdrh sqlite3VdbeChangeP5(pParse->pVdbe, p5); 2492f7794c1Sdrh if( p5 & SQLITE_AFF_MASK ){ 250da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, in1, 1); 251da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, in2, 1); 2522f7794c1Sdrh } 25335573356Sdrh return addr; 254be5c89acSdrh } 255be5c89acSdrh 256be5c89acSdrh /* 257a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 25817435752Sdrh ** for this node is obtained from sqlite3_malloc(). The calling function 259a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 260a76b5dfcSdrh */ 26117435752Sdrh Expr *sqlite3Expr( 262a1644fd8Sdanielk1977 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 26317435752Sdrh int op, /* Expression opcode */ 26417435752Sdrh Expr *pLeft, /* Left operand */ 26517435752Sdrh Expr *pRight, /* Right operand */ 26617435752Sdrh const Token *pToken /* Argument token */ 26717435752Sdrh ){ 268a76b5dfcSdrh Expr *pNew; 2695c070538Sdrh static const Expr zeroExpr; 2705c070538Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(Expr)); 271a76b5dfcSdrh if( pNew==0 ){ 272d5d56523Sdanielk1977 /* When malloc fails, delete pLeft and pRight. Expressions passed to 273d5d56523Sdanielk1977 ** this function must always be allocated with sqlite3Expr() for this 274d5d56523Sdanielk1977 ** reason. 275d5d56523Sdanielk1977 */ 276d5d56523Sdanielk1977 sqlite3ExprDelete(pLeft); 277d5d56523Sdanielk1977 sqlite3ExprDelete(pRight); 278a76b5dfcSdrh return 0; 279a76b5dfcSdrh } 2805c070538Sdrh *pNew = zeroExpr; 281a76b5dfcSdrh pNew->op = op; 282a76b5dfcSdrh pNew->pLeft = pLeft; 283a76b5dfcSdrh pNew->pRight = pRight; 284a58fdfb1Sdanielk1977 pNew->iAgg = -1; 285a76b5dfcSdrh if( pToken ){ 2864b59ab5eSdrh assert( pToken->dyn==0 ); 287145716b3Sdrh pNew->span = pNew->token = *pToken; 288a34001c9Sdrh }else if( pLeft ){ 289a34001c9Sdrh if( pRight ){ 2904adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 2915ffb3ac8Sdrh if( pRight->flags & EP_ExpCollate ){ 292a34001c9Sdrh pNew->flags |= EP_ExpCollate; 293a34001c9Sdrh pNew->pColl = pRight->pColl; 294a34001c9Sdrh } 295a34001c9Sdrh } 2965ffb3ac8Sdrh if( pLeft->flags & EP_ExpCollate ){ 297a34001c9Sdrh pNew->flags |= EP_ExpCollate; 298a34001c9Sdrh pNew->pColl = pLeft->pColl; 299a34001c9Sdrh } 300a76b5dfcSdrh } 301fc976065Sdanielk1977 302fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 303a76b5dfcSdrh return pNew; 304a76b5dfcSdrh } 305a76b5dfcSdrh 306a76b5dfcSdrh /* 30717435752Sdrh ** Works like sqlite3Expr() except that it takes an extra Parse* 30817435752Sdrh ** argument and notifies the associated connection object if malloc fails. 309206f3d96Sdrh */ 31017435752Sdrh Expr *sqlite3PExpr( 31117435752Sdrh Parse *pParse, /* Parsing context */ 31217435752Sdrh int op, /* Expression opcode */ 31317435752Sdrh Expr *pLeft, /* Left operand */ 31417435752Sdrh Expr *pRight, /* Right operand */ 31517435752Sdrh const Token *pToken /* Argument token */ 31617435752Sdrh ){ 317a1644fd8Sdanielk1977 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); 318206f3d96Sdrh } 319206f3d96Sdrh 320206f3d96Sdrh /* 3214e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 322b7654111Sdrh ** that look like this: #1 #2 ... These terms refer to registers 323b7654111Sdrh ** in the virtual machine. #N is the N-th register. 3244e0cff60Sdrh ** 3254e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 3264e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 3274e0cff60Sdrh ** The returns an expression that will code to extract the value from 3284e0cff60Sdrh ** that memory location as needed. 3294e0cff60Sdrh */ 3304e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 3314e0cff60Sdrh Vdbe *v = pParse->pVdbe; 3324e0cff60Sdrh Expr *p; 3334e0cff60Sdrh if( pParse->nested==0 ){ 3344e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 335a1644fd8Sdanielk1977 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); 3364e0cff60Sdrh } 337bb7ac00bSdrh if( v==0 ) return 0; 338a1644fd8Sdanielk1977 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); 33973c42a13Sdrh if( p==0 ){ 34073c42a13Sdrh return 0; /* Malloc failed */ 34173c42a13Sdrh } 342b7654111Sdrh p->iTable = atoi((char*)&pToken->z[1]); 3434e0cff60Sdrh return p; 3444e0cff60Sdrh } 3454e0cff60Sdrh 3464e0cff60Sdrh /* 34791bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 34891bb0eedSdrh ** NULL, then just return the other expression. 34991bb0eedSdrh */ 3501e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 35191bb0eedSdrh if( pLeft==0 ){ 35291bb0eedSdrh return pRight; 35391bb0eedSdrh }else if( pRight==0 ){ 35491bb0eedSdrh return pLeft; 35591bb0eedSdrh }else{ 356880c15beSdanielk1977 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0); 35791bb0eedSdrh } 35891bb0eedSdrh } 35991bb0eedSdrh 36091bb0eedSdrh /* 3616977fea8Sdrh ** Set the Expr.span field of the given expression to span all 362a76b5dfcSdrh ** text between the two given tokens. 363a76b5dfcSdrh */ 3644adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 3654efc4754Sdrh assert( pRight!=0 ); 3664efc4754Sdrh assert( pLeft!=0 ); 367f3a65f7eSdrh if( pExpr && pRight->z && pLeft->z ){ 368ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 369145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 3706977fea8Sdrh pExpr->span.z = pLeft->z; 37197903fefSdrh pExpr->span.n = pRight->n + (pRight->z - pLeft->z); 3724b59ab5eSdrh }else{ 3736977fea8Sdrh pExpr->span.z = 0; 3744b59ab5eSdrh } 375a76b5dfcSdrh } 376a76b5dfcSdrh } 377a76b5dfcSdrh 378a76b5dfcSdrh /* 379a76b5dfcSdrh ** Construct a new expression node for a function with multiple 380a76b5dfcSdrh ** arguments. 381a76b5dfcSdrh */ 38217435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 383a76b5dfcSdrh Expr *pNew; 3844b202ae2Sdanielk1977 assert( pToken ); 38517435752Sdrh pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) ); 386a76b5dfcSdrh if( pNew==0 ){ 387d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ 388a76b5dfcSdrh return 0; 389a76b5dfcSdrh } 390a76b5dfcSdrh pNew->op = TK_FUNCTION; 391a76b5dfcSdrh pNew->pList = pList; 3924b59ab5eSdrh assert( pToken->dyn==0 ); 393a76b5dfcSdrh pNew->token = *pToken; 3946977fea8Sdrh pNew->span = pNew->token; 395fc976065Sdanielk1977 396fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 397a76b5dfcSdrh return pNew; 398a76b5dfcSdrh } 399a76b5dfcSdrh 400a76b5dfcSdrh /* 401fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 402fa6bc000Sdrh ** in the original SQL statement. 403fa6bc000Sdrh ** 404fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 405fa6bc000Sdrh ** variable number. 406fa6bc000Sdrh ** 407fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 408fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 409fa6bc000Sdrh ** the SQL statement comes from an external source. 410fa6bc000Sdrh ** 411fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 412fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 413fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 414fa6bc000Sdrh ** assigned. 415fa6bc000Sdrh */ 416fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 417fa6bc000Sdrh Token *pToken; 41817435752Sdrh sqlite3 *db = pParse->db; 41917435752Sdrh 420fa6bc000Sdrh if( pExpr==0 ) return; 421fa6bc000Sdrh pToken = &pExpr->token; 422fa6bc000Sdrh assert( pToken->n>=1 ); 423fa6bc000Sdrh assert( pToken->z!=0 ); 424fa6bc000Sdrh assert( pToken->z[0]!=0 ); 425fa6bc000Sdrh if( pToken->n==1 ){ 426fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 427fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 428fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 429fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 430fa6bc000Sdrh ** use it as the variable number */ 431fa6bc000Sdrh int i; 4322646da7eSdrh pExpr->iTable = i = atoi((char*)&pToken->z[1]); 433c5499befSdrh testcase( i==0 ); 434c5499befSdrh testcase( i==1 ); 435c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 436c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 437bb4957f8Sdrh if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 438fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 439bb4957f8Sdrh db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 440fa6bc000Sdrh } 441fa6bc000Sdrh if( i>pParse->nVar ){ 442fa6bc000Sdrh pParse->nVar = i; 443fa6bc000Sdrh } 444fa6bc000Sdrh }else{ 445fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 446fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 447fa6bc000Sdrh ** has never appeared before, reuse the same variable number 448fa6bc000Sdrh */ 449fa6bc000Sdrh int i, n; 450fa6bc000Sdrh n = pToken->n; 451fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 452fa6bc000Sdrh Expr *pE; 453fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 454fa6bc000Sdrh && pE->token.n==n 455fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 456fa6bc000Sdrh pExpr->iTable = pE->iTable; 457fa6bc000Sdrh break; 458fa6bc000Sdrh } 459fa6bc000Sdrh } 460fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 461fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 462fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 463fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 46417435752Sdrh pParse->apVarExpr = 46517435752Sdrh sqlite3DbReallocOrFree( 46617435752Sdrh db, 46717435752Sdrh pParse->apVarExpr, 46817435752Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) 46917435752Sdrh ); 470fa6bc000Sdrh } 47117435752Sdrh if( !db->mallocFailed ){ 472fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 473fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 474fa6bc000Sdrh } 475fa6bc000Sdrh } 476fa6bc000Sdrh } 477bb4957f8Sdrh if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 478832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 479832b2664Sdanielk1977 } 480fa6bc000Sdrh } 481fa6bc000Sdrh 482fa6bc000Sdrh /* 483a2e00042Sdrh ** Recursively delete an expression tree. 484a2e00042Sdrh */ 4854adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 486a2e00042Sdrh if( p==0 ) return; 48717435752Sdrh if( p->span.dyn ) sqlite3_free((char*)p->span.z); 48817435752Sdrh if( p->token.dyn ) sqlite3_free((char*)p->token.z); 4894adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 4904adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 4914adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 4924adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 49317435752Sdrh sqlite3_free(p); 494a2e00042Sdrh } 495a2e00042Sdrh 496d2687b77Sdrh /* 497d2687b77Sdrh ** The Expr.token field might be a string literal that is quoted. 498d2687b77Sdrh ** If so, remove the quotation marks. 499d2687b77Sdrh */ 50017435752Sdrh void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ 501d2687b77Sdrh if( ExprHasAnyProperty(p, EP_Dequoted) ){ 502d2687b77Sdrh return; 503d2687b77Sdrh } 504d2687b77Sdrh ExprSetProperty(p, EP_Dequoted); 505d2687b77Sdrh if( p->token.dyn==0 ){ 50617435752Sdrh sqlite3TokenCopy(db, &p->token, &p->token); 507d2687b77Sdrh } 508d2687b77Sdrh sqlite3Dequote((char*)p->token.z); 509d2687b77Sdrh } 510d2687b77Sdrh 511a76b5dfcSdrh 512a76b5dfcSdrh /* 513ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 514ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 515ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 516ff78bd2fSdrh ** without effecting the originals. 517ff78bd2fSdrh ** 5184adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 5194adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 520ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 521ff78bd2fSdrh ** 522ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 523ff78bd2fSdrh */ 5241e536953Sdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){ 525ff78bd2fSdrh Expr *pNew; 526ff78bd2fSdrh if( p==0 ) return 0; 52717435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); 528ff78bd2fSdrh if( pNew==0 ) return 0; 5293b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 5306977fea8Sdrh if( p->token.z!=0 ){ 53117435752Sdrh pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); 5324b59ab5eSdrh pNew->token.dyn = 1; 5334b59ab5eSdrh }else{ 5344efc4754Sdrh assert( pNew->token.z==0 ); 5354b59ab5eSdrh } 5366977fea8Sdrh pNew->span.z = 0; 53717435752Sdrh pNew->pLeft = sqlite3ExprDup(db, p->pLeft); 53817435752Sdrh pNew->pRight = sqlite3ExprDup(db, p->pRight); 53917435752Sdrh pNew->pList = sqlite3ExprListDup(db, p->pList); 54017435752Sdrh pNew->pSelect = sqlite3SelectDup(db, p->pSelect); 541ff78bd2fSdrh return pNew; 542ff78bd2fSdrh } 54317435752Sdrh void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ 54417435752Sdrh if( pTo->dyn ) sqlite3_free((char*)pTo->z); 5454b59ab5eSdrh if( pFrom->z ){ 5464b59ab5eSdrh pTo->n = pFrom->n; 54717435752Sdrh pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); 5484b59ab5eSdrh pTo->dyn = 1; 5494b59ab5eSdrh }else{ 5504b59ab5eSdrh pTo->z = 0; 5514b59ab5eSdrh } 5524b59ab5eSdrh } 55317435752Sdrh ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ 554ff78bd2fSdrh ExprList *pNew; 555145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 556ff78bd2fSdrh int i; 557ff78bd2fSdrh if( p==0 ) return 0; 55817435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 559ff78bd2fSdrh if( pNew==0 ) return 0; 56031dad9daSdanielk1977 pNew->iECursor = 0; 5614305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 56217435752Sdrh pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); 563e0048400Sdanielk1977 if( pItem==0 ){ 56417435752Sdrh sqlite3_free(pNew); 565e0048400Sdanielk1977 return 0; 566e0048400Sdanielk1977 } 567145716b3Sdrh pOldItem = p->a; 568145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 5694b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 57017435752Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); 5716977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 5726977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 5734b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 5744b59ab5eSdrh ** the names of columns in the result set needs this information */ 57517435752Sdrh sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); 5764b59ab5eSdrh } 5771f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 5786f7adc8aSdrh || pOldExpr->span.z==0 57917435752Sdrh || db->mallocFailed ); 58017435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 581145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 582145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 5833e7bc9caSdrh pItem->done = 0; 584ff78bd2fSdrh } 585ff78bd2fSdrh return pNew; 586ff78bd2fSdrh } 58793758c8dSdanielk1977 58893758c8dSdanielk1977 /* 58993758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 59093758c8dSdanielk1977 ** the build, then none of the following routines, except for 59193758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 59293758c8dSdanielk1977 ** called with a NULL argument. 59393758c8dSdanielk1977 */ 5946a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 5956a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 59617435752Sdrh SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ 597ad3cab52Sdrh SrcList *pNew; 598ad3cab52Sdrh int i; 599113088ecSdrh int nByte; 600ad3cab52Sdrh if( p==0 ) return 0; 601113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 60217435752Sdrh pNew = sqlite3DbMallocRaw(db, nByte ); 603ad3cab52Sdrh if( pNew==0 ) return 0; 6044305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 605ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 6064efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 6074efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 608ed8a3bb1Sdrh Table *pTab; 60917435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 61017435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 61117435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 6124efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 6134efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 6141787ccabSdanielk1977 pNewItem->isPopulated = pOldItem->isPopulated; 615ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 616ed8a3bb1Sdrh if( pTab ){ 617ed8a3bb1Sdrh pTab->nRef++; 618a1cb183dSdanielk1977 } 61917435752Sdrh pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); 62017435752Sdrh pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); 62117435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 6226c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 623ad3cab52Sdrh } 624ad3cab52Sdrh return pNew; 625ad3cab52Sdrh } 62617435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 627ff78bd2fSdrh IdList *pNew; 628ff78bd2fSdrh int i; 629ff78bd2fSdrh if( p==0 ) return 0; 63017435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 631ff78bd2fSdrh if( pNew==0 ) return 0; 6324305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 63317435752Sdrh pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); 634d5d56523Sdanielk1977 if( pNew->a==0 ){ 63517435752Sdrh sqlite3_free(pNew); 636d5d56523Sdanielk1977 return 0; 637d5d56523Sdanielk1977 } 638ff78bd2fSdrh for(i=0; i<p->nId; i++){ 6394efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 6404efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 64117435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 6424efc4754Sdrh pNewItem->idx = pOldItem->idx; 643ff78bd2fSdrh } 644ff78bd2fSdrh return pNew; 645ff78bd2fSdrh } 64617435752Sdrh Select *sqlite3SelectDup(sqlite3 *db, Select *p){ 647ff78bd2fSdrh Select *pNew; 648ff78bd2fSdrh if( p==0 ) return 0; 64917435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); 650ff78bd2fSdrh if( pNew==0 ) return 0; 651ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 65217435752Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList); 65317435752Sdrh pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); 65417435752Sdrh pNew->pWhere = sqlite3ExprDup(db, p->pWhere); 65517435752Sdrh pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); 65617435752Sdrh pNew->pHaving = sqlite3ExprDup(db, p->pHaving); 65717435752Sdrh pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); 658ff78bd2fSdrh pNew->op = p->op; 65917435752Sdrh pNew->pPrior = sqlite3SelectDup(db, p->pPrior); 66017435752Sdrh pNew->pLimit = sqlite3ExprDup(db, p->pLimit); 66117435752Sdrh pNew->pOffset = sqlite3ExprDup(db, p->pOffset); 6627b58daeaSdrh pNew->iLimit = -1; 6637b58daeaSdrh pNew->iOffset = -1; 664a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 665a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 666b9bb7c18Sdrh pNew->usesEphm = 0; 6678e647b81Sdrh pNew->disallowOrderBy = 0; 6680342b1f5Sdrh pNew->pRightmost = 0; 669b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 670b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 671b9bb7c18Sdrh pNew->addrOpenEphm[2] = -1; 672ff78bd2fSdrh return pNew; 673ff78bd2fSdrh } 67493758c8dSdanielk1977 #else 67517435752Sdrh Select *sqlite3SelectDup(sqlite3 *db, Select *p){ 67693758c8dSdanielk1977 assert( p==0 ); 67793758c8dSdanielk1977 return 0; 67893758c8dSdanielk1977 } 67993758c8dSdanielk1977 #endif 680ff78bd2fSdrh 681ff78bd2fSdrh 682ff78bd2fSdrh /* 683a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 684a76b5dfcSdrh ** initially NULL, then create a new expression list. 685a76b5dfcSdrh */ 68617435752Sdrh ExprList *sqlite3ExprListAppend( 68717435752Sdrh Parse *pParse, /* Parsing context */ 68817435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 68917435752Sdrh Expr *pExpr, /* Expression to be appended */ 69017435752Sdrh Token *pName /* AS keyword for the expression */ 69117435752Sdrh ){ 69217435752Sdrh sqlite3 *db = pParse->db; 693a76b5dfcSdrh if( pList==0 ){ 69417435752Sdrh pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); 695a76b5dfcSdrh if( pList==0 ){ 696d5d56523Sdanielk1977 goto no_mem; 697a76b5dfcSdrh } 6984efc4754Sdrh assert( pList->nAlloc==0 ); 699a76b5dfcSdrh } 7004305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 701d5d56523Sdanielk1977 struct ExprList_item *a; 702d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 70326783a58Sdanielk1977 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); 704d5d56523Sdanielk1977 if( a==0 ){ 705d5d56523Sdanielk1977 goto no_mem; 706a76b5dfcSdrh } 707d5d56523Sdanielk1977 pList->a = a; 708d5d56523Sdanielk1977 pList->nAlloc = n; 709a76b5dfcSdrh } 7104efc4754Sdrh assert( pList->a!=0 ); 7114efc4754Sdrh if( pExpr || pName ){ 7124efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 7134efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 71417435752Sdrh pItem->zName = sqlite3NameFromToken(db, pName); 715e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 716a76b5dfcSdrh } 717a76b5dfcSdrh return pList; 718d5d56523Sdanielk1977 719d5d56523Sdanielk1977 no_mem: 720d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 721d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 722d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 723d5d56523Sdanielk1977 return 0; 724a76b5dfcSdrh } 725a76b5dfcSdrh 726a76b5dfcSdrh /* 7277a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 7287a15a4beSdanielk1977 ** leave an error message in pParse. 7297a15a4beSdanielk1977 */ 7307a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 7317a15a4beSdanielk1977 Parse *pParse, 7327a15a4beSdanielk1977 ExprList *pEList, 7337a15a4beSdanielk1977 const char *zObject 7347a15a4beSdanielk1977 ){ 735b1a6c3c1Sdrh int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 736c5499befSdrh testcase( pEList && pEList->nExpr==mx ); 737c5499befSdrh testcase( pEList && pEList->nExpr==mx+1 ); 738b1a6c3c1Sdrh if( pEList && pEList->nExpr>mx ){ 7397a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 7407a15a4beSdanielk1977 } 7417a15a4beSdanielk1977 } 7427a15a4beSdanielk1977 743fc976065Sdanielk1977 744fc976065Sdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 745fc976065Sdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 746fc976065Sdanielk1977 ** of any expression tree referenced by the structure passed as the 747fc976065Sdanielk1977 ** first argument. 748fc976065Sdanielk1977 ** 749fc976065Sdanielk1977 ** If this maximum height is greater than the current value pointed 750fc976065Sdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 751fc976065Sdanielk1977 ** value. 752fc976065Sdanielk1977 */ 753fc976065Sdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 754fc976065Sdanielk1977 if( p ){ 755fc976065Sdanielk1977 if( p->nHeight>*pnHeight ){ 756fc976065Sdanielk1977 *pnHeight = p->nHeight; 757fc976065Sdanielk1977 } 758fc976065Sdanielk1977 } 759fc976065Sdanielk1977 } 760fc976065Sdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 761fc976065Sdanielk1977 if( p ){ 762fc976065Sdanielk1977 int i; 763fc976065Sdanielk1977 for(i=0; i<p->nExpr; i++){ 764fc976065Sdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 765fc976065Sdanielk1977 } 766fc976065Sdanielk1977 } 767fc976065Sdanielk1977 } 768fc976065Sdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 769fc976065Sdanielk1977 if( p ){ 770fc976065Sdanielk1977 heightOfExpr(p->pWhere, pnHeight); 771fc976065Sdanielk1977 heightOfExpr(p->pHaving, pnHeight); 772fc976065Sdanielk1977 heightOfExpr(p->pLimit, pnHeight); 773fc976065Sdanielk1977 heightOfExpr(p->pOffset, pnHeight); 774fc976065Sdanielk1977 heightOfExprList(p->pEList, pnHeight); 775fc976065Sdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 776fc976065Sdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 777fc976065Sdanielk1977 heightOfSelect(p->pPrior, pnHeight); 778fc976065Sdanielk1977 } 779fc976065Sdanielk1977 } 780fc976065Sdanielk1977 781fc976065Sdanielk1977 /* 782fc976065Sdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 783fc976065Sdanielk1977 ** argument. An expression with no children, Expr.pList or 784fc976065Sdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 785fc976065Sdanielk1977 ** has a height equal to the maximum height of any other 786fc976065Sdanielk1977 ** referenced Expr plus one. 787fc976065Sdanielk1977 */ 788fc976065Sdanielk1977 void sqlite3ExprSetHeight(Expr *p){ 789fc976065Sdanielk1977 int nHeight = 0; 790fc976065Sdanielk1977 heightOfExpr(p->pLeft, &nHeight); 791fc976065Sdanielk1977 heightOfExpr(p->pRight, &nHeight); 792fc976065Sdanielk1977 heightOfExprList(p->pList, &nHeight); 793fc976065Sdanielk1977 heightOfSelect(p->pSelect, &nHeight); 794fc976065Sdanielk1977 p->nHeight = nHeight + 1; 795fc976065Sdanielk1977 } 796fc976065Sdanielk1977 797fc976065Sdanielk1977 /* 798fc976065Sdanielk1977 ** Return the maximum height of any expression tree referenced 799fc976065Sdanielk1977 ** by the select statement passed as an argument. 800fc976065Sdanielk1977 */ 801fc976065Sdanielk1977 int sqlite3SelectExprHeight(Select *p){ 802fc976065Sdanielk1977 int nHeight = 0; 803fc976065Sdanielk1977 heightOfSelect(p, &nHeight); 804fc976065Sdanielk1977 return nHeight; 805fc976065Sdanielk1977 } 806fc976065Sdanielk1977 8077a15a4beSdanielk1977 /* 808a76b5dfcSdrh ** Delete an entire expression list. 809a76b5dfcSdrh */ 8104adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 811a76b5dfcSdrh int i; 812be5c89acSdrh struct ExprList_item *pItem; 813a76b5dfcSdrh if( pList==0 ) return; 8141bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 8151bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 816be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 817be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 81817435752Sdrh sqlite3_free(pItem->zName); 819a76b5dfcSdrh } 82017435752Sdrh sqlite3_free(pList->a); 82117435752Sdrh sqlite3_free(pList); 822a76b5dfcSdrh } 823a76b5dfcSdrh 824a76b5dfcSdrh /* 825678ccce8Sdrh ** Walk an expression tree. Call xFunc for each node visited. xFunc 826678ccce8Sdrh ** is called on the node before xFunc is called on the nodes children. 82773b211abSdrh ** 828626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 829626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 830626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 831626a879aSdrh ** the tree walk completely. 832626a879aSdrh ** 833626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 834626a879aSdrh ** and 0 to continue. 83587abf5c0Sdrh ** 83687abf5c0Sdrh ** NOTICE: This routine does *not* descend into subqueries. 837626a879aSdrh */ 838a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 839626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 840626a879aSdrh int rc; 841626a879aSdrh if( pExpr==0 ) return 0; 842626a879aSdrh rc = (*xFunc)(pArg, pExpr); 843626a879aSdrh if( rc==0 ){ 844626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 845626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 846a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 847626a879aSdrh } 848626a879aSdrh return rc>1; 849626a879aSdrh } 850626a879aSdrh 851626a879aSdrh /* 852a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 853a58fdfb1Sdanielk1977 */ 854a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 855a58fdfb1Sdanielk1977 int i; 856a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 857a58fdfb1Sdanielk1977 if( !p ) return 0; 858a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 859a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 860a58fdfb1Sdanielk1977 } 861a58fdfb1Sdanielk1977 return 0; 862a58fdfb1Sdanielk1977 } 863a58fdfb1Sdanielk1977 864a58fdfb1Sdanielk1977 /* 865a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 866a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 867a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 868a58fdfb1Sdanielk1977 */ 869a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 870a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 871a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 872a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 873a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 874a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 87515d7982aSdanielk1977 if( p->pPrior ){ 87615d7982aSdanielk1977 walkSelectExpr(p->pPrior, xFunc, pArg); 87715d7982aSdanielk1977 } 878a58fdfb1Sdanielk1977 return 0; 879a58fdfb1Sdanielk1977 } 880a58fdfb1Sdanielk1977 881a58fdfb1Sdanielk1977 882a58fdfb1Sdanielk1977 /* 883626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 884626a879aSdrh ** 885626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 88673b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 88773b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 88873b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 88973b211abSdrh ** then do nothing. 89073b211abSdrh ** 89173b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 89273b211abSdrh ** the expression as constant, then we assume the whole expression 89373b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 894626a879aSdrh */ 895626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 8960a168377Sdrh int *pN = (int*)pArg; 8970a168377Sdrh 8980a168377Sdrh /* If *pArg is 3 then any term of the expression that comes from 8990a168377Sdrh ** the ON or USING clauses of a join disqualifies the expression 9000a168377Sdrh ** from being considered constant. */ 9010a168377Sdrh if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ 9020a168377Sdrh *pN = 0; 9030a168377Sdrh return 2; 9040a168377Sdrh } 9050a168377Sdrh 906626a879aSdrh switch( pExpr->op ){ 907eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 908eb55bd2fSdrh ** and *pArg==2 */ 909eb55bd2fSdrh case TK_FUNCTION: 9100a168377Sdrh if( (*pN)==2 ) return 0; 911eb55bd2fSdrh /* Fall through */ 912626a879aSdrh case TK_ID: 913626a879aSdrh case TK_COLUMN: 914626a879aSdrh case TK_DOT: 915626a879aSdrh case TK_AGG_FUNCTION: 91613449892Sdrh case TK_AGG_COLUMN: 917fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 918fe2093d7Sdrh case TK_SELECT: 919fe2093d7Sdrh case TK_EXISTS: 920c5499befSdrh testcase( pExpr->op==TK_SELECT ); 921c5499befSdrh testcase( pExpr->op==TK_EXISTS ); 922fe2093d7Sdrh #endif 923c5499befSdrh testcase( pExpr->op==TK_ID ); 924c5499befSdrh testcase( pExpr->op==TK_COLUMN ); 925c5499befSdrh testcase( pExpr->op==TK_DOT ); 926c5499befSdrh testcase( pExpr->op==TK_AGG_FUNCTION ); 927c5499befSdrh testcase( pExpr->op==TK_AGG_COLUMN ); 9280a168377Sdrh *pN = 0; 929626a879aSdrh return 2; 93087abf5c0Sdrh case TK_IN: 93187abf5c0Sdrh if( pExpr->pSelect ){ 9320a168377Sdrh *pN = 0; 93387abf5c0Sdrh return 2; 93487abf5c0Sdrh } 935626a879aSdrh default: 936626a879aSdrh return 0; 937626a879aSdrh } 938626a879aSdrh } 939626a879aSdrh 940626a879aSdrh /* 941fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 942eb55bd2fSdrh ** and 0 if it involves variables or function calls. 9432398937bSdrh ** 9442398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 9452398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 9462398937bSdrh ** a constant. 947fef5208cSdrh */ 9484adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 949626a879aSdrh int isConst = 1; 950626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 951626a879aSdrh return isConst; 952fef5208cSdrh } 953fef5208cSdrh 954fef5208cSdrh /* 955eb55bd2fSdrh ** Walk an expression tree. Return 1 if the expression is constant 9560a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 9570a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 9580a168377Sdrh ** an ON or USING clause. 9590a168377Sdrh */ 9600a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 9610a168377Sdrh int isConst = 3; 9620a168377Sdrh walkExprTree(p, exprNodeIsConstant, &isConst); 9630a168377Sdrh return isConst!=0; 9640a168377Sdrh } 9650a168377Sdrh 9660a168377Sdrh /* 9670a168377Sdrh ** Walk an expression tree. Return 1 if the expression is constant 968eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 969eb55bd2fSdrh ** are any variables. 970eb55bd2fSdrh ** 971eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 972eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 973eb55bd2fSdrh ** a constant. 974eb55bd2fSdrh */ 975eb55bd2fSdrh int sqlite3ExprIsConstantOrFunction(Expr *p){ 976eb55bd2fSdrh int isConst = 2; 977eb55bd2fSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 978eb55bd2fSdrh return isConst!=0; 979eb55bd2fSdrh } 980eb55bd2fSdrh 981eb55bd2fSdrh /* 98273b211abSdrh ** If the expression p codes a constant integer that is small enough 983202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 984202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 985202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 986e4de1febSdrh */ 9874adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 988e4de1febSdrh switch( p->op ){ 989e4de1febSdrh case TK_INTEGER: { 9902646da7eSdrh if( sqlite3GetInt32((char*)p->token.z, pValue) ){ 991e4de1febSdrh return 1; 992e4de1febSdrh } 993202b2df7Sdrh break; 994202b2df7Sdrh } 9954b59ab5eSdrh case TK_UPLUS: { 9964adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 9974b59ab5eSdrh } 998e4de1febSdrh case TK_UMINUS: { 999e4de1febSdrh int v; 10004adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 1001e4de1febSdrh *pValue = -v; 1002e4de1febSdrh return 1; 1003e4de1febSdrh } 1004e4de1febSdrh break; 1005e4de1febSdrh } 1006e4de1febSdrh default: break; 1007e4de1febSdrh } 1008e4de1febSdrh return 0; 1009e4de1febSdrh } 1010e4de1febSdrh 1011e4de1febSdrh /* 1012c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 1013c4a3c779Sdrh */ 10144adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 10154adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 10164adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 10174adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 1018c4a3c779Sdrh return 0; 1019c4a3c779Sdrh } 1020c4a3c779Sdrh 1021c4a3c779Sdrh /* 10228141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 10238141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 10248141f61eSdrh ** expression node refer back to that source column. The following changes 10258141f61eSdrh ** are made to pExpr: 10268141f61eSdrh ** 10278141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 10288141f61eSdrh ** the table. 10298141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 10308141f61eSdrh ** from pSrcList. 10318141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 10328141f61eSdrh ** pExpr->op Set to TK_COLUMN. 10338141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 10348141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 10358141f61eSdrh ** 10368141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 10378141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 10388141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 10398141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 10408141f61eSdrh ** means that the form of the name is Z and that columns from any table 10418141f61eSdrh ** can be used. 10428141f61eSdrh ** 10438141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 10448141f61eSdrh ** in pParse and return non-zero. Return zero on success. 10458141f61eSdrh */ 10468141f61eSdrh static int lookupName( 10478141f61eSdrh Parse *pParse, /* The parsing context */ 10488141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 10498141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 10508141f61eSdrh Token *pColumnToken, /* Name of the column. */ 1051626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 10528141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 10538141f61eSdrh ){ 10548141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 10558141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 10568141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 10578141f61eSdrh int i, j; /* Loop counters */ 10588141f61eSdrh int cnt = 0; /* Number of matching column names */ 10598141f61eSdrh int cntTab = 0; /* Number of matching table names */ 10609bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 106151669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 106251669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 106373b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 1064728b5779Sdrh Schema *pSchema = 0; /* Schema of the expression */ 10658141f61eSdrh 10668141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 106717435752Sdrh zDb = sqlite3NameFromToken(db, pDbToken); 106817435752Sdrh zTab = sqlite3NameFromToken(db, pTableToken); 106917435752Sdrh zCol = sqlite3NameFromToken(db, pColumnToken); 107017435752Sdrh if( db->mallocFailed ){ 1071d5d56523Sdanielk1977 goto lookupname_end; 10728141f61eSdrh } 10738141f61eSdrh 10748141f61eSdrh pExpr->iTable = -1; 1075626a879aSdrh while( pNC && cnt==0 ){ 1076ffe07b2dSdrh ExprList *pEList; 1077626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 1078626a879aSdrh 1079b3bce662Sdanielk1977 if( pSrcList ){ 108051669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 108143617e9aSdrh Table *pTab; 108243617e9aSdrh int iDb; 10838141f61eSdrh Column *pCol; 10848141f61eSdrh 108543617e9aSdrh pTab = pItem->pTab; 108643617e9aSdrh assert( pTab!=0 ); 108743617e9aSdrh iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 10888141f61eSdrh assert( pTab->nCol>0 ); 10898141f61eSdrh if( zTab ){ 10908141f61eSdrh if( pItem->zAlias ){ 10918141f61eSdrh char *zTabName = pItem->zAlias; 10924adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 10938141f61eSdrh }else{ 10948141f61eSdrh char *zTabName = pTab->zName; 10954adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 1096da184236Sdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ 10978141f61eSdrh continue; 10988141f61eSdrh } 10998141f61eSdrh } 11008141f61eSdrh } 11018141f61eSdrh if( 0==(cntTab++) ){ 11028141f61eSdrh pExpr->iTable = pItem->iCursor; 1103728b5779Sdrh pSchema = pTab->pSchema; 110451669863Sdrh pMatch = pItem; 11058141f61eSdrh } 11068141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 11074adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1108b3bf556eSdanielk1977 const char *zColl = pTab->aCol[j].zColl; 1109873fac0cSdrh IdList *pUsing; 11108141f61eSdrh cnt++; 11118141f61eSdrh pExpr->iTable = pItem->iCursor; 111251669863Sdrh pMatch = pItem; 1113728b5779Sdrh pSchema = pTab->pSchema; 11148141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 11158141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 1116a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 11178b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1118b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 11198b4c40d8Sdrh } 112061dfc31dSdrh if( i<pSrcList->nSrc-1 ){ 112161dfc31dSdrh if( pItem[1].jointype & JT_NATURAL ){ 1122355ef361Sdrh /* If this match occurred in the left table of a natural join, 1123355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 1124355ef361Sdrh pItem++; 1125355ef361Sdrh i++; 112661dfc31dSdrh }else if( (pUsing = pItem[1].pUsing)!=0 ){ 1127873fac0cSdrh /* If this match occurs on a column that is in the USING clause 1128873fac0cSdrh ** of a join, skip the search of the right table of the join 1129873fac0cSdrh ** to avoid a duplicate match there. */ 1130873fac0cSdrh int k; 1131873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 1132873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 1133873fac0cSdrh pItem++; 1134873fac0cSdrh i++; 1135873fac0cSdrh break; 1136873fac0cSdrh } 1137873fac0cSdrh } 1138873fac0cSdrh } 113961dfc31dSdrh } 11408141f61eSdrh break; 11418141f61eSdrh } 11428141f61eSdrh } 11438141f61eSdrh } 1144b3bce662Sdanielk1977 } 11458141f61eSdrh 1146b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 11478141f61eSdrh /* If we have not already resolved the name, then maybe 11488141f61eSdrh ** it is a new.* or old.* trigger argument reference 11498141f61eSdrh */ 11508141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 11518141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 11528141f61eSdrh Table *pTab = 0; 11538f2c54e6Sdanielk1977 u32 *piColMask; 11544adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 11558141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 11568141f61eSdrh assert( pTriggerStack->pTab ); 11578141f61eSdrh pTab = pTriggerStack->pTab; 11588f2c54e6Sdanielk1977 piColMask = &(pTriggerStack->newColMask); 11594adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 11608141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 11618141f61eSdrh assert( pTriggerStack->pTab ); 11628141f61eSdrh pTab = pTriggerStack->pTab; 11638f2c54e6Sdanielk1977 piColMask = &(pTriggerStack->oldColMask); 11648141f61eSdrh } 11658141f61eSdrh 11668141f61eSdrh if( pTab ){ 1167f0113000Sdanielk1977 int iCol; 11688141f61eSdrh Column *pCol = pTab->aCol; 11698141f61eSdrh 1170728b5779Sdrh pSchema = pTab->pSchema; 11718141f61eSdrh cntTab++; 1172f0113000Sdanielk1977 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { 11734adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1174f0113000Sdanielk1977 const char *zColl = pTab->aCol[iCol].zColl; 11758141f61eSdrh cnt++; 1176f0113000Sdanielk1977 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; 1177f0113000Sdanielk1977 pExpr->affinity = pTab->aCol[iCol].affinity; 11788b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1179b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 11808b4c40d8Sdrh } 1181aee18ef8Sdanielk1977 pExpr->pTab = pTab; 11828f2c54e6Sdanielk1977 if( iCol>=0 ){ 1183c5499befSdrh testcase( iCol==31 ); 1184c5499befSdrh testcase( iCol==32 ); 11858f2c54e6Sdanielk1977 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0); 11868f2c54e6Sdanielk1977 } 11878141f61eSdrh break; 11888141f61eSdrh } 11898141f61eSdrh } 11908141f61eSdrh } 11918141f61eSdrh } 1192b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 11938141f61eSdrh 11948141f61eSdrh /* 11958141f61eSdrh ** Perhaps the name is a reference to the ROWID 11968141f61eSdrh */ 11974adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 11988141f61eSdrh cnt = 1; 11998141f61eSdrh pExpr->iColumn = -1; 12008a51256cSdrh pExpr->affinity = SQLITE_AFF_INTEGER; 12018141f61eSdrh } 12028141f61eSdrh 12038141f61eSdrh /* 12048141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 12058141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 12068141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 12078141f61eSdrh ** 12088141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 12098141f61eSdrh ** 12108141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 12118141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 12128141f61eSdrh ** Note that the expression in the result set should have already been 12138141f61eSdrh ** resolved by the time the WHERE clause is resolved. 12148141f61eSdrh */ 1215ffe07b2dSdrh if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ 12168141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 12178141f61eSdrh char *zAs = pEList->a[j].zName; 12184adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 121936379e97Sdrh Expr *pDup, *pOrig; 12208141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 12214f07e5fbSdrh assert( pExpr->pList==0 ); 12224f07e5fbSdrh assert( pExpr->pSelect==0 ); 122336379e97Sdrh pOrig = pEList->a[j].pExpr; 122436379e97Sdrh if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ 122536379e97Sdrh sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); 122617435752Sdrh sqlite3_free(zCol); 122736379e97Sdrh return 2; 122836379e97Sdrh } 12291e536953Sdanielk1977 pDup = sqlite3ExprDup(db, pOrig); 12304f07e5fbSdrh if( pExpr->flags & EP_ExpCollate ){ 12314f07e5fbSdrh pDup->pColl = pExpr->pColl; 12324f07e5fbSdrh pDup->flags |= EP_ExpCollate; 12334f07e5fbSdrh } 123417435752Sdrh if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z); 123517435752Sdrh if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z); 12364f07e5fbSdrh memcpy(pExpr, pDup, sizeof(*pExpr)); 123717435752Sdrh sqlite3_free(pDup); 123815ccce1cSdrh cnt = 1; 1239c9cf6e3dSdanielk1977 pMatch = 0; 12408141f61eSdrh assert( zTab==0 && zDb==0 ); 124115ccce1cSdrh goto lookupname_end_2; 12428141f61eSdrh } 12438141f61eSdrh } 12448141f61eSdrh } 12458141f61eSdrh 1246626a879aSdrh /* Advance to the next name context. The loop will exit when either 1247626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 1248626a879aSdrh */ 1249626a879aSdrh if( cnt==0 ){ 1250626a879aSdrh pNC = pNC->pNext; 1251626a879aSdrh } 1252626a879aSdrh } 1253626a879aSdrh 12548141f61eSdrh /* 12558141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 12568141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 12578141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 12588141f61eSdrh ** case, we need to return right away and not make any changes to 12598141f61eSdrh ** pExpr. 126015ccce1cSdrh ** 126115ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 126215ccce1cSdrh ** fields are not changed in any context. 12638141f61eSdrh */ 12648141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 126517435752Sdrh sqlite3_free(zCol); 12668141f61eSdrh return 0; 12678141f61eSdrh } 12688141f61eSdrh 12698141f61eSdrh /* 12708141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 12718141f61eSdrh ** more matches. Either way, we have an error. 12728141f61eSdrh */ 12738141f61eSdrh if( cnt!=1 ){ 1274de4fcfddSdrh const char *zErr; 1275de4fcfddSdrh zErr = cnt==0 ? "no such column" : "ambiguous column name"; 12768141f61eSdrh if( zDb ){ 1277de4fcfddSdrh sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); 12788141f61eSdrh }else if( zTab ){ 1279de4fcfddSdrh sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); 12808141f61eSdrh }else{ 1281de4fcfddSdrh sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); 12828141f61eSdrh } 128373b211abSdrh pTopNC->nErr++; 12848141f61eSdrh } 12858141f61eSdrh 128651669863Sdrh /* If a column from a table in pSrcList is referenced, then record 128751669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 128851669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 128951669863Sdrh ** column number is greater than the number of bits in the bitmask 129051669863Sdrh ** then set the high-order bit of the bitmask. 129151669863Sdrh */ 129251669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 129351669863Sdrh int n = pExpr->iColumn; 1294c5499befSdrh testcase( n==sizeof(Bitmask)*8-1 ); 129551669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 129651669863Sdrh n = sizeof(Bitmask)*8-1; 129751669863Sdrh } 129851669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 1299ca83ac51Sdrh pMatch->colUsed |= ((Bitmask)1)<<n; 130051669863Sdrh } 130151669863Sdrh 1302d5d56523Sdanielk1977 lookupname_end: 13038141f61eSdrh /* Clean up and return 13048141f61eSdrh */ 130517435752Sdrh sqlite3_free(zDb); 130617435752Sdrh sqlite3_free(zTab); 13074adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 13088141f61eSdrh pExpr->pLeft = 0; 13094adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 13108141f61eSdrh pExpr->pRight = 0; 13118141f61eSdrh pExpr->op = TK_COLUMN; 131215ccce1cSdrh lookupname_end_2: 131317435752Sdrh sqlite3_free(zCol); 1314626a879aSdrh if( cnt==1 ){ 1315b3bce662Sdanielk1977 assert( pNC!=0 ); 1316728b5779Sdrh sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); 1317aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 1318aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 1319aee18ef8Sdanielk1977 } 132015ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 132115ccce1cSdrh ** the point where the name matched. */ 132215ccce1cSdrh for(;;){ 132315ccce1cSdrh assert( pTopNC!=0 ); 132415ccce1cSdrh pTopNC->nRef++; 132515ccce1cSdrh if( pTopNC==pNC ) break; 132615ccce1cSdrh pTopNC = pTopNC->pNext; 1327626a879aSdrh } 132815ccce1cSdrh return 0; 132915ccce1cSdrh } else { 133015ccce1cSdrh return 1; 133115ccce1cSdrh } 13328141f61eSdrh } 13338141f61eSdrh 13348141f61eSdrh /* 1335626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1336626a879aSdrh ** 133773b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1338626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 133973b211abSdrh ** the tree or 2 to abort the tree walk. 134073b211abSdrh ** 134173b211abSdrh ** This routine also does error checking and name resolution for 134273b211abSdrh ** function names. The operator for aggregate functions is changed 134373b211abSdrh ** to TK_AGG_FUNCTION. 1344626a879aSdrh */ 1345626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1346626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1347626a879aSdrh Parse *pParse; 1348626a879aSdrh 1349b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1350626a879aSdrh assert( pNC!=0 ); 1351626a879aSdrh pParse = pNC->pParse; 1352b3bce662Sdanielk1977 1353626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1354626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1355626a879aSdrh #ifndef NDEBUG 1356f0113000Sdanielk1977 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ 1357f0113000Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 1358940fac9dSdanielk1977 int i; 1359f0113000Sdanielk1977 for(i=0; i<pNC->pSrcList->nSrc; i++){ 1360626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1361626a879aSdrh } 1362626a879aSdrh } 1363626a879aSdrh #endif 1364626a879aSdrh switch( pExpr->op ){ 1365626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1366626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1367626a879aSdrh ** strings (ex: 'abc') are always string literals. 1368626a879aSdrh */ 1369626a879aSdrh case TK_STRING: { 1370626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1371626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1372626a879aSdrh } 1373626a879aSdrh /* A lone identifier is the name of a column. 1374626a879aSdrh */ 1375626a879aSdrh case TK_ID: { 1376626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1377626a879aSdrh return 1; 1378626a879aSdrh } 1379626a879aSdrh 1380626a879aSdrh /* A table name and column name: ID.ID 1381626a879aSdrh ** Or a database, table and column: ID.ID.ID 1382626a879aSdrh */ 1383626a879aSdrh case TK_DOT: { 1384626a879aSdrh Token *pColumn; 1385626a879aSdrh Token *pTable; 1386626a879aSdrh Token *pDb; 1387626a879aSdrh Expr *pRight; 1388626a879aSdrh 1389b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1390626a879aSdrh pRight = pExpr->pRight; 1391626a879aSdrh if( pRight->op==TK_ID ){ 1392626a879aSdrh pDb = 0; 1393626a879aSdrh pTable = &pExpr->pLeft->token; 1394626a879aSdrh pColumn = &pRight->token; 1395626a879aSdrh }else{ 1396626a879aSdrh assert( pRight->op==TK_DOT ); 1397626a879aSdrh pDb = &pExpr->pLeft->token; 1398626a879aSdrh pTable = &pRight->pLeft->token; 1399626a879aSdrh pColumn = &pRight->pRight->token; 1400626a879aSdrh } 1401626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1402626a879aSdrh return 1; 1403626a879aSdrh } 1404626a879aSdrh 1405626a879aSdrh /* Resolve function names 1406626a879aSdrh */ 1407b71090fdSdrh case TK_CONST_FUNC: 1408626a879aSdrh case TK_FUNCTION: { 1409626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1410626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1411626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1412626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1413626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1414626a879aSdrh int i; 14155169bbc6Sdrh int auth; /* Authorization to use the function */ 1416626a879aSdrh int nId; /* Number of characters in function name */ 1417626a879aSdrh const char *zId; /* The function name. */ 141873b211abSdrh FuncDef *pDef; /* Information about the function */ 141914db2665Sdanielk1977 int enc = ENC(pParse->db); /* The database encoding */ 1420626a879aSdrh 14212646da7eSdrh zId = (char*)pExpr->token.z; 1422b71090fdSdrh nId = pExpr->token.n; 1423626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1424626a879aSdrh if( pDef==0 ){ 1425626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1426626a879aSdrh if( pDef==0 ){ 1427626a879aSdrh no_such_func = 1; 1428626a879aSdrh }else{ 1429626a879aSdrh wrong_num_args = 1; 1430626a879aSdrh } 1431626a879aSdrh }else{ 1432626a879aSdrh is_agg = pDef->xFunc==0; 1433626a879aSdrh } 14342fca7fefSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 14355169bbc6Sdrh if( pDef ){ 14365169bbc6Sdrh auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); 14375169bbc6Sdrh if( auth!=SQLITE_OK ){ 14385169bbc6Sdrh if( auth==SQLITE_DENY ){ 14395169bbc6Sdrh sqlite3ErrorMsg(pParse, "not authorized to use function: %s", 14405169bbc6Sdrh pDef->zName); 14415169bbc6Sdrh pNC->nErr++; 14425169bbc6Sdrh } 14435169bbc6Sdrh pExpr->op = TK_NULL; 14445169bbc6Sdrh return 1; 14455169bbc6Sdrh } 14465169bbc6Sdrh } 1447b8b14219Sdrh #endif 1448626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1449626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1450626a879aSdrh pNC->nErr++; 1451626a879aSdrh is_agg = 0; 1452626a879aSdrh }else if( no_such_func ){ 1453626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1454626a879aSdrh pNC->nErr++; 1455626a879aSdrh }else if( wrong_num_args ){ 1456626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1457626a879aSdrh nId, zId); 1458626a879aSdrh pNC->nErr++; 1459626a879aSdrh } 1460626a879aSdrh if( is_agg ){ 1461626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1462626a879aSdrh pNC->hasAgg = 1; 1463626a879aSdrh } 146473b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1465626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 146673b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1467626a879aSdrh } 146873b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1469626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1470626a879aSdrh ** type of the function 1471626a879aSdrh */ 1472626a879aSdrh return is_agg; 1473626a879aSdrh } 1474b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1475b3bce662Sdanielk1977 case TK_SELECT: 1476b3bce662Sdanielk1977 case TK_EXISTS: 1477b3bce662Sdanielk1977 #endif 1478b3bce662Sdanielk1977 case TK_IN: { 1479b3bce662Sdanielk1977 if( pExpr->pSelect ){ 14808a9f38feSdrh int nRef = pNC->nRef; 148106f6541eSdrh #ifndef SQLITE_OMIT_CHECK 148206f6541eSdrh if( pNC->isCheck ){ 148306f6541eSdrh sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); 148406f6541eSdrh } 148506f6541eSdrh #endif 1486b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1487b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1488b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1489b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1490b3bce662Sdanielk1977 } 1491b3bce662Sdanielk1977 } 14924284fb07Sdrh break; 1493b3bce662Sdanielk1977 } 14944284fb07Sdrh #ifndef SQLITE_OMIT_CHECK 14954284fb07Sdrh case TK_VARIABLE: { 14964284fb07Sdrh if( pNC->isCheck ){ 14974284fb07Sdrh sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); 14984284fb07Sdrh } 14994284fb07Sdrh break; 15004284fb07Sdrh } 15014284fb07Sdrh #endif 1502626a879aSdrh } 1503626a879aSdrh return 0; 1504626a879aSdrh } 1505626a879aSdrh 1506626a879aSdrh /* 1507cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1508967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1509aacc543eSdrh ** index to the table in the table list and a column offset. The 1510aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1511aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1512832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1513aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1514aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1515aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1516aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1517aacc543eSdrh ** alias for ROWID. 151819a775c2Sdrh ** 1519626a879aSdrh ** Also resolve function names and check the functions for proper 1520626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1521626a879aSdrh ** have the correct number of arguments. Leave an error message 1522626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1523626a879aSdrh ** 152473b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 152573b211abSdrh ** property on the expression. 1526626a879aSdrh */ 1527626a879aSdrh int sqlite3ExprResolveNames( 1528b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1529b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1530626a879aSdrh ){ 153113449892Sdrh int savedHasAgg; 1532bb4957f8Sdrh 153373b211abSdrh if( pExpr==0 ) return 0; 1534bb4957f8Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 1535bb4957f8Sdrh { 1536bb4957f8Sdrh int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 1537bb4957f8Sdrh if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){ 1538fc976065Sdanielk1977 sqlite3ErrorMsg(pNC->pParse, 1539bb4957f8Sdrh "Expression tree is too large (maximum depth %d)", mxDepth 1540fc976065Sdanielk1977 ); 1541fc976065Sdanielk1977 return 1; 1542fc976065Sdanielk1977 } 1543fc976065Sdanielk1977 pNC->pParse->nHeight += pExpr->nHeight; 1544bb4957f8Sdrh } 1545fc976065Sdanielk1977 #endif 154613449892Sdrh savedHasAgg = pNC->hasAgg; 154713449892Sdrh pNC->hasAgg = 0; 1548b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1549bb4957f8Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 1550fc976065Sdanielk1977 pNC->pParse->nHeight -= pExpr->nHeight; 1551fc976065Sdanielk1977 #endif 1552b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 155373b211abSdrh ExprSetProperty(pExpr, EP_Error); 155473b211abSdrh } 155513449892Sdrh if( pNC->hasAgg ){ 155613449892Sdrh ExprSetProperty(pExpr, EP_Agg); 155713449892Sdrh }else if( savedHasAgg ){ 155813449892Sdrh pNC->hasAgg = 1; 155913449892Sdrh } 156073b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1561626a879aSdrh } 1562626a879aSdrh 15631398ad36Sdrh /* 15641398ad36Sdrh ** A pointer instance of this structure is used to pass information 15651398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 15661398ad36Sdrh */ 15671398ad36Sdrh typedef struct QueryCoder QueryCoder; 15681398ad36Sdrh struct QueryCoder { 15691398ad36Sdrh Parse *pParse; /* The parsing context */ 15701398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 15711398ad36Sdrh }; 15721398ad36Sdrh 15739a96b668Sdanielk1977 #ifdef SQLITE_TEST 15749a96b668Sdanielk1977 int sqlite3_enable_in_opt = 1; 15759a96b668Sdanielk1977 #else 15769a96b668Sdanielk1977 #define sqlite3_enable_in_opt 1 15779a96b668Sdanielk1977 #endif 15789a96b668Sdanielk1977 15799a96b668Sdanielk1977 /* 15809a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 15819a96b668Sdanielk1977 ** It's job is to find or create a b-tree structure that may be used 15829a96b668Sdanielk1977 ** either to test for membership of the (...) set or to iterate through 158385b623f2Sdrh ** its members, skipping duplicates. 15849a96b668Sdanielk1977 ** 15859a96b668Sdanielk1977 ** The cursor opened on the structure (database table, database index 15869a96b668Sdanielk1977 ** or ephermal table) is stored in pX->iTable before this function returns. 15879a96b668Sdanielk1977 ** The returned value indicates the structure type, as follows: 15889a96b668Sdanielk1977 ** 15899a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 15902d401ab8Sdrh ** IN_INDEX_INDEX - The cursor was opened on a database index. 15919a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 15929a96b668Sdanielk1977 ** populated epheremal table. 15939a96b668Sdanielk1977 ** 15949a96b668Sdanielk1977 ** An existing structure may only be used if the SELECT is of the simple 15959a96b668Sdanielk1977 ** form: 15969a96b668Sdanielk1977 ** 15979a96b668Sdanielk1977 ** SELECT <column> FROM <table> 15989a96b668Sdanielk1977 ** 15999a96b668Sdanielk1977 ** If the mustBeUnique parameter is false, the structure will be used 16009a96b668Sdanielk1977 ** for fast set membership tests. In this case an epheremal table must 16019a96b668Sdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 160285b623f2Sdrh ** be found with <column> as its left-most column. 16039a96b668Sdanielk1977 ** 16049a96b668Sdanielk1977 ** If mustBeUnique is true, then the structure will be used to iterate 16059a96b668Sdanielk1977 ** through the set members, skipping any duplicates. In this case an 16069a96b668Sdanielk1977 ** epheremal table must be used unless the selected <column> is guaranteed 16079a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 16089a96b668Sdanielk1977 ** is unique by virtue of a constraint or implicit index. 16099a96b668Sdanielk1977 */ 1610284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 16119a96b668Sdanielk1977 int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){ 16129a96b668Sdanielk1977 Select *p; 16139a96b668Sdanielk1977 int eType = 0; 16149a96b668Sdanielk1977 int iTab = pParse->nTab++; 16159a96b668Sdanielk1977 16169a96b668Sdanielk1977 /* The follwing if(...) expression is true if the SELECT is of the 16179a96b668Sdanielk1977 ** simple form: 16189a96b668Sdanielk1977 ** 16199a96b668Sdanielk1977 ** SELECT <column> FROM <table> 16209a96b668Sdanielk1977 ** 16219a96b668Sdanielk1977 ** If this is the case, it may be possible to use an existing table 16229a96b668Sdanielk1977 ** or index instead of generating an epheremal table. 16239a96b668Sdanielk1977 */ 16249a96b668Sdanielk1977 if( sqlite3_enable_in_opt 1625c81945e4Sdrh && (p=pX->pSelect)!=0 && !p->pPrior 16269a96b668Sdanielk1977 && !p->isDistinct && !p->isAgg && !p->pGroupBy 16279a96b668Sdanielk1977 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect 1628b2b95d41Sdanielk1977 && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect 16299a96b668Sdanielk1977 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN 16309a96b668Sdanielk1977 && !p->pLimit && !p->pOffset && !p->pWhere 16319a96b668Sdanielk1977 ){ 16329a96b668Sdanielk1977 sqlite3 *db = pParse->db; 16339a96b668Sdanielk1977 Index *pIdx; 16349a96b668Sdanielk1977 Expr *pExpr = p->pEList->a[0].pExpr; 16359a96b668Sdanielk1977 int iCol = pExpr->iColumn; 16369a96b668Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 16379a96b668Sdanielk1977 16389a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 16399a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 16409a96b668Sdanielk1977 ** successful here. 16419a96b668Sdanielk1977 */ 16429a96b668Sdanielk1977 assert(v); 16439a96b668Sdanielk1977 if( iCol<0 ){ 16440a07c107Sdrh int iMem = ++pParse->nMem; 16459a96b668Sdanielk1977 int iAddr; 16469a96b668Sdanielk1977 Table *pTab = p->pSrc->a[0].pTab; 16479a96b668Sdanielk1977 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 16489a96b668Sdanielk1977 sqlite3VdbeUsesBtree(v, iDb); 16499a96b668Sdanielk1977 1650892d3179Sdrh iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); 16514c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); 16529a96b668Sdanielk1977 16539a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 16549a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 16559a96b668Sdanielk1977 16569a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 16579a96b668Sdanielk1977 }else{ 16589a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 16599a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 16609a96b668Sdanielk1977 ** to this collation sequence. 16619a96b668Sdanielk1977 */ 16629a96b668Sdanielk1977 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); 16639a96b668Sdanielk1977 16649a96b668Sdanielk1977 /* Check that the affinity that will be used to perform the 16659a96b668Sdanielk1977 ** comparison is the same as the affinity of the column. If 16669a96b668Sdanielk1977 ** it is not, it is not possible to use any index. 16679a96b668Sdanielk1977 */ 16689a96b668Sdanielk1977 Table *pTab = p->pSrc->a[0].pTab; 16699a96b668Sdanielk1977 char aff = comparisonAffinity(pX); 16709a96b668Sdanielk1977 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); 16719a96b668Sdanielk1977 16729a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 16739a96b668Sdanielk1977 if( (pIdx->aiColumn[0]==iCol) 16749a96b668Sdanielk1977 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0)) 16759a96b668Sdanielk1977 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) 16769a96b668Sdanielk1977 ){ 16779a96b668Sdanielk1977 int iDb; 16780a07c107Sdrh int iMem = ++pParse->nMem; 16799a96b668Sdanielk1977 int iAddr; 16809a96b668Sdanielk1977 char *pKey; 16819a96b668Sdanielk1977 16829a96b668Sdanielk1977 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); 16839a96b668Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema); 16849a96b668Sdanielk1977 sqlite3VdbeUsesBtree(v, iDb); 16859a96b668Sdanielk1977 1686892d3179Sdrh iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); 16874c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); 16889a96b668Sdanielk1977 1689cd3e8f7cSdanielk1977 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn); 1690207872a4Sdanielk1977 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, 169166a5167bSdrh pKey,P4_KEYINFO_HANDOFF); 1692207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 16939a96b668Sdanielk1977 eType = IN_INDEX_INDEX; 16949a96b668Sdanielk1977 16959a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 16969a96b668Sdanielk1977 } 16979a96b668Sdanielk1977 } 16989a96b668Sdanielk1977 } 16999a96b668Sdanielk1977 } 17009a96b668Sdanielk1977 17019a96b668Sdanielk1977 if( eType==0 ){ 17029a96b668Sdanielk1977 sqlite3CodeSubselect(pParse, pX); 17039a96b668Sdanielk1977 eType = IN_INDEX_EPH; 17049a96b668Sdanielk1977 }else{ 17059a96b668Sdanielk1977 pX->iTable = iTab; 17069a96b668Sdanielk1977 } 17079a96b668Sdanielk1977 return eType; 17089a96b668Sdanielk1977 } 1709284f4acaSdanielk1977 #endif 1710626a879aSdrh 1711626a879aSdrh /* 17129cbe6352Sdrh ** Generate code for scalar subqueries used as an expression 17139cbe6352Sdrh ** and IN operators. Examples: 1714626a879aSdrh ** 17159cbe6352Sdrh ** (SELECT a FROM b) -- subquery 17169cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 17179cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 17189cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 1719fef5208cSdrh ** 17209cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 17219cbe6352Sdrh ** operator or subquery. 1722cce7d176Sdrh */ 172351522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1724b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 172557dbd7b3Sdrh int testAddr = 0; /* One-time test address */ 1726b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1727b3bce662Sdanielk1977 if( v==0 ) return; 1728b3bce662Sdanielk1977 1729fc976065Sdanielk1977 173057dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 173157dbd7b3Sdrh ** if any of the following is true: 173257dbd7b3Sdrh ** 173357dbd7b3Sdrh ** * The right-hand side is a correlated subquery 173457dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 173557dbd7b3Sdrh ** * We are inside a trigger 173657dbd7b3Sdrh ** 173757dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 173857dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1739b3bce662Sdanielk1977 */ 1740b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 17410a07c107Sdrh int mem = ++pParse->nMem; 1742892d3179Sdrh sqlite3VdbeAddOp1(v, OP_If, mem); 1743892d3179Sdrh testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); 174417435752Sdrh assert( testAddr>0 || pParse->db->mallocFailed ); 1745b3bce662Sdanielk1977 } 1746b3bce662Sdanielk1977 1747cce7d176Sdrh switch( pExpr->op ){ 1748fef5208cSdrh case TK_IN: { 1749e014a838Sdanielk1977 char affinity; 1750d3d39e93Sdrh KeyInfo keyInfo; 1751b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 1752d3d39e93Sdrh 1753bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1754e014a838Sdanielk1977 1755e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 175657dbd7b3Sdrh ** expression it is handled the same way. A virtual table is 1757e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1758e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1759fef5208cSdrh ** 1760e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1761e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1762e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1763e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1764e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1765e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1766e014a838Sdanielk1977 ** is used. 1767fef5208cSdrh */ 1768832508b7Sdrh pExpr->iTable = pParse->nTab++; 1769cd3e8f7cSdanielk1977 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1); 1770d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1771d3d39e93Sdrh keyInfo.nField = 1; 1772e014a838Sdanielk1977 1773e014a838Sdanielk1977 if( pExpr->pSelect ){ 1774e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1775e014a838Sdanielk1977 ** 1776e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1777e014a838Sdanielk1977 ** table allocated and opened above. 1778e014a838Sdanielk1977 */ 17791013c932Sdrh SelectDest dest; 1780be5c89acSdrh ExprList *pEList; 17811013c932Sdrh 17821013c932Sdrh sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 17831013c932Sdrh dest.affinity = (int)affinity; 1784e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 17856c8c8ce0Sdanielk1977 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){ 178694ccde58Sdrh return; 178794ccde58Sdrh } 1788be5c89acSdrh pEList = pExpr->pSelect->pEList; 1789be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 1790bcbb04e5Sdanielk1977 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, 1791be5c89acSdrh pEList->a[0].pExpr); 17920202b29eSdanielk1977 } 1793fef5208cSdrh }else if( pExpr->pList ){ 1794fef5208cSdrh /* Case 2: expr IN (exprlist) 1795fef5208cSdrh ** 1796e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1797e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1798e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1799e014a838Sdanielk1977 ** a column, use numeric affinity. 1800fef5208cSdrh */ 1801e014a838Sdanielk1977 int i; 180257dbd7b3Sdrh ExprList *pList = pExpr->pList; 180357dbd7b3Sdrh struct ExprList_item *pItem; 18042d401ab8Sdrh int r1, r2; 180557dbd7b3Sdrh 1806e014a838Sdanielk1977 if( !affinity ){ 18078159a35fSdrh affinity = SQLITE_AFF_NONE; 1808e014a838Sdanielk1977 } 18090202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1810e014a838Sdanielk1977 1811e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 18122d401ab8Sdrh r1 = sqlite3GetTempReg(pParse); 18132d401ab8Sdrh r2 = sqlite3GetTempReg(pParse); 181457dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 181557dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1816e014a838Sdanielk1977 181757dbd7b3Sdrh /* If the expression is not constant then we will need to 181857dbd7b3Sdrh ** disable the test that was generated above that makes sure 181957dbd7b3Sdrh ** this code only executes once. Because for a non-constant 182057dbd7b3Sdrh ** expression we need to rerun this code each time. 182157dbd7b3Sdrh */ 1822892d3179Sdrh if( testAddr && !sqlite3ExprIsConstant(pE2) ){ 1823892d3179Sdrh sqlite3VdbeChangeToNoop(v, testAddr-1, 2); 182457dbd7b3Sdrh testAddr = 0; 18254794b980Sdrh } 1826e014a838Sdanielk1977 1827e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 1828e55cbd72Sdrh pParse->disableColCache++; 18292d401ab8Sdrh sqlite3ExprCode(pParse, pE2, r1); 1830c5499befSdrh assert( pParse->disableColCache>0 ); 1831e55cbd72Sdrh pParse->disableColCache--; 18321db639ceSdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); 1833da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, r1, 1); 18342d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); 1835fef5208cSdrh } 18362d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r1); 18372d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r2); 1838fef5208cSdrh } 183966a5167bSdrh sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); 1840b3bce662Sdanielk1977 break; 1841fef5208cSdrh } 1842fef5208cSdrh 184351522cd3Sdrh case TK_EXISTS: 184419a775c2Sdrh case TK_SELECT: { 1845fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1846fef5208cSdrh ** value of this select in a memory cell and record the number 1847967e8b73Sdrh ** of the memory cell in iColumn. 1848fef5208cSdrh */ 18492646da7eSdrh static const Token one = { (u8*)"1", 0, 1 }; 185051522cd3Sdrh Select *pSel; 18516c8c8ce0Sdanielk1977 SelectDest dest; 18521398ad36Sdrh 185351522cd3Sdrh pSel = pExpr->pSelect; 18541013c932Sdrh sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); 185551522cd3Sdrh if( pExpr->op==TK_SELECT ){ 18566c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 18574c583128Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); 1858d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 185951522cd3Sdrh }else{ 18606c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 18614c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); 1862d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 186351522cd3Sdrh } 1864ec7429aeSdrh sqlite3ExprDelete(pSel->pLimit); 1865a1644fd8Sdanielk1977 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); 18666c8c8ce0Sdanielk1977 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){ 186794ccde58Sdrh return; 186894ccde58Sdrh } 18696c8c8ce0Sdanielk1977 pExpr->iColumn = dest.iParm; 1870b3bce662Sdanielk1977 break; 187119a775c2Sdrh } 1872cce7d176Sdrh } 1873b3bce662Sdanielk1977 187457dbd7b3Sdrh if( testAddr ){ 1875892d3179Sdrh sqlite3VdbeJumpHere(v, testAddr-1); 1876b3bce662Sdanielk1977 } 1877fc976065Sdanielk1977 1878b3bce662Sdanielk1977 return; 1879cce7d176Sdrh } 188051522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1881cce7d176Sdrh 1882cce7d176Sdrh /* 1883598f1340Sdrh ** Duplicate an 8-byte value 1884598f1340Sdrh */ 1885598f1340Sdrh static char *dup8bytes(Vdbe *v, const char *in){ 1886598f1340Sdrh char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); 1887598f1340Sdrh if( out ){ 1888598f1340Sdrh memcpy(out, in, 8); 1889598f1340Sdrh } 1890598f1340Sdrh return out; 1891598f1340Sdrh } 1892598f1340Sdrh 1893598f1340Sdrh /* 1894598f1340Sdrh ** Generate an instruction that will put the floating point 18959cbf3425Sdrh ** value described by z[0..n-1] into register iMem. 18960cf19ed8Sdrh ** 18970cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 18980cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 18990cf19ed8Sdrh ** like the continuation of the number. 1900598f1340Sdrh */ 19019de221dfSdrh static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ 1902598f1340Sdrh assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); 1903598f1340Sdrh if( z ){ 1904598f1340Sdrh double value; 1905598f1340Sdrh char *zV; 19060cf19ed8Sdrh assert( !isdigit(z[n]) ); 1907598f1340Sdrh sqlite3AtoF(z, &value); 1908598f1340Sdrh if( negateFlag ) value = -value; 1909598f1340Sdrh zV = dup8bytes(v, (char*)&value); 19109de221dfSdrh sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); 1911598f1340Sdrh } 1912598f1340Sdrh } 1913598f1340Sdrh 1914598f1340Sdrh 1915598f1340Sdrh /* 1916fec19aadSdrh ** Generate an instruction that will put the integer describe by 19179cbf3425Sdrh ** text z[0..n-1] into register iMem. 19180cf19ed8Sdrh ** 19190cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 19200cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 19210cf19ed8Sdrh ** like the continuation of the number. 1922fec19aadSdrh */ 19239de221dfSdrh static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){ 1924abb6fcabSdrh assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); 1925c9cf901dSdanielk1977 if( z ){ 1926fec19aadSdrh int i; 19270cf19ed8Sdrh assert( !isdigit(z[n]) ); 19286fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 19299de221dfSdrh if( negFlag ) i = -i; 19309de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 19319de221dfSdrh }else if( sqlite3FitsIn64Bits(z, negFlag) ){ 1932598f1340Sdrh i64 value; 1933598f1340Sdrh char *zV; 1934598f1340Sdrh sqlite3Atoi64(z, &value); 19359de221dfSdrh if( negFlag ) value = -value; 1936598f1340Sdrh zV = dup8bytes(v, (char*)&value); 19379de221dfSdrh sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); 1938fec19aadSdrh }else{ 19399de221dfSdrh codeReal(v, z, n, negFlag, iMem); 1940fec19aadSdrh } 1941fec19aadSdrh } 1942c9cf901dSdanielk1977 } 1943fec19aadSdrh 1944945498f3Sdrh 1945945498f3Sdrh /* 1946945498f3Sdrh ** Generate code that will extract the iColumn-th column from 1947e55cbd72Sdrh ** table pTab and store the column value in a register. An effort 1948e55cbd72Sdrh ** is made to store the column value in register iReg, but this is 1949e55cbd72Sdrh ** not guaranteed. The location of the column value is returned. 1950e55cbd72Sdrh ** 1951e55cbd72Sdrh ** There must be an open cursor to pTab in iTable when this routine 1952e55cbd72Sdrh ** is called. If iColumn<0 then code is generated that extracts the rowid. 1953da250ea5Sdrh ** 1954da250ea5Sdrh ** This routine might attempt to reuse the value of the column that 1955da250ea5Sdrh ** has already been loaded into a register. The value will always 1956da250ea5Sdrh ** be used if it has not undergone any affinity changes. But if 1957da250ea5Sdrh ** an affinity change has occurred, then the cached value will only be 1958da250ea5Sdrh ** used if allowAffChng is true. 1959945498f3Sdrh */ 1960e55cbd72Sdrh int sqlite3ExprCodeGetColumn( 1961e55cbd72Sdrh Parse *pParse, /* Parsing and code generating context */ 19622133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 19632133d822Sdrh int iColumn, /* Index of the table column */ 19642133d822Sdrh int iTable, /* The cursor pointing to the table */ 1965da250ea5Sdrh int iReg, /* Store results here */ 1966da250ea5Sdrh int allowAffChng /* True if prior affinity changes are OK */ 19672133d822Sdrh ){ 1968e55cbd72Sdrh Vdbe *v = pParse->pVdbe; 1969e55cbd72Sdrh int i; 1970da250ea5Sdrh struct yColCache *p; 1971e55cbd72Sdrh 1972da250ea5Sdrh for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ 1973da250ea5Sdrh if( p->iTable==iTable && p->iColumn==iColumn 1974da250ea5Sdrh && (!p->affChange || allowAffChng) ){ 1975e55cbd72Sdrh #if 0 1976e55cbd72Sdrh sqlite3VdbeAddOp0(v, OP_Noop); 1977da250ea5Sdrh VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg)); 1978e55cbd72Sdrh #endif 1979da250ea5Sdrh return p->iReg; 1980e55cbd72Sdrh } 1981e55cbd72Sdrh } 1982e55cbd72Sdrh assert( v!=0 ); 1983945498f3Sdrh if( iColumn<0 ){ 1984945498f3Sdrh int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; 19852133d822Sdrh sqlite3VdbeAddOp2(v, op, iTable, iReg); 1986945498f3Sdrh }else if( pTab==0 ){ 19872133d822Sdrh sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg); 1988945498f3Sdrh }else{ 1989945498f3Sdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 19902133d822Sdrh sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); 1991945498f3Sdrh sqlite3ColumnDefault(v, pTab, iColumn); 1992945498f3Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 1993945498f3Sdrh if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ 19942133d822Sdrh sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); 1995945498f3Sdrh } 1996945498f3Sdrh #endif 1997945498f3Sdrh } 1998e55cbd72Sdrh if( pParse->disableColCache==0 ){ 1999e55cbd72Sdrh i = pParse->iColCache; 2000da250ea5Sdrh p = &pParse->aColCache[i]; 2001da250ea5Sdrh p->iTable = iTable; 2002da250ea5Sdrh p->iColumn = iColumn; 2003da250ea5Sdrh p->iReg = iReg; 2004c5499befSdrh p->affChange = 0; 2005e55cbd72Sdrh i++; 20062f7794c1Sdrh if( i>=ArraySize(pParse->aColCache) ) i = 0; 2007e55cbd72Sdrh if( i>pParse->nColCache ) pParse->nColCache = i; 20082f7794c1Sdrh pParse->iColCache = i; 2009e55cbd72Sdrh } 2010e55cbd72Sdrh return iReg; 2011e55cbd72Sdrh } 2012e55cbd72Sdrh 2013e55cbd72Sdrh /* 2014e55cbd72Sdrh ** Clear all column cache entries associated with the vdbe 2015e55cbd72Sdrh ** cursor with cursor number iTable. 2016e55cbd72Sdrh */ 2017e55cbd72Sdrh void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){ 2018e55cbd72Sdrh if( iTable<0 ){ 2019e55cbd72Sdrh pParse->nColCache = 0; 2020e55cbd72Sdrh pParse->iColCache = 0; 2021e55cbd72Sdrh }else{ 2022e55cbd72Sdrh int i; 2023e55cbd72Sdrh for(i=0; i<pParse->nColCache; i++){ 2024e55cbd72Sdrh if( pParse->aColCache[i].iTable==iTable ){ 2025c5499befSdrh testcase( i==pParse->nColCache-1 ); 2026e55cbd72Sdrh pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; 2027e55cbd72Sdrh pParse->iColCache = pParse->nColCache; 2028e55cbd72Sdrh } 2029e55cbd72Sdrh } 2030da250ea5Sdrh } 2031da250ea5Sdrh } 2032e55cbd72Sdrh 2033e55cbd72Sdrh /* 2034da250ea5Sdrh ** Record the fact that an affinity change has occurred on iCount 2035da250ea5Sdrh ** registers starting with iStart. 2036e55cbd72Sdrh */ 2037da250ea5Sdrh void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 2038da250ea5Sdrh int iEnd = iStart + iCount - 1; 2039e55cbd72Sdrh int i; 2040e55cbd72Sdrh for(i=0; i<pParse->nColCache; i++){ 2041e55cbd72Sdrh int r = pParse->aColCache[i].iReg; 2042da250ea5Sdrh if( r>=iStart && r<=iEnd ){ 2043da250ea5Sdrh pParse->aColCache[i].affChange = 1; 2044e55cbd72Sdrh } 2045e55cbd72Sdrh } 2046e55cbd72Sdrh } 2047e55cbd72Sdrh 2048e55cbd72Sdrh /* 2049e55cbd72Sdrh ** Generate code to moves content from one register to another. 2050e55cbd72Sdrh ** Keep the column cache up-to-date. 2051e55cbd72Sdrh */ 2052e55cbd72Sdrh void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo){ 2053e55cbd72Sdrh int i; 2054e55cbd72Sdrh if( iFrom==iTo ) return; 2055e55cbd72Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_Move, iFrom, iTo); 2056e55cbd72Sdrh for(i=0; i<pParse->nColCache; i++){ 2057e55cbd72Sdrh if( pParse->aColCache[i].iReg==iFrom ){ 2058e55cbd72Sdrh pParse->aColCache[i].iReg = iTo; 2059e55cbd72Sdrh } 2060e55cbd72Sdrh } 2061945498f3Sdrh } 2062945498f3Sdrh 2063fec19aadSdrh /* 2064652fbf55Sdrh ** Return true if any register in the range iFrom..iTo (inclusive) 2065652fbf55Sdrh ** is used as part of the column cache. 2066652fbf55Sdrh */ 2067652fbf55Sdrh static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 2068652fbf55Sdrh int i; 2069652fbf55Sdrh for(i=0; i<pParse->nColCache; i++){ 2070652fbf55Sdrh int r = pParse->aColCache[i].iReg; 2071652fbf55Sdrh if( r>=iFrom && r<=iTo ) return 1; 2072652fbf55Sdrh } 2073652fbf55Sdrh return 0; 2074652fbf55Sdrh } 2075652fbf55Sdrh 2076652fbf55Sdrh /* 2077652fbf55Sdrh ** Theres is a value in register iCurrent. We ultimately want 2078652fbf55Sdrh ** the value to be in register iTarget. It might be that 2079652fbf55Sdrh ** iCurrent and iTarget are the same register. 2080652fbf55Sdrh ** 2081652fbf55Sdrh ** We are going to modify the value, so we need to make sure it 2082652fbf55Sdrh ** is not a cached register. If iCurrent is a cached register, 2083652fbf55Sdrh ** then try to move the value over to iTarget. If iTarget is a 2084652fbf55Sdrh ** cached register, then clear the corresponding cache line. 2085652fbf55Sdrh ** 2086652fbf55Sdrh ** Return the register that the value ends up in. 2087652fbf55Sdrh */ 2088652fbf55Sdrh int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){ 2089da250ea5Sdrh int i; 2090652fbf55Sdrh assert( pParse->pVdbe!=0 ); 2091652fbf55Sdrh if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){ 2092652fbf55Sdrh return iCurrent; 2093652fbf55Sdrh } 20942f7794c1Sdrh if( iCurrent!=iTarget ){ 2095652fbf55Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget); 20962f7794c1Sdrh } 2097da250ea5Sdrh for(i=0; i<pParse->nColCache; i++){ 2098da250ea5Sdrh if( pParse->aColCache[i].iReg==iTarget ){ 2099da250ea5Sdrh pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; 2100da250ea5Sdrh pParse->iColCache = pParse->nColCache; 2101da250ea5Sdrh } 2102da250ea5Sdrh } 2103652fbf55Sdrh return iTarget; 2104652fbf55Sdrh } 2105652fbf55Sdrh 2106652fbf55Sdrh /* 2107191b54cbSdrh ** If the last instruction coded is an ephemeral copy of any of 2108191b54cbSdrh ** the registers in the nReg registers beginning with iReg, then 2109191b54cbSdrh ** convert the last instruction from OP_SCopy to OP_Copy. 2110191b54cbSdrh */ 2111191b54cbSdrh void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){ 2112191b54cbSdrh int addr; 2113191b54cbSdrh VdbeOp *pOp; 2114191b54cbSdrh Vdbe *v; 2115191b54cbSdrh 2116191b54cbSdrh v = pParse->pVdbe; 2117191b54cbSdrh addr = sqlite3VdbeCurrentAddr(v); 2118191b54cbSdrh pOp = sqlite3VdbeGetOp(v, addr-1); 2119*d7eb2ed5Sdanielk1977 assert( pOp || pParse->db->mallocFailed ); 2120*d7eb2ed5Sdanielk1977 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){ 2121191b54cbSdrh pOp->opcode = OP_Copy; 2122191b54cbSdrh } 2123191b54cbSdrh } 2124191b54cbSdrh 2125191b54cbSdrh /* 2126cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 21272dcef11bSdrh ** expression. Attempt to store the results in register "target". 21282dcef11bSdrh ** Return the register where results are stored. 2129389a1adbSdrh ** 21302dcef11bSdrh ** With this routine, there is no guaranteed that results will 21312dcef11bSdrh ** be stored in target. The result might be stored in some other 21322dcef11bSdrh ** register if it is convenient to do so. The calling function 21332dcef11bSdrh ** must check the return code and move the results to the desired 21342dcef11bSdrh ** register. 2135cce7d176Sdrh */ 2136678ccce8Sdrh int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 21372dcef11bSdrh Vdbe *v = pParse->pVdbe; /* The VM under construction */ 21382dcef11bSdrh int op; /* The opcode being coded */ 21392dcef11bSdrh int inReg = target; /* Results stored in register inReg */ 21402dcef11bSdrh int regFree1 = 0; /* If non-zero free this temporary register */ 21412dcef11bSdrh int regFree2 = 0; /* If non-zero free this temporary register */ 2142678ccce8Sdrh int r1, r2, r3, r4; /* Various register numbers */ 2143ffe07b2dSdrh 2144389a1adbSdrh assert( v!=0 || pParse->db->mallocFailed ); 21459cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 2146389a1adbSdrh if( v==0 ) return 0; 2147389a1adbSdrh 2148389a1adbSdrh if( pExpr==0 ){ 2149389a1adbSdrh op = TK_NULL; 2150389a1adbSdrh }else{ 2151f2bc013cSdrh op = pExpr->op; 2152389a1adbSdrh } 2153f2bc013cSdrh switch( op ){ 215413449892Sdrh case TK_AGG_COLUMN: { 215513449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 215613449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 215713449892Sdrh if( !pAggInfo->directMode ){ 21589de221dfSdrh assert( pCol->iMem>0 ); 21599de221dfSdrh inReg = pCol->iMem; 216013449892Sdrh break; 216113449892Sdrh }else if( pAggInfo->useSortingIdx ){ 2162389a1adbSdrh sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, 2163389a1adbSdrh pCol->iSorterColumn, target); 216413449892Sdrh break; 216513449892Sdrh } 216613449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 216713449892Sdrh } 2168967e8b73Sdrh case TK_COLUMN: { 2169ffe07b2dSdrh if( pExpr->iTable<0 ){ 2170ffe07b2dSdrh /* This only happens when coding check constraints */ 2171aa9b8963Sdrh assert( pParse->ckBase>0 ); 2172aa9b8963Sdrh inReg = pExpr->iColumn + pParse->ckBase; 2173c4a3c779Sdrh }else{ 2174c5499befSdrh testcase( (pExpr->flags & EP_AnyAff)!=0 ); 2175e55cbd72Sdrh inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 2176da250ea5Sdrh pExpr->iColumn, pExpr->iTable, target, 2177da250ea5Sdrh pExpr->flags & EP_AnyAff); 21782282792aSdrh } 2179cce7d176Sdrh break; 2180cce7d176Sdrh } 2181cce7d176Sdrh case TK_INTEGER: { 21829de221dfSdrh codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); 2183fec19aadSdrh break; 218451e9a445Sdrh } 2185598f1340Sdrh case TK_FLOAT: { 21869de221dfSdrh codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); 2187598f1340Sdrh break; 2188598f1340Sdrh } 2189fec19aadSdrh case TK_STRING: { 21901e536953Sdanielk1977 sqlite3DequoteExpr(pParse->db, pExpr); 21919de221dfSdrh sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0, 219266a5167bSdrh (char*)pExpr->token.z, pExpr->token.n); 2193cce7d176Sdrh break; 2194cce7d176Sdrh } 2195f0863fe5Sdrh case TK_NULL: { 21969de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 2197f0863fe5Sdrh break; 2198f0863fe5Sdrh } 21995338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 2200c572ef7fSdanielk1977 case TK_BLOB: { 22016c8c6cecSdrh int n; 22026c8c6cecSdrh const char *z; 2203ca48c90fSdrh char *zBlob; 2204ca48c90fSdrh assert( pExpr->token.n>=3 ); 2205ca48c90fSdrh assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); 2206ca48c90fSdrh assert( pExpr->token.z[1]=='\'' ); 2207ca48c90fSdrh assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); 22086c8c6cecSdrh n = pExpr->token.n - 3; 22092646da7eSdrh z = (char*)pExpr->token.z + 2; 2210ca48c90fSdrh zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 2211ca48c90fSdrh sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 2212c572ef7fSdanielk1977 break; 2213c572ef7fSdanielk1977 } 22145338a5f7Sdanielk1977 #endif 221550457896Sdrh case TK_VARIABLE: { 22169de221dfSdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target); 2217895d7472Sdrh if( pExpr->token.n>1 ){ 221866a5167bSdrh sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n); 2219895d7472Sdrh } 222050457896Sdrh break; 222150457896Sdrh } 22224e0cff60Sdrh case TK_REGISTER: { 22239de221dfSdrh inReg = pExpr->iTable; 22244e0cff60Sdrh break; 22254e0cff60Sdrh } 2226487e262fSdrh #ifndef SQLITE_OMIT_CAST 2227487e262fSdrh case TK_CAST: { 2228487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 2229f0113000Sdanielk1977 int aff, to_op; 22302dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 22318a51256cSdrh aff = sqlite3AffinityType(&pExpr->token); 2232f0113000Sdanielk1977 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; 2233f0113000Sdanielk1977 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); 2234f0113000Sdanielk1977 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); 2235f0113000Sdanielk1977 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); 2236f0113000Sdanielk1977 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); 2237f0113000Sdanielk1977 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); 2238c5499befSdrh testcase( to_op==OP_ToText ); 2239c5499befSdrh testcase( to_op==OP_ToBlob ); 2240c5499befSdrh testcase( to_op==OP_ToNumeric ); 2241c5499befSdrh testcase( to_op==OP_ToInt ); 2242c5499befSdrh testcase( to_op==OP_ToReal ); 22432dcef11bSdrh sqlite3VdbeAddOp1(v, to_op, inReg); 2244c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 2245b3843a82Sdrh sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 2246487e262fSdrh break; 2247487e262fSdrh } 2248487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 2249c9b84a1fSdrh case TK_LT: 2250c9b84a1fSdrh case TK_LE: 2251c9b84a1fSdrh case TK_GT: 2252c9b84a1fSdrh case TK_GE: 2253c9b84a1fSdrh case TK_NE: 2254c9b84a1fSdrh case TK_EQ: { 2255f2bc013cSdrh assert( TK_LT==OP_Lt ); 2256f2bc013cSdrh assert( TK_LE==OP_Le ); 2257f2bc013cSdrh assert( TK_GT==OP_Gt ); 2258f2bc013cSdrh assert( TK_GE==OP_Ge ); 2259f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2260f2bc013cSdrh assert( TK_NE==OP_Ne ); 2261c5499befSdrh testcase( op==TK_LT ); 2262c5499befSdrh testcase( op==TK_LE ); 2263c5499befSdrh testcase( op==TK_GT ); 2264c5499befSdrh testcase( op==TK_GE ); 2265c5499befSdrh testcase( op==TK_EQ ); 2266c5499befSdrh testcase( op==TK_NE ); 2267da250ea5Sdrh codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, 2268da250ea5Sdrh pExpr->pRight, &r2, ®Free2); 226935573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 227035573356Sdrh r1, r2, inReg, SQLITE_STOREP2); 2271c5499befSdrh testcase( regFree1==0 ); 2272c5499befSdrh testcase( regFree2==0 ); 2273a37cdde0Sdanielk1977 break; 2274c9b84a1fSdrh } 2275cce7d176Sdrh case TK_AND: 2276cce7d176Sdrh case TK_OR: 2277cce7d176Sdrh case TK_PLUS: 2278cce7d176Sdrh case TK_STAR: 2279cce7d176Sdrh case TK_MINUS: 2280bf4133cbSdrh case TK_REM: 2281bf4133cbSdrh case TK_BITAND: 2282bf4133cbSdrh case TK_BITOR: 228317c40294Sdrh case TK_SLASH: 2284bf4133cbSdrh case TK_LSHIFT: 2285855eb1cfSdrh case TK_RSHIFT: 22860040077dSdrh case TK_CONCAT: { 2287f2bc013cSdrh assert( TK_AND==OP_And ); 2288f2bc013cSdrh assert( TK_OR==OP_Or ); 2289f2bc013cSdrh assert( TK_PLUS==OP_Add ); 2290f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 2291f2bc013cSdrh assert( TK_REM==OP_Remainder ); 2292f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 2293f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 2294f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 2295f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 2296f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 2297f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 2298c5499befSdrh testcase( op==TK_AND ); 2299c5499befSdrh testcase( op==TK_OR ); 2300c5499befSdrh testcase( op==TK_PLUS ); 2301c5499befSdrh testcase( op==TK_MINUS ); 2302c5499befSdrh testcase( op==TK_REM ); 2303c5499befSdrh testcase( op==TK_BITAND ); 2304c5499befSdrh testcase( op==TK_BITOR ); 2305c5499befSdrh testcase( op==TK_SLASH ); 2306c5499befSdrh testcase( op==TK_LSHIFT ); 2307c5499befSdrh testcase( op==TK_RSHIFT ); 2308c5499befSdrh testcase( op==TK_CONCAT ); 23092dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 23102dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 23115b6afba9Sdrh sqlite3VdbeAddOp3(v, op, r2, r1, target); 2312c5499befSdrh testcase( regFree1==0 ); 2313c5499befSdrh testcase( regFree2==0 ); 23140040077dSdrh break; 23150040077dSdrh } 2316cce7d176Sdrh case TK_UMINUS: { 2317fec19aadSdrh Expr *pLeft = pExpr->pLeft; 2318fec19aadSdrh assert( pLeft ); 2319fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 2320fec19aadSdrh Token *p = &pLeft->token; 2321fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 23229de221dfSdrh codeReal(v, (char*)p->z, p->n, 1, target); 2323e6840900Sdrh }else{ 23249de221dfSdrh codeInteger(v, (char*)p->z, p->n, 1, target); 2325e6840900Sdrh } 23263c84ddffSdrh }else{ 23272dcef11bSdrh regFree1 = r1 = sqlite3GetTempReg(pParse); 23283c84ddffSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); 2329e55cbd72Sdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 23302dcef11bSdrh sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 2331c5499befSdrh testcase( regFree2==0 ); 23323c84ddffSdrh } 23339de221dfSdrh inReg = target; 23346e142f54Sdrh break; 23356e142f54Sdrh } 2336bf4133cbSdrh case TK_BITNOT: 23376e142f54Sdrh case TK_NOT: { 2338f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 2339f2bc013cSdrh assert( TK_NOT==OP_Not ); 2340c5499befSdrh testcase( op==TK_BITNOT ); 2341c5499befSdrh testcase( op==TK_NOT ); 23422dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 2343c5499befSdrh testcase( inReg==target ); 2344c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 2345652fbf55Sdrh inReg = sqlite3ExprWritableRegister(pParse, inReg, target); 23462dcef11bSdrh sqlite3VdbeAddOp1(v, op, inReg); 2347cce7d176Sdrh break; 2348cce7d176Sdrh } 2349cce7d176Sdrh case TK_ISNULL: 2350cce7d176Sdrh case TK_NOTNULL: { 23516a288a33Sdrh int addr; 2352f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2353f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 2354c5499befSdrh testcase( op==TK_ISNULL ); 2355c5499befSdrh testcase( op==TK_NOTNULL ); 23569de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 23572dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2358c5499befSdrh testcase( regFree1==0 ); 23592dcef11bSdrh addr = sqlite3VdbeAddOp1(v, op, r1); 23609de221dfSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); 23616a288a33Sdrh sqlite3VdbeJumpHere(v, addr); 2362a37cdde0Sdanielk1977 break; 2363f2bc013cSdrh } 23642282792aSdrh case TK_AGG_FUNCTION: { 236513449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 23667e56e711Sdrh if( pInfo==0 ){ 23677e56e711Sdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", 23687e56e711Sdrh &pExpr->span); 23697e56e711Sdrh }else{ 23709de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 23717e56e711Sdrh } 23722282792aSdrh break; 23732282792aSdrh } 2374b71090fdSdrh case TK_CONST_FUNC: 2375cce7d176Sdrh case TK_FUNCTION: { 2376cce7d176Sdrh ExprList *pList = pExpr->pList; 237789425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 23780bce8354Sdrh FuncDef *pDef; 23794b59ab5eSdrh int nId; 23804b59ab5eSdrh const char *zId; 238113449892Sdrh int constMask = 0; 2382682f68b0Sdanielk1977 int i; 238317435752Sdrh sqlite3 *db = pParse->db; 238417435752Sdrh u8 enc = ENC(db); 2385dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 238617435752Sdrh 2387c5499befSdrh testcase( op==TK_CONST_FUNC ); 2388c5499befSdrh testcase( op==TK_FUNCTION ); 23892646da7eSdrh zId = (char*)pExpr->token.z; 2390b71090fdSdrh nId = pExpr->token.n; 2391d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 23920bce8354Sdrh assert( pDef!=0 ); 2393892d3179Sdrh if( pList ){ 2394892d3179Sdrh nExpr = pList->nExpr; 23952dcef11bSdrh r1 = sqlite3GetTempRange(pParse, nExpr); 2396191b54cbSdrh sqlite3ExprCodeExprList(pParse, pList, r1, 1); 2397892d3179Sdrh }else{ 2398d847eaadSdrh nExpr = r1 = 0; 2399892d3179Sdrh } 2400b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 2401a43fa227Sdrh /* Possibly overload the function if the first argument is 2402a43fa227Sdrh ** a virtual table column. 2403a43fa227Sdrh ** 2404a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 2405a43fa227Sdrh ** second argument, not the first, as the argument to test to 2406a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 2407a43fa227Sdrh ** the left operand of infix functions (the operand we want to 2408a43fa227Sdrh ** control overloading) ends up as the second argument to the 2409a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 2410a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 2411a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 2412a43fa227Sdrh */ 24136a03a1c5Sdrh if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ 241417435752Sdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); 24156a03a1c5Sdrh }else if( nExpr>0 ){ 241617435752Sdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); 2417b7f6f68fSdrh } 2418b7f6f68fSdrh #endif 2419682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 2420d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 242113449892Sdrh constMask |= (1<<i); 2422d02eb1fdSdanielk1977 } 2423dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 2424dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 2425dc1bdc4fSdanielk1977 } 2426dc1bdc4fSdanielk1977 } 2427dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 2428dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 242966a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 2430682f68b0Sdanielk1977 } 24312dcef11bSdrh sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, 243266a5167bSdrh (char*)pDef, P4_FUNCDEF); 243398757157Sdrh sqlite3VdbeChangeP5(v, nExpr); 24342dcef11bSdrh if( nExpr ){ 24352dcef11bSdrh sqlite3ReleaseTempRange(pParse, r1, nExpr); 24362dcef11bSdrh } 2437da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, r1, nExpr); 24386ec2733bSdrh break; 24396ec2733bSdrh } 2440fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 2441fe2093d7Sdrh case TK_EXISTS: 244219a775c2Sdrh case TK_SELECT: { 2443c5499befSdrh testcase( op==TK_EXISTS ); 2444c5499befSdrh testcase( op==TK_SELECT ); 244541714d6fSdrh if( pExpr->iColumn==0 ){ 2446b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 244741714d6fSdrh } 24489de221dfSdrh inReg = pExpr->iColumn; 244919a775c2Sdrh break; 245019a775c2Sdrh } 2451fef5208cSdrh case TK_IN: { 24526a288a33Sdrh int j1, j2, j3, j4, j5; 245394a11211Sdrh char affinity; 24549a96b668Sdanielk1977 int eType; 24559a96b668Sdanielk1977 24569a96b668Sdanielk1977 eType = sqlite3FindInIndex(pParse, pExpr, 0); 2457e014a838Sdanielk1977 2458e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 2459e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 246066a5167bSdrh ** P4 of OP_MakeRecord. 2461e014a838Sdanielk1977 */ 246294a11211Sdrh affinity = comparisonAffinity(pExpr); 2463e014a838Sdanielk1977 24642dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 2465e014a838Sdanielk1977 2466e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 2467e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 2468e014a838Sdanielk1977 */ 24692dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2470c5499befSdrh testcase( regFree1==0 ); 24712dcef11bSdrh j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); 24722dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 24736a288a33Sdrh j2 = sqlite3VdbeAddOp0(v, OP_Goto); 24746a288a33Sdrh sqlite3VdbeJumpHere(v, j1); 24759a96b668Sdanielk1977 if( eType==IN_INDEX_ROWID ){ 2476678ccce8Sdrh j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, r1); 24772dcef11bSdrh j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1); 24786a288a33Sdrh j5 = sqlite3VdbeAddOp0(v, OP_Goto); 24796a288a33Sdrh sqlite3VdbeJumpHere(v, j3); 24806a288a33Sdrh sqlite3VdbeJumpHere(v, j4); 24819a96b668Sdanielk1977 }else{ 24822dcef11bSdrh r2 = regFree2 = sqlite3GetTempReg(pParse); 24831db639ceSdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); 2484da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, r1, 1); 24852dcef11bSdrh j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2); 24869a96b668Sdanielk1977 } 24872dcef11bSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); 24886a288a33Sdrh sqlite3VdbeJumpHere(v, j2); 24896a288a33Sdrh sqlite3VdbeJumpHere(v, j5); 2490fef5208cSdrh break; 2491fef5208cSdrh } 249293758c8dSdanielk1977 #endif 24932dcef11bSdrh /* 24942dcef11bSdrh ** x BETWEEN y AND z 24952dcef11bSdrh ** 24962dcef11bSdrh ** This is equivalent to 24972dcef11bSdrh ** 24982dcef11bSdrh ** x>=y AND x<=z 24992dcef11bSdrh ** 25002dcef11bSdrh ** X is stored in pExpr->pLeft. 25012dcef11bSdrh ** Y is stored in pExpr->pList->a[0].pExpr. 25022dcef11bSdrh ** Z is stored in pExpr->pList->a[1].pExpr. 25032dcef11bSdrh */ 2504fef5208cSdrh case TK_BETWEEN: { 2505be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2506be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 2507be5c89acSdrh Expr *pRight = pLItem->pExpr; 250835573356Sdrh 2509da250ea5Sdrh codeCompareOperands(pParse, pLeft, &r1, ®Free1, 2510da250ea5Sdrh pRight, &r2, ®Free2); 2511c5499befSdrh testcase( regFree1==0 ); 2512c5499befSdrh testcase( regFree2==0 ); 25132dcef11bSdrh r3 = sqlite3GetTempReg(pParse); 2514678ccce8Sdrh r4 = sqlite3GetTempReg(pParse); 251535573356Sdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 251635573356Sdrh r1, r2, r3, SQLITE_STOREP2); 2517be5c89acSdrh pLItem++; 2518be5c89acSdrh pRight = pLItem->pExpr; 25192dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 25202dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 2521c5499befSdrh testcase( regFree2==0 ); 2522678ccce8Sdrh codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); 2523678ccce8Sdrh sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); 25242dcef11bSdrh sqlite3ReleaseTempReg(pParse, r3); 2525678ccce8Sdrh sqlite3ReleaseTempReg(pParse, r4); 2526fef5208cSdrh break; 2527fef5208cSdrh } 25284f07e5fbSdrh case TK_UPLUS: { 25292dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 2530a2e00042Sdrh break; 2531a2e00042Sdrh } 25322dcef11bSdrh 25332dcef11bSdrh /* 25342dcef11bSdrh ** Form A: 25352dcef11bSdrh ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 25362dcef11bSdrh ** 25372dcef11bSdrh ** Form B: 25382dcef11bSdrh ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 25392dcef11bSdrh ** 25402dcef11bSdrh ** Form A is can be transformed into the equivalent form B as follows: 25412dcef11bSdrh ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 25422dcef11bSdrh ** WHEN x=eN THEN rN ELSE y END 25432dcef11bSdrh ** 25442dcef11bSdrh ** X (if it exists) is in pExpr->pLeft. 25452dcef11bSdrh ** Y is in pExpr->pRight. The Y is also optional. If there is no 25462dcef11bSdrh ** ELSE clause and no other term matches, then the result of the 25472dcef11bSdrh ** exprssion is NULL. 25482dcef11bSdrh ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 25492dcef11bSdrh ** 25502dcef11bSdrh ** The result of the expression is the Ri for the first matching Ei, 25512dcef11bSdrh ** or if there is no matching Ei, the ELSE term Y, or if there is 25522dcef11bSdrh ** no ELSE term, NULL. 25532dcef11bSdrh */ 255417a7f8ddSdrh case TK_CASE: { 25552dcef11bSdrh int endLabel; /* GOTO label for end of CASE stmt */ 25562dcef11bSdrh int nextCase; /* GOTO label for next WHEN clause */ 25572dcef11bSdrh int nExpr; /* 2x number of WHEN terms */ 25582dcef11bSdrh int i; /* Loop counter */ 25592dcef11bSdrh ExprList *pEList; /* List of WHEN terms */ 25602dcef11bSdrh struct ExprList_item *aListelem; /* Array of WHEN terms */ 25612dcef11bSdrh Expr opCompare; /* The X==Ei expression */ 25622dcef11bSdrh Expr cacheX; /* Cached expression X */ 25632dcef11bSdrh Expr *pX; /* The X expression */ 25642dcef11bSdrh Expr *pTest; /* X==Ei (form A) or just Ei (form B) */ 256517a7f8ddSdrh 256617a7f8ddSdrh assert(pExpr->pList); 256717a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 256817a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 2569be5c89acSdrh pEList = pExpr->pList; 2570be5c89acSdrh aListelem = pEList->a; 2571be5c89acSdrh nExpr = pEList->nExpr; 25722dcef11bSdrh endLabel = sqlite3VdbeMakeLabel(v); 25732dcef11bSdrh if( (pX = pExpr->pLeft)!=0 ){ 25742dcef11bSdrh cacheX = *pX; 2575c5499befSdrh testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER ); 25762dcef11bSdrh cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); 2577c5499befSdrh testcase( regFree1==0 ); 25782dcef11bSdrh cacheX.op = TK_REGISTER; 2579678ccce8Sdrh cacheX.iColumn = 0; 25802dcef11bSdrh opCompare.op = TK_EQ; 25812dcef11bSdrh opCompare.pLeft = &cacheX; 25822dcef11bSdrh pTest = &opCompare; 2583cce7d176Sdrh } 2584c5499befSdrh pParse->disableColCache++; 2585f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 25862dcef11bSdrh if( pX ){ 25872dcef11bSdrh opCompare.pRight = aListelem[i].pExpr; 2588f5905aa7Sdrh }else{ 25892dcef11bSdrh pTest = aListelem[i].pExpr; 259017a7f8ddSdrh } 25912dcef11bSdrh nextCase = sqlite3VdbeMakeLabel(v); 2592c5499befSdrh testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER ); 25932dcef11bSdrh sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 2594c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 2595c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); 25969de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 25972dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); 25982dcef11bSdrh sqlite3VdbeResolveLabel(v, nextCase); 2599f570f011Sdrh } 260017a7f8ddSdrh if( pExpr->pRight ){ 26019de221dfSdrh sqlite3ExprCode(pParse, pExpr->pRight, target); 260217a7f8ddSdrh }else{ 26039de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 260417a7f8ddSdrh } 26052dcef11bSdrh sqlite3VdbeResolveLabel(v, endLabel); 2606c5499befSdrh assert( pParse->disableColCache>0 ); 2607c5499befSdrh pParse->disableColCache--; 26086f34903eSdanielk1977 break; 26096f34903eSdanielk1977 } 26105338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 26116f34903eSdanielk1977 case TK_RAISE: { 26126f34903eSdanielk1977 if( !pParse->trigStack ){ 26134adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2614da93d238Sdrh "RAISE() may only be used within a trigger-program"); 2615389a1adbSdrh return 0; 26166f34903eSdanielk1977 } 2617ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 2618ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 26196f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 2620ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 26211e536953Sdanielk1977 sqlite3DequoteExpr(pParse->db, pExpr); 262266a5167bSdrh sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0, 26232646da7eSdrh (char*)pExpr->token.z, pExpr->token.n); 26246f34903eSdanielk1977 } else { 26256f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 262666a5167bSdrh sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); 262766a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 2628d4e70ebdSdrh VdbeComment((v, "raise(IGNORE)")); 26296f34903eSdanielk1977 } 2630ffe07b2dSdrh break; 263117a7f8ddSdrh } 26325338a5f7Sdanielk1977 #endif 2633ffe07b2dSdrh } 26342dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 26352dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 26362dcef11bSdrh return inReg; 26375b6afba9Sdrh } 26382dcef11bSdrh 26392dcef11bSdrh /* 26402dcef11bSdrh ** Generate code to evaluate an expression and store the results 26412dcef11bSdrh ** into a register. Return the register number where the results 26422dcef11bSdrh ** are stored. 26432dcef11bSdrh ** 26442dcef11bSdrh ** If the register is a temporary register that can be deallocated, 2645678ccce8Sdrh ** then write its number into *pReg. If the result register is not 26462dcef11bSdrh ** a temporary, then set *pReg to zero. 26472dcef11bSdrh */ 26482dcef11bSdrh int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 26492dcef11bSdrh int r1 = sqlite3GetTempReg(pParse); 26502dcef11bSdrh int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 26512dcef11bSdrh if( r2==r1 ){ 26522dcef11bSdrh *pReg = r1; 26532dcef11bSdrh }else{ 26542dcef11bSdrh sqlite3ReleaseTempReg(pParse, r1); 26552dcef11bSdrh *pReg = 0; 26562dcef11bSdrh } 26572dcef11bSdrh return r2; 26582dcef11bSdrh } 26592dcef11bSdrh 26602dcef11bSdrh /* 26612dcef11bSdrh ** Generate code that will evaluate expression pExpr and store the 26622dcef11bSdrh ** results in register target. The results are guaranteed to appear 26632dcef11bSdrh ** in register target. 26642dcef11bSdrh */ 26652dcef11bSdrh int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 26669cbf3425Sdrh int inReg; 26679cbf3425Sdrh 26689cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 26699cbf3425Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 26700e359b30Sdrh assert( pParse->pVdbe || pParse->db->mallocFailed ); 26710e359b30Sdrh if( inReg!=target && pParse->pVdbe ){ 26729cbf3425Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 267317a7f8ddSdrh } 2674389a1adbSdrh return target; 2675cce7d176Sdrh } 2676cce7d176Sdrh 2677cce7d176Sdrh /* 26782dcef11bSdrh ** Generate code that evalutes the given expression and puts the result 2679de4fcfddSdrh ** in register target. 268025303780Sdrh ** 26812dcef11bSdrh ** Also make a copy of the expression results into another "cache" register 26822dcef11bSdrh ** and modify the expression so that the next time it is evaluated, 26832dcef11bSdrh ** the result is a copy of the cache register. 26842dcef11bSdrh ** 26852dcef11bSdrh ** This routine is used for expressions that are used multiple 26862dcef11bSdrh ** times. They are evaluated once and the results of the expression 26872dcef11bSdrh ** are reused. 268825303780Sdrh */ 26892dcef11bSdrh int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 269025303780Sdrh Vdbe *v = pParse->pVdbe; 26912dcef11bSdrh int inReg; 26922dcef11bSdrh inReg = sqlite3ExprCode(pParse, pExpr, target); 2693de4fcfddSdrh assert( target>0 ); 26942dcef11bSdrh if( pExpr->op!=TK_REGISTER ){ 269525303780Sdrh int iMem; 26962dcef11bSdrh iMem = ++pParse->nMem; 26972dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); 26982dcef11bSdrh pExpr->iTable = iMem; 2699678ccce8Sdrh pExpr->iColumn = pExpr->op; 270025303780Sdrh pExpr->op = TK_REGISTER; 270125303780Sdrh } 27022dcef11bSdrh return inReg; 270325303780Sdrh } 27042dcef11bSdrh 2705678ccce8Sdrh /* 270647de955eSdrh ** Return TRUE if pExpr is an constant expression that is appropriate 270747de955eSdrh ** for factoring out of a loop. Appropriate expressions are: 270847de955eSdrh ** 270947de955eSdrh ** * Any expression that evaluates to two or more opcodes. 271047de955eSdrh ** 271147de955eSdrh ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, 271247de955eSdrh ** or OP_Variable that does not need to be placed in a 271347de955eSdrh ** specific register. 271447de955eSdrh ** 271547de955eSdrh ** There is no point in factoring out single-instruction constant 271647de955eSdrh ** expressions that need to be placed in a particular register. 271747de955eSdrh ** We could factor them out, but then we would end up adding an 271847de955eSdrh ** OP_SCopy instruction to move the value into the correct register 271947de955eSdrh ** later. We might as well just use the original instruction and 272047de955eSdrh ** avoid the OP_SCopy. 272147de955eSdrh */ 272247de955eSdrh static int isAppropriateForFactoring(Expr *p){ 272347de955eSdrh if( !sqlite3ExprIsConstantNotJoin(p) ){ 272447de955eSdrh return 0; /* Only constant expressions are appropriate for factoring */ 272547de955eSdrh } 272647de955eSdrh if( (p->flags & EP_FixedDest)==0 ){ 272747de955eSdrh return 1; /* Any constant without a fixed destination is appropriate */ 272847de955eSdrh } 272947de955eSdrh while( p->op==TK_UPLUS ) p = p->pLeft; 273047de955eSdrh switch( p->op ){ 273147de955eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 273247de955eSdrh case TK_BLOB: 273347de955eSdrh #endif 273447de955eSdrh case TK_VARIABLE: 273547de955eSdrh case TK_INTEGER: 273647de955eSdrh case TK_FLOAT: 273747de955eSdrh case TK_NULL: 273847de955eSdrh case TK_STRING: { 273947de955eSdrh testcase( p->op==TK_BLOB ); 274047de955eSdrh testcase( p->op==TK_VARIABLE ); 274147de955eSdrh testcase( p->op==TK_INTEGER ); 274247de955eSdrh testcase( p->op==TK_FLOAT ); 274347de955eSdrh testcase( p->op==TK_NULL ); 274447de955eSdrh testcase( p->op==TK_STRING ); 274547de955eSdrh /* Single-instruction constants with a fixed destination are 274647de955eSdrh ** better done in-line. If we factor them, they will just end 274747de955eSdrh ** up generating an OP_SCopy to move the value to the destination 274847de955eSdrh ** register. */ 274947de955eSdrh return 0; 275047de955eSdrh } 275147de955eSdrh case TK_UMINUS: { 275247de955eSdrh if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ 275347de955eSdrh return 0; 275447de955eSdrh } 275547de955eSdrh break; 275647de955eSdrh } 275747de955eSdrh default: { 275847de955eSdrh break; 275947de955eSdrh } 276047de955eSdrh } 276147de955eSdrh return 1; 276247de955eSdrh } 276347de955eSdrh 276447de955eSdrh /* 276547de955eSdrh ** If pExpr is a constant expression that is appropriate for 276647de955eSdrh ** factoring out of a loop, then evaluate the expression 2767678ccce8Sdrh ** into a register and convert the expression into a TK_REGISTER 2768678ccce8Sdrh ** expression. 2769678ccce8Sdrh */ 2770678ccce8Sdrh static int evalConstExpr(void *pArg, Expr *pExpr){ 2771678ccce8Sdrh Parse *pParse = (Parse*)pArg; 277247de955eSdrh switch( pExpr->op ){ 277347de955eSdrh case TK_REGISTER: { 2774678ccce8Sdrh return 1; 2775678ccce8Sdrh } 277647de955eSdrh case TK_FUNCTION: 277747de955eSdrh case TK_AGG_FUNCTION: 277847de955eSdrh case TK_CONST_FUNC: { 277947de955eSdrh /* The arguments to a function have a fixed destination. 278047de955eSdrh ** Mark them this way to avoid generated unneeded OP_SCopy 278147de955eSdrh ** instructions. 278247de955eSdrh */ 278347de955eSdrh ExprList *pList = pExpr->pList; 278447de955eSdrh if( pList ){ 278547de955eSdrh int i = pList->nExpr; 278647de955eSdrh struct ExprList_item *pItem = pList->a; 278747de955eSdrh for(; i>0; i--, pItem++){ 278847de955eSdrh if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest; 278947de955eSdrh } 279047de955eSdrh } 279147de955eSdrh break; 279247de955eSdrh } 279347de955eSdrh } 279447de955eSdrh if( isAppropriateForFactoring(pExpr) ){ 2795678ccce8Sdrh int r1 = ++pParse->nMem; 2796678ccce8Sdrh int r2; 2797678ccce8Sdrh r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 2798c5499befSdrh if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1); 2799678ccce8Sdrh pExpr->iColumn = pExpr->op; 2800678ccce8Sdrh pExpr->op = TK_REGISTER; 2801678ccce8Sdrh pExpr->iTable = r2; 2802678ccce8Sdrh return 1; 2803678ccce8Sdrh } 2804678ccce8Sdrh return 0; 2805678ccce8Sdrh } 2806678ccce8Sdrh 2807678ccce8Sdrh /* 2808678ccce8Sdrh ** Preevaluate constant subexpressions within pExpr and store the 2809678ccce8Sdrh ** results in registers. Modify pExpr so that the constant subexpresions 2810678ccce8Sdrh ** are TK_REGISTER opcodes that refer to the precomputed values. 2811678ccce8Sdrh */ 2812678ccce8Sdrh void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ 2813678ccce8Sdrh walkExprTree(pExpr, evalConstExpr, pParse); 2814678ccce8Sdrh } 2815678ccce8Sdrh 281625303780Sdrh 281725303780Sdrh /* 2818268380caSdrh ** Generate code that pushes the value of every element of the given 28199cbf3425Sdrh ** expression list into a sequence of registers beginning at target. 2820268380caSdrh ** 2821892d3179Sdrh ** Return the number of elements evaluated. 2822268380caSdrh */ 28234adee20fSdanielk1977 int sqlite3ExprCodeExprList( 2824268380caSdrh Parse *pParse, /* Parsing context */ 2825389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 2826191b54cbSdrh int target, /* Where to write results */ 2827191b54cbSdrh int doHardCopy /* Call sqlite3ExprHardCopy on each element if true */ 2828268380caSdrh ){ 2829268380caSdrh struct ExprList_item *pItem; 28309cbf3425Sdrh int i, n; 2831892d3179Sdrh assert( pList!=0 || pParse->db->mallocFailed ); 2832892d3179Sdrh if( pList==0 ){ 2833892d3179Sdrh return 0; 2834892d3179Sdrh } 28359cbf3425Sdrh assert( target>0 ); 2836268380caSdrh n = pList->nExpr; 2837191b54cbSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 2838191b54cbSdrh sqlite3ExprCode(pParse, pItem->pExpr, target+i); 2839191b54cbSdrh if( doHardCopy ) sqlite3ExprHardCopy(pParse, target, n); 2840268380caSdrh } 2841f9b596ebSdrh return n; 2842268380caSdrh } 2843268380caSdrh 2844268380caSdrh /* 2845cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 2846cce7d176Sdrh ** to the label "dest" if the expression is true but execution 2847cce7d176Sdrh ** continues straight thru if the expression is false. 2848f5905aa7Sdrh ** 2849f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 285035573356Sdrh ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 2851f2bc013cSdrh ** 2852f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 2853f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 2854f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 2855f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 2856f2bc013cSdrh ** below verify that the numbers are aligned correctly. 2857cce7d176Sdrh */ 28584adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2859cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2860cce7d176Sdrh int op = 0; 28612dcef11bSdrh int regFree1 = 0; 28622dcef11bSdrh int regFree2 = 0; 28632dcef11bSdrh int r1, r2; 28642dcef11bSdrh 286535573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 2866daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2867f2bc013cSdrh op = pExpr->op; 2868f2bc013cSdrh switch( op ){ 2869cce7d176Sdrh case TK_AND: { 28704adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 2871c5499befSdrh testcase( jumpIfNull==0 ); 2872c5499befSdrh testcase( pParse->disableColCache==0 ); 287335573356Sdrh sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 2874e55cbd72Sdrh pParse->disableColCache++; 28754adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 2876c5499befSdrh assert( pParse->disableColCache>0 ); 2877e55cbd72Sdrh pParse->disableColCache--; 28784adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2879cce7d176Sdrh break; 2880cce7d176Sdrh } 2881cce7d176Sdrh case TK_OR: { 2882c5499befSdrh testcase( jumpIfNull==0 ); 2883c5499befSdrh testcase( pParse->disableColCache==0 ); 28844adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 2885e55cbd72Sdrh pParse->disableColCache++; 28864adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 2887c5499befSdrh assert( pParse->disableColCache>0 ); 2888e55cbd72Sdrh pParse->disableColCache--; 2889cce7d176Sdrh break; 2890cce7d176Sdrh } 2891cce7d176Sdrh case TK_NOT: { 2892c5499befSdrh testcase( jumpIfNull==0 ); 28934adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 2894cce7d176Sdrh break; 2895cce7d176Sdrh } 2896cce7d176Sdrh case TK_LT: 2897cce7d176Sdrh case TK_LE: 2898cce7d176Sdrh case TK_GT: 2899cce7d176Sdrh case TK_GE: 2900cce7d176Sdrh case TK_NE: 29010ac65892Sdrh case TK_EQ: { 2902f2bc013cSdrh assert( TK_LT==OP_Lt ); 2903f2bc013cSdrh assert( TK_LE==OP_Le ); 2904f2bc013cSdrh assert( TK_GT==OP_Gt ); 2905f2bc013cSdrh assert( TK_GE==OP_Ge ); 2906f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2907f2bc013cSdrh assert( TK_NE==OP_Ne ); 2908c5499befSdrh testcase( op==TK_LT ); 2909c5499befSdrh testcase( op==TK_LE ); 2910c5499befSdrh testcase( op==TK_GT ); 2911c5499befSdrh testcase( op==TK_GE ); 2912c5499befSdrh testcase( op==TK_EQ ); 2913c5499befSdrh testcase( op==TK_NE ); 2914c5499befSdrh testcase( jumpIfNull==0 ); 2915da250ea5Sdrh codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, 2916da250ea5Sdrh pExpr->pRight, &r2, ®Free2); 291735573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 29182dcef11bSdrh r1, r2, dest, jumpIfNull); 2919c5499befSdrh testcase( regFree1==0 ); 2920c5499befSdrh testcase( regFree2==0 ); 2921cce7d176Sdrh break; 2922cce7d176Sdrh } 2923cce7d176Sdrh case TK_ISNULL: 2924cce7d176Sdrh case TK_NOTNULL: { 2925f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2926f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 2927c5499befSdrh testcase( op==TK_ISNULL ); 2928c5499befSdrh testcase( op==TK_NOTNULL ); 29292dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 29302dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 2931c5499befSdrh testcase( regFree1==0 ); 2932cce7d176Sdrh break; 2933cce7d176Sdrh } 2934fef5208cSdrh case TK_BETWEEN: { 29352dcef11bSdrh /* x BETWEEN y AND z 29360202b29eSdanielk1977 ** 29372dcef11bSdrh ** Is equivalent to 29382dcef11bSdrh ** 29392dcef11bSdrh ** x>=y AND x<=z 29402dcef11bSdrh ** 29412dcef11bSdrh ** Code it as such, taking care to do the common subexpression 29422dcef11bSdrh ** elementation of x. 29430202b29eSdanielk1977 */ 29442dcef11bSdrh Expr exprAnd; 29452dcef11bSdrh Expr compLeft; 29462dcef11bSdrh Expr compRight; 29472dcef11bSdrh Expr exprX; 29480202b29eSdanielk1977 29492dcef11bSdrh exprX = *pExpr->pLeft; 29502dcef11bSdrh exprAnd.op = TK_AND; 29512dcef11bSdrh exprAnd.pLeft = &compLeft; 29522dcef11bSdrh exprAnd.pRight = &compRight; 29532dcef11bSdrh compLeft.op = TK_GE; 29542dcef11bSdrh compLeft.pLeft = &exprX; 29552dcef11bSdrh compLeft.pRight = pExpr->pList->a[0].pExpr; 29562dcef11bSdrh compRight.op = TK_LE; 29572dcef11bSdrh compRight.pLeft = &exprX; 29582dcef11bSdrh compRight.pRight = pExpr->pList->a[1].pExpr; 29592dcef11bSdrh exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); 2960c5499befSdrh testcase( regFree1==0 ); 29612dcef11bSdrh exprX.op = TK_REGISTER; 2962c5499befSdrh testcase( jumpIfNull==0 ); 29632dcef11bSdrh sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); 2964fef5208cSdrh break; 2965fef5208cSdrh } 2966cce7d176Sdrh default: { 29672dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 29682dcef11bSdrh sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 2969c5499befSdrh testcase( regFree1==0 ); 2970c5499befSdrh testcase( jumpIfNull==0 ); 2971cce7d176Sdrh break; 2972cce7d176Sdrh } 2973cce7d176Sdrh } 29742dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 29752dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 2976cce7d176Sdrh } 2977cce7d176Sdrh 2978cce7d176Sdrh /* 297966b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 2980cce7d176Sdrh ** to the label "dest" if the expression is false but execution 2981cce7d176Sdrh ** continues straight thru if the expression is true. 2982f5905aa7Sdrh ** 2983f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 298435573356Sdrh ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 298535573356Sdrh ** is 0. 2986cce7d176Sdrh */ 29874adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2988cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2989cce7d176Sdrh int op = 0; 29902dcef11bSdrh int regFree1 = 0; 29912dcef11bSdrh int regFree2 = 0; 29922dcef11bSdrh int r1, r2; 29932dcef11bSdrh 299435573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 2995daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2996f2bc013cSdrh 2997f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 2998f2bc013cSdrh ** 2999f2bc013cSdrh ** pExpr->op op 3000f2bc013cSdrh ** --------- ---------- 3001f2bc013cSdrh ** TK_ISNULL OP_NotNull 3002f2bc013cSdrh ** TK_NOTNULL OP_IsNull 3003f2bc013cSdrh ** TK_NE OP_Eq 3004f2bc013cSdrh ** TK_EQ OP_Ne 3005f2bc013cSdrh ** TK_GT OP_Le 3006f2bc013cSdrh ** TK_LE OP_Gt 3007f2bc013cSdrh ** TK_GE OP_Lt 3008f2bc013cSdrh ** TK_LT OP_Ge 3009f2bc013cSdrh ** 3010f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 3011f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 3012f2bc013cSdrh ** can compute the mapping above using the following expression. 3013f2bc013cSdrh ** Assert()s verify that the computation is correct. 3014f2bc013cSdrh */ 3015f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 3016f2bc013cSdrh 3017f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 3018f2bc013cSdrh */ 3019f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 3020f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 3021f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 3022f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 3023f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 3024f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 3025f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 3026f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 3027f2bc013cSdrh 3028cce7d176Sdrh switch( pExpr->op ){ 3029cce7d176Sdrh case TK_AND: { 3030c5499befSdrh testcase( jumpIfNull==0 ); 3031c5499befSdrh testcase( pParse->disableColCache==0 ); 30324adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 3033e55cbd72Sdrh pParse->disableColCache++; 30344adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 3035c5499befSdrh assert( pParse->disableColCache>0 ); 3036e55cbd72Sdrh pParse->disableColCache--; 3037cce7d176Sdrh break; 3038cce7d176Sdrh } 3039cce7d176Sdrh case TK_OR: { 30404adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3041c5499befSdrh testcase( jumpIfNull==0 ); 3042c5499befSdrh testcase( pParse->disableColCache==0 ); 304335573356Sdrh sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 3044e55cbd72Sdrh pParse->disableColCache++; 30454adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 3046c5499befSdrh assert( pParse->disableColCache>0 ); 3047e55cbd72Sdrh pParse->disableColCache--; 30484adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3049cce7d176Sdrh break; 3050cce7d176Sdrh } 3051cce7d176Sdrh case TK_NOT: { 30524adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 3053cce7d176Sdrh break; 3054cce7d176Sdrh } 3055cce7d176Sdrh case TK_LT: 3056cce7d176Sdrh case TK_LE: 3057cce7d176Sdrh case TK_GT: 3058cce7d176Sdrh case TK_GE: 3059cce7d176Sdrh case TK_NE: 3060cce7d176Sdrh case TK_EQ: { 3061c5499befSdrh testcase( op==TK_LT ); 3062c5499befSdrh testcase( op==TK_LE ); 3063c5499befSdrh testcase( op==TK_GT ); 3064c5499befSdrh testcase( op==TK_GE ); 3065c5499befSdrh testcase( op==TK_EQ ); 3066c5499befSdrh testcase( op==TK_NE ); 3067c5499befSdrh testcase( jumpIfNull==0 ); 3068da250ea5Sdrh codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, 3069da250ea5Sdrh pExpr->pRight, &r2, ®Free2); 307035573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 30712dcef11bSdrh r1, r2, dest, jumpIfNull); 3072c5499befSdrh testcase( regFree1==0 ); 3073c5499befSdrh testcase( regFree2==0 ); 3074cce7d176Sdrh break; 3075cce7d176Sdrh } 3076cce7d176Sdrh case TK_ISNULL: 3077cce7d176Sdrh case TK_NOTNULL: { 3078c5499befSdrh testcase( op==TK_ISNULL ); 3079c5499befSdrh testcase( op==TK_NOTNULL ); 30802dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 30812dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 3082c5499befSdrh testcase( regFree1==0 ); 3083cce7d176Sdrh break; 3084cce7d176Sdrh } 3085fef5208cSdrh case TK_BETWEEN: { 30862dcef11bSdrh /* x BETWEEN y AND z 30870202b29eSdanielk1977 ** 30882dcef11bSdrh ** Is equivalent to 30892dcef11bSdrh ** 30902dcef11bSdrh ** x>=y AND x<=z 30912dcef11bSdrh ** 30922dcef11bSdrh ** Code it as such, taking care to do the common subexpression 30932dcef11bSdrh ** elementation of x. 30940202b29eSdanielk1977 */ 30952dcef11bSdrh Expr exprAnd; 30962dcef11bSdrh Expr compLeft; 30972dcef11bSdrh Expr compRight; 30982dcef11bSdrh Expr exprX; 3099be5c89acSdrh 31002dcef11bSdrh exprX = *pExpr->pLeft; 31012dcef11bSdrh exprAnd.op = TK_AND; 31022dcef11bSdrh exprAnd.pLeft = &compLeft; 31032dcef11bSdrh exprAnd.pRight = &compRight; 31042dcef11bSdrh compLeft.op = TK_GE; 31052dcef11bSdrh compLeft.pLeft = &exprX; 31062dcef11bSdrh compLeft.pRight = pExpr->pList->a[0].pExpr; 31072dcef11bSdrh compRight.op = TK_LE; 31082dcef11bSdrh compRight.pLeft = &exprX; 31092dcef11bSdrh compRight.pRight = pExpr->pList->a[1].pExpr; 31102dcef11bSdrh exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); 3111c5499befSdrh testcase( regFree1==0 ); 31122dcef11bSdrh exprX.op = TK_REGISTER; 3113c5499befSdrh testcase( jumpIfNull==0 ); 31142dcef11bSdrh sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); 3115fef5208cSdrh break; 3116fef5208cSdrh } 3117cce7d176Sdrh default: { 31182dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 31192dcef11bSdrh sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 3120c5499befSdrh testcase( regFree1==0 ); 3121c5499befSdrh testcase( jumpIfNull==0 ); 3122cce7d176Sdrh break; 3123cce7d176Sdrh } 3124cce7d176Sdrh } 31252dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 31262dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 3127cce7d176Sdrh } 31282282792aSdrh 31292282792aSdrh /* 31302282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 31312282792aSdrh ** if they are identical and return FALSE if they differ in any way. 3132d40aab0eSdrh ** 3133d40aab0eSdrh ** Sometimes this routine will return FALSE even if the two expressions 3134d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 3135d40aab0eSdrh ** identical, we return FALSE just to be safe. So if this routine 3136d40aab0eSdrh ** returns false, then you do not really know for certain if the two 3137d40aab0eSdrh ** expressions are the same. But if you get a TRUE return, then you 3138d40aab0eSdrh ** can be sure the expressions are the same. In the places where 3139d40aab0eSdrh ** this routine is used, it does not hurt to get an extra FALSE - that 3140d40aab0eSdrh ** just might result in some slightly slower code. But returning 3141d40aab0eSdrh ** an incorrect TRUE could lead to a malfunction. 31422282792aSdrh */ 31434adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 31442282792aSdrh int i; 31454b202ae2Sdanielk1977 if( pA==0||pB==0 ){ 31464b202ae2Sdanielk1977 return pB==pA; 31472282792aSdrh } 31482282792aSdrh if( pA->op!=pB->op ) return 0; 3149fd357974Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; 31504adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 31514adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 31522282792aSdrh if( pA->pList ){ 31532282792aSdrh if( pB->pList==0 ) return 0; 31542282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 31552282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 31564adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 31572282792aSdrh return 0; 31582282792aSdrh } 31592282792aSdrh } 31602282792aSdrh }else if( pB->pList ){ 31612282792aSdrh return 0; 31622282792aSdrh } 31632282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 31642f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 3165dd73521bSdrh if( pA->op!=TK_COLUMN && pA->token.z ){ 31662282792aSdrh if( pB->token.z==0 ) return 0; 31676977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 31682646da7eSdrh if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ 31692646da7eSdrh return 0; 31702646da7eSdrh } 31712282792aSdrh } 31722282792aSdrh return 1; 31732282792aSdrh } 31742282792aSdrh 317513449892Sdrh 31762282792aSdrh /* 317713449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 317813449892Sdrh ** the new element. Return a negative number if malloc fails. 31792282792aSdrh */ 318017435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 318113449892Sdrh int i; 3182cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 318317435752Sdrh db, 3184cf643729Sdrh pInfo->aCol, 3185cf643729Sdrh sizeof(pInfo->aCol[0]), 3186cf643729Sdrh 3, 3187cf643729Sdrh &pInfo->nColumn, 3188cf643729Sdrh &pInfo->nColumnAlloc, 3189cf643729Sdrh &i 3190cf643729Sdrh ); 319113449892Sdrh return i; 31922282792aSdrh } 319313449892Sdrh 319413449892Sdrh /* 319513449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 319613449892Sdrh ** the new element. Return a negative number if malloc fails. 319713449892Sdrh */ 319817435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 319913449892Sdrh int i; 3200cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 320117435752Sdrh db, 3202cf643729Sdrh pInfo->aFunc, 3203cf643729Sdrh sizeof(pInfo->aFunc[0]), 3204cf643729Sdrh 3, 3205cf643729Sdrh &pInfo->nFunc, 3206cf643729Sdrh &pInfo->nFuncAlloc, 3207cf643729Sdrh &i 3208cf643729Sdrh ); 320913449892Sdrh return i; 32102282792aSdrh } 32112282792aSdrh 32122282792aSdrh /* 3213626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 3214626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 3215626a879aSdrh ** for additional information. 32162282792aSdrh ** 3217626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 32182282792aSdrh */ 3219626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 32202282792aSdrh int i; 3221a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 3222a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 3223a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 322413449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 322513449892Sdrh 32262282792aSdrh switch( pExpr->op ){ 322789c69d00Sdrh case TK_AGG_COLUMN: 3228967e8b73Sdrh case TK_COLUMN: { 322913449892Sdrh /* Check to see if the column is in one of the tables in the FROM 323013449892Sdrh ** clause of the aggregate query */ 323113449892Sdrh if( pSrcList ){ 323213449892Sdrh struct SrcList_item *pItem = pSrcList->a; 323313449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 323413449892Sdrh struct AggInfo_col *pCol; 323513449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 323613449892Sdrh /* If we reach this point, it means that pExpr refers to a table 323713449892Sdrh ** that is in the FROM clause of the aggregate query. 323813449892Sdrh ** 323913449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 324013449892Sdrh ** is not an entry there already. 324113449892Sdrh */ 32427f906d63Sdrh int k; 324313449892Sdrh pCol = pAggInfo->aCol; 32447f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 324513449892Sdrh if( pCol->iTable==pExpr->iTable && 324613449892Sdrh pCol->iColumn==pExpr->iColumn ){ 32472282792aSdrh break; 32482282792aSdrh } 32492282792aSdrh } 32501e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 32511e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 32521e536953Sdanielk1977 ){ 32537f906d63Sdrh pCol = &pAggInfo->aCol[k]; 32540817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 325513449892Sdrh pCol->iTable = pExpr->iTable; 325613449892Sdrh pCol->iColumn = pExpr->iColumn; 32570a07c107Sdrh pCol->iMem = ++pParse->nMem; 325813449892Sdrh pCol->iSorterColumn = -1; 32595774b806Sdrh pCol->pExpr = pExpr; 326013449892Sdrh if( pAggInfo->pGroupBy ){ 326113449892Sdrh int j, n; 326213449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 326313449892Sdrh struct ExprList_item *pTerm = pGB->a; 326413449892Sdrh n = pGB->nExpr; 326513449892Sdrh for(j=0; j<n; j++, pTerm++){ 326613449892Sdrh Expr *pE = pTerm->pExpr; 326713449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 326813449892Sdrh pE->iColumn==pExpr->iColumn ){ 326913449892Sdrh pCol->iSorterColumn = j; 327013449892Sdrh break; 32712282792aSdrh } 327213449892Sdrh } 327313449892Sdrh } 327413449892Sdrh if( pCol->iSorterColumn<0 ){ 327513449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 327613449892Sdrh } 327713449892Sdrh } 327813449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 327913449892Sdrh ** because it was there before or because we just created it). 328013449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 328113449892Sdrh ** pAggInfo->aCol[] entry. 328213449892Sdrh */ 328313449892Sdrh pExpr->pAggInfo = pAggInfo; 328413449892Sdrh pExpr->op = TK_AGG_COLUMN; 32857f906d63Sdrh pExpr->iAgg = k; 328613449892Sdrh break; 328713449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 328813449892Sdrh } /* end loop over pSrcList */ 3289a58fdfb1Sdanielk1977 } 3290626a879aSdrh return 1; 32912282792aSdrh } 32922282792aSdrh case TK_AGG_FUNCTION: { 329313449892Sdrh /* The pNC->nDepth==0 test causes aggregate functions in subqueries 329413449892Sdrh ** to be ignored */ 3295a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 329613449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 329713449892Sdrh ** function that is already in the pAggInfo structure 329813449892Sdrh */ 329913449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 330013449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 330113449892Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ 33022282792aSdrh break; 33032282792aSdrh } 33042282792aSdrh } 330513449892Sdrh if( i>=pAggInfo->nFunc ){ 330613449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 330713449892Sdrh */ 330814db2665Sdanielk1977 u8 enc = ENC(pParse->db); 33091e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 331013449892Sdrh if( i>=0 ){ 331113449892Sdrh pItem = &pAggInfo->aFunc[i]; 331213449892Sdrh pItem->pExpr = pExpr; 33130a07c107Sdrh pItem->iMem = ++pParse->nMem; 331413449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 33152646da7eSdrh (char*)pExpr->token.z, pExpr->token.n, 3316d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 3317fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 3318fd357974Sdrh pItem->iDistinct = pParse->nTab++; 3319fd357974Sdrh }else{ 3320fd357974Sdrh pItem->iDistinct = -1; 3321fd357974Sdrh } 33222282792aSdrh } 332313449892Sdrh } 332413449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 332513449892Sdrh */ 33262282792aSdrh pExpr->iAgg = i; 332713449892Sdrh pExpr->pAggInfo = pAggInfo; 3328626a879aSdrh return 1; 33292282792aSdrh } 33302282792aSdrh } 3331a58fdfb1Sdanielk1977 } 333213449892Sdrh 333313449892Sdrh /* Recursively walk subqueries looking for TK_COLUMN nodes that need 333413449892Sdrh ** to be changed to TK_AGG_COLUMN. But increment nDepth so that 333513449892Sdrh ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. 333613449892Sdrh */ 3337a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 3338a58fdfb1Sdanielk1977 pNC->nDepth++; 3339a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 3340a58fdfb1Sdanielk1977 pNC->nDepth--; 3341a58fdfb1Sdanielk1977 } 3342626a879aSdrh return 0; 33432282792aSdrh } 3344626a879aSdrh 3345626a879aSdrh /* 3346626a879aSdrh ** Analyze the given expression looking for aggregate functions and 3347626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 3348626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 3349626a879aSdrh ** 3350626a879aSdrh ** This routine should only be called after the expression has been 3351626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 3352626a879aSdrh */ 3353d2b3e23bSdrh void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 3354a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 33552282792aSdrh } 33565d9a4af9Sdrh 33575d9a4af9Sdrh /* 33585d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 33595d9a4af9Sdrh ** expression list. Return the number of errors. 33605d9a4af9Sdrh ** 33615d9a4af9Sdrh ** If an error is found, the analysis is cut short. 33625d9a4af9Sdrh */ 3363d2b3e23bSdrh void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 33645d9a4af9Sdrh struct ExprList_item *pItem; 33655d9a4af9Sdrh int i; 33665d9a4af9Sdrh if( pList ){ 3367d2b3e23bSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 3368d2b3e23bSdrh sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 33695d9a4af9Sdrh } 33705d9a4af9Sdrh } 33715d9a4af9Sdrh } 3372892d3179Sdrh 3373892d3179Sdrh /* 3374892d3179Sdrh ** Allocate or deallocate temporary use registers during code generation. 3375892d3179Sdrh */ 3376892d3179Sdrh int sqlite3GetTempReg(Parse *pParse){ 3377e55cbd72Sdrh int i, r; 3378e55cbd72Sdrh if( pParse->nTempReg==0 ){ 3379892d3179Sdrh return ++pParse->nMem; 3380892d3179Sdrh } 3381e55cbd72Sdrh for(i=0; i<pParse->nTempReg; i++){ 3382e55cbd72Sdrh r = pParse->aTempReg[i]; 3383e55cbd72Sdrh if( usedAsColumnCache(pParse, r, r) ) continue; 3384e55cbd72Sdrh } 3385e55cbd72Sdrh if( i>=pParse->nTempReg ){ 3386e55cbd72Sdrh return ++pParse->nMem; 3387e55cbd72Sdrh } 3388e55cbd72Sdrh while( i<pParse->nTempReg-1 ){ 3389e55cbd72Sdrh pParse->aTempReg[i] = pParse->aTempReg[i+1]; 3390e55cbd72Sdrh } 3391e55cbd72Sdrh pParse->nTempReg--; 3392e55cbd72Sdrh return r; 3393892d3179Sdrh } 3394892d3179Sdrh void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 33952dcef11bSdrh if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 3396892d3179Sdrh pParse->aTempReg[pParse->nTempReg++] = iReg; 3397892d3179Sdrh } 3398892d3179Sdrh } 3399892d3179Sdrh 3400892d3179Sdrh /* 3401892d3179Sdrh ** Allocate or deallocate a block of nReg consecutive registers 3402892d3179Sdrh */ 3403892d3179Sdrh int sqlite3GetTempRange(Parse *pParse, int nReg){ 3404e55cbd72Sdrh int i, n; 3405892d3179Sdrh i = pParse->iRangeReg; 3406e55cbd72Sdrh n = pParse->nRangeReg; 3407e55cbd72Sdrh if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){ 3408892d3179Sdrh pParse->iRangeReg += nReg; 3409892d3179Sdrh pParse->nRangeReg -= nReg; 3410892d3179Sdrh }else{ 3411892d3179Sdrh i = pParse->nMem+1; 3412892d3179Sdrh pParse->nMem += nReg; 3413892d3179Sdrh } 3414892d3179Sdrh return i; 3415892d3179Sdrh } 3416892d3179Sdrh void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 3417892d3179Sdrh if( nReg>pParse->nRangeReg ){ 3418892d3179Sdrh pParse->nRangeReg = nReg; 3419892d3179Sdrh pParse->iRangeReg = iReg; 3420892d3179Sdrh } 3421892d3179Sdrh } 3422