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*15d7982aSdanielk1977 ** $Id: expr.c,v 1.294 2007/05/15 07:00:34 danielk1977 Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20e014a838Sdanielk1977 /* 21e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any. 22e014a838Sdanielk1977 ** 23e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias, 24e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the 25e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned, 26e014a838Sdanielk1977 ** indicating no affinity for the expression. 27e014a838Sdanielk1977 ** 28e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all 29e014a838Sdanielk1977 ** have an affinity: 30e014a838Sdanielk1977 ** 31e014a838Sdanielk1977 ** CREATE TABLE t1(a); 32e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a; 33e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b; 34e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1); 35e014a838Sdanielk1977 */ 36bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){ 37487e262fSdrh int op = pExpr->op; 38487e262fSdrh if( op==TK_SELECT ){ 39bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 40a37cdde0Sdanielk1977 } 41487e262fSdrh #ifndef SQLITE_OMIT_CAST 42487e262fSdrh if( op==TK_CAST ){ 438a51256cSdrh return sqlite3AffinityType(&pExpr->token); 44487e262fSdrh } 45487e262fSdrh #endif 46a37cdde0Sdanielk1977 return pExpr->affinity; 47a37cdde0Sdanielk1977 } 48a37cdde0Sdanielk1977 4953db1458Sdrh /* 508b4c40d8Sdrh ** Set the collating sequence for expression pExpr to be the collating 518b4c40d8Sdrh ** sequence named by pToken. Return a pointer to the revised expression. 52a34001c9Sdrh ** The collating sequence is marked as "explicit" using the EP_ExpCollate 53a34001c9Sdrh ** flag. An explicit collating sequence will override implicit 54a34001c9Sdrh ** collating sequences. 558b4c40d8Sdrh */ 568b4c40d8Sdrh Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){ 578b4c40d8Sdrh CollSeq *pColl; 588b4c40d8Sdrh if( pExpr==0 ) return 0; 598b4c40d8Sdrh pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n); 608b4c40d8Sdrh if( pColl ){ 618b4c40d8Sdrh pExpr->pColl = pColl; 628b4c40d8Sdrh pExpr->flags |= EP_ExpCollate; 638b4c40d8Sdrh } 648b4c40d8Sdrh return pExpr; 658b4c40d8Sdrh } 668b4c40d8Sdrh 678b4c40d8Sdrh /* 680202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 690202b29eSdanielk1977 ** there is no default collation type, return 0. 700202b29eSdanielk1977 */ 717cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 727cedc8d4Sdanielk1977 CollSeq *pColl = 0; 730202b29eSdanielk1977 if( pExpr ){ 747cedc8d4Sdanielk1977 pColl = pExpr->pColl; 754f07e5fbSdrh if( pExpr->op==TK_CAST && !pColl ){ 767cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 770202b29eSdanielk1977 } 780202b29eSdanielk1977 } 797cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 807cedc8d4Sdanielk1977 pColl = 0; 817cedc8d4Sdanielk1977 } 827cedc8d4Sdanielk1977 return pColl; 830202b29eSdanielk1977 } 840202b29eSdanielk1977 850202b29eSdanielk1977 /* 86626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 87626a879aSdrh ** type affinity of the other operand. This routine returns the 8853db1458Sdrh ** type affinity that should be used for the comparison operator. 8953db1458Sdrh */ 90e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 91bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 92e014a838Sdanielk1977 if( aff1 && aff2 ){ 938df447f0Sdrh /* Both sides of the comparison are columns. If one has numeric 948df447f0Sdrh ** affinity, use that. Otherwise use no affinity. 95e014a838Sdanielk1977 */ 968a51256cSdrh if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ 97e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 98e014a838Sdanielk1977 }else{ 99e014a838Sdanielk1977 return SQLITE_AFF_NONE; 100e014a838Sdanielk1977 } 101e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 1025f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 1035f6a87b3Sdrh ** results directly. 104e014a838Sdanielk1977 */ 1055f6a87b3Sdrh return SQLITE_AFF_NONE; 106e014a838Sdanielk1977 }else{ 107e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 108fe05af87Sdrh assert( aff1==0 || aff2==0 ); 109e014a838Sdanielk1977 return (aff1 + aff2); 110e014a838Sdanielk1977 } 111e014a838Sdanielk1977 } 112e014a838Sdanielk1977 11353db1458Sdrh /* 11453db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 11553db1458Sdrh ** be applied to both operands prior to doing the comparison. 11653db1458Sdrh */ 117e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 118e014a838Sdanielk1977 char aff; 119e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 120e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 121e014a838Sdanielk1977 pExpr->op==TK_NE ); 122e014a838Sdanielk1977 assert( pExpr->pLeft ); 123bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 124e014a838Sdanielk1977 if( pExpr->pRight ){ 125e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 126e014a838Sdanielk1977 } 127e014a838Sdanielk1977 else if( pExpr->pSelect ){ 128e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 129e014a838Sdanielk1977 } 130e014a838Sdanielk1977 else if( !aff ){ 131de087bd5Sdrh aff = SQLITE_AFF_NONE; 132e014a838Sdanielk1977 } 133e014a838Sdanielk1977 return aff; 134e014a838Sdanielk1977 } 135e014a838Sdanielk1977 136e014a838Sdanielk1977 /* 137e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 138e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 139e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 140e014a838Sdanielk1977 ** the comparison in pExpr. 141e014a838Sdanielk1977 */ 142e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 143e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 1448a51256cSdrh switch( aff ){ 1458a51256cSdrh case SQLITE_AFF_NONE: 1468a51256cSdrh return 1; 1478a51256cSdrh case SQLITE_AFF_TEXT: 1488a51256cSdrh return idx_affinity==SQLITE_AFF_TEXT; 1498a51256cSdrh default: 1508a51256cSdrh return sqlite3IsNumericAffinity(idx_affinity); 1518a51256cSdrh } 152e014a838Sdanielk1977 } 153e014a838Sdanielk1977 154a37cdde0Sdanielk1977 /* 155a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 156a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 157a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 158a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 159a37cdde0Sdanielk1977 ** evaluates to NULL. 160a37cdde0Sdanielk1977 */ 161e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 162bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 163f0863fe5Sdrh return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0); 164a37cdde0Sdanielk1977 } 165a37cdde0Sdanielk1977 166a2e00042Sdrh /* 1670202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1680202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1690202b29eSdanielk1977 ** 1700202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1710202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1720202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1730202b29eSdanielk1977 ** type. 1740202b29eSdanielk1977 */ 1757cedc8d4Sdanielk1977 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ 176ec41ddacSdrh CollSeq *pColl; 177ec41ddacSdrh assert( pLeft ); 178ec41ddacSdrh assert( pRight ); 179ec41ddacSdrh if( pLeft->flags & EP_ExpCollate ){ 180ec41ddacSdrh assert( pLeft->pColl ); 181ec41ddacSdrh pColl = pLeft->pColl; 182ec41ddacSdrh }else if( pRight->flags & EP_ExpCollate ){ 183ec41ddacSdrh assert( pRight->pColl ); 184ec41ddacSdrh pColl = pRight->pColl; 185ec41ddacSdrh }else{ 186ec41ddacSdrh pColl = sqlite3ExprCollSeq(pParse, pLeft); 1870202b29eSdanielk1977 if( !pColl ){ 1887cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 1890202b29eSdanielk1977 } 190ec41ddacSdrh } 1910202b29eSdanielk1977 return pColl; 1920202b29eSdanielk1977 } 1930202b29eSdanielk1977 1940202b29eSdanielk1977 /* 195be5c89acSdrh ** Generate code for a comparison operator. 196be5c89acSdrh */ 197be5c89acSdrh static int codeCompare( 198be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 199be5c89acSdrh Expr *pLeft, /* The left operand */ 200be5c89acSdrh Expr *pRight, /* The right operand */ 201be5c89acSdrh int opcode, /* The comparison opcode */ 202be5c89acSdrh int dest, /* Jump here if true. */ 203be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 204be5c89acSdrh ){ 205be5c89acSdrh int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); 206be5c89acSdrh CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); 207be5c89acSdrh return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); 208be5c89acSdrh } 209be5c89acSdrh 210be5c89acSdrh /* 211a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 212a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 213a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 214a76b5dfcSdrh */ 215e4e72072Sdrh Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ 216a76b5dfcSdrh Expr *pNew; 217a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 218a76b5dfcSdrh if( pNew==0 ){ 219d5d56523Sdanielk1977 /* When malloc fails, delete pLeft and pRight. Expressions passed to 220d5d56523Sdanielk1977 ** this function must always be allocated with sqlite3Expr() for this 221d5d56523Sdanielk1977 ** reason. 222d5d56523Sdanielk1977 */ 223d5d56523Sdanielk1977 sqlite3ExprDelete(pLeft); 224d5d56523Sdanielk1977 sqlite3ExprDelete(pRight); 225a76b5dfcSdrh return 0; 226a76b5dfcSdrh } 227a76b5dfcSdrh pNew->op = op; 228a76b5dfcSdrh pNew->pLeft = pLeft; 229a76b5dfcSdrh pNew->pRight = pRight; 230a58fdfb1Sdanielk1977 pNew->iAgg = -1; 231a76b5dfcSdrh if( pToken ){ 2324b59ab5eSdrh assert( pToken->dyn==0 ); 233145716b3Sdrh pNew->span = pNew->token = *pToken; 234a34001c9Sdrh }else if( pLeft ){ 235a34001c9Sdrh if( pRight ){ 2364adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 2375ffb3ac8Sdrh if( pRight->flags & EP_ExpCollate ){ 238a34001c9Sdrh pNew->flags |= EP_ExpCollate; 239a34001c9Sdrh pNew->pColl = pRight->pColl; 240a34001c9Sdrh } 241a34001c9Sdrh } 2425ffb3ac8Sdrh if( pLeft->flags & EP_ExpCollate ){ 243a34001c9Sdrh pNew->flags |= EP_ExpCollate; 244a34001c9Sdrh pNew->pColl = pLeft->pColl; 245a34001c9Sdrh } 246a76b5dfcSdrh } 247fc976065Sdanielk1977 248fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 249a76b5dfcSdrh return pNew; 250a76b5dfcSdrh } 251a76b5dfcSdrh 252a76b5dfcSdrh /* 253206f3d96Sdrh ** Works like sqlite3Expr() but frees its pLeft and pRight arguments 254206f3d96Sdrh ** if it fails due to a malloc problem. 255206f3d96Sdrh */ 256206f3d96Sdrh Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ 257206f3d96Sdrh Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken); 258206f3d96Sdrh if( pNew==0 ){ 259206f3d96Sdrh sqlite3ExprDelete(pLeft); 260206f3d96Sdrh sqlite3ExprDelete(pRight); 261206f3d96Sdrh } 262206f3d96Sdrh return pNew; 263206f3d96Sdrh } 264206f3d96Sdrh 265206f3d96Sdrh /* 2664e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 2674e0cff60Sdrh ** that look like this: #0 #1 #2 ... These terms refer to elements 268288d37f1Sdrh ** on the stack. "#0" means the top of the stack. 269288d37f1Sdrh ** "#1" means the next down on the stack. And so forth. 2704e0cff60Sdrh ** 2714e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 2724e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 2734e0cff60Sdrh ** The returns an expression that will code to extract the value from 2744e0cff60Sdrh ** that memory location as needed. 2754e0cff60Sdrh */ 2764e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 2774e0cff60Sdrh Vdbe *v = pParse->pVdbe; 2784e0cff60Sdrh Expr *p; 2794e0cff60Sdrh int depth; 2804e0cff60Sdrh if( pParse->nested==0 ){ 2814e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 2824e05c83bSdrh return sqlite3Expr(TK_NULL, 0, 0, 0); 2834e0cff60Sdrh } 284bb7ac00bSdrh if( v==0 ) return 0; 2854e0cff60Sdrh p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); 28673c42a13Sdrh if( p==0 ){ 28773c42a13Sdrh return 0; /* Malloc failed */ 28873c42a13Sdrh } 2892646da7eSdrh depth = atoi((char*)&pToken->z[1]); 2904e0cff60Sdrh p->iTable = pParse->nMem++; 2914e0cff60Sdrh sqlite3VdbeAddOp(v, OP_Dup, depth, 0); 2924e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1); 2934e0cff60Sdrh return p; 2944e0cff60Sdrh } 2954e0cff60Sdrh 2964e0cff60Sdrh /* 29791bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 29891bb0eedSdrh ** NULL, then just return the other expression. 29991bb0eedSdrh */ 30091bb0eedSdrh Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ 30191bb0eedSdrh if( pLeft==0 ){ 30291bb0eedSdrh return pRight; 30391bb0eedSdrh }else if( pRight==0 ){ 30491bb0eedSdrh return pLeft; 30591bb0eedSdrh }else{ 30691bb0eedSdrh return sqlite3Expr(TK_AND, pLeft, pRight, 0); 30791bb0eedSdrh } 30891bb0eedSdrh } 30991bb0eedSdrh 31091bb0eedSdrh /* 3116977fea8Sdrh ** Set the Expr.span field of the given expression to span all 312a76b5dfcSdrh ** text between the two given tokens. 313a76b5dfcSdrh */ 3144adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 3154efc4754Sdrh assert( pRight!=0 ); 3164efc4754Sdrh assert( pLeft!=0 ); 3179e12800dSdanielk1977 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){ 318ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 319145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 3206977fea8Sdrh pExpr->span.z = pLeft->z; 32197903fefSdrh pExpr->span.n = pRight->n + (pRight->z - pLeft->z); 3224b59ab5eSdrh }else{ 3236977fea8Sdrh pExpr->span.z = 0; 3244b59ab5eSdrh } 325a76b5dfcSdrh } 326a76b5dfcSdrh } 327a76b5dfcSdrh 328a76b5dfcSdrh /* 329a76b5dfcSdrh ** Construct a new expression node for a function with multiple 330a76b5dfcSdrh ** arguments. 331a76b5dfcSdrh */ 3324adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 333a76b5dfcSdrh Expr *pNew; 3344b202ae2Sdanielk1977 assert( pToken ); 335a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 336a76b5dfcSdrh if( pNew==0 ){ 337d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */ 338a76b5dfcSdrh return 0; 339a76b5dfcSdrh } 340a76b5dfcSdrh pNew->op = TK_FUNCTION; 341a76b5dfcSdrh pNew->pList = pList; 3424b59ab5eSdrh assert( pToken->dyn==0 ); 343a76b5dfcSdrh pNew->token = *pToken; 3446977fea8Sdrh pNew->span = pNew->token; 345fc976065Sdanielk1977 346fc976065Sdanielk1977 sqlite3ExprSetHeight(pNew); 347a76b5dfcSdrh return pNew; 348a76b5dfcSdrh } 349a76b5dfcSdrh 350a76b5dfcSdrh /* 351fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 352fa6bc000Sdrh ** in the original SQL statement. 353fa6bc000Sdrh ** 354fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 355fa6bc000Sdrh ** variable number. 356fa6bc000Sdrh ** 357fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 358fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 359fa6bc000Sdrh ** the SQL statement comes from an external source. 360fa6bc000Sdrh ** 361fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 362fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 363fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 364fa6bc000Sdrh ** assigned. 365fa6bc000Sdrh */ 366fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 367fa6bc000Sdrh Token *pToken; 368fa6bc000Sdrh if( pExpr==0 ) return; 369fa6bc000Sdrh pToken = &pExpr->token; 370fa6bc000Sdrh assert( pToken->n>=1 ); 371fa6bc000Sdrh assert( pToken->z!=0 ); 372fa6bc000Sdrh assert( pToken->z[0]!=0 ); 373fa6bc000Sdrh if( pToken->n==1 ){ 374fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 375fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 376fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 377fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 378fa6bc000Sdrh ** use it as the variable number */ 379fa6bc000Sdrh int i; 3802646da7eSdrh pExpr->iTable = i = atoi((char*)&pToken->z[1]); 381fa6bc000Sdrh if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ 382fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 383fa6bc000Sdrh SQLITE_MAX_VARIABLE_NUMBER); 384fa6bc000Sdrh } 385fa6bc000Sdrh if( i>pParse->nVar ){ 386fa6bc000Sdrh pParse->nVar = i; 387fa6bc000Sdrh } 388fa6bc000Sdrh }else{ 389fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 390fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 391fa6bc000Sdrh ** has never appeared before, reuse the same variable number 392fa6bc000Sdrh */ 393fa6bc000Sdrh int i, n; 394fa6bc000Sdrh n = pToken->n; 395fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 396fa6bc000Sdrh Expr *pE; 397fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 398fa6bc000Sdrh && pE->token.n==n 399fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 400fa6bc000Sdrh pExpr->iTable = pE->iTable; 401fa6bc000Sdrh break; 402fa6bc000Sdrh } 403fa6bc000Sdrh } 404fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 405fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 406fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 407fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 408cf643729Sdrh pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr, 409fa6bc000Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); 410fa6bc000Sdrh } 4119e12800dSdanielk1977 if( !sqlite3MallocFailed() ){ 412fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 413fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 414fa6bc000Sdrh } 415fa6bc000Sdrh } 416fa6bc000Sdrh } 417832b2664Sdanielk1977 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){ 418832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 419832b2664Sdanielk1977 } 420fa6bc000Sdrh } 421fa6bc000Sdrh 422fa6bc000Sdrh /* 423a2e00042Sdrh ** Recursively delete an expression tree. 424a2e00042Sdrh */ 4254adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 426a2e00042Sdrh if( p==0 ) return; 4274efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 4284efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 4294adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 4304adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 4314adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 4324adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 433a2e00042Sdrh sqliteFree(p); 434a2e00042Sdrh } 435a2e00042Sdrh 436d2687b77Sdrh /* 437d2687b77Sdrh ** The Expr.token field might be a string literal that is quoted. 438d2687b77Sdrh ** If so, remove the quotation marks. 439d2687b77Sdrh */ 440d2687b77Sdrh void sqlite3DequoteExpr(Expr *p){ 441d2687b77Sdrh if( ExprHasAnyProperty(p, EP_Dequoted) ){ 442d2687b77Sdrh return; 443d2687b77Sdrh } 444d2687b77Sdrh ExprSetProperty(p, EP_Dequoted); 445d2687b77Sdrh if( p->token.dyn==0 ){ 446d2687b77Sdrh sqlite3TokenCopy(&p->token, &p->token); 447d2687b77Sdrh } 448d2687b77Sdrh sqlite3Dequote((char*)p->token.z); 449d2687b77Sdrh } 450d2687b77Sdrh 451a76b5dfcSdrh 452a76b5dfcSdrh /* 453ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 454ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 455ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 456ff78bd2fSdrh ** without effecting the originals. 457ff78bd2fSdrh ** 4584adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 4594adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 460ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 461ff78bd2fSdrh ** 462ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 463ff78bd2fSdrh */ 4644adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 465ff78bd2fSdrh Expr *pNew; 466ff78bd2fSdrh if( p==0 ) return 0; 467fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 468ff78bd2fSdrh if( pNew==0 ) return 0; 4693b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 4706977fea8Sdrh if( p->token.z!=0 ){ 4712646da7eSdrh pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n); 4724b59ab5eSdrh pNew->token.dyn = 1; 4734b59ab5eSdrh }else{ 4744efc4754Sdrh assert( pNew->token.z==0 ); 4754b59ab5eSdrh } 4766977fea8Sdrh pNew->span.z = 0; 4774adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 4784adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 4794adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 4804adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 481ff78bd2fSdrh return pNew; 482ff78bd2fSdrh } 4834adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 4844b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 4854b59ab5eSdrh if( pFrom->z ){ 4864b59ab5eSdrh pTo->n = pFrom->n; 4872646da7eSdrh pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n); 4884b59ab5eSdrh pTo->dyn = 1; 4894b59ab5eSdrh }else{ 4904b59ab5eSdrh pTo->z = 0; 4914b59ab5eSdrh } 4924b59ab5eSdrh } 4934adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 494ff78bd2fSdrh ExprList *pNew; 495145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 496ff78bd2fSdrh int i; 497ff78bd2fSdrh if( p==0 ) return 0; 498ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 499ff78bd2fSdrh if( pNew==0 ) return 0; 5004305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 5013e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 502e0048400Sdanielk1977 if( pItem==0 ){ 503e0048400Sdanielk1977 sqliteFree(pNew); 504e0048400Sdanielk1977 return 0; 505e0048400Sdanielk1977 } 506145716b3Sdrh pOldItem = p->a; 507145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 5084b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 509145716b3Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); 5106977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 5116977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 5124b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 5134b59ab5eSdrh ** the names of columns in the result set needs this information */ 5144adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 5154b59ab5eSdrh } 5161f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 5176f7adc8aSdrh || pOldExpr->span.z==0 5189e12800dSdanielk1977 || sqlite3MallocFailed() ); 519145716b3Sdrh pItem->zName = sqliteStrDup(pOldItem->zName); 520145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 521145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 5223e7bc9caSdrh pItem->done = 0; 523ff78bd2fSdrh } 524ff78bd2fSdrh return pNew; 525ff78bd2fSdrh } 52693758c8dSdanielk1977 52793758c8dSdanielk1977 /* 52893758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 52993758c8dSdanielk1977 ** the build, then none of the following routines, except for 53093758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 53193758c8dSdanielk1977 ** called with a NULL argument. 53293758c8dSdanielk1977 */ 5336a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 5346a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 5354adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 536ad3cab52Sdrh SrcList *pNew; 537ad3cab52Sdrh int i; 538113088ecSdrh int nByte; 539ad3cab52Sdrh if( p==0 ) return 0; 540113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 5414efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 542ad3cab52Sdrh if( pNew==0 ) return 0; 5434305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 544ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 5454efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 5464efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 547ed8a3bb1Sdrh Table *pTab; 5484efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 5494efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 5504efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 5514efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 5524efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 5531787ccabSdanielk1977 pNewItem->isPopulated = pOldItem->isPopulated; 554ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 555ed8a3bb1Sdrh if( pTab ){ 556ed8a3bb1Sdrh pTab->nRef++; 557a1cb183dSdanielk1977 } 5584adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 5594adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 5604adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 5616c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 562ad3cab52Sdrh } 563ad3cab52Sdrh return pNew; 564ad3cab52Sdrh } 5654adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 566ff78bd2fSdrh IdList *pNew; 567ff78bd2fSdrh int i; 568ff78bd2fSdrh if( p==0 ) return 0; 5694efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 570ff78bd2fSdrh if( pNew==0 ) return 0; 5714305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 5724efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 573d5d56523Sdanielk1977 if( pNew->a==0 ){ 574d5d56523Sdanielk1977 sqliteFree(pNew); 575d5d56523Sdanielk1977 return 0; 576d5d56523Sdanielk1977 } 577ff78bd2fSdrh for(i=0; i<p->nId; i++){ 5784efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 5794efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 5804efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 5814efc4754Sdrh pNewItem->idx = pOldItem->idx; 582ff78bd2fSdrh } 583ff78bd2fSdrh return pNew; 584ff78bd2fSdrh } 5854adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 586ff78bd2fSdrh Select *pNew; 587ff78bd2fSdrh if( p==0 ) return 0; 5884efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 589ff78bd2fSdrh if( pNew==0 ) return 0; 590ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 5914adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 5924adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 5934adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 5944adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 5954adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 5964adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 597ff78bd2fSdrh pNew->op = p->op; 5984adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 599a2dc3b1aSdanielk1977 pNew->pLimit = sqlite3ExprDup(p->pLimit); 600a2dc3b1aSdanielk1977 pNew->pOffset = sqlite3ExprDup(p->pOffset); 6017b58daeaSdrh pNew->iLimit = -1; 6027b58daeaSdrh pNew->iOffset = -1; 603a1cb183dSdanielk1977 pNew->isResolved = p->isResolved; 604a1cb183dSdanielk1977 pNew->isAgg = p->isAgg; 605b9bb7c18Sdrh pNew->usesEphm = 0; 6068e647b81Sdrh pNew->disallowOrderBy = 0; 6070342b1f5Sdrh pNew->pRightmost = 0; 608b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 609b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 610b9bb7c18Sdrh pNew->addrOpenEphm[2] = -1; 611ff78bd2fSdrh return pNew; 612ff78bd2fSdrh } 61393758c8dSdanielk1977 #else 61493758c8dSdanielk1977 Select *sqlite3SelectDup(Select *p){ 61593758c8dSdanielk1977 assert( p==0 ); 61693758c8dSdanielk1977 return 0; 61793758c8dSdanielk1977 } 61893758c8dSdanielk1977 #endif 619ff78bd2fSdrh 620ff78bd2fSdrh 621ff78bd2fSdrh /* 622a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 623a76b5dfcSdrh ** initially NULL, then create a new expression list. 624a76b5dfcSdrh */ 6254adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 626a76b5dfcSdrh if( pList==0 ){ 627a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 628a76b5dfcSdrh if( pList==0 ){ 629d5d56523Sdanielk1977 goto no_mem; 630a76b5dfcSdrh } 6314efc4754Sdrh assert( pList->nAlloc==0 ); 632a76b5dfcSdrh } 6334305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 634d5d56523Sdanielk1977 struct ExprList_item *a; 635d5d56523Sdanielk1977 int n = pList->nAlloc*2 + 4; 636d5d56523Sdanielk1977 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); 637d5d56523Sdanielk1977 if( a==0 ){ 638d5d56523Sdanielk1977 goto no_mem; 639a76b5dfcSdrh } 640d5d56523Sdanielk1977 pList->a = a; 641d5d56523Sdanielk1977 pList->nAlloc = n; 642a76b5dfcSdrh } 6434efc4754Sdrh assert( pList->a!=0 ); 6444efc4754Sdrh if( pExpr || pName ){ 6454efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 6464efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 647a99db3b6Sdrh pItem->zName = sqlite3NameFromToken(pName); 648e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 649a76b5dfcSdrh } 650a76b5dfcSdrh return pList; 651d5d56523Sdanielk1977 652d5d56523Sdanielk1977 no_mem: 653d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 654d5d56523Sdanielk1977 sqlite3ExprDelete(pExpr); 655d5d56523Sdanielk1977 sqlite3ExprListDelete(pList); 656d5d56523Sdanielk1977 return 0; 657a76b5dfcSdrh } 658a76b5dfcSdrh 659a76b5dfcSdrh /* 6607a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 6617a15a4beSdanielk1977 ** leave an error message in pParse. 6627a15a4beSdanielk1977 */ 6637a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 6647a15a4beSdanielk1977 Parse *pParse, 6657a15a4beSdanielk1977 ExprList *pEList, 6667a15a4beSdanielk1977 int iLimit, 6677a15a4beSdanielk1977 const char *zObject 6687a15a4beSdanielk1977 ){ 669b4fc6794Sdanielk1977 if( pEList && pEList->nExpr>iLimit ){ 6707a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 6717a15a4beSdanielk1977 } 6727a15a4beSdanielk1977 } 6737a15a4beSdanielk1977 674fc976065Sdanielk1977 675fc976065Sdanielk1977 #if SQLITE_MAX_EXPR_DEPTH>0 676fc976065Sdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 677fc976065Sdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 678fc976065Sdanielk1977 ** of any expression tree referenced by the structure passed as the 679fc976065Sdanielk1977 ** first argument. 680fc976065Sdanielk1977 ** 681fc976065Sdanielk1977 ** If this maximum height is greater than the current value pointed 682fc976065Sdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 683fc976065Sdanielk1977 ** value. 684fc976065Sdanielk1977 */ 685fc976065Sdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 686fc976065Sdanielk1977 if( p ){ 687fc976065Sdanielk1977 if( p->nHeight>*pnHeight ){ 688fc976065Sdanielk1977 *pnHeight = p->nHeight; 689fc976065Sdanielk1977 } 690fc976065Sdanielk1977 } 691fc976065Sdanielk1977 } 692fc976065Sdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 693fc976065Sdanielk1977 if( p ){ 694fc976065Sdanielk1977 int i; 695fc976065Sdanielk1977 for(i=0; i<p->nExpr; i++){ 696fc976065Sdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 697fc976065Sdanielk1977 } 698fc976065Sdanielk1977 } 699fc976065Sdanielk1977 } 700fc976065Sdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 701fc976065Sdanielk1977 if( p ){ 702fc976065Sdanielk1977 heightOfExpr(p->pWhere, pnHeight); 703fc976065Sdanielk1977 heightOfExpr(p->pHaving, pnHeight); 704fc976065Sdanielk1977 heightOfExpr(p->pLimit, pnHeight); 705fc976065Sdanielk1977 heightOfExpr(p->pOffset, pnHeight); 706fc976065Sdanielk1977 heightOfExprList(p->pEList, pnHeight); 707fc976065Sdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 708fc976065Sdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 709fc976065Sdanielk1977 heightOfSelect(p->pPrior, pnHeight); 710fc976065Sdanielk1977 } 711fc976065Sdanielk1977 } 712fc976065Sdanielk1977 713fc976065Sdanielk1977 /* 714fc976065Sdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 715fc976065Sdanielk1977 ** argument. An expression with no children, Expr.pList or 716fc976065Sdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 717fc976065Sdanielk1977 ** has a height equal to the maximum height of any other 718fc976065Sdanielk1977 ** referenced Expr plus one. 719fc976065Sdanielk1977 */ 720fc976065Sdanielk1977 void sqlite3ExprSetHeight(Expr *p){ 721fc976065Sdanielk1977 int nHeight = 0; 722fc976065Sdanielk1977 heightOfExpr(p->pLeft, &nHeight); 723fc976065Sdanielk1977 heightOfExpr(p->pRight, &nHeight); 724fc976065Sdanielk1977 heightOfExprList(p->pList, &nHeight); 725fc976065Sdanielk1977 heightOfSelect(p->pSelect, &nHeight); 726fc976065Sdanielk1977 p->nHeight = nHeight + 1; 727fc976065Sdanielk1977 } 728fc976065Sdanielk1977 729fc976065Sdanielk1977 /* 730fc976065Sdanielk1977 ** Return the maximum height of any expression tree referenced 731fc976065Sdanielk1977 ** by the select statement passed as an argument. 732fc976065Sdanielk1977 */ 733fc976065Sdanielk1977 int sqlite3SelectExprHeight(Select *p){ 734fc976065Sdanielk1977 int nHeight = 0; 735fc976065Sdanielk1977 heightOfSelect(p, &nHeight); 736fc976065Sdanielk1977 return nHeight; 737fc976065Sdanielk1977 } 738fc976065Sdanielk1977 #endif 739fc976065Sdanielk1977 7407a15a4beSdanielk1977 /* 741a76b5dfcSdrh ** Delete an entire expression list. 742a76b5dfcSdrh */ 7434adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 744a76b5dfcSdrh int i; 745be5c89acSdrh struct ExprList_item *pItem; 746a76b5dfcSdrh if( pList==0 ) return; 7471bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 7481bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 749be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 750be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 751be5c89acSdrh sqliteFree(pItem->zName); 752a76b5dfcSdrh } 753a76b5dfcSdrh sqliteFree(pList->a); 754a76b5dfcSdrh sqliteFree(pList); 755a76b5dfcSdrh } 756a76b5dfcSdrh 757a76b5dfcSdrh /* 758626a879aSdrh ** Walk an expression tree. Call xFunc for each node visited. 75973b211abSdrh ** 760626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 761626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 762626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 763626a879aSdrh ** the tree walk completely. 764626a879aSdrh ** 765626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 766626a879aSdrh ** and 0 to continue. 76787abf5c0Sdrh ** 76887abf5c0Sdrh ** NOTICE: This routine does *not* descend into subqueries. 769626a879aSdrh */ 770a58fdfb1Sdanielk1977 static int walkExprList(ExprList *, int (*)(void *, Expr*), void *); 771626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 772626a879aSdrh int rc; 773626a879aSdrh if( pExpr==0 ) return 0; 774626a879aSdrh rc = (*xFunc)(pArg, pExpr); 775626a879aSdrh if( rc==0 ){ 776626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 777626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 778a58fdfb1Sdanielk1977 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1; 779626a879aSdrh } 780626a879aSdrh return rc>1; 781626a879aSdrh } 782626a879aSdrh 783626a879aSdrh /* 784a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in list p. 785a58fdfb1Sdanielk1977 */ 786a58fdfb1Sdanielk1977 static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){ 787a58fdfb1Sdanielk1977 int i; 788a58fdfb1Sdanielk1977 struct ExprList_item *pItem; 789a58fdfb1Sdanielk1977 if( !p ) return 0; 790a58fdfb1Sdanielk1977 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){ 791a58fdfb1Sdanielk1977 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 792a58fdfb1Sdanielk1977 } 793a58fdfb1Sdanielk1977 return 0; 794a58fdfb1Sdanielk1977 } 795a58fdfb1Sdanielk1977 796a58fdfb1Sdanielk1977 /* 797a58fdfb1Sdanielk1977 ** Call walkExprTree() for every expression in Select p, not including 798a58fdfb1Sdanielk1977 ** expressions that are part of sub-selects in any FROM clause or the LIMIT 799a58fdfb1Sdanielk1977 ** or OFFSET expressions.. 800a58fdfb1Sdanielk1977 */ 801a58fdfb1Sdanielk1977 static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){ 802a58fdfb1Sdanielk1977 walkExprList(p->pEList, xFunc, pArg); 803a58fdfb1Sdanielk1977 walkExprTree(p->pWhere, xFunc, pArg); 804a58fdfb1Sdanielk1977 walkExprList(p->pGroupBy, xFunc, pArg); 805a58fdfb1Sdanielk1977 walkExprTree(p->pHaving, xFunc, pArg); 806a58fdfb1Sdanielk1977 walkExprList(p->pOrderBy, xFunc, pArg); 807*15d7982aSdanielk1977 if( p->pPrior ){ 808*15d7982aSdanielk1977 walkSelectExpr(p->pPrior, xFunc, pArg); 809*15d7982aSdanielk1977 } 810a58fdfb1Sdanielk1977 return 0; 811a58fdfb1Sdanielk1977 } 812a58fdfb1Sdanielk1977 813a58fdfb1Sdanielk1977 814a58fdfb1Sdanielk1977 /* 815626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 816626a879aSdrh ** 817626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 81873b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 81973b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 82073b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 82173b211abSdrh ** then do nothing. 82273b211abSdrh ** 82373b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 82473b211abSdrh ** the expression as constant, then we assume the whole expression 82573b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 826626a879aSdrh */ 827626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 828626a879aSdrh switch( pExpr->op ){ 829eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 830eb55bd2fSdrh ** and *pArg==2 */ 831eb55bd2fSdrh case TK_FUNCTION: 832eb55bd2fSdrh if( *((int*)pArg)==2 ) return 0; 833eb55bd2fSdrh /* Fall through */ 834626a879aSdrh case TK_ID: 835626a879aSdrh case TK_COLUMN: 836626a879aSdrh case TK_DOT: 837626a879aSdrh case TK_AGG_FUNCTION: 83813449892Sdrh case TK_AGG_COLUMN: 839fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 840fe2093d7Sdrh case TK_SELECT: 841fe2093d7Sdrh case TK_EXISTS: 842fe2093d7Sdrh #endif 843626a879aSdrh *((int*)pArg) = 0; 844626a879aSdrh return 2; 84587abf5c0Sdrh case TK_IN: 84687abf5c0Sdrh if( pExpr->pSelect ){ 84787abf5c0Sdrh *((int*)pArg) = 0; 84887abf5c0Sdrh return 2; 84987abf5c0Sdrh } 850626a879aSdrh default: 851626a879aSdrh return 0; 852626a879aSdrh } 853626a879aSdrh } 854626a879aSdrh 855626a879aSdrh /* 856fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 857eb55bd2fSdrh ** and 0 if it involves variables or function calls. 8582398937bSdrh ** 8592398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 8602398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 8612398937bSdrh ** a constant. 862fef5208cSdrh */ 8634adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 864626a879aSdrh int isConst = 1; 865626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 866626a879aSdrh return isConst; 867fef5208cSdrh } 868fef5208cSdrh 869fef5208cSdrh /* 870eb55bd2fSdrh ** Walk an expression tree. Return 1 if the expression is constant 871eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 872eb55bd2fSdrh ** are any variables. 873eb55bd2fSdrh ** 874eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 875eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 876eb55bd2fSdrh ** a constant. 877eb55bd2fSdrh */ 878eb55bd2fSdrh int sqlite3ExprIsConstantOrFunction(Expr *p){ 879eb55bd2fSdrh int isConst = 2; 880eb55bd2fSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 881eb55bd2fSdrh return isConst!=0; 882eb55bd2fSdrh } 883eb55bd2fSdrh 884eb55bd2fSdrh /* 88573b211abSdrh ** If the expression p codes a constant integer that is small enough 886202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 887202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 888202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 889e4de1febSdrh */ 8904adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 891e4de1febSdrh switch( p->op ){ 892e4de1febSdrh case TK_INTEGER: { 8932646da7eSdrh if( sqlite3GetInt32((char*)p->token.z, pValue) ){ 894e4de1febSdrh return 1; 895e4de1febSdrh } 896202b2df7Sdrh break; 897202b2df7Sdrh } 8984b59ab5eSdrh case TK_UPLUS: { 8994adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 9004b59ab5eSdrh } 901e4de1febSdrh case TK_UMINUS: { 902e4de1febSdrh int v; 9034adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 904e4de1febSdrh *pValue = -v; 905e4de1febSdrh return 1; 906e4de1febSdrh } 907e4de1febSdrh break; 908e4de1febSdrh } 909e4de1febSdrh default: break; 910e4de1febSdrh } 911e4de1febSdrh return 0; 912e4de1febSdrh } 913e4de1febSdrh 914e4de1febSdrh /* 915c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 916c4a3c779Sdrh */ 9174adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 9184adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 9194adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 9204adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 921c4a3c779Sdrh return 0; 922c4a3c779Sdrh } 923c4a3c779Sdrh 924c4a3c779Sdrh /* 9258141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 9268141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 9278141f61eSdrh ** expression node refer back to that source column. The following changes 9288141f61eSdrh ** are made to pExpr: 9298141f61eSdrh ** 9308141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 9318141f61eSdrh ** the table. 9328141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 9338141f61eSdrh ** from pSrcList. 9348141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 9358141f61eSdrh ** pExpr->op Set to TK_COLUMN. 9368141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 9378141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 9388141f61eSdrh ** 9398141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 9408141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 9418141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 9428141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 9438141f61eSdrh ** means that the form of the name is Z and that columns from any table 9448141f61eSdrh ** can be used. 9458141f61eSdrh ** 9468141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 9478141f61eSdrh ** in pParse and return non-zero. Return zero on success. 9488141f61eSdrh */ 9498141f61eSdrh static int lookupName( 9508141f61eSdrh Parse *pParse, /* The parsing context */ 9518141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 9528141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 9538141f61eSdrh Token *pColumnToken, /* Name of the column. */ 954626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 9558141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 9568141f61eSdrh ){ 9578141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 9588141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 9598141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 9608141f61eSdrh int i, j; /* Loop counters */ 9618141f61eSdrh int cnt = 0; /* Number of matching column names */ 9628141f61eSdrh int cntTab = 0; /* Number of matching table names */ 9639bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 96451669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 96551669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 96673b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 9678141f61eSdrh 9688141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 969a99db3b6Sdrh zDb = sqlite3NameFromToken(pDbToken); 970a99db3b6Sdrh zTab = sqlite3NameFromToken(pTableToken); 971a99db3b6Sdrh zCol = sqlite3NameFromToken(pColumnToken); 9729e12800dSdanielk1977 if( sqlite3MallocFailed() ){ 973d5d56523Sdanielk1977 goto lookupname_end; 9748141f61eSdrh } 9758141f61eSdrh 9768141f61eSdrh pExpr->iTable = -1; 977626a879aSdrh while( pNC && cnt==0 ){ 978ffe07b2dSdrh ExprList *pEList; 979626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 980626a879aSdrh 981b3bce662Sdanielk1977 if( pSrcList ){ 98251669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 98343617e9aSdrh Table *pTab; 98443617e9aSdrh int iDb; 9858141f61eSdrh Column *pCol; 9868141f61eSdrh 98743617e9aSdrh pTab = pItem->pTab; 98843617e9aSdrh assert( pTab!=0 ); 98943617e9aSdrh iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 9908141f61eSdrh assert( pTab->nCol>0 ); 9918141f61eSdrh if( zTab ){ 9928141f61eSdrh if( pItem->zAlias ){ 9938141f61eSdrh char *zTabName = pItem->zAlias; 9944adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 9958141f61eSdrh }else{ 9968141f61eSdrh char *zTabName = pTab->zName; 9974adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 998da184236Sdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ 9998141f61eSdrh continue; 10008141f61eSdrh } 10018141f61eSdrh } 10028141f61eSdrh } 10038141f61eSdrh if( 0==(cntTab++) ){ 10048141f61eSdrh pExpr->iTable = pItem->iCursor; 1005da184236Sdanielk1977 pExpr->pSchema = pTab->pSchema; 100651669863Sdrh pMatch = pItem; 10078141f61eSdrh } 10088141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 10094adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1010b3bf556eSdanielk1977 const char *zColl = pTab->aCol[j].zColl; 1011873fac0cSdrh IdList *pUsing; 10128141f61eSdrh cnt++; 10138141f61eSdrh pExpr->iTable = pItem->iCursor; 101451669863Sdrh pMatch = pItem; 1015da184236Sdanielk1977 pExpr->pSchema = pTab->pSchema; 10168141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 10178141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 1018a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 10198b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1020b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 10218b4c40d8Sdrh } 102261dfc31dSdrh if( i<pSrcList->nSrc-1 ){ 102361dfc31dSdrh if( pItem[1].jointype & JT_NATURAL ){ 1024355ef361Sdrh /* If this match occurred in the left table of a natural join, 1025355ef361Sdrh ** then skip the right table to avoid a duplicate match */ 1026355ef361Sdrh pItem++; 1027355ef361Sdrh i++; 102861dfc31dSdrh }else if( (pUsing = pItem[1].pUsing)!=0 ){ 1029873fac0cSdrh /* If this match occurs on a column that is in the USING clause 1030873fac0cSdrh ** of a join, skip the search of the right table of the join 1031873fac0cSdrh ** to avoid a duplicate match there. */ 1032873fac0cSdrh int k; 1033873fac0cSdrh for(k=0; k<pUsing->nId; k++){ 1034873fac0cSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ 1035873fac0cSdrh pItem++; 1036873fac0cSdrh i++; 1037873fac0cSdrh break; 1038873fac0cSdrh } 1039873fac0cSdrh } 1040873fac0cSdrh } 104161dfc31dSdrh } 10428141f61eSdrh break; 10438141f61eSdrh } 10448141f61eSdrh } 10458141f61eSdrh } 1046b3bce662Sdanielk1977 } 10478141f61eSdrh 1048b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 10498141f61eSdrh /* If we have not already resolved the name, then maybe 10508141f61eSdrh ** it is a new.* or old.* trigger argument reference 10518141f61eSdrh */ 10528141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 10538141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 10548141f61eSdrh Table *pTab = 0; 10554adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 10568141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 10578141f61eSdrh assert( pTriggerStack->pTab ); 10588141f61eSdrh pTab = pTriggerStack->pTab; 10594adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 10608141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 10618141f61eSdrh assert( pTriggerStack->pTab ); 10628141f61eSdrh pTab = pTriggerStack->pTab; 10638141f61eSdrh } 10648141f61eSdrh 10658141f61eSdrh if( pTab ){ 1066f0113000Sdanielk1977 int iCol; 10678141f61eSdrh Column *pCol = pTab->aCol; 10688141f61eSdrh 1069da184236Sdanielk1977 pExpr->pSchema = pTab->pSchema; 10708141f61eSdrh cntTab++; 1071f0113000Sdanielk1977 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { 10724adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 1073f0113000Sdanielk1977 const char *zColl = pTab->aCol[iCol].zColl; 10748141f61eSdrh cnt++; 1075f0113000Sdanielk1977 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; 1076f0113000Sdanielk1977 pExpr->affinity = pTab->aCol[iCol].affinity; 10778b4c40d8Sdrh if( (pExpr->flags & EP_ExpCollate)==0 ){ 1078b3bf556eSdanielk1977 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); 10798b4c40d8Sdrh } 1080aee18ef8Sdanielk1977 pExpr->pTab = pTab; 10818141f61eSdrh break; 10828141f61eSdrh } 10838141f61eSdrh } 10848141f61eSdrh } 10858141f61eSdrh } 1086b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 10878141f61eSdrh 10888141f61eSdrh /* 10898141f61eSdrh ** Perhaps the name is a reference to the ROWID 10908141f61eSdrh */ 10914adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 10928141f61eSdrh cnt = 1; 10938141f61eSdrh pExpr->iColumn = -1; 10948a51256cSdrh pExpr->affinity = SQLITE_AFF_INTEGER; 10958141f61eSdrh } 10968141f61eSdrh 10978141f61eSdrh /* 10988141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 10998141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 11008141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 11018141f61eSdrh ** 11028141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 11038141f61eSdrh ** 11048141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 11058141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 11068141f61eSdrh ** Note that the expression in the result set should have already been 11078141f61eSdrh ** resolved by the time the WHERE clause is resolved. 11088141f61eSdrh */ 1109ffe07b2dSdrh if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ 11108141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 11118141f61eSdrh char *zAs = pEList->a[j].zName; 11124adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 11134f07e5fbSdrh Expr *pDup; 11148141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 11154f07e5fbSdrh assert( pExpr->pList==0 ); 11164f07e5fbSdrh assert( pExpr->pSelect==0 ); 11174f07e5fbSdrh pDup = sqlite3ExprDup(pEList->a[j].pExpr); 11184f07e5fbSdrh if( pExpr->flags & EP_ExpCollate ){ 11194f07e5fbSdrh pDup->pColl = pExpr->pColl; 11204f07e5fbSdrh pDup->flags |= EP_ExpCollate; 11214f07e5fbSdrh } 11224f07e5fbSdrh memcpy(pExpr, pDup, sizeof(*pExpr)); 11234f07e5fbSdrh sqliteFree(pDup); 112415ccce1cSdrh cnt = 1; 11258141f61eSdrh assert( zTab==0 && zDb==0 ); 112615ccce1cSdrh goto lookupname_end_2; 11278141f61eSdrh } 11288141f61eSdrh } 11298141f61eSdrh } 11308141f61eSdrh 1131626a879aSdrh /* Advance to the next name context. The loop will exit when either 1132626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 1133626a879aSdrh */ 1134626a879aSdrh if( cnt==0 ){ 1135626a879aSdrh pNC = pNC->pNext; 1136626a879aSdrh } 1137626a879aSdrh } 1138626a879aSdrh 11398141f61eSdrh /* 11408141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 11418141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 11428141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 11438141f61eSdrh ** case, we need to return right away and not make any changes to 11448141f61eSdrh ** pExpr. 114515ccce1cSdrh ** 114615ccce1cSdrh ** Because no reference was made to outer contexts, the pNC->nRef 114715ccce1cSdrh ** fields are not changed in any context. 11488141f61eSdrh */ 11498141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 11508141f61eSdrh sqliteFree(zCol); 11518141f61eSdrh return 0; 11528141f61eSdrh } 11538141f61eSdrh 11548141f61eSdrh /* 11558141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 11568141f61eSdrh ** more matches. Either way, we have an error. 11578141f61eSdrh */ 11588141f61eSdrh if( cnt!=1 ){ 11598141f61eSdrh char *z = 0; 11608141f61eSdrh char *zErr; 11618141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 11628141f61eSdrh if( zDb ){ 1163f93339deSdrh sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0); 11648141f61eSdrh }else if( zTab ){ 1165f93339deSdrh sqlite3SetString(&z, zTab, ".", zCol, (char*)0); 11668141f61eSdrh }else{ 11678141f61eSdrh z = sqliteStrDup(zCol); 11688141f61eSdrh } 11694adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 11708141f61eSdrh sqliteFree(z); 117173b211abSdrh pTopNC->nErr++; 11728141f61eSdrh } 11738141f61eSdrh 117451669863Sdrh /* If a column from a table in pSrcList is referenced, then record 117551669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 117651669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 117751669863Sdrh ** column number is greater than the number of bits in the bitmask 117851669863Sdrh ** then set the high-order bit of the bitmask. 117951669863Sdrh */ 118051669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 118151669863Sdrh int n = pExpr->iColumn; 118251669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 118351669863Sdrh n = sizeof(Bitmask)*8-1; 118451669863Sdrh } 118551669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 1186ca83ac51Sdrh pMatch->colUsed |= ((Bitmask)1)<<n; 118751669863Sdrh } 118851669863Sdrh 1189d5d56523Sdanielk1977 lookupname_end: 11908141f61eSdrh /* Clean up and return 11918141f61eSdrh */ 11928141f61eSdrh sqliteFree(zDb); 11938141f61eSdrh sqliteFree(zTab); 11944adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 11958141f61eSdrh pExpr->pLeft = 0; 11964adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 11978141f61eSdrh pExpr->pRight = 0; 11988141f61eSdrh pExpr->op = TK_COLUMN; 119915ccce1cSdrh lookupname_end_2: 120015ccce1cSdrh sqliteFree(zCol); 1201626a879aSdrh if( cnt==1 ){ 1202b3bce662Sdanielk1977 assert( pNC!=0 ); 1203626a879aSdrh sqlite3AuthRead(pParse, pExpr, pNC->pSrcList); 1204aee18ef8Sdanielk1977 if( pMatch && !pMatch->pSelect ){ 1205aee18ef8Sdanielk1977 pExpr->pTab = pMatch->pTab; 1206aee18ef8Sdanielk1977 } 120715ccce1cSdrh /* Increment the nRef value on all name contexts from TopNC up to 120815ccce1cSdrh ** the point where the name matched. */ 120915ccce1cSdrh for(;;){ 121015ccce1cSdrh assert( pTopNC!=0 ); 121115ccce1cSdrh pTopNC->nRef++; 121215ccce1cSdrh if( pTopNC==pNC ) break; 121315ccce1cSdrh pTopNC = pTopNC->pNext; 1214626a879aSdrh } 121515ccce1cSdrh return 0; 121615ccce1cSdrh } else { 121715ccce1cSdrh return 1; 121815ccce1cSdrh } 12198141f61eSdrh } 12208141f61eSdrh 12218141f61eSdrh /* 1222626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 1223626a879aSdrh ** 122473b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 1225626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 122673b211abSdrh ** the tree or 2 to abort the tree walk. 122773b211abSdrh ** 122873b211abSdrh ** This routine also does error checking and name resolution for 122973b211abSdrh ** function names. The operator for aggregate functions is changed 123073b211abSdrh ** to TK_AGG_FUNCTION. 1231626a879aSdrh */ 1232626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 1233626a879aSdrh NameContext *pNC = (NameContext*)pArg; 1234626a879aSdrh Parse *pParse; 1235626a879aSdrh 1236b3bce662Sdanielk1977 if( pExpr==0 ) return 1; 1237626a879aSdrh assert( pNC!=0 ); 1238626a879aSdrh pParse = pNC->pParse; 1239b3bce662Sdanielk1977 1240626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 1241626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 1242626a879aSdrh #ifndef NDEBUG 1243f0113000Sdanielk1977 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ 1244f0113000Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 1245940fac9dSdanielk1977 int i; 1246f0113000Sdanielk1977 for(i=0; i<pNC->pSrcList->nSrc; i++){ 1247626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 1248626a879aSdrh } 1249626a879aSdrh } 1250626a879aSdrh #endif 1251626a879aSdrh switch( pExpr->op ){ 1252626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 1253626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 1254626a879aSdrh ** strings (ex: 'abc') are always string literals. 1255626a879aSdrh */ 1256626a879aSdrh case TK_STRING: { 1257626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 1258626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 1259626a879aSdrh } 1260626a879aSdrh /* A lone identifier is the name of a column. 1261626a879aSdrh */ 1262626a879aSdrh case TK_ID: { 1263626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1264626a879aSdrh return 1; 1265626a879aSdrh } 1266626a879aSdrh 1267626a879aSdrh /* A table name and column name: ID.ID 1268626a879aSdrh ** Or a database, table and column: ID.ID.ID 1269626a879aSdrh */ 1270626a879aSdrh case TK_DOT: { 1271626a879aSdrh Token *pColumn; 1272626a879aSdrh Token *pTable; 1273626a879aSdrh Token *pDb; 1274626a879aSdrh Expr *pRight; 1275626a879aSdrh 1276b3bce662Sdanielk1977 /* if( pSrcList==0 ) break; */ 1277626a879aSdrh pRight = pExpr->pRight; 1278626a879aSdrh if( pRight->op==TK_ID ){ 1279626a879aSdrh pDb = 0; 1280626a879aSdrh pTable = &pExpr->pLeft->token; 1281626a879aSdrh pColumn = &pRight->token; 1282626a879aSdrh }else{ 1283626a879aSdrh assert( pRight->op==TK_DOT ); 1284626a879aSdrh pDb = &pExpr->pLeft->token; 1285626a879aSdrh pTable = &pRight->pLeft->token; 1286626a879aSdrh pColumn = &pRight->pRight->token; 1287626a879aSdrh } 1288626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1289626a879aSdrh return 1; 1290626a879aSdrh } 1291626a879aSdrh 1292626a879aSdrh /* Resolve function names 1293626a879aSdrh */ 1294b71090fdSdrh case TK_CONST_FUNC: 1295626a879aSdrh case TK_FUNCTION: { 1296626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1297626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1298626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1299626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1300626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1301626a879aSdrh int i; 13025169bbc6Sdrh int auth; /* Authorization to use the function */ 1303626a879aSdrh int nId; /* Number of characters in function name */ 1304626a879aSdrh const char *zId; /* The function name. */ 130573b211abSdrh FuncDef *pDef; /* Information about the function */ 130614db2665Sdanielk1977 int enc = ENC(pParse->db); /* The database encoding */ 1307626a879aSdrh 13082646da7eSdrh zId = (char*)pExpr->token.z; 1309b71090fdSdrh nId = pExpr->token.n; 1310626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1311626a879aSdrh if( pDef==0 ){ 1312626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1313626a879aSdrh if( pDef==0 ){ 1314626a879aSdrh no_such_func = 1; 1315626a879aSdrh }else{ 1316626a879aSdrh wrong_num_args = 1; 1317626a879aSdrh } 1318626a879aSdrh }else{ 1319626a879aSdrh is_agg = pDef->xFunc==0; 1320626a879aSdrh } 13212fca7fefSdrh #ifndef SQLITE_OMIT_AUTHORIZATION 13225169bbc6Sdrh if( pDef ){ 13235169bbc6Sdrh auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); 13245169bbc6Sdrh if( auth!=SQLITE_OK ){ 13255169bbc6Sdrh if( auth==SQLITE_DENY ){ 13265169bbc6Sdrh sqlite3ErrorMsg(pParse, "not authorized to use function: %s", 13275169bbc6Sdrh pDef->zName); 13285169bbc6Sdrh pNC->nErr++; 13295169bbc6Sdrh } 13305169bbc6Sdrh pExpr->op = TK_NULL; 13315169bbc6Sdrh return 1; 13325169bbc6Sdrh } 13335169bbc6Sdrh } 1334b8b14219Sdrh #endif 1335626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1336626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1337626a879aSdrh pNC->nErr++; 1338626a879aSdrh is_agg = 0; 1339626a879aSdrh }else if( no_such_func ){ 1340626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1341626a879aSdrh pNC->nErr++; 1342626a879aSdrh }else if( wrong_num_args ){ 1343626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1344626a879aSdrh nId, zId); 1345626a879aSdrh pNC->nErr++; 1346626a879aSdrh } 1347626a879aSdrh if( is_agg ){ 1348626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1349626a879aSdrh pNC->hasAgg = 1; 1350626a879aSdrh } 135173b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1352626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 135373b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1354626a879aSdrh } 135573b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1356626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1357626a879aSdrh ** type of the function 1358626a879aSdrh */ 1359626a879aSdrh return is_agg; 1360626a879aSdrh } 1361b3bce662Sdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1362b3bce662Sdanielk1977 case TK_SELECT: 1363b3bce662Sdanielk1977 case TK_EXISTS: 1364b3bce662Sdanielk1977 #endif 1365b3bce662Sdanielk1977 case TK_IN: { 1366b3bce662Sdanielk1977 if( pExpr->pSelect ){ 13678a9f38feSdrh int nRef = pNC->nRef; 136806f6541eSdrh #ifndef SQLITE_OMIT_CHECK 136906f6541eSdrh if( pNC->isCheck ){ 137006f6541eSdrh sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); 137106f6541eSdrh } 137206f6541eSdrh #endif 1373b3bce662Sdanielk1977 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); 1374b3bce662Sdanielk1977 assert( pNC->nRef>=nRef ); 1375b3bce662Sdanielk1977 if( nRef!=pNC->nRef ){ 1376b3bce662Sdanielk1977 ExprSetProperty(pExpr, EP_VarSelect); 1377b3bce662Sdanielk1977 } 1378b3bce662Sdanielk1977 } 13794284fb07Sdrh break; 1380b3bce662Sdanielk1977 } 13814284fb07Sdrh #ifndef SQLITE_OMIT_CHECK 13824284fb07Sdrh case TK_VARIABLE: { 13834284fb07Sdrh if( pNC->isCheck ){ 13844284fb07Sdrh sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); 13854284fb07Sdrh } 13864284fb07Sdrh break; 13874284fb07Sdrh } 13884284fb07Sdrh #endif 1389626a879aSdrh } 1390626a879aSdrh return 0; 1391626a879aSdrh } 1392626a879aSdrh 1393626a879aSdrh /* 1394cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1395967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1396aacc543eSdrh ** index to the table in the table list and a column offset. The 1397aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1398aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1399832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1400aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1401aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1402aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1403aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1404aacc543eSdrh ** alias for ROWID. 140519a775c2Sdrh ** 1406626a879aSdrh ** Also resolve function names and check the functions for proper 1407626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1408626a879aSdrh ** have the correct number of arguments. Leave an error message 1409626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1410626a879aSdrh ** 141173b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 141273b211abSdrh ** property on the expression. 1413626a879aSdrh */ 1414626a879aSdrh int sqlite3ExprResolveNames( 1415b3bce662Sdanielk1977 NameContext *pNC, /* Namespace to resolve expressions in. */ 1416b3bce662Sdanielk1977 Expr *pExpr /* The expression to be analyzed. */ 1417626a879aSdrh ){ 141813449892Sdrh int savedHasAgg; 141973b211abSdrh if( pExpr==0 ) return 0; 1420fc976065Sdanielk1977 #if SQLITE_MAX_EXPR_DEPTH>0 1421fc976065Sdanielk1977 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){ 1422fc976065Sdanielk1977 sqlite3ErrorMsg(pNC->pParse, 1423fc976065Sdanielk1977 "Expression tree is too large (maximum depth %d)", 1424fc976065Sdanielk1977 SQLITE_MAX_EXPR_DEPTH 1425fc976065Sdanielk1977 ); 1426fc976065Sdanielk1977 return 1; 1427fc976065Sdanielk1977 } 1428fc976065Sdanielk1977 pNC->pParse->nHeight += pExpr->nHeight; 1429fc976065Sdanielk1977 #endif 143013449892Sdrh savedHasAgg = pNC->hasAgg; 143113449892Sdrh pNC->hasAgg = 0; 1432b3bce662Sdanielk1977 walkExprTree(pExpr, nameResolverStep, pNC); 1433fc976065Sdanielk1977 #if SQLITE_MAX_EXPR_DEPTH>0 1434fc976065Sdanielk1977 pNC->pParse->nHeight -= pExpr->nHeight; 1435fc976065Sdanielk1977 #endif 1436b3bce662Sdanielk1977 if( pNC->nErr>0 ){ 143773b211abSdrh ExprSetProperty(pExpr, EP_Error); 143873b211abSdrh } 143913449892Sdrh if( pNC->hasAgg ){ 144013449892Sdrh ExprSetProperty(pExpr, EP_Agg); 144113449892Sdrh }else if( savedHasAgg ){ 144213449892Sdrh pNC->hasAgg = 1; 144313449892Sdrh } 144473b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1445626a879aSdrh } 1446626a879aSdrh 14471398ad36Sdrh /* 14481398ad36Sdrh ** A pointer instance of this structure is used to pass information 14491398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 14501398ad36Sdrh */ 14511398ad36Sdrh typedef struct QueryCoder QueryCoder; 14521398ad36Sdrh struct QueryCoder { 14531398ad36Sdrh Parse *pParse; /* The parsing context */ 14541398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 14551398ad36Sdrh }; 14561398ad36Sdrh 1457626a879aSdrh 1458626a879aSdrh /* 14599cbe6352Sdrh ** Generate code for scalar subqueries used as an expression 14609cbe6352Sdrh ** and IN operators. Examples: 1461626a879aSdrh ** 14629cbe6352Sdrh ** (SELECT a FROM b) -- subquery 14639cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 14649cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 14659cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 1466fef5208cSdrh ** 14679cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 14689cbe6352Sdrh ** operator or subquery. 1469cce7d176Sdrh */ 147051522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1471b3bce662Sdanielk1977 void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 147257dbd7b3Sdrh int testAddr = 0; /* One-time test address */ 1473b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1474b3bce662Sdanielk1977 if( v==0 ) return; 1475b3bce662Sdanielk1977 1476fc976065Sdanielk1977 147757dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 147857dbd7b3Sdrh ** if any of the following is true: 147957dbd7b3Sdrh ** 148057dbd7b3Sdrh ** * The right-hand side is a correlated subquery 148157dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 148257dbd7b3Sdrh ** * We are inside a trigger 148357dbd7b3Sdrh ** 148457dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 148557dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1486b3bce662Sdanielk1977 */ 1487b3bce662Sdanielk1977 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ 1488b3bce662Sdanielk1977 int mem = pParse->nMem++; 1489b3bce662Sdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); 149057dbd7b3Sdrh testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0); 14919e12800dSdanielk1977 assert( testAddr>0 || sqlite3MallocFailed() ); 1492d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, mem); 1493b3bce662Sdanielk1977 } 1494b3bce662Sdanielk1977 1495cce7d176Sdrh switch( pExpr->op ){ 1496fef5208cSdrh case TK_IN: { 1497e014a838Sdanielk1977 char affinity; 1498d3d39e93Sdrh KeyInfo keyInfo; 1499b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 1500d3d39e93Sdrh 1501bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1502e014a838Sdanielk1977 1503e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 150457dbd7b3Sdrh ** expression it is handled the same way. A virtual table is 1505e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1506e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1507fef5208cSdrh ** 1508e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1509e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1510e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1511e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1512e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1513e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1514e014a838Sdanielk1977 ** is used. 1515fef5208cSdrh */ 1516832508b7Sdrh pExpr->iTable = pParse->nTab++; 1517b9bb7c18Sdrh addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0); 1518d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1519d3d39e93Sdrh keyInfo.nField = 1; 1520f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 1521e014a838Sdanielk1977 1522e014a838Sdanielk1977 if( pExpr->pSelect ){ 1523e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1524e014a838Sdanielk1977 ** 1525e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1526e014a838Sdanielk1977 ** table allocated and opened above. 1527e014a838Sdanielk1977 */ 1528e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 1529be5c89acSdrh ExprList *pEList; 1530e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 153194ccde58Sdrh if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){ 153294ccde58Sdrh return; 153394ccde58Sdrh } 1534be5c89acSdrh pEList = pExpr->pSelect->pEList; 1535be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 15367cedc8d4Sdanielk1977 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 1537be5c89acSdrh pEList->a[0].pExpr); 15380202b29eSdanielk1977 } 1539fef5208cSdrh }else if( pExpr->pList ){ 1540fef5208cSdrh /* Case 2: expr IN (exprlist) 1541fef5208cSdrh ** 1542e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1543e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1544e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1545e014a838Sdanielk1977 ** a column, use numeric affinity. 1546fef5208cSdrh */ 1547e014a838Sdanielk1977 int i; 154857dbd7b3Sdrh ExprList *pList = pExpr->pList; 154957dbd7b3Sdrh struct ExprList_item *pItem; 155057dbd7b3Sdrh 1551e014a838Sdanielk1977 if( !affinity ){ 15528159a35fSdrh affinity = SQLITE_AFF_NONE; 1553e014a838Sdanielk1977 } 15540202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1555e014a838Sdanielk1977 1556e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 155757dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 155857dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1559e014a838Sdanielk1977 156057dbd7b3Sdrh /* If the expression is not constant then we will need to 156157dbd7b3Sdrh ** disable the test that was generated above that makes sure 156257dbd7b3Sdrh ** this code only executes once. Because for a non-constant 156357dbd7b3Sdrh ** expression we need to rerun this code each time. 156457dbd7b3Sdrh */ 15656c30be8eSdrh if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){ 1566f8875400Sdrh sqlite3VdbeChangeToNoop(v, testAddr-1, 3); 156757dbd7b3Sdrh testAddr = 0; 15684794b980Sdrh } 1569e014a838Sdanielk1977 1570e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 15714adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 157294a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 1573f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0); 1574fef5208cSdrh } 1575fef5208cSdrh } 15760202b29eSdanielk1977 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 1577b3bce662Sdanielk1977 break; 1578fef5208cSdrh } 1579fef5208cSdrh 158051522cd3Sdrh case TK_EXISTS: 158119a775c2Sdrh case TK_SELECT: { 1582fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1583fef5208cSdrh ** value of this select in a memory cell and record the number 1584967e8b73Sdrh ** of the memory cell in iColumn. 1585fef5208cSdrh */ 15862646da7eSdrh static const Token one = { (u8*)"1", 0, 1 }; 158751522cd3Sdrh Select *pSel; 1588ec7429aeSdrh int iMem; 1589ec7429aeSdrh int sop; 15901398ad36Sdrh 1591ec7429aeSdrh pExpr->iColumn = iMem = pParse->nMem++; 159251522cd3Sdrh pSel = pExpr->pSelect; 159351522cd3Sdrh if( pExpr->op==TK_SELECT ){ 159451522cd3Sdrh sop = SRT_Mem; 1595ec7429aeSdrh sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0); 1596ec7429aeSdrh VdbeComment((v, "# Init subquery result")); 159751522cd3Sdrh }else{ 159851522cd3Sdrh sop = SRT_Exists; 1599ec7429aeSdrh sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem); 1600ec7429aeSdrh VdbeComment((v, "# Init EXISTS result")); 160151522cd3Sdrh } 1602ec7429aeSdrh sqlite3ExprDelete(pSel->pLimit); 1603ec7429aeSdrh pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one); 160494ccde58Sdrh if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){ 160594ccde58Sdrh return; 160694ccde58Sdrh } 1607b3bce662Sdanielk1977 break; 160819a775c2Sdrh } 1609cce7d176Sdrh } 1610b3bce662Sdanielk1977 161157dbd7b3Sdrh if( testAddr ){ 1612d654be80Sdrh sqlite3VdbeJumpHere(v, testAddr); 1613b3bce662Sdanielk1977 } 1614fc976065Sdanielk1977 1615b3bce662Sdanielk1977 return; 1616cce7d176Sdrh } 161751522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1618cce7d176Sdrh 1619cce7d176Sdrh /* 1620fec19aadSdrh ** Generate an instruction that will put the integer describe by 1621fec19aadSdrh ** text z[0..n-1] on the stack. 1622fec19aadSdrh */ 1623fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1624fec19aadSdrh int i; 16256fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 16266fec0762Sdrh sqlite3VdbeAddOp(v, OP_Integer, i, 0); 16276fec0762Sdrh }else if( sqlite3FitsIn64Bits(z) ){ 162829dda4aeSdrh sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n); 1629fec19aadSdrh }else{ 1630fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1631fec19aadSdrh } 1632fec19aadSdrh } 1633fec19aadSdrh 1634945498f3Sdrh 1635945498f3Sdrh /* 1636945498f3Sdrh ** Generate code that will extract the iColumn-th column from 1637945498f3Sdrh ** table pTab and push that column value on the stack. There 1638945498f3Sdrh ** is an open cursor to pTab in iTable. If iColumn<0 then 1639945498f3Sdrh ** code is generated that extracts the rowid. 1640945498f3Sdrh */ 1641945498f3Sdrh void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){ 1642945498f3Sdrh if( iColumn<0 ){ 1643945498f3Sdrh int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; 1644945498f3Sdrh sqlite3VdbeAddOp(v, op, iTable, 0); 1645945498f3Sdrh }else if( pTab==0 ){ 1646945498f3Sdrh sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn); 1647945498f3Sdrh }else{ 1648945498f3Sdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 1649945498f3Sdrh sqlite3VdbeAddOp(v, op, iTable, iColumn); 1650945498f3Sdrh sqlite3ColumnDefault(v, pTab, iColumn); 1651945498f3Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 1652945498f3Sdrh if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ 1653945498f3Sdrh sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0); 1654945498f3Sdrh } 1655945498f3Sdrh #endif 1656945498f3Sdrh } 1657945498f3Sdrh } 1658945498f3Sdrh 1659fec19aadSdrh /* 1660cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 16611ccde15dSdrh ** expression and leave the result on the top of stack. 1662f2bc013cSdrh ** 1663f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1664f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1665f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1666f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1667f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1668cce7d176Sdrh */ 16694adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1670cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1671cce7d176Sdrh int op; 1672ffe07b2dSdrh int stackChng = 1; /* Amount of change to stack depth */ 1673ffe07b2dSdrh 16747977a17fSdanielk1977 if( v==0 ) return; 16757977a17fSdanielk1977 if( pExpr==0 ){ 1676f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 16777977a17fSdanielk1977 return; 16787977a17fSdanielk1977 } 1679f2bc013cSdrh op = pExpr->op; 1680f2bc013cSdrh switch( op ){ 168113449892Sdrh case TK_AGG_COLUMN: { 168213449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 168313449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 168413449892Sdrh if( !pAggInfo->directMode ){ 168513449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0); 168613449892Sdrh break; 168713449892Sdrh }else if( pAggInfo->useSortingIdx ){ 168813449892Sdrh sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx, 168913449892Sdrh pCol->iSorterColumn); 169013449892Sdrh break; 169113449892Sdrh } 169213449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 169313449892Sdrh } 1694967e8b73Sdrh case TK_COLUMN: { 1695ffe07b2dSdrh if( pExpr->iTable<0 ){ 1696ffe07b2dSdrh /* This only happens when coding check constraints */ 1697ffe07b2dSdrh assert( pParse->ckOffset>0 ); 1698ffe07b2dSdrh sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1); 1699c4a3c779Sdrh }else{ 1700945498f3Sdrh sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable); 17012282792aSdrh } 1702cce7d176Sdrh break; 1703cce7d176Sdrh } 1704cce7d176Sdrh case TK_INTEGER: { 17052646da7eSdrh codeInteger(v, (char*)pExpr->token.z, pExpr->token.n); 1706fec19aadSdrh break; 170751e9a445Sdrh } 1708fec19aadSdrh case TK_FLOAT: 1709fec19aadSdrh case TK_STRING: { 1710f2bc013cSdrh assert( TK_FLOAT==OP_Real ); 1711f2bc013cSdrh assert( TK_STRING==OP_String8 ); 1712d2687b77Sdrh sqlite3DequoteExpr(pExpr); 17132646da7eSdrh sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n); 1714cce7d176Sdrh break; 1715cce7d176Sdrh } 1716f0863fe5Sdrh case TK_NULL: { 1717f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1718f0863fe5Sdrh break; 1719f0863fe5Sdrh } 17205338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 1721c572ef7fSdanielk1977 case TK_BLOB: { 17226c8c6cecSdrh int n; 17236c8c6cecSdrh const char *z; 1724f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 17256c8c6cecSdrh n = pExpr->token.n - 3; 17262646da7eSdrh z = (char*)pExpr->token.z + 2; 17276c8c6cecSdrh assert( n>=0 ); 17286c8c6cecSdrh if( n==0 ){ 17296c8c6cecSdrh z = ""; 17306c8c6cecSdrh } 17316c8c6cecSdrh sqlite3VdbeOp3(v, op, 0, 0, z, n); 1732c572ef7fSdanielk1977 break; 1733c572ef7fSdanielk1977 } 17345338a5f7Sdanielk1977 #endif 173550457896Sdrh case TK_VARIABLE: { 17364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1737895d7472Sdrh if( pExpr->token.n>1 ){ 17382646da7eSdrh sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n); 1739895d7472Sdrh } 174050457896Sdrh break; 174150457896Sdrh } 17424e0cff60Sdrh case TK_REGISTER: { 17434e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); 17444e0cff60Sdrh break; 17454e0cff60Sdrh } 1746487e262fSdrh #ifndef SQLITE_OMIT_CAST 1747487e262fSdrh case TK_CAST: { 1748487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 1749f0113000Sdanielk1977 int aff, to_op; 1750487e262fSdrh sqlite3ExprCode(pParse, pExpr->pLeft); 17518a51256cSdrh aff = sqlite3AffinityType(&pExpr->token); 1752f0113000Sdanielk1977 to_op = aff - SQLITE_AFF_TEXT + OP_ToText; 1753f0113000Sdanielk1977 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); 1754f0113000Sdanielk1977 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); 1755f0113000Sdanielk1977 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); 1756f0113000Sdanielk1977 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); 1757f0113000Sdanielk1977 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); 1758f0113000Sdanielk1977 sqlite3VdbeAddOp(v, to_op, 0, 0); 1759ffe07b2dSdrh stackChng = 0; 1760487e262fSdrh break; 1761487e262fSdrh } 1762487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 1763c9b84a1fSdrh case TK_LT: 1764c9b84a1fSdrh case TK_LE: 1765c9b84a1fSdrh case TK_GT: 1766c9b84a1fSdrh case TK_GE: 1767c9b84a1fSdrh case TK_NE: 1768c9b84a1fSdrh case TK_EQ: { 1769f2bc013cSdrh assert( TK_LT==OP_Lt ); 1770f2bc013cSdrh assert( TK_LE==OP_Le ); 1771f2bc013cSdrh assert( TK_GT==OP_Gt ); 1772f2bc013cSdrh assert( TK_GE==OP_Ge ); 1773f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1774f2bc013cSdrh assert( TK_NE==OP_Ne ); 1775a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1776a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1777be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 1778ffe07b2dSdrh stackChng = -1; 1779a37cdde0Sdanielk1977 break; 1780c9b84a1fSdrh } 1781cce7d176Sdrh case TK_AND: 1782cce7d176Sdrh case TK_OR: 1783cce7d176Sdrh case TK_PLUS: 1784cce7d176Sdrh case TK_STAR: 1785cce7d176Sdrh case TK_MINUS: 1786bf4133cbSdrh case TK_REM: 1787bf4133cbSdrh case TK_BITAND: 1788bf4133cbSdrh case TK_BITOR: 178917c40294Sdrh case TK_SLASH: 1790bf4133cbSdrh case TK_LSHIFT: 1791855eb1cfSdrh case TK_RSHIFT: 17920040077dSdrh case TK_CONCAT: { 1793f2bc013cSdrh assert( TK_AND==OP_And ); 1794f2bc013cSdrh assert( TK_OR==OP_Or ); 1795f2bc013cSdrh assert( TK_PLUS==OP_Add ); 1796f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 1797f2bc013cSdrh assert( TK_REM==OP_Remainder ); 1798f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 1799f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 1800f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 1801f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 1802f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 1803f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 18044adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18054adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1806855eb1cfSdrh sqlite3VdbeAddOp(v, op, 0, 0); 1807ffe07b2dSdrh stackChng = -1; 18080040077dSdrh break; 18090040077dSdrh } 1810cce7d176Sdrh case TK_UMINUS: { 1811fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1812fec19aadSdrh assert( pLeft ); 1813fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1814fec19aadSdrh Token *p = &pLeft->token; 18159267bdceSdrh char *z = sqlite3MPrintf("-%.*s", p->n, p->z); 1816fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1817fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1818e6840900Sdrh }else{ 1819fec19aadSdrh codeInteger(v, z, p->n+1); 1820e6840900Sdrh } 18216e142f54Sdrh sqliteFree(z); 18226e142f54Sdrh break; 18236e142f54Sdrh } 18241ccde15dSdrh /* Fall through into TK_NOT */ 18256e142f54Sdrh } 1826bf4133cbSdrh case TK_BITNOT: 18276e142f54Sdrh case TK_NOT: { 1828f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 1829f2bc013cSdrh assert( TK_NOT==OP_Not ); 18304adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18314adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1832ffe07b2dSdrh stackChng = 0; 1833cce7d176Sdrh break; 1834cce7d176Sdrh } 1835cce7d176Sdrh case TK_ISNULL: 1836cce7d176Sdrh case TK_NOTNULL: { 1837cce7d176Sdrh int dest; 1838f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1839f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 18404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 18414adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18424adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 18434adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 18444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1845ffe07b2dSdrh stackChng = 0; 1846a37cdde0Sdanielk1977 break; 1847f2bc013cSdrh } 18482282792aSdrh case TK_AGG_FUNCTION: { 184913449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 18507e56e711Sdrh if( pInfo==0 ){ 18517e56e711Sdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", 18527e56e711Sdrh &pExpr->span); 18537e56e711Sdrh }else{ 185413449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0); 18557e56e711Sdrh } 18562282792aSdrh break; 18572282792aSdrh } 1858b71090fdSdrh case TK_CONST_FUNC: 1859cce7d176Sdrh case TK_FUNCTION: { 1860cce7d176Sdrh ExprList *pList = pExpr->pList; 186189425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 18620bce8354Sdrh FuncDef *pDef; 18634b59ab5eSdrh int nId; 18644b59ab5eSdrh const char *zId; 186513449892Sdrh int constMask = 0; 1866682f68b0Sdanielk1977 int i; 186714db2665Sdanielk1977 u8 enc = ENC(pParse->db); 1868dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 18692646da7eSdrh zId = (char*)pExpr->token.z; 1870b71090fdSdrh nId = pExpr->token.n; 1871d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 18720bce8354Sdrh assert( pDef!=0 ); 1873f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1874b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 1875a43fa227Sdrh /* Possibly overload the function if the first argument is 1876a43fa227Sdrh ** a virtual table column. 1877a43fa227Sdrh ** 1878a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 1879a43fa227Sdrh ** second argument, not the first, as the argument to test to 1880a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 1881a43fa227Sdrh ** the left operand of infix functions (the operand we want to 1882a43fa227Sdrh ** control overloading) ends up as the second argument to the 1883a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 1884a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 1885a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 1886a43fa227Sdrh */ 18876a03a1c5Sdrh if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ 18886a03a1c5Sdrh pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr); 18896a03a1c5Sdrh }else if( nExpr>0 ){ 1890b7f6f68fSdrh pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr); 1891b7f6f68fSdrh } 1892b7f6f68fSdrh #endif 1893682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 1894d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 189513449892Sdrh constMask |= (1<<i); 1896d02eb1fdSdanielk1977 } 1897dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 1898dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 1899dc1bdc4fSdanielk1977 } 1900dc1bdc4fSdanielk1977 } 1901dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 1902dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 1903d8123366Sdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 1904682f68b0Sdanielk1977 } 190513449892Sdrh sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF); 1906ffe07b2dSdrh stackChng = 1-nExpr; 19076ec2733bSdrh break; 19086ec2733bSdrh } 1909fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1910fe2093d7Sdrh case TK_EXISTS: 191119a775c2Sdrh case TK_SELECT: { 191241714d6fSdrh if( pExpr->iColumn==0 ){ 1913b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 191441714d6fSdrh } 19154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1916ad6d9460Sdrh VdbeComment((v, "# load subquery result")); 191719a775c2Sdrh break; 191819a775c2Sdrh } 1919fef5208cSdrh case TK_IN: { 1920fef5208cSdrh int addr; 192194a11211Sdrh char affinity; 1922afa5f680Sdrh int ckOffset = pParse->ckOffset; 1923b3bce662Sdanielk1977 sqlite3CodeSubselect(pParse, pExpr); 1924e014a838Sdanielk1977 1925e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1926e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1927ededfd5eSdanielk1977 ** P3 of OP_MakeRecord. 1928e014a838Sdanielk1977 */ 192994a11211Sdrh affinity = comparisonAffinity(pExpr); 1930e014a838Sdanielk1977 19314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1932cdbd8effSdanielk1977 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0); 1933e014a838Sdanielk1977 1934e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1935e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1936e014a838Sdanielk1977 */ 19374adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 19384adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1939e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 19404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 1941f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 1942e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 194394a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ 1944e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1945e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1946e014a838Sdanielk1977 1947fef5208cSdrh break; 1948fef5208cSdrh } 194993758c8dSdanielk1977 #endif 1950fef5208cSdrh case TK_BETWEEN: { 1951be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1952be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 1953be5c89acSdrh Expr *pRight = pLItem->pExpr; 1954be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 19554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1956be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1957be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 19584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1959be5c89acSdrh pLItem++; 1960be5c89acSdrh pRight = pLItem->pExpr; 1961be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1962be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 19634adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1964fef5208cSdrh break; 1965fef5208cSdrh } 19664f07e5fbSdrh case TK_UPLUS: { 19674adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1968ffe07b2dSdrh stackChng = 0; 1969a2e00042Sdrh break; 1970a2e00042Sdrh } 197117a7f8ddSdrh case TK_CASE: { 197217a7f8ddSdrh int expr_end_label; 1973f5905aa7Sdrh int jumpInst; 1974f5905aa7Sdrh int nExpr; 197517a7f8ddSdrh int i; 1976be5c89acSdrh ExprList *pEList; 1977be5c89acSdrh struct ExprList_item *aListelem; 197817a7f8ddSdrh 197917a7f8ddSdrh assert(pExpr->pList); 198017a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 198117a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1982be5c89acSdrh pEList = pExpr->pList; 1983be5c89acSdrh aListelem = pEList->a; 1984be5c89acSdrh nExpr = pEList->nExpr; 19854adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 198617a7f8ddSdrh if( pExpr->pLeft ){ 19874adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1988cce7d176Sdrh } 1989f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 1990be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr); 199117a7f8ddSdrh if( pExpr->pLeft ){ 19924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1993be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 1994be5c89acSdrh OP_Ne, 0, 1); 19954adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1996f5905aa7Sdrh }else{ 19974adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 199817a7f8ddSdrh } 1999be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr); 20004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 2001d654be80Sdrh sqlite3VdbeJumpHere(v, jumpInst); 200217a7f8ddSdrh } 2003f570f011Sdrh if( pExpr->pLeft ){ 20044adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 2005f570f011Sdrh } 200617a7f8ddSdrh if( pExpr->pRight ){ 20074adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 200817a7f8ddSdrh }else{ 2009f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 201017a7f8ddSdrh } 20114adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 20126f34903eSdanielk1977 break; 20136f34903eSdanielk1977 } 20145338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 20156f34903eSdanielk1977 case TK_RAISE: { 20166f34903eSdanielk1977 if( !pParse->trigStack ){ 20174adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 2018da93d238Sdrh "RAISE() may only be used within a trigger-program"); 20196f34903eSdanielk1977 return; 20206f34903eSdanielk1977 } 2021ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 2022ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 20236f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 2024ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 2025d2687b77Sdrh sqlite3DequoteExpr(pExpr); 20264adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 20272646da7eSdrh (char*)pExpr->token.z, pExpr->token.n); 20286f34903eSdanielk1977 } else { 20296f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 2030344737f6Sdrh sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 2031ad6d9460Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 2032ad6d9460Sdrh VdbeComment((v, "# raise(IGNORE)")); 20336f34903eSdanielk1977 } 2034ffe07b2dSdrh stackChng = 0; 2035ffe07b2dSdrh break; 203617a7f8ddSdrh } 20375338a5f7Sdanielk1977 #endif 2038ffe07b2dSdrh } 2039ffe07b2dSdrh 2040ffe07b2dSdrh if( pParse->ckOffset ){ 2041ffe07b2dSdrh pParse->ckOffset += stackChng; 2042ffe07b2dSdrh assert( pParse->ckOffset ); 204317a7f8ddSdrh } 2044cce7d176Sdrh } 2045cce7d176Sdrh 204693758c8dSdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 2047cce7d176Sdrh /* 204825303780Sdrh ** Generate code that evalutes the given expression and leaves the result 204925303780Sdrh ** on the stack. See also sqlite3ExprCode(). 205025303780Sdrh ** 205125303780Sdrh ** This routine might also cache the result and modify the pExpr tree 205225303780Sdrh ** so that it will make use of the cached result on subsequent evaluations 205325303780Sdrh ** rather than evaluate the whole expression again. Trivial expressions are 205425303780Sdrh ** not cached. If the expression is cached, its result is stored in a 205525303780Sdrh ** memory location. 205625303780Sdrh */ 205725303780Sdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ 205825303780Sdrh Vdbe *v = pParse->pVdbe; 205925303780Sdrh int iMem; 206025303780Sdrh int addr1, addr2; 206125303780Sdrh if( v==0 ) return; 206225303780Sdrh addr1 = sqlite3VdbeCurrentAddr(v); 206325303780Sdrh sqlite3ExprCode(pParse, pExpr); 206425303780Sdrh addr2 = sqlite3VdbeCurrentAddr(v); 206525303780Sdrh if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){ 206625303780Sdrh iMem = pExpr->iTable = pParse->nMem++; 206725303780Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); 206825303780Sdrh pExpr->op = TK_REGISTER; 206925303780Sdrh } 207025303780Sdrh } 207193758c8dSdanielk1977 #endif 207225303780Sdrh 207325303780Sdrh /* 2074268380caSdrh ** Generate code that pushes the value of every element of the given 2075f9b596ebSdrh ** expression list onto the stack. 2076268380caSdrh ** 2077268380caSdrh ** Return the number of elements pushed onto the stack. 2078268380caSdrh */ 20794adee20fSdanielk1977 int sqlite3ExprCodeExprList( 2080268380caSdrh Parse *pParse, /* Parsing context */ 2081f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 2082268380caSdrh ){ 2083268380caSdrh struct ExprList_item *pItem; 2084268380caSdrh int i, n; 2085268380caSdrh if( pList==0 ) return 0; 2086268380caSdrh n = pList->nExpr; 2087c182d163Sdrh for(pItem=pList->a, i=n; i>0; i--, pItem++){ 20884adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 2089268380caSdrh } 2090f9b596ebSdrh return n; 2091268380caSdrh } 2092268380caSdrh 2093268380caSdrh /* 2094cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 2095cce7d176Sdrh ** to the label "dest" if the expression is true but execution 2096cce7d176Sdrh ** continues straight thru if the expression is false. 2097f5905aa7Sdrh ** 2098f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 2099f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 2100f2bc013cSdrh ** 2101f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 2102f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 2103f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 2104f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 2105f2bc013cSdrh ** below verify that the numbers are aligned correctly. 2106cce7d176Sdrh */ 21074adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2108cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2109cce7d176Sdrh int op = 0; 2110ffe07b2dSdrh int ckOffset = pParse->ckOffset; 2111daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2112f2bc013cSdrh op = pExpr->op; 2113f2bc013cSdrh switch( op ){ 2114cce7d176Sdrh case TK_AND: { 21154adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 21164adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 21174adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 21184adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2119cce7d176Sdrh break; 2120cce7d176Sdrh } 2121cce7d176Sdrh case TK_OR: { 21224adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 21234adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 2124cce7d176Sdrh break; 2125cce7d176Sdrh } 2126cce7d176Sdrh case TK_NOT: { 21274adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 2128cce7d176Sdrh break; 2129cce7d176Sdrh } 2130cce7d176Sdrh case TK_LT: 2131cce7d176Sdrh case TK_LE: 2132cce7d176Sdrh case TK_GT: 2133cce7d176Sdrh case TK_GE: 2134cce7d176Sdrh case TK_NE: 21350ac65892Sdrh case TK_EQ: { 2136f2bc013cSdrh assert( TK_LT==OP_Lt ); 2137f2bc013cSdrh assert( TK_LE==OP_Le ); 2138f2bc013cSdrh assert( TK_GT==OP_Gt ); 2139f2bc013cSdrh assert( TK_GE==OP_Ge ); 2140f2bc013cSdrh assert( TK_EQ==OP_Eq ); 2141f2bc013cSdrh assert( TK_NE==OP_Ne ); 21424adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 21434adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 2144be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 2145cce7d176Sdrh break; 2146cce7d176Sdrh } 2147cce7d176Sdrh case TK_ISNULL: 2148cce7d176Sdrh case TK_NOTNULL: { 2149f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 2150f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 21514adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 21524adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 2153cce7d176Sdrh break; 2154cce7d176Sdrh } 2155fef5208cSdrh case TK_BETWEEN: { 21560202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 21570202b29eSdanielk1977 ** 21580202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 21590202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 21600202b29eSdanielk1977 ** 3 ... 21610202b29eSdanielk1977 */ 2162f5905aa7Sdrh int addr; 2163be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2164be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 2165be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 21664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 2167be5c89acSdrh sqlite3ExprCode(pParse, pRight); 2168be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 21690202b29eSdanielk1977 2170be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 2171be5c89acSdrh sqlite3ExprCode(pParse, pRight); 2172be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 21730202b29eSdanielk1977 21744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 2175d654be80Sdrh sqlite3VdbeJumpHere(v, addr); 21764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 2177fef5208cSdrh break; 2178fef5208cSdrh } 2179cce7d176Sdrh default: { 21804adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 21814adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 2182cce7d176Sdrh break; 2183cce7d176Sdrh } 2184cce7d176Sdrh } 2185ffe07b2dSdrh pParse->ckOffset = ckOffset; 2186cce7d176Sdrh } 2187cce7d176Sdrh 2188cce7d176Sdrh /* 218966b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 2190cce7d176Sdrh ** to the label "dest" if the expression is false but execution 2191cce7d176Sdrh ** continues straight thru if the expression is true. 2192f5905aa7Sdrh ** 2193f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 2194f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 2195cce7d176Sdrh */ 21964adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 2197cce7d176Sdrh Vdbe *v = pParse->pVdbe; 2198cce7d176Sdrh int op = 0; 2199ffe07b2dSdrh int ckOffset = pParse->ckOffset; 2200daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 2201f2bc013cSdrh 2202f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 2203f2bc013cSdrh ** 2204f2bc013cSdrh ** pExpr->op op 2205f2bc013cSdrh ** --------- ---------- 2206f2bc013cSdrh ** TK_ISNULL OP_NotNull 2207f2bc013cSdrh ** TK_NOTNULL OP_IsNull 2208f2bc013cSdrh ** TK_NE OP_Eq 2209f2bc013cSdrh ** TK_EQ OP_Ne 2210f2bc013cSdrh ** TK_GT OP_Le 2211f2bc013cSdrh ** TK_LE OP_Gt 2212f2bc013cSdrh ** TK_GE OP_Lt 2213f2bc013cSdrh ** TK_LT OP_Ge 2214f2bc013cSdrh ** 2215f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 2216f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 2217f2bc013cSdrh ** can compute the mapping above using the following expression. 2218f2bc013cSdrh ** Assert()s verify that the computation is correct. 2219f2bc013cSdrh */ 2220f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 2221f2bc013cSdrh 2222f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 2223f2bc013cSdrh */ 2224f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 2225f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 2226f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 2227f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 2228f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 2229f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 2230f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 2231f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 2232f2bc013cSdrh 2233cce7d176Sdrh switch( pExpr->op ){ 2234cce7d176Sdrh case TK_AND: { 22354adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 22364adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 2237cce7d176Sdrh break; 2238cce7d176Sdrh } 2239cce7d176Sdrh case TK_OR: { 22404adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 22414adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 22424adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 22434adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 2244cce7d176Sdrh break; 2245cce7d176Sdrh } 2246cce7d176Sdrh case TK_NOT: { 22474adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 2248cce7d176Sdrh break; 2249cce7d176Sdrh } 2250cce7d176Sdrh case TK_LT: 2251cce7d176Sdrh case TK_LE: 2252cce7d176Sdrh case TK_GT: 2253cce7d176Sdrh case TK_GE: 2254cce7d176Sdrh case TK_NE: 2255cce7d176Sdrh case TK_EQ: { 22564adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 22574adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 2258be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 2259cce7d176Sdrh break; 2260cce7d176Sdrh } 2261cce7d176Sdrh case TK_ISNULL: 2262cce7d176Sdrh case TK_NOTNULL: { 22634adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 22644adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 2265cce7d176Sdrh break; 2266cce7d176Sdrh } 2267fef5208cSdrh case TK_BETWEEN: { 22680202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 22690202b29eSdanielk1977 ** 22700202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 22710202b29eSdanielk1977 ** 2 GOTO <dest> 22720202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 22730202b29eSdanielk1977 */ 2274fef5208cSdrh int addr; 2275be5c89acSdrh Expr *pLeft = pExpr->pLeft; 2276be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 2277be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 22784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 2279be5c89acSdrh sqlite3ExprCode(pParse, pRight); 22804adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 2281be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 2282be5c89acSdrh 22834adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 22844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 2285be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 2286be5c89acSdrh sqlite3ExprCode(pParse, pRight); 2287be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 2288fef5208cSdrh break; 2289fef5208cSdrh } 2290cce7d176Sdrh default: { 22914adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 22924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 2293cce7d176Sdrh break; 2294cce7d176Sdrh } 2295cce7d176Sdrh } 2296ffe07b2dSdrh pParse->ckOffset = ckOffset; 2297cce7d176Sdrh } 22982282792aSdrh 22992282792aSdrh /* 23002282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 23012282792aSdrh ** if they are identical and return FALSE if they differ in any way. 2302d40aab0eSdrh ** 2303d40aab0eSdrh ** Sometimes this routine will return FALSE even if the two expressions 2304d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 2305d40aab0eSdrh ** identical, we return FALSE just to be safe. So if this routine 2306d40aab0eSdrh ** returns false, then you do not really know for certain if the two 2307d40aab0eSdrh ** expressions are the same. But if you get a TRUE return, then you 2308d40aab0eSdrh ** can be sure the expressions are the same. In the places where 2309d40aab0eSdrh ** this routine is used, it does not hurt to get an extra FALSE - that 2310d40aab0eSdrh ** just might result in some slightly slower code. But returning 2311d40aab0eSdrh ** an incorrect TRUE could lead to a malfunction. 23122282792aSdrh */ 23134adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 23142282792aSdrh int i; 23154b202ae2Sdanielk1977 if( pA==0||pB==0 ){ 23164b202ae2Sdanielk1977 return pB==pA; 23172282792aSdrh } 23182282792aSdrh if( pA->op!=pB->op ) return 0; 2319fd357974Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; 23204adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 23214adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 23222282792aSdrh if( pA->pList ){ 23232282792aSdrh if( pB->pList==0 ) return 0; 23242282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 23252282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 23264adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 23272282792aSdrh return 0; 23282282792aSdrh } 23292282792aSdrh } 23302282792aSdrh }else if( pB->pList ){ 23312282792aSdrh return 0; 23322282792aSdrh } 23332282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 23342f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 2335dd73521bSdrh if( pA->op!=TK_COLUMN && pA->token.z ){ 23362282792aSdrh if( pB->token.z==0 ) return 0; 23376977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 23382646da7eSdrh if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ 23392646da7eSdrh return 0; 23402646da7eSdrh } 23412282792aSdrh } 23422282792aSdrh return 1; 23432282792aSdrh } 23442282792aSdrh 234513449892Sdrh 23462282792aSdrh /* 234713449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 234813449892Sdrh ** the new element. Return a negative number if malloc fails. 23492282792aSdrh */ 235013449892Sdrh static int addAggInfoColumn(AggInfo *pInfo){ 235113449892Sdrh int i; 2352cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 2353cf643729Sdrh pInfo->aCol, 2354cf643729Sdrh sizeof(pInfo->aCol[0]), 2355cf643729Sdrh 3, 2356cf643729Sdrh &pInfo->nColumn, 2357cf643729Sdrh &pInfo->nColumnAlloc, 2358cf643729Sdrh &i 2359cf643729Sdrh ); 236013449892Sdrh return i; 23612282792aSdrh } 236213449892Sdrh 236313449892Sdrh /* 236413449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 236513449892Sdrh ** the new element. Return a negative number if malloc fails. 236613449892Sdrh */ 236713449892Sdrh static int addAggInfoFunc(AggInfo *pInfo){ 236813449892Sdrh int i; 2369cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 2370cf643729Sdrh pInfo->aFunc, 2371cf643729Sdrh sizeof(pInfo->aFunc[0]), 2372cf643729Sdrh 3, 2373cf643729Sdrh &pInfo->nFunc, 2374cf643729Sdrh &pInfo->nFuncAlloc, 2375cf643729Sdrh &i 2376cf643729Sdrh ); 237713449892Sdrh return i; 23782282792aSdrh } 23792282792aSdrh 23802282792aSdrh /* 2381626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 2382626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 2383626a879aSdrh ** for additional information. 23842282792aSdrh ** 2385626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 23862282792aSdrh */ 2387626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 23882282792aSdrh int i; 2389a58fdfb1Sdanielk1977 NameContext *pNC = (NameContext *)pArg; 2390a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 2391a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 239213449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 239313449892Sdrh 23942282792aSdrh 23952282792aSdrh switch( pExpr->op ){ 239689c69d00Sdrh case TK_AGG_COLUMN: 2397967e8b73Sdrh case TK_COLUMN: { 239813449892Sdrh /* Check to see if the column is in one of the tables in the FROM 239913449892Sdrh ** clause of the aggregate query */ 240013449892Sdrh if( pSrcList ){ 240113449892Sdrh struct SrcList_item *pItem = pSrcList->a; 240213449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 240313449892Sdrh struct AggInfo_col *pCol; 240413449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 240513449892Sdrh /* If we reach this point, it means that pExpr refers to a table 240613449892Sdrh ** that is in the FROM clause of the aggregate query. 240713449892Sdrh ** 240813449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 240913449892Sdrh ** is not an entry there already. 241013449892Sdrh */ 24117f906d63Sdrh int k; 241213449892Sdrh pCol = pAggInfo->aCol; 24137f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 241413449892Sdrh if( pCol->iTable==pExpr->iTable && 241513449892Sdrh pCol->iColumn==pExpr->iColumn ){ 24162282792aSdrh break; 24172282792aSdrh } 24182282792aSdrh } 24197f906d63Sdrh if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){ 24207f906d63Sdrh pCol = &pAggInfo->aCol[k]; 24210817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 242213449892Sdrh pCol->iTable = pExpr->iTable; 242313449892Sdrh pCol->iColumn = pExpr->iColumn; 242413449892Sdrh pCol->iMem = pParse->nMem++; 242513449892Sdrh pCol->iSorterColumn = -1; 24265774b806Sdrh pCol->pExpr = pExpr; 242713449892Sdrh if( pAggInfo->pGroupBy ){ 242813449892Sdrh int j, n; 242913449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 243013449892Sdrh struct ExprList_item *pTerm = pGB->a; 243113449892Sdrh n = pGB->nExpr; 243213449892Sdrh for(j=0; j<n; j++, pTerm++){ 243313449892Sdrh Expr *pE = pTerm->pExpr; 243413449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 243513449892Sdrh pE->iColumn==pExpr->iColumn ){ 243613449892Sdrh pCol->iSorterColumn = j; 243713449892Sdrh break; 24382282792aSdrh } 243913449892Sdrh } 244013449892Sdrh } 244113449892Sdrh if( pCol->iSorterColumn<0 ){ 244213449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 244313449892Sdrh } 244413449892Sdrh } 244513449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 244613449892Sdrh ** because it was there before or because we just created it). 244713449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 244813449892Sdrh ** pAggInfo->aCol[] entry. 244913449892Sdrh */ 245013449892Sdrh pExpr->pAggInfo = pAggInfo; 245113449892Sdrh pExpr->op = TK_AGG_COLUMN; 24527f906d63Sdrh pExpr->iAgg = k; 245313449892Sdrh break; 245413449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 245513449892Sdrh } /* end loop over pSrcList */ 2456a58fdfb1Sdanielk1977 } 2457626a879aSdrh return 1; 24582282792aSdrh } 24592282792aSdrh case TK_AGG_FUNCTION: { 246013449892Sdrh /* The pNC->nDepth==0 test causes aggregate functions in subqueries 246113449892Sdrh ** to be ignored */ 2462a58fdfb1Sdanielk1977 if( pNC->nDepth==0 ){ 246313449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 246413449892Sdrh ** function that is already in the pAggInfo structure 246513449892Sdrh */ 246613449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 246713449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 246813449892Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ 24692282792aSdrh break; 24702282792aSdrh } 24712282792aSdrh } 247213449892Sdrh if( i>=pAggInfo->nFunc ){ 247313449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 247413449892Sdrh */ 247514db2665Sdanielk1977 u8 enc = ENC(pParse->db); 247613449892Sdrh i = addAggInfoFunc(pAggInfo); 247713449892Sdrh if( i>=0 ){ 247813449892Sdrh pItem = &pAggInfo->aFunc[i]; 247913449892Sdrh pItem->pExpr = pExpr; 248013449892Sdrh pItem->iMem = pParse->nMem++; 248113449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 24822646da7eSdrh (char*)pExpr->token.z, pExpr->token.n, 2483d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 2484fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 2485fd357974Sdrh pItem->iDistinct = pParse->nTab++; 2486fd357974Sdrh }else{ 2487fd357974Sdrh pItem->iDistinct = -1; 2488fd357974Sdrh } 24892282792aSdrh } 249013449892Sdrh } 249113449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 249213449892Sdrh */ 24932282792aSdrh pExpr->iAgg = i; 249413449892Sdrh pExpr->pAggInfo = pAggInfo; 2495626a879aSdrh return 1; 24962282792aSdrh } 24972282792aSdrh } 2498a58fdfb1Sdanielk1977 } 249913449892Sdrh 250013449892Sdrh /* Recursively walk subqueries looking for TK_COLUMN nodes that need 250113449892Sdrh ** to be changed to TK_AGG_COLUMN. But increment nDepth so that 250213449892Sdrh ** TK_AGG_FUNCTION nodes in subqueries will be unchanged. 250313449892Sdrh */ 2504a58fdfb1Sdanielk1977 if( pExpr->pSelect ){ 2505a58fdfb1Sdanielk1977 pNC->nDepth++; 2506a58fdfb1Sdanielk1977 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC); 2507a58fdfb1Sdanielk1977 pNC->nDepth--; 2508a58fdfb1Sdanielk1977 } 2509626a879aSdrh return 0; 25102282792aSdrh } 2511626a879aSdrh 2512626a879aSdrh /* 2513626a879aSdrh ** Analyze the given expression looking for aggregate functions and 2514626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 2515626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 2516626a879aSdrh ** 2517626a879aSdrh ** This routine should only be called after the expression has been 2518626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 2519626a879aSdrh ** 2520626a879aSdrh ** If errors are seen, leave an error message in zErrMsg and return 2521626a879aSdrh ** the number of errors. 2522626a879aSdrh */ 2523a58fdfb1Sdanielk1977 int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 2524a58fdfb1Sdanielk1977 int nErr = pNC->pParse->nErr; 2525a58fdfb1Sdanielk1977 walkExprTree(pExpr, analyzeAggregate, pNC); 2526a58fdfb1Sdanielk1977 return pNC->pParse->nErr - nErr; 25272282792aSdrh } 25285d9a4af9Sdrh 25295d9a4af9Sdrh /* 25305d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 25315d9a4af9Sdrh ** expression list. Return the number of errors. 25325d9a4af9Sdrh ** 25335d9a4af9Sdrh ** If an error is found, the analysis is cut short. 25345d9a4af9Sdrh */ 25355d9a4af9Sdrh int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 25365d9a4af9Sdrh struct ExprList_item *pItem; 25375d9a4af9Sdrh int i; 25385d9a4af9Sdrh int nErr = 0; 25395d9a4af9Sdrh if( pList ){ 25405d9a4af9Sdrh for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){ 25415d9a4af9Sdrh nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 25425d9a4af9Sdrh } 25435d9a4af9Sdrh } 25445d9a4af9Sdrh return nErr; 25455d9a4af9Sdrh } 2546