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*c5499befSdrh ** $Id: expr.c,v 1.364 2008/04/01 15:06:34 drh 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; 269a1644fd8Sdanielk1977 pNew = sqlite3DbMallocZero(db, sizeof(Expr)); 270a76b5dfcSdrh if( pNew==0 ){ 271d5d56523Sdanielk1977 /* When malloc fails, delete pLeft and pRight. Expressions passed to 272d5d56523Sdanielk1977 ** this function must always be allocated with sqlite3Expr() for this 273d5d56523Sdanielk1977 ** reason. 274d5d56523Sdanielk1977 */ 275d5d56523Sdanielk1977 sqlite3ExprDelete(pLeft); 276d5d56523Sdanielk1977 sqlite3ExprDelete(pRight); 277a76b5dfcSdrh return 0; 278a76b5dfcSdrh } 279a76b5dfcSdrh pNew->op = op; 280a76b5dfcSdrh pNew->pLeft = pLeft; 281a76b5dfcSdrh pNew->pRight = pRight; 282a58fdfb1Sdanielk1977 pNew->iAgg = -1; 283a76b5dfcSdrh if( pToken ){ 2844b59ab5eSdrh assert( pToken->dyn==0 ); 285145716b3Sdrh pNew->span = pNew->token = *pToken; 286a34001c9Sdrh }else if( pLeft ){ 287a34001c9Sdrh if( pRight ){ 2884adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 2895ffb3ac8Sdrh if( pRight->flags & EP_ExpCollate ){ 290a34001c9Sdrh pNew->flags |= EP_ExpCollate; 291a34001c9Sdrh pNew->pColl = pRight->pColl; 292a34001c9Sdrh } 293a34001c9Sdrh } 2945ffb3ac8Sdrh if( pLeft->flags & EP_ExpCollate ){ 295a34001c9Sdrh pNew->flags |= EP_ExpCollate; 296a34001c9Sdrh pNew->pColl = pLeft->pColl; 297a34001c9Sdrh } 298a76b5dfcSdrh } 299fc976065Sdanielk1977 300fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 301a76b5dfcSdrh return pNew; 302a76b5dfcSdrh } 303a76b5dfcSdrh 304a76b5dfcSdrh /* 30517435752Sdrh ** Works like sqlite3Expr() except that it takes an extra Parse* 30617435752Sdrh ** argument and notifies the associated connection object if malloc fails. 307206f3d96Sdrh */ 30817435752Sdrh Expr *sqlite3PExpr( 30917435752Sdrh Parse *pParse, /* Parsing context */ 31017435752Sdrh int op, /* Expression opcode */ 31117435752Sdrh Expr *pLeft, /* Left operand */ 31217435752Sdrh Expr *pRight, /* Right operand */ 31317435752Sdrh const Token *pToken /* Argument token */ 31417435752Sdrh ){ 315a1644fd8Sdanielk1977 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); 316206f3d96Sdrh } 317206f3d96Sdrh 318206f3d96Sdrh /* 3194e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 320b7654111Sdrh ** that look like this: #1 #2 ... These terms refer to registers 321b7654111Sdrh ** in the virtual machine. #N is the N-th register. 3224e0cff60Sdrh ** 3234e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 3244e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 3254e0cff60Sdrh ** The returns an expression that will code to extract the value from 3264e0cff60Sdrh ** that memory location as needed. 3274e0cff60Sdrh */ 3284e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 3294e0cff60Sdrh Vdbe *v = pParse->pVdbe; 3304e0cff60Sdrh Expr *p; 3314e0cff60Sdrh if( pParse->nested==0 ){ 3324e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 333a1644fd8Sdanielk1977 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); 3344e0cff60Sdrh } 335bb7ac00bSdrh if( v==0 ) return 0; 336a1644fd8Sdanielk1977 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); 33773c42a13Sdrh if( p==0 ){ 33873c42a13Sdrh return 0; /* Malloc failed */ 33973c42a13Sdrh } 340b7654111Sdrh p->iTable = atoi((char*)&pToken->z[1]); 3414e0cff60Sdrh return p; 3424e0cff60Sdrh } 3434e0cff60Sdrh 3444e0cff60Sdrh /* 34591bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 34691bb0eedSdrh ** NULL, then just return the other expression. 34791bb0eedSdrh */ 3481e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 34991bb0eedSdrh if( pLeft==0 ){ 35091bb0eedSdrh return pRight; 35191bb0eedSdrh }else if( pRight==0 ){ 35291bb0eedSdrh return pLeft; 35391bb0eedSdrh }else{ 354880c15beSdanielk1977 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0); 35591bb0eedSdrh } 35691bb0eedSdrh } 35791bb0eedSdrh 35891bb0eedSdrh /* 3596977fea8Sdrh ** Set the Expr.span field of the given expression to span all 360a76b5dfcSdrh ** text between the two given tokens. 361a76b5dfcSdrh */ 3624adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 3634efc4754Sdrh assert( pRight!=0 ); 3644efc4754Sdrh assert( pLeft!=0 ); 365f3a65f7eSdrh if( pExpr && pRight->z && pLeft->z ){ 366ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 367145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 3686977fea8Sdrh pExpr->span.z = pLeft->z; 36997903fefSdrh pExpr->span.n = pRight->n + (pRight->z - pLeft->z); 3704b59ab5eSdrh }else{ 3716977fea8Sdrh pExpr->span.z = 0; 3724b59ab5eSdrh } 373a76b5dfcSdrh } 374a76b5dfcSdrh } 375a76b5dfcSdrh 376a76b5dfcSdrh /* 377a76b5dfcSdrh ** Construct a new expression node for a function with multiple 378a76b5dfcSdrh ** arguments. 379a76b5dfcSdrh */ 38017435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 381a76b5dfcSdrh Expr *pNew; 3824b202ae2Sdanielk1977 assert( pToken ); 38317435752Sdrh pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) ); 384a76b5dfcSdrh if( pNew==0 ){ 385d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ 386a76b5dfcSdrh return 0; 387a76b5dfcSdrh } 388a76b5dfcSdrh pNew->op = TK_FUNCTION; 389a76b5dfcSdrh pNew->pList = pList; 3904b59ab5eSdrh assert( pToken->dyn==0 ); 391a76b5dfcSdrh pNew->token = *pToken; 3926977fea8Sdrh pNew->span = pNew->token; 393fc976065Sdanielk1977 394fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 395a76b5dfcSdrh return pNew; 396a76b5dfcSdrh } 397a76b5dfcSdrh 398a76b5dfcSdrh /* 399fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 400fa6bc000Sdrh ** in the original SQL statement. 401fa6bc000Sdrh ** 402fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 403fa6bc000Sdrh ** variable number. 404fa6bc000Sdrh ** 405fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 406fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 407fa6bc000Sdrh ** the SQL statement comes from an external source. 408fa6bc000Sdrh ** 409fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 410fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 411fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 412fa6bc000Sdrh ** assigned. 413fa6bc000Sdrh */ 414fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 415fa6bc000Sdrh Token *pToken; 41617435752Sdrh sqlite3 *db = pParse->db; 41717435752Sdrh 418fa6bc000Sdrh if( pExpr==0 ) return; 419fa6bc000Sdrh pToken = &pExpr->token; 420fa6bc000Sdrh assert( pToken->n>=1 ); 421fa6bc000Sdrh assert( pToken->z!=0 ); 422fa6bc000Sdrh assert( pToken->z[0]!=0 ); 423fa6bc000Sdrh if( pToken->n==1 ){ 424fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 425fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 426fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 427fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 428fa6bc000Sdrh ** use it as the variable number */ 429fa6bc000Sdrh int i; 4302646da7eSdrh pExpr->iTable = i = atoi((char*)&pToken->z[1]); 431*c5499befSdrh testcase( i==0 ); 432*c5499befSdrh testcase( i==1 ); 433*c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 434*c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 435bb4957f8Sdrh if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 436fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 437bb4957f8Sdrh db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 438fa6bc000Sdrh } 439fa6bc000Sdrh if( i>pParse->nVar ){ 440fa6bc000Sdrh pParse->nVar = i; 441fa6bc000Sdrh } 442fa6bc000Sdrh }else{ 443fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 444fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 445fa6bc000Sdrh ** has never appeared before, reuse the same variable number 446fa6bc000Sdrh */ 447fa6bc000Sdrh int i, n; 448fa6bc000Sdrh n = pToken->n; 449fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 450fa6bc000Sdrh Expr *pE; 451fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 452fa6bc000Sdrh && pE->token.n==n 453fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 454fa6bc000Sdrh pExpr->iTable = pE->iTable; 455fa6bc000Sdrh break; 456fa6bc000Sdrh } 457fa6bc000Sdrh } 458fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 459fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 460fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 461fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 46217435752Sdrh pParse->apVarExpr = 46317435752Sdrh sqlite3DbReallocOrFree( 46417435752Sdrh db, 46517435752Sdrh pParse->apVarExpr, 46617435752Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) 46717435752Sdrh ); 468fa6bc000Sdrh } 46917435752Sdrh if( !db->mallocFailed ){ 470fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 471fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 472fa6bc000Sdrh } 473fa6bc000Sdrh } 474fa6bc000Sdrh } 475bb4957f8Sdrh if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 476832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 477832b2664Sdanielk1977 } 478fa6bc000Sdrh } 479fa6bc000Sdrh 480fa6bc000Sdrh /* 481a2e00042Sdrh ** Recursively delete an expression tree. 482a2e00042Sdrh */ 4834adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 484a2e00042Sdrh if( p==0 ) return; 48517435752Sdrh if( p->span.dyn ) sqlite3_free((char*)p->span.z); 48617435752Sdrh if( p->token.dyn ) sqlite3_free((char*)p->token.z); 4874adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 4884adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 4894adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 4904adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 49117435752Sdrh sqlite3_free(p); 492a2e00042Sdrh } 493a2e00042Sdrh 494d2687b77Sdrh /* 495d2687b77Sdrh ** The Expr.token field might be a string literal that is quoted. 496d2687b77Sdrh ** If so, remove the quotation marks. 497d2687b77Sdrh */ 49817435752Sdrh void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ 499d2687b77Sdrh if( ExprHasAnyProperty(p, EP_Dequoted) ){ 500d2687b77Sdrh return; 501d2687b77Sdrh } 502d2687b77Sdrh ExprSetProperty(p, EP_Dequoted); 503d2687b77Sdrh if( p->token.dyn==0 ){ 50417435752Sdrh sqlite3TokenCopy(db, &p->token, &p->token); 505d2687b77Sdrh } 506d2687b77Sdrh sqlite3Dequote((char*)p->token.z); 507d2687b77Sdrh } 508d2687b77Sdrh 509a76b5dfcSdrh 510a76b5dfcSdrh /* 511ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 512ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 513ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 514ff78bd2fSdrh ** without effecting the originals. 515ff78bd2fSdrh ** 5164adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 5174adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 518ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 519ff78bd2fSdrh ** 520ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 521ff78bd2fSdrh */ 5221e536953Sdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){ 523ff78bd2fSdrh Expr *pNew; 524ff78bd2fSdrh if( p==0 ) return 0; 52517435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); 526ff78bd2fSdrh if( pNew==0 ) return 0; 5273b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 5286977fea8Sdrh if( p->token.z!=0 ){ 52917435752Sdrh pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); 5304b59ab5eSdrh pNew->token.dyn = 1; 5314b59ab5eSdrh }else{ 5324efc4754Sdrh assert( pNew->token.z==0 ); 5334b59ab5eSdrh } 5346977fea8Sdrh pNew->span.z = 0; 53517435752Sdrh pNew->pLeft = sqlite3ExprDup(db, p->pLeft); 53617435752Sdrh pNew->pRight = sqlite3ExprDup(db, p->pRight); 53717435752Sdrh pNew->pList = sqlite3ExprListDup(db, p->pList); 53817435752Sdrh pNew->pSelect = sqlite3SelectDup(db, p->pSelect); 539ff78bd2fSdrh return pNew; 540ff78bd2fSdrh } 54117435752Sdrh void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ 54217435752Sdrh if( pTo->dyn ) sqlite3_free((char*)pTo->z); 5434b59ab5eSdrh if( pFrom->z ){ 5444b59ab5eSdrh pTo->n = pFrom->n; 54517435752Sdrh pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); 5464b59ab5eSdrh pTo->dyn = 1; 5474b59ab5eSdrh }else{ 5484b59ab5eSdrh pTo->z = 0; 5494b59ab5eSdrh } 5504b59ab5eSdrh } 55117435752Sdrh ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ 552ff78bd2fSdrh ExprList *pNew; 553145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 554ff78bd2fSdrh int i; 555ff78bd2fSdrh if( p==0 ) return 0; 55617435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 557ff78bd2fSdrh if( pNew==0 ) return 0; 55831dad9daSdanielk1977 pNew->iECursor = 0; 5594305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 56017435752Sdrh pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); 561e0048400Sdanielk1977 if( pItem==0 ){ 56217435752Sdrh sqlite3_free(pNew); 563e0048400Sdanielk1977 return 0; 564e0048400Sdanielk1977 } 565145716b3Sdrh pOldItem = p->a; 566145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 5674b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 56817435752Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); 5696977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 5706977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 5714b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 5724b59ab5eSdrh ** the names of columns in the result set needs this information */ 57317435752Sdrh sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); 5744b59ab5eSdrh } 5751f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 5766f7adc8aSdrh || pOldExpr->span.z==0 57717435752Sdrh || db->mallocFailed ); 57817435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 579145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 580145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 5813e7bc9caSdrh pItem->done = 0; 582ff78bd2fSdrh } 583ff78bd2fSdrh return pNew; 584ff78bd2fSdrh } 58593758c8dSdanielk1977 58693758c8dSdanielk1977 /* 58793758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 58893758c8dSdanielk1977 ** the build, then none of the following routines, except for 58993758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 59093758c8dSdanielk1977 ** called with a NULL argument. 59193758c8dSdanielk1977 */ 5926a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 5936a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 59417435752Sdrh SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ 595ad3cab52Sdrh SrcList *pNew; 596ad3cab52Sdrh int i; 597113088ecSdrh int nByte; 598ad3cab52Sdrh if( p==0 ) return 0; 599113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 60017435752Sdrh pNew = sqlite3DbMallocRaw(db, nByte ); 601ad3cab52Sdrh if( pNew==0 ) return 0; 6024305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 603ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 6044efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 6054efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 606ed8a3bb1Sdrh Table *pTab; 60717435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 60817435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 60917435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 6104efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 6114efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 6121787ccabSdanielk1977 pNewItem->isPopulated = pOldItem->isPopulated; 613ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 614ed8a3bb1Sdrh if( pTab ){ 615ed8a3bb1Sdrh pTab->nRef++; 616a1cb183dSdanielk1977 } 61717435752Sdrh pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); 61817435752Sdrh pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); 61917435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 6206c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 621ad3cab52Sdrh } 622ad3cab52Sdrh return pNew; 623ad3cab52Sdrh } 62417435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 625ff78bd2fSdrh IdList *pNew; 626ff78bd2fSdrh int i; 627ff78bd2fSdrh if( p==0 ) return 0; 62817435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 629ff78bd2fSdrh if( pNew==0 ) return 0; 6304305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 63117435752Sdrh pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); 632d5d56523Sdanielk1977 if( pNew->a==0 ){ 63317435752Sdrh sqlite3_free(pNew); 634d5d56523Sdanielk1977 return 0; 635d5d56523Sdanielk1977 } 636ff78bd2fSdrh for(i=0; i<p->nId; i++){ 6374efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 6384efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 63917435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 6404efc4754Sdrh pNewItem->idx = pOldItem->idx; 641ff78bd2fSdrh } 642ff78bd2fSdrh return pNew; 643ff78bd2fSdrh } 64417435752Sdrh Select *sqlite3SelectDup(sqlite3 *db, Select *p){ 645ff78bd2fSdrh Select *pNew; 646ff78bd2fSdrh if( p==0 ) return 0; 64717435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); 648ff78bd2fSdrh if( pNew==0 ) return 0; 649ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 65017435752Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList); 65117435752Sdrh pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); 65217435752Sdrh pNew->pWhere = sqlite3ExprDup(db, p->pWhere); 65317435752Sdrh pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); 65417435752Sdrh pNew->pHaving = sqlite3ExprDup(db, p->pHaving); 65517435752Sdrh pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); 656ff78bd2fSdrh pNew->op = p->op; 65717435752Sdrh pNew->pPrior = sqlite3SelectDup(db, p->pPrior); 65817435752Sdrh pNew->pLimit = sqlite3ExprDup(db, p->pLimit); 65917435752Sdrh pNew->pOffset = sqlite3ExprDup(db, p->pOffset); 6607b58daeaSdrh pNew->iLimit = -1; 6617b58daeaSdrh pNew->iOffset = -1; 662a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 663a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 664b9bb7c18Sdrh pNew->usesEphm = 0; 6658e647b81Sdrh pNew->disallowOrderBy = 0; 6660342b1f5Sdrh pNew->pRightmost = 0; 667b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 668b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 669b9bb7c18Sdrh pNew->addrOpenEphm[2] = -1; 670ff78bd2fSdrh return pNew; 671ff78bd2fSdrh } 67293758c8dSdanielk1977 #else 67317435752Sdrh Select *sqlite3SelectDup(sqlite3 *db, Select *p){ 67493758c8dSdanielk1977 assert( p==0 ); 67593758c8dSdanielk1977 return 0; 67693758c8dSdanielk1977 } 67793758c8dSdanielk1977 #endif 678ff78bd2fSdrh 679ff78bd2fSdrh 680ff78bd2fSdrh /* 681a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 682a76b5dfcSdrh ** initially NULL, then create a new expression list. 683a76b5dfcSdrh */ 68417435752Sdrh ExprList *sqlite3ExprListAppend( 68517435752Sdrh Parse *pParse, /* Parsing context */ 68617435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 68717435752Sdrh Expr *pExpr, /* Expression to be appended */ 68817435752Sdrh Token *pName /* AS keyword for the expression */ 68917435752Sdrh ){ 69017435752Sdrh sqlite3 *db = pParse->db; 691a76b5dfcSdrh if( pList==0 ){ 69217435752Sdrh pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); 693a76b5dfcSdrh if( pList==0 ){ 694d5d56523Sdanielk1977 goto no_mem; 695a76b5dfcSdrh } 6964efc4754Sdrh assert( pList->nAlloc==0 ); 697a76b5dfcSdrh } 6984305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 699d5d56523Sdanielk1977 struct ExprList_item *a; 700d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 70126783a58Sdanielk1977 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); 702d5d56523Sdanielk1977 if( a==0 ){ 703d5d56523Sdanielk1977 goto no_mem; 704a76b5dfcSdrh } 705d5d56523Sdanielk1977 pList->a = a; 706d5d56523Sdanielk1977 pList->nAlloc = n; 707a76b5dfcSdrh } 7084efc4754Sdrh assert( pList->a!=0 ); 7094efc4754Sdrh if( pExpr || pName ){ 7104efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 7114efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 71217435752Sdrh pItem->zName = sqlite3NameFromToken(db, pName); 713e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 714a76b5dfcSdrh } 715a76b5dfcSdrh return pList; 716d5d56523Sdanielk1977 717d5d56523Sdanielk1977 no_mem: 718d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 719d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 720d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 721d5d56523Sdanielk1977 return 0; 722a76b5dfcSdrh } 723a76b5dfcSdrh 724a76b5dfcSdrh /* 7257a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 7267a15a4beSdanielk1977 ** leave an error message in pParse. 7277a15a4beSdanielk1977 */ 7287a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 7297a15a4beSdanielk1977 Parse *pParse, 7307a15a4beSdanielk1977 ExprList *pEList, 7317a15a4beSdanielk1977 const char *zObject 7327a15a4beSdanielk1977 ){ 733b1a6c3c1Sdrh int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 734*c5499befSdrh testcase( pEList && pEList->nExpr==mx ); 735*c5499befSdrh testcase( pEList && pEList->nExpr==mx+1 ); 736b1a6c3c1Sdrh if( pEList && pEList->nExpr>mx ){ 7377a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 7387a15a4beSdanielk1977 } 7397a15a4beSdanielk1977 } 7407a15a4beSdanielk1977 741fc976065Sdanielk1977 742fc976065Sdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 743fc976065Sdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 744fc976065Sdanielk1977 ** of any expression tree referenced by the structure passed as the 745fc976065Sdanielk1977 ** first argument. 746fc976065Sdanielk1977 ** 747fc976065Sdanielk1977 ** If this maximum height is greater than the current value pointed 748fc976065Sdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 749fc976065Sdanielk1977 ** value. 750fc976065Sdanielk1977 */ 751fc976065Sdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 752fc976065Sdanielk1977 if( p ){ 753fc976065Sdanielk1977 if( p->nHeight>*pnHeight ){ 754fc976065Sdanielk1977 *pnHeight = p->nHeight; 755fc976065Sdanielk1977 } 756fc976065Sdanielk1977 } 757fc976065Sdanielk1977 } 758fc976065Sdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 759fc976065Sdanielk1977 if( p ){ 760fc976065Sdanielk1977 int i; 761fc976065Sdanielk1977 for(i=0; i<p->nExpr; i++){ 762fc976065Sdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 763fc976065Sdanielk1977 } 764fc976065Sdanielk1977 } 765fc976065Sdanielk1977 } 766fc976065Sdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 767fc976065Sdanielk1977 if( p ){ 768fc976065Sdanielk1977 heightOfExpr(p->pWhere, pnHeight); 769fc976065Sdanielk1977 heightOfExpr(p->pHaving, pnHeight); 770fc976065Sdanielk1977 heightOfExpr(p->pLimit, pnHeight); 771fc976065Sdanielk1977 heightOfExpr(p->pOffset, pnHeight); 772fc976065Sdanielk1977 heightOfExprList(p->pEList, pnHeight); 773fc976065Sdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 774fc976065Sdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 775fc976065Sdanielk1977 heightOfSelect(p->pPrior, pnHeight); 776fc976065Sdanielk1977 } 777fc976065Sdanielk1977 } 778fc976065Sdanielk1977 779fc976065Sdanielk1977 /* 780fc976065Sdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 781fc976065Sdanielk1977 ** argument. An expression with no children, Expr.pList or 782fc976065Sdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 783fc976065Sdanielk1977 ** has a height equal to the maximum height of any other 784fc976065Sdanielk1977 ** referenced Expr plus one. 785fc976065Sdanielk1977 */ 786fc976065Sdanielk1977 void sqlite3ExprSetHeight(Expr *p){ 787fc976065Sdanielk1977 int nHeight = 0; 788fc976065Sdanielk1977 heightOfExpr(p->pLeft, &nHeight); 789fc976065Sdanielk1977 heightOfExpr(p->pRight, &nHeight); 790fc976065Sdanielk1977 heightOfExprList(p->pList, &nHeight); 791fc976065Sdanielk1977 heightOfSelect(p->pSelect, &nHeight); 792fc976065Sdanielk1977 p->nHeight = nHeight + 1; 793fc976065Sdanielk1977 } 794fc976065Sdanielk1977 795fc976065Sdanielk1977 /* 796fc976065Sdanielk1977 ** Return the maximum height of any expression tree referenced 797fc976065Sdanielk1977 ** by the select statement passed as an argument. 798fc976065Sdanielk1977 */ 799fc976065Sdanielk1977 int sqlite3SelectExprHeight(Select *p){ 800fc976065Sdanielk1977 int nHeight = 0; 801fc976065Sdanielk1977 heightOfSelect(p, &nHeight); 802fc976065Sdanielk1977 return nHeight; 803fc976065Sdanielk1977 } 804fc976065Sdanielk1977 8057a15a4beSdanielk1977 /* 806a76b5dfcSdrh ** Delete an entire expression list. 807a76b5dfcSdrh */ 8084adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 809a76b5dfcSdrh int i; 810be5c89acSdrh struct ExprList_item *pItem; 811a76b5dfcSdrh if( pList==0 ) return; 8121bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 8131bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 814be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 815be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 81617435752Sdrh sqlite3_free(pItem->zName); 817a76b5dfcSdrh } 81817435752Sdrh sqlite3_free(pList->a); 81917435752Sdrh sqlite3_free(pList); 820a76b5dfcSdrh } 821a76b5dfcSdrh 822a76b5dfcSdrh /* 823678ccce8Sdrh ** Walk an expression tree. Call xFunc for each node visited. xFunc 824678ccce8Sdrh ** is called on the node before xFunc is called on the nodes children. 82573b211abSdrh ** 826626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 827626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 828626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 829626a879aSdrh ** the tree walk completely. 830626a879aSdrh ** 831626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 832626a879aSdrh ** and 0 to continue. 83387abf5c0Sdrh ** 83487abf5c0Sdrh ** NOTICE: This routine does *not* descend into subqueries. 835626a879aSdrh */ 836a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 837626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 838626a879aSdrh int rc; 839626a879aSdrh if( pExpr==0 ) return 0; 840626a879aSdrh rc = (*xFunc)(pArg, pExpr); 841626a879aSdrh if( rc==0 ){ 842626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 843626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 844a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 845626a879aSdrh } 846626a879aSdrh return rc>1; 847626a879aSdrh } 848626a879aSdrh 849626a879aSdrh /* 850a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 851a58fdfb1Sdanielk1977 */ 852a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 853a58fdfb1Sdanielk1977 int i; 854a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 855a58fdfb1Sdanielk1977 if( !p ) return 0; 856a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 857a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 858a58fdfb1Sdanielk1977 } 859a58fdfb1Sdanielk1977 return 0; 860a58fdfb1Sdanielk1977 } 861a58fdfb1Sdanielk1977 862a58fdfb1Sdanielk1977 /* 863a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 864a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 865a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 866a58fdfb1Sdanielk1977 */ 867a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 868a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 869a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 870a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 871a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 872a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 87315d7982aSdanielk1977 if( p->pPrior ){ 87415d7982aSdanielk1977 walkSelectExpr(p->pPrior, xFunc, pArg); 87515d7982aSdanielk1977 } 876a58fdfb1Sdanielk1977 return 0; 877a58fdfb1Sdanielk1977 } 878a58fdfb1Sdanielk1977 879a58fdfb1Sdanielk1977 880a58fdfb1Sdanielk1977 /* 881626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 882626a879aSdrh ** 883626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 88473b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 88573b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 88673b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 88773b211abSdrh ** then do nothing. 88873b211abSdrh ** 88973b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 89073b211abSdrh ** the expression as constant, then we assume the whole expression 89173b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 892626a879aSdrh */ 893626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 8940a168377Sdrh int *pN = (int*)pArg; 8950a168377Sdrh 8960a168377Sdrh /* If *pArg is 3 then any term of the expression that comes from 8970a168377Sdrh ** the ON or USING clauses of a join disqualifies the expression 8980a168377Sdrh ** from being considered constant. */ 8990a168377Sdrh if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ 9000a168377Sdrh *pN = 0; 9010a168377Sdrh return 2; 9020a168377Sdrh } 9030a168377Sdrh 904626a879aSdrh switch( pExpr->op ){ 905eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 906eb55bd2fSdrh ** and *pArg==2 */ 907eb55bd2fSdrh case TK_FUNCTION: 9080a168377Sdrh if( (*pN)==2 ) return 0; 909eb55bd2fSdrh /* Fall through */ 910626a879aSdrh case TK_ID: 911626a879aSdrh case TK_COLUMN: 912626a879aSdrh case TK_DOT: 913626a879aSdrh case TK_AGG_FUNCTION: 91413449892Sdrh case TK_AGG_COLUMN: 915fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 916fe2093d7Sdrh case TK_SELECT: 917fe2093d7Sdrh case TK_EXISTS: 918*c5499befSdrh testcase( pExpr->op==TK_SELECT ); 919*c5499befSdrh testcase( pExpr->op==TK_EXISTS ); 920fe2093d7Sdrh #endif 921*c5499befSdrh testcase( pExpr->op==TK_ID ); 922*c5499befSdrh testcase( pExpr->op==TK_COLUMN ); 923*c5499befSdrh testcase( pExpr->op==TK_DOT ); 924*c5499befSdrh testcase( pExpr->op==TK_AGG_FUNCTION ); 925*c5499befSdrh testcase( pExpr->op==TK_AGG_COLUMN ); 9260a168377Sdrh *pN = 0; 927626a879aSdrh return 2; 92887abf5c0Sdrh case TK_IN: 92987abf5c0Sdrh if( pExpr->pSelect ){ 9300a168377Sdrh *pN = 0; 93187abf5c0Sdrh return 2; 93287abf5c0Sdrh } 933626a879aSdrh default: 934626a879aSdrh return 0; 935626a879aSdrh } 936626a879aSdrh } 937626a879aSdrh 938626a879aSdrh /* 939fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 940eb55bd2fSdrh ** and 0 if it involves variables or function calls. 9412398937bSdrh ** 9422398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 9432398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 9442398937bSdrh ** a constant. 945fef5208cSdrh */ 9464adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 947626a879aSdrh int isConst = 1; 948626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 949626a879aSdrh return isConst; 950fef5208cSdrh } 951fef5208cSdrh 952fef5208cSdrh /* 953eb55bd2fSdrh ** Walk an expression tree. Return 1 if the expression is constant 9540a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 9550a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 9560a168377Sdrh ** an ON or USING clause. 9570a168377Sdrh */ 9580a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 9590a168377Sdrh int isConst = 3; 9600a168377Sdrh walkExprTree(p, exprNodeIsConstant, &isConst); 9610a168377Sdrh return isConst!=0; 9620a168377Sdrh } 9630a168377Sdrh 9640a168377Sdrh /* 9650a168377Sdrh ** Walk an expression tree. Return 1 if the expression is constant 966eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 967eb55bd2fSdrh ** are any variables. 968eb55bd2fSdrh ** 969eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 970eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 971eb55bd2fSdrh ** a constant. 972eb55bd2fSdrh */ 973eb55bd2fSdrh int sqlite3ExprIsConstantOrFunction(Expr *p){ 974eb55bd2fSdrh int isConst = 2; 975eb55bd2fSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 976eb55bd2fSdrh return isConst!=0; 977eb55bd2fSdrh } 978eb55bd2fSdrh 979eb55bd2fSdrh /* 98073b211abSdrh ** If the expression p codes a constant integer that is small enough 981202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 982202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 983202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 984e4de1febSdrh */ 9854adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 986e4de1febSdrh switch( p->op ){ 987e4de1febSdrh case TK_INTEGER: { 9882646da7eSdrh if( sqlite3GetInt32((char*)p->token.z, pValue) ){ 989e4de1febSdrh return 1; 990e4de1febSdrh } 991202b2df7Sdrh break; 992202b2df7Sdrh } 9934b59ab5eSdrh case TK_UPLUS: { 9944adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 9954b59ab5eSdrh } 996e4de1febSdrh case TK_UMINUS: { 997e4de1febSdrh int v; 9984adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 999e4de1febSdrh *pValue = -v; 1000e4de1febSdrh return 1; 1001e4de1febSdrh } 1002e4de1febSdrh break; 1003e4de1febSdrh } 1004e4de1febSdrh default: break; 1005e4de1febSdrh } 1006e4de1febSdrh return 0; 1007e4de1febSdrh } 1008e4de1febSdrh 1009e4de1febSdrh /* 1010c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 1011c4a3c779Sdrh */ 10124adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 10134adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 10144adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 10154adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 1016c4a3c779Sdrh return 0; 1017c4a3c779Sdrh } 1018c4a3c779Sdrh 1019c4a3c779Sdrh /* 10208141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 10218141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 10228141f61eSdrh ** expression node refer back to that source column. The following changes 10238141f61eSdrh ** are made to pExpr: 10248141f61eSdrh ** 10258141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 10268141f61eSdrh ** the table. 10278141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 10288141f61eSdrh ** from pSrcList. 10298141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 10308141f61eSdrh ** pExpr->op Set to TK_COLUMN. 10318141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 10328141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 10338141f61eSdrh ** 10348141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 10358141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 10368141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 10378141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 10388141f61eSdrh ** means that the form of the name is Z and that columns from any table 10398141f61eSdrh ** can be used. 10408141f61eSdrh ** 10418141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 10428141f61eSdrh ** in pParse and return non-zero. Return zero on success. 10438141f61eSdrh */ 10448141f61eSdrh static int lookupName( 10458141f61eSdrh Parse *pParse, /* The parsing context */ 10468141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 10478141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 10488141f61eSdrh Token *pColumnToken, /* Name of the column. */ 1049626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 10508141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 10518141f61eSdrh ){ 10528141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 10538141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 10548141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 10558141f61eSdrh int i, j; /* Loop counters */ 10568141f61eSdrh int cnt = 0; /* Number of matching column names */ 10578141f61eSdrh int cntTab = 0; /* Number of matching table names */ 10589bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 105951669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 106051669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 106173b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 1062728b5779Sdrh Schema *pSchema = 0; /* Schema of the expression */ 10638141f61eSdrh 10648141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 106517435752Sdrh zDb = sqlite3NameFromToken(db, pDbToken); 106617435752Sdrh zTab = sqlite3NameFromToken(db, pTableToken); 106717435752Sdrh zCol = sqlite3NameFromToken(db, pColumnToken); 106817435752Sdrh if( db->mallocFailed ){ 1069d5d56523Sdanielk1977 goto lookupname_end; 10708141f61eSdrh } 10718141f61eSdrh 10728141f61eSdrh pExpr->iTable = -1; 1073626a879aSdrh while( pNC && cnt==0 ){ 1074ffe07b2dSdrh ExprList *pEList; 1075626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 1076626a879aSdrh 1077b3bce662Sdanielk1977 if( pSrcList ){ 107851669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 107943617e9aSdrh Table *pTab; 108043617e9aSdrh int iDb; 10818141f61eSdrh Column *pCol; 10828141f61eSdrh 108343617e9aSdrh pTab = pItem->pTab; 108443617e9aSdrh assert( pTab!=0 ); 108543617e9aSdrh iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 10868141f61eSdrh assert( pTab->nCol>0 ); 10878141f61eSdrh if( zTab ){ 10888141f61eSdrh if( pItem->zAlias ){ 10898141f61eSdrh char *zTabName = pItem->zAlias; 10904adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 10918141f61eSdrh }else{ 10928141f61eSdrh char *zTabName = pTab->zName; 10934adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 1094da184236Sdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ 10958141f61eSdrh continue; 10968141f61eSdrh } 10978141f61eSdrh } 10988141f61eSdrh } 10998141f61eSdrh if( 0==(cntTab++) ){ 11008141f61eSdrh pExpr->iTable = pItem->iCursor; 1101728b5779Sdrh pSchema = pTab->pSchema; 110251669863Sdrh pMatch = pItem; 11038141f61eSdrh } 11048141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 11054adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1106b3bf556eSdanielk1977 const char *zColl = pTab->aCol[j].zColl; 1107873fac0cSdrh IdList *pUsing; 11088141f61eSdrh cnt++; 11098141f61eSdrh pExpr->iTable = pItem->iCursor; 111051669863Sdrh pMatch = pItem; 1111728b5779Sdrh pSchema = pTab->pSchema; 11128141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 11138141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 1114a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 11158b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1116b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 11178b4c40d8Sdrh } 111861dfc31dSdrh if( i<pSrcList->nSrc-1 ){ 111961dfc31dSdrh if( pItem[1].jointype & JT_NATURAL ){ 1120355ef361Sdrh /* If this match occurred in the left table of a natural join, 1121355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 1122355ef361Sdrh pItem++; 1123355ef361Sdrh i++; 112461dfc31dSdrh }else if( (pUsing = pItem[1].pUsing)!=0 ){ 1125873fac0cSdrh /* If this match occurs on a column that is in the USING clause 1126873fac0cSdrh ** of a join, skip the search of the right table of the join 1127873fac0cSdrh ** to avoid a duplicate match there. */ 1128873fac0cSdrh int k; 1129873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 1130873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 1131873fac0cSdrh pItem++; 1132873fac0cSdrh i++; 1133873fac0cSdrh break; 1134873fac0cSdrh } 1135873fac0cSdrh } 1136873fac0cSdrh } 113761dfc31dSdrh } 11388141f61eSdrh break; 11398141f61eSdrh } 11408141f61eSdrh } 11418141f61eSdrh } 1142b3bce662Sdanielk1977 } 11438141f61eSdrh 1144b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 11458141f61eSdrh /* If we have not already resolved the name, then maybe 11468141f61eSdrh ** it is a new.* or old.* trigger argument reference 11478141f61eSdrh */ 11488141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 11498141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 11508141f61eSdrh Table *pTab = 0; 11518f2c54e6Sdanielk1977 u32 *piColMask; 11524adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 11538141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 11548141f61eSdrh assert( pTriggerStack->pTab ); 11558141f61eSdrh pTab = pTriggerStack->pTab; 11568f2c54e6Sdanielk1977 piColMask = &(pTriggerStack->newColMask); 11574adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 11588141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 11598141f61eSdrh assert( pTriggerStack->pTab ); 11608141f61eSdrh pTab = pTriggerStack->pTab; 11618f2c54e6Sdanielk1977 piColMask = &(pTriggerStack->oldColMask); 11628141f61eSdrh } 11638141f61eSdrh 11648141f61eSdrh if( pTab ){ 1165f0113000Sdanielk1977 int iCol; 11668141f61eSdrh Column *pCol = pTab->aCol; 11678141f61eSdrh 1168728b5779Sdrh pSchema = pTab->pSchema; 11698141f61eSdrh cntTab++; 1170f0113000Sdanielk1977 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { 11714adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1172f0113000Sdanielk1977 const char *zColl = pTab->aCol[iCol].zColl; 11738141f61eSdrh cnt++; 1174f0113000Sdanielk1977 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; 1175f0113000Sdanielk1977 pExpr->affinity = pTab->aCol[iCol].affinity; 11768b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1177b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 11788b4c40d8Sdrh } 1179aee18ef8Sdanielk1977 pExpr->pTab = pTab; 11808f2c54e6Sdanielk1977 if( iCol>=0 ){ 1181*c5499befSdrh testcase( iCol==31 ); 1182*c5499befSdrh testcase( iCol==32 ); 11838f2c54e6Sdanielk1977 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0); 11848f2c54e6Sdanielk1977 } 11858141f61eSdrh break; 11868141f61eSdrh } 11878141f61eSdrh } 11888141f61eSdrh } 11898141f61eSdrh } 1190b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 11918141f61eSdrh 11928141f61eSdrh /* 11938141f61eSdrh ** Perhaps the name is a reference to the ROWID 11948141f61eSdrh */ 11954adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 11968141f61eSdrh cnt = 1; 11978141f61eSdrh pExpr->iColumn = -1; 11988a51256cSdrh pExpr->affinity = SQLITE_AFF_INTEGER; 11998141f61eSdrh } 12008141f61eSdrh 12018141f61eSdrh /* 12028141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 12038141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 12048141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 12058141f61eSdrh ** 12068141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 12078141f61eSdrh ** 12088141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 12098141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 12108141f61eSdrh ** Note that the expression in the result set should have already been 12118141f61eSdrh ** resolved by the time the WHERE clause is resolved. 12128141f61eSdrh */ 1213ffe07b2dSdrh if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ 12148141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 12158141f61eSdrh char *zAs = pEList->a[j].zName; 12164adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 121736379e97Sdrh Expr *pDup, *pOrig; 12188141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 12194f07e5fbSdrh assert( pExpr->pList==0 ); 12204f07e5fbSdrh assert( pExpr->pSelect==0 ); 122136379e97Sdrh pOrig = pEList->a[j].pExpr; 122236379e97Sdrh if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ 122336379e97Sdrh sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); 122417435752Sdrh sqlite3_free(zCol); 122536379e97Sdrh return 2; 122636379e97Sdrh } 12271e536953Sdanielk1977 pDup = sqlite3ExprDup(db, pOrig); 12284f07e5fbSdrh if( pExpr->flags & EP_ExpCollate ){ 12294f07e5fbSdrh pDup->pColl = pExpr->pColl; 12304f07e5fbSdrh pDup->flags |= EP_ExpCollate; 12314f07e5fbSdrh } 123217435752Sdrh if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z); 123317435752Sdrh if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z); 12344f07e5fbSdrh memcpy(pExpr, pDup, sizeof(*pExpr)); 123517435752Sdrh sqlite3_free(pDup); 123615ccce1cSdrh cnt = 1; 1237c9cf6e3dSdanielk1977 pMatch = 0; 12388141f61eSdrh assert( zTab==0 && zDb==0 ); 123915ccce1cSdrh goto lookupname_end_2; 12408141f61eSdrh } 12418141f61eSdrh } 12428141f61eSdrh } 12438141f61eSdrh 1244626a879aSdrh /* Advance to the next name context. The loop will exit when either 1245626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 1246626a879aSdrh */ 1247626a879aSdrh if( cnt==0 ){ 1248626a879aSdrh pNC = pNC->pNext; 1249626a879aSdrh } 1250626a879aSdrh } 1251626a879aSdrh 12528141f61eSdrh /* 12538141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 12548141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 12558141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 12568141f61eSdrh ** case, we need to return right away and not make any changes to 12578141f61eSdrh ** pExpr. 125815ccce1cSdrh ** 125915ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 126015ccce1cSdrh ** fields are not changed in any context. 12618141f61eSdrh */ 12628141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 126317435752Sdrh sqlite3_free(zCol); 12648141f61eSdrh return 0; 12658141f61eSdrh } 12668141f61eSdrh 12678141f61eSdrh /* 12688141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 12698141f61eSdrh ** more matches. Either way, we have an error. 12708141f61eSdrh */ 12718141f61eSdrh if( cnt!=1 ){ 1272de4fcfddSdrh const char *zErr; 1273de4fcfddSdrh zErr = cnt==0 ? "no such column" : "ambiguous column name"; 12748141f61eSdrh if( zDb ){ 1275de4fcfddSdrh sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); 12768141f61eSdrh }else if( zTab ){ 1277de4fcfddSdrh sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); 12788141f61eSdrh }else{ 1279de4fcfddSdrh sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); 12808141f61eSdrh } 128173b211abSdrh pTopNC->nErr++; 12828141f61eSdrh } 12838141f61eSdrh 128451669863Sdrh /* If a column from a table in pSrcList is referenced, then record 128551669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 128651669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 128751669863Sdrh ** column number is greater than the number of bits in the bitmask 128851669863Sdrh ** then set the high-order bit of the bitmask. 128951669863Sdrh */ 129051669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 129151669863Sdrh int n = pExpr->iColumn; 1292*c5499befSdrh testcase( n==sizeof(Bitmask)*8-1 ); 129351669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 129451669863Sdrh n = sizeof(Bitmask)*8-1; 129551669863Sdrh } 129651669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 1297ca83ac51Sdrh pMatch->colUsed |= ((Bitmask)1)<<n; 129851669863Sdrh } 129951669863Sdrh 1300d5d56523Sdanielk1977 lookupname_end: 13018141f61eSdrh /* Clean up and return 13028141f61eSdrh */ 130317435752Sdrh sqlite3_free(zDb); 130417435752Sdrh sqlite3_free(zTab); 13054adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 13068141f61eSdrh pExpr->pLeft = 0; 13074adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 13088141f61eSdrh pExpr->pRight = 0; 13098141f61eSdrh pExpr->op = TK_COLUMN; 131015ccce1cSdrh lookupname_end_2: 131117435752Sdrh sqlite3_free(zCol); 1312626a879aSdrh if( cnt==1 ){ 1313b3bce662Sdanielk1977 assert( pNC!=0 ); 1314728b5779Sdrh sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); 1315aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 1316aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 1317aee18ef8Sdanielk1977 } 131815ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 131915ccce1cSdrh ** the point where the name matched. */ 132015ccce1cSdrh for(;;){ 132115ccce1cSdrh assert( pTopNC!=0 ); 132215ccce1cSdrh pTopNC->nRef++; 132315ccce1cSdrh if( pTopNC==pNC ) break; 132415ccce1cSdrh pTopNC = pTopNC->pNext; 1325626a879aSdrh } 132615ccce1cSdrh return 0; 132715ccce1cSdrh } else { 132815ccce1cSdrh return 1; 132915ccce1cSdrh } 13308141f61eSdrh } 13318141f61eSdrh 13328141f61eSdrh /* 1333626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1334626a879aSdrh ** 133573b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1336626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 133773b211abSdrh ** the tree or 2 to abort the tree walk. 133873b211abSdrh ** 133973b211abSdrh ** This routine also does error checking and name resolution for 134073b211abSdrh ** function names. The operator for aggregate functions is changed 134173b211abSdrh ** to TK_AGG_FUNCTION. 1342626a879aSdrh */ 1343626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1344626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1345626a879aSdrh Parse *pParse; 1346626a879aSdrh 1347b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1348626a879aSdrh assert( pNC!=0 ); 1349626a879aSdrh pParse = pNC->pParse; 1350b3bce662Sdanielk1977 1351626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1352626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1353626a879aSdrh #ifndef NDEBUG 1354f0113000Sdanielk1977 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ 1355f0113000Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 1356940fac9dSdanielk1977 int i; 1357f0113000Sdanielk1977 for(i=0; i<pNC->pSrcList->nSrc; i++){ 1358626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1359626a879aSdrh } 1360626a879aSdrh } 1361626a879aSdrh #endif 1362626a879aSdrh switch( pExpr->op ){ 1363626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1364626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1365626a879aSdrh ** strings (ex: 'abc') are always string literals. 1366626a879aSdrh */ 1367626a879aSdrh case TK_STRING: { 1368626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1369626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1370626a879aSdrh } 1371626a879aSdrh /* A lone identifier is the name of a column. 1372626a879aSdrh */ 1373626a879aSdrh case TK_ID: { 1374626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1375626a879aSdrh return 1; 1376626a879aSdrh } 1377626a879aSdrh 1378626a879aSdrh /* A table name and column name: ID.ID 1379626a879aSdrh ** Or a database, table and column: ID.ID.ID 1380626a879aSdrh */ 1381626a879aSdrh case TK_DOT: { 1382626a879aSdrh Token *pColumn; 1383626a879aSdrh Token *pTable; 1384626a879aSdrh Token *pDb; 1385626a879aSdrh Expr *pRight; 1386626a879aSdrh 1387b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1388626a879aSdrh pRight = pExpr->pRight; 1389626a879aSdrh if( pRight->op==TK_ID ){ 1390626a879aSdrh pDb = 0; 1391626a879aSdrh pTable = &pExpr->pLeft->token; 1392626a879aSdrh pColumn = &pRight->token; 1393626a879aSdrh }else{ 1394626a879aSdrh assert( pRight->op==TK_DOT ); 1395626a879aSdrh pDb = &pExpr->pLeft->token; 1396626a879aSdrh pTable = &pRight->pLeft->token; 1397626a879aSdrh pColumn = &pRight->pRight->token; 1398626a879aSdrh } 1399626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1400626a879aSdrh return 1; 1401626a879aSdrh } 1402626a879aSdrh 1403626a879aSdrh /* Resolve function names 1404626a879aSdrh */ 1405b71090fdSdrh case TK_CONST_FUNC: 1406626a879aSdrh case TK_FUNCTION: { 1407626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1408626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1409626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1410626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1411626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1412626a879aSdrh int i; 14135169bbc6Sdrh int auth; /* Authorization to use the function */ 1414626a879aSdrh int nId; /* Number of characters in function name */ 1415626a879aSdrh const char *zId; /* The function name. */ 141673b211abSdrh FuncDef *pDef; /* Information about the function */ 141714db2665Sdanielk1977 int enc = ENC(pParse->db); /* The database encoding */ 1418626a879aSdrh 14192646da7eSdrh zId = (char*)pExpr->token.z; 1420b71090fdSdrh nId = pExpr->token.n; 1421626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1422626a879aSdrh if( pDef==0 ){ 1423626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1424626a879aSdrh if( pDef==0 ){ 1425626a879aSdrh no_such_func = 1; 1426626a879aSdrh }else{ 1427626a879aSdrh wrong_num_args = 1; 1428626a879aSdrh } 1429626a879aSdrh }else{ 1430626a879aSdrh is_agg = pDef->xFunc==0; 1431626a879aSdrh } 14322fca7fefSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 14335169bbc6Sdrh if( pDef ){ 14345169bbc6Sdrh auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); 14355169bbc6Sdrh if( auth!=SQLITE_OK ){ 14365169bbc6Sdrh if( auth==SQLITE_DENY ){ 14375169bbc6Sdrh sqlite3ErrorMsg(pParse, "not authorized to use function: %s", 14385169bbc6Sdrh pDef->zName); 14395169bbc6Sdrh pNC->nErr++; 14405169bbc6Sdrh } 14415169bbc6Sdrh pExpr->op = TK_NULL; 14425169bbc6Sdrh return 1; 14435169bbc6Sdrh } 14445169bbc6Sdrh } 1445b8b14219Sdrh #endif 1446626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1447626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1448626a879aSdrh pNC->nErr++; 1449626a879aSdrh is_agg = 0; 1450626a879aSdrh }else if( no_such_func ){ 1451626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1452626a879aSdrh pNC->nErr++; 1453626a879aSdrh }else if( wrong_num_args ){ 1454626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1455626a879aSdrh nId, zId); 1456626a879aSdrh pNC->nErr++; 1457626a879aSdrh } 1458626a879aSdrh if( is_agg ){ 1459626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1460626a879aSdrh pNC->hasAgg = 1; 1461626a879aSdrh } 146273b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1463626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 146473b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1465626a879aSdrh } 146673b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1467626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1468626a879aSdrh ** type of the function 1469626a879aSdrh */ 1470626a879aSdrh return is_agg; 1471626a879aSdrh } 1472b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1473b3bce662Sdanielk1977 case TK_SELECT: 1474b3bce662Sdanielk1977 case TK_EXISTS: 1475b3bce662Sdanielk1977 #endif 1476b3bce662Sdanielk1977 case TK_IN: { 1477b3bce662Sdanielk1977 if( pExpr->pSelect ){ 14788a9f38feSdrh int nRef = pNC->nRef; 147906f6541eSdrh #ifndef SQLITE_OMIT_CHECK 148006f6541eSdrh if( pNC->isCheck ){ 148106f6541eSdrh sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); 148206f6541eSdrh } 148306f6541eSdrh #endif 1484b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1485b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1486b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1487b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1488b3bce662Sdanielk1977 } 1489b3bce662Sdanielk1977 } 14904284fb07Sdrh break; 1491b3bce662Sdanielk1977 } 14924284fb07Sdrh #ifndef SQLITE_OMIT_CHECK 14934284fb07Sdrh case TK_VARIABLE: { 14944284fb07Sdrh if( pNC->isCheck ){ 14954284fb07Sdrh sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); 14964284fb07Sdrh } 14974284fb07Sdrh break; 14984284fb07Sdrh } 14994284fb07Sdrh #endif 1500626a879aSdrh } 1501626a879aSdrh return 0; 1502626a879aSdrh } 1503626a879aSdrh 1504626a879aSdrh /* 1505cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1506967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1507aacc543eSdrh ** index to the table in the table list and a column offset. The 1508aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1509aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1510832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1511aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1512aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1513aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1514aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1515aacc543eSdrh ** alias for ROWID. 151619a775c2Sdrh ** 1517626a879aSdrh ** Also resolve function names and check the functions for proper 1518626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1519626a879aSdrh ** have the correct number of arguments. Leave an error message 1520626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1521626a879aSdrh ** 152273b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 152373b211abSdrh ** property on the expression. 1524626a879aSdrh */ 1525626a879aSdrh int sqlite3ExprResolveNames( 1526b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1527b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1528626a879aSdrh ){ 152913449892Sdrh int savedHasAgg; 1530bb4957f8Sdrh 153173b211abSdrh if( pExpr==0 ) return 0; 1532bb4957f8Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 1533bb4957f8Sdrh { 1534bb4957f8Sdrh int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 1535bb4957f8Sdrh if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){ 1536fc976065Sdanielk1977 sqlite3ErrorMsg(pNC->pParse, 1537bb4957f8Sdrh "Expression tree is too large (maximum depth %d)", mxDepth 1538fc976065Sdanielk1977 ); 1539fc976065Sdanielk1977 return 1; 1540fc976065Sdanielk1977 } 1541fc976065Sdanielk1977 pNC->pParse->nHeight += pExpr->nHeight; 1542bb4957f8Sdrh } 1543fc976065Sdanielk1977 #endif 154413449892Sdrh savedHasAgg = pNC->hasAgg; 154513449892Sdrh pNC->hasAgg = 0; 1546b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1547bb4957f8Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 1548fc976065Sdanielk1977 pNC->pParse->nHeight -= pExpr->nHeight; 1549fc976065Sdanielk1977 #endif 1550b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 155173b211abSdrh ExprSetProperty(pExpr, EP_Error); 155273b211abSdrh } 155313449892Sdrh if( pNC->hasAgg ){ 155413449892Sdrh ExprSetProperty(pExpr, EP_Agg); 155513449892Sdrh }else if( savedHasAgg ){ 155613449892Sdrh pNC->hasAgg = 1; 155713449892Sdrh } 155873b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1559626a879aSdrh } 1560626a879aSdrh 15611398ad36Sdrh /* 15621398ad36Sdrh ** A pointer instance of this structure is used to pass information 15631398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 15641398ad36Sdrh */ 15651398ad36Sdrh typedef struct QueryCoder QueryCoder; 15661398ad36Sdrh struct QueryCoder { 15671398ad36Sdrh Parse *pParse; /* The parsing context */ 15681398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 15691398ad36Sdrh }; 15701398ad36Sdrh 15719a96b668Sdanielk1977 #ifdef SQLITE_TEST 15729a96b668Sdanielk1977 int sqlite3_enable_in_opt = 1; 15739a96b668Sdanielk1977 #else 15749a96b668Sdanielk1977 #define sqlite3_enable_in_opt 1 15759a96b668Sdanielk1977 #endif 15769a96b668Sdanielk1977 15779a96b668Sdanielk1977 /* 15789a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 15799a96b668Sdanielk1977 ** It's job is to find or create a b-tree structure that may be used 15809a96b668Sdanielk1977 ** either to test for membership of the (...) set or to iterate through 158185b623f2Sdrh ** its members, skipping duplicates. 15829a96b668Sdanielk1977 ** 15839a96b668Sdanielk1977 ** The cursor opened on the structure (database table, database index 15849a96b668Sdanielk1977 ** or ephermal table) is stored in pX->iTable before this function returns. 15859a96b668Sdanielk1977 ** The returned value indicates the structure type, as follows: 15869a96b668Sdanielk1977 ** 15879a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 15882d401ab8Sdrh ** IN_INDEX_INDEX - The cursor was opened on a database index. 15899a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 15909a96b668Sdanielk1977 ** populated epheremal table. 15919a96b668Sdanielk1977 ** 15929a96b668Sdanielk1977 ** An existing structure may only be used if the SELECT is of the simple 15939a96b668Sdanielk1977 ** form: 15949a96b668Sdanielk1977 ** 15959a96b668Sdanielk1977 ** SELECT <column> FROM <table> 15969a96b668Sdanielk1977 ** 15979a96b668Sdanielk1977 ** If the mustBeUnique parameter is false, the structure will be used 15989a96b668Sdanielk1977 ** for fast set membership tests. In this case an epheremal table must 15999a96b668Sdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 160085b623f2Sdrh ** be found with <column> as its left-most column. 16019a96b668Sdanielk1977 ** 16029a96b668Sdanielk1977 ** If mustBeUnique is true, then the structure will be used to iterate 16039a96b668Sdanielk1977 ** through the set members, skipping any duplicates. In this case an 16049a96b668Sdanielk1977 ** epheremal table must be used unless the selected <column> is guaranteed 16059a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 16069a96b668Sdanielk1977 ** is unique by virtue of a constraint or implicit index. 16079a96b668Sdanielk1977 */ 1608284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 16099a96b668Sdanielk1977 int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){ 16109a96b668Sdanielk1977 Select *p; 16119a96b668Sdanielk1977 int eType = 0; 16129a96b668Sdanielk1977 int iTab = pParse->nTab++; 16139a96b668Sdanielk1977 16149a96b668Sdanielk1977 /* The follwing if(...) expression is true if the SELECT is of the 16159a96b668Sdanielk1977 ** simple form: 16169a96b668Sdanielk1977 ** 16179a96b668Sdanielk1977 ** SELECT <column> FROM <table> 16189a96b668Sdanielk1977 ** 16199a96b668Sdanielk1977 ** If this is the case, it may be possible to use an existing table 16209a96b668Sdanielk1977 ** or index instead of generating an epheremal table. 16219a96b668Sdanielk1977 */ 16229a96b668Sdanielk1977 if( sqlite3_enable_in_opt 1623c81945e4Sdrh && (p=pX->pSelect)!=0 && !p->pPrior 16249a96b668Sdanielk1977 && !p->isDistinct && !p->isAgg && !p->pGroupBy 16259a96b668Sdanielk1977 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect 1626b2b95d41Sdanielk1977 && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect 16279a96b668Sdanielk1977 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN 16289a96b668Sdanielk1977 && !p->pLimit && !p->pOffset && !p->pWhere 16299a96b668Sdanielk1977 ){ 16309a96b668Sdanielk1977 sqlite3 *db = pParse->db; 16319a96b668Sdanielk1977 Index *pIdx; 16329a96b668Sdanielk1977 Expr *pExpr = p->pEList->a[0].pExpr; 16339a96b668Sdanielk1977 int iCol = pExpr->iColumn; 16349a96b668Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 16359a96b668Sdanielk1977 16369a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 16379a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 16389a96b668Sdanielk1977 ** successful here. 16399a96b668Sdanielk1977 */ 16409a96b668Sdanielk1977 assert(v); 16419a96b668Sdanielk1977 if( iCol<0 ){ 16420a07c107Sdrh int iMem = ++pParse->nMem; 16439a96b668Sdanielk1977 int iAddr; 16449a96b668Sdanielk1977 Table *pTab = p->pSrc->a[0].pTab; 16459a96b668Sdanielk1977 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 16469a96b668Sdanielk1977 sqlite3VdbeUsesBtree(v, iDb); 16479a96b668Sdanielk1977 1648892d3179Sdrh iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); 16494c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); 16509a96b668Sdanielk1977 16519a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 16529a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 16539a96b668Sdanielk1977 16549a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 16559a96b668Sdanielk1977 }else{ 16569a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 16579a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 16589a96b668Sdanielk1977 ** to this collation sequence. 16599a96b668Sdanielk1977 */ 16609a96b668Sdanielk1977 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); 16619a96b668Sdanielk1977 16629a96b668Sdanielk1977 /* Check that the affinity that will be used to perform the 16639a96b668Sdanielk1977 ** comparison is the same as the affinity of the column. If 16649a96b668Sdanielk1977 ** it is not, it is not possible to use any index. 16659a96b668Sdanielk1977 */ 16669a96b668Sdanielk1977 Table *pTab = p->pSrc->a[0].pTab; 16679a96b668Sdanielk1977 char aff = comparisonAffinity(pX); 16689a96b668Sdanielk1977 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); 16699a96b668Sdanielk1977 16709a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 16719a96b668Sdanielk1977 if( (pIdx->aiColumn[0]==iCol) 16729a96b668Sdanielk1977 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0)) 16739a96b668Sdanielk1977 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) 16749a96b668Sdanielk1977 ){ 16759a96b668Sdanielk1977 int iDb; 16760a07c107Sdrh int iMem = ++pParse->nMem; 16779a96b668Sdanielk1977 int iAddr; 16789a96b668Sdanielk1977 char *pKey; 16799a96b668Sdanielk1977 16809a96b668Sdanielk1977 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); 16819a96b668Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema); 16829a96b668Sdanielk1977 sqlite3VdbeUsesBtree(v, iDb); 16839a96b668Sdanielk1977 1684892d3179Sdrh iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); 16854c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); 16869a96b668Sdanielk1977 1687cd3e8f7cSdanielk1977 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn); 1688207872a4Sdanielk1977 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, 168966a5167bSdrh pKey,P4_KEYINFO_HANDOFF); 1690207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 16919a96b668Sdanielk1977 eType = IN_INDEX_INDEX; 16929a96b668Sdanielk1977 16939a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 16949a96b668Sdanielk1977 } 16959a96b668Sdanielk1977 } 16969a96b668Sdanielk1977 } 16979a96b668Sdanielk1977 } 16989a96b668Sdanielk1977 16999a96b668Sdanielk1977 if( eType==0 ){ 17009a96b668Sdanielk1977 sqlite3CodeSubselect(pParse, pX); 17019a96b668Sdanielk1977 eType = IN_INDEX_EPH; 17029a96b668Sdanielk1977 }else{ 17039a96b668Sdanielk1977 pX->iTable = iTab; 17049a96b668Sdanielk1977 } 17059a96b668Sdanielk1977 return eType; 17069a96b668Sdanielk1977 } 1707284f4acaSdanielk1977 #endif 1708626a879aSdrh 1709626a879aSdrh /* 17109cbe6352Sdrh ** Generate code for scalar subqueries used as an expression 17119cbe6352Sdrh ** and IN operators. Examples: 1712626a879aSdrh ** 17139cbe6352Sdrh ** (SELECT a FROM b) -- subquery 17149cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 17159cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 17169cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 1717fef5208cSdrh ** 17189cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 17199cbe6352Sdrh ** operator or subquery. 1720cce7d176Sdrh */ 172151522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1722b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 172357dbd7b3Sdrh int testAddr = 0; /* One-time test address */ 1724b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1725b3bce662Sdanielk1977 if( v==0 ) return; 1726b3bce662Sdanielk1977 1727fc976065Sdanielk1977 172857dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 172957dbd7b3Sdrh ** if any of the following is true: 173057dbd7b3Sdrh ** 173157dbd7b3Sdrh ** * The right-hand side is a correlated subquery 173257dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 173357dbd7b3Sdrh ** * We are inside a trigger 173457dbd7b3Sdrh ** 173557dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 173657dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1737b3bce662Sdanielk1977 */ 1738b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 17390a07c107Sdrh int mem = ++pParse->nMem; 1740892d3179Sdrh sqlite3VdbeAddOp1(v, OP_If, mem); 1741892d3179Sdrh testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); 174217435752Sdrh assert( testAddr>0 || pParse->db->mallocFailed ); 1743b3bce662Sdanielk1977 } 1744b3bce662Sdanielk1977 1745cce7d176Sdrh switch( pExpr->op ){ 1746fef5208cSdrh case TK_IN: { 1747e014a838Sdanielk1977 char affinity; 1748d3d39e93Sdrh KeyInfo keyInfo; 1749b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 1750d3d39e93Sdrh 1751bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1752e014a838Sdanielk1977 1753e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 175457dbd7b3Sdrh ** expression it is handled the same way. A virtual table is 1755e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1756e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1757fef5208cSdrh ** 1758e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1759e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1760e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1761e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1762e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1763e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1764e014a838Sdanielk1977 ** is used. 1765fef5208cSdrh */ 1766832508b7Sdrh pExpr->iTable = pParse->nTab++; 1767cd3e8f7cSdanielk1977 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1); 1768d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1769d3d39e93Sdrh keyInfo.nField = 1; 1770e014a838Sdanielk1977 1771e014a838Sdanielk1977 if( pExpr->pSelect ){ 1772e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1773e014a838Sdanielk1977 ** 1774e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1775e014a838Sdanielk1977 ** table allocated and opened above. 1776e014a838Sdanielk1977 */ 17771013c932Sdrh SelectDest dest; 1778be5c89acSdrh ExprList *pEList; 17791013c932Sdrh 17801013c932Sdrh sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 17811013c932Sdrh dest.affinity = (int)affinity; 1782e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 17836c8c8ce0Sdanielk1977 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){ 178494ccde58Sdrh return; 178594ccde58Sdrh } 1786be5c89acSdrh pEList = pExpr->pSelect->pEList; 1787be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 1788bcbb04e5Sdanielk1977 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, 1789be5c89acSdrh pEList->a[0].pExpr); 17900202b29eSdanielk1977 } 1791fef5208cSdrh }else if( pExpr->pList ){ 1792fef5208cSdrh /* Case 2: expr IN (exprlist) 1793fef5208cSdrh ** 1794e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1795e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1796e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1797e014a838Sdanielk1977 ** a column, use numeric affinity. 1798fef5208cSdrh */ 1799e014a838Sdanielk1977 int i; 180057dbd7b3Sdrh ExprList *pList = pExpr->pList; 180157dbd7b3Sdrh struct ExprList_item *pItem; 18022d401ab8Sdrh int r1, r2; 180357dbd7b3Sdrh 1804e014a838Sdanielk1977 if( !affinity ){ 18058159a35fSdrh affinity = SQLITE_AFF_NONE; 1806e014a838Sdanielk1977 } 18070202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1808e014a838Sdanielk1977 1809e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 18102d401ab8Sdrh r1 = sqlite3GetTempReg(pParse); 18112d401ab8Sdrh r2 = sqlite3GetTempReg(pParse); 181257dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 181357dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1814e014a838Sdanielk1977 181557dbd7b3Sdrh /* If the expression is not constant then we will need to 181657dbd7b3Sdrh ** disable the test that was generated above that makes sure 181757dbd7b3Sdrh ** this code only executes once. Because for a non-constant 181857dbd7b3Sdrh ** expression we need to rerun this code each time. 181957dbd7b3Sdrh */ 1820892d3179Sdrh if( testAddr && !sqlite3ExprIsConstant(pE2) ){ 1821892d3179Sdrh sqlite3VdbeChangeToNoop(v, testAddr-1, 2); 182257dbd7b3Sdrh testAddr = 0; 18234794b980Sdrh } 1824e014a838Sdanielk1977 1825e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 1826e55cbd72Sdrh pParse->disableColCache++; 18272d401ab8Sdrh sqlite3ExprCode(pParse, pE2, r1); 1828*c5499befSdrh assert( pParse->disableColCache>0 ); 1829e55cbd72Sdrh pParse->disableColCache--; 18301db639ceSdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); 1831da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, r1, 1); 18322d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); 1833fef5208cSdrh } 18342d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r1); 18352d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r2); 1836fef5208cSdrh } 183766a5167bSdrh sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); 1838b3bce662Sdanielk1977 break; 1839fef5208cSdrh } 1840fef5208cSdrh 184151522cd3Sdrh case TK_EXISTS: 184219a775c2Sdrh case TK_SELECT: { 1843fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1844fef5208cSdrh ** value of this select in a memory cell and record the number 1845967e8b73Sdrh ** of the memory cell in iColumn. 1846fef5208cSdrh */ 18472646da7eSdrh static const Token one = { (u8*)"1", 0, 1 }; 184851522cd3Sdrh Select *pSel; 18496c8c8ce0Sdanielk1977 SelectDest dest; 18501398ad36Sdrh 185151522cd3Sdrh pSel = pExpr->pSelect; 18521013c932Sdrh sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); 185351522cd3Sdrh if( pExpr->op==TK_SELECT ){ 18546c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 18554c583128Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); 1856d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 185751522cd3Sdrh }else{ 18586c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 18594c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); 1860d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 186151522cd3Sdrh } 1862ec7429aeSdrh sqlite3ExprDelete(pSel->pLimit); 1863a1644fd8Sdanielk1977 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); 18646c8c8ce0Sdanielk1977 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){ 186594ccde58Sdrh return; 186694ccde58Sdrh } 18676c8c8ce0Sdanielk1977 pExpr->iColumn = dest.iParm; 1868b3bce662Sdanielk1977 break; 186919a775c2Sdrh } 1870cce7d176Sdrh } 1871b3bce662Sdanielk1977 187257dbd7b3Sdrh if( testAddr ){ 1873892d3179Sdrh sqlite3VdbeJumpHere(v, testAddr-1); 1874b3bce662Sdanielk1977 } 1875fc976065Sdanielk1977 1876b3bce662Sdanielk1977 return; 1877cce7d176Sdrh } 187851522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1879cce7d176Sdrh 1880cce7d176Sdrh /* 1881598f1340Sdrh ** Duplicate an 8-byte value 1882598f1340Sdrh */ 1883598f1340Sdrh static char *dup8bytes(Vdbe *v, const char *in){ 1884598f1340Sdrh char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); 1885598f1340Sdrh if( out ){ 1886598f1340Sdrh memcpy(out, in, 8); 1887598f1340Sdrh } 1888598f1340Sdrh return out; 1889598f1340Sdrh } 1890598f1340Sdrh 1891598f1340Sdrh /* 1892598f1340Sdrh ** Generate an instruction that will put the floating point 18939cbf3425Sdrh ** value described by z[0..n-1] into register iMem. 18940cf19ed8Sdrh ** 18950cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 18960cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 18970cf19ed8Sdrh ** like the continuation of the number. 1898598f1340Sdrh */ 18999de221dfSdrh static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ 1900598f1340Sdrh assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); 1901598f1340Sdrh if( z ){ 1902598f1340Sdrh double value; 1903598f1340Sdrh char *zV; 19040cf19ed8Sdrh assert( !isdigit(z[n]) ); 1905598f1340Sdrh sqlite3AtoF(z, &value); 1906598f1340Sdrh if( negateFlag ) value = -value; 1907598f1340Sdrh zV = dup8bytes(v, (char*)&value); 19089de221dfSdrh sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); 1909598f1340Sdrh } 1910598f1340Sdrh } 1911598f1340Sdrh 1912598f1340Sdrh 1913598f1340Sdrh /* 1914fec19aadSdrh ** Generate an instruction that will put the integer describe by 19159cbf3425Sdrh ** text z[0..n-1] into register iMem. 19160cf19ed8Sdrh ** 19170cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 19180cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 19190cf19ed8Sdrh ** like the continuation of the number. 1920fec19aadSdrh */ 19219de221dfSdrh static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){ 1922abb6fcabSdrh assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); 1923c9cf901dSdanielk1977 if( z ){ 1924fec19aadSdrh int i; 19250cf19ed8Sdrh assert( !isdigit(z[n]) ); 19266fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 19279de221dfSdrh if( negFlag ) i = -i; 19289de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 19299de221dfSdrh }else if( sqlite3FitsIn64Bits(z, negFlag) ){ 1930598f1340Sdrh i64 value; 1931598f1340Sdrh char *zV; 1932598f1340Sdrh sqlite3Atoi64(z, &value); 19339de221dfSdrh if( negFlag ) value = -value; 1934598f1340Sdrh zV = dup8bytes(v, (char*)&value); 19359de221dfSdrh sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); 1936fec19aadSdrh }else{ 19379de221dfSdrh codeReal(v, z, n, negFlag, iMem); 1938fec19aadSdrh } 1939fec19aadSdrh } 1940c9cf901dSdanielk1977 } 1941fec19aadSdrh 1942945498f3Sdrh 1943945498f3Sdrh /* 1944945498f3Sdrh ** Generate code that will extract the iColumn-th column from 1945e55cbd72Sdrh ** table pTab and store the column value in a register. An effort 1946e55cbd72Sdrh ** is made to store the column value in register iReg, but this is 1947e55cbd72Sdrh ** not guaranteed. The location of the column value is returned. 1948e55cbd72Sdrh ** 1949e55cbd72Sdrh ** There must be an open cursor to pTab in iTable when this routine 1950e55cbd72Sdrh ** is called. If iColumn<0 then code is generated that extracts the rowid. 1951da250ea5Sdrh ** 1952da250ea5Sdrh ** This routine might attempt to reuse the value of the column that 1953da250ea5Sdrh ** has already been loaded into a register. The value will always 1954da250ea5Sdrh ** be used if it has not undergone any affinity changes. But if 1955da250ea5Sdrh ** an affinity change has occurred, then the cached value will only be 1956da250ea5Sdrh ** used if allowAffChng is true. 1957945498f3Sdrh */ 1958e55cbd72Sdrh int sqlite3ExprCodeGetColumn( 1959e55cbd72Sdrh Parse *pParse, /* Parsing and code generating context */ 19602133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 19612133d822Sdrh int iColumn, /* Index of the table column */ 19622133d822Sdrh int iTable, /* The cursor pointing to the table */ 1963da250ea5Sdrh int iReg, /* Store results here */ 1964da250ea5Sdrh int allowAffChng /* True if prior affinity changes are OK */ 19652133d822Sdrh ){ 1966e55cbd72Sdrh Vdbe *v = pParse->pVdbe; 1967e55cbd72Sdrh int i; 1968da250ea5Sdrh struct yColCache *p; 1969e55cbd72Sdrh 1970da250ea5Sdrh for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ 1971da250ea5Sdrh if( p->iTable==iTable && p->iColumn==iColumn 1972da250ea5Sdrh && (!p->affChange || allowAffChng) ){ 1973e55cbd72Sdrh #if 0 1974e55cbd72Sdrh sqlite3VdbeAddOp0(v, OP_Noop); 1975da250ea5Sdrh VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg)); 1976e55cbd72Sdrh #endif 1977da250ea5Sdrh return p->iReg; 1978e55cbd72Sdrh } 1979e55cbd72Sdrh } 1980e55cbd72Sdrh assert( v!=0 ); 1981945498f3Sdrh if( iColumn<0 ){ 1982945498f3Sdrh int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; 19832133d822Sdrh sqlite3VdbeAddOp2(v, op, iTable, iReg); 1984945498f3Sdrh }else if( pTab==0 ){ 19852133d822Sdrh sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg); 1986945498f3Sdrh }else{ 1987945498f3Sdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 19882133d822Sdrh sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); 1989945498f3Sdrh sqlite3ColumnDefault(v, pTab, iColumn); 1990945498f3Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 1991945498f3Sdrh if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ 19922133d822Sdrh sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); 1993945498f3Sdrh } 1994945498f3Sdrh #endif 1995945498f3Sdrh } 1996e55cbd72Sdrh if( pParse->disableColCache==0 ){ 1997e55cbd72Sdrh i = pParse->iColCache; 1998da250ea5Sdrh p = &pParse->aColCache[i]; 1999da250ea5Sdrh p->iTable = iTable; 2000da250ea5Sdrh p->iColumn = iColumn; 2001da250ea5Sdrh p->iReg = iReg; 2002*c5499befSdrh p->affChange = 0; 2003e55cbd72Sdrh i++; 20042f7794c1Sdrh if( i>=ArraySize(pParse->aColCache) ) i = 0; 2005e55cbd72Sdrh if( i>pParse->nColCache ) pParse->nColCache = i; 20062f7794c1Sdrh pParse->iColCache = i; 2007e55cbd72Sdrh } 2008e55cbd72Sdrh return iReg; 2009e55cbd72Sdrh } 2010e55cbd72Sdrh 2011e55cbd72Sdrh /* 2012e55cbd72Sdrh ** Clear all column cache entries associated with the vdbe 2013e55cbd72Sdrh ** cursor with cursor number iTable. 2014e55cbd72Sdrh */ 2015e55cbd72Sdrh void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){ 2016e55cbd72Sdrh if( iTable<0 ){ 2017e55cbd72Sdrh pParse->nColCache = 0; 2018e55cbd72Sdrh pParse->iColCache = 0; 2019e55cbd72Sdrh }else{ 2020e55cbd72Sdrh int i; 2021e55cbd72Sdrh for(i=0; i<pParse->nColCache; i++){ 2022e55cbd72Sdrh if( pParse->aColCache[i].iTable==iTable ){ 2023*c5499befSdrh testcase( i==pParse->nColCache-1 ); 2024e55cbd72Sdrh pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; 2025e55cbd72Sdrh pParse->iColCache = pParse->nColCache; 2026e55cbd72Sdrh } 2027e55cbd72Sdrh } 2028da250ea5Sdrh } 2029da250ea5Sdrh } 2030e55cbd72Sdrh 2031e55cbd72Sdrh /* 2032da250ea5Sdrh ** Record the fact that an affinity change has occurred on iCount 2033da250ea5Sdrh ** registers starting with iStart. 2034e55cbd72Sdrh */ 2035da250ea5Sdrh void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 2036da250ea5Sdrh int iEnd = iStart + iCount - 1; 2037e55cbd72Sdrh int i; 2038e55cbd72Sdrh for(i=0; i<pParse->nColCache; i++){ 2039e55cbd72Sdrh int r = pParse->aColCache[i].iReg; 2040da250ea5Sdrh if( r>=iStart && r<=iEnd ){ 2041da250ea5Sdrh pParse->aColCache[i].affChange = 1; 2042e55cbd72Sdrh } 2043e55cbd72Sdrh } 2044e55cbd72Sdrh } 2045e55cbd72Sdrh 2046e55cbd72Sdrh /* 2047e55cbd72Sdrh ** Generate code to moves content from one register to another. 2048e55cbd72Sdrh ** Keep the column cache up-to-date. 2049e55cbd72Sdrh */ 2050e55cbd72Sdrh void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo){ 2051e55cbd72Sdrh int i; 2052e55cbd72Sdrh if( iFrom==iTo ) return; 2053e55cbd72Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_Move, iFrom, iTo); 2054e55cbd72Sdrh for(i=0; i<pParse->nColCache; i++){ 2055e55cbd72Sdrh if( pParse->aColCache[i].iReg==iFrom ){ 2056e55cbd72Sdrh pParse->aColCache[i].iReg = iTo; 2057e55cbd72Sdrh } 2058e55cbd72Sdrh } 2059945498f3Sdrh } 2060945498f3Sdrh 2061fec19aadSdrh /* 2062652fbf55Sdrh ** Return true if any register in the range iFrom..iTo (inclusive) 2063652fbf55Sdrh ** is used as part of the column cache. 2064652fbf55Sdrh */ 2065652fbf55Sdrh static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 2066652fbf55Sdrh int i; 2067652fbf55Sdrh for(i=0; i<pParse->nColCache; i++){ 2068652fbf55Sdrh int r = pParse->aColCache[i].iReg; 2069652fbf55Sdrh if( r>=iFrom && r<=iTo ) return 1; 2070652fbf55Sdrh } 2071652fbf55Sdrh return 0; 2072652fbf55Sdrh } 2073652fbf55Sdrh 2074652fbf55Sdrh /* 2075652fbf55Sdrh ** Theres is a value in register iCurrent. We ultimately want 2076652fbf55Sdrh ** the value to be in register iTarget. It might be that 2077652fbf55Sdrh ** iCurrent and iTarget are the same register. 2078652fbf55Sdrh ** 2079652fbf55Sdrh ** We are going to modify the value, so we need to make sure it 2080652fbf55Sdrh ** is not a cached register. If iCurrent is a cached register, 2081652fbf55Sdrh ** then try to move the value over to iTarget. If iTarget is a 2082652fbf55Sdrh ** cached register, then clear the corresponding cache line. 2083652fbf55Sdrh ** 2084652fbf55Sdrh ** Return the register that the value ends up in. 2085652fbf55Sdrh */ 2086652fbf55Sdrh int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){ 2087da250ea5Sdrh int i; 2088652fbf55Sdrh assert( pParse->pVdbe!=0 ); 2089652fbf55Sdrh if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){ 2090652fbf55Sdrh return iCurrent; 2091652fbf55Sdrh } 20922f7794c1Sdrh if( iCurrent!=iTarget ){ 2093652fbf55Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget); 20942f7794c1Sdrh } 2095da250ea5Sdrh for(i=0; i<pParse->nColCache; i++){ 2096da250ea5Sdrh if( pParse->aColCache[i].iReg==iTarget ){ 2097da250ea5Sdrh pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; 2098da250ea5Sdrh pParse->iColCache = pParse->nColCache; 2099da250ea5Sdrh } 2100da250ea5Sdrh } 2101652fbf55Sdrh return iTarget; 2102652fbf55Sdrh } 2103652fbf55Sdrh 2104652fbf55Sdrh /* 2105cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 21062dcef11bSdrh ** expression. Attempt to store the results in register "target". 21072dcef11bSdrh ** Return the register where results are stored. 2108389a1adbSdrh ** 21092dcef11bSdrh ** With this routine, there is no guaranteed that results will 21102dcef11bSdrh ** be stored in target. The result might be stored in some other 21112dcef11bSdrh ** register if it is convenient to do so. The calling function 21122dcef11bSdrh ** must check the return code and move the results to the desired 21132dcef11bSdrh ** register. 2114cce7d176Sdrh */ 2115678ccce8Sdrh int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 21162dcef11bSdrh Vdbe *v = pParse->pVdbe; /* The VM under construction */ 21172dcef11bSdrh int op; /* The opcode being coded */ 21182dcef11bSdrh int inReg = target; /* Results stored in register inReg */ 21192dcef11bSdrh int regFree1 = 0; /* If non-zero free this temporary register */ 21202dcef11bSdrh int regFree2 = 0; /* If non-zero free this temporary register */ 2121678ccce8Sdrh int r1, r2, r3, r4; /* Various register numbers */ 2122ffe07b2dSdrh 2123389a1adbSdrh assert( v!=0 || pParse->db->mallocFailed ); 21249cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 2125389a1adbSdrh if( v==0 ) return 0; 2126389a1adbSdrh 2127389a1adbSdrh if( pExpr==0 ){ 2128389a1adbSdrh op = TK_NULL; 2129389a1adbSdrh }else{ 2130f2bc013cSdrh op = pExpr->op; 2131389a1adbSdrh } 2132f2bc013cSdrh switch( op ){ 213313449892Sdrh case TK_AGG_COLUMN: { 213413449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 213513449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 213613449892Sdrh if( !pAggInfo->directMode ){ 21379de221dfSdrh assert( pCol->iMem>0 ); 21389de221dfSdrh inReg = pCol->iMem; 213913449892Sdrh break; 214013449892Sdrh }else if( pAggInfo->useSortingIdx ){ 2141389a1adbSdrh sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, 2142389a1adbSdrh pCol->iSorterColumn, target); 214313449892Sdrh break; 214413449892Sdrh } 214513449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 214613449892Sdrh } 2147967e8b73Sdrh case TK_COLUMN: { 2148ffe07b2dSdrh if( pExpr->iTable<0 ){ 2149ffe07b2dSdrh /* This only happens when coding check constraints */ 2150aa9b8963Sdrh assert( pParse->ckBase>0 ); 2151aa9b8963Sdrh inReg = pExpr->iColumn + pParse->ckBase; 2152c4a3c779Sdrh }else{ 2153*c5499befSdrh testcase( (pExpr->flags & EP_AnyAff)!=0 ); 2154e55cbd72Sdrh inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 2155da250ea5Sdrh pExpr->iColumn, pExpr->iTable, target, 2156da250ea5Sdrh pExpr->flags & EP_AnyAff); 21572282792aSdrh } 2158cce7d176Sdrh break; 2159cce7d176Sdrh } 2160cce7d176Sdrh case TK_INTEGER: { 21619de221dfSdrh codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); 2162fec19aadSdrh break; 216351e9a445Sdrh } 2164598f1340Sdrh case TK_FLOAT: { 21659de221dfSdrh codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); 2166598f1340Sdrh break; 2167598f1340Sdrh } 2168fec19aadSdrh case TK_STRING: { 21691e536953Sdanielk1977 sqlite3DequoteExpr(pParse->db, pExpr); 21709de221dfSdrh sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0, 217166a5167bSdrh (char*)pExpr->token.z, pExpr->token.n); 2172cce7d176Sdrh break; 2173cce7d176Sdrh } 2174f0863fe5Sdrh case TK_NULL: { 21759de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 2176f0863fe5Sdrh break; 2177f0863fe5Sdrh } 21785338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 2179c572ef7fSdanielk1977 case TK_BLOB: { 21806c8c6cecSdrh int n; 21816c8c6cecSdrh const char *z; 2182ca48c90fSdrh char *zBlob; 2183ca48c90fSdrh assert( pExpr->token.n>=3 ); 2184ca48c90fSdrh assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); 2185ca48c90fSdrh assert( pExpr->token.z[1]=='\'' ); 2186ca48c90fSdrh assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); 21876c8c6cecSdrh n = pExpr->token.n - 3; 21882646da7eSdrh z = (char*)pExpr->token.z + 2; 2189ca48c90fSdrh zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 2190ca48c90fSdrh sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 2191c572ef7fSdanielk1977 break; 2192c572ef7fSdanielk1977 } 21935338a5f7Sdanielk1977 #endif 219450457896Sdrh case TK_VARIABLE: { 21959de221dfSdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target); 2196895d7472Sdrh if( pExpr->token.n>1 ){ 219766a5167bSdrh sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n); 2198895d7472Sdrh } 219950457896Sdrh break; 220050457896Sdrh } 22014e0cff60Sdrh case TK_REGISTER: { 22029de221dfSdrh inReg = pExpr->iTable; 22034e0cff60Sdrh break; 22044e0cff60Sdrh } 2205487e262fSdrh #ifndef SQLITE_OMIT_CAST 2206487e262fSdrh case TK_CAST: { 2207487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 2208f0113000Sdanielk1977 int aff, to_op; 22092dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 22108a51256cSdrh aff = sqlite3AffinityType(&pExpr->token); 2211f0113000Sdanielk1977 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; 2212f0113000Sdanielk1977 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); 2213f0113000Sdanielk1977 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); 2214f0113000Sdanielk1977 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); 2215f0113000Sdanielk1977 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); 2216f0113000Sdanielk1977 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); 2217*c5499befSdrh testcase( to_op==OP_ToText ); 2218*c5499befSdrh testcase( to_op==OP_ToBlob ); 2219*c5499befSdrh testcase( to_op==OP_ToNumeric ); 2220*c5499befSdrh testcase( to_op==OP_ToInt ); 2221*c5499befSdrh testcase( to_op==OP_ToReal ); 22222dcef11bSdrh sqlite3VdbeAddOp1(v, to_op, inReg); 2223*c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 2224b3843a82Sdrh sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 2225487e262fSdrh break; 2226487e262fSdrh } 2227487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 2228c9b84a1fSdrh case TK_LT: 2229c9b84a1fSdrh case TK_LE: 2230c9b84a1fSdrh case TK_GT: 2231c9b84a1fSdrh case TK_GE: 2232c9b84a1fSdrh case TK_NE: 2233c9b84a1fSdrh case TK_EQ: { 2234f2bc013cSdrh assert( TK_LT==OP_Lt ); 2235f2bc013cSdrh assert( TK_LE==OP_Le ); 2236f2bc013cSdrh assert( TK_GT==OP_Gt ); 2237f2bc013cSdrh assert( TK_GE==OP_Ge ); 2238f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2239f2bc013cSdrh assert( TK_NE==OP_Ne ); 2240*c5499befSdrh testcase( op==TK_LT ); 2241*c5499befSdrh testcase( op==TK_LE ); 2242*c5499befSdrh testcase( op==TK_GT ); 2243*c5499befSdrh testcase( op==TK_GE ); 2244*c5499befSdrh testcase( op==TK_EQ ); 2245*c5499befSdrh testcase( op==TK_NE ); 2246da250ea5Sdrh codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, 2247da250ea5Sdrh pExpr->pRight, &r2, ®Free2); 224835573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 224935573356Sdrh r1, r2, inReg, SQLITE_STOREP2); 2250*c5499befSdrh testcase( regFree1==0 ); 2251*c5499befSdrh testcase( regFree2==0 ); 2252a37cdde0Sdanielk1977 break; 2253c9b84a1fSdrh } 2254cce7d176Sdrh case TK_AND: 2255cce7d176Sdrh case TK_OR: 2256cce7d176Sdrh case TK_PLUS: 2257cce7d176Sdrh case TK_STAR: 2258cce7d176Sdrh case TK_MINUS: 2259bf4133cbSdrh case TK_REM: 2260bf4133cbSdrh case TK_BITAND: 2261bf4133cbSdrh case TK_BITOR: 226217c40294Sdrh case TK_SLASH: 2263bf4133cbSdrh case TK_LSHIFT: 2264855eb1cfSdrh case TK_RSHIFT: 22650040077dSdrh case TK_CONCAT: { 2266f2bc013cSdrh assert( TK_AND==OP_And ); 2267f2bc013cSdrh assert( TK_OR==OP_Or ); 2268f2bc013cSdrh assert( TK_PLUS==OP_Add ); 2269f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 2270f2bc013cSdrh assert( TK_REM==OP_Remainder ); 2271f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 2272f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 2273f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 2274f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 2275f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 2276f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 2277*c5499befSdrh testcase( op==TK_AND ); 2278*c5499befSdrh testcase( op==TK_OR ); 2279*c5499befSdrh testcase( op==TK_PLUS ); 2280*c5499befSdrh testcase( op==TK_MINUS ); 2281*c5499befSdrh testcase( op==TK_REM ); 2282*c5499befSdrh testcase( op==TK_BITAND ); 2283*c5499befSdrh testcase( op==TK_BITOR ); 2284*c5499befSdrh testcase( op==TK_SLASH ); 2285*c5499befSdrh testcase( op==TK_LSHIFT ); 2286*c5499befSdrh testcase( op==TK_RSHIFT ); 2287*c5499befSdrh testcase( op==TK_CONCAT ); 22882dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 22892dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 22905b6afba9Sdrh sqlite3VdbeAddOp3(v, op, r2, r1, target); 2291*c5499befSdrh testcase( regFree1==0 ); 2292*c5499befSdrh testcase( regFree2==0 ); 22930040077dSdrh break; 22940040077dSdrh } 2295cce7d176Sdrh case TK_UMINUS: { 2296fec19aadSdrh Expr *pLeft = pExpr->pLeft; 2297fec19aadSdrh assert( pLeft ); 2298fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 2299fec19aadSdrh Token *p = &pLeft->token; 2300fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 23019de221dfSdrh codeReal(v, (char*)p->z, p->n, 1, target); 2302e6840900Sdrh }else{ 23039de221dfSdrh codeInteger(v, (char*)p->z, p->n, 1, target); 2304e6840900Sdrh } 23053c84ddffSdrh }else{ 23062dcef11bSdrh regFree1 = r1 = sqlite3GetTempReg(pParse); 23073c84ddffSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); 2308e55cbd72Sdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 23092dcef11bSdrh sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 2310*c5499befSdrh testcase( regFree2==0 ); 23113c84ddffSdrh } 23129de221dfSdrh inReg = target; 23136e142f54Sdrh break; 23146e142f54Sdrh } 2315bf4133cbSdrh case TK_BITNOT: 23166e142f54Sdrh case TK_NOT: { 2317f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 2318f2bc013cSdrh assert( TK_NOT==OP_Not ); 2319*c5499befSdrh testcase( op==TK_BITNOT ); 2320*c5499befSdrh testcase( op==TK_NOT ); 23212dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 2322*c5499befSdrh testcase( inReg==target ); 2323*c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 2324652fbf55Sdrh inReg = sqlite3ExprWritableRegister(pParse, inReg, target); 23252dcef11bSdrh sqlite3VdbeAddOp1(v, op, inReg); 2326cce7d176Sdrh break; 2327cce7d176Sdrh } 2328cce7d176Sdrh case TK_ISNULL: 2329cce7d176Sdrh case TK_NOTNULL: { 23306a288a33Sdrh int addr; 2331f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2332f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 2333*c5499befSdrh testcase( op==TK_ISNULL ); 2334*c5499befSdrh testcase( op==TK_NOTNULL ); 23359de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 23362dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2337*c5499befSdrh testcase( regFree1==0 ); 23382dcef11bSdrh addr = sqlite3VdbeAddOp1(v, op, r1); 23399de221dfSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); 23406a288a33Sdrh sqlite3VdbeJumpHere(v, addr); 2341a37cdde0Sdanielk1977 break; 2342f2bc013cSdrh } 23432282792aSdrh case TK_AGG_FUNCTION: { 234413449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 23457e56e711Sdrh if( pInfo==0 ){ 23467e56e711Sdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", 23477e56e711Sdrh &pExpr->span); 23487e56e711Sdrh }else{ 23499de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 23507e56e711Sdrh } 23512282792aSdrh break; 23522282792aSdrh } 2353b71090fdSdrh case TK_CONST_FUNC: 2354cce7d176Sdrh case TK_FUNCTION: { 2355cce7d176Sdrh ExprList *pList = pExpr->pList; 235689425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 23570bce8354Sdrh FuncDef *pDef; 23584b59ab5eSdrh int nId; 23594b59ab5eSdrh const char *zId; 236013449892Sdrh int constMask = 0; 2361682f68b0Sdanielk1977 int i; 236217435752Sdrh sqlite3 *db = pParse->db; 236317435752Sdrh u8 enc = ENC(db); 2364dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 236517435752Sdrh 2366*c5499befSdrh testcase( op==TK_CONST_FUNC ); 2367*c5499befSdrh testcase( op==TK_FUNCTION ); 23682646da7eSdrh zId = (char*)pExpr->token.z; 2369b71090fdSdrh nId = pExpr->token.n; 2370d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 23710bce8354Sdrh assert( pDef!=0 ); 2372892d3179Sdrh if( pList ){ 2373892d3179Sdrh nExpr = pList->nExpr; 23742dcef11bSdrh r1 = sqlite3GetTempRange(pParse, nExpr); 23752dcef11bSdrh sqlite3ExprCodeExprList(pParse, pList, r1); 2376892d3179Sdrh }else{ 2377d847eaadSdrh nExpr = r1 = 0; 2378892d3179Sdrh } 2379b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 2380a43fa227Sdrh /* Possibly overload the function if the first argument is 2381a43fa227Sdrh ** a virtual table column. 2382a43fa227Sdrh ** 2383a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 2384a43fa227Sdrh ** second argument, not the first, as the argument to test to 2385a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 2386a43fa227Sdrh ** the left operand of infix functions (the operand we want to 2387a43fa227Sdrh ** control overloading) ends up as the second argument to the 2388a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 2389a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 2390a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 2391a43fa227Sdrh */ 23926a03a1c5Sdrh if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ 239317435752Sdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); 23946a03a1c5Sdrh }else if( nExpr>0 ){ 239517435752Sdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); 2396b7f6f68fSdrh } 2397b7f6f68fSdrh #endif 2398682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 2399d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 240013449892Sdrh constMask |= (1<<i); 2401d02eb1fdSdanielk1977 } 2402dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 2403dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 2404dc1bdc4fSdanielk1977 } 2405dc1bdc4fSdanielk1977 } 2406dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 2407dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 240866a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 2409682f68b0Sdanielk1977 } 24102dcef11bSdrh sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, 241166a5167bSdrh (char*)pDef, P4_FUNCDEF); 241298757157Sdrh sqlite3VdbeChangeP5(v, nExpr); 24132dcef11bSdrh if( nExpr ){ 24142dcef11bSdrh sqlite3ReleaseTempRange(pParse, r1, nExpr); 24152dcef11bSdrh } 2416da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, r1, nExpr); 24176ec2733bSdrh break; 24186ec2733bSdrh } 2419fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 2420fe2093d7Sdrh case TK_EXISTS: 242119a775c2Sdrh case TK_SELECT: { 2422*c5499befSdrh testcase( op==TK_EXISTS ); 2423*c5499befSdrh testcase( op==TK_SELECT ); 242441714d6fSdrh if( pExpr->iColumn==0 ){ 2425b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 242641714d6fSdrh } 24279de221dfSdrh inReg = pExpr->iColumn; 242819a775c2Sdrh break; 242919a775c2Sdrh } 2430fef5208cSdrh case TK_IN: { 24316a288a33Sdrh int j1, j2, j3, j4, j5; 243294a11211Sdrh char affinity; 24339a96b668Sdanielk1977 int eType; 24349a96b668Sdanielk1977 24359a96b668Sdanielk1977 eType = sqlite3FindInIndex(pParse, pExpr, 0); 2436e014a838Sdanielk1977 2437e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 2438e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 243966a5167bSdrh ** P4 of OP_MakeRecord. 2440e014a838Sdanielk1977 */ 244194a11211Sdrh affinity = comparisonAffinity(pExpr); 2442e014a838Sdanielk1977 24432dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 2444e014a838Sdanielk1977 2445e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 2446e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 2447e014a838Sdanielk1977 */ 24482dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2449*c5499befSdrh testcase( regFree1==0 ); 24502dcef11bSdrh j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); 24512dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 24526a288a33Sdrh j2 = sqlite3VdbeAddOp0(v, OP_Goto); 24536a288a33Sdrh sqlite3VdbeJumpHere(v, j1); 24549a96b668Sdanielk1977 if( eType==IN_INDEX_ROWID ){ 2455678ccce8Sdrh j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, r1); 24562dcef11bSdrh j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1); 24576a288a33Sdrh j5 = sqlite3VdbeAddOp0(v, OP_Goto); 24586a288a33Sdrh sqlite3VdbeJumpHere(v, j3); 24596a288a33Sdrh sqlite3VdbeJumpHere(v, j4); 24609a96b668Sdanielk1977 }else{ 24612dcef11bSdrh r2 = regFree2 = sqlite3GetTempReg(pParse); 24621db639ceSdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); 2463da250ea5Sdrh sqlite3ExprCacheAffinityChange(pParse, r1, 1); 24642dcef11bSdrh j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2); 24659a96b668Sdanielk1977 } 24662dcef11bSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); 24676a288a33Sdrh sqlite3VdbeJumpHere(v, j2); 24686a288a33Sdrh sqlite3VdbeJumpHere(v, j5); 2469fef5208cSdrh break; 2470fef5208cSdrh } 247193758c8dSdanielk1977 #endif 24722dcef11bSdrh /* 24732dcef11bSdrh ** x BETWEEN y AND z 24742dcef11bSdrh ** 24752dcef11bSdrh ** This is equivalent to 24762dcef11bSdrh ** 24772dcef11bSdrh ** x>=y AND x<=z 24782dcef11bSdrh ** 24792dcef11bSdrh ** X is stored in pExpr->pLeft. 24802dcef11bSdrh ** Y is stored in pExpr->pList->a[0].pExpr. 24812dcef11bSdrh ** Z is stored in pExpr->pList->a[1].pExpr. 24822dcef11bSdrh */ 2483fef5208cSdrh case TK_BETWEEN: { 2484be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2485be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 2486be5c89acSdrh Expr *pRight = pLItem->pExpr; 248735573356Sdrh 2488da250ea5Sdrh codeCompareOperands(pParse, pLeft, &r1, ®Free1, 2489da250ea5Sdrh pRight, &r2, ®Free2); 2490*c5499befSdrh testcase( regFree1==0 ); 2491*c5499befSdrh testcase( regFree2==0 ); 24922dcef11bSdrh r3 = sqlite3GetTempReg(pParse); 2493678ccce8Sdrh r4 = sqlite3GetTempReg(pParse); 249435573356Sdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 249535573356Sdrh r1, r2, r3, SQLITE_STOREP2); 2496be5c89acSdrh pLItem++; 2497be5c89acSdrh pRight = pLItem->pExpr; 24982dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 24992dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 2500*c5499befSdrh testcase( regFree2==0 ); 2501678ccce8Sdrh codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); 2502678ccce8Sdrh sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); 25032dcef11bSdrh sqlite3ReleaseTempReg(pParse, r3); 2504678ccce8Sdrh sqlite3ReleaseTempReg(pParse, r4); 2505fef5208cSdrh break; 2506fef5208cSdrh } 25074f07e5fbSdrh case TK_UPLUS: { 25082dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 2509a2e00042Sdrh break; 2510a2e00042Sdrh } 25112dcef11bSdrh 25122dcef11bSdrh /* 25132dcef11bSdrh ** Form A: 25142dcef11bSdrh ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 25152dcef11bSdrh ** 25162dcef11bSdrh ** Form B: 25172dcef11bSdrh ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 25182dcef11bSdrh ** 25192dcef11bSdrh ** Form A is can be transformed into the equivalent form B as follows: 25202dcef11bSdrh ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 25212dcef11bSdrh ** WHEN x=eN THEN rN ELSE y END 25222dcef11bSdrh ** 25232dcef11bSdrh ** X (if it exists) is in pExpr->pLeft. 25242dcef11bSdrh ** Y is in pExpr->pRight. The Y is also optional. If there is no 25252dcef11bSdrh ** ELSE clause and no other term matches, then the result of the 25262dcef11bSdrh ** exprssion is NULL. 25272dcef11bSdrh ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 25282dcef11bSdrh ** 25292dcef11bSdrh ** The result of the expression is the Ri for the first matching Ei, 25302dcef11bSdrh ** or if there is no matching Ei, the ELSE term Y, or if there is 25312dcef11bSdrh ** no ELSE term, NULL. 25322dcef11bSdrh */ 253317a7f8ddSdrh case TK_CASE: { 25342dcef11bSdrh int endLabel; /* GOTO label for end of CASE stmt */ 25352dcef11bSdrh int nextCase; /* GOTO label for next WHEN clause */ 25362dcef11bSdrh int nExpr; /* 2x number of WHEN terms */ 25372dcef11bSdrh int i; /* Loop counter */ 25382dcef11bSdrh ExprList *pEList; /* List of WHEN terms */ 25392dcef11bSdrh struct ExprList_item *aListelem; /* Array of WHEN terms */ 25402dcef11bSdrh Expr opCompare; /* The X==Ei expression */ 25412dcef11bSdrh Expr cacheX; /* Cached expression X */ 25422dcef11bSdrh Expr *pX; /* The X expression */ 25432dcef11bSdrh Expr *pTest; /* X==Ei (form A) or just Ei (form B) */ 254417a7f8ddSdrh 254517a7f8ddSdrh assert(pExpr->pList); 254617a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 254717a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 2548be5c89acSdrh pEList = pExpr->pList; 2549be5c89acSdrh aListelem = pEList->a; 2550be5c89acSdrh nExpr = pEList->nExpr; 25512dcef11bSdrh endLabel = sqlite3VdbeMakeLabel(v); 25522dcef11bSdrh if( (pX = pExpr->pLeft)!=0 ){ 25532dcef11bSdrh cacheX = *pX; 2554*c5499befSdrh testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER ); 25552dcef11bSdrh cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); 2556*c5499befSdrh testcase( regFree1==0 ); 25572dcef11bSdrh cacheX.op = TK_REGISTER; 2558678ccce8Sdrh cacheX.iColumn = 0; 25592dcef11bSdrh opCompare.op = TK_EQ; 25602dcef11bSdrh opCompare.pLeft = &cacheX; 25612dcef11bSdrh pTest = &opCompare; 2562cce7d176Sdrh } 2563*c5499befSdrh pParse->disableColCache++; 2564f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 25652dcef11bSdrh if( pX ){ 25662dcef11bSdrh opCompare.pRight = aListelem[i].pExpr; 2567f5905aa7Sdrh }else{ 25682dcef11bSdrh pTest = aListelem[i].pExpr; 256917a7f8ddSdrh } 25702dcef11bSdrh nextCase = sqlite3VdbeMakeLabel(v); 2571*c5499befSdrh testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER ); 25722dcef11bSdrh sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 2573*c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 2574*c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); 25759de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 25762dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); 25772dcef11bSdrh sqlite3VdbeResolveLabel(v, nextCase); 2578f570f011Sdrh } 257917a7f8ddSdrh if( pExpr->pRight ){ 25809de221dfSdrh sqlite3ExprCode(pParse, pExpr->pRight, target); 258117a7f8ddSdrh }else{ 25829de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 258317a7f8ddSdrh } 25842dcef11bSdrh sqlite3VdbeResolveLabel(v, endLabel); 2585*c5499befSdrh assert( pParse->disableColCache>0 ); 2586*c5499befSdrh pParse->disableColCache--; 25876f34903eSdanielk1977 break; 25886f34903eSdanielk1977 } 25895338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 25906f34903eSdanielk1977 case TK_RAISE: { 25916f34903eSdanielk1977 if( !pParse->trigStack ){ 25924adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2593da93d238Sdrh "RAISE() may only be used within a trigger-program"); 2594389a1adbSdrh return 0; 25956f34903eSdanielk1977 } 2596ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 2597ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 25986f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 2599ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 26001e536953Sdanielk1977 sqlite3DequoteExpr(pParse->db, pExpr); 260166a5167bSdrh sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0, 26022646da7eSdrh (char*)pExpr->token.z, pExpr->token.n); 26036f34903eSdanielk1977 } else { 26046f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 260566a5167bSdrh sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); 260666a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 2607d4e70ebdSdrh VdbeComment((v, "raise(IGNORE)")); 26086f34903eSdanielk1977 } 2609ffe07b2dSdrh break; 261017a7f8ddSdrh } 26115338a5f7Sdanielk1977 #endif 2612ffe07b2dSdrh } 26132dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 26142dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 26152dcef11bSdrh return inReg; 26165b6afba9Sdrh } 26172dcef11bSdrh 26182dcef11bSdrh /* 26192dcef11bSdrh ** Generate code to evaluate an expression and store the results 26202dcef11bSdrh ** into a register. Return the register number where the results 26212dcef11bSdrh ** are stored. 26222dcef11bSdrh ** 26232dcef11bSdrh ** If the register is a temporary register that can be deallocated, 2624678ccce8Sdrh ** then write its number into *pReg. If the result register is not 26252dcef11bSdrh ** a temporary, then set *pReg to zero. 26262dcef11bSdrh */ 26272dcef11bSdrh int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 26282dcef11bSdrh int r1 = sqlite3GetTempReg(pParse); 26292dcef11bSdrh int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 26302dcef11bSdrh if( r2==r1 ){ 26312dcef11bSdrh *pReg = r1; 26322dcef11bSdrh }else{ 26332dcef11bSdrh sqlite3ReleaseTempReg(pParse, r1); 26342dcef11bSdrh *pReg = 0; 26352dcef11bSdrh } 26362dcef11bSdrh return r2; 26372dcef11bSdrh } 26382dcef11bSdrh 26392dcef11bSdrh /* 26402dcef11bSdrh ** Generate code that will evaluate expression pExpr and store the 26412dcef11bSdrh ** results in register target. The results are guaranteed to appear 26422dcef11bSdrh ** in register target. 26432dcef11bSdrh */ 26442dcef11bSdrh int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 26459cbf3425Sdrh int inReg; 26469cbf3425Sdrh 26479cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 26489cbf3425Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 26490e359b30Sdrh assert( pParse->pVdbe || pParse->db->mallocFailed ); 26500e359b30Sdrh if( inReg!=target && pParse->pVdbe ){ 26519cbf3425Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 265217a7f8ddSdrh } 2653389a1adbSdrh return target; 2654cce7d176Sdrh } 2655cce7d176Sdrh 2656cce7d176Sdrh /* 26572dcef11bSdrh ** Generate code that evalutes the given expression and puts the result 2658de4fcfddSdrh ** in register target. 265925303780Sdrh ** 26602dcef11bSdrh ** Also make a copy of the expression results into another "cache" register 26612dcef11bSdrh ** and modify the expression so that the next time it is evaluated, 26622dcef11bSdrh ** the result is a copy of the cache register. 26632dcef11bSdrh ** 26642dcef11bSdrh ** This routine is used for expressions that are used multiple 26652dcef11bSdrh ** times. They are evaluated once and the results of the expression 26662dcef11bSdrh ** are reused. 266725303780Sdrh */ 26682dcef11bSdrh int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 266925303780Sdrh Vdbe *v = pParse->pVdbe; 26702dcef11bSdrh int inReg; 26712dcef11bSdrh inReg = sqlite3ExprCode(pParse, pExpr, target); 2672de4fcfddSdrh assert( target>0 ); 26732dcef11bSdrh if( pExpr->op!=TK_REGISTER ){ 267425303780Sdrh int iMem; 26752dcef11bSdrh iMem = ++pParse->nMem; 26762dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); 26772dcef11bSdrh pExpr->iTable = iMem; 2678678ccce8Sdrh pExpr->iColumn = pExpr->op; 267925303780Sdrh pExpr->op = TK_REGISTER; 268025303780Sdrh } 26812dcef11bSdrh return inReg; 268225303780Sdrh } 26832dcef11bSdrh 2684678ccce8Sdrh /* 2685678ccce8Sdrh ** If pExpr is a constant expression, then evaluate the expression 2686678ccce8Sdrh ** into a register and convert the expression into a TK_REGISTER 2687678ccce8Sdrh ** expression. 2688678ccce8Sdrh */ 2689678ccce8Sdrh static int evalConstExpr(void *pArg, Expr *pExpr){ 2690678ccce8Sdrh Parse *pParse = (Parse*)pArg; 2691678ccce8Sdrh if( pExpr->op==TK_REGISTER ){ 2692678ccce8Sdrh return 1; 2693678ccce8Sdrh } 2694678ccce8Sdrh if( sqlite3ExprIsConstantNotJoin(pExpr) ){ 2695678ccce8Sdrh int r1 = ++pParse->nMem; 2696678ccce8Sdrh int r2; 2697678ccce8Sdrh r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 2698*c5499befSdrh if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1); 2699678ccce8Sdrh pExpr->iColumn = pExpr->op; 2700678ccce8Sdrh pExpr->op = TK_REGISTER; 2701678ccce8Sdrh pExpr->iTable = r2; 2702678ccce8Sdrh return 1; 2703678ccce8Sdrh } 2704678ccce8Sdrh return 0; 2705678ccce8Sdrh } 2706678ccce8Sdrh 2707678ccce8Sdrh /* 2708678ccce8Sdrh ** Preevaluate constant subexpressions within pExpr and store the 2709678ccce8Sdrh ** results in registers. Modify pExpr so that the constant subexpresions 2710678ccce8Sdrh ** are TK_REGISTER opcodes that refer to the precomputed values. 2711678ccce8Sdrh */ 2712678ccce8Sdrh void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ 2713678ccce8Sdrh walkExprTree(pExpr, evalConstExpr, pParse); 2714678ccce8Sdrh } 2715678ccce8Sdrh 271625303780Sdrh 271725303780Sdrh /* 2718268380caSdrh ** Generate code that pushes the value of every element of the given 27199cbf3425Sdrh ** expression list into a sequence of registers beginning at target. 2720268380caSdrh ** 2721892d3179Sdrh ** Return the number of elements evaluated. 2722268380caSdrh */ 27234adee20fSdanielk1977 int sqlite3ExprCodeExprList( 2724268380caSdrh Parse *pParse, /* Parsing context */ 2725389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 2726389a1adbSdrh int target /* Where to write results */ 2727268380caSdrh ){ 2728268380caSdrh struct ExprList_item *pItem; 27299cbf3425Sdrh int i, n; 2730892d3179Sdrh assert( pList!=0 || pParse->db->mallocFailed ); 2731892d3179Sdrh if( pList==0 ){ 2732892d3179Sdrh return 0; 2733892d3179Sdrh } 27349cbf3425Sdrh assert( target>0 ); 2735268380caSdrh n = pList->nExpr; 2736c182d163Sdrh for(pItem=pList->a, i=n; i>0; i--, pItem++){ 2737389a1adbSdrh sqlite3ExprCode(pParse, pItem->pExpr, target); 27389cbf3425Sdrh target++; 2739268380caSdrh } 2740f9b596ebSdrh return n; 2741268380caSdrh } 2742268380caSdrh 2743268380caSdrh /* 2744cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 2745cce7d176Sdrh ** to the label "dest" if the expression is true but execution 2746cce7d176Sdrh ** continues straight thru if the expression is false. 2747f5905aa7Sdrh ** 2748f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 274935573356Sdrh ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 2750f2bc013cSdrh ** 2751f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 2752f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 2753f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 2754f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 2755f2bc013cSdrh ** below verify that the numbers are aligned correctly. 2756cce7d176Sdrh */ 27574adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2758cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2759cce7d176Sdrh int op = 0; 27602dcef11bSdrh int regFree1 = 0; 27612dcef11bSdrh int regFree2 = 0; 27622dcef11bSdrh int r1, r2; 27632dcef11bSdrh 276435573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 2765daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2766f2bc013cSdrh op = pExpr->op; 2767f2bc013cSdrh switch( op ){ 2768cce7d176Sdrh case TK_AND: { 27694adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 2770*c5499befSdrh testcase( jumpIfNull==0 ); 2771*c5499befSdrh testcase( pParse->disableColCache==0 ); 277235573356Sdrh sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 2773e55cbd72Sdrh pParse->disableColCache++; 27744adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 2775*c5499befSdrh assert( pParse->disableColCache>0 ); 2776e55cbd72Sdrh pParse->disableColCache--; 27774adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2778cce7d176Sdrh break; 2779cce7d176Sdrh } 2780cce7d176Sdrh case TK_OR: { 2781*c5499befSdrh testcase( jumpIfNull==0 ); 2782*c5499befSdrh testcase( pParse->disableColCache==0 ); 27834adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 2784e55cbd72Sdrh pParse->disableColCache++; 27854adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 2786*c5499befSdrh assert( pParse->disableColCache>0 ); 2787e55cbd72Sdrh pParse->disableColCache--; 2788cce7d176Sdrh break; 2789cce7d176Sdrh } 2790cce7d176Sdrh case TK_NOT: { 2791*c5499befSdrh testcase( jumpIfNull==0 ); 27924adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 2793cce7d176Sdrh break; 2794cce7d176Sdrh } 2795cce7d176Sdrh case TK_LT: 2796cce7d176Sdrh case TK_LE: 2797cce7d176Sdrh case TK_GT: 2798cce7d176Sdrh case TK_GE: 2799cce7d176Sdrh case TK_NE: 28000ac65892Sdrh case TK_EQ: { 2801f2bc013cSdrh assert( TK_LT==OP_Lt ); 2802f2bc013cSdrh assert( TK_LE==OP_Le ); 2803f2bc013cSdrh assert( TK_GT==OP_Gt ); 2804f2bc013cSdrh assert( TK_GE==OP_Ge ); 2805f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2806f2bc013cSdrh assert( TK_NE==OP_Ne ); 2807*c5499befSdrh testcase( op==TK_LT ); 2808*c5499befSdrh testcase( op==TK_LE ); 2809*c5499befSdrh testcase( op==TK_GT ); 2810*c5499befSdrh testcase( op==TK_GE ); 2811*c5499befSdrh testcase( op==TK_EQ ); 2812*c5499befSdrh testcase( op==TK_NE ); 2813*c5499befSdrh testcase( jumpIfNull==0 ); 2814da250ea5Sdrh codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, 2815da250ea5Sdrh pExpr->pRight, &r2, ®Free2); 281635573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 28172dcef11bSdrh r1, r2, dest, jumpIfNull); 2818*c5499befSdrh testcase( regFree1==0 ); 2819*c5499befSdrh testcase( regFree2==0 ); 2820cce7d176Sdrh break; 2821cce7d176Sdrh } 2822cce7d176Sdrh case TK_ISNULL: 2823cce7d176Sdrh case TK_NOTNULL: { 2824f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2825f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 2826*c5499befSdrh testcase( op==TK_ISNULL ); 2827*c5499befSdrh testcase( op==TK_NOTNULL ); 28282dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 28292dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 2830*c5499befSdrh testcase( regFree1==0 ); 2831cce7d176Sdrh break; 2832cce7d176Sdrh } 2833fef5208cSdrh case TK_BETWEEN: { 28342dcef11bSdrh /* x BETWEEN y AND z 28350202b29eSdanielk1977 ** 28362dcef11bSdrh ** Is equivalent to 28372dcef11bSdrh ** 28382dcef11bSdrh ** x>=y AND x<=z 28392dcef11bSdrh ** 28402dcef11bSdrh ** Code it as such, taking care to do the common subexpression 28412dcef11bSdrh ** elementation of x. 28420202b29eSdanielk1977 */ 28432dcef11bSdrh Expr exprAnd; 28442dcef11bSdrh Expr compLeft; 28452dcef11bSdrh Expr compRight; 28462dcef11bSdrh Expr exprX; 28470202b29eSdanielk1977 28482dcef11bSdrh exprX = *pExpr->pLeft; 28492dcef11bSdrh exprAnd.op = TK_AND; 28502dcef11bSdrh exprAnd.pLeft = &compLeft; 28512dcef11bSdrh exprAnd.pRight = &compRight; 28522dcef11bSdrh compLeft.op = TK_GE; 28532dcef11bSdrh compLeft.pLeft = &exprX; 28542dcef11bSdrh compLeft.pRight = pExpr->pList->a[0].pExpr; 28552dcef11bSdrh compRight.op = TK_LE; 28562dcef11bSdrh compRight.pLeft = &exprX; 28572dcef11bSdrh compRight.pRight = pExpr->pList->a[1].pExpr; 28582dcef11bSdrh exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); 2859*c5499befSdrh testcase( regFree1==0 ); 28602dcef11bSdrh exprX.op = TK_REGISTER; 2861*c5499befSdrh testcase( jumpIfNull==0 ); 28622dcef11bSdrh sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); 2863fef5208cSdrh break; 2864fef5208cSdrh } 2865cce7d176Sdrh default: { 28662dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 28672dcef11bSdrh sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 2868*c5499befSdrh testcase( regFree1==0 ); 2869*c5499befSdrh testcase( jumpIfNull==0 ); 2870cce7d176Sdrh break; 2871cce7d176Sdrh } 2872cce7d176Sdrh } 28732dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 28742dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 2875cce7d176Sdrh } 2876cce7d176Sdrh 2877cce7d176Sdrh /* 287866b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 2879cce7d176Sdrh ** to the label "dest" if the expression is false but execution 2880cce7d176Sdrh ** continues straight thru if the expression is true. 2881f5905aa7Sdrh ** 2882f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 288335573356Sdrh ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 288435573356Sdrh ** is 0. 2885cce7d176Sdrh */ 28864adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2887cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2888cce7d176Sdrh int op = 0; 28892dcef11bSdrh int regFree1 = 0; 28902dcef11bSdrh int regFree2 = 0; 28912dcef11bSdrh int r1, r2; 28922dcef11bSdrh 289335573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 2894daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2895f2bc013cSdrh 2896f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 2897f2bc013cSdrh ** 2898f2bc013cSdrh ** pExpr->op op 2899f2bc013cSdrh ** --------- ---------- 2900f2bc013cSdrh ** TK_ISNULL OP_NotNull 2901f2bc013cSdrh ** TK_NOTNULL OP_IsNull 2902f2bc013cSdrh ** TK_NE OP_Eq 2903f2bc013cSdrh ** TK_EQ OP_Ne 2904f2bc013cSdrh ** TK_GT OP_Le 2905f2bc013cSdrh ** TK_LE OP_Gt 2906f2bc013cSdrh ** TK_GE OP_Lt 2907f2bc013cSdrh ** TK_LT OP_Ge 2908f2bc013cSdrh ** 2909f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 2910f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 2911f2bc013cSdrh ** can compute the mapping above using the following expression. 2912f2bc013cSdrh ** Assert()s verify that the computation is correct. 2913f2bc013cSdrh */ 2914f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 2915f2bc013cSdrh 2916f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 2917f2bc013cSdrh */ 2918f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 2919f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 2920f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 2921f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 2922f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 2923f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 2924f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 2925f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 2926f2bc013cSdrh 2927cce7d176Sdrh switch( pExpr->op ){ 2928cce7d176Sdrh case TK_AND: { 2929*c5499befSdrh testcase( jumpIfNull==0 ); 2930*c5499befSdrh testcase( pParse->disableColCache==0 ); 29314adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 2932e55cbd72Sdrh pParse->disableColCache++; 29334adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 2934*c5499befSdrh assert( pParse->disableColCache>0 ); 2935e55cbd72Sdrh pParse->disableColCache--; 2936cce7d176Sdrh break; 2937cce7d176Sdrh } 2938cce7d176Sdrh case TK_OR: { 29394adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 2940*c5499befSdrh testcase( jumpIfNull==0 ); 2941*c5499befSdrh testcase( pParse->disableColCache==0 ); 294235573356Sdrh sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 2943e55cbd72Sdrh pParse->disableColCache++; 29444adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 2945*c5499befSdrh assert( pParse->disableColCache>0 ); 2946e55cbd72Sdrh pParse->disableColCache--; 29474adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2948cce7d176Sdrh break; 2949cce7d176Sdrh } 2950cce7d176Sdrh case TK_NOT: { 29514adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 2952cce7d176Sdrh break; 2953cce7d176Sdrh } 2954cce7d176Sdrh case TK_LT: 2955cce7d176Sdrh case TK_LE: 2956cce7d176Sdrh case TK_GT: 2957cce7d176Sdrh case TK_GE: 2958cce7d176Sdrh case TK_NE: 2959cce7d176Sdrh case TK_EQ: { 2960*c5499befSdrh testcase( op==TK_LT ); 2961*c5499befSdrh testcase( op==TK_LE ); 2962*c5499befSdrh testcase( op==TK_GT ); 2963*c5499befSdrh testcase( op==TK_GE ); 2964*c5499befSdrh testcase( op==TK_EQ ); 2965*c5499befSdrh testcase( op==TK_NE ); 2966*c5499befSdrh testcase( jumpIfNull==0 ); 2967da250ea5Sdrh codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, 2968da250ea5Sdrh pExpr->pRight, &r2, ®Free2); 296935573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 29702dcef11bSdrh r1, r2, dest, jumpIfNull); 2971*c5499befSdrh testcase( regFree1==0 ); 2972*c5499befSdrh testcase( regFree2==0 ); 2973cce7d176Sdrh break; 2974cce7d176Sdrh } 2975cce7d176Sdrh case TK_ISNULL: 2976cce7d176Sdrh case TK_NOTNULL: { 2977*c5499befSdrh testcase( op==TK_ISNULL ); 2978*c5499befSdrh testcase( op==TK_NOTNULL ); 29792dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 29802dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 2981*c5499befSdrh testcase( regFree1==0 ); 2982cce7d176Sdrh break; 2983cce7d176Sdrh } 2984fef5208cSdrh case TK_BETWEEN: { 29852dcef11bSdrh /* x BETWEEN y AND z 29860202b29eSdanielk1977 ** 29872dcef11bSdrh ** Is equivalent to 29882dcef11bSdrh ** 29892dcef11bSdrh ** x>=y AND x<=z 29902dcef11bSdrh ** 29912dcef11bSdrh ** Code it as such, taking care to do the common subexpression 29922dcef11bSdrh ** elementation of x. 29930202b29eSdanielk1977 */ 29942dcef11bSdrh Expr exprAnd; 29952dcef11bSdrh Expr compLeft; 29962dcef11bSdrh Expr compRight; 29972dcef11bSdrh Expr exprX; 2998be5c89acSdrh 29992dcef11bSdrh exprX = *pExpr->pLeft; 30002dcef11bSdrh exprAnd.op = TK_AND; 30012dcef11bSdrh exprAnd.pLeft = &compLeft; 30022dcef11bSdrh exprAnd.pRight = &compRight; 30032dcef11bSdrh compLeft.op = TK_GE; 30042dcef11bSdrh compLeft.pLeft = &exprX; 30052dcef11bSdrh compLeft.pRight = pExpr->pList->a[0].pExpr; 30062dcef11bSdrh compRight.op = TK_LE; 30072dcef11bSdrh compRight.pLeft = &exprX; 30082dcef11bSdrh compRight.pRight = pExpr->pList->a[1].pExpr; 30092dcef11bSdrh exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); 3010*c5499befSdrh testcase( regFree1==0 ); 30112dcef11bSdrh exprX.op = TK_REGISTER; 3012*c5499befSdrh testcase( jumpIfNull==0 ); 30132dcef11bSdrh sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); 3014fef5208cSdrh break; 3015fef5208cSdrh } 3016cce7d176Sdrh default: { 30172dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 30182dcef11bSdrh sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 3019*c5499befSdrh testcase( regFree1==0 ); 3020*c5499befSdrh testcase( jumpIfNull==0 ); 3021cce7d176Sdrh break; 3022cce7d176Sdrh } 3023cce7d176Sdrh } 30242dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 30252dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 3026cce7d176Sdrh } 30272282792aSdrh 30282282792aSdrh /* 30292282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 30302282792aSdrh ** if they are identical and return FALSE if they differ in any way. 3031d40aab0eSdrh ** 3032d40aab0eSdrh ** Sometimes this routine will return FALSE even if the two expressions 3033d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 3034d40aab0eSdrh ** identical, we return FALSE just to be safe. So if this routine 3035d40aab0eSdrh ** returns false, then you do not really know for certain if the two 3036d40aab0eSdrh ** expressions are the same. But if you get a TRUE return, then you 3037d40aab0eSdrh ** can be sure the expressions are the same. In the places where 3038d40aab0eSdrh ** this routine is used, it does not hurt to get an extra FALSE - that 3039d40aab0eSdrh ** just might result in some slightly slower code. But returning 3040d40aab0eSdrh ** an incorrect TRUE could lead to a malfunction. 30412282792aSdrh */ 30424adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 30432282792aSdrh int i; 30444b202ae2Sdanielk1977 if( pA==0||pB==0 ){ 30454b202ae2Sdanielk1977 return pB==pA; 30462282792aSdrh } 30472282792aSdrh if( pA->op!=pB->op ) return 0; 3048fd357974Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; 30494adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 30504adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 30512282792aSdrh if( pA->pList ){ 30522282792aSdrh if( pB->pList==0 ) return 0; 30532282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 30542282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 30554adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 30562282792aSdrh return 0; 30572282792aSdrh } 30582282792aSdrh } 30592282792aSdrh }else if( pB->pList ){ 30602282792aSdrh return 0; 30612282792aSdrh } 30622282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 30632f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 3064dd73521bSdrh if( pA->op!=TK_COLUMN && pA->token.z ){ 30652282792aSdrh if( pB->token.z==0 ) return 0; 30666977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 30672646da7eSdrh if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ 30682646da7eSdrh return 0; 30692646da7eSdrh } 30702282792aSdrh } 30712282792aSdrh return 1; 30722282792aSdrh } 30732282792aSdrh 307413449892Sdrh 30752282792aSdrh /* 307613449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 307713449892Sdrh ** the new element. Return a negative number if malloc fails. 30782282792aSdrh */ 307917435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 308013449892Sdrh int i; 3081cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 308217435752Sdrh db, 3083cf643729Sdrh pInfo->aCol, 3084cf643729Sdrh sizeof(pInfo->aCol[0]), 3085cf643729Sdrh 3, 3086cf643729Sdrh &pInfo->nColumn, 3087cf643729Sdrh &pInfo->nColumnAlloc, 3088cf643729Sdrh &i 3089cf643729Sdrh ); 309013449892Sdrh return i; 30912282792aSdrh } 309213449892Sdrh 309313449892Sdrh /* 309413449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 309513449892Sdrh ** the new element. Return a negative number if malloc fails. 309613449892Sdrh */ 309717435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 309813449892Sdrh int i; 3099cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 310017435752Sdrh db, 3101cf643729Sdrh pInfo->aFunc, 3102cf643729Sdrh sizeof(pInfo->aFunc[0]), 3103cf643729Sdrh 3, 3104cf643729Sdrh &pInfo->nFunc, 3105cf643729Sdrh &pInfo->nFuncAlloc, 3106cf643729Sdrh &i 3107cf643729Sdrh ); 310813449892Sdrh return i; 31092282792aSdrh } 31102282792aSdrh 31112282792aSdrh /* 3112626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 3113626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 3114626a879aSdrh ** for additional information. 31152282792aSdrh ** 3116626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 31172282792aSdrh */ 3118626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 31192282792aSdrh int i; 3120a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 3121a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 3122a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 312313449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 312413449892Sdrh 31252282792aSdrh switch( pExpr->op ){ 312689c69d00Sdrh case TK_AGG_COLUMN: 3127967e8b73Sdrh case TK_COLUMN: { 312813449892Sdrh /* Check to see if the column is in one of the tables in the FROM 312913449892Sdrh ** clause of the aggregate query */ 313013449892Sdrh if( pSrcList ){ 313113449892Sdrh struct SrcList_item *pItem = pSrcList->a; 313213449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 313313449892Sdrh struct AggInfo_col *pCol; 313413449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 313513449892Sdrh /* If we reach this point, it means that pExpr refers to a table 313613449892Sdrh ** that is in the FROM clause of the aggregate query. 313713449892Sdrh ** 313813449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 313913449892Sdrh ** is not an entry there already. 314013449892Sdrh */ 31417f906d63Sdrh int k; 314213449892Sdrh pCol = pAggInfo->aCol; 31437f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 314413449892Sdrh if( pCol->iTable==pExpr->iTable && 314513449892Sdrh pCol->iColumn==pExpr->iColumn ){ 31462282792aSdrh break; 31472282792aSdrh } 31482282792aSdrh } 31491e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 31501e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 31511e536953Sdanielk1977 ){ 31527f906d63Sdrh pCol = &pAggInfo->aCol[k]; 31530817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 315413449892Sdrh pCol->iTable = pExpr->iTable; 315513449892Sdrh pCol->iColumn = pExpr->iColumn; 31560a07c107Sdrh pCol->iMem = ++pParse->nMem; 315713449892Sdrh pCol->iSorterColumn = -1; 31585774b806Sdrh pCol->pExpr = pExpr; 315913449892Sdrh if( pAggInfo->pGroupBy ){ 316013449892Sdrh int j, n; 316113449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 316213449892Sdrh struct ExprList_item *pTerm = pGB->a; 316313449892Sdrh n = pGB->nExpr; 316413449892Sdrh for(j=0; j<n; j++, pTerm++){ 316513449892Sdrh Expr *pE = pTerm->pExpr; 316613449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 316713449892Sdrh pE->iColumn==pExpr->iColumn ){ 316813449892Sdrh pCol->iSorterColumn = j; 316913449892Sdrh break; 31702282792aSdrh } 317113449892Sdrh } 317213449892Sdrh } 317313449892Sdrh if( pCol->iSorterColumn<0 ){ 317413449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 317513449892Sdrh } 317613449892Sdrh } 317713449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 317813449892Sdrh ** because it was there before or because we just created it). 317913449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 318013449892Sdrh ** pAggInfo->aCol[] entry. 318113449892Sdrh */ 318213449892Sdrh pExpr->pAggInfo = pAggInfo; 318313449892Sdrh pExpr->op = TK_AGG_COLUMN; 31847f906d63Sdrh pExpr->iAgg = k; 318513449892Sdrh break; 318613449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 318713449892Sdrh } /* end loop over pSrcList */ 3188a58fdfb1Sdanielk1977 } 3189626a879aSdrh return 1; 31902282792aSdrh } 31912282792aSdrh case TK_AGG_FUNCTION: { 319213449892Sdrh /* The pNC->nDepth==0 test causes aggregate functions in subqueries 319313449892Sdrh ** to be ignored */ 3194a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 319513449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 319613449892Sdrh ** function that is already in the pAggInfo structure 319713449892Sdrh */ 319813449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 319913449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 320013449892Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ 32012282792aSdrh break; 32022282792aSdrh } 32032282792aSdrh } 320413449892Sdrh if( i>=pAggInfo->nFunc ){ 320513449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 320613449892Sdrh */ 320714db2665Sdanielk1977 u8 enc = ENC(pParse->db); 32081e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 320913449892Sdrh if( i>=0 ){ 321013449892Sdrh pItem = &pAggInfo->aFunc[i]; 321113449892Sdrh pItem->pExpr = pExpr; 32120a07c107Sdrh pItem->iMem = ++pParse->nMem; 321313449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 32142646da7eSdrh (char*)pExpr->token.z, pExpr->token.n, 3215d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 3216fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 3217fd357974Sdrh pItem->iDistinct = pParse->nTab++; 3218fd357974Sdrh }else{ 3219fd357974Sdrh pItem->iDistinct = -1; 3220fd357974Sdrh } 32212282792aSdrh } 322213449892Sdrh } 322313449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 322413449892Sdrh */ 32252282792aSdrh pExpr->iAgg = i; 322613449892Sdrh pExpr->pAggInfo = pAggInfo; 3227626a879aSdrh return 1; 32282282792aSdrh } 32292282792aSdrh } 3230a58fdfb1Sdanielk1977 } 323113449892Sdrh 323213449892Sdrh /* Recursively walk subqueries looking for TK_COLUMN nodes that need 323313449892Sdrh ** to be changed to TK_AGG_COLUMN. But increment nDepth so that 323413449892Sdrh ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. 323513449892Sdrh */ 3236a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 3237a58fdfb1Sdanielk1977 pNC->nDepth++; 3238a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 3239a58fdfb1Sdanielk1977 pNC->nDepth--; 3240a58fdfb1Sdanielk1977 } 3241626a879aSdrh return 0; 32422282792aSdrh } 3243626a879aSdrh 3244626a879aSdrh /* 3245626a879aSdrh ** Analyze the given expression looking for aggregate functions and 3246626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 3247626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 3248626a879aSdrh ** 3249626a879aSdrh ** This routine should only be called after the expression has been 3250626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 3251626a879aSdrh */ 3252d2b3e23bSdrh void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 3253a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 32542282792aSdrh } 32555d9a4af9Sdrh 32565d9a4af9Sdrh /* 32575d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 32585d9a4af9Sdrh ** expression list. Return the number of errors. 32595d9a4af9Sdrh ** 32605d9a4af9Sdrh ** If an error is found, the analysis is cut short. 32615d9a4af9Sdrh */ 3262d2b3e23bSdrh void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 32635d9a4af9Sdrh struct ExprList_item *pItem; 32645d9a4af9Sdrh int i; 32655d9a4af9Sdrh if( pList ){ 3266d2b3e23bSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 3267d2b3e23bSdrh sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 32685d9a4af9Sdrh } 32695d9a4af9Sdrh } 32705d9a4af9Sdrh } 3271892d3179Sdrh 3272892d3179Sdrh /* 3273892d3179Sdrh ** Allocate or deallocate temporary use registers during code generation. 3274892d3179Sdrh */ 3275892d3179Sdrh int sqlite3GetTempReg(Parse *pParse){ 3276e55cbd72Sdrh int i, r; 3277e55cbd72Sdrh if( pParse->nTempReg==0 ){ 3278892d3179Sdrh return ++pParse->nMem; 3279892d3179Sdrh } 3280e55cbd72Sdrh for(i=0; i<pParse->nTempReg; i++){ 3281e55cbd72Sdrh r = pParse->aTempReg[i]; 3282e55cbd72Sdrh if( usedAsColumnCache(pParse, r, r) ) continue; 3283e55cbd72Sdrh } 3284e55cbd72Sdrh if( i>=pParse->nTempReg ){ 3285e55cbd72Sdrh return ++pParse->nMem; 3286e55cbd72Sdrh } 3287e55cbd72Sdrh while( i<pParse->nTempReg-1 ){ 3288e55cbd72Sdrh pParse->aTempReg[i] = pParse->aTempReg[i+1]; 3289e55cbd72Sdrh } 3290e55cbd72Sdrh pParse->nTempReg--; 3291e55cbd72Sdrh return r; 3292892d3179Sdrh } 3293892d3179Sdrh void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 32942dcef11bSdrh if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 3295892d3179Sdrh pParse->aTempReg[pParse->nTempReg++] = iReg; 3296892d3179Sdrh } 3297892d3179Sdrh } 3298892d3179Sdrh 3299892d3179Sdrh /* 3300892d3179Sdrh ** Allocate or deallocate a block of nReg consecutive registers 3301892d3179Sdrh */ 3302892d3179Sdrh int sqlite3GetTempRange(Parse *pParse, int nReg){ 3303e55cbd72Sdrh int i, n; 3304892d3179Sdrh i = pParse->iRangeReg; 3305e55cbd72Sdrh n = pParse->nRangeReg; 3306e55cbd72Sdrh if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){ 3307892d3179Sdrh pParse->iRangeReg += nReg; 3308892d3179Sdrh pParse->nRangeReg -= nReg; 3309892d3179Sdrh }else{ 3310892d3179Sdrh i = pParse->nMem+1; 3311892d3179Sdrh pParse->nMem += nReg; 3312892d3179Sdrh } 3313892d3179Sdrh return i; 3314892d3179Sdrh } 3315892d3179Sdrh void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 3316892d3179Sdrh if( nReg>pParse->nRangeReg ){ 3317892d3179Sdrh pParse->nRangeReg = nReg; 3318892d3179Sdrh pParse->iRangeReg = iReg; 3319892d3179Sdrh } 3320892d3179Sdrh } 3321