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*268380caSdrh ** $Id: expr.c,v 1.112 2004/02/25 13:47:31 drh Exp $ 16cce7d176Sdrh */ 17cce7d176Sdrh #include "sqliteInt.h" 1804738cb9Sdrh #include <ctype.h> 19a2e00042Sdrh 20a2e00042Sdrh /* 21a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 22a76b5dfcSdrh ** for this node is obtained from sqliteMalloc(). The calling function 23a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 24a76b5dfcSdrh */ 25a76b5dfcSdrh Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ 26a76b5dfcSdrh Expr *pNew; 27a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 28a76b5dfcSdrh if( pNew==0 ){ 294efc4754Sdrh /* When malloc fails, we leak memory from pLeft and pRight */ 30a76b5dfcSdrh return 0; 31a76b5dfcSdrh } 32a76b5dfcSdrh pNew->op = op; 33a76b5dfcSdrh pNew->pLeft = pLeft; 34a76b5dfcSdrh pNew->pRight = pRight; 35a76b5dfcSdrh if( pToken ){ 364b59ab5eSdrh assert( pToken->dyn==0 ); 37a76b5dfcSdrh pNew->token = *pToken; 386977fea8Sdrh pNew->span = *pToken; 39a76b5dfcSdrh }else{ 404efc4754Sdrh assert( pNew->token.dyn==0 ); 414efc4754Sdrh assert( pNew->token.z==0 ); 424efc4754Sdrh assert( pNew->token.n==0 ); 436977fea8Sdrh if( pLeft && pRight ){ 446977fea8Sdrh sqliteExprSpan(pNew, &pLeft->span, &pRight->span); 456977fea8Sdrh }else{ 466977fea8Sdrh pNew->span = pNew->token; 476977fea8Sdrh } 48a76b5dfcSdrh } 49a76b5dfcSdrh return pNew; 50a76b5dfcSdrh } 51a76b5dfcSdrh 52a76b5dfcSdrh /* 536977fea8Sdrh ** Set the Expr.span field of the given expression to span all 54a76b5dfcSdrh ** text between the two given tokens. 55a76b5dfcSdrh */ 56a76b5dfcSdrh void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ 574efc4754Sdrh assert( pRight!=0 ); 584efc4754Sdrh assert( pLeft!=0 ); 594efc4754Sdrh /* Note: pExpr might be NULL due to a prior malloc failure */ 604efc4754Sdrh if( pExpr && pRight->z && pLeft->z ){ 614b59ab5eSdrh if( pLeft->dyn==0 && pRight->dyn==0 ){ 626977fea8Sdrh pExpr->span.z = pLeft->z; 636977fea8Sdrh pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); 644b59ab5eSdrh }else{ 656977fea8Sdrh pExpr->span.z = 0; 664b59ab5eSdrh } 67a76b5dfcSdrh } 68a76b5dfcSdrh } 69a76b5dfcSdrh 70a76b5dfcSdrh /* 71a76b5dfcSdrh ** Construct a new expression node for a function with multiple 72a76b5dfcSdrh ** arguments. 73a76b5dfcSdrh */ 74a76b5dfcSdrh Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ 75a76b5dfcSdrh Expr *pNew; 76a76b5dfcSdrh pNew = sqliteMalloc( sizeof(Expr) ); 77a76b5dfcSdrh if( pNew==0 ){ 784efc4754Sdrh /* sqliteExprListDelete(pList); // Leak pList when malloc fails */ 79a76b5dfcSdrh return 0; 80a76b5dfcSdrh } 81a76b5dfcSdrh pNew->op = TK_FUNCTION; 82a76b5dfcSdrh pNew->pList = pList; 83a76b5dfcSdrh if( pToken ){ 844b59ab5eSdrh assert( pToken->dyn==0 ); 85a76b5dfcSdrh pNew->token = *pToken; 86a76b5dfcSdrh }else{ 87a76b5dfcSdrh pNew->token.z = 0; 88a76b5dfcSdrh } 896977fea8Sdrh pNew->span = pNew->token; 90a76b5dfcSdrh return pNew; 91a76b5dfcSdrh } 92a76b5dfcSdrh 93a76b5dfcSdrh /* 94a2e00042Sdrh ** Recursively delete an expression tree. 95a2e00042Sdrh */ 96a2e00042Sdrh void sqliteExprDelete(Expr *p){ 97a2e00042Sdrh if( p==0 ) return; 984efc4754Sdrh if( p->span.dyn ) sqliteFree((char*)p->span.z); 994efc4754Sdrh if( p->token.dyn ) sqliteFree((char*)p->token.z); 1004efc4754Sdrh sqliteExprDelete(p->pLeft); 1014efc4754Sdrh sqliteExprDelete(p->pRight); 1024efc4754Sdrh sqliteExprListDelete(p->pList); 1034efc4754Sdrh sqliteSelectDelete(p->pSelect); 104a2e00042Sdrh sqliteFree(p); 105a2e00042Sdrh } 106a2e00042Sdrh 107a76b5dfcSdrh 108a76b5dfcSdrh /* 109ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 110ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 111ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 112ff78bd2fSdrh ** without effecting the originals. 113ff78bd2fSdrh ** 114ad3cab52Sdrh ** The expression list, ID, and source lists return by sqliteExprListDup(), 115ad3cab52Sdrh ** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded 116ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 117ff78bd2fSdrh ** 118ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 119ff78bd2fSdrh */ 120ff78bd2fSdrh Expr *sqliteExprDup(Expr *p){ 121ff78bd2fSdrh Expr *pNew; 122ff78bd2fSdrh if( p==0 ) return 0; 123fcb78a49Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 124ff78bd2fSdrh if( pNew==0 ) return 0; 1253b167c75Sdrh memcpy(pNew, p, sizeof(*pNew)); 1266977fea8Sdrh if( p->token.z!=0 ){ 1274b59ab5eSdrh pNew->token.z = sqliteStrDup(p->token.z); 1284b59ab5eSdrh pNew->token.dyn = 1; 1294b59ab5eSdrh }else{ 1304efc4754Sdrh assert( pNew->token.z==0 ); 1314b59ab5eSdrh } 1326977fea8Sdrh pNew->span.z = 0; 133ff78bd2fSdrh pNew->pLeft = sqliteExprDup(p->pLeft); 134ff78bd2fSdrh pNew->pRight = sqliteExprDup(p->pRight); 135ff78bd2fSdrh pNew->pList = sqliteExprListDup(p->pList); 136ff78bd2fSdrh pNew->pSelect = sqliteSelectDup(p->pSelect); 137ff78bd2fSdrh return pNew; 138ff78bd2fSdrh } 1394b59ab5eSdrh void sqliteTokenCopy(Token *pTo, Token *pFrom){ 1404b59ab5eSdrh if( pTo->dyn ) sqliteFree((char*)pTo->z); 1414b59ab5eSdrh if( pFrom->z ){ 1424b59ab5eSdrh pTo->n = pFrom->n; 1434b59ab5eSdrh pTo->z = sqliteStrNDup(pFrom->z, pFrom->n); 1444b59ab5eSdrh pTo->dyn = 1; 1454b59ab5eSdrh }else{ 1464b59ab5eSdrh pTo->z = 0; 1474b59ab5eSdrh } 1484b59ab5eSdrh } 149ff78bd2fSdrh ExprList *sqliteExprListDup(ExprList *p){ 150ff78bd2fSdrh ExprList *pNew; 1513e7bc9caSdrh struct ExprList_item *pItem; 152ff78bd2fSdrh int i; 153ff78bd2fSdrh if( p==0 ) return 0; 154ff78bd2fSdrh pNew = sqliteMalloc( sizeof(*pNew) ); 155ff78bd2fSdrh if( pNew==0 ) return 0; 1564305d103Sdrh pNew->nExpr = pNew->nAlloc = p->nExpr; 1573e7bc9caSdrh pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); 1583e7bc9caSdrh for(i=0; pItem && i<p->nExpr; i++, pItem++){ 1594b59ab5eSdrh Expr *pNewExpr, *pOldExpr; 1603e7bc9caSdrh pItem->pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr); 1616977fea8Sdrh if( pOldExpr->span.z!=0 && pNewExpr ){ 1626977fea8Sdrh /* Always make a copy of the span for top-level expressions in the 1634b59ab5eSdrh ** expression list. The logic in SELECT processing that determines 1644b59ab5eSdrh ** the names of columns in the result set needs this information */ 1656977fea8Sdrh sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span); 1664b59ab5eSdrh } 1671f3e905cSdrh assert( pNewExpr==0 || pNewExpr->span.z!=0 1681f3e905cSdrh || pOldExpr->span.z==0 || sqlite_malloc_failed ); 1693e7bc9caSdrh pItem->zName = sqliteStrDup(p->a[i].zName); 1703e7bc9caSdrh pItem->sortOrder = p->a[i].sortOrder; 1713e7bc9caSdrh pItem->isAgg = p->a[i].isAgg; 1723e7bc9caSdrh pItem->done = 0; 173ff78bd2fSdrh } 174ff78bd2fSdrh return pNew; 175ff78bd2fSdrh } 176ad3cab52Sdrh SrcList *sqliteSrcListDup(SrcList *p){ 177ad3cab52Sdrh SrcList *pNew; 178ad3cab52Sdrh int i; 179113088ecSdrh int nByte; 180ad3cab52Sdrh if( p==0 ) return 0; 181113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 1824efc4754Sdrh pNew = sqliteMallocRaw( nByte ); 183ad3cab52Sdrh if( pNew==0 ) return 0; 1844305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 185ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 1864efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 1874efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 1884efc4754Sdrh pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase); 1894efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 1904efc4754Sdrh pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias); 1914efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 1924efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 1934efc4754Sdrh pNewItem->pTab = 0; 1944efc4754Sdrh pNewItem->pSelect = sqliteSelectDup(pOldItem->pSelect); 1954efc4754Sdrh pNewItem->pOn = sqliteExprDup(pOldItem->pOn); 1964efc4754Sdrh pNewItem->pUsing = sqliteIdListDup(pOldItem->pUsing); 197ad3cab52Sdrh } 198ad3cab52Sdrh return pNew; 199ad3cab52Sdrh } 200ff78bd2fSdrh IdList *sqliteIdListDup(IdList *p){ 201ff78bd2fSdrh IdList *pNew; 202ff78bd2fSdrh int i; 203ff78bd2fSdrh if( p==0 ) return 0; 2044efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*pNew) ); 205ff78bd2fSdrh if( pNew==0 ) return 0; 2064305d103Sdrh pNew->nId = pNew->nAlloc = p->nId; 2074efc4754Sdrh pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) ); 208e4697f5eSdrh if( pNew->a==0 ) return 0; 209ff78bd2fSdrh for(i=0; i<p->nId; i++){ 2104efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 2114efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 2124efc4754Sdrh pNewItem->zName = sqliteStrDup(pOldItem->zName); 2134efc4754Sdrh pNewItem->idx = pOldItem->idx; 214ff78bd2fSdrh } 215ff78bd2fSdrh return pNew; 216ff78bd2fSdrh } 217ff78bd2fSdrh Select *sqliteSelectDup(Select *p){ 218ff78bd2fSdrh Select *pNew; 219ff78bd2fSdrh if( p==0 ) return 0; 2204efc4754Sdrh pNew = sqliteMallocRaw( sizeof(*p) ); 221ff78bd2fSdrh if( pNew==0 ) return 0; 222ff78bd2fSdrh pNew->isDistinct = p->isDistinct; 223ff78bd2fSdrh pNew->pEList = sqliteExprListDup(p->pEList); 224ad3cab52Sdrh pNew->pSrc = sqliteSrcListDup(p->pSrc); 225ff78bd2fSdrh pNew->pWhere = sqliteExprDup(p->pWhere); 226ff78bd2fSdrh pNew->pGroupBy = sqliteExprListDup(p->pGroupBy); 227ff78bd2fSdrh pNew->pHaving = sqliteExprDup(p->pHaving); 228ff78bd2fSdrh pNew->pOrderBy = sqliteExprListDup(p->pOrderBy); 229ff78bd2fSdrh pNew->op = p->op; 230ff78bd2fSdrh pNew->pPrior = sqliteSelectDup(p->pPrior); 231ff78bd2fSdrh pNew->nLimit = p->nLimit; 232ff78bd2fSdrh pNew->nOffset = p->nOffset; 233ff78bd2fSdrh pNew->zSelect = 0; 2347b58daeaSdrh pNew->iLimit = -1; 2357b58daeaSdrh pNew->iOffset = -1; 236ff78bd2fSdrh return pNew; 237ff78bd2fSdrh } 238ff78bd2fSdrh 239ff78bd2fSdrh 240ff78bd2fSdrh /* 241a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 242a76b5dfcSdrh ** initially NULL, then create a new expression list. 243a76b5dfcSdrh */ 244a76b5dfcSdrh ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ 245a76b5dfcSdrh if( pList==0 ){ 246a76b5dfcSdrh pList = sqliteMalloc( sizeof(ExprList) ); 247a76b5dfcSdrh if( pList==0 ){ 2484efc4754Sdrh /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */ 249a76b5dfcSdrh return 0; 250a76b5dfcSdrh } 2514efc4754Sdrh assert( pList->nAlloc==0 ); 252a76b5dfcSdrh } 2534305d103Sdrh if( pList->nAlloc<=pList->nExpr ){ 2544305d103Sdrh pList->nAlloc = pList->nAlloc*2 + 4; 2554efc4754Sdrh pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0])); 2564efc4754Sdrh if( pList->a==0 ){ 2574efc4754Sdrh /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */ 2584efc4754Sdrh pList->nExpr = pList->nAlloc = 0; 259a76b5dfcSdrh return pList; 260a76b5dfcSdrh } 261a76b5dfcSdrh } 2624efc4754Sdrh assert( pList->a!=0 ); 2634efc4754Sdrh if( pExpr || pName ){ 2644efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 2654efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 2664efc4754Sdrh pItem->pExpr = pExpr; 267a76b5dfcSdrh if( pName ){ 2684efc4754Sdrh sqliteSetNString(&pItem->zName, pName->z, pName->n, 0); 2694efc4754Sdrh sqliteDequote(pItem->zName); 270a76b5dfcSdrh } 271a76b5dfcSdrh } 272a76b5dfcSdrh return pList; 273a76b5dfcSdrh } 274a76b5dfcSdrh 275a76b5dfcSdrh /* 276a76b5dfcSdrh ** Delete an entire expression list. 277a76b5dfcSdrh */ 278a76b5dfcSdrh void sqliteExprListDelete(ExprList *pList){ 279a76b5dfcSdrh int i; 280a76b5dfcSdrh if( pList==0 ) return; 281a76b5dfcSdrh for(i=0; i<pList->nExpr; i++){ 282a76b5dfcSdrh sqliteExprDelete(pList->a[i].pExpr); 283a76b5dfcSdrh sqliteFree(pList->a[i].zName); 284a76b5dfcSdrh } 285a76b5dfcSdrh sqliteFree(pList->a); 286a76b5dfcSdrh sqliteFree(pList); 287a76b5dfcSdrh } 288a76b5dfcSdrh 289a76b5dfcSdrh /* 290fef5208cSdrh ** Walk an expression tree. Return 1 if the expression is constant 291fef5208cSdrh ** and 0 if it involves variables. 2922398937bSdrh ** 2932398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 2942398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 2952398937bSdrh ** a constant. 296fef5208cSdrh */ 2979208643dSdrh int sqliteExprIsConstant(Expr *p){ 298fef5208cSdrh switch( p->op ){ 299fef5208cSdrh case TK_ID: 300967e8b73Sdrh case TK_COLUMN: 301fef5208cSdrh case TK_DOT: 3027bdc0c1dSdrh case TK_FUNCTION: 303fef5208cSdrh return 0; 3047bdc0c1dSdrh case TK_NULL: 3052398937bSdrh case TK_STRING: 3069208643dSdrh case TK_INTEGER: 3079208643dSdrh case TK_FLOAT: 30850457896Sdrh case TK_VARIABLE: 3099208643dSdrh return 1; 310fef5208cSdrh default: { 3119208643dSdrh if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0; 3129208643dSdrh if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0; 313fef5208cSdrh if( p->pList ){ 314fef5208cSdrh int i; 315fef5208cSdrh for(i=0; i<p->pList->nExpr; i++){ 3169208643dSdrh if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0; 317fef5208cSdrh } 318fef5208cSdrh } 3199208643dSdrh return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); 320fef5208cSdrh } 321fef5208cSdrh } 3229208643dSdrh return 0; 323fef5208cSdrh } 324fef5208cSdrh 325fef5208cSdrh /* 326202b2df7Sdrh ** If the given expression codes a constant integer that is small enough 327202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 328202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 329202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 330e4de1febSdrh */ 331e4de1febSdrh int sqliteExprIsInteger(Expr *p, int *pValue){ 332e4de1febSdrh switch( p->op ){ 333e4de1febSdrh case TK_INTEGER: { 334202b2df7Sdrh if( sqliteFitsIn32Bits(p->token.z) ){ 335e4de1febSdrh *pValue = atoi(p->token.z); 336e4de1febSdrh return 1; 337e4de1febSdrh } 338202b2df7Sdrh break; 339202b2df7Sdrh } 340e4de1febSdrh case TK_STRING: { 341bd790ee3Sdrh const char *z = p->token.z; 342e4de1febSdrh int n = p->token.n; 343bd790ee3Sdrh if( n>0 && z[0]=='-' ){ z++; n--; } 344e4de1febSdrh while( n>0 && *z && isdigit(*z) ){ z++; n--; } 345202b2df7Sdrh if( n==0 && sqliteFitsIn32Bits(p->token.z) ){ 346e4de1febSdrh *pValue = atoi(p->token.z); 347e4de1febSdrh return 1; 348e4de1febSdrh } 349e4de1febSdrh break; 350e4de1febSdrh } 3514b59ab5eSdrh case TK_UPLUS: { 3524b59ab5eSdrh return sqliteExprIsInteger(p->pLeft, pValue); 3534b59ab5eSdrh } 354e4de1febSdrh case TK_UMINUS: { 355e4de1febSdrh int v; 356e4de1febSdrh if( sqliteExprIsInteger(p->pLeft, &v) ){ 357e4de1febSdrh *pValue = -v; 358e4de1febSdrh return 1; 359e4de1febSdrh } 360e4de1febSdrh break; 361e4de1febSdrh } 362e4de1febSdrh default: break; 363e4de1febSdrh } 364e4de1febSdrh return 0; 365e4de1febSdrh } 366e4de1febSdrh 367e4de1febSdrh /* 368c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 369c4a3c779Sdrh */ 370a9f9d1c0Sdrh int sqliteIsRowid(const char *z){ 371c4a3c779Sdrh if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1; 372c4a3c779Sdrh if( sqliteStrICmp(z, "ROWID")==0 ) return 1; 373c4a3c779Sdrh if( sqliteStrICmp(z, "OID")==0 ) return 1; 374c4a3c779Sdrh return 0; 375c4a3c779Sdrh } 376c4a3c779Sdrh 377c4a3c779Sdrh /* 3788141f61eSdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 3798141f61eSdrh ** that name in the set of source tables in pSrcList and make the pExpr 3808141f61eSdrh ** expression node refer back to that source column. The following changes 3818141f61eSdrh ** are made to pExpr: 3828141f61eSdrh ** 3838141f61eSdrh ** pExpr->iDb Set the index in db->aDb[] of the database holding 3848141f61eSdrh ** the table. 3858141f61eSdrh ** pExpr->iTable Set to the cursor number for the table obtained 3868141f61eSdrh ** from pSrcList. 3878141f61eSdrh ** pExpr->iColumn Set to the column number within the table. 3888141f61eSdrh ** pExpr->dataType Set to the appropriate data type for the column. 3898141f61eSdrh ** pExpr->op Set to TK_COLUMN. 3908141f61eSdrh ** pExpr->pLeft Any expression this points to is deleted 3918141f61eSdrh ** pExpr->pRight Any expression this points to is deleted. 3928141f61eSdrh ** 3938141f61eSdrh ** The pDbToken is the name of the database (the "X"). This value may be 3948141f61eSdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 3958141f61eSdrh ** can be used. The pTableToken is the name of the table (the "Y"). This 3968141f61eSdrh ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it 3978141f61eSdrh ** means that the form of the name is Z and that columns from any table 3988141f61eSdrh ** can be used. 3998141f61eSdrh ** 4008141f61eSdrh ** If the name cannot be resolved unambiguously, leave an error message 4018141f61eSdrh ** in pParse and return non-zero. Return zero on success. 4028141f61eSdrh */ 4038141f61eSdrh static int lookupName( 4048141f61eSdrh Parse *pParse, /* The parsing context */ 4058141f61eSdrh Token *pDbToken, /* Name of the database containing table, or NULL */ 4068141f61eSdrh Token *pTableToken, /* Name of table containing column, or NULL */ 4078141f61eSdrh Token *pColumnToken, /* Name of the column. */ 4088141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 4098141f61eSdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 4108141f61eSdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 4118141f61eSdrh ){ 4128141f61eSdrh char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ 4138141f61eSdrh char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ 4148141f61eSdrh char *zCol = 0; /* Name of the column. The "Z" */ 4158141f61eSdrh int i, j; /* Loop counters */ 4168141f61eSdrh int cnt = 0; /* Number of matching column names */ 4178141f61eSdrh int cntTab = 0; /* Number of matching table names */ 4187e26d750Sdrh sqlite *db = pParse->db; /* The database */ 4198141f61eSdrh 4208141f61eSdrh assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ 4218141f61eSdrh if( pDbToken && pDbToken->z ){ 4228141f61eSdrh zDb = sqliteStrNDup(pDbToken->z, pDbToken->n); 4238141f61eSdrh sqliteDequote(zDb); 4248141f61eSdrh }else{ 4258141f61eSdrh zDb = 0; 4268141f61eSdrh } 4278141f61eSdrh if( pTableToken && pTableToken->z ){ 4288141f61eSdrh zTab = sqliteStrNDup(pTableToken->z, pTableToken->n); 4298141f61eSdrh sqliteDequote(zTab); 4308141f61eSdrh }else{ 4318141f61eSdrh assert( zDb==0 ); 4328141f61eSdrh zTab = 0; 4338141f61eSdrh } 4348141f61eSdrh zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n); 4358141f61eSdrh sqliteDequote(zCol); 4368141f61eSdrh if( sqlite_malloc_failed ){ 4378141f61eSdrh return 1; /* Leak memory (zDb and zTab) if malloc fails */ 4388141f61eSdrh } 4398141f61eSdrh assert( zTab==0 || pEList==0 ); 4408141f61eSdrh 4418141f61eSdrh pExpr->iTable = -1; 4428141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 4438141f61eSdrh struct SrcList_item *pItem = &pSrcList->a[i]; 4448141f61eSdrh Table *pTab = pItem->pTab; 4458141f61eSdrh Column *pCol; 4468141f61eSdrh 4478141f61eSdrh if( pTab==0 ) continue; 4488141f61eSdrh assert( pTab->nCol>0 ); 4498141f61eSdrh if( zTab ){ 4508141f61eSdrh if( pItem->zAlias ){ 4518141f61eSdrh char *zTabName = pItem->zAlias; 4528141f61eSdrh if( sqliteStrICmp(zTabName, zTab)!=0 ) continue; 4538141f61eSdrh }else{ 4548141f61eSdrh char *zTabName = pTab->zName; 4558141f61eSdrh if( zTabName==0 || sqliteStrICmp(zTabName, zTab)!=0 ) continue; 4568141f61eSdrh if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){ 4578141f61eSdrh continue; 4588141f61eSdrh } 4598141f61eSdrh } 4608141f61eSdrh } 4618141f61eSdrh if( 0==(cntTab++) ){ 4628141f61eSdrh pExpr->iTable = pItem->iCursor; 4638141f61eSdrh pExpr->iDb = pTab->iDb; 4648141f61eSdrh } 4658141f61eSdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 4668141f61eSdrh if( sqliteStrICmp(pCol->zName, zCol)==0 ){ 4678141f61eSdrh cnt++; 4688141f61eSdrh pExpr->iTable = pItem->iCursor; 4698141f61eSdrh pExpr->iDb = pTab->iDb; 4708141f61eSdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 4718141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 4728141f61eSdrh pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK; 4738141f61eSdrh break; 4748141f61eSdrh } 4758141f61eSdrh } 4768141f61eSdrh } 4778141f61eSdrh 4788141f61eSdrh /* If we have not already resolved the name, then maybe 4798141f61eSdrh ** it is a new.* or old.* trigger argument reference 4808141f61eSdrh */ 4818141f61eSdrh if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ 4828141f61eSdrh TriggerStack *pTriggerStack = pParse->trigStack; 4838141f61eSdrh Table *pTab = 0; 4848141f61eSdrh if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zTab) == 0 ){ 4858141f61eSdrh pExpr->iTable = pTriggerStack->newIdx; 4868141f61eSdrh assert( pTriggerStack->pTab ); 4878141f61eSdrh pTab = pTriggerStack->pTab; 4888141f61eSdrh }else if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zTab) == 0 ){ 4898141f61eSdrh pExpr->iTable = pTriggerStack->oldIdx; 4908141f61eSdrh assert( pTriggerStack->pTab ); 4918141f61eSdrh pTab = pTriggerStack->pTab; 4928141f61eSdrh } 4938141f61eSdrh 4948141f61eSdrh if( pTab ){ 4958141f61eSdrh int j; 4968141f61eSdrh Column *pCol = pTab->aCol; 4978141f61eSdrh 4988141f61eSdrh pExpr->iDb = pTab->iDb; 4998141f61eSdrh cntTab++; 5008141f61eSdrh for(j=0; j < pTab->nCol; j++, pCol++) { 5018141f61eSdrh if( sqliteStrICmp(pCol->zName, zCol)==0 ){ 5028141f61eSdrh cnt++; 5038141f61eSdrh pExpr->iColumn = j==pTab->iPKey ? -1 : j; 5048141f61eSdrh pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK; 5058141f61eSdrh break; 5068141f61eSdrh } 5078141f61eSdrh } 5088141f61eSdrh } 5098141f61eSdrh } 5108141f61eSdrh 5118141f61eSdrh /* 5128141f61eSdrh ** Perhaps the name is a reference to the ROWID 5138141f61eSdrh */ 5148141f61eSdrh if( cnt==0 && cntTab==1 && sqliteIsRowid(zCol) ){ 5158141f61eSdrh cnt = 1; 5168141f61eSdrh pExpr->iColumn = -1; 5178141f61eSdrh pExpr->dataType = SQLITE_SO_NUM; 5188141f61eSdrh } 5198141f61eSdrh 5208141f61eSdrh /* 5218141f61eSdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 5228141f61eSdrh ** might refer to an result-set alias. This happens, for example, when 5238141f61eSdrh ** we are resolving names in the WHERE clause of the following command: 5248141f61eSdrh ** 5258141f61eSdrh ** SELECT a+b AS x FROM table WHERE x<10; 5268141f61eSdrh ** 5278141f61eSdrh ** In cases like this, replace pExpr with a copy of the expression that 5288141f61eSdrh ** forms the result set entry ("a+b" in the example) and return immediately. 5298141f61eSdrh ** Note that the expression in the result set should have already been 5308141f61eSdrh ** resolved by the time the WHERE clause is resolved. 5318141f61eSdrh */ 5328141f61eSdrh if( cnt==0 && pEList!=0 ){ 5338141f61eSdrh for(j=0; j<pEList->nExpr; j++){ 5348141f61eSdrh char *zAs = pEList->a[j].zName; 5358141f61eSdrh if( zAs!=0 && sqliteStrICmp(zAs, zCol)==0 ){ 5368141f61eSdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 5378141f61eSdrh pExpr->op = TK_AS; 5388141f61eSdrh pExpr->iColumn = j; 5398141f61eSdrh pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr); 5408141f61eSdrh sqliteFree(zCol); 5418141f61eSdrh assert( zTab==0 && zDb==0 ); 5428141f61eSdrh return 0; 5438141f61eSdrh } 5448141f61eSdrh } 5458141f61eSdrh } 5468141f61eSdrh 5478141f61eSdrh /* 5488141f61eSdrh ** If X and Y are NULL (in other words if only the column name Z is 5498141f61eSdrh ** supplied) and the value of Z is enclosed in double-quotes, then 5508141f61eSdrh ** Z is a string literal if it doesn't match any column names. In that 5518141f61eSdrh ** case, we need to return right away and not make any changes to 5528141f61eSdrh ** pExpr. 5538141f61eSdrh */ 5548141f61eSdrh if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ 5558141f61eSdrh sqliteFree(zCol); 5568141f61eSdrh return 0; 5578141f61eSdrh } 5588141f61eSdrh 5598141f61eSdrh /* 5608141f61eSdrh ** cnt==0 means there was not match. cnt>1 means there were two or 5618141f61eSdrh ** more matches. Either way, we have an error. 5628141f61eSdrh */ 5638141f61eSdrh if( cnt!=1 ){ 5648141f61eSdrh char *z = 0; 5658141f61eSdrh char *zErr; 5668141f61eSdrh zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; 5678141f61eSdrh if( zDb ){ 5688141f61eSdrh sqliteSetString(&z, zDb, ".", zTab, ".", zCol, 0); 5698141f61eSdrh }else if( zTab ){ 5708141f61eSdrh sqliteSetString(&z, zTab, ".", zCol, 0); 5718141f61eSdrh }else{ 5728141f61eSdrh z = sqliteStrDup(zCol); 5738141f61eSdrh } 5748141f61eSdrh sqliteErrorMsg(pParse, zErr, z); 5758141f61eSdrh sqliteFree(z); 5768141f61eSdrh } 5778141f61eSdrh 5788141f61eSdrh /* Clean up and return 5798141f61eSdrh */ 5808141f61eSdrh sqliteFree(zDb); 5818141f61eSdrh sqliteFree(zTab); 5828141f61eSdrh sqliteFree(zCol); 5838141f61eSdrh sqliteExprDelete(pExpr->pLeft); 5848141f61eSdrh pExpr->pLeft = 0; 5858141f61eSdrh sqliteExprDelete(pExpr->pRight); 5868141f61eSdrh pExpr->pRight = 0; 5878141f61eSdrh pExpr->op = TK_COLUMN; 5888141f61eSdrh sqliteAuthRead(pParse, pExpr, pSrcList); 5898141f61eSdrh return cnt!=1; 5908141f61eSdrh } 5918141f61eSdrh 5928141f61eSdrh /* 593cce7d176Sdrh ** This routine walks an expression tree and resolves references to 594967e8b73Sdrh ** table columns. Nodes of the form ID.ID or ID resolve into an 595aacc543eSdrh ** index to the table in the table list and a column offset. The 596aacc543eSdrh ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable 597aacc543eSdrh ** value is changed to the index of the referenced table in pTabList 598832508b7Sdrh ** plus the "base" value. The base value will ultimately become the 599aacc543eSdrh ** VDBE cursor number for a cursor that is pointing into the referenced 600aacc543eSdrh ** table. The Expr.iColumn value is changed to the index of the column 601aacc543eSdrh ** of the referenced table. The Expr.iColumn value for the special 602aacc543eSdrh ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an 603aacc543eSdrh ** alias for ROWID. 60419a775c2Sdrh ** 605fef5208cSdrh ** We also check for instances of the IN operator. IN comes in two 606fef5208cSdrh ** forms: 607fef5208cSdrh ** 608fef5208cSdrh ** expr IN (exprlist) 609fef5208cSdrh ** and 610fef5208cSdrh ** expr IN (SELECT ...) 611fef5208cSdrh ** 612fef5208cSdrh ** The first form is handled by creating a set holding the list 613fef5208cSdrh ** of allowed values. The second form causes the SELECT to generate 614fef5208cSdrh ** a temporary table. 615fef5208cSdrh ** 616fef5208cSdrh ** This routine also looks for scalar SELECTs that are part of an expression. 61719a775c2Sdrh ** If it finds any, it generates code to write the value of that select 61819a775c2Sdrh ** into a memory cell. 619cce7d176Sdrh ** 620967e8b73Sdrh ** Unknown columns or tables provoke an error. The function returns 621cce7d176Sdrh ** the number of errors seen and leaves an error message on pParse->zErrMsg. 622cce7d176Sdrh */ 623a2e00042Sdrh int sqliteExprResolveIds( 624a2e00042Sdrh Parse *pParse, /* The parser context */ 6258141f61eSdrh SrcList *pSrcList, /* List of tables used to resolve column names */ 626a2e00042Sdrh ExprList *pEList, /* List of expressions used to resolve "AS" */ 627a2e00042Sdrh Expr *pExpr /* The expression to be analyzed. */ 628a2e00042Sdrh ){ 6296a3ea0e6Sdrh int i; 6306a3ea0e6Sdrh 6318141f61eSdrh if( pExpr==0 || pSrcList==0 ) return 0; 6328141f61eSdrh for(i=0; i<pSrcList->nSrc; i++){ 6338141f61eSdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab ); 6346a3ea0e6Sdrh } 635cce7d176Sdrh switch( pExpr->op ){ 6362398937bSdrh /* Double-quoted strings (ex: "abc") are used as identifiers if 6372398937bSdrh ** possible. Otherwise they remain as strings. Single-quoted 6382398937bSdrh ** strings (ex: 'abc') are always string literals. 6392398937bSdrh */ 6402398937bSdrh case TK_STRING: { 6412398937bSdrh if( pExpr->token.z[0]=='\'' ) break; 6422398937bSdrh /* Fall thru into the TK_ID case if this is a double-quoted string */ 6432398937bSdrh } 6448141f61eSdrh /* A lone identifier is the name of a columnd. 645a2e00042Sdrh */ 646cce7d176Sdrh case TK_ID: { 6478141f61eSdrh if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){ 648cce7d176Sdrh return 1; 649ed6c8671Sdrh } 650cce7d176Sdrh break; 651cce7d176Sdrh } 652cce7d176Sdrh 653d24cc427Sdrh /* A table name and column name: ID.ID 654d24cc427Sdrh ** Or a database, table and column: ID.ID.ID 655d24cc427Sdrh */ 656cce7d176Sdrh case TK_DOT: { 6578141f61eSdrh Token *pColumn; 6588141f61eSdrh Token *pTable; 6598141f61eSdrh Token *pDb; 6608141f61eSdrh Expr *pRight; 661cce7d176Sdrh 662cce7d176Sdrh pRight = pExpr->pRight; 663d24cc427Sdrh if( pRight->op==TK_ID ){ 6648141f61eSdrh pDb = 0; 6658141f61eSdrh pTable = &pExpr->pLeft->token; 6668141f61eSdrh pColumn = &pRight->token; 667d24cc427Sdrh }else{ 6688141f61eSdrh assert( pRight->op==TK_DOT ); 6698141f61eSdrh pDb = &pExpr->pLeft->token; 6708141f61eSdrh pTable = &pRight->pLeft->token; 6718141f61eSdrh pColumn = &pRight->pRight->token; 672d24cc427Sdrh } 6738141f61eSdrh if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){ 674daffd0e5Sdrh return 1; 675daffd0e5Sdrh } 676cce7d176Sdrh break; 677cce7d176Sdrh } 678cce7d176Sdrh 679fef5208cSdrh case TK_IN: { 680d8bc7086Sdrh Vdbe *v = sqliteGetVdbe(pParse); 681fef5208cSdrh if( v==0 ) return 1; 6828141f61eSdrh if( sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 683cfab11bcSdrh return 1; 684cfab11bcSdrh } 685fef5208cSdrh if( pExpr->pSelect ){ 686fef5208cSdrh /* Case 1: expr IN (SELECT ...) 687fef5208cSdrh ** 688fef5208cSdrh ** Generate code to write the results of the select into a temporary 6894794b980Sdrh ** table. The cursor number of the temporary table has already 6904794b980Sdrh ** been put in iTable by sqliteExprResolveInSelect(). 691fef5208cSdrh */ 692832508b7Sdrh pExpr->iTable = pParse->nTab++; 693c6b52df3Sdrh sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1); 694832508b7Sdrh sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0); 695fef5208cSdrh }else if( pExpr->pList ){ 696fef5208cSdrh /* Case 2: expr IN (exprlist) 697fef5208cSdrh ** 698fef5208cSdrh ** Create a set to put the exprlist values in. The Set id is stored 699fef5208cSdrh ** in iTable. 700fef5208cSdrh */ 701fef5208cSdrh int i, iSet; 702fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 703fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 7049208643dSdrh if( !sqliteExprIsConstant(pE2) ){ 705da93d238Sdrh sqliteErrorMsg(pParse, 706da93d238Sdrh "right-hand side of IN operator must be constant"); 707fef5208cSdrh return 1; 708fef5208cSdrh } 7094794b980Sdrh if( sqliteExprCheck(pParse, pE2, 0, 0) ){ 7104794b980Sdrh return 1; 7114794b980Sdrh } 712fef5208cSdrh } 713fef5208cSdrh iSet = pExpr->iTable = pParse->nSet++; 714fef5208cSdrh for(i=0; i<pExpr->pList->nExpr; i++){ 715fef5208cSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 716fef5208cSdrh switch( pE2->op ){ 717fef5208cSdrh case TK_FLOAT: 718fef5208cSdrh case TK_INTEGER: 719fef5208cSdrh case TK_STRING: { 720701a0aebSdrh int addr; 721a76b5dfcSdrh assert( pE2->token.z ); 722701a0aebSdrh addr = sqliteVdbeOp3(v, OP_SetInsert, iSet, 0, 723701a0aebSdrh pE2->token.z, pE2->token.n); 724fef5208cSdrh sqliteVdbeDequoteP3(v, addr); 725fef5208cSdrh break; 726fef5208cSdrh } 727fef5208cSdrh default: { 728fef5208cSdrh sqliteExprCode(pParse, pE2); 72999fcd718Sdrh sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); 730fef5208cSdrh break; 731fef5208cSdrh } 732fef5208cSdrh } 733fef5208cSdrh } 734fef5208cSdrh } 735cfab11bcSdrh break; 736fef5208cSdrh } 737fef5208cSdrh 73819a775c2Sdrh case TK_SELECT: { 739fef5208cSdrh /* This has to be a scalar SELECT. Generate code to put the 740fef5208cSdrh ** value of this select in a memory cell and record the number 741967e8b73Sdrh ** of the memory cell in iColumn. 742fef5208cSdrh */ 743967e8b73Sdrh pExpr->iColumn = pParse->nMem++; 744832508b7Sdrh if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){ 74519a775c2Sdrh return 1; 74619a775c2Sdrh } 74719a775c2Sdrh break; 74819a775c2Sdrh } 74919a775c2Sdrh 750cce7d176Sdrh /* For all else, just recursively walk the tree */ 751cce7d176Sdrh default: { 752cce7d176Sdrh if( pExpr->pLeft 7538141f61eSdrh && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){ 754cce7d176Sdrh return 1; 755cce7d176Sdrh } 756cce7d176Sdrh if( pExpr->pRight 7578141f61eSdrh && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){ 758cce7d176Sdrh return 1; 759cce7d176Sdrh } 760cce7d176Sdrh if( pExpr->pList ){ 761cce7d176Sdrh int i; 762cce7d176Sdrh ExprList *pList = pExpr->pList; 763cce7d176Sdrh for(i=0; i<pList->nExpr; i++){ 764832508b7Sdrh Expr *pArg = pList->a[i].pExpr; 7658141f61eSdrh if( sqliteExprResolveIds(pParse, pSrcList, pEList, pArg) ){ 766cce7d176Sdrh return 1; 767cce7d176Sdrh } 768cce7d176Sdrh } 769cce7d176Sdrh } 770cce7d176Sdrh } 771cce7d176Sdrh } 772cce7d176Sdrh return 0; 773cce7d176Sdrh } 774cce7d176Sdrh 775cce7d176Sdrh /* 7764b59ab5eSdrh ** pExpr is a node that defines a function of some kind. It might 7774b59ab5eSdrh ** be a syntactic function like "count(x)" or it might be a function 7784b59ab5eSdrh ** that implements an operator, like "a LIKE b". 7794b59ab5eSdrh ** 7804b59ab5eSdrh ** This routine makes *pzName point to the name of the function and 7814b59ab5eSdrh ** *pnName hold the number of characters in the function name. 7824b59ab5eSdrh */ 7834b59ab5eSdrh static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){ 7844b59ab5eSdrh switch( pExpr->op ){ 7854b59ab5eSdrh case TK_FUNCTION: { 7864b59ab5eSdrh *pzName = pExpr->token.z; 7876977fea8Sdrh *pnName = pExpr->token.n; 7884b59ab5eSdrh break; 7894b59ab5eSdrh } 7904b59ab5eSdrh case TK_LIKE: { 7914b59ab5eSdrh *pzName = "like"; 7924b59ab5eSdrh *pnName = 4; 7934b59ab5eSdrh break; 7944b59ab5eSdrh } 7954b59ab5eSdrh case TK_GLOB: { 7964b59ab5eSdrh *pzName = "glob"; 7974b59ab5eSdrh *pnName = 4; 7984b59ab5eSdrh break; 7994b59ab5eSdrh } 8004b59ab5eSdrh default: { 8014b59ab5eSdrh *pzName = "can't happen"; 8024b59ab5eSdrh *pnName = 12; 8034b59ab5eSdrh break; 8044b59ab5eSdrh } 8054b59ab5eSdrh } 8064b59ab5eSdrh } 8074b59ab5eSdrh 8084b59ab5eSdrh /* 809cce7d176Sdrh ** Error check the functions in an expression. Make sure all 810cce7d176Sdrh ** function names are recognized and all functions have the correct 811cce7d176Sdrh ** number of arguments. Leave an error message in pParse->zErrMsg 812cce7d176Sdrh ** if anything is amiss. Return the number of errors. 813cce7d176Sdrh ** 814cce7d176Sdrh ** if pIsAgg is not null and this expression is an aggregate function 815cce7d176Sdrh ** (like count(*) or max(value)) then write a 1 into *pIsAgg. 816cce7d176Sdrh */ 817cce7d176Sdrh int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ 818cce7d176Sdrh int nErr = 0; 819cce7d176Sdrh if( pExpr==0 ) return 0; 820cce7d176Sdrh switch( pExpr->op ){ 8214b59ab5eSdrh case TK_GLOB: 8224b59ab5eSdrh case TK_LIKE: 823cce7d176Sdrh case TK_FUNCTION: { 824c9b84a1fSdrh int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */ 825c9b84a1fSdrh int no_such_func = 0; /* True if no such function exists */ 826c9b84a1fSdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 827c9b84a1fSdrh int is_agg = 0; /* True if is an aggregate function */ 828cce7d176Sdrh int i; 8294b59ab5eSdrh int nId; /* Number of characters in function name */ 8304b59ab5eSdrh const char *zId; /* The function name. */ 8310bce8354Sdrh FuncDef *pDef; 8320bce8354Sdrh 8334b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 8344b59ab5eSdrh pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0); 8350bce8354Sdrh if( pDef==0 ){ 8364b59ab5eSdrh pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0); 8370bce8354Sdrh if( pDef==0 ){ 838cce7d176Sdrh no_such_func = 1; 8398e0a2f90Sdrh }else{ 8408e0a2f90Sdrh wrong_num_args = 1; 8418e0a2f90Sdrh } 8428e0a2f90Sdrh }else{ 8430bce8354Sdrh is_agg = pDef->xFunc==0; 844cce7d176Sdrh } 8458e0a2f90Sdrh if( is_agg && !allowAgg ){ 846f7a9e1acSdrh sqliteErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId); 8478e0a2f90Sdrh nErr++; 8488e0a2f90Sdrh is_agg = 0; 8498e0a2f90Sdrh }else if( no_such_func ){ 850f7a9e1acSdrh sqliteErrorMsg(pParse, "no such function: %.*s", nId, zId); 851cce7d176Sdrh nErr++; 8528e0a2f90Sdrh }else if( wrong_num_args ){ 853f7a9e1acSdrh sqliteErrorMsg(pParse,"wrong number of arguments to function %.*s()", 854f7a9e1acSdrh nId, zId); 8558e0a2f90Sdrh nErr++; 856cce7d176Sdrh } 857f7a9e1acSdrh if( is_agg ){ 858f7a9e1acSdrh pExpr->op = TK_AGG_FUNCTION; 859f7a9e1acSdrh if( pIsAgg ) *pIsAgg = 1; 860f7a9e1acSdrh } 861cce7d176Sdrh for(i=0; nErr==0 && i<n; i++){ 8624cfa7934Sdrh nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, 8634cfa7934Sdrh allowAgg && !is_agg, pIsAgg); 864cce7d176Sdrh } 865c9b84a1fSdrh if( pDef==0 ){ 866*268380caSdrh /* Already reported an error */ 867c9b84a1fSdrh }else if( pDef->dataType>=0 ){ 868c9b84a1fSdrh if( pDef->dataType<n ){ 869c9b84a1fSdrh pExpr->dataType = 870c9b84a1fSdrh sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr); 871c9b84a1fSdrh }else{ 872c9b84a1fSdrh pExpr->dataType = SQLITE_SO_NUM; 873c9b84a1fSdrh } 874c9b84a1fSdrh }else if( pDef->dataType==SQLITE_ARGS ){ 875c9b84a1fSdrh pDef->dataType = SQLITE_SO_TEXT; 876c9b84a1fSdrh for(i=0; i<n; i++){ 877c9b84a1fSdrh if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){ 878c9b84a1fSdrh pExpr->dataType = SQLITE_SO_NUM; 879c9b84a1fSdrh break; 880c9b84a1fSdrh } 881c9b84a1fSdrh } 882c9b84a1fSdrh }else if( pDef->dataType==SQLITE_NUMERIC ){ 883c9b84a1fSdrh pExpr->dataType = SQLITE_SO_NUM; 884c9b84a1fSdrh }else{ 885c9b84a1fSdrh pExpr->dataType = SQLITE_SO_TEXT; 886c9b84a1fSdrh } 887cce7d176Sdrh } 888cce7d176Sdrh default: { 889cce7d176Sdrh if( pExpr->pLeft ){ 8902282792aSdrh nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); 891cce7d176Sdrh } 892cce7d176Sdrh if( nErr==0 && pExpr->pRight ){ 8932282792aSdrh nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); 894cce7d176Sdrh } 895fef5208cSdrh if( nErr==0 && pExpr->pList ){ 896fef5208cSdrh int n = pExpr->pList->nExpr; 897fef5208cSdrh int i; 898fef5208cSdrh for(i=0; nErr==0 && i<n; i++){ 8992282792aSdrh Expr *pE2 = pExpr->pList->a[i].pExpr; 9002282792aSdrh nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg); 901fef5208cSdrh } 902fef5208cSdrh } 903cce7d176Sdrh break; 904cce7d176Sdrh } 905cce7d176Sdrh } 906cce7d176Sdrh return nErr; 907cce7d176Sdrh } 908cce7d176Sdrh 909cce7d176Sdrh /* 910c9b84a1fSdrh ** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the 911c9b84a1fSdrh ** given expression should sort as numeric values or as text. 912c9b84a1fSdrh ** 913c9b84a1fSdrh ** The sqliteExprResolveIds() and sqliteExprCheck() routines must have 914c9b84a1fSdrh ** both been called on the expression before it is passed to this routine. 915c9b84a1fSdrh */ 916c9b84a1fSdrh int sqliteExprType(Expr *p){ 917c9b84a1fSdrh if( p==0 ) return SQLITE_SO_NUM; 918c9b84a1fSdrh while( p ) switch( p->op ){ 919c9b84a1fSdrh case TK_PLUS: 920c9b84a1fSdrh case TK_MINUS: 921c9b84a1fSdrh case TK_STAR: 922c9b84a1fSdrh case TK_SLASH: 923c9b84a1fSdrh case TK_AND: 924c9b84a1fSdrh case TK_OR: 925c9b84a1fSdrh case TK_ISNULL: 926c9b84a1fSdrh case TK_NOTNULL: 927c9b84a1fSdrh case TK_NOT: 928c9b84a1fSdrh case TK_UMINUS: 9294b59ab5eSdrh case TK_UPLUS: 930c9b84a1fSdrh case TK_BITAND: 931c9b84a1fSdrh case TK_BITOR: 932c9b84a1fSdrh case TK_BITNOT: 933c9b84a1fSdrh case TK_LSHIFT: 934c9b84a1fSdrh case TK_RSHIFT: 935c9b84a1fSdrh case TK_REM: 936c9b84a1fSdrh case TK_INTEGER: 937c9b84a1fSdrh case TK_FLOAT: 938c9b84a1fSdrh case TK_IN: 939c9b84a1fSdrh case TK_BETWEEN: 9404b59ab5eSdrh case TK_GLOB: 9414b59ab5eSdrh case TK_LIKE: 942c9b84a1fSdrh return SQLITE_SO_NUM; 943c9b84a1fSdrh 944c9b84a1fSdrh case TK_STRING: 945c9b84a1fSdrh case TK_NULL: 946c9b84a1fSdrh case TK_CONCAT: 94750457896Sdrh case TK_VARIABLE: 948c9b84a1fSdrh return SQLITE_SO_TEXT; 949c9b84a1fSdrh 950c9b84a1fSdrh case TK_LT: 951c9b84a1fSdrh case TK_LE: 952c9b84a1fSdrh case TK_GT: 953c9b84a1fSdrh case TK_GE: 954c9b84a1fSdrh case TK_NE: 955c9b84a1fSdrh case TK_EQ: 956c9b84a1fSdrh if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){ 957c9b84a1fSdrh return SQLITE_SO_NUM; 958c9b84a1fSdrh } 959c9b84a1fSdrh p = p->pRight; 960c9b84a1fSdrh break; 961c9b84a1fSdrh 962c9b84a1fSdrh case TK_AS: 963c9b84a1fSdrh p = p->pLeft; 964c9b84a1fSdrh break; 965c9b84a1fSdrh 966c9b84a1fSdrh case TK_COLUMN: 967c9b84a1fSdrh case TK_FUNCTION: 968c9b84a1fSdrh case TK_AGG_FUNCTION: 969c9b84a1fSdrh return p->dataType; 970c9b84a1fSdrh 971c9b84a1fSdrh case TK_SELECT: 972c9b84a1fSdrh assert( p->pSelect ); 973c9b84a1fSdrh assert( p->pSelect->pEList ); 974c9b84a1fSdrh assert( p->pSelect->pEList->nExpr>0 ); 975c9b84a1fSdrh p = p->pSelect->pEList->a[0].pExpr; 976c9b84a1fSdrh break; 977c9b84a1fSdrh 978b1363206Sdrh case TK_CASE: { 979b1363206Sdrh if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){ 980b1363206Sdrh return SQLITE_SO_NUM; 981b1363206Sdrh } 982b1363206Sdrh if( p->pList ){ 983b1363206Sdrh int i; 984b1363206Sdrh ExprList *pList = p->pList; 985b1363206Sdrh for(i=1; i<pList->nExpr; i+=2){ 986b1363206Sdrh if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){ 987b1363206Sdrh return SQLITE_SO_NUM; 988b1363206Sdrh } 989b1363206Sdrh } 990b1363206Sdrh } 991b1363206Sdrh return SQLITE_SO_TEXT; 992b1363206Sdrh } 993b1363206Sdrh 994c9b84a1fSdrh default: 995c9b84a1fSdrh assert( p->op==TK_ABORT ); /* Can't Happen */ 996c9b84a1fSdrh break; 997c9b84a1fSdrh } 998c9b84a1fSdrh return SQLITE_SO_NUM; 999c9b84a1fSdrh } 1000c9b84a1fSdrh 1001c9b84a1fSdrh /* 1002cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 10031ccde15dSdrh ** expression and leave the result on the top of stack. 1004cce7d176Sdrh */ 1005cce7d176Sdrh void sqliteExprCode(Parse *pParse, Expr *pExpr){ 1006cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1007cce7d176Sdrh int op; 1008daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1009cce7d176Sdrh switch( pExpr->op ){ 1010cce7d176Sdrh case TK_PLUS: op = OP_Add; break; 1011cce7d176Sdrh case TK_MINUS: op = OP_Subtract; break; 1012cce7d176Sdrh case TK_STAR: op = OP_Multiply; break; 1013cce7d176Sdrh case TK_SLASH: op = OP_Divide; break; 1014cce7d176Sdrh case TK_AND: op = OP_And; break; 1015cce7d176Sdrh case TK_OR: op = OP_Or; break; 1016cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1017cce7d176Sdrh case TK_LE: op = OP_Le; break; 1018cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1019cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1020cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1021cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1022cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1023cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1024cce7d176Sdrh case TK_NOT: op = OP_Not; break; 1025cce7d176Sdrh case TK_UMINUS: op = OP_Negative; break; 1026bf4133cbSdrh case TK_BITAND: op = OP_BitAnd; break; 1027bf4133cbSdrh case TK_BITOR: op = OP_BitOr; break; 1028bf4133cbSdrh case TK_BITNOT: op = OP_BitNot; break; 1029bf4133cbSdrh case TK_LSHIFT: op = OP_ShiftLeft; break; 1030bf4133cbSdrh case TK_RSHIFT: op = OP_ShiftRight; break; 1031bf4133cbSdrh case TK_REM: op = OP_Remainder; break; 1032cce7d176Sdrh default: break; 1033cce7d176Sdrh } 1034cce7d176Sdrh switch( pExpr->op ){ 1035967e8b73Sdrh case TK_COLUMN: { 10362282792aSdrh if( pParse->useAgg ){ 103799fcd718Sdrh sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 1038c4a3c779Sdrh }else if( pExpr->iColumn>=0 ){ 103999fcd718Sdrh sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); 1040c4a3c779Sdrh }else{ 104199fcd718Sdrh sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0); 10422282792aSdrh } 1043cce7d176Sdrh break; 1044cce7d176Sdrh } 104551e9a445Sdrh case TK_STRING: 104651e9a445Sdrh case TK_FLOAT: 1047cce7d176Sdrh case TK_INTEGER: { 104851e9a445Sdrh if( pExpr->op==TK_INTEGER && sqliteFitsIn32Bits(pExpr->token.z) ){ 1049202b2df7Sdrh sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0); 105051e9a445Sdrh }else{ 10517a7c7390Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 105251e9a445Sdrh } 1053a76b5dfcSdrh assert( pExpr->token.z ); 10547a7c7390Sdrh sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); 105551e9a445Sdrh sqliteVdbeDequoteP3(v, -1); 1056cce7d176Sdrh break; 1057cce7d176Sdrh } 1058cce7d176Sdrh case TK_NULL: { 105999fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 1060cce7d176Sdrh break; 1061cce7d176Sdrh } 106250457896Sdrh case TK_VARIABLE: { 10637c972decSdrh sqliteVdbeAddOp(v, OP_Variable, pExpr->iTable, 0); 106450457896Sdrh break; 106550457896Sdrh } 1066c9b84a1fSdrh case TK_LT: 1067c9b84a1fSdrh case TK_LE: 1068c9b84a1fSdrh case TK_GT: 1069c9b84a1fSdrh case TK_GE: 1070c9b84a1fSdrh case TK_NE: 1071c9b84a1fSdrh case TK_EQ: { 1072491791a8Sdrh if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ 1073c9b84a1fSdrh op += 6; /* Convert numeric opcodes to text opcodes */ 1074c9b84a1fSdrh } 1075c9b84a1fSdrh /* Fall through into the next case */ 1076c9b84a1fSdrh } 1077cce7d176Sdrh case TK_AND: 1078cce7d176Sdrh case TK_OR: 1079cce7d176Sdrh case TK_PLUS: 1080cce7d176Sdrh case TK_STAR: 1081cce7d176Sdrh case TK_MINUS: 1082bf4133cbSdrh case TK_REM: 1083bf4133cbSdrh case TK_BITAND: 1084bf4133cbSdrh case TK_BITOR: 1085c9b84a1fSdrh case TK_SLASH: { 1086cce7d176Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1087cce7d176Sdrh sqliteExprCode(pParse, pExpr->pRight); 108899fcd718Sdrh sqliteVdbeAddOp(v, op, 0, 0); 1089cce7d176Sdrh break; 1090cce7d176Sdrh } 1091bf4133cbSdrh case TK_LSHIFT: 1092bf4133cbSdrh case TK_RSHIFT: { 1093bf4133cbSdrh sqliteExprCode(pParse, pExpr->pRight); 1094bf4133cbSdrh sqliteExprCode(pParse, pExpr->pLeft); 1095bf4133cbSdrh sqliteVdbeAddOp(v, op, 0, 0); 1096bf4133cbSdrh break; 1097bf4133cbSdrh } 10980040077dSdrh case TK_CONCAT: { 10990040077dSdrh sqliteExprCode(pParse, pExpr->pLeft); 11000040077dSdrh sqliteExprCode(pParse, pExpr->pRight); 110199fcd718Sdrh sqliteVdbeAddOp(v, OP_Concat, 2, 0); 11020040077dSdrh break; 11030040077dSdrh } 1104cce7d176Sdrh case TK_UMINUS: { 11056e142f54Sdrh assert( pExpr->pLeft ); 11067a7c7390Sdrh if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){ 11076e142f54Sdrh Token *p = &pExpr->pLeft->token; 11086e142f54Sdrh char *z = sqliteMalloc( p->n + 2 ); 11096e142f54Sdrh sprintf(z, "-%.*s", p->n, p->z); 1110202b2df7Sdrh if( pExpr->pLeft->op==TK_INTEGER && sqliteFitsIn32Bits(z) ){ 1111e6840900Sdrh sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0); 1112e6840900Sdrh }else{ 111399fcd718Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 1114e6840900Sdrh } 111599fcd718Sdrh sqliteVdbeChangeP3(v, -1, z, p->n+1); 11166e142f54Sdrh sqliteFree(z); 11176e142f54Sdrh break; 11186e142f54Sdrh } 11191ccde15dSdrh /* Fall through into TK_NOT */ 11206e142f54Sdrh } 1121bf4133cbSdrh case TK_BITNOT: 11226e142f54Sdrh case TK_NOT: { 1123cce7d176Sdrh sqliteExprCode(pParse, pExpr->pLeft); 112499fcd718Sdrh sqliteVdbeAddOp(v, op, 0, 0); 1125cce7d176Sdrh break; 1126cce7d176Sdrh } 1127cce7d176Sdrh case TK_ISNULL: 1128cce7d176Sdrh case TK_NOTNULL: { 1129cce7d176Sdrh int dest; 113099fcd718Sdrh sqliteVdbeAddOp(v, OP_Integer, 1, 0); 1131cce7d176Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1132cce7d176Sdrh dest = sqliteVdbeCurrentAddr(v) + 2; 1133f5905aa7Sdrh sqliteVdbeAddOp(v, op, 1, dest); 113499fcd718Sdrh sqliteVdbeAddOp(v, OP_AddImm, -1, 0); 1135cce7d176Sdrh break; 1136cce7d176Sdrh } 11372282792aSdrh case TK_AGG_FUNCTION: { 113899fcd718Sdrh sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); 11392282792aSdrh break; 11402282792aSdrh } 11414b59ab5eSdrh case TK_GLOB: 11424b59ab5eSdrh case TK_LIKE: 1143cce7d176Sdrh case TK_FUNCTION: { 1144cce7d176Sdrh ExprList *pList = pExpr->pList; 114589425d5eSdrh int nExpr = pList ? pList->nExpr : 0; 11460bce8354Sdrh FuncDef *pDef; 11474b59ab5eSdrh int nId; 11484b59ab5eSdrh const char *zId; 11494b59ab5eSdrh getFunctionName(pExpr, &zId, &nId); 11504b59ab5eSdrh pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0); 11510bce8354Sdrh assert( pDef!=0 ); 1152*268380caSdrh nExpr = sqliteExprCodeExprList(pParse, pList, pDef->includeTypes); 1153701a0aebSdrh sqliteVdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER); 11546ec2733bSdrh break; 11556ec2733bSdrh } 115619a775c2Sdrh case TK_SELECT: { 115799fcd718Sdrh sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); 115819a775c2Sdrh break; 115919a775c2Sdrh } 1160fef5208cSdrh case TK_IN: { 1161fef5208cSdrh int addr; 116299fcd718Sdrh sqliteVdbeAddOp(v, OP_Integer, 1, 0); 1163fef5208cSdrh sqliteExprCode(pParse, pExpr->pLeft); 1164fef5208cSdrh addr = sqliteVdbeCurrentAddr(v); 1165f5905aa7Sdrh sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4); 1166f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1167f5905aa7Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 1168f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, addr+6); 1169fef5208cSdrh if( pExpr->pSelect ){ 1170f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6); 1171fef5208cSdrh }else{ 1172f5905aa7Sdrh sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6); 1173fef5208cSdrh } 117499fcd718Sdrh sqliteVdbeAddOp(v, OP_AddImm, -1, 0); 1175fef5208cSdrh break; 1176fef5208cSdrh } 1177fef5208cSdrh case TK_BETWEEN: { 1178f5905aa7Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1179f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 1180f5905aa7Sdrh sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); 1181f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Ge, 0, 0); 1182f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Pull, 1, 0); 1183f5905aa7Sdrh sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); 1184f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Le, 0, 0); 1185f5905aa7Sdrh sqliteVdbeAddOp(v, OP_And, 0, 0); 1186fef5208cSdrh break; 1187fef5208cSdrh } 118851e9a445Sdrh case TK_UPLUS: 1189a2e00042Sdrh case TK_AS: { 1190a2e00042Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1191a2e00042Sdrh break; 1192a2e00042Sdrh } 119317a7f8ddSdrh case TK_CASE: { 119417a7f8ddSdrh int expr_end_label; 1195f5905aa7Sdrh int jumpInst; 1196f5905aa7Sdrh int addr; 1197f5905aa7Sdrh int nExpr; 119817a7f8ddSdrh int i; 119917a7f8ddSdrh 120017a7f8ddSdrh assert(pExpr->pList); 120117a7f8ddSdrh assert((pExpr->pList->nExpr % 2) == 0); 120217a7f8ddSdrh assert(pExpr->pList->nExpr > 0); 1203f5905aa7Sdrh nExpr = pExpr->pList->nExpr; 1204f5905aa7Sdrh expr_end_label = sqliteVdbeMakeLabel(v); 120517a7f8ddSdrh if( pExpr->pLeft ){ 120617a7f8ddSdrh sqliteExprCode(pParse, pExpr->pLeft); 1207cce7d176Sdrh } 1208f5905aa7Sdrh for(i=0; i<nExpr; i=i+2){ 120917a7f8ddSdrh sqliteExprCode(pParse, pExpr->pList->a[i].pExpr); 121017a7f8ddSdrh if( pExpr->pLeft ){ 1211f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Dup, 1, 1); 1212f570f011Sdrh jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0); 1213f570f011Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1214f5905aa7Sdrh }else{ 1215f570f011Sdrh jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0); 121617a7f8ddSdrh } 121717a7f8ddSdrh sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr); 1218f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label); 1219f5905aa7Sdrh addr = sqliteVdbeCurrentAddr(v); 1220f5905aa7Sdrh sqliteVdbeChangeP2(v, jumpInst, addr); 122117a7f8ddSdrh } 1222f570f011Sdrh if( pExpr->pLeft ){ 1223f570f011Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1224f570f011Sdrh } 122517a7f8ddSdrh if( pExpr->pRight ){ 122617a7f8ddSdrh sqliteExprCode(pParse, pExpr->pRight); 122717a7f8ddSdrh }else{ 1228f5905aa7Sdrh sqliteVdbeAddOp(v, OP_String, 0, 0); 122917a7f8ddSdrh } 1230f5905aa7Sdrh sqliteVdbeResolveLabel(v, expr_end_label); 12316f34903eSdanielk1977 break; 12326f34903eSdanielk1977 } 12336f34903eSdanielk1977 case TK_RAISE: { 12346f34903eSdanielk1977 if( !pParse->trigStack ){ 1235da93d238Sdrh sqliteErrorMsg(pParse, 1236da93d238Sdrh "RAISE() may only be used within a trigger-program"); 12376f34903eSdanielk1977 pParse->nErr++; 12386f34903eSdanielk1977 return; 12396f34903eSdanielk1977 } 12406f34903eSdanielk1977 if( pExpr->iColumn == OE_Rollback || 12416f34903eSdanielk1977 pExpr->iColumn == OE_Abort || 12426f34903eSdanielk1977 pExpr->iColumn == OE_Fail ){ 1243701a0aebSdrh sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 1244701a0aebSdrh pExpr->token.z, pExpr->token.n); 1245701a0aebSdrh sqliteVdbeDequoteP3(v, -1); 12466f34903eSdanielk1977 } else { 12476f34903eSdanielk1977 assert( pExpr->iColumn == OE_Ignore ); 1248701a0aebSdrh sqliteVdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump, 1249701a0aebSdrh "(IGNORE jump)", 0); 12506f34903eSdanielk1977 } 125117a7f8ddSdrh } 125217a7f8ddSdrh break; 125317a7f8ddSdrh } 1254cce7d176Sdrh } 1255cce7d176Sdrh 1256cce7d176Sdrh /* 1257*268380caSdrh ** Generate code that pushes the value of every element of the given 1258*268380caSdrh ** expression list onto the stack. If the includeTypes flag is true, 1259*268380caSdrh ** then also push a string that is the datatype of each element onto 1260*268380caSdrh ** the stack after the value. 1261*268380caSdrh ** 1262*268380caSdrh ** Return the number of elements pushed onto the stack. 1263*268380caSdrh */ 1264*268380caSdrh int sqliteExprCodeExprList( 1265*268380caSdrh Parse *pParse, /* Parsing context */ 1266*268380caSdrh ExprList *pList, /* The expression list to be coded */ 1267*268380caSdrh int includeTypes /* TRUE to put datatypes on the stack too */ 1268*268380caSdrh ){ 1269*268380caSdrh struct ExprList_item *pItem; 1270*268380caSdrh int i, n; 1271*268380caSdrh Vdbe *v; 1272*268380caSdrh if( pList==0 ) return 0; 1273*268380caSdrh v = sqliteGetVdbe(pParse); 1274*268380caSdrh n = pList->nExpr; 1275*268380caSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 1276*268380caSdrh sqliteExprCode(pParse, pItem->pExpr); 1277*268380caSdrh if( includeTypes ){ 1278*268380caSdrh sqliteVdbeOp3(v, OP_String, 0, 0, 1279*268380caSdrh sqliteExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text", 1280*268380caSdrh P3_STATIC); 1281*268380caSdrh } 1282*268380caSdrh } 1283*268380caSdrh return includeTypes ? n*2 : n; 1284*268380caSdrh } 1285*268380caSdrh 1286*268380caSdrh /* 1287cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 1288cce7d176Sdrh ** to the label "dest" if the expression is true but execution 1289cce7d176Sdrh ** continues straight thru if the expression is false. 1290f5905aa7Sdrh ** 1291f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 1292f5905aa7Sdrh ** take the jump if the jumpIfNull flag is true. 1293cce7d176Sdrh */ 1294f5905aa7Sdrh void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1295cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1296cce7d176Sdrh int op = 0; 1297daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1298cce7d176Sdrh switch( pExpr->op ){ 1299cce7d176Sdrh case TK_LT: op = OP_Lt; break; 1300cce7d176Sdrh case TK_LE: op = OP_Le; break; 1301cce7d176Sdrh case TK_GT: op = OP_Gt; break; 1302cce7d176Sdrh case TK_GE: op = OP_Ge; break; 1303cce7d176Sdrh case TK_NE: op = OP_Ne; break; 1304cce7d176Sdrh case TK_EQ: op = OP_Eq; break; 1305cce7d176Sdrh case TK_ISNULL: op = OP_IsNull; break; 1306cce7d176Sdrh case TK_NOTNULL: op = OP_NotNull; break; 1307cce7d176Sdrh default: break; 1308cce7d176Sdrh } 1309cce7d176Sdrh switch( pExpr->op ){ 1310cce7d176Sdrh case TK_AND: { 1311cce7d176Sdrh int d2 = sqliteVdbeMakeLabel(v); 1312f5905aa7Sdrh sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); 1313f5905aa7Sdrh sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1314cce7d176Sdrh sqliteVdbeResolveLabel(v, d2); 1315cce7d176Sdrh break; 1316cce7d176Sdrh } 1317cce7d176Sdrh case TK_OR: { 1318f5905aa7Sdrh sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1319f5905aa7Sdrh sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 1320cce7d176Sdrh break; 1321cce7d176Sdrh } 1322cce7d176Sdrh case TK_NOT: { 1323f5905aa7Sdrh sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1324cce7d176Sdrh break; 1325cce7d176Sdrh } 1326cce7d176Sdrh case TK_LT: 1327cce7d176Sdrh case TK_LE: 1328cce7d176Sdrh case TK_GT: 1329cce7d176Sdrh case TK_GE: 1330cce7d176Sdrh case TK_NE: 13310ac65892Sdrh case TK_EQ: { 1332cce7d176Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1333cce7d176Sdrh sqliteExprCode(pParse, pExpr->pRight); 1334491791a8Sdrh if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ 1335c9b84a1fSdrh op += 6; /* Convert numeric opcodes to text opcodes */ 1336c9b84a1fSdrh } 1337f5905aa7Sdrh sqliteVdbeAddOp(v, op, jumpIfNull, dest); 1338cce7d176Sdrh break; 1339cce7d176Sdrh } 1340cce7d176Sdrh case TK_ISNULL: 1341cce7d176Sdrh case TK_NOTNULL: { 1342cce7d176Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1343f5905aa7Sdrh sqliteVdbeAddOp(v, op, 1, dest); 1344cce7d176Sdrh break; 1345cce7d176Sdrh } 1346fef5208cSdrh case TK_IN: { 1347f5905aa7Sdrh int addr; 1348cfab11bcSdrh sqliteExprCode(pParse, pExpr->pLeft); 1349f5905aa7Sdrh addr = sqliteVdbeCurrentAddr(v); 1350f5905aa7Sdrh sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); 1351f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1352f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1353fef5208cSdrh if( pExpr->pSelect ){ 135499fcd718Sdrh sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest); 1355fef5208cSdrh }else{ 135699fcd718Sdrh sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest); 1357fef5208cSdrh } 1358fef5208cSdrh break; 1359fef5208cSdrh } 1360fef5208cSdrh case TK_BETWEEN: { 1361f5905aa7Sdrh int addr; 1362fef5208cSdrh sqliteExprCode(pParse, pExpr->pLeft); 136399fcd718Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 1364fef5208cSdrh sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); 1365f5905aa7Sdrh addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0); 1366fef5208cSdrh sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); 1367f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest); 136899fcd718Sdrh sqliteVdbeAddOp(v, OP_Integer, 0, 0); 1369f5905aa7Sdrh sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v)); 137099fcd718Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1371fef5208cSdrh break; 1372fef5208cSdrh } 1373cce7d176Sdrh default: { 1374cce7d176Sdrh sqliteExprCode(pParse, pExpr); 1375f5905aa7Sdrh sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest); 1376cce7d176Sdrh break; 1377cce7d176Sdrh } 1378cce7d176Sdrh } 1379cce7d176Sdrh } 1380cce7d176Sdrh 1381cce7d176Sdrh /* 138266b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 1383cce7d176Sdrh ** to the label "dest" if the expression is false but execution 1384cce7d176Sdrh ** continues straight thru if the expression is true. 1385f5905aa7Sdrh ** 1386f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 1387f5905aa7Sdrh ** jump if jumpIfNull is true or fall through if jumpIfNull is false. 1388cce7d176Sdrh */ 1389f5905aa7Sdrh void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 1390cce7d176Sdrh Vdbe *v = pParse->pVdbe; 1391cce7d176Sdrh int op = 0; 1392daffd0e5Sdrh if( v==0 || pExpr==0 ) return; 1393cce7d176Sdrh switch( pExpr->op ){ 1394cce7d176Sdrh case TK_LT: op = OP_Ge; break; 1395cce7d176Sdrh case TK_LE: op = OP_Gt; break; 1396cce7d176Sdrh case TK_GT: op = OP_Le; break; 1397cce7d176Sdrh case TK_GE: op = OP_Lt; break; 1398cce7d176Sdrh case TK_NE: op = OP_Eq; break; 1399cce7d176Sdrh case TK_EQ: op = OP_Ne; break; 1400cce7d176Sdrh case TK_ISNULL: op = OP_NotNull; break; 1401cce7d176Sdrh case TK_NOTNULL: op = OP_IsNull; break; 1402cce7d176Sdrh default: break; 1403cce7d176Sdrh } 1404cce7d176Sdrh switch( pExpr->op ){ 1405cce7d176Sdrh case TK_AND: { 1406f5905aa7Sdrh sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 1407f5905aa7Sdrh sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1408cce7d176Sdrh break; 1409cce7d176Sdrh } 1410cce7d176Sdrh case TK_OR: { 1411cce7d176Sdrh int d2 = sqliteVdbeMakeLabel(v); 1412f5905aa7Sdrh sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); 1413f5905aa7Sdrh sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 1414cce7d176Sdrh sqliteVdbeResolveLabel(v, d2); 1415cce7d176Sdrh break; 1416cce7d176Sdrh } 1417cce7d176Sdrh case TK_NOT: { 1418f5905aa7Sdrh sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 1419cce7d176Sdrh break; 1420cce7d176Sdrh } 1421cce7d176Sdrh case TK_LT: 1422cce7d176Sdrh case TK_LE: 1423cce7d176Sdrh case TK_GT: 1424cce7d176Sdrh case TK_GE: 1425cce7d176Sdrh case TK_NE: 1426cce7d176Sdrh case TK_EQ: { 1427491791a8Sdrh if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){ 14288f619ccdSdrh /* Convert numeric comparison opcodes into text comparison opcodes. 14298f619ccdSdrh ** This step depends on the fact that the text comparision opcodes are 14308f619ccdSdrh ** always 6 greater than their corresponding numeric comparison 14318f619ccdSdrh ** opcodes. 14328f619ccdSdrh */ 14338f619ccdSdrh assert( OP_Eq+6 == OP_StrEq ); 14348f619ccdSdrh op += 6; 1435c9b84a1fSdrh } 1436cce7d176Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1437cce7d176Sdrh sqliteExprCode(pParse, pExpr->pRight); 1438f5905aa7Sdrh sqliteVdbeAddOp(v, op, jumpIfNull, dest); 1439cce7d176Sdrh break; 1440cce7d176Sdrh } 1441cce7d176Sdrh case TK_ISNULL: 1442cce7d176Sdrh case TK_NOTNULL: { 1443cce7d176Sdrh sqliteExprCode(pParse, pExpr->pLeft); 1444f5905aa7Sdrh sqliteVdbeAddOp(v, op, 1, dest); 1445cce7d176Sdrh break; 1446cce7d176Sdrh } 1447fef5208cSdrh case TK_IN: { 1448f5905aa7Sdrh int addr; 1449cfab11bcSdrh sqliteExprCode(pParse, pExpr->pLeft); 1450f5905aa7Sdrh addr = sqliteVdbeCurrentAddr(v); 1451f5905aa7Sdrh sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3); 1452f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 1453f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4); 1454fef5208cSdrh if( pExpr->pSelect ){ 145599fcd718Sdrh sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); 1456fef5208cSdrh }else{ 145799fcd718Sdrh sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); 1458fef5208cSdrh } 1459fef5208cSdrh break; 1460fef5208cSdrh } 1461fef5208cSdrh case TK_BETWEEN: { 1462fef5208cSdrh int addr; 1463fef5208cSdrh sqliteExprCode(pParse, pExpr->pLeft); 146499fcd718Sdrh sqliteVdbeAddOp(v, OP_Dup, 0, 0); 1465fef5208cSdrh sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); 1466fef5208cSdrh addr = sqliteVdbeCurrentAddr(v); 1467f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3); 146899fcd718Sdrh sqliteVdbeAddOp(v, OP_Pop, 1, 0); 146999fcd718Sdrh sqliteVdbeAddOp(v, OP_Goto, 0, dest); 1470fef5208cSdrh sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); 1471f5905aa7Sdrh sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest); 1472fef5208cSdrh break; 1473fef5208cSdrh } 1474cce7d176Sdrh default: { 1475cce7d176Sdrh sqliteExprCode(pParse, pExpr); 1476461c281aSdrh sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest); 1477cce7d176Sdrh break; 1478cce7d176Sdrh } 1479cce7d176Sdrh } 1480cce7d176Sdrh } 14812282792aSdrh 14822282792aSdrh /* 14832282792aSdrh ** Do a deep comparison of two expression trees. Return TRUE (non-zero) 14842282792aSdrh ** if they are identical and return FALSE if they differ in any way. 14852282792aSdrh */ 1486d8bc7086Sdrh int sqliteExprCompare(Expr *pA, Expr *pB){ 14872282792aSdrh int i; 14882282792aSdrh if( pA==0 ){ 14892282792aSdrh return pB==0; 14902282792aSdrh }else if( pB==0 ){ 14912282792aSdrh return 0; 14922282792aSdrh } 14932282792aSdrh if( pA->op!=pB->op ) return 0; 1494d8bc7086Sdrh if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0; 1495d8bc7086Sdrh if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0; 14962282792aSdrh if( pA->pList ){ 14972282792aSdrh if( pB->pList==0 ) return 0; 14982282792aSdrh if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; 14992282792aSdrh for(i=0; i<pA->pList->nExpr; i++){ 1500d8bc7086Sdrh if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ 15012282792aSdrh return 0; 15022282792aSdrh } 15032282792aSdrh } 15042282792aSdrh }else if( pB->pList ){ 15052282792aSdrh return 0; 15062282792aSdrh } 15072282792aSdrh if( pA->pSelect || pB->pSelect ) return 0; 15082f2c01e5Sdrh if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; 15092282792aSdrh if( pA->token.z ){ 15102282792aSdrh if( pB->token.z==0 ) return 0; 15116977fea8Sdrh if( pB->token.n!=pA->token.n ) return 0; 15126977fea8Sdrh if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0; 15132282792aSdrh } 15142282792aSdrh return 1; 15152282792aSdrh } 15162282792aSdrh 15172282792aSdrh /* 15182282792aSdrh ** Add a new element to the pParse->aAgg[] array and return its index. 15192282792aSdrh */ 15202282792aSdrh static int appendAggInfo(Parse *pParse){ 15212282792aSdrh if( (pParse->nAgg & 0x7)==0 ){ 15222282792aSdrh int amt = pParse->nAgg + 8; 15236d4abfbeSdrh AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); 15246d4abfbeSdrh if( aAgg==0 ){ 15252282792aSdrh return -1; 15262282792aSdrh } 15276d4abfbeSdrh pParse->aAgg = aAgg; 15282282792aSdrh } 15292282792aSdrh memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); 15302282792aSdrh return pParse->nAgg++; 15312282792aSdrh } 15322282792aSdrh 15332282792aSdrh /* 15342282792aSdrh ** Analyze the given expression looking for aggregate functions and 15352282792aSdrh ** for variables that need to be added to the pParse->aAgg[] array. 15362282792aSdrh ** Make additional entries to the pParse->aAgg[] array as necessary. 15372282792aSdrh ** 15382282792aSdrh ** This routine should only be called after the expression has been 15392282792aSdrh ** analyzed by sqliteExprResolveIds() and sqliteExprCheck(). 15402282792aSdrh ** 15412282792aSdrh ** If errors are seen, leave an error message in zErrMsg and return 15422282792aSdrh ** the number of errors. 15432282792aSdrh */ 15442282792aSdrh int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ 15452282792aSdrh int i; 15462282792aSdrh AggExpr *aAgg; 15472282792aSdrh int nErr = 0; 15482282792aSdrh 15492282792aSdrh if( pExpr==0 ) return 0; 15502282792aSdrh switch( pExpr->op ){ 1551967e8b73Sdrh case TK_COLUMN: { 15522282792aSdrh aAgg = pParse->aAgg; 15532282792aSdrh for(i=0; i<pParse->nAgg; i++){ 15542282792aSdrh if( aAgg[i].isAgg ) continue; 15552282792aSdrh if( aAgg[i].pExpr->iTable==pExpr->iTable 1556967e8b73Sdrh && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ 15572282792aSdrh break; 15582282792aSdrh } 15592282792aSdrh } 15602282792aSdrh if( i>=pParse->nAgg ){ 15612282792aSdrh i = appendAggInfo(pParse); 15622282792aSdrh if( i<0 ) return 1; 15632282792aSdrh pParse->aAgg[i].isAgg = 0; 15642282792aSdrh pParse->aAgg[i].pExpr = pExpr; 15652282792aSdrh } 1566aaf88729Sdrh pExpr->iAgg = i; 15672282792aSdrh break; 15682282792aSdrh } 15692282792aSdrh case TK_AGG_FUNCTION: { 15702282792aSdrh aAgg = pParse->aAgg; 15712282792aSdrh for(i=0; i<pParse->nAgg; i++){ 15722282792aSdrh if( !aAgg[i].isAgg ) continue; 1573d8bc7086Sdrh if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){ 15742282792aSdrh break; 15752282792aSdrh } 15762282792aSdrh } 15772282792aSdrh if( i>=pParse->nAgg ){ 15782282792aSdrh i = appendAggInfo(pParse); 15792282792aSdrh if( i<0 ) return 1; 15802282792aSdrh pParse->aAgg[i].isAgg = 1; 15812282792aSdrh pParse->aAgg[i].pExpr = pExpr; 15820bce8354Sdrh pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db, 15836977fea8Sdrh pExpr->token.z, pExpr->token.n, 1584f55f25f0Sdrh pExpr->pList ? pExpr->pList->nExpr : 0, 0); 15852282792aSdrh } 15862282792aSdrh pExpr->iAgg = i; 15872282792aSdrh break; 15882282792aSdrh } 15892282792aSdrh default: { 15902282792aSdrh if( pExpr->pLeft ){ 15912282792aSdrh nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft); 15922282792aSdrh } 15932282792aSdrh if( nErr==0 && pExpr->pRight ){ 15942282792aSdrh nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight); 15952282792aSdrh } 15962282792aSdrh if( nErr==0 && pExpr->pList ){ 15972282792aSdrh int n = pExpr->pList->nExpr; 15982282792aSdrh int i; 15992282792aSdrh for(i=0; nErr==0 && i<n; i++){ 16002282792aSdrh nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); 16012282792aSdrh } 16022282792aSdrh } 16032282792aSdrh break; 16042282792aSdrh } 16052282792aSdrh } 16062282792aSdrh return nErr; 16072282792aSdrh } 16088e0a2f90Sdrh 16098e0a2f90Sdrh /* 16108e0a2f90Sdrh ** Locate a user function given a name and a number of arguments. 16110bce8354Sdrh ** Return a pointer to the FuncDef structure that defines that 16128e0a2f90Sdrh ** function, or return NULL if the function does not exist. 16138e0a2f90Sdrh ** 16140bce8354Sdrh ** If the createFlag argument is true, then a new (blank) FuncDef 16158e0a2f90Sdrh ** structure is created and liked into the "db" structure if a 16168e0a2f90Sdrh ** no matching function previously existed. When createFlag is true 16178e0a2f90Sdrh ** and the nArg parameter is -1, then only a function that accepts 16188e0a2f90Sdrh ** any number of arguments will be returned. 16198e0a2f90Sdrh ** 16208e0a2f90Sdrh ** If createFlag is false and nArg is -1, then the first valid 16218e0a2f90Sdrh ** function found is returned. A function is valid if either xFunc 16228e0a2f90Sdrh ** or xStep is non-zero. 16238e0a2f90Sdrh */ 16240bce8354Sdrh FuncDef *sqliteFindFunction( 16258e0a2f90Sdrh sqlite *db, /* An open database */ 16268e0a2f90Sdrh const char *zName, /* Name of the function. Not null-terminated */ 16278e0a2f90Sdrh int nName, /* Number of characters in the name */ 16288e0a2f90Sdrh int nArg, /* Number of arguments. -1 means any number */ 16298e0a2f90Sdrh int createFlag /* Create new entry if true and does not otherwise exist */ 16308e0a2f90Sdrh ){ 16310bce8354Sdrh FuncDef *pFirst, *p, *pMaybe; 16320bce8354Sdrh pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName); 16331350b030Sdrh if( p && !createFlag && nArg<0 ){ 16348e0a2f90Sdrh while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } 16358e0a2f90Sdrh return p; 16368e0a2f90Sdrh } 16378e0a2f90Sdrh pMaybe = 0; 16388e0a2f90Sdrh while( p && p->nArg!=nArg ){ 16398e0a2f90Sdrh if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; 16408e0a2f90Sdrh p = p->pNext; 16418e0a2f90Sdrh } 16428e0a2f90Sdrh if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ 16438e0a2f90Sdrh return 0; 16448e0a2f90Sdrh } 16458e0a2f90Sdrh if( p==0 && pMaybe ){ 16468e0a2f90Sdrh assert( createFlag==0 ); 16478e0a2f90Sdrh return pMaybe; 16488e0a2f90Sdrh } 164989425d5eSdrh if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){ 16508e0a2f90Sdrh p->nArg = nArg; 16518e0a2f90Sdrh p->pNext = pFirst; 1652c9b84a1fSdrh p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC; 16530bce8354Sdrh sqliteHashInsert(&db->aFunc, zName, nName, (void*)p); 16548e0a2f90Sdrh } 16558e0a2f90Sdrh return p; 16568e0a2f90Sdrh } 1657