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*51522cd3Sdrh ** $Id: expr.c,v 1.185 2005/01/20 13:36:20 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20e014a838Sdanielk1977 /* 21e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any. 22e014a838Sdanielk1977 ** 23e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias, 24e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the 25e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned, 26e014a838Sdanielk1977 ** indicating no affinity for the expression. 27e014a838Sdanielk1977 ** 28e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all 29e014a838Sdanielk1977 ** have an affinity: 30e014a838Sdanielk1977 ** 31e014a838Sdanielk1977 ** CREATE TABLE t1(a); 32e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a; 33e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b; 34e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1); 35e014a838Sdanielk1977 */ 36bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){ 37a37cdde0Sdanielk1977 if( pExpr->op==TK_AS ){ 38bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pLeft); 39a37cdde0Sdanielk1977 } 40a37cdde0Sdanielk1977 if( pExpr->op==TK_SELECT ){ 41bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 42a37cdde0Sdanielk1977 } 43a37cdde0Sdanielk1977 return pExpr->affinity; 44a37cdde0Sdanielk1977 } 45a37cdde0Sdanielk1977 4653db1458Sdrh /* 470202b29eSdanielk1977 ** Return the default collation sequence for the expression pExpr. If 480202b29eSdanielk1977 ** there is no default collation type, return 0. 490202b29eSdanielk1977 */ 507cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 517cedc8d4Sdanielk1977 CollSeq *pColl = 0; 520202b29eSdanielk1977 if( pExpr ){ 537cedc8d4Sdanielk1977 pColl = pExpr->pColl; 547cedc8d4Sdanielk1977 if( pExpr->op==TK_AS && !pColl ){ 557cedc8d4Sdanielk1977 return sqlite3ExprCollSeq(pParse, pExpr->pLeft); 560202b29eSdanielk1977 } 570202b29eSdanielk1977 } 587cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 597cedc8d4Sdanielk1977 pColl = 0; 607cedc8d4Sdanielk1977 } 617cedc8d4Sdanielk1977 return pColl; 620202b29eSdanielk1977 } 630202b29eSdanielk1977 640202b29eSdanielk1977 /* 65626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 66626a879aSdrh ** type affinity of the other operand. This routine returns the 6753db1458Sdrh ** type affinity that should be used for the comparison operator. 6853db1458Sdrh */ 69e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 70bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 71e014a838Sdanielk1977 if( aff1 && aff2 ){ 72e014a838Sdanielk1977 /* Both sides of the comparison are columns. If one has numeric or 73e014a838Sdanielk1977 ** integer affinity, use that. Otherwise use no affinity. 74e014a838Sdanielk1977 */ 75e014a838Sdanielk1977 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ 76e014a838Sdanielk1977 return SQLITE_AFF_INTEGER; 77e014a838Sdanielk1977 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 78e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 79e014a838Sdanielk1977 }else{ 80e014a838Sdanielk1977 return SQLITE_AFF_NONE; 81e014a838Sdanielk1977 } 82e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 835f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 845f6a87b3Sdrh ** results directly. 85e014a838Sdanielk1977 */ 865f6a87b3Sdrh /* return SQLITE_AFF_NUMERIC; // Ticket #805 */ 875f6a87b3Sdrh return SQLITE_AFF_NONE; 88e014a838Sdanielk1977 }else{ 89e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 90e014a838Sdanielk1977 return (aff1 + aff2); 91e014a838Sdanielk1977 } 92e014a838Sdanielk1977 } 93e014a838Sdanielk1977 9453db1458Sdrh /* 9553db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 9653db1458Sdrh ** be applied to both operands prior to doing the comparison. 9753db1458Sdrh */ 98e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 99e014a838Sdanielk1977 char aff; 100e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 101e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 102e014a838Sdanielk1977 pExpr->op==TK_NE ); 103e014a838Sdanielk1977 assert( pExpr->pLeft ); 104bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 105e014a838Sdanielk1977 if( pExpr->pRight ){ 106e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 107e014a838Sdanielk1977 } 108e014a838Sdanielk1977 else if( pExpr->pSelect ){ 109e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 110e014a838Sdanielk1977 } 111e014a838Sdanielk1977 else if( !aff ){ 112e014a838Sdanielk1977 aff = SQLITE_AFF_NUMERIC; 113e014a838Sdanielk1977 } 114e014a838Sdanielk1977 return aff; 115e014a838Sdanielk1977 } 116e014a838Sdanielk1977 117e014a838Sdanielk1977 /* 118e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 119e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 120e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 121e014a838Sdanielk1977 ** the comparison in pExpr. 122e014a838Sdanielk1977 */ 123e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 124e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 125e014a838Sdanielk1977 return 126e014a838Sdanielk1977 (aff==SQLITE_AFF_NONE) || 127e014a838Sdanielk1977 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) || 128e014a838Sdanielk1977 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) || 129e014a838Sdanielk1977 (aff==idx_affinity); 130e014a838Sdanielk1977 } 131e014a838Sdanielk1977 132a37cdde0Sdanielk1977 /* 133a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 134a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 135a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 136a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 137a37cdde0Sdanielk1977 ** evaluates to NULL. 138a37cdde0Sdanielk1977 */ 139e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 140bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 141e014a838Sdanielk1977 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0); 142a37cdde0Sdanielk1977 } 143a37cdde0Sdanielk1977 144a2e00042Sdrh /* 1450202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 1460202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 1470202b29eSdanielk1977 ** 1480202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 1490202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 1500202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 1510202b29eSdanielk1977 ** type. 1520202b29eSdanielk1977 */ 1537cedc8d4Sdanielk1977 static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){ 1547cedc8d4Sdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft); 1550202b29eSdanielk1977 if( !pColl ){ 1567cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 1570202b29eSdanielk1977 } 1580202b29eSdanielk1977 return pColl; 1590202b29eSdanielk1977 } 1600202b29eSdanielk1977 1610202b29eSdanielk1977 /* 162be5c89acSdrh ** Generate code for a comparison operator. 163be5c89acSdrh */ 164be5c89acSdrh static int codeCompare( 165be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 166be5c89acSdrh Expr *pLeft, /* The left operand */ 167be5c89acSdrh Expr *pRight, /* The right operand */ 168be5c89acSdrh int opcode, /* The comparison opcode */ 169be5c89acSdrh int dest, /* Jump here if true. */ 170be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 171be5c89acSdrh ){ 172be5c89acSdrh int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); 173be5c89acSdrh CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight); 174be5c89acSdrh return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); 175be5c89acSdrh } 176be5c89acSdrh 177be5c89acSdrh /* 178a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 179a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 180a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 181a76b5dfcSdrh */ 182e4e72072Sdrh Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){ 183a76b5dfcSdrh Expr *pNew; 184a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 185a76b5dfcSdrh if( pNew==0 ){ 1864efc4754Sdrh /* When malloc fails, we leak memory from pLeft and pRight */ 187a76b5dfcSdrh return 0; 188a76b5dfcSdrh } 189a76b5dfcSdrh pNew->op = op; 190a76b5dfcSdrh pNew->pLeft = pLeft; 191a76b5dfcSdrh pNew->pRight = pRight; 192a76b5dfcSdrh if( pToken ){ 1934b59ab5eSdrh assert( pToken->dyn==0 ); 194145716b3Sdrh pNew->span = pNew->token = *pToken; 195145716b3Sdrh }else if( pLeft && pRight ){ 1964adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 197a76b5dfcSdrh } 198a76b5dfcSdrh return pNew; 199a76b5dfcSdrh } 200a76b5dfcSdrh 201a76b5dfcSdrh /* 2024e0cff60Sdrh ** When doing a nested parse, you can include terms in an expression 2034e0cff60Sdrh ** that look like this: #0 #1 #2 ... These terms refer to elements 2044e0cff60Sdrh ** on the stack. "#0" (or just "#") means the top of the stack. 2052958a4e6Sdrh ** "#1" means the next down on the stack. And so forth. #-1 means 2062958a4e6Sdrh ** memory location 0. #-2 means memory location 1. And so forth. 2074e0cff60Sdrh ** 2084e0cff60Sdrh ** This routine is called by the parser to deal with on of those terms. 2094e0cff60Sdrh ** It immediately generates code to store the value in a memory location. 2104e0cff60Sdrh ** The returns an expression that will code to extract the value from 2114e0cff60Sdrh ** that memory location as needed. 2124e0cff60Sdrh */ 2134e0cff60Sdrh Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ 2144e0cff60Sdrh Vdbe *v = pParse->pVdbe; 2154e0cff60Sdrh Expr *p; 2164e0cff60Sdrh int depth; 2174e0cff60Sdrh if( v==0 ) return 0; 2184e0cff60Sdrh if( pParse->nested==0 ){ 2194e0cff60Sdrh sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); 2204e0cff60Sdrh return 0; 2214e0cff60Sdrh } 2224e0cff60Sdrh p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); 22373c42a13Sdrh if( p==0 ){ 22473c42a13Sdrh return 0; /* Malloc failed */ 22573c42a13Sdrh } 2264e0cff60Sdrh depth = atoi(&pToken->z[1]); 2272958a4e6Sdrh if( depth>=0 ){ 2284e0cff60Sdrh p->iTable = pParse->nMem++; 2294e0cff60Sdrh sqlite3VdbeAddOp(v, OP_Dup, depth, 0); 2304e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1); 2312958a4e6Sdrh }else{ 2322958a4e6Sdrh p->iTable = -1-depth; 2332958a4e6Sdrh } 2344e0cff60Sdrh return p; 2354e0cff60Sdrh } 2364e0cff60Sdrh 2374e0cff60Sdrh /* 23891bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 23991bb0eedSdrh ** NULL, then just return the other expression. 24091bb0eedSdrh */ 24191bb0eedSdrh Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){ 24291bb0eedSdrh if( pLeft==0 ){ 24391bb0eedSdrh return pRight; 24491bb0eedSdrh }else if( pRight==0 ){ 24591bb0eedSdrh return pLeft; 24691bb0eedSdrh }else{ 24791bb0eedSdrh return sqlite3Expr(TK_AND, pLeft, pRight, 0); 24891bb0eedSdrh } 24991bb0eedSdrh } 25091bb0eedSdrh 25191bb0eedSdrh /* 2526977fea8Sdrh ** Set the Expr.span field of the given expression to span all 253a76b5dfcSdrh ** text between the two given tokens. 254a76b5dfcSdrh */ 2554adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 2564efc4754Sdrh assert( pRight!=0 ); 2574efc4754Sdrh assert( pLeft!=0 ); 25871c697efSdrh if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){ 259ad6d9460Sdrh assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 ); 260145716b3Sdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 2616977fea8Sdrh pExpr->span.z = pLeft->z; 2626977fea8Sdrh pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); 2634b59ab5eSdrh }else{ 2646977fea8Sdrh pExpr->span.z = 0; 2654b59ab5eSdrh } 266a76b5dfcSdrh } 267a76b5dfcSdrh } 268a76b5dfcSdrh 269a76b5dfcSdrh /* 270a76b5dfcSdrh ** Construct a new expression node for a function with multiple 271a76b5dfcSdrh ** arguments. 272a76b5dfcSdrh */ 2734adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 274a76b5dfcSdrh Expr *pNew; 275a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 276a76b5dfcSdrh if( pNew==0 ){ 2774adee20fSdanielk1977 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */ 278a76b5dfcSdrh return 0; 279a76b5dfcSdrh } 280a76b5dfcSdrh pNew->op = TK_FUNCTION; 281a76b5dfcSdrh pNew->pList = pList; 282a76b5dfcSdrh if( pToken ){ 2834b59ab5eSdrh assert( pToken->dyn==0 ); 284a76b5dfcSdrh pNew->token = *pToken; 285a76b5dfcSdrh }else{ 286a76b5dfcSdrh pNew->token.z = 0; 287a76b5dfcSdrh } 2886977fea8Sdrh pNew->span = pNew->token; 289a76b5dfcSdrh return pNew; 290a76b5dfcSdrh } 291a76b5dfcSdrh 292a76b5dfcSdrh /* 293fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 294fa6bc000Sdrh ** in the original SQL statement. 295fa6bc000Sdrh ** 296fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 297fa6bc000Sdrh ** variable number. 298fa6bc000Sdrh ** 299fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 300fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 301fa6bc000Sdrh ** the SQL statement comes from an external source. 302fa6bc000Sdrh ** 303fa6bc000Sdrh ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number 304fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 305fa6bc000Sdrh ** instance of the wildcard, the next sequenial variable number is 306fa6bc000Sdrh ** assigned. 307fa6bc000Sdrh */ 308fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 309fa6bc000Sdrh Token *pToken; 310fa6bc000Sdrh if( pExpr==0 ) return; 311fa6bc000Sdrh pToken = &pExpr->token; 312fa6bc000Sdrh assert( pToken->n>=1 ); 313fa6bc000Sdrh assert( pToken->z!=0 ); 314fa6bc000Sdrh assert( pToken->z[0]!=0 ); 315fa6bc000Sdrh if( pToken->n==1 ){ 316fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 317fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 318fa6bc000Sdrh }else if( pToken->z[0]=='?' ){ 319fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 320fa6bc000Sdrh ** use it as the variable number */ 321fa6bc000Sdrh int i; 322fa6bc000Sdrh pExpr->iTable = i = atoi(&pToken->z[1]); 323fa6bc000Sdrh if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){ 324fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 325fa6bc000Sdrh SQLITE_MAX_VARIABLE_NUMBER); 326fa6bc000Sdrh } 327fa6bc000Sdrh if( i>pParse->nVar ){ 328fa6bc000Sdrh pParse->nVar = i; 329fa6bc000Sdrh } 330fa6bc000Sdrh }else{ 331fa6bc000Sdrh /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable 332fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 333fa6bc000Sdrh ** has never appeared before, reuse the same variable number 334fa6bc000Sdrh */ 335fa6bc000Sdrh int i, n; 336fa6bc000Sdrh n = pToken->n; 337fa6bc000Sdrh for(i=0; i<pParse->nVarExpr; i++){ 338fa6bc000Sdrh Expr *pE; 339fa6bc000Sdrh if( (pE = pParse->apVarExpr[i])!=0 340fa6bc000Sdrh && pE->token.n==n 341fa6bc000Sdrh && memcmp(pE->token.z, pToken->z, n)==0 ){ 342fa6bc000Sdrh pExpr->iTable = pE->iTable; 343fa6bc000Sdrh break; 344fa6bc000Sdrh } 345fa6bc000Sdrh } 346fa6bc000Sdrh if( i>=pParse->nVarExpr ){ 347fa6bc000Sdrh pExpr->iTable = ++pParse->nVar; 348fa6bc000Sdrh if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ 349fa6bc000Sdrh pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; 350fa6bc000Sdrh pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr, 351fa6bc000Sdrh pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) ); 352fa6bc000Sdrh } 353fa6bc000Sdrh if( !sqlite3_malloc_failed ){ 354fa6bc000Sdrh assert( pParse->apVarExpr!=0 ); 355fa6bc000Sdrh pParse->apVarExpr[pParse->nVarExpr++] = pExpr; 356fa6bc000Sdrh } 357fa6bc000Sdrh } 358fa6bc000Sdrh } 359fa6bc000Sdrh } 360fa6bc000Sdrh 361fa6bc000Sdrh /* 362a2e00042Sdrh ** Recursively delete an expression tree. 363a2e00042Sdrh */ 3644adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 365a2e00042Sdrh if( p==0 ) return; 3664efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 3674efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 3684adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 3694adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 3704adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 3714adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 372a2e00042Sdrh sqliteFree(p); 373a2e00042Sdrh } 374a2e00042Sdrh 375a76b5dfcSdrh 376a76b5dfcSdrh /* 377ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 378ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 379ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 380ff78bd2fSdrh ** without effecting the originals. 381ff78bd2fSdrh ** 3824adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 3834adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 384ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 385ff78bd2fSdrh ** 386ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 387ff78bd2fSdrh */ 3884adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 389ff78bd2fSdrh Expr *pNew; 390ff78bd2fSdrh if( p==0 ) return 0; 391fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 392ff78bd2fSdrh if( pNew==0 ) return 0; 3933b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 3946977fea8Sdrh if( p->token.z!=0 ){ 395b9ecf6faSdrh pNew->token.z = sqliteStrNDup(p->token.z, p->token.n); 3964b59ab5eSdrh pNew->token.dyn = 1; 3974b59ab5eSdrh }else{ 3984efc4754Sdrh assert( pNew->token.z==0 ); 3994b59ab5eSdrh } 4006977fea8Sdrh pNew->span.z = 0; 4014adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 4024adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 4034adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 4044adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 405ff78bd2fSdrh return pNew; 406ff78bd2fSdrh } 4074adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 4084b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 4094b59ab5eSdrh if( pFrom->z ){ 4104b59ab5eSdrh pTo->n = pFrom->n; 4114b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 4124b59ab5eSdrh pTo->dyn = 1; 4134b59ab5eSdrh }else{ 4144b59ab5eSdrh pTo->z = 0; 4154b59ab5eSdrh } 4164b59ab5eSdrh } 4174adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 418ff78bd2fSdrh ExprList *pNew; 419145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 420ff78bd2fSdrh int i; 421ff78bd2fSdrh if( p==0 ) return 0; 422ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 423ff78bd2fSdrh if( pNew==0 ) return 0; 4244305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 4253e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 426e0048400Sdanielk1977 if( pItem==0 ){ 427e0048400Sdanielk1977 sqliteFree(pNew); 428e0048400Sdanielk1977 return 0; 429e0048400Sdanielk1977 } 430145716b3Sdrh pOldItem = p->a; 431145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 4324b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 433145716b3Sdrh pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr); 4346977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 4356977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 4364b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 4374b59ab5eSdrh ** the names of columns in the result set needs this information */ 4384adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 4394b59ab5eSdrh } 4401f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 44124b03fd0Sdanielk1977 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 442145716b3Sdrh pItem->zName = sqliteStrDup(pOldItem->zName); 443145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 444145716b3Sdrh pItem->isAgg = pOldItem->isAgg; 4453e7bc9caSdrh pItem->done = 0; 446ff78bd2fSdrh } 447ff78bd2fSdrh return pNew; 448ff78bd2fSdrh } 4494adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 450ad3cab52Sdrh SrcList *pNew; 451ad3cab52Sdrh int i; 452113088ecSdrh int nByte; 453ad3cab52Sdrh if( p==0 ) return 0; 454113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 4554efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 456ad3cab52Sdrh if( pNew==0 ) return 0; 4574305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 458ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 4594efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 4604efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 4614efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 4624efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 4634efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 4644efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 4654efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 4664efc4754Sdrh pNewItem->pTab = 0; 4674adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 4684adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 4694adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 470ad3cab52Sdrh } 471ad3cab52Sdrh return pNew; 472ad3cab52Sdrh } 4734adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 474ff78bd2fSdrh IdList *pNew; 475ff78bd2fSdrh int i; 476ff78bd2fSdrh if( p==0 ) return 0; 4774efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 478ff78bd2fSdrh if( pNew==0 ) return 0; 4794305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 4804efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 481e4697f5eSdrh if( pNew->a==0 ) return 0; 482ff78bd2fSdrh for(i=0; i<p->nId; i++){ 4834efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 4844efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 4854efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 4864efc4754Sdrh pNewItem->idx = pOldItem->idx; 487ff78bd2fSdrh } 488ff78bd2fSdrh return pNew; 489ff78bd2fSdrh } 4904adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 491ff78bd2fSdrh Select *pNew; 492ff78bd2fSdrh if( p==0 ) return 0; 4934efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 494ff78bd2fSdrh if( pNew==0 ) return 0; 495ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 4964adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 4974adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 4984adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 4994adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 5004adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 5014adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 502ff78bd2fSdrh pNew->op = p->op; 5034adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 504ff78bd2fSdrh pNew->nLimit = p->nLimit; 505ff78bd2fSdrh pNew->nOffset = p->nOffset; 5067b58daeaSdrh pNew->iLimit = -1; 5077b58daeaSdrh pNew->iOffset = -1; 508dc1bdc4fSdanielk1977 pNew->ppOpenTemp = 0; 509b6c29897Sdrh pNew->pFetch = 0; 510ff78bd2fSdrh return pNew; 511ff78bd2fSdrh } 512ff78bd2fSdrh 513ff78bd2fSdrh 514ff78bd2fSdrh /* 515a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 516a76b5dfcSdrh ** initially NULL, then create a new expression list. 517a76b5dfcSdrh */ 5184adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 519a76b5dfcSdrh if( pList==0 ){ 520a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 521a76b5dfcSdrh if( pList==0 ){ 5224adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 523a76b5dfcSdrh return 0; 524a76b5dfcSdrh } 5254efc4754Sdrh assert( pList->nAlloc==0 ); 526a76b5dfcSdrh } 5274305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 5284305d103Sdrh pList->nAlloc = pList->nAlloc*2 + 4; 5294efc4754Sdrh pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); 5304efc4754Sdrh if( pList->a==0 ){ 5314adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 5324efc4754Sdrh pList->nExpr = pList->nAlloc = 0; 533a76b5dfcSdrh return pList; 534a76b5dfcSdrh } 535a76b5dfcSdrh } 5364efc4754Sdrh assert( pList->a!=0 ); 5374efc4754Sdrh if( pExpr || pName ){ 5384efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 5394efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 5404efc4754Sdrh pItem->pExpr = pExpr; 541a99db3b6Sdrh pItem->zName = sqlite3NameFromToken(pName); 542a76b5dfcSdrh } 543a76b5dfcSdrh return pList; 544a76b5dfcSdrh } 545a76b5dfcSdrh 546a76b5dfcSdrh /* 547a76b5dfcSdrh ** Delete an entire expression list. 548a76b5dfcSdrh */ 5494adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 550a76b5dfcSdrh int i; 551be5c89acSdrh struct ExprList_item *pItem; 552a76b5dfcSdrh if( pList==0 ) return; 5531bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 5541bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 555be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 556be5c89acSdrh sqlite3ExprDelete(pItem->pExpr); 557be5c89acSdrh sqliteFree(pItem->zName); 558a76b5dfcSdrh } 559a76b5dfcSdrh sqliteFree(pList->a); 560a76b5dfcSdrh sqliteFree(pList); 561a76b5dfcSdrh } 562a76b5dfcSdrh 563a76b5dfcSdrh /* 564626a879aSdrh ** Walk an expression tree. Call xFunc for each node visited. 56573b211abSdrh ** 566626a879aSdrh ** The return value from xFunc determines whether the tree walk continues. 567626a879aSdrh ** 0 means continue walking the tree. 1 means do not walk children 568626a879aSdrh ** of the current node but continue with siblings. 2 means abandon 569626a879aSdrh ** the tree walk completely. 570626a879aSdrh ** 571626a879aSdrh ** The return value from this routine is 1 to abandon the tree walk 572626a879aSdrh ** and 0 to continue. 573626a879aSdrh */ 574626a879aSdrh static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){ 575626a879aSdrh ExprList *pList; 576626a879aSdrh int rc; 577626a879aSdrh if( pExpr==0 ) return 0; 578626a879aSdrh rc = (*xFunc)(pArg, pExpr); 579626a879aSdrh if( rc==0 ){ 580626a879aSdrh if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1; 581626a879aSdrh if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1; 582626a879aSdrh pList = pExpr->pList; 583626a879aSdrh if( pList ){ 584626a879aSdrh int i; 585626a879aSdrh struct ExprList_item *pItem; 586626a879aSdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 587626a879aSdrh if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1; 588626a879aSdrh } 589626a879aSdrh } 590626a879aSdrh } 591626a879aSdrh return rc>1; 592626a879aSdrh } 593626a879aSdrh 594626a879aSdrh /* 595626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 596626a879aSdrh ** 597626a879aSdrh ** pArg is really a pointer to an integer. If we can tell by looking 59873b211abSdrh ** at pExpr that the expression that contains pExpr is not a constant 59973b211abSdrh ** expression, then set *pArg to 0 and return 2 to abandon the tree walk. 60073b211abSdrh ** If pExpr does does not disqualify the expression from being a constant 60173b211abSdrh ** then do nothing. 60273b211abSdrh ** 60373b211abSdrh ** After walking the whole tree, if no nodes are found that disqualify 60473b211abSdrh ** the expression as constant, then we assume the whole expression 60573b211abSdrh ** is constant. See sqlite3ExprIsConstant() for additional information. 606626a879aSdrh */ 607626a879aSdrh static int exprNodeIsConstant(void *pArg, Expr *pExpr){ 608626a879aSdrh switch( pExpr->op ){ 609626a879aSdrh case TK_ID: 610626a879aSdrh case TK_COLUMN: 611626a879aSdrh case TK_DOT: 612626a879aSdrh case TK_AGG_FUNCTION: 613626a879aSdrh case TK_FUNCTION: 614626a879aSdrh *((int*)pArg) = 0; 615626a879aSdrh return 2; 616626a879aSdrh default: 617626a879aSdrh return 0; 618626a879aSdrh } 619626a879aSdrh } 620626a879aSdrh 621626a879aSdrh /* 622fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 623fef5208cSdrh ** and 0 if it involves variables. 6242398937bSdrh ** 6252398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 6262398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 6272398937bSdrh ** a constant. 628fef5208cSdrh */ 6294adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 630626a879aSdrh int isConst = 1; 631626a879aSdrh walkExprTree(p, exprNodeIsConstant, &isConst); 632626a879aSdrh return isConst; 633fef5208cSdrh } 634fef5208cSdrh 635fef5208cSdrh /* 63673b211abSdrh ** If the expression p codes a constant integer that is small enough 637202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 638202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 639202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 640e4de1febSdrh */ 6414adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 642e4de1febSdrh switch( p->op ){ 643e4de1febSdrh case TK_INTEGER: { 644fec19aadSdrh if( sqlite3GetInt32(p->token.z, pValue) ){ 645e4de1febSdrh return 1; 646e4de1febSdrh } 647202b2df7Sdrh break; 648202b2df7Sdrh } 6494b59ab5eSdrh case TK_UPLUS: { 6504adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 6514b59ab5eSdrh } 652e4de1febSdrh case TK_UMINUS: { 653e4de1febSdrh int v; 6544adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 655e4de1febSdrh *pValue = -v; 656e4de1febSdrh return 1; 657e4de1febSdrh } 658e4de1febSdrh break; 659e4de1febSdrh } 660e4de1febSdrh default: break; 661e4de1febSdrh } 662e4de1febSdrh return 0; 663e4de1febSdrh } 664e4de1febSdrh 665e4de1febSdrh /* 666c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 667c4a3c779Sdrh */ 6684adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 6694adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 6704adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 6714adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 672c4a3c779Sdrh return 0; 673c4a3c779Sdrh } 674c4a3c779Sdrh 675c4a3c779Sdrh /* 6768141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 6778141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 6788141f61eSdrh ** expression node refer back to that source column. The following changes 6798141f61eSdrh ** are made to pExpr: 6808141f61eSdrh ** 6818141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 6828141f61eSdrh ** the table. 6838141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 6848141f61eSdrh ** from pSrcList. 6858141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 6868141f61eSdrh ** pExpr->op Set to TK_COLUMN. 6878141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 6888141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 6898141f61eSdrh ** 6908141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 6918141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 6928141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 6938141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 6948141f61eSdrh ** means that the form of the name is Z and that columns from any table 6958141f61eSdrh ** can be used. 6968141f61eSdrh ** 6978141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 6988141f61eSdrh ** in pParse and return non-zero. Return zero on success. 6998141f61eSdrh */ 7008141f61eSdrh static int lookupName( 7018141f61eSdrh Parse *pParse, /* The parsing context */ 7028141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 7038141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 7048141f61eSdrh Token *pColumnToken, /* Name of the column. */ 705626a879aSdrh NameContext *pNC, /* The name context used to resolve the name */ 7068141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 7078141f61eSdrh ){ 7088141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 7098141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 7108141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 7118141f61eSdrh int i, j; /* Loop counters */ 7128141f61eSdrh int cnt = 0; /* Number of matching column names */ 7138141f61eSdrh int cntTab = 0; /* Number of matching table names */ 7149bb575fdSdrh sqlite3 *db = pParse->db; /* The database */ 71551669863Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 71651669863Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 71773b211abSdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 7188141f61eSdrh 7198141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 720a99db3b6Sdrh zDb = sqlite3NameFromToken(pDbToken); 721a99db3b6Sdrh zTab = sqlite3NameFromToken(pTableToken); 722a99db3b6Sdrh zCol = sqlite3NameFromToken(pColumnToken); 72324b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 7248141f61eSdrh return 1; /* Leak memory (zDb and zTab) if malloc fails */ 7258141f61eSdrh } 7268141f61eSdrh 7278141f61eSdrh pExpr->iTable = -1; 728626a879aSdrh while( pNC && cnt==0 ){ 729626a879aSdrh SrcList *pSrcList = pNC->pSrcList; 730626a879aSdrh ExprList *pEList = pNC->pEList; 731626a879aSdrh 732626a879aSdrh pNC->nRef++; 733626a879aSdrh /* assert( zTab==0 || pEList==0 ); */ 73451669863Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 7358141f61eSdrh Table *pTab = pItem->pTab; 7368141f61eSdrh Column *pCol; 7378141f61eSdrh 7388141f61eSdrh if( pTab==0 ) continue; 7398141f61eSdrh assert( pTab->nCol>0 ); 7408141f61eSdrh if( zTab ){ 7418141f61eSdrh if( pItem->zAlias ){ 7428141f61eSdrh char *zTabName = pItem->zAlias; 7434adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 7448141f61eSdrh }else{ 7458141f61eSdrh char *zTabName = pTab->zName; 7464adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 7474adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 7488141f61eSdrh continue; 7498141f61eSdrh } 7508141f61eSdrh } 7518141f61eSdrh } 7528141f61eSdrh if( 0==(cntTab++) ){ 7538141f61eSdrh pExpr->iTable = pItem->iCursor; 7548141f61eSdrh pExpr->iDb = pTab->iDb; 75551669863Sdrh pMatch = pItem; 7568141f61eSdrh } 7578141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 7584adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 7598141f61eSdrh cnt++; 7608141f61eSdrh pExpr->iTable = pItem->iCursor; 76151669863Sdrh pMatch = pItem; 7628141f61eSdrh pExpr->iDb = pTab->iDb; 7638141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 7648141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 765a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 7660202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 7678141f61eSdrh break; 7688141f61eSdrh } 7698141f61eSdrh } 7708141f61eSdrh } 7718141f61eSdrh 772b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 7738141f61eSdrh /* If we have not already resolved the name, then maybe 7748141f61eSdrh ** it is a new.* or old.* trigger argument reference 7758141f61eSdrh */ 7768141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 7778141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 7788141f61eSdrh Table *pTab = 0; 7794adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 7808141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 7818141f61eSdrh assert( pTriggerStack->pTab ); 7828141f61eSdrh pTab = pTriggerStack->pTab; 7834adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ 7848141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 7858141f61eSdrh assert( pTriggerStack->pTab ); 7868141f61eSdrh pTab = pTriggerStack->pTab; 7878141f61eSdrh } 7888141f61eSdrh 7898141f61eSdrh if( pTab ){ 7908141f61eSdrh int j; 7918141f61eSdrh Column *pCol = pTab->aCol; 7928141f61eSdrh 7938141f61eSdrh pExpr->iDb = pTab->iDb; 7948141f61eSdrh cntTab++; 7958141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 7964adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 7978141f61eSdrh cnt++; 7988141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 799a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 8000202b29eSdanielk1977 pExpr->pColl = pTab->aCol[j].pColl; 8018141f61eSdrh break; 8028141f61eSdrh } 8038141f61eSdrh } 8048141f61eSdrh } 8058141f61eSdrh } 806b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 8078141f61eSdrh 8088141f61eSdrh /* 8098141f61eSdrh ** Perhaps the name is a reference to the ROWID 8108141f61eSdrh */ 8114adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 8128141f61eSdrh cnt = 1; 8138141f61eSdrh pExpr->iColumn = -1; 814a37cdde0Sdanielk1977 pExpr->affinity = SQLITE_AFF_INTEGER; 8158141f61eSdrh } 8168141f61eSdrh 8178141f61eSdrh /* 8188141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 8198141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 8208141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 8218141f61eSdrh ** 8228141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 8238141f61eSdrh ** 8248141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 8258141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 8268141f61eSdrh ** Note that the expression in the result set should have already been 8278141f61eSdrh ** resolved by the time the WHERE clause is resolved. 8288141f61eSdrh */ 82979d5f63fSdrh if( cnt==0 && pEList!=0 && zTab==0 ){ 8308141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 8318141f61eSdrh char *zAs = pEList->a[j].zName; 8324adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 8338141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 8348141f61eSdrh pExpr->op = TK_AS; 8358141f61eSdrh pExpr->iColumn = j; 8364adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 8378141f61eSdrh sqliteFree(zCol); 8388141f61eSdrh assert( zTab==0 && zDb==0 ); 8398141f61eSdrh return 0; 8408141f61eSdrh } 8418141f61eSdrh } 8428141f61eSdrh } 8438141f61eSdrh 844626a879aSdrh /* Advance to the next name context. The loop will exit when either 845626a879aSdrh ** we have a match (cnt>0) or when we run out of name contexts. 846626a879aSdrh */ 847626a879aSdrh if( cnt==0 ){ 848626a879aSdrh pNC = pNC->pNext; 849626a879aSdrh } 850626a879aSdrh } 851626a879aSdrh 8528141f61eSdrh /* 8538141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 8548141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 8558141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 8568141f61eSdrh ** case, we need to return right away and not make any changes to 8578141f61eSdrh ** pExpr. 8588141f61eSdrh */ 8598141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 8608141f61eSdrh sqliteFree(zCol); 8618141f61eSdrh return 0; 8628141f61eSdrh } 8638141f61eSdrh 8648141f61eSdrh /* 8658141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 8668141f61eSdrh ** more matches. Either way, we have an error. 8678141f61eSdrh */ 8688141f61eSdrh if( cnt!=1 ){ 8698141f61eSdrh char *z = 0; 8708141f61eSdrh char *zErr; 8718141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 8728141f61eSdrh if( zDb ){ 8734adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 8748141f61eSdrh }else if( zTab ){ 8754adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 8768141f61eSdrh }else{ 8778141f61eSdrh z = sqliteStrDup(zCol); 8788141f61eSdrh } 8794adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 8808141f61eSdrh sqliteFree(z); 88173b211abSdrh pTopNC->nErr++; 8828141f61eSdrh } 8838141f61eSdrh 88451669863Sdrh /* If a column from a table in pSrcList is referenced, then record 88551669863Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 88651669863Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 88751669863Sdrh ** column number is greater than the number of bits in the bitmask 88851669863Sdrh ** then set the high-order bit of the bitmask. 88951669863Sdrh */ 89051669863Sdrh if( pExpr->iColumn>=0 && pMatch!=0 ){ 89151669863Sdrh int n = pExpr->iColumn; 89251669863Sdrh if( n>=sizeof(Bitmask)*8 ){ 89351669863Sdrh n = sizeof(Bitmask)*8-1; 89451669863Sdrh } 89551669863Sdrh assert( pMatch->iCursor==pExpr->iTable ); 89651669863Sdrh pMatch->colUsed |= 1<<n; 89751669863Sdrh } 89851669863Sdrh 8998141f61eSdrh /* Clean up and return 9008141f61eSdrh */ 9018141f61eSdrh sqliteFree(zDb); 9028141f61eSdrh sqliteFree(zTab); 9038141f61eSdrh sqliteFree(zCol); 9044adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 9058141f61eSdrh pExpr->pLeft = 0; 9064adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 9078141f61eSdrh pExpr->pRight = 0; 9088141f61eSdrh pExpr->op = TK_COLUMN; 909626a879aSdrh if( cnt==1 ){ 910626a879aSdrh assert( pNC!=0 && pNC->pSrcList!=0 ); 911626a879aSdrh sqlite3AuthRead(pParse, pExpr, pNC->pSrcList); 912626a879aSdrh } 9138141f61eSdrh return cnt!=1; 9148141f61eSdrh } 9158141f61eSdrh 9168141f61eSdrh /* 917626a879aSdrh ** pExpr is a node that defines a function of some kind. It might 918626a879aSdrh ** be a syntactic function like "count(x)" or it might be a function 919626a879aSdrh ** that implements an operator, like "a LIKE b". 920626a879aSdrh ** 921626a879aSdrh ** This routine makes *pzName point to the name of the function and 922626a879aSdrh ** *pnName hold the number of characters in the function name. 923626a879aSdrh */ 924626a879aSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 925626a879aSdrh switch( pExpr->op ){ 926626a879aSdrh case TK_FUNCTION: { 927626a879aSdrh *pzName = pExpr->token.z; 928626a879aSdrh *pnName = pExpr->token.n; 929626a879aSdrh break; 930626a879aSdrh } 931626a879aSdrh case TK_LIKE: { 932626a879aSdrh *pzName = "like"; 933626a879aSdrh *pnName = 4; 934626a879aSdrh break; 935626a879aSdrh } 936626a879aSdrh case TK_GLOB: { 937626a879aSdrh *pzName = "glob"; 938626a879aSdrh *pnName = 4; 939626a879aSdrh break; 940626a879aSdrh } 941626a879aSdrh case TK_CTIME: { 942626a879aSdrh *pzName = "current_time"; 943626a879aSdrh *pnName = 12; 944626a879aSdrh break; 945626a879aSdrh } 946626a879aSdrh case TK_CDATE: { 947626a879aSdrh *pzName = "current_date"; 948626a879aSdrh *pnName = 12; 949626a879aSdrh break; 950626a879aSdrh } 951626a879aSdrh case TK_CTIMESTAMP: { 952626a879aSdrh *pzName = "current_timestamp"; 953626a879aSdrh *pnName = 17; 954626a879aSdrh break; 955626a879aSdrh } 956626a879aSdrh } 957626a879aSdrh } 958626a879aSdrh 959626a879aSdrh /* 960626a879aSdrh ** This routine is designed as an xFunc for walkExprTree(). 961626a879aSdrh ** 96273b211abSdrh ** Resolve symbolic names into TK_COLUMN operators for the current 963626a879aSdrh ** node in the expression tree. Return 0 to continue the search down 96473b211abSdrh ** the tree or 2 to abort the tree walk. 96573b211abSdrh ** 96673b211abSdrh ** This routine also does error checking and name resolution for 96773b211abSdrh ** function names. The operator for aggregate functions is changed 96873b211abSdrh ** to TK_AGG_FUNCTION. 969626a879aSdrh */ 970626a879aSdrh static int nameResolverStep(void *pArg, Expr *pExpr){ 971626a879aSdrh NameContext *pNC = (NameContext*)pArg; 972626a879aSdrh SrcList *pSrcList; 973626a879aSdrh Parse *pParse; 974626a879aSdrh int i; 975626a879aSdrh 976626a879aSdrh assert( pNC!=0 ); 977626a879aSdrh pSrcList = pNC->pSrcList; 978626a879aSdrh pParse = pNC->pParse; 979626a879aSdrh if( pExpr==0 ) return 1; 980626a879aSdrh if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1; 981626a879aSdrh ExprSetProperty(pExpr, EP_Resolved); 982626a879aSdrh #ifndef NDEBUG 983626a879aSdrh if( pSrcList ){ 984626a879aSdrh for(i=0; i<pSrcList->nSrc; i++){ 985626a879aSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 986626a879aSdrh } 987626a879aSdrh } 988626a879aSdrh #endif 989626a879aSdrh switch( pExpr->op ){ 990626a879aSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 991626a879aSdrh ** possible. Otherwise they remain as strings. Single-quoted 992626a879aSdrh ** strings (ex: 'abc') are always string literals. 993626a879aSdrh */ 994626a879aSdrh case TK_STRING: { 995626a879aSdrh if( pExpr->token.z[0]=='\'' ) break; 996626a879aSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 997626a879aSdrh } 998626a879aSdrh /* A lone identifier is the name of a column. 999626a879aSdrh */ 1000626a879aSdrh case TK_ID: { 1001626a879aSdrh if( pSrcList==0 ) break; 1002626a879aSdrh lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); 1003626a879aSdrh return 1; 1004626a879aSdrh } 1005626a879aSdrh 1006626a879aSdrh /* A table name and column name: ID.ID 1007626a879aSdrh ** Or a database, table and column: ID.ID.ID 1008626a879aSdrh */ 1009626a879aSdrh case TK_DOT: { 1010626a879aSdrh Token *pColumn; 1011626a879aSdrh Token *pTable; 1012626a879aSdrh Token *pDb; 1013626a879aSdrh Expr *pRight; 1014626a879aSdrh 1015626a879aSdrh if( pSrcList==0 ) break; 1016626a879aSdrh pRight = pExpr->pRight; 1017626a879aSdrh if( pRight->op==TK_ID ){ 1018626a879aSdrh pDb = 0; 1019626a879aSdrh pTable = &pExpr->pLeft->token; 1020626a879aSdrh pColumn = &pRight->token; 1021626a879aSdrh }else{ 1022626a879aSdrh assert( pRight->op==TK_DOT ); 1023626a879aSdrh pDb = &pExpr->pLeft->token; 1024626a879aSdrh pTable = &pRight->pLeft->token; 1025626a879aSdrh pColumn = &pRight->pRight->token; 1026626a879aSdrh } 1027626a879aSdrh lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); 1028626a879aSdrh return 1; 1029626a879aSdrh } 1030626a879aSdrh 1031626a879aSdrh /* Resolve function names 1032626a879aSdrh */ 1033626a879aSdrh case TK_CTIME: 1034626a879aSdrh case TK_CTIMESTAMP: 1035626a879aSdrh case TK_CDATE: 1036626a879aSdrh /* Note: The above three were a seperate case in sqlmoto. Reason? */ 1037626a879aSdrh case TK_GLOB: 1038626a879aSdrh case TK_LIKE: 1039626a879aSdrh case TK_FUNCTION: { 1040626a879aSdrh ExprList *pList = pExpr->pList; /* The argument list */ 1041626a879aSdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 1042626a879aSdrh int no_such_func = 0; /* True if no such function exists */ 1043626a879aSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 1044626a879aSdrh int is_agg = 0; /* True if is an aggregate function */ 1045626a879aSdrh int i; 1046626a879aSdrh int nId; /* Number of characters in function name */ 1047626a879aSdrh const char *zId; /* The function name. */ 104873b211abSdrh FuncDef *pDef; /* Information about the function */ 104973b211abSdrh int enc = pParse->db->enc; /* The database encoding */ 1050626a879aSdrh 1051626a879aSdrh getFunctionName(pExpr, &zId, &nId); 1052626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 1053626a879aSdrh if( pDef==0 ){ 1054626a879aSdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); 1055626a879aSdrh if( pDef==0 ){ 1056626a879aSdrh no_such_func = 1; 1057626a879aSdrh }else{ 1058626a879aSdrh wrong_num_args = 1; 1059626a879aSdrh } 1060626a879aSdrh }else{ 1061626a879aSdrh is_agg = pDef->xFunc==0; 1062626a879aSdrh } 1063626a879aSdrh if( is_agg && !pNC->allowAgg ){ 1064626a879aSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 1065626a879aSdrh pNC->nErr++; 1066626a879aSdrh is_agg = 0; 1067626a879aSdrh }else if( no_such_func ){ 1068626a879aSdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 1069626a879aSdrh pNC->nErr++; 1070626a879aSdrh }else if( wrong_num_args ){ 1071626a879aSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 1072626a879aSdrh nId, zId); 1073626a879aSdrh pNC->nErr++; 1074626a879aSdrh } 1075626a879aSdrh if( is_agg ){ 1076626a879aSdrh pExpr->op = TK_AGG_FUNCTION; 1077626a879aSdrh pNC->hasAgg = 1; 1078626a879aSdrh } 107973b211abSdrh if( is_agg ) pNC->allowAgg = 0; 1080626a879aSdrh for(i=0; pNC->nErr==0 && i<n; i++){ 108173b211abSdrh walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC); 1082626a879aSdrh } 108373b211abSdrh if( is_agg ) pNC->allowAgg = 1; 1084626a879aSdrh /* FIX ME: Compute pExpr->affinity based on the expected return 1085626a879aSdrh ** type of the function 1086626a879aSdrh */ 1087626a879aSdrh return is_agg; 1088626a879aSdrh } 1089626a879aSdrh } 1090626a879aSdrh return 0; 1091626a879aSdrh } 1092626a879aSdrh 10931398ad36Sdrh /* Forward declaration */ 10941398ad36Sdrh static int sqlite3ExprCodeSubquery(Parse*, NameContext*, Expr*); 10951398ad36Sdrh 1096626a879aSdrh /* 1097cce7d176Sdrh ** This routine walks an expression tree and resolves references to 1098967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 1099aacc543eSdrh ** index to the table in the table list and a column offset. The 1100aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 1101aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 1102832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 1103aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 1104aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 1105aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 1106aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 1107aacc543eSdrh ** alias for ROWID. 110819a775c2Sdrh ** 1109626a879aSdrh ** Also resolve function names and check the functions for proper 1110626a879aSdrh ** usage. Make sure all function names are recognized and all functions 1111626a879aSdrh ** have the correct number of arguments. Leave an error message 1112626a879aSdrh ** in pParse->zErrMsg if anything is amiss. Return the number of errors. 1113626a879aSdrh ** 111473b211abSdrh ** If the expression contains aggregate functions then set the EP_Agg 111573b211abSdrh ** property on the expression. 1116626a879aSdrh */ 1117626a879aSdrh int sqlite3ExprResolveNames( 1118626a879aSdrh Parse *pParse, /* The parser context */ 1119626a879aSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 1120626a879aSdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 11211398ad36Sdrh NameContext *pNC, /* Namespace of enclosing statement */ 1122626a879aSdrh Expr *pExpr, /* The expression to be analyzed. */ 1123626a879aSdrh int allowAgg, /* True to allow aggregate expressions */ 1124626a879aSdrh int codeSubquery /* If true, then generate code for subqueries too */ 1125626a879aSdrh ){ 1126626a879aSdrh NameContext sNC; 1127626a879aSdrh 112873b211abSdrh if( pExpr==0 ) return 0; 1129626a879aSdrh memset(&sNC, 0, sizeof(sNC)); 1130626a879aSdrh sNC.pSrcList = pSrcList; 1131626a879aSdrh sNC.pParse = pParse; 1132626a879aSdrh sNC.pEList = pEList; 1133626a879aSdrh sNC.allowAgg = allowAgg; 11341398ad36Sdrh sNC.pNext = pNC; 1135626a879aSdrh walkExprTree(pExpr, nameResolverStep, &sNC); 113673b211abSdrh if( sNC.hasAgg ){ 113773b211abSdrh ExprSetProperty(pExpr, EP_Agg); 1138626a879aSdrh } 113973b211abSdrh if( sNC.nErr>0 ){ 114073b211abSdrh ExprSetProperty(pExpr, EP_Error); 11411398ad36Sdrh }else if( codeSubquery && sqlite3ExprCodeSubquery(pParse, &sNC, pExpr) ){ 114273b211abSdrh return 1; 114373b211abSdrh } 114473b211abSdrh return ExprHasProperty(pExpr, EP_Error); 1145626a879aSdrh } 1146626a879aSdrh 11471398ad36Sdrh /* 11481398ad36Sdrh ** A pointer instance of this structure is used to pass information 11491398ad36Sdrh ** through walkExprTree into codeSubqueryStep(). 11501398ad36Sdrh */ 11511398ad36Sdrh typedef struct QueryCoder QueryCoder; 11521398ad36Sdrh struct QueryCoder { 11531398ad36Sdrh Parse *pParse; /* The parsing context */ 11541398ad36Sdrh NameContext *pNC; /* Namespace of first enclosing query */ 11551398ad36Sdrh }; 11561398ad36Sdrh 1157626a879aSdrh 1158626a879aSdrh /* 1159626a879aSdrh ** Generate code for subqueries and IN operators. 1160626a879aSdrh ** 116173b211abSdrh ** IN operators comes in two forms: 1162fef5208cSdrh ** 1163fef5208cSdrh ** expr IN (exprlist) 1164fef5208cSdrh ** and 1165fef5208cSdrh ** expr IN (SELECT ...) 1166fef5208cSdrh ** 1167fef5208cSdrh ** The first form is handled by creating a set holding the list 1168fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 1169fef5208cSdrh ** a temporary table. 1170fef5208cSdrh ** 1171fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression. 117219a775c2Sdrh ** If it finds any, it generates code to write the value of that select 117319a775c2Sdrh ** into a memory cell. 117473b211abSdrh ** 117573b211abSdrh ** This routine is a callback for wallExprTree() used to implement 117673b211abSdrh ** sqlite3ExprCodeSubquery(). See comments on those routines for 117773b211abSdrh ** additional information. 1178cce7d176Sdrh */ 1179*51522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1180626a879aSdrh static int codeSubqueryStep(void *pArg, Expr *pExpr){ 11811398ad36Sdrh QueryCoder *pCoder = (QueryCoder*)pArg; 11821398ad36Sdrh Parse *pParse = pCoder->pParse; 11836a3ea0e6Sdrh 1184cce7d176Sdrh switch( pExpr->op ){ 1185fef5208cSdrh case TK_IN: { 1186e014a838Sdanielk1977 char affinity; 11874adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 1188d3d39e93Sdrh KeyInfo keyInfo; 11890202b29eSdanielk1977 int addr; /* Address of OP_OpenTemp instruction */ 1190d3d39e93Sdrh 1191626a879aSdrh if( v==0 ) return 2; 1192bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 1193e014a838Sdanielk1977 1194e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 1195e014a838Sdanielk1977 ** expression it is handled the same way. A temporary table is 1196e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1197e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1198fef5208cSdrh ** 1199e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1200e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1201e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1202e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1203e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1204e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1205e014a838Sdanielk1977 ** is used. 1206fef5208cSdrh */ 1207832508b7Sdrh pExpr->iTable = pParse->nTab++; 12080202b29eSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0); 1209d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 1210d3d39e93Sdrh keyInfo.nField = 1; 1211f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 1212e014a838Sdanielk1977 1213e014a838Sdanielk1977 if( pExpr->pSelect ){ 1214e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1215e014a838Sdanielk1977 ** 1216e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1217e014a838Sdanielk1977 ** table allocated and opened above. 1218e014a838Sdanielk1977 */ 1219e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 1220be5c89acSdrh ExprList *pEList; 1221e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 12221398ad36Sdrh sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0, 0); 1223be5c89acSdrh pEList = pExpr->pSelect->pEList; 1224be5c89acSdrh if( pEList && pEList->nExpr>0 ){ 12257cedc8d4Sdanielk1977 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft, 1226be5c89acSdrh pEList->a[0].pExpr); 12270202b29eSdanielk1977 } 1228fef5208cSdrh }else if( pExpr->pList ){ 1229fef5208cSdrh /* Case 2: expr IN (exprlist) 1230fef5208cSdrh ** 1231e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1232e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1233e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1234e014a838Sdanielk1977 ** a column, use numeric affinity. 1235fef5208cSdrh */ 1236e014a838Sdanielk1977 int i; 1237e014a838Sdanielk1977 if( !affinity ){ 1238e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 1239e014a838Sdanielk1977 } 12400202b29eSdanielk1977 keyInfo.aColl[0] = pExpr->pLeft->pColl; 1241e014a838Sdanielk1977 1242e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 1243fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 1244fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 1245e014a838Sdanielk1977 1246e014a838Sdanielk1977 /* Check that the expression is constant and valid. */ 12474adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 12484adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1249da93d238Sdrh "right-hand side of IN operator must be constant"); 1250626a879aSdrh return 2; 1251fef5208cSdrh } 12521398ad36Sdrh if( sqlite3ExprResolveNames(pParse, 0, 0, 0, pE2, 0, 0) ){ 1253626a879aSdrh return 2; 12544794b980Sdrh } 1255e014a838Sdanielk1977 1256e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 12574adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 125894a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); 12590f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1260e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0); 1261fef5208cSdrh } 1262fef5208cSdrh } 12630202b29eSdanielk1977 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); 1264626a879aSdrh return 1; 1265fef5208cSdrh } 1266fef5208cSdrh 1267*51522cd3Sdrh case TK_EXISTS: 126819a775c2Sdrh case TK_SELECT: { 1269fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 1270fef5208cSdrh ** value of this select in a memory cell and record the number 1271967e8b73Sdrh ** of the memory cell in iColumn. 1272fef5208cSdrh */ 12731398ad36Sdrh NameContext *pNC; 12741398ad36Sdrh int nRef; 12751398ad36Sdrh Vdbe *v; 12761398ad36Sdrh int addr; 1277*51522cd3Sdrh int sop; 1278*51522cd3Sdrh Select *pSel; 12791398ad36Sdrh 12801398ad36Sdrh pNC = pCoder->pNC; 12811398ad36Sdrh if( pNC ) nRef = pNC->nRef; 12823119bc42Sdrh sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ 12831398ad36Sdrh v = sqlite3GetVdbe(pParse); 12841398ad36Sdrh addr = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 1285967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 1286*51522cd3Sdrh pSel = pExpr->pSelect; 1287*51522cd3Sdrh if( pExpr->op==TK_SELECT ){ 1288*51522cd3Sdrh sop = SRT_Mem; 1289*51522cd3Sdrh }else{ 1290*51522cd3Sdrh static const Token one = { "1", 0, 1 }; 1291*51522cd3Sdrh sop = SRT_Exists; 1292*51522cd3Sdrh sqlite3ExprListDelete(pSel->pEList); 1293*51522cd3Sdrh pSel->pEList = sqlite3ExprListAppend(0, 1294*51522cd3Sdrh sqlite3Expr(TK_INTEGER, 0, 0, &one), 0); 1295*51522cd3Sdrh } 1296*51522cd3Sdrh sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0, pNC); 12971398ad36Sdrh if( pNC && pNC->nRef>nRef ){ 12981398ad36Sdrh /* Subquery value changes. Evaluate at each use */ 12991398ad36Sdrh pExpr->iTable = addr+1; 13001398ad36Sdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 13011398ad36Sdrh sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 13021398ad36Sdrh }else{ 13031398ad36Sdrh /* Subquery value is constant. evaluate only once. */ 13041398ad36Sdrh pExpr->iTable = -1; 13051398ad36Sdrh sqlite3VdbeChangeP2(v, addr, addr+1); 13061398ad36Sdrh } 130719a775c2Sdrh return 1; 130819a775c2Sdrh } 1309cce7d176Sdrh } 1310cce7d176Sdrh return 0; 1311cce7d176Sdrh } 1312*51522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1313cce7d176Sdrh 1314cce7d176Sdrh /* 131573b211abSdrh ** Generate code to evaluate subqueries and IN operators contained 131673b211abSdrh ** in expression pExpr. 13174b59ab5eSdrh */ 13181398ad36Sdrh static int sqlite3ExprCodeSubquery( 13191398ad36Sdrh Parse *pParse, /* Parser */ 13201398ad36Sdrh NameContext *pNC, /* First enclosing namespace. Often NULL */ 13211398ad36Sdrh Expr *pExpr /* Subquery to be coded */ 13221398ad36Sdrh ){ 1323*51522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 13241398ad36Sdrh QueryCoder sCoder; 13251398ad36Sdrh sCoder.pParse = pParse; 13261398ad36Sdrh sCoder.pNC = pNC; 13271398ad36Sdrh walkExprTree(pExpr, codeSubqueryStep, &sCoder); 1328*51522cd3Sdrh #endif 1329626a879aSdrh return 0; 1330290c1948Sdrh } 1331290c1948Sdrh 1332290c1948Sdrh /* 1333fec19aadSdrh ** Generate an instruction that will put the integer describe by 1334fec19aadSdrh ** text z[0..n-1] on the stack. 1335fec19aadSdrh */ 1336fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1337fec19aadSdrh int i; 13386fec0762Sdrh if( sqlite3GetInt32(z, &i) ){ 13396fec0762Sdrh sqlite3VdbeAddOp(v, OP_Integer, i, 0); 13406fec0762Sdrh }else if( sqlite3FitsIn64Bits(z) ){ 13416fec0762Sdrh sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n); 1342fec19aadSdrh }else{ 1343fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1344fec19aadSdrh } 1345fec19aadSdrh } 1346fec19aadSdrh 1347fec19aadSdrh /* 1348cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 13491ccde15dSdrh ** expression and leave the result on the top of stack. 1350f2bc013cSdrh ** 1351f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1352f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1353f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1354f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1355f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1356cce7d176Sdrh */ 13574adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1358cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1359cce7d176Sdrh int op; 13607977a17fSdanielk1977 if( v==0 ) return; 13617977a17fSdanielk1977 if( pExpr==0 ){ 13627977a17fSdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */ 13637977a17fSdanielk1977 return; 13647977a17fSdanielk1977 } 1365f2bc013cSdrh op = pExpr->op; 1366f2bc013cSdrh switch( op ){ 1367967e8b73Sdrh case TK_COLUMN: { 13682282792aSdrh if( pParse->useAgg ){ 13694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1370c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 13714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1372145716b3Sdrh #ifndef NDEBUG 1373145716b3Sdrh if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){ 1374ad6d9460Sdrh VdbeComment((v, "# %T", &pExpr->span)); 1375145716b3Sdrh } 1376145716b3Sdrh #endif 1377c4a3c779Sdrh }else{ 13784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 13792282792aSdrh } 1380cce7d176Sdrh break; 1381cce7d176Sdrh } 1382cce7d176Sdrh case TK_INTEGER: { 1383fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1384fec19aadSdrh break; 138551e9a445Sdrh } 1386fec19aadSdrh case TK_FLOAT: 1387fec19aadSdrh case TK_STRING: { 1388f2bc013cSdrh assert( TK_FLOAT==OP_Real ); 1389f2bc013cSdrh assert( TK_STRING==OP_String8 ); 1390fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 13914adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1392cce7d176Sdrh break; 1393cce7d176Sdrh } 13945338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 1395c572ef7fSdanielk1977 case TK_BLOB: { 1396f2bc013cSdrh assert( TK_BLOB==OP_HexBlob ); 1397c572ef7fSdanielk1977 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1); 1398c572ef7fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1399c572ef7fSdanielk1977 break; 1400c572ef7fSdanielk1977 } 14015338a5f7Sdanielk1977 #endif 1402cce7d176Sdrh case TK_NULL: { 14030f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1404cce7d176Sdrh break; 1405cce7d176Sdrh } 140650457896Sdrh case TK_VARIABLE: { 14074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 1408895d7472Sdrh if( pExpr->token.n>1 ){ 1409895d7472Sdrh sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 1410895d7472Sdrh } 141150457896Sdrh break; 141250457896Sdrh } 14134e0cff60Sdrh case TK_REGISTER: { 14144e0cff60Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); 14154e0cff60Sdrh break; 14164e0cff60Sdrh } 1417c9b84a1fSdrh case TK_LT: 1418c9b84a1fSdrh case TK_LE: 1419c9b84a1fSdrh case TK_GT: 1420c9b84a1fSdrh case TK_GE: 1421c9b84a1fSdrh case TK_NE: 1422c9b84a1fSdrh case TK_EQ: { 1423f2bc013cSdrh assert( TK_LT==OP_Lt ); 1424f2bc013cSdrh assert( TK_LE==OP_Le ); 1425f2bc013cSdrh assert( TK_GT==OP_Gt ); 1426f2bc013cSdrh assert( TK_GE==OP_Ge ); 1427f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1428f2bc013cSdrh assert( TK_NE==OP_Ne ); 1429a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1430a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1431be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); 1432a37cdde0Sdanielk1977 break; 1433c9b84a1fSdrh } 1434cce7d176Sdrh case TK_AND: 1435cce7d176Sdrh case TK_OR: 1436cce7d176Sdrh case TK_PLUS: 1437cce7d176Sdrh case TK_STAR: 1438cce7d176Sdrh case TK_MINUS: 1439bf4133cbSdrh case TK_REM: 1440bf4133cbSdrh case TK_BITAND: 1441bf4133cbSdrh case TK_BITOR: 144217c40294Sdrh case TK_SLASH: 1443bf4133cbSdrh case TK_LSHIFT: 1444855eb1cfSdrh case TK_RSHIFT: 14450040077dSdrh case TK_CONCAT: { 1446f2bc013cSdrh assert( TK_AND==OP_And ); 1447f2bc013cSdrh assert( TK_OR==OP_Or ); 1448f2bc013cSdrh assert( TK_PLUS==OP_Add ); 1449f2bc013cSdrh assert( TK_MINUS==OP_Subtract ); 1450f2bc013cSdrh assert( TK_REM==OP_Remainder ); 1451f2bc013cSdrh assert( TK_BITAND==OP_BitAnd ); 1452f2bc013cSdrh assert( TK_BITOR==OP_BitOr ); 1453f2bc013cSdrh assert( TK_SLASH==OP_Divide ); 1454f2bc013cSdrh assert( TK_LSHIFT==OP_ShiftLeft ); 1455f2bc013cSdrh assert( TK_RSHIFT==OP_ShiftRight ); 1456f2bc013cSdrh assert( TK_CONCAT==OP_Concat ); 14574adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14584adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1459855eb1cfSdrh sqlite3VdbeAddOp(v, op, 0, 0); 14600040077dSdrh break; 14610040077dSdrh } 1462cce7d176Sdrh case TK_UMINUS: { 1463fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1464fec19aadSdrh assert( pLeft ); 1465fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1466fec19aadSdrh Token *p = &pLeft->token; 14676e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 14686e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1469fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1470fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1471e6840900Sdrh }else{ 1472fec19aadSdrh codeInteger(v, z, p->n+1); 1473e6840900Sdrh } 14746e142f54Sdrh sqliteFree(z); 14756e142f54Sdrh break; 14766e142f54Sdrh } 14771ccde15dSdrh /* Fall through into TK_NOT */ 14786e142f54Sdrh } 1479bf4133cbSdrh case TK_BITNOT: 14806e142f54Sdrh case TK_NOT: { 1481f2bc013cSdrh assert( TK_BITNOT==OP_BitNot ); 1482f2bc013cSdrh assert( TK_NOT==OP_Not ); 14834adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14844adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1485cce7d176Sdrh break; 1486cce7d176Sdrh } 1487cce7d176Sdrh case TK_ISNULL: 1488cce7d176Sdrh case TK_NOTNULL: { 1489cce7d176Sdrh int dest; 1490f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1491f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 14924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 14934adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14944adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 14954adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 14964adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1497a37cdde0Sdanielk1977 break; 1498f2bc013cSdrh } 14992282792aSdrh case TK_AGG_FUNCTION: { 15004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 15012282792aSdrh break; 15022282792aSdrh } 15037977a17fSdanielk1977 case TK_CDATE: 15047977a17fSdanielk1977 case TK_CTIME: 15057977a17fSdanielk1977 case TK_CTIMESTAMP: 15064b59ab5eSdrh case TK_GLOB: 15074b59ab5eSdrh case TK_LIKE: 1508cce7d176Sdrh case TK_FUNCTION: { 1509cce7d176Sdrh ExprList *pList = pExpr->pList; 151089425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 15110bce8354Sdrh FuncDef *pDef; 15124b59ab5eSdrh int nId; 15134b59ab5eSdrh const char *zId; 1514682f68b0Sdanielk1977 int p2 = 0; 1515682f68b0Sdanielk1977 int i; 1516d8123366Sdanielk1977 u8 enc = pParse->db->enc; 1517dc1bdc4fSdanielk1977 CollSeq *pColl = 0; 15184b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 1519d8123366Sdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); 15200bce8354Sdrh assert( pDef!=0 ); 1521f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1522682f68b0Sdanielk1977 for(i=0; i<nExpr && i<32; i++){ 1523d02eb1fdSdanielk1977 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ 1524d02eb1fdSdanielk1977 p2 |= (1<<i); 1525d02eb1fdSdanielk1977 } 1526dc1bdc4fSdanielk1977 if( pDef->needCollSeq && !pColl ){ 1527dc1bdc4fSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); 1528dc1bdc4fSdanielk1977 } 1529dc1bdc4fSdanielk1977 } 1530dc1bdc4fSdanielk1977 if( pDef->needCollSeq ){ 1531dc1bdc4fSdanielk1977 if( !pColl ) pColl = pParse->db->pDfltColl; 1532d8123366Sdanielk1977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 1533682f68b0Sdanielk1977 } 1534682f68b0Sdanielk1977 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF); 15356ec2733bSdrh break; 15366ec2733bSdrh } 153719a775c2Sdrh case TK_SELECT: { 15381398ad36Sdrh if( pExpr->iTable>=0 ){ 15391398ad36Sdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, pExpr->iTable); 15401398ad36Sdrh VdbeComment((v, "# run subquery")); 15411398ad36Sdrh } 15424adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 1543ad6d9460Sdrh VdbeComment((v, "# load subquery result")); 154419a775c2Sdrh break; 154519a775c2Sdrh } 1546fef5208cSdrh case TK_IN: { 1547fef5208cSdrh int addr; 154894a11211Sdrh char affinity; 1549e014a838Sdanielk1977 1550e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1551e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1552ededfd5eSdanielk1977 ** P3 of OP_MakeRecord. 1553e014a838Sdanielk1977 */ 155494a11211Sdrh affinity = comparisonAffinity(pExpr); 1555e014a838Sdanielk1977 15564adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1557e014a838Sdanielk1977 1558e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1559e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1560e014a838Sdanielk1977 */ 15614adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15624adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1563e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 15644adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 15650f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 1566e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 156794a11211Sdrh sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ 1568e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1569e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1570e014a838Sdanielk1977 1571fef5208cSdrh break; 1572fef5208cSdrh } 1573fef5208cSdrh case TK_BETWEEN: { 1574be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1575be5c89acSdrh struct ExprList_item *pLItem = pExpr->pList->a; 1576be5c89acSdrh Expr *pRight = pLItem->pExpr; 1577be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 15784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1579be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1580be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); 15814adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 1582be5c89acSdrh pLItem++; 1583be5c89acSdrh pRight = pLItem->pExpr; 1584be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1585be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); 15864adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1587fef5208cSdrh break; 1588fef5208cSdrh } 158951e9a445Sdrh case TK_UPLUS: 1590a2e00042Sdrh case TK_AS: { 15914adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1592a2e00042Sdrh break; 1593a2e00042Sdrh } 159417a7f8ddSdrh case TK_CASE: { 159517a7f8ddSdrh int expr_end_label; 1596f5905aa7Sdrh int jumpInst; 1597f5905aa7Sdrh int addr; 1598f5905aa7Sdrh int nExpr; 159917a7f8ddSdrh int i; 1600be5c89acSdrh ExprList *pEList; 1601be5c89acSdrh struct ExprList_item *aListelem; 160217a7f8ddSdrh 160317a7f8ddSdrh assert(pExpr->pList); 160417a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 160517a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1606be5c89acSdrh pEList = pExpr->pList; 1607be5c89acSdrh aListelem = pEList->a; 1608be5c89acSdrh nExpr = pEList->nExpr; 16094adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 161017a7f8ddSdrh if( pExpr->pLeft ){ 16114adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1612cce7d176Sdrh } 1613f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 1614be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i].pExpr); 161517a7f8ddSdrh if( pExpr->pLeft ){ 16164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 1617be5c89acSdrh jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, 1618be5c89acSdrh OP_Ne, 0, 1); 16194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1620f5905aa7Sdrh }else{ 16214adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 162217a7f8ddSdrh } 1623be5c89acSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr); 16244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 16254adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 16264adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 162717a7f8ddSdrh } 1628f570f011Sdrh if( pExpr->pLeft ){ 16294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1630f570f011Sdrh } 163117a7f8ddSdrh if( pExpr->pRight ){ 16324adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 163317a7f8ddSdrh }else{ 16340f69c1e3Sdanielk1977 sqlite3VdbeAddOp(v, OP_String8, 0, 0); 163517a7f8ddSdrh } 16364adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 16376f34903eSdanielk1977 break; 16386f34903eSdanielk1977 } 16395338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 16406f34903eSdanielk1977 case TK_RAISE: { 16416f34903eSdanielk1977 if( !pParse->trigStack ){ 16424adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1643da93d238Sdrh "RAISE() may only be used within a trigger-program"); 16446f34903eSdanielk1977 return; 16456f34903eSdanielk1977 } 1646ad6d9460Sdrh if( pExpr->iColumn!=OE_Ignore ){ 1647ad6d9460Sdrh assert( pExpr->iColumn==OE_Rollback || 16486f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 1649ad6d9460Sdrh pExpr->iColumn == OE_Fail ); 16504adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1651701a0aebSdrh pExpr->token.z, pExpr->token.n); 16524adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 16536f34903eSdanielk1977 } else { 16546f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 1655344737f6Sdrh sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); 1656ad6d9460Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); 1657ad6d9460Sdrh VdbeComment((v, "# raise(IGNORE)")); 16586f34903eSdanielk1977 } 165917a7f8ddSdrh } 16605338a5f7Sdanielk1977 #endif 166117a7f8ddSdrh break; 166217a7f8ddSdrh } 1663cce7d176Sdrh } 1664cce7d176Sdrh 1665cce7d176Sdrh /* 166625303780Sdrh ** Generate code that evalutes the given expression and leaves the result 166725303780Sdrh ** on the stack. See also sqlite3ExprCode(). 166825303780Sdrh ** 166925303780Sdrh ** This routine might also cache the result and modify the pExpr tree 167025303780Sdrh ** so that it will make use of the cached result on subsequent evaluations 167125303780Sdrh ** rather than evaluate the whole expression again. Trivial expressions are 167225303780Sdrh ** not cached. If the expression is cached, its result is stored in a 167325303780Sdrh ** memory location. 167425303780Sdrh */ 167525303780Sdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ 167625303780Sdrh Vdbe *v = pParse->pVdbe; 167725303780Sdrh int iMem; 167825303780Sdrh int addr1, addr2; 167925303780Sdrh if( v==0 ) return; 168025303780Sdrh addr1 = sqlite3VdbeCurrentAddr(v); 168125303780Sdrh sqlite3ExprCode(pParse, pExpr); 168225303780Sdrh addr2 = sqlite3VdbeCurrentAddr(v); 168325303780Sdrh if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){ 168425303780Sdrh iMem = pExpr->iTable = pParse->nMem++; 168525303780Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); 168625303780Sdrh pExpr->op = TK_REGISTER; 168725303780Sdrh } 168825303780Sdrh } 168925303780Sdrh 169025303780Sdrh /* 1691268380caSdrh ** Generate code that pushes the value of every element of the given 1692f9b596ebSdrh ** expression list onto the stack. 1693268380caSdrh ** 1694268380caSdrh ** Return the number of elements pushed onto the stack. 1695268380caSdrh */ 16964adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1697268380caSdrh Parse *pParse, /* Parsing context */ 1698f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1699268380caSdrh ){ 1700268380caSdrh struct ExprList_item *pItem; 1701268380caSdrh int i, n; 1702268380caSdrh Vdbe *v; 1703268380caSdrh if( pList==0 ) return 0; 17044adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1705268380caSdrh n = pList->nExpr; 1706268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 17074adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1708268380caSdrh } 1709f9b596ebSdrh return n; 1710268380caSdrh } 1711268380caSdrh 1712268380caSdrh /* 1713cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1714cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1715cce7d176Sdrh ** continues straight thru if the expression is false. 1716f5905aa7Sdrh ** 1717f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1718f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1719f2bc013cSdrh ** 1720f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 1721f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 1722f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 1723f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 1724f2bc013cSdrh ** below verify that the numbers are aligned correctly. 1725cce7d176Sdrh */ 17264adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1727cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1728cce7d176Sdrh int op = 0; 1729daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1730f2bc013cSdrh op = pExpr->op; 1731f2bc013cSdrh switch( op ){ 1732cce7d176Sdrh case TK_AND: { 17334adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 17344adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 17354adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 17364adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1737cce7d176Sdrh break; 1738cce7d176Sdrh } 1739cce7d176Sdrh case TK_OR: { 17404adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 17414adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1742cce7d176Sdrh break; 1743cce7d176Sdrh } 1744cce7d176Sdrh case TK_NOT: { 17454adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1746cce7d176Sdrh break; 1747cce7d176Sdrh } 1748cce7d176Sdrh case TK_LT: 1749cce7d176Sdrh case TK_LE: 1750cce7d176Sdrh case TK_GT: 1751cce7d176Sdrh case TK_GE: 1752cce7d176Sdrh case TK_NE: 17530ac65892Sdrh case TK_EQ: { 1754f2bc013cSdrh assert( TK_LT==OP_Lt ); 1755f2bc013cSdrh assert( TK_LE==OP_Le ); 1756f2bc013cSdrh assert( TK_GT==OP_Gt ); 1757f2bc013cSdrh assert( TK_GE==OP_Ge ); 1758f2bc013cSdrh assert( TK_EQ==OP_Eq ); 1759f2bc013cSdrh assert( TK_NE==OP_Ne ); 17604adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 17614adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1762be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1763cce7d176Sdrh break; 1764cce7d176Sdrh } 1765cce7d176Sdrh case TK_ISNULL: 1766cce7d176Sdrh case TK_NOTNULL: { 1767f2bc013cSdrh assert( TK_ISNULL==OP_IsNull ); 1768f2bc013cSdrh assert( TK_NOTNULL==OP_NotNull ); 17694adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 17704adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1771cce7d176Sdrh break; 1772cce7d176Sdrh } 1773fef5208cSdrh case TK_BETWEEN: { 17740202b29eSdanielk1977 /* The expression "x BETWEEN y AND z" is implemented as: 17750202b29eSdanielk1977 ** 17760202b29eSdanielk1977 ** 1 IF (x < y) GOTO 3 17770202b29eSdanielk1977 ** 2 IF (x <= z) GOTO <dest> 17780202b29eSdanielk1977 ** 3 ... 17790202b29eSdanielk1977 */ 1780f5905aa7Sdrh int addr; 1781be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1782be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1783be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 17844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1785be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1786be5c89acSdrh addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); 17870202b29eSdanielk1977 1788be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1789be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1790be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); 17910202b29eSdanielk1977 17924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 17934adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 17944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1795fef5208cSdrh break; 1796fef5208cSdrh } 1797cce7d176Sdrh default: { 17984adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 17994adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1800cce7d176Sdrh break; 1801cce7d176Sdrh } 1802cce7d176Sdrh } 1803cce7d176Sdrh } 1804cce7d176Sdrh 1805cce7d176Sdrh /* 180666b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1807cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1808cce7d176Sdrh ** continues straight thru if the expression is true. 1809f5905aa7Sdrh ** 1810f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1811f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1812cce7d176Sdrh */ 18134adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1814cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1815cce7d176Sdrh int op = 0; 1816daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1817f2bc013cSdrh 1818f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 1819f2bc013cSdrh ** 1820f2bc013cSdrh ** pExpr->op op 1821f2bc013cSdrh ** --------- ---------- 1822f2bc013cSdrh ** TK_ISNULL OP_NotNull 1823f2bc013cSdrh ** TK_NOTNULL OP_IsNull 1824f2bc013cSdrh ** TK_NE OP_Eq 1825f2bc013cSdrh ** TK_EQ OP_Ne 1826f2bc013cSdrh ** TK_GT OP_Le 1827f2bc013cSdrh ** TK_LE OP_Gt 1828f2bc013cSdrh ** TK_GE OP_Lt 1829f2bc013cSdrh ** TK_LT OP_Ge 1830f2bc013cSdrh ** 1831f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 1832f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 1833f2bc013cSdrh ** can compute the mapping above using the following expression. 1834f2bc013cSdrh ** Assert()s verify that the computation is correct. 1835f2bc013cSdrh */ 1836f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 1837f2bc013cSdrh 1838f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 1839f2bc013cSdrh */ 1840f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 1841f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 1842f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 1843f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 1844f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 1845f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 1846f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 1847f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 1848f2bc013cSdrh 1849cce7d176Sdrh switch( pExpr->op ){ 1850cce7d176Sdrh case TK_AND: { 18514adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 18524adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1853cce7d176Sdrh break; 1854cce7d176Sdrh } 1855cce7d176Sdrh case TK_OR: { 18564adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 18574adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 18584adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 18594adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1860cce7d176Sdrh break; 1861cce7d176Sdrh } 1862cce7d176Sdrh case TK_NOT: { 18634adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1864cce7d176Sdrh break; 1865cce7d176Sdrh } 1866cce7d176Sdrh case TK_LT: 1867cce7d176Sdrh case TK_LE: 1868cce7d176Sdrh case TK_GT: 1869cce7d176Sdrh case TK_GE: 1870cce7d176Sdrh case TK_NE: 1871cce7d176Sdrh case TK_EQ: { 18724adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18734adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1874be5c89acSdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); 1875cce7d176Sdrh break; 1876cce7d176Sdrh } 1877cce7d176Sdrh case TK_ISNULL: 1878cce7d176Sdrh case TK_NOTNULL: { 18794adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 18804adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1881cce7d176Sdrh break; 1882cce7d176Sdrh } 1883fef5208cSdrh case TK_BETWEEN: { 18840202b29eSdanielk1977 /* The expression is "x BETWEEN y AND z". It is implemented as: 18850202b29eSdanielk1977 ** 18860202b29eSdanielk1977 ** 1 IF (x >= y) GOTO 3 18870202b29eSdanielk1977 ** 2 GOTO <dest> 18880202b29eSdanielk1977 ** 3 IF (x > z) GOTO <dest> 18890202b29eSdanielk1977 */ 1890fef5208cSdrh int addr; 1891be5c89acSdrh Expr *pLeft = pExpr->pLeft; 1892be5c89acSdrh Expr *pRight = pExpr->pList->a[0].pExpr; 1893be5c89acSdrh sqlite3ExprCode(pParse, pLeft); 18944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 1895be5c89acSdrh sqlite3ExprCode(pParse, pRight); 18964adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1897be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); 1898be5c89acSdrh 18994adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 19004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 1901be5c89acSdrh pRight = pExpr->pList->a[1].pExpr; 1902be5c89acSdrh sqlite3ExprCode(pParse, pRight); 1903be5c89acSdrh codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); 1904fef5208cSdrh break; 1905fef5208cSdrh } 1906cce7d176Sdrh default: { 19074adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 19084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1909cce7d176Sdrh break; 1910cce7d176Sdrh } 1911cce7d176Sdrh } 1912cce7d176Sdrh } 19132282792aSdrh 19142282792aSdrh /* 19152282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 19162282792aSdrh ** if they are identical and return FALSE if they differ in any way. 19172282792aSdrh */ 19184adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 19192282792aSdrh int i; 19202282792aSdrh if( pA==0 ){ 19212282792aSdrh return pB==0; 19222282792aSdrh }else if( pB==0 ){ 19232282792aSdrh return 0; 19242282792aSdrh } 19252282792aSdrh if( pA->op!=pB->op ) return 0; 19264adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 19274adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 19282282792aSdrh if( pA->pList ){ 19292282792aSdrh if( pB->pList==0 ) return 0; 19302282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 19312282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 19324adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 19332282792aSdrh return 0; 19342282792aSdrh } 19352282792aSdrh } 19362282792aSdrh }else if( pB->pList ){ 19372282792aSdrh return 0; 19382282792aSdrh } 19392282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 19402f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 19412282792aSdrh if( pA->token.z ){ 19422282792aSdrh if( pB->token.z==0 ) return 0; 19436977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 19444adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 19452282792aSdrh } 19462282792aSdrh return 1; 19472282792aSdrh } 19482282792aSdrh 19492282792aSdrh /* 19502282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 195173b211abSdrh ** The new element is initialized to zero. The calling function is 195273b211abSdrh ** expected to fill it in. 19532282792aSdrh */ 19542282792aSdrh static int appendAggInfo(Parse *pParse){ 19552282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 19562282792aSdrh int amt = pParse->nAgg + 8; 19576d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 19586d4abfbeSdrh if( aAgg==0 ){ 19592282792aSdrh return -1; 19602282792aSdrh } 19616d4abfbeSdrh pParse->aAgg = aAgg; 19622282792aSdrh } 19632282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 19642282792aSdrh return pParse->nAgg++; 19652282792aSdrh } 19662282792aSdrh 19672282792aSdrh /* 1968626a879aSdrh ** This is an xFunc for walkExprTree() used to implement 1969626a879aSdrh ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 1970626a879aSdrh ** for additional information. 19712282792aSdrh ** 1972626a879aSdrh ** This routine analyzes the aggregate function at pExpr. 19732282792aSdrh */ 1974626a879aSdrh static int analyzeAggregate(void *pArg, Expr *pExpr){ 19752282792aSdrh int i; 19762282792aSdrh AggExpr *aAgg; 1977626a879aSdrh Parse *pParse = (Parse*)pArg; 19782282792aSdrh 19792282792aSdrh switch( pExpr->op ){ 1980967e8b73Sdrh case TK_COLUMN: { 19812282792aSdrh aAgg = pParse->aAgg; 19822282792aSdrh for(i=0; i<pParse->nAgg; i++){ 19832282792aSdrh if( aAgg[i].isAgg ) continue; 19842282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 1985967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 19862282792aSdrh break; 19872282792aSdrh } 19882282792aSdrh } 19892282792aSdrh if( i>=pParse->nAgg ){ 19902282792aSdrh i = appendAggInfo(pParse); 19912282792aSdrh if( i<0 ) return 1; 19922282792aSdrh pParse->aAgg[i].isAgg = 0; 19932282792aSdrh pParse->aAgg[i].pExpr = pExpr; 19942282792aSdrh } 1995aaf88729Sdrh pExpr->iAgg = i; 1996626a879aSdrh return 1; 19972282792aSdrh } 19982282792aSdrh case TK_AGG_FUNCTION: { 19992282792aSdrh aAgg = pParse->aAgg; 20002282792aSdrh for(i=0; i<pParse->nAgg; i++){ 20012282792aSdrh if( !aAgg[i].isAgg ) continue; 20024adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 20032282792aSdrh break; 20042282792aSdrh } 20052282792aSdrh } 20062282792aSdrh if( i>=pParse->nAgg ){ 2007d8123366Sdanielk1977 u8 enc = pParse->db->enc; 20082282792aSdrh i = appendAggInfo(pParse); 20092282792aSdrh if( i<0 ) return 1; 20102282792aSdrh pParse->aAgg[i].isAgg = 1; 20112282792aSdrh pParse->aAgg[i].pExpr = pExpr; 20124adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 20136977fea8Sdrh pExpr->token.z, pExpr->token.n, 2014d8123366Sdanielk1977 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); 20152282792aSdrh } 20162282792aSdrh pExpr->iAgg = i; 2017626a879aSdrh return 1; 20182282792aSdrh } 20192282792aSdrh } 2020626a879aSdrh return 0; 20212282792aSdrh } 2022626a879aSdrh 2023626a879aSdrh /* 2024626a879aSdrh ** Analyze the given expression looking for aggregate functions and 2025626a879aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 2026626a879aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 2027626a879aSdrh ** 2028626a879aSdrh ** This routine should only be called after the expression has been 2029626a879aSdrh ** analyzed by sqlite3ExprResolveNames(). 2030626a879aSdrh ** 2031626a879aSdrh ** If errors are seen, leave an error message in zErrMsg and return 2032626a879aSdrh ** the number of errors. 2033626a879aSdrh */ 2034626a879aSdrh int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 2035626a879aSdrh int nErr = pParse->nErr; 2036626a879aSdrh walkExprTree(pExpr, analyzeAggregate, pParse); 2037626a879aSdrh return pParse->nErr - nErr; 20382282792aSdrh } 20398e0a2f90Sdrh 20408e0a2f90Sdrh /* 2041d02eb1fdSdanielk1977 ** Locate a user function given a name, a number of arguments and a flag 2042d02eb1fdSdanielk1977 ** indicating whether the function prefers UTF-16 over UTF-8. Return a 2043d02eb1fdSdanielk1977 ** pointer to the FuncDef structure that defines that function, or return 2044d02eb1fdSdanielk1977 ** NULL if the function does not exist. 20458e0a2f90Sdrh ** 20460bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef 20478e0a2f90Sdrh ** structure is created and liked into the "db" structure if a 20488e0a2f90Sdrh ** no matching function previously existed. When createFlag is true 20498e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts 20508e0a2f90Sdrh ** any number of arguments will be returned. 20518e0a2f90Sdrh ** 20528e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid 20538e0a2f90Sdrh ** function found is returned. A function is valid if either xFunc 20548e0a2f90Sdrh ** or xStep is non-zero. 2055d02eb1fdSdanielk1977 ** 2056d02eb1fdSdanielk1977 ** If createFlag is false, then a function with the required name and 2057d02eb1fdSdanielk1977 ** number of arguments may be returned even if the eTextRep flag does not 2058d02eb1fdSdanielk1977 ** match that requested. 20598e0a2f90Sdrh */ 20604adee20fSdanielk1977 FuncDef *sqlite3FindFunction( 20619bb575fdSdrh sqlite3 *db, /* An open database */ 20628e0a2f90Sdrh const char *zName, /* Name of the function. Not null-terminated */ 20638e0a2f90Sdrh int nName, /* Number of characters in the name */ 20648e0a2f90Sdrh int nArg, /* Number of arguments. -1 means any number */ 2065d8123366Sdanielk1977 u8 enc, /* Preferred text encoding */ 20668e0a2f90Sdrh int createFlag /* Create new entry if true and does not otherwise exist */ 20678e0a2f90Sdrh ){ 2068d02eb1fdSdanielk1977 FuncDef *p; /* Iterator variable */ 2069d02eb1fdSdanielk1977 FuncDef *pFirst; /* First function with this name */ 2070d02eb1fdSdanielk1977 FuncDef *pBest = 0; /* Best match found so far */ 2071d8123366Sdanielk1977 int bestmatch = 0; 2072d02eb1fdSdanielk1977 2073d8123366Sdanielk1977 2074d8123366Sdanielk1977 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); 2075d02eb1fdSdanielk1977 if( nArg<-1 ) nArg = -1; 2076d02eb1fdSdanielk1977 2077d02eb1fdSdanielk1977 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); 2078d02eb1fdSdanielk1977 for(p=pFirst; p; p=p->pNext){ 2079d8123366Sdanielk1977 /* During the search for the best function definition, bestmatch is set 2080d8123366Sdanielk1977 ** as follows to indicate the quality of the match with the definition 2081d8123366Sdanielk1977 ** pointed to by pBest: 2082d8123366Sdanielk1977 ** 2083d8123366Sdanielk1977 ** 0: pBest is NULL. No match has been found. 2084d8123366Sdanielk1977 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16 2085d8123366Sdanielk1977 ** encoding is requested, or vice versa. 2086d8123366Sdanielk1977 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is 2087d8123366Sdanielk1977 ** requested, or vice versa. 2088d8123366Sdanielk1977 ** 3: A variable arguments function using the same text encoding. 2089d8123366Sdanielk1977 ** 4: A function with the exact number of arguments requested that 2090d8123366Sdanielk1977 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa. 2091d8123366Sdanielk1977 ** 5: A function with the exact number of arguments requested that 2092d8123366Sdanielk1977 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa. 2093d8123366Sdanielk1977 ** 6: An exact match. 2094d8123366Sdanielk1977 ** 2095d8123366Sdanielk1977 ** A larger value of 'matchqual' indicates a more desirable match. 2096d8123366Sdanielk1977 */ 2097e12c17baSdanielk1977 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){ 2098d8123366Sdanielk1977 int match = 1; /* Quality of this match */ 2099d8123366Sdanielk1977 if( p->nArg==nArg || nArg==-1 ){ 2100d8123366Sdanielk1977 match = 4; 21018e0a2f90Sdrh } 2102d8123366Sdanielk1977 if( enc==p->iPrefEnc ){ 2103d8123366Sdanielk1977 match += 2; 21048e0a2f90Sdrh } 2105d8123366Sdanielk1977 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) || 2106d8123366Sdanielk1977 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){ 2107d8123366Sdanielk1977 match += 1; 2108d02eb1fdSdanielk1977 } 2109d8123366Sdanielk1977 2110d8123366Sdanielk1977 if( match>bestmatch ){ 2111d02eb1fdSdanielk1977 pBest = p; 2112d8123366Sdanielk1977 bestmatch = match; 2113d02eb1fdSdanielk1977 } 2114d02eb1fdSdanielk1977 } 2115d02eb1fdSdanielk1977 } 2116d02eb1fdSdanielk1977 2117d8123366Sdanielk1977 /* If the createFlag parameter is true, and the seach did not reveal an 2118d8123366Sdanielk1977 ** exact match for the name, number of arguments and encoding, then add a 2119d8123366Sdanielk1977 ** new entry to the hash table and return it. 2120d8123366Sdanielk1977 */ 2121d8123366Sdanielk1977 if( createFlag && bestmatch<6 && 2122d02eb1fdSdanielk1977 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){ 2123d02eb1fdSdanielk1977 pBest->nArg = nArg; 2124d02eb1fdSdanielk1977 pBest->pNext = pFirst; 2125d02eb1fdSdanielk1977 pBest->zName = (char*)&pBest[1]; 2126d8123366Sdanielk1977 pBest->iPrefEnc = enc; 2127d02eb1fdSdanielk1977 memcpy(pBest->zName, zName, nName); 2128d02eb1fdSdanielk1977 pBest->zName[nName] = 0; 21292c336549Sdanielk1977 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){ 21302c336549Sdanielk1977 sqliteFree(pBest); 21312c336549Sdanielk1977 return 0; 21322c336549Sdanielk1977 } 2133d02eb1fdSdanielk1977 } 2134d02eb1fdSdanielk1977 2135d02eb1fdSdanielk1977 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ 2136d02eb1fdSdanielk1977 return pBest; 2137d02eb1fdSdanielk1977 } 21388e0a2f90Sdrh return 0; 21398e0a2f90Sdrh } 2140