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*f3218feaSdrh ** $Id: expr.c,v 1.131 2004/05/28 08:21:06 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: 426c572ef7fSdanielk1977 case TK_BLOB: 4279208643dSdrh case TK_INTEGER: 4289208643dSdrh case TK_FLOAT: 42950457896Sdrh case TK_VARIABLE: 4309208643dSdrh return 1; 431fef5208cSdrh default: { 4324adee20fSdanielk1977 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0; 4334adee20fSdanielk1977 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0; 434fef5208cSdrh if( p->pList ){ 435fef5208cSdrh int i; 436fef5208cSdrh for(i=0; i<p->pList->nExpr; i++){ 4374adee20fSdanielk1977 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0; 438fef5208cSdrh } 439fef5208cSdrh } 4409208643dSdrh return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); 441fef5208cSdrh } 442fef5208cSdrh } 4439208643dSdrh return 0; 444fef5208cSdrh } 445fef5208cSdrh 446fef5208cSdrh /* 447202b2df7Sdrh ** If the given expression codes a constant integer that is small enough 448202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 449202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 450202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 451e4de1febSdrh */ 4524adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 453e4de1febSdrh switch( p->op ){ 454e4de1febSdrh case TK_INTEGER: { 455fec19aadSdrh if( sqlite3GetInt32(p->token.z, pValue) ){ 456e4de1febSdrh return 1; 457e4de1febSdrh } 458202b2df7Sdrh break; 459202b2df7Sdrh } 460e4de1febSdrh case TK_STRING: { 461bd790ee3Sdrh const char *z = p->token.z; 462e4de1febSdrh int n = p->token.n; 463bd790ee3Sdrh if( n>0 && z[0]=='-' ){ z++; n--; } 464e4de1febSdrh while( n>0 && *z && isdigit(*z) ){ z++; n--; } 465fec19aadSdrh if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){ 466e4de1febSdrh return 1; 467e4de1febSdrh } 468e4de1febSdrh break; 469e4de1febSdrh } 4704b59ab5eSdrh case TK_UPLUS: { 4714adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 4724b59ab5eSdrh } 473e4de1febSdrh case TK_UMINUS: { 474e4de1febSdrh int v; 4754adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 476e4de1febSdrh *pValue = -v; 477e4de1febSdrh return 1; 478e4de1febSdrh } 479e4de1febSdrh break; 480e4de1febSdrh } 481e4de1febSdrh default: break; 482e4de1febSdrh } 483e4de1febSdrh return 0; 484e4de1febSdrh } 485e4de1febSdrh 486e4de1febSdrh /* 487c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 488c4a3c779Sdrh */ 4894adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 4904adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 4914adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 4924adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 493c4a3c779Sdrh return 0; 494c4a3c779Sdrh } 495c4a3c779Sdrh 496c4a3c779Sdrh /* 4978141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 4988141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 4998141f61eSdrh ** expression node refer back to that source column. The following changes 5008141f61eSdrh ** are made to pExpr: 5018141f61eSdrh ** 5028141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 5038141f61eSdrh ** the table. 5048141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 5058141f61eSdrh ** from pSrcList. 5068141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 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); 827*f3218feaSdrh sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); 828e014a838Sdanielk1977 829e014a838Sdanielk1977 if( pExpr->pSelect ){ 830e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 831e014a838Sdanielk1977 ** 832e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 833e014a838Sdanielk1977 ** table allocated and opened above. 834e014a838Sdanielk1977 */ 835e014a838Sdanielk1977 int iParm = pExpr->iTable + (((int)affinity)<<16); 836e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 837bf3b721fSdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0); 838fef5208cSdrh }else if( pExpr->pList ){ 839fef5208cSdrh /* Case 2: expr IN (exprlist) 840fef5208cSdrh ** 841e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 842e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 843e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 844e014a838Sdanielk1977 ** a column, use numeric affinity. 845fef5208cSdrh */ 846e014a838Sdanielk1977 int i; 847e014a838Sdanielk1977 char const *affStr; 848e014a838Sdanielk1977 if( !affinity ){ 849e014a838Sdanielk1977 affinity = SQLITE_AFF_NUMERIC; 850e014a838Sdanielk1977 } 851e014a838Sdanielk1977 affStr = sqlite3AffinityString(affinity); 852e014a838Sdanielk1977 853e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 854fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 855fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 856e014a838Sdanielk1977 857e014a838Sdanielk1977 /* Check that the expression is constant and valid. */ 8584adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 8594adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 860da93d238Sdrh "right-hand side of IN operator must be constant"); 861fef5208cSdrh return 1; 862fef5208cSdrh } 8634adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){ 8644794b980Sdrh return 1; 8654794b980Sdrh } 866e014a838Sdanielk1977 867e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 8684adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 869e014a838Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); 870e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 871e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0); 872fef5208cSdrh } 873fef5208cSdrh } 874cfab11bcSdrh break; 875fef5208cSdrh } 876fef5208cSdrh 87719a775c2Sdrh case TK_SELECT: { 878fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 879fef5208cSdrh ** value of this select in a memory cell and record the number 880967e8b73Sdrh ** of the memory cell in iColumn. 881fef5208cSdrh */ 882967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 883bf3b721fSdanielk1977 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){ 88419a775c2Sdrh return 1; 88519a775c2Sdrh } 88619a775c2Sdrh break; 88719a775c2Sdrh } 88819a775c2Sdrh 889cce7d176Sdrh /* For all else, just recursively walk the tree */ 890cce7d176Sdrh default: { 891cce7d176Sdrh if( pExpr->pLeft 8924adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 893cce7d176Sdrh return 1; 894cce7d176Sdrh } 895cce7d176Sdrh if( pExpr->pRight 8964adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ 897cce7d176Sdrh return 1; 898cce7d176Sdrh } 899cce7d176Sdrh if( pExpr->pList ){ 900cce7d176Sdrh int i; 901cce7d176Sdrh ExprList *pList = pExpr->pList; 902cce7d176Sdrh for(i=0; i<pList->nExpr; i++){ 903832508b7Sdrh Expr *pArg = pList->a[i].pExpr; 9044adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){ 905cce7d176Sdrh return 1; 906cce7d176Sdrh } 907cce7d176Sdrh } 908cce7d176Sdrh } 909cce7d176Sdrh } 910cce7d176Sdrh } 911cce7d176Sdrh return 0; 912cce7d176Sdrh } 913cce7d176Sdrh 914cce7d176Sdrh /* 9154b59ab5eSdrh ** pExpr is a node that defines a function of some kind. It might 9164b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function 9174b59ab5eSdrh ** that implements an operator, like "a LIKE b". 9184b59ab5eSdrh ** 9194b59ab5eSdrh ** This routine makes *pzName point to the name of the function and 9204b59ab5eSdrh ** *pnName hold the number of characters in the function name. 9214b59ab5eSdrh */ 9224b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 9234b59ab5eSdrh switch( pExpr->op ){ 9244b59ab5eSdrh case TK_FUNCTION: { 9254b59ab5eSdrh *pzName = pExpr->token.z; 9266977fea8Sdrh *pnName = pExpr->token.n; 9274b59ab5eSdrh break; 9284b59ab5eSdrh } 9294b59ab5eSdrh case TK_LIKE: { 9304b59ab5eSdrh *pzName = "like"; 9314b59ab5eSdrh *pnName = 4; 9324b59ab5eSdrh break; 9334b59ab5eSdrh } 9344b59ab5eSdrh case TK_GLOB: { 9354b59ab5eSdrh *pzName = "glob"; 9364b59ab5eSdrh *pnName = 4; 9374b59ab5eSdrh break; 9384b59ab5eSdrh } 9394b59ab5eSdrh default: { 9404b59ab5eSdrh *pzName = "can't happen"; 9414b59ab5eSdrh *pnName = 12; 9424b59ab5eSdrh break; 9434b59ab5eSdrh } 9444b59ab5eSdrh } 9454b59ab5eSdrh } 9464b59ab5eSdrh 9474b59ab5eSdrh /* 948cce7d176Sdrh ** Error check the functions in an expression. Make sure all 949cce7d176Sdrh ** function names are recognized and all functions have the correct 950cce7d176Sdrh ** number of arguments. Leave an error message in pParse->zErrMsg 951cce7d176Sdrh ** if anything is amiss. Return the number of errors. 952cce7d176Sdrh ** 953cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function 954cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 955cce7d176Sdrh */ 9564adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 957cce7d176Sdrh int nErr = 0; 958cce7d176Sdrh if( pExpr==0 ) return 0; 959cce7d176Sdrh switch( pExpr->op ){ 9604b59ab5eSdrh case TK_GLOB: 9614b59ab5eSdrh case TK_LIKE: 962cce7d176Sdrh case TK_FUNCTION: { 963c9b84a1fSdrh int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 964c9b84a1fSdrh int no_such_func = 0; /* True if no such function exists */ 965c9b84a1fSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 966c9b84a1fSdrh int is_agg = 0; /* True if is an aggregate function */ 967cce7d176Sdrh int i; 9684b59ab5eSdrh int nId; /* Number of characters in function name */ 9694b59ab5eSdrh const char *zId; /* The function name. */ 9700bce8354Sdrh FuncDef *pDef; 9710bce8354Sdrh 9724b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 9734adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0); 9740bce8354Sdrh if( pDef==0 ){ 9754adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0); 9760bce8354Sdrh if( pDef==0 ){ 977cce7d176Sdrh no_such_func = 1; 9788e0a2f90Sdrh }else{ 9798e0a2f90Sdrh wrong_num_args = 1; 9808e0a2f90Sdrh } 9818e0a2f90Sdrh }else{ 9820bce8354Sdrh is_agg = pDef->xFunc==0; 983cce7d176Sdrh } 9848e0a2f90Sdrh if( is_agg && !allowAgg ){ 9854adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); 9868e0a2f90Sdrh nErr++; 9878e0a2f90Sdrh is_agg = 0; 9888e0a2f90Sdrh }else if( no_such_func ){ 9894adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 990cce7d176Sdrh nErr++; 9918e0a2f90Sdrh }else if( wrong_num_args ){ 9924adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 993f7a9e1acSdrh nId, zId); 9948e0a2f90Sdrh nErr++; 995cce7d176Sdrh } 996f7a9e1acSdrh if( is_agg ){ 997f7a9e1acSdrh pExpr->op = TK_AGG_FUNCTION; 998f7a9e1acSdrh if( pIsAgg ) *pIsAgg = 1; 999f7a9e1acSdrh } 1000cce7d176Sdrh for(i=0; nErr==0 && i<n; i++){ 10014adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr, 10024cfa7934Sdrh allowAgg && !is_agg, pIsAgg); 1003cce7d176Sdrh } 1004d3d39e93Sdrh /** TODO: Compute pExpr->affinity based on the expected return 1005d3d39e93Sdrh ** type of the function */ 1006cce7d176Sdrh } 1007cce7d176Sdrh default: { 1008cce7d176Sdrh if( pExpr->pLeft ){ 10094adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 1010cce7d176Sdrh } 1011cce7d176Sdrh if( nErr==0 && pExpr->pRight ){ 10124adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 1013cce7d176Sdrh } 1014fef5208cSdrh if( nErr==0 && pExpr->pList ){ 1015fef5208cSdrh int n = pExpr->pList->nExpr; 1016fef5208cSdrh int i; 1017fef5208cSdrh for(i=0; nErr==0 && i<n; i++){ 10182282792aSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 10194adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg); 1020fef5208cSdrh } 1021fef5208cSdrh } 1022cce7d176Sdrh break; 1023cce7d176Sdrh } 1024cce7d176Sdrh } 1025cce7d176Sdrh return nErr; 1026cce7d176Sdrh } 1027cce7d176Sdrh 1028cce7d176Sdrh /* 1029d3d39e93Sdrh ** Return one of the SQLITE_AFF_* affinity types that indicates the likely 1030d3d39e93Sdrh ** data type of the result of the given expression. 1031d3d39e93Sdrh ** 1032d3d39e93Sdrh ** Not every expression has a fixed type. If the type cannot be determined 1033d3d39e93Sdrh ** at compile-time, then try to return the type affinity if the expression 1034d3d39e93Sdrh ** is a column. Otherwise just return SQLITE_AFF_NONE. 1035c9b84a1fSdrh ** 10364adee20fSdanielk1977 ** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have 1037c9b84a1fSdrh ** both been called on the expression before it is passed to this routine. 1038c9b84a1fSdrh */ 10394adee20fSdanielk1977 int sqlite3ExprType(Expr *p){ 1040d3d39e93Sdrh if( p==0 ) return SQLITE_AFF_NONE; 1041c9b84a1fSdrh while( p ) switch( p->op ){ 1042c9b84a1fSdrh case TK_CONCAT: 1043736c22b8Sdrh case TK_STRING: 1044c572ef7fSdanielk1977 case TK_BLOB: 1045d3d39e93Sdrh return SQLITE_AFF_TEXT; 1046c9b84a1fSdrh 1047c9b84a1fSdrh case TK_AS: 1048c9b84a1fSdrh p = p->pLeft; 1049c9b84a1fSdrh break; 1050c9b84a1fSdrh 1051736c22b8Sdrh case TK_VARIABLE: 1052d3d39e93Sdrh case TK_NULL: 1053d3d39e93Sdrh return SQLITE_AFF_NONE; 1054c9b84a1fSdrh 1055d3d39e93Sdrh case TK_SELECT: /*** FIX ME ****/ 1056d3d39e93Sdrh case TK_COLUMN: /*** FIX ME ****/ 1057d3d39e93Sdrh case TK_CASE: /*** FIX ME ****/ 1058b1363206Sdrh 1059c9b84a1fSdrh default: 1060d3d39e93Sdrh return SQLITE_AFF_NUMERIC; 1061c9b84a1fSdrh } 1062d3d39e93Sdrh return SQLITE_AFF_NONE; 1063c9b84a1fSdrh } 1064c9b84a1fSdrh 1065c9b84a1fSdrh /* 1066fec19aadSdrh ** Generate an instruction that will put the integer describe by 1067fec19aadSdrh ** text z[0..n-1] on the stack. 1068fec19aadSdrh */ 1069fec19aadSdrh static void codeInteger(Vdbe *v, const char *z, int n){ 1070fec19aadSdrh int i; 1071fec19aadSdrh if( sqlite3GetInt32(z, &i) || (i=0, sqlite3FitsIn64Bits(z))!=0 ){ 1072fec19aadSdrh sqlite3VdbeOp3(v, OP_Integer, i, 0, z, n); 1073fec19aadSdrh }else{ 1074fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n); 1075fec19aadSdrh } 1076fec19aadSdrh } 1077fec19aadSdrh 1078fec19aadSdrh /* 1079cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 10801ccde15dSdrh ** expression and leave the result on the top of stack. 1081cce7d176Sdrh */ 10824adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1083cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1084cce7d176Sdrh int op; 1085daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1086cce7d176Sdrh switch( pExpr->op ){ 1087cce7d176Sdrh case TK_PLUS: op = OP_Add; break; 1088cce7d176Sdrh case TK_MINUS: op = OP_Subtract; break; 1089cce7d176Sdrh case TK_STAR: op = OP_Multiply; break; 1090cce7d176Sdrh case TK_SLASH: op = OP_Divide; break; 1091cce7d176Sdrh case TK_AND: op = OP_And; break; 1092cce7d176Sdrh case TK_OR: op = OP_Or; break; 1093cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1094cce7d176Sdrh case TK_LE: op = OP_Le; break; 1095cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1096cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1097cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1098cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1099cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1100cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1101cce7d176Sdrh case TK_NOT: op = OP_Not; break; 1102cce7d176Sdrh case TK_UMINUS: op = OP_Negative; break; 1103bf4133cbSdrh case TK_BITAND: op = OP_BitAnd; break; 1104bf4133cbSdrh case TK_BITOR: op = OP_BitOr; break; 1105bf4133cbSdrh case TK_BITNOT: op = OP_BitNot; break; 1106bf4133cbSdrh case TK_LSHIFT: op = OP_ShiftLeft; break; 1107bf4133cbSdrh case TK_RSHIFT: op = OP_ShiftRight; break; 1108bf4133cbSdrh case TK_REM: op = OP_Remainder; break; 1109fec19aadSdrh case TK_FLOAT: op = OP_Real; break; 1110fec19aadSdrh case TK_STRING: op = OP_String; break; 1111c572ef7fSdanielk1977 case TK_BLOB: op = OP_HexBlob; break; 1112cce7d176Sdrh default: break; 1113cce7d176Sdrh } 1114cce7d176Sdrh switch( pExpr->op ){ 1115967e8b73Sdrh case TK_COLUMN: { 11162282792aSdrh if( pParse->useAgg ){ 11174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1118c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 11194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1120c4a3c779Sdrh }else{ 11214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 11222282792aSdrh } 1123cce7d176Sdrh break; 1124cce7d176Sdrh } 1125cce7d176Sdrh case TK_INTEGER: { 1126fec19aadSdrh codeInteger(v, pExpr->token.z, pExpr->token.n); 1127fec19aadSdrh break; 112851e9a445Sdrh } 1129fec19aadSdrh case TK_FLOAT: 1130fec19aadSdrh case TK_STRING: { 1131fec19aadSdrh sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n); 11324adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1133cce7d176Sdrh break; 1134cce7d176Sdrh } 1135c572ef7fSdanielk1977 case TK_BLOB: { 1136c572ef7fSdanielk1977 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1); 1137c572ef7fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1138c572ef7fSdanielk1977 break; 1139c572ef7fSdanielk1977 } 1140cce7d176Sdrh case TK_NULL: { 11414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 1142cce7d176Sdrh break; 1143cce7d176Sdrh } 114450457896Sdrh case TK_VARIABLE: { 11454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 114650457896Sdrh break; 114750457896Sdrh } 1148c9b84a1fSdrh case TK_LT: 1149c9b84a1fSdrh case TK_LE: 1150c9b84a1fSdrh case TK_GT: 1151c9b84a1fSdrh case TK_GE: 1152c9b84a1fSdrh case TK_NE: 1153c9b84a1fSdrh case TK_EQ: { 1154a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0); 1155a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1156a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1157a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, 0); 1158a37cdde0Sdanielk1977 break; 1159c9b84a1fSdrh } 1160cce7d176Sdrh case TK_AND: 1161cce7d176Sdrh case TK_OR: 1162cce7d176Sdrh case TK_PLUS: 1163cce7d176Sdrh case TK_STAR: 1164cce7d176Sdrh case TK_MINUS: 1165bf4133cbSdrh case TK_REM: 1166bf4133cbSdrh case TK_BITAND: 1167bf4133cbSdrh case TK_BITOR: 1168c9b84a1fSdrh case TK_SLASH: { 11694adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11704adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11714adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1172cce7d176Sdrh break; 1173cce7d176Sdrh } 1174bf4133cbSdrh case TK_LSHIFT: 1175bf4133cbSdrh case TK_RSHIFT: { 11764adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11774adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11784adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1179bf4133cbSdrh break; 1180bf4133cbSdrh } 11810040077dSdrh case TK_CONCAT: { 11824adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11834adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Concat, 2, 0); 11850040077dSdrh break; 11860040077dSdrh } 1187cce7d176Sdrh case TK_UMINUS: { 1188fec19aadSdrh Expr *pLeft = pExpr->pLeft; 1189fec19aadSdrh assert( pLeft ); 1190fec19aadSdrh if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ 1191fec19aadSdrh Token *p = &pLeft->token; 11926e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 11936e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1194fec19aadSdrh if( pLeft->op==TK_FLOAT ){ 1195fec19aadSdrh sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1); 1196e6840900Sdrh }else{ 1197fec19aadSdrh codeInteger(v, z, p->n+1); 1198e6840900Sdrh } 11996e142f54Sdrh sqliteFree(z); 12006e142f54Sdrh break; 12016e142f54Sdrh } 12021ccde15dSdrh /* Fall through into TK_NOT */ 12036e142f54Sdrh } 1204bf4133cbSdrh case TK_BITNOT: 12056e142f54Sdrh case TK_NOT: { 12064adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12074adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1208cce7d176Sdrh break; 1209cce7d176Sdrh } 1210cce7d176Sdrh case TK_ISNULL: 1211cce7d176Sdrh case TK_NOTNULL: { 1212cce7d176Sdrh int dest; 12134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 12144adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12154adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 12164adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 12174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1218cce7d176Sdrh } 1219a37cdde0Sdanielk1977 break; 12202282792aSdrh case TK_AGG_FUNCTION: { 12214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 12222282792aSdrh break; 12232282792aSdrh } 12244b59ab5eSdrh case TK_GLOB: 12254b59ab5eSdrh case TK_LIKE: 1226cce7d176Sdrh case TK_FUNCTION: { 1227cce7d176Sdrh ExprList *pList = pExpr->pList; 122889425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 12290bce8354Sdrh FuncDef *pDef; 12304b59ab5eSdrh int nId; 12314b59ab5eSdrh const char *zId; 12324b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 12334adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0); 12340bce8354Sdrh assert( pDef!=0 ); 1235f9b596ebSdrh nExpr = sqlite3ExprCodeExprList(pParse, pList); 1236f9b596ebSdrh sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_FUNCDEF); 12376ec2733bSdrh break; 12386ec2733bSdrh } 123919a775c2Sdrh case TK_SELECT: { 12404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 124119a775c2Sdrh break; 124219a775c2Sdrh } 1243fef5208cSdrh case TK_IN: { 1244fef5208cSdrh int addr; 1245e014a838Sdanielk1977 char const *affStr; 1246e014a838Sdanielk1977 1247e014a838Sdanielk1977 /* Figure out the affinity to use to create a key from the results 1248e014a838Sdanielk1977 ** of the expression. affinityStr stores a static string suitable for 1249e014a838Sdanielk1977 ** P3 of OP_MakeKey. 1250e014a838Sdanielk1977 */ 1251e014a838Sdanielk1977 affStr = sqlite3AffinityString(comparisonAffinity(pExpr)); 1252e014a838Sdanielk1977 12534adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 1254e014a838Sdanielk1977 1255e014a838Sdanielk1977 /* Code the <expr> from "<expr> IN (...)". The temporary table 1256e014a838Sdanielk1977 ** pExpr->iTable contains the values that make up the (...) set. 1257e014a838Sdanielk1977 */ 12584adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12594adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 1260e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ 12614adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 12624adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 1263e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7); 1264e014a838Sdanielk1977 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */ 1265e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7); 1266e014a838Sdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ 1267e014a838Sdanielk1977 1268fef5208cSdrh break; 1269fef5208cSdrh } 1270fef5208cSdrh case TK_BETWEEN: { 12714adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12724adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 12734adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 12744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, 0, 0); 12754adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 12764adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 12774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, 0, 0); 12784adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1279fef5208cSdrh break; 1280fef5208cSdrh } 128151e9a445Sdrh case TK_UPLUS: 1282a2e00042Sdrh case TK_AS: { 12834adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1284a2e00042Sdrh break; 1285a2e00042Sdrh } 128617a7f8ddSdrh case TK_CASE: { 128717a7f8ddSdrh int expr_end_label; 1288f5905aa7Sdrh int jumpInst; 1289f5905aa7Sdrh int addr; 1290f5905aa7Sdrh int nExpr; 129117a7f8ddSdrh int i; 129217a7f8ddSdrh 129317a7f8ddSdrh assert(pExpr->pList); 129417a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 129517a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1296f5905aa7Sdrh nExpr = pExpr->pList->nExpr; 12974adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 129817a7f8ddSdrh if( pExpr->pLeft ){ 12994adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1300cce7d176Sdrh } 1301f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 13024adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr); 130317a7f8ddSdrh if( pExpr->pLeft ){ 13044adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 13054adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0); 13064adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1307f5905aa7Sdrh }else{ 13084adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 130917a7f8ddSdrh } 13104adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr); 13114adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 13124adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 13134adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 131417a7f8ddSdrh } 1315f570f011Sdrh if( pExpr->pLeft ){ 13164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1317f570f011Sdrh } 131817a7f8ddSdrh if( pExpr->pRight ){ 13194adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 132017a7f8ddSdrh }else{ 13214adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 132217a7f8ddSdrh } 13234adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 13246f34903eSdanielk1977 break; 13256f34903eSdanielk1977 } 13266f34903eSdanielk1977 case TK_RAISE: { 13276f34903eSdanielk1977 if( !pParse->trigStack ){ 13284adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1329da93d238Sdrh "RAISE() may only be used within a trigger-program"); 13306f34903eSdanielk1977 pParse->nErr++; 13316f34903eSdanielk1977 return; 13326f34903eSdanielk1977 } 13336f34903eSdanielk1977 if( pExpr->iColumn == OE_Rollback || 13346f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 13356f34903eSdanielk1977 pExpr->iColumn == OE_Fail ){ 13364adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1337701a0aebSdrh pExpr->token.z, pExpr->token.n); 13384adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 13396f34903eSdanielk1977 } else { 13406f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 13414adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump, 1342701a0aebSdrh "(IGNORE jump)", 0); 13436f34903eSdanielk1977 } 134417a7f8ddSdrh } 134517a7f8ddSdrh break; 134617a7f8ddSdrh } 1347cce7d176Sdrh } 1348cce7d176Sdrh 1349cce7d176Sdrh /* 1350268380caSdrh ** Generate code that pushes the value of every element of the given 1351f9b596ebSdrh ** expression list onto the stack. 1352268380caSdrh ** 1353268380caSdrh ** Return the number of elements pushed onto the stack. 1354268380caSdrh */ 13554adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1356268380caSdrh Parse *pParse, /* Parsing context */ 1357f9b596ebSdrh ExprList *pList /* The expression list to be coded */ 1358268380caSdrh ){ 1359268380caSdrh struct ExprList_item *pItem; 1360268380caSdrh int i, n; 1361268380caSdrh Vdbe *v; 1362268380caSdrh if( pList==0 ) return 0; 13634adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1364268380caSdrh n = pList->nExpr; 1365268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 13664adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1367268380caSdrh } 1368f9b596ebSdrh return n; 1369268380caSdrh } 1370268380caSdrh 1371268380caSdrh /* 1372cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1373cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1374cce7d176Sdrh ** continues straight thru if the expression is false. 1375f5905aa7Sdrh ** 1376f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1377f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1378cce7d176Sdrh */ 13794adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1380cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1381cce7d176Sdrh int op = 0; 1382daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1383cce7d176Sdrh switch( pExpr->op ){ 1384cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1385cce7d176Sdrh case TK_LE: op = OP_Le; break; 1386cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1387cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1388cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1389cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1390cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1391cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1392cce7d176Sdrh default: break; 1393cce7d176Sdrh } 1394cce7d176Sdrh switch( pExpr->op ){ 1395cce7d176Sdrh case TK_AND: { 13964adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 13974adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 13984adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 13994adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1400cce7d176Sdrh break; 1401cce7d176Sdrh } 1402cce7d176Sdrh case TK_OR: { 14034adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 14044adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1405cce7d176Sdrh break; 1406cce7d176Sdrh } 1407cce7d176Sdrh case TK_NOT: { 14084adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1409cce7d176Sdrh break; 1410cce7d176Sdrh } 1411cce7d176Sdrh case TK_LT: 1412cce7d176Sdrh case TK_LE: 1413cce7d176Sdrh case TK_GT: 1414cce7d176Sdrh case TK_GE: 1415cce7d176Sdrh case TK_NE: 14160ac65892Sdrh case TK_EQ: { 1417a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 14184adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14194adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1420a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1421cce7d176Sdrh break; 1422cce7d176Sdrh } 1423cce7d176Sdrh case TK_ISNULL: 1424cce7d176Sdrh case TK_NOTNULL: { 14254adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14264adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1427cce7d176Sdrh break; 1428cce7d176Sdrh } 1429fef5208cSdrh case TK_BETWEEN: { 1430f5905aa7Sdrh int addr; 14314adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 14334adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 14344adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0); 14354adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 14364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest); 14374adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 14384adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 14394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1440fef5208cSdrh break; 1441fef5208cSdrh } 1442cce7d176Sdrh default: { 14434adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 14444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1445cce7d176Sdrh break; 1446cce7d176Sdrh } 1447cce7d176Sdrh } 1448cce7d176Sdrh } 1449cce7d176Sdrh 1450cce7d176Sdrh /* 145166b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1452cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1453cce7d176Sdrh ** continues straight thru if the expression is true. 1454f5905aa7Sdrh ** 1455f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1456f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1457cce7d176Sdrh */ 14584adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1459cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1460cce7d176Sdrh int op = 0; 1461daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1462cce7d176Sdrh switch( pExpr->op ){ 1463cce7d176Sdrh case TK_LT: op = OP_Ge; break; 1464cce7d176Sdrh case TK_LE: op = OP_Gt; break; 1465cce7d176Sdrh case TK_GT: op = OP_Le; break; 1466cce7d176Sdrh case TK_GE: op = OP_Lt; break; 1467cce7d176Sdrh case TK_NE: op = OP_Eq; break; 1468cce7d176Sdrh case TK_EQ: op = OP_Ne; break; 1469cce7d176Sdrh case TK_ISNULL: op = OP_NotNull; break; 1470cce7d176Sdrh case TK_NOTNULL: op = OP_IsNull; break; 1471cce7d176Sdrh default: break; 1472cce7d176Sdrh } 1473cce7d176Sdrh switch( pExpr->op ){ 1474cce7d176Sdrh case TK_AND: { 14754adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 14764adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1477cce7d176Sdrh break; 1478cce7d176Sdrh } 1479cce7d176Sdrh case TK_OR: { 14804adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 14814adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 14824adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 14834adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1484cce7d176Sdrh break; 1485cce7d176Sdrh } 1486cce7d176Sdrh case TK_NOT: { 14874adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1488cce7d176Sdrh break; 1489cce7d176Sdrh } 1490cce7d176Sdrh case TK_LT: 1491cce7d176Sdrh case TK_LE: 1492cce7d176Sdrh case TK_GT: 1493cce7d176Sdrh case TK_GE: 1494cce7d176Sdrh case TK_NE: 1495cce7d176Sdrh case TK_EQ: { 1496a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 14974adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14984adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1499a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1500cce7d176Sdrh break; 1501cce7d176Sdrh } 1502cce7d176Sdrh case TK_ISNULL: 1503cce7d176Sdrh case TK_NOTNULL: { 15044adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15054adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1506cce7d176Sdrh break; 1507cce7d176Sdrh } 1508e014a838Sdanielk1977 #if 0 1509fef5208cSdrh case TK_IN: { 1510f5905aa7Sdrh int addr; 15114adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15124adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3); 15144adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1516fef5208cSdrh if( pExpr->pSelect ){ 15174adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); 1518fef5208cSdrh }else{ 15194adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); 1520fef5208cSdrh } 1521fef5208cSdrh break; 1522fef5208cSdrh } 1523e014a838Sdanielk1977 #endif 1524fef5208cSdrh case TK_BETWEEN: { 1525fef5208cSdrh int addr; 15264adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 15284adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 15294adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15304adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); 15314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 15334adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 15344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest); 1535fef5208cSdrh break; 1536fef5208cSdrh } 1537cce7d176Sdrh default: { 15384adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 15394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1540cce7d176Sdrh break; 1541cce7d176Sdrh } 1542cce7d176Sdrh } 1543cce7d176Sdrh } 15442282792aSdrh 15452282792aSdrh /* 15462282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 15472282792aSdrh ** if they are identical and return FALSE if they differ in any way. 15482282792aSdrh */ 15494adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 15502282792aSdrh int i; 15512282792aSdrh if( pA==0 ){ 15522282792aSdrh return pB==0; 15532282792aSdrh }else if( pB==0 ){ 15542282792aSdrh return 0; 15552282792aSdrh } 15562282792aSdrh if( pA->op!=pB->op ) return 0; 15574adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 15584adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 15592282792aSdrh if( pA->pList ){ 15602282792aSdrh if( pB->pList==0 ) return 0; 15612282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 15622282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 15634adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 15642282792aSdrh return 0; 15652282792aSdrh } 15662282792aSdrh } 15672282792aSdrh }else if( pB->pList ){ 15682282792aSdrh return 0; 15692282792aSdrh } 15702282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 15712f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 15722282792aSdrh if( pA->token.z ){ 15732282792aSdrh if( pB->token.z==0 ) return 0; 15746977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 15754adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 15762282792aSdrh } 15772282792aSdrh return 1; 15782282792aSdrh } 15792282792aSdrh 15802282792aSdrh /* 15812282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 15822282792aSdrh */ 15832282792aSdrh static int appendAggInfo(Parse *pParse){ 15842282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 15852282792aSdrh int amt = pParse->nAgg + 8; 15866d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 15876d4abfbeSdrh if( aAgg==0 ){ 15882282792aSdrh return -1; 15892282792aSdrh } 15906d4abfbeSdrh pParse->aAgg = aAgg; 15912282792aSdrh } 15922282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 15932282792aSdrh return pParse->nAgg++; 15942282792aSdrh } 15952282792aSdrh 15962282792aSdrh /* 15972282792aSdrh ** Analyze the given expression looking for aggregate functions and 15982282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 15992282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 16002282792aSdrh ** 16012282792aSdrh ** This routine should only be called after the expression has been 16024adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck(). 16032282792aSdrh ** 16042282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return 16052282792aSdrh ** the number of errors. 16062282792aSdrh */ 16074adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 16082282792aSdrh int i; 16092282792aSdrh AggExpr *aAgg; 16102282792aSdrh int nErr = 0; 16112282792aSdrh 16122282792aSdrh if( pExpr==0 ) return 0; 16132282792aSdrh switch( pExpr->op ){ 1614967e8b73Sdrh case TK_COLUMN: { 16152282792aSdrh aAgg = pParse->aAgg; 16162282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16172282792aSdrh if( aAgg[i].isAgg ) continue; 16182282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 1619967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 16202282792aSdrh break; 16212282792aSdrh } 16222282792aSdrh } 16232282792aSdrh if( i>=pParse->nAgg ){ 16242282792aSdrh i = appendAggInfo(pParse); 16252282792aSdrh if( i<0 ) return 1; 16262282792aSdrh pParse->aAgg[i].isAgg = 0; 16272282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16282282792aSdrh } 1629aaf88729Sdrh pExpr->iAgg = i; 16302282792aSdrh break; 16312282792aSdrh } 16322282792aSdrh case TK_AGG_FUNCTION: { 16332282792aSdrh aAgg = pParse->aAgg; 16342282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16352282792aSdrh if( !aAgg[i].isAgg ) continue; 16364adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 16372282792aSdrh break; 16382282792aSdrh } 16392282792aSdrh } 16402282792aSdrh if( i>=pParse->nAgg ){ 16412282792aSdrh i = appendAggInfo(pParse); 16422282792aSdrh if( i<0 ) return 1; 16432282792aSdrh pParse->aAgg[i].isAgg = 1; 16442282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16454adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 16466977fea8Sdrh pExpr->token.z, pExpr->token.n, 1647f55f25f0Sdrh pExpr->pList ? pExpr->pList->nExpr : 0, 0); 16482282792aSdrh } 16492282792aSdrh pExpr->iAgg = i; 16502282792aSdrh break; 16512282792aSdrh } 16522282792aSdrh default: { 16532282792aSdrh if( pExpr->pLeft ){ 16544adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft); 16552282792aSdrh } 16562282792aSdrh if( nErr==0 && pExpr->pRight ){ 16574adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight); 16582282792aSdrh } 16592282792aSdrh if( nErr==0 && pExpr->pList ){ 16602282792aSdrh int n = pExpr->pList->nExpr; 16612282792aSdrh int i; 16622282792aSdrh for(i=0; nErr==0 && i<n; i++){ 16634adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 16642282792aSdrh } 16652282792aSdrh } 16662282792aSdrh break; 16672282792aSdrh } 16682282792aSdrh } 16692282792aSdrh return nErr; 16702282792aSdrh } 16718e0a2f90Sdrh 16728e0a2f90Sdrh /* 16738e0a2f90Sdrh ** Locate a user function given a name and a number of arguments. 16740bce8354Sdrh ** Return a pointer to the FuncDef structure that defines that 16758e0a2f90Sdrh ** function, or return NULL if the function does not exist. 16768e0a2f90Sdrh ** 16770bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef 16788e0a2f90Sdrh ** structure is created and liked into the "db" structure if a 16798e0a2f90Sdrh ** no matching function previously existed. When createFlag is true 16808e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts 16818e0a2f90Sdrh ** any number of arguments will be returned. 16828e0a2f90Sdrh ** 16838e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid 16848e0a2f90Sdrh ** function found is returned. A function is valid if either xFunc 16858e0a2f90Sdrh ** or xStep is non-zero. 16868e0a2f90Sdrh */ 16874adee20fSdanielk1977 FuncDef *sqlite3FindFunction( 16888e0a2f90Sdrh sqlite *db, /* An open database */ 16898e0a2f90Sdrh const char *zName, /* Name of the function. Not null-terminated */ 16908e0a2f90Sdrh int nName, /* Number of characters in the name */ 16918e0a2f90Sdrh int nArg, /* Number of arguments. -1 means any number */ 16928e0a2f90Sdrh int createFlag /* Create new entry if true and does not otherwise exist */ 16938e0a2f90Sdrh ){ 16940bce8354Sdrh FuncDef *pFirst, *p, *pMaybe; 16954adee20fSdanielk1977 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); 16961350b030Sdrh if( p && !createFlag && nArg<0 ){ 16978e0a2f90Sdrh while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } 16988e0a2f90Sdrh return p; 16998e0a2f90Sdrh } 17008e0a2f90Sdrh pMaybe = 0; 17018e0a2f90Sdrh while( p && p->nArg!=nArg ){ 17028e0a2f90Sdrh if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; 17038e0a2f90Sdrh p = p->pNext; 17048e0a2f90Sdrh } 17058e0a2f90Sdrh if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ 17068e0a2f90Sdrh return 0; 17078e0a2f90Sdrh } 17088e0a2f90Sdrh if( p==0 && pMaybe ){ 17098e0a2f90Sdrh assert( createFlag==0 ); 17108e0a2f90Sdrh return pMaybe; 17118e0a2f90Sdrh } 1712f9b596ebSdrh if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)+nName+1))!=0 ){ 17138e0a2f90Sdrh p->nArg = nArg; 17148e0a2f90Sdrh p->pNext = pFirst; 1715f9b596ebSdrh p->zName = (char*)&p[1]; 1716f9b596ebSdrh memcpy(p->zName, zName, nName); 1717f9b596ebSdrh p->zName[nName] = 0; 1718f9b596ebSdrh sqlite3HashInsert(&db->aFunc, p->zName, nName, (void*)p); 17198e0a2f90Sdrh } 17208e0a2f90Sdrh return p; 17218e0a2f90Sdrh } 1722