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*f9b596ebSdrh ** $Id: expr.c,v 1.128 2004/05/26 16:54:43 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->op Set to TK_COLUMN. 5078141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 5088141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 5098141f61eSdrh ** 5108141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 5118141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 5128141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 5138141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 5148141f61eSdrh ** means that the form of the name is Z and that columns from any table 5158141f61eSdrh ** can be used. 5168141f61eSdrh ** 5178141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 5188141f61eSdrh ** in pParse and return non-zero. Return zero on success. 5198141f61eSdrh */ 5208141f61eSdrh static int lookupName( 5218141f61eSdrh Parse *pParse, /* The parsing context */ 5228141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 5238141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 5248141f61eSdrh Token *pColumnToken, /* Name of the column. */ 5258141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 5268141f61eSdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 5278141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 5288141f61eSdrh ){ 5298141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 5308141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 5318141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 5328141f61eSdrh int i, j; /* Loop counters */ 5338141f61eSdrh int cnt = 0; /* Number of matching column names */ 5348141f61eSdrh int cntTab = 0; /* Number of matching table names */ 5357e26d750Sdrh sqlite *db = pParse->db; /* The database */ 5368141f61eSdrh 5378141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 5388141f61eSdrh if( pDbToken && pDbToken->z ){ 5398141f61eSdrh zDb = sqliteStrNDup(pDbToken->z, pDbToken->n); 5404adee20fSdanielk1977 sqlite3Dequote(zDb); 5418141f61eSdrh }else{ 5428141f61eSdrh zDb = 0; 5438141f61eSdrh } 5448141f61eSdrh if( pTableToken && pTableToken->z ){ 5458141f61eSdrh zTab = sqliteStrNDup(pTableToken->z, pTableToken->n); 5464adee20fSdanielk1977 sqlite3Dequote(zTab); 5478141f61eSdrh }else{ 5488141f61eSdrh assert( zDb==0 ); 5498141f61eSdrh zTab = 0; 5508141f61eSdrh } 5518141f61eSdrh zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n); 5524adee20fSdanielk1977 sqlite3Dequote(zCol); 55324b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 5548141f61eSdrh return 1; /* Leak memory (zDb and zTab) if malloc fails */ 5558141f61eSdrh } 5568141f61eSdrh assert( zTab==0 || pEList==0 ); 5578141f61eSdrh 5588141f61eSdrh pExpr->iTable = -1; 5598141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 5608141f61eSdrh struct SrcList_item *pItem = &pSrcList->a[i]; 5618141f61eSdrh Table *pTab = pItem->pTab; 5628141f61eSdrh Column *pCol; 5638141f61eSdrh 5648141f61eSdrh if( pTab==0 ) continue; 5658141f61eSdrh assert( pTab->nCol>0 ); 5668141f61eSdrh if( zTab ){ 5678141f61eSdrh if( pItem->zAlias ){ 5688141f61eSdrh char *zTabName = pItem->zAlias; 5694adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 5708141f61eSdrh }else{ 5718141f61eSdrh char *zTabName = pTab->zName; 5724adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 5734adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 5748141f61eSdrh continue; 5758141f61eSdrh } 5768141f61eSdrh } 5778141f61eSdrh } 5788141f61eSdrh if( 0==(cntTab++) ){ 5798141f61eSdrh pExpr->iTable = pItem->iCursor; 5808141f61eSdrh pExpr->iDb = pTab->iDb; 5818141f61eSdrh } 5828141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 5834adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 5848141f61eSdrh cnt++; 5858141f61eSdrh pExpr->iTable = pItem->iCursor; 5868141f61eSdrh pExpr->iDb = pTab->iDb; 5878141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 5888141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 589a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 5908141f61eSdrh break; 5918141f61eSdrh } 5928141f61eSdrh } 5938141f61eSdrh } 5948141f61eSdrh 5958141f61eSdrh /* If we have not already resolved the name, then maybe 5968141f61eSdrh ** it is a new.* or old.* trigger argument reference 5978141f61eSdrh */ 5988141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 5998141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 6008141f61eSdrh Table *pTab = 0; 6014adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 6028141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 6038141f61eSdrh assert( pTriggerStack->pTab ); 6048141f61eSdrh pTab = pTriggerStack->pTab; 6054adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){ 6068141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 6078141f61eSdrh assert( pTriggerStack->pTab ); 6088141f61eSdrh pTab = pTriggerStack->pTab; 6098141f61eSdrh } 6108141f61eSdrh 6118141f61eSdrh if( pTab ){ 6128141f61eSdrh int j; 6138141f61eSdrh Column *pCol = pTab->aCol; 6148141f61eSdrh 6158141f61eSdrh pExpr->iDb = pTab->iDb; 6168141f61eSdrh cntTab++; 6178141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 6184adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 6198141f61eSdrh cnt++; 6208141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 621a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 6228141f61eSdrh break; 6238141f61eSdrh } 6248141f61eSdrh } 6258141f61eSdrh } 6268141f61eSdrh } 6278141f61eSdrh 6288141f61eSdrh /* 6298141f61eSdrh ** Perhaps the name is a reference to the ROWID 6308141f61eSdrh */ 6314adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 6328141f61eSdrh cnt = 1; 6338141f61eSdrh pExpr->iColumn = -1; 634a37cdde0Sdanielk1977 pExpr->affinity = SQLITE_AFF_INTEGER; 6358141f61eSdrh } 6368141f61eSdrh 6378141f61eSdrh /* 6388141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 6398141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 6408141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 6418141f61eSdrh ** 6428141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 6438141f61eSdrh ** 6448141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 6458141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 6468141f61eSdrh ** Note that the expression in the result set should have already been 6478141f61eSdrh ** resolved by the time the WHERE clause is resolved. 6488141f61eSdrh */ 6498141f61eSdrh if( cnt==0 && pEList!=0 ){ 6508141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 6518141f61eSdrh char *zAs = pEList->a[j].zName; 6524adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 6538141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 6548141f61eSdrh pExpr->op = TK_AS; 6558141f61eSdrh pExpr->iColumn = j; 6564adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 6578141f61eSdrh sqliteFree(zCol); 6588141f61eSdrh assert( zTab==0 && zDb==0 ); 6598141f61eSdrh return 0; 6608141f61eSdrh } 6618141f61eSdrh } 6628141f61eSdrh } 6638141f61eSdrh 6648141f61eSdrh /* 6658141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 6668141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 6678141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 6688141f61eSdrh ** case, we need to return right away and not make any changes to 6698141f61eSdrh ** pExpr. 6708141f61eSdrh */ 6718141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 6728141f61eSdrh sqliteFree(zCol); 6738141f61eSdrh return 0; 6748141f61eSdrh } 6758141f61eSdrh 6768141f61eSdrh /* 6778141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 6788141f61eSdrh ** more matches. Either way, we have an error. 6798141f61eSdrh */ 6808141f61eSdrh if( cnt!=1 ){ 6818141f61eSdrh char *z = 0; 6828141f61eSdrh char *zErr; 6838141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 6848141f61eSdrh if( zDb ){ 6854adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 6868141f61eSdrh }else if( zTab ){ 6874adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 6888141f61eSdrh }else{ 6898141f61eSdrh z = sqliteStrDup(zCol); 6908141f61eSdrh } 6914adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 6928141f61eSdrh sqliteFree(z); 6938141f61eSdrh } 6948141f61eSdrh 6958141f61eSdrh /* Clean up and return 6968141f61eSdrh */ 6978141f61eSdrh sqliteFree(zDb); 6988141f61eSdrh sqliteFree(zTab); 6998141f61eSdrh sqliteFree(zCol); 7004adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 7018141f61eSdrh pExpr->pLeft = 0; 7024adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 7038141f61eSdrh pExpr->pRight = 0; 7048141f61eSdrh pExpr->op = TK_COLUMN; 7054adee20fSdanielk1977 sqlite3AuthRead(pParse, pExpr, pSrcList); 7068141f61eSdrh return cnt!=1; 7078141f61eSdrh } 7088141f61eSdrh 7098141f61eSdrh /* 710cce7d176Sdrh ** This routine walks an expression tree and resolves references to 711967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 712aacc543eSdrh ** index to the table in the table list and a column offset. The 713aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 714aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 715832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 716aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 717aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 718aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 719aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 720aacc543eSdrh ** alias for ROWID. 72119a775c2Sdrh ** 722fef5208cSdrh ** We also check for instances of the IN operator. IN comes in two 723fef5208cSdrh ** forms: 724fef5208cSdrh ** 725fef5208cSdrh ** expr IN (exprlist) 726fef5208cSdrh ** and 727fef5208cSdrh ** expr IN (SELECT ...) 728fef5208cSdrh ** 729fef5208cSdrh ** The first form is handled by creating a set holding the list 730fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 731fef5208cSdrh ** a temporary table. 732fef5208cSdrh ** 733fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression. 73419a775c2Sdrh ** If it finds any, it generates code to write the value of that select 73519a775c2Sdrh ** into a memory cell. 736cce7d176Sdrh ** 737967e8b73Sdrh ** Unknown columns or tables provoke an error. The function returns 738cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg. 739cce7d176Sdrh */ 7404adee20fSdanielk1977 int sqlite3ExprResolveIds( 741a2e00042Sdrh Parse *pParse, /* The parser context */ 7428141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 743a2e00042Sdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 744a2e00042Sdrh Expr *pExpr /* The expression to be analyzed. */ 745a2e00042Sdrh ){ 7466a3ea0e6Sdrh int i; 7476a3ea0e6Sdrh 7488141f61eSdrh if( pExpr==0 || pSrcList==0 ) return 0; 7498141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 7508141f61eSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab ); 7516a3ea0e6Sdrh } 752cce7d176Sdrh switch( pExpr->op ){ 7532398937bSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 7542398937bSdrh ** possible. Otherwise they remain as strings. Single-quoted 7552398937bSdrh ** strings (ex: 'abc') are always string literals. 7562398937bSdrh */ 7572398937bSdrh case TK_STRING: { 7582398937bSdrh if( pExpr->token.z[0]=='\'' ) break; 7592398937bSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 7602398937bSdrh } 7618141f61eSdrh /* A lone identifier is the name of a columnd. 762a2e00042Sdrh */ 763cce7d176Sdrh case TK_ID: { 7648141f61eSdrh if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){ 765cce7d176Sdrh return 1; 766ed6c8671Sdrh } 767cce7d176Sdrh break; 768cce7d176Sdrh } 769cce7d176Sdrh 770d24cc427Sdrh /* A table name and column name: ID.ID 771d24cc427Sdrh ** Or a database, table and column: ID.ID.ID 772d24cc427Sdrh */ 773cce7d176Sdrh case TK_DOT: { 7748141f61eSdrh Token *pColumn; 7758141f61eSdrh Token *pTable; 7768141f61eSdrh Token *pDb; 7778141f61eSdrh Expr *pRight; 778cce7d176Sdrh 779cce7d176Sdrh pRight = pExpr->pRight; 780d24cc427Sdrh if( pRight->op==TK_ID ){ 7818141f61eSdrh pDb = 0; 7828141f61eSdrh pTable = &pExpr->pLeft->token; 7838141f61eSdrh pColumn = &pRight->token; 784d24cc427Sdrh }else{ 7858141f61eSdrh assert( pRight->op==TK_DOT ); 7868141f61eSdrh pDb = &pExpr->pLeft->token; 7878141f61eSdrh pTable = &pRight->pLeft->token; 7888141f61eSdrh pColumn = &pRight->pRight->token; 789d24cc427Sdrh } 7908141f61eSdrh if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){ 791daffd0e5Sdrh return 1; 792daffd0e5Sdrh } 793cce7d176Sdrh break; 794cce7d176Sdrh } 795cce7d176Sdrh 796fef5208cSdrh case TK_IN: { 797e014a838Sdanielk1977 char affinity; 7984adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 799d3d39e93Sdrh KeyInfo keyInfo; 800d3d39e93Sdrh 801fef5208cSdrh if( v==0 ) return 1; 8024adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 803cfab11bcSdrh return 1; 804cfab11bcSdrh } 805bf3b721fSdanielk1977 affinity = sqlite3ExprAffinity(pExpr->pLeft); 806e014a838Sdanielk1977 807e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 808e014a838Sdanielk1977 ** expression it is handled the same way. A temporary table is 809e014a838Sdanielk1977 ** filled with single-field index keys representing the results 810e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 811fef5208cSdrh ** 812e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 813e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 814e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 815e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 816e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 817e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 818e014a838Sdanielk1977 ** is used. 819fef5208cSdrh */ 820832508b7Sdrh pExpr->iTable = pParse->nTab++; 821d3d39e93Sdrh memset(&keyInfo, 0, sizeof(keyInfo)); 822d3d39e93Sdrh keyInfo.nField = 1; 823d3d39e93Sdrh keyInfo.aColl[0] = pParse->db->pDfltColl; 824d3d39e93Sdrh sqlite3VdbeOp3(v, OP_OpenTemp, pExpr->iTable, 0, \ 825d3d39e93Sdrh (char*)&keyInfo, P3_KEYINFO); 826e014a838Sdanielk1977 827e014a838Sdanielk1977 if( pExpr->pSelect ){ 828e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 829e014a838Sdanielk1977 ** 830e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 831e014a838Sdanielk1977 ** table allocated and opened above. 832e014a838Sdanielk1977 */ 833e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 834e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 835bf3b721fSdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 836fef5208cSdrh }else if( pExpr->pList ){ 837fef5208cSdrh /* Case 2: expr IN (exprlist) 838fef5208cSdrh ** 839e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 840e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 841e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 842e014a838Sdanielk1977 ** a column, use numeric affinity. 843fef5208cSdrh */ 844e014a838Sdanielk1977 int i; 845e014a838Sdanielk1977 char const *affStr; 846e014a838Sdanielk1977 if( !affinity ){ 847e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 848e014a838Sdanielk1977 } 849e014a838Sdanielk1977 affStr = sqlite3AffinityString(affinity); 850e014a838Sdanielk1977 851e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 852fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 853fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 854e014a838Sdanielk1977 855e014a838Sdanielk1977 /* Check that the expression is constant and valid. */ 8564adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 8574adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 858da93d238Sdrh "right-hand side of IN operator must be constant"); 859fef5208cSdrh return 1; 860fef5208cSdrh } 8614adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){ 8624794b980Sdrh return 1; 8634794b980Sdrh } 864e014a838Sdanielk1977 865e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 8664adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 867e014a838Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); 868e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 869e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0); 870fef5208cSdrh } 871fef5208cSdrh } 872cfab11bcSdrh break; 873fef5208cSdrh } 874fef5208cSdrh 87519a775c2Sdrh case TK_SELECT: { 876fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 877fef5208cSdrh ** value of this select in a memory cell and record the number 878967e8b73Sdrh ** of the memory cell in iColumn. 879fef5208cSdrh */ 880967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 881bf3b721fSdanielk1977 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){ 88219a775c2Sdrh return 1; 88319a775c2Sdrh } 88419a775c2Sdrh break; 88519a775c2Sdrh } 88619a775c2Sdrh 887cce7d176Sdrh /* For all else, just recursively walk the tree */ 888cce7d176Sdrh default: { 889cce7d176Sdrh if( pExpr->pLeft 8904adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 891cce7d176Sdrh return 1; 892cce7d176Sdrh } 893cce7d176Sdrh if( pExpr->pRight 8944adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ 895cce7d176Sdrh return 1; 896cce7d176Sdrh } 897cce7d176Sdrh if( pExpr->pList ){ 898cce7d176Sdrh int i; 899cce7d176Sdrh ExprList *pList = pExpr->pList; 900cce7d176Sdrh for(i=0; i<pList->nExpr; i++){ 901832508b7Sdrh Expr *pArg = pList->a[i].pExpr; 9024adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){ 903cce7d176Sdrh return 1; 904cce7d176Sdrh } 905cce7d176Sdrh } 906cce7d176Sdrh } 907cce7d176Sdrh } 908cce7d176Sdrh } 909cce7d176Sdrh return 0; 910cce7d176Sdrh } 911cce7d176Sdrh 912cce7d176Sdrh /* 9134b59ab5eSdrh ** pExpr is a node that defines a function of some kind. It might 9144b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function 9154b59ab5eSdrh ** that implements an operator, like "a LIKE b". 9164b59ab5eSdrh ** 9174b59ab5eSdrh ** This routine makes *pzName point to the name of the function and 9184b59ab5eSdrh ** *pnName hold the number of characters in the function name. 9194b59ab5eSdrh */ 9204b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 9214b59ab5eSdrh switch( pExpr->op ){ 9224b59ab5eSdrh case TK_FUNCTION: { 9234b59ab5eSdrh *pzName = pExpr->token.z; 9246977fea8Sdrh *pnName = pExpr->token.n; 9254b59ab5eSdrh break; 9264b59ab5eSdrh } 9274b59ab5eSdrh case TK_LIKE: { 9284b59ab5eSdrh *pzName = "like"; 9294b59ab5eSdrh *pnName = 4; 9304b59ab5eSdrh break; 9314b59ab5eSdrh } 9324b59ab5eSdrh case TK_GLOB: { 9334b59ab5eSdrh *pzName = "glob"; 9344b59ab5eSdrh *pnName = 4; 9354b59ab5eSdrh break; 9364b59ab5eSdrh } 9374b59ab5eSdrh default: { 9384b59ab5eSdrh *pzName = "can't happen"; 9394b59ab5eSdrh *pnName = 12; 9404b59ab5eSdrh break; 9414b59ab5eSdrh } 9424b59ab5eSdrh } 9434b59ab5eSdrh } 9444b59ab5eSdrh 9454b59ab5eSdrh /* 946cce7d176Sdrh ** Error check the functions in an expression. Make sure all 947cce7d176Sdrh ** function names are recognized and all functions have the correct 948cce7d176Sdrh ** number of arguments. Leave an error message in pParse->zErrMsg 949cce7d176Sdrh ** if anything is amiss. Return the number of errors. 950cce7d176Sdrh ** 951cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function 952cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 953cce7d176Sdrh */ 9544adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 955cce7d176Sdrh int nErr = 0; 956cce7d176Sdrh if( pExpr==0 ) return 0; 957cce7d176Sdrh switch( pExpr->op ){ 9584b59ab5eSdrh case TK_GLOB: 9594b59ab5eSdrh case TK_LIKE: 960cce7d176Sdrh case TK_FUNCTION: { 961c9b84a1fSdrh int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 962c9b84a1fSdrh int no_such_func = 0; /* True if no such function exists */ 963c9b84a1fSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 964c9b84a1fSdrh int is_agg = 0; /* True if is an aggregate function */ 965cce7d176Sdrh int i; 9664b59ab5eSdrh int nId; /* Number of characters in function name */ 9674b59ab5eSdrh const char *zId; /* The function name. */ 9680bce8354Sdrh FuncDef *pDef; 9690bce8354Sdrh 9704b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 9714adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0); 9720bce8354Sdrh if( pDef==0 ){ 9734adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0); 9740bce8354Sdrh if( pDef==0 ){ 975cce7d176Sdrh no_such_func = 1; 9768e0a2f90Sdrh }else{ 9778e0a2f90Sdrh wrong_num_args = 1; 9788e0a2f90Sdrh } 9798e0a2f90Sdrh }else{ 9800bce8354Sdrh is_agg = pDef->xFunc==0; 981cce7d176Sdrh } 9828e0a2f90Sdrh if( is_agg && !allowAgg ){ 9834adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); 9848e0a2f90Sdrh nErr++; 9858e0a2f90Sdrh is_agg = 0; 9868e0a2f90Sdrh }else if( no_such_func ){ 9874adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 988cce7d176Sdrh nErr++; 9898e0a2f90Sdrh }else if( wrong_num_args ){ 9904adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 991f7a9e1acSdrh nId, zId); 9928e0a2f90Sdrh nErr++; 993cce7d176Sdrh } 994f7a9e1acSdrh if( is_agg ){ 995f7a9e1acSdrh pExpr->op = TK_AGG_FUNCTION; 996f7a9e1acSdrh if( pIsAgg ) *pIsAgg = 1; 997f7a9e1acSdrh } 998cce7d176Sdrh for(i=0; nErr==0 && i<n; i++){ 9994adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr, 10004cfa7934Sdrh allowAgg && !is_agg, pIsAgg); 1001cce7d176Sdrh } 1002d3d39e93Sdrh /** TODO: Compute pExpr->affinity based on the expected return 1003d3d39e93Sdrh ** type of the function */ 1004cce7d176Sdrh } 1005cce7d176Sdrh default: { 1006cce7d176Sdrh if( pExpr->pLeft ){ 10074adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 1008cce7d176Sdrh } 1009cce7d176Sdrh if( nErr==0 && pExpr->pRight ){ 10104adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 1011cce7d176Sdrh } 1012fef5208cSdrh if( nErr==0 && pExpr->pList ){ 1013fef5208cSdrh int n = pExpr->pList->nExpr; 1014fef5208cSdrh int i; 1015fef5208cSdrh for(i=0; nErr==0 && i<n; i++){ 10162282792aSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 10174adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg); 1018fef5208cSdrh } 1019fef5208cSdrh } 1020cce7d176Sdrh break; 1021cce7d176Sdrh } 1022cce7d176Sdrh } 1023cce7d176Sdrh return nErr; 1024cce7d176Sdrh } 1025cce7d176Sdrh 1026cce7d176Sdrh /* 1027d3d39e93Sdrh ** Return one of the SQLITE_AFF_* affinity types that indicates the likely 1028d3d39e93Sdrh ** data type of the result of the given expression. 1029d3d39e93Sdrh ** 1030d3d39e93Sdrh ** Not every expression has a fixed type. If the type cannot be determined 1031d3d39e93Sdrh ** at compile-time, then try to return the type affinity if the expression 1032d3d39e93Sdrh ** is a column. Otherwise just return SQLITE_AFF_NONE. 1033c9b84a1fSdrh ** 10344adee20fSdanielk1977 ** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have 1035c9b84a1fSdrh ** both been called on the expression before it is passed to this routine. 1036c9b84a1fSdrh */ 10374adee20fSdanielk1977 int sqlite3ExprType(Expr *p){ 1038d3d39e93Sdrh if( p==0 ) return SQLITE_AFF_NONE; 1039c9b84a1fSdrh while( p ) switch( p->op ){ 1040c9b84a1fSdrh case TK_CONCAT: 1041736c22b8Sdrh case TK_STRING: 1042d3d39e93Sdrh return SQLITE_AFF_TEXT; 1043c9b84a1fSdrh 1044c9b84a1fSdrh case TK_AS: 1045c9b84a1fSdrh p = p->pLeft; 1046c9b84a1fSdrh break; 1047c9b84a1fSdrh 1048736c22b8Sdrh case TK_VARIABLE: 1049d3d39e93Sdrh case TK_NULL: 1050d3d39e93Sdrh return SQLITE_AFF_NONE; 1051c9b84a1fSdrh 1052d3d39e93Sdrh case TK_SELECT: /*** FIX ME ****/ 1053d3d39e93Sdrh case TK_COLUMN: /*** FIX ME ****/ 1054d3d39e93Sdrh case TK_CASE: /*** FIX ME ****/ 1055b1363206Sdrh 1056c9b84a1fSdrh default: 1057d3d39e93Sdrh return SQLITE_AFF_NUMERIC; 1058c9b84a1fSdrh } 1059d3d39e93Sdrh return SQLITE_AFF_NONE; 1060c9b84a1fSdrh } 1061c9b84a1fSdrh 1062c9b84a1fSdrh /* 1063fec19aadSdrh ** Generate an instruction that will put the integer describe by 1064fec19aadSdrh ** text z[0..n-1] on the stack. 1065fec19aadSdrh */ 1066fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1067fec19aadSdrh int i; 1068fec19aadSdrh if( sqlite3GetInt32(z, &i) || (i=0, sqlite3FitsIn64Bits(z))!=0 ){ 1069fec19aadSdrh sqlite3VdbeOp3(v, OP_Integer, i, 0, z, n); 1070fec19aadSdrh }else{ 1071fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1072fec19aadSdrh } 1073fec19aadSdrh } 1074fec19aadSdrh 1075fec19aadSdrh /* 1076cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 10771ccde15dSdrh ** expression and leave the result on the top of stack. 1078cce7d176Sdrh */ 10794adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1080cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1081cce7d176Sdrh int op; 1082daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1083cce7d176Sdrh switch( pExpr->op ){ 1084cce7d176Sdrh case TK_PLUS: op = OP_Add; break; 1085cce7d176Sdrh case TK_MINUS: op = OP_Subtract; break; 1086cce7d176Sdrh case TK_STAR: op = OP_Multiply; break; 1087cce7d176Sdrh case TK_SLASH: op = OP_Divide; break; 1088cce7d176Sdrh case TK_AND: op = OP_And; break; 1089cce7d176Sdrh case TK_OR: op = OP_Or; break; 1090cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1091cce7d176Sdrh case TK_LE: op = OP_Le; break; 1092cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1093cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1094cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1095cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1096cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1097cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1098cce7d176Sdrh case TK_NOT: op = OP_Not; break; 1099cce7d176Sdrh case TK_UMINUS: op = OP_Negative; break; 1100bf4133cbSdrh case TK_BITAND: op = OP_BitAnd; break; 1101bf4133cbSdrh case TK_BITOR: op = OP_BitOr; break; 1102bf4133cbSdrh case TK_BITNOT: op = OP_BitNot; break; 1103bf4133cbSdrh case TK_LSHIFT: op = OP_ShiftLeft; break; 1104bf4133cbSdrh case TK_RSHIFT: op = OP_ShiftRight; break; 1105bf4133cbSdrh case TK_REM: op = OP_Remainder; break; 1106fec19aadSdrh case TK_FLOAT: op = OP_Real; break; 1107fec19aadSdrh case TK_STRING: op = OP_String; break; 1108cce7d176Sdrh default: break; 1109cce7d176Sdrh } 1110cce7d176Sdrh switch( pExpr->op ){ 1111967e8b73Sdrh case TK_COLUMN: { 11122282792aSdrh if( pParse->useAgg ){ 11134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1114c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 11154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1116c4a3c779Sdrh }else{ 11174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 11182282792aSdrh } 1119cce7d176Sdrh break; 1120cce7d176Sdrh } 1121cce7d176Sdrh case TK_INTEGER: { 1122fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1123fec19aadSdrh break; 112451e9a445Sdrh } 1125fec19aadSdrh case TK_FLOAT: 1126fec19aadSdrh case TK_STRING: { 1127fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 11284adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1129cce7d176Sdrh break; 1130cce7d176Sdrh } 1131cce7d176Sdrh case TK_NULL: { 11324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 1133cce7d176Sdrh break; 1134cce7d176Sdrh } 113550457896Sdrh case TK_VARIABLE: { 11364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 113750457896Sdrh break; 113850457896Sdrh } 1139c9b84a1fSdrh case TK_LT: 1140c9b84a1fSdrh case TK_LE: 1141c9b84a1fSdrh case TK_GT: 1142c9b84a1fSdrh case TK_GE: 1143c9b84a1fSdrh case TK_NE: 1144c9b84a1fSdrh case TK_EQ: { 1145a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0); 1146a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1147a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1148a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, 0); 1149a37cdde0Sdanielk1977 break; 1150c9b84a1fSdrh } 1151cce7d176Sdrh case TK_AND: 1152cce7d176Sdrh case TK_OR: 1153cce7d176Sdrh case TK_PLUS: 1154cce7d176Sdrh case TK_STAR: 1155cce7d176Sdrh case TK_MINUS: 1156bf4133cbSdrh case TK_REM: 1157bf4133cbSdrh case TK_BITAND: 1158bf4133cbSdrh case TK_BITOR: 1159c9b84a1fSdrh case TK_SLASH: { 11604adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11614adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11624adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1163cce7d176Sdrh break; 1164cce7d176Sdrh } 1165bf4133cbSdrh case TK_LSHIFT: 1166bf4133cbSdrh case TK_RSHIFT: { 11674adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11684adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11694adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1170bf4133cbSdrh break; 1171bf4133cbSdrh } 11720040077dSdrh case TK_CONCAT: { 11734adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11744adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Concat, 2, 0); 11760040077dSdrh break; 11770040077dSdrh } 1178cce7d176Sdrh case TK_UMINUS: { 1179fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1180fec19aadSdrh assert( pLeft ); 1181fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1182fec19aadSdrh Token *p = &pLeft->token; 11836e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 11846e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1185fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1186fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1187e6840900Sdrh }else{ 1188fec19aadSdrh codeInteger(v, z, p->n+1); 1189e6840900Sdrh } 11906e142f54Sdrh sqliteFree(z); 11916e142f54Sdrh break; 11926e142f54Sdrh } 11931ccde15dSdrh /* Fall through into TK_NOT */ 11946e142f54Sdrh } 1195bf4133cbSdrh case TK_BITNOT: 11966e142f54Sdrh case TK_NOT: { 11974adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11984adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1199cce7d176Sdrh break; 1200cce7d176Sdrh } 1201cce7d176Sdrh case TK_ISNULL: 1202cce7d176Sdrh case TK_NOTNULL: { 1203cce7d176Sdrh int dest; 12044adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 12054adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12064adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 12074adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 12084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1209cce7d176Sdrh } 1210a37cdde0Sdanielk1977 break; 12112282792aSdrh case TK_AGG_FUNCTION: { 12124adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 12132282792aSdrh break; 12142282792aSdrh } 12154b59ab5eSdrh case TK_GLOB: 12164b59ab5eSdrh case TK_LIKE: 1217cce7d176Sdrh case TK_FUNCTION: { 1218cce7d176Sdrh ExprList *pList = pExpr->pList; 121989425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 12200bce8354Sdrh FuncDef *pDef; 12214b59ab5eSdrh int nId; 12224b59ab5eSdrh const char *zId; 12234b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 12244adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0); 12250bce8354Sdrh assert( pDef!=0 ); 1226*f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1227a37cdde0Sdanielk1977 /* FIX ME: The following is a temporary hack. */ 1228a37cdde0Sdanielk1977 if( 0==sqlite3StrNICmp(zId, "classof", nId) ){ 1229a37cdde0Sdanielk1977 assert( nExpr==1 ); 1230fec19aadSdrh sqlite3VdbeAddOp(v, OP_Class, nExpr, 0); 1231a37cdde0Sdanielk1977 }else{ 1232*f9b596ebSdrh sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_FUNCDEF); 1233a37cdde0Sdanielk1977 } 12346ec2733bSdrh break; 12356ec2733bSdrh } 123619a775c2Sdrh case TK_SELECT: { 12374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 123819a775c2Sdrh break; 123919a775c2Sdrh } 1240fef5208cSdrh case TK_IN: { 1241fef5208cSdrh int addr; 1242e014a838Sdanielk1977 char const *affStr; 1243e014a838Sdanielk1977 1244e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1245e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1246e014a838Sdanielk1977 ** P3 of OP_MakeKey. 1247e014a838Sdanielk1977 */ 1248e014a838Sdanielk1977 affStr = sqlite3AffinityString(comparisonAffinity(pExpr)); 1249e014a838Sdanielk1977 12504adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1251e014a838Sdanielk1977 1252e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1253e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1254e014a838Sdanielk1977 */ 12554adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12564adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1257e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 12584adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 12594adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 1260e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 1261e014a838Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */ 1262e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1263e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1264e014a838Sdanielk1977 1265fef5208cSdrh break; 1266fef5208cSdrh } 1267fef5208cSdrh case TK_BETWEEN: { 12684adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12694adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 12704adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 12714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, 0, 0); 12724adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 12734adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 12744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, 0, 0); 12754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1276fef5208cSdrh break; 1277fef5208cSdrh } 127851e9a445Sdrh case TK_UPLUS: 1279a2e00042Sdrh case TK_AS: { 12804adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1281a2e00042Sdrh break; 1282a2e00042Sdrh } 128317a7f8ddSdrh case TK_CASE: { 128417a7f8ddSdrh int expr_end_label; 1285f5905aa7Sdrh int jumpInst; 1286f5905aa7Sdrh int addr; 1287f5905aa7Sdrh int nExpr; 128817a7f8ddSdrh int i; 128917a7f8ddSdrh 129017a7f8ddSdrh assert(pExpr->pList); 129117a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 129217a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1293f5905aa7Sdrh nExpr = pExpr->pList->nExpr; 12944adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 129517a7f8ddSdrh if( pExpr->pLeft ){ 12964adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1297cce7d176Sdrh } 1298f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 12994adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr); 130017a7f8ddSdrh if( pExpr->pLeft ){ 13014adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 13024adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0); 13034adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1304f5905aa7Sdrh }else{ 13054adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 130617a7f8ddSdrh } 13074adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr); 13084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 13094adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 13104adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 131117a7f8ddSdrh } 1312f570f011Sdrh if( pExpr->pLeft ){ 13134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1314f570f011Sdrh } 131517a7f8ddSdrh if( pExpr->pRight ){ 13164adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 131717a7f8ddSdrh }else{ 13184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 131917a7f8ddSdrh } 13204adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 13216f34903eSdanielk1977 break; 13226f34903eSdanielk1977 } 13236f34903eSdanielk1977 case TK_RAISE: { 13246f34903eSdanielk1977 if( !pParse->trigStack ){ 13254adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1326da93d238Sdrh "RAISE() may only be used within a trigger-program"); 13276f34903eSdanielk1977 pParse->nErr++; 13286f34903eSdanielk1977 return; 13296f34903eSdanielk1977 } 13306f34903eSdanielk1977 if( pExpr->iColumn == OE_Rollback || 13316f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 13326f34903eSdanielk1977 pExpr->iColumn == OE_Fail ){ 13334adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1334701a0aebSdrh pExpr->token.z, pExpr->token.n); 13354adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 13366f34903eSdanielk1977 } else { 13376f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 13384adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump, 1339701a0aebSdrh "(IGNORE jump)", 0); 13406f34903eSdanielk1977 } 134117a7f8ddSdrh } 134217a7f8ddSdrh break; 134317a7f8ddSdrh } 1344cce7d176Sdrh } 1345cce7d176Sdrh 1346cce7d176Sdrh /* 1347268380caSdrh ** Generate code that pushes the value of every element of the given 1348*f9b596ebSdrh ** expression list onto the stack. 1349268380caSdrh ** 1350268380caSdrh ** Return the number of elements pushed onto the stack. 1351268380caSdrh */ 13524adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1353268380caSdrh Parse *pParse, /* Parsing context */ 1354*f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1355268380caSdrh ){ 1356268380caSdrh struct ExprList_item *pItem; 1357268380caSdrh int i, n; 1358268380caSdrh Vdbe *v; 1359268380caSdrh if( pList==0 ) return 0; 13604adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1361268380caSdrh n = pList->nExpr; 1362268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 13634adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1364268380caSdrh } 1365*f9b596ebSdrh return n; 1366268380caSdrh } 1367268380caSdrh 1368268380caSdrh /* 1369cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1370cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1371cce7d176Sdrh ** continues straight thru if the expression is false. 1372f5905aa7Sdrh ** 1373f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1374f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1375cce7d176Sdrh */ 13764adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1377cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1378cce7d176Sdrh int op = 0; 1379daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1380cce7d176Sdrh switch( pExpr->op ){ 1381cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1382cce7d176Sdrh case TK_LE: op = OP_Le; break; 1383cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1384cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1385cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1386cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1387cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1388cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1389cce7d176Sdrh default: break; 1390cce7d176Sdrh } 1391cce7d176Sdrh switch( pExpr->op ){ 1392cce7d176Sdrh case TK_AND: { 13934adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 13944adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 13954adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 13964adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1397cce7d176Sdrh break; 1398cce7d176Sdrh } 1399cce7d176Sdrh case TK_OR: { 14004adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 14014adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1402cce7d176Sdrh break; 1403cce7d176Sdrh } 1404cce7d176Sdrh case TK_NOT: { 14054adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1406cce7d176Sdrh break; 1407cce7d176Sdrh } 1408cce7d176Sdrh case TK_LT: 1409cce7d176Sdrh case TK_LE: 1410cce7d176Sdrh case TK_GT: 1411cce7d176Sdrh case TK_GE: 1412cce7d176Sdrh case TK_NE: 14130ac65892Sdrh case TK_EQ: { 1414a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 14154adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14164adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1417a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1418cce7d176Sdrh break; 1419cce7d176Sdrh } 1420cce7d176Sdrh case TK_ISNULL: 1421cce7d176Sdrh case TK_NOTNULL: { 14224adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14234adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1424cce7d176Sdrh break; 1425cce7d176Sdrh } 1426fef5208cSdrh case TK_BETWEEN: { 1427f5905aa7Sdrh int addr; 14284adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 14304adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 14314adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0); 14324adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 14334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest); 14344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 14354adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 14364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1437fef5208cSdrh break; 1438fef5208cSdrh } 1439cce7d176Sdrh default: { 14404adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 14414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1442cce7d176Sdrh break; 1443cce7d176Sdrh } 1444cce7d176Sdrh } 1445cce7d176Sdrh } 1446cce7d176Sdrh 1447cce7d176Sdrh /* 144866b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1449cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1450cce7d176Sdrh ** continues straight thru if the expression is true. 1451f5905aa7Sdrh ** 1452f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1453f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1454cce7d176Sdrh */ 14554adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1456cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1457cce7d176Sdrh int op = 0; 1458daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1459cce7d176Sdrh switch( pExpr->op ){ 1460cce7d176Sdrh case TK_LT: op = OP_Ge; break; 1461cce7d176Sdrh case TK_LE: op = OP_Gt; break; 1462cce7d176Sdrh case TK_GT: op = OP_Le; break; 1463cce7d176Sdrh case TK_GE: op = OP_Lt; break; 1464cce7d176Sdrh case TK_NE: op = OP_Eq; break; 1465cce7d176Sdrh case TK_EQ: op = OP_Ne; break; 1466cce7d176Sdrh case TK_ISNULL: op = OP_NotNull; break; 1467cce7d176Sdrh case TK_NOTNULL: op = OP_IsNull; break; 1468cce7d176Sdrh default: break; 1469cce7d176Sdrh } 1470cce7d176Sdrh switch( pExpr->op ){ 1471cce7d176Sdrh case TK_AND: { 14724adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 14734adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1474cce7d176Sdrh break; 1475cce7d176Sdrh } 1476cce7d176Sdrh case TK_OR: { 14774adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 14784adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 14794adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 14804adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1481cce7d176Sdrh break; 1482cce7d176Sdrh } 1483cce7d176Sdrh case TK_NOT: { 14844adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1485cce7d176Sdrh break; 1486cce7d176Sdrh } 1487cce7d176Sdrh case TK_LT: 1488cce7d176Sdrh case TK_LE: 1489cce7d176Sdrh case TK_GT: 1490cce7d176Sdrh case TK_GE: 1491cce7d176Sdrh case TK_NE: 1492cce7d176Sdrh case TK_EQ: { 1493a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 14944adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14954adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1496a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1497cce7d176Sdrh break; 1498cce7d176Sdrh } 1499cce7d176Sdrh case TK_ISNULL: 1500cce7d176Sdrh case TK_NOTNULL: { 15014adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15024adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1503cce7d176Sdrh break; 1504cce7d176Sdrh } 1505e014a838Sdanielk1977 #if 0 1506fef5208cSdrh case TK_IN: { 1507f5905aa7Sdrh int addr; 15084adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15094adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15104adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3); 15114adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15124adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1513fef5208cSdrh if( pExpr->pSelect ){ 15144adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); 1515fef5208cSdrh }else{ 15164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); 1517fef5208cSdrh } 1518fef5208cSdrh break; 1519fef5208cSdrh } 1520e014a838Sdanielk1977 #endif 1521fef5208cSdrh case TK_BETWEEN: { 1522fef5208cSdrh int addr; 15234adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 15254adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 15264adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); 15284adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15294adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 15304adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 15314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest); 1532fef5208cSdrh break; 1533fef5208cSdrh } 1534cce7d176Sdrh default: { 15354adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 15364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1537cce7d176Sdrh break; 1538cce7d176Sdrh } 1539cce7d176Sdrh } 1540cce7d176Sdrh } 15412282792aSdrh 15422282792aSdrh /* 15432282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 15442282792aSdrh ** if they are identical and return FALSE if they differ in any way. 15452282792aSdrh */ 15464adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 15472282792aSdrh int i; 15482282792aSdrh if( pA==0 ){ 15492282792aSdrh return pB==0; 15502282792aSdrh }else if( pB==0 ){ 15512282792aSdrh return 0; 15522282792aSdrh } 15532282792aSdrh if( pA->op!=pB->op ) return 0; 15544adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 15554adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 15562282792aSdrh if( pA->pList ){ 15572282792aSdrh if( pB->pList==0 ) return 0; 15582282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 15592282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 15604adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 15612282792aSdrh return 0; 15622282792aSdrh } 15632282792aSdrh } 15642282792aSdrh }else if( pB->pList ){ 15652282792aSdrh return 0; 15662282792aSdrh } 15672282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 15682f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 15692282792aSdrh if( pA->token.z ){ 15702282792aSdrh if( pB->token.z==0 ) return 0; 15716977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 15724adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 15732282792aSdrh } 15742282792aSdrh return 1; 15752282792aSdrh } 15762282792aSdrh 15772282792aSdrh /* 15782282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 15792282792aSdrh */ 15802282792aSdrh static int appendAggInfo(Parse *pParse){ 15812282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 15822282792aSdrh int amt = pParse->nAgg + 8; 15836d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 15846d4abfbeSdrh if( aAgg==0 ){ 15852282792aSdrh return -1; 15862282792aSdrh } 15876d4abfbeSdrh pParse->aAgg = aAgg; 15882282792aSdrh } 15892282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 15902282792aSdrh return pParse->nAgg++; 15912282792aSdrh } 15922282792aSdrh 15932282792aSdrh /* 15942282792aSdrh ** Analyze the given expression looking for aggregate functions and 15952282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 15962282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 15972282792aSdrh ** 15982282792aSdrh ** This routine should only be called after the expression has been 15994adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck(). 16002282792aSdrh ** 16012282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return 16022282792aSdrh ** the number of errors. 16032282792aSdrh */ 16044adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 16052282792aSdrh int i; 16062282792aSdrh AggExpr *aAgg; 16072282792aSdrh int nErr = 0; 16082282792aSdrh 16092282792aSdrh if( pExpr==0 ) return 0; 16102282792aSdrh switch( pExpr->op ){ 1611967e8b73Sdrh case TK_COLUMN: { 16122282792aSdrh aAgg = pParse->aAgg; 16132282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16142282792aSdrh if( aAgg[i].isAgg ) continue; 16152282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 1616967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 16172282792aSdrh break; 16182282792aSdrh } 16192282792aSdrh } 16202282792aSdrh if( i>=pParse->nAgg ){ 16212282792aSdrh i = appendAggInfo(pParse); 16222282792aSdrh if( i<0 ) return 1; 16232282792aSdrh pParse->aAgg[i].isAgg = 0; 16242282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16252282792aSdrh } 1626aaf88729Sdrh pExpr->iAgg = i; 16272282792aSdrh break; 16282282792aSdrh } 16292282792aSdrh case TK_AGG_FUNCTION: { 16302282792aSdrh aAgg = pParse->aAgg; 16312282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16322282792aSdrh if( !aAgg[i].isAgg ) continue; 16334adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 16342282792aSdrh break; 16352282792aSdrh } 16362282792aSdrh } 16372282792aSdrh if( i>=pParse->nAgg ){ 16382282792aSdrh i = appendAggInfo(pParse); 16392282792aSdrh if( i<0 ) return 1; 16402282792aSdrh pParse->aAgg[i].isAgg = 1; 16412282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16424adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 16436977fea8Sdrh pExpr->token.z, pExpr->token.n, 1644f55f25f0Sdrh pExpr->pList ? pExpr->pList->nExpr : 0, 0); 16452282792aSdrh } 16462282792aSdrh pExpr->iAgg = i; 16472282792aSdrh break; 16482282792aSdrh } 16492282792aSdrh default: { 16502282792aSdrh if( pExpr->pLeft ){ 16514adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft); 16522282792aSdrh } 16532282792aSdrh if( nErr==0 && pExpr->pRight ){ 16544adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight); 16552282792aSdrh } 16562282792aSdrh if( nErr==0 && pExpr->pList ){ 16572282792aSdrh int n = pExpr->pList->nExpr; 16582282792aSdrh int i; 16592282792aSdrh for(i=0; nErr==0 && i<n; i++){ 16604adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 16612282792aSdrh } 16622282792aSdrh } 16632282792aSdrh break; 16642282792aSdrh } 16652282792aSdrh } 16662282792aSdrh return nErr; 16672282792aSdrh } 16688e0a2f90Sdrh 16698e0a2f90Sdrh /* 16708e0a2f90Sdrh ** Locate a user function given a name and a number of arguments. 16710bce8354Sdrh ** Return a pointer to the FuncDef structure that defines that 16728e0a2f90Sdrh ** function, or return NULL if the function does not exist. 16738e0a2f90Sdrh ** 16740bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef 16758e0a2f90Sdrh ** structure is created and liked into the "db" structure if a 16768e0a2f90Sdrh ** no matching function previously existed. When createFlag is true 16778e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts 16788e0a2f90Sdrh ** any number of arguments will be returned. 16798e0a2f90Sdrh ** 16808e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid 16818e0a2f90Sdrh ** function found is returned. A function is valid if either xFunc 16828e0a2f90Sdrh ** or xStep is non-zero. 16838e0a2f90Sdrh */ 16844adee20fSdanielk1977 FuncDef *sqlite3FindFunction( 16858e0a2f90Sdrh sqlite *db, /* An open database */ 16868e0a2f90Sdrh const char *zName, /* Name of the function. Not null-terminated */ 16878e0a2f90Sdrh int nName, /* Number of characters in the name */ 16888e0a2f90Sdrh int nArg, /* Number of arguments. -1 means any number */ 16898e0a2f90Sdrh int createFlag /* Create new entry if true and does not otherwise exist */ 16908e0a2f90Sdrh ){ 16910bce8354Sdrh FuncDef *pFirst, *p, *pMaybe; 16924adee20fSdanielk1977 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); 16931350b030Sdrh if( p && !createFlag && nArg<0 ){ 16948e0a2f90Sdrh while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } 16958e0a2f90Sdrh return p; 16968e0a2f90Sdrh } 16978e0a2f90Sdrh pMaybe = 0; 16988e0a2f90Sdrh while( p && p->nArg!=nArg ){ 16998e0a2f90Sdrh if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; 17008e0a2f90Sdrh p = p->pNext; 17018e0a2f90Sdrh } 17028e0a2f90Sdrh if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ 17038e0a2f90Sdrh return 0; 17048e0a2f90Sdrh } 17058e0a2f90Sdrh if( p==0 && pMaybe ){ 17068e0a2f90Sdrh assert( createFlag==0 ); 17078e0a2f90Sdrh return pMaybe; 17088e0a2f90Sdrh } 1709*f9b596ebSdrh if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)+nName+1))!=0 ){ 17108e0a2f90Sdrh p->nArg = nArg; 17118e0a2f90Sdrh p->pNext = pFirst; 1712*f9b596ebSdrh p->zName = (char*)&p[1]; 1713*f9b596ebSdrh memcpy(p->zName, zName, nName); 1714*f9b596ebSdrh p->zName[nName] = 0; 1715*f9b596ebSdrh sqlite3HashInsert(&db->aFunc, p->zName, nName, (void*)p); 17168e0a2f90Sdrh } 17178e0a2f90Sdrh return p; 17188e0a2f90Sdrh } 1719