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*eb55bd2fSdrh ** $Id: expr.c,v 1.209 2005/06/30 17:04:21 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_AS ){ 39bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pLeft); 40a37cdde0Sdanielk1977 } 41487e262fSdrh if( op==TK_SELECT ){ 42bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 43a37cdde0Sdanielk1977 } 44487e262fSdrh #ifndef SQLITE_OMIT_CAST 45487e262fSdrh if( op==TK_CAST ){ 46487e262fSdrh return sqlite3AffinityType(&pExpr->token); 47487e262fSdrh } 48487e262fSdrh #endif 49a37cdde0Sdanielk1977 return pExpr->affinity; 50a37cdde0Sdanielk1977 } 51a37cdde0Sdanielk1977 5253db1458Sdrh /* 530202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 540202b29eSdanielk1977 ** there is no default collation type, return 0. 550202b29eSdanielk1977 */ 567cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 577cedc8d4Sdanielk1977 CollSeq *pColl = 0; 580202b29eSdanielk1977 if( pExpr ){ 597cedc8d4Sdanielk1977 pColl = pExpr->pColl; 60487e262fSdrh if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){ 617cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 620202b29eSdanielk1977 } 630202b29eSdanielk1977 } 647cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 657cedc8d4Sdanielk1977 pColl = 0; 667cedc8d4Sdanielk1977 } 677cedc8d4Sdanielk1977 return pColl; 680202b29eSdanielk1977 } 690202b29eSdanielk1977 700202b29eSdanielk1977 /* 71626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 72626a879aSdrh ** type affinity of the other operand. This routine returns the 7353db1458Sdrh ** type affinity that should be used for the comparison operator. 7453db1458Sdrh */ 75e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 76bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 77e014a838Sdanielk1977 if( aff1 && aff2 ){ 78e014a838Sdanielk1977 /* Both sides of the comparison are columns. If one has numeric or 79e014a838Sdanielk1977 ** integer affinity, use that. Otherwise use no affinity. 80e014a838Sdanielk1977 */ 81e014a838Sdanielk1977 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ 82e014a838Sdanielk1977 return SQLITE_AFF_INTEGER; 83e014a838Sdanielk1977 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 84e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 85e014a838Sdanielk1977 }else{ 86e014a838Sdanielk1977 return SQLITE_AFF_NONE; 87e014a838Sdanielk1977 } 88e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 895f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 905f6a87b3Sdrh ** results directly. 91e014a838Sdanielk1977 */ 925f6a87b3Sdrh /* return SQLITE_AFF_NUMERIC; // Ticket #805 */ 935f6a87b3Sdrh return SQLITE_AFF_NONE; 94e014a838Sdanielk1977 }else{ 95e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 96e014a838Sdanielk1977 return (aff1 + aff2); 97e014a838Sdanielk1977 } 98e014a838Sdanielk1977 } 99e014a838Sdanielk1977 10053db1458Sdrh /* 10153db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 10253db1458Sdrh ** be applied to both operands prior to doing the comparison. 10353db1458Sdrh */ 104e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 105e014a838Sdanielk1977 char aff; 106e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 107e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 108e014a838Sdanielk1977 pExpr->op==TK_NE ); 109e014a838Sdanielk1977 assert( pExpr->pLeft ); 110bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 111e014a838Sdanielk1977 if( pExpr->pRight ){ 112e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 113e014a838Sdanielk1977 } 114e014a838Sdanielk1977 else if( pExpr->pSelect ){ 115e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 116e014a838Sdanielk1977 } 117e014a838Sdanielk1977 else if( !aff ){ 118e014a838Sdanielk1977 aff = SQLITE_AFF_NUMERIC; 119e014a838Sdanielk1977 } 120e014a838Sdanielk1977 return aff; 121e014a838Sdanielk1977 } 122e014a838Sdanielk1977 123e014a838Sdanielk1977 /* 124e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 125e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 126e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 127e014a838Sdanielk1977 ** the comparison in pExpr. 128e014a838Sdanielk1977 */ 129e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 130e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 131e014a838Sdanielk1977 return 132e014a838Sdanielk1977 (aff==SQLITE_AFF_NONE) || 133e014a838Sdanielk1977 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) || 134e014a838Sdanielk1977 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) || 135e014a838Sdanielk1977 (aff==idx_affinity); 136e014a838Sdanielk1977 } 137e014a838Sdanielk1977 138a37cdde0Sdanielk1977 /* 139a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 140a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 141a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 142a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 143a37cdde0Sdanielk1977 ** evaluates to NULL. 144a37cdde0Sdanielk1977 */ 145e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 146bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 147f0863fe5Sdrh return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0); 148a37cdde0Sdanielk1977 } 149a37cdde0Sdanielk1977 150a2e00042Sdrh /* 1510202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1520202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1530202b29eSdanielk1977 ** 1540202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1550202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1560202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1570202b29eSdanielk1977 ** type. 1580202b29eSdanielk1977 */ 1597cedc8d4Sdanielk1977 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ 1607cedc8d4Sdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft); 1610202b29eSdanielk1977 if( !pColl ){ 1627cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 1630202b29eSdanielk1977 } 1640202b29eSdanielk1977 return pColl; 1650202b29eSdanielk1977 } 1660202b29eSdanielk1977 1670202b29eSdanielk1977 /* 168be5c89acSdrh ** Generate code for a comparison operator. 169be5c89acSdrh */ 170be5c89acSdrh static int codeCompare( 171be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 172be5c89acSdrh Expr *pLeft, /* The left operand */ 173be5c89acSdrh Expr *pRight, /* The right operand */ 174be5c89acSdrh int opcode, /* The comparison opcode */ 175be5c89acSdrh int dest, /* Jump here if true. */ 176be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 177be5c89acSdrh ){ 178be5c89acSdrh int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); 179be5c89acSdrh CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); 180be5c89acSdrh return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); 181be5c89acSdrh } 182be5c89acSdrh 183be5c89acSdrh /* 184a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 185a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 186a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 187a76b5dfcSdrh */ 188e4e72072Sdrh Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ 189a76b5dfcSdrh Expr *pNew; 190a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 191a76b5dfcSdrh if( pNew==0 ){ 192d5d56523Sdanielk1977 /* When malloc fails, delete pLeft and pRight. Expressions passed to 193d5d56523Sdanielk1977 ** this function must always be allocated with sqlite3Expr() for this 194d5d56523Sdanielk1977 ** reason. 195d5d56523Sdanielk1977 */ 196d5d56523Sdanielk1977 sqlite3ExprDelete(pLeft); 197d5d56523Sdanielk1977 sqlite3ExprDelete(pRight); 198a76b5dfcSdrh return 0; 199a76b5dfcSdrh } 200a76b5dfcSdrh pNew->op = op; 201a76b5dfcSdrh pNew->pLeft = pLeft; 202a76b5dfcSdrh pNew->pRight = pRight; 203a58fdfb1Sdanielk1977 pNew->iAgg = -1; 204a76b5dfcSdrh if( pToken ){ 2054b59ab5eSdrh assert( pToken->dyn==0 ); 206145716b3Sdrh pNew->span = pNew->token = *pToken; 207145716b3Sdrh }else if( pLeft && pRight ){ 2084adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 209a76b5dfcSdrh } 210a76b5dfcSdrh return pNew; 211a76b5dfcSdrh } 212a76b5dfcSdrh 213a76b5dfcSdrh /* 2144e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 2154e0cff60Sdrh ** that look like this: #0 #1 #2 ... These terms refer to elements 216288d37f1Sdrh ** on the stack. "#0" means the top of the stack. 217288d37f1Sdrh ** "#1" means the next down on the stack. And so forth. 2184e0cff60Sdrh ** 2194e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 2204e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 2214e0cff60Sdrh ** The returns an expression that will code to extract the value from 2224e0cff60Sdrh ** that memory location as needed. 2234e0cff60Sdrh */ 2244e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 2254e0cff60Sdrh Vdbe *v = pParse->pVdbe; 2264e0cff60Sdrh Expr *p; 2274e0cff60Sdrh int depth; 2284e0cff60Sdrh if( v==0 ) return 0; 2294e0cff60Sdrh if( pParse->nested==0 ){ 2304e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 2314e0cff60Sdrh return 0; 2324e0cff60Sdrh } 2334e0cff60Sdrh p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); 23473c42a13Sdrh if( p==0 ){ 23573c42a13Sdrh return 0; /* Malloc failed */ 23673c42a13Sdrh } 2374e0cff60Sdrh depth = atoi(&pToken->z[1]); 2384e0cff60Sdrh p->iTable = pParse->nMem++; 2394e0cff60Sdrh sqlite3VdbeAddOp(v, OP_Dup, depth, 0); 2404e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1); 2414e0cff60Sdrh return p; 2424e0cff60Sdrh } 2434e0cff60Sdrh 2444e0cff60Sdrh /* 24591bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 24691bb0eedSdrh ** NULL, then just return the other expression. 24791bb0eedSdrh */ 24891bb0eedSdrh Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ 24991bb0eedSdrh if( pLeft==0 ){ 25091bb0eedSdrh return pRight; 25191bb0eedSdrh }else if( pRight==0 ){ 25291bb0eedSdrh return pLeft; 25391bb0eedSdrh }else{ 25491bb0eedSdrh return sqlite3Expr(TK_AND, pLeft, pRight, 0); 25591bb0eedSdrh } 25691bb0eedSdrh } 25791bb0eedSdrh 25891bb0eedSdrh /* 2596977fea8Sdrh ** Set the Expr.span field of the given expression to span all 260a76b5dfcSdrh ** text between the two given tokens. 261a76b5dfcSdrh */ 2624adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 2634efc4754Sdrh assert( pRight!=0 ); 2644efc4754Sdrh assert( pLeft!=0 ); 26571c697efSdrh if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){ 266ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 267145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 2686977fea8Sdrh pExpr->span.z = pLeft->z; 26997903fefSdrh pExpr->span.n = pRight->n + (pRight->z - pLeft->z); 2704b59ab5eSdrh }else{ 2716977fea8Sdrh pExpr->span.z = 0; 2724b59ab5eSdrh } 273a76b5dfcSdrh } 274a76b5dfcSdrh } 275a76b5dfcSdrh 276a76b5dfcSdrh /* 277a76b5dfcSdrh ** Construct a new expression node for a function with multiple 278a76b5dfcSdrh ** arguments. 279a76b5dfcSdrh */ 2804adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 281a76b5dfcSdrh Expr *pNew; 282a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 283a76b5dfcSdrh if( pNew==0 ){ 284d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ 285a76b5dfcSdrh return 0; 286a76b5dfcSdrh } 287a76b5dfcSdrh pNew->op = TK_FUNCTION; 288a76b5dfcSdrh pNew->pList = pList; 289a76b5dfcSdrh if( pToken ){ 2904b59ab5eSdrh assert( pToken->dyn==0 ); 291a76b5dfcSdrh pNew->token = *pToken; 292a76b5dfcSdrh }else{ 293a76b5dfcSdrh pNew->token.z = 0; 294a76b5dfcSdrh } 2956977fea8Sdrh pNew->span = pNew->token; 296a76b5dfcSdrh return pNew; 297a76b5dfcSdrh } 298a76b5dfcSdrh 299a76b5dfcSdrh /* 300fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 301fa6bc000Sdrh ** in the original SQL statement. 302fa6bc000Sdrh ** 303fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 304fa6bc000Sdrh ** variable number. 305fa6bc000Sdrh ** 306fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 307fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 308fa6bc000Sdrh ** the SQL statement comes from an external source. 309fa6bc000Sdrh ** 310fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 311fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 312fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 313fa6bc000Sdrh ** assigned. 314fa6bc000Sdrh */ 315fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 316fa6bc000Sdrh Token *pToken; 317fa6bc000Sdrh if( pExpr==0 ) return; 318fa6bc000Sdrh pToken = &pExpr->token; 319fa6bc000Sdrh assert( pToken->n>=1 ); 320fa6bc000Sdrh assert( pToken->z!=0 ); 321fa6bc000Sdrh assert( pToken->z[0]!=0 ); 322fa6bc000Sdrh if( pToken->n==1 ){ 323fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 324fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 325fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 326fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 327fa6bc000Sdrh ** use it as the variable number */ 328fa6bc000Sdrh int i; 329fa6bc000Sdrh pExpr->iTable = i = atoi(&pToken->z[1]); 330fa6bc000Sdrh if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ 331fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 332fa6bc000Sdrh SQLITE_MAX_VARIABLE_NUMBER); 333fa6bc000Sdrh } 334fa6bc000Sdrh if( i>pParse->nVar ){ 335fa6bc000Sdrh pParse->nVar = i; 336fa6bc000Sdrh } 337fa6bc000Sdrh }else{ 338fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 339fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 340fa6bc000Sdrh ** has never appeared before, reuse the same variable number 341fa6bc000Sdrh */ 342fa6bc000Sdrh int i, n; 343fa6bc000Sdrh n = pToken->n; 344fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 345fa6bc000Sdrh Expr *pE; 346fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 347fa6bc000Sdrh && pE->token.n==n 348fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 349fa6bc000Sdrh pExpr->iTable = pE->iTable; 350fa6bc000Sdrh break; 351fa6bc000Sdrh } 352fa6bc000Sdrh } 353fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 354fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 355fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 356fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 357fa6bc000Sdrh pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr, 358fa6bc000Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); 359fa6bc000Sdrh } 360fa6bc000Sdrh if( !sqlite3_malloc_failed ){ 361fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 362fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 363fa6bc000Sdrh } 364fa6bc000Sdrh } 365fa6bc000Sdrh } 366fa6bc000Sdrh } 367fa6bc000Sdrh 368fa6bc000Sdrh /* 369a2e00042Sdrh ** Recursively delete an expression tree. 370a2e00042Sdrh */ 3714adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 372a2e00042Sdrh if( p==0 ) return; 3734efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 3744efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 3754adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 3764adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 3774adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 3784adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 379a2e00042Sdrh sqliteFree(p); 380a2e00042Sdrh } 381a2e00042Sdrh 382a76b5dfcSdrh 383a76b5dfcSdrh /* 384ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 385ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 386ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 387ff78bd2fSdrh ** without effecting the originals. 388ff78bd2fSdrh ** 3894adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 3904adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 391ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 392ff78bd2fSdrh ** 393ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 394ff78bd2fSdrh */ 3954adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 396ff78bd2fSdrh Expr *pNew; 397ff78bd2fSdrh if( p==0 ) return 0; 398fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 399ff78bd2fSdrh if( pNew==0 ) return 0; 4003b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 4016977fea8Sdrh if( p->token.z!=0 ){ 402b9ecf6faSdrh pNew->token.z = sqliteStrNDup(p->token.z, p->token.n); 4034b59ab5eSdrh pNew->token.dyn = 1; 4044b59ab5eSdrh }else{ 4054efc4754Sdrh assert( pNew->token.z==0 ); 4064b59ab5eSdrh } 4076977fea8Sdrh pNew->span.z = 0; 4084adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 4094adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 4104adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 4114adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 412aee18ef8Sdanielk1977 pNew->pTab = p->pTab; 413ff78bd2fSdrh return pNew; 414ff78bd2fSdrh } 4154adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 4164b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 4174b59ab5eSdrh if( pFrom->z ){ 4184b59ab5eSdrh pTo->n = pFrom->n; 4194b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 4204b59ab5eSdrh pTo->dyn = 1; 4214b59ab5eSdrh }else{ 4224b59ab5eSdrh pTo->z = 0; 4234b59ab5eSdrh } 4244b59ab5eSdrh } 4254adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 426ff78bd2fSdrh ExprList *pNew; 427145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 428ff78bd2fSdrh int i; 429ff78bd2fSdrh if( p==0 ) return 0; 430ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 431ff78bd2fSdrh if( pNew==0 ) return 0; 4324305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 4333e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 434e0048400Sdanielk1977 if( pItem==0 ){ 435e0048400Sdanielk1977 sqliteFree(pNew); 436e0048400Sdanielk1977 return 0; 437e0048400Sdanielk1977 } 438145716b3Sdrh pOldItem = p->a; 439145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 4404b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 441145716b3Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); 4426977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 4436977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 4444b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 4454b59ab5eSdrh ** the names of columns in the result set needs this information */ 4464adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 4474b59ab5eSdrh } 4481f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 44924b03fd0Sdanielk1977 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 450145716b3Sdrh pItem->zName = sqliteStrDup(pOldItem->zName); 451145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 452145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 4533e7bc9caSdrh pItem->done = 0; 454ff78bd2fSdrh } 455ff78bd2fSdrh return pNew; 456ff78bd2fSdrh } 45793758c8dSdanielk1977 45893758c8dSdanielk1977 /* 45993758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 46093758c8dSdanielk1977 ** the build, then none of the following routines, except for 46193758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 46293758c8dSdanielk1977 ** called with a NULL argument. 46393758c8dSdanielk1977 */ 4646a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 4656a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 4664adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 467ad3cab52Sdrh SrcList *pNew; 468ad3cab52Sdrh int i; 469113088ecSdrh int nByte; 470ad3cab52Sdrh if( p==0 ) return 0; 471113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 4724efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 473ad3cab52Sdrh if( pNew==0 ) return 0; 4744305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 475ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 4764efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 4774efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 478ed8a3bb1Sdrh Table *pTab; 4794efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 4804efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 4814efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 4824efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 4834efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 484ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 485ed8a3bb1Sdrh if( pTab ){ 486ed8a3bb1Sdrh pTab->nRef++; 487a1cb183dSdanielk1977 } 4884adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 4894adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 4904adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 4916c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 492ad3cab52Sdrh } 493ad3cab52Sdrh return pNew; 494ad3cab52Sdrh } 4954adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 496ff78bd2fSdrh IdList *pNew; 497ff78bd2fSdrh int i; 498ff78bd2fSdrh if( p==0 ) return 0; 4994efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 500ff78bd2fSdrh if( pNew==0 ) return 0; 5014305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 5024efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 503d5d56523Sdanielk1977 if( pNew->a==0 ){ 504d5d56523Sdanielk1977 sqliteFree(pNew); 505d5d56523Sdanielk1977 return 0; 506d5d56523Sdanielk1977 } 507ff78bd2fSdrh for(i=0; i<p->nId; i++){ 5084efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 5094efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 5104efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 5114efc4754Sdrh pNewItem->idx = pOldItem->idx; 512ff78bd2fSdrh } 513ff78bd2fSdrh return pNew; 514ff78bd2fSdrh } 5154adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 516ff78bd2fSdrh Select *pNew; 517ff78bd2fSdrh if( p==0 ) return 0; 5184efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 519ff78bd2fSdrh if( pNew==0 ) return 0; 520ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 5214adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 5224adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 5234adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 5244adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 5254adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 5264adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 527ff78bd2fSdrh pNew->op = p->op; 5284adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 529a2dc3b1aSdanielk1977 pNew->pLimit = sqlite3ExprDup(p->pLimit); 530a2dc3b1aSdanielk1977 pNew->pOffset = sqlite3ExprDup(p->pOffset); 5317b58daeaSdrh pNew->iLimit = -1; 5327b58daeaSdrh pNew->iOffset = -1; 533dc1bdc4fSdanielk1977 pNew->ppOpenTemp = 0; 534a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 535a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 536ff78bd2fSdrh return pNew; 537ff78bd2fSdrh } 53893758c8dSdanielk1977 #else 53993758c8dSdanielk1977 Select *sqlite3SelectDup(Select *p){ 54093758c8dSdanielk1977 assert( p==0 ); 54193758c8dSdanielk1977 return 0; 54293758c8dSdanielk1977 } 54393758c8dSdanielk1977 #endif 544ff78bd2fSdrh 545ff78bd2fSdrh 546ff78bd2fSdrh /* 547a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 548a76b5dfcSdrh ** initially NULL, then create a new expression list. 549a76b5dfcSdrh */ 5504adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 551a76b5dfcSdrh if( pList==0 ){ 552a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 553a76b5dfcSdrh if( pList==0 ){ 554d5d56523Sdanielk1977 goto no_mem; 555a76b5dfcSdrh } 5564efc4754Sdrh assert( pList->nAlloc==0 ); 557a76b5dfcSdrh } 5584305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 559d5d56523Sdanielk1977 struct ExprList_item *a; 560d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 561d5d56523Sdanielk1977 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); 562d5d56523Sdanielk1977 if( a==0 ){ 563d5d56523Sdanielk1977 goto no_mem; 564a76b5dfcSdrh } 565d5d56523Sdanielk1977 pList->a = a; 566d5d56523Sdanielk1977 pList->nAlloc = n; 567a76b5dfcSdrh } 5684efc4754Sdrh assert( pList->a!=0 ); 5694efc4754Sdrh if( pExpr || pName ){ 5704efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 5714efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 572a99db3b6Sdrh pItem->zName = sqlite3NameFromToken(pName); 573e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 574a76b5dfcSdrh } 575a76b5dfcSdrh return pList; 576d5d56523Sdanielk1977 577d5d56523Sdanielk1977 no_mem: 578d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 579d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 580d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 581d5d56523Sdanielk1977 return 0; 582a76b5dfcSdrh } 583a76b5dfcSdrh 584a76b5dfcSdrh /* 585a76b5dfcSdrh ** Delete an entire expression list. 586a76b5dfcSdrh */ 5874adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 588a76b5dfcSdrh int i; 589be5c89acSdrh struct ExprList_item *pItem; 590a76b5dfcSdrh if( pList==0 ) return; 5911bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 5921bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 593be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 594be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 595be5c89acSdrh sqliteFree(pItem->zName); 596a76b5dfcSdrh } 597a76b5dfcSdrh sqliteFree(pList->a); 598a76b5dfcSdrh sqliteFree(pList); 599a76b5dfcSdrh } 600a76b5dfcSdrh 601a76b5dfcSdrh /* 602626a879aSdrh ** Walk an expression tree. Call xFunc for each node visited. 60373b211abSdrh ** 604626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 605626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 606626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 607626a879aSdrh ** the tree walk completely. 608626a879aSdrh ** 609626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 610626a879aSdrh ** and 0 to continue. 611626a879aSdrh */ 612a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 613626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 614626a879aSdrh int rc; 615626a879aSdrh if( pExpr==0 ) return 0; 616626a879aSdrh rc = (*xFunc)(pArg, pExpr); 617626a879aSdrh if( rc==0 ){ 618626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 619626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 620a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 621626a879aSdrh } 622626a879aSdrh return rc>1; 623626a879aSdrh } 624626a879aSdrh 625626a879aSdrh /* 626a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 627a58fdfb1Sdanielk1977 */ 628a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 629a58fdfb1Sdanielk1977 int i; 630a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 631a58fdfb1Sdanielk1977 if( !p ) return 0; 632a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 633a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 634a58fdfb1Sdanielk1977 } 635a58fdfb1Sdanielk1977 return 0; 636a58fdfb1Sdanielk1977 } 637a58fdfb1Sdanielk1977 638a58fdfb1Sdanielk1977 /* 639a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 640a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 641a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 642a58fdfb1Sdanielk1977 */ 643a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 644a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 645a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 646a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 647a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 648a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 649a58fdfb1Sdanielk1977 return 0; 650a58fdfb1Sdanielk1977 } 651a58fdfb1Sdanielk1977 652a58fdfb1Sdanielk1977 653a58fdfb1Sdanielk1977 /* 654626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 655626a879aSdrh ** 656626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 65773b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 65873b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 65973b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 66073b211abSdrh ** then do nothing. 66173b211abSdrh ** 66273b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 66373b211abSdrh ** the expression as constant, then we assume the whole expression 66473b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 665626a879aSdrh */ 666626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 667626a879aSdrh switch( pExpr->op ){ 668*eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 669*eb55bd2fSdrh ** and *pArg==2 */ 670*eb55bd2fSdrh case TK_FUNCTION: 671*eb55bd2fSdrh if( *((int*)pArg)==2 ) return 0; 672*eb55bd2fSdrh /* Fall through */ 673626a879aSdrh case TK_ID: 674626a879aSdrh case TK_COLUMN: 675626a879aSdrh case TK_DOT: 676626a879aSdrh case TK_AGG_FUNCTION: 677fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 678fe2093d7Sdrh case TK_SELECT: 679fe2093d7Sdrh case TK_EXISTS: 680fe2093d7Sdrh #endif 681626a879aSdrh *((int*)pArg) = 0; 682626a879aSdrh return 2; 683626a879aSdrh default: 684626a879aSdrh return 0; 685626a879aSdrh } 686626a879aSdrh } 687626a879aSdrh 688626a879aSdrh /* 689fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 690*eb55bd2fSdrh ** and 0 if it involves variables or function calls. 6912398937bSdrh ** 6922398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 6932398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 6942398937bSdrh ** a constant. 695fef5208cSdrh */ 6964adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 697626a879aSdrh int isConst = 1; 698626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 699626a879aSdrh return isConst; 700fef5208cSdrh } 701fef5208cSdrh 702fef5208cSdrh /* 703*eb55bd2fSdrh ** Walk an expression tree. Return 1 if the expression is constant 704*eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 705*eb55bd2fSdrh ** are any variables. 706*eb55bd2fSdrh ** 707*eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 708*eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 709*eb55bd2fSdrh ** a constant. 710*eb55bd2fSdrh */ 711*eb55bd2fSdrh int sqlite3ExprIsConstantOrFunction(Expr *p){ 712*eb55bd2fSdrh int isConst = 2; 713*eb55bd2fSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 714*eb55bd2fSdrh return isConst!=0; 715*eb55bd2fSdrh } 716*eb55bd2fSdrh 717*eb55bd2fSdrh /* 71873b211abSdrh ** If the expression p codes a constant integer that is small enough 719202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 720202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 721202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 722e4de1febSdrh */ 7234adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 724e4de1febSdrh switch( p->op ){ 725e4de1febSdrh case TK_INTEGER: { 726fec19aadSdrh if( sqlite3GetInt32(p->token.z, pValue) ){ 727e4de1febSdrh return 1; 728e4de1febSdrh } 729202b2df7Sdrh break; 730202b2df7Sdrh } 7314b59ab5eSdrh case TK_UPLUS: { 7324adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 7334b59ab5eSdrh } 734e4de1febSdrh case TK_UMINUS: { 735e4de1febSdrh int v; 7364adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 737e4de1febSdrh *pValue = -v; 738e4de1febSdrh return 1; 739e4de1febSdrh } 740e4de1febSdrh break; 741e4de1febSdrh } 742e4de1febSdrh default: break; 743e4de1febSdrh } 744e4de1febSdrh return 0; 745e4de1febSdrh } 746e4de1febSdrh 747e4de1febSdrh /* 748c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 749c4a3c779Sdrh */ 7504adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 7514adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 7524adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 7534adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 754c4a3c779Sdrh return 0; 755c4a3c779Sdrh } 756c4a3c779Sdrh 757c4a3c779Sdrh /* 7588141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 7598141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 7608141f61eSdrh ** expression node refer back to that source column. The following changes 7618141f61eSdrh ** are made to pExpr: 7628141f61eSdrh ** 7638141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 7648141f61eSdrh ** the table. 7658141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 7668141f61eSdrh ** from pSrcList. 7678141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 7688141f61eSdrh ** pExpr->op Set to TK_COLUMN. 7698141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 7708141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 7718141f61eSdrh ** 7728141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 7738141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 7748141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 7758141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 7768141f61eSdrh ** means that the form of the name is Z and that columns from any table 7778141f61eSdrh ** can be used. 7788141f61eSdrh ** 7798141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 7808141f61eSdrh ** in pParse and return non-zero. Return zero on success. 7818141f61eSdrh */ 7828141f61eSdrh static int lookupName( 7838141f61eSdrh Parse *pParse, /* The parsing context */ 7848141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 7858141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 7868141f61eSdrh Token *pColumnToken, /* Name of the column. */ 787626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 7888141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 7898141f61eSdrh ){ 7908141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 7918141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 7928141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 7938141f61eSdrh int i, j; /* Loop counters */ 7948141f61eSdrh int cnt = 0; /* Number of matching column names */ 7958141f61eSdrh int cntTab = 0; /* Number of matching table names */ 7969bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 79751669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 79851669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 79973b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 8008141f61eSdrh 8018141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 802a99db3b6Sdrh zDb = sqlite3NameFromToken(pDbToken); 803a99db3b6Sdrh zTab = sqlite3NameFromToken(pTableToken); 804a99db3b6Sdrh zCol = sqlite3NameFromToken(pColumnToken); 80524b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 806d5d56523Sdanielk1977 goto lookupname_end; 8078141f61eSdrh } 8088141f61eSdrh 8098141f61eSdrh pExpr->iTable = -1; 810626a879aSdrh while( pNC && cnt==0 ){ 811626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 812626a879aSdrh ExprList *pEList = pNC->pEList; 813626a879aSdrh 814626a879aSdrh /* assert( zTab==0 || pEList==0 ); */ 815b3bce662Sdanielk1977 if( pSrcList ){ 81651669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 8178141f61eSdrh Table *pTab = pItem->pTab; 8188141f61eSdrh Column *pCol; 8198141f61eSdrh 8208141f61eSdrh if( pTab==0 ) continue; 8218141f61eSdrh assert( pTab->nCol>0 ); 8228141f61eSdrh if( zTab ){ 8238141f61eSdrh if( pItem->zAlias ){ 8248141f61eSdrh char *zTabName = pItem->zAlias; 8254adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 8268141f61eSdrh }else{ 8278141f61eSdrh char *zTabName = pTab->zName; 8284adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 8294adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 8308141f61eSdrh continue; 8318141f61eSdrh } 8328141f61eSdrh } 8338141f61eSdrh } 8348141f61eSdrh if( 0==(cntTab++) ){ 8358141f61eSdrh pExpr->iTable = pItem->iCursor; 8368141f61eSdrh pExpr->iDb = pTab->iDb; 83751669863Sdrh pMatch = pItem; 8388141f61eSdrh } 8398141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 8404adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 841873fac0cSdrh IdList *pUsing; 8428141f61eSdrh cnt++; 8438141f61eSdrh pExpr->iTable = pItem->iCursor; 84451669863Sdrh pMatch = pItem; 8458141f61eSdrh pExpr->iDb = pTab->iDb; 8468141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 8478141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 848a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 8490202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 850355ef361Sdrh if( pItem->jointype & JT_NATURAL ){ 851355ef361Sdrh /* If this match occurred in the left table of a natural join, 852355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 853355ef361Sdrh pItem++; 854355ef361Sdrh i++; 855355ef361Sdrh } 856873fac0cSdrh if( (pUsing = pItem->pUsing)!=0 ){ 857873fac0cSdrh /* If this match occurs on a column that is in the USING clause 858873fac0cSdrh ** of a join, skip the search of the right table of the join 859873fac0cSdrh ** to avoid a duplicate match there. */ 860873fac0cSdrh int k; 861873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 862873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 863873fac0cSdrh pItem++; 864873fac0cSdrh i++; 865873fac0cSdrh break; 866873fac0cSdrh } 867873fac0cSdrh } 868873fac0cSdrh } 8698141f61eSdrh break; 8708141f61eSdrh } 8718141f61eSdrh } 8728141f61eSdrh } 873b3bce662Sdanielk1977 } 8748141f61eSdrh 875b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 8768141f61eSdrh /* If we have not already resolved the name, then maybe 8778141f61eSdrh ** it is a new.* or old.* trigger argument reference 8788141f61eSdrh */ 8798141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 8808141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 8818141f61eSdrh Table *pTab = 0; 8824adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 8838141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 8848141f61eSdrh assert( pTriggerStack->pTab ); 8858141f61eSdrh pTab = pTriggerStack->pTab; 8864adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 8878141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 8888141f61eSdrh assert( pTriggerStack->pTab ); 8898141f61eSdrh pTab = pTriggerStack->pTab; 8908141f61eSdrh } 8918141f61eSdrh 8928141f61eSdrh if( pTab ){ 8938141f61eSdrh int j; 8948141f61eSdrh Column *pCol = pTab->aCol; 8958141f61eSdrh 8968141f61eSdrh pExpr->iDb = pTab->iDb; 8978141f61eSdrh cntTab++; 8988141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 8994adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 9008141f61eSdrh cnt++; 9018141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 902a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 9030202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 904aee18ef8Sdanielk1977 pExpr->pTab = pTab; 9058141f61eSdrh break; 9068141f61eSdrh } 9078141f61eSdrh } 9088141f61eSdrh } 9098141f61eSdrh } 910b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 9118141f61eSdrh 9128141f61eSdrh /* 9138141f61eSdrh ** Perhaps the name is a reference to the ROWID 9148141f61eSdrh */ 9154adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 9168141f61eSdrh cnt = 1; 9178141f61eSdrh pExpr->iColumn = -1; 918a37cdde0Sdanielk1977 pExpr->affinity = SQLITE_AFF_INTEGER; 9198141f61eSdrh } 9208141f61eSdrh 9218141f61eSdrh /* 9228141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 9238141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 9248141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 9258141f61eSdrh ** 9268141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 9278141f61eSdrh ** 9288141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 9298141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 9308141f61eSdrh ** Note that the expression in the result set should have already been 9318141f61eSdrh ** resolved by the time the WHERE clause is resolved. 9328141f61eSdrh */ 93379d5f63fSdrh if( cnt==0 && pEList!=0 && zTab==0 ){ 9348141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 9358141f61eSdrh char *zAs = pEList->a[j].zName; 9364adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 9378141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 9388141f61eSdrh pExpr->op = TK_AS; 9398141f61eSdrh pExpr->iColumn = j; 9404adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 94115ccce1cSdrh cnt = 1; 9428141f61eSdrh assert( zTab==0 && zDb==0 ); 94315ccce1cSdrh goto lookupname_end_2; 9448141f61eSdrh } 9458141f61eSdrh } 9468141f61eSdrh } 9478141f61eSdrh 948626a879aSdrh /* Advance to the next name context. The loop will exit when either 949626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 950626a879aSdrh */ 951626a879aSdrh if( cnt==0 ){ 952626a879aSdrh pNC = pNC->pNext; 953626a879aSdrh } 954626a879aSdrh } 955626a879aSdrh 9568141f61eSdrh /* 9578141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 9588141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 9598141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 9608141f61eSdrh ** case, we need to return right away and not make any changes to 9618141f61eSdrh ** pExpr. 96215ccce1cSdrh ** 96315ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 96415ccce1cSdrh ** fields are not changed in any context. 9658141f61eSdrh */ 9668141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 9678141f61eSdrh sqliteFree(zCol); 9688141f61eSdrh return 0; 9698141f61eSdrh } 9708141f61eSdrh 9718141f61eSdrh /* 9728141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 9738141f61eSdrh ** more matches. Either way, we have an error. 9748141f61eSdrh */ 9758141f61eSdrh if( cnt!=1 ){ 9768141f61eSdrh char *z = 0; 9778141f61eSdrh char *zErr; 9788141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 9798141f61eSdrh if( zDb ){ 9804adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 9818141f61eSdrh }else if( zTab ){ 9824adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 9838141f61eSdrh }else{ 9848141f61eSdrh z = sqliteStrDup(zCol); 9858141f61eSdrh } 9864adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 9878141f61eSdrh sqliteFree(z); 98873b211abSdrh pTopNC->nErr++; 9898141f61eSdrh } 9908141f61eSdrh 99151669863Sdrh /* If a column from a table in pSrcList is referenced, then record 99251669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 99351669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 99451669863Sdrh ** column number is greater than the number of bits in the bitmask 99551669863Sdrh ** then set the high-order bit of the bitmask. 99651669863Sdrh */ 99751669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 99851669863Sdrh int n = pExpr->iColumn; 99951669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 100051669863Sdrh n = sizeof(Bitmask)*8-1; 100151669863Sdrh } 100251669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 100351669863Sdrh pMatch->colUsed |= 1<<n; 100451669863Sdrh } 100551669863Sdrh 1006d5d56523Sdanielk1977 lookupname_end: 10078141f61eSdrh /* Clean up and return 10088141f61eSdrh */ 10098141f61eSdrh sqliteFree(zDb); 10108141f61eSdrh sqliteFree(zTab); 10114adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 10128141f61eSdrh pExpr->pLeft = 0; 10134adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 10148141f61eSdrh pExpr->pRight = 0; 10158141f61eSdrh pExpr->op = TK_COLUMN; 101615ccce1cSdrh lookupname_end_2: 101715ccce1cSdrh sqliteFree(zCol); 1018626a879aSdrh if( cnt==1 ){ 1019b3bce662Sdanielk1977 assert( pNC!=0 ); 1020626a879aSdrh sqlite3AuthRead(pParse, pExpr, pNC->pSrcList); 1021aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 1022aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 1023aee18ef8Sdanielk1977 } 102415ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 102515ccce1cSdrh ** the point where the name matched. */ 102615ccce1cSdrh for(;;){ 102715ccce1cSdrh assert( pTopNC!=0 ); 102815ccce1cSdrh pTopNC->nRef++; 102915ccce1cSdrh if( pTopNC==pNC ) break; 103015ccce1cSdrh pTopNC = pTopNC->pNext; 1031626a879aSdrh } 103215ccce1cSdrh return 0; 103315ccce1cSdrh } else { 103415ccce1cSdrh return 1; 103515ccce1cSdrh } 10368141f61eSdrh } 10378141f61eSdrh 10388141f61eSdrh /* 1039626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1040626a879aSdrh ** 104173b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1042626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 104373b211abSdrh ** the tree or 2 to abort the tree walk. 104473b211abSdrh ** 104573b211abSdrh ** This routine also does error checking and name resolution for 104673b211abSdrh ** function names. The operator for aggregate functions is changed 104773b211abSdrh ** to TK_AGG_FUNCTION. 1048626a879aSdrh */ 1049626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1050626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1051626a879aSdrh SrcList *pSrcList; 1052626a879aSdrh Parse *pParse; 1053626a879aSdrh 1054b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1055626a879aSdrh assert( pNC!=0 ); 1056626a879aSdrh pSrcList = pNC->pSrcList; 1057626a879aSdrh pParse = pNC->pParse; 1058b3bce662Sdanielk1977 1059626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1060626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1061626a879aSdrh #ifndef NDEBUG 1062626a879aSdrh if( pSrcList ){ 1063940fac9dSdanielk1977 int i; 1064626a879aSdrh for(i=0; i<pSrcList->nSrc; i++){ 1065626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1066626a879aSdrh } 1067626a879aSdrh } 1068626a879aSdrh #endif 1069626a879aSdrh switch( pExpr->op ){ 1070626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1071626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1072626a879aSdrh ** strings (ex: 'abc') are always string literals. 1073626a879aSdrh */ 1074626a879aSdrh case TK_STRING: { 1075626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1076626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1077626a879aSdrh } 1078626a879aSdrh /* A lone identifier is the name of a column. 1079626a879aSdrh */ 1080626a879aSdrh case TK_ID: { 1081626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1082626a879aSdrh return 1; 1083626a879aSdrh } 1084626a879aSdrh 1085626a879aSdrh /* A table name and column name: ID.ID 1086626a879aSdrh ** Or a database, table and column: ID.ID.ID 1087626a879aSdrh */ 1088626a879aSdrh case TK_DOT: { 1089626a879aSdrh Token *pColumn; 1090626a879aSdrh Token *pTable; 1091626a879aSdrh Token *pDb; 1092626a879aSdrh Expr *pRight; 1093626a879aSdrh 1094b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1095626a879aSdrh pRight = pExpr->pRight; 1096626a879aSdrh if( pRight->op==TK_ID ){ 1097626a879aSdrh pDb = 0; 1098626a879aSdrh pTable = &pExpr->pLeft->token; 1099626a879aSdrh pColumn = &pRight->token; 1100626a879aSdrh }else{ 1101626a879aSdrh assert( pRight->op==TK_DOT ); 1102626a879aSdrh pDb = &pExpr->pLeft->token; 1103626a879aSdrh pTable = &pRight->pLeft->token; 1104626a879aSdrh pColumn = &pRight->pRight->token; 1105626a879aSdrh } 1106626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1107626a879aSdrh return 1; 1108626a879aSdrh } 1109626a879aSdrh 1110626a879aSdrh /* Resolve function names 1111626a879aSdrh */ 1112b71090fdSdrh case TK_CONST_FUNC: 1113626a879aSdrh case TK_FUNCTION: { 1114626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1115626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1116626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1117626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1118626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1119626a879aSdrh int i; 1120626a879aSdrh int nId; /* Number of characters in function name */ 1121626a879aSdrh const char *zId; /* The function name. */ 112273b211abSdrh FuncDef *pDef; /* Information about the function */ 112373b211abSdrh int enc = pParse->db->enc; /* The database encoding */ 1124626a879aSdrh 1125b71090fdSdrh zId = pExpr->token.z; 1126b71090fdSdrh nId = pExpr->token.n; 1127626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1128626a879aSdrh if( pDef==0 ){ 1129626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1130626a879aSdrh if( pDef==0 ){ 1131626a879aSdrh no_such_func = 1; 1132626a879aSdrh }else{ 1133626a879aSdrh wrong_num_args = 1; 1134626a879aSdrh } 1135626a879aSdrh }else{ 1136626a879aSdrh is_agg = pDef->xFunc==0; 1137626a879aSdrh } 1138626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1139626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1140626a879aSdrh pNC->nErr++; 1141626a879aSdrh is_agg = 0; 1142626a879aSdrh }else if( no_such_func ){ 1143626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1144626a879aSdrh pNC->nErr++; 1145626a879aSdrh }else if( wrong_num_args ){ 1146626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1147626a879aSdrh nId, zId); 1148626a879aSdrh pNC->nErr++; 1149626a879aSdrh } 1150626a879aSdrh if( is_agg ){ 1151626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1152626a879aSdrh pNC->hasAgg = 1; 1153626a879aSdrh } 115473b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1155626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 115673b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1157626a879aSdrh } 115873b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1159626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1160626a879aSdrh ** type of the function 1161626a879aSdrh */ 1162626a879aSdrh return is_agg; 1163626a879aSdrh } 1164b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1165b3bce662Sdanielk1977 case TK_SELECT: 1166b3bce662Sdanielk1977 case TK_EXISTS: 1167b3bce662Sdanielk1977 #endif 1168b3bce662Sdanielk1977 case TK_IN: { 1169b3bce662Sdanielk1977 if( pExpr->pSelect ){ 1170b3bce662Sdanielk1977 int nRef = pNC->nRef; 1171b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1172b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1173b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1174b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1175b3bce662Sdanielk1977 } 1176b3bce662Sdanielk1977 } 1177b3bce662Sdanielk1977 } 1178626a879aSdrh } 1179626a879aSdrh return 0; 1180626a879aSdrh } 1181626a879aSdrh 1182626a879aSdrh /* 1183cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1184967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1185aacc543eSdrh ** index to the table in the table list and a column offset. The 1186aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1187aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1188832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1189aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1190aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1191aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1192aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1193aacc543eSdrh ** alias for ROWID. 119419a775c2Sdrh ** 1195626a879aSdrh ** Also resolve function names and check the functions for proper 1196626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1197626a879aSdrh ** have the correct number of arguments. Leave an error message 1198626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1199626a879aSdrh ** 120073b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 120173b211abSdrh ** property on the expression. 1202626a879aSdrh */ 1203626a879aSdrh int sqlite3ExprResolveNames( 1204b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1205b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1206626a879aSdrh ){ 120773b211abSdrh if( pExpr==0 ) return 0; 1208b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1209b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 121073b211abSdrh ExprSetProperty(pExpr, EP_Error); 121173b211abSdrh } 121273b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1213626a879aSdrh } 1214626a879aSdrh 12151398ad36Sdrh /* 12161398ad36Sdrh ** A pointer instance of this structure is used to pass information 12171398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 12181398ad36Sdrh */ 12191398ad36Sdrh typedef struct QueryCoder QueryCoder; 12201398ad36Sdrh struct QueryCoder { 12211398ad36Sdrh Parse *pParse; /* The parsing context */ 12221398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 12231398ad36Sdrh }; 12241398ad36Sdrh 1225626a879aSdrh 1226626a879aSdrh /* 1227626a879aSdrh ** Generate code for subqueries and IN operators. 1228626a879aSdrh ** 122973b211abSdrh ** IN operators comes in two forms: 1230fef5208cSdrh ** 1231fef5208cSdrh ** expr IN (exprlist) 1232fef5208cSdrh ** and 1233fef5208cSdrh ** expr IN (SELECT ...) 1234fef5208cSdrh ** 1235fef5208cSdrh ** The first form is handled by creating a set holding the list 1236fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 1237fef5208cSdrh ** a temporary table. 1238cce7d176Sdrh */ 123951522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1240b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 1241b3bce662Sdanielk1977 int label = 0; /* Address after sub-select code */ 1242b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1243b3bce662Sdanielk1977 if( v==0 ) return; 1244b3bce662Sdanielk1977 1245b3bce662Sdanielk1977 /* If this is not a variable (correlated) select, then execute 1246b3bce662Sdanielk1977 ** it only once. Unless this is part of a trigger program. In 1247b3bce662Sdanielk1977 ** that case re-execute every time (this could be optimized). 1248b3bce662Sdanielk1977 */ 1249b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 1250b3bce662Sdanielk1977 int mem = pParse->nMem++; 1251b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); 1252b3bce662Sdanielk1977 label = sqlite3VdbeMakeLabel(v); 1253b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_If, 0, label); 1254b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1255b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1); 1256b3bce662Sdanielk1977 } 1257b3bce662Sdanielk1977 1258b3bce662Sdanielk1977 if( pExpr->pSelect ){ 1259b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0); 1260b3bce662Sdanielk1977 } 12616a3ea0e6Sdrh 1262cce7d176Sdrh switch( pExpr->op ){ 1263fef5208cSdrh case TK_IN: { 1264e014a838Sdanielk1977 char affinity; 1265d3d39e93Sdrh KeyInfo keyInfo; 12660202b29eSdanielk1977 int addr; /* Address of OP_OpenTemp instruction */ 1267d3d39e93Sdrh 1268bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1269e014a838Sdanielk1977 1270e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 1271e014a838Sdanielk1977 ** expression it is handled the same way. A temporary table is 1272e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1273e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1274fef5208cSdrh ** 1275e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1276e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1277e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1278e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1279e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1280e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1281e014a838Sdanielk1977 ** is used. 1282fef5208cSdrh */ 1283832508b7Sdrh pExpr->iTable = pParse->nTab++; 12840202b29eSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0); 1285d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1286d3d39e93Sdrh keyInfo.nField = 1; 1287f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 1288e014a838Sdanielk1977 1289e014a838Sdanielk1977 if( pExpr->pSelect ){ 1290e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1291e014a838Sdanielk1977 ** 1292e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1293e014a838Sdanielk1977 ** table allocated and opened above. 1294e014a838Sdanielk1977 */ 1295e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 1296be5c89acSdrh ExprList *pEList; 1297e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 1298b3bce662Sdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 1299be5c89acSdrh pEList = pExpr->pSelect->pEList; 1300be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 13017cedc8d4Sdanielk1977 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 1302be5c89acSdrh pEList->a[0].pExpr); 13030202b29eSdanielk1977 } 1304fef5208cSdrh }else if( pExpr->pList ){ 1305fef5208cSdrh /* Case 2: expr IN (exprlist) 1306fef5208cSdrh ** 1307e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1308e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1309e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1310e014a838Sdanielk1977 ** a column, use numeric affinity. 1311fef5208cSdrh */ 1312e014a838Sdanielk1977 int i; 1313e014a838Sdanielk1977 if( !affinity ){ 1314e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 1315e014a838Sdanielk1977 } 13160202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1317e014a838Sdanielk1977 1318e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 1319fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 1320fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 1321e014a838Sdanielk1977 1322e014a838Sdanielk1977 /* Check that the expression is constant and valid. */ 13234adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 13244adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1325da93d238Sdrh "right-hand side of IN operator must be constant"); 1326b3bce662Sdanielk1977 return; 13274794b980Sdrh } 1328e014a838Sdanielk1977 1329e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 13304adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 133194a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 1332f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0); 1333fef5208cSdrh } 1334fef5208cSdrh } 13350202b29eSdanielk1977 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 1336b3bce662Sdanielk1977 break; 1337fef5208cSdrh } 1338fef5208cSdrh 133951522cd3Sdrh case TK_EXISTS: 134019a775c2Sdrh case TK_SELECT: { 1341fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1342fef5208cSdrh ** value of this select in a memory cell and record the number 1343967e8b73Sdrh ** of the memory cell in iColumn. 1344fef5208cSdrh */ 134551522cd3Sdrh int sop; 134651522cd3Sdrh Select *pSel; 13471398ad36Sdrh 1348967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 134951522cd3Sdrh pSel = pExpr->pSelect; 135051522cd3Sdrh if( pExpr->op==TK_SELECT ){ 135151522cd3Sdrh sop = SRT_Mem; 135251522cd3Sdrh }else{ 135351522cd3Sdrh static const Token one = { "1", 0, 1 }; 135451522cd3Sdrh sop = SRT_Exists; 135551522cd3Sdrh sqlite3ExprListDelete(pSel->pEList); 135651522cd3Sdrh pSel->pEList = sqlite3ExprListAppend(0, 135751522cd3Sdrh sqlite3Expr(TK_INTEGER, 0, 0, &one), 0); 135851522cd3Sdrh } 1359b3bce662Sdanielk1977 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0); 1360b3bce662Sdanielk1977 break; 136119a775c2Sdrh } 1362cce7d176Sdrh } 1363b3bce662Sdanielk1977 1364b3bce662Sdanielk1977 if( pExpr->pSelect ){ 1365b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0); 1366b3bce662Sdanielk1977 } 1367b3bce662Sdanielk1977 if( label<0 ){ 1368b3bce662Sdanielk1977 sqlite3VdbeResolveLabel(v, label); 1369b3bce662Sdanielk1977 } 1370b3bce662Sdanielk1977 return; 1371cce7d176Sdrh } 137251522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1373cce7d176Sdrh 1374cce7d176Sdrh /* 1375fec19aadSdrh ** Generate an instruction that will put the integer describe by 1376fec19aadSdrh ** text z[0..n-1] on the stack. 1377fec19aadSdrh */ 1378fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1379fec19aadSdrh int i; 13806fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 13816fec0762Sdrh sqlite3VdbeAddOp(v, OP_Integer, i, 0); 13826fec0762Sdrh }else if( sqlite3FitsIn64Bits(z) ){ 13836fec0762Sdrh sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n); 1384fec19aadSdrh }else{ 1385fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1386fec19aadSdrh } 1387fec19aadSdrh } 1388fec19aadSdrh 1389fec19aadSdrh /* 1390cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 13911ccde15dSdrh ** expression and leave the result on the top of stack. 1392f2bc013cSdrh ** 1393f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1394f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1395f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1396f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1397f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1398cce7d176Sdrh */ 13994adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1400cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1401cce7d176Sdrh int op; 14027977a17fSdanielk1977 if( v==0 ) return; 14037977a17fSdanielk1977 if( pExpr==0 ){ 1404f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 14057977a17fSdanielk1977 return; 14067977a17fSdanielk1977 } 1407f2bc013cSdrh op = pExpr->op; 1408f2bc013cSdrh switch( op ){ 1409967e8b73Sdrh case TK_COLUMN: { 1410a58fdfb1Sdanielk1977 if( !pParse->fillAgg && pExpr->iAgg>=0 ){ 1411a58fdfb1Sdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg); 1412c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 14134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1414aee18ef8Sdanielk1977 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn); 1415c4a3c779Sdrh }else{ 1416f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0); 14172282792aSdrh } 1418cce7d176Sdrh break; 1419cce7d176Sdrh } 1420cce7d176Sdrh case TK_INTEGER: { 1421fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1422fec19aadSdrh break; 142351e9a445Sdrh } 1424fec19aadSdrh case TK_FLOAT: 1425fec19aadSdrh case TK_STRING: { 1426f2bc013cSdrh assert( TK_FLOAT==OP_Real ); 1427f2bc013cSdrh assert( TK_STRING==OP_String8 ); 1428fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 14294adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1430cce7d176Sdrh break; 1431cce7d176Sdrh } 1432f0863fe5Sdrh case TK_NULL: { 1433f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1434f0863fe5Sdrh break; 1435f0863fe5Sdrh } 14365338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 1437c572ef7fSdanielk1977 case TK_BLOB: { 1438f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 1439c572ef7fSdanielk1977 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1); 1440c572ef7fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1441c572ef7fSdanielk1977 break; 1442c572ef7fSdanielk1977 } 14435338a5f7Sdanielk1977 #endif 144450457896Sdrh case TK_VARIABLE: { 14454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1446895d7472Sdrh if( pExpr->token.n>1 ){ 1447895d7472Sdrh sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 1448895d7472Sdrh } 144950457896Sdrh break; 145050457896Sdrh } 14514e0cff60Sdrh case TK_REGISTER: { 14524e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); 14534e0cff60Sdrh break; 14544e0cff60Sdrh } 1455487e262fSdrh #ifndef SQLITE_OMIT_CAST 1456487e262fSdrh case TK_CAST: { 1457487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 1458487e262fSdrh int aff, op; 1459487e262fSdrh sqlite3ExprCode(pParse, pExpr->pLeft); 1460487e262fSdrh aff = sqlite3AffinityType(&pExpr->token); 1461487e262fSdrh switch( aff ){ 1462487e262fSdrh case SQLITE_AFF_INTEGER: op = OP_ToInt; break; 1463487e262fSdrh case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break; 1464487e262fSdrh case SQLITE_AFF_TEXT: op = OP_ToText; break; 1465487e262fSdrh case SQLITE_AFF_NONE: op = OP_ToBlob; break; 1466487e262fSdrh } 1467487e262fSdrh sqlite3VdbeAddOp(v, op, 0, 0); 1468487e262fSdrh break; 1469487e262fSdrh } 1470487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 1471c9b84a1fSdrh case TK_LT: 1472c9b84a1fSdrh case TK_LE: 1473c9b84a1fSdrh case TK_GT: 1474c9b84a1fSdrh case TK_GE: 1475c9b84a1fSdrh case TK_NE: 1476c9b84a1fSdrh case TK_EQ: { 1477f2bc013cSdrh assert( TK_LT==OP_Lt ); 1478f2bc013cSdrh assert( TK_LE==OP_Le ); 1479f2bc013cSdrh assert( TK_GT==OP_Gt ); 1480f2bc013cSdrh assert( TK_GE==OP_Ge ); 1481f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1482f2bc013cSdrh assert( TK_NE==OP_Ne ); 1483a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1484a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1485be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 1486a37cdde0Sdanielk1977 break; 1487c9b84a1fSdrh } 1488cce7d176Sdrh case TK_AND: 1489cce7d176Sdrh case TK_OR: 1490cce7d176Sdrh case TK_PLUS: 1491cce7d176Sdrh case TK_STAR: 1492cce7d176Sdrh case TK_MINUS: 1493bf4133cbSdrh case TK_REM: 1494bf4133cbSdrh case TK_BITAND: 1495bf4133cbSdrh case TK_BITOR: 149617c40294Sdrh case TK_SLASH: 1497bf4133cbSdrh case TK_LSHIFT: 1498855eb1cfSdrh case TK_RSHIFT: 14990040077dSdrh case TK_CONCAT: { 1500f2bc013cSdrh assert( TK_AND==OP_And ); 1501f2bc013cSdrh assert( TK_OR==OP_Or ); 1502f2bc013cSdrh assert( TK_PLUS==OP_Add ); 1503f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 1504f2bc013cSdrh assert( TK_REM==OP_Remainder ); 1505f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 1506f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 1507f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 1508f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 1509f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 1510f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 15114adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15124adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1513855eb1cfSdrh sqlite3VdbeAddOp(v, op, 0, 0); 15140040077dSdrh break; 15150040077dSdrh } 1516cce7d176Sdrh case TK_UMINUS: { 1517fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1518fec19aadSdrh assert( pLeft ); 1519fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1520fec19aadSdrh Token *p = &pLeft->token; 15216e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 15226e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1523fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1524fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1525e6840900Sdrh }else{ 1526fec19aadSdrh codeInteger(v, z, p->n+1); 1527e6840900Sdrh } 15286e142f54Sdrh sqliteFree(z); 15296e142f54Sdrh break; 15306e142f54Sdrh } 15311ccde15dSdrh /* Fall through into TK_NOT */ 15326e142f54Sdrh } 1533bf4133cbSdrh case TK_BITNOT: 15346e142f54Sdrh case TK_NOT: { 1535f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 1536f2bc013cSdrh assert( TK_NOT==OP_Not ); 15374adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15384adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1539cce7d176Sdrh break; 1540cce7d176Sdrh } 1541cce7d176Sdrh case TK_ISNULL: 1542cce7d176Sdrh case TK_NOTNULL: { 1543cce7d176Sdrh int dest; 1544f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1545f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 15464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 15474adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15484adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 15494adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 15504adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1551a37cdde0Sdanielk1977 break; 1552f2bc013cSdrh } 15532282792aSdrh case TK_AGG_FUNCTION: { 15544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 15552282792aSdrh break; 15562282792aSdrh } 1557b71090fdSdrh case TK_CONST_FUNC: 1558cce7d176Sdrh case TK_FUNCTION: { 1559cce7d176Sdrh ExprList *pList = pExpr->pList; 156089425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 15610bce8354Sdrh FuncDef *pDef; 15624b59ab5eSdrh int nId; 15634b59ab5eSdrh const char *zId; 1564682f68b0Sdanielk1977 int p2 = 0; 1565682f68b0Sdanielk1977 int i; 1566d8123366Sdanielk1977 u8 enc = pParse->db->enc; 1567dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 1568b71090fdSdrh zId = pExpr->token.z; 1569b71090fdSdrh nId = pExpr->token.n; 1570d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 15710bce8354Sdrh assert( pDef!=0 ); 1572f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1573682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 1574d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 1575d02eb1fdSdanielk1977 p2 |= (1<<i); 1576d02eb1fdSdanielk1977 } 1577dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 1578dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 1579dc1bdc4fSdanielk1977 } 1580dc1bdc4fSdanielk1977 } 1581dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 1582dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 1583d8123366Sdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 1584682f68b0Sdanielk1977 } 1585682f68b0Sdanielk1977 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF); 15866ec2733bSdrh break; 15876ec2733bSdrh } 1588fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1589fe2093d7Sdrh case TK_EXISTS: 159019a775c2Sdrh case TK_SELECT: { 1591b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 15924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1593ad6d9460Sdrh VdbeComment((v, "# load subquery result")); 159419a775c2Sdrh break; 159519a775c2Sdrh } 1596fef5208cSdrh case TK_IN: { 1597fef5208cSdrh int addr; 159894a11211Sdrh char affinity; 1599b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 1600e014a838Sdanielk1977 1601e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1602e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1603ededfd5eSdanielk1977 ** P3 of OP_MakeRecord. 1604e014a838Sdanielk1977 */ 160594a11211Sdrh affinity = comparisonAffinity(pExpr); 1606e014a838Sdanielk1977 16074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1608e014a838Sdanielk1977 1609e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1610e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1611e014a838Sdanielk1977 */ 16124adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 16134adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1614e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 16154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 1616f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1617e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 161894a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ 1619e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1620e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1621e014a838Sdanielk1977 1622fef5208cSdrh break; 1623fef5208cSdrh } 162493758c8dSdanielk1977 #endif 1625fef5208cSdrh case TK_BETWEEN: { 1626be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1627be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 1628be5c89acSdrh Expr *pRight = pLItem->pExpr; 1629be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 16304adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1631be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1632be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 16334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1634be5c89acSdrh pLItem++; 1635be5c89acSdrh pRight = pLItem->pExpr; 1636be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1637be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 16384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1639fef5208cSdrh break; 1640fef5208cSdrh } 164151e9a445Sdrh case TK_UPLUS: 1642a2e00042Sdrh case TK_AS: { 16434adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1644a2e00042Sdrh break; 1645a2e00042Sdrh } 164617a7f8ddSdrh case TK_CASE: { 164717a7f8ddSdrh int expr_end_label; 1648f5905aa7Sdrh int jumpInst; 1649f5905aa7Sdrh int addr; 1650f5905aa7Sdrh int nExpr; 165117a7f8ddSdrh int i; 1652be5c89acSdrh ExprList *pEList; 1653be5c89acSdrh struct ExprList_item *aListelem; 165417a7f8ddSdrh 165517a7f8ddSdrh assert(pExpr->pList); 165617a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 165717a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1658be5c89acSdrh pEList = pExpr->pList; 1659be5c89acSdrh aListelem = pEList->a; 1660be5c89acSdrh nExpr = pEList->nExpr; 16614adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 166217a7f8ddSdrh if( pExpr->pLeft ){ 16634adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1664cce7d176Sdrh } 1665f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 1666be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr); 166717a7f8ddSdrh if( pExpr->pLeft ){ 16684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1669be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 1670be5c89acSdrh OP_Ne, 0, 1); 16714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1672f5905aa7Sdrh }else{ 16734adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 167417a7f8ddSdrh } 1675be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr); 16764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 16774adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 16784adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 167917a7f8ddSdrh } 1680f570f011Sdrh if( pExpr->pLeft ){ 16814adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1682f570f011Sdrh } 168317a7f8ddSdrh if( pExpr->pRight ){ 16844adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 168517a7f8ddSdrh }else{ 1686f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 168717a7f8ddSdrh } 16884adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 16896f34903eSdanielk1977 break; 16906f34903eSdanielk1977 } 16915338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 16926f34903eSdanielk1977 case TK_RAISE: { 16936f34903eSdanielk1977 if( !pParse->trigStack ){ 16944adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1695da93d238Sdrh "RAISE() may only be used within a trigger-program"); 16966f34903eSdanielk1977 return; 16976f34903eSdanielk1977 } 1698ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 1699ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 17006f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 1701ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 17024adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1703701a0aebSdrh pExpr->token.z, pExpr->token.n); 17044adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 17056f34903eSdanielk1977 } else { 17066f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 1707344737f6Sdrh sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 1708ad6d9460Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 1709ad6d9460Sdrh VdbeComment((v, "# raise(IGNORE)")); 17106f34903eSdanielk1977 } 171117a7f8ddSdrh } 17125338a5f7Sdanielk1977 #endif 171317a7f8ddSdrh break; 171417a7f8ddSdrh } 1715cce7d176Sdrh } 1716cce7d176Sdrh 171793758c8dSdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 1718cce7d176Sdrh /* 171925303780Sdrh ** Generate code that evalutes the given expression and leaves the result 172025303780Sdrh ** on the stack. See also sqlite3ExprCode(). 172125303780Sdrh ** 172225303780Sdrh ** This routine might also cache the result and modify the pExpr tree 172325303780Sdrh ** so that it will make use of the cached result on subsequent evaluations 172425303780Sdrh ** rather than evaluate the whole expression again. Trivial expressions are 172525303780Sdrh ** not cached. If the expression is cached, its result is stored in a 172625303780Sdrh ** memory location. 172725303780Sdrh */ 172825303780Sdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ 172925303780Sdrh Vdbe *v = pParse->pVdbe; 173025303780Sdrh int iMem; 173125303780Sdrh int addr1, addr2; 173225303780Sdrh if( v==0 ) return; 173325303780Sdrh addr1 = sqlite3VdbeCurrentAddr(v); 173425303780Sdrh sqlite3ExprCode(pParse, pExpr); 173525303780Sdrh addr2 = sqlite3VdbeCurrentAddr(v); 173625303780Sdrh if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){ 173725303780Sdrh iMem = pExpr->iTable = pParse->nMem++; 173825303780Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); 173925303780Sdrh pExpr->op = TK_REGISTER; 174025303780Sdrh } 174125303780Sdrh } 174293758c8dSdanielk1977 #endif 174325303780Sdrh 174425303780Sdrh /* 1745268380caSdrh ** Generate code that pushes the value of every element of the given 1746f9b596ebSdrh ** expression list onto the stack. 1747268380caSdrh ** 1748268380caSdrh ** Return the number of elements pushed onto the stack. 1749268380caSdrh */ 17504adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1751268380caSdrh Parse *pParse, /* Parsing context */ 1752f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1753268380caSdrh ){ 1754268380caSdrh struct ExprList_item *pItem; 1755268380caSdrh int i, n; 1756268380caSdrh Vdbe *v; 1757268380caSdrh if( pList==0 ) return 0; 17584adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1759268380caSdrh n = pList->nExpr; 1760268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 17614adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1762268380caSdrh } 1763f9b596ebSdrh return n; 1764268380caSdrh } 1765268380caSdrh 1766268380caSdrh /* 1767cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1768cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1769cce7d176Sdrh ** continues straight thru if the expression is false. 1770f5905aa7Sdrh ** 1771f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1772f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1773f2bc013cSdrh ** 1774f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1775f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1776f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1777f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1778f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1779cce7d176Sdrh */ 17804adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1781cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1782cce7d176Sdrh int op = 0; 1783daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1784f2bc013cSdrh op = pExpr->op; 1785f2bc013cSdrh switch( op ){ 1786cce7d176Sdrh case TK_AND: { 17874adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 17884adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 17894adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 17904adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1791cce7d176Sdrh break; 1792cce7d176Sdrh } 1793cce7d176Sdrh case TK_OR: { 17944adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 17954adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1796cce7d176Sdrh break; 1797cce7d176Sdrh } 1798cce7d176Sdrh case TK_NOT: { 17994adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1800cce7d176Sdrh break; 1801cce7d176Sdrh } 1802cce7d176Sdrh case TK_LT: 1803cce7d176Sdrh case TK_LE: 1804cce7d176Sdrh case TK_GT: 1805cce7d176Sdrh case TK_GE: 1806cce7d176Sdrh case TK_NE: 18070ac65892Sdrh case TK_EQ: { 1808f2bc013cSdrh assert( TK_LT==OP_Lt ); 1809f2bc013cSdrh assert( TK_LE==OP_Le ); 1810f2bc013cSdrh assert( TK_GT==OP_Gt ); 1811f2bc013cSdrh assert( TK_GE==OP_Ge ); 1812f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1813f2bc013cSdrh assert( TK_NE==OP_Ne ); 18144adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18154adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1816be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1817cce7d176Sdrh break; 1818cce7d176Sdrh } 1819cce7d176Sdrh case TK_ISNULL: 1820cce7d176Sdrh case TK_NOTNULL: { 1821f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1822f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 18234adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18244adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1825cce7d176Sdrh break; 1826cce7d176Sdrh } 1827fef5208cSdrh case TK_BETWEEN: { 18280202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 18290202b29eSdanielk1977 ** 18300202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 18310202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 18320202b29eSdanielk1977 ** 3 ... 18330202b29eSdanielk1977 */ 1834f5905aa7Sdrh int addr; 1835be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1836be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1837be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 18384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1839be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1840be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 18410202b29eSdanielk1977 1842be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1843be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1844be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 18450202b29eSdanielk1977 18464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 18474adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 18484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1849fef5208cSdrh break; 1850fef5208cSdrh } 1851cce7d176Sdrh default: { 18524adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 18534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1854cce7d176Sdrh break; 1855cce7d176Sdrh } 1856cce7d176Sdrh } 1857cce7d176Sdrh } 1858cce7d176Sdrh 1859cce7d176Sdrh /* 186066b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1861cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1862cce7d176Sdrh ** continues straight thru if the expression is true. 1863f5905aa7Sdrh ** 1864f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1865f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1866cce7d176Sdrh */ 18674adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1868cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1869cce7d176Sdrh int op = 0; 1870daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1871f2bc013cSdrh 1872f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 1873f2bc013cSdrh ** 1874f2bc013cSdrh ** pExpr->op op 1875f2bc013cSdrh ** --------- ---------- 1876f2bc013cSdrh ** TK_ISNULL OP_NotNull 1877f2bc013cSdrh ** TK_NOTNULL OP_IsNull 1878f2bc013cSdrh ** TK_NE OP_Eq 1879f2bc013cSdrh ** TK_EQ OP_Ne 1880f2bc013cSdrh ** TK_GT OP_Le 1881f2bc013cSdrh ** TK_LE OP_Gt 1882f2bc013cSdrh ** TK_GE OP_Lt 1883f2bc013cSdrh ** TK_LT OP_Ge 1884f2bc013cSdrh ** 1885f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 1886f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 1887f2bc013cSdrh ** can compute the mapping above using the following expression. 1888f2bc013cSdrh ** Assert()s verify that the computation is correct. 1889f2bc013cSdrh */ 1890f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 1891f2bc013cSdrh 1892f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 1893f2bc013cSdrh */ 1894f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 1895f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 1896f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 1897f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 1898f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 1899f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 1900f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 1901f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 1902f2bc013cSdrh 1903cce7d176Sdrh switch( pExpr->op ){ 1904cce7d176Sdrh case TK_AND: { 19054adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 19064adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1907cce7d176Sdrh break; 1908cce7d176Sdrh } 1909cce7d176Sdrh case TK_OR: { 19104adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 19114adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 19124adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 19134adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1914cce7d176Sdrh break; 1915cce7d176Sdrh } 1916cce7d176Sdrh case TK_NOT: { 19174adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1918cce7d176Sdrh break; 1919cce7d176Sdrh } 1920cce7d176Sdrh case TK_LT: 1921cce7d176Sdrh case TK_LE: 1922cce7d176Sdrh case TK_GT: 1923cce7d176Sdrh case TK_GE: 1924cce7d176Sdrh case TK_NE: 1925cce7d176Sdrh case TK_EQ: { 19264adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 19274adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1928be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1929cce7d176Sdrh break; 1930cce7d176Sdrh } 1931cce7d176Sdrh case TK_ISNULL: 1932cce7d176Sdrh case TK_NOTNULL: { 19334adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 19344adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1935cce7d176Sdrh break; 1936cce7d176Sdrh } 1937fef5208cSdrh case TK_BETWEEN: { 19380202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 19390202b29eSdanielk1977 ** 19400202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 19410202b29eSdanielk1977 ** 2 GOTO <dest> 19420202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 19430202b29eSdanielk1977 */ 1944fef5208cSdrh int addr; 1945be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1946be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1947be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 19484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1949be5c89acSdrh sqlite3ExprCode(pParse, pRight); 19504adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1951be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 1952be5c89acSdrh 19534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 19544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 1955be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1956be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1957be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 1958fef5208cSdrh break; 1959fef5208cSdrh } 1960cce7d176Sdrh default: { 19614adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 19624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1963cce7d176Sdrh break; 1964cce7d176Sdrh } 1965cce7d176Sdrh } 1966cce7d176Sdrh } 19672282792aSdrh 19682282792aSdrh /* 19692282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 19702282792aSdrh ** if they are identical and return FALSE if they differ in any way. 19712282792aSdrh */ 19724adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 19732282792aSdrh int i; 19742282792aSdrh if( pA==0 ){ 19752282792aSdrh return pB==0; 19762282792aSdrh }else if( pB==0 ){ 19772282792aSdrh return 0; 19782282792aSdrh } 19792282792aSdrh if( pA->op!=pB->op ) return 0; 19804adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 19814adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 19822282792aSdrh if( pA->pList ){ 19832282792aSdrh if( pB->pList==0 ) return 0; 19842282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 19852282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 19864adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 19872282792aSdrh return 0; 19882282792aSdrh } 19892282792aSdrh } 19902282792aSdrh }else if( pB->pList ){ 19912282792aSdrh return 0; 19922282792aSdrh } 19932282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 19942f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 19952282792aSdrh if( pA->token.z ){ 19962282792aSdrh if( pB->token.z==0 ) return 0; 19976977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 19984adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 19992282792aSdrh } 20002282792aSdrh return 1; 20012282792aSdrh } 20022282792aSdrh 20032282792aSdrh /* 20042282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 200573b211abSdrh ** The new element is initialized to zero. The calling function is 200673b211abSdrh ** expected to fill it in. 20072282792aSdrh */ 20082282792aSdrh static int appendAggInfo(Parse *pParse){ 20092282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 20102282792aSdrh int amt = pParse->nAgg + 8; 20116d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 20126d4abfbeSdrh if( aAgg==0 ){ 20132282792aSdrh return -1; 20142282792aSdrh } 20156d4abfbeSdrh pParse->aAgg = aAgg; 20162282792aSdrh } 20172282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 20182282792aSdrh return pParse->nAgg++; 20192282792aSdrh } 20202282792aSdrh 20212282792aSdrh /* 2022626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 2023626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 2024626a879aSdrh ** for additional information. 20252282792aSdrh ** 2026626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 20272282792aSdrh */ 2028626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 20292282792aSdrh int i; 20302282792aSdrh AggExpr *aAgg; 2031a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 2032a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 2033a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 20342282792aSdrh 20352282792aSdrh switch( pExpr->op ){ 2036967e8b73Sdrh case TK_COLUMN: { 2037a58fdfb1Sdanielk1977 for(i=0; pSrcList && i<pSrcList->nSrc; i++){ 2038a58fdfb1Sdanielk1977 if( pExpr->iTable==pSrcList->a[i].iCursor ){ 20392282792aSdrh aAgg = pParse->aAgg; 20402282792aSdrh for(i=0; i<pParse->nAgg; i++){ 20412282792aSdrh if( aAgg[i].isAgg ) continue; 20422282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 2043967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 20442282792aSdrh break; 20452282792aSdrh } 20462282792aSdrh } 20472282792aSdrh if( i>=pParse->nAgg ){ 20482282792aSdrh i = appendAggInfo(pParse); 20492282792aSdrh if( i<0 ) return 1; 20502282792aSdrh pParse->aAgg[i].isAgg = 0; 20512282792aSdrh pParse->aAgg[i].pExpr = pExpr; 20522282792aSdrh } 2053aaf88729Sdrh pExpr->iAgg = i; 2054a58fdfb1Sdanielk1977 pExpr->iAggCtx = pNC->nDepth; 2055a58fdfb1Sdanielk1977 return 1; 2056a58fdfb1Sdanielk1977 } 2057a58fdfb1Sdanielk1977 } 2058626a879aSdrh return 1; 20592282792aSdrh } 20602282792aSdrh case TK_AGG_FUNCTION: { 2061a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 20622282792aSdrh aAgg = pParse->aAgg; 20632282792aSdrh for(i=0; i<pParse->nAgg; i++){ 20642282792aSdrh if( !aAgg[i].isAgg ) continue; 20654adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 20662282792aSdrh break; 20672282792aSdrh } 20682282792aSdrh } 20692282792aSdrh if( i>=pParse->nAgg ){ 2070d8123366Sdanielk1977 u8 enc = pParse->db->enc; 20712282792aSdrh i = appendAggInfo(pParse); 20722282792aSdrh if( i<0 ) return 1; 20732282792aSdrh pParse->aAgg[i].isAgg = 1; 20742282792aSdrh pParse->aAgg[i].pExpr = pExpr; 20754adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 20766977fea8Sdrh pExpr->token.z, pExpr->token.n, 2077d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 20782282792aSdrh } 20792282792aSdrh pExpr->iAgg = i; 2080626a879aSdrh return 1; 20812282792aSdrh } 20822282792aSdrh } 2083a58fdfb1Sdanielk1977 } 2084a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 2085a58fdfb1Sdanielk1977 pNC->nDepth++; 2086a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 2087a58fdfb1Sdanielk1977 pNC->nDepth--; 2088a58fdfb1Sdanielk1977 } 2089626a879aSdrh return 0; 20902282792aSdrh } 2091626a879aSdrh 2092626a879aSdrh /* 2093626a879aSdrh ** Analyze the given expression looking for aggregate functions and 2094626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 2095626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 2096626a879aSdrh ** 2097626a879aSdrh ** This routine should only be called after the expression has been 2098626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 2099626a879aSdrh ** 2100626a879aSdrh ** If errors are seen, leave an error message in zErrMsg and return 2101626a879aSdrh ** the number of errors. 2102626a879aSdrh */ 2103a58fdfb1Sdanielk1977 int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 2104a58fdfb1Sdanielk1977 int nErr = pNC->pParse->nErr; 2105a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 2106a58fdfb1Sdanielk1977 return pNC->pParse->nErr - nErr; 21072282792aSdrh } 2108