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*9de221dfSdrh ** $Id: expr.c,v 1.333 2008/01/05 06:51:30 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 /* 161a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 162a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 163a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 164a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 165a37cdde0Sdanielk1977 ** evaluates to NULL. 166a37cdde0Sdanielk1977 */ 167e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 168bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 169f0863fe5Sdrh return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0); 170a37cdde0Sdanielk1977 } 171a37cdde0Sdanielk1977 172a2e00042Sdrh /* 1730202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1740202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1750202b29eSdanielk1977 ** 1760202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1770202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1780202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1790202b29eSdanielk1977 ** type. 180bcbb04e5Sdanielk1977 ** 181bcbb04e5Sdanielk1977 ** Argument pRight (but not pLeft) may be a null pointer. In this case, 182bcbb04e5Sdanielk1977 ** it is not considered. 1830202b29eSdanielk1977 */ 184bcbb04e5Sdanielk1977 CollSeq *sqlite3BinaryCompareCollSeq( 185bcbb04e5Sdanielk1977 Parse *pParse, 186bcbb04e5Sdanielk1977 Expr *pLeft, 187bcbb04e5Sdanielk1977 Expr *pRight 188bcbb04e5Sdanielk1977 ){ 189ec41ddacSdrh CollSeq *pColl; 190ec41ddacSdrh assert( pLeft ); 191ec41ddacSdrh if( pLeft->flags & EP_ExpCollate ){ 192ec41ddacSdrh assert( pLeft->pColl ); 193ec41ddacSdrh pColl = pLeft->pColl; 194bcbb04e5Sdanielk1977 }else if( pRight && pRight->flags & EP_ExpCollate ){ 195ec41ddacSdrh assert( pRight->pColl ); 196ec41ddacSdrh pColl = pRight->pColl; 197ec41ddacSdrh }else{ 198ec41ddacSdrh pColl = sqlite3ExprCollSeq(pParse, pLeft); 1990202b29eSdanielk1977 if( !pColl ){ 2007cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 2010202b29eSdanielk1977 } 202ec41ddacSdrh } 2030202b29eSdanielk1977 return pColl; 2040202b29eSdanielk1977 } 2050202b29eSdanielk1977 2060202b29eSdanielk1977 /* 207be5c89acSdrh ** Generate code for a comparison operator. 208be5c89acSdrh */ 209be5c89acSdrh static int codeCompare( 210be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 211be5c89acSdrh Expr *pLeft, /* The left operand */ 212be5c89acSdrh Expr *pRight, /* The right operand */ 213be5c89acSdrh int opcode, /* The comparison opcode */ 214be5c89acSdrh int dest, /* Jump here if true. */ 215be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 216be5c89acSdrh ){ 217be5c89acSdrh int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); 218bcbb04e5Sdanielk1977 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); 21966a5167bSdrh return sqlite3VdbeAddOp4(pParse->pVdbe, opcode, p1, dest, 0, 22066a5167bSdrh (void*)p3, P4_COLLSEQ); 221be5c89acSdrh } 222be5c89acSdrh 223be5c89acSdrh /* 224a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 22517435752Sdrh ** for this node is obtained from sqlite3_malloc(). The calling function 226a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 227a76b5dfcSdrh */ 22817435752Sdrh Expr *sqlite3Expr( 229a1644fd8Sdanielk1977 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 23017435752Sdrh int op, /* Expression opcode */ 23117435752Sdrh Expr *pLeft, /* Left operand */ 23217435752Sdrh Expr *pRight, /* Right operand */ 23317435752Sdrh const Token *pToken /* Argument token */ 23417435752Sdrh ){ 235a76b5dfcSdrh Expr *pNew; 236a1644fd8Sdanielk1977 pNew = sqlite3DbMallocZero(db, sizeof(Expr)); 237a76b5dfcSdrh if( pNew==0 ){ 238d5d56523Sdanielk1977 /* When malloc fails, delete pLeft and pRight. Expressions passed to 239d5d56523Sdanielk1977 ** this function must always be allocated with sqlite3Expr() for this 240d5d56523Sdanielk1977 ** reason. 241d5d56523Sdanielk1977 */ 242d5d56523Sdanielk1977 sqlite3ExprDelete(pLeft); 243d5d56523Sdanielk1977 sqlite3ExprDelete(pRight); 244a76b5dfcSdrh return 0; 245a76b5dfcSdrh } 246a76b5dfcSdrh pNew->op = op; 247a76b5dfcSdrh pNew->pLeft = pLeft; 248a76b5dfcSdrh pNew->pRight = pRight; 249a58fdfb1Sdanielk1977 pNew->iAgg = -1; 250a76b5dfcSdrh if( pToken ){ 2514b59ab5eSdrh assert( pToken->dyn==0 ); 252145716b3Sdrh pNew->span = pNew->token = *pToken; 253a34001c9Sdrh }else if( pLeft ){ 254a34001c9Sdrh if( pRight ){ 2554adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 2565ffb3ac8Sdrh if( pRight->flags & EP_ExpCollate ){ 257a34001c9Sdrh pNew->flags |= EP_ExpCollate; 258a34001c9Sdrh pNew->pColl = pRight->pColl; 259a34001c9Sdrh } 260a34001c9Sdrh } 2615ffb3ac8Sdrh if( pLeft->flags & EP_ExpCollate ){ 262a34001c9Sdrh pNew->flags |= EP_ExpCollate; 263a34001c9Sdrh pNew->pColl = pLeft->pColl; 264a34001c9Sdrh } 265a76b5dfcSdrh } 266fc976065Sdanielk1977 267fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 268a76b5dfcSdrh return pNew; 269a76b5dfcSdrh } 270a76b5dfcSdrh 271a76b5dfcSdrh /* 27217435752Sdrh ** Works like sqlite3Expr() except that it takes an extra Parse* 27317435752Sdrh ** argument and notifies the associated connection object if malloc fails. 274206f3d96Sdrh */ 27517435752Sdrh Expr *sqlite3PExpr( 27617435752Sdrh Parse *pParse, /* Parsing context */ 27717435752Sdrh int op, /* Expression opcode */ 27817435752Sdrh Expr *pLeft, /* Left operand */ 27917435752Sdrh Expr *pRight, /* Right operand */ 28017435752Sdrh const Token *pToken /* Argument token */ 28117435752Sdrh ){ 282a1644fd8Sdanielk1977 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); 283206f3d96Sdrh } 284206f3d96Sdrh 285206f3d96Sdrh /* 2864e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 2874e0cff60Sdrh ** that look like this: #0 #1 #2 ... These terms refer to elements 288288d37f1Sdrh ** on the stack. "#0" means the top of the stack. 289288d37f1Sdrh ** "#1" means the next down on the stack. And so forth. 2904e0cff60Sdrh ** 2914e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 2924e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 2934e0cff60Sdrh ** The returns an expression that will code to extract the value from 2944e0cff60Sdrh ** that memory location as needed. 2954e0cff60Sdrh */ 2964e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 2974e0cff60Sdrh Vdbe *v = pParse->pVdbe; 2984e0cff60Sdrh Expr *p; 2994e0cff60Sdrh int depth; 3004e0cff60Sdrh if( pParse->nested==0 ){ 3014e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 302a1644fd8Sdanielk1977 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); 3034e0cff60Sdrh } 304bb7ac00bSdrh if( v==0 ) return 0; 305a1644fd8Sdanielk1977 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); 30673c42a13Sdrh if( p==0 ){ 30773c42a13Sdrh return 0; /* Malloc failed */ 30873c42a13Sdrh } 3092646da7eSdrh depth = atoi((char*)&pToken->z[1]); 3100a07c107Sdrh p->iTable = ++pParse->nMem; 311b1fdb2adSdrh sqlite3VdbeAddOp2(v, OP_Copy, -depth, p->iTable); 3124e0cff60Sdrh return p; 3134e0cff60Sdrh } 3144e0cff60Sdrh 3154e0cff60Sdrh /* 31691bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 31791bb0eedSdrh ** NULL, then just return the other expression. 31891bb0eedSdrh */ 3191e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 32091bb0eedSdrh if( pLeft==0 ){ 32191bb0eedSdrh return pRight; 32291bb0eedSdrh }else if( pRight==0 ){ 32391bb0eedSdrh return pLeft; 32491bb0eedSdrh }else{ 325880c15beSdanielk1977 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0); 32691bb0eedSdrh } 32791bb0eedSdrh } 32891bb0eedSdrh 32991bb0eedSdrh /* 3306977fea8Sdrh ** Set the Expr.span field of the given expression to span all 331a76b5dfcSdrh ** text between the two given tokens. 332a76b5dfcSdrh */ 3334adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 3344efc4754Sdrh assert( pRight!=0 ); 3354efc4754Sdrh assert( pLeft!=0 ); 336f3a65f7eSdrh if( pExpr && pRight->z && pLeft->z ){ 337ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 338145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 3396977fea8Sdrh pExpr->span.z = pLeft->z; 34097903fefSdrh pExpr->span.n = pRight->n + (pRight->z - pLeft->z); 3414b59ab5eSdrh }else{ 3426977fea8Sdrh pExpr->span.z = 0; 3434b59ab5eSdrh } 344a76b5dfcSdrh } 345a76b5dfcSdrh } 346a76b5dfcSdrh 347a76b5dfcSdrh /* 348a76b5dfcSdrh ** Construct a new expression node for a function with multiple 349a76b5dfcSdrh ** arguments. 350a76b5dfcSdrh */ 35117435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 352a76b5dfcSdrh Expr *pNew; 3534b202ae2Sdanielk1977 assert( pToken ); 35417435752Sdrh pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) ); 355a76b5dfcSdrh if( pNew==0 ){ 356d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ 357a76b5dfcSdrh return 0; 358a76b5dfcSdrh } 359a76b5dfcSdrh pNew->op = TK_FUNCTION; 360a76b5dfcSdrh pNew->pList = pList; 3614b59ab5eSdrh assert( pToken->dyn==0 ); 362a76b5dfcSdrh pNew->token = *pToken; 3636977fea8Sdrh pNew->span = pNew->token; 364fc976065Sdanielk1977 365fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 366a76b5dfcSdrh return pNew; 367a76b5dfcSdrh } 368a76b5dfcSdrh 369a76b5dfcSdrh /* 370fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 371fa6bc000Sdrh ** in the original SQL statement. 372fa6bc000Sdrh ** 373fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 374fa6bc000Sdrh ** variable number. 375fa6bc000Sdrh ** 376fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 377fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 378fa6bc000Sdrh ** the SQL statement comes from an external source. 379fa6bc000Sdrh ** 380fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 381fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 382fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 383fa6bc000Sdrh ** assigned. 384fa6bc000Sdrh */ 385fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 386fa6bc000Sdrh Token *pToken; 38717435752Sdrh sqlite3 *db = pParse->db; 38817435752Sdrh 389fa6bc000Sdrh if( pExpr==0 ) return; 390fa6bc000Sdrh pToken = &pExpr->token; 391fa6bc000Sdrh assert( pToken->n>=1 ); 392fa6bc000Sdrh assert( pToken->z!=0 ); 393fa6bc000Sdrh assert( pToken->z[0]!=0 ); 394fa6bc000Sdrh if( pToken->n==1 ){ 395fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 396fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 397fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 398fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 399fa6bc000Sdrh ** use it as the variable number */ 400fa6bc000Sdrh int i; 4012646da7eSdrh pExpr->iTable = i = atoi((char*)&pToken->z[1]); 402fa6bc000Sdrh if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ 403fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 404fa6bc000Sdrh SQLITE_MAX_VARIABLE_NUMBER); 405fa6bc000Sdrh } 406fa6bc000Sdrh if( i>pParse->nVar ){ 407fa6bc000Sdrh pParse->nVar = i; 408fa6bc000Sdrh } 409fa6bc000Sdrh }else{ 410fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 411fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 412fa6bc000Sdrh ** has never appeared before, reuse the same variable number 413fa6bc000Sdrh */ 414fa6bc000Sdrh int i, n; 415fa6bc000Sdrh n = pToken->n; 416fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 417fa6bc000Sdrh Expr *pE; 418fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 419fa6bc000Sdrh && pE->token.n==n 420fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 421fa6bc000Sdrh pExpr->iTable = pE->iTable; 422fa6bc000Sdrh break; 423fa6bc000Sdrh } 424fa6bc000Sdrh } 425fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 426fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 427fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 428fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 42917435752Sdrh pParse->apVarExpr = 43017435752Sdrh sqlite3DbReallocOrFree( 43117435752Sdrh db, 43217435752Sdrh pParse->apVarExpr, 43317435752Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) 43417435752Sdrh ); 435fa6bc000Sdrh } 43617435752Sdrh if( !db->mallocFailed ){ 437fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 438fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 439fa6bc000Sdrh } 440fa6bc000Sdrh } 441fa6bc000Sdrh } 442832b2664Sdanielk1977 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){ 443832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 444832b2664Sdanielk1977 } 445fa6bc000Sdrh } 446fa6bc000Sdrh 447fa6bc000Sdrh /* 448a2e00042Sdrh ** Recursively delete an expression tree. 449a2e00042Sdrh */ 4504adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 451a2e00042Sdrh if( p==0 ) return; 45217435752Sdrh if( p->span.dyn ) sqlite3_free((char*)p->span.z); 45317435752Sdrh if( p->token.dyn ) sqlite3_free((char*)p->token.z); 4544adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 4554adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 4564adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 4574adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 45817435752Sdrh sqlite3_free(p); 459a2e00042Sdrh } 460a2e00042Sdrh 461d2687b77Sdrh /* 462d2687b77Sdrh ** The Expr.token field might be a string literal that is quoted. 463d2687b77Sdrh ** If so, remove the quotation marks. 464d2687b77Sdrh */ 46517435752Sdrh void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ 466d2687b77Sdrh if( ExprHasAnyProperty(p, EP_Dequoted) ){ 467d2687b77Sdrh return; 468d2687b77Sdrh } 469d2687b77Sdrh ExprSetProperty(p, EP_Dequoted); 470d2687b77Sdrh if( p->token.dyn==0 ){ 47117435752Sdrh sqlite3TokenCopy(db, &p->token, &p->token); 472d2687b77Sdrh } 473d2687b77Sdrh sqlite3Dequote((char*)p->token.z); 474d2687b77Sdrh } 475d2687b77Sdrh 476a76b5dfcSdrh 477a76b5dfcSdrh /* 478ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 479ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 480ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 481ff78bd2fSdrh ** without effecting the originals. 482ff78bd2fSdrh ** 4834adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 4844adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 485ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 486ff78bd2fSdrh ** 487ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 488ff78bd2fSdrh */ 4891e536953Sdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){ 490ff78bd2fSdrh Expr *pNew; 491ff78bd2fSdrh if( p==0 ) return 0; 49217435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); 493ff78bd2fSdrh if( pNew==0 ) return 0; 4943b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 4956977fea8Sdrh if( p->token.z!=0 ){ 49617435752Sdrh pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); 4974b59ab5eSdrh pNew->token.dyn = 1; 4984b59ab5eSdrh }else{ 4994efc4754Sdrh assert( pNew->token.z==0 ); 5004b59ab5eSdrh } 5016977fea8Sdrh pNew->span.z = 0; 50217435752Sdrh pNew->pLeft = sqlite3ExprDup(db, p->pLeft); 50317435752Sdrh pNew->pRight = sqlite3ExprDup(db, p->pRight); 50417435752Sdrh pNew->pList = sqlite3ExprListDup(db, p->pList); 50517435752Sdrh pNew->pSelect = sqlite3SelectDup(db, p->pSelect); 506ff78bd2fSdrh return pNew; 507ff78bd2fSdrh } 50817435752Sdrh void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ 50917435752Sdrh if( pTo->dyn ) sqlite3_free((char*)pTo->z); 5104b59ab5eSdrh if( pFrom->z ){ 5114b59ab5eSdrh pTo->n = pFrom->n; 51217435752Sdrh pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); 5134b59ab5eSdrh pTo->dyn = 1; 5144b59ab5eSdrh }else{ 5154b59ab5eSdrh pTo->z = 0; 5164b59ab5eSdrh } 5174b59ab5eSdrh } 51817435752Sdrh ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ 519ff78bd2fSdrh ExprList *pNew; 520145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 521ff78bd2fSdrh int i; 522ff78bd2fSdrh if( p==0 ) return 0; 52317435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 524ff78bd2fSdrh if( pNew==0 ) return 0; 52531dad9daSdanielk1977 pNew->iECursor = 0; 5264305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 52717435752Sdrh pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); 528e0048400Sdanielk1977 if( pItem==0 ){ 52917435752Sdrh sqlite3_free(pNew); 530e0048400Sdanielk1977 return 0; 531e0048400Sdanielk1977 } 532145716b3Sdrh pOldItem = p->a; 533145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 5344b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 53517435752Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); 5366977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 5376977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 5384b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 5394b59ab5eSdrh ** the names of columns in the result set needs this information */ 54017435752Sdrh sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); 5414b59ab5eSdrh } 5421f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 5436f7adc8aSdrh || pOldExpr->span.z==0 54417435752Sdrh || db->mallocFailed ); 54517435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 546145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 547145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 5483e7bc9caSdrh pItem->done = 0; 549ff78bd2fSdrh } 550ff78bd2fSdrh return pNew; 551ff78bd2fSdrh } 55293758c8dSdanielk1977 55393758c8dSdanielk1977 /* 55493758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 55593758c8dSdanielk1977 ** the build, then none of the following routines, except for 55693758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 55793758c8dSdanielk1977 ** called with a NULL argument. 55893758c8dSdanielk1977 */ 5596a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 5606a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 56117435752Sdrh SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ 562ad3cab52Sdrh SrcList *pNew; 563ad3cab52Sdrh int i; 564113088ecSdrh int nByte; 565ad3cab52Sdrh if( p==0 ) return 0; 566113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 56717435752Sdrh pNew = sqlite3DbMallocRaw(db, nByte ); 568ad3cab52Sdrh if( pNew==0 ) return 0; 5694305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 570ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 5714efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 5724efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 573ed8a3bb1Sdrh Table *pTab; 57417435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 57517435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 57617435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 5774efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 5784efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 5791787ccabSdanielk1977 pNewItem->isPopulated = pOldItem->isPopulated; 580ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 581ed8a3bb1Sdrh if( pTab ){ 582ed8a3bb1Sdrh pTab->nRef++; 583a1cb183dSdanielk1977 } 58417435752Sdrh pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); 58517435752Sdrh pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); 58617435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 5876c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 588ad3cab52Sdrh } 589ad3cab52Sdrh return pNew; 590ad3cab52Sdrh } 59117435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 592ff78bd2fSdrh IdList *pNew; 593ff78bd2fSdrh int i; 594ff78bd2fSdrh if( p==0 ) return 0; 59517435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 596ff78bd2fSdrh if( pNew==0 ) return 0; 5974305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 59817435752Sdrh pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); 599d5d56523Sdanielk1977 if( pNew->a==0 ){ 60017435752Sdrh sqlite3_free(pNew); 601d5d56523Sdanielk1977 return 0; 602d5d56523Sdanielk1977 } 603ff78bd2fSdrh for(i=0; i<p->nId; i++){ 6044efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 6054efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 60617435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 6074efc4754Sdrh pNewItem->idx = pOldItem->idx; 608ff78bd2fSdrh } 609ff78bd2fSdrh return pNew; 610ff78bd2fSdrh } 61117435752Sdrh Select *sqlite3SelectDup(sqlite3 *db, Select *p){ 612ff78bd2fSdrh Select *pNew; 613ff78bd2fSdrh if( p==0 ) return 0; 61417435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); 615ff78bd2fSdrh if( pNew==0 ) return 0; 616ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 61717435752Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList); 61817435752Sdrh pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); 61917435752Sdrh pNew->pWhere = sqlite3ExprDup(db, p->pWhere); 62017435752Sdrh pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); 62117435752Sdrh pNew->pHaving = sqlite3ExprDup(db, p->pHaving); 62217435752Sdrh pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); 623ff78bd2fSdrh pNew->op = p->op; 62417435752Sdrh pNew->pPrior = sqlite3SelectDup(db, p->pPrior); 62517435752Sdrh pNew->pLimit = sqlite3ExprDup(db, p->pLimit); 62617435752Sdrh pNew->pOffset = sqlite3ExprDup(db, p->pOffset); 6277b58daeaSdrh pNew->iLimit = -1; 6287b58daeaSdrh pNew->iOffset = -1; 629a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 630a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 631b9bb7c18Sdrh pNew->usesEphm = 0; 6328e647b81Sdrh pNew->disallowOrderBy = 0; 6330342b1f5Sdrh pNew->pRightmost = 0; 634b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 635b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 636b9bb7c18Sdrh pNew->addrOpenEphm[2] = -1; 637ff78bd2fSdrh return pNew; 638ff78bd2fSdrh } 63993758c8dSdanielk1977 #else 64017435752Sdrh Select *sqlite3SelectDup(sqlite3 *db, Select *p){ 64193758c8dSdanielk1977 assert( p==0 ); 64293758c8dSdanielk1977 return 0; 64393758c8dSdanielk1977 } 64493758c8dSdanielk1977 #endif 645ff78bd2fSdrh 646ff78bd2fSdrh 647ff78bd2fSdrh /* 648a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 649a76b5dfcSdrh ** initially NULL, then create a new expression list. 650a76b5dfcSdrh */ 65117435752Sdrh ExprList *sqlite3ExprListAppend( 65217435752Sdrh Parse *pParse, /* Parsing context */ 65317435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 65417435752Sdrh Expr *pExpr, /* Expression to be appended */ 65517435752Sdrh Token *pName /* AS keyword for the expression */ 65617435752Sdrh ){ 65717435752Sdrh sqlite3 *db = pParse->db; 658a76b5dfcSdrh if( pList==0 ){ 65917435752Sdrh pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); 660a76b5dfcSdrh if( pList==0 ){ 661d5d56523Sdanielk1977 goto no_mem; 662a76b5dfcSdrh } 6634efc4754Sdrh assert( pList->nAlloc==0 ); 664a76b5dfcSdrh } 6654305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 666d5d56523Sdanielk1977 struct ExprList_item *a; 667d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 66826783a58Sdanielk1977 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); 669d5d56523Sdanielk1977 if( a==0 ){ 670d5d56523Sdanielk1977 goto no_mem; 671a76b5dfcSdrh } 672d5d56523Sdanielk1977 pList->a = a; 673d5d56523Sdanielk1977 pList->nAlloc = n; 674a76b5dfcSdrh } 6754efc4754Sdrh assert( pList->a!=0 ); 6764efc4754Sdrh if( pExpr || pName ){ 6774efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 6784efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 67917435752Sdrh pItem->zName = sqlite3NameFromToken(db, pName); 680e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 681a76b5dfcSdrh } 682a76b5dfcSdrh return pList; 683d5d56523Sdanielk1977 684d5d56523Sdanielk1977 no_mem: 685d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 686d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 687d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 688d5d56523Sdanielk1977 return 0; 689a76b5dfcSdrh } 690a76b5dfcSdrh 691a76b5dfcSdrh /* 6927a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 6937a15a4beSdanielk1977 ** leave an error message in pParse. 6947a15a4beSdanielk1977 */ 6957a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 6967a15a4beSdanielk1977 Parse *pParse, 6977a15a4beSdanielk1977 ExprList *pEList, 6987a15a4beSdanielk1977 int iLimit, 6997a15a4beSdanielk1977 const char *zObject 7007a15a4beSdanielk1977 ){ 701b4fc6794Sdanielk1977 if( pEList && pEList->nExpr>iLimit ){ 7027a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 7037a15a4beSdanielk1977 } 7047a15a4beSdanielk1977 } 7057a15a4beSdanielk1977 706fc976065Sdanielk1977 707e6a58a4eSdanielk1977 #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0 708fc976065Sdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 709fc976065Sdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 710fc976065Sdanielk1977 ** of any expression tree referenced by the structure passed as the 711fc976065Sdanielk1977 ** first argument. 712fc976065Sdanielk1977 ** 713fc976065Sdanielk1977 ** If this maximum height is greater than the current value pointed 714fc976065Sdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 715fc976065Sdanielk1977 ** value. 716fc976065Sdanielk1977 */ 717fc976065Sdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 718fc976065Sdanielk1977 if( p ){ 719fc976065Sdanielk1977 if( p->nHeight>*pnHeight ){ 720fc976065Sdanielk1977 *pnHeight = p->nHeight; 721fc976065Sdanielk1977 } 722fc976065Sdanielk1977 } 723fc976065Sdanielk1977 } 724fc976065Sdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 725fc976065Sdanielk1977 if( p ){ 726fc976065Sdanielk1977 int i; 727fc976065Sdanielk1977 for(i=0; i<p->nExpr; i++){ 728fc976065Sdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 729fc976065Sdanielk1977 } 730fc976065Sdanielk1977 } 731fc976065Sdanielk1977 } 732fc976065Sdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 733fc976065Sdanielk1977 if( p ){ 734fc976065Sdanielk1977 heightOfExpr(p->pWhere, pnHeight); 735fc976065Sdanielk1977 heightOfExpr(p->pHaving, pnHeight); 736fc976065Sdanielk1977 heightOfExpr(p->pLimit, pnHeight); 737fc976065Sdanielk1977 heightOfExpr(p->pOffset, pnHeight); 738fc976065Sdanielk1977 heightOfExprList(p->pEList, pnHeight); 739fc976065Sdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 740fc976065Sdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 741fc976065Sdanielk1977 heightOfSelect(p->pPrior, pnHeight); 742fc976065Sdanielk1977 } 743fc976065Sdanielk1977 } 744fc976065Sdanielk1977 745fc976065Sdanielk1977 /* 746fc976065Sdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 747fc976065Sdanielk1977 ** argument. An expression with no children, Expr.pList or 748fc976065Sdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 749fc976065Sdanielk1977 ** has a height equal to the maximum height of any other 750fc976065Sdanielk1977 ** referenced Expr plus one. 751fc976065Sdanielk1977 */ 752fc976065Sdanielk1977 void sqlite3ExprSetHeight(Expr *p){ 753fc976065Sdanielk1977 int nHeight = 0; 754fc976065Sdanielk1977 heightOfExpr(p->pLeft, &nHeight); 755fc976065Sdanielk1977 heightOfExpr(p->pRight, &nHeight); 756fc976065Sdanielk1977 heightOfExprList(p->pList, &nHeight); 757fc976065Sdanielk1977 heightOfSelect(p->pSelect, &nHeight); 758fc976065Sdanielk1977 p->nHeight = nHeight + 1; 759fc976065Sdanielk1977 } 760fc976065Sdanielk1977 761fc976065Sdanielk1977 /* 762fc976065Sdanielk1977 ** Return the maximum height of any expression tree referenced 763fc976065Sdanielk1977 ** by the select statement passed as an argument. 764fc976065Sdanielk1977 */ 765fc976065Sdanielk1977 int sqlite3SelectExprHeight(Select *p){ 766fc976065Sdanielk1977 int nHeight = 0; 767fc976065Sdanielk1977 heightOfSelect(p, &nHeight); 768fc976065Sdanielk1977 return nHeight; 769fc976065Sdanielk1977 } 770fc976065Sdanielk1977 #endif 771fc976065Sdanielk1977 7727a15a4beSdanielk1977 /* 773a76b5dfcSdrh ** Delete an entire expression list. 774a76b5dfcSdrh */ 7754adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 776a76b5dfcSdrh int i; 777be5c89acSdrh struct ExprList_item *pItem; 778a76b5dfcSdrh if( pList==0 ) return; 7791bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 7801bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 781be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 782be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 78317435752Sdrh sqlite3_free(pItem->zName); 784a76b5dfcSdrh } 78517435752Sdrh sqlite3_free(pList->a); 78617435752Sdrh sqlite3_free(pList); 787a76b5dfcSdrh } 788a76b5dfcSdrh 789a76b5dfcSdrh /* 790626a879aSdrh ** Walk an expression tree. Call xFunc for each node visited. 79173b211abSdrh ** 792626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 793626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 794626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 795626a879aSdrh ** the tree walk completely. 796626a879aSdrh ** 797626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 798626a879aSdrh ** and 0 to continue. 79987abf5c0Sdrh ** 80087abf5c0Sdrh ** NOTICE: This routine does *not* descend into subqueries. 801626a879aSdrh */ 802a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 803626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 804626a879aSdrh int rc; 805626a879aSdrh if( pExpr==0 ) return 0; 806626a879aSdrh rc = (*xFunc)(pArg, pExpr); 807626a879aSdrh if( rc==0 ){ 808626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 809626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 810a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 811626a879aSdrh } 812626a879aSdrh return rc>1; 813626a879aSdrh } 814626a879aSdrh 815626a879aSdrh /* 816a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 817a58fdfb1Sdanielk1977 */ 818a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 819a58fdfb1Sdanielk1977 int i; 820a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 821a58fdfb1Sdanielk1977 if( !p ) return 0; 822a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 823a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 824a58fdfb1Sdanielk1977 } 825a58fdfb1Sdanielk1977 return 0; 826a58fdfb1Sdanielk1977 } 827a58fdfb1Sdanielk1977 828a58fdfb1Sdanielk1977 /* 829a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 830a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 831a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 832a58fdfb1Sdanielk1977 */ 833a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 834a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 835a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 836a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 837a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 838a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 83915d7982aSdanielk1977 if( p->pPrior ){ 84015d7982aSdanielk1977 walkSelectExpr(p->pPrior, xFunc, pArg); 84115d7982aSdanielk1977 } 842a58fdfb1Sdanielk1977 return 0; 843a58fdfb1Sdanielk1977 } 844a58fdfb1Sdanielk1977 845a58fdfb1Sdanielk1977 846a58fdfb1Sdanielk1977 /* 847626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 848626a879aSdrh ** 849626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 85073b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 85173b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 85273b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 85373b211abSdrh ** then do nothing. 85473b211abSdrh ** 85573b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 85673b211abSdrh ** the expression as constant, then we assume the whole expression 85773b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 858626a879aSdrh */ 859626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 8600a168377Sdrh int *pN = (int*)pArg; 8610a168377Sdrh 8620a168377Sdrh /* If *pArg is 3 then any term of the expression that comes from 8630a168377Sdrh ** the ON or USING clauses of a join disqualifies the expression 8640a168377Sdrh ** from being considered constant. */ 8650a168377Sdrh if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ 8660a168377Sdrh *pN = 0; 8670a168377Sdrh return 2; 8680a168377Sdrh } 8690a168377Sdrh 870626a879aSdrh switch( pExpr->op ){ 871eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 872eb55bd2fSdrh ** and *pArg==2 */ 873eb55bd2fSdrh case TK_FUNCTION: 8740a168377Sdrh if( (*pN)==2 ) return 0; 875eb55bd2fSdrh /* Fall through */ 876626a879aSdrh case TK_ID: 877626a879aSdrh case TK_COLUMN: 878626a879aSdrh case TK_DOT: 879626a879aSdrh case TK_AGG_FUNCTION: 88013449892Sdrh case TK_AGG_COLUMN: 881fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 882fe2093d7Sdrh case TK_SELECT: 883fe2093d7Sdrh case TK_EXISTS: 884fe2093d7Sdrh #endif 8850a168377Sdrh *pN = 0; 886626a879aSdrh return 2; 88787abf5c0Sdrh case TK_IN: 88887abf5c0Sdrh if( pExpr->pSelect ){ 8890a168377Sdrh *pN = 0; 89087abf5c0Sdrh return 2; 89187abf5c0Sdrh } 892626a879aSdrh default: 893626a879aSdrh return 0; 894626a879aSdrh } 895626a879aSdrh } 896626a879aSdrh 897626a879aSdrh /* 898fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 899eb55bd2fSdrh ** and 0 if it involves variables or function calls. 9002398937bSdrh ** 9012398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 9022398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 9032398937bSdrh ** a constant. 904fef5208cSdrh */ 9054adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 906626a879aSdrh int isConst = 1; 907626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 908626a879aSdrh return isConst; 909fef5208cSdrh } 910fef5208cSdrh 911fef5208cSdrh /* 912eb55bd2fSdrh ** Walk an expression tree. Return 1 if the expression is constant 9130a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 9140a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 9150a168377Sdrh ** an ON or USING clause. 9160a168377Sdrh */ 9170a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 9180a168377Sdrh int isConst = 3; 9190a168377Sdrh walkExprTree(p, exprNodeIsConstant, &isConst); 9200a168377Sdrh return isConst!=0; 9210a168377Sdrh } 9220a168377Sdrh 9230a168377Sdrh /* 9240a168377Sdrh ** Walk an expression tree. Return 1 if the expression is constant 925eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 926eb55bd2fSdrh ** are any variables. 927eb55bd2fSdrh ** 928eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 929eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 930eb55bd2fSdrh ** a constant. 931eb55bd2fSdrh */ 932eb55bd2fSdrh int sqlite3ExprIsConstantOrFunction(Expr *p){ 933eb55bd2fSdrh int isConst = 2; 934eb55bd2fSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 935eb55bd2fSdrh return isConst!=0; 936eb55bd2fSdrh } 937eb55bd2fSdrh 938eb55bd2fSdrh /* 93973b211abSdrh ** If the expression p codes a constant integer that is small enough 940202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 941202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 942202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 943e4de1febSdrh */ 9444adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 945e4de1febSdrh switch( p->op ){ 946e4de1febSdrh case TK_INTEGER: { 9472646da7eSdrh if( sqlite3GetInt32((char*)p->token.z, pValue) ){ 948e4de1febSdrh return 1; 949e4de1febSdrh } 950202b2df7Sdrh break; 951202b2df7Sdrh } 9524b59ab5eSdrh case TK_UPLUS: { 9534adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 9544b59ab5eSdrh } 955e4de1febSdrh case TK_UMINUS: { 956e4de1febSdrh int v; 9574adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 958e4de1febSdrh *pValue = -v; 959e4de1febSdrh return 1; 960e4de1febSdrh } 961e4de1febSdrh break; 962e4de1febSdrh } 963e4de1febSdrh default: break; 964e4de1febSdrh } 965e4de1febSdrh return 0; 966e4de1febSdrh } 967e4de1febSdrh 968e4de1febSdrh /* 969c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 970c4a3c779Sdrh */ 9714adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 9724adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 9734adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 9744adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 975c4a3c779Sdrh return 0; 976c4a3c779Sdrh } 977c4a3c779Sdrh 978c4a3c779Sdrh /* 9798141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 9808141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 9818141f61eSdrh ** expression node refer back to that source column. The following changes 9828141f61eSdrh ** are made to pExpr: 9838141f61eSdrh ** 9848141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 9858141f61eSdrh ** the table. 9868141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 9878141f61eSdrh ** from pSrcList. 9888141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 9898141f61eSdrh ** pExpr->op Set to TK_COLUMN. 9908141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 9918141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 9928141f61eSdrh ** 9938141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 9948141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 9958141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 9968141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 9978141f61eSdrh ** means that the form of the name is Z and that columns from any table 9988141f61eSdrh ** can be used. 9998141f61eSdrh ** 10008141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 10018141f61eSdrh ** in pParse and return non-zero. Return zero on success. 10028141f61eSdrh */ 10038141f61eSdrh static int lookupName( 10048141f61eSdrh Parse *pParse, /* The parsing context */ 10058141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 10068141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 10078141f61eSdrh Token *pColumnToken, /* Name of the column. */ 1008626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 10098141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 10108141f61eSdrh ){ 10118141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 10128141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 10138141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 10148141f61eSdrh int i, j; /* Loop counters */ 10158141f61eSdrh int cnt = 0; /* Number of matching column names */ 10168141f61eSdrh int cntTab = 0; /* Number of matching table names */ 10179bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 101851669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 101951669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 102073b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 1021728b5779Sdrh Schema *pSchema = 0; /* Schema of the expression */ 10228141f61eSdrh 10238141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 102417435752Sdrh zDb = sqlite3NameFromToken(db, pDbToken); 102517435752Sdrh zTab = sqlite3NameFromToken(db, pTableToken); 102617435752Sdrh zCol = sqlite3NameFromToken(db, pColumnToken); 102717435752Sdrh if( db->mallocFailed ){ 1028d5d56523Sdanielk1977 goto lookupname_end; 10298141f61eSdrh } 10308141f61eSdrh 10318141f61eSdrh pExpr->iTable = -1; 1032626a879aSdrh while( pNC && cnt==0 ){ 1033ffe07b2dSdrh ExprList *pEList; 1034626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 1035626a879aSdrh 1036b3bce662Sdanielk1977 if( pSrcList ){ 103751669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 103843617e9aSdrh Table *pTab; 103943617e9aSdrh int iDb; 10408141f61eSdrh Column *pCol; 10418141f61eSdrh 104243617e9aSdrh pTab = pItem->pTab; 104343617e9aSdrh assert( pTab!=0 ); 104443617e9aSdrh iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 10458141f61eSdrh assert( pTab->nCol>0 ); 10468141f61eSdrh if( zTab ){ 10478141f61eSdrh if( pItem->zAlias ){ 10488141f61eSdrh char *zTabName = pItem->zAlias; 10494adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 10508141f61eSdrh }else{ 10518141f61eSdrh char *zTabName = pTab->zName; 10524adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 1053da184236Sdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ 10548141f61eSdrh continue; 10558141f61eSdrh } 10568141f61eSdrh } 10578141f61eSdrh } 10588141f61eSdrh if( 0==(cntTab++) ){ 10598141f61eSdrh pExpr->iTable = pItem->iCursor; 1060728b5779Sdrh pSchema = pTab->pSchema; 106151669863Sdrh pMatch = pItem; 10628141f61eSdrh } 10638141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 10644adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1065b3bf556eSdanielk1977 const char *zColl = pTab->aCol[j].zColl; 1066873fac0cSdrh IdList *pUsing; 10678141f61eSdrh cnt++; 10688141f61eSdrh pExpr->iTable = pItem->iCursor; 106951669863Sdrh pMatch = pItem; 1070728b5779Sdrh pSchema = pTab->pSchema; 10718141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 10728141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 1073a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 10748b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1075b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 10768b4c40d8Sdrh } 107761dfc31dSdrh if( i<pSrcList->nSrc-1 ){ 107861dfc31dSdrh if( pItem[1].jointype & JT_NATURAL ){ 1079355ef361Sdrh /* If this match occurred in the left table of a natural join, 1080355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 1081355ef361Sdrh pItem++; 1082355ef361Sdrh i++; 108361dfc31dSdrh }else if( (pUsing = pItem[1].pUsing)!=0 ){ 1084873fac0cSdrh /* If this match occurs on a column that is in the USING clause 1085873fac0cSdrh ** of a join, skip the search of the right table of the join 1086873fac0cSdrh ** to avoid a duplicate match there. */ 1087873fac0cSdrh int k; 1088873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 1089873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 1090873fac0cSdrh pItem++; 1091873fac0cSdrh i++; 1092873fac0cSdrh break; 1093873fac0cSdrh } 1094873fac0cSdrh } 1095873fac0cSdrh } 109661dfc31dSdrh } 10978141f61eSdrh break; 10988141f61eSdrh } 10998141f61eSdrh } 11008141f61eSdrh } 1101b3bce662Sdanielk1977 } 11028141f61eSdrh 1103b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 11048141f61eSdrh /* If we have not already resolved the name, then maybe 11058141f61eSdrh ** it is a new.* or old.* trigger argument reference 11068141f61eSdrh */ 11078141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 11088141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 11098141f61eSdrh Table *pTab = 0; 11108f2c54e6Sdanielk1977 u32 *piColMask; 11114adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 11128141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 11138141f61eSdrh assert( pTriggerStack->pTab ); 11148141f61eSdrh pTab = pTriggerStack->pTab; 11158f2c54e6Sdanielk1977 piColMask = &(pTriggerStack->newColMask); 11164adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 11178141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 11188141f61eSdrh assert( pTriggerStack->pTab ); 11198141f61eSdrh pTab = pTriggerStack->pTab; 11208f2c54e6Sdanielk1977 piColMask = &(pTriggerStack->oldColMask); 11218141f61eSdrh } 11228141f61eSdrh 11238141f61eSdrh if( pTab ){ 1124f0113000Sdanielk1977 int iCol; 11258141f61eSdrh Column *pCol = pTab->aCol; 11268141f61eSdrh 1127728b5779Sdrh pSchema = pTab->pSchema; 11288141f61eSdrh cntTab++; 1129f0113000Sdanielk1977 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { 11304adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1131f0113000Sdanielk1977 const char *zColl = pTab->aCol[iCol].zColl; 11328141f61eSdrh cnt++; 1133f0113000Sdanielk1977 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; 1134f0113000Sdanielk1977 pExpr->affinity = pTab->aCol[iCol].affinity; 11358b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1136b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 11378b4c40d8Sdrh } 1138aee18ef8Sdanielk1977 pExpr->pTab = pTab; 11398f2c54e6Sdanielk1977 if( iCol>=0 ){ 11408f2c54e6Sdanielk1977 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0); 11418f2c54e6Sdanielk1977 } 11428141f61eSdrh break; 11438141f61eSdrh } 11448141f61eSdrh } 11458141f61eSdrh } 11468141f61eSdrh } 1147b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 11488141f61eSdrh 11498141f61eSdrh /* 11508141f61eSdrh ** Perhaps the name is a reference to the ROWID 11518141f61eSdrh */ 11524adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 11538141f61eSdrh cnt = 1; 11548141f61eSdrh pExpr->iColumn = -1; 11558a51256cSdrh pExpr->affinity = SQLITE_AFF_INTEGER; 11568141f61eSdrh } 11578141f61eSdrh 11588141f61eSdrh /* 11598141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 11608141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 11618141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 11628141f61eSdrh ** 11638141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 11648141f61eSdrh ** 11658141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 11668141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 11678141f61eSdrh ** Note that the expression in the result set should have already been 11688141f61eSdrh ** resolved by the time the WHERE clause is resolved. 11698141f61eSdrh */ 1170ffe07b2dSdrh if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ 11718141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 11728141f61eSdrh char *zAs = pEList->a[j].zName; 11734adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 117436379e97Sdrh Expr *pDup, *pOrig; 11758141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 11764f07e5fbSdrh assert( pExpr->pList==0 ); 11774f07e5fbSdrh assert( pExpr->pSelect==0 ); 117836379e97Sdrh pOrig = pEList->a[j].pExpr; 117936379e97Sdrh if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ 118036379e97Sdrh sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); 118117435752Sdrh sqlite3_free(zCol); 118236379e97Sdrh return 2; 118336379e97Sdrh } 11841e536953Sdanielk1977 pDup = sqlite3ExprDup(db, pOrig); 11854f07e5fbSdrh if( pExpr->flags & EP_ExpCollate ){ 11864f07e5fbSdrh pDup->pColl = pExpr->pColl; 11874f07e5fbSdrh pDup->flags |= EP_ExpCollate; 11884f07e5fbSdrh } 118917435752Sdrh if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z); 119017435752Sdrh if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z); 11914f07e5fbSdrh memcpy(pExpr, pDup, sizeof(*pExpr)); 119217435752Sdrh sqlite3_free(pDup); 119315ccce1cSdrh cnt = 1; 1194c9cf6e3dSdanielk1977 pMatch = 0; 11958141f61eSdrh assert( zTab==0 && zDb==0 ); 119615ccce1cSdrh goto lookupname_end_2; 11978141f61eSdrh } 11988141f61eSdrh } 11998141f61eSdrh } 12008141f61eSdrh 1201626a879aSdrh /* Advance to the next name context. The loop will exit when either 1202626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 1203626a879aSdrh */ 1204626a879aSdrh if( cnt==0 ){ 1205626a879aSdrh pNC = pNC->pNext; 1206626a879aSdrh } 1207626a879aSdrh } 1208626a879aSdrh 12098141f61eSdrh /* 12108141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 12118141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 12128141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 12138141f61eSdrh ** case, we need to return right away and not make any changes to 12148141f61eSdrh ** pExpr. 121515ccce1cSdrh ** 121615ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 121715ccce1cSdrh ** fields are not changed in any context. 12188141f61eSdrh */ 12198141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 122017435752Sdrh sqlite3_free(zCol); 12218141f61eSdrh return 0; 12228141f61eSdrh } 12238141f61eSdrh 12248141f61eSdrh /* 12258141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 12268141f61eSdrh ** more matches. Either way, we have an error. 12278141f61eSdrh */ 12288141f61eSdrh if( cnt!=1 ){ 12298141f61eSdrh char *z = 0; 12308141f61eSdrh char *zErr; 12318141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 12328141f61eSdrh if( zDb ){ 1233f93339deSdrh sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0); 12348141f61eSdrh }else if( zTab ){ 1235f93339deSdrh sqlite3SetString(&z, zTab, ".", zCol, (char*)0); 12368141f61eSdrh }else{ 123717435752Sdrh z = sqlite3StrDup(zCol); 12388141f61eSdrh } 1239a1644fd8Sdanielk1977 if( z ){ 12404adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 124117435752Sdrh sqlite3_free(z); 124273b211abSdrh pTopNC->nErr++; 1243a1644fd8Sdanielk1977 }else{ 1244a1644fd8Sdanielk1977 db->mallocFailed = 1; 1245a1644fd8Sdanielk1977 } 12468141f61eSdrh } 12478141f61eSdrh 124851669863Sdrh /* If a column from a table in pSrcList is referenced, then record 124951669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 125051669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 125151669863Sdrh ** column number is greater than the number of bits in the bitmask 125251669863Sdrh ** then set the high-order bit of the bitmask. 125351669863Sdrh */ 125451669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 125551669863Sdrh int n = pExpr->iColumn; 125651669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 125751669863Sdrh n = sizeof(Bitmask)*8-1; 125851669863Sdrh } 125951669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 1260ca83ac51Sdrh pMatch->colUsed |= ((Bitmask)1)<<n; 126151669863Sdrh } 126251669863Sdrh 1263d5d56523Sdanielk1977 lookupname_end: 12648141f61eSdrh /* Clean up and return 12658141f61eSdrh */ 126617435752Sdrh sqlite3_free(zDb); 126717435752Sdrh sqlite3_free(zTab); 12684adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 12698141f61eSdrh pExpr->pLeft = 0; 12704adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 12718141f61eSdrh pExpr->pRight = 0; 12728141f61eSdrh pExpr->op = TK_COLUMN; 127315ccce1cSdrh lookupname_end_2: 127417435752Sdrh sqlite3_free(zCol); 1275626a879aSdrh if( cnt==1 ){ 1276b3bce662Sdanielk1977 assert( pNC!=0 ); 1277728b5779Sdrh sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); 1278aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 1279aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 1280aee18ef8Sdanielk1977 } 128115ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 128215ccce1cSdrh ** the point where the name matched. */ 128315ccce1cSdrh for(;;){ 128415ccce1cSdrh assert( pTopNC!=0 ); 128515ccce1cSdrh pTopNC->nRef++; 128615ccce1cSdrh if( pTopNC==pNC ) break; 128715ccce1cSdrh pTopNC = pTopNC->pNext; 1288626a879aSdrh } 128915ccce1cSdrh return 0; 129015ccce1cSdrh } else { 129115ccce1cSdrh return 1; 129215ccce1cSdrh } 12938141f61eSdrh } 12948141f61eSdrh 12958141f61eSdrh /* 1296626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1297626a879aSdrh ** 129873b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1299626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 130073b211abSdrh ** the tree or 2 to abort the tree walk. 130173b211abSdrh ** 130273b211abSdrh ** This routine also does error checking and name resolution for 130373b211abSdrh ** function names. The operator for aggregate functions is changed 130473b211abSdrh ** to TK_AGG_FUNCTION. 1305626a879aSdrh */ 1306626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1307626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1308626a879aSdrh Parse *pParse; 1309626a879aSdrh 1310b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1311626a879aSdrh assert( pNC!=0 ); 1312626a879aSdrh pParse = pNC->pParse; 1313b3bce662Sdanielk1977 1314626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1315626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1316626a879aSdrh #ifndef NDEBUG 1317f0113000Sdanielk1977 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ 1318f0113000Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 1319940fac9dSdanielk1977 int i; 1320f0113000Sdanielk1977 for(i=0; i<pNC->pSrcList->nSrc; i++){ 1321626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1322626a879aSdrh } 1323626a879aSdrh } 1324626a879aSdrh #endif 1325626a879aSdrh switch( pExpr->op ){ 1326626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1327626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1328626a879aSdrh ** strings (ex: 'abc') are always string literals. 1329626a879aSdrh */ 1330626a879aSdrh case TK_STRING: { 1331626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1332626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1333626a879aSdrh } 1334626a879aSdrh /* A lone identifier is the name of a column. 1335626a879aSdrh */ 1336626a879aSdrh case TK_ID: { 1337626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1338626a879aSdrh return 1; 1339626a879aSdrh } 1340626a879aSdrh 1341626a879aSdrh /* A table name and column name: ID.ID 1342626a879aSdrh ** Or a database, table and column: ID.ID.ID 1343626a879aSdrh */ 1344626a879aSdrh case TK_DOT: { 1345626a879aSdrh Token *pColumn; 1346626a879aSdrh Token *pTable; 1347626a879aSdrh Token *pDb; 1348626a879aSdrh Expr *pRight; 1349626a879aSdrh 1350b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1351626a879aSdrh pRight = pExpr->pRight; 1352626a879aSdrh if( pRight->op==TK_ID ){ 1353626a879aSdrh pDb = 0; 1354626a879aSdrh pTable = &pExpr->pLeft->token; 1355626a879aSdrh pColumn = &pRight->token; 1356626a879aSdrh }else{ 1357626a879aSdrh assert( pRight->op==TK_DOT ); 1358626a879aSdrh pDb = &pExpr->pLeft->token; 1359626a879aSdrh pTable = &pRight->pLeft->token; 1360626a879aSdrh pColumn = &pRight->pRight->token; 1361626a879aSdrh } 1362626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1363626a879aSdrh return 1; 1364626a879aSdrh } 1365626a879aSdrh 1366626a879aSdrh /* Resolve function names 1367626a879aSdrh */ 1368b71090fdSdrh case TK_CONST_FUNC: 1369626a879aSdrh case TK_FUNCTION: { 1370626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1371626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1372626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1373626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1374626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1375626a879aSdrh int i; 13765169bbc6Sdrh int auth; /* Authorization to use the function */ 1377626a879aSdrh int nId; /* Number of characters in function name */ 1378626a879aSdrh const char *zId; /* The function name. */ 137973b211abSdrh FuncDef *pDef; /* Information about the function */ 138014db2665Sdanielk1977 int enc = ENC(pParse->db); /* The database encoding */ 1381626a879aSdrh 13822646da7eSdrh zId = (char*)pExpr->token.z; 1383b71090fdSdrh nId = pExpr->token.n; 1384626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1385626a879aSdrh if( pDef==0 ){ 1386626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1387626a879aSdrh if( pDef==0 ){ 1388626a879aSdrh no_such_func = 1; 1389626a879aSdrh }else{ 1390626a879aSdrh wrong_num_args = 1; 1391626a879aSdrh } 1392626a879aSdrh }else{ 1393626a879aSdrh is_agg = pDef->xFunc==0; 1394626a879aSdrh } 13952fca7fefSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 13965169bbc6Sdrh if( pDef ){ 13975169bbc6Sdrh auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); 13985169bbc6Sdrh if( auth!=SQLITE_OK ){ 13995169bbc6Sdrh if( auth==SQLITE_DENY ){ 14005169bbc6Sdrh sqlite3ErrorMsg(pParse, "not authorized to use function: %s", 14015169bbc6Sdrh pDef->zName); 14025169bbc6Sdrh pNC->nErr++; 14035169bbc6Sdrh } 14045169bbc6Sdrh pExpr->op = TK_NULL; 14055169bbc6Sdrh return 1; 14065169bbc6Sdrh } 14075169bbc6Sdrh } 1408b8b14219Sdrh #endif 1409626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1410626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1411626a879aSdrh pNC->nErr++; 1412626a879aSdrh is_agg = 0; 1413626a879aSdrh }else if( no_such_func ){ 1414626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1415626a879aSdrh pNC->nErr++; 1416626a879aSdrh }else if( wrong_num_args ){ 1417626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1418626a879aSdrh nId, zId); 1419626a879aSdrh pNC->nErr++; 1420626a879aSdrh } 1421626a879aSdrh if( is_agg ){ 1422626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1423626a879aSdrh pNC->hasAgg = 1; 1424626a879aSdrh } 142573b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1426626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 142773b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1428626a879aSdrh } 142973b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1430626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1431626a879aSdrh ** type of the function 1432626a879aSdrh */ 1433626a879aSdrh return is_agg; 1434626a879aSdrh } 1435b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1436b3bce662Sdanielk1977 case TK_SELECT: 1437b3bce662Sdanielk1977 case TK_EXISTS: 1438b3bce662Sdanielk1977 #endif 1439b3bce662Sdanielk1977 case TK_IN: { 1440b3bce662Sdanielk1977 if( pExpr->pSelect ){ 14418a9f38feSdrh int nRef = pNC->nRef; 144206f6541eSdrh #ifndef SQLITE_OMIT_CHECK 144306f6541eSdrh if( pNC->isCheck ){ 144406f6541eSdrh sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); 144506f6541eSdrh } 144606f6541eSdrh #endif 1447b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1448b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1449b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1450b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1451b3bce662Sdanielk1977 } 1452b3bce662Sdanielk1977 } 14534284fb07Sdrh break; 1454b3bce662Sdanielk1977 } 14554284fb07Sdrh #ifndef SQLITE_OMIT_CHECK 14564284fb07Sdrh case TK_VARIABLE: { 14574284fb07Sdrh if( pNC->isCheck ){ 14584284fb07Sdrh sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); 14594284fb07Sdrh } 14604284fb07Sdrh break; 14614284fb07Sdrh } 14624284fb07Sdrh #endif 1463626a879aSdrh } 1464626a879aSdrh return 0; 1465626a879aSdrh } 1466626a879aSdrh 1467626a879aSdrh /* 1468cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1469967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1470aacc543eSdrh ** index to the table in the table list and a column offset. The 1471aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1472aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1473832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1474aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1475aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1476aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1477aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1478aacc543eSdrh ** alias for ROWID. 147919a775c2Sdrh ** 1480626a879aSdrh ** Also resolve function names and check the functions for proper 1481626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1482626a879aSdrh ** have the correct number of arguments. Leave an error message 1483626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1484626a879aSdrh ** 148573b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 148673b211abSdrh ** property on the expression. 1487626a879aSdrh */ 1488626a879aSdrh int sqlite3ExprResolveNames( 1489b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1490b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1491626a879aSdrh ){ 149213449892Sdrh int savedHasAgg; 149373b211abSdrh if( pExpr==0 ) return 0; 1494e6a58a4eSdanielk1977 #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0 1495fc976065Sdanielk1977 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){ 1496fc976065Sdanielk1977 sqlite3ErrorMsg(pNC->pParse, 1497fc976065Sdanielk1977 "Expression tree is too large (maximum depth %d)", 1498fc976065Sdanielk1977 SQLITE_MAX_EXPR_DEPTH 1499fc976065Sdanielk1977 ); 1500fc976065Sdanielk1977 return 1; 1501fc976065Sdanielk1977 } 1502fc976065Sdanielk1977 pNC->pParse->nHeight += pExpr->nHeight; 1503fc976065Sdanielk1977 #endif 150413449892Sdrh savedHasAgg = pNC->hasAgg; 150513449892Sdrh pNC->hasAgg = 0; 1506b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1507e6a58a4eSdanielk1977 #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0 1508fc976065Sdanielk1977 pNC->pParse->nHeight -= pExpr->nHeight; 1509fc976065Sdanielk1977 #endif 1510b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 151173b211abSdrh ExprSetProperty(pExpr, EP_Error); 151273b211abSdrh } 151313449892Sdrh if( pNC->hasAgg ){ 151413449892Sdrh ExprSetProperty(pExpr, EP_Agg); 151513449892Sdrh }else if( savedHasAgg ){ 151613449892Sdrh pNC->hasAgg = 1; 151713449892Sdrh } 151873b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1519626a879aSdrh } 1520626a879aSdrh 15211398ad36Sdrh /* 15221398ad36Sdrh ** A pointer instance of this structure is used to pass information 15231398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 15241398ad36Sdrh */ 15251398ad36Sdrh typedef struct QueryCoder QueryCoder; 15261398ad36Sdrh struct QueryCoder { 15271398ad36Sdrh Parse *pParse; /* The parsing context */ 15281398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 15291398ad36Sdrh }; 15301398ad36Sdrh 15319a96b668Sdanielk1977 #ifdef SQLITE_TEST 15329a96b668Sdanielk1977 int sqlite3_enable_in_opt = 1; 15339a96b668Sdanielk1977 #else 15349a96b668Sdanielk1977 #define sqlite3_enable_in_opt 1 15359a96b668Sdanielk1977 #endif 15369a96b668Sdanielk1977 15379a96b668Sdanielk1977 /* 15389a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 15399a96b668Sdanielk1977 ** It's job is to find or create a b-tree structure that may be used 15409a96b668Sdanielk1977 ** either to test for membership of the (...) set or to iterate through 154185b623f2Sdrh ** its members, skipping duplicates. 15429a96b668Sdanielk1977 ** 15439a96b668Sdanielk1977 ** The cursor opened on the structure (database table, database index 15449a96b668Sdanielk1977 ** or ephermal table) is stored in pX->iTable before this function returns. 15459a96b668Sdanielk1977 ** The returned value indicates the structure type, as follows: 15469a96b668Sdanielk1977 ** 15479a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 15489a96b668Sdanielk1977 ** IN_INDEX_INDEX - The cursor was opened on a database indec. 15499a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 15509a96b668Sdanielk1977 ** populated epheremal table. 15519a96b668Sdanielk1977 ** 15529a96b668Sdanielk1977 ** An existing structure may only be used if the SELECT is of the simple 15539a96b668Sdanielk1977 ** form: 15549a96b668Sdanielk1977 ** 15559a96b668Sdanielk1977 ** SELECT <column> FROM <table> 15569a96b668Sdanielk1977 ** 15579a96b668Sdanielk1977 ** If the mustBeUnique parameter is false, the structure will be used 15589a96b668Sdanielk1977 ** for fast set membership tests. In this case an epheremal table must 15599a96b668Sdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 156085b623f2Sdrh ** be found with <column> as its left-most column. 15619a96b668Sdanielk1977 ** 15629a96b668Sdanielk1977 ** If mustBeUnique is true, then the structure will be used to iterate 15639a96b668Sdanielk1977 ** through the set members, skipping any duplicates. In this case an 15649a96b668Sdanielk1977 ** epheremal table must be used unless the selected <column> is guaranteed 15659a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 15669a96b668Sdanielk1977 ** is unique by virtue of a constraint or implicit index. 15679a96b668Sdanielk1977 */ 1568284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 15699a96b668Sdanielk1977 int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){ 15709a96b668Sdanielk1977 Select *p; 15719a96b668Sdanielk1977 int eType = 0; 15729a96b668Sdanielk1977 int iTab = pParse->nTab++; 15739a96b668Sdanielk1977 15749a96b668Sdanielk1977 /* The follwing if(...) expression is true if the SELECT is of the 15759a96b668Sdanielk1977 ** simple form: 15769a96b668Sdanielk1977 ** 15779a96b668Sdanielk1977 ** SELECT <column> FROM <table> 15789a96b668Sdanielk1977 ** 15799a96b668Sdanielk1977 ** If this is the case, it may be possible to use an existing table 15809a96b668Sdanielk1977 ** or index instead of generating an epheremal table. 15819a96b668Sdanielk1977 */ 15829a96b668Sdanielk1977 if( sqlite3_enable_in_opt 15839a96b668Sdanielk1977 && (p=pX->pSelect) && !p->pPrior 15849a96b668Sdanielk1977 && !p->isDistinct && !p->isAgg && !p->pGroupBy 15859a96b668Sdanielk1977 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect 15869a96b668Sdanielk1977 && !p->pSrc->a[0].pTab->pSelect 15879a96b668Sdanielk1977 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN 15889a96b668Sdanielk1977 && !p->pLimit && !p->pOffset && !p->pWhere 15899a96b668Sdanielk1977 ){ 15909a96b668Sdanielk1977 sqlite3 *db = pParse->db; 15919a96b668Sdanielk1977 Index *pIdx; 15929a96b668Sdanielk1977 Expr *pExpr = p->pEList->a[0].pExpr; 15939a96b668Sdanielk1977 int iCol = pExpr->iColumn; 15949a96b668Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 15959a96b668Sdanielk1977 15969a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 15979a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 15989a96b668Sdanielk1977 ** successful here. 15999a96b668Sdanielk1977 */ 16009a96b668Sdanielk1977 assert(v); 16019a96b668Sdanielk1977 if( iCol<0 ){ 16020a07c107Sdrh int iMem = ++pParse->nMem; 16039a96b668Sdanielk1977 int iAddr; 16049a96b668Sdanielk1977 Table *pTab = p->pSrc->a[0].pTab; 16059a96b668Sdanielk1977 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 16069a96b668Sdanielk1977 sqlite3VdbeUsesBtree(v, iDb); 16079a96b668Sdanielk1977 1608b1fdb2adSdrh sqlite3VdbeAddOp1(v, OP_SCopy, iMem); 160966a5167bSdrh iAddr = sqlite3VdbeAddOp2(v, OP_If, 0, iMem); 16104c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); 16119a96b668Sdanielk1977 16129a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 16139a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 16149a96b668Sdanielk1977 16159a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 16169a96b668Sdanielk1977 }else{ 16179a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 16189a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 16199a96b668Sdanielk1977 ** to this collation sequence. 16209a96b668Sdanielk1977 */ 16219a96b668Sdanielk1977 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); 16229a96b668Sdanielk1977 16239a96b668Sdanielk1977 /* Check that the affinity that will be used to perform the 16249a96b668Sdanielk1977 ** comparison is the same as the affinity of the column. If 16259a96b668Sdanielk1977 ** it is not, it is not possible to use any index. 16269a96b668Sdanielk1977 */ 16279a96b668Sdanielk1977 Table *pTab = p->pSrc->a[0].pTab; 16289a96b668Sdanielk1977 char aff = comparisonAffinity(pX); 16299a96b668Sdanielk1977 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); 16309a96b668Sdanielk1977 16319a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 16329a96b668Sdanielk1977 if( (pIdx->aiColumn[0]==iCol) 16339a96b668Sdanielk1977 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0)) 16349a96b668Sdanielk1977 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) 16359a96b668Sdanielk1977 ){ 16369a96b668Sdanielk1977 int iDb; 16370a07c107Sdrh int iMem = ++pParse->nMem; 16389a96b668Sdanielk1977 int iAddr; 16399a96b668Sdanielk1977 char *pKey; 16409a96b668Sdanielk1977 16419a96b668Sdanielk1977 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); 16429a96b668Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema); 16439a96b668Sdanielk1977 sqlite3VdbeUsesBtree(v, iDb); 16449a96b668Sdanielk1977 1645b1fdb2adSdrh sqlite3VdbeAddOp1(v, OP_SCopy, iMem); 164666a5167bSdrh iAddr = sqlite3VdbeAddOp2(v, OP_If, 0, iMem); 16474c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); 16489a96b668Sdanielk1977 1649207872a4Sdanielk1977 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, 165066a5167bSdrh pKey,P4_KEYINFO_HANDOFF); 1651207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 16529a96b668Sdanielk1977 eType = IN_INDEX_INDEX; 165366a5167bSdrh sqlite3VdbeAddOp2(v, OP_SetNumColumns, iTab, pIdx->nColumn); 16549a96b668Sdanielk1977 16559a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 16569a96b668Sdanielk1977 } 16579a96b668Sdanielk1977 } 16589a96b668Sdanielk1977 } 16599a96b668Sdanielk1977 } 16609a96b668Sdanielk1977 16619a96b668Sdanielk1977 if( eType==0 ){ 16629a96b668Sdanielk1977 sqlite3CodeSubselect(pParse, pX); 16639a96b668Sdanielk1977 eType = IN_INDEX_EPH; 16649a96b668Sdanielk1977 }else{ 16659a96b668Sdanielk1977 pX->iTable = iTab; 16669a96b668Sdanielk1977 } 16679a96b668Sdanielk1977 return eType; 16689a96b668Sdanielk1977 } 1669284f4acaSdanielk1977 #endif 1670626a879aSdrh 1671626a879aSdrh /* 16729cbe6352Sdrh ** Generate code for scalar subqueries used as an expression 16739cbe6352Sdrh ** and IN operators. Examples: 1674626a879aSdrh ** 16759cbe6352Sdrh ** (SELECT a FROM b) -- subquery 16769cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 16779cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 16789cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 1679fef5208cSdrh ** 16809cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 16819cbe6352Sdrh ** operator or subquery. 1682cce7d176Sdrh */ 168351522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1684b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 168557dbd7b3Sdrh int testAddr = 0; /* One-time test address */ 1686b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1687b3bce662Sdanielk1977 if( v==0 ) return; 1688b3bce662Sdanielk1977 1689fc976065Sdanielk1977 169057dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 169157dbd7b3Sdrh ** if any of the following is true: 169257dbd7b3Sdrh ** 169357dbd7b3Sdrh ** * The right-hand side is a correlated subquery 169457dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 169557dbd7b3Sdrh ** * We are inside a trigger 169657dbd7b3Sdrh ** 169757dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 169857dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1699b3bce662Sdanielk1977 */ 1700b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 17010a07c107Sdrh int mem = ++pParse->nMem; 1702b1fdb2adSdrh sqlite3VdbeAddOp1(v, OP_SCopy, mem); 170366a5167bSdrh testAddr = sqlite3VdbeAddOp0(v, OP_If); 170417435752Sdrh assert( testAddr>0 || pParse->db->mallocFailed ); 17054c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); 1706b3bce662Sdanielk1977 } 1707b3bce662Sdanielk1977 1708cce7d176Sdrh switch( pExpr->op ){ 1709fef5208cSdrh case TK_IN: { 1710e014a838Sdanielk1977 char affinity; 1711d3d39e93Sdrh KeyInfo keyInfo; 1712b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 1713d3d39e93Sdrh 1714bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1715e014a838Sdanielk1977 1716e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 171757dbd7b3Sdrh ** expression it is handled the same way. A virtual table is 1718e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1719e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1720fef5208cSdrh ** 1721e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1722e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1723e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1724e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1725e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1726e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1727e014a838Sdanielk1977 ** is used. 1728fef5208cSdrh */ 1729832508b7Sdrh pExpr->iTable = pParse->nTab++; 173066a5167bSdrh addr = sqlite3VdbeAddOp1(v, OP_OpenEphemeral, pExpr->iTable); 1731d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1732d3d39e93Sdrh keyInfo.nField = 1; 173366a5167bSdrh sqlite3VdbeAddOp2(v, OP_SetNumColumns, pExpr->iTable, 1); 1734e014a838Sdanielk1977 1735e014a838Sdanielk1977 if( pExpr->pSelect ){ 1736e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1737e014a838Sdanielk1977 ** 1738e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1739e014a838Sdanielk1977 ** table allocated and opened above. 1740e014a838Sdanielk1977 */ 17416c8c8ce0Sdanielk1977 SelectDest dest = {SRT_Set, 0, 0}; 17426c8c8ce0Sdanielk1977 dest.iParm = pExpr->iTable; 17436c8c8ce0Sdanielk1977 dest.affinity = (int)affinity; 1744be5c89acSdrh ExprList *pEList; 1745e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 17466c8c8ce0Sdanielk1977 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){ 174794ccde58Sdrh return; 174894ccde58Sdrh } 1749be5c89acSdrh pEList = pExpr->pSelect->pEList; 1750be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 1751bcbb04e5Sdanielk1977 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, 1752be5c89acSdrh pEList->a[0].pExpr); 17530202b29eSdanielk1977 } 1754fef5208cSdrh }else if( pExpr->pList ){ 1755fef5208cSdrh /* Case 2: expr IN (exprlist) 1756fef5208cSdrh ** 1757e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1758e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1759e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1760e014a838Sdanielk1977 ** a column, use numeric affinity. 1761fef5208cSdrh */ 1762e014a838Sdanielk1977 int i; 176357dbd7b3Sdrh ExprList *pList = pExpr->pList; 176457dbd7b3Sdrh struct ExprList_item *pItem; 176557dbd7b3Sdrh 1766e014a838Sdanielk1977 if( !affinity ){ 17678159a35fSdrh affinity = SQLITE_AFF_NONE; 1768e014a838Sdanielk1977 } 17690202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1770e014a838Sdanielk1977 1771e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 177257dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 177357dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1774e014a838Sdanielk1977 177557dbd7b3Sdrh /* If the expression is not constant then we will need to 177657dbd7b3Sdrh ** disable the test that was generated above that makes sure 177757dbd7b3Sdrh ** this code only executes once. Because for a non-constant 177857dbd7b3Sdrh ** expression we need to rerun this code each time. 177957dbd7b3Sdrh */ 17806c30be8eSdrh if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){ 1781f8875400Sdrh sqlite3VdbeChangeToNoop(v, testAddr-1, 3); 178257dbd7b3Sdrh testAddr = 0; 17834794b980Sdrh } 1784e014a838Sdanielk1977 1785e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 1786389a1adbSdrh sqlite3ExprCode(pParse, pE2, 0); 178766a5167bSdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, 1, 0, 0, &affinity, 1); 178866a5167bSdrh sqlite3VdbeAddOp1(v, OP_IdxInsert, pExpr->iTable); 1789fef5208cSdrh } 1790fef5208cSdrh } 179166a5167bSdrh sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); 1792b3bce662Sdanielk1977 break; 1793fef5208cSdrh } 1794fef5208cSdrh 179551522cd3Sdrh case TK_EXISTS: 179619a775c2Sdrh case TK_SELECT: { 1797fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1798fef5208cSdrh ** value of this select in a memory cell and record the number 1799967e8b73Sdrh ** of the memory cell in iColumn. 1800fef5208cSdrh */ 18012646da7eSdrh static const Token one = { (u8*)"1", 0, 1 }; 180251522cd3Sdrh Select *pSel; 18036c8c8ce0Sdanielk1977 SelectDest dest; 18041398ad36Sdrh 180551522cd3Sdrh pSel = pExpr->pSelect; 18060a07c107Sdrh dest.iParm = ++pParse->nMem; 180751522cd3Sdrh if( pExpr->op==TK_SELECT ){ 18086c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 18094c583128Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); 1810d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 181151522cd3Sdrh }else{ 18126c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 18134c583128Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); 1814d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 181551522cd3Sdrh } 1816ec7429aeSdrh sqlite3ExprDelete(pSel->pLimit); 1817a1644fd8Sdanielk1977 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); 18186c8c8ce0Sdanielk1977 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){ 181994ccde58Sdrh return; 182094ccde58Sdrh } 18216c8c8ce0Sdanielk1977 pExpr->iColumn = dest.iParm; 1822b3bce662Sdanielk1977 break; 182319a775c2Sdrh } 1824cce7d176Sdrh } 1825b3bce662Sdanielk1977 182657dbd7b3Sdrh if( testAddr ){ 1827d654be80Sdrh sqlite3VdbeJumpHere(v, testAddr); 1828b3bce662Sdanielk1977 } 1829fc976065Sdanielk1977 1830b3bce662Sdanielk1977 return; 1831cce7d176Sdrh } 183251522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1833cce7d176Sdrh 1834cce7d176Sdrh /* 1835598f1340Sdrh ** Duplicate an 8-byte value 1836598f1340Sdrh */ 1837598f1340Sdrh static char *dup8bytes(Vdbe *v, const char *in){ 1838598f1340Sdrh char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); 1839598f1340Sdrh if( out ){ 1840598f1340Sdrh memcpy(out, in, 8); 1841598f1340Sdrh } 1842598f1340Sdrh return out; 1843598f1340Sdrh } 1844598f1340Sdrh 1845598f1340Sdrh /* 1846598f1340Sdrh ** Generate an instruction that will put the floating point 1847598f1340Sdrh ** value described by z[0..n-1] on the stack. 18480cf19ed8Sdrh ** 18490cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 18500cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 18510cf19ed8Sdrh ** like the continuation of the number. 1852598f1340Sdrh */ 1853*9de221dfSdrh static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ 1854598f1340Sdrh assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); 1855598f1340Sdrh if( z ){ 1856598f1340Sdrh double value; 1857598f1340Sdrh char *zV; 18580cf19ed8Sdrh assert( !isdigit(z[n]) ); 1859598f1340Sdrh sqlite3AtoF(z, &value); 1860598f1340Sdrh if( negateFlag ) value = -value; 1861598f1340Sdrh zV = dup8bytes(v, (char*)&value); 1862*9de221dfSdrh sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); 1863598f1340Sdrh } 1864598f1340Sdrh } 1865598f1340Sdrh 1866598f1340Sdrh 1867598f1340Sdrh /* 1868fec19aadSdrh ** Generate an instruction that will put the integer describe by 1869fec19aadSdrh ** text z[0..n-1] on the stack. 18700cf19ed8Sdrh ** 18710cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 18720cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 18730cf19ed8Sdrh ** like the continuation of the number. 1874fec19aadSdrh */ 1875*9de221dfSdrh static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){ 1876abb6fcabSdrh assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); 1877c9cf901dSdanielk1977 if( z ){ 1878fec19aadSdrh int i; 18790cf19ed8Sdrh assert( !isdigit(z[n]) ); 18806fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 1881*9de221dfSdrh if( negFlag ) i = -i; 1882*9de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 1883*9de221dfSdrh }else if( sqlite3FitsIn64Bits(z, negFlag) ){ 1884598f1340Sdrh i64 value; 1885598f1340Sdrh char *zV; 1886598f1340Sdrh sqlite3Atoi64(z, &value); 1887*9de221dfSdrh if( negFlag ) value = -value; 1888598f1340Sdrh zV = dup8bytes(v, (char*)&value); 1889*9de221dfSdrh sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); 1890fec19aadSdrh }else{ 1891*9de221dfSdrh codeReal(v, z, n, negFlag, iMem); 1892fec19aadSdrh } 1893fec19aadSdrh } 1894c9cf901dSdanielk1977 } 1895fec19aadSdrh 1896945498f3Sdrh 1897945498f3Sdrh /* 1898945498f3Sdrh ** Generate code that will extract the iColumn-th column from 18992133d822Sdrh ** table pTab and store the column value in register iMem, or on 19002133d822Sdrh ** the stack if iMem==0. There is an open cursor to pTab in 19012133d822Sdrh ** iTable. If iColumn<0 then code is generated that extracts the rowid. 1902945498f3Sdrh */ 19032133d822Sdrh void sqlite3ExprCodeGetColumn( 19042133d822Sdrh Vdbe *v, /* The VM being created */ 19052133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 19062133d822Sdrh int iColumn, /* Index of the table column */ 19072133d822Sdrh int iTable, /* The cursor pointing to the table */ 19082133d822Sdrh int iReg /* Store results here */ 19092133d822Sdrh ){ 1910945498f3Sdrh if( iColumn<0 ){ 1911945498f3Sdrh int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; 19122133d822Sdrh sqlite3VdbeAddOp2(v, op, iTable, iReg); 1913945498f3Sdrh }else if( pTab==0 ){ 19142133d822Sdrh sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg); 1915945498f3Sdrh }else{ 1916945498f3Sdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 19172133d822Sdrh sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); 1918945498f3Sdrh sqlite3ColumnDefault(v, pTab, iColumn); 1919945498f3Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 1920945498f3Sdrh if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ 19212133d822Sdrh sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); 1922945498f3Sdrh } 1923945498f3Sdrh #endif 1924945498f3Sdrh } 1925945498f3Sdrh } 1926945498f3Sdrh 1927fec19aadSdrh /* 1928cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 1929389a1adbSdrh ** expression and leaves the result in a register on on the stack. 1930389a1adbSdrh ** 1931389a1adbSdrh ** If the target register number is negative, allocate a new 1932389a1adbSdrh ** register to store the result. If the target register number 1933389a1adbSdrh ** is zero then push the result onto the stack. Return the target 1934389a1adbSdrh ** register number regardless. 1935f2bc013cSdrh ** 1936f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1937f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1938f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1939f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1940f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1941cce7d176Sdrh */ 1942389a1adbSdrh int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 1943cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1944cce7d176Sdrh int op; 1945389a1adbSdrh int inReg = 0; 1946389a1adbSdrh int stackChng = 0; 1947*9de221dfSdrh int origTarget = target; 1948ffe07b2dSdrh 1949389a1adbSdrh assert( v!=0 || pParse->db->mallocFailed ); 1950389a1adbSdrh if( v==0 ) return 0; 1951389a1adbSdrh if( target<0 ){ 1952389a1adbSdrh target = ++pParse->nMem; 1953389a1adbSdrh }else if( target==0 ){ 1954389a1adbSdrh stackChng = 1; 19557977a17fSdanielk1977 } 1956389a1adbSdrh 1957389a1adbSdrh if( pExpr==0 ){ 1958389a1adbSdrh op = TK_NULL; 1959389a1adbSdrh }else{ 1960f2bc013cSdrh op = pExpr->op; 1961389a1adbSdrh } 1962f2bc013cSdrh switch( op ){ 196313449892Sdrh case TK_AGG_COLUMN: { 196413449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 196513449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 196613449892Sdrh if( !pAggInfo->directMode ){ 1967*9de221dfSdrh assert( pCol->iMem>0 ); 1968*9de221dfSdrh inReg = pCol->iMem; 196913449892Sdrh break; 197013449892Sdrh }else if( pAggInfo->useSortingIdx ){ 1971389a1adbSdrh sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, 1972389a1adbSdrh pCol->iSorterColumn, target); 1973*9de221dfSdrh inReg = target; 197413449892Sdrh break; 197513449892Sdrh } 197613449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 197713449892Sdrh } 1978967e8b73Sdrh case TK_COLUMN: { 1979ffe07b2dSdrh if( pExpr->iTable<0 ){ 1980ffe07b2dSdrh /* This only happens when coding check constraints */ 1981ffe07b2dSdrh assert( pParse->ckOffset>0 ); 1982b1fdb2adSdrh sqlite3VdbeAddOp1(v, OP_SCopy, -(pParse->ckOffset-pExpr->iColumn-1)); 1983*9de221dfSdrh /* inReg = -(pParse->ckOffset-pExpr->iColumn-1); */ 1984c4a3c779Sdrh }else{ 19852133d822Sdrh sqlite3ExprCodeGetColumn(v, pExpr->pTab, 1986389a1adbSdrh pExpr->iColumn, pExpr->iTable, target); 1987*9de221dfSdrh inReg = target; 19882282792aSdrh } 1989cce7d176Sdrh break; 1990cce7d176Sdrh } 1991cce7d176Sdrh case TK_INTEGER: { 1992*9de221dfSdrh codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); 1993*9de221dfSdrh inReg = target; 1994fec19aadSdrh break; 199551e9a445Sdrh } 1996598f1340Sdrh case TK_FLOAT: { 1997*9de221dfSdrh codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); 1998*9de221dfSdrh inReg = target; 1999598f1340Sdrh break; 2000598f1340Sdrh } 2001fec19aadSdrh case TK_STRING: { 20021e536953Sdanielk1977 sqlite3DequoteExpr(pParse->db, pExpr); 2003*9de221dfSdrh sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0, 200466a5167bSdrh (char*)pExpr->token.z, pExpr->token.n); 2005*9de221dfSdrh inReg = target; 2006cce7d176Sdrh break; 2007cce7d176Sdrh } 2008f0863fe5Sdrh case TK_NULL: { 2009*9de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 2010*9de221dfSdrh inReg = target; 2011f0863fe5Sdrh break; 2012f0863fe5Sdrh } 20135338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 2014c572ef7fSdanielk1977 case TK_BLOB: { 20156c8c6cecSdrh int n; 20166c8c6cecSdrh const char *z; 2017f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 20186c8c6cecSdrh n = pExpr->token.n - 3; 20192646da7eSdrh z = (char*)pExpr->token.z + 2; 20206c8c6cecSdrh assert( n>=0 ); 20216c8c6cecSdrh if( n==0 ){ 20226c8c6cecSdrh z = ""; 20236c8c6cecSdrh } 2024*9de221dfSdrh sqlite3VdbeAddOp4(v, op, 0, target, 0, z, n); 2025*9de221dfSdrh inReg = target; 2026c572ef7fSdanielk1977 break; 2027c572ef7fSdanielk1977 } 20285338a5f7Sdanielk1977 #endif 202950457896Sdrh case TK_VARIABLE: { 2030*9de221dfSdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target); 2031895d7472Sdrh if( pExpr->token.n>1 ){ 203266a5167bSdrh sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n); 2033895d7472Sdrh } 2034*9de221dfSdrh inReg = target; 203550457896Sdrh break; 203650457896Sdrh } 20374e0cff60Sdrh case TK_REGISTER: { 2038*9de221dfSdrh inReg = pExpr->iTable; 20394e0cff60Sdrh break; 20404e0cff60Sdrh } 2041487e262fSdrh #ifndef SQLITE_OMIT_CAST 2042487e262fSdrh case TK_CAST: { 2043487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 2044f0113000Sdanielk1977 int aff, to_op; 2045*9de221dfSdrh sqlite3ExprCode(pParse, pExpr->pLeft, target); 20468a51256cSdrh aff = sqlite3AffinityType(&pExpr->token); 2047f0113000Sdanielk1977 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; 2048f0113000Sdanielk1977 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); 2049f0113000Sdanielk1977 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); 2050f0113000Sdanielk1977 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); 2051f0113000Sdanielk1977 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); 2052f0113000Sdanielk1977 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); 2053*9de221dfSdrh sqlite3VdbeAddOp1(v, to_op, target); 2054ffe07b2dSdrh stackChng = 0; 2055*9de221dfSdrh inReg = target; 2056487e262fSdrh break; 2057487e262fSdrh } 2058487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 2059c9b84a1fSdrh case TK_LT: 2060c9b84a1fSdrh case TK_LE: 2061c9b84a1fSdrh case TK_GT: 2062c9b84a1fSdrh case TK_GE: 2063c9b84a1fSdrh case TK_NE: 2064c9b84a1fSdrh case TK_EQ: { 2065f2bc013cSdrh assert( TK_LT==OP_Lt ); 2066f2bc013cSdrh assert( TK_LE==OP_Le ); 2067f2bc013cSdrh assert( TK_GT==OP_Gt ); 2068f2bc013cSdrh assert( TK_GE==OP_Ge ); 2069f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2070f2bc013cSdrh assert( TK_NE==OP_Ne ); 2071389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 2072389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pRight, 0); 2073be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 2074ffe07b2dSdrh stackChng = -1; 2075a37cdde0Sdanielk1977 break; 2076c9b84a1fSdrh } 2077cce7d176Sdrh case TK_AND: 2078cce7d176Sdrh case TK_OR: 2079cce7d176Sdrh case TK_PLUS: 2080cce7d176Sdrh case TK_STAR: 2081cce7d176Sdrh case TK_MINUS: 2082bf4133cbSdrh case TK_REM: 2083bf4133cbSdrh case TK_BITAND: 2084bf4133cbSdrh case TK_BITOR: 208517c40294Sdrh case TK_SLASH: 2086bf4133cbSdrh case TK_LSHIFT: 2087855eb1cfSdrh case TK_RSHIFT: 20880040077dSdrh case TK_CONCAT: { 2089f2bc013cSdrh assert( TK_AND==OP_And ); 2090f2bc013cSdrh assert( TK_OR==OP_Or ); 2091f2bc013cSdrh assert( TK_PLUS==OP_Add ); 2092f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 2093f2bc013cSdrh assert( TK_REM==OP_Remainder ); 2094f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 2095f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 2096f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 2097f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 2098f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 2099f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 2100389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 2101389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pRight, 0); 210266a5167bSdrh sqlite3VdbeAddOp0(v, op); 2103ffe07b2dSdrh stackChng = -1; 21040040077dSdrh break; 21050040077dSdrh } 2106cce7d176Sdrh case TK_UMINUS: { 2107fec19aadSdrh Expr *pLeft = pExpr->pLeft; 2108fec19aadSdrh assert( pLeft ); 2109fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 2110fec19aadSdrh Token *p = &pLeft->token; 2111fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 2112*9de221dfSdrh codeReal(v, (char*)p->z, p->n, 1, target); 2113e6840900Sdrh }else{ 2114*9de221dfSdrh codeInteger(v, (char*)p->z, p->n, 1, target); 2115e6840900Sdrh } 2116*9de221dfSdrh inReg = target; 21176e142f54Sdrh break; 21186e142f54Sdrh } 21191ccde15dSdrh /* Fall through into TK_NOT */ 21206e142f54Sdrh } 2121bf4133cbSdrh case TK_BITNOT: 21226e142f54Sdrh case TK_NOT: { 2123f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 2124f2bc013cSdrh assert( TK_NOT==OP_Not ); 2125389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 212666a5167bSdrh sqlite3VdbeAddOp0(v, op); 2127ffe07b2dSdrh stackChng = 0; 2128cce7d176Sdrh break; 2129cce7d176Sdrh } 2130cce7d176Sdrh case TK_ISNULL: 2131cce7d176Sdrh case TK_NOTNULL: { 2132cce7d176Sdrh int dest; 2133f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2134f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 2135*9de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 2136389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 21374adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 213866a5167bSdrh sqlite3VdbeAddOp2(v, op, 1, dest); 2139*9de221dfSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); 2140ffe07b2dSdrh stackChng = 0; 2141*9de221dfSdrh inReg = target; 2142a37cdde0Sdanielk1977 break; 2143f2bc013cSdrh } 21442282792aSdrh case TK_AGG_FUNCTION: { 214513449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 21467e56e711Sdrh if( pInfo==0 ){ 21477e56e711Sdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", 21487e56e711Sdrh &pExpr->span); 21497e56e711Sdrh }else{ 2150*9de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 21517e56e711Sdrh } 21522282792aSdrh break; 21532282792aSdrh } 2154b71090fdSdrh case TK_CONST_FUNC: 2155cce7d176Sdrh case TK_FUNCTION: { 2156cce7d176Sdrh ExprList *pList = pExpr->pList; 215789425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 21580bce8354Sdrh FuncDef *pDef; 21594b59ab5eSdrh int nId; 21604b59ab5eSdrh const char *zId; 216113449892Sdrh int constMask = 0; 2162682f68b0Sdanielk1977 int i; 216317435752Sdrh sqlite3 *db = pParse->db; 216417435752Sdrh u8 enc = ENC(db); 2165dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 216617435752Sdrh 21672646da7eSdrh zId = (char*)pExpr->token.z; 2168b71090fdSdrh nId = pExpr->token.n; 2169d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 21700bce8354Sdrh assert( pDef!=0 ); 2171389a1adbSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList, 0); 2172b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 2173a43fa227Sdrh /* Possibly overload the function if the first argument is 2174a43fa227Sdrh ** a virtual table column. 2175a43fa227Sdrh ** 2176a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 2177a43fa227Sdrh ** second argument, not the first, as the argument to test to 2178a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 2179a43fa227Sdrh ** the left operand of infix functions (the operand we want to 2180a43fa227Sdrh ** control overloading) ends up as the second argument to the 2181a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 2182a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 2183a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 2184a43fa227Sdrh */ 21856a03a1c5Sdrh if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ 218617435752Sdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); 21876a03a1c5Sdrh }else if( nExpr>0 ){ 218817435752Sdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); 2189b7f6f68fSdrh } 2190b7f6f68fSdrh #endif 2191682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 2192d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 219313449892Sdrh constMask |= (1<<i); 2194d02eb1fdSdanielk1977 } 2195dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 2196dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 2197dc1bdc4fSdanielk1977 } 2198dc1bdc4fSdanielk1977 } 2199dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 2200dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 220166a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 2202682f68b0Sdanielk1977 } 220366a5167bSdrh sqlite3VdbeAddOp4(v, OP_Function, constMask, nExpr, 0, 220466a5167bSdrh (char*)pDef, P4_FUNCDEF); 2205ffe07b2dSdrh stackChng = 1-nExpr; 22066ec2733bSdrh break; 22076ec2733bSdrh } 2208fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 2209fe2093d7Sdrh case TK_EXISTS: 221019a775c2Sdrh case TK_SELECT: { 221141714d6fSdrh if( pExpr->iColumn==0 ){ 2212b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 221341714d6fSdrh } 2214*9de221dfSdrh inReg = pExpr->iColumn; 2215*9de221dfSdrh /* sqlite3VdbeAddOp1(v, OP_SCopy, pExpr->iColumn); 2216*9de221dfSdrh VdbeComment((v, "load subquery result")); */ 221719a775c2Sdrh break; 221819a775c2Sdrh } 2219fef5208cSdrh case TK_IN: { 2220fef5208cSdrh int addr; 222194a11211Sdrh char affinity; 2222afa5f680Sdrh int ckOffset = pParse->ckOffset; 22239a96b668Sdanielk1977 int eType; 22249a96b668Sdanielk1977 int iLabel = sqlite3VdbeMakeLabel(v); 22259a96b668Sdanielk1977 22269a96b668Sdanielk1977 eType = sqlite3FindInIndex(pParse, pExpr, 0); 2227e014a838Sdanielk1977 2228e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 2229e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 223066a5167bSdrh ** P4 of OP_MakeRecord. 2231e014a838Sdanielk1977 */ 223294a11211Sdrh affinity = comparisonAffinity(pExpr); 2233e014a838Sdanielk1977 223466a5167bSdrh sqlite3VdbeAddOp1(v, OP_Integer, 1); 2235cdbd8effSdanielk1977 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0); 2236e014a838Sdanielk1977 2237e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 2238e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 2239e014a838Sdanielk1977 */ 2240389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 22414adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 224266a5167bSdrh sqlite3VdbeAddOp2(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 224366a5167bSdrh sqlite3VdbeAddOp1(v, OP_Pop, 2); 224466a5167bSdrh sqlite3VdbeAddOp0(v, OP_Null); 224566a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, iLabel); 22469a96b668Sdanielk1977 if( eType==IN_INDEX_ROWID ){ 22479a96b668Sdanielk1977 int iAddr = sqlite3VdbeCurrentAddr(v)+3; 224866a5167bSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, 1, iAddr); 224966a5167bSdrh sqlite3VdbeAddOp2(v, OP_NotExists, pExpr->iTable, iAddr); 225066a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, pExpr->iTable, iLabel); 22519a96b668Sdanielk1977 }else{ 225266a5167bSdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, 1, 0, 0, 225366a5167bSdrh &affinity, 1); /* addr + 4 */ 225466a5167bSdrh sqlite3VdbeAddOp2(v, OP_Found, pExpr->iTable, iLabel); 22559a96b668Sdanielk1977 } 22568558cde1Sdrh sqlite3VdbeAddOp2(v, OP_AddImm, 0, -1); /* addr + 6 */ 22579a96b668Sdanielk1977 sqlite3VdbeResolveLabel(v, iLabel); 2258e014a838Sdanielk1977 2259fef5208cSdrh break; 2260fef5208cSdrh } 226193758c8dSdanielk1977 #endif 2262fef5208cSdrh case TK_BETWEEN: { 2263be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2264be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 2265be5c89acSdrh Expr *pRight = pLItem->pExpr; 2266389a1adbSdrh sqlite3ExprCode(pParse, pLeft, 0); 2267b1fdb2adSdrh sqlite3VdbeAddOp0(v, OP_Copy); 2268389a1adbSdrh sqlite3ExprCode(pParse, pRight, 0); 2269be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 227066a5167bSdrh sqlite3VdbeAddOp1(v, OP_Pull, 1); 2271be5c89acSdrh pLItem++; 2272be5c89acSdrh pRight = pLItem->pExpr; 2273389a1adbSdrh sqlite3ExprCode(pParse, pRight, 0); 2274be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 227566a5167bSdrh sqlite3VdbeAddOp0(v, OP_And); 2276fef5208cSdrh break; 2277fef5208cSdrh } 22784f07e5fbSdrh case TK_UPLUS: { 2279*9de221dfSdrh inReg = sqlite3ExprCode(pParse, pExpr->pLeft, origTarget); 2280ffe07b2dSdrh stackChng = 0; 2281a2e00042Sdrh break; 2282a2e00042Sdrh } 228317a7f8ddSdrh case TK_CASE: { 228417a7f8ddSdrh int expr_end_label; 2285f5905aa7Sdrh int jumpInst; 2286f5905aa7Sdrh int nExpr; 228717a7f8ddSdrh int i; 2288be5c89acSdrh ExprList *pEList; 2289be5c89acSdrh struct ExprList_item *aListelem; 229017a7f8ddSdrh 229117a7f8ddSdrh assert(pExpr->pList); 229217a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 229317a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 2294be5c89acSdrh pEList = pExpr->pList; 2295be5c89acSdrh aListelem = pEList->a; 2296be5c89acSdrh nExpr = pEList->nExpr; 22974adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 229817a7f8ddSdrh if( pExpr->pLeft ){ 2299389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 2300cce7d176Sdrh } 2301f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 2302389a1adbSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr, 0); 230317a7f8ddSdrh if( pExpr->pLeft ){ 2304b1fdb2adSdrh sqlite3VdbeAddOp1(v, OP_SCopy, -1); 2305be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 2306be5c89acSdrh OP_Ne, 0, 1); 230766a5167bSdrh sqlite3VdbeAddOp1(v, OP_Pop, 1); 2308f5905aa7Sdrh }else{ 230966a5167bSdrh jumpInst = sqlite3VdbeAddOp2(v, OP_IfNot, 1, 0); 231017a7f8ddSdrh } 2311*9de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 231266a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, expr_end_label); 2313d654be80Sdrh sqlite3VdbeJumpHere(v, jumpInst); 231417a7f8ddSdrh } 2315f570f011Sdrh if( pExpr->pLeft ){ 231666a5167bSdrh sqlite3VdbeAddOp2(v, OP_Pop, 1, 0); 2317f570f011Sdrh } 231817a7f8ddSdrh if( pExpr->pRight ){ 2319*9de221dfSdrh sqlite3ExprCode(pParse, pExpr->pRight, target); 232017a7f8ddSdrh }else{ 2321*9de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 232217a7f8ddSdrh } 23234adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 2324*9de221dfSdrh inReg = target; 23256f34903eSdanielk1977 break; 23266f34903eSdanielk1977 } 23275338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 23286f34903eSdanielk1977 case TK_RAISE: { 23296f34903eSdanielk1977 if( !pParse->trigStack ){ 23304adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2331da93d238Sdrh "RAISE() may only be used within a trigger-program"); 2332389a1adbSdrh return 0; 23336f34903eSdanielk1977 } 2334ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 2335ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 23366f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 2337ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 23381e536953Sdanielk1977 sqlite3DequoteExpr(pParse->db, pExpr); 233966a5167bSdrh sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0, 23402646da7eSdrh (char*)pExpr->token.z, pExpr->token.n); 23416f34903eSdanielk1977 } else { 23426f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 234366a5167bSdrh sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); 234466a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 2345d4e70ebdSdrh VdbeComment((v, "raise(IGNORE)")); 23466f34903eSdanielk1977 } 2347ffe07b2dSdrh stackChng = 0; 2348ffe07b2dSdrh break; 234917a7f8ddSdrh } 23505338a5f7Sdanielk1977 #endif 2351ffe07b2dSdrh } 2352*9de221dfSdrh if( inReg!=target && origTarget!=-1 ){ 2353*9de221dfSdrh sqlite3VdbeAddOp2(v, (inReg>0 ? OP_SCopy : OP_Move), inReg, target); 2354389a1adbSdrh stackChng = 0; 2355389a1adbSdrh } 2356ffe07b2dSdrh if( pParse->ckOffset ){ 2357ffe07b2dSdrh pParse->ckOffset += stackChng; 2358ffe07b2dSdrh assert( pParse->ckOffset ); 235917a7f8ddSdrh } 2360389a1adbSdrh return target; 2361cce7d176Sdrh } 2362cce7d176Sdrh 236393758c8dSdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 2364cce7d176Sdrh /* 236525303780Sdrh ** Generate code that evalutes the given expression and leaves the result 236625303780Sdrh ** on the stack. See also sqlite3ExprCode(). 236725303780Sdrh ** 236825303780Sdrh ** This routine might also cache the result and modify the pExpr tree 236925303780Sdrh ** so that it will make use of the cached result on subsequent evaluations 237025303780Sdrh ** rather than evaluate the whole expression again. Trivial expressions are 237125303780Sdrh ** not cached. If the expression is cached, its result is stored in a 237225303780Sdrh ** memory location. 237325303780Sdrh */ 237425303780Sdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ 237525303780Sdrh Vdbe *v = pParse->pVdbe; 237649df6b74Sdrh VdbeOp *pOp; 237725303780Sdrh int iMem; 237825303780Sdrh int addr1, addr2; 237925303780Sdrh if( v==0 ) return; 238025303780Sdrh addr1 = sqlite3VdbeCurrentAddr(v); 2381389a1adbSdrh sqlite3ExprCode(pParse, pExpr, 0); 238225303780Sdrh addr2 = sqlite3VdbeCurrentAddr(v); 238349df6b74Sdrh if( addr2>addr1+1 238449df6b74Sdrh || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){ 23850a07c107Sdrh iMem = pExpr->iTable = ++pParse->nMem; 2386b1fdb2adSdrh sqlite3VdbeAddOp2(v, OP_Copy, 0, iMem); 238725303780Sdrh pExpr->op = TK_REGISTER; 238825303780Sdrh } 238925303780Sdrh } 239093758c8dSdanielk1977 #endif 239125303780Sdrh 239225303780Sdrh /* 2393268380caSdrh ** Generate code that pushes the value of every element of the given 2394f9b596ebSdrh ** expression list onto the stack. 2395268380caSdrh ** 2396268380caSdrh ** Return the number of elements pushed onto the stack. 2397268380caSdrh */ 23984adee20fSdanielk1977 int sqlite3ExprCodeExprList( 2399268380caSdrh Parse *pParse, /* Parsing context */ 2400389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 2401389a1adbSdrh int target /* Where to write results */ 2402268380caSdrh ){ 2403268380caSdrh struct ExprList_item *pItem; 2404389a1adbSdrh int i, n, incr = 1; 2405268380caSdrh if( pList==0 ) return 0; 2406268380caSdrh n = pList->nExpr; 2407389a1adbSdrh if( target<0 ){ 2408389a1adbSdrh target = pParse->nMem+1; 2409389a1adbSdrh pParse->nMem += n; 2410389a1adbSdrh }else if( target==0 ){ 2411389a1adbSdrh incr = 0; 2412389a1adbSdrh } 2413c182d163Sdrh for(pItem=pList->a, i=n; i>0; i--, pItem++){ 2414389a1adbSdrh sqlite3ExprCode(pParse, pItem->pExpr, target); 2415389a1adbSdrh target += incr; 2416268380caSdrh } 2417f9b596ebSdrh return n; 2418268380caSdrh } 2419268380caSdrh 2420268380caSdrh /* 2421cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 2422cce7d176Sdrh ** to the label "dest" if the expression is true but execution 2423cce7d176Sdrh ** continues straight thru if the expression is false. 2424f5905aa7Sdrh ** 2425f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 2426f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 2427f2bc013cSdrh ** 2428f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 2429f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 2430f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 2431f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 2432f2bc013cSdrh ** below verify that the numbers are aligned correctly. 2433cce7d176Sdrh */ 24344adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2435cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2436cce7d176Sdrh int op = 0; 2437ffe07b2dSdrh int ckOffset = pParse->ckOffset; 2438daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2439f2bc013cSdrh op = pExpr->op; 2440f2bc013cSdrh switch( op ){ 2441cce7d176Sdrh case TK_AND: { 24424adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 24434adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 24444adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 24454adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2446cce7d176Sdrh break; 2447cce7d176Sdrh } 2448cce7d176Sdrh case TK_OR: { 24494adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 24504adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 2451cce7d176Sdrh break; 2452cce7d176Sdrh } 2453cce7d176Sdrh case TK_NOT: { 24544adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 2455cce7d176Sdrh break; 2456cce7d176Sdrh } 2457cce7d176Sdrh case TK_LT: 2458cce7d176Sdrh case TK_LE: 2459cce7d176Sdrh case TK_GT: 2460cce7d176Sdrh case TK_GE: 2461cce7d176Sdrh case TK_NE: 24620ac65892Sdrh case TK_EQ: { 2463f2bc013cSdrh assert( TK_LT==OP_Lt ); 2464f2bc013cSdrh assert( TK_LE==OP_Le ); 2465f2bc013cSdrh assert( TK_GT==OP_Gt ); 2466f2bc013cSdrh assert( TK_GE==OP_Ge ); 2467f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2468f2bc013cSdrh assert( TK_NE==OP_Ne ); 2469389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 2470389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pRight, 0); 2471be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 2472cce7d176Sdrh break; 2473cce7d176Sdrh } 2474cce7d176Sdrh case TK_ISNULL: 2475cce7d176Sdrh case TK_NOTNULL: { 2476f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2477f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 2478389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 247966a5167bSdrh sqlite3VdbeAddOp2(v, op, 1, dest); 2480cce7d176Sdrh break; 2481cce7d176Sdrh } 2482fef5208cSdrh case TK_BETWEEN: { 24830202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 24840202b29eSdanielk1977 ** 24850202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 24860202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 24870202b29eSdanielk1977 ** 3 ... 24880202b29eSdanielk1977 */ 2489f5905aa7Sdrh int addr; 2490be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2491be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 2492389a1adbSdrh sqlite3ExprCode(pParse, pLeft, 0); 2493b1fdb2adSdrh sqlite3VdbeAddOp0(v, OP_Copy); 2494389a1adbSdrh sqlite3ExprCode(pParse, pRight, 0); 2495be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 24960202b29eSdanielk1977 2497be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 2498389a1adbSdrh sqlite3ExprCode(pParse, pRight, 0); 2499be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 25000202b29eSdanielk1977 250166a5167bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, 0); 2502d654be80Sdrh sqlite3VdbeJumpHere(v, addr); 250366a5167bSdrh sqlite3VdbeAddOp2(v, OP_Pop, 1, 0); 2504fef5208cSdrh break; 2505fef5208cSdrh } 2506cce7d176Sdrh default: { 2507389a1adbSdrh sqlite3ExprCode(pParse, pExpr, 0); 250866a5167bSdrh sqlite3VdbeAddOp2(v, OP_If, jumpIfNull, dest); 2509cce7d176Sdrh break; 2510cce7d176Sdrh } 2511cce7d176Sdrh } 2512ffe07b2dSdrh pParse->ckOffset = ckOffset; 2513cce7d176Sdrh } 2514cce7d176Sdrh 2515cce7d176Sdrh /* 251666b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 2517cce7d176Sdrh ** to the label "dest" if the expression is false but execution 2518cce7d176Sdrh ** continues straight thru if the expression is true. 2519f5905aa7Sdrh ** 2520f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 2521f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 2522cce7d176Sdrh */ 25234adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2524cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2525cce7d176Sdrh int op = 0; 2526ffe07b2dSdrh int ckOffset = pParse->ckOffset; 2527daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2528f2bc013cSdrh 2529f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 2530f2bc013cSdrh ** 2531f2bc013cSdrh ** pExpr->op op 2532f2bc013cSdrh ** --------- ---------- 2533f2bc013cSdrh ** TK_ISNULL OP_NotNull 2534f2bc013cSdrh ** TK_NOTNULL OP_IsNull 2535f2bc013cSdrh ** TK_NE OP_Eq 2536f2bc013cSdrh ** TK_EQ OP_Ne 2537f2bc013cSdrh ** TK_GT OP_Le 2538f2bc013cSdrh ** TK_LE OP_Gt 2539f2bc013cSdrh ** TK_GE OP_Lt 2540f2bc013cSdrh ** TK_LT OP_Ge 2541f2bc013cSdrh ** 2542f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 2543f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 2544f2bc013cSdrh ** can compute the mapping above using the following expression. 2545f2bc013cSdrh ** Assert()s verify that the computation is correct. 2546f2bc013cSdrh */ 2547f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 2548f2bc013cSdrh 2549f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 2550f2bc013cSdrh */ 2551f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 2552f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 2553f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 2554f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 2555f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 2556f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 2557f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 2558f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 2559f2bc013cSdrh 2560cce7d176Sdrh switch( pExpr->op ){ 2561cce7d176Sdrh case TK_AND: { 25624adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 25634adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 2564cce7d176Sdrh break; 2565cce7d176Sdrh } 2566cce7d176Sdrh case TK_OR: { 25674adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 25684adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 25694adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 25704adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2571cce7d176Sdrh break; 2572cce7d176Sdrh } 2573cce7d176Sdrh case TK_NOT: { 25744adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 2575cce7d176Sdrh break; 2576cce7d176Sdrh } 2577cce7d176Sdrh case TK_LT: 2578cce7d176Sdrh case TK_LE: 2579cce7d176Sdrh case TK_GT: 2580cce7d176Sdrh case TK_GE: 2581cce7d176Sdrh case TK_NE: 2582cce7d176Sdrh case TK_EQ: { 2583389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 2584389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pRight, 0); 2585be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 2586cce7d176Sdrh break; 2587cce7d176Sdrh } 2588cce7d176Sdrh case TK_ISNULL: 2589cce7d176Sdrh case TK_NOTNULL: { 2590389a1adbSdrh sqlite3ExprCode(pParse, pExpr->pLeft, 0); 259166a5167bSdrh sqlite3VdbeAddOp2(v, op, 1, dest); 2592cce7d176Sdrh break; 2593cce7d176Sdrh } 2594fef5208cSdrh case TK_BETWEEN: { 25950202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 25960202b29eSdanielk1977 ** 25970202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 25980202b29eSdanielk1977 ** 2 GOTO <dest> 25990202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 26000202b29eSdanielk1977 */ 2601fef5208cSdrh int addr; 2602be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2603be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 2604389a1adbSdrh sqlite3ExprCode(pParse, pLeft, 0); 2605b1fdb2adSdrh sqlite3VdbeAddOp0(v, OP_Copy); 2606389a1adbSdrh sqlite3ExprCode(pParse, pRight, 0); 26074adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 2608be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 2609be5c89acSdrh 261066a5167bSdrh sqlite3VdbeAddOp2(v, OP_Pop, 1, 0); 261166a5167bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); 2612be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 2613389a1adbSdrh sqlite3ExprCode(pParse, pRight, 0); 2614be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 2615fef5208cSdrh break; 2616fef5208cSdrh } 2617cce7d176Sdrh default: { 2618389a1adbSdrh sqlite3ExprCode(pParse, pExpr, 0); 261966a5167bSdrh sqlite3VdbeAddOp2(v, OP_IfNot, jumpIfNull, dest); 2620cce7d176Sdrh break; 2621cce7d176Sdrh } 2622cce7d176Sdrh } 2623ffe07b2dSdrh pParse->ckOffset = ckOffset; 2624cce7d176Sdrh } 26252282792aSdrh 26262282792aSdrh /* 26272282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 26282282792aSdrh ** if they are identical and return FALSE if they differ in any way. 2629d40aab0eSdrh ** 2630d40aab0eSdrh ** Sometimes this routine will return FALSE even if the two expressions 2631d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 2632d40aab0eSdrh ** identical, we return FALSE just to be safe. So if this routine 2633d40aab0eSdrh ** returns false, then you do not really know for certain if the two 2634d40aab0eSdrh ** expressions are the same. But if you get a TRUE return, then you 2635d40aab0eSdrh ** can be sure the expressions are the same. In the places where 2636d40aab0eSdrh ** this routine is used, it does not hurt to get an extra FALSE - that 2637d40aab0eSdrh ** just might result in some slightly slower code. But returning 2638d40aab0eSdrh ** an incorrect TRUE could lead to a malfunction. 26392282792aSdrh */ 26404adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 26412282792aSdrh int i; 26424b202ae2Sdanielk1977 if( pA==0||pB==0 ){ 26434b202ae2Sdanielk1977 return pB==pA; 26442282792aSdrh } 26452282792aSdrh if( pA->op!=pB->op ) return 0; 2646fd357974Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; 26474adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 26484adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 26492282792aSdrh if( pA->pList ){ 26502282792aSdrh if( pB->pList==0 ) return 0; 26512282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 26522282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 26534adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 26542282792aSdrh return 0; 26552282792aSdrh } 26562282792aSdrh } 26572282792aSdrh }else if( pB->pList ){ 26582282792aSdrh return 0; 26592282792aSdrh } 26602282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 26612f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 2662dd73521bSdrh if( pA->op!=TK_COLUMN && pA->token.z ){ 26632282792aSdrh if( pB->token.z==0 ) return 0; 26646977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 26652646da7eSdrh if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ 26662646da7eSdrh return 0; 26672646da7eSdrh } 26682282792aSdrh } 26692282792aSdrh return 1; 26702282792aSdrh } 26712282792aSdrh 267213449892Sdrh 26732282792aSdrh /* 267413449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 267513449892Sdrh ** the new element. Return a negative number if malloc fails. 26762282792aSdrh */ 267717435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 267813449892Sdrh int i; 2679cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 268017435752Sdrh db, 2681cf643729Sdrh pInfo->aCol, 2682cf643729Sdrh sizeof(pInfo->aCol[0]), 2683cf643729Sdrh 3, 2684cf643729Sdrh &pInfo->nColumn, 2685cf643729Sdrh &pInfo->nColumnAlloc, 2686cf643729Sdrh &i 2687cf643729Sdrh ); 268813449892Sdrh return i; 26892282792aSdrh } 269013449892Sdrh 269113449892Sdrh /* 269213449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 269313449892Sdrh ** the new element. Return a negative number if malloc fails. 269413449892Sdrh */ 269517435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 269613449892Sdrh int i; 2697cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 269817435752Sdrh db, 2699cf643729Sdrh pInfo->aFunc, 2700cf643729Sdrh sizeof(pInfo->aFunc[0]), 2701cf643729Sdrh 3, 2702cf643729Sdrh &pInfo->nFunc, 2703cf643729Sdrh &pInfo->nFuncAlloc, 2704cf643729Sdrh &i 2705cf643729Sdrh ); 270613449892Sdrh return i; 27072282792aSdrh } 27082282792aSdrh 27092282792aSdrh /* 2710626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 2711626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 2712626a879aSdrh ** for additional information. 27132282792aSdrh ** 2714626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 27152282792aSdrh */ 2716626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 27172282792aSdrh int i; 2718a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 2719a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 2720a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 272113449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 272213449892Sdrh 27232282792aSdrh switch( pExpr->op ){ 272489c69d00Sdrh case TK_AGG_COLUMN: 2725967e8b73Sdrh case TK_COLUMN: { 272613449892Sdrh /* Check to see if the column is in one of the tables in the FROM 272713449892Sdrh ** clause of the aggregate query */ 272813449892Sdrh if( pSrcList ){ 272913449892Sdrh struct SrcList_item *pItem = pSrcList->a; 273013449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 273113449892Sdrh struct AggInfo_col *pCol; 273213449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 273313449892Sdrh /* If we reach this point, it means that pExpr refers to a table 273413449892Sdrh ** that is in the FROM clause of the aggregate query. 273513449892Sdrh ** 273613449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 273713449892Sdrh ** is not an entry there already. 273813449892Sdrh */ 27397f906d63Sdrh int k; 274013449892Sdrh pCol = pAggInfo->aCol; 27417f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 274213449892Sdrh if( pCol->iTable==pExpr->iTable && 274313449892Sdrh pCol->iColumn==pExpr->iColumn ){ 27442282792aSdrh break; 27452282792aSdrh } 27462282792aSdrh } 27471e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 27481e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 27491e536953Sdanielk1977 ){ 27507f906d63Sdrh pCol = &pAggInfo->aCol[k]; 27510817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 275213449892Sdrh pCol->iTable = pExpr->iTable; 275313449892Sdrh pCol->iColumn = pExpr->iColumn; 27540a07c107Sdrh pCol->iMem = ++pParse->nMem; 275513449892Sdrh pCol->iSorterColumn = -1; 27565774b806Sdrh pCol->pExpr = pExpr; 275713449892Sdrh if( pAggInfo->pGroupBy ){ 275813449892Sdrh int j, n; 275913449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 276013449892Sdrh struct ExprList_item *pTerm = pGB->a; 276113449892Sdrh n = pGB->nExpr; 276213449892Sdrh for(j=0; j<n; j++, pTerm++){ 276313449892Sdrh Expr *pE = pTerm->pExpr; 276413449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 276513449892Sdrh pE->iColumn==pExpr->iColumn ){ 276613449892Sdrh pCol->iSorterColumn = j; 276713449892Sdrh break; 27682282792aSdrh } 276913449892Sdrh } 277013449892Sdrh } 277113449892Sdrh if( pCol->iSorterColumn<0 ){ 277213449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 277313449892Sdrh } 277413449892Sdrh } 277513449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 277613449892Sdrh ** because it was there before or because we just created it). 277713449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 277813449892Sdrh ** pAggInfo->aCol[] entry. 277913449892Sdrh */ 278013449892Sdrh pExpr->pAggInfo = pAggInfo; 278113449892Sdrh pExpr->op = TK_AGG_COLUMN; 27827f906d63Sdrh pExpr->iAgg = k; 278313449892Sdrh break; 278413449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 278513449892Sdrh } /* end loop over pSrcList */ 2786a58fdfb1Sdanielk1977 } 2787626a879aSdrh return 1; 27882282792aSdrh } 27892282792aSdrh case TK_AGG_FUNCTION: { 279013449892Sdrh /* The pNC->nDepth==0 test causes aggregate functions in subqueries 279113449892Sdrh ** to be ignored */ 2792a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 279313449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 279413449892Sdrh ** function that is already in the pAggInfo structure 279513449892Sdrh */ 279613449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 279713449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 279813449892Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ 27992282792aSdrh break; 28002282792aSdrh } 28012282792aSdrh } 280213449892Sdrh if( i>=pAggInfo->nFunc ){ 280313449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 280413449892Sdrh */ 280514db2665Sdanielk1977 u8 enc = ENC(pParse->db); 28061e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 280713449892Sdrh if( i>=0 ){ 280813449892Sdrh pItem = &pAggInfo->aFunc[i]; 280913449892Sdrh pItem->pExpr = pExpr; 28100a07c107Sdrh pItem->iMem = ++pParse->nMem; 281113449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 28122646da7eSdrh (char*)pExpr->token.z, pExpr->token.n, 2813d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 2814fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 2815fd357974Sdrh pItem->iDistinct = pParse->nTab++; 2816fd357974Sdrh }else{ 2817fd357974Sdrh pItem->iDistinct = -1; 2818fd357974Sdrh } 28192282792aSdrh } 282013449892Sdrh } 282113449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 282213449892Sdrh */ 28232282792aSdrh pExpr->iAgg = i; 282413449892Sdrh pExpr->pAggInfo = pAggInfo; 2825626a879aSdrh return 1; 28262282792aSdrh } 28272282792aSdrh } 2828a58fdfb1Sdanielk1977 } 282913449892Sdrh 283013449892Sdrh /* Recursively walk subqueries looking for TK_COLUMN nodes that need 283113449892Sdrh ** to be changed to TK_AGG_COLUMN. But increment nDepth so that 283213449892Sdrh ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. 283313449892Sdrh */ 2834a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 2835a58fdfb1Sdanielk1977 pNC->nDepth++; 2836a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 2837a58fdfb1Sdanielk1977 pNC->nDepth--; 2838a58fdfb1Sdanielk1977 } 2839626a879aSdrh return 0; 28402282792aSdrh } 2841626a879aSdrh 2842626a879aSdrh /* 2843626a879aSdrh ** Analyze the given expression looking for aggregate functions and 2844626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 2845626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 2846626a879aSdrh ** 2847626a879aSdrh ** This routine should only be called after the expression has been 2848626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 2849626a879aSdrh ** 2850626a879aSdrh ** If errors are seen, leave an error message in zErrMsg and return 2851626a879aSdrh ** the number of errors. 2852626a879aSdrh */ 2853a58fdfb1Sdanielk1977 int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 2854a58fdfb1Sdanielk1977 int nErr = pNC->pParse->nErr; 2855a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 2856a58fdfb1Sdanielk1977 return pNC->pParse->nErr - nErr; 28572282792aSdrh } 28585d9a4af9Sdrh 28595d9a4af9Sdrh /* 28605d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 28615d9a4af9Sdrh ** expression list. Return the number of errors. 28625d9a4af9Sdrh ** 28635d9a4af9Sdrh ** If an error is found, the analysis is cut short. 28645d9a4af9Sdrh */ 28655d9a4af9Sdrh int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 28665d9a4af9Sdrh struct ExprList_item *pItem; 28675d9a4af9Sdrh int i; 28685d9a4af9Sdrh int nErr = 0; 28695d9a4af9Sdrh if( pList ){ 28705d9a4af9Sdrh for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){ 28715d9a4af9Sdrh nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 28725d9a4af9Sdrh } 28735d9a4af9Sdrh } 28745d9a4af9Sdrh return nErr; 28755d9a4af9Sdrh } 2876