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*5ffb3ac8Sdrh ** $Id: expr.c,v 1.285 2007/04/18 17:07:58 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 ){ 468a51256cSdrh return sqlite3AffinityType(&pExpr->token); 47487e262fSdrh } 48487e262fSdrh #endif 49a37cdde0Sdanielk1977 return pExpr->affinity; 50a37cdde0Sdanielk1977 } 51a37cdde0Sdanielk1977 5253db1458Sdrh /* 538b4c40d8Sdrh ** Set the collating sequence for expression pExpr to be the collating 548b4c40d8Sdrh ** sequence named by pToken. Return a pointer to the revised expression. 55a34001c9Sdrh ** The collating sequence is marked as "explicit" using the EP_ExpCollate 56a34001c9Sdrh ** flag. An explicit collating sequence will override implicit 57a34001c9Sdrh ** collating sequences. 588b4c40d8Sdrh */ 598b4c40d8Sdrh Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){ 608b4c40d8Sdrh CollSeq *pColl; 618b4c40d8Sdrh if( pExpr==0 ) return 0; 628b4c40d8Sdrh pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n); 638b4c40d8Sdrh if( pColl ){ 648b4c40d8Sdrh pExpr->pColl = pColl; 658b4c40d8Sdrh pExpr->flags |= EP_ExpCollate; 668b4c40d8Sdrh } 678b4c40d8Sdrh return pExpr; 688b4c40d8Sdrh } 698b4c40d8Sdrh 708b4c40d8Sdrh /* 710202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 720202b29eSdanielk1977 ** there is no default collation type, return 0. 730202b29eSdanielk1977 */ 747cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 757cedc8d4Sdanielk1977 CollSeq *pColl = 0; 760202b29eSdanielk1977 if( pExpr ){ 777cedc8d4Sdanielk1977 pColl = pExpr->pColl; 78487e262fSdrh if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){ 797cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 800202b29eSdanielk1977 } 810202b29eSdanielk1977 } 827cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 837cedc8d4Sdanielk1977 pColl = 0; 847cedc8d4Sdanielk1977 } 857cedc8d4Sdanielk1977 return pColl; 860202b29eSdanielk1977 } 870202b29eSdanielk1977 880202b29eSdanielk1977 /* 89626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 90626a879aSdrh ** type affinity of the other operand. This routine returns the 9153db1458Sdrh ** type affinity that should be used for the comparison operator. 9253db1458Sdrh */ 93e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 94bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 95e014a838Sdanielk1977 if( aff1 && aff2 ){ 968df447f0Sdrh /* Both sides of the comparison are columns. If one has numeric 978df447f0Sdrh ** affinity, use that. Otherwise use no affinity. 98e014a838Sdanielk1977 */ 998a51256cSdrh if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ 100e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 101e014a838Sdanielk1977 }else{ 102e014a838Sdanielk1977 return SQLITE_AFF_NONE; 103e014a838Sdanielk1977 } 104e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 1055f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 1065f6a87b3Sdrh ** results directly. 107e014a838Sdanielk1977 */ 1085f6a87b3Sdrh return SQLITE_AFF_NONE; 109e014a838Sdanielk1977 }else{ 110e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 111fe05af87Sdrh assert( aff1==0 || aff2==0 ); 112e014a838Sdanielk1977 return (aff1 + aff2); 113e014a838Sdanielk1977 } 114e014a838Sdanielk1977 } 115e014a838Sdanielk1977 11653db1458Sdrh /* 11753db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 11853db1458Sdrh ** be applied to both operands prior to doing the comparison. 11953db1458Sdrh */ 120e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 121e014a838Sdanielk1977 char aff; 122e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 123e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 124e014a838Sdanielk1977 pExpr->op==TK_NE ); 125e014a838Sdanielk1977 assert( pExpr->pLeft ); 126bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 127e014a838Sdanielk1977 if( pExpr->pRight ){ 128e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 129e014a838Sdanielk1977 } 130e014a838Sdanielk1977 else if( pExpr->pSelect ){ 131e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 132e014a838Sdanielk1977 } 133e014a838Sdanielk1977 else if( !aff ){ 134de087bd5Sdrh aff = SQLITE_AFF_NONE; 135e014a838Sdanielk1977 } 136e014a838Sdanielk1977 return aff; 137e014a838Sdanielk1977 } 138e014a838Sdanielk1977 139e014a838Sdanielk1977 /* 140e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 141e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 142e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 143e014a838Sdanielk1977 ** the comparison in pExpr. 144e014a838Sdanielk1977 */ 145e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 146e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 1478a51256cSdrh switch( aff ){ 1488a51256cSdrh case SQLITE_AFF_NONE: 1498a51256cSdrh return 1; 1508a51256cSdrh case SQLITE_AFF_TEXT: 1518a51256cSdrh return idx_affinity==SQLITE_AFF_TEXT; 1528a51256cSdrh default: 1538a51256cSdrh return sqlite3IsNumericAffinity(idx_affinity); 1548a51256cSdrh } 155e014a838Sdanielk1977 } 156e014a838Sdanielk1977 157a37cdde0Sdanielk1977 /* 158a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 159a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 160a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 161a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 162a37cdde0Sdanielk1977 ** evaluates to NULL. 163a37cdde0Sdanielk1977 */ 164e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 165bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 166f0863fe5Sdrh return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0); 167a37cdde0Sdanielk1977 } 168a37cdde0Sdanielk1977 169a2e00042Sdrh /* 1700202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1710202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1720202b29eSdanielk1977 ** 1730202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1740202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1750202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1760202b29eSdanielk1977 ** type. 1770202b29eSdanielk1977 */ 1787cedc8d4Sdanielk1977 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ 179ec41ddacSdrh CollSeq *pColl; 180ec41ddacSdrh assert( pLeft ); 181ec41ddacSdrh assert( pRight ); 182ec41ddacSdrh if( pLeft->flags & EP_ExpCollate ){ 183ec41ddacSdrh assert( pLeft->pColl ); 184ec41ddacSdrh pColl = pLeft->pColl; 185ec41ddacSdrh }else if( pRight->flags & EP_ExpCollate ){ 186ec41ddacSdrh assert( pRight->pColl ); 187ec41ddacSdrh pColl = pRight->pColl; 188ec41ddacSdrh }else{ 189ec41ddacSdrh pColl = sqlite3ExprCollSeq(pParse, pLeft); 1900202b29eSdanielk1977 if( !pColl ){ 1917cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 1920202b29eSdanielk1977 } 193ec41ddacSdrh } 1940202b29eSdanielk1977 return pColl; 1950202b29eSdanielk1977 } 1960202b29eSdanielk1977 1970202b29eSdanielk1977 /* 198be5c89acSdrh ** Generate code for a comparison operator. 199be5c89acSdrh */ 200be5c89acSdrh static int codeCompare( 201be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 202be5c89acSdrh Expr *pLeft, /* The left operand */ 203be5c89acSdrh Expr *pRight, /* The right operand */ 204be5c89acSdrh int opcode, /* The comparison opcode */ 205be5c89acSdrh int dest, /* Jump here if true. */ 206be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 207be5c89acSdrh ){ 208be5c89acSdrh int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); 209be5c89acSdrh CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); 210be5c89acSdrh return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); 211be5c89acSdrh } 212be5c89acSdrh 213be5c89acSdrh /* 214a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 215a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 216a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 217a76b5dfcSdrh */ 218e4e72072Sdrh Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ 219a76b5dfcSdrh Expr *pNew; 220a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 221a76b5dfcSdrh if( pNew==0 ){ 222d5d56523Sdanielk1977 /* When malloc fails, delete pLeft and pRight. Expressions passed to 223d5d56523Sdanielk1977 ** this function must always be allocated with sqlite3Expr() for this 224d5d56523Sdanielk1977 ** reason. 225d5d56523Sdanielk1977 */ 226d5d56523Sdanielk1977 sqlite3ExprDelete(pLeft); 227d5d56523Sdanielk1977 sqlite3ExprDelete(pRight); 228a76b5dfcSdrh return 0; 229a76b5dfcSdrh } 230a76b5dfcSdrh pNew->op = op; 231a76b5dfcSdrh pNew->pLeft = pLeft; 232a76b5dfcSdrh pNew->pRight = pRight; 233a58fdfb1Sdanielk1977 pNew->iAgg = -1; 234a76b5dfcSdrh if( pToken ){ 2354b59ab5eSdrh assert( pToken->dyn==0 ); 236145716b3Sdrh pNew->span = pNew->token = *pToken; 237a34001c9Sdrh }else if( pLeft ){ 238a34001c9Sdrh if( pRight ){ 2394adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 240*5ffb3ac8Sdrh if( pRight->flags & EP_ExpCollate ){ 241a34001c9Sdrh pNew->flags |= EP_ExpCollate; 242a34001c9Sdrh pNew->pColl = pRight->pColl; 243a34001c9Sdrh } 244a34001c9Sdrh } 245*5ffb3ac8Sdrh if( pLeft->flags & EP_ExpCollate ){ 246a34001c9Sdrh pNew->flags |= EP_ExpCollate; 247a34001c9Sdrh pNew->pColl = pLeft->pColl; 248a34001c9Sdrh } 249a76b5dfcSdrh } 250a76b5dfcSdrh return pNew; 251a76b5dfcSdrh } 252a76b5dfcSdrh 253a76b5dfcSdrh /* 254206f3d96Sdrh ** Works like sqlite3Expr() but frees its pLeft and pRight arguments 255206f3d96Sdrh ** if it fails due to a malloc problem. 256206f3d96Sdrh */ 257206f3d96Sdrh Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ 258206f3d96Sdrh Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken); 259206f3d96Sdrh if( pNew==0 ){ 260206f3d96Sdrh sqlite3ExprDelete(pLeft); 261206f3d96Sdrh sqlite3ExprDelete(pRight); 262206f3d96Sdrh } 263206f3d96Sdrh return pNew; 264206f3d96Sdrh } 265206f3d96Sdrh 266206f3d96Sdrh /* 2674e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 2684e0cff60Sdrh ** that look like this: #0 #1 #2 ... These terms refer to elements 269288d37f1Sdrh ** on the stack. "#0" means the top of the stack. 270288d37f1Sdrh ** "#1" means the next down on the stack. And so forth. 2714e0cff60Sdrh ** 2724e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 2734e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 2744e0cff60Sdrh ** The returns an expression that will code to extract the value from 2754e0cff60Sdrh ** that memory location as needed. 2764e0cff60Sdrh */ 2774e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 2784e0cff60Sdrh Vdbe *v = pParse->pVdbe; 2794e0cff60Sdrh Expr *p; 2804e0cff60Sdrh int depth; 2814e0cff60Sdrh if( pParse->nested==0 ){ 2824e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 2834e0cff60Sdrh return 0; 2844e0cff60Sdrh } 285bb7ac00bSdrh if( v==0 ) return 0; 2864e0cff60Sdrh p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); 28773c42a13Sdrh if( p==0 ){ 28873c42a13Sdrh return 0; /* Malloc failed */ 28973c42a13Sdrh } 2902646da7eSdrh depth = atoi((char*)&pToken->z[1]); 2914e0cff60Sdrh p->iTable = pParse->nMem++; 2924e0cff60Sdrh sqlite3VdbeAddOp(v, OP_Dup, depth, 0); 2934e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1); 2944e0cff60Sdrh return p; 2954e0cff60Sdrh } 2964e0cff60Sdrh 2974e0cff60Sdrh /* 29891bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 29991bb0eedSdrh ** NULL, then just return the other expression. 30091bb0eedSdrh */ 30191bb0eedSdrh Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ 30291bb0eedSdrh if( pLeft==0 ){ 30391bb0eedSdrh return pRight; 30491bb0eedSdrh }else if( pRight==0 ){ 30591bb0eedSdrh return pLeft; 30691bb0eedSdrh }else{ 30791bb0eedSdrh return sqlite3Expr(TK_AND, pLeft, pRight, 0); 30891bb0eedSdrh } 30991bb0eedSdrh } 31091bb0eedSdrh 31191bb0eedSdrh /* 3126977fea8Sdrh ** Set the Expr.span field of the given expression to span all 313a76b5dfcSdrh ** text between the two given tokens. 314a76b5dfcSdrh */ 3154adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 3164efc4754Sdrh assert( pRight!=0 ); 3174efc4754Sdrh assert( pLeft!=0 ); 3189e12800dSdanielk1977 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){ 319ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 320145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 3216977fea8Sdrh pExpr->span.z = pLeft->z; 32297903fefSdrh pExpr->span.n = pRight->n + (pRight->z - pLeft->z); 3234b59ab5eSdrh }else{ 3246977fea8Sdrh pExpr->span.z = 0; 3254b59ab5eSdrh } 326a76b5dfcSdrh } 327a76b5dfcSdrh } 328a76b5dfcSdrh 329a76b5dfcSdrh /* 330a76b5dfcSdrh ** Construct a new expression node for a function with multiple 331a76b5dfcSdrh ** arguments. 332a76b5dfcSdrh */ 3334adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 334a76b5dfcSdrh Expr *pNew; 3354b202ae2Sdanielk1977 assert( pToken ); 336a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 337a76b5dfcSdrh if( pNew==0 ){ 338d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ 339a76b5dfcSdrh return 0; 340a76b5dfcSdrh } 341a76b5dfcSdrh pNew->op = TK_FUNCTION; 342a76b5dfcSdrh pNew->pList = pList; 3434b59ab5eSdrh assert( pToken->dyn==0 ); 344a76b5dfcSdrh pNew->token = *pToken; 3456977fea8Sdrh pNew->span = pNew->token; 346a76b5dfcSdrh return pNew; 347a76b5dfcSdrh } 348a76b5dfcSdrh 349a76b5dfcSdrh /* 350fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 351fa6bc000Sdrh ** in the original SQL statement. 352fa6bc000Sdrh ** 353fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 354fa6bc000Sdrh ** variable number. 355fa6bc000Sdrh ** 356fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 357fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 358fa6bc000Sdrh ** the SQL statement comes from an external source. 359fa6bc000Sdrh ** 360fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 361fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 362fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 363fa6bc000Sdrh ** assigned. 364fa6bc000Sdrh */ 365fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 366fa6bc000Sdrh Token *pToken; 367fa6bc000Sdrh if( pExpr==0 ) return; 368fa6bc000Sdrh pToken = &pExpr->token; 369fa6bc000Sdrh assert( pToken->n>=1 ); 370fa6bc000Sdrh assert( pToken->z!=0 ); 371fa6bc000Sdrh assert( pToken->z[0]!=0 ); 372fa6bc000Sdrh if( pToken->n==1 ){ 373fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 374fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 375fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 376fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 377fa6bc000Sdrh ** use it as the variable number */ 378fa6bc000Sdrh int i; 3792646da7eSdrh pExpr->iTable = i = atoi((char*)&pToken->z[1]); 380fa6bc000Sdrh if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ 381fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 382fa6bc000Sdrh SQLITE_MAX_VARIABLE_NUMBER); 383fa6bc000Sdrh } 384fa6bc000Sdrh if( i>pParse->nVar ){ 385fa6bc000Sdrh pParse->nVar = i; 386fa6bc000Sdrh } 387fa6bc000Sdrh }else{ 388fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 389fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 390fa6bc000Sdrh ** has never appeared before, reuse the same variable number 391fa6bc000Sdrh */ 392fa6bc000Sdrh int i, n; 393fa6bc000Sdrh n = pToken->n; 394fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 395fa6bc000Sdrh Expr *pE; 396fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 397fa6bc000Sdrh && pE->token.n==n 398fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 399fa6bc000Sdrh pExpr->iTable = pE->iTable; 400fa6bc000Sdrh break; 401fa6bc000Sdrh } 402fa6bc000Sdrh } 403fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 404fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 405fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 406fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 407cf643729Sdrh pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr, 408fa6bc000Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); 409fa6bc000Sdrh } 4109e12800dSdanielk1977 if( !sqlite3MallocFailed() ){ 411fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 412fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 413fa6bc000Sdrh } 414fa6bc000Sdrh } 415fa6bc000Sdrh } 416fa6bc000Sdrh } 417fa6bc000Sdrh 418fa6bc000Sdrh /* 419a2e00042Sdrh ** Recursively delete an expression tree. 420a2e00042Sdrh */ 4214adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 422a2e00042Sdrh if( p==0 ) return; 4234efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 4244efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 4254adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 4264adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 4274adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 4284adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 429a2e00042Sdrh sqliteFree(p); 430a2e00042Sdrh } 431a2e00042Sdrh 432d2687b77Sdrh /* 433d2687b77Sdrh ** The Expr.token field might be a string literal that is quoted. 434d2687b77Sdrh ** If so, remove the quotation marks. 435d2687b77Sdrh */ 436d2687b77Sdrh void sqlite3DequoteExpr(Expr *p){ 437d2687b77Sdrh if( ExprHasAnyProperty(p, EP_Dequoted) ){ 438d2687b77Sdrh return; 439d2687b77Sdrh } 440d2687b77Sdrh ExprSetProperty(p, EP_Dequoted); 441d2687b77Sdrh if( p->token.dyn==0 ){ 442d2687b77Sdrh sqlite3TokenCopy(&p->token, &p->token); 443d2687b77Sdrh } 444d2687b77Sdrh sqlite3Dequote((char*)p->token.z); 445d2687b77Sdrh } 446d2687b77Sdrh 447a76b5dfcSdrh 448a76b5dfcSdrh /* 449ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 450ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 451ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 452ff78bd2fSdrh ** without effecting the originals. 453ff78bd2fSdrh ** 4544adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 4554adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 456ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 457ff78bd2fSdrh ** 458ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 459ff78bd2fSdrh */ 4604adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 461ff78bd2fSdrh Expr *pNew; 462ff78bd2fSdrh if( p==0 ) return 0; 463fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 464ff78bd2fSdrh if( pNew==0 ) return 0; 4653b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 4666977fea8Sdrh if( p->token.z!=0 ){ 4672646da7eSdrh pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n); 4684b59ab5eSdrh pNew->token.dyn = 1; 4694b59ab5eSdrh }else{ 4704efc4754Sdrh assert( pNew->token.z==0 ); 4714b59ab5eSdrh } 4726977fea8Sdrh pNew->span.z = 0; 4734adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 4744adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 4754adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 4764adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 477aee18ef8Sdanielk1977 pNew->pTab = p->pTab; 478ff78bd2fSdrh return pNew; 479ff78bd2fSdrh } 4804adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 4814b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 4824b59ab5eSdrh if( pFrom->z ){ 4834b59ab5eSdrh pTo->n = pFrom->n; 4842646da7eSdrh pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n); 4854b59ab5eSdrh pTo->dyn = 1; 4864b59ab5eSdrh }else{ 4874b59ab5eSdrh pTo->z = 0; 4884b59ab5eSdrh } 4894b59ab5eSdrh } 4904adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 491ff78bd2fSdrh ExprList *pNew; 492145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 493ff78bd2fSdrh int i; 494ff78bd2fSdrh if( p==0 ) return 0; 495ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 496ff78bd2fSdrh if( pNew==0 ) return 0; 4974305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 4983e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 499e0048400Sdanielk1977 if( pItem==0 ){ 500e0048400Sdanielk1977 sqliteFree(pNew); 501e0048400Sdanielk1977 return 0; 502e0048400Sdanielk1977 } 503145716b3Sdrh pOldItem = p->a; 504145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 5054b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 506145716b3Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); 5076977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 5086977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 5094b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 5104b59ab5eSdrh ** the names of columns in the result set needs this information */ 5114adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 5124b59ab5eSdrh } 5131f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 5146f7adc8aSdrh || pOldExpr->span.z==0 5159e12800dSdanielk1977 || sqlite3MallocFailed() ); 516145716b3Sdrh pItem->zName = sqliteStrDup(pOldItem->zName); 517145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 518145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 5193e7bc9caSdrh pItem->done = 0; 520ff78bd2fSdrh } 521ff78bd2fSdrh return pNew; 522ff78bd2fSdrh } 52393758c8dSdanielk1977 52493758c8dSdanielk1977 /* 52593758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 52693758c8dSdanielk1977 ** the build, then none of the following routines, except for 52793758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 52893758c8dSdanielk1977 ** called with a NULL argument. 52993758c8dSdanielk1977 */ 5306a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 5316a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 5324adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 533ad3cab52Sdrh SrcList *pNew; 534ad3cab52Sdrh int i; 535113088ecSdrh int nByte; 536ad3cab52Sdrh if( p==0 ) return 0; 537113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 5384efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 539ad3cab52Sdrh if( pNew==0 ) return 0; 5404305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 541ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 5424efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 5434efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 544ed8a3bb1Sdrh Table *pTab; 5454efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 5464efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 5474efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 5484efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 5494efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 5501787ccabSdanielk1977 pNewItem->isPopulated = pOldItem->isPopulated; 551ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 552ed8a3bb1Sdrh if( pTab ){ 553ed8a3bb1Sdrh pTab->nRef++; 554a1cb183dSdanielk1977 } 5554adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 5564adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 5574adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 5586c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 559ad3cab52Sdrh } 560ad3cab52Sdrh return pNew; 561ad3cab52Sdrh } 5624adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 563ff78bd2fSdrh IdList *pNew; 564ff78bd2fSdrh int i; 565ff78bd2fSdrh if( p==0 ) return 0; 5664efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 567ff78bd2fSdrh if( pNew==0 ) return 0; 5684305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 5694efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 570d5d56523Sdanielk1977 if( pNew->a==0 ){ 571d5d56523Sdanielk1977 sqliteFree(pNew); 572d5d56523Sdanielk1977 return 0; 573d5d56523Sdanielk1977 } 574ff78bd2fSdrh for(i=0; i<p->nId; i++){ 5754efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 5764efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 5774efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 5784efc4754Sdrh pNewItem->idx = pOldItem->idx; 579ff78bd2fSdrh } 580ff78bd2fSdrh return pNew; 581ff78bd2fSdrh } 5824adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 583ff78bd2fSdrh Select *pNew; 584ff78bd2fSdrh if( p==0 ) return 0; 5854efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 586ff78bd2fSdrh if( pNew==0 ) return 0; 587ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 5884adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 5894adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 5904adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 5914adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 5924adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 5934adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 594ff78bd2fSdrh pNew->op = p->op; 5954adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 596a2dc3b1aSdanielk1977 pNew->pLimit = sqlite3ExprDup(p->pLimit); 597a2dc3b1aSdanielk1977 pNew->pOffset = sqlite3ExprDup(p->pOffset); 5987b58daeaSdrh pNew->iLimit = -1; 5997b58daeaSdrh pNew->iOffset = -1; 600a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 601a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 602b9bb7c18Sdrh pNew->usesEphm = 0; 6038e647b81Sdrh pNew->disallowOrderBy = 0; 6040342b1f5Sdrh pNew->pRightmost = 0; 605b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 606b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 607b9bb7c18Sdrh pNew->addrOpenEphm[2] = -1; 608ff78bd2fSdrh return pNew; 609ff78bd2fSdrh } 61093758c8dSdanielk1977 #else 61193758c8dSdanielk1977 Select *sqlite3SelectDup(Select *p){ 61293758c8dSdanielk1977 assert( p==0 ); 61393758c8dSdanielk1977 return 0; 61493758c8dSdanielk1977 } 61593758c8dSdanielk1977 #endif 616ff78bd2fSdrh 617ff78bd2fSdrh 618ff78bd2fSdrh /* 619a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 620a76b5dfcSdrh ** initially NULL, then create a new expression list. 621a76b5dfcSdrh */ 6224adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 623a76b5dfcSdrh if( pList==0 ){ 624a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 625a76b5dfcSdrh if( pList==0 ){ 626d5d56523Sdanielk1977 goto no_mem; 627a76b5dfcSdrh } 6284efc4754Sdrh assert( pList->nAlloc==0 ); 629a76b5dfcSdrh } 6304305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 631d5d56523Sdanielk1977 struct ExprList_item *a; 632d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 633d5d56523Sdanielk1977 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); 634d5d56523Sdanielk1977 if( a==0 ){ 635d5d56523Sdanielk1977 goto no_mem; 636a76b5dfcSdrh } 637d5d56523Sdanielk1977 pList->a = a; 638d5d56523Sdanielk1977 pList->nAlloc = n; 639a76b5dfcSdrh } 6404efc4754Sdrh assert( pList->a!=0 ); 6414efc4754Sdrh if( pExpr || pName ){ 6424efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 6434efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 644a99db3b6Sdrh pItem->zName = sqlite3NameFromToken(pName); 645e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 646a76b5dfcSdrh } 647a76b5dfcSdrh return pList; 648d5d56523Sdanielk1977 649d5d56523Sdanielk1977 no_mem: 650d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 651d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 652d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 653d5d56523Sdanielk1977 return 0; 654a76b5dfcSdrh } 655a76b5dfcSdrh 656a76b5dfcSdrh /* 657a76b5dfcSdrh ** Delete an entire expression list. 658a76b5dfcSdrh */ 6594adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 660a76b5dfcSdrh int i; 661be5c89acSdrh struct ExprList_item *pItem; 662a76b5dfcSdrh if( pList==0 ) return; 6631bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 6641bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 665be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 666be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 667be5c89acSdrh sqliteFree(pItem->zName); 668a76b5dfcSdrh } 669a76b5dfcSdrh sqliteFree(pList->a); 670a76b5dfcSdrh sqliteFree(pList); 671a76b5dfcSdrh } 672a76b5dfcSdrh 673a76b5dfcSdrh /* 674626a879aSdrh ** Walk an expression tree. Call xFunc for each node visited. 67573b211abSdrh ** 676626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 677626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 678626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 679626a879aSdrh ** the tree walk completely. 680626a879aSdrh ** 681626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 682626a879aSdrh ** and 0 to continue. 68387abf5c0Sdrh ** 68487abf5c0Sdrh ** NOTICE: This routine does *not* descend into subqueries. 685626a879aSdrh */ 686a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 687626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 688626a879aSdrh int rc; 689626a879aSdrh if( pExpr==0 ) return 0; 690626a879aSdrh rc = (*xFunc)(pArg, pExpr); 691626a879aSdrh if( rc==0 ){ 692626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 693626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 694a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 695626a879aSdrh } 696626a879aSdrh return rc>1; 697626a879aSdrh } 698626a879aSdrh 699626a879aSdrh /* 700a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 701a58fdfb1Sdanielk1977 */ 702a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 703a58fdfb1Sdanielk1977 int i; 704a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 705a58fdfb1Sdanielk1977 if( !p ) return 0; 706a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 707a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 708a58fdfb1Sdanielk1977 } 709a58fdfb1Sdanielk1977 return 0; 710a58fdfb1Sdanielk1977 } 711a58fdfb1Sdanielk1977 712a58fdfb1Sdanielk1977 /* 713a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 714a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 715a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 716a58fdfb1Sdanielk1977 */ 717a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 718a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 719a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 720a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 721a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 722a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 723a58fdfb1Sdanielk1977 return 0; 724a58fdfb1Sdanielk1977 } 725a58fdfb1Sdanielk1977 726a58fdfb1Sdanielk1977 727a58fdfb1Sdanielk1977 /* 728626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 729626a879aSdrh ** 730626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 73173b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 73273b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 73373b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 73473b211abSdrh ** then do nothing. 73573b211abSdrh ** 73673b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 73773b211abSdrh ** the expression as constant, then we assume the whole expression 73873b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 739626a879aSdrh */ 740626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 741626a879aSdrh switch( pExpr->op ){ 742eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 743eb55bd2fSdrh ** and *pArg==2 */ 744eb55bd2fSdrh case TK_FUNCTION: 745eb55bd2fSdrh if( *((int*)pArg)==2 ) return 0; 746eb55bd2fSdrh /* Fall through */ 747626a879aSdrh case TK_ID: 748626a879aSdrh case TK_COLUMN: 749626a879aSdrh case TK_DOT: 750626a879aSdrh case TK_AGG_FUNCTION: 75113449892Sdrh case TK_AGG_COLUMN: 752fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 753fe2093d7Sdrh case TK_SELECT: 754fe2093d7Sdrh case TK_EXISTS: 755fe2093d7Sdrh #endif 756626a879aSdrh *((int*)pArg) = 0; 757626a879aSdrh return 2; 75887abf5c0Sdrh case TK_IN: 75987abf5c0Sdrh if( pExpr->pSelect ){ 76087abf5c0Sdrh *((int*)pArg) = 0; 76187abf5c0Sdrh return 2; 76287abf5c0Sdrh } 763626a879aSdrh default: 764626a879aSdrh return 0; 765626a879aSdrh } 766626a879aSdrh } 767626a879aSdrh 768626a879aSdrh /* 769fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 770eb55bd2fSdrh ** and 0 if it involves variables or function calls. 7712398937bSdrh ** 7722398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 7732398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 7742398937bSdrh ** a constant. 775fef5208cSdrh */ 7764adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 777626a879aSdrh int isConst = 1; 778626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 779626a879aSdrh return isConst; 780fef5208cSdrh } 781fef5208cSdrh 782fef5208cSdrh /* 783eb55bd2fSdrh ** Walk an expression tree. Return 1 if the expression is constant 784eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 785eb55bd2fSdrh ** are any variables. 786eb55bd2fSdrh ** 787eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 788eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 789eb55bd2fSdrh ** a constant. 790eb55bd2fSdrh */ 791eb55bd2fSdrh int sqlite3ExprIsConstantOrFunction(Expr *p){ 792eb55bd2fSdrh int isConst = 2; 793eb55bd2fSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 794eb55bd2fSdrh return isConst!=0; 795eb55bd2fSdrh } 796eb55bd2fSdrh 797eb55bd2fSdrh /* 79873b211abSdrh ** If the expression p codes a constant integer that is small enough 799202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 800202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 801202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 802e4de1febSdrh */ 8034adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 804e4de1febSdrh switch( p->op ){ 805e4de1febSdrh case TK_INTEGER: { 8062646da7eSdrh if( sqlite3GetInt32((char*)p->token.z, pValue) ){ 807e4de1febSdrh return 1; 808e4de1febSdrh } 809202b2df7Sdrh break; 810202b2df7Sdrh } 8114b59ab5eSdrh case TK_UPLUS: { 8124adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 8134b59ab5eSdrh } 814e4de1febSdrh case TK_UMINUS: { 815e4de1febSdrh int v; 8164adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 817e4de1febSdrh *pValue = -v; 818e4de1febSdrh return 1; 819e4de1febSdrh } 820e4de1febSdrh break; 821e4de1febSdrh } 822e4de1febSdrh default: break; 823e4de1febSdrh } 824e4de1febSdrh return 0; 825e4de1febSdrh } 826e4de1febSdrh 827e4de1febSdrh /* 828c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 829c4a3c779Sdrh */ 8304adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 8314adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 8324adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 8334adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 834c4a3c779Sdrh return 0; 835c4a3c779Sdrh } 836c4a3c779Sdrh 837c4a3c779Sdrh /* 8388141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 8398141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 8408141f61eSdrh ** expression node refer back to that source column. The following changes 8418141f61eSdrh ** are made to pExpr: 8428141f61eSdrh ** 8438141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 8448141f61eSdrh ** the table. 8458141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 8468141f61eSdrh ** from pSrcList. 8478141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 8488141f61eSdrh ** pExpr->op Set to TK_COLUMN. 8498141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 8508141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 8518141f61eSdrh ** 8528141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 8538141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 8548141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 8558141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 8568141f61eSdrh ** means that the form of the name is Z and that columns from any table 8578141f61eSdrh ** can be used. 8588141f61eSdrh ** 8598141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 8608141f61eSdrh ** in pParse and return non-zero. Return zero on success. 8618141f61eSdrh */ 8628141f61eSdrh static int lookupName( 8638141f61eSdrh Parse *pParse, /* The parsing context */ 8648141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 8658141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 8668141f61eSdrh Token *pColumnToken, /* Name of the column. */ 867626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 8688141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 8698141f61eSdrh ){ 8708141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 8718141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 8728141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 8738141f61eSdrh int i, j; /* Loop counters */ 8748141f61eSdrh int cnt = 0; /* Number of matching column names */ 8758141f61eSdrh int cntTab = 0; /* Number of matching table names */ 8769bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 87751669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 87851669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 87973b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 8808141f61eSdrh 8818141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 882a99db3b6Sdrh zDb = sqlite3NameFromToken(pDbToken); 883a99db3b6Sdrh zTab = sqlite3NameFromToken(pTableToken); 884a99db3b6Sdrh zCol = sqlite3NameFromToken(pColumnToken); 8859e12800dSdanielk1977 if( sqlite3MallocFailed() ){ 886d5d56523Sdanielk1977 goto lookupname_end; 8878141f61eSdrh } 8888141f61eSdrh 8898141f61eSdrh pExpr->iTable = -1; 890626a879aSdrh while( pNC && cnt==0 ){ 891ffe07b2dSdrh ExprList *pEList; 892626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 893626a879aSdrh 894b3bce662Sdanielk1977 if( pSrcList ){ 89551669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 89643617e9aSdrh Table *pTab; 89743617e9aSdrh int iDb; 8988141f61eSdrh Column *pCol; 8998141f61eSdrh 90043617e9aSdrh pTab = pItem->pTab; 90143617e9aSdrh assert( pTab!=0 ); 90243617e9aSdrh iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 9038141f61eSdrh assert( pTab->nCol>0 ); 9048141f61eSdrh if( zTab ){ 9058141f61eSdrh if( pItem->zAlias ){ 9068141f61eSdrh char *zTabName = pItem->zAlias; 9074adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 9088141f61eSdrh }else{ 9098141f61eSdrh char *zTabName = pTab->zName; 9104adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 911da184236Sdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ 9128141f61eSdrh continue; 9138141f61eSdrh } 9148141f61eSdrh } 9158141f61eSdrh } 9168141f61eSdrh if( 0==(cntTab++) ){ 9178141f61eSdrh pExpr->iTable = pItem->iCursor; 918da184236Sdanielk1977 pExpr->pSchema = pTab->pSchema; 91951669863Sdrh pMatch = pItem; 9208141f61eSdrh } 9218141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 9224adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 923b3bf556eSdanielk1977 const char *zColl = pTab->aCol[j].zColl; 924873fac0cSdrh IdList *pUsing; 9258141f61eSdrh cnt++; 9268141f61eSdrh pExpr->iTable = pItem->iCursor; 92751669863Sdrh pMatch = pItem; 928da184236Sdanielk1977 pExpr->pSchema = pTab->pSchema; 9298141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 9308141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 931a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 9328b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 933b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 9348b4c40d8Sdrh } 93561dfc31dSdrh if( i<pSrcList->nSrc-1 ){ 93661dfc31dSdrh if( pItem[1].jointype & JT_NATURAL ){ 937355ef361Sdrh /* If this match occurred in the left table of a natural join, 938355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 939355ef361Sdrh pItem++; 940355ef361Sdrh i++; 94161dfc31dSdrh }else if( (pUsing = pItem[1].pUsing)!=0 ){ 942873fac0cSdrh /* If this match occurs on a column that is in the USING clause 943873fac0cSdrh ** of a join, skip the search of the right table of the join 944873fac0cSdrh ** to avoid a duplicate match there. */ 945873fac0cSdrh int k; 946873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 947873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 948873fac0cSdrh pItem++; 949873fac0cSdrh i++; 950873fac0cSdrh break; 951873fac0cSdrh } 952873fac0cSdrh } 953873fac0cSdrh } 95461dfc31dSdrh } 9558141f61eSdrh break; 9568141f61eSdrh } 9578141f61eSdrh } 9588141f61eSdrh } 959b3bce662Sdanielk1977 } 9608141f61eSdrh 961b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 9628141f61eSdrh /* If we have not already resolved the name, then maybe 9638141f61eSdrh ** it is a new.* or old.* trigger argument reference 9648141f61eSdrh */ 9658141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 9668141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 9678141f61eSdrh Table *pTab = 0; 9684adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 9698141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 9708141f61eSdrh assert( pTriggerStack->pTab ); 9718141f61eSdrh pTab = pTriggerStack->pTab; 9724adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 9738141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 9748141f61eSdrh assert( pTriggerStack->pTab ); 9758141f61eSdrh pTab = pTriggerStack->pTab; 9768141f61eSdrh } 9778141f61eSdrh 9788141f61eSdrh if( pTab ){ 979f0113000Sdanielk1977 int iCol; 9808141f61eSdrh Column *pCol = pTab->aCol; 9818141f61eSdrh 982da184236Sdanielk1977 pExpr->pSchema = pTab->pSchema; 9838141f61eSdrh cntTab++; 984f0113000Sdanielk1977 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { 9854adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 986f0113000Sdanielk1977 const char *zColl = pTab->aCol[iCol].zColl; 9878141f61eSdrh cnt++; 988f0113000Sdanielk1977 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; 989f0113000Sdanielk1977 pExpr->affinity = pTab->aCol[iCol].affinity; 9908b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 991b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 9928b4c40d8Sdrh } 993aee18ef8Sdanielk1977 pExpr->pTab = pTab; 9948141f61eSdrh break; 9958141f61eSdrh } 9968141f61eSdrh } 9978141f61eSdrh } 9988141f61eSdrh } 999b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 10008141f61eSdrh 10018141f61eSdrh /* 10028141f61eSdrh ** Perhaps the name is a reference to the ROWID 10038141f61eSdrh */ 10044adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 10058141f61eSdrh cnt = 1; 10068141f61eSdrh pExpr->iColumn = -1; 10078a51256cSdrh pExpr->affinity = SQLITE_AFF_INTEGER; 10088141f61eSdrh } 10098141f61eSdrh 10108141f61eSdrh /* 10118141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 10128141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 10138141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 10148141f61eSdrh ** 10158141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 10168141f61eSdrh ** 10178141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 10188141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 10198141f61eSdrh ** Note that the expression in the result set should have already been 10208141f61eSdrh ** resolved by the time the WHERE clause is resolved. 10218141f61eSdrh */ 1022ffe07b2dSdrh if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ 10238141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 10248141f61eSdrh char *zAs = pEList->a[j].zName; 10254adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 10268141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 10278141f61eSdrh pExpr->op = TK_AS; 10288141f61eSdrh pExpr->iColumn = j; 10294adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 103015ccce1cSdrh cnt = 1; 10318141f61eSdrh assert( zTab==0 && zDb==0 ); 103215ccce1cSdrh goto lookupname_end_2; 10338141f61eSdrh } 10348141f61eSdrh } 10358141f61eSdrh } 10368141f61eSdrh 1037626a879aSdrh /* Advance to the next name context. The loop will exit when either 1038626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 1039626a879aSdrh */ 1040626a879aSdrh if( cnt==0 ){ 1041626a879aSdrh pNC = pNC->pNext; 1042626a879aSdrh } 1043626a879aSdrh } 1044626a879aSdrh 10458141f61eSdrh /* 10468141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 10478141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 10488141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 10498141f61eSdrh ** case, we need to return right away and not make any changes to 10508141f61eSdrh ** pExpr. 105115ccce1cSdrh ** 105215ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 105315ccce1cSdrh ** fields are not changed in any context. 10548141f61eSdrh */ 10558141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 10568141f61eSdrh sqliteFree(zCol); 10578141f61eSdrh return 0; 10588141f61eSdrh } 10598141f61eSdrh 10608141f61eSdrh /* 10618141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 10628141f61eSdrh ** more matches. Either way, we have an error. 10638141f61eSdrh */ 10648141f61eSdrh if( cnt!=1 ){ 10658141f61eSdrh char *z = 0; 10668141f61eSdrh char *zErr; 10678141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 10688141f61eSdrh if( zDb ){ 1069f93339deSdrh sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0); 10708141f61eSdrh }else if( zTab ){ 1071f93339deSdrh sqlite3SetString(&z, zTab, ".", zCol, (char*)0); 10728141f61eSdrh }else{ 10738141f61eSdrh z = sqliteStrDup(zCol); 10748141f61eSdrh } 10754adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 10768141f61eSdrh sqliteFree(z); 107773b211abSdrh pTopNC->nErr++; 10788141f61eSdrh } 10798141f61eSdrh 108051669863Sdrh /* If a column from a table in pSrcList is referenced, then record 108151669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 108251669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 108351669863Sdrh ** column number is greater than the number of bits in the bitmask 108451669863Sdrh ** then set the high-order bit of the bitmask. 108551669863Sdrh */ 108651669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 108751669863Sdrh int n = pExpr->iColumn; 108851669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 108951669863Sdrh n = sizeof(Bitmask)*8-1; 109051669863Sdrh } 109151669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 1092ca83ac51Sdrh pMatch->colUsed |= ((Bitmask)1)<<n; 109351669863Sdrh } 109451669863Sdrh 1095d5d56523Sdanielk1977 lookupname_end: 10968141f61eSdrh /* Clean up and return 10978141f61eSdrh */ 10988141f61eSdrh sqliteFree(zDb); 10998141f61eSdrh sqliteFree(zTab); 11004adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 11018141f61eSdrh pExpr->pLeft = 0; 11024adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 11038141f61eSdrh pExpr->pRight = 0; 11048141f61eSdrh pExpr->op = TK_COLUMN; 110515ccce1cSdrh lookupname_end_2: 110615ccce1cSdrh sqliteFree(zCol); 1107626a879aSdrh if( cnt==1 ){ 1108b3bce662Sdanielk1977 assert( pNC!=0 ); 1109626a879aSdrh sqlite3AuthRead(pParse, pExpr, pNC->pSrcList); 1110aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 1111aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 1112aee18ef8Sdanielk1977 } 111315ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 111415ccce1cSdrh ** the point where the name matched. */ 111515ccce1cSdrh for(;;){ 111615ccce1cSdrh assert( pTopNC!=0 ); 111715ccce1cSdrh pTopNC->nRef++; 111815ccce1cSdrh if( pTopNC==pNC ) break; 111915ccce1cSdrh pTopNC = pTopNC->pNext; 1120626a879aSdrh } 112115ccce1cSdrh return 0; 112215ccce1cSdrh } else { 112315ccce1cSdrh return 1; 112415ccce1cSdrh } 11258141f61eSdrh } 11268141f61eSdrh 11278141f61eSdrh /* 1128626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1129626a879aSdrh ** 113073b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1131626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 113273b211abSdrh ** the tree or 2 to abort the tree walk. 113373b211abSdrh ** 113473b211abSdrh ** This routine also does error checking and name resolution for 113573b211abSdrh ** function names. The operator for aggregate functions is changed 113673b211abSdrh ** to TK_AGG_FUNCTION. 1137626a879aSdrh */ 1138626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1139626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1140626a879aSdrh Parse *pParse; 1141626a879aSdrh 1142b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1143626a879aSdrh assert( pNC!=0 ); 1144626a879aSdrh pParse = pNC->pParse; 1145b3bce662Sdanielk1977 1146626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1147626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1148626a879aSdrh #ifndef NDEBUG 1149f0113000Sdanielk1977 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ 1150f0113000Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 1151940fac9dSdanielk1977 int i; 1152f0113000Sdanielk1977 for(i=0; i<pNC->pSrcList->nSrc; i++){ 1153626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1154626a879aSdrh } 1155626a879aSdrh } 1156626a879aSdrh #endif 1157626a879aSdrh switch( pExpr->op ){ 1158626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1159626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1160626a879aSdrh ** strings (ex: 'abc') are always string literals. 1161626a879aSdrh */ 1162626a879aSdrh case TK_STRING: { 1163626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1164626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1165626a879aSdrh } 1166626a879aSdrh /* A lone identifier is the name of a column. 1167626a879aSdrh */ 1168626a879aSdrh case TK_ID: { 1169626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1170626a879aSdrh return 1; 1171626a879aSdrh } 1172626a879aSdrh 1173626a879aSdrh /* A table name and column name: ID.ID 1174626a879aSdrh ** Or a database, table and column: ID.ID.ID 1175626a879aSdrh */ 1176626a879aSdrh case TK_DOT: { 1177626a879aSdrh Token *pColumn; 1178626a879aSdrh Token *pTable; 1179626a879aSdrh Token *pDb; 1180626a879aSdrh Expr *pRight; 1181626a879aSdrh 1182b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1183626a879aSdrh pRight = pExpr->pRight; 1184626a879aSdrh if( pRight->op==TK_ID ){ 1185626a879aSdrh pDb = 0; 1186626a879aSdrh pTable = &pExpr->pLeft->token; 1187626a879aSdrh pColumn = &pRight->token; 1188626a879aSdrh }else{ 1189626a879aSdrh assert( pRight->op==TK_DOT ); 1190626a879aSdrh pDb = &pExpr->pLeft->token; 1191626a879aSdrh pTable = &pRight->pLeft->token; 1192626a879aSdrh pColumn = &pRight->pRight->token; 1193626a879aSdrh } 1194626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1195626a879aSdrh return 1; 1196626a879aSdrh } 1197626a879aSdrh 1198626a879aSdrh /* Resolve function names 1199626a879aSdrh */ 1200b71090fdSdrh case TK_CONST_FUNC: 1201626a879aSdrh case TK_FUNCTION: { 1202626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1203626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1204626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1205626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1206626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1207626a879aSdrh int i; 12085169bbc6Sdrh int auth; /* Authorization to use the function */ 1209626a879aSdrh int nId; /* Number of characters in function name */ 1210626a879aSdrh const char *zId; /* The function name. */ 121173b211abSdrh FuncDef *pDef; /* Information about the function */ 121214db2665Sdanielk1977 int enc = ENC(pParse->db); /* The database encoding */ 1213626a879aSdrh 12142646da7eSdrh zId = (char*)pExpr->token.z; 1215b71090fdSdrh nId = pExpr->token.n; 1216626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1217626a879aSdrh if( pDef==0 ){ 1218626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1219626a879aSdrh if( pDef==0 ){ 1220626a879aSdrh no_such_func = 1; 1221626a879aSdrh }else{ 1222626a879aSdrh wrong_num_args = 1; 1223626a879aSdrh } 1224626a879aSdrh }else{ 1225626a879aSdrh is_agg = pDef->xFunc==0; 1226626a879aSdrh } 12272fca7fefSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 12285169bbc6Sdrh if( pDef ){ 12295169bbc6Sdrh auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); 12305169bbc6Sdrh if( auth!=SQLITE_OK ){ 12315169bbc6Sdrh if( auth==SQLITE_DENY ){ 12325169bbc6Sdrh sqlite3ErrorMsg(pParse, "not authorized to use function: %s", 12335169bbc6Sdrh pDef->zName); 12345169bbc6Sdrh pNC->nErr++; 12355169bbc6Sdrh } 12365169bbc6Sdrh pExpr->op = TK_NULL; 12375169bbc6Sdrh return 1; 12385169bbc6Sdrh } 12395169bbc6Sdrh } 1240b8b14219Sdrh #endif 1241626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1242626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1243626a879aSdrh pNC->nErr++; 1244626a879aSdrh is_agg = 0; 1245626a879aSdrh }else if( no_such_func ){ 1246626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1247626a879aSdrh pNC->nErr++; 1248626a879aSdrh }else if( wrong_num_args ){ 1249626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1250626a879aSdrh nId, zId); 1251626a879aSdrh pNC->nErr++; 1252626a879aSdrh } 1253626a879aSdrh if( is_agg ){ 1254626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1255626a879aSdrh pNC->hasAgg = 1; 1256626a879aSdrh } 125773b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1258626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 125973b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1260626a879aSdrh } 126173b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1262626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1263626a879aSdrh ** type of the function 1264626a879aSdrh */ 1265626a879aSdrh return is_agg; 1266626a879aSdrh } 1267b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1268b3bce662Sdanielk1977 case TK_SELECT: 1269b3bce662Sdanielk1977 case TK_EXISTS: 1270b3bce662Sdanielk1977 #endif 1271b3bce662Sdanielk1977 case TK_IN: { 1272b3bce662Sdanielk1977 if( pExpr->pSelect ){ 12738a9f38feSdrh int nRef = pNC->nRef; 127406f6541eSdrh #ifndef SQLITE_OMIT_CHECK 127506f6541eSdrh if( pNC->isCheck ){ 127606f6541eSdrh sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); 127706f6541eSdrh } 127806f6541eSdrh #endif 1279b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1280b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1281b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1282b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1283b3bce662Sdanielk1977 } 1284b3bce662Sdanielk1977 } 12854284fb07Sdrh break; 1286b3bce662Sdanielk1977 } 12874284fb07Sdrh #ifndef SQLITE_OMIT_CHECK 12884284fb07Sdrh case TK_VARIABLE: { 12894284fb07Sdrh if( pNC->isCheck ){ 12904284fb07Sdrh sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); 12914284fb07Sdrh } 12924284fb07Sdrh break; 12934284fb07Sdrh } 12944284fb07Sdrh #endif 1295626a879aSdrh } 1296626a879aSdrh return 0; 1297626a879aSdrh } 1298626a879aSdrh 1299626a879aSdrh /* 1300cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1301967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1302aacc543eSdrh ** index to the table in the table list and a column offset. The 1303aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1304aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1305832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1306aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1307aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1308aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1309aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1310aacc543eSdrh ** alias for ROWID. 131119a775c2Sdrh ** 1312626a879aSdrh ** Also resolve function names and check the functions for proper 1313626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1314626a879aSdrh ** have the correct number of arguments. Leave an error message 1315626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1316626a879aSdrh ** 131773b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 131873b211abSdrh ** property on the expression. 1319626a879aSdrh */ 1320626a879aSdrh int sqlite3ExprResolveNames( 1321b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1322b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1323626a879aSdrh ){ 132413449892Sdrh int savedHasAgg; 132573b211abSdrh if( pExpr==0 ) return 0; 132613449892Sdrh savedHasAgg = pNC->hasAgg; 132713449892Sdrh pNC->hasAgg = 0; 1328b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1329b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 133073b211abSdrh ExprSetProperty(pExpr, EP_Error); 133173b211abSdrh } 133213449892Sdrh if( pNC->hasAgg ){ 133313449892Sdrh ExprSetProperty(pExpr, EP_Agg); 133413449892Sdrh }else if( savedHasAgg ){ 133513449892Sdrh pNC->hasAgg = 1; 133613449892Sdrh } 133773b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1338626a879aSdrh } 1339626a879aSdrh 13401398ad36Sdrh /* 13411398ad36Sdrh ** A pointer instance of this structure is used to pass information 13421398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 13431398ad36Sdrh */ 13441398ad36Sdrh typedef struct QueryCoder QueryCoder; 13451398ad36Sdrh struct QueryCoder { 13461398ad36Sdrh Parse *pParse; /* The parsing context */ 13471398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 13481398ad36Sdrh }; 13491398ad36Sdrh 1350626a879aSdrh 1351626a879aSdrh /* 13529cbe6352Sdrh ** Generate code for scalar subqueries used as an expression 13539cbe6352Sdrh ** and IN operators. Examples: 1354626a879aSdrh ** 13559cbe6352Sdrh ** (SELECT a FROM b) -- subquery 13569cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 13579cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 13589cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 1359fef5208cSdrh ** 13609cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 13619cbe6352Sdrh ** operator or subquery. 1362cce7d176Sdrh */ 136351522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1364b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 136557dbd7b3Sdrh int testAddr = 0; /* One-time test address */ 1366b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1367b3bce662Sdanielk1977 if( v==0 ) return; 1368b3bce662Sdanielk1977 136957dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 137057dbd7b3Sdrh ** if any of the following is true: 137157dbd7b3Sdrh ** 137257dbd7b3Sdrh ** * The right-hand side is a correlated subquery 137357dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 137457dbd7b3Sdrh ** * We are inside a trigger 137557dbd7b3Sdrh ** 137657dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 137757dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1378b3bce662Sdanielk1977 */ 1379b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 1380b3bce662Sdanielk1977 int mem = pParse->nMem++; 1381b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); 138257dbd7b3Sdrh testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0); 13839e12800dSdanielk1977 assert( testAddr>0 || sqlite3MallocFailed() ); 1384d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, mem); 1385b3bce662Sdanielk1977 } 1386b3bce662Sdanielk1977 1387cce7d176Sdrh switch( pExpr->op ){ 1388fef5208cSdrh case TK_IN: { 1389e014a838Sdanielk1977 char affinity; 1390d3d39e93Sdrh KeyInfo keyInfo; 1391b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 1392d3d39e93Sdrh 1393bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1394e014a838Sdanielk1977 1395e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 139657dbd7b3Sdrh ** expression it is handled the same way. A virtual table is 1397e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1398e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1399fef5208cSdrh ** 1400e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1401e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1402e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1403e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1404e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1405e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1406e014a838Sdanielk1977 ** is used. 1407fef5208cSdrh */ 1408832508b7Sdrh pExpr->iTable = pParse->nTab++; 1409b9bb7c18Sdrh addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0); 1410d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1411d3d39e93Sdrh keyInfo.nField = 1; 1412f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 1413e014a838Sdanielk1977 1414e014a838Sdanielk1977 if( pExpr->pSelect ){ 1415e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1416e014a838Sdanielk1977 ** 1417e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1418e014a838Sdanielk1977 ** table allocated and opened above. 1419e014a838Sdanielk1977 */ 1420e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 1421be5c89acSdrh ExprList *pEList; 1422e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 142394ccde58Sdrh if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){ 142494ccde58Sdrh return; 142594ccde58Sdrh } 1426be5c89acSdrh pEList = pExpr->pSelect->pEList; 1427be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 14287cedc8d4Sdanielk1977 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 1429be5c89acSdrh pEList->a[0].pExpr); 14300202b29eSdanielk1977 } 1431fef5208cSdrh }else if( pExpr->pList ){ 1432fef5208cSdrh /* Case 2: expr IN (exprlist) 1433fef5208cSdrh ** 1434e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1435e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1436e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1437e014a838Sdanielk1977 ** a column, use numeric affinity. 1438fef5208cSdrh */ 1439e014a838Sdanielk1977 int i; 144057dbd7b3Sdrh ExprList *pList = pExpr->pList; 144157dbd7b3Sdrh struct ExprList_item *pItem; 144257dbd7b3Sdrh 1443e014a838Sdanielk1977 if( !affinity ){ 14448159a35fSdrh affinity = SQLITE_AFF_NONE; 1445e014a838Sdanielk1977 } 14460202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1447e014a838Sdanielk1977 1448e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 144957dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 145057dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1451e014a838Sdanielk1977 145257dbd7b3Sdrh /* If the expression is not constant then we will need to 145357dbd7b3Sdrh ** disable the test that was generated above that makes sure 145457dbd7b3Sdrh ** this code only executes once. Because for a non-constant 145557dbd7b3Sdrh ** expression we need to rerun this code each time. 145657dbd7b3Sdrh */ 14576c30be8eSdrh if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){ 1458f8875400Sdrh sqlite3VdbeChangeToNoop(v, testAddr-1, 3); 145957dbd7b3Sdrh testAddr = 0; 14604794b980Sdrh } 1461e014a838Sdanielk1977 1462e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 14634adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 146494a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 1465f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0); 1466fef5208cSdrh } 1467fef5208cSdrh } 14680202b29eSdanielk1977 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 1469b3bce662Sdanielk1977 break; 1470fef5208cSdrh } 1471fef5208cSdrh 147251522cd3Sdrh case TK_EXISTS: 147319a775c2Sdrh case TK_SELECT: { 1474fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1475fef5208cSdrh ** value of this select in a memory cell and record the number 1476967e8b73Sdrh ** of the memory cell in iColumn. 1477fef5208cSdrh */ 14782646da7eSdrh static const Token one = { (u8*)"1", 0, 1 }; 147951522cd3Sdrh Select *pSel; 1480ec7429aeSdrh int iMem; 1481ec7429aeSdrh int sop; 14821398ad36Sdrh 1483ec7429aeSdrh pExpr->iColumn = iMem = pParse->nMem++; 148451522cd3Sdrh pSel = pExpr->pSelect; 148551522cd3Sdrh if( pExpr->op==TK_SELECT ){ 148651522cd3Sdrh sop = SRT_Mem; 1487ec7429aeSdrh sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0); 1488ec7429aeSdrh VdbeComment((v, "# Init subquery result")); 148951522cd3Sdrh }else{ 149051522cd3Sdrh sop = SRT_Exists; 1491ec7429aeSdrh sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem); 1492ec7429aeSdrh VdbeComment((v, "# Init EXISTS result")); 149351522cd3Sdrh } 1494ec7429aeSdrh sqlite3ExprDelete(pSel->pLimit); 1495ec7429aeSdrh pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one); 149694ccde58Sdrh if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){ 149794ccde58Sdrh return; 149894ccde58Sdrh } 1499b3bce662Sdanielk1977 break; 150019a775c2Sdrh } 1501cce7d176Sdrh } 1502b3bce662Sdanielk1977 150357dbd7b3Sdrh if( testAddr ){ 1504d654be80Sdrh sqlite3VdbeJumpHere(v, testAddr); 1505b3bce662Sdanielk1977 } 1506b3bce662Sdanielk1977 return; 1507cce7d176Sdrh } 150851522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1509cce7d176Sdrh 1510cce7d176Sdrh /* 1511fec19aadSdrh ** Generate an instruction that will put the integer describe by 1512fec19aadSdrh ** text z[0..n-1] on the stack. 1513fec19aadSdrh */ 1514fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1515fec19aadSdrh int i; 15166fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 15176fec0762Sdrh sqlite3VdbeAddOp(v, OP_Integer, i, 0); 15186fec0762Sdrh }else if( sqlite3FitsIn64Bits(z) ){ 151929dda4aeSdrh sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n); 1520fec19aadSdrh }else{ 1521fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1522fec19aadSdrh } 1523fec19aadSdrh } 1524fec19aadSdrh 1525945498f3Sdrh 1526945498f3Sdrh /* 1527945498f3Sdrh ** Generate code that will extract the iColumn-th column from 1528945498f3Sdrh ** table pTab and push that column value on the stack. There 1529945498f3Sdrh ** is an open cursor to pTab in iTable. If iColumn<0 then 1530945498f3Sdrh ** code is generated that extracts the rowid. 1531945498f3Sdrh */ 1532945498f3Sdrh void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){ 1533945498f3Sdrh if( iColumn<0 ){ 1534945498f3Sdrh int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; 1535945498f3Sdrh sqlite3VdbeAddOp(v, op, iTable, 0); 1536945498f3Sdrh }else if( pTab==0 ){ 1537945498f3Sdrh sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn); 1538945498f3Sdrh }else{ 1539945498f3Sdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 1540945498f3Sdrh sqlite3VdbeAddOp(v, op, iTable, iColumn); 1541945498f3Sdrh sqlite3ColumnDefault(v, pTab, iColumn); 1542945498f3Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 1543945498f3Sdrh if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ 1544945498f3Sdrh sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0); 1545945498f3Sdrh } 1546945498f3Sdrh #endif 1547945498f3Sdrh } 1548945498f3Sdrh } 1549945498f3Sdrh 1550fec19aadSdrh /* 1551cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 15521ccde15dSdrh ** expression and leave the result on the top of stack. 1553f2bc013cSdrh ** 1554f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1555f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1556f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1557f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1558f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1559cce7d176Sdrh */ 15604adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1561cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1562cce7d176Sdrh int op; 1563ffe07b2dSdrh int stackChng = 1; /* Amount of change to stack depth */ 1564ffe07b2dSdrh 15657977a17fSdanielk1977 if( v==0 ) return; 15667977a17fSdanielk1977 if( pExpr==0 ){ 1567f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 15687977a17fSdanielk1977 return; 15697977a17fSdanielk1977 } 1570f2bc013cSdrh op = pExpr->op; 1571f2bc013cSdrh switch( op ){ 157213449892Sdrh case TK_AGG_COLUMN: { 157313449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 157413449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 157513449892Sdrh if( !pAggInfo->directMode ){ 157613449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0); 157713449892Sdrh break; 157813449892Sdrh }else if( pAggInfo->useSortingIdx ){ 157913449892Sdrh sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx, 158013449892Sdrh pCol->iSorterColumn); 158113449892Sdrh break; 158213449892Sdrh } 158313449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 158413449892Sdrh } 1585967e8b73Sdrh case TK_COLUMN: { 1586ffe07b2dSdrh if( pExpr->iTable<0 ){ 1587ffe07b2dSdrh /* This only happens when coding check constraints */ 1588ffe07b2dSdrh assert( pParse->ckOffset>0 ); 1589ffe07b2dSdrh sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1); 1590c4a3c779Sdrh }else{ 1591945498f3Sdrh sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable); 15922282792aSdrh } 1593cce7d176Sdrh break; 1594cce7d176Sdrh } 1595cce7d176Sdrh case TK_INTEGER: { 15962646da7eSdrh codeInteger(v, (char*)pExpr->token.z, pExpr->token.n); 1597fec19aadSdrh break; 159851e9a445Sdrh } 1599fec19aadSdrh case TK_FLOAT: 1600fec19aadSdrh case TK_STRING: { 1601f2bc013cSdrh assert( TK_FLOAT==OP_Real ); 1602f2bc013cSdrh assert( TK_STRING==OP_String8 ); 1603d2687b77Sdrh sqlite3DequoteExpr(pExpr); 16042646da7eSdrh sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n); 1605cce7d176Sdrh break; 1606cce7d176Sdrh } 1607f0863fe5Sdrh case TK_NULL: { 1608f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1609f0863fe5Sdrh break; 1610f0863fe5Sdrh } 16115338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 1612c572ef7fSdanielk1977 case TK_BLOB: { 16136c8c6cecSdrh int n; 16146c8c6cecSdrh const char *z; 1615f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 16166c8c6cecSdrh n = pExpr->token.n - 3; 16172646da7eSdrh z = (char*)pExpr->token.z + 2; 16186c8c6cecSdrh assert( n>=0 ); 16196c8c6cecSdrh if( n==0 ){ 16206c8c6cecSdrh z = ""; 16216c8c6cecSdrh } 16226c8c6cecSdrh sqlite3VdbeOp3(v, op, 0, 0, z, n); 1623c572ef7fSdanielk1977 break; 1624c572ef7fSdanielk1977 } 16255338a5f7Sdanielk1977 #endif 162650457896Sdrh case TK_VARIABLE: { 16274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1628895d7472Sdrh if( pExpr->token.n>1 ){ 16292646da7eSdrh sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n); 1630895d7472Sdrh } 163150457896Sdrh break; 163250457896Sdrh } 16334e0cff60Sdrh case TK_REGISTER: { 16344e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); 16354e0cff60Sdrh break; 16364e0cff60Sdrh } 1637487e262fSdrh #ifndef SQLITE_OMIT_CAST 1638487e262fSdrh case TK_CAST: { 1639487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 1640f0113000Sdanielk1977 int aff, to_op; 1641487e262fSdrh sqlite3ExprCode(pParse, pExpr->pLeft); 16428a51256cSdrh aff = sqlite3AffinityType(&pExpr->token); 1643f0113000Sdanielk1977 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; 1644f0113000Sdanielk1977 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); 1645f0113000Sdanielk1977 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); 1646f0113000Sdanielk1977 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); 1647f0113000Sdanielk1977 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); 1648f0113000Sdanielk1977 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); 1649f0113000Sdanielk1977 sqlite3VdbeAddOp(v, to_op, 0, 0); 1650ffe07b2dSdrh stackChng = 0; 1651487e262fSdrh break; 1652487e262fSdrh } 1653487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 1654c9b84a1fSdrh case TK_LT: 1655c9b84a1fSdrh case TK_LE: 1656c9b84a1fSdrh case TK_GT: 1657c9b84a1fSdrh case TK_GE: 1658c9b84a1fSdrh case TK_NE: 1659c9b84a1fSdrh case TK_EQ: { 1660f2bc013cSdrh assert( TK_LT==OP_Lt ); 1661f2bc013cSdrh assert( TK_LE==OP_Le ); 1662f2bc013cSdrh assert( TK_GT==OP_Gt ); 1663f2bc013cSdrh assert( TK_GE==OP_Ge ); 1664f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1665f2bc013cSdrh assert( TK_NE==OP_Ne ); 1666a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1667a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1668be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 1669ffe07b2dSdrh stackChng = -1; 1670a37cdde0Sdanielk1977 break; 1671c9b84a1fSdrh } 1672cce7d176Sdrh case TK_AND: 1673cce7d176Sdrh case TK_OR: 1674cce7d176Sdrh case TK_PLUS: 1675cce7d176Sdrh case TK_STAR: 1676cce7d176Sdrh case TK_MINUS: 1677bf4133cbSdrh case TK_REM: 1678bf4133cbSdrh case TK_BITAND: 1679bf4133cbSdrh case TK_BITOR: 168017c40294Sdrh case TK_SLASH: 1681bf4133cbSdrh case TK_LSHIFT: 1682855eb1cfSdrh case TK_RSHIFT: 16830040077dSdrh case TK_CONCAT: { 1684f2bc013cSdrh assert( TK_AND==OP_And ); 1685f2bc013cSdrh assert( TK_OR==OP_Or ); 1686f2bc013cSdrh assert( TK_PLUS==OP_Add ); 1687f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 1688f2bc013cSdrh assert( TK_REM==OP_Remainder ); 1689f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 1690f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 1691f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 1692f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 1693f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 1694f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 16954adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 16964adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1697855eb1cfSdrh sqlite3VdbeAddOp(v, op, 0, 0); 1698ffe07b2dSdrh stackChng = -1; 16990040077dSdrh break; 17000040077dSdrh } 1701cce7d176Sdrh case TK_UMINUS: { 1702fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1703fec19aadSdrh assert( pLeft ); 1704fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1705fec19aadSdrh Token *p = &pLeft->token; 17069267bdceSdrh char *z = sqlite3MPrintf("-%.*s", p->n, p->z); 1707fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1708fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1709e6840900Sdrh }else{ 1710fec19aadSdrh codeInteger(v, z, p->n+1); 1711e6840900Sdrh } 17126e142f54Sdrh sqliteFree(z); 17136e142f54Sdrh break; 17146e142f54Sdrh } 17151ccde15dSdrh /* Fall through into TK_NOT */ 17166e142f54Sdrh } 1717bf4133cbSdrh case TK_BITNOT: 17186e142f54Sdrh case TK_NOT: { 1719f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 1720f2bc013cSdrh assert( TK_NOT==OP_Not ); 17214adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 17224adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1723ffe07b2dSdrh stackChng = 0; 1724cce7d176Sdrh break; 1725cce7d176Sdrh } 1726cce7d176Sdrh case TK_ISNULL: 1727cce7d176Sdrh case TK_NOTNULL: { 1728cce7d176Sdrh int dest; 1729f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1730f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 17314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 17324adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 17334adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 17344adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 17354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1736ffe07b2dSdrh stackChng = 0; 1737a37cdde0Sdanielk1977 break; 1738f2bc013cSdrh } 17392282792aSdrh case TK_AGG_FUNCTION: { 174013449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 17417e56e711Sdrh if( pInfo==0 ){ 17427e56e711Sdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", 17437e56e711Sdrh &pExpr->span); 17447e56e711Sdrh }else{ 174513449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0); 17467e56e711Sdrh } 17472282792aSdrh break; 17482282792aSdrh } 1749b71090fdSdrh case TK_CONST_FUNC: 1750cce7d176Sdrh case TK_FUNCTION: { 1751cce7d176Sdrh ExprList *pList = pExpr->pList; 175289425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 17530bce8354Sdrh FuncDef *pDef; 17544b59ab5eSdrh int nId; 17554b59ab5eSdrh const char *zId; 175613449892Sdrh int constMask = 0; 1757682f68b0Sdanielk1977 int i; 175814db2665Sdanielk1977 u8 enc = ENC(pParse->db); 1759dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 17602646da7eSdrh zId = (char*)pExpr->token.z; 1761b71090fdSdrh nId = pExpr->token.n; 1762d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 17630bce8354Sdrh assert( pDef!=0 ); 1764f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1765b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 1766a43fa227Sdrh /* Possibly overload the function if the first argument is 1767a43fa227Sdrh ** a virtual table column. 1768a43fa227Sdrh ** 1769a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 1770a43fa227Sdrh ** second argument, not the first, as the argument to test to 1771a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 1772a43fa227Sdrh ** the left operand of infix functions (the operand we want to 1773a43fa227Sdrh ** control overloading) ends up as the second argument to the 1774a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 1775a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 1776a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 1777a43fa227Sdrh */ 17786a03a1c5Sdrh if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ 17796a03a1c5Sdrh pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr); 17806a03a1c5Sdrh }else if( nExpr>0 ){ 1781b7f6f68fSdrh pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr); 1782b7f6f68fSdrh } 1783b7f6f68fSdrh #endif 1784682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 1785d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 178613449892Sdrh constMask |= (1<<i); 1787d02eb1fdSdanielk1977 } 1788dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 1789dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 1790dc1bdc4fSdanielk1977 } 1791dc1bdc4fSdanielk1977 } 1792dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 1793dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 1794d8123366Sdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 1795682f68b0Sdanielk1977 } 179613449892Sdrh sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF); 1797ffe07b2dSdrh stackChng = 1-nExpr; 17986ec2733bSdrh break; 17996ec2733bSdrh } 1800fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1801fe2093d7Sdrh case TK_EXISTS: 180219a775c2Sdrh case TK_SELECT: { 180341714d6fSdrh if( pExpr->iColumn==0 ){ 1804b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 180541714d6fSdrh } 18064adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1807ad6d9460Sdrh VdbeComment((v, "# load subquery result")); 180819a775c2Sdrh break; 180919a775c2Sdrh } 1810fef5208cSdrh case TK_IN: { 1811fef5208cSdrh int addr; 181294a11211Sdrh char affinity; 1813afa5f680Sdrh int ckOffset = pParse->ckOffset; 1814b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 1815e014a838Sdanielk1977 1816e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1817e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1818ededfd5eSdanielk1977 ** P3 of OP_MakeRecord. 1819e014a838Sdanielk1977 */ 182094a11211Sdrh affinity = comparisonAffinity(pExpr); 1821e014a838Sdanielk1977 18224adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1823afa5f680Sdrh pParse->ckOffset = ckOffset+1; 1824e014a838Sdanielk1977 1825e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1826e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1827e014a838Sdanielk1977 */ 18284adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18294adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1830e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 18314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 1832f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1833e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 183494a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ 1835e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1836e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1837e014a838Sdanielk1977 1838fef5208cSdrh break; 1839fef5208cSdrh } 184093758c8dSdanielk1977 #endif 1841fef5208cSdrh case TK_BETWEEN: { 1842be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1843be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 1844be5c89acSdrh Expr *pRight = pLItem->pExpr; 1845be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 18464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1847be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1848be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 18494adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1850be5c89acSdrh pLItem++; 1851be5c89acSdrh pRight = pLItem->pExpr; 1852be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1853be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 18544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1855fef5208cSdrh break; 1856fef5208cSdrh } 185751e9a445Sdrh case TK_UPLUS: 1858a2e00042Sdrh case TK_AS: { 18594adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1860ffe07b2dSdrh stackChng = 0; 1861a2e00042Sdrh break; 1862a2e00042Sdrh } 186317a7f8ddSdrh case TK_CASE: { 186417a7f8ddSdrh int expr_end_label; 1865f5905aa7Sdrh int jumpInst; 1866f5905aa7Sdrh int nExpr; 186717a7f8ddSdrh int i; 1868be5c89acSdrh ExprList *pEList; 1869be5c89acSdrh struct ExprList_item *aListelem; 187017a7f8ddSdrh 187117a7f8ddSdrh assert(pExpr->pList); 187217a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 187317a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1874be5c89acSdrh pEList = pExpr->pList; 1875be5c89acSdrh aListelem = pEList->a; 1876be5c89acSdrh nExpr = pEList->nExpr; 18774adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 187817a7f8ddSdrh if( pExpr->pLeft ){ 18794adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1880cce7d176Sdrh } 1881f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 1882be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr); 188317a7f8ddSdrh if( pExpr->pLeft ){ 18844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1885be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 1886be5c89acSdrh OP_Ne, 0, 1); 18874adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1888f5905aa7Sdrh }else{ 18894adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 189017a7f8ddSdrh } 1891be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr); 18924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 1893d654be80Sdrh sqlite3VdbeJumpHere(v, jumpInst); 189417a7f8ddSdrh } 1895f570f011Sdrh if( pExpr->pLeft ){ 18964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1897f570f011Sdrh } 189817a7f8ddSdrh if( pExpr->pRight ){ 18994adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 190017a7f8ddSdrh }else{ 1901f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 190217a7f8ddSdrh } 19034adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 19046f34903eSdanielk1977 break; 19056f34903eSdanielk1977 } 19065338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 19076f34903eSdanielk1977 case TK_RAISE: { 19086f34903eSdanielk1977 if( !pParse->trigStack ){ 19094adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1910da93d238Sdrh "RAISE() may only be used within a trigger-program"); 19116f34903eSdanielk1977 return; 19126f34903eSdanielk1977 } 1913ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 1914ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 19156f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 1916ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 1917d2687b77Sdrh sqlite3DequoteExpr(pExpr); 19184adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 19192646da7eSdrh (char*)pExpr->token.z, pExpr->token.n); 19206f34903eSdanielk1977 } else { 19216f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 1922344737f6Sdrh sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 1923ad6d9460Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 1924ad6d9460Sdrh VdbeComment((v, "# raise(IGNORE)")); 19256f34903eSdanielk1977 } 1926ffe07b2dSdrh stackChng = 0; 1927ffe07b2dSdrh break; 192817a7f8ddSdrh } 19295338a5f7Sdanielk1977 #endif 1930ffe07b2dSdrh } 1931ffe07b2dSdrh 1932ffe07b2dSdrh if( pParse->ckOffset ){ 1933ffe07b2dSdrh pParse->ckOffset += stackChng; 1934ffe07b2dSdrh assert( pParse->ckOffset ); 193517a7f8ddSdrh } 1936cce7d176Sdrh } 1937cce7d176Sdrh 193893758c8dSdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 1939cce7d176Sdrh /* 194025303780Sdrh ** Generate code that evalutes the given expression and leaves the result 194125303780Sdrh ** on the stack. See also sqlite3ExprCode(). 194225303780Sdrh ** 194325303780Sdrh ** This routine might also cache the result and modify the pExpr tree 194425303780Sdrh ** so that it will make use of the cached result on subsequent evaluations 194525303780Sdrh ** rather than evaluate the whole expression again. Trivial expressions are 194625303780Sdrh ** not cached. If the expression is cached, its result is stored in a 194725303780Sdrh ** memory location. 194825303780Sdrh */ 194925303780Sdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ 195025303780Sdrh Vdbe *v = pParse->pVdbe; 195125303780Sdrh int iMem; 195225303780Sdrh int addr1, addr2; 195325303780Sdrh if( v==0 ) return; 195425303780Sdrh addr1 = sqlite3VdbeCurrentAddr(v); 195525303780Sdrh sqlite3ExprCode(pParse, pExpr); 195625303780Sdrh addr2 = sqlite3VdbeCurrentAddr(v); 195725303780Sdrh if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){ 195825303780Sdrh iMem = pExpr->iTable = pParse->nMem++; 195925303780Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); 196025303780Sdrh pExpr->op = TK_REGISTER; 196125303780Sdrh } 196225303780Sdrh } 196393758c8dSdanielk1977 #endif 196425303780Sdrh 196525303780Sdrh /* 1966268380caSdrh ** Generate code that pushes the value of every element of the given 1967f9b596ebSdrh ** expression list onto the stack. 1968268380caSdrh ** 1969268380caSdrh ** Return the number of elements pushed onto the stack. 1970268380caSdrh */ 19714adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1972268380caSdrh Parse *pParse, /* Parsing context */ 1973f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1974268380caSdrh ){ 1975268380caSdrh struct ExprList_item *pItem; 1976268380caSdrh int i, n; 1977268380caSdrh if( pList==0 ) return 0; 1978268380caSdrh n = pList->nExpr; 1979c182d163Sdrh for(pItem=pList->a, i=n; i>0; i--, pItem++){ 19804adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1981268380caSdrh } 1982f9b596ebSdrh return n; 1983268380caSdrh } 1984268380caSdrh 1985268380caSdrh /* 1986cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1987cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1988cce7d176Sdrh ** continues straight thru if the expression is false. 1989f5905aa7Sdrh ** 1990f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1991f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1992f2bc013cSdrh ** 1993f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1994f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1995f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1996f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1997f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1998cce7d176Sdrh */ 19994adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2000cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2001cce7d176Sdrh int op = 0; 2002ffe07b2dSdrh int ckOffset = pParse->ckOffset; 2003daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2004f2bc013cSdrh op = pExpr->op; 2005f2bc013cSdrh switch( op ){ 2006cce7d176Sdrh case TK_AND: { 20074adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 20084adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 20094adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 20104adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2011cce7d176Sdrh break; 2012cce7d176Sdrh } 2013cce7d176Sdrh case TK_OR: { 20144adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 20154adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 2016cce7d176Sdrh break; 2017cce7d176Sdrh } 2018cce7d176Sdrh case TK_NOT: { 20194adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 2020cce7d176Sdrh break; 2021cce7d176Sdrh } 2022cce7d176Sdrh case TK_LT: 2023cce7d176Sdrh case TK_LE: 2024cce7d176Sdrh case TK_GT: 2025cce7d176Sdrh case TK_GE: 2026cce7d176Sdrh case TK_NE: 20270ac65892Sdrh case TK_EQ: { 2028f2bc013cSdrh assert( TK_LT==OP_Lt ); 2029f2bc013cSdrh assert( TK_LE==OP_Le ); 2030f2bc013cSdrh assert( TK_GT==OP_Gt ); 2031f2bc013cSdrh assert( TK_GE==OP_Ge ); 2032f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2033f2bc013cSdrh assert( TK_NE==OP_Ne ); 20344adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 20354adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 2036be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 2037cce7d176Sdrh break; 2038cce7d176Sdrh } 2039cce7d176Sdrh case TK_ISNULL: 2040cce7d176Sdrh case TK_NOTNULL: { 2041f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2042f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 20434adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 20444adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 2045cce7d176Sdrh break; 2046cce7d176Sdrh } 2047fef5208cSdrh case TK_BETWEEN: { 20480202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 20490202b29eSdanielk1977 ** 20500202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 20510202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 20520202b29eSdanielk1977 ** 3 ... 20530202b29eSdanielk1977 */ 2054f5905aa7Sdrh int addr; 2055be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2056be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 2057be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 20584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 2059be5c89acSdrh sqlite3ExprCode(pParse, pRight); 2060be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 20610202b29eSdanielk1977 2062be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 2063be5c89acSdrh sqlite3ExprCode(pParse, pRight); 2064be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 20650202b29eSdanielk1977 20664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 2067d654be80Sdrh sqlite3VdbeJumpHere(v, addr); 20684adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 2069fef5208cSdrh break; 2070fef5208cSdrh } 2071cce7d176Sdrh default: { 20724adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 20734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 2074cce7d176Sdrh break; 2075cce7d176Sdrh } 2076cce7d176Sdrh } 2077ffe07b2dSdrh pParse->ckOffset = ckOffset; 2078cce7d176Sdrh } 2079cce7d176Sdrh 2080cce7d176Sdrh /* 208166b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 2082cce7d176Sdrh ** to the label "dest" if the expression is false but execution 2083cce7d176Sdrh ** continues straight thru if the expression is true. 2084f5905aa7Sdrh ** 2085f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 2086f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 2087cce7d176Sdrh */ 20884adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2089cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2090cce7d176Sdrh int op = 0; 2091ffe07b2dSdrh int ckOffset = pParse->ckOffset; 2092daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2093f2bc013cSdrh 2094f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 2095f2bc013cSdrh ** 2096f2bc013cSdrh ** pExpr->op op 2097f2bc013cSdrh ** --------- ---------- 2098f2bc013cSdrh ** TK_ISNULL OP_NotNull 2099f2bc013cSdrh ** TK_NOTNULL OP_IsNull 2100f2bc013cSdrh ** TK_NE OP_Eq 2101f2bc013cSdrh ** TK_EQ OP_Ne 2102f2bc013cSdrh ** TK_GT OP_Le 2103f2bc013cSdrh ** TK_LE OP_Gt 2104f2bc013cSdrh ** TK_GE OP_Lt 2105f2bc013cSdrh ** TK_LT OP_Ge 2106f2bc013cSdrh ** 2107f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 2108f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 2109f2bc013cSdrh ** can compute the mapping above using the following expression. 2110f2bc013cSdrh ** Assert()s verify that the computation is correct. 2111f2bc013cSdrh */ 2112f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 2113f2bc013cSdrh 2114f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 2115f2bc013cSdrh */ 2116f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 2117f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 2118f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 2119f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 2120f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 2121f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 2122f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 2123f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 2124f2bc013cSdrh 2125cce7d176Sdrh switch( pExpr->op ){ 2126cce7d176Sdrh case TK_AND: { 21274adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 21284adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 2129cce7d176Sdrh break; 2130cce7d176Sdrh } 2131cce7d176Sdrh case TK_OR: { 21324adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 21334adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 21344adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 21354adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2136cce7d176Sdrh break; 2137cce7d176Sdrh } 2138cce7d176Sdrh case TK_NOT: { 21394adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 2140cce7d176Sdrh break; 2141cce7d176Sdrh } 2142cce7d176Sdrh case TK_LT: 2143cce7d176Sdrh case TK_LE: 2144cce7d176Sdrh case TK_GT: 2145cce7d176Sdrh case TK_GE: 2146cce7d176Sdrh case TK_NE: 2147cce7d176Sdrh case TK_EQ: { 21484adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 21494adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 2150be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 2151cce7d176Sdrh break; 2152cce7d176Sdrh } 2153cce7d176Sdrh case TK_ISNULL: 2154cce7d176Sdrh case TK_NOTNULL: { 21554adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 21564adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 2157cce7d176Sdrh break; 2158cce7d176Sdrh } 2159fef5208cSdrh case TK_BETWEEN: { 21600202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 21610202b29eSdanielk1977 ** 21620202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 21630202b29eSdanielk1977 ** 2 GOTO <dest> 21640202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 21650202b29eSdanielk1977 */ 2166fef5208cSdrh int addr; 2167be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2168be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 2169be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 21704adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 2171be5c89acSdrh sqlite3ExprCode(pParse, pRight); 21724adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 2173be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 2174be5c89acSdrh 21754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 21764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 2177be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 2178be5c89acSdrh sqlite3ExprCode(pParse, pRight); 2179be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 2180fef5208cSdrh break; 2181fef5208cSdrh } 2182cce7d176Sdrh default: { 21834adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 21844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 2185cce7d176Sdrh break; 2186cce7d176Sdrh } 2187cce7d176Sdrh } 2188ffe07b2dSdrh pParse->ckOffset = ckOffset; 2189cce7d176Sdrh } 21902282792aSdrh 21912282792aSdrh /* 21922282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 21932282792aSdrh ** if they are identical and return FALSE if they differ in any way. 2194d40aab0eSdrh ** 2195d40aab0eSdrh ** Sometimes this routine will return FALSE even if the two expressions 2196d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 2197d40aab0eSdrh ** identical, we return FALSE just to be safe. So if this routine 2198d40aab0eSdrh ** returns false, then you do not really know for certain if the two 2199d40aab0eSdrh ** expressions are the same. But if you get a TRUE return, then you 2200d40aab0eSdrh ** can be sure the expressions are the same. In the places where 2201d40aab0eSdrh ** this routine is used, it does not hurt to get an extra FALSE - that 2202d40aab0eSdrh ** just might result in some slightly slower code. But returning 2203d40aab0eSdrh ** an incorrect TRUE could lead to a malfunction. 22042282792aSdrh */ 22054adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 22062282792aSdrh int i; 22074b202ae2Sdanielk1977 if( pA==0||pB==0 ){ 22084b202ae2Sdanielk1977 return pB==pA; 22092282792aSdrh } 22102282792aSdrh if( pA->op!=pB->op ) return 0; 2211fd357974Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; 22124adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 22134adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 22142282792aSdrh if( pA->pList ){ 22152282792aSdrh if( pB->pList==0 ) return 0; 22162282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 22172282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 22184adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 22192282792aSdrh return 0; 22202282792aSdrh } 22212282792aSdrh } 22222282792aSdrh }else if( pB->pList ){ 22232282792aSdrh return 0; 22242282792aSdrh } 22252282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 22262f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 2227dd73521bSdrh if( pA->op!=TK_COLUMN && pA->token.z ){ 22282282792aSdrh if( pB->token.z==0 ) return 0; 22296977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 22302646da7eSdrh if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ 22312646da7eSdrh return 0; 22322646da7eSdrh } 22332282792aSdrh } 22342282792aSdrh return 1; 22352282792aSdrh } 22362282792aSdrh 223713449892Sdrh 22382282792aSdrh /* 223913449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 224013449892Sdrh ** the new element. Return a negative number if malloc fails. 22412282792aSdrh */ 224213449892Sdrh static int addAggInfoColumn(AggInfo *pInfo){ 224313449892Sdrh int i; 2244cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 2245cf643729Sdrh pInfo->aCol, 2246cf643729Sdrh sizeof(pInfo->aCol[0]), 2247cf643729Sdrh 3, 2248cf643729Sdrh &pInfo->nColumn, 2249cf643729Sdrh &pInfo->nColumnAlloc, 2250cf643729Sdrh &i 2251cf643729Sdrh ); 225213449892Sdrh return i; 22532282792aSdrh } 225413449892Sdrh 225513449892Sdrh /* 225613449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 225713449892Sdrh ** the new element. Return a negative number if malloc fails. 225813449892Sdrh */ 225913449892Sdrh static int addAggInfoFunc(AggInfo *pInfo){ 226013449892Sdrh int i; 2261cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 2262cf643729Sdrh pInfo->aFunc, 2263cf643729Sdrh sizeof(pInfo->aFunc[0]), 2264cf643729Sdrh 3, 2265cf643729Sdrh &pInfo->nFunc, 2266cf643729Sdrh &pInfo->nFuncAlloc, 2267cf643729Sdrh &i 2268cf643729Sdrh ); 226913449892Sdrh return i; 22702282792aSdrh } 22712282792aSdrh 22722282792aSdrh /* 2273626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 2274626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 2275626a879aSdrh ** for additional information. 22762282792aSdrh ** 2277626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 22782282792aSdrh */ 2279626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 22802282792aSdrh int i; 2281a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 2282a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 2283a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 228413449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 228513449892Sdrh 22862282792aSdrh 22872282792aSdrh switch( pExpr->op ){ 228889c69d00Sdrh case TK_AGG_COLUMN: 2289967e8b73Sdrh case TK_COLUMN: { 229013449892Sdrh /* Check to see if the column is in one of the tables in the FROM 229113449892Sdrh ** clause of the aggregate query */ 229213449892Sdrh if( pSrcList ){ 229313449892Sdrh struct SrcList_item *pItem = pSrcList->a; 229413449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 229513449892Sdrh struct AggInfo_col *pCol; 229613449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 229713449892Sdrh /* If we reach this point, it means that pExpr refers to a table 229813449892Sdrh ** that is in the FROM clause of the aggregate query. 229913449892Sdrh ** 230013449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 230113449892Sdrh ** is not an entry there already. 230213449892Sdrh */ 23037f906d63Sdrh int k; 230413449892Sdrh pCol = pAggInfo->aCol; 23057f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 230613449892Sdrh if( pCol->iTable==pExpr->iTable && 230713449892Sdrh pCol->iColumn==pExpr->iColumn ){ 23082282792aSdrh break; 23092282792aSdrh } 23102282792aSdrh } 23117f906d63Sdrh if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){ 23127f906d63Sdrh pCol = &pAggInfo->aCol[k]; 23130817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 231413449892Sdrh pCol->iTable = pExpr->iTable; 231513449892Sdrh pCol->iColumn = pExpr->iColumn; 231613449892Sdrh pCol->iMem = pParse->nMem++; 231713449892Sdrh pCol->iSorterColumn = -1; 23185774b806Sdrh pCol->pExpr = pExpr; 231913449892Sdrh if( pAggInfo->pGroupBy ){ 232013449892Sdrh int j, n; 232113449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 232213449892Sdrh struct ExprList_item *pTerm = pGB->a; 232313449892Sdrh n = pGB->nExpr; 232413449892Sdrh for(j=0; j<n; j++, pTerm++){ 232513449892Sdrh Expr *pE = pTerm->pExpr; 232613449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 232713449892Sdrh pE->iColumn==pExpr->iColumn ){ 232813449892Sdrh pCol->iSorterColumn = j; 232913449892Sdrh break; 23302282792aSdrh } 233113449892Sdrh } 233213449892Sdrh } 233313449892Sdrh if( pCol->iSorterColumn<0 ){ 233413449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 233513449892Sdrh } 233613449892Sdrh } 233713449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 233813449892Sdrh ** because it was there before or because we just created it). 233913449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 234013449892Sdrh ** pAggInfo->aCol[] entry. 234113449892Sdrh */ 234213449892Sdrh pExpr->pAggInfo = pAggInfo; 234313449892Sdrh pExpr->op = TK_AGG_COLUMN; 23447f906d63Sdrh pExpr->iAgg = k; 234513449892Sdrh break; 234613449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 234713449892Sdrh } /* end loop over pSrcList */ 2348a58fdfb1Sdanielk1977 } 2349626a879aSdrh return 1; 23502282792aSdrh } 23512282792aSdrh case TK_AGG_FUNCTION: { 235213449892Sdrh /* The pNC->nDepth==0 test causes aggregate functions in subqueries 235313449892Sdrh ** to be ignored */ 2354a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 235513449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 235613449892Sdrh ** function that is already in the pAggInfo structure 235713449892Sdrh */ 235813449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 235913449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 236013449892Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ 23612282792aSdrh break; 23622282792aSdrh } 23632282792aSdrh } 236413449892Sdrh if( i>=pAggInfo->nFunc ){ 236513449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 236613449892Sdrh */ 236714db2665Sdanielk1977 u8 enc = ENC(pParse->db); 236813449892Sdrh i = addAggInfoFunc(pAggInfo); 236913449892Sdrh if( i>=0 ){ 237013449892Sdrh pItem = &pAggInfo->aFunc[i]; 237113449892Sdrh pItem->pExpr = pExpr; 237213449892Sdrh pItem->iMem = pParse->nMem++; 237313449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 23742646da7eSdrh (char*)pExpr->token.z, pExpr->token.n, 2375d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 2376fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 2377fd357974Sdrh pItem->iDistinct = pParse->nTab++; 2378fd357974Sdrh }else{ 2379fd357974Sdrh pItem->iDistinct = -1; 2380fd357974Sdrh } 23812282792aSdrh } 238213449892Sdrh } 238313449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 238413449892Sdrh */ 23852282792aSdrh pExpr->iAgg = i; 238613449892Sdrh pExpr->pAggInfo = pAggInfo; 2387626a879aSdrh return 1; 23882282792aSdrh } 23892282792aSdrh } 2390a58fdfb1Sdanielk1977 } 239113449892Sdrh 239213449892Sdrh /* Recursively walk subqueries looking for TK_COLUMN nodes that need 239313449892Sdrh ** to be changed to TK_AGG_COLUMN. But increment nDepth so that 239413449892Sdrh ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. 239513449892Sdrh */ 2396a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 2397a58fdfb1Sdanielk1977 pNC->nDepth++; 2398a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 2399a58fdfb1Sdanielk1977 pNC->nDepth--; 2400a58fdfb1Sdanielk1977 } 2401626a879aSdrh return 0; 24022282792aSdrh } 2403626a879aSdrh 2404626a879aSdrh /* 2405626a879aSdrh ** Analyze the given expression looking for aggregate functions and 2406626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 2407626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 2408626a879aSdrh ** 2409626a879aSdrh ** This routine should only be called after the expression has been 2410626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 2411626a879aSdrh ** 2412626a879aSdrh ** If errors are seen, leave an error message in zErrMsg and return 2413626a879aSdrh ** the number of errors. 2414626a879aSdrh */ 2415a58fdfb1Sdanielk1977 int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 2416a58fdfb1Sdanielk1977 int nErr = pNC->pParse->nErr; 2417a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 2418a58fdfb1Sdanielk1977 return pNC->pParse->nErr - nErr; 24192282792aSdrh } 24205d9a4af9Sdrh 24215d9a4af9Sdrh /* 24225d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 24235d9a4af9Sdrh ** expression list. Return the number of errors. 24245d9a4af9Sdrh ** 24255d9a4af9Sdrh ** If an error is found, the analysis is cut short. 24265d9a4af9Sdrh */ 24275d9a4af9Sdrh int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 24285d9a4af9Sdrh struct ExprList_item *pItem; 24295d9a4af9Sdrh int i; 24305d9a4af9Sdrh int nErr = 0; 24315d9a4af9Sdrh if( pList ){ 24325d9a4af9Sdrh for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){ 24335d9a4af9Sdrh nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 24345d9a4af9Sdrh } 24355d9a4af9Sdrh } 24365d9a4af9Sdrh return nErr; 24375d9a4af9Sdrh } 2438