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*8a9f38feSdrh ** $Id: expr.c,v 1.236 2005/11/05 15:07:56 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20e014a838Sdanielk1977 /* 21e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any. 22e014a838Sdanielk1977 ** 23e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias, 24e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the 25e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned, 26e014a838Sdanielk1977 ** indicating no affinity for the expression. 27e014a838Sdanielk1977 ** 28e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all 29e014a838Sdanielk1977 ** have an affinity: 30e014a838Sdanielk1977 ** 31e014a838Sdanielk1977 ** CREATE TABLE t1(a); 32e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a; 33e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b; 34e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1); 35e014a838Sdanielk1977 */ 36bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){ 37487e262fSdrh int op = pExpr->op; 38487e262fSdrh if( op==TK_AS ){ 39bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pLeft); 40a37cdde0Sdanielk1977 } 41487e262fSdrh if( op==TK_SELECT ){ 42bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 43a37cdde0Sdanielk1977 } 44487e262fSdrh #ifndef SQLITE_OMIT_CAST 45487e262fSdrh if( op==TK_CAST ){ 468df447f0Sdrh return sqlite3AffinityType(&pExpr->token, 0); 47487e262fSdrh } 48487e262fSdrh #endif 49a37cdde0Sdanielk1977 return pExpr->affinity; 50a37cdde0Sdanielk1977 } 51a37cdde0Sdanielk1977 5253db1458Sdrh /* 530202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 540202b29eSdanielk1977 ** there is no default collation type, return 0. 550202b29eSdanielk1977 */ 567cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 577cedc8d4Sdanielk1977 CollSeq *pColl = 0; 580202b29eSdanielk1977 if( pExpr ){ 597cedc8d4Sdanielk1977 pColl = pExpr->pColl; 60487e262fSdrh if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){ 617cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 620202b29eSdanielk1977 } 630202b29eSdanielk1977 } 647cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 657cedc8d4Sdanielk1977 pColl = 0; 667cedc8d4Sdanielk1977 } 677cedc8d4Sdanielk1977 return pColl; 680202b29eSdanielk1977 } 690202b29eSdanielk1977 700202b29eSdanielk1977 /* 71626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 72626a879aSdrh ** type affinity of the other operand. This routine returns the 7353db1458Sdrh ** type affinity that should be used for the comparison operator. 7453db1458Sdrh */ 75e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 76bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 77e014a838Sdanielk1977 if( aff1 && aff2 ){ 788df447f0Sdrh /* Both sides of the comparison are columns. If one has numeric 798df447f0Sdrh ** affinity, use that. Otherwise use no affinity. 80e014a838Sdanielk1977 */ 818df447f0Sdrh if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 82e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 83e014a838Sdanielk1977 }else{ 84e014a838Sdanielk1977 return SQLITE_AFF_NONE; 85e014a838Sdanielk1977 } 86e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 875f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 885f6a87b3Sdrh ** results directly. 89e014a838Sdanielk1977 */ 905f6a87b3Sdrh return SQLITE_AFF_NONE; 91e014a838Sdanielk1977 }else{ 92e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 93fe05af87Sdrh assert( aff1==0 || aff2==0 ); 94e014a838Sdanielk1977 return (aff1 + aff2); 95e014a838Sdanielk1977 } 96e014a838Sdanielk1977 } 97e014a838Sdanielk1977 9853db1458Sdrh /* 9953db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 10053db1458Sdrh ** be applied to both operands prior to doing the comparison. 10153db1458Sdrh */ 102e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 103e014a838Sdanielk1977 char aff; 104e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 105e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 106e014a838Sdanielk1977 pExpr->op==TK_NE ); 107e014a838Sdanielk1977 assert( pExpr->pLeft ); 108bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 109e014a838Sdanielk1977 if( pExpr->pRight ){ 110e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 111e014a838Sdanielk1977 } 112e014a838Sdanielk1977 else if( pExpr->pSelect ){ 113e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 114e014a838Sdanielk1977 } 115e014a838Sdanielk1977 else if( !aff ){ 116e014a838Sdanielk1977 aff = SQLITE_AFF_NUMERIC; 117e014a838Sdanielk1977 } 118e014a838Sdanielk1977 return aff; 119e014a838Sdanielk1977 } 120e014a838Sdanielk1977 121e014a838Sdanielk1977 /* 122e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 123e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 124e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 125e014a838Sdanielk1977 ** the comparison in pExpr. 126e014a838Sdanielk1977 */ 127e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 128e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 1298df447f0Sdrh return (aff==SQLITE_AFF_NONE) || (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 210288d37f1Sdrh ** on the stack. "#0" means the top of the stack. 211288d37f1Sdrh ** "#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( pParse->nested==0 ){ 2234e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 2244e0cff60Sdrh return 0; 2254e0cff60Sdrh } 226bb7ac00bSdrh if( v==0 ) return 0; 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; 35153f733c7Sdrh sqlite3ReallocOrFree((void**)&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 376d2687b77Sdrh /* 377d2687b77Sdrh ** The Expr.token field might be a string literal that is quoted. 378d2687b77Sdrh ** If so, remove the quotation marks. 379d2687b77Sdrh */ 380d2687b77Sdrh void sqlite3DequoteExpr(Expr *p){ 381d2687b77Sdrh if( ExprHasAnyProperty(p, EP_Dequoted) ){ 382d2687b77Sdrh return; 383d2687b77Sdrh } 384d2687b77Sdrh ExprSetProperty(p, EP_Dequoted); 385d2687b77Sdrh if( p->token.dyn==0 ){ 386d2687b77Sdrh sqlite3TokenCopy(&p->token, &p->token); 387d2687b77Sdrh } 388d2687b77Sdrh sqlite3Dequote((char*)p->token.z); 389d2687b77Sdrh } 390d2687b77Sdrh 391a76b5dfcSdrh 392a76b5dfcSdrh /* 393ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 394ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 395ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 396ff78bd2fSdrh ** without effecting the originals. 397ff78bd2fSdrh ** 3984adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 3994adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 400ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 401ff78bd2fSdrh ** 402ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 403ff78bd2fSdrh */ 4044adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 405ff78bd2fSdrh Expr *pNew; 406ff78bd2fSdrh if( p==0 ) return 0; 407fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 408ff78bd2fSdrh if( pNew==0 ) return 0; 4093b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 4106977fea8Sdrh if( p->token.z!=0 ){ 411b9ecf6faSdrh pNew->token.z = sqliteStrNDup(p->token.z, p->token.n); 4124b59ab5eSdrh pNew->token.dyn = 1; 4134b59ab5eSdrh }else{ 4144efc4754Sdrh assert( pNew->token.z==0 ); 4154b59ab5eSdrh } 4166977fea8Sdrh pNew->span.z = 0; 4174adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 4184adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 4194adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 4204adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 421aee18ef8Sdanielk1977 pNew->pTab = p->pTab; 422ff78bd2fSdrh return pNew; 423ff78bd2fSdrh } 4244adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 4254b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 4264b59ab5eSdrh if( pFrom->z ){ 4274b59ab5eSdrh pTo->n = pFrom->n; 4284b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 4294b59ab5eSdrh pTo->dyn = 1; 4304b59ab5eSdrh }else{ 4314b59ab5eSdrh pTo->z = 0; 4324b59ab5eSdrh } 4334b59ab5eSdrh } 4344adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 435ff78bd2fSdrh ExprList *pNew; 436145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 437ff78bd2fSdrh int i; 438ff78bd2fSdrh if( p==0 ) return 0; 439ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 440ff78bd2fSdrh if( pNew==0 ) return 0; 4414305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 4423e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 443e0048400Sdanielk1977 if( pItem==0 ){ 444e0048400Sdanielk1977 sqliteFree(pNew); 445e0048400Sdanielk1977 return 0; 446e0048400Sdanielk1977 } 447145716b3Sdrh pOldItem = p->a; 448145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 4494b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 450145716b3Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); 4516977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 4526977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 4534b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 4544b59ab5eSdrh ** the names of columns in the result set needs this information */ 4554adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 4564b59ab5eSdrh } 4571f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 45824b03fd0Sdanielk1977 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 459145716b3Sdrh pItem->zName = sqliteStrDup(pOldItem->zName); 460145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 461145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 4623e7bc9caSdrh pItem->done = 0; 463ff78bd2fSdrh } 464ff78bd2fSdrh return pNew; 465ff78bd2fSdrh } 46693758c8dSdanielk1977 46793758c8dSdanielk1977 /* 46893758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 46993758c8dSdanielk1977 ** the build, then none of the following routines, except for 47093758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 47193758c8dSdanielk1977 ** called with a NULL argument. 47293758c8dSdanielk1977 */ 4736a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 4746a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 4754adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 476ad3cab52Sdrh SrcList *pNew; 477ad3cab52Sdrh int i; 478113088ecSdrh int nByte; 479ad3cab52Sdrh if( p==0 ) return 0; 480113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 4814efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 482ad3cab52Sdrh if( pNew==0 ) return 0; 4834305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 484ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 4854efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 4864efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 487ed8a3bb1Sdrh Table *pTab; 4884efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 4894efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 4904efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 4914efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 4924efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 493ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 494ed8a3bb1Sdrh if( pTab ){ 495ed8a3bb1Sdrh pTab->nRef++; 496a1cb183dSdanielk1977 } 4974adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 4984adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 4994adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 5006c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 501ad3cab52Sdrh } 502ad3cab52Sdrh return pNew; 503ad3cab52Sdrh } 5044adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 505ff78bd2fSdrh IdList *pNew; 506ff78bd2fSdrh int i; 507ff78bd2fSdrh if( p==0 ) return 0; 5084efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 509ff78bd2fSdrh if( pNew==0 ) return 0; 5104305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 5114efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 512d5d56523Sdanielk1977 if( pNew->a==0 ){ 513d5d56523Sdanielk1977 sqliteFree(pNew); 514d5d56523Sdanielk1977 return 0; 515d5d56523Sdanielk1977 } 516ff78bd2fSdrh for(i=0; i<p->nId; i++){ 5174efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 5184efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 5194efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 5204efc4754Sdrh pNewItem->idx = pOldItem->idx; 521ff78bd2fSdrh } 522ff78bd2fSdrh return pNew; 523ff78bd2fSdrh } 5244adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 525ff78bd2fSdrh Select *pNew; 526ff78bd2fSdrh if( p==0 ) return 0; 5274efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 528ff78bd2fSdrh if( pNew==0 ) return 0; 529ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 5304adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 5314adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 5324adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 5334adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 5344adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 5354adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 536ff78bd2fSdrh pNew->op = p->op; 5374adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 538a2dc3b1aSdanielk1977 pNew->pLimit = sqlite3ExprDup(p->pLimit); 539a2dc3b1aSdanielk1977 pNew->pOffset = sqlite3ExprDup(p->pOffset); 5407b58daeaSdrh pNew->iLimit = -1; 5417b58daeaSdrh pNew->iOffset = -1; 542a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 543a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 5448e647b81Sdrh pNew->usesVirt = 0; 5458e647b81Sdrh pNew->disallowOrderBy = 0; 5460342b1f5Sdrh pNew->pRightmost = 0; 5470342b1f5Sdrh pNew->addrOpenVirt[0] = -1; 5480342b1f5Sdrh pNew->addrOpenVirt[1] = -1; 5490342b1f5Sdrh pNew->addrOpenVirt[2] = -1; 550ff78bd2fSdrh return pNew; 551ff78bd2fSdrh } 55293758c8dSdanielk1977 #else 55393758c8dSdanielk1977 Select *sqlite3SelectDup(Select *p){ 55493758c8dSdanielk1977 assert( p==0 ); 55593758c8dSdanielk1977 return 0; 55693758c8dSdanielk1977 } 55793758c8dSdanielk1977 #endif 558ff78bd2fSdrh 559ff78bd2fSdrh 560ff78bd2fSdrh /* 561a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 562a76b5dfcSdrh ** initially NULL, then create a new expression list. 563a76b5dfcSdrh */ 5644adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 565a76b5dfcSdrh if( pList==0 ){ 566a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 567a76b5dfcSdrh if( pList==0 ){ 568d5d56523Sdanielk1977 goto no_mem; 569a76b5dfcSdrh } 5704efc4754Sdrh assert( pList->nAlloc==0 ); 571a76b5dfcSdrh } 5724305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 573d5d56523Sdanielk1977 struct ExprList_item *a; 574d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 575d5d56523Sdanielk1977 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); 576d5d56523Sdanielk1977 if( a==0 ){ 577d5d56523Sdanielk1977 goto no_mem; 578a76b5dfcSdrh } 579d5d56523Sdanielk1977 pList->a = a; 580d5d56523Sdanielk1977 pList->nAlloc = n; 581a76b5dfcSdrh } 5824efc4754Sdrh assert( pList->a!=0 ); 5834efc4754Sdrh if( pExpr || pName ){ 5844efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 5854efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 586a99db3b6Sdrh pItem->zName = sqlite3NameFromToken(pName); 587e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 588a76b5dfcSdrh } 589a76b5dfcSdrh return pList; 590d5d56523Sdanielk1977 591d5d56523Sdanielk1977 no_mem: 592d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 593d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 594d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 595d5d56523Sdanielk1977 return 0; 596a76b5dfcSdrh } 597a76b5dfcSdrh 598a76b5dfcSdrh /* 599a76b5dfcSdrh ** Delete an entire expression list. 600a76b5dfcSdrh */ 6014adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 602a76b5dfcSdrh int i; 603be5c89acSdrh struct ExprList_item *pItem; 604a76b5dfcSdrh if( pList==0 ) return; 6051bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 6061bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 607be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 608be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 609be5c89acSdrh sqliteFree(pItem->zName); 610a76b5dfcSdrh } 611a76b5dfcSdrh sqliteFree(pList->a); 612a76b5dfcSdrh sqliteFree(pList); 613a76b5dfcSdrh } 614a76b5dfcSdrh 615a76b5dfcSdrh /* 616626a879aSdrh ** Walk an expression tree. Call xFunc for each node visited. 61773b211abSdrh ** 618626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 619626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 620626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 621626a879aSdrh ** the tree walk completely. 622626a879aSdrh ** 623626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 624626a879aSdrh ** and 0 to continue. 62587abf5c0Sdrh ** 62687abf5c0Sdrh ** NOTICE: This routine does *not* descend into subqueries. 627626a879aSdrh */ 628a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 629626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 630626a879aSdrh int rc; 631626a879aSdrh if( pExpr==0 ) return 0; 632626a879aSdrh rc = (*xFunc)(pArg, pExpr); 633626a879aSdrh if( rc==0 ){ 634626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 635626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 636a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 637626a879aSdrh } 638626a879aSdrh return rc>1; 639626a879aSdrh } 640626a879aSdrh 641626a879aSdrh /* 642a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 643a58fdfb1Sdanielk1977 */ 644a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 645a58fdfb1Sdanielk1977 int i; 646a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 647a58fdfb1Sdanielk1977 if( !p ) return 0; 648a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 649a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 650a58fdfb1Sdanielk1977 } 651a58fdfb1Sdanielk1977 return 0; 652a58fdfb1Sdanielk1977 } 653a58fdfb1Sdanielk1977 654a58fdfb1Sdanielk1977 /* 655a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 656a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 657a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 658a58fdfb1Sdanielk1977 */ 659a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 660a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 661a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 662a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 663a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 664a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 665a58fdfb1Sdanielk1977 return 0; 666a58fdfb1Sdanielk1977 } 667a58fdfb1Sdanielk1977 668a58fdfb1Sdanielk1977 669a58fdfb1Sdanielk1977 /* 670626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 671626a879aSdrh ** 672626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 67373b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 67473b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 67573b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 67673b211abSdrh ** then do nothing. 67773b211abSdrh ** 67873b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 67973b211abSdrh ** the expression as constant, then we assume the whole expression 68073b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 681626a879aSdrh */ 682626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 683626a879aSdrh switch( pExpr->op ){ 684eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 685eb55bd2fSdrh ** and *pArg==2 */ 686eb55bd2fSdrh case TK_FUNCTION: 687eb55bd2fSdrh if( *((int*)pArg)==2 ) return 0; 688eb55bd2fSdrh /* Fall through */ 689626a879aSdrh case TK_ID: 690626a879aSdrh case TK_COLUMN: 691626a879aSdrh case TK_DOT: 692626a879aSdrh case TK_AGG_FUNCTION: 69313449892Sdrh case TK_AGG_COLUMN: 694fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 695fe2093d7Sdrh case TK_SELECT: 696fe2093d7Sdrh case TK_EXISTS: 697fe2093d7Sdrh #endif 698626a879aSdrh *((int*)pArg) = 0; 699626a879aSdrh return 2; 70087abf5c0Sdrh case TK_IN: 70187abf5c0Sdrh if( pExpr->pSelect ){ 70287abf5c0Sdrh *((int*)pArg) = 0; 70387abf5c0Sdrh return 2; 70487abf5c0Sdrh } 705626a879aSdrh default: 706626a879aSdrh return 0; 707626a879aSdrh } 708626a879aSdrh } 709626a879aSdrh 710626a879aSdrh /* 711fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 712eb55bd2fSdrh ** and 0 if it involves variables or function calls. 7132398937bSdrh ** 7142398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 7152398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 7162398937bSdrh ** a constant. 717fef5208cSdrh */ 7184adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 719626a879aSdrh int isConst = 1; 720626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 721626a879aSdrh return isConst; 722fef5208cSdrh } 723fef5208cSdrh 724fef5208cSdrh /* 725eb55bd2fSdrh ** Walk an expression tree. Return 1 if the expression is constant 726eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 727eb55bd2fSdrh ** are any variables. 728eb55bd2fSdrh ** 729eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 730eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 731eb55bd2fSdrh ** a constant. 732eb55bd2fSdrh */ 733eb55bd2fSdrh int sqlite3ExprIsConstantOrFunction(Expr *p){ 734eb55bd2fSdrh int isConst = 2; 735eb55bd2fSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 736eb55bd2fSdrh return isConst!=0; 737eb55bd2fSdrh } 738eb55bd2fSdrh 739eb55bd2fSdrh /* 74073b211abSdrh ** If the expression p codes a constant integer that is small enough 741202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 742202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 743202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 744e4de1febSdrh */ 7454adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 746e4de1febSdrh switch( p->op ){ 747e4de1febSdrh case TK_INTEGER: { 748fec19aadSdrh if( sqlite3GetInt32(p->token.z, pValue) ){ 749e4de1febSdrh return 1; 750e4de1febSdrh } 751202b2df7Sdrh break; 752202b2df7Sdrh } 7534b59ab5eSdrh case TK_UPLUS: { 7544adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 7554b59ab5eSdrh } 756e4de1febSdrh case TK_UMINUS: { 757e4de1febSdrh int v; 7584adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 759e4de1febSdrh *pValue = -v; 760e4de1febSdrh return 1; 761e4de1febSdrh } 762e4de1febSdrh break; 763e4de1febSdrh } 764e4de1febSdrh default: break; 765e4de1febSdrh } 766e4de1febSdrh return 0; 767e4de1febSdrh } 768e4de1febSdrh 769e4de1febSdrh /* 770c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 771c4a3c779Sdrh */ 7724adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 7734adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 7744adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 7754adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 776c4a3c779Sdrh return 0; 777c4a3c779Sdrh } 778c4a3c779Sdrh 779c4a3c779Sdrh /* 7808141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 7818141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 7828141f61eSdrh ** expression node refer back to that source column. The following changes 7838141f61eSdrh ** are made to pExpr: 7848141f61eSdrh ** 7858141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 7868141f61eSdrh ** the table. 7878141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 7888141f61eSdrh ** from pSrcList. 7898141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 7908141f61eSdrh ** pExpr->op Set to TK_COLUMN. 7918141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 7928141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 7938141f61eSdrh ** 7948141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 7958141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 7968141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 7978141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 7988141f61eSdrh ** means that the form of the name is Z and that columns from any table 7998141f61eSdrh ** can be used. 8008141f61eSdrh ** 8018141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 8028141f61eSdrh ** in pParse and return non-zero. Return zero on success. 8038141f61eSdrh */ 8048141f61eSdrh static int lookupName( 8058141f61eSdrh Parse *pParse, /* The parsing context */ 8068141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 8078141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 8088141f61eSdrh Token *pColumnToken, /* Name of the column. */ 809626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 8108141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 8118141f61eSdrh ){ 8128141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 8138141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 8148141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 8158141f61eSdrh int i, j; /* Loop counters */ 8168141f61eSdrh int cnt = 0; /* Number of matching column names */ 8178141f61eSdrh int cntTab = 0; /* Number of matching table names */ 8189bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 81951669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 82051669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 82173b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 8228141f61eSdrh 8238141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 824a99db3b6Sdrh zDb = sqlite3NameFromToken(pDbToken); 825a99db3b6Sdrh zTab = sqlite3NameFromToken(pTableToken); 826a99db3b6Sdrh zCol = sqlite3NameFromToken(pColumnToken); 82724b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 828d5d56523Sdanielk1977 goto lookupname_end; 8298141f61eSdrh } 8308141f61eSdrh 8318141f61eSdrh pExpr->iTable = -1; 832626a879aSdrh while( pNC && cnt==0 ){ 833ffe07b2dSdrh ExprList *pEList; 834626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 835626a879aSdrh 836b3bce662Sdanielk1977 if( pSrcList ){ 83751669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 8388141f61eSdrh Table *pTab = pItem->pTab; 8398141f61eSdrh Column *pCol; 8408141f61eSdrh 8418141f61eSdrh if( pTab==0 ) continue; 8428141f61eSdrh assert( pTab->nCol>0 ); 8438141f61eSdrh if( zTab ){ 8448141f61eSdrh if( pItem->zAlias ){ 8458141f61eSdrh char *zTabName = pItem->zAlias; 8464adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 8478141f61eSdrh }else{ 8488141f61eSdrh char *zTabName = pTab->zName; 8494adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 8504adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 8518141f61eSdrh continue; 8528141f61eSdrh } 8538141f61eSdrh } 8548141f61eSdrh } 8558141f61eSdrh if( 0==(cntTab++) ){ 8568141f61eSdrh pExpr->iTable = pItem->iCursor; 8578141f61eSdrh pExpr->iDb = pTab->iDb; 85851669863Sdrh pMatch = pItem; 8598141f61eSdrh } 8608141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 8614adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 862873fac0cSdrh IdList *pUsing; 8638141f61eSdrh cnt++; 8648141f61eSdrh pExpr->iTable = pItem->iCursor; 86551669863Sdrh pMatch = pItem; 8668141f61eSdrh pExpr->iDb = pTab->iDb; 8678141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 8688141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 869a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 8700202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 871355ef361Sdrh if( pItem->jointype & JT_NATURAL ){ 872355ef361Sdrh /* If this match occurred in the left table of a natural join, 873355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 874355ef361Sdrh pItem++; 875355ef361Sdrh i++; 876355ef361Sdrh } 877873fac0cSdrh if( (pUsing = pItem->pUsing)!=0 ){ 878873fac0cSdrh /* If this match occurs on a column that is in the USING clause 879873fac0cSdrh ** of a join, skip the search of the right table of the join 880873fac0cSdrh ** to avoid a duplicate match there. */ 881873fac0cSdrh int k; 882873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 883873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 884873fac0cSdrh pItem++; 885873fac0cSdrh i++; 886873fac0cSdrh break; 887873fac0cSdrh } 888873fac0cSdrh } 889873fac0cSdrh } 8908141f61eSdrh break; 8918141f61eSdrh } 8928141f61eSdrh } 8938141f61eSdrh } 894b3bce662Sdanielk1977 } 8958141f61eSdrh 896b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 8978141f61eSdrh /* If we have not already resolved the name, then maybe 8988141f61eSdrh ** it is a new.* or old.* trigger argument reference 8998141f61eSdrh */ 9008141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 9018141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 9028141f61eSdrh Table *pTab = 0; 9034adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 9048141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 9058141f61eSdrh assert( pTriggerStack->pTab ); 9068141f61eSdrh pTab = pTriggerStack->pTab; 9074adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 9088141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 9098141f61eSdrh assert( pTriggerStack->pTab ); 9108141f61eSdrh pTab = pTriggerStack->pTab; 9118141f61eSdrh } 9128141f61eSdrh 9138141f61eSdrh if( pTab ){ 9148141f61eSdrh int j; 9158141f61eSdrh Column *pCol = pTab->aCol; 9168141f61eSdrh 9178141f61eSdrh pExpr->iDb = pTab->iDb; 9188141f61eSdrh cntTab++; 9198141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 9204adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 9218141f61eSdrh cnt++; 9228141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 923a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 9240202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 925aee18ef8Sdanielk1977 pExpr->pTab = pTab; 9268141f61eSdrh break; 9278141f61eSdrh } 9288141f61eSdrh } 9298141f61eSdrh } 9308141f61eSdrh } 931b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 9328141f61eSdrh 9338141f61eSdrh /* 9348141f61eSdrh ** Perhaps the name is a reference to the ROWID 9358141f61eSdrh */ 9364adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 9378141f61eSdrh cnt = 1; 9388141f61eSdrh pExpr->iColumn = -1; 9398df447f0Sdrh pExpr->affinity = SQLITE_AFF_NUMERIC; 9408141f61eSdrh } 9418141f61eSdrh 9428141f61eSdrh /* 9438141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 9448141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 9458141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 9468141f61eSdrh ** 9478141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 9488141f61eSdrh ** 9498141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 9508141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 9518141f61eSdrh ** Note that the expression in the result set should have already been 9528141f61eSdrh ** resolved by the time the WHERE clause is resolved. 9538141f61eSdrh */ 954ffe07b2dSdrh if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ 9558141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 9568141f61eSdrh char *zAs = pEList->a[j].zName; 9574adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 9588141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 9598141f61eSdrh pExpr->op = TK_AS; 9608141f61eSdrh pExpr->iColumn = j; 9614adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 96215ccce1cSdrh cnt = 1; 9638141f61eSdrh assert( zTab==0 && zDb==0 ); 96415ccce1cSdrh goto lookupname_end_2; 9658141f61eSdrh } 9668141f61eSdrh } 9678141f61eSdrh } 9688141f61eSdrh 969626a879aSdrh /* Advance to the next name context. The loop will exit when either 970626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 971626a879aSdrh */ 972626a879aSdrh if( cnt==0 ){ 973626a879aSdrh pNC = pNC->pNext; 974626a879aSdrh } 975626a879aSdrh } 976626a879aSdrh 9778141f61eSdrh /* 9788141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 9798141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 9808141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 9818141f61eSdrh ** case, we need to return right away and not make any changes to 9828141f61eSdrh ** pExpr. 98315ccce1cSdrh ** 98415ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 98515ccce1cSdrh ** fields are not changed in any context. 9868141f61eSdrh */ 9878141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 9888141f61eSdrh sqliteFree(zCol); 9898141f61eSdrh return 0; 9908141f61eSdrh } 9918141f61eSdrh 9928141f61eSdrh /* 9938141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 9948141f61eSdrh ** more matches. Either way, we have an error. 9958141f61eSdrh */ 9968141f61eSdrh if( cnt!=1 ){ 9978141f61eSdrh char *z = 0; 9988141f61eSdrh char *zErr; 9998141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 10008141f61eSdrh if( zDb ){ 10014adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 10028141f61eSdrh }else if( zTab ){ 10034adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 10048141f61eSdrh }else{ 10058141f61eSdrh z = sqliteStrDup(zCol); 10068141f61eSdrh } 10074adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 10088141f61eSdrh sqliteFree(z); 100973b211abSdrh pTopNC->nErr++; 10108141f61eSdrh } 10118141f61eSdrh 101251669863Sdrh /* If a column from a table in pSrcList is referenced, then record 101351669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 101451669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 101551669863Sdrh ** column number is greater than the number of bits in the bitmask 101651669863Sdrh ** then set the high-order bit of the bitmask. 101751669863Sdrh */ 101851669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 101951669863Sdrh int n = pExpr->iColumn; 102051669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 102151669863Sdrh n = sizeof(Bitmask)*8-1; 102251669863Sdrh } 102351669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 102451669863Sdrh pMatch->colUsed |= 1<<n; 102551669863Sdrh } 102651669863Sdrh 1027d5d56523Sdanielk1977 lookupname_end: 10288141f61eSdrh /* Clean up and return 10298141f61eSdrh */ 10308141f61eSdrh sqliteFree(zDb); 10318141f61eSdrh sqliteFree(zTab); 10324adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 10338141f61eSdrh pExpr->pLeft = 0; 10344adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 10358141f61eSdrh pExpr->pRight = 0; 10368141f61eSdrh pExpr->op = TK_COLUMN; 103715ccce1cSdrh lookupname_end_2: 103815ccce1cSdrh sqliteFree(zCol); 1039626a879aSdrh if( cnt==1 ){ 1040b3bce662Sdanielk1977 assert( pNC!=0 ); 1041626a879aSdrh sqlite3AuthRead(pParse, pExpr, pNC->pSrcList); 1042aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 1043aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 1044aee18ef8Sdanielk1977 } 104515ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 104615ccce1cSdrh ** the point where the name matched. */ 104715ccce1cSdrh for(;;){ 104815ccce1cSdrh assert( pTopNC!=0 ); 104915ccce1cSdrh pTopNC->nRef++; 105015ccce1cSdrh if( pTopNC==pNC ) break; 105115ccce1cSdrh pTopNC = pTopNC->pNext; 1052626a879aSdrh } 105315ccce1cSdrh return 0; 105415ccce1cSdrh } else { 105515ccce1cSdrh return 1; 105615ccce1cSdrh } 10578141f61eSdrh } 10588141f61eSdrh 10598141f61eSdrh /* 1060626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1061626a879aSdrh ** 106273b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1063626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 106473b211abSdrh ** the tree or 2 to abort the tree walk. 106573b211abSdrh ** 106673b211abSdrh ** This routine also does error checking and name resolution for 106773b211abSdrh ** function names. The operator for aggregate functions is changed 106873b211abSdrh ** to TK_AGG_FUNCTION. 1069626a879aSdrh */ 1070626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1071626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1072626a879aSdrh SrcList *pSrcList; 1073626a879aSdrh Parse *pParse; 1074626a879aSdrh 1075b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1076626a879aSdrh assert( pNC!=0 ); 1077626a879aSdrh pSrcList = pNC->pSrcList; 1078626a879aSdrh pParse = pNC->pParse; 1079b3bce662Sdanielk1977 1080626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1081626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1082626a879aSdrh #ifndef NDEBUG 1083ffe07b2dSdrh if( pSrcList && pSrcList->nAlloc>0 ){ 1084940fac9dSdanielk1977 int i; 1085626a879aSdrh for(i=0; i<pSrcList->nSrc; i++){ 1086626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1087626a879aSdrh } 1088626a879aSdrh } 1089626a879aSdrh #endif 1090626a879aSdrh switch( pExpr->op ){ 1091626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1092626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1093626a879aSdrh ** strings (ex: 'abc') are always string literals. 1094626a879aSdrh */ 1095626a879aSdrh case TK_STRING: { 1096626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1097626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1098626a879aSdrh } 1099626a879aSdrh /* A lone identifier is the name of a column. 1100626a879aSdrh */ 1101626a879aSdrh case TK_ID: { 1102626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1103626a879aSdrh return 1; 1104626a879aSdrh } 1105626a879aSdrh 1106626a879aSdrh /* A table name and column name: ID.ID 1107626a879aSdrh ** Or a database, table and column: ID.ID.ID 1108626a879aSdrh */ 1109626a879aSdrh case TK_DOT: { 1110626a879aSdrh Token *pColumn; 1111626a879aSdrh Token *pTable; 1112626a879aSdrh Token *pDb; 1113626a879aSdrh Expr *pRight; 1114626a879aSdrh 1115b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1116626a879aSdrh pRight = pExpr->pRight; 1117626a879aSdrh if( pRight->op==TK_ID ){ 1118626a879aSdrh pDb = 0; 1119626a879aSdrh pTable = &pExpr->pLeft->token; 1120626a879aSdrh pColumn = &pRight->token; 1121626a879aSdrh }else{ 1122626a879aSdrh assert( pRight->op==TK_DOT ); 1123626a879aSdrh pDb = &pExpr->pLeft->token; 1124626a879aSdrh pTable = &pRight->pLeft->token; 1125626a879aSdrh pColumn = &pRight->pRight->token; 1126626a879aSdrh } 1127626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1128626a879aSdrh return 1; 1129626a879aSdrh } 1130626a879aSdrh 1131626a879aSdrh /* Resolve function names 1132626a879aSdrh */ 1133b71090fdSdrh case TK_CONST_FUNC: 1134626a879aSdrh case TK_FUNCTION: { 1135626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1136626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1137626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1138626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1139626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1140626a879aSdrh int i; 1141626a879aSdrh int nId; /* Number of characters in function name */ 1142626a879aSdrh const char *zId; /* The function name. */ 114373b211abSdrh FuncDef *pDef; /* Information about the function */ 114473b211abSdrh int enc = pParse->db->enc; /* The database encoding */ 1145626a879aSdrh 1146b71090fdSdrh zId = pExpr->token.z; 1147b71090fdSdrh nId = pExpr->token.n; 1148626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1149626a879aSdrh if( pDef==0 ){ 1150626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1151626a879aSdrh if( pDef==0 ){ 1152626a879aSdrh no_such_func = 1; 1153626a879aSdrh }else{ 1154626a879aSdrh wrong_num_args = 1; 1155626a879aSdrh } 1156626a879aSdrh }else{ 1157626a879aSdrh is_agg = pDef->xFunc==0; 1158626a879aSdrh } 1159626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1160626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1161626a879aSdrh pNC->nErr++; 1162626a879aSdrh is_agg = 0; 1163626a879aSdrh }else if( no_such_func ){ 1164626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1165626a879aSdrh pNC->nErr++; 1166626a879aSdrh }else if( wrong_num_args ){ 1167626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1168626a879aSdrh nId, zId); 1169626a879aSdrh pNC->nErr++; 1170626a879aSdrh } 1171626a879aSdrh if( is_agg ){ 1172626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1173626a879aSdrh pNC->hasAgg = 1; 1174626a879aSdrh } 117573b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1176626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 117773b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1178626a879aSdrh } 117973b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1180626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1181626a879aSdrh ** type of the function 1182626a879aSdrh */ 1183626a879aSdrh return is_agg; 1184626a879aSdrh } 1185b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1186b3bce662Sdanielk1977 case TK_SELECT: 1187b3bce662Sdanielk1977 case TK_EXISTS: 1188b3bce662Sdanielk1977 #endif 1189b3bce662Sdanielk1977 case TK_IN: { 1190b3bce662Sdanielk1977 if( pExpr->pSelect ){ 1191*8a9f38feSdrh int nRef = pNC->nRef; 119206f6541eSdrh #ifndef SQLITE_OMIT_CHECK 119306f6541eSdrh if( pNC->isCheck ){ 119406f6541eSdrh sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); 119506f6541eSdrh } 119606f6541eSdrh #endif 1197b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1198b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1199b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1200b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1201b3bce662Sdanielk1977 } 1202b3bce662Sdanielk1977 } 12034284fb07Sdrh break; 1204b3bce662Sdanielk1977 } 12054284fb07Sdrh #ifndef SQLITE_OMIT_CHECK 12064284fb07Sdrh case TK_VARIABLE: { 12074284fb07Sdrh if( pNC->isCheck ){ 12084284fb07Sdrh sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); 12094284fb07Sdrh } 12104284fb07Sdrh break; 12114284fb07Sdrh } 12124284fb07Sdrh #endif 1213626a879aSdrh } 1214626a879aSdrh return 0; 1215626a879aSdrh } 1216626a879aSdrh 1217626a879aSdrh /* 1218cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1219967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1220aacc543eSdrh ** index to the table in the table list and a column offset. The 1221aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1222aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1223832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1224aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1225aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1226aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1227aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1228aacc543eSdrh ** alias for ROWID. 122919a775c2Sdrh ** 1230626a879aSdrh ** Also resolve function names and check the functions for proper 1231626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1232626a879aSdrh ** have the correct number of arguments. Leave an error message 1233626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1234626a879aSdrh ** 123573b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 123673b211abSdrh ** property on the expression. 1237626a879aSdrh */ 1238626a879aSdrh int sqlite3ExprResolveNames( 1239b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1240b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1241626a879aSdrh ){ 124213449892Sdrh int savedHasAgg; 124373b211abSdrh if( pExpr==0 ) return 0; 124413449892Sdrh savedHasAgg = pNC->hasAgg; 124513449892Sdrh pNC->hasAgg = 0; 1246b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1247b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 124873b211abSdrh ExprSetProperty(pExpr, EP_Error); 124973b211abSdrh } 125013449892Sdrh if( pNC->hasAgg ){ 125113449892Sdrh ExprSetProperty(pExpr, EP_Agg); 125213449892Sdrh }else if( savedHasAgg ){ 125313449892Sdrh pNC->hasAgg = 1; 125413449892Sdrh } 125573b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1256626a879aSdrh } 1257626a879aSdrh 12581398ad36Sdrh /* 12591398ad36Sdrh ** A pointer instance of this structure is used to pass information 12601398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 12611398ad36Sdrh */ 12621398ad36Sdrh typedef struct QueryCoder QueryCoder; 12631398ad36Sdrh struct QueryCoder { 12641398ad36Sdrh Parse *pParse; /* The parsing context */ 12651398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 12661398ad36Sdrh }; 12671398ad36Sdrh 1268626a879aSdrh 1269626a879aSdrh /* 1270626a879aSdrh ** Generate code for subqueries and IN operators. 1271626a879aSdrh ** 127273b211abSdrh ** IN operators comes in two forms: 1273fef5208cSdrh ** 1274fef5208cSdrh ** expr IN (exprlist) 1275fef5208cSdrh ** and 1276fef5208cSdrh ** expr IN (SELECT ...) 1277fef5208cSdrh ** 1278fef5208cSdrh ** The first form is handled by creating a set holding the list 1279fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 1280fef5208cSdrh ** a temporary table. 1281cce7d176Sdrh */ 128251522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1283b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 128457dbd7b3Sdrh int testAddr = 0; /* One-time test address */ 1285b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1286b3bce662Sdanielk1977 if( v==0 ) return; 1287b3bce662Sdanielk1977 128857dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 128957dbd7b3Sdrh ** if any of the following is true: 129057dbd7b3Sdrh ** 129157dbd7b3Sdrh ** * The right-hand side is a correlated subquery 129257dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 129357dbd7b3Sdrh ** * We are inside a trigger 129457dbd7b3Sdrh ** 129557dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 129657dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1297b3bce662Sdanielk1977 */ 1298b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 1299b3bce662Sdanielk1977 int mem = pParse->nMem++; 1300b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); 130157dbd7b3Sdrh testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0); 130253f733c7Sdrh assert( testAddr>0 || sqlite3_malloc_failed ); 1303d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, mem); 1304b3bce662Sdanielk1977 } 1305b3bce662Sdanielk1977 1306cce7d176Sdrh switch( pExpr->op ){ 1307fef5208cSdrh case TK_IN: { 1308e014a838Sdanielk1977 char affinity; 1309d3d39e93Sdrh KeyInfo keyInfo; 13109170dd7eSdrh int addr; /* Address of OP_OpenVirtual instruction */ 1311d3d39e93Sdrh 1312bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1313e014a838Sdanielk1977 1314e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 131557dbd7b3Sdrh ** expression it is handled the same way. A virtual table is 1316e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1317e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1318fef5208cSdrh ** 1319e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1320e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1321e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1322e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1323e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1324e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1325e014a838Sdanielk1977 ** is used. 1326fef5208cSdrh */ 1327832508b7Sdrh pExpr->iTable = pParse->nTab++; 13289170dd7eSdrh addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0); 1329d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1330d3d39e93Sdrh keyInfo.nField = 1; 1331f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 1332e014a838Sdanielk1977 1333e014a838Sdanielk1977 if( pExpr->pSelect ){ 1334e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1335e014a838Sdanielk1977 ** 1336e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1337e014a838Sdanielk1977 ** table allocated and opened above. 1338e014a838Sdanielk1977 */ 1339e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 1340be5c89acSdrh ExprList *pEList; 1341e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 1342b3bce662Sdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 1343be5c89acSdrh pEList = pExpr->pSelect->pEList; 1344be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 13457cedc8d4Sdanielk1977 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 1346be5c89acSdrh pEList->a[0].pExpr); 13470202b29eSdanielk1977 } 1348fef5208cSdrh }else if( pExpr->pList ){ 1349fef5208cSdrh /* Case 2: expr IN (exprlist) 1350fef5208cSdrh ** 1351e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1352e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1353e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1354e014a838Sdanielk1977 ** a column, use numeric affinity. 1355fef5208cSdrh */ 1356e014a838Sdanielk1977 int i; 135757dbd7b3Sdrh ExprList *pList = pExpr->pList; 135857dbd7b3Sdrh struct ExprList_item *pItem; 135957dbd7b3Sdrh 1360e014a838Sdanielk1977 if( !affinity ){ 1361e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 1362e014a838Sdanielk1977 } 13630202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1364e014a838Sdanielk1977 1365e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 136657dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 136757dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1368e014a838Sdanielk1977 136957dbd7b3Sdrh /* If the expression is not constant then we will need to 137057dbd7b3Sdrh ** disable the test that was generated above that makes sure 137157dbd7b3Sdrh ** this code only executes once. Because for a non-constant 137257dbd7b3Sdrh ** expression we need to rerun this code each time. 137357dbd7b3Sdrh */ 13746c30be8eSdrh if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){ 137557dbd7b3Sdrh VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1); 137657dbd7b3Sdrh int i; 1377d654be80Sdrh for(i=0; i<3; i++){ 137857dbd7b3Sdrh aOp[i].opcode = OP_Noop; 137957dbd7b3Sdrh } 138057dbd7b3Sdrh testAddr = 0; 13814794b980Sdrh } 1382e014a838Sdanielk1977 1383e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 13844adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 138594a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 1386f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0); 1387fef5208cSdrh } 1388fef5208cSdrh } 13890202b29eSdanielk1977 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 1390b3bce662Sdanielk1977 break; 1391fef5208cSdrh } 1392fef5208cSdrh 139351522cd3Sdrh case TK_EXISTS: 139419a775c2Sdrh case TK_SELECT: { 1395fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1396fef5208cSdrh ** value of this select in a memory cell and record the number 1397967e8b73Sdrh ** of the memory cell in iColumn. 1398fef5208cSdrh */ 1399ec7429aeSdrh static const Token one = { "1", 0, 1 }; 140051522cd3Sdrh Select *pSel; 1401ec7429aeSdrh int iMem; 1402ec7429aeSdrh int sop; 14031398ad36Sdrh 1404ec7429aeSdrh pExpr->iColumn = iMem = pParse->nMem++; 140551522cd3Sdrh pSel = pExpr->pSelect; 140651522cd3Sdrh if( pExpr->op==TK_SELECT ){ 140751522cd3Sdrh sop = SRT_Mem; 1408ec7429aeSdrh sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0); 1409ec7429aeSdrh VdbeComment((v, "# Init subquery result")); 141051522cd3Sdrh }else{ 141151522cd3Sdrh sop = SRT_Exists; 1412ec7429aeSdrh sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem); 1413ec7429aeSdrh VdbeComment((v, "# Init EXISTS result")); 141451522cd3Sdrh } 1415ec7429aeSdrh sqlite3ExprDelete(pSel->pLimit); 1416ec7429aeSdrh pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one); 1417ec7429aeSdrh sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0); 1418b3bce662Sdanielk1977 break; 141919a775c2Sdrh } 1420cce7d176Sdrh } 1421b3bce662Sdanielk1977 142257dbd7b3Sdrh if( testAddr ){ 1423d654be80Sdrh sqlite3VdbeJumpHere(v, testAddr); 1424b3bce662Sdanielk1977 } 1425b3bce662Sdanielk1977 return; 1426cce7d176Sdrh } 142751522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1428cce7d176Sdrh 1429cce7d176Sdrh /* 1430fec19aadSdrh ** Generate an instruction that will put the integer describe by 1431fec19aadSdrh ** text z[0..n-1] on the stack. 1432fec19aadSdrh */ 1433fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1434fec19aadSdrh int i; 14356fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 14366fec0762Sdrh sqlite3VdbeAddOp(v, OP_Integer, i, 0); 14376fec0762Sdrh }else if( sqlite3FitsIn64Bits(z) ){ 143829dda4aeSdrh sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n); 1439fec19aadSdrh }else{ 1440fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1441fec19aadSdrh } 1442fec19aadSdrh } 1443fec19aadSdrh 1444fec19aadSdrh /* 1445cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 14461ccde15dSdrh ** expression and leave the result on the top of stack. 1447f2bc013cSdrh ** 1448f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1449f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1450f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1451f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1452f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1453cce7d176Sdrh */ 14544adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1455cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1456cce7d176Sdrh int op; 1457ffe07b2dSdrh int stackChng = 1; /* Amount of change to stack depth */ 1458ffe07b2dSdrh 14597977a17fSdanielk1977 if( v==0 ) return; 14607977a17fSdanielk1977 if( pExpr==0 ){ 1461f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 14627977a17fSdanielk1977 return; 14637977a17fSdanielk1977 } 1464f2bc013cSdrh op = pExpr->op; 1465f2bc013cSdrh switch( op ){ 146613449892Sdrh case TK_AGG_COLUMN: { 146713449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 146813449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 146913449892Sdrh if( !pAggInfo->directMode ){ 147013449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0); 147113449892Sdrh break; 147213449892Sdrh }else if( pAggInfo->useSortingIdx ){ 147313449892Sdrh sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx, 147413449892Sdrh pCol->iSorterColumn); 147513449892Sdrh break; 147613449892Sdrh } 147713449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 147813449892Sdrh } 1479967e8b73Sdrh case TK_COLUMN: { 1480ffe07b2dSdrh if( pExpr->iTable<0 ){ 1481ffe07b2dSdrh /* This only happens when coding check constraints */ 1482ffe07b2dSdrh assert( pParse->ckOffset>0 ); 1483ffe07b2dSdrh sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1); 1484ffe07b2dSdrh }else if( pExpr->iColumn>=0 ){ 14854adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1486aee18ef8Sdanielk1977 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn); 1487c4a3c779Sdrh }else{ 1488f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0); 14892282792aSdrh } 1490cce7d176Sdrh break; 1491cce7d176Sdrh } 1492cce7d176Sdrh case TK_INTEGER: { 1493fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1494fec19aadSdrh break; 149551e9a445Sdrh } 1496fec19aadSdrh case TK_FLOAT: 1497fec19aadSdrh case TK_STRING: { 1498f2bc013cSdrh assert( TK_FLOAT==OP_Real ); 1499f2bc013cSdrh assert( TK_STRING==OP_String8 ); 1500d2687b77Sdrh sqlite3DequoteExpr(pExpr); 1501fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 1502cce7d176Sdrh break; 1503cce7d176Sdrh } 1504f0863fe5Sdrh case TK_NULL: { 1505f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1506f0863fe5Sdrh break; 1507f0863fe5Sdrh } 15085338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 1509c572ef7fSdanielk1977 case TK_BLOB: { 15106c8c6cecSdrh int n; 15116c8c6cecSdrh const char *z; 1512f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 15136c8c6cecSdrh n = pExpr->token.n - 3; 15146c8c6cecSdrh z = pExpr->token.z + 2; 15156c8c6cecSdrh assert( n>=0 ); 15166c8c6cecSdrh if( n==0 ){ 15176c8c6cecSdrh z = ""; 15186c8c6cecSdrh } 15196c8c6cecSdrh sqlite3VdbeOp3(v, op, 0, 0, z, n); 1520c572ef7fSdanielk1977 break; 1521c572ef7fSdanielk1977 } 15225338a5f7Sdanielk1977 #endif 152350457896Sdrh case TK_VARIABLE: { 15244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1525895d7472Sdrh if( pExpr->token.n>1 ){ 1526895d7472Sdrh sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 1527895d7472Sdrh } 152850457896Sdrh break; 152950457896Sdrh } 15304e0cff60Sdrh case TK_REGISTER: { 15314e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); 15324e0cff60Sdrh break; 15334e0cff60Sdrh } 1534487e262fSdrh #ifndef SQLITE_OMIT_CAST 1535487e262fSdrh case TK_CAST: { 1536487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 1537487e262fSdrh int aff, op; 1538487e262fSdrh sqlite3ExprCode(pParse, pExpr->pLeft); 15398df447f0Sdrh aff = sqlite3AffinityType(&pExpr->token, 1); 1540487e262fSdrh switch( aff ){ 1541487e262fSdrh case SQLITE_AFF_INTEGER: op = OP_ToInt; break; 1542487e262fSdrh case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break; 1543487e262fSdrh case SQLITE_AFF_TEXT: op = OP_ToText; break; 1544487e262fSdrh case SQLITE_AFF_NONE: op = OP_ToBlob; break; 1545487e262fSdrh } 1546487e262fSdrh sqlite3VdbeAddOp(v, op, 0, 0); 1547ffe07b2dSdrh stackChng = 0; 1548487e262fSdrh break; 1549487e262fSdrh } 1550487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 1551c9b84a1fSdrh case TK_LT: 1552c9b84a1fSdrh case TK_LE: 1553c9b84a1fSdrh case TK_GT: 1554c9b84a1fSdrh case TK_GE: 1555c9b84a1fSdrh case TK_NE: 1556c9b84a1fSdrh case TK_EQ: { 1557f2bc013cSdrh assert( TK_LT==OP_Lt ); 1558f2bc013cSdrh assert( TK_LE==OP_Le ); 1559f2bc013cSdrh assert( TK_GT==OP_Gt ); 1560f2bc013cSdrh assert( TK_GE==OP_Ge ); 1561f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1562f2bc013cSdrh assert( TK_NE==OP_Ne ); 1563a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1564a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1565be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 1566ffe07b2dSdrh stackChng = -1; 1567a37cdde0Sdanielk1977 break; 1568c9b84a1fSdrh } 1569cce7d176Sdrh case TK_AND: 1570cce7d176Sdrh case TK_OR: 1571cce7d176Sdrh case TK_PLUS: 1572cce7d176Sdrh case TK_STAR: 1573cce7d176Sdrh case TK_MINUS: 1574bf4133cbSdrh case TK_REM: 1575bf4133cbSdrh case TK_BITAND: 1576bf4133cbSdrh case TK_BITOR: 157717c40294Sdrh case TK_SLASH: 1578bf4133cbSdrh case TK_LSHIFT: 1579855eb1cfSdrh case TK_RSHIFT: 15800040077dSdrh case TK_CONCAT: { 1581f2bc013cSdrh assert( TK_AND==OP_And ); 1582f2bc013cSdrh assert( TK_OR==OP_Or ); 1583f2bc013cSdrh assert( TK_PLUS==OP_Add ); 1584f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 1585f2bc013cSdrh assert( TK_REM==OP_Remainder ); 1586f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 1587f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 1588f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 1589f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 1590f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 1591f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 15924adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15934adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1594855eb1cfSdrh sqlite3VdbeAddOp(v, op, 0, 0); 1595ffe07b2dSdrh stackChng = -1; 15960040077dSdrh break; 15970040077dSdrh } 1598cce7d176Sdrh case TK_UMINUS: { 1599fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1600fec19aadSdrh assert( pLeft ); 1601fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1602fec19aadSdrh Token *p = &pLeft->token; 16036e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 16046e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1605fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1606fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1607e6840900Sdrh }else{ 1608fec19aadSdrh codeInteger(v, z, p->n+1); 1609e6840900Sdrh } 16106e142f54Sdrh sqliteFree(z); 16116e142f54Sdrh break; 16126e142f54Sdrh } 16131ccde15dSdrh /* Fall through into TK_NOT */ 16146e142f54Sdrh } 1615bf4133cbSdrh case TK_BITNOT: 16166e142f54Sdrh case TK_NOT: { 1617f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 1618f2bc013cSdrh assert( TK_NOT==OP_Not ); 16194adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 16204adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1621ffe07b2dSdrh stackChng = 0; 1622cce7d176Sdrh break; 1623cce7d176Sdrh } 1624cce7d176Sdrh case TK_ISNULL: 1625cce7d176Sdrh case TK_NOTNULL: { 1626cce7d176Sdrh int dest; 1627f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1628f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 16294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 16304adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 16314adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 16324adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 16334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1634ffe07b2dSdrh stackChng = 0; 1635a37cdde0Sdanielk1977 break; 1636f2bc013cSdrh } 16372282792aSdrh case TK_AGG_FUNCTION: { 163813449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 163913449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0); 16402282792aSdrh break; 16412282792aSdrh } 1642b71090fdSdrh case TK_CONST_FUNC: 1643cce7d176Sdrh case TK_FUNCTION: { 1644cce7d176Sdrh ExprList *pList = pExpr->pList; 164589425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 16460bce8354Sdrh FuncDef *pDef; 16474b59ab5eSdrh int nId; 16484b59ab5eSdrh const char *zId; 164913449892Sdrh int constMask = 0; 1650682f68b0Sdanielk1977 int i; 1651d8123366Sdanielk1977 u8 enc = pParse->db->enc; 1652dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 1653b71090fdSdrh zId = pExpr->token.z; 1654b71090fdSdrh nId = pExpr->token.n; 1655d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 16560bce8354Sdrh assert( pDef!=0 ); 1657f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1658682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 1659d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 166013449892Sdrh constMask |= (1<<i); 1661d02eb1fdSdanielk1977 } 1662dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 1663dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 1664dc1bdc4fSdanielk1977 } 1665dc1bdc4fSdanielk1977 } 1666dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 1667dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 1668d8123366Sdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 1669682f68b0Sdanielk1977 } 167013449892Sdrh sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF); 1671ffe07b2dSdrh stackChng = 1-nExpr; 16726ec2733bSdrh break; 16736ec2733bSdrh } 1674fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1675fe2093d7Sdrh case TK_EXISTS: 167619a775c2Sdrh case TK_SELECT: { 1677b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 16784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1679ad6d9460Sdrh VdbeComment((v, "# load subquery result")); 168019a775c2Sdrh break; 168119a775c2Sdrh } 1682fef5208cSdrh case TK_IN: { 1683fef5208cSdrh int addr; 168494a11211Sdrh char affinity; 1685b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 1686e014a838Sdanielk1977 1687e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1688e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1689ededfd5eSdanielk1977 ** P3 of OP_MakeRecord. 1690e014a838Sdanielk1977 */ 169194a11211Sdrh affinity = comparisonAffinity(pExpr); 1692e014a838Sdanielk1977 16934adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1694e014a838Sdanielk1977 1695e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1696e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1697e014a838Sdanielk1977 */ 16984adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 16994adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1700e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 17014adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 1702f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1703e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 170494a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ 1705e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1706e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1707e014a838Sdanielk1977 1708fef5208cSdrh break; 1709fef5208cSdrh } 171093758c8dSdanielk1977 #endif 1711fef5208cSdrh case TK_BETWEEN: { 1712be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1713be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 1714be5c89acSdrh Expr *pRight = pLItem->pExpr; 1715be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 17164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1717be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1718be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 17194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1720be5c89acSdrh pLItem++; 1721be5c89acSdrh pRight = pLItem->pExpr; 1722be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1723be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 17244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1725fef5208cSdrh break; 1726fef5208cSdrh } 172751e9a445Sdrh case TK_UPLUS: 1728a2e00042Sdrh case TK_AS: { 17294adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1730ffe07b2dSdrh stackChng = 0; 1731a2e00042Sdrh break; 1732a2e00042Sdrh } 173317a7f8ddSdrh case TK_CASE: { 173417a7f8ddSdrh int expr_end_label; 1735f5905aa7Sdrh int jumpInst; 1736f5905aa7Sdrh int nExpr; 173717a7f8ddSdrh int i; 1738be5c89acSdrh ExprList *pEList; 1739be5c89acSdrh struct ExprList_item *aListelem; 174017a7f8ddSdrh 174117a7f8ddSdrh assert(pExpr->pList); 174217a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 174317a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1744be5c89acSdrh pEList = pExpr->pList; 1745be5c89acSdrh aListelem = pEList->a; 1746be5c89acSdrh nExpr = pEList->nExpr; 17474adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 174817a7f8ddSdrh if( pExpr->pLeft ){ 17494adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1750cce7d176Sdrh } 1751f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 1752be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr); 175317a7f8ddSdrh if( pExpr->pLeft ){ 17544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1755be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 1756be5c89acSdrh OP_Ne, 0, 1); 17574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1758f5905aa7Sdrh }else{ 17594adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 176017a7f8ddSdrh } 1761be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr); 17624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 1763d654be80Sdrh sqlite3VdbeJumpHere(v, jumpInst); 176417a7f8ddSdrh } 1765f570f011Sdrh if( pExpr->pLeft ){ 17664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1767f570f011Sdrh } 176817a7f8ddSdrh if( pExpr->pRight ){ 17694adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 177017a7f8ddSdrh }else{ 1771f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 177217a7f8ddSdrh } 17734adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 17746f34903eSdanielk1977 break; 17756f34903eSdanielk1977 } 17765338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 17776f34903eSdanielk1977 case TK_RAISE: { 17786f34903eSdanielk1977 if( !pParse->trigStack ){ 17794adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1780da93d238Sdrh "RAISE() may only be used within a trigger-program"); 17816f34903eSdanielk1977 return; 17826f34903eSdanielk1977 } 1783ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 1784ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 17856f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 1786ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 1787d2687b77Sdrh sqlite3DequoteExpr(pExpr); 17884adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1789701a0aebSdrh pExpr->token.z, pExpr->token.n); 17906f34903eSdanielk1977 } else { 17916f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 1792344737f6Sdrh sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 1793ad6d9460Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 1794ad6d9460Sdrh VdbeComment((v, "# raise(IGNORE)")); 17956f34903eSdanielk1977 } 1796ffe07b2dSdrh stackChng = 0; 1797ffe07b2dSdrh break; 179817a7f8ddSdrh } 17995338a5f7Sdanielk1977 #endif 1800ffe07b2dSdrh } 1801ffe07b2dSdrh 1802ffe07b2dSdrh if( pParse->ckOffset ){ 1803ffe07b2dSdrh pParse->ckOffset += stackChng; 1804ffe07b2dSdrh assert( pParse->ckOffset ); 180517a7f8ddSdrh } 1806cce7d176Sdrh } 1807cce7d176Sdrh 180893758c8dSdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 1809cce7d176Sdrh /* 181025303780Sdrh ** Generate code that evalutes the given expression and leaves the result 181125303780Sdrh ** on the stack. See also sqlite3ExprCode(). 181225303780Sdrh ** 181325303780Sdrh ** This routine might also cache the result and modify the pExpr tree 181425303780Sdrh ** so that it will make use of the cached result on subsequent evaluations 181525303780Sdrh ** rather than evaluate the whole expression again. Trivial expressions are 181625303780Sdrh ** not cached. If the expression is cached, its result is stored in a 181725303780Sdrh ** memory location. 181825303780Sdrh */ 181925303780Sdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ 182025303780Sdrh Vdbe *v = pParse->pVdbe; 182125303780Sdrh int iMem; 182225303780Sdrh int addr1, addr2; 182325303780Sdrh if( v==0 ) return; 182425303780Sdrh addr1 = sqlite3VdbeCurrentAddr(v); 182525303780Sdrh sqlite3ExprCode(pParse, pExpr); 182625303780Sdrh addr2 = sqlite3VdbeCurrentAddr(v); 182725303780Sdrh if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){ 182825303780Sdrh iMem = pExpr->iTable = pParse->nMem++; 182925303780Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); 183025303780Sdrh pExpr->op = TK_REGISTER; 183125303780Sdrh } 183225303780Sdrh } 183393758c8dSdanielk1977 #endif 183425303780Sdrh 183525303780Sdrh /* 1836268380caSdrh ** Generate code that pushes the value of every element of the given 1837f9b596ebSdrh ** expression list onto the stack. 1838268380caSdrh ** 1839268380caSdrh ** Return the number of elements pushed onto the stack. 1840268380caSdrh */ 18414adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1842268380caSdrh Parse *pParse, /* Parsing context */ 1843f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1844268380caSdrh ){ 1845268380caSdrh struct ExprList_item *pItem; 1846268380caSdrh int i, n; 1847268380caSdrh if( pList==0 ) return 0; 1848268380caSdrh n = pList->nExpr; 1849c182d163Sdrh for(pItem=pList->a, i=n; i>0; i--, pItem++){ 18504adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1851268380caSdrh } 1852f9b596ebSdrh return n; 1853268380caSdrh } 1854268380caSdrh 1855268380caSdrh /* 1856cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1857cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1858cce7d176Sdrh ** continues straight thru if the expression is false. 1859f5905aa7Sdrh ** 1860f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1861f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1862f2bc013cSdrh ** 1863f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1864f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1865f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1866f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1867f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1868cce7d176Sdrh */ 18694adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1870cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1871cce7d176Sdrh int op = 0; 1872ffe07b2dSdrh int ckOffset = pParse->ckOffset; 1873daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1874f2bc013cSdrh op = pExpr->op; 1875f2bc013cSdrh switch( op ){ 1876cce7d176Sdrh case TK_AND: { 18774adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 18784adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 18794adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 18804adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1881cce7d176Sdrh break; 1882cce7d176Sdrh } 1883cce7d176Sdrh case TK_OR: { 18844adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 18854adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1886cce7d176Sdrh break; 1887cce7d176Sdrh } 1888cce7d176Sdrh case TK_NOT: { 18894adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1890cce7d176Sdrh break; 1891cce7d176Sdrh } 1892cce7d176Sdrh case TK_LT: 1893cce7d176Sdrh case TK_LE: 1894cce7d176Sdrh case TK_GT: 1895cce7d176Sdrh case TK_GE: 1896cce7d176Sdrh case TK_NE: 18970ac65892Sdrh case TK_EQ: { 1898f2bc013cSdrh assert( TK_LT==OP_Lt ); 1899f2bc013cSdrh assert( TK_LE==OP_Le ); 1900f2bc013cSdrh assert( TK_GT==OP_Gt ); 1901f2bc013cSdrh assert( TK_GE==OP_Ge ); 1902f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1903f2bc013cSdrh assert( TK_NE==OP_Ne ); 19044adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 19054adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1906be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1907cce7d176Sdrh break; 1908cce7d176Sdrh } 1909cce7d176Sdrh case TK_ISNULL: 1910cce7d176Sdrh case TK_NOTNULL: { 1911f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1912f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 19134adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 19144adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1915cce7d176Sdrh break; 1916cce7d176Sdrh } 1917fef5208cSdrh case TK_BETWEEN: { 19180202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 19190202b29eSdanielk1977 ** 19200202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 19210202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 19220202b29eSdanielk1977 ** 3 ... 19230202b29eSdanielk1977 */ 1924f5905aa7Sdrh int addr; 1925be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1926be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1927be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 19284adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1929be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1930be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 19310202b29eSdanielk1977 1932be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1933be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1934be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 19350202b29eSdanielk1977 19364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 1937d654be80Sdrh sqlite3VdbeJumpHere(v, addr); 19384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1939fef5208cSdrh break; 1940fef5208cSdrh } 1941cce7d176Sdrh default: { 19424adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 19434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1944cce7d176Sdrh break; 1945cce7d176Sdrh } 1946cce7d176Sdrh } 1947ffe07b2dSdrh pParse->ckOffset = ckOffset; 1948cce7d176Sdrh } 1949cce7d176Sdrh 1950cce7d176Sdrh /* 195166b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1952cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1953cce7d176Sdrh ** continues straight thru if the expression is true. 1954f5905aa7Sdrh ** 1955f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1956f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1957cce7d176Sdrh */ 19584adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1959cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1960cce7d176Sdrh int op = 0; 1961ffe07b2dSdrh int ckOffset = pParse->ckOffset; 1962daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1963f2bc013cSdrh 1964f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 1965f2bc013cSdrh ** 1966f2bc013cSdrh ** pExpr->op op 1967f2bc013cSdrh ** --------- ---------- 1968f2bc013cSdrh ** TK_ISNULL OP_NotNull 1969f2bc013cSdrh ** TK_NOTNULL OP_IsNull 1970f2bc013cSdrh ** TK_NE OP_Eq 1971f2bc013cSdrh ** TK_EQ OP_Ne 1972f2bc013cSdrh ** TK_GT OP_Le 1973f2bc013cSdrh ** TK_LE OP_Gt 1974f2bc013cSdrh ** TK_GE OP_Lt 1975f2bc013cSdrh ** TK_LT OP_Ge 1976f2bc013cSdrh ** 1977f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 1978f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 1979f2bc013cSdrh ** can compute the mapping above using the following expression. 1980f2bc013cSdrh ** Assert()s verify that the computation is correct. 1981f2bc013cSdrh */ 1982f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 1983f2bc013cSdrh 1984f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 1985f2bc013cSdrh */ 1986f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 1987f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 1988f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 1989f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 1990f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 1991f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 1992f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 1993f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 1994f2bc013cSdrh 1995cce7d176Sdrh switch( pExpr->op ){ 1996cce7d176Sdrh case TK_AND: { 19974adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 19984adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1999cce7d176Sdrh break; 2000cce7d176Sdrh } 2001cce7d176Sdrh case TK_OR: { 20024adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 20034adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 20044adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 20054adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2006cce7d176Sdrh break; 2007cce7d176Sdrh } 2008cce7d176Sdrh case TK_NOT: { 20094adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 2010cce7d176Sdrh break; 2011cce7d176Sdrh } 2012cce7d176Sdrh case TK_LT: 2013cce7d176Sdrh case TK_LE: 2014cce7d176Sdrh case TK_GT: 2015cce7d176Sdrh case TK_GE: 2016cce7d176Sdrh case TK_NE: 2017cce7d176Sdrh case TK_EQ: { 20184adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 20194adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 2020be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 2021cce7d176Sdrh break; 2022cce7d176Sdrh } 2023cce7d176Sdrh case TK_ISNULL: 2024cce7d176Sdrh case TK_NOTNULL: { 20254adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 20264adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 2027cce7d176Sdrh break; 2028cce7d176Sdrh } 2029fef5208cSdrh case TK_BETWEEN: { 20300202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 20310202b29eSdanielk1977 ** 20320202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 20330202b29eSdanielk1977 ** 2 GOTO <dest> 20340202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 20350202b29eSdanielk1977 */ 2036fef5208cSdrh int addr; 2037be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2038be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 2039be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 20404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 2041be5c89acSdrh sqlite3ExprCode(pParse, pRight); 20424adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 2043be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 2044be5c89acSdrh 20454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 20464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 2047be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 2048be5c89acSdrh sqlite3ExprCode(pParse, pRight); 2049be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 2050fef5208cSdrh break; 2051fef5208cSdrh } 2052cce7d176Sdrh default: { 20534adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 20544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 2055cce7d176Sdrh break; 2056cce7d176Sdrh } 2057cce7d176Sdrh } 2058ffe07b2dSdrh pParse->ckOffset = ckOffset; 2059cce7d176Sdrh } 20602282792aSdrh 20612282792aSdrh /* 20622282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 20632282792aSdrh ** if they are identical and return FALSE if they differ in any way. 20642282792aSdrh */ 20654adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 20662282792aSdrh int i; 20672282792aSdrh if( pA==0 ){ 20682282792aSdrh return pB==0; 20692282792aSdrh }else if( pB==0 ){ 20702282792aSdrh return 0; 20712282792aSdrh } 20722282792aSdrh if( pA->op!=pB->op ) return 0; 2073fd357974Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; 20744adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 20754adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 20762282792aSdrh if( pA->pList ){ 20772282792aSdrh if( pB->pList==0 ) return 0; 20782282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 20792282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 20804adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 20812282792aSdrh return 0; 20822282792aSdrh } 20832282792aSdrh } 20842282792aSdrh }else if( pB->pList ){ 20852282792aSdrh return 0; 20862282792aSdrh } 20872282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 20882f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 20892282792aSdrh if( pA->token.z ){ 20902282792aSdrh if( pB->token.z==0 ) return 0; 20916977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 20924adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 20932282792aSdrh } 20942282792aSdrh return 1; 20952282792aSdrh } 20962282792aSdrh 209713449892Sdrh 20982282792aSdrh /* 209913449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 210013449892Sdrh ** the new element. Return a negative number if malloc fails. 21012282792aSdrh */ 210213449892Sdrh static int addAggInfoColumn(AggInfo *pInfo){ 210313449892Sdrh int i; 210413449892Sdrh i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3); 210513449892Sdrh if( i<0 ){ 21062282792aSdrh return -1; 21072282792aSdrh } 210813449892Sdrh return i; 21092282792aSdrh } 211013449892Sdrh 211113449892Sdrh /* 211213449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 211313449892Sdrh ** the new element. Return a negative number if malloc fails. 211413449892Sdrh */ 211513449892Sdrh static int addAggInfoFunc(AggInfo *pInfo){ 211613449892Sdrh int i; 211713449892Sdrh i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2); 211813449892Sdrh if( i<0 ){ 211913449892Sdrh return -1; 212013449892Sdrh } 212113449892Sdrh return i; 21222282792aSdrh } 21232282792aSdrh 21242282792aSdrh /* 2125626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 2126626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 2127626a879aSdrh ** for additional information. 21282282792aSdrh ** 2129626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 21302282792aSdrh */ 2131626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 21322282792aSdrh int i; 2133a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 2134a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 2135a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 213613449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 213713449892Sdrh 21382282792aSdrh 21392282792aSdrh switch( pExpr->op ){ 2140967e8b73Sdrh case TK_COLUMN: { 214113449892Sdrh /* Check to see if the column is in one of the tables in the FROM 214213449892Sdrh ** clause of the aggregate query */ 214313449892Sdrh if( pSrcList ){ 214413449892Sdrh struct SrcList_item *pItem = pSrcList->a; 214513449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 214613449892Sdrh struct AggInfo_col *pCol; 214713449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 214813449892Sdrh /* If we reach this point, it means that pExpr refers to a table 214913449892Sdrh ** that is in the FROM clause of the aggregate query. 215013449892Sdrh ** 215113449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 215213449892Sdrh ** is not an entry there already. 215313449892Sdrh */ 215413449892Sdrh pCol = pAggInfo->aCol; 215513449892Sdrh for(i=0; i<pAggInfo->nColumn; i++, pCol++){ 215613449892Sdrh if( pCol->iTable==pExpr->iTable && 215713449892Sdrh pCol->iColumn==pExpr->iColumn ){ 21582282792aSdrh break; 21592282792aSdrh } 21602282792aSdrh } 216113449892Sdrh if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){ 216213449892Sdrh pCol = &pAggInfo->aCol[i]; 216313449892Sdrh pCol->iTable = pExpr->iTable; 216413449892Sdrh pCol->iColumn = pExpr->iColumn; 216513449892Sdrh pCol->iMem = pParse->nMem++; 216613449892Sdrh pCol->iSorterColumn = -1; 21675774b806Sdrh pCol->pExpr = pExpr; 216813449892Sdrh if( pAggInfo->pGroupBy ){ 216913449892Sdrh int j, n; 217013449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 217113449892Sdrh struct ExprList_item *pTerm = pGB->a; 217213449892Sdrh n = pGB->nExpr; 217313449892Sdrh for(j=0; j<n; j++, pTerm++){ 217413449892Sdrh Expr *pE = pTerm->pExpr; 217513449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 217613449892Sdrh pE->iColumn==pExpr->iColumn ){ 217713449892Sdrh pCol->iSorterColumn = j; 217813449892Sdrh break; 21792282792aSdrh } 218013449892Sdrh } 218113449892Sdrh } 218213449892Sdrh if( pCol->iSorterColumn<0 ){ 218313449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 218413449892Sdrh } 218513449892Sdrh } 218613449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 218713449892Sdrh ** because it was there before or because we just created it). 218813449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 218913449892Sdrh ** pAggInfo->aCol[] entry. 219013449892Sdrh */ 219113449892Sdrh pExpr->pAggInfo = pAggInfo; 219213449892Sdrh pExpr->op = TK_AGG_COLUMN; 2193aaf88729Sdrh pExpr->iAgg = i; 219413449892Sdrh break; 219513449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 219613449892Sdrh } /* end loop over pSrcList */ 2197a58fdfb1Sdanielk1977 } 2198626a879aSdrh return 1; 21992282792aSdrh } 22002282792aSdrh case TK_AGG_FUNCTION: { 220113449892Sdrh /* The pNC->nDepth==0 test causes aggregate functions in subqueries 220213449892Sdrh ** to be ignored */ 2203a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 220413449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 220513449892Sdrh ** function that is already in the pAggInfo structure 220613449892Sdrh */ 220713449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 220813449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 220913449892Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ 22102282792aSdrh break; 22112282792aSdrh } 22122282792aSdrh } 221313449892Sdrh if( i>=pAggInfo->nFunc ){ 221413449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 221513449892Sdrh */ 2216d8123366Sdanielk1977 u8 enc = pParse->db->enc; 221713449892Sdrh i = addAggInfoFunc(pAggInfo); 221813449892Sdrh if( i>=0 ){ 221913449892Sdrh pItem = &pAggInfo->aFunc[i]; 222013449892Sdrh pItem->pExpr = pExpr; 222113449892Sdrh pItem->iMem = pParse->nMem++; 222213449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 22236977fea8Sdrh pExpr->token.z, pExpr->token.n, 2224d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 2225fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 2226fd357974Sdrh pItem->iDistinct = pParse->nTab++; 2227fd357974Sdrh }else{ 2228fd357974Sdrh pItem->iDistinct = -1; 2229fd357974Sdrh } 22302282792aSdrh } 223113449892Sdrh } 223213449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 223313449892Sdrh */ 22342282792aSdrh pExpr->iAgg = i; 223513449892Sdrh pExpr->pAggInfo = pAggInfo; 2236626a879aSdrh return 1; 22372282792aSdrh } 22382282792aSdrh } 2239a58fdfb1Sdanielk1977 } 224013449892Sdrh 224113449892Sdrh /* Recursively walk subqueries looking for TK_COLUMN nodes that need 224213449892Sdrh ** to be changed to TK_AGG_COLUMN. But increment nDepth so that 224313449892Sdrh ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. 224413449892Sdrh */ 2245a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 2246a58fdfb1Sdanielk1977 pNC->nDepth++; 2247a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 2248a58fdfb1Sdanielk1977 pNC->nDepth--; 2249a58fdfb1Sdanielk1977 } 2250626a879aSdrh return 0; 22512282792aSdrh } 2252626a879aSdrh 2253626a879aSdrh /* 2254626a879aSdrh ** Analyze the given expression looking for aggregate functions and 2255626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 2256626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 2257626a879aSdrh ** 2258626a879aSdrh ** This routine should only be called after the expression has been 2259626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 2260626a879aSdrh ** 2261626a879aSdrh ** If errors are seen, leave an error message in zErrMsg and return 2262626a879aSdrh ** the number of errors. 2263626a879aSdrh */ 2264a58fdfb1Sdanielk1977 int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 2265a58fdfb1Sdanielk1977 int nErr = pNC->pParse->nErr; 2266a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 2267a58fdfb1Sdanielk1977 return pNC->pParse->nErr - nErr; 22682282792aSdrh } 22695d9a4af9Sdrh 22705d9a4af9Sdrh /* 22715d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 22725d9a4af9Sdrh ** expression list. Return the number of errors. 22735d9a4af9Sdrh ** 22745d9a4af9Sdrh ** If an error is found, the analysis is cut short. 22755d9a4af9Sdrh */ 22765d9a4af9Sdrh int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 22775d9a4af9Sdrh struct ExprList_item *pItem; 22785d9a4af9Sdrh int i; 22795d9a4af9Sdrh int nErr = 0; 22805d9a4af9Sdrh if( pList ){ 22815d9a4af9Sdrh for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){ 22825d9a4af9Sdrh nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 22835d9a4af9Sdrh } 22845d9a4af9Sdrh } 22855d9a4af9Sdrh return nErr; 22865d9a4af9Sdrh } 2287