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*93edea93Sdanielk1977 ** $Id: expr.c,v 1.120 2004/05/16 22:55:28 danielk1977 Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20a37cdde0Sdanielk1977 static char exprAffinity(Expr *pExpr){ 21a37cdde0Sdanielk1977 if( pExpr->op==TK_AS ){ 22a37cdde0Sdanielk1977 return exprAffinity(pExpr->pLeft); 23a37cdde0Sdanielk1977 } 24a37cdde0Sdanielk1977 if( pExpr->op==TK_SELECT ){ 25a37cdde0Sdanielk1977 return exprAffinity(pExpr->pSelect->pEList->a[0].pExpr); 26a37cdde0Sdanielk1977 } 27a37cdde0Sdanielk1977 return pExpr->affinity; 28a37cdde0Sdanielk1977 } 29a37cdde0Sdanielk1977 30a37cdde0Sdanielk1977 /* 31a37cdde0Sdanielk1977 ** Return the P1 value that should be used for a binary comparison 32a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 33a37cdde0Sdanielk1977 ** If jumpIfNull is true, then set the low byte of the returned 34a37cdde0Sdanielk1977 ** P1 value to tell the opcode to jump if either expression 35a37cdde0Sdanielk1977 ** evaluates to NULL. 36a37cdde0Sdanielk1977 */ 37a37cdde0Sdanielk1977 int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 38a37cdde0Sdanielk1977 char aff1 = exprAffinity(pExpr1); 39a37cdde0Sdanielk1977 char aff2 = exprAffinity(pExpr2); 40a37cdde0Sdanielk1977 41a37cdde0Sdanielk1977 if( aff1 && aff2 ){ 42a37cdde0Sdanielk1977 /* Both sides of the comparison are columns. If one has numeric or 43a37cdde0Sdanielk1977 ** integer affinity, use that. Otherwise use no affinity. 44a37cdde0Sdanielk1977 */ 45a37cdde0Sdanielk1977 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){ 46a37cdde0Sdanielk1977 aff1 = SQLITE_AFF_INTEGER; 47a37cdde0Sdanielk1977 }else 48a37cdde0Sdanielk1977 if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){ 49a37cdde0Sdanielk1977 aff1 = SQLITE_AFF_NUMERIC; 50a37cdde0Sdanielk1977 }else{ 51a37cdde0Sdanielk1977 aff1 = SQLITE_AFF_NONE; 52a37cdde0Sdanielk1977 } 53a37cdde0Sdanielk1977 }else if( !aff1 ){ 54a37cdde0Sdanielk1977 aff1 = aff2; 55a37cdde0Sdanielk1977 } 56a37cdde0Sdanielk1977 57a37cdde0Sdanielk1977 return (((int)aff1)<<8)+(jumpIfNull?1:0); 58a37cdde0Sdanielk1977 } 59a37cdde0Sdanielk1977 60a2e00042Sdrh /* 61a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 62a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 63a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 64a76b5dfcSdrh */ 654adee20fSdanielk1977 Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ 66a76b5dfcSdrh Expr *pNew; 67a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 68a76b5dfcSdrh if( pNew==0 ){ 694efc4754Sdrh /* When malloc fails, we leak memory from pLeft and pRight */ 70a76b5dfcSdrh return 0; 71a76b5dfcSdrh } 72a76b5dfcSdrh pNew->op = op; 73a76b5dfcSdrh pNew->pLeft = pLeft; 74a76b5dfcSdrh pNew->pRight = pRight; 75a76b5dfcSdrh if( pToken ){ 764b59ab5eSdrh assert( pToken->dyn==0 ); 77a76b5dfcSdrh pNew->token = *pToken; 786977fea8Sdrh pNew->span = *pToken; 79a76b5dfcSdrh }else{ 804efc4754Sdrh assert( pNew->token.dyn==0 ); 814efc4754Sdrh assert( pNew->token.z==0 ); 824efc4754Sdrh assert( pNew->token.n==0 ); 836977fea8Sdrh if( pLeft && pRight ){ 844adee20fSdanielk1977 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); 856977fea8Sdrh }else{ 866977fea8Sdrh pNew->span = pNew->token; 876977fea8Sdrh } 88a76b5dfcSdrh } 89a76b5dfcSdrh return pNew; 90a76b5dfcSdrh } 91a76b5dfcSdrh 92a76b5dfcSdrh /* 936977fea8Sdrh ** Set the Expr.span field of the given expression to span all 94a76b5dfcSdrh ** text between the two given tokens. 95a76b5dfcSdrh */ 964adee20fSdanielk1977 void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 974efc4754Sdrh assert( pRight!=0 ); 984efc4754Sdrh assert( pLeft!=0 ); 994efc4754Sdrh /* Note: pExpr might be NULL due to a prior malloc failure */ 1004efc4754Sdrh if( pExpr && pRight->z && pLeft->z ){ 1014b59ab5eSdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 1026977fea8Sdrh pExpr->span.z = pLeft->z; 1036977fea8Sdrh pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); 1044b59ab5eSdrh }else{ 1056977fea8Sdrh pExpr->span.z = 0; 1064b59ab5eSdrh } 107a76b5dfcSdrh } 108a76b5dfcSdrh } 109a76b5dfcSdrh 110a76b5dfcSdrh /* 111a76b5dfcSdrh ** Construct a new expression node for a function with multiple 112a76b5dfcSdrh ** arguments. 113a76b5dfcSdrh */ 1144adee20fSdanielk1977 Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){ 115a76b5dfcSdrh Expr *pNew; 116a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 117a76b5dfcSdrh if( pNew==0 ){ 1184adee20fSdanielk1977 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */ 119a76b5dfcSdrh return 0; 120a76b5dfcSdrh } 121a76b5dfcSdrh pNew->op = TK_FUNCTION; 122a76b5dfcSdrh pNew->pList = pList; 123a76b5dfcSdrh if( pToken ){ 1244b59ab5eSdrh assert( pToken->dyn==0 ); 125a76b5dfcSdrh pNew->token = *pToken; 126a76b5dfcSdrh }else{ 127a76b5dfcSdrh pNew->token.z = 0; 128a76b5dfcSdrh } 1296977fea8Sdrh pNew->span = pNew->token; 130a76b5dfcSdrh return pNew; 131a76b5dfcSdrh } 132a76b5dfcSdrh 133a76b5dfcSdrh /* 134a2e00042Sdrh ** Recursively delete an expression tree. 135a2e00042Sdrh */ 1364adee20fSdanielk1977 void sqlite3ExprDelete(Expr *p){ 137a2e00042Sdrh if( p==0 ) return; 1384efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 1394efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 1404adee20fSdanielk1977 sqlite3ExprDelete(p->pLeft); 1414adee20fSdanielk1977 sqlite3ExprDelete(p->pRight); 1424adee20fSdanielk1977 sqlite3ExprListDelete(p->pList); 1434adee20fSdanielk1977 sqlite3SelectDelete(p->pSelect); 144a2e00042Sdrh sqliteFree(p); 145a2e00042Sdrh } 146a2e00042Sdrh 147a76b5dfcSdrh 148a76b5dfcSdrh /* 149ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 150ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 151ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 152ff78bd2fSdrh ** without effecting the originals. 153ff78bd2fSdrh ** 1544adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 1554adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 156ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 157ff78bd2fSdrh ** 158ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 159ff78bd2fSdrh */ 1604adee20fSdanielk1977 Expr *sqlite3ExprDup(Expr *p){ 161ff78bd2fSdrh Expr *pNew; 162ff78bd2fSdrh if( p==0 ) return 0; 163fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 164ff78bd2fSdrh if( pNew==0 ) return 0; 1653b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 1666977fea8Sdrh if( p->token.z!=0 ){ 1674b59ab5eSdrh pNew->token.z = sqliteStrDup(p->token.z); 1684b59ab5eSdrh pNew->token.dyn = 1; 1694b59ab5eSdrh }else{ 1704efc4754Sdrh assert( pNew->token.z==0 ); 1714b59ab5eSdrh } 1726977fea8Sdrh pNew->span.z = 0; 1734adee20fSdanielk1977 pNew->pLeft = sqlite3ExprDup(p->pLeft); 1744adee20fSdanielk1977 pNew->pRight = sqlite3ExprDup(p->pRight); 1754adee20fSdanielk1977 pNew->pList = sqlite3ExprListDup(p->pList); 1764adee20fSdanielk1977 pNew->pSelect = sqlite3SelectDup(p->pSelect); 177ff78bd2fSdrh return pNew; 178ff78bd2fSdrh } 1794adee20fSdanielk1977 void sqlite3TokenCopy(Token *pTo, Token *pFrom){ 1804b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 1814b59ab5eSdrh if( pFrom->z ){ 1824b59ab5eSdrh pTo->n = pFrom->n; 1834b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 1844b59ab5eSdrh pTo->dyn = 1; 1854b59ab5eSdrh }else{ 1864b59ab5eSdrh pTo->z = 0; 1874b59ab5eSdrh } 1884b59ab5eSdrh } 1894adee20fSdanielk1977 ExprList *sqlite3ExprListDup(ExprList *p){ 190ff78bd2fSdrh ExprList *pNew; 1913e7bc9caSdrh struct ExprList_item *pItem; 192ff78bd2fSdrh int i; 193ff78bd2fSdrh if( p==0 ) return 0; 194ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 195ff78bd2fSdrh if( pNew==0 ) return 0; 1964305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 1973e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 1981bdd9b57Sdrh if( pItem==0 ) return 0; /* Leaks memory after a malloc failure */ 1991bdd9b57Sdrh for(i=0; i<p->nExpr; i++, pItem++){ 2004b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 2014adee20fSdanielk1977 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr); 2026977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 2036977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 2044b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 2054b59ab5eSdrh ** the names of columns in the result set needs this information */ 2064adee20fSdanielk1977 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span); 2074b59ab5eSdrh } 2081f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 20924b03fd0Sdanielk1977 || pOldExpr->span.z==0 || sqlite3_malloc_failed ); 2103e7bc9caSdrh pItem->zName = sqliteStrDup(p->a[i].zName); 2113e7bc9caSdrh pItem->sortOrder = p->a[i].sortOrder; 2123e7bc9caSdrh pItem->isAgg = p->a[i].isAgg; 2133e7bc9caSdrh pItem->done = 0; 214ff78bd2fSdrh } 215ff78bd2fSdrh return pNew; 216ff78bd2fSdrh } 2174adee20fSdanielk1977 SrcList *sqlite3SrcListDup(SrcList *p){ 218ad3cab52Sdrh SrcList *pNew; 219ad3cab52Sdrh int i; 220113088ecSdrh int nByte; 221ad3cab52Sdrh if( p==0 ) return 0; 222113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 2234efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 224ad3cab52Sdrh if( pNew==0 ) return 0; 2254305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 226ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 2274efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 2284efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 2294efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 2304efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 2314efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 2324efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 2334efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 2344efc4754Sdrh pNewItem->pTab = 0; 2354adee20fSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect); 2364adee20fSdanielk1977 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn); 2374adee20fSdanielk1977 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing); 238ad3cab52Sdrh } 239ad3cab52Sdrh return pNew; 240ad3cab52Sdrh } 2414adee20fSdanielk1977 IdList *sqlite3IdListDup(IdList *p){ 242ff78bd2fSdrh IdList *pNew; 243ff78bd2fSdrh int i; 244ff78bd2fSdrh if( p==0 ) return 0; 2454efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 246ff78bd2fSdrh if( pNew==0 ) return 0; 2474305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 2484efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 249e4697f5eSdrh if( pNew->a==0 ) return 0; 250ff78bd2fSdrh for(i=0; i<p->nId; i++){ 2514efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 2524efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 2534efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 2544efc4754Sdrh pNewItem->idx = pOldItem->idx; 255ff78bd2fSdrh } 256ff78bd2fSdrh return pNew; 257ff78bd2fSdrh } 2584adee20fSdanielk1977 Select *sqlite3SelectDup(Select *p){ 259ff78bd2fSdrh Select *pNew; 260ff78bd2fSdrh if( p==0 ) return 0; 2614efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 262ff78bd2fSdrh if( pNew==0 ) return 0; 263ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 2644adee20fSdanielk1977 pNew->pEList = sqlite3ExprListDup(p->pEList); 2654adee20fSdanielk1977 pNew->pSrc = sqlite3SrcListDup(p->pSrc); 2664adee20fSdanielk1977 pNew->pWhere = sqlite3ExprDup(p->pWhere); 2674adee20fSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy); 2684adee20fSdanielk1977 pNew->pHaving = sqlite3ExprDup(p->pHaving); 2694adee20fSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy); 270ff78bd2fSdrh pNew->op = p->op; 2714adee20fSdanielk1977 pNew->pPrior = sqlite3SelectDup(p->pPrior); 272ff78bd2fSdrh pNew->nLimit = p->nLimit; 273ff78bd2fSdrh pNew->nOffset = p->nOffset; 274ff78bd2fSdrh pNew->zSelect = 0; 2757b58daeaSdrh pNew->iLimit = -1; 2767b58daeaSdrh pNew->iOffset = -1; 277ff78bd2fSdrh return pNew; 278ff78bd2fSdrh } 279ff78bd2fSdrh 280ff78bd2fSdrh 281ff78bd2fSdrh /* 282a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 283a76b5dfcSdrh ** initially NULL, then create a new expression list. 284a76b5dfcSdrh */ 2854adee20fSdanielk1977 ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 286a76b5dfcSdrh if( pList==0 ){ 287a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 288a76b5dfcSdrh if( pList==0 ){ 2894adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 290a76b5dfcSdrh return 0; 291a76b5dfcSdrh } 2924efc4754Sdrh assert( pList->nAlloc==0 ); 293a76b5dfcSdrh } 2944305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 2954305d103Sdrh pList->nAlloc = pList->nAlloc*2 + 4; 2964efc4754Sdrh pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); 2974efc4754Sdrh if( pList->a==0 ){ 2984adee20fSdanielk1977 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */ 2994efc4754Sdrh pList->nExpr = pList->nAlloc = 0; 300a76b5dfcSdrh return pList; 301a76b5dfcSdrh } 302a76b5dfcSdrh } 3034efc4754Sdrh assert( pList->a!=0 ); 3044efc4754Sdrh if( pExpr || pName ){ 3054efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 3064efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 3074efc4754Sdrh pItem->pExpr = pExpr; 308a76b5dfcSdrh if( pName ){ 3094adee20fSdanielk1977 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0); 3104adee20fSdanielk1977 sqlite3Dequote(pItem->zName); 311a76b5dfcSdrh } 312a76b5dfcSdrh } 313a76b5dfcSdrh return pList; 314a76b5dfcSdrh } 315a76b5dfcSdrh 316a76b5dfcSdrh /* 317a76b5dfcSdrh ** Delete an entire expression list. 318a76b5dfcSdrh */ 3194adee20fSdanielk1977 void sqlite3ExprListDelete(ExprList *pList){ 320a76b5dfcSdrh int i; 321a76b5dfcSdrh if( pList==0 ) return; 3221bdd9b57Sdrh assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); 3231bdd9b57Sdrh assert( pList->nExpr<=pList->nAlloc ); 324a76b5dfcSdrh for(i=0; i<pList->nExpr; i++){ 3254adee20fSdanielk1977 sqlite3ExprDelete(pList->a[i].pExpr); 326a76b5dfcSdrh sqliteFree(pList->a[i].zName); 327a76b5dfcSdrh } 328a76b5dfcSdrh sqliteFree(pList->a); 329a76b5dfcSdrh sqliteFree(pList); 330a76b5dfcSdrh } 331a76b5dfcSdrh 332a76b5dfcSdrh /* 333fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 334fef5208cSdrh ** and 0 if it involves variables. 3352398937bSdrh ** 3362398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 3372398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 3382398937bSdrh ** a constant. 339fef5208cSdrh */ 3404adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 341fef5208cSdrh switch( p->op ){ 342fef5208cSdrh case TK_ID: 343967e8b73Sdrh case TK_COLUMN: 344fef5208cSdrh case TK_DOT: 3457bdc0c1dSdrh case TK_FUNCTION: 346fef5208cSdrh return 0; 3477bdc0c1dSdrh case TK_NULL: 3482398937bSdrh case TK_STRING: 3499208643dSdrh case TK_INTEGER: 3509208643dSdrh case TK_FLOAT: 35150457896Sdrh case TK_VARIABLE: 3529208643dSdrh return 1; 353fef5208cSdrh default: { 3544adee20fSdanielk1977 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0; 3554adee20fSdanielk1977 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0; 356fef5208cSdrh if( p->pList ){ 357fef5208cSdrh int i; 358fef5208cSdrh for(i=0; i<p->pList->nExpr; i++){ 3594adee20fSdanielk1977 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0; 360fef5208cSdrh } 361fef5208cSdrh } 3629208643dSdrh return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); 363fef5208cSdrh } 364fef5208cSdrh } 3659208643dSdrh return 0; 366fef5208cSdrh } 367fef5208cSdrh 368fef5208cSdrh /* 369202b2df7Sdrh ** If the given expression codes a constant integer that is small enough 370202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 371202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 372202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 373e4de1febSdrh */ 3744adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 375e4de1febSdrh switch( p->op ){ 376e4de1febSdrh case TK_INTEGER: { 3774adee20fSdanielk1977 if( sqlite3FitsIn32Bits(p->token.z) ){ 378e4de1febSdrh *pValue = atoi(p->token.z); 379e4de1febSdrh return 1; 380e4de1febSdrh } 381202b2df7Sdrh break; 382202b2df7Sdrh } 383e4de1febSdrh case TK_STRING: { 384bd790ee3Sdrh const char *z = p->token.z; 385e4de1febSdrh int n = p->token.n; 386bd790ee3Sdrh if( n>0 && z[0]=='-' ){ z++; n--; } 387e4de1febSdrh while( n>0 && *z && isdigit(*z) ){ z++; n--; } 3884adee20fSdanielk1977 if( n==0 && sqlite3FitsIn32Bits(p->token.z) ){ 389e4de1febSdrh *pValue = atoi(p->token.z); 390e4de1febSdrh return 1; 391e4de1febSdrh } 392e4de1febSdrh break; 393e4de1febSdrh } 3944b59ab5eSdrh case TK_UPLUS: { 3954adee20fSdanielk1977 return sqlite3ExprIsInteger(p->pLeft, pValue); 3964b59ab5eSdrh } 397e4de1febSdrh case TK_UMINUS: { 398e4de1febSdrh int v; 3994adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 400e4de1febSdrh *pValue = -v; 401e4de1febSdrh return 1; 402e4de1febSdrh } 403e4de1febSdrh break; 404e4de1febSdrh } 405e4de1febSdrh default: break; 406e4de1febSdrh } 407e4de1febSdrh return 0; 408e4de1febSdrh } 409e4de1febSdrh 410e4de1febSdrh /* 411c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 412c4a3c779Sdrh */ 4134adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 4144adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 4154adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 4164adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 417c4a3c779Sdrh return 0; 418c4a3c779Sdrh } 419c4a3c779Sdrh 420c4a3c779Sdrh /* 4218141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 4228141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 4238141f61eSdrh ** expression node refer back to that source column. The following changes 4248141f61eSdrh ** are made to pExpr: 4258141f61eSdrh ** 4268141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 4278141f61eSdrh ** the table. 4288141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 4298141f61eSdrh ** from pSrcList. 4308141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 4318141f61eSdrh ** pExpr->dataType Set to the appropriate data type for the column. 4328141f61eSdrh ** pExpr->op Set to TK_COLUMN. 4338141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 4348141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 4358141f61eSdrh ** 4368141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 4378141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 4388141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 4398141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 4408141f61eSdrh ** means that the form of the name is Z and that columns from any table 4418141f61eSdrh ** can be used. 4428141f61eSdrh ** 4438141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 4448141f61eSdrh ** in pParse and return non-zero. Return zero on success. 4458141f61eSdrh */ 4468141f61eSdrh static int lookupName( 4478141f61eSdrh Parse *pParse, /* The parsing context */ 4488141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 4498141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 4508141f61eSdrh Token *pColumnToken, /* Name of the column. */ 4518141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 4528141f61eSdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 4538141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 4548141f61eSdrh ){ 4558141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 4568141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 4578141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 4588141f61eSdrh int i, j; /* Loop counters */ 4598141f61eSdrh int cnt = 0; /* Number of matching column names */ 4608141f61eSdrh int cntTab = 0; /* Number of matching table names */ 4617e26d750Sdrh sqlite *db = pParse->db; /* The database */ 4628141f61eSdrh 4638141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 4648141f61eSdrh if( pDbToken && pDbToken->z ){ 4658141f61eSdrh zDb = sqliteStrNDup(pDbToken->z, pDbToken->n); 4664adee20fSdanielk1977 sqlite3Dequote(zDb); 4678141f61eSdrh }else{ 4688141f61eSdrh zDb = 0; 4698141f61eSdrh } 4708141f61eSdrh if( pTableToken && pTableToken->z ){ 4718141f61eSdrh zTab = sqliteStrNDup(pTableToken->z, pTableToken->n); 4724adee20fSdanielk1977 sqlite3Dequote(zTab); 4738141f61eSdrh }else{ 4748141f61eSdrh assert( zDb==0 ); 4758141f61eSdrh zTab = 0; 4768141f61eSdrh } 4778141f61eSdrh zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n); 4784adee20fSdanielk1977 sqlite3Dequote(zCol); 47924b03fd0Sdanielk1977 if( sqlite3_malloc_failed ){ 4808141f61eSdrh return 1; /* Leak memory (zDb and zTab) if malloc fails */ 4818141f61eSdrh } 4828141f61eSdrh assert( zTab==0 || pEList==0 ); 4838141f61eSdrh 4848141f61eSdrh pExpr->iTable = -1; 4858141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 4868141f61eSdrh struct SrcList_item *pItem = &pSrcList->a[i]; 4878141f61eSdrh Table *pTab = pItem->pTab; 4888141f61eSdrh Column *pCol; 4898141f61eSdrh 4908141f61eSdrh if( pTab==0 ) continue; 4918141f61eSdrh assert( pTab->nCol>0 ); 4928141f61eSdrh if( zTab ){ 4938141f61eSdrh if( pItem->zAlias ){ 4948141f61eSdrh char *zTabName = pItem->zAlias; 4954adee20fSdanielk1977 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 4968141f61eSdrh }else{ 4978141f61eSdrh char *zTabName = pTab->zName; 4984adee20fSdanielk1977 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; 4994adee20fSdanielk1977 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 5008141f61eSdrh continue; 5018141f61eSdrh } 5028141f61eSdrh } 5038141f61eSdrh } 5048141f61eSdrh if( 0==(cntTab++) ){ 5058141f61eSdrh pExpr->iTable = pItem->iCursor; 5068141f61eSdrh pExpr->iDb = pTab->iDb; 5078141f61eSdrh } 5088141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 5094adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 5108141f61eSdrh cnt++; 5118141f61eSdrh pExpr->iTable = pItem->iCursor; 5128141f61eSdrh pExpr->iDb = pTab->iDb; 5138141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 5148141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 515a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 516a37cdde0Sdanielk1977 517a37cdde0Sdanielk1977 /* FIX ME: Expr::dataType will be removed... */ 518a37cdde0Sdanielk1977 pExpr->dataType = 519a37cdde0Sdanielk1977 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM); 5208141f61eSdrh break; 5218141f61eSdrh } 5228141f61eSdrh } 5238141f61eSdrh } 5248141f61eSdrh 5258141f61eSdrh /* If we have not already resolved the name, then maybe 5268141f61eSdrh ** it is a new.* or old.* trigger argument reference 5278141f61eSdrh */ 5288141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 5298141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 5308141f61eSdrh Table *pTab = 0; 5314adee20fSdanielk1977 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ 5328141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 5338141f61eSdrh assert( pTriggerStack->pTab ); 5348141f61eSdrh pTab = pTriggerStack->pTab; 5354adee20fSdanielk1977 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){ 5368141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 5378141f61eSdrh assert( pTriggerStack->pTab ); 5388141f61eSdrh pTab = pTriggerStack->pTab; 5398141f61eSdrh } 5408141f61eSdrh 5418141f61eSdrh if( pTab ){ 5428141f61eSdrh int j; 5438141f61eSdrh Column *pCol = pTab->aCol; 5448141f61eSdrh 5458141f61eSdrh pExpr->iDb = pTab->iDb; 5468141f61eSdrh cntTab++; 5478141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 5484adee20fSdanielk1977 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 5498141f61eSdrh cnt++; 5508141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 551a37cdde0Sdanielk1977 pExpr->affinity = pTab->aCol[j].affinity; 552a37cdde0Sdanielk1977 /* FIX ME: Expr::dataType will be removed... */ 553a37cdde0Sdanielk1977 pExpr->dataType = 554a37cdde0Sdanielk1977 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM); 5558141f61eSdrh break; 5568141f61eSdrh } 5578141f61eSdrh } 5588141f61eSdrh } 5598141f61eSdrh } 5608141f61eSdrh 5618141f61eSdrh /* 5628141f61eSdrh ** Perhaps the name is a reference to the ROWID 5638141f61eSdrh */ 5644adee20fSdanielk1977 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ 5658141f61eSdrh cnt = 1; 5668141f61eSdrh pExpr->iColumn = -1; 5678141f61eSdrh pExpr->dataType = SQLITE_SO_NUM; 568a37cdde0Sdanielk1977 pExpr->affinity = SQLITE_AFF_INTEGER; 5698141f61eSdrh } 5708141f61eSdrh 5718141f61eSdrh /* 5728141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 5738141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 5748141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 5758141f61eSdrh ** 5768141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 5778141f61eSdrh ** 5788141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 5798141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 5808141f61eSdrh ** Note that the expression in the result set should have already been 5818141f61eSdrh ** resolved by the time the WHERE clause is resolved. 5828141f61eSdrh */ 5838141f61eSdrh if( cnt==0 && pEList!=0 ){ 5848141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 5858141f61eSdrh char *zAs = pEList->a[j].zName; 5864adee20fSdanielk1977 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 5878141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 5888141f61eSdrh pExpr->op = TK_AS; 5898141f61eSdrh pExpr->iColumn = j; 5904adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr); 5918141f61eSdrh sqliteFree(zCol); 5928141f61eSdrh assert( zTab==0 && zDb==0 ); 5938141f61eSdrh return 0; 5948141f61eSdrh } 5958141f61eSdrh } 5968141f61eSdrh } 5978141f61eSdrh 5988141f61eSdrh /* 5998141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 6008141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 6018141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 6028141f61eSdrh ** case, we need to return right away and not make any changes to 6038141f61eSdrh ** pExpr. 6048141f61eSdrh */ 6058141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 6068141f61eSdrh sqliteFree(zCol); 6078141f61eSdrh return 0; 6088141f61eSdrh } 6098141f61eSdrh 6108141f61eSdrh /* 6118141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 6128141f61eSdrh ** more matches. Either way, we have an error. 6138141f61eSdrh */ 6148141f61eSdrh if( cnt!=1 ){ 6158141f61eSdrh char *z = 0; 6168141f61eSdrh char *zErr; 6178141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 6188141f61eSdrh if( zDb ){ 6194adee20fSdanielk1977 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0); 6208141f61eSdrh }else if( zTab ){ 6214adee20fSdanielk1977 sqlite3SetString(&z, zTab, ".", zCol, 0); 6228141f61eSdrh }else{ 6238141f61eSdrh z = sqliteStrDup(zCol); 6248141f61eSdrh } 6254adee20fSdanielk1977 sqlite3ErrorMsg(pParse, zErr, z); 6268141f61eSdrh sqliteFree(z); 6278141f61eSdrh } 6288141f61eSdrh 6298141f61eSdrh /* Clean up and return 6308141f61eSdrh */ 6318141f61eSdrh sqliteFree(zDb); 6328141f61eSdrh sqliteFree(zTab); 6338141f61eSdrh sqliteFree(zCol); 6344adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pLeft); 6358141f61eSdrh pExpr->pLeft = 0; 6364adee20fSdanielk1977 sqlite3ExprDelete(pExpr->pRight); 6378141f61eSdrh pExpr->pRight = 0; 6388141f61eSdrh pExpr->op = TK_COLUMN; 6394adee20fSdanielk1977 sqlite3AuthRead(pParse, pExpr, pSrcList); 6408141f61eSdrh return cnt!=1; 6418141f61eSdrh } 6428141f61eSdrh 6438141f61eSdrh /* 644cce7d176Sdrh ** This routine walks an expression tree and resolves references to 645967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 646aacc543eSdrh ** index to the table in the table list and a column offset. The 647aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 648aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 649832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 650aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 651aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 652aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 653aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 654aacc543eSdrh ** alias for ROWID. 65519a775c2Sdrh ** 656fef5208cSdrh ** We also check for instances of the IN operator. IN comes in two 657fef5208cSdrh ** forms: 658fef5208cSdrh ** 659fef5208cSdrh ** expr IN (exprlist) 660fef5208cSdrh ** and 661fef5208cSdrh ** expr IN (SELECT ...) 662fef5208cSdrh ** 663fef5208cSdrh ** The first form is handled by creating a set holding the list 664fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 665fef5208cSdrh ** a temporary table. 666fef5208cSdrh ** 667fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression. 66819a775c2Sdrh ** If it finds any, it generates code to write the value of that select 66919a775c2Sdrh ** into a memory cell. 670cce7d176Sdrh ** 671967e8b73Sdrh ** Unknown columns or tables provoke an error. The function returns 672cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg. 673cce7d176Sdrh */ 6744adee20fSdanielk1977 int sqlite3ExprResolveIds( 675a2e00042Sdrh Parse *pParse, /* The parser context */ 6768141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 677a2e00042Sdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 678a2e00042Sdrh Expr *pExpr /* The expression to be analyzed. */ 679a2e00042Sdrh ){ 6806a3ea0e6Sdrh int i; 6816a3ea0e6Sdrh 6828141f61eSdrh if( pExpr==0 || pSrcList==0 ) return 0; 6838141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 6848141f61eSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab ); 6856a3ea0e6Sdrh } 686cce7d176Sdrh switch( pExpr->op ){ 6872398937bSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 6882398937bSdrh ** possible. Otherwise they remain as strings. Single-quoted 6892398937bSdrh ** strings (ex: 'abc') are always string literals. 6902398937bSdrh */ 6912398937bSdrh case TK_STRING: { 6922398937bSdrh if( pExpr->token.z[0]=='\'' ) break; 6932398937bSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 6942398937bSdrh } 6958141f61eSdrh /* A lone identifier is the name of a columnd. 696a2e00042Sdrh */ 697cce7d176Sdrh case TK_ID: { 6988141f61eSdrh if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){ 699cce7d176Sdrh return 1; 700ed6c8671Sdrh } 701cce7d176Sdrh break; 702cce7d176Sdrh } 703cce7d176Sdrh 704d24cc427Sdrh /* A table name and column name: ID.ID 705d24cc427Sdrh ** Or a database, table and column: ID.ID.ID 706d24cc427Sdrh */ 707cce7d176Sdrh case TK_DOT: { 7088141f61eSdrh Token *pColumn; 7098141f61eSdrh Token *pTable; 7108141f61eSdrh Token *pDb; 7118141f61eSdrh Expr *pRight; 712cce7d176Sdrh 713cce7d176Sdrh pRight = pExpr->pRight; 714d24cc427Sdrh if( pRight->op==TK_ID ){ 7158141f61eSdrh pDb = 0; 7168141f61eSdrh pTable = &pExpr->pLeft->token; 7178141f61eSdrh pColumn = &pRight->token; 718d24cc427Sdrh }else{ 7198141f61eSdrh assert( pRight->op==TK_DOT ); 7208141f61eSdrh pDb = &pExpr->pLeft->token; 7218141f61eSdrh pTable = &pRight->pLeft->token; 7228141f61eSdrh pColumn = &pRight->pRight->token; 723d24cc427Sdrh } 7248141f61eSdrh if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){ 725daffd0e5Sdrh return 1; 726daffd0e5Sdrh } 727cce7d176Sdrh break; 728cce7d176Sdrh } 729cce7d176Sdrh 730fef5208cSdrh case TK_IN: { 7314adee20fSdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 732fef5208cSdrh if( v==0 ) return 1; 7334adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 734cfab11bcSdrh return 1; 735cfab11bcSdrh } 736fef5208cSdrh if( pExpr->pSelect ){ 737fef5208cSdrh /* Case 1: expr IN (SELECT ...) 738fef5208cSdrh ** 739fef5208cSdrh ** Generate code to write the results of the select into a temporary 7404794b980Sdrh ** table. The cursor number of the temporary table has already 7414adee20fSdanielk1977 ** been put in iTable by sqlite3ExprResolveInSelect(). 742fef5208cSdrh */ 743832508b7Sdrh pExpr->iTable = pParse->nTab++; 7444adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1); 7454adee20fSdanielk1977 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0); 746fef5208cSdrh }else if( pExpr->pList ){ 747fef5208cSdrh /* Case 2: expr IN (exprlist) 748fef5208cSdrh ** 749fef5208cSdrh ** Create a set to put the exprlist values in. The Set id is stored 750fef5208cSdrh ** in iTable. 751fef5208cSdrh */ 752fef5208cSdrh int i, iSet; 753fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 754fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 7554adee20fSdanielk1977 if( !sqlite3ExprIsConstant(pE2) ){ 7564adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 757da93d238Sdrh "right-hand side of IN operator must be constant"); 758fef5208cSdrh return 1; 759fef5208cSdrh } 7604adee20fSdanielk1977 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){ 7614794b980Sdrh return 1; 7624794b980Sdrh } 763fef5208cSdrh } 764fef5208cSdrh iSet = pExpr->iTable = pParse->nSet++; 765fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 766fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 767fef5208cSdrh switch( pE2->op ){ 768fef5208cSdrh case TK_FLOAT: 769fef5208cSdrh case TK_INTEGER: 770fef5208cSdrh case TK_STRING: { 771701a0aebSdrh int addr; 772a76b5dfcSdrh assert( pE2->token.z ); 7734adee20fSdanielk1977 addr = sqlite3VdbeOp3(v, OP_SetInsert, iSet, 0, 774701a0aebSdrh pE2->token.z, pE2->token.n); 7754adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, addr); 776fef5208cSdrh break; 777fef5208cSdrh } 778fef5208cSdrh default: { 7794adee20fSdanielk1977 sqlite3ExprCode(pParse, pE2); 7804adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetInsert, iSet, 0); 781fef5208cSdrh break; 782fef5208cSdrh } 783fef5208cSdrh } 784fef5208cSdrh } 785fef5208cSdrh } 786cfab11bcSdrh break; 787fef5208cSdrh } 788fef5208cSdrh 78919a775c2Sdrh case TK_SELECT: { 790fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 791fef5208cSdrh ** value of this select in a memory cell and record the number 792967e8b73Sdrh ** of the memory cell in iColumn. 793fef5208cSdrh */ 794967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 7954adee20fSdanielk1977 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){ 79619a775c2Sdrh return 1; 79719a775c2Sdrh } 79819a775c2Sdrh break; 79919a775c2Sdrh } 80019a775c2Sdrh 801cce7d176Sdrh /* For all else, just recursively walk the tree */ 802cce7d176Sdrh default: { 803cce7d176Sdrh if( pExpr->pLeft 8044adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 805cce7d176Sdrh return 1; 806cce7d176Sdrh } 807cce7d176Sdrh if( pExpr->pRight 8084adee20fSdanielk1977 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ 809cce7d176Sdrh return 1; 810cce7d176Sdrh } 811cce7d176Sdrh if( pExpr->pList ){ 812cce7d176Sdrh int i; 813cce7d176Sdrh ExprList *pList = pExpr->pList; 814cce7d176Sdrh for(i=0; i<pList->nExpr; i++){ 815832508b7Sdrh Expr *pArg = pList->a[i].pExpr; 8164adee20fSdanielk1977 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){ 817cce7d176Sdrh return 1; 818cce7d176Sdrh } 819cce7d176Sdrh } 820cce7d176Sdrh } 821cce7d176Sdrh } 822cce7d176Sdrh } 823cce7d176Sdrh return 0; 824cce7d176Sdrh } 825cce7d176Sdrh 826cce7d176Sdrh /* 8274b59ab5eSdrh ** pExpr is a node that defines a function of some kind. It might 8284b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function 8294b59ab5eSdrh ** that implements an operator, like "a LIKE b". 8304b59ab5eSdrh ** 8314b59ab5eSdrh ** This routine makes *pzName point to the name of the function and 8324b59ab5eSdrh ** *pnName hold the number of characters in the function name. 8334b59ab5eSdrh */ 8344b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 8354b59ab5eSdrh switch( pExpr->op ){ 8364b59ab5eSdrh case TK_FUNCTION: { 8374b59ab5eSdrh *pzName = pExpr->token.z; 8386977fea8Sdrh *pnName = pExpr->token.n; 8394b59ab5eSdrh break; 8404b59ab5eSdrh } 8414b59ab5eSdrh case TK_LIKE: { 8424b59ab5eSdrh *pzName = "like"; 8434b59ab5eSdrh *pnName = 4; 8444b59ab5eSdrh break; 8454b59ab5eSdrh } 8464b59ab5eSdrh case TK_GLOB: { 8474b59ab5eSdrh *pzName = "glob"; 8484b59ab5eSdrh *pnName = 4; 8494b59ab5eSdrh break; 8504b59ab5eSdrh } 8514b59ab5eSdrh default: { 8524b59ab5eSdrh *pzName = "can't happen"; 8534b59ab5eSdrh *pnName = 12; 8544b59ab5eSdrh break; 8554b59ab5eSdrh } 8564b59ab5eSdrh } 8574b59ab5eSdrh } 8584b59ab5eSdrh 8594b59ab5eSdrh /* 860cce7d176Sdrh ** Error check the functions in an expression. Make sure all 861cce7d176Sdrh ** function names are recognized and all functions have the correct 862cce7d176Sdrh ** number of arguments. Leave an error message in pParse->zErrMsg 863cce7d176Sdrh ** if anything is amiss. Return the number of errors. 864cce7d176Sdrh ** 865cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function 866cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 867cce7d176Sdrh */ 8684adee20fSdanielk1977 int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 869cce7d176Sdrh int nErr = 0; 870cce7d176Sdrh if( pExpr==0 ) return 0; 871cce7d176Sdrh switch( pExpr->op ){ 8724b59ab5eSdrh case TK_GLOB: 8734b59ab5eSdrh case TK_LIKE: 874cce7d176Sdrh case TK_FUNCTION: { 875c9b84a1fSdrh int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 876c9b84a1fSdrh int no_such_func = 0; /* True if no such function exists */ 877c9b84a1fSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 878c9b84a1fSdrh int is_agg = 0; /* True if is an aggregate function */ 879cce7d176Sdrh int i; 8804b59ab5eSdrh int nId; /* Number of characters in function name */ 8814b59ab5eSdrh const char *zId; /* The function name. */ 8820bce8354Sdrh FuncDef *pDef; 8830bce8354Sdrh 8844b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 8854adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0); 8860bce8354Sdrh if( pDef==0 ){ 8874adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0); 8880bce8354Sdrh if( pDef==0 ){ 889cce7d176Sdrh no_such_func = 1; 8908e0a2f90Sdrh }else{ 8918e0a2f90Sdrh wrong_num_args = 1; 8928e0a2f90Sdrh } 8938e0a2f90Sdrh }else{ 8940bce8354Sdrh is_agg = pDef->xFunc==0; 895cce7d176Sdrh } 8968e0a2f90Sdrh if( is_agg && !allowAgg ){ 8974adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); 8988e0a2f90Sdrh nErr++; 8998e0a2f90Sdrh is_agg = 0; 9008e0a2f90Sdrh }else if( no_such_func ){ 9014adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 902cce7d176Sdrh nErr++; 9038e0a2f90Sdrh }else if( wrong_num_args ){ 9044adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 905f7a9e1acSdrh nId, zId); 9068e0a2f90Sdrh nErr++; 907cce7d176Sdrh } 908f7a9e1acSdrh if( is_agg ){ 909f7a9e1acSdrh pExpr->op = TK_AGG_FUNCTION; 910f7a9e1acSdrh if( pIsAgg ) *pIsAgg = 1; 911f7a9e1acSdrh } 912cce7d176Sdrh for(i=0; nErr==0 && i<n; i++){ 9134adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr, 9144cfa7934Sdrh allowAgg && !is_agg, pIsAgg); 915cce7d176Sdrh } 916c9b84a1fSdrh if( pDef==0 ){ 917268380caSdrh /* Already reported an error */ 918c9b84a1fSdrh }else if( pDef->dataType>=0 ){ 919c9b84a1fSdrh if( pDef->dataType<n ){ 920c9b84a1fSdrh pExpr->dataType = 9214adee20fSdanielk1977 sqlite3ExprType(pExpr->pList->a[pDef->dataType].pExpr); 922c9b84a1fSdrh }else{ 923c9b84a1fSdrh pExpr->dataType = SQLITE_SO_NUM; 924c9b84a1fSdrh } 925c9b84a1fSdrh }else if( pDef->dataType==SQLITE_ARGS ){ 926c9b84a1fSdrh pDef->dataType = SQLITE_SO_TEXT; 927c9b84a1fSdrh for(i=0; i<n; i++){ 9284adee20fSdanielk1977 if( sqlite3ExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){ 929c9b84a1fSdrh pExpr->dataType = SQLITE_SO_NUM; 930c9b84a1fSdrh break; 931c9b84a1fSdrh } 932c9b84a1fSdrh } 933c9b84a1fSdrh }else if( pDef->dataType==SQLITE_NUMERIC ){ 934c9b84a1fSdrh pExpr->dataType = SQLITE_SO_NUM; 935c9b84a1fSdrh }else{ 936c9b84a1fSdrh pExpr->dataType = SQLITE_SO_TEXT; 937c9b84a1fSdrh } 938cce7d176Sdrh } 939cce7d176Sdrh default: { 940cce7d176Sdrh if( pExpr->pLeft ){ 9414adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 942cce7d176Sdrh } 943cce7d176Sdrh if( nErr==0 && pExpr->pRight ){ 9444adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 945cce7d176Sdrh } 946fef5208cSdrh if( nErr==0 && pExpr->pList ){ 947fef5208cSdrh int n = pExpr->pList->nExpr; 948fef5208cSdrh int i; 949fef5208cSdrh for(i=0; nErr==0 && i<n; i++){ 9502282792aSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 9514adee20fSdanielk1977 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg); 952fef5208cSdrh } 953fef5208cSdrh } 954cce7d176Sdrh break; 955cce7d176Sdrh } 956cce7d176Sdrh } 957cce7d176Sdrh return nErr; 958cce7d176Sdrh } 959cce7d176Sdrh 960cce7d176Sdrh /* 961c9b84a1fSdrh ** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the 962c9b84a1fSdrh ** given expression should sort as numeric values or as text. 963c9b84a1fSdrh ** 9644adee20fSdanielk1977 ** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have 965c9b84a1fSdrh ** both been called on the expression before it is passed to this routine. 966c9b84a1fSdrh */ 9674adee20fSdanielk1977 int sqlite3ExprType(Expr *p){ 968c9b84a1fSdrh if( p==0 ) return SQLITE_SO_NUM; 969c9b84a1fSdrh while( p ) switch( p->op ){ 970c9b84a1fSdrh case TK_PLUS: 971c9b84a1fSdrh case TK_MINUS: 972c9b84a1fSdrh case TK_STAR: 973c9b84a1fSdrh case TK_SLASH: 974c9b84a1fSdrh case TK_AND: 975c9b84a1fSdrh case TK_OR: 976c9b84a1fSdrh case TK_ISNULL: 977c9b84a1fSdrh case TK_NOTNULL: 978c9b84a1fSdrh case TK_NOT: 979c9b84a1fSdrh case TK_UMINUS: 9804b59ab5eSdrh case TK_UPLUS: 981c9b84a1fSdrh case TK_BITAND: 982c9b84a1fSdrh case TK_BITOR: 983c9b84a1fSdrh case TK_BITNOT: 984c9b84a1fSdrh case TK_LSHIFT: 985c9b84a1fSdrh case TK_RSHIFT: 986c9b84a1fSdrh case TK_REM: 987c9b84a1fSdrh case TK_INTEGER: 988c9b84a1fSdrh case TK_FLOAT: 989c9b84a1fSdrh case TK_IN: 990c9b84a1fSdrh case TK_BETWEEN: 9914b59ab5eSdrh case TK_GLOB: 9924b59ab5eSdrh case TK_LIKE: 993c9b84a1fSdrh return SQLITE_SO_NUM; 994c9b84a1fSdrh 995c9b84a1fSdrh case TK_STRING: 996c9b84a1fSdrh case TK_NULL: 997c9b84a1fSdrh case TK_CONCAT: 99850457896Sdrh case TK_VARIABLE: 999c9b84a1fSdrh return SQLITE_SO_TEXT; 1000c9b84a1fSdrh 1001c9b84a1fSdrh case TK_LT: 1002c9b84a1fSdrh case TK_LE: 1003c9b84a1fSdrh case TK_GT: 1004c9b84a1fSdrh case TK_GE: 1005c9b84a1fSdrh case TK_NE: 1006c9b84a1fSdrh case TK_EQ: 10074adee20fSdanielk1977 if( sqlite3ExprType(p->pLeft)==SQLITE_SO_NUM ){ 1008c9b84a1fSdrh return SQLITE_SO_NUM; 1009c9b84a1fSdrh } 1010c9b84a1fSdrh p = p->pRight; 1011c9b84a1fSdrh break; 1012c9b84a1fSdrh 1013c9b84a1fSdrh case TK_AS: 1014c9b84a1fSdrh p = p->pLeft; 1015c9b84a1fSdrh break; 1016c9b84a1fSdrh 1017c9b84a1fSdrh case TK_COLUMN: 1018c9b84a1fSdrh case TK_FUNCTION: 1019c9b84a1fSdrh case TK_AGG_FUNCTION: 1020c9b84a1fSdrh return p->dataType; 1021c9b84a1fSdrh 1022c9b84a1fSdrh case TK_SELECT: 1023c9b84a1fSdrh assert( p->pSelect ); 1024c9b84a1fSdrh assert( p->pSelect->pEList ); 1025c9b84a1fSdrh assert( p->pSelect->pEList->nExpr>0 ); 1026c9b84a1fSdrh p = p->pSelect->pEList->a[0].pExpr; 1027c9b84a1fSdrh break; 1028c9b84a1fSdrh 1029b1363206Sdrh case TK_CASE: { 10304adee20fSdanielk1977 if( p->pRight && sqlite3ExprType(p->pRight)==SQLITE_SO_NUM ){ 1031b1363206Sdrh return SQLITE_SO_NUM; 1032b1363206Sdrh } 1033b1363206Sdrh if( p->pList ){ 1034b1363206Sdrh int i; 1035b1363206Sdrh ExprList *pList = p->pList; 1036b1363206Sdrh for(i=1; i<pList->nExpr; i+=2){ 10374adee20fSdanielk1977 if( sqlite3ExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){ 1038b1363206Sdrh return SQLITE_SO_NUM; 1039b1363206Sdrh } 1040b1363206Sdrh } 1041b1363206Sdrh } 1042b1363206Sdrh return SQLITE_SO_TEXT; 1043b1363206Sdrh } 1044b1363206Sdrh 1045c9b84a1fSdrh default: 1046c9b84a1fSdrh assert( p->op==TK_ABORT ); /* Can't Happen */ 1047c9b84a1fSdrh break; 1048c9b84a1fSdrh } 1049c9b84a1fSdrh return SQLITE_SO_NUM; 1050c9b84a1fSdrh } 1051c9b84a1fSdrh 1052c9b84a1fSdrh /* 1053cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 10541ccde15dSdrh ** expression and leave the result on the top of stack. 1055cce7d176Sdrh */ 10564adee20fSdanielk1977 void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ 1057cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1058cce7d176Sdrh int op; 1059daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1060cce7d176Sdrh switch( pExpr->op ){ 1061cce7d176Sdrh case TK_PLUS: op = OP_Add; break; 1062cce7d176Sdrh case TK_MINUS: op = OP_Subtract; break; 1063cce7d176Sdrh case TK_STAR: op = OP_Multiply; break; 1064cce7d176Sdrh case TK_SLASH: op = OP_Divide; break; 1065cce7d176Sdrh case TK_AND: op = OP_And; break; 1066cce7d176Sdrh case TK_OR: op = OP_Or; break; 1067cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1068cce7d176Sdrh case TK_LE: op = OP_Le; break; 1069cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1070cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1071cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1072cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1073cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1074cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1075cce7d176Sdrh case TK_NOT: op = OP_Not; break; 1076cce7d176Sdrh case TK_UMINUS: op = OP_Negative; break; 1077bf4133cbSdrh case TK_BITAND: op = OP_BitAnd; break; 1078bf4133cbSdrh case TK_BITOR: op = OP_BitOr; break; 1079bf4133cbSdrh case TK_BITNOT: op = OP_BitNot; break; 1080bf4133cbSdrh case TK_LSHIFT: op = OP_ShiftLeft; break; 1081bf4133cbSdrh case TK_RSHIFT: op = OP_ShiftRight; break; 1082bf4133cbSdrh case TK_REM: op = OP_Remainder; break; 1083cce7d176Sdrh default: break; 1084cce7d176Sdrh } 1085cce7d176Sdrh switch( pExpr->op ){ 1086967e8b73Sdrh case TK_COLUMN: { 10872282792aSdrh if( pParse->useAgg ){ 10884adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1089c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 10904adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1091c4a3c779Sdrh }else{ 10924adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 10932282792aSdrh } 1094cce7d176Sdrh break; 1095cce7d176Sdrh } 109651e9a445Sdrh case TK_STRING: 109751e9a445Sdrh case TK_FLOAT: 1098cce7d176Sdrh case TK_INTEGER: { 10994adee20fSdanielk1977 if( pExpr->op==TK_INTEGER && sqlite3FitsIn32Bits(pExpr->token.z) ){ 11004adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0); 1101f9dd2c2eSdanielk1977 }else if( pExpr->op==TK_FLOAT || pExpr->op==TK_INTEGER ){ 1102a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, OP_Real, 0, 0); 110351e9a445Sdrh }else{ 11044adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 110551e9a445Sdrh } 1106a76b5dfcSdrh assert( pExpr->token.z ); 11074adee20fSdanielk1977 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 11084adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 1109cce7d176Sdrh break; 1110cce7d176Sdrh } 1111cce7d176Sdrh case TK_NULL: { 11124adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 1113cce7d176Sdrh break; 1114cce7d176Sdrh } 111550457896Sdrh case TK_VARIABLE: { 11164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 111750457896Sdrh break; 111850457896Sdrh } 1119c9b84a1fSdrh case TK_LT: 1120c9b84a1fSdrh case TK_LE: 1121c9b84a1fSdrh case TK_GT: 1122c9b84a1fSdrh case TK_GE: 1123c9b84a1fSdrh case TK_NE: 1124c9b84a1fSdrh case TK_EQ: { 1125a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0); 1126a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1127a37cdde0Sdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1128a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, 0); 1129a37cdde0Sdanielk1977 break; 1130a37cdde0Sdanielk1977 #if 0 11313d68f03aSdanielk1977 if( sqlite3ExprType(pExpr)==SQLITE_SO_TEXT ){ 1132c9b84a1fSdrh op += 6; /* Convert numeric opcodes to text opcodes */ 1133c9b84a1fSdrh } 1134c9b84a1fSdrh /* Fall through into the next case */ 1135a37cdde0Sdanielk1977 #endif 1136c9b84a1fSdrh } 1137cce7d176Sdrh case TK_AND: 1138cce7d176Sdrh case TK_OR: 1139cce7d176Sdrh case TK_PLUS: 1140cce7d176Sdrh case TK_STAR: 1141cce7d176Sdrh case TK_MINUS: 1142bf4133cbSdrh case TK_REM: 1143bf4133cbSdrh case TK_BITAND: 1144bf4133cbSdrh case TK_BITOR: 1145c9b84a1fSdrh case TK_SLASH: { 11464adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11474adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11484adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1149cce7d176Sdrh break; 1150cce7d176Sdrh } 1151bf4133cbSdrh case TK_LSHIFT: 1152bf4133cbSdrh case TK_RSHIFT: { 11534adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11544adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11554adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1156bf4133cbSdrh break; 1157bf4133cbSdrh } 11580040077dSdrh case TK_CONCAT: { 11594adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11604adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 11614adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Concat, 2, 0); 11620040077dSdrh break; 11630040077dSdrh } 1164cce7d176Sdrh case TK_UMINUS: { 11656e142f54Sdrh assert( pExpr->pLeft ); 11667a7c7390Sdrh if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){ 11676e142f54Sdrh Token *p = &pExpr->pLeft->token; 11686e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 11696e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 11704adee20fSdanielk1977 if( pExpr->pLeft->op==TK_INTEGER && sqlite3FitsIn32Bits(z) ){ 11714adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, atoi(z), 0); 1172e6840900Sdrh }else{ 1173*93edea93Sdanielk1977 sqlite3VdbeAddOp(v, OP_Real, 0, 0); 1174e6840900Sdrh } 11754adee20fSdanielk1977 sqlite3VdbeChangeP3(v, -1, z, p->n+1); 11766e142f54Sdrh sqliteFree(z); 11776e142f54Sdrh break; 11786e142f54Sdrh } 11791ccde15dSdrh /* Fall through into TK_NOT */ 11806e142f54Sdrh } 1181bf4133cbSdrh case TK_BITNOT: 11826e142f54Sdrh case TK_NOT: { 11834adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11844adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 0, 0); 1185cce7d176Sdrh break; 1186cce7d176Sdrh } 1187cce7d176Sdrh case TK_ISNULL: 1188cce7d176Sdrh case TK_NOTNULL: { 1189cce7d176Sdrh int dest; 11904adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 11914adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 11924adee20fSdanielk1977 dest = sqlite3VdbeCurrentAddr(v) + 2; 11934adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 11944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1195cce7d176Sdrh } 1196a37cdde0Sdanielk1977 break; 11972282792aSdrh case TK_AGG_FUNCTION: { 11984adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 11992282792aSdrh break; 12002282792aSdrh } 12014b59ab5eSdrh case TK_GLOB: 12024b59ab5eSdrh case TK_LIKE: 1203cce7d176Sdrh case TK_FUNCTION: { 1204cce7d176Sdrh ExprList *pList = pExpr->pList; 120589425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 12060bce8354Sdrh FuncDef *pDef; 12074b59ab5eSdrh int nId; 12084b59ab5eSdrh const char *zId; 12094b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 12104adee20fSdanielk1977 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0); 12110bce8354Sdrh assert( pDef!=0 ); 12124adee20fSdanielk1977 nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes); 1213a37cdde0Sdanielk1977 /* FIX ME: The following is a temporary hack. */ 1214a37cdde0Sdanielk1977 if( 0==sqlite3StrNICmp(zId, "classof", nId) ){ 1215a37cdde0Sdanielk1977 assert( nExpr==1 ); 1216a37cdde0Sdanielk1977 sqlite3VdbeOp3(v, OP_Class, nExpr, 0, 0, 0); 1217a37cdde0Sdanielk1977 }else{ 12184adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER); 1219a37cdde0Sdanielk1977 } 12206ec2733bSdrh break; 12216ec2733bSdrh } 122219a775c2Sdrh case TK_SELECT: { 12234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 122419a775c2Sdrh break; 122519a775c2Sdrh } 1226fef5208cSdrh case TK_IN: { 1227fef5208cSdrh int addr; 12284adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 1, 0); 12294adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12304adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 12314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); 12324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 2, 0); 12334adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 12344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+6); 1235fef5208cSdrh if( pExpr->pSelect ){ 12364adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+6); 1237fef5208cSdrh }else{ 12384adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6); 1239fef5208cSdrh } 12404adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); 1241fef5208cSdrh break; 1242fef5208cSdrh } 1243fef5208cSdrh case TK_BETWEEN: { 12444adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 12454adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 12464adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 12474adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, 0, 0); 12484adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); 12494adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 12504adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, 0, 0); 12514adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_And, 0, 0); 1252fef5208cSdrh break; 1253fef5208cSdrh } 125451e9a445Sdrh case TK_UPLUS: 1255a2e00042Sdrh case TK_AS: { 12564adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1257a2e00042Sdrh break; 1258a2e00042Sdrh } 125917a7f8ddSdrh case TK_CASE: { 126017a7f8ddSdrh int expr_end_label; 1261f5905aa7Sdrh int jumpInst; 1262f5905aa7Sdrh int addr; 1263f5905aa7Sdrh int nExpr; 126417a7f8ddSdrh int i; 126517a7f8ddSdrh 126617a7f8ddSdrh assert(pExpr->pList); 126717a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 126817a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1269f5905aa7Sdrh nExpr = pExpr->pList->nExpr; 12704adee20fSdanielk1977 expr_end_label = sqlite3VdbeMakeLabel(v); 127117a7f8ddSdrh if( pExpr->pLeft ){ 12724adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 1273cce7d176Sdrh } 1274f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 12754adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr); 127617a7f8ddSdrh if( pExpr->pLeft ){ 12774adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 1, 1); 12784adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0); 12794adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1280f5905aa7Sdrh }else{ 12814adee20fSdanielk1977 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); 128217a7f8ddSdrh } 12834adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr); 12844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); 12854adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 12864adee20fSdanielk1977 sqlite3VdbeChangeP2(v, jumpInst, addr); 128717a7f8ddSdrh } 1288f570f011Sdrh if( pExpr->pLeft ){ 12894adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1290f570f011Sdrh } 129117a7f8ddSdrh if( pExpr->pRight ){ 12924adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 129317a7f8ddSdrh }else{ 12944adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_String, 0, 0); 129517a7f8ddSdrh } 12964adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, expr_end_label); 12976f34903eSdanielk1977 break; 12986f34903eSdanielk1977 } 12996f34903eSdanielk1977 case TK_RAISE: { 13006f34903eSdanielk1977 if( !pParse->trigStack ){ 13014adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1302da93d238Sdrh "RAISE() may only be used within a trigger-program"); 13036f34903eSdanielk1977 pParse->nErr++; 13046f34903eSdanielk1977 return; 13056f34903eSdanielk1977 } 13066f34903eSdanielk1977 if( pExpr->iColumn == OE_Rollback || 13076f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 13086f34903eSdanielk1977 pExpr->iColumn == OE_Fail ){ 13094adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1310701a0aebSdrh pExpr->token.z, pExpr->token.n); 13114adee20fSdanielk1977 sqlite3VdbeDequoteP3(v, -1); 13126f34903eSdanielk1977 } else { 13136f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 13144adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump, 1315701a0aebSdrh "(IGNORE jump)", 0); 13166f34903eSdanielk1977 } 131717a7f8ddSdrh } 131817a7f8ddSdrh break; 131917a7f8ddSdrh } 1320cce7d176Sdrh } 1321cce7d176Sdrh 1322cce7d176Sdrh /* 1323268380caSdrh ** Generate code that pushes the value of every element of the given 1324268380caSdrh ** expression list onto the stack. If the includeTypes flag is true, 1325268380caSdrh ** then also push a string that is the datatype of each element onto 1326268380caSdrh ** the stack after the value. 1327268380caSdrh ** 1328268380caSdrh ** Return the number of elements pushed onto the stack. 1329268380caSdrh */ 13304adee20fSdanielk1977 int sqlite3ExprCodeExprList( 1331268380caSdrh Parse *pParse, /* Parsing context */ 1332268380caSdrh ExprList *pList, /* The expression list to be coded */ 1333268380caSdrh int includeTypes /* TRUE to put datatypes on the stack too */ 1334268380caSdrh ){ 1335268380caSdrh struct ExprList_item *pItem; 1336268380caSdrh int i, n; 1337268380caSdrh Vdbe *v; 1338268380caSdrh if( pList==0 ) return 0; 13394adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1340268380caSdrh n = pList->nExpr; 1341268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 13424adee20fSdanielk1977 sqlite3ExprCode(pParse, pItem->pExpr); 1343268380caSdrh if( includeTypes ){ 13444adee20fSdanielk1977 sqlite3VdbeOp3(v, OP_String, 0, 0, 13454adee20fSdanielk1977 sqlite3ExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text", 1346268380caSdrh P3_STATIC); 1347268380caSdrh } 1348268380caSdrh } 1349268380caSdrh return includeTypes ? n*2 : n; 1350268380caSdrh } 1351268380caSdrh 1352268380caSdrh /* 1353cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1354cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1355cce7d176Sdrh ** continues straight thru if the expression is false. 1356f5905aa7Sdrh ** 1357f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1358f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1359cce7d176Sdrh */ 13604adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1361cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1362cce7d176Sdrh int op = 0; 1363daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1364cce7d176Sdrh switch( pExpr->op ){ 1365cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1366cce7d176Sdrh case TK_LE: op = OP_Le; break; 1367cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1368cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1369cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1370cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1371cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1372cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1373cce7d176Sdrh default: break; 1374cce7d176Sdrh } 1375cce7d176Sdrh switch( pExpr->op ){ 1376cce7d176Sdrh case TK_AND: { 13774adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 13784adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 13794adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 13804adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1381cce7d176Sdrh break; 1382cce7d176Sdrh } 1383cce7d176Sdrh case TK_OR: { 13844adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 13854adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1386cce7d176Sdrh break; 1387cce7d176Sdrh } 1388cce7d176Sdrh case TK_NOT: { 13894adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1390cce7d176Sdrh break; 1391cce7d176Sdrh } 1392cce7d176Sdrh case TK_LT: 1393cce7d176Sdrh case TK_LE: 1394cce7d176Sdrh case TK_GT: 1395cce7d176Sdrh case TK_GE: 1396cce7d176Sdrh case TK_NE: 13970ac65892Sdrh case TK_EQ: { 1398a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 13994adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14004adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1401a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1402cce7d176Sdrh break; 1403cce7d176Sdrh } 1404cce7d176Sdrh case TK_ISNULL: 1405cce7d176Sdrh case TK_NOTNULL: { 14064adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14074adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1408cce7d176Sdrh break; 1409cce7d176Sdrh } 1410fef5208cSdrh case TK_IN: { 1411f5905aa7Sdrh int addr; 14124adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14134adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 14144adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3); 14154adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 14164adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1417fef5208cSdrh if( pExpr->pSelect ){ 14184adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, dest); 1419fef5208cSdrh }else{ 14204adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetFound, pExpr->iTable, dest); 1421fef5208cSdrh } 1422fef5208cSdrh break; 1423fef5208cSdrh } 1424fef5208cSdrh case TK_BETWEEN: { 1425f5905aa7Sdrh int addr; 14264adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 14284adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 14294adee20fSdanielk1977 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0); 14304adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 14314adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest); 14324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 14334adee20fSdanielk1977 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v)); 14344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 1435fef5208cSdrh break; 1436fef5208cSdrh } 1437cce7d176Sdrh default: { 14384adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 14394adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); 1440cce7d176Sdrh break; 1441cce7d176Sdrh } 1442cce7d176Sdrh } 1443cce7d176Sdrh } 1444cce7d176Sdrh 1445cce7d176Sdrh /* 144666b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1447cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1448cce7d176Sdrh ** continues straight thru if the expression is true. 1449f5905aa7Sdrh ** 1450f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1451f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1452cce7d176Sdrh */ 14534adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1454cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1455cce7d176Sdrh int op = 0; 1456daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1457cce7d176Sdrh switch( pExpr->op ){ 1458cce7d176Sdrh case TK_LT: op = OP_Ge; break; 1459cce7d176Sdrh case TK_LE: op = OP_Gt; break; 1460cce7d176Sdrh case TK_GT: op = OP_Le; break; 1461cce7d176Sdrh case TK_GE: op = OP_Lt; break; 1462cce7d176Sdrh case TK_NE: op = OP_Eq; break; 1463cce7d176Sdrh case TK_EQ: op = OP_Ne; break; 1464cce7d176Sdrh case TK_ISNULL: op = OP_NotNull; break; 1465cce7d176Sdrh case TK_NOTNULL: op = OP_IsNull; break; 1466cce7d176Sdrh default: break; 1467cce7d176Sdrh } 1468cce7d176Sdrh switch( pExpr->op ){ 1469cce7d176Sdrh case TK_AND: { 14704adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 14714adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1472cce7d176Sdrh break; 1473cce7d176Sdrh } 1474cce7d176Sdrh case TK_OR: { 14754adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 14764adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 14774adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 14784adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 1479cce7d176Sdrh break; 1480cce7d176Sdrh } 1481cce7d176Sdrh case TK_NOT: { 14824adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1483cce7d176Sdrh break; 1484cce7d176Sdrh } 1485cce7d176Sdrh case TK_LT: 1486cce7d176Sdrh case TK_LE: 1487cce7d176Sdrh case TK_GT: 1488cce7d176Sdrh case TK_GE: 1489cce7d176Sdrh case TK_NE: 1490cce7d176Sdrh case TK_EQ: { 1491a37cdde0Sdanielk1977 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull); 14924adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 14934adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pRight); 1494a37cdde0Sdanielk1977 sqlite3VdbeAddOp(v, op, p1, dest); 1495cce7d176Sdrh break; 1496cce7d176Sdrh } 1497cce7d176Sdrh case TK_ISNULL: 1498cce7d176Sdrh case TK_NOTNULL: { 14994adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15004adee20fSdanielk1977 sqlite3VdbeAddOp(v, op, 1, dest); 1501cce7d176Sdrh break; 1502cce7d176Sdrh } 1503fef5208cSdrh case TK_IN: { 1504f5905aa7Sdrh int addr; 15054adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15064adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15074adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3); 15084adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15094adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1510fef5208cSdrh if( pExpr->pSelect ){ 15114adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); 1512fef5208cSdrh }else{ 15134adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); 1514fef5208cSdrh } 1515fef5208cSdrh break; 1516fef5208cSdrh } 1517fef5208cSdrh case TK_BETWEEN: { 1518fef5208cSdrh int addr; 15194adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pLeft); 15204adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); 15214adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr); 15224adee20fSdanielk1977 addr = sqlite3VdbeCurrentAddr(v); 15234adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); 15244adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 15254adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Goto, 0, dest); 15264adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr); 15274adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest); 1528fef5208cSdrh break; 1529fef5208cSdrh } 1530cce7d176Sdrh default: { 15314adee20fSdanielk1977 sqlite3ExprCode(pParse, pExpr); 15324adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1533cce7d176Sdrh break; 1534cce7d176Sdrh } 1535cce7d176Sdrh } 1536cce7d176Sdrh } 15372282792aSdrh 15382282792aSdrh /* 15392282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 15402282792aSdrh ** if they are identical and return FALSE if they differ in any way. 15412282792aSdrh */ 15424adee20fSdanielk1977 int sqlite3ExprCompare(Expr *pA, Expr *pB){ 15432282792aSdrh int i; 15442282792aSdrh if( pA==0 ){ 15452282792aSdrh return pB==0; 15462282792aSdrh }else if( pB==0 ){ 15472282792aSdrh return 0; 15482282792aSdrh } 15492282792aSdrh if( pA->op!=pB->op ) return 0; 15504adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; 15514adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; 15522282792aSdrh if( pA->pList ){ 15532282792aSdrh if( pB->pList==0 ) return 0; 15542282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 15552282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 15564adee20fSdanielk1977 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 15572282792aSdrh return 0; 15582282792aSdrh } 15592282792aSdrh } 15602282792aSdrh }else if( pB->pList ){ 15612282792aSdrh return 0; 15622282792aSdrh } 15632282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 15642f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 15652282792aSdrh if( pA->token.z ){ 15662282792aSdrh if( pB->token.z==0 ) return 0; 15676977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 15684adee20fSdanielk1977 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 15692282792aSdrh } 15702282792aSdrh return 1; 15712282792aSdrh } 15722282792aSdrh 15732282792aSdrh /* 15742282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 15752282792aSdrh */ 15762282792aSdrh static int appendAggInfo(Parse *pParse){ 15772282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 15782282792aSdrh int amt = pParse->nAgg + 8; 15796d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 15806d4abfbeSdrh if( aAgg==0 ){ 15812282792aSdrh return -1; 15822282792aSdrh } 15836d4abfbeSdrh pParse->aAgg = aAgg; 15842282792aSdrh } 15852282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 15862282792aSdrh return pParse->nAgg++; 15872282792aSdrh } 15882282792aSdrh 15892282792aSdrh /* 15902282792aSdrh ** Analyze the given expression looking for aggregate functions and 15912282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 15922282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 15932282792aSdrh ** 15942282792aSdrh ** This routine should only be called after the expression has been 15954adee20fSdanielk1977 ** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck(). 15962282792aSdrh ** 15972282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return 15982282792aSdrh ** the number of errors. 15992282792aSdrh */ 16004adee20fSdanielk1977 int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 16012282792aSdrh int i; 16022282792aSdrh AggExpr *aAgg; 16032282792aSdrh int nErr = 0; 16042282792aSdrh 16052282792aSdrh if( pExpr==0 ) return 0; 16062282792aSdrh switch( pExpr->op ){ 1607967e8b73Sdrh case TK_COLUMN: { 16082282792aSdrh aAgg = pParse->aAgg; 16092282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16102282792aSdrh if( aAgg[i].isAgg ) continue; 16112282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 1612967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 16132282792aSdrh break; 16142282792aSdrh } 16152282792aSdrh } 16162282792aSdrh if( i>=pParse->nAgg ){ 16172282792aSdrh i = appendAggInfo(pParse); 16182282792aSdrh if( i<0 ) return 1; 16192282792aSdrh pParse->aAgg[i].isAgg = 0; 16202282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16212282792aSdrh } 1622aaf88729Sdrh pExpr->iAgg = i; 16232282792aSdrh break; 16242282792aSdrh } 16252282792aSdrh case TK_AGG_FUNCTION: { 16262282792aSdrh aAgg = pParse->aAgg; 16272282792aSdrh for(i=0; i<pParse->nAgg; i++){ 16282282792aSdrh if( !aAgg[i].isAgg ) continue; 16294adee20fSdanielk1977 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){ 16302282792aSdrh break; 16312282792aSdrh } 16322282792aSdrh } 16332282792aSdrh if( i>=pParse->nAgg ){ 16342282792aSdrh i = appendAggInfo(pParse); 16352282792aSdrh if( i<0 ) return 1; 16362282792aSdrh pParse->aAgg[i].isAgg = 1; 16372282792aSdrh pParse->aAgg[i].pExpr = pExpr; 16384adee20fSdanielk1977 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db, 16396977fea8Sdrh pExpr->token.z, pExpr->token.n, 1640f55f25f0Sdrh pExpr->pList ? pExpr->pList->nExpr : 0, 0); 16412282792aSdrh } 16422282792aSdrh pExpr->iAgg = i; 16432282792aSdrh break; 16442282792aSdrh } 16452282792aSdrh default: { 16462282792aSdrh if( pExpr->pLeft ){ 16474adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft); 16482282792aSdrh } 16492282792aSdrh if( nErr==0 && pExpr->pRight ){ 16504adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight); 16512282792aSdrh } 16522282792aSdrh if( nErr==0 && pExpr->pList ){ 16532282792aSdrh int n = pExpr->pList->nExpr; 16542282792aSdrh int i; 16552282792aSdrh for(i=0; nErr==0 && i<n; i++){ 16564adee20fSdanielk1977 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 16572282792aSdrh } 16582282792aSdrh } 16592282792aSdrh break; 16602282792aSdrh } 16612282792aSdrh } 16622282792aSdrh return nErr; 16632282792aSdrh } 16648e0a2f90Sdrh 16658e0a2f90Sdrh /* 16668e0a2f90Sdrh ** Locate a user function given a name and a number of arguments. 16670bce8354Sdrh ** Return a pointer to the FuncDef structure that defines that 16688e0a2f90Sdrh ** function, or return NULL if the function does not exist. 16698e0a2f90Sdrh ** 16700bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef 16718e0a2f90Sdrh ** structure is created and liked into the "db" structure if a 16728e0a2f90Sdrh ** no matching function previously existed. When createFlag is true 16738e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts 16748e0a2f90Sdrh ** any number of arguments will be returned. 16758e0a2f90Sdrh ** 16768e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid 16778e0a2f90Sdrh ** function found is returned. A function is valid if either xFunc 16788e0a2f90Sdrh ** or xStep is non-zero. 16798e0a2f90Sdrh */ 16804adee20fSdanielk1977 FuncDef *sqlite3FindFunction( 16818e0a2f90Sdrh sqlite *db, /* An open database */ 16828e0a2f90Sdrh const char *zName, /* Name of the function. Not null-terminated */ 16838e0a2f90Sdrh int nName, /* Number of characters in the name */ 16848e0a2f90Sdrh int nArg, /* Number of arguments. -1 means any number */ 16858e0a2f90Sdrh int createFlag /* Create new entry if true and does not otherwise exist */ 16868e0a2f90Sdrh ){ 16870bce8354Sdrh FuncDef *pFirst, *p, *pMaybe; 16884adee20fSdanielk1977 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName); 16891350b030Sdrh if( p && !createFlag && nArg<0 ){ 16908e0a2f90Sdrh while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } 16918e0a2f90Sdrh return p; 16928e0a2f90Sdrh } 16938e0a2f90Sdrh pMaybe = 0; 16948e0a2f90Sdrh while( p && p->nArg!=nArg ){ 16958e0a2f90Sdrh if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; 16968e0a2f90Sdrh p = p->pNext; 16978e0a2f90Sdrh } 16988e0a2f90Sdrh if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ 16998e0a2f90Sdrh return 0; 17008e0a2f90Sdrh } 17018e0a2f90Sdrh if( p==0 && pMaybe ){ 17028e0a2f90Sdrh assert( createFlag==0 ); 17038e0a2f90Sdrh return pMaybe; 17048e0a2f90Sdrh } 170589425d5eSdrh if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){ 17068e0a2f90Sdrh p->nArg = nArg; 17078e0a2f90Sdrh p->pNext = pFirst; 1708c9b84a1fSdrh p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC; 17094adee20fSdanielk1977 sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p); 17108e0a2f90Sdrh } 17118e0a2f90Sdrh return p; 17128e0a2f90Sdrh } 17134adee20fSdanielk1977 17144adee20fSdanielk1977 17154adee20fSdanielk1977 1716