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*288d37f1Sdrh ** $Id: expr.c,v 1.207 2005/06/22 08:48:06 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){ 37a37cdde0Sdanielk1977 if( pExpr->op==TK_AS ){ 38bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pLeft); 39a37cdde0Sdanielk1977 } 40a37cdde0Sdanielk1977 if( pExpr->op==TK_SELECT ){ 41bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 42a37cdde0Sdanielk1977 } 43a37cdde0Sdanielk1977 return pExpr->affinity; 44a37cdde0Sdanielk1977 } 45a37cdde0Sdanielk1977 4653db1458Sdrh /* 470202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 480202b29eSdanielk1977 ** there is no default collation type, return 0. 490202b29eSdanielk1977 */ 507cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 517cedc8d4Sdanielk1977 CollSeq *pColl = 0; 520202b29eSdanielk1977 if( pExpr ){ 537cedc8d4Sdanielk1977 pColl = pExpr->pColl; 547cedc8d4Sdanielk1977 if( pExpr->op==TK_AS && !pColl ){ 557cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 560202b29eSdanielk1977 } 570202b29eSdanielk1977 } 587cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 597cedc8d4Sdanielk1977 pColl = 0; 607cedc8d4Sdanielk1977 } 617cedc8d4Sdanielk1977 return pColl; 620202b29eSdanielk1977 } 630202b29eSdanielk1977 640202b29eSdanielk1977 /* 65626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 66626a879aSdrh ** type affinity of the other operand. This routine returns the 6753db1458Sdrh ** type affinity that should be used for the comparison operator. 6853db1458Sdrh */ 69e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 70bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 71e014a838Sdanielk1977 if( aff1 && aff2 ){ 72e014a838Sdanielk1977 /* Both sides of the comparison are columns. If one has numeric or 73e014a838Sdanielk1977 ** integer affinity, use that. Otherwise use no affinity. 74e014a838Sdanielk1977 */ 75e014a838Sdanielk1977 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ 76e014a838Sdanielk1977 return SQLITE_AFF_INTEGER; 77e014a838Sdanielk1977 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 78e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 79e014a838Sdanielk1977 }else{ 80e014a838Sdanielk1977 return SQLITE_AFF_NONE; 81e014a838Sdanielk1977 } 82e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 835f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 845f6a87b3Sdrh ** results directly. 85e014a838Sdanielk1977 */ 865f6a87b3Sdrh /* return SQLITE_AFF_NUMERIC; // Ticket #805 */ 875f6a87b3Sdrh return SQLITE_AFF_NONE; 88e014a838Sdanielk1977 }else{ 89e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 90e014a838Sdanielk1977 return (aff1 + aff2); 91e014a838Sdanielk1977 } 92e014a838Sdanielk1977 } 93e014a838Sdanielk1977 9453db1458Sdrh /* 9553db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 9653db1458Sdrh ** be applied to both operands prior to doing the comparison. 9753db1458Sdrh */ 98e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 99e014a838Sdanielk1977 char aff; 100e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 101e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 102e014a838Sdanielk1977 pExpr->op==TK_NE ); 103e014a838Sdanielk1977 assert( pExpr->pLeft ); 104bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 105e014a838Sdanielk1977 if( pExpr->pRight ){ 106e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 107e014a838Sdanielk1977 } 108e014a838Sdanielk1977 else if( pExpr->pSelect ){ 109e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 110e014a838Sdanielk1977 } 111e014a838Sdanielk1977 else if( !aff ){ 112e014a838Sdanielk1977 aff = SQLITE_AFF_NUMERIC; 113e014a838Sdanielk1977 } 114e014a838Sdanielk1977 return aff; 115e014a838Sdanielk1977 } 116e014a838Sdanielk1977 117e014a838Sdanielk1977 /* 118e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 119e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 120e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 121e014a838Sdanielk1977 ** the comparison in pExpr. 122e014a838Sdanielk1977 */ 123e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 124e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 125e014a838Sdanielk1977 return 126e014a838Sdanielk1977 (aff==SQLITE_AFF_NONE) || 127e014a838Sdanielk1977 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) || 128e014a838Sdanielk1977 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) || 129e014a838Sdanielk1977 (aff==idx_affinity); 130e014a838Sdanielk1977 } 131e014a838Sdanielk1977 132a37cdde0Sdanielk1977 /* 133a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 134a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 135a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 136a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 137a37cdde0Sdanielk1977 ** evaluates to NULL. 138a37cdde0Sdanielk1977 */ 139e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 140bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 141f0863fe5Sdrh return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0); 142a37cdde0Sdanielk1977 } 143a37cdde0Sdanielk1977 144a2e00042Sdrh /* 1450202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1460202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1470202b29eSdanielk1977 ** 1480202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1490202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1500202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1510202b29eSdanielk1977 ** type. 1520202b29eSdanielk1977 */ 1537cedc8d4Sdanielk1977 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ 1547cedc8d4Sdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft); 1550202b29eSdanielk1977 if( !pColl ){ 1567cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 1570202b29eSdanielk1977 } 1580202b29eSdanielk1977 return pColl; 1590202b29eSdanielk1977 } 1600202b29eSdanielk1977 1610202b29eSdanielk1977 /* 162be5c89acSdrh ** Generate code for a comparison operator. 163be5c89acSdrh */ 164be5c89acSdrh static int codeCompare( 165be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 166be5c89acSdrh Expr *pLeft, /* The left operand */ 167be5c89acSdrh Expr *pRight, /* The right operand */ 168be5c89acSdrh int opcode, /* The comparison opcode */ 169be5c89acSdrh int dest, /* Jump here if true. */ 170be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 171be5c89acSdrh ){ 172be5c89acSdrh int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); 173be5c89acSdrh CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); 174be5c89acSdrh return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); 175be5c89acSdrh } 176be5c89acSdrh 177be5c89acSdrh /* 178a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 179a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 180a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 181a76b5dfcSdrh */ 182e4e72072Sdrh Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ 183a76b5dfcSdrh Expr *pNew; 184a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 185a76b5dfcSdrh if( pNew==0 ){ 186d5d56523Sdanielk1977 /* When malloc fails, delete pLeft and pRight. Expressions passed to 187d5d56523Sdanielk1977 ** this function must always be allocated with sqlite3Expr() for this 188d5d56523Sdanielk1977 ** reason. 189d5d56523Sdanielk1977 */ 190d5d56523Sdanielk1977 sqlite3ExprDelete(pLeft); 191d5d56523Sdanielk1977 sqlite3ExprDelete(pRight); 192a76b5dfcSdrh return 0; 193a76b5dfcSdrh } 194a76b5dfcSdrh pNew->op = op; 195a76b5dfcSdrh pNew->pLeft = pLeft; 196a76b5dfcSdrh pNew->pRight = pRight; 197a58fdfb1Sdanielk1977 pNew->iAgg = -1; 198a76b5dfcSdrh if( pToken ){ 1994b59ab5eSdrh assert( pToken->dyn==0 ); 200145716b3Sdrh pNew->span = pNew->token = *pToken; 201145716b3Sdrh }else if( pLeft && pRight ){ 2024adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 203a76b5dfcSdrh } 204a76b5dfcSdrh return pNew; 205a76b5dfcSdrh } 206a76b5dfcSdrh 207a76b5dfcSdrh /* 2084e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 2094e0cff60Sdrh ** that look like this: #0 #1 #2 ... These terms refer to elements 210*288d37f1Sdrh ** on the stack. "#0" means the top of the stack. 211*288d37f1Sdrh ** "#1" means the next down on the stack. And so forth. 2124e0cff60Sdrh ** 2134e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 2144e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 2154e0cff60Sdrh ** The returns an expression that will code to extract the value from 2164e0cff60Sdrh ** that memory location as needed. 2174e0cff60Sdrh */ 2184e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 2194e0cff60Sdrh Vdbe *v = pParse->pVdbe; 2204e0cff60Sdrh Expr *p; 2214e0cff60Sdrh int depth; 2224e0cff60Sdrh if( v==0 ) return 0; 2234e0cff60Sdrh if( pParse->nested==0 ){ 2244e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 2254e0cff60Sdrh return 0; 2264e0cff60Sdrh } 2274e0cff60Sdrh p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); 22873c42a13Sdrh if( p==0 ){ 22973c42a13Sdrh return 0; /* Malloc failed */ 23073c42a13Sdrh } 2314e0cff60Sdrh depth = atoi(&pToken->z[1]); 2324e0cff60Sdrh p->iTable = pParse->nMem++; 2334e0cff60Sdrh sqlite3VdbeAddOp(v, OP_Dup, depth, 0); 2344e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1); 2354e0cff60Sdrh return p; 2364e0cff60Sdrh } 2374e0cff60Sdrh 2384e0cff60Sdrh /* 23991bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 24091bb0eedSdrh ** NULL, then just return the other expression. 24191bb0eedSdrh */ 24291bb0eedSdrh Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ 24391bb0eedSdrh if( pLeft==0 ){ 24491bb0eedSdrh return pRight; 24591bb0eedSdrh }else if( pRight==0 ){ 24691bb0eedSdrh return pLeft; 24791bb0eedSdrh }else{ 24891bb0eedSdrh return sqlite3Expr(TK_AND, pLeft, pRight, 0); 24991bb0eedSdrh } 25091bb0eedSdrh } 25191bb0eedSdrh 25291bb0eedSdrh /* 2536977fea8Sdrh ** Set the Expr.span field of the given expression to span all 254a76b5dfcSdrh ** text between the two given tokens. 255a76b5dfcSdrh */ 2564adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 2574efc4754Sdrh assert( pRight!=0 ); 2584efc4754Sdrh assert( pLeft!=0 ); 25971c697efSdrh if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){ 260ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 261145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 2626977fea8Sdrh pExpr->span.z = pLeft->z; 26397903fefSdrh pExpr->span.n = pRight->n + (pRight->z - pLeft->z); 2644b59ab5eSdrh }else{ 2656977fea8Sdrh pExpr->span.z = 0; 2664b59ab5eSdrh } 267a76b5dfcSdrh } 268a76b5dfcSdrh } 269a76b5dfcSdrh 270a76b5dfcSdrh /* 271a76b5dfcSdrh ** Construct a new expression node for a function with multiple 272a76b5dfcSdrh ** arguments. 273a76b5dfcSdrh */ 2744adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 275a76b5dfcSdrh Expr *pNew; 276a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 277a76b5dfcSdrh if( pNew==0 ){ 278d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ 279a76b5dfcSdrh return 0; 280a76b5dfcSdrh } 281a76b5dfcSdrh pNew->op = TK_FUNCTION; 282a76b5dfcSdrh pNew->pList = pList; 283a76b5dfcSdrh if( pToken ){ 2844b59ab5eSdrh assert( pToken->dyn==0 ); 285a76b5dfcSdrh pNew->token = *pToken; 286a76b5dfcSdrh }else{ 287a76b5dfcSdrh pNew->token.z = 0; 288a76b5dfcSdrh } 2896977fea8Sdrh pNew->span = pNew->token; 290a76b5dfcSdrh return pNew; 291a76b5dfcSdrh } 292a76b5dfcSdrh 293a76b5dfcSdrh /* 294fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 295fa6bc000Sdrh ** in the original SQL statement. 296fa6bc000Sdrh ** 297fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 298fa6bc000Sdrh ** variable number. 299fa6bc000Sdrh ** 300fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 301fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 302fa6bc000Sdrh ** the SQL statement comes from an external source. 303fa6bc000Sdrh ** 304fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 305fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 306fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 307fa6bc000Sdrh ** assigned. 308fa6bc000Sdrh */ 309fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 310fa6bc000Sdrh Token *pToken; 311fa6bc000Sdrh if( pExpr==0 ) return; 312fa6bc000Sdrh pToken = &pExpr->token; 313fa6bc000Sdrh assert( pToken->n>=1 ); 314fa6bc000Sdrh assert( pToken->z!=0 ); 315fa6bc000Sdrh assert( pToken->z[0]!=0 ); 316fa6bc000Sdrh if( pToken->n==1 ){ 317fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 318fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 319fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 320fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 321fa6bc000Sdrh ** use it as the variable number */ 322fa6bc000Sdrh int i; 323fa6bc000Sdrh pExpr->iTable = i = atoi(&pToken->z[1]); 324fa6bc000Sdrh if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ 325fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 326fa6bc000Sdrh SQLITE_MAX_VARIABLE_NUMBER); 327fa6bc000Sdrh } 328fa6bc000Sdrh if( i>pParse->nVar ){ 329fa6bc000Sdrh pParse->nVar = i; 330fa6bc000Sdrh } 331fa6bc000Sdrh }else{ 332fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 333fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 334fa6bc000Sdrh ** has never appeared before, reuse the same variable number 335fa6bc000Sdrh */ 336fa6bc000Sdrh int i, n; 337fa6bc000Sdrh n = pToken->n; 338fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 339fa6bc000Sdrh Expr *pE; 340fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 341fa6bc000Sdrh && pE->token.n==n 342fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 343fa6bc000Sdrh pExpr->iTable = pE->iTable; 344fa6bc000Sdrh break; 345fa6bc000Sdrh } 346fa6bc000Sdrh } 347fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 348fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 349fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 350fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 351fa6bc000Sdrh pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr, 352fa6bc000Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); 353fa6bc000Sdrh } 354fa6bc000Sdrh if( !sqlite3_malloc_failed ){ 355fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 356fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 357fa6bc000Sdrh } 358fa6bc000Sdrh } 359fa6bc000Sdrh } 360fa6bc000Sdrh } 361fa6bc000Sdrh 362fa6bc000Sdrh /* 363a2e00042Sdrh ** Recursively delete an expression tree. 364a2e00042Sdrh */ 3654adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 366a2e00042Sdrh if( p==0 ) return; 3674efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 3684efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 3694adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 3704adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 3714adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 3724adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 373a2e00042Sdrh sqliteFree(p); 374a2e00042Sdrh } 375a2e00042Sdrh 376a76b5dfcSdrh 377a76b5dfcSdrh /* 378ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 379ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 380ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 381ff78bd2fSdrh ** without effecting the originals. 382ff78bd2fSdrh ** 3834adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 3844adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 385ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 386ff78bd2fSdrh ** 387ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 388ff78bd2fSdrh */ 3894adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 390ff78bd2fSdrh Expr *pNew; 391ff78bd2fSdrh if( p==0 ) return 0; 392fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 393ff78bd2fSdrh if( pNew==0 ) return 0; 3943b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 3956977fea8Sdrh if( p->token.z!=0 ){ 396b9ecf6faSdrh pNew->token.z = sqliteStrNDup(p->token.z, p->token.n); 3974b59ab5eSdrh pNew->token.dyn = 1; 3984b59ab5eSdrh }else{ 3994efc4754Sdrh assert( pNew->token.z==0 ); 4004b59ab5eSdrh } 4016977fea8Sdrh pNew->span.z = 0; 4024adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 4034adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 4044adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 4054adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 406aee18ef8Sdanielk1977 pNew->pTab = p->pTab; 407ff78bd2fSdrh return pNew; 408ff78bd2fSdrh } 4094adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 4104b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 4114b59ab5eSdrh if( pFrom->z ){ 4124b59ab5eSdrh pTo->n = pFrom->n; 4134b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 4144b59ab5eSdrh pTo->dyn = 1; 4154b59ab5eSdrh }else{ 4164b59ab5eSdrh pTo->z = 0; 4174b59ab5eSdrh } 4184b59ab5eSdrh } 4194adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 420ff78bd2fSdrh ExprList *pNew; 421145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 422ff78bd2fSdrh int i; 423ff78bd2fSdrh if( p==0 ) return 0; 424ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 425ff78bd2fSdrh if( pNew==0 ) return 0; 4264305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 4273e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 428e0048400Sdanielk1977 if( pItem==0 ){ 429e0048400Sdanielk1977 sqliteFree(pNew); 430e0048400Sdanielk1977 return 0; 431e0048400Sdanielk1977 } 432145716b3Sdrh pOldItem = p->a; 433145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 4344b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 435145716b3Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); 4366977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 4376977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 4384b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 4394b59ab5eSdrh ** the names of columns in the result set needs this information */ 4404adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 4414b59ab5eSdrh } 4421f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 44324b03fd0Sdanielk1977 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 444145716b3Sdrh pItem->zName = sqliteStrDup(pOldItem->zName); 445145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 446145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 4473e7bc9caSdrh pItem->done = 0; 448ff78bd2fSdrh } 449ff78bd2fSdrh return pNew; 450ff78bd2fSdrh } 45193758c8dSdanielk1977 45293758c8dSdanielk1977 /* 45393758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 45493758c8dSdanielk1977 ** the build, then none of the following routines, except for 45593758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 45693758c8dSdanielk1977 ** called with a NULL argument. 45793758c8dSdanielk1977 */ 4586a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 4596a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 4604adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 461ad3cab52Sdrh SrcList *pNew; 462ad3cab52Sdrh int i; 463113088ecSdrh int nByte; 464ad3cab52Sdrh if( p==0 ) return 0; 465113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 4664efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 467ad3cab52Sdrh if( pNew==0 ) return 0; 4684305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 469ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 4704efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 4714efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 472ed8a3bb1Sdrh Table *pTab; 4734efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 4744efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 4754efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 4764efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 4774efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 478ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 479ed8a3bb1Sdrh if( pTab ){ 480ed8a3bb1Sdrh pTab->nRef++; 481a1cb183dSdanielk1977 } 4824adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 4834adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 4844adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 4856c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 486ad3cab52Sdrh } 487ad3cab52Sdrh return pNew; 488ad3cab52Sdrh } 4894adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 490ff78bd2fSdrh IdList *pNew; 491ff78bd2fSdrh int i; 492ff78bd2fSdrh if( p==0 ) return 0; 4934efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 494ff78bd2fSdrh if( pNew==0 ) return 0; 4954305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 4964efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 497d5d56523Sdanielk1977 if( pNew->a==0 ){ 498d5d56523Sdanielk1977 sqliteFree(pNew); 499d5d56523Sdanielk1977 return 0; 500d5d56523Sdanielk1977 } 501ff78bd2fSdrh for(i=0; i<p->nId; i++){ 5024efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 5034efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 5044efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 5054efc4754Sdrh pNewItem->idx = pOldItem->idx; 506ff78bd2fSdrh } 507ff78bd2fSdrh return pNew; 508ff78bd2fSdrh } 5094adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 510ff78bd2fSdrh Select *pNew; 511ff78bd2fSdrh if( p==0 ) return 0; 5124efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 513ff78bd2fSdrh if( pNew==0 ) return 0; 514ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 5154adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 5164adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 5174adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 5184adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 5194adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 5204adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 521ff78bd2fSdrh pNew->op = p->op; 5224adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 523a2dc3b1aSdanielk1977 pNew->pLimit = sqlite3ExprDup(p->pLimit); 524a2dc3b1aSdanielk1977 pNew->pOffset = sqlite3ExprDup(p->pOffset); 5257b58daeaSdrh pNew->iLimit = -1; 5267b58daeaSdrh pNew->iOffset = -1; 527dc1bdc4fSdanielk1977 pNew->ppOpenTemp = 0; 528a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 529a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 530ff78bd2fSdrh return pNew; 531ff78bd2fSdrh } 53293758c8dSdanielk1977 #else 53393758c8dSdanielk1977 Select *sqlite3SelectDup(Select *p){ 53493758c8dSdanielk1977 assert( p==0 ); 53593758c8dSdanielk1977 return 0; 53693758c8dSdanielk1977 } 53793758c8dSdanielk1977 #endif 538ff78bd2fSdrh 539ff78bd2fSdrh 540ff78bd2fSdrh /* 541a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 542a76b5dfcSdrh ** initially NULL, then create a new expression list. 543a76b5dfcSdrh */ 5444adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 545a76b5dfcSdrh if( pList==0 ){ 546a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 547a76b5dfcSdrh if( pList==0 ){ 548d5d56523Sdanielk1977 goto no_mem; 549a76b5dfcSdrh } 5504efc4754Sdrh assert( pList->nAlloc==0 ); 551a76b5dfcSdrh } 5524305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 553d5d56523Sdanielk1977 struct ExprList_item *a; 554d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 555d5d56523Sdanielk1977 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); 556d5d56523Sdanielk1977 if( a==0 ){ 557d5d56523Sdanielk1977 goto no_mem; 558a76b5dfcSdrh } 559d5d56523Sdanielk1977 pList->a = a; 560d5d56523Sdanielk1977 pList->nAlloc = n; 561a76b5dfcSdrh } 5624efc4754Sdrh assert( pList->a!=0 ); 5634efc4754Sdrh if( pExpr || pName ){ 5644efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 5654efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 566a99db3b6Sdrh pItem->zName = sqlite3NameFromToken(pName); 567e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 568a76b5dfcSdrh } 569a76b5dfcSdrh return pList; 570d5d56523Sdanielk1977 571d5d56523Sdanielk1977 no_mem: 572d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 573d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 574d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 575d5d56523Sdanielk1977 return 0; 576a76b5dfcSdrh } 577a76b5dfcSdrh 578a76b5dfcSdrh /* 579a76b5dfcSdrh ** Delete an entire expression list. 580a76b5dfcSdrh */ 5814adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 582a76b5dfcSdrh int i; 583be5c89acSdrh struct ExprList_item *pItem; 584a76b5dfcSdrh if( pList==0 ) return; 5851bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 5861bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 587be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 588be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 589be5c89acSdrh sqliteFree(pItem->zName); 590a76b5dfcSdrh } 591a76b5dfcSdrh sqliteFree(pList->a); 592a76b5dfcSdrh sqliteFree(pList); 593a76b5dfcSdrh } 594a76b5dfcSdrh 595a76b5dfcSdrh /* 596626a879aSdrh ** Walk an expression tree. Call xFunc for each node visited. 59773b211abSdrh ** 598626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 599626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 600626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 601626a879aSdrh ** the tree walk completely. 602626a879aSdrh ** 603626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 604626a879aSdrh ** and 0 to continue. 605626a879aSdrh */ 606a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 607626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 608626a879aSdrh int rc; 609626a879aSdrh if( pExpr==0 ) return 0; 610626a879aSdrh rc = (*xFunc)(pArg, pExpr); 611626a879aSdrh if( rc==0 ){ 612626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 613626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 614a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 615626a879aSdrh } 616626a879aSdrh return rc>1; 617626a879aSdrh } 618626a879aSdrh 619626a879aSdrh /* 620a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 621a58fdfb1Sdanielk1977 */ 622a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 623a58fdfb1Sdanielk1977 int i; 624a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 625a58fdfb1Sdanielk1977 if( !p ) return 0; 626a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 627a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 628a58fdfb1Sdanielk1977 } 629a58fdfb1Sdanielk1977 return 0; 630a58fdfb1Sdanielk1977 } 631a58fdfb1Sdanielk1977 632a58fdfb1Sdanielk1977 /* 633a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 634a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 635a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 636a58fdfb1Sdanielk1977 */ 637a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 638a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 639a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 640a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 641a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 642a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 643a58fdfb1Sdanielk1977 return 0; 644a58fdfb1Sdanielk1977 } 645a58fdfb1Sdanielk1977 646a58fdfb1Sdanielk1977 647a58fdfb1Sdanielk1977 /* 648626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 649626a879aSdrh ** 650626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 65173b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 65273b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 65373b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 65473b211abSdrh ** then do nothing. 65573b211abSdrh ** 65673b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 65773b211abSdrh ** the expression as constant, then we assume the whole expression 65873b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 659626a879aSdrh */ 660626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 661626a879aSdrh switch( pExpr->op ){ 662626a879aSdrh case TK_ID: 663626a879aSdrh case TK_COLUMN: 664626a879aSdrh case TK_DOT: 665626a879aSdrh case TK_AGG_FUNCTION: 666626a879aSdrh case TK_FUNCTION: 667fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 668fe2093d7Sdrh case TK_SELECT: 669fe2093d7Sdrh case TK_EXISTS: 670fe2093d7Sdrh #endif 671626a879aSdrh *((int*)pArg) = 0; 672626a879aSdrh return 2; 673626a879aSdrh default: 674626a879aSdrh return 0; 675626a879aSdrh } 676626a879aSdrh } 677626a879aSdrh 678626a879aSdrh /* 679fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 680fef5208cSdrh ** and 0 if it involves variables. 6812398937bSdrh ** 6822398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 6832398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 6842398937bSdrh ** a constant. 685fef5208cSdrh */ 6864adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 687626a879aSdrh int isConst = 1; 688626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 689626a879aSdrh return isConst; 690fef5208cSdrh } 691fef5208cSdrh 692fef5208cSdrh /* 69373b211abSdrh ** If the expression p codes a constant integer that is small enough 694202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 695202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 696202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 697e4de1febSdrh */ 6984adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 699e4de1febSdrh switch( p->op ){ 700e4de1febSdrh case TK_INTEGER: { 701fec19aadSdrh if( sqlite3GetInt32(p->token.z, pValue) ){ 702e4de1febSdrh return 1; 703e4de1febSdrh } 704202b2df7Sdrh break; 705202b2df7Sdrh } 7064b59ab5eSdrh case TK_UPLUS: { 7074adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 7084b59ab5eSdrh } 709e4de1febSdrh case TK_UMINUS: { 710e4de1febSdrh int v; 7114adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 712e4de1febSdrh *pValue = -v; 713e4de1febSdrh return 1; 714e4de1febSdrh } 715e4de1febSdrh break; 716e4de1febSdrh } 717e4de1febSdrh default: break; 718e4de1febSdrh } 719e4de1febSdrh return 0; 720e4de1febSdrh } 721e4de1febSdrh 722e4de1febSdrh /* 723c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 724c4a3c779Sdrh */ 7254adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 7264adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 7274adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 7284adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 729c4a3c779Sdrh return 0; 730c4a3c779Sdrh } 731c4a3c779Sdrh 732c4a3c779Sdrh /* 7338141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 7348141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 7358141f61eSdrh ** expression node refer back to that source column. The following changes 7368141f61eSdrh ** are made to pExpr: 7378141f61eSdrh ** 7388141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 7398141f61eSdrh ** the table. 7408141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 7418141f61eSdrh ** from pSrcList. 7428141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 7438141f61eSdrh ** pExpr->op Set to TK_COLUMN. 7448141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 7458141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 7468141f61eSdrh ** 7478141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 7488141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 7498141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 7508141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 7518141f61eSdrh ** means that the form of the name is Z and that columns from any table 7528141f61eSdrh ** can be used. 7538141f61eSdrh ** 7548141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 7558141f61eSdrh ** in pParse and return non-zero. Return zero on success. 7568141f61eSdrh */ 7578141f61eSdrh static int lookupName( 7588141f61eSdrh Parse *pParse, /* The parsing context */ 7598141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 7608141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 7618141f61eSdrh Token *pColumnToken, /* Name of the column. */ 762626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 7638141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 7648141f61eSdrh ){ 7658141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 7668141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 7678141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 7688141f61eSdrh int i, j; /* Loop counters */ 7698141f61eSdrh int cnt = 0; /* Number of matching column names */ 7708141f61eSdrh int cntTab = 0; /* Number of matching table names */ 7719bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 77251669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 77351669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 77473b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 7758141f61eSdrh 7768141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 777a99db3b6Sdrh zDb = sqlite3NameFromToken(pDbToken); 778a99db3b6Sdrh zTab = sqlite3NameFromToken(pTableToken); 779a99db3b6Sdrh zCol = sqlite3NameFromToken(pColumnToken); 78024b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 781d5d56523Sdanielk1977 goto lookupname_end; 7828141f61eSdrh } 7838141f61eSdrh 7848141f61eSdrh pExpr->iTable = -1; 785626a879aSdrh while( pNC && cnt==0 ){ 786626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 787626a879aSdrh ExprList *pEList = pNC->pEList; 788626a879aSdrh 789626a879aSdrh /* assert( zTab==0 || pEList==0 ); */ 790b3bce662Sdanielk1977 if( pSrcList ){ 79151669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 7928141f61eSdrh Table *pTab = pItem->pTab; 7938141f61eSdrh Column *pCol; 7948141f61eSdrh 7958141f61eSdrh if( pTab==0 ) continue; 7968141f61eSdrh assert( pTab->nCol>0 ); 7978141f61eSdrh if( zTab ){ 7988141f61eSdrh if( pItem->zAlias ){ 7998141f61eSdrh char *zTabName = pItem->zAlias; 8004adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 8018141f61eSdrh }else{ 8028141f61eSdrh char *zTabName = pTab->zName; 8034adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 8044adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 8058141f61eSdrh continue; 8068141f61eSdrh } 8078141f61eSdrh } 8088141f61eSdrh } 8098141f61eSdrh if( 0==(cntTab++) ){ 8108141f61eSdrh pExpr->iTable = pItem->iCursor; 8118141f61eSdrh pExpr->iDb = pTab->iDb; 81251669863Sdrh pMatch = pItem; 8138141f61eSdrh } 8148141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 8154adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 816873fac0cSdrh IdList *pUsing; 8178141f61eSdrh cnt++; 8188141f61eSdrh pExpr->iTable = pItem->iCursor; 81951669863Sdrh pMatch = pItem; 8208141f61eSdrh pExpr->iDb = pTab->iDb; 8218141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 8228141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 823a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 8240202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 825355ef361Sdrh if( pItem->jointype & JT_NATURAL ){ 826355ef361Sdrh /* If this match occurred in the left table of a natural join, 827355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 828355ef361Sdrh pItem++; 829355ef361Sdrh i++; 830355ef361Sdrh } 831873fac0cSdrh if( (pUsing = pItem->pUsing)!=0 ){ 832873fac0cSdrh /* If this match occurs on a column that is in the USING clause 833873fac0cSdrh ** of a join, skip the search of the right table of the join 834873fac0cSdrh ** to avoid a duplicate match there. */ 835873fac0cSdrh int k; 836873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 837873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 838873fac0cSdrh pItem++; 839873fac0cSdrh i++; 840873fac0cSdrh break; 841873fac0cSdrh } 842873fac0cSdrh } 843873fac0cSdrh } 8448141f61eSdrh break; 8458141f61eSdrh } 8468141f61eSdrh } 8478141f61eSdrh } 848b3bce662Sdanielk1977 } 8498141f61eSdrh 850b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 8518141f61eSdrh /* If we have not already resolved the name, then maybe 8528141f61eSdrh ** it is a new.* or old.* trigger argument reference 8538141f61eSdrh */ 8548141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 8558141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 8568141f61eSdrh Table *pTab = 0; 8574adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 8588141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 8598141f61eSdrh assert( pTriggerStack->pTab ); 8608141f61eSdrh pTab = pTriggerStack->pTab; 8614adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 8628141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 8638141f61eSdrh assert( pTriggerStack->pTab ); 8648141f61eSdrh pTab = pTriggerStack->pTab; 8658141f61eSdrh } 8668141f61eSdrh 8678141f61eSdrh if( pTab ){ 8688141f61eSdrh int j; 8698141f61eSdrh Column *pCol = pTab->aCol; 8708141f61eSdrh 8718141f61eSdrh pExpr->iDb = pTab->iDb; 8728141f61eSdrh cntTab++; 8738141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 8744adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 8758141f61eSdrh cnt++; 8768141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 877a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 8780202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 879aee18ef8Sdanielk1977 pExpr->pTab = pTab; 8808141f61eSdrh break; 8818141f61eSdrh } 8828141f61eSdrh } 8838141f61eSdrh } 8848141f61eSdrh } 885b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 8868141f61eSdrh 8878141f61eSdrh /* 8888141f61eSdrh ** Perhaps the name is a reference to the ROWID 8898141f61eSdrh */ 8904adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 8918141f61eSdrh cnt = 1; 8928141f61eSdrh pExpr->iColumn = -1; 893a37cdde0Sdanielk1977 pExpr->affinity = SQLITE_AFF_INTEGER; 8948141f61eSdrh } 8958141f61eSdrh 8968141f61eSdrh /* 8978141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 8988141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 8998141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 9008141f61eSdrh ** 9018141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 9028141f61eSdrh ** 9038141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 9048141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 9058141f61eSdrh ** Note that the expression in the result set should have already been 9068141f61eSdrh ** resolved by the time the WHERE clause is resolved. 9078141f61eSdrh */ 90879d5f63fSdrh if( cnt==0 && pEList!=0 && zTab==0 ){ 9098141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 9108141f61eSdrh char *zAs = pEList->a[j].zName; 9114adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 9128141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 9138141f61eSdrh pExpr->op = TK_AS; 9148141f61eSdrh pExpr->iColumn = j; 9154adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 91615ccce1cSdrh cnt = 1; 9178141f61eSdrh assert( zTab==0 && zDb==0 ); 91815ccce1cSdrh goto lookupname_end_2; 9198141f61eSdrh } 9208141f61eSdrh } 9218141f61eSdrh } 9228141f61eSdrh 923626a879aSdrh /* Advance to the next name context. The loop will exit when either 924626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 925626a879aSdrh */ 926626a879aSdrh if( cnt==0 ){ 927626a879aSdrh pNC = pNC->pNext; 928626a879aSdrh } 929626a879aSdrh } 930626a879aSdrh 9318141f61eSdrh /* 9328141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 9338141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 9348141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 9358141f61eSdrh ** case, we need to return right away and not make any changes to 9368141f61eSdrh ** pExpr. 93715ccce1cSdrh ** 93815ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 93915ccce1cSdrh ** fields are not changed in any context. 9408141f61eSdrh */ 9418141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 9428141f61eSdrh sqliteFree(zCol); 9438141f61eSdrh return 0; 9448141f61eSdrh } 9458141f61eSdrh 9468141f61eSdrh /* 9478141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 9488141f61eSdrh ** more matches. Either way, we have an error. 9498141f61eSdrh */ 9508141f61eSdrh if( cnt!=1 ){ 9518141f61eSdrh char *z = 0; 9528141f61eSdrh char *zErr; 9538141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 9548141f61eSdrh if( zDb ){ 9554adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 9568141f61eSdrh }else if( zTab ){ 9574adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 9588141f61eSdrh }else{ 9598141f61eSdrh z = sqliteStrDup(zCol); 9608141f61eSdrh } 9614adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 9628141f61eSdrh sqliteFree(z); 96373b211abSdrh pTopNC->nErr++; 9648141f61eSdrh } 9658141f61eSdrh 96651669863Sdrh /* If a column from a table in pSrcList is referenced, then record 96751669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 96851669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 96951669863Sdrh ** column number is greater than the number of bits in the bitmask 97051669863Sdrh ** then set the high-order bit of the bitmask. 97151669863Sdrh */ 97251669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 97351669863Sdrh int n = pExpr->iColumn; 97451669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 97551669863Sdrh n = sizeof(Bitmask)*8-1; 97651669863Sdrh } 97751669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 97851669863Sdrh pMatch->colUsed |= 1<<n; 97951669863Sdrh } 98051669863Sdrh 981d5d56523Sdanielk1977 lookupname_end: 9828141f61eSdrh /* Clean up and return 9838141f61eSdrh */ 9848141f61eSdrh sqliteFree(zDb); 9858141f61eSdrh sqliteFree(zTab); 9864adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 9878141f61eSdrh pExpr->pLeft = 0; 9884adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 9898141f61eSdrh pExpr->pRight = 0; 9908141f61eSdrh pExpr->op = TK_COLUMN; 99115ccce1cSdrh lookupname_end_2: 99215ccce1cSdrh sqliteFree(zCol); 993626a879aSdrh if( cnt==1 ){ 994b3bce662Sdanielk1977 assert( pNC!=0 ); 995626a879aSdrh sqlite3AuthRead(pParse, pExpr, pNC->pSrcList); 996aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 997aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 998aee18ef8Sdanielk1977 } 99915ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 100015ccce1cSdrh ** the point where the name matched. */ 100115ccce1cSdrh for(;;){ 100215ccce1cSdrh assert( pTopNC!=0 ); 100315ccce1cSdrh pTopNC->nRef++; 100415ccce1cSdrh if( pTopNC==pNC ) break; 100515ccce1cSdrh pTopNC = pTopNC->pNext; 1006626a879aSdrh } 100715ccce1cSdrh return 0; 100815ccce1cSdrh } else { 100915ccce1cSdrh return 1; 101015ccce1cSdrh } 10118141f61eSdrh } 10128141f61eSdrh 10138141f61eSdrh /* 1014626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1015626a879aSdrh ** 101673b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1017626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 101873b211abSdrh ** the tree or 2 to abort the tree walk. 101973b211abSdrh ** 102073b211abSdrh ** This routine also does error checking and name resolution for 102173b211abSdrh ** function names. The operator for aggregate functions is changed 102273b211abSdrh ** to TK_AGG_FUNCTION. 1023626a879aSdrh */ 1024626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1025626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1026626a879aSdrh SrcList *pSrcList; 1027626a879aSdrh Parse *pParse; 1028626a879aSdrh 1029b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1030626a879aSdrh assert( pNC!=0 ); 1031626a879aSdrh pSrcList = pNC->pSrcList; 1032626a879aSdrh pParse = pNC->pParse; 1033b3bce662Sdanielk1977 1034626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1035626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1036626a879aSdrh #ifndef NDEBUG 1037626a879aSdrh if( pSrcList ){ 1038940fac9dSdanielk1977 int i; 1039626a879aSdrh for(i=0; i<pSrcList->nSrc; i++){ 1040626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1041626a879aSdrh } 1042626a879aSdrh } 1043626a879aSdrh #endif 1044626a879aSdrh switch( pExpr->op ){ 1045626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1046626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1047626a879aSdrh ** strings (ex: 'abc') are always string literals. 1048626a879aSdrh */ 1049626a879aSdrh case TK_STRING: { 1050626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1051626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1052626a879aSdrh } 1053626a879aSdrh /* A lone identifier is the name of a column. 1054626a879aSdrh */ 1055626a879aSdrh case TK_ID: { 1056626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1057626a879aSdrh return 1; 1058626a879aSdrh } 1059626a879aSdrh 1060626a879aSdrh /* A table name and column name: ID.ID 1061626a879aSdrh ** Or a database, table and column: ID.ID.ID 1062626a879aSdrh */ 1063626a879aSdrh case TK_DOT: { 1064626a879aSdrh Token *pColumn; 1065626a879aSdrh Token *pTable; 1066626a879aSdrh Token *pDb; 1067626a879aSdrh Expr *pRight; 1068626a879aSdrh 1069b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1070626a879aSdrh pRight = pExpr->pRight; 1071626a879aSdrh if( pRight->op==TK_ID ){ 1072626a879aSdrh pDb = 0; 1073626a879aSdrh pTable = &pExpr->pLeft->token; 1074626a879aSdrh pColumn = &pRight->token; 1075626a879aSdrh }else{ 1076626a879aSdrh assert( pRight->op==TK_DOT ); 1077626a879aSdrh pDb = &pExpr->pLeft->token; 1078626a879aSdrh pTable = &pRight->pLeft->token; 1079626a879aSdrh pColumn = &pRight->pRight->token; 1080626a879aSdrh } 1081626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1082626a879aSdrh return 1; 1083626a879aSdrh } 1084626a879aSdrh 1085626a879aSdrh /* Resolve function names 1086626a879aSdrh */ 1087b71090fdSdrh case TK_CONST_FUNC: 1088626a879aSdrh case TK_FUNCTION: { 1089626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1090626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1091626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1092626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1093626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1094626a879aSdrh int i; 1095626a879aSdrh int nId; /* Number of characters in function name */ 1096626a879aSdrh const char *zId; /* The function name. */ 109773b211abSdrh FuncDef *pDef; /* Information about the function */ 109873b211abSdrh int enc = pParse->db->enc; /* The database encoding */ 1099626a879aSdrh 1100b71090fdSdrh zId = pExpr->token.z; 1101b71090fdSdrh nId = pExpr->token.n; 1102626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1103626a879aSdrh if( pDef==0 ){ 1104626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1105626a879aSdrh if( pDef==0 ){ 1106626a879aSdrh no_such_func = 1; 1107626a879aSdrh }else{ 1108626a879aSdrh wrong_num_args = 1; 1109626a879aSdrh } 1110626a879aSdrh }else{ 1111626a879aSdrh is_agg = pDef->xFunc==0; 1112626a879aSdrh } 1113626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1114626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1115626a879aSdrh pNC->nErr++; 1116626a879aSdrh is_agg = 0; 1117626a879aSdrh }else if( no_such_func ){ 1118626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1119626a879aSdrh pNC->nErr++; 1120626a879aSdrh }else if( wrong_num_args ){ 1121626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1122626a879aSdrh nId, zId); 1123626a879aSdrh pNC->nErr++; 1124626a879aSdrh } 1125626a879aSdrh if( is_agg ){ 1126626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1127626a879aSdrh pNC->hasAgg = 1; 1128626a879aSdrh } 112973b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1130626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 113173b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1132626a879aSdrh } 113373b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1134626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1135626a879aSdrh ** type of the function 1136626a879aSdrh */ 1137626a879aSdrh return is_agg; 1138626a879aSdrh } 1139b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1140b3bce662Sdanielk1977 case TK_SELECT: 1141b3bce662Sdanielk1977 case TK_EXISTS: 1142b3bce662Sdanielk1977 #endif 1143b3bce662Sdanielk1977 case TK_IN: { 1144b3bce662Sdanielk1977 if( pExpr->pSelect ){ 1145b3bce662Sdanielk1977 int nRef = pNC->nRef; 1146b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1147b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1148b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1149b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1150b3bce662Sdanielk1977 } 1151b3bce662Sdanielk1977 } 1152b3bce662Sdanielk1977 } 1153626a879aSdrh } 1154626a879aSdrh return 0; 1155626a879aSdrh } 1156626a879aSdrh 1157626a879aSdrh /* 1158cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1159967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1160aacc543eSdrh ** index to the table in the table list and a column offset. The 1161aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1162aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1163832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1164aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1165aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1166aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1167aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1168aacc543eSdrh ** alias for ROWID. 116919a775c2Sdrh ** 1170626a879aSdrh ** Also resolve function names and check the functions for proper 1171626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1172626a879aSdrh ** have the correct number of arguments. Leave an error message 1173626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1174626a879aSdrh ** 117573b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 117673b211abSdrh ** property on the expression. 1177626a879aSdrh */ 1178626a879aSdrh int sqlite3ExprResolveNames( 1179b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1180b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1181626a879aSdrh ){ 118273b211abSdrh if( pExpr==0 ) return 0; 1183b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1184b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 118573b211abSdrh ExprSetProperty(pExpr, EP_Error); 118673b211abSdrh } 118773b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1188626a879aSdrh } 1189626a879aSdrh 11901398ad36Sdrh /* 11911398ad36Sdrh ** A pointer instance of this structure is used to pass information 11921398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 11931398ad36Sdrh */ 11941398ad36Sdrh typedef struct QueryCoder QueryCoder; 11951398ad36Sdrh struct QueryCoder { 11961398ad36Sdrh Parse *pParse; /* The parsing context */ 11971398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 11981398ad36Sdrh }; 11991398ad36Sdrh 1200626a879aSdrh 1201626a879aSdrh /* 1202626a879aSdrh ** Generate code for subqueries and IN operators. 1203626a879aSdrh ** 120473b211abSdrh ** IN operators comes in two forms: 1205fef5208cSdrh ** 1206fef5208cSdrh ** expr IN (exprlist) 1207fef5208cSdrh ** and 1208fef5208cSdrh ** expr IN (SELECT ...) 1209fef5208cSdrh ** 1210fef5208cSdrh ** The first form is handled by creating a set holding the list 1211fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 1212fef5208cSdrh ** a temporary table. 1213cce7d176Sdrh */ 121451522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1215b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 1216b3bce662Sdanielk1977 int label = 0; /* Address after sub-select code */ 1217b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1218b3bce662Sdanielk1977 if( v==0 ) return; 1219b3bce662Sdanielk1977 1220b3bce662Sdanielk1977 /* If this is not a variable (correlated) select, then execute 1221b3bce662Sdanielk1977 ** it only once. Unless this is part of a trigger program. In 1222b3bce662Sdanielk1977 ** that case re-execute every time (this could be optimized). 1223b3bce662Sdanielk1977 */ 1224b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 1225b3bce662Sdanielk1977 int mem = pParse->nMem++; 1226b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); 1227b3bce662Sdanielk1977 label = sqlite3VdbeMakeLabel(v); 1228b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_If, 0, label); 1229b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1230b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1); 1231b3bce662Sdanielk1977 } 1232b3bce662Sdanielk1977 1233b3bce662Sdanielk1977 if( pExpr->pSelect ){ 1234b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0); 1235b3bce662Sdanielk1977 } 12366a3ea0e6Sdrh 1237cce7d176Sdrh switch( pExpr->op ){ 1238fef5208cSdrh case TK_IN: { 1239e014a838Sdanielk1977 char affinity; 1240d3d39e93Sdrh KeyInfo keyInfo; 12410202b29eSdanielk1977 int addr; /* Address of OP_OpenTemp instruction */ 1242d3d39e93Sdrh 1243bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1244e014a838Sdanielk1977 1245e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 1246e014a838Sdanielk1977 ** expression it is handled the same way. A temporary table is 1247e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1248e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1249fef5208cSdrh ** 1250e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1251e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1252e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1253e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1254e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1255e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1256e014a838Sdanielk1977 ** is used. 1257fef5208cSdrh */ 1258832508b7Sdrh pExpr->iTable = pParse->nTab++; 12590202b29eSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0); 1260d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1261d3d39e93Sdrh keyInfo.nField = 1; 1262f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 1263e014a838Sdanielk1977 1264e014a838Sdanielk1977 if( pExpr->pSelect ){ 1265e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1266e014a838Sdanielk1977 ** 1267e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1268e014a838Sdanielk1977 ** table allocated and opened above. 1269e014a838Sdanielk1977 */ 1270e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 1271be5c89acSdrh ExprList *pEList; 1272e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 1273b3bce662Sdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 1274be5c89acSdrh pEList = pExpr->pSelect->pEList; 1275be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 12767cedc8d4Sdanielk1977 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 1277be5c89acSdrh pEList->a[0].pExpr); 12780202b29eSdanielk1977 } 1279fef5208cSdrh }else if( pExpr->pList ){ 1280fef5208cSdrh /* Case 2: expr IN (exprlist) 1281fef5208cSdrh ** 1282e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1283e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1284e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1285e014a838Sdanielk1977 ** a column, use numeric affinity. 1286fef5208cSdrh */ 1287e014a838Sdanielk1977 int i; 1288e014a838Sdanielk1977 if( !affinity ){ 1289e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 1290e014a838Sdanielk1977 } 12910202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1292e014a838Sdanielk1977 1293e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 1294fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 1295fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 1296e014a838Sdanielk1977 1297e014a838Sdanielk1977 /* Check that the expression is constant and valid. */ 12984adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 12994adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1300da93d238Sdrh "right-hand side of IN operator must be constant"); 1301b3bce662Sdanielk1977 return; 13024794b980Sdrh } 1303e014a838Sdanielk1977 1304e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 13054adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 130694a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 1307f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0); 1308fef5208cSdrh } 1309fef5208cSdrh } 13100202b29eSdanielk1977 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 1311b3bce662Sdanielk1977 break; 1312fef5208cSdrh } 1313fef5208cSdrh 131451522cd3Sdrh case TK_EXISTS: 131519a775c2Sdrh case TK_SELECT: { 1316fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1317fef5208cSdrh ** value of this select in a memory cell and record the number 1318967e8b73Sdrh ** of the memory cell in iColumn. 1319fef5208cSdrh */ 132051522cd3Sdrh int sop; 132151522cd3Sdrh Select *pSel; 13221398ad36Sdrh 1323967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 132451522cd3Sdrh pSel = pExpr->pSelect; 132551522cd3Sdrh if( pExpr->op==TK_SELECT ){ 132651522cd3Sdrh sop = SRT_Mem; 132751522cd3Sdrh }else{ 132851522cd3Sdrh static const Token one = { "1", 0, 1 }; 132951522cd3Sdrh sop = SRT_Exists; 133051522cd3Sdrh sqlite3ExprListDelete(pSel->pEList); 133151522cd3Sdrh pSel->pEList = sqlite3ExprListAppend(0, 133251522cd3Sdrh sqlite3Expr(TK_INTEGER, 0, 0, &one), 0); 133351522cd3Sdrh } 1334b3bce662Sdanielk1977 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0); 1335b3bce662Sdanielk1977 break; 133619a775c2Sdrh } 1337cce7d176Sdrh } 1338b3bce662Sdanielk1977 1339b3bce662Sdanielk1977 if( pExpr->pSelect ){ 1340b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0); 1341b3bce662Sdanielk1977 } 1342b3bce662Sdanielk1977 if( label<0 ){ 1343b3bce662Sdanielk1977 sqlite3VdbeResolveLabel(v, label); 1344b3bce662Sdanielk1977 } 1345b3bce662Sdanielk1977 return; 1346cce7d176Sdrh } 134751522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1348cce7d176Sdrh 1349cce7d176Sdrh /* 1350fec19aadSdrh ** Generate an instruction that will put the integer describe by 1351fec19aadSdrh ** text z[0..n-1] on the stack. 1352fec19aadSdrh */ 1353fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1354fec19aadSdrh int i; 13556fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 13566fec0762Sdrh sqlite3VdbeAddOp(v, OP_Integer, i, 0); 13576fec0762Sdrh }else if( sqlite3FitsIn64Bits(z) ){ 13586fec0762Sdrh sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n); 1359fec19aadSdrh }else{ 1360fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1361fec19aadSdrh } 1362fec19aadSdrh } 1363fec19aadSdrh 1364fec19aadSdrh /* 1365cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 13661ccde15dSdrh ** expression and leave the result on the top of stack. 1367f2bc013cSdrh ** 1368f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1369f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1370f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1371f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1372f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1373cce7d176Sdrh */ 13744adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1375cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1376cce7d176Sdrh int op; 13777977a17fSdanielk1977 if( v==0 ) return; 13787977a17fSdanielk1977 if( pExpr==0 ){ 1379f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 13807977a17fSdanielk1977 return; 13817977a17fSdanielk1977 } 1382f2bc013cSdrh op = pExpr->op; 1383f2bc013cSdrh switch( op ){ 1384967e8b73Sdrh case TK_COLUMN: { 1385a58fdfb1Sdanielk1977 if( !pParse->fillAgg && pExpr->iAgg>=0 ){ 1386a58fdfb1Sdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg); 1387c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 13884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1389aee18ef8Sdanielk1977 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn); 1390c4a3c779Sdrh }else{ 1391f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0); 13922282792aSdrh } 1393cce7d176Sdrh break; 1394cce7d176Sdrh } 1395cce7d176Sdrh case TK_INTEGER: { 1396fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1397fec19aadSdrh break; 139851e9a445Sdrh } 1399fec19aadSdrh case TK_FLOAT: 1400fec19aadSdrh case TK_STRING: { 1401f2bc013cSdrh assert( TK_FLOAT==OP_Real ); 1402f2bc013cSdrh assert( TK_STRING==OP_String8 ); 1403fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 14044adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1405cce7d176Sdrh break; 1406cce7d176Sdrh } 1407f0863fe5Sdrh case TK_NULL: { 1408f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1409f0863fe5Sdrh break; 1410f0863fe5Sdrh } 14115338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 1412c572ef7fSdanielk1977 case TK_BLOB: { 1413f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 1414c572ef7fSdanielk1977 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1); 1415c572ef7fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1416c572ef7fSdanielk1977 break; 1417c572ef7fSdanielk1977 } 14185338a5f7Sdanielk1977 #endif 141950457896Sdrh case TK_VARIABLE: { 14204adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1421895d7472Sdrh if( pExpr->token.n>1 ){ 1422895d7472Sdrh sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 1423895d7472Sdrh } 142450457896Sdrh break; 142550457896Sdrh } 14264e0cff60Sdrh case TK_REGISTER: { 14274e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); 14284e0cff60Sdrh break; 14294e0cff60Sdrh } 1430c9b84a1fSdrh case TK_LT: 1431c9b84a1fSdrh case TK_LE: 1432c9b84a1fSdrh case TK_GT: 1433c9b84a1fSdrh case TK_GE: 1434c9b84a1fSdrh case TK_NE: 1435c9b84a1fSdrh case TK_EQ: { 1436f2bc013cSdrh assert( TK_LT==OP_Lt ); 1437f2bc013cSdrh assert( TK_LE==OP_Le ); 1438f2bc013cSdrh assert( TK_GT==OP_Gt ); 1439f2bc013cSdrh assert( TK_GE==OP_Ge ); 1440f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1441f2bc013cSdrh assert( TK_NE==OP_Ne ); 1442a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1443a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1444be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 1445a37cdde0Sdanielk1977 break; 1446c9b84a1fSdrh } 1447cce7d176Sdrh case TK_AND: 1448cce7d176Sdrh case TK_OR: 1449cce7d176Sdrh case TK_PLUS: 1450cce7d176Sdrh case TK_STAR: 1451cce7d176Sdrh case TK_MINUS: 1452bf4133cbSdrh case TK_REM: 1453bf4133cbSdrh case TK_BITAND: 1454bf4133cbSdrh case TK_BITOR: 145517c40294Sdrh case TK_SLASH: 1456bf4133cbSdrh case TK_LSHIFT: 1457855eb1cfSdrh case TK_RSHIFT: 14580040077dSdrh case TK_CONCAT: { 1459f2bc013cSdrh assert( TK_AND==OP_And ); 1460f2bc013cSdrh assert( TK_OR==OP_Or ); 1461f2bc013cSdrh assert( TK_PLUS==OP_Add ); 1462f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 1463f2bc013cSdrh assert( TK_REM==OP_Remainder ); 1464f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 1465f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 1466f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 1467f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 1468f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 1469f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 14704adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14714adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1472855eb1cfSdrh sqlite3VdbeAddOp(v, op, 0, 0); 14730040077dSdrh break; 14740040077dSdrh } 1475cce7d176Sdrh case TK_UMINUS: { 1476fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1477fec19aadSdrh assert( pLeft ); 1478fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1479fec19aadSdrh Token *p = &pLeft->token; 14806e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 14816e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1482fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1483fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1484e6840900Sdrh }else{ 1485fec19aadSdrh codeInteger(v, z, p->n+1); 1486e6840900Sdrh } 14876e142f54Sdrh sqliteFree(z); 14886e142f54Sdrh break; 14896e142f54Sdrh } 14901ccde15dSdrh /* Fall through into TK_NOT */ 14916e142f54Sdrh } 1492bf4133cbSdrh case TK_BITNOT: 14936e142f54Sdrh case TK_NOT: { 1494f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 1495f2bc013cSdrh assert( TK_NOT==OP_Not ); 14964adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14974adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1498cce7d176Sdrh break; 1499cce7d176Sdrh } 1500cce7d176Sdrh case TK_ISNULL: 1501cce7d176Sdrh case TK_NOTNULL: { 1502cce7d176Sdrh int dest; 1503f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1504f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 15054adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 15064adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15074adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 15084adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 15094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1510a37cdde0Sdanielk1977 break; 1511f2bc013cSdrh } 15122282792aSdrh case TK_AGG_FUNCTION: { 15134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 15142282792aSdrh break; 15152282792aSdrh } 1516b71090fdSdrh case TK_CONST_FUNC: 1517cce7d176Sdrh case TK_FUNCTION: { 1518cce7d176Sdrh ExprList *pList = pExpr->pList; 151989425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 15200bce8354Sdrh FuncDef *pDef; 15214b59ab5eSdrh int nId; 15224b59ab5eSdrh const char *zId; 1523682f68b0Sdanielk1977 int p2 = 0; 1524682f68b0Sdanielk1977 int i; 1525d8123366Sdanielk1977 u8 enc = pParse->db->enc; 1526dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 1527b71090fdSdrh zId = pExpr->token.z; 1528b71090fdSdrh nId = pExpr->token.n; 1529d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 15300bce8354Sdrh assert( pDef!=0 ); 1531f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1532682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 1533d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 1534d02eb1fdSdanielk1977 p2 |= (1<<i); 1535d02eb1fdSdanielk1977 } 1536dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 1537dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 1538dc1bdc4fSdanielk1977 } 1539dc1bdc4fSdanielk1977 } 1540dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 1541dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 1542d8123366Sdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 1543682f68b0Sdanielk1977 } 1544682f68b0Sdanielk1977 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF); 15456ec2733bSdrh break; 15466ec2733bSdrh } 1547fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1548fe2093d7Sdrh case TK_EXISTS: 154919a775c2Sdrh case TK_SELECT: { 1550b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 15514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1552ad6d9460Sdrh VdbeComment((v, "# load subquery result")); 155319a775c2Sdrh break; 155419a775c2Sdrh } 1555fef5208cSdrh case TK_IN: { 1556fef5208cSdrh int addr; 155794a11211Sdrh char affinity; 1558b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 1559e014a838Sdanielk1977 1560e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1561e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1562ededfd5eSdanielk1977 ** P3 of OP_MakeRecord. 1563e014a838Sdanielk1977 */ 156494a11211Sdrh affinity = comparisonAffinity(pExpr); 1565e014a838Sdanielk1977 15664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1567e014a838Sdanielk1977 1568e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1569e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1570e014a838Sdanielk1977 */ 15714adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15724adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1573e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 15744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 1575f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1576e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 157794a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ 1578e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1579e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1580e014a838Sdanielk1977 1581fef5208cSdrh break; 1582fef5208cSdrh } 158393758c8dSdanielk1977 #endif 1584fef5208cSdrh case TK_BETWEEN: { 1585be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1586be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 1587be5c89acSdrh Expr *pRight = pLItem->pExpr; 1588be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 15894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1590be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1591be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 15924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1593be5c89acSdrh pLItem++; 1594be5c89acSdrh pRight = pLItem->pExpr; 1595be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1596be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 15974adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1598fef5208cSdrh break; 1599fef5208cSdrh } 160051e9a445Sdrh case TK_UPLUS: 1601a2e00042Sdrh case TK_AS: { 16024adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1603a2e00042Sdrh break; 1604a2e00042Sdrh } 160517a7f8ddSdrh case TK_CASE: { 160617a7f8ddSdrh int expr_end_label; 1607f5905aa7Sdrh int jumpInst; 1608f5905aa7Sdrh int addr; 1609f5905aa7Sdrh int nExpr; 161017a7f8ddSdrh int i; 1611be5c89acSdrh ExprList *pEList; 1612be5c89acSdrh struct ExprList_item *aListelem; 161317a7f8ddSdrh 161417a7f8ddSdrh assert(pExpr->pList); 161517a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 161617a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1617be5c89acSdrh pEList = pExpr->pList; 1618be5c89acSdrh aListelem = pEList->a; 1619be5c89acSdrh nExpr = pEList->nExpr; 16204adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 162117a7f8ddSdrh if( pExpr->pLeft ){ 16224adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1623cce7d176Sdrh } 1624f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 1625be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr); 162617a7f8ddSdrh if( pExpr->pLeft ){ 16274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1628be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 1629be5c89acSdrh OP_Ne, 0, 1); 16304adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1631f5905aa7Sdrh }else{ 16324adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 163317a7f8ddSdrh } 1634be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr); 16354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 16364adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 16374adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 163817a7f8ddSdrh } 1639f570f011Sdrh if( pExpr->pLeft ){ 16404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1641f570f011Sdrh } 164217a7f8ddSdrh if( pExpr->pRight ){ 16434adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 164417a7f8ddSdrh }else{ 1645f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 164617a7f8ddSdrh } 16474adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 16486f34903eSdanielk1977 break; 16496f34903eSdanielk1977 } 16505338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 16516f34903eSdanielk1977 case TK_RAISE: { 16526f34903eSdanielk1977 if( !pParse->trigStack ){ 16534adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1654da93d238Sdrh "RAISE() may only be used within a trigger-program"); 16556f34903eSdanielk1977 return; 16566f34903eSdanielk1977 } 1657ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 1658ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 16596f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 1660ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 16614adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1662701a0aebSdrh pExpr->token.z, pExpr->token.n); 16634adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 16646f34903eSdanielk1977 } else { 16656f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 1666344737f6Sdrh sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 1667ad6d9460Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 1668ad6d9460Sdrh VdbeComment((v, "# raise(IGNORE)")); 16696f34903eSdanielk1977 } 167017a7f8ddSdrh } 16715338a5f7Sdanielk1977 #endif 167217a7f8ddSdrh break; 167317a7f8ddSdrh } 1674cce7d176Sdrh } 1675cce7d176Sdrh 167693758c8dSdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 1677cce7d176Sdrh /* 167825303780Sdrh ** Generate code that evalutes the given expression and leaves the result 167925303780Sdrh ** on the stack. See also sqlite3ExprCode(). 168025303780Sdrh ** 168125303780Sdrh ** This routine might also cache the result and modify the pExpr tree 168225303780Sdrh ** so that it will make use of the cached result on subsequent evaluations 168325303780Sdrh ** rather than evaluate the whole expression again. Trivial expressions are 168425303780Sdrh ** not cached. If the expression is cached, its result is stored in a 168525303780Sdrh ** memory location. 168625303780Sdrh */ 168725303780Sdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ 168825303780Sdrh Vdbe *v = pParse->pVdbe; 168925303780Sdrh int iMem; 169025303780Sdrh int addr1, addr2; 169125303780Sdrh if( v==0 ) return; 169225303780Sdrh addr1 = sqlite3VdbeCurrentAddr(v); 169325303780Sdrh sqlite3ExprCode(pParse, pExpr); 169425303780Sdrh addr2 = sqlite3VdbeCurrentAddr(v); 169525303780Sdrh if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){ 169625303780Sdrh iMem = pExpr->iTable = pParse->nMem++; 169725303780Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); 169825303780Sdrh pExpr->op = TK_REGISTER; 169925303780Sdrh } 170025303780Sdrh } 170193758c8dSdanielk1977 #endif 170225303780Sdrh 170325303780Sdrh /* 1704268380caSdrh ** Generate code that pushes the value of every element of the given 1705f9b596ebSdrh ** expression list onto the stack. 1706268380caSdrh ** 1707268380caSdrh ** Return the number of elements pushed onto the stack. 1708268380caSdrh */ 17094adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1710268380caSdrh Parse *pParse, /* Parsing context */ 1711f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1712268380caSdrh ){ 1713268380caSdrh struct ExprList_item *pItem; 1714268380caSdrh int i, n; 1715268380caSdrh Vdbe *v; 1716268380caSdrh if( pList==0 ) return 0; 17174adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1718268380caSdrh n = pList->nExpr; 1719268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 17204adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1721268380caSdrh } 1722f9b596ebSdrh return n; 1723268380caSdrh } 1724268380caSdrh 1725268380caSdrh /* 1726cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1727cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1728cce7d176Sdrh ** continues straight thru if the expression is false. 1729f5905aa7Sdrh ** 1730f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1731f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1732f2bc013cSdrh ** 1733f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1734f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1735f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1736f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1737f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1738cce7d176Sdrh */ 17394adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1740cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1741cce7d176Sdrh int op = 0; 1742daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1743f2bc013cSdrh op = pExpr->op; 1744f2bc013cSdrh switch( op ){ 1745cce7d176Sdrh case TK_AND: { 17464adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 17474adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 17484adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 17494adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1750cce7d176Sdrh break; 1751cce7d176Sdrh } 1752cce7d176Sdrh case TK_OR: { 17534adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 17544adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1755cce7d176Sdrh break; 1756cce7d176Sdrh } 1757cce7d176Sdrh case TK_NOT: { 17584adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1759cce7d176Sdrh break; 1760cce7d176Sdrh } 1761cce7d176Sdrh case TK_LT: 1762cce7d176Sdrh case TK_LE: 1763cce7d176Sdrh case TK_GT: 1764cce7d176Sdrh case TK_GE: 1765cce7d176Sdrh case TK_NE: 17660ac65892Sdrh case TK_EQ: { 1767f2bc013cSdrh assert( TK_LT==OP_Lt ); 1768f2bc013cSdrh assert( TK_LE==OP_Le ); 1769f2bc013cSdrh assert( TK_GT==OP_Gt ); 1770f2bc013cSdrh assert( TK_GE==OP_Ge ); 1771f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1772f2bc013cSdrh assert( TK_NE==OP_Ne ); 17734adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 17744adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1775be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1776cce7d176Sdrh break; 1777cce7d176Sdrh } 1778cce7d176Sdrh case TK_ISNULL: 1779cce7d176Sdrh case TK_NOTNULL: { 1780f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1781f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 17824adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 17834adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1784cce7d176Sdrh break; 1785cce7d176Sdrh } 1786fef5208cSdrh case TK_BETWEEN: { 17870202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 17880202b29eSdanielk1977 ** 17890202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 17900202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 17910202b29eSdanielk1977 ** 3 ... 17920202b29eSdanielk1977 */ 1793f5905aa7Sdrh int addr; 1794be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1795be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1796be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 17974adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1798be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1799be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 18000202b29eSdanielk1977 1801be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1802be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1803be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 18040202b29eSdanielk1977 18054adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 18064adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 18074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1808fef5208cSdrh break; 1809fef5208cSdrh } 1810cce7d176Sdrh default: { 18114adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 18124adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1813cce7d176Sdrh break; 1814cce7d176Sdrh } 1815cce7d176Sdrh } 1816cce7d176Sdrh } 1817cce7d176Sdrh 1818cce7d176Sdrh /* 181966b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1820cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1821cce7d176Sdrh ** continues straight thru if the expression is true. 1822f5905aa7Sdrh ** 1823f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1824f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1825cce7d176Sdrh */ 18264adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1827cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1828cce7d176Sdrh int op = 0; 1829daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1830f2bc013cSdrh 1831f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 1832f2bc013cSdrh ** 1833f2bc013cSdrh ** pExpr->op op 1834f2bc013cSdrh ** --------- ---------- 1835f2bc013cSdrh ** TK_ISNULL OP_NotNull 1836f2bc013cSdrh ** TK_NOTNULL OP_IsNull 1837f2bc013cSdrh ** TK_NE OP_Eq 1838f2bc013cSdrh ** TK_EQ OP_Ne 1839f2bc013cSdrh ** TK_GT OP_Le 1840f2bc013cSdrh ** TK_LE OP_Gt 1841f2bc013cSdrh ** TK_GE OP_Lt 1842f2bc013cSdrh ** TK_LT OP_Ge 1843f2bc013cSdrh ** 1844f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 1845f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 1846f2bc013cSdrh ** can compute the mapping above using the following expression. 1847f2bc013cSdrh ** Assert()s verify that the computation is correct. 1848f2bc013cSdrh */ 1849f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 1850f2bc013cSdrh 1851f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 1852f2bc013cSdrh */ 1853f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 1854f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 1855f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 1856f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 1857f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 1858f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 1859f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 1860f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 1861f2bc013cSdrh 1862cce7d176Sdrh switch( pExpr->op ){ 1863cce7d176Sdrh case TK_AND: { 18644adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 18654adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1866cce7d176Sdrh break; 1867cce7d176Sdrh } 1868cce7d176Sdrh case TK_OR: { 18694adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 18704adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 18714adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 18724adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1873cce7d176Sdrh break; 1874cce7d176Sdrh } 1875cce7d176Sdrh case TK_NOT: { 18764adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1877cce7d176Sdrh break; 1878cce7d176Sdrh } 1879cce7d176Sdrh case TK_LT: 1880cce7d176Sdrh case TK_LE: 1881cce7d176Sdrh case TK_GT: 1882cce7d176Sdrh case TK_GE: 1883cce7d176Sdrh case TK_NE: 1884cce7d176Sdrh case TK_EQ: { 18854adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18864adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1887be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1888cce7d176Sdrh break; 1889cce7d176Sdrh } 1890cce7d176Sdrh case TK_ISNULL: 1891cce7d176Sdrh case TK_NOTNULL: { 18924adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18934adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1894cce7d176Sdrh break; 1895cce7d176Sdrh } 1896fef5208cSdrh case TK_BETWEEN: { 18970202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 18980202b29eSdanielk1977 ** 18990202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 19000202b29eSdanielk1977 ** 2 GOTO <dest> 19010202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 19020202b29eSdanielk1977 */ 1903fef5208cSdrh int addr; 1904be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1905be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1906be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 19074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1908be5c89acSdrh sqlite3ExprCode(pParse, pRight); 19094adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1910be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 1911be5c89acSdrh 19124adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 19134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 1914be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1915be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1916be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 1917fef5208cSdrh break; 1918fef5208cSdrh } 1919cce7d176Sdrh default: { 19204adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 19214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1922cce7d176Sdrh break; 1923cce7d176Sdrh } 1924cce7d176Sdrh } 1925cce7d176Sdrh } 19262282792aSdrh 19272282792aSdrh /* 19282282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 19292282792aSdrh ** if they are identical and return FALSE if they differ in any way. 19302282792aSdrh */ 19314adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 19322282792aSdrh int i; 19332282792aSdrh if( pA==0 ){ 19342282792aSdrh return pB==0; 19352282792aSdrh }else if( pB==0 ){ 19362282792aSdrh return 0; 19372282792aSdrh } 19382282792aSdrh if( pA->op!=pB->op ) return 0; 19394adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 19404adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 19412282792aSdrh if( pA->pList ){ 19422282792aSdrh if( pB->pList==0 ) return 0; 19432282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 19442282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 19454adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 19462282792aSdrh return 0; 19472282792aSdrh } 19482282792aSdrh } 19492282792aSdrh }else if( pB->pList ){ 19502282792aSdrh return 0; 19512282792aSdrh } 19522282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 19532f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 19542282792aSdrh if( pA->token.z ){ 19552282792aSdrh if( pB->token.z==0 ) return 0; 19566977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 19574adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 19582282792aSdrh } 19592282792aSdrh return 1; 19602282792aSdrh } 19612282792aSdrh 19622282792aSdrh /* 19632282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 196473b211abSdrh ** The new element is initialized to zero. The calling function is 196573b211abSdrh ** expected to fill it in. 19662282792aSdrh */ 19672282792aSdrh static int appendAggInfo(Parse *pParse){ 19682282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 19692282792aSdrh int amt = pParse->nAgg + 8; 19706d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 19716d4abfbeSdrh if( aAgg==0 ){ 19722282792aSdrh return -1; 19732282792aSdrh } 19746d4abfbeSdrh pParse->aAgg = aAgg; 19752282792aSdrh } 19762282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 19772282792aSdrh return pParse->nAgg++; 19782282792aSdrh } 19792282792aSdrh 19802282792aSdrh /* 1981626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 1982626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 1983626a879aSdrh ** for additional information. 19842282792aSdrh ** 1985626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 19862282792aSdrh */ 1987626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 19882282792aSdrh int i; 19892282792aSdrh AggExpr *aAgg; 1990a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 1991a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 1992a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 19932282792aSdrh 19942282792aSdrh switch( pExpr->op ){ 1995967e8b73Sdrh case TK_COLUMN: { 1996a58fdfb1Sdanielk1977 for(i=0; pSrcList && i<pSrcList->nSrc; i++){ 1997a58fdfb1Sdanielk1977 if( pExpr->iTable==pSrcList->a[i].iCursor ){ 19982282792aSdrh aAgg = pParse->aAgg; 19992282792aSdrh for(i=0; i<pParse->nAgg; i++){ 20002282792aSdrh if( aAgg[i].isAgg ) continue; 20012282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 2002967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 20032282792aSdrh break; 20042282792aSdrh } 20052282792aSdrh } 20062282792aSdrh if( i>=pParse->nAgg ){ 20072282792aSdrh i = appendAggInfo(pParse); 20082282792aSdrh if( i<0 ) return 1; 20092282792aSdrh pParse->aAgg[i].isAgg = 0; 20102282792aSdrh pParse->aAgg[i].pExpr = pExpr; 20112282792aSdrh } 2012aaf88729Sdrh pExpr->iAgg = i; 2013a58fdfb1Sdanielk1977 pExpr->iAggCtx = pNC->nDepth; 2014a58fdfb1Sdanielk1977 return 1; 2015a58fdfb1Sdanielk1977 } 2016a58fdfb1Sdanielk1977 } 2017626a879aSdrh return 1; 20182282792aSdrh } 20192282792aSdrh case TK_AGG_FUNCTION: { 2020a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 20212282792aSdrh aAgg = pParse->aAgg; 20222282792aSdrh for(i=0; i<pParse->nAgg; i++){ 20232282792aSdrh if( !aAgg[i].isAgg ) continue; 20244adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 20252282792aSdrh break; 20262282792aSdrh } 20272282792aSdrh } 20282282792aSdrh if( i>=pParse->nAgg ){ 2029d8123366Sdanielk1977 u8 enc = pParse->db->enc; 20302282792aSdrh i = appendAggInfo(pParse); 20312282792aSdrh if( i<0 ) return 1; 20322282792aSdrh pParse->aAgg[i].isAgg = 1; 20332282792aSdrh pParse->aAgg[i].pExpr = pExpr; 20344adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 20356977fea8Sdrh pExpr->token.z, pExpr->token.n, 2036d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 20372282792aSdrh } 20382282792aSdrh pExpr->iAgg = i; 2039626a879aSdrh return 1; 20402282792aSdrh } 20412282792aSdrh } 2042a58fdfb1Sdanielk1977 } 2043a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 2044a58fdfb1Sdanielk1977 pNC->nDepth++; 2045a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 2046a58fdfb1Sdanielk1977 pNC->nDepth--; 2047a58fdfb1Sdanielk1977 } 2048626a879aSdrh return 0; 20492282792aSdrh } 2050626a879aSdrh 2051626a879aSdrh /* 2052626a879aSdrh ** Analyze the given expression looking for aggregate functions and 2053626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 2054626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 2055626a879aSdrh ** 2056626a879aSdrh ** This routine should only be called after the expression has been 2057626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 2058626a879aSdrh ** 2059626a879aSdrh ** If errors are seen, leave an error message in zErrMsg and return 2060626a879aSdrh ** the number of errors. 2061626a879aSdrh */ 2062a58fdfb1Sdanielk1977 int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 2063a58fdfb1Sdanielk1977 int nErr = pNC->pParse->nErr; 2064a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 2065a58fdfb1Sdanielk1977 return pNC->pParse->nErr - nErr; 20662282792aSdrh } 2067