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*736c22b8Sdrh ** $Id: expr.c,v 1.126 2004/05/21 02:14:25 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20e014a838Sdanielk1977 char const *sqlite3AffinityString(char affinity){ 21e014a838Sdanielk1977 switch( affinity ){ 22e014a838Sdanielk1977 case SQLITE_AFF_INTEGER: return "i"; 23e014a838Sdanielk1977 case SQLITE_AFF_NUMERIC: return "n"; 24e014a838Sdanielk1977 case SQLITE_AFF_TEXT: return "t"; 25e014a838Sdanielk1977 case SQLITE_AFF_NONE: return "o"; 26e014a838Sdanielk1977 default: 27e014a838Sdanielk1977 assert(0); 28e014a838Sdanielk1977 } 29e014a838Sdanielk1977 } 30e014a838Sdanielk1977 31e014a838Sdanielk1977 32e014a838Sdanielk1977 /* 33e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any. 34e014a838Sdanielk1977 ** 35e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias, 36e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the 37e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned, 38e014a838Sdanielk1977 ** indicating no affinity for the expression. 39e014a838Sdanielk1977 ** 40e014a838Sdanielk1977 ** i.e. the WHERE clause expresssions in the following statements all 41e014a838Sdanielk1977 ** have an affinity: 42e014a838Sdanielk1977 ** 43e014a838Sdanielk1977 ** CREATE TABLE t1(a); 44e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a; 45e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b; 46e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1); 47e014a838Sdanielk1977 */ 48bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){ 49a37cdde0Sdanielk1977 if( pExpr->op==TK_AS ){ 50bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pLeft); 51a37cdde0Sdanielk1977 } 52a37cdde0Sdanielk1977 if( pExpr->op==TK_SELECT ){ 53bf3b721fSdanielk1977 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 54a37cdde0Sdanielk1977 } 55a37cdde0Sdanielk1977 return pExpr->affinity; 56a37cdde0Sdanielk1977 } 57a37cdde0Sdanielk1977 5853db1458Sdrh /* 5953db1458Sdrh ** pExpr is the left operand of a comparison operator. aff2 is the 6053db1458Sdrh ** type affinity of the right operand. This routine returns the 6153db1458Sdrh ** type affinity that should be used for the comparison operator. 6253db1458Sdrh */ 63e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 64bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 65e014a838Sdanielk1977 if( aff1 && aff2 ){ 66e014a838Sdanielk1977 /* Both sides of the comparison are columns. If one has numeric or 67e014a838Sdanielk1977 ** integer affinity, use that. Otherwise use no affinity. 68e014a838Sdanielk1977 */ 69e014a838Sdanielk1977 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ 70e014a838Sdanielk1977 return SQLITE_AFF_INTEGER; 71e014a838Sdanielk1977 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 72e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 73e014a838Sdanielk1977 }else{ 74e014a838Sdanielk1977 return SQLITE_AFF_NONE; 75e014a838Sdanielk1977 } 76e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 77e014a838Sdanielk1977 /* Neither side of the comparison is a column. Use numeric affinity 78e014a838Sdanielk1977 ** for the comparison. 79e014a838Sdanielk1977 */ 80e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 81e014a838Sdanielk1977 }else{ 82e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 83e014a838Sdanielk1977 return (aff1 + aff2); 84e014a838Sdanielk1977 } 85e014a838Sdanielk1977 } 86e014a838Sdanielk1977 8753db1458Sdrh /* 8853db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 8953db1458Sdrh ** be applied to both operands prior to doing the comparison. 9053db1458Sdrh */ 91e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 92e014a838Sdanielk1977 char aff; 93e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 94e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 95e014a838Sdanielk1977 pExpr->op==TK_NE ); 96e014a838Sdanielk1977 assert( pExpr->pLeft ); 97bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 98e014a838Sdanielk1977 if( pExpr->pRight ){ 99e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 100e014a838Sdanielk1977 } 101e014a838Sdanielk1977 else if( pExpr->pSelect ){ 102e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); 103e014a838Sdanielk1977 } 104e014a838Sdanielk1977 else if( !aff ){ 105e014a838Sdanielk1977 aff = SQLITE_AFF_NUMERIC; 106e014a838Sdanielk1977 } 107e014a838Sdanielk1977 return aff; 108e014a838Sdanielk1977 } 109e014a838Sdanielk1977 110e014a838Sdanielk1977 /* 111e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 112e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 113e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 114e014a838Sdanielk1977 ** the comparison in pExpr. 115e014a838Sdanielk1977 */ 116e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 117e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 118e014a838Sdanielk1977 return 119e014a838Sdanielk1977 (aff==SQLITE_AFF_NONE) || 120e014a838Sdanielk1977 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) || 121e014a838Sdanielk1977 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) || 122e014a838Sdanielk1977 (aff==idx_affinity); 123e014a838Sdanielk1977 } 124e014a838Sdanielk1977 125a37cdde0Sdanielk1977 /* 126a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 127a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 128a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 129a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 130a37cdde0Sdanielk1977 ** evaluates to NULL. 131a37cdde0Sdanielk1977 */ 132e014a838Sdanielk1977 static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 133bf3b721fSdanielk1977 char aff = sqlite3ExprAffinity(pExpr2); 134e014a838Sdanielk1977 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0); 135a37cdde0Sdanielk1977 } 136a37cdde0Sdanielk1977 137a2e00042Sdrh /* 138a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 139a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 140a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 141a76b5dfcSdrh */ 1424adee20fSdanielk1977 Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ 143a76b5dfcSdrh Expr *pNew; 144a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 145a76b5dfcSdrh if( pNew==0 ){ 1464efc4754Sdrh /* When malloc fails, we leak memory from pLeft and pRight */ 147a76b5dfcSdrh return 0; 148a76b5dfcSdrh } 149a76b5dfcSdrh pNew->op = op; 150a76b5dfcSdrh pNew->pLeft = pLeft; 151a76b5dfcSdrh pNew->pRight = pRight; 152a76b5dfcSdrh if( pToken ){ 1534b59ab5eSdrh assert( pToken->dyn==0 ); 154a76b5dfcSdrh pNew->token = *pToken; 1556977fea8Sdrh pNew->span = *pToken; 156a76b5dfcSdrh }else{ 1574efc4754Sdrh assert( pNew->token.dyn==0 ); 1584efc4754Sdrh assert( pNew->token.z==0 ); 1594efc4754Sdrh assert( pNew->token.n==0 ); 1606977fea8Sdrh if( pLeft && pRight ){ 1614adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 1626977fea8Sdrh }else{ 1636977fea8Sdrh pNew->span = pNew->token; 1646977fea8Sdrh } 165a76b5dfcSdrh } 166a76b5dfcSdrh return pNew; 167a76b5dfcSdrh } 168a76b5dfcSdrh 169a76b5dfcSdrh /* 1706977fea8Sdrh ** Set the Expr.span field of the given expression to span all 171a76b5dfcSdrh ** text between the two given tokens. 172a76b5dfcSdrh */ 1734adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 1744efc4754Sdrh assert( pRight!=0 ); 1754efc4754Sdrh assert( pLeft!=0 ); 1764efc4754Sdrh /* Note: pExpr might be NULL due to a prior malloc failure */ 1774efc4754Sdrh if( pExpr && pRight->z && pLeft->z ){ 1784b59ab5eSdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 1796977fea8Sdrh pExpr->span.z = pLeft->z; 1806977fea8Sdrh pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); 1814b59ab5eSdrh }else{ 1826977fea8Sdrh pExpr->span.z = 0; 1834b59ab5eSdrh } 184a76b5dfcSdrh } 185a76b5dfcSdrh } 186a76b5dfcSdrh 187a76b5dfcSdrh /* 188a76b5dfcSdrh ** Construct a new expression node for a function with multiple 189a76b5dfcSdrh ** arguments. 190a76b5dfcSdrh */ 1914adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 192a76b5dfcSdrh Expr *pNew; 193a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 194a76b5dfcSdrh if( pNew==0 ){ 1954adee20fSdanielk1977 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */ 196a76b5dfcSdrh return 0; 197a76b5dfcSdrh } 198a76b5dfcSdrh pNew->op = TK_FUNCTION; 199a76b5dfcSdrh pNew->pList = pList; 200a76b5dfcSdrh if( pToken ){ 2014b59ab5eSdrh assert( pToken->dyn==0 ); 202a76b5dfcSdrh pNew->token = *pToken; 203a76b5dfcSdrh }else{ 204a76b5dfcSdrh pNew->token.z = 0; 205a76b5dfcSdrh } 2066977fea8Sdrh pNew->span = pNew->token; 207a76b5dfcSdrh return pNew; 208a76b5dfcSdrh } 209a76b5dfcSdrh 210a76b5dfcSdrh /* 211a2e00042Sdrh ** Recursively delete an expression tree. 212a2e00042Sdrh */ 2134adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 214a2e00042Sdrh if( p==0 ) return; 2154efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 2164efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 2174adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 2184adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 2194adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 2204adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 221a2e00042Sdrh sqliteFree(p); 222a2e00042Sdrh } 223a2e00042Sdrh 224a76b5dfcSdrh 225a76b5dfcSdrh /* 226ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 227ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 228ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 229ff78bd2fSdrh ** without effecting the originals. 230ff78bd2fSdrh ** 2314adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 2324adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 233ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 234ff78bd2fSdrh ** 235ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 236ff78bd2fSdrh */ 2374adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 238ff78bd2fSdrh Expr *pNew; 239ff78bd2fSdrh if( p==0 ) return 0; 240fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 241ff78bd2fSdrh if( pNew==0 ) return 0; 2423b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 2436977fea8Sdrh if( p->token.z!=0 ){ 2444b59ab5eSdrh pNew->token.z = sqliteStrDup(p->token.z); 2454b59ab5eSdrh pNew->token.dyn = 1; 2464b59ab5eSdrh }else{ 2474efc4754Sdrh assert( pNew->token.z==0 ); 2484b59ab5eSdrh } 2496977fea8Sdrh pNew->span.z = 0; 2504adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 2514adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 2524adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 2534adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 254ff78bd2fSdrh return pNew; 255ff78bd2fSdrh } 2564adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 2574b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 2584b59ab5eSdrh if( pFrom->z ){ 2594b59ab5eSdrh pTo->n = pFrom->n; 2604b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 2614b59ab5eSdrh pTo->dyn = 1; 2624b59ab5eSdrh }else{ 2634b59ab5eSdrh pTo->z = 0; 2644b59ab5eSdrh } 2654b59ab5eSdrh } 2664adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 267ff78bd2fSdrh ExprList *pNew; 2683e7bc9caSdrh struct ExprList_item *pItem; 269ff78bd2fSdrh int i; 270ff78bd2fSdrh if( p==0 ) return 0; 271ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 272ff78bd2fSdrh if( pNew==0 ) return 0; 2734305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 2743e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 2751bdd9b57Sdrh if( pItem==0 ) return 0; /* Leaks memory after a malloc failure */ 2761bdd9b57Sdrh for(i=0; i<p->nExpr; i++, pItem++){ 2774b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 2784adee20fSdanielk1977 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr); 2796977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 2806977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 2814b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 2824b59ab5eSdrh ** the names of columns in the result set needs this information */ 2834adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 2844b59ab5eSdrh } 2851f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 28624b03fd0Sdanielk1977 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 2873e7bc9caSdrh pItem->zName = sqliteStrDup(p->a[i].zName); 2883e7bc9caSdrh pItem->sortOrder = p->a[i].sortOrder; 2893e7bc9caSdrh pItem->isAgg = p->a[i].isAgg; 2903e7bc9caSdrh pItem->done = 0; 291ff78bd2fSdrh } 292ff78bd2fSdrh return pNew; 293ff78bd2fSdrh } 2944adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 295ad3cab52Sdrh SrcList *pNew; 296ad3cab52Sdrh int i; 297113088ecSdrh int nByte; 298ad3cab52Sdrh if( p==0 ) return 0; 299113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 3004efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 301ad3cab52Sdrh if( pNew==0 ) return 0; 3024305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 303ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 3044efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 3054efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 3064efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 3074efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 3084efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 3094efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 3104efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 3114efc4754Sdrh pNewItem->pTab = 0; 3124adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 3134adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 3144adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 315ad3cab52Sdrh } 316ad3cab52Sdrh return pNew; 317ad3cab52Sdrh } 3184adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 319ff78bd2fSdrh IdList *pNew; 320ff78bd2fSdrh int i; 321ff78bd2fSdrh if( p==0 ) return 0; 3224efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 323ff78bd2fSdrh if( pNew==0 ) return 0; 3244305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 3254efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 326e4697f5eSdrh if( pNew->a==0 ) return 0; 327ff78bd2fSdrh for(i=0; i<p->nId; i++){ 3284efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 3294efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 3304efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 3314efc4754Sdrh pNewItem->idx = pOldItem->idx; 332ff78bd2fSdrh } 333ff78bd2fSdrh return pNew; 334ff78bd2fSdrh } 3354adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 336ff78bd2fSdrh Select *pNew; 337ff78bd2fSdrh if( p==0 ) return 0; 3384efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 339ff78bd2fSdrh if( pNew==0 ) return 0; 340ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 3414adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 3424adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 3434adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 3444adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 3454adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 3464adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 347ff78bd2fSdrh pNew->op = p->op; 3484adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 349ff78bd2fSdrh pNew->nLimit = p->nLimit; 350ff78bd2fSdrh pNew->nOffset = p->nOffset; 351ff78bd2fSdrh pNew->zSelect = 0; 3527b58daeaSdrh pNew->iLimit = -1; 3537b58daeaSdrh pNew->iOffset = -1; 354ff78bd2fSdrh return pNew; 355ff78bd2fSdrh } 356ff78bd2fSdrh 357ff78bd2fSdrh 358ff78bd2fSdrh /* 359a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 360a76b5dfcSdrh ** initially NULL, then create a new expression list. 361a76b5dfcSdrh */ 3624adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 363a76b5dfcSdrh if( pList==0 ){ 364a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 365a76b5dfcSdrh if( pList==0 ){ 3664adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 367a76b5dfcSdrh return 0; 368a76b5dfcSdrh } 3694efc4754Sdrh assert( pList->nAlloc==0 ); 370a76b5dfcSdrh } 3714305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 3724305d103Sdrh pList->nAlloc = pList->nAlloc*2 + 4; 3734efc4754Sdrh pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); 3744efc4754Sdrh if( pList->a==0 ){ 3754adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 3764efc4754Sdrh pList->nExpr = pList->nAlloc = 0; 377a76b5dfcSdrh return pList; 378a76b5dfcSdrh } 379a76b5dfcSdrh } 3804efc4754Sdrh assert( pList->a!=0 ); 3814efc4754Sdrh if( pExpr || pName ){ 3824efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 3834efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 3844efc4754Sdrh pItem->pExpr = pExpr; 385a76b5dfcSdrh if( pName ){ 3864adee20fSdanielk1977 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0); 3874adee20fSdanielk1977 sqlite3Dequote(pItem->zName); 388a76b5dfcSdrh } 389a76b5dfcSdrh } 390a76b5dfcSdrh return pList; 391a76b5dfcSdrh } 392a76b5dfcSdrh 393a76b5dfcSdrh /* 394a76b5dfcSdrh ** Delete an entire expression list. 395a76b5dfcSdrh */ 3964adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 397a76b5dfcSdrh int i; 398a76b5dfcSdrh if( pList==0 ) return; 3991bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 4001bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 401a76b5dfcSdrh for(i=0; i<pList->nExpr; i++){ 4024adee20fSdanielk1977 sqlite3ExprDelete(pList->a[i].pExpr); 403a76b5dfcSdrh sqliteFree(pList->a[i].zName); 404a76b5dfcSdrh } 405a76b5dfcSdrh sqliteFree(pList->a); 406a76b5dfcSdrh sqliteFree(pList); 407a76b5dfcSdrh } 408a76b5dfcSdrh 409a76b5dfcSdrh /* 410fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 411fef5208cSdrh ** and 0 if it involves variables. 4122398937bSdrh ** 4132398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 4142398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 4152398937bSdrh ** a constant. 416fef5208cSdrh */ 4174adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 418fef5208cSdrh switch( p->op ){ 419fef5208cSdrh case TK_ID: 420967e8b73Sdrh case TK_COLUMN: 421fef5208cSdrh case TK_DOT: 4227bdc0c1dSdrh case TK_FUNCTION: 423fef5208cSdrh return 0; 4247bdc0c1dSdrh case TK_NULL: 4252398937bSdrh case TK_STRING: 4269208643dSdrh case TK_INTEGER: 4279208643dSdrh case TK_FLOAT: 42850457896Sdrh case TK_VARIABLE: 4299208643dSdrh return 1; 430fef5208cSdrh default: { 4314adee20fSdanielk1977 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0; 4324adee20fSdanielk1977 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0; 433fef5208cSdrh if( p->pList ){ 434fef5208cSdrh int i; 435fef5208cSdrh for(i=0; i<p->pList->nExpr; i++){ 4364adee20fSdanielk1977 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0; 437fef5208cSdrh } 438fef5208cSdrh } 4399208643dSdrh return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); 440fef5208cSdrh } 441fef5208cSdrh } 4429208643dSdrh return 0; 443fef5208cSdrh } 444fef5208cSdrh 445fef5208cSdrh /* 446202b2df7Sdrh ** If the given expression codes a constant integer that is small enough 447202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 448202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 449202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 450e4de1febSdrh */ 4514adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 452e4de1febSdrh switch( p->op ){ 453e4de1febSdrh case TK_INTEGER: { 454fec19aadSdrh if( sqlite3GetInt32(p->token.z, pValue) ){ 455e4de1febSdrh return 1; 456e4de1febSdrh } 457202b2df7Sdrh break; 458202b2df7Sdrh } 459e4de1febSdrh case TK_STRING: { 460bd790ee3Sdrh const char *z = p->token.z; 461e4de1febSdrh int n = p->token.n; 462bd790ee3Sdrh if( n>0 && z[0]=='-' ){ z++; n--; } 463e4de1febSdrh while( n>0 && *z && isdigit(*z) ){ z++; n--; } 464fec19aadSdrh if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){ 465e4de1febSdrh return 1; 466e4de1febSdrh } 467e4de1febSdrh break; 468e4de1febSdrh } 4694b59ab5eSdrh case TK_UPLUS: { 4704adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 4714b59ab5eSdrh } 472e4de1febSdrh case TK_UMINUS: { 473e4de1febSdrh int v; 4744adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 475e4de1febSdrh *pValue = -v; 476e4de1febSdrh return 1; 477e4de1febSdrh } 478e4de1febSdrh break; 479e4de1febSdrh } 480e4de1febSdrh default: break; 481e4de1febSdrh } 482e4de1febSdrh return 0; 483e4de1febSdrh } 484e4de1febSdrh 485e4de1febSdrh /* 486c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 487c4a3c779Sdrh */ 4884adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 4894adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 4904adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 4914adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 492c4a3c779Sdrh return 0; 493c4a3c779Sdrh } 494c4a3c779Sdrh 495c4a3c779Sdrh /* 4968141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 4978141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 4988141f61eSdrh ** expression node refer back to that source column. The following changes 4998141f61eSdrh ** are made to pExpr: 5008141f61eSdrh ** 5018141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 5028141f61eSdrh ** the table. 5038141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 5048141f61eSdrh ** from pSrcList. 5058141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 5068141f61eSdrh ** pExpr->dataType Set to the appropriate data type for the column. 5078141f61eSdrh ** pExpr->op Set to TK_COLUMN. 5088141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 5098141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 5108141f61eSdrh ** 5118141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 5128141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 5138141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 5148141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 5158141f61eSdrh ** means that the form of the name is Z and that columns from any table 5168141f61eSdrh ** can be used. 5178141f61eSdrh ** 5188141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 5198141f61eSdrh ** in pParse and return non-zero. Return zero on success. 5208141f61eSdrh */ 5218141f61eSdrh static int lookupName( 5228141f61eSdrh Parse *pParse, /* The parsing context */ 5238141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 5248141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 5258141f61eSdrh Token *pColumnToken, /* Name of the column. */ 5268141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 5278141f61eSdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 5288141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 5298141f61eSdrh ){ 5308141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 5318141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 5328141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 5338141f61eSdrh int i, j; /* Loop counters */ 5348141f61eSdrh int cnt = 0; /* Number of matching column names */ 5358141f61eSdrh int cntTab = 0; /* Number of matching table names */ 5367e26d750Sdrh sqlite *db = pParse->db; /* The database */ 5378141f61eSdrh 5388141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 5398141f61eSdrh if( pDbToken && pDbToken->z ){ 5408141f61eSdrh zDb = sqliteStrNDup(pDbToken->z, pDbToken->n); 5414adee20fSdanielk1977 sqlite3Dequote(zDb); 5428141f61eSdrh }else{ 5438141f61eSdrh zDb = 0; 5448141f61eSdrh } 5458141f61eSdrh if( pTableToken && pTableToken->z ){ 5468141f61eSdrh zTab = sqliteStrNDup(pTableToken->z, pTableToken->n); 5474adee20fSdanielk1977 sqlite3Dequote(zTab); 5488141f61eSdrh }else{ 5498141f61eSdrh assert( zDb==0 ); 5508141f61eSdrh zTab = 0; 5518141f61eSdrh } 5528141f61eSdrh zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n); 5534adee20fSdanielk1977 sqlite3Dequote(zCol); 55424b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 5558141f61eSdrh return 1; /* Leak memory (zDb and zTab) if malloc fails */ 5568141f61eSdrh } 5578141f61eSdrh assert( zTab==0 || pEList==0 ); 5588141f61eSdrh 5598141f61eSdrh pExpr->iTable = -1; 5608141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 5618141f61eSdrh struct SrcList_item *pItem = &pSrcList->a[i]; 5628141f61eSdrh Table *pTab = pItem->pTab; 5638141f61eSdrh Column *pCol; 5648141f61eSdrh 5658141f61eSdrh if( pTab==0 ) continue; 5668141f61eSdrh assert( pTab->nCol>0 ); 5678141f61eSdrh if( zTab ){ 5688141f61eSdrh if( pItem->zAlias ){ 5698141f61eSdrh char *zTabName = pItem->zAlias; 5704adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 5718141f61eSdrh }else{ 5728141f61eSdrh char *zTabName = pTab->zName; 5734adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 5744adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 5758141f61eSdrh continue; 5768141f61eSdrh } 5778141f61eSdrh } 5788141f61eSdrh } 5798141f61eSdrh if( 0==(cntTab++) ){ 5808141f61eSdrh pExpr->iTable = pItem->iCursor; 5818141f61eSdrh pExpr->iDb = pTab->iDb; 5828141f61eSdrh } 5838141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 5844adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 5858141f61eSdrh cnt++; 5868141f61eSdrh pExpr->iTable = pItem->iCursor; 5878141f61eSdrh pExpr->iDb = pTab->iDb; 5888141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 5898141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 590a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 5918141f61eSdrh break; 5928141f61eSdrh } 5938141f61eSdrh } 5948141f61eSdrh } 5958141f61eSdrh 5968141f61eSdrh /* If we have not already resolved the name, then maybe 5978141f61eSdrh ** it is a new.* or old.* trigger argument reference 5988141f61eSdrh */ 5998141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 6008141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 6018141f61eSdrh Table *pTab = 0; 6024adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 6038141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 6048141f61eSdrh assert( pTriggerStack->pTab ); 6058141f61eSdrh pTab = pTriggerStack->pTab; 6064adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){ 6078141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 6088141f61eSdrh assert( pTriggerStack->pTab ); 6098141f61eSdrh pTab = pTriggerStack->pTab; 6108141f61eSdrh } 6118141f61eSdrh 6128141f61eSdrh if( pTab ){ 6138141f61eSdrh int j; 6148141f61eSdrh Column *pCol = pTab->aCol; 6158141f61eSdrh 6168141f61eSdrh pExpr->iDb = pTab->iDb; 6178141f61eSdrh cntTab++; 6188141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 6194adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 6208141f61eSdrh cnt++; 6218141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 622a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 6238141f61eSdrh break; 6248141f61eSdrh } 6258141f61eSdrh } 6268141f61eSdrh } 6278141f61eSdrh } 6288141f61eSdrh 6298141f61eSdrh /* 6308141f61eSdrh ** Perhaps the name is a reference to the ROWID 6318141f61eSdrh */ 6324adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 6338141f61eSdrh cnt = 1; 6348141f61eSdrh pExpr->iColumn = -1; 635a37cdde0Sdanielk1977 pExpr->affinity = SQLITE_AFF_INTEGER; 6368141f61eSdrh } 6378141f61eSdrh 6388141f61eSdrh /* 6398141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 6408141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 6418141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 6428141f61eSdrh ** 6438141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 6448141f61eSdrh ** 6458141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 6468141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 6478141f61eSdrh ** Note that the expression in the result set should have already been 6488141f61eSdrh ** resolved by the time the WHERE clause is resolved. 6498141f61eSdrh */ 6508141f61eSdrh if( cnt==0 && pEList!=0 ){ 6518141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 6528141f61eSdrh char *zAs = pEList->a[j].zName; 6534adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 6548141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 6558141f61eSdrh pExpr->op = TK_AS; 6568141f61eSdrh pExpr->iColumn = j; 6574adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 6588141f61eSdrh sqliteFree(zCol); 6598141f61eSdrh assert( zTab==0 && zDb==0 ); 6608141f61eSdrh return 0; 6618141f61eSdrh } 6628141f61eSdrh } 6638141f61eSdrh } 6648141f61eSdrh 6658141f61eSdrh /* 6668141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 6678141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 6688141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 6698141f61eSdrh ** case, we need to return right away and not make any changes to 6708141f61eSdrh ** pExpr. 6718141f61eSdrh */ 6728141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 6738141f61eSdrh sqliteFree(zCol); 6748141f61eSdrh return 0; 6758141f61eSdrh } 6768141f61eSdrh 6778141f61eSdrh /* 6788141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 6798141f61eSdrh ** more matches. Either way, we have an error. 6808141f61eSdrh */ 6818141f61eSdrh if( cnt!=1 ){ 6828141f61eSdrh char *z = 0; 6838141f61eSdrh char *zErr; 6848141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 6858141f61eSdrh if( zDb ){ 6864adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 6878141f61eSdrh }else if( zTab ){ 6884adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 6898141f61eSdrh }else{ 6908141f61eSdrh z = sqliteStrDup(zCol); 6918141f61eSdrh } 6924adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 6938141f61eSdrh sqliteFree(z); 6948141f61eSdrh } 6958141f61eSdrh 6968141f61eSdrh /* Clean up and return 6978141f61eSdrh */ 6988141f61eSdrh sqliteFree(zDb); 6998141f61eSdrh sqliteFree(zTab); 7008141f61eSdrh sqliteFree(zCol); 7014adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 7028141f61eSdrh pExpr->pLeft = 0; 7034adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 7048141f61eSdrh pExpr->pRight = 0; 7058141f61eSdrh pExpr->op = TK_COLUMN; 7064adee20fSdanielk1977 sqlite3AuthRead(pParse, pExpr, pSrcList); 7078141f61eSdrh return cnt!=1; 7088141f61eSdrh } 7098141f61eSdrh 7108141f61eSdrh /* 711cce7d176Sdrh ** This routine walks an expression tree and resolves references to 712967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 713aacc543eSdrh ** index to the table in the table list and a column offset. The 714aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 715aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 716832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 717aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 718aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 719aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 720aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 721aacc543eSdrh ** alias for ROWID. 72219a775c2Sdrh ** 723fef5208cSdrh ** We also check for instances of the IN operator. IN comes in two 724fef5208cSdrh ** forms: 725fef5208cSdrh ** 726fef5208cSdrh ** expr IN (exprlist) 727fef5208cSdrh ** and 728fef5208cSdrh ** expr IN (SELECT ...) 729fef5208cSdrh ** 730fef5208cSdrh ** The first form is handled by creating a set holding the list 731fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 732fef5208cSdrh ** a temporary table. 733fef5208cSdrh ** 734fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression. 73519a775c2Sdrh ** If it finds any, it generates code to write the value of that select 73619a775c2Sdrh ** into a memory cell. 737cce7d176Sdrh ** 738967e8b73Sdrh ** Unknown columns or tables provoke an error. The function returns 739cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg. 740cce7d176Sdrh */ 7414adee20fSdanielk1977 int sqlite3ExprResolveIds( 742a2e00042Sdrh Parse *pParse, /* The parser context */ 7438141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 744a2e00042Sdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 745a2e00042Sdrh Expr *pExpr /* The expression to be analyzed. */ 746a2e00042Sdrh ){ 7476a3ea0e6Sdrh int i; 7486a3ea0e6Sdrh 7498141f61eSdrh if( pExpr==0 || pSrcList==0 ) return 0; 7508141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 7518141f61eSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab ); 7526a3ea0e6Sdrh } 753cce7d176Sdrh switch( pExpr->op ){ 7542398937bSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 7552398937bSdrh ** possible. Otherwise they remain as strings. Single-quoted 7562398937bSdrh ** strings (ex: 'abc') are always string literals. 7572398937bSdrh */ 7582398937bSdrh case TK_STRING: { 7592398937bSdrh if( pExpr->token.z[0]=='\'' ) break; 7602398937bSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 7612398937bSdrh } 7628141f61eSdrh /* A lone identifier is the name of a columnd. 763a2e00042Sdrh */ 764cce7d176Sdrh case TK_ID: { 7658141f61eSdrh if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){ 766cce7d176Sdrh return 1; 767ed6c8671Sdrh } 768cce7d176Sdrh break; 769cce7d176Sdrh } 770cce7d176Sdrh 771d24cc427Sdrh /* A table name and column name: ID.ID 772d24cc427Sdrh ** Or a database, table and column: ID.ID.ID 773d24cc427Sdrh */ 774cce7d176Sdrh case TK_DOT: { 7758141f61eSdrh Token *pColumn; 7768141f61eSdrh Token *pTable; 7778141f61eSdrh Token *pDb; 7788141f61eSdrh Expr *pRight; 779cce7d176Sdrh 780cce7d176Sdrh pRight = pExpr->pRight; 781d24cc427Sdrh if( pRight->op==TK_ID ){ 7828141f61eSdrh pDb = 0; 7838141f61eSdrh pTable = &pExpr->pLeft->token; 7848141f61eSdrh pColumn = &pRight->token; 785d24cc427Sdrh }else{ 7868141f61eSdrh assert( pRight->op==TK_DOT ); 7878141f61eSdrh pDb = &pExpr->pLeft->token; 7888141f61eSdrh pTable = &pRight->pLeft->token; 7898141f61eSdrh pColumn = &pRight->pRight->token; 790d24cc427Sdrh } 7918141f61eSdrh if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){ 792daffd0e5Sdrh return 1; 793daffd0e5Sdrh } 794cce7d176Sdrh break; 795cce7d176Sdrh } 796cce7d176Sdrh 797fef5208cSdrh case TK_IN: { 798e014a838Sdanielk1977 char affinity; 7994adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 800d3d39e93Sdrh KeyInfo keyInfo; 801d3d39e93Sdrh 802fef5208cSdrh if( v==0 ) return 1; 8034adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 804cfab11bcSdrh return 1; 805cfab11bcSdrh } 806bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 807e014a838Sdanielk1977 808e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 809e014a838Sdanielk1977 ** expression it is handled the same way. A temporary table is 810e014a838Sdanielk1977 ** filled with single-field index keys representing the results 811e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 812fef5208cSdrh ** 813e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 814e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 815e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 816e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 817e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 818e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 819e014a838Sdanielk1977 ** is used. 820fef5208cSdrh */ 821832508b7Sdrh pExpr->iTable = pParse->nTab++; 822d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 823d3d39e93Sdrh keyInfo.nField = 1; 824d3d39e93Sdrh keyInfo.aColl[0] = pParse->db->pDfltColl; 825d3d39e93Sdrh sqlite3VdbeOp3(v, OP_OpenTemp, pExpr->iTable, 0, \ 826d3d39e93Sdrh (char*)&keyInfo, P3_KEYINFO); 827e014a838Sdanielk1977 828e014a838Sdanielk1977 if( pExpr->pSelect ){ 829e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 830e014a838Sdanielk1977 ** 831e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 832e014a838Sdanielk1977 ** table allocated and opened above. 833e014a838Sdanielk1977 */ 834e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 835e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 836bf3b721fSdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 837fef5208cSdrh }else if( pExpr->pList ){ 838fef5208cSdrh /* Case 2: expr IN (exprlist) 839fef5208cSdrh ** 840e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 841e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 842e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 843e014a838Sdanielk1977 ** a column, use numeric affinity. 844fef5208cSdrh */ 845e014a838Sdanielk1977 int i; 846e014a838Sdanielk1977 char const *affStr; 847e014a838Sdanielk1977 if( !affinity ){ 848e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 849e014a838Sdanielk1977 } 850e014a838Sdanielk1977 affStr = sqlite3AffinityString(affinity); 851e014a838Sdanielk1977 852e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 853fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 854fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 855e014a838Sdanielk1977 856e014a838Sdanielk1977 /* Check that the expression is constant and valid. */ 8574adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 8584adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 859da93d238Sdrh "right-hand side of IN operator must be constant"); 860fef5208cSdrh return 1; 861fef5208cSdrh } 8624adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){ 8634794b980Sdrh return 1; 8644794b980Sdrh } 865e014a838Sdanielk1977 866e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 8674adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 868e014a838Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); 869e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 870e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0); 871fef5208cSdrh } 872fef5208cSdrh } 873cfab11bcSdrh break; 874fef5208cSdrh } 875fef5208cSdrh 87619a775c2Sdrh case TK_SELECT: { 877fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 878fef5208cSdrh ** value of this select in a memory cell and record the number 879967e8b73Sdrh ** of the memory cell in iColumn. 880fef5208cSdrh */ 881967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 882bf3b721fSdanielk1977 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){ 88319a775c2Sdrh return 1; 88419a775c2Sdrh } 88519a775c2Sdrh break; 88619a775c2Sdrh } 88719a775c2Sdrh 888cce7d176Sdrh /* For all else, just recursively walk the tree */ 889cce7d176Sdrh default: { 890cce7d176Sdrh if( pExpr->pLeft 8914adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 892cce7d176Sdrh return 1; 893cce7d176Sdrh } 894cce7d176Sdrh if( pExpr->pRight 8954adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ 896cce7d176Sdrh return 1; 897cce7d176Sdrh } 898cce7d176Sdrh if( pExpr->pList ){ 899cce7d176Sdrh int i; 900cce7d176Sdrh ExprList *pList = pExpr->pList; 901cce7d176Sdrh for(i=0; i<pList->nExpr; i++){ 902832508b7Sdrh Expr *pArg = pList->a[i].pExpr; 9034adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){ 904cce7d176Sdrh return 1; 905cce7d176Sdrh } 906cce7d176Sdrh } 907cce7d176Sdrh } 908cce7d176Sdrh } 909cce7d176Sdrh } 910cce7d176Sdrh return 0; 911cce7d176Sdrh } 912cce7d176Sdrh 913cce7d176Sdrh /* 9144b59ab5eSdrh ** pExpr is a node that defines a function of some kind. It might 9154b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function 9164b59ab5eSdrh ** that implements an operator, like "a LIKE b". 9174b59ab5eSdrh ** 9184b59ab5eSdrh ** This routine makes *pzName point to the name of the function and 9194b59ab5eSdrh ** *pnName hold the number of characters in the function name. 9204b59ab5eSdrh */ 9214b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 9224b59ab5eSdrh switch( pExpr->op ){ 9234b59ab5eSdrh case TK_FUNCTION: { 9244b59ab5eSdrh *pzName = pExpr->token.z; 9256977fea8Sdrh *pnName = pExpr->token.n; 9264b59ab5eSdrh break; 9274b59ab5eSdrh } 9284b59ab5eSdrh case TK_LIKE: { 9294b59ab5eSdrh *pzName = "like"; 9304b59ab5eSdrh *pnName = 4; 9314b59ab5eSdrh break; 9324b59ab5eSdrh } 9334b59ab5eSdrh case TK_GLOB: { 9344b59ab5eSdrh *pzName = "glob"; 9354b59ab5eSdrh *pnName = 4; 9364b59ab5eSdrh break; 9374b59ab5eSdrh } 9384b59ab5eSdrh default: { 9394b59ab5eSdrh *pzName = "can't happen"; 9404b59ab5eSdrh *pnName = 12; 9414b59ab5eSdrh break; 9424b59ab5eSdrh } 9434b59ab5eSdrh } 9444b59ab5eSdrh } 9454b59ab5eSdrh 9464b59ab5eSdrh /* 947cce7d176Sdrh ** Error check the functions in an expression. Make sure all 948cce7d176Sdrh ** function names are recognized and all functions have the correct 949cce7d176Sdrh ** number of arguments. Leave an error message in pParse->zErrMsg 950cce7d176Sdrh ** if anything is amiss. Return the number of errors. 951cce7d176Sdrh ** 952cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function 953cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 954cce7d176Sdrh */ 9554adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 956cce7d176Sdrh int nErr = 0; 957cce7d176Sdrh if( pExpr==0 ) return 0; 958cce7d176Sdrh switch( pExpr->op ){ 9594b59ab5eSdrh case TK_GLOB: 9604b59ab5eSdrh case TK_LIKE: 961cce7d176Sdrh case TK_FUNCTION: { 962c9b84a1fSdrh int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 963c9b84a1fSdrh int no_such_func = 0; /* True if no such function exists */ 964c9b84a1fSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 965c9b84a1fSdrh int is_agg = 0; /* True if is an aggregate function */ 966cce7d176Sdrh int i; 9674b59ab5eSdrh int nId; /* Number of characters in function name */ 9684b59ab5eSdrh const char *zId; /* The function name. */ 9690bce8354Sdrh FuncDef *pDef; 9700bce8354Sdrh 9714b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 9724adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0); 9730bce8354Sdrh if( pDef==0 ){ 9744adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0); 9750bce8354Sdrh if( pDef==0 ){ 976cce7d176Sdrh no_such_func = 1; 9778e0a2f90Sdrh }else{ 9788e0a2f90Sdrh wrong_num_args = 1; 9798e0a2f90Sdrh } 9808e0a2f90Sdrh }else{ 9810bce8354Sdrh is_agg = pDef->xFunc==0; 982cce7d176Sdrh } 9838e0a2f90Sdrh if( is_agg && !allowAgg ){ 9844adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); 9858e0a2f90Sdrh nErr++; 9868e0a2f90Sdrh is_agg = 0; 9878e0a2f90Sdrh }else if( no_such_func ){ 9884adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 989cce7d176Sdrh nErr++; 9908e0a2f90Sdrh }else if( wrong_num_args ){ 9914adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 992f7a9e1acSdrh nId, zId); 9938e0a2f90Sdrh nErr++; 994cce7d176Sdrh } 995f7a9e1acSdrh if( is_agg ){ 996f7a9e1acSdrh pExpr->op = TK_AGG_FUNCTION; 997f7a9e1acSdrh if( pIsAgg ) *pIsAgg = 1; 998f7a9e1acSdrh } 999cce7d176Sdrh for(i=0; nErr==0 && i<n; i++){ 10004adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr, 10014cfa7934Sdrh allowAgg && !is_agg, pIsAgg); 1002cce7d176Sdrh } 1003d3d39e93Sdrh /** TODO: Compute pExpr->affinity based on the expected return 1004d3d39e93Sdrh ** type of the function */ 1005cce7d176Sdrh } 1006cce7d176Sdrh default: { 1007cce7d176Sdrh if( pExpr->pLeft ){ 10084adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 1009cce7d176Sdrh } 1010cce7d176Sdrh if( nErr==0 && pExpr->pRight ){ 10114adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 1012cce7d176Sdrh } 1013fef5208cSdrh if( nErr==0 && pExpr->pList ){ 1014fef5208cSdrh int n = pExpr->pList->nExpr; 1015fef5208cSdrh int i; 1016fef5208cSdrh for(i=0; nErr==0 && i<n; i++){ 10172282792aSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 10184adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg); 1019fef5208cSdrh } 1020fef5208cSdrh } 1021cce7d176Sdrh break; 1022cce7d176Sdrh } 1023cce7d176Sdrh } 1024cce7d176Sdrh return nErr; 1025cce7d176Sdrh } 1026cce7d176Sdrh 1027cce7d176Sdrh /* 1028d3d39e93Sdrh ** Return one of the SQLITE_AFF_* affinity types that indicates the likely 1029d3d39e93Sdrh ** data type of the result of the given expression. 1030d3d39e93Sdrh ** 1031d3d39e93Sdrh ** Not every expression has a fixed type. If the type cannot be determined 1032d3d39e93Sdrh ** at compile-time, then try to return the type affinity if the expression 1033d3d39e93Sdrh ** is a column. Otherwise just return SQLITE_AFF_NONE. 1034c9b84a1fSdrh ** 10354adee20fSdanielk1977 ** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have 1036c9b84a1fSdrh ** both been called on the expression before it is passed to this routine. 1037c9b84a1fSdrh */ 10384adee20fSdanielk1977 int sqlite3ExprType(Expr *p){ 1039d3d39e93Sdrh if( p==0 ) return SQLITE_AFF_NONE; 1040c9b84a1fSdrh while( p ) switch( p->op ){ 1041c9b84a1fSdrh case TK_CONCAT: 1042*736c22b8Sdrh case TK_STRING: 1043d3d39e93Sdrh return SQLITE_AFF_TEXT; 1044c9b84a1fSdrh 1045c9b84a1fSdrh case TK_AS: 1046c9b84a1fSdrh p = p->pLeft; 1047c9b84a1fSdrh break; 1048c9b84a1fSdrh 1049*736c22b8Sdrh case TK_VARIABLE: 1050d3d39e93Sdrh case TK_NULL: 1051d3d39e93Sdrh return SQLITE_AFF_NONE; 1052c9b84a1fSdrh 1053d3d39e93Sdrh case TK_SELECT: /*** FIX ME ****/ 1054d3d39e93Sdrh case TK_COLUMN: /*** FIX ME ****/ 1055d3d39e93Sdrh case TK_CASE: /*** FIX ME ****/ 1056b1363206Sdrh 1057c9b84a1fSdrh default: 1058d3d39e93Sdrh return SQLITE_AFF_NUMERIC; 1059c9b84a1fSdrh } 1060d3d39e93Sdrh return SQLITE_AFF_NONE; 1061c9b84a1fSdrh } 1062c9b84a1fSdrh 1063c9b84a1fSdrh /* 1064fec19aadSdrh ** Generate an instruction that will put the integer describe by 1065fec19aadSdrh ** text z[0..n-1] on the stack. 1066fec19aadSdrh */ 1067fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1068fec19aadSdrh int i; 1069fec19aadSdrh if( sqlite3GetInt32(z, &i) || (i=0, sqlite3FitsIn64Bits(z))!=0 ){ 1070fec19aadSdrh sqlite3VdbeOp3(v, OP_Integer, i, 0, z, n); 1071fec19aadSdrh }else{ 1072fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1073fec19aadSdrh } 1074fec19aadSdrh } 1075fec19aadSdrh 1076fec19aadSdrh /* 1077cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 10781ccde15dSdrh ** expression and leave the result on the top of stack. 1079cce7d176Sdrh */ 10804adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1081cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1082cce7d176Sdrh int op; 1083daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1084cce7d176Sdrh switch( pExpr->op ){ 1085cce7d176Sdrh case TK_PLUS: op = OP_Add; break; 1086cce7d176Sdrh case TK_MINUS: op = OP_Subtract; break; 1087cce7d176Sdrh case TK_STAR: op = OP_Multiply; break; 1088cce7d176Sdrh case TK_SLASH: op = OP_Divide; break; 1089cce7d176Sdrh case TK_AND: op = OP_And; break; 1090cce7d176Sdrh case TK_OR: op = OP_Or; break; 1091cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1092cce7d176Sdrh case TK_LE: op = OP_Le; break; 1093cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1094cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1095cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1096cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1097cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1098cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1099cce7d176Sdrh case TK_NOT: op = OP_Not; break; 1100cce7d176Sdrh case TK_UMINUS: op = OP_Negative; break; 1101bf4133cbSdrh case TK_BITAND: op = OP_BitAnd; break; 1102bf4133cbSdrh case TK_BITOR: op = OP_BitOr; break; 1103bf4133cbSdrh case TK_BITNOT: op = OP_BitNot; break; 1104bf4133cbSdrh case TK_LSHIFT: op = OP_ShiftLeft; break; 1105bf4133cbSdrh case TK_RSHIFT: op = OP_ShiftRight; break; 1106bf4133cbSdrh case TK_REM: op = OP_Remainder; break; 1107fec19aadSdrh case TK_FLOAT: op = OP_Real; break; 1108fec19aadSdrh case TK_STRING: op = OP_String; break; 1109cce7d176Sdrh default: break; 1110cce7d176Sdrh } 1111cce7d176Sdrh switch( pExpr->op ){ 1112967e8b73Sdrh case TK_COLUMN: { 11132282792aSdrh if( pParse->useAgg ){ 11144adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1115c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 11164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1117c4a3c779Sdrh }else{ 11184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 11192282792aSdrh } 1120cce7d176Sdrh break; 1121cce7d176Sdrh } 1122cce7d176Sdrh case TK_INTEGER: { 1123fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1124fec19aadSdrh break; 112551e9a445Sdrh } 1126fec19aadSdrh case TK_FLOAT: 1127fec19aadSdrh case TK_STRING: { 1128fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 11294adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1130cce7d176Sdrh break; 1131cce7d176Sdrh } 1132cce7d176Sdrh case TK_NULL: { 11334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 1134cce7d176Sdrh break; 1135cce7d176Sdrh } 113650457896Sdrh case TK_VARIABLE: { 11374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 113850457896Sdrh break; 113950457896Sdrh } 1140c9b84a1fSdrh case TK_LT: 1141c9b84a1fSdrh case TK_LE: 1142c9b84a1fSdrh case TK_GT: 1143c9b84a1fSdrh case TK_GE: 1144c9b84a1fSdrh case TK_NE: 1145c9b84a1fSdrh case TK_EQ: { 1146a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0); 1147a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1148a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1149a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, 0); 1150a37cdde0Sdanielk1977 break; 1151c9b84a1fSdrh } 1152cce7d176Sdrh case TK_AND: 1153cce7d176Sdrh case TK_OR: 1154cce7d176Sdrh case TK_PLUS: 1155cce7d176Sdrh case TK_STAR: 1156cce7d176Sdrh case TK_MINUS: 1157bf4133cbSdrh case TK_REM: 1158bf4133cbSdrh case TK_BITAND: 1159bf4133cbSdrh case TK_BITOR: 1160c9b84a1fSdrh case TK_SLASH: { 11614adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11624adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11634adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1164cce7d176Sdrh break; 1165cce7d176Sdrh } 1166bf4133cbSdrh case TK_LSHIFT: 1167bf4133cbSdrh case TK_RSHIFT: { 11684adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11694adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11704adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1171bf4133cbSdrh break; 1172bf4133cbSdrh } 11730040077dSdrh case TK_CONCAT: { 11744adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11754adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Concat, 2, 0); 11770040077dSdrh break; 11780040077dSdrh } 1179cce7d176Sdrh case TK_UMINUS: { 1180fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1181fec19aadSdrh assert( pLeft ); 1182fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1183fec19aadSdrh Token *p = &pLeft->token; 11846e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 11856e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1186fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1187fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1188e6840900Sdrh }else{ 1189fec19aadSdrh codeInteger(v, z, p->n+1); 1190e6840900Sdrh } 11916e142f54Sdrh sqliteFree(z); 11926e142f54Sdrh break; 11936e142f54Sdrh } 11941ccde15dSdrh /* Fall through into TK_NOT */ 11956e142f54Sdrh } 1196bf4133cbSdrh case TK_BITNOT: 11976e142f54Sdrh case TK_NOT: { 11984adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11994adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1200cce7d176Sdrh break; 1201cce7d176Sdrh } 1202cce7d176Sdrh case TK_ISNULL: 1203cce7d176Sdrh case TK_NOTNULL: { 1204cce7d176Sdrh int dest; 12054adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 12064adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12074adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 12084adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 12094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1210cce7d176Sdrh } 1211a37cdde0Sdanielk1977 break; 12122282792aSdrh case TK_AGG_FUNCTION: { 12134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 12142282792aSdrh break; 12152282792aSdrh } 12164b59ab5eSdrh case TK_GLOB: 12174b59ab5eSdrh case TK_LIKE: 1218cce7d176Sdrh case TK_FUNCTION: { 1219cce7d176Sdrh ExprList *pList = pExpr->pList; 122089425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 12210bce8354Sdrh FuncDef *pDef; 12224b59ab5eSdrh int nId; 12234b59ab5eSdrh const char *zId; 12244b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 12254adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0); 12260bce8354Sdrh assert( pDef!=0 ); 12274adee20fSdanielk1977 nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes); 1228a37cdde0Sdanielk1977 /* FIX ME: The following is a temporary hack. */ 1229a37cdde0Sdanielk1977 if( 0==sqlite3StrNICmp(zId, "classof", nId) ){ 1230a37cdde0Sdanielk1977 assert( nExpr==1 ); 1231fec19aadSdrh sqlite3VdbeAddOp(v, OP_Class, nExpr, 0); 1232a37cdde0Sdanielk1977 }else{ 12334adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER); 1234a37cdde0Sdanielk1977 } 12356ec2733bSdrh break; 12366ec2733bSdrh } 123719a775c2Sdrh case TK_SELECT: { 12384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 123919a775c2Sdrh break; 124019a775c2Sdrh } 1241fef5208cSdrh case TK_IN: { 1242fef5208cSdrh int addr; 1243e014a838Sdanielk1977 char const *affStr; 1244e014a838Sdanielk1977 1245e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1246e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1247e014a838Sdanielk1977 ** P3 of OP_MakeKey. 1248e014a838Sdanielk1977 */ 1249e014a838Sdanielk1977 affStr = sqlite3AffinityString(comparisonAffinity(pExpr)); 1250e014a838Sdanielk1977 12514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1252e014a838Sdanielk1977 1253e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1254e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1255e014a838Sdanielk1977 */ 12564adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12574adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1258e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 12594adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 12604adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 1261e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 1262e014a838Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */ 1263e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1264e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1265e014a838Sdanielk1977 1266fef5208cSdrh break; 1267fef5208cSdrh } 1268fef5208cSdrh case TK_BETWEEN: { 12694adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12704adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 12714adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 12724adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, 0, 0); 12734adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 12744adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 12754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, 0, 0); 12764adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1277fef5208cSdrh break; 1278fef5208cSdrh } 127951e9a445Sdrh case TK_UPLUS: 1280a2e00042Sdrh case TK_AS: { 12814adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1282a2e00042Sdrh break; 1283a2e00042Sdrh } 128417a7f8ddSdrh case TK_CASE: { 128517a7f8ddSdrh int expr_end_label; 1286f5905aa7Sdrh int jumpInst; 1287f5905aa7Sdrh int addr; 1288f5905aa7Sdrh int nExpr; 128917a7f8ddSdrh int i; 129017a7f8ddSdrh 129117a7f8ddSdrh assert(pExpr->pList); 129217a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 129317a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1294f5905aa7Sdrh nExpr = pExpr->pList->nExpr; 12954adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 129617a7f8ddSdrh if( pExpr->pLeft ){ 12974adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1298cce7d176Sdrh } 1299f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 13004adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr); 130117a7f8ddSdrh if( pExpr->pLeft ){ 13024adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 13034adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0); 13044adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1305f5905aa7Sdrh }else{ 13064adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 130717a7f8ddSdrh } 13084adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr); 13094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 13104adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 13114adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 131217a7f8ddSdrh } 1313f570f011Sdrh if( pExpr->pLeft ){ 13144adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1315f570f011Sdrh } 131617a7f8ddSdrh if( pExpr->pRight ){ 13174adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 131817a7f8ddSdrh }else{ 13194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 132017a7f8ddSdrh } 13214adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 13226f34903eSdanielk1977 break; 13236f34903eSdanielk1977 } 13246f34903eSdanielk1977 case TK_RAISE: { 13256f34903eSdanielk1977 if( !pParse->trigStack ){ 13264adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1327da93d238Sdrh "RAISE() may only be used within a trigger-program"); 13286f34903eSdanielk1977 pParse->nErr++; 13296f34903eSdanielk1977 return; 13306f34903eSdanielk1977 } 13316f34903eSdanielk1977 if( pExpr->iColumn == OE_Rollback || 13326f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 13336f34903eSdanielk1977 pExpr->iColumn == OE_Fail ){ 13344adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1335701a0aebSdrh pExpr->token.z, pExpr->token.n); 13364adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 13376f34903eSdanielk1977 } else { 13386f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 13394adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump, 1340701a0aebSdrh "(IGNORE jump)", 0); 13416f34903eSdanielk1977 } 134217a7f8ddSdrh } 134317a7f8ddSdrh break; 134417a7f8ddSdrh } 1345cce7d176Sdrh } 1346cce7d176Sdrh 1347cce7d176Sdrh /* 1348268380caSdrh ** Generate code that pushes the value of every element of the given 1349268380caSdrh ** expression list onto the stack. If the includeTypes flag is true, 1350268380caSdrh ** then also push a string that is the datatype of each element onto 1351268380caSdrh ** the stack after the value. 1352268380caSdrh ** 1353268380caSdrh ** Return the number of elements pushed onto the stack. 1354268380caSdrh */ 13554adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1356268380caSdrh Parse *pParse, /* Parsing context */ 1357268380caSdrh ExprList *pList, /* The expression list to be coded */ 1358268380caSdrh int includeTypes /* TRUE to put datatypes on the stack too */ 1359268380caSdrh ){ 1360268380caSdrh struct ExprList_item *pItem; 1361268380caSdrh int i, n; 1362268380caSdrh Vdbe *v; 1363268380caSdrh if( pList==0 ) return 0; 13644adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1365268380caSdrh n = pList->nExpr; 1366268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 13674adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1368268380caSdrh if( includeTypes ){ 1369d3d39e93Sdrh /** DEPRECATED. This will go away with the new function interface **/ 1370d3d39e93Sdrh sqlite3VdbeOp3(v, OP_String, 0, 0, "numeric", P3_STATIC); 1371268380caSdrh } 1372268380caSdrh } 1373268380caSdrh return includeTypes ? n*2 : n; 1374268380caSdrh } 1375268380caSdrh 1376268380caSdrh /* 1377cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1378cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1379cce7d176Sdrh ** continues straight thru if the expression is false. 1380f5905aa7Sdrh ** 1381f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1382f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1383cce7d176Sdrh */ 13844adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1385cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1386cce7d176Sdrh int op = 0; 1387daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1388cce7d176Sdrh switch( pExpr->op ){ 1389cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1390cce7d176Sdrh case TK_LE: op = OP_Le; break; 1391cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1392cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1393cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1394cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1395cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1396cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1397cce7d176Sdrh default: break; 1398cce7d176Sdrh } 1399cce7d176Sdrh switch( pExpr->op ){ 1400cce7d176Sdrh case TK_AND: { 14014adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 14024adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 14034adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 14044adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1405cce7d176Sdrh break; 1406cce7d176Sdrh } 1407cce7d176Sdrh case TK_OR: { 14084adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 14094adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1410cce7d176Sdrh break; 1411cce7d176Sdrh } 1412cce7d176Sdrh case TK_NOT: { 14134adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1414cce7d176Sdrh break; 1415cce7d176Sdrh } 1416cce7d176Sdrh case TK_LT: 1417cce7d176Sdrh case TK_LE: 1418cce7d176Sdrh case TK_GT: 1419cce7d176Sdrh case TK_GE: 1420cce7d176Sdrh case TK_NE: 14210ac65892Sdrh case TK_EQ: { 1422a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 14234adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14244adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1425a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1426cce7d176Sdrh break; 1427cce7d176Sdrh } 1428cce7d176Sdrh case TK_ISNULL: 1429cce7d176Sdrh case TK_NOTNULL: { 14304adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14314adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1432cce7d176Sdrh break; 1433cce7d176Sdrh } 1434e014a838Sdanielk1977 #if 0 1435fef5208cSdrh case TK_IN: { 1436f5905aa7Sdrh int addr; 14374adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14384adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 14394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3); 14404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 14414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1442fef5208cSdrh if( pExpr->pSelect ){ 14434adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, dest); 1444fef5208cSdrh }else{ 14454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetFound, pExpr->iTable, dest); 1446fef5208cSdrh } 1447fef5208cSdrh break; 1448fef5208cSdrh } 1449e014a838Sdanielk1977 #endif 1450fef5208cSdrh case TK_BETWEEN: { 1451f5905aa7Sdrh int addr; 14524adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 14544adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 14554adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0); 14564adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 14574adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest); 14584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 14594adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 14604adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1461fef5208cSdrh break; 1462fef5208cSdrh } 1463cce7d176Sdrh default: { 14644adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 14654adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1466cce7d176Sdrh break; 1467cce7d176Sdrh } 1468cce7d176Sdrh } 1469cce7d176Sdrh } 1470cce7d176Sdrh 1471cce7d176Sdrh /* 147266b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1473cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1474cce7d176Sdrh ** continues straight thru if the expression is true. 1475f5905aa7Sdrh ** 1476f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1477f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1478cce7d176Sdrh */ 14794adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1480cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1481cce7d176Sdrh int op = 0; 1482daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1483cce7d176Sdrh switch( pExpr->op ){ 1484cce7d176Sdrh case TK_LT: op = OP_Ge; break; 1485cce7d176Sdrh case TK_LE: op = OP_Gt; break; 1486cce7d176Sdrh case TK_GT: op = OP_Le; break; 1487cce7d176Sdrh case TK_GE: op = OP_Lt; break; 1488cce7d176Sdrh case TK_NE: op = OP_Eq; break; 1489cce7d176Sdrh case TK_EQ: op = OP_Ne; break; 1490cce7d176Sdrh case TK_ISNULL: op = OP_NotNull; break; 1491cce7d176Sdrh case TK_NOTNULL: op = OP_IsNull; break; 1492cce7d176Sdrh default: break; 1493cce7d176Sdrh } 1494cce7d176Sdrh switch( pExpr->op ){ 1495cce7d176Sdrh case TK_AND: { 14964adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 14974adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1498cce7d176Sdrh break; 1499cce7d176Sdrh } 1500cce7d176Sdrh case TK_OR: { 15014adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 15024adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 15034adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 15044adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1505cce7d176Sdrh break; 1506cce7d176Sdrh } 1507cce7d176Sdrh case TK_NOT: { 15084adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1509cce7d176Sdrh break; 1510cce7d176Sdrh } 1511cce7d176Sdrh case TK_LT: 1512cce7d176Sdrh case TK_LE: 1513cce7d176Sdrh case TK_GT: 1514cce7d176Sdrh case TK_GE: 1515cce7d176Sdrh case TK_NE: 1516cce7d176Sdrh case TK_EQ: { 1517a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 15184adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15194adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1520a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1521cce7d176Sdrh break; 1522cce7d176Sdrh } 1523cce7d176Sdrh case TK_ISNULL: 1524cce7d176Sdrh case TK_NOTNULL: { 15254adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15264adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1527cce7d176Sdrh break; 1528cce7d176Sdrh } 1529e014a838Sdanielk1977 #if 0 1530fef5208cSdrh case TK_IN: { 1531f5905aa7Sdrh int addr; 15324adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15334adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3); 15354adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1537fef5208cSdrh if( pExpr->pSelect ){ 15384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); 1539fef5208cSdrh }else{ 15404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); 1541fef5208cSdrh } 1542fef5208cSdrh break; 1543fef5208cSdrh } 1544e014a838Sdanielk1977 #endif 1545fef5208cSdrh case TK_BETWEEN: { 1546fef5208cSdrh int addr; 15474adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 15494adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 15504adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); 15524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 15544adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 15554adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest); 1556fef5208cSdrh break; 1557fef5208cSdrh } 1558cce7d176Sdrh default: { 15594adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 15604adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1561cce7d176Sdrh break; 1562cce7d176Sdrh } 1563cce7d176Sdrh } 1564cce7d176Sdrh } 15652282792aSdrh 15662282792aSdrh /* 15672282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 15682282792aSdrh ** if they are identical and return FALSE if they differ in any way. 15692282792aSdrh */ 15704adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 15712282792aSdrh int i; 15722282792aSdrh if( pA==0 ){ 15732282792aSdrh return pB==0; 15742282792aSdrh }else if( pB==0 ){ 15752282792aSdrh return 0; 15762282792aSdrh } 15772282792aSdrh if( pA->op!=pB->op ) return 0; 15784adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 15794adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 15802282792aSdrh if( pA->pList ){ 15812282792aSdrh if( pB->pList==0 ) return 0; 15822282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 15832282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 15844adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 15852282792aSdrh return 0; 15862282792aSdrh } 15872282792aSdrh } 15882282792aSdrh }else if( pB->pList ){ 15892282792aSdrh return 0; 15902282792aSdrh } 15912282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 15922f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 15932282792aSdrh if( pA->token.z ){ 15942282792aSdrh if( pB->token.z==0 ) return 0; 15956977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 15964adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 15972282792aSdrh } 15982282792aSdrh return 1; 15992282792aSdrh } 16002282792aSdrh 16012282792aSdrh /* 16022282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 16032282792aSdrh */ 16042282792aSdrh static int appendAggInfo(Parse *pParse){ 16052282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 16062282792aSdrh int amt = pParse->nAgg + 8; 16076d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 16086d4abfbeSdrh if( aAgg==0 ){ 16092282792aSdrh return -1; 16102282792aSdrh } 16116d4abfbeSdrh pParse->aAgg = aAgg; 16122282792aSdrh } 16132282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 16142282792aSdrh return pParse->nAgg++; 16152282792aSdrh } 16162282792aSdrh 16172282792aSdrh /* 16182282792aSdrh ** Analyze the given expression looking for aggregate functions and 16192282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 16202282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 16212282792aSdrh ** 16222282792aSdrh ** This routine should only be called after the expression has been 16234adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck(). 16242282792aSdrh ** 16252282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return 16262282792aSdrh ** the number of errors. 16272282792aSdrh */ 16284adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 16292282792aSdrh int i; 16302282792aSdrh AggExpr *aAgg; 16312282792aSdrh int nErr = 0; 16322282792aSdrh 16332282792aSdrh if( pExpr==0 ) return 0; 16342282792aSdrh switch( pExpr->op ){ 1635967e8b73Sdrh case TK_COLUMN: { 16362282792aSdrh aAgg = pParse->aAgg; 16372282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16382282792aSdrh if( aAgg[i].isAgg ) continue; 16392282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 1640967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 16412282792aSdrh break; 16422282792aSdrh } 16432282792aSdrh } 16442282792aSdrh if( i>=pParse->nAgg ){ 16452282792aSdrh i = appendAggInfo(pParse); 16462282792aSdrh if( i<0 ) return 1; 16472282792aSdrh pParse->aAgg[i].isAgg = 0; 16482282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16492282792aSdrh } 1650aaf88729Sdrh pExpr->iAgg = i; 16512282792aSdrh break; 16522282792aSdrh } 16532282792aSdrh case TK_AGG_FUNCTION: { 16542282792aSdrh aAgg = pParse->aAgg; 16552282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16562282792aSdrh if( !aAgg[i].isAgg ) continue; 16574adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 16582282792aSdrh break; 16592282792aSdrh } 16602282792aSdrh } 16612282792aSdrh if( i>=pParse->nAgg ){ 16622282792aSdrh i = appendAggInfo(pParse); 16632282792aSdrh if( i<0 ) return 1; 16642282792aSdrh pParse->aAgg[i].isAgg = 1; 16652282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16664adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 16676977fea8Sdrh pExpr->token.z, pExpr->token.n, 1668f55f25f0Sdrh pExpr->pList ? pExpr->pList->nExpr : 0, 0); 16692282792aSdrh } 16702282792aSdrh pExpr->iAgg = i; 16712282792aSdrh break; 16722282792aSdrh } 16732282792aSdrh default: { 16742282792aSdrh if( pExpr->pLeft ){ 16754adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft); 16762282792aSdrh } 16772282792aSdrh if( nErr==0 && pExpr->pRight ){ 16784adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight); 16792282792aSdrh } 16802282792aSdrh if( nErr==0 && pExpr->pList ){ 16812282792aSdrh int n = pExpr->pList->nExpr; 16822282792aSdrh int i; 16832282792aSdrh for(i=0; nErr==0 && i<n; i++){ 16844adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 16852282792aSdrh } 16862282792aSdrh } 16872282792aSdrh break; 16882282792aSdrh } 16892282792aSdrh } 16902282792aSdrh return nErr; 16912282792aSdrh } 16928e0a2f90Sdrh 16938e0a2f90Sdrh /* 16948e0a2f90Sdrh ** Locate a user function given a name and a number of arguments. 16950bce8354Sdrh ** Return a pointer to the FuncDef structure that defines that 16968e0a2f90Sdrh ** function, or return NULL if the function does not exist. 16978e0a2f90Sdrh ** 16980bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef 16998e0a2f90Sdrh ** structure is created and liked into the "db" structure if a 17008e0a2f90Sdrh ** no matching function previously existed. When createFlag is true 17018e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts 17028e0a2f90Sdrh ** any number of arguments will be returned. 17038e0a2f90Sdrh ** 17048e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid 17058e0a2f90Sdrh ** function found is returned. A function is valid if either xFunc 17068e0a2f90Sdrh ** or xStep is non-zero. 17078e0a2f90Sdrh */ 17084adee20fSdanielk1977 FuncDef *sqlite3FindFunction( 17098e0a2f90Sdrh sqlite *db, /* An open database */ 17108e0a2f90Sdrh const char *zName, /* Name of the function. Not null-terminated */ 17118e0a2f90Sdrh int nName, /* Number of characters in the name */ 17128e0a2f90Sdrh int nArg, /* Number of arguments. -1 means any number */ 17138e0a2f90Sdrh int createFlag /* Create new entry if true and does not otherwise exist */ 17148e0a2f90Sdrh ){ 17150bce8354Sdrh FuncDef *pFirst, *p, *pMaybe; 17164adee20fSdanielk1977 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); 17171350b030Sdrh if( p && !createFlag && nArg<0 ){ 17188e0a2f90Sdrh while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } 17198e0a2f90Sdrh return p; 17208e0a2f90Sdrh } 17218e0a2f90Sdrh pMaybe = 0; 17228e0a2f90Sdrh while( p && p->nArg!=nArg ){ 17238e0a2f90Sdrh if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; 17248e0a2f90Sdrh p = p->pNext; 17258e0a2f90Sdrh } 17268e0a2f90Sdrh if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ 17278e0a2f90Sdrh return 0; 17288e0a2f90Sdrh } 17298e0a2f90Sdrh if( p==0 && pMaybe ){ 17308e0a2f90Sdrh assert( createFlag==0 ); 17318e0a2f90Sdrh return pMaybe; 17328e0a2f90Sdrh } 173389425d5eSdrh if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){ 17348e0a2f90Sdrh p->nArg = nArg; 17358e0a2f90Sdrh p->pNext = pFirst; 1736c9b84a1fSdrh p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC; 17374adee20fSdanielk1977 sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p); 17388e0a2f90Sdrh } 17398e0a2f90Sdrh return p; 17408e0a2f90Sdrh } 1741