17d10d5a6Sdrh /* 27d10d5a6Sdrh ** 2008 August 18 37d10d5a6Sdrh ** 47d10d5a6Sdrh ** The author disclaims copyright to this source code. In place of 57d10d5a6Sdrh ** a legal notice, here is a blessing: 67d10d5a6Sdrh ** 77d10d5a6Sdrh ** May you do good and not evil. 87d10d5a6Sdrh ** May you find forgiveness for yourself and forgive others. 97d10d5a6Sdrh ** May you share freely, never taking more than you give. 107d10d5a6Sdrh ** 117d10d5a6Sdrh ************************************************************************* 127d10d5a6Sdrh ** 137d10d5a6Sdrh ** This file contains routines used for walking the parser tree and 147d10d5a6Sdrh ** resolve all identifiers by associating them with a particular 157d10d5a6Sdrh ** table and column. 167d10d5a6Sdrh */ 177d10d5a6Sdrh #include "sqliteInt.h" 187d10d5a6Sdrh #include <stdlib.h> 197d10d5a6Sdrh #include <string.h> 207d10d5a6Sdrh 217d10d5a6Sdrh /* 22ed551b95Sdrh ** Walk the expression tree pExpr and increase the aggregate function 23ed551b95Sdrh ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node. 24ed551b95Sdrh ** This needs to occur when copying a TK_AGG_FUNCTION node from an 25ed551b95Sdrh ** outer query into an inner subquery. 26ed551b95Sdrh ** 27ed551b95Sdrh ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) 28ed551b95Sdrh ** is a helper function - a callback for the tree walker. 29ed551b95Sdrh */ 30ed551b95Sdrh static int incrAggDepth(Walker *pWalker, Expr *pExpr){ 31ed551b95Sdrh if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i; 32ed551b95Sdrh return WRC_Continue; 33ed551b95Sdrh } 34ed551b95Sdrh static void incrAggFunctionDepth(Expr *pExpr, int N){ 35ed551b95Sdrh if( N>0 ){ 36ed551b95Sdrh Walker w; 37ed551b95Sdrh memset(&w, 0, sizeof(w)); 38ed551b95Sdrh w.xExprCallback = incrAggDepth; 39ed551b95Sdrh w.u.i = N; 40ed551b95Sdrh sqlite3WalkExpr(&w, pExpr); 41ed551b95Sdrh } 42ed551b95Sdrh } 43ed551b95Sdrh 44ed551b95Sdrh /* 458b213899Sdrh ** Turn the pExpr expression into an alias for the iCol-th column of the 468b213899Sdrh ** result set in pEList. 478b213899Sdrh ** 488b213899Sdrh ** If the result set column is a simple column reference, then this routine 498b213899Sdrh ** makes an exact copy. But for any other kind of expression, this 508b213899Sdrh ** routine make a copy of the result set column as the argument to the 518b213899Sdrh ** TK_AS operator. The TK_AS operator causes the expression to be 528b213899Sdrh ** evaluated just once and then reused for each alias. 538b213899Sdrh ** 548b213899Sdrh ** The reason for suppressing the TK_AS term when the expression is a simple 558b213899Sdrh ** column reference is so that the column reference will be recognized as 568b213899Sdrh ** usable by indices within the WHERE clause processing logic. 578b213899Sdrh ** 58e35463b3Sdrh ** The TK_AS operator is inhibited if zType[0]=='G'. This means 598b213899Sdrh ** that in a GROUP BY clause, the expression is evaluated twice. Hence: 608b213899Sdrh ** 618b213899Sdrh ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY x 628b213899Sdrh ** 638b213899Sdrh ** Is equivalent to: 648b213899Sdrh ** 658b213899Sdrh ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5 668b213899Sdrh ** 678b213899Sdrh ** The result of random()%5 in the GROUP BY clause is probably different 68e35463b3Sdrh ** from the result in the result-set. On the other hand Standard SQL does 69e35463b3Sdrh ** not allow the GROUP BY clause to contain references to result-set columns. 70e35463b3Sdrh ** So this should never come up in well-formed queries. 71ed551b95Sdrh ** 720a8a406eSdrh ** If the reference is followed by a COLLATE operator, then make sure 730a8a406eSdrh ** the COLLATE operator is preserved. For example: 740a8a406eSdrh ** 750a8a406eSdrh ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase; 760a8a406eSdrh ** 770a8a406eSdrh ** Should be transformed into: 780a8a406eSdrh ** 790a8a406eSdrh ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; 800a8a406eSdrh ** 81ed551b95Sdrh ** The nSubquery parameter specifies how many levels of subquery the 82ed551b95Sdrh ** alias is removed from the original expression. The usually value is 83ed551b95Sdrh ** zero but it might be more if the alias is contained within a subquery 84ed551b95Sdrh ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION 85ed551b95Sdrh ** structures must be increased by the nSubquery amount. 868b213899Sdrh */ 878b213899Sdrh static void resolveAlias( 888b213899Sdrh Parse *pParse, /* Parsing context */ 898b213899Sdrh ExprList *pEList, /* A result set */ 908b213899Sdrh int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ 918b213899Sdrh Expr *pExpr, /* Transform this into an alias to the result set */ 92ed551b95Sdrh const char *zType, /* "GROUP" or "ORDER" or "" */ 93ed551b95Sdrh int nSubquery /* Number of subqueries that the label is moving */ 948b213899Sdrh ){ 958b213899Sdrh Expr *pOrig; /* The iCol-th column of the result set */ 968b213899Sdrh Expr *pDup; /* Copy of pOrig */ 978b213899Sdrh sqlite3 *db; /* The database connection */ 988b213899Sdrh 998b213899Sdrh assert( iCol>=0 && iCol<pEList->nExpr ); 1008b213899Sdrh pOrig = pEList->a[iCol].pExpr; 1018b213899Sdrh assert( pOrig!=0 ); 1028b213899Sdrh assert( pOrig->flags & EP_Resolved ); 1038b213899Sdrh db = pParse->db; 1046ab3a2ecSdanielk1977 pDup = sqlite3ExprDup(db, pOrig, 0); 1050a8a406eSdrh if( pDup==0 ) return; 1060a8a406eSdrh if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ 107ed551b95Sdrh incrAggFunctionDepth(pDup, nSubquery); 1088b213899Sdrh pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0); 1098b213899Sdrh if( pDup==0 ) return; 110a4c3c87eSdrh ExprSetProperty(pDup, EP_Skip); 111c2acc4e4Sdrh if( pEList->a[iCol].u.x.iAlias==0 ){ 112c2acc4e4Sdrh pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias); 1138b213899Sdrh } 114c2acc4e4Sdrh pDup->iTable = pEList->a[iCol].u.x.iAlias; 115b7916a78Sdrh } 1160a8a406eSdrh if( pExpr->op==TK_COLLATE ){ 1170a8a406eSdrh pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); 1180a8a406eSdrh } 119f6963f99Sdan 120f6963f99Sdan /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 121f6963f99Sdan ** prevents ExprDelete() from deleting the Expr structure itself, 122f6963f99Sdan ** allowing it to be repopulated by the memcpy() on the following line. 123bd13d34bSdrh ** The pExpr->u.zToken might point into memory that will be freed by the 124bd13d34bSdrh ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to 125bd13d34bSdrh ** make a copy of the token before doing the sqlite3DbFree(). 126f6963f99Sdan */ 127f6963f99Sdan ExprSetProperty(pExpr, EP_Static); 128f6963f99Sdan sqlite3ExprDelete(db, pExpr); 1298b213899Sdrh memcpy(pExpr, pDup, sizeof(*pExpr)); 1300a8a406eSdrh if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){ 1310a8a406eSdrh assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 ); 1320a8a406eSdrh pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken); 133c5cd1249Sdrh pExpr->flags |= EP_MemToken; 1340a8a406eSdrh } 1358b213899Sdrh sqlite3DbFree(db, pDup); 1368b213899Sdrh } 1378b213899Sdrh 138e802c5daSdrh 139e802c5daSdrh /* 140e802c5daSdrh ** Return TRUE if the name zCol occurs anywhere in the USING clause. 141e802c5daSdrh ** 142e802c5daSdrh ** Return FALSE if the USING clause is NULL or if it does not contain 143e802c5daSdrh ** zCol. 144e802c5daSdrh */ 145e802c5daSdrh static int nameInUsingClause(IdList *pUsing, const char *zCol){ 146e802c5daSdrh if( pUsing ){ 147e802c5daSdrh int k; 148e802c5daSdrh for(k=0; k<pUsing->nId; k++){ 149e802c5daSdrh if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1; 150e802c5daSdrh } 151e802c5daSdrh } 152e802c5daSdrh return 0; 153e802c5daSdrh } 154e802c5daSdrh 1553e3f1a5bSdrh /* 1563e3f1a5bSdrh ** Subqueries stores the original database, table and column names for their 1573e3f1a5bSdrh ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN". 1583e3f1a5bSdrh ** Check to see if the zSpan given to this routine matches the zDb, zTab, 1593e3f1a5bSdrh ** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will 1603e3f1a5bSdrh ** match anything. 1613e3f1a5bSdrh */ 1623e3f1a5bSdrh int sqlite3MatchSpanName( 1633e3f1a5bSdrh const char *zSpan, 1643e3f1a5bSdrh const char *zCol, 1653e3f1a5bSdrh const char *zTab, 1663e3f1a5bSdrh const char *zDb 1673e3f1a5bSdrh ){ 1683e3f1a5bSdrh int n; 1693e3f1a5bSdrh for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} 170dd1dd489Sdrh if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){ 1713e3f1a5bSdrh return 0; 1723e3f1a5bSdrh } 1733e3f1a5bSdrh zSpan += n+1; 1743e3f1a5bSdrh for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} 175dd1dd489Sdrh if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){ 1763e3f1a5bSdrh return 0; 1773e3f1a5bSdrh } 1783e3f1a5bSdrh zSpan += n+1; 1793e3f1a5bSdrh if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){ 1803e3f1a5bSdrh return 0; 1813e3f1a5bSdrh } 1823e3f1a5bSdrh return 1; 1833e3f1a5bSdrh } 184e802c5daSdrh 1858b213899Sdrh /* 1867d10d5a6Sdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up 1877d10d5a6Sdrh ** that name in the set of source tables in pSrcList and make the pExpr 1887d10d5a6Sdrh ** expression node refer back to that source column. The following changes 1897d10d5a6Sdrh ** are made to pExpr: 1907d10d5a6Sdrh ** 1917d10d5a6Sdrh ** pExpr->iDb Set the index in db->aDb[] of the database X 1927d10d5a6Sdrh ** (even if X is implied). 1937d10d5a6Sdrh ** pExpr->iTable Set to the cursor number for the table obtained 1947d10d5a6Sdrh ** from pSrcList. 1957d10d5a6Sdrh ** pExpr->pTab Points to the Table structure of X.Y (even if 1967d10d5a6Sdrh ** X and/or Y are implied.) 1977d10d5a6Sdrh ** pExpr->iColumn Set to the column number within the table. 1987d10d5a6Sdrh ** pExpr->op Set to TK_COLUMN. 1997d10d5a6Sdrh ** pExpr->pLeft Any expression this points to is deleted 2007d10d5a6Sdrh ** pExpr->pRight Any expression this points to is deleted. 2017d10d5a6Sdrh ** 202b7916a78Sdrh ** The zDb variable is the name of the database (the "X"). This value may be 2037d10d5a6Sdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database 204b7916a78Sdrh ** can be used. The zTable variable is the name of the table (the "Y"). This 205b7916a78Sdrh ** value can be NULL if zDb is also NULL. If zTable is NULL it 2067d10d5a6Sdrh ** means that the form of the name is Z and that columns from any table 2077d10d5a6Sdrh ** can be used. 2087d10d5a6Sdrh ** 2097d10d5a6Sdrh ** If the name cannot be resolved unambiguously, leave an error message 210f7828b5cSdrh ** in pParse and return WRC_Abort. Return WRC_Prune on success. 2117d10d5a6Sdrh */ 2127d10d5a6Sdrh static int lookupName( 2137d10d5a6Sdrh Parse *pParse, /* The parsing context */ 214b7916a78Sdrh const char *zDb, /* Name of the database containing table, or NULL */ 215b7916a78Sdrh const char *zTab, /* Name of table containing column, or NULL */ 216b7916a78Sdrh const char *zCol, /* Name of the column. */ 2177d10d5a6Sdrh NameContext *pNC, /* The name context used to resolve the name */ 2187d10d5a6Sdrh Expr *pExpr /* Make this EXPR node point to the selected column */ 2197d10d5a6Sdrh ){ 2207d10d5a6Sdrh int i, j; /* Loop counters */ 2217d10d5a6Sdrh int cnt = 0; /* Number of matching column names */ 2227d10d5a6Sdrh int cntTab = 0; /* Number of matching table names */ 223ed551b95Sdrh int nSubquery = 0; /* How many levels of subquery */ 2247d10d5a6Sdrh sqlite3 *db = pParse->db; /* The database connection */ 2257d10d5a6Sdrh struct SrcList_item *pItem; /* Use for looping over pSrcList items */ 2267d10d5a6Sdrh struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ 2277d10d5a6Sdrh NameContext *pTopNC = pNC; /* First namecontext in the list */ 2287d10d5a6Sdrh Schema *pSchema = 0; /* Schema of the expression */ 22911f9b033Sdrh int isTrigger = 0; /* True if resolved to a trigger column */ 23011f9b033Sdrh Table *pTab = 0; /* Table hold the row */ 23111f9b033Sdrh Column *pCol; /* A column of pTab */ 2327d10d5a6Sdrh 233e34c647eSshane assert( pNC ); /* the name context cannot be NULL. */ 234b7916a78Sdrh assert( zCol ); /* The Z in X.Y.Z cannot be NULL */ 235c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 2367d10d5a6Sdrh 2377d10d5a6Sdrh /* Initialize the node to no-match */ 2387d10d5a6Sdrh pExpr->iTable = -1; 2397d10d5a6Sdrh pExpr->pTab = 0; 240ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 2417d10d5a6Sdrh 2428f25d18bSdrh /* Translate the schema name in zDb into a pointer to the corresponding 2438f25d18bSdrh ** schema. If not found, pSchema will remain NULL and nothing will match 2448f25d18bSdrh ** resulting in an appropriate error message toward the end of this routine 2458f25d18bSdrh */ 2468f25d18bSdrh if( zDb ){ 2471e7d43c9Sdrh testcase( pNC->ncFlags & NC_PartIdx ); 2481e7d43c9Sdrh testcase( pNC->ncFlags & NC_IsCheck ); 2491e7d43c9Sdrh if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){ 2501e7d43c9Sdrh /* Silently ignore database qualifiers inside CHECK constraints and partial 2511e7d43c9Sdrh ** indices. Do not raise errors because that might break legacy and 2521e7d43c9Sdrh ** because it does not hurt anything to just ignore the database name. */ 2531e7d43c9Sdrh zDb = 0; 2541e7d43c9Sdrh }else{ 2558f25d18bSdrh for(i=0; i<db->nDb; i++){ 2568f25d18bSdrh assert( db->aDb[i].zName ); 2578f25d18bSdrh if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){ 2588f25d18bSdrh pSchema = db->aDb[i].pSchema; 2598f25d18bSdrh break; 2608f25d18bSdrh } 2618f25d18bSdrh } 2628f25d18bSdrh } 2631e7d43c9Sdrh } 2648f25d18bSdrh 2657d10d5a6Sdrh /* Start at the inner-most context and move outward until a match is found */ 2667d10d5a6Sdrh while( pNC && cnt==0 ){ 2677d10d5a6Sdrh ExprList *pEList; 2687d10d5a6Sdrh SrcList *pSrcList = pNC->pSrcList; 2697d10d5a6Sdrh 2707d10d5a6Sdrh if( pSrcList ){ 2717d10d5a6Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ 2727d10d5a6Sdrh pTab = pItem->pTab; 273f436620eSdrh assert( pTab!=0 && pTab->zName!=0 ); 2747d10d5a6Sdrh assert( pTab->nCol>0 ); 2758f25d18bSdrh if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){ 2768f25d18bSdrh int hit = 0; 277928d9c62Sdrh pEList = pItem->pSelect->pEList; 2788f25d18bSdrh for(j=0; j<pEList->nExpr; j++){ 2793e3f1a5bSdrh if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){ 2808f25d18bSdrh cnt++; 2818f25d18bSdrh cntTab = 2; 2828f25d18bSdrh pMatch = pItem; 2838f25d18bSdrh pExpr->iColumn = j; 28438b384a0Sdrh hit = 1; 2858f25d18bSdrh } 2868f25d18bSdrh } 2878f25d18bSdrh if( hit || zTab==0 ) continue; 2888f25d18bSdrh } 289c75e09c7Sdrh if( zDb && pTab->pSchema!=pSchema ){ 290c75e09c7Sdrh continue; 291c75e09c7Sdrh } 2927d10d5a6Sdrh if( zTab ){ 2938f25d18bSdrh const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName; 2948f25d18bSdrh assert( zTabName!=0 ); 2958f25d18bSdrh if( sqlite3StrICmp(zTabName, zTab)!=0 ){ 29673c0fdc7Sdrh continue; 29773c0fdc7Sdrh } 2987d10d5a6Sdrh } 2997d10d5a6Sdrh if( 0==(cntTab++) ){ 3007d10d5a6Sdrh pMatch = pItem; 3017d10d5a6Sdrh } 3027d10d5a6Sdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 3037d10d5a6Sdrh if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 304e802c5daSdrh /* If there has been exactly one prior match and this match 305e802c5daSdrh ** is for the right-hand table of a NATURAL JOIN or is in a 306e802c5daSdrh ** USING clause, then skip this match. 307e802c5daSdrh */ 308e802c5daSdrh if( cnt==1 ){ 309e802c5daSdrh if( pItem->jointype & JT_NATURAL ) continue; 310e802c5daSdrh if( nameInUsingClause(pItem->pUsing, zCol) ) continue; 311e802c5daSdrh } 3127d10d5a6Sdrh cnt++; 3137d10d5a6Sdrh pMatch = pItem; 3147d10d5a6Sdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 315cf697396Sshane pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; 3167d10d5a6Sdrh break; 3177d10d5a6Sdrh } 3187d10d5a6Sdrh } 3197d10d5a6Sdrh } 3208f25d18bSdrh if( pMatch ){ 3218f25d18bSdrh pExpr->iTable = pMatch->iCursor; 3228f25d18bSdrh pExpr->pTab = pMatch->pTab; 3238f25d18bSdrh pSchema = pExpr->pTab->pSchema; 3247d10d5a6Sdrh } 3258f25d18bSdrh } /* if( pSrcList ) */ 3267d10d5a6Sdrh 3277d10d5a6Sdrh #ifndef SQLITE_OMIT_TRIGGER 3287d10d5a6Sdrh /* If we have not already resolved the name, then maybe 3297d10d5a6Sdrh ** it is a new.* or old.* trigger argument reference 3307d10d5a6Sdrh */ 33111f9b033Sdrh if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){ 33265a7cd16Sdan int op = pParse->eTriggerOp; 33365a7cd16Sdan assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT ); 33465a7cd16Sdan if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){ 335165921a7Sdan pExpr->iTable = 1; 336165921a7Sdan pTab = pParse->pTriggerTab; 33765a7cd16Sdan }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){ 338165921a7Sdan pExpr->iTable = 0; 339165921a7Sdan pTab = pParse->pTriggerTab; 3407d10d5a6Sdrh } 3417d10d5a6Sdrh 3427d10d5a6Sdrh if( pTab ){ 3437d10d5a6Sdrh int iCol; 3447d10d5a6Sdrh pSchema = pTab->pSchema; 3457d10d5a6Sdrh cntTab++; 346511717c6Sdrh for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){ 3477d10d5a6Sdrh if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 3482bd93516Sdan if( iCol==pTab->iPKey ){ 3492bd93516Sdan iCol = -1; 3502bd93516Sdan } 3517d10d5a6Sdrh break; 3527d10d5a6Sdrh } 3537d10d5a6Sdrh } 35411f9b033Sdrh if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){ 355*bbbb0e80Sdrh /* IMP: R-24309-18625 */ 356*bbbb0e80Sdrh /* IMP: R-44911-55124 */ 357*bbbb0e80Sdrh iCol = -1; 3587d10d5a6Sdrh } 3592bd93516Sdan if( iCol<pTab->nCol ){ 3602bd93516Sdan cnt++; 3612bd93516Sdan if( iCol<0 ){ 3622bd93516Sdan pExpr->affinity = SQLITE_AFF_INTEGER; 3632832ad42Sdan }else if( pExpr->iTable==0 ){ 3642832ad42Sdan testcase( iCol==31 ); 3652832ad42Sdan testcase( iCol==32 ); 3662832ad42Sdan pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); 367bb5f168fSdan }else{ 368bb5f168fSdan testcase( iCol==31 ); 369bb5f168fSdan testcase( iCol==32 ); 370bb5f168fSdan pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); 3712bd93516Sdan } 372cea72b2dSshane pExpr->iColumn = (i16)iCol; 3732bd93516Sdan pExpr->pTab = pTab; 3742bd93516Sdan isTrigger = 1; 3752bd93516Sdan } 3762bd93516Sdan } 3777d10d5a6Sdrh } 3787d10d5a6Sdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 3797d10d5a6Sdrh 3807d10d5a6Sdrh /* 3817d10d5a6Sdrh ** Perhaps the name is a reference to the ROWID 3827d10d5a6Sdrh */ 38311f9b033Sdrh assert( pTab!=0 || cntTab==0 ); 38411f9b033Sdrh if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) && HasRowid(pTab) ){ 3857d10d5a6Sdrh cnt = 1; 386c79c761fSdrh pExpr->iColumn = -1; /* IMP: R-44911-55124 */ 3877d10d5a6Sdrh pExpr->affinity = SQLITE_AFF_INTEGER; 3887d10d5a6Sdrh } 3897d10d5a6Sdrh 3907d10d5a6Sdrh /* 3917d10d5a6Sdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 3927d10d5a6Sdrh ** might refer to an result-set alias. This happens, for example, when 3937d10d5a6Sdrh ** we are resolving names in the WHERE clause of the following command: 3947d10d5a6Sdrh ** 3957d10d5a6Sdrh ** SELECT a+b AS x FROM table WHERE x<10; 3967d10d5a6Sdrh ** 3977d10d5a6Sdrh ** In cases like this, replace pExpr with a copy of the expression that 3987d10d5a6Sdrh ** forms the result set entry ("a+b" in the example) and return immediately. 3997d10d5a6Sdrh ** Note that the expression in the result set should have already been 4007d10d5a6Sdrh ** resolved by the time the WHERE clause is resolved. 401e35463b3Sdrh ** 402e35463b3Sdrh ** The ability to use an output result-set column in the WHERE, GROUP BY, 403e35463b3Sdrh ** or HAVING clauses, or as part of a larger expression in the ORDRE BY 404e35463b3Sdrh ** clause is not standard SQL. This is a (goofy) SQLite extension, that 405e35463b3Sdrh ** is supported for backwards compatibility only. TO DO: Issue a warning 406e35463b3Sdrh ** on sqlite3_log() whenever the capability is used. 4077d10d5a6Sdrh */ 408a3a5bd9bSdrh if( (pEList = pNC->pEList)!=0 409a3a5bd9bSdrh && zTab==0 410e35463b3Sdrh && cnt==0 411a3a5bd9bSdrh ){ 4127d10d5a6Sdrh for(j=0; j<pEList->nExpr; j++){ 4137d10d5a6Sdrh char *zAs = pEList->a[j].zName; 4147d10d5a6Sdrh if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 4158b213899Sdrh Expr *pOrig; 4167d10d5a6Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 ); 4176ab3a2ecSdanielk1977 assert( pExpr->x.pList==0 ); 4186ab3a2ecSdanielk1977 assert( pExpr->x.pSelect==0 ); 4197d10d5a6Sdrh pOrig = pEList->a[j].pExpr; 420a51009b2Sdrh if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){ 4217d10d5a6Sdrh sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); 422f7828b5cSdrh return WRC_Abort; 4237d10d5a6Sdrh } 424ed551b95Sdrh resolveAlias(pParse, pEList, j, pExpr, "", nSubquery); 4257d10d5a6Sdrh cnt = 1; 4267d10d5a6Sdrh pMatch = 0; 4277d10d5a6Sdrh assert( zTab==0 && zDb==0 ); 428b7916a78Sdrh goto lookupname_end; 4297d10d5a6Sdrh } 4307d10d5a6Sdrh } 4317d10d5a6Sdrh } 4327d10d5a6Sdrh 4337d10d5a6Sdrh /* Advance to the next name context. The loop will exit when either 4347d10d5a6Sdrh ** we have a match (cnt>0) or when we run out of name contexts. 4357d10d5a6Sdrh */ 4367d10d5a6Sdrh if( cnt==0 ){ 4377d10d5a6Sdrh pNC = pNC->pNext; 438ed551b95Sdrh nSubquery++; 4397d10d5a6Sdrh } 4407d10d5a6Sdrh } 4417d10d5a6Sdrh 4427d10d5a6Sdrh /* 4437d10d5a6Sdrh ** If X and Y are NULL (in other words if only the column name Z is 4447d10d5a6Sdrh ** supplied) and the value of Z is enclosed in double-quotes, then 4457d10d5a6Sdrh ** Z is a string literal if it doesn't match any column names. In that 4467d10d5a6Sdrh ** case, we need to return right away and not make any changes to 4477d10d5a6Sdrh ** pExpr. 4487d10d5a6Sdrh ** 4497d10d5a6Sdrh ** Because no reference was made to outer contexts, the pNC->nRef 4507d10d5a6Sdrh ** fields are not changed in any context. 4517d10d5a6Sdrh */ 45224fb627aSdrh if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){ 4537d10d5a6Sdrh pExpr->op = TK_STRING; 4541885d1c2Sdrh pExpr->pTab = 0; 455f7828b5cSdrh return WRC_Prune; 4567d10d5a6Sdrh } 4577d10d5a6Sdrh 4587d10d5a6Sdrh /* 4597d10d5a6Sdrh ** cnt==0 means there was not match. cnt>1 means there were two or 4607d10d5a6Sdrh ** more matches. Either way, we have an error. 4617d10d5a6Sdrh */ 4627d10d5a6Sdrh if( cnt!=1 ){ 4637d10d5a6Sdrh const char *zErr; 4647d10d5a6Sdrh zErr = cnt==0 ? "no such column" : "ambiguous column name"; 4657d10d5a6Sdrh if( zDb ){ 4667d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); 4677d10d5a6Sdrh }else if( zTab ){ 4687d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); 4697d10d5a6Sdrh }else{ 4707d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); 4717d10d5a6Sdrh } 4721db95106Sdan pParse->checkSchema = 1; 4737d10d5a6Sdrh pTopNC->nErr++; 4747d10d5a6Sdrh } 4757d10d5a6Sdrh 4767d10d5a6Sdrh /* If a column from a table in pSrcList is referenced, then record 4777d10d5a6Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes 4787d10d5a6Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the 4797d10d5a6Sdrh ** column number is greater than the number of bits in the bitmask 4807d10d5a6Sdrh ** then set the high-order bit of the bitmask. 4817d10d5a6Sdrh */ 4822d2e7bd3Sdanielk1977 if( pExpr->iColumn>=0 && pMatch!=0 ){ 4837d10d5a6Sdrh int n = pExpr->iColumn; 48400e13613Sdanielk1977 testcase( n==BMS-1 ); 48500e13613Sdanielk1977 if( n>=BMS ){ 48600e13613Sdanielk1977 n = BMS-1; 4877d10d5a6Sdrh } 4887d10d5a6Sdrh assert( pMatch->iCursor==pExpr->iTable ); 4897d10d5a6Sdrh pMatch->colUsed |= ((Bitmask)1)<<n; 4907d10d5a6Sdrh } 4917d10d5a6Sdrh 4927d10d5a6Sdrh /* Clean up and return 4937d10d5a6Sdrh */ 4947d10d5a6Sdrh sqlite3ExprDelete(db, pExpr->pLeft); 4957d10d5a6Sdrh pExpr->pLeft = 0; 4967d10d5a6Sdrh sqlite3ExprDelete(db, pExpr->pRight); 4977d10d5a6Sdrh pExpr->pRight = 0; 4982bd93516Sdan pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN); 499b7916a78Sdrh lookupname_end: 5007d10d5a6Sdrh if( cnt==1 ){ 5017d10d5a6Sdrh assert( pNC!=0 ); 502a3a5bd9bSdrh if( pExpr->op!=TK_AS ){ 5037d10d5a6Sdrh sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); 504a3a5bd9bSdrh } 5057d10d5a6Sdrh /* Increment the nRef value on all name contexts from TopNC up to 5067d10d5a6Sdrh ** the point where the name matched. */ 5077d10d5a6Sdrh for(;;){ 5087d10d5a6Sdrh assert( pTopNC!=0 ); 5097d10d5a6Sdrh pTopNC->nRef++; 5107d10d5a6Sdrh if( pTopNC==pNC ) break; 5117d10d5a6Sdrh pTopNC = pTopNC->pNext; 5127d10d5a6Sdrh } 513f7828b5cSdrh return WRC_Prune; 5147d10d5a6Sdrh } else { 515f7828b5cSdrh return WRC_Abort; 5167d10d5a6Sdrh } 5177d10d5a6Sdrh } 5187d10d5a6Sdrh 5197d10d5a6Sdrh /* 520f7b0b0adSdan ** Allocate and return a pointer to an expression to load the column iCol 5219e48165bSdrh ** from datasource iSrc in SrcList pSrc. 522f7b0b0adSdan */ 523f7b0b0adSdan Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){ 524f7b0b0adSdan Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0); 525f7b0b0adSdan if( p ){ 526f7b0b0adSdan struct SrcList_item *pItem = &pSrc->a[iSrc]; 527f7b0b0adSdan p->pTab = pItem->pTab; 528f7b0b0adSdan p->iTable = pItem->iCursor; 529f7b0b0adSdan if( p->pTab->iPKey==iCol ){ 530f7b0b0adSdan p->iColumn = -1; 531f7b0b0adSdan }else{ 5328677d308Sdrh p->iColumn = (ynVar)iCol; 5337caba669Sdrh testcase( iCol==BMS ); 5347caba669Sdrh testcase( iCol==BMS-1 ); 535f7b0b0adSdan pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); 536f7b0b0adSdan } 537f7b0b0adSdan ExprSetProperty(p, EP_Resolved); 538f7b0b0adSdan } 539f7b0b0adSdan return p; 540f7b0b0adSdan } 541f7b0b0adSdan 542f7b0b0adSdan /* 5433780be11Sdrh ** Report an error that an expression is not valid for a partial index WHERE 5443780be11Sdrh ** clause. 5453780be11Sdrh */ 5463780be11Sdrh static void notValidPartIdxWhere( 5473780be11Sdrh Parse *pParse, /* Leave error message here */ 5483780be11Sdrh NameContext *pNC, /* The name context */ 5493780be11Sdrh const char *zMsg /* Type of error */ 5503780be11Sdrh ){ 5513780be11Sdrh if( (pNC->ncFlags & NC_PartIdx)!=0 ){ 5523780be11Sdrh sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses", 5533780be11Sdrh zMsg); 5543780be11Sdrh } 5553780be11Sdrh } 5563780be11Sdrh 5573780be11Sdrh #ifndef SQLITE_OMIT_CHECK 5583780be11Sdrh /* 5593780be11Sdrh ** Report an error that an expression is not valid for a CHECK constraint. 5603780be11Sdrh */ 5613780be11Sdrh static void notValidCheckConstraint( 5623780be11Sdrh Parse *pParse, /* Leave error message here */ 5633780be11Sdrh NameContext *pNC, /* The name context */ 5643780be11Sdrh const char *zMsg /* Type of error */ 5653780be11Sdrh ){ 5663780be11Sdrh if( (pNC->ncFlags & NC_IsCheck)!=0 ){ 5673780be11Sdrh sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg); 5683780be11Sdrh } 5693780be11Sdrh } 5703780be11Sdrh #else 5713780be11Sdrh # define notValidCheckConstraint(P,N,M) 5723780be11Sdrh #endif 5733780be11Sdrh 574cca9f3d2Sdrh /* 575cca9f3d2Sdrh ** Expression p should encode a floating point value between 1.0 and 0.0. 576cca9f3d2Sdrh ** Return 1024 times this value. Or return -1 if p is not a floating point 577cca9f3d2Sdrh ** value between 1.0 and 0.0. 578cca9f3d2Sdrh */ 579cca9f3d2Sdrh static int exprProbability(Expr *p){ 580cca9f3d2Sdrh double r = -1.0; 581cca9f3d2Sdrh if( p->op!=TK_FLOAT ) return -1; 582cca9f3d2Sdrh sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); 58309328c00Sdrh assert( r>=0.0 ); 58409328c00Sdrh if( r>1.0 ) return -1; 585cca9f3d2Sdrh return (int)(r*1000.0); 586cca9f3d2Sdrh } 5873780be11Sdrh 5883780be11Sdrh /* 5897d10d5a6Sdrh ** This routine is callback for sqlite3WalkExpr(). 5907d10d5a6Sdrh ** 5917d10d5a6Sdrh ** Resolve symbolic names into TK_COLUMN operators for the current 5927d10d5a6Sdrh ** node in the expression tree. Return 0 to continue the search down 5937d10d5a6Sdrh ** the tree or 2 to abort the tree walk. 5947d10d5a6Sdrh ** 5957d10d5a6Sdrh ** This routine also does error checking and name resolution for 5967d10d5a6Sdrh ** function names. The operator for aggregate functions is changed 5977d10d5a6Sdrh ** to TK_AGG_FUNCTION. 5987d10d5a6Sdrh */ 5997d10d5a6Sdrh static int resolveExprStep(Walker *pWalker, Expr *pExpr){ 6007d10d5a6Sdrh NameContext *pNC; 6017d10d5a6Sdrh Parse *pParse; 6027d10d5a6Sdrh 6037d10d5a6Sdrh pNC = pWalker->u.pNC; 6047d10d5a6Sdrh assert( pNC!=0 ); 6057d10d5a6Sdrh pParse = pNC->pParse; 6067d10d5a6Sdrh assert( pParse==pWalker->pParse ); 6077d10d5a6Sdrh 608c5cd1249Sdrh if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune; 6097d10d5a6Sdrh ExprSetProperty(pExpr, EP_Resolved); 6107d10d5a6Sdrh #ifndef NDEBUG 6117d10d5a6Sdrh if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ 6127d10d5a6Sdrh SrcList *pSrcList = pNC->pSrcList; 6137d10d5a6Sdrh int i; 6147d10d5a6Sdrh for(i=0; i<pNC->pSrcList->nSrc; i++){ 6157d10d5a6Sdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); 6167d10d5a6Sdrh } 6177d10d5a6Sdrh } 6187d10d5a6Sdrh #endif 6197d10d5a6Sdrh switch( pExpr->op ){ 62041204f1fSdrh 621273f619bSshane #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) 62241204f1fSdrh /* The special operator TK_ROW means use the rowid for the first 62341204f1fSdrh ** column in the FROM clause. This is used by the LIMIT and ORDER BY 62441204f1fSdrh ** clause processing on UPDATE and DELETE statements. 62541204f1fSdrh */ 62641204f1fSdrh case TK_ROW: { 62741204f1fSdrh SrcList *pSrcList = pNC->pSrcList; 62841204f1fSdrh struct SrcList_item *pItem; 62941204f1fSdrh assert( pSrcList && pSrcList->nSrc==1 ); 63041204f1fSdrh pItem = pSrcList->a; 63141204f1fSdrh pExpr->op = TK_COLUMN; 63241204f1fSdrh pExpr->pTab = pItem->pTab; 63341204f1fSdrh pExpr->iTable = pItem->iCursor; 63441204f1fSdrh pExpr->iColumn = -1; 63541204f1fSdrh pExpr->affinity = SQLITE_AFF_INTEGER; 63641204f1fSdrh break; 63741204f1fSdrh } 638273f619bSshane #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */ 63941204f1fSdrh 6407d10d5a6Sdrh /* A lone identifier is the name of a column. 6417d10d5a6Sdrh */ 6427d10d5a6Sdrh case TK_ID: { 643f7828b5cSdrh return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr); 6447d10d5a6Sdrh } 6457d10d5a6Sdrh 6467d10d5a6Sdrh /* A table name and column name: ID.ID 6477d10d5a6Sdrh ** Or a database, table and column: ID.ID.ID 6487d10d5a6Sdrh */ 6497d10d5a6Sdrh case TK_DOT: { 650b7916a78Sdrh const char *zColumn; 651b7916a78Sdrh const char *zTable; 652b7916a78Sdrh const char *zDb; 6537d10d5a6Sdrh Expr *pRight; 6547d10d5a6Sdrh 6557d10d5a6Sdrh /* if( pSrcList==0 ) break; */ 6567d10d5a6Sdrh pRight = pExpr->pRight; 6577d10d5a6Sdrh if( pRight->op==TK_ID ){ 658b7916a78Sdrh zDb = 0; 65933e619fcSdrh zTable = pExpr->pLeft->u.zToken; 66033e619fcSdrh zColumn = pRight->u.zToken; 6617d10d5a6Sdrh }else{ 6627d10d5a6Sdrh assert( pRight->op==TK_DOT ); 66333e619fcSdrh zDb = pExpr->pLeft->u.zToken; 66433e619fcSdrh zTable = pRight->pLeft->u.zToken; 66533e619fcSdrh zColumn = pRight->pRight->u.zToken; 6667d10d5a6Sdrh } 667f7828b5cSdrh return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr); 6687d10d5a6Sdrh } 6697d10d5a6Sdrh 6707d10d5a6Sdrh /* Resolve function names 6717d10d5a6Sdrh */ 6727d10d5a6Sdrh case TK_FUNCTION: { 6736ab3a2ecSdanielk1977 ExprList *pList = pExpr->x.pList; /* The argument list */ 6747d10d5a6Sdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */ 6757d10d5a6Sdrh int no_such_func = 0; /* True if no such function exists */ 6767d10d5a6Sdrh int wrong_num_args = 0; /* True if wrong number of arguments */ 6777d10d5a6Sdrh int is_agg = 0; /* True if is an aggregate function */ 6787d10d5a6Sdrh int auth; /* Authorization to use the function */ 6797d10d5a6Sdrh int nId; /* Number of characters in function name */ 6807d10d5a6Sdrh const char *zId; /* The function name. */ 6817d10d5a6Sdrh FuncDef *pDef; /* Information about the function */ 682ea678832Sdrh u8 enc = ENC(pParse->db); /* The database encoding */ 6837d10d5a6Sdrh 6846ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 6853780be11Sdrh notValidPartIdxWhere(pParse, pNC, "functions"); 68633e619fcSdrh zId = pExpr->u.zToken; 687b7916a78Sdrh nId = sqlite3Strlen30(zId); 6887d10d5a6Sdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 6897d10d5a6Sdrh if( pDef==0 ){ 69089d5d6a2Sdrh pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0); 6917d10d5a6Sdrh if( pDef==0 ){ 6927d10d5a6Sdrh no_such_func = 1; 6937d10d5a6Sdrh }else{ 6947d10d5a6Sdrh wrong_num_args = 1; 6957d10d5a6Sdrh } 6967d10d5a6Sdrh }else{ 6977d10d5a6Sdrh is_agg = pDef->xFunc==0; 698cca9f3d2Sdrh if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 699a4c3c87eSdrh ExprSetProperty(pExpr, EP_Unlikely|EP_Skip); 700cca9f3d2Sdrh if( n==2 ){ 701cca9f3d2Sdrh pExpr->iTable = exprProbability(pList->a[1].pExpr); 702cca9f3d2Sdrh if( pExpr->iTable<0 ){ 703aae0f9e4Sdrh sqlite3ErrorMsg(pParse, "second argument to likelihood() must be a " 704aae0f9e4Sdrh "constant between 0.0 and 1.0"); 705cca9f3d2Sdrh pNC->nErr++; 706cca9f3d2Sdrh } 707cca9f3d2Sdrh }else{ 7083432daa8Sdrh /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to 7093432daa8Sdrh ** likelihood(X, 0.0625). 710b3265260Sdrh ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for 7113432daa8Sdrh ** likelihood(X,0.0625). */ 712abfa6d52Sdrh pExpr->iTable = 62; /* TUNING: Default 2nd arg to unlikely() is 0.0625 */ 713cca9f3d2Sdrh } 714cca9f3d2Sdrh } 7157d10d5a6Sdrh } 7167d10d5a6Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION 7177d10d5a6Sdrh if( pDef ){ 7187d10d5a6Sdrh auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); 7197d10d5a6Sdrh if( auth!=SQLITE_OK ){ 7207d10d5a6Sdrh if( auth==SQLITE_DENY ){ 7217d10d5a6Sdrh sqlite3ErrorMsg(pParse, "not authorized to use function: %s", 7227d10d5a6Sdrh pDef->zName); 7237d10d5a6Sdrh pNC->nErr++; 7247d10d5a6Sdrh } 7257d10d5a6Sdrh pExpr->op = TK_NULL; 7267d10d5a6Sdrh return WRC_Prune; 7277d10d5a6Sdrh } 728b1fba286Sdrh if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Constant); 7297d10d5a6Sdrh } 7307d10d5a6Sdrh #endif 731a51009b2Sdrh if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){ 7327d10d5a6Sdrh sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 7337d10d5a6Sdrh pNC->nErr++; 7347d10d5a6Sdrh is_agg = 0; 735ddd1fc72Sdrh }else if( no_such_func && pParse->db->init.busy==0 ){ 7367d10d5a6Sdrh sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 7377d10d5a6Sdrh pNC->nErr++; 7387d10d5a6Sdrh }else if( wrong_num_args ){ 7397d10d5a6Sdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 7407d10d5a6Sdrh nId, zId); 7417d10d5a6Sdrh pNC->nErr++; 7427d10d5a6Sdrh } 743a51009b2Sdrh if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg; 7447d10d5a6Sdrh sqlite3WalkExprList(pWalker, pList); 745030796dfSdrh if( is_agg ){ 746030796dfSdrh NameContext *pNC2 = pNC; 747030796dfSdrh pExpr->op = TK_AGG_FUNCTION; 748030796dfSdrh pExpr->op2 = 0; 749030796dfSdrh while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){ 750030796dfSdrh pExpr->op2++; 751030796dfSdrh pNC2 = pNC2->pNext; 752030796dfSdrh } 753030796dfSdrh if( pNC2 ) pNC2->ncFlags |= NC_HasAgg; 754030796dfSdrh pNC->ncFlags |= NC_AllowAgg; 755030796dfSdrh } 7567d10d5a6Sdrh /* FIX ME: Compute pExpr->affinity based on the expected return 7577d10d5a6Sdrh ** type of the function 7587d10d5a6Sdrh */ 7597d10d5a6Sdrh return WRC_Prune; 7607d10d5a6Sdrh } 7617d10d5a6Sdrh #ifndef SQLITE_OMIT_SUBQUERY 7627d10d5a6Sdrh case TK_SELECT: 76373c0fdc7Sdrh case TK_EXISTS: testcase( pExpr->op==TK_EXISTS ); 7647d10d5a6Sdrh #endif 7657d10d5a6Sdrh case TK_IN: { 76673c0fdc7Sdrh testcase( pExpr->op==TK_IN ); 7676ab3a2ecSdanielk1977 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 7687d10d5a6Sdrh int nRef = pNC->nRef; 7693780be11Sdrh notValidCheckConstraint(pParse, pNC, "subqueries"); 7703780be11Sdrh notValidPartIdxWhere(pParse, pNC, "subqueries"); 7716ab3a2ecSdanielk1977 sqlite3WalkSelect(pWalker, pExpr->x.pSelect); 7727d10d5a6Sdrh assert( pNC->nRef>=nRef ); 7737d10d5a6Sdrh if( nRef!=pNC->nRef ){ 7747d10d5a6Sdrh ExprSetProperty(pExpr, EP_VarSelect); 7757d10d5a6Sdrh } 7767d10d5a6Sdrh } 7777d10d5a6Sdrh break; 7787d10d5a6Sdrh } 7797d10d5a6Sdrh case TK_VARIABLE: { 7803780be11Sdrh notValidCheckConstraint(pParse, pNC, "parameters"); 7813780be11Sdrh notValidPartIdxWhere(pParse, pNC, "parameters"); 7827d10d5a6Sdrh break; 7837d10d5a6Sdrh } 7847d10d5a6Sdrh } 7857d10d5a6Sdrh return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; 7867d10d5a6Sdrh } 7877d10d5a6Sdrh 7887d10d5a6Sdrh /* 7897d10d5a6Sdrh ** pEList is a list of expressions which are really the result set of the 7907d10d5a6Sdrh ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. 7917d10d5a6Sdrh ** This routine checks to see if pE is a simple identifier which corresponds 7927d10d5a6Sdrh ** to the AS-name of one of the terms of the expression list. If it is, 7937d10d5a6Sdrh ** this routine return an integer between 1 and N where N is the number of 7947d10d5a6Sdrh ** elements in pEList, corresponding to the matching entry. If there is 7957d10d5a6Sdrh ** no match, or if pE is not a simple identifier, then this routine 7967d10d5a6Sdrh ** return 0. 7977d10d5a6Sdrh ** 7987d10d5a6Sdrh ** pEList has been resolved. pE has not. 7997d10d5a6Sdrh */ 8007d10d5a6Sdrh static int resolveAsName( 8017d10d5a6Sdrh Parse *pParse, /* Parsing context for error messages */ 8027d10d5a6Sdrh ExprList *pEList, /* List of expressions to scan */ 8037d10d5a6Sdrh Expr *pE /* Expression we are trying to match */ 8047d10d5a6Sdrh ){ 8057d10d5a6Sdrh int i; /* Loop counter */ 8067d10d5a6Sdrh 807cf697396Sshane UNUSED_PARAMETER(pParse); 808cf697396Sshane 80973c0fdc7Sdrh if( pE->op==TK_ID ){ 81033e619fcSdrh char *zCol = pE->u.zToken; 8117d10d5a6Sdrh for(i=0; i<pEList->nExpr; i++){ 8127d10d5a6Sdrh char *zAs = pEList->a[i].zName; 8137d10d5a6Sdrh if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 8147d10d5a6Sdrh return i+1; 8157d10d5a6Sdrh } 8167d10d5a6Sdrh } 8177d10d5a6Sdrh } 8187d10d5a6Sdrh return 0; 8197d10d5a6Sdrh } 8207d10d5a6Sdrh 8217d10d5a6Sdrh /* 8227d10d5a6Sdrh ** pE is a pointer to an expression which is a single term in the 8237d10d5a6Sdrh ** ORDER BY of a compound SELECT. The expression has not been 8247d10d5a6Sdrh ** name resolved. 8257d10d5a6Sdrh ** 8267d10d5a6Sdrh ** At the point this routine is called, we already know that the 8277d10d5a6Sdrh ** ORDER BY term is not an integer index into the result set. That 8287d10d5a6Sdrh ** case is handled by the calling routine. 8297d10d5a6Sdrh ** 8307d10d5a6Sdrh ** Attempt to match pE against result set columns in the left-most 8317d10d5a6Sdrh ** SELECT statement. Return the index i of the matching column, 8327d10d5a6Sdrh ** as an indication to the caller that it should sort by the i-th column. 8337d10d5a6Sdrh ** The left-most column is 1. In other words, the value returned is the 8347d10d5a6Sdrh ** same integer value that would be used in the SQL statement to indicate 8357d10d5a6Sdrh ** the column. 8367d10d5a6Sdrh ** 8377d10d5a6Sdrh ** If there is no match, return 0. Return -1 if an error occurs. 8387d10d5a6Sdrh */ 8397d10d5a6Sdrh static int resolveOrderByTermToExprList( 8407d10d5a6Sdrh Parse *pParse, /* Parsing context for error messages */ 8417d10d5a6Sdrh Select *pSelect, /* The SELECT statement with the ORDER BY clause */ 8427d10d5a6Sdrh Expr *pE /* The specific ORDER BY term */ 8437d10d5a6Sdrh ){ 8447d10d5a6Sdrh int i; /* Loop counter */ 8457d10d5a6Sdrh ExprList *pEList; /* The columns of the result set */ 8467d10d5a6Sdrh NameContext nc; /* Name context for resolving pE */ 847a7564663Sdrh sqlite3 *db; /* Database connection */ 848a7564663Sdrh int rc; /* Return code from subprocedures */ 849a7564663Sdrh u8 savedSuppErr; /* Saved value of db->suppressErr */ 8507d10d5a6Sdrh 8517d10d5a6Sdrh assert( sqlite3ExprIsInteger(pE, &i)==0 ); 8527d10d5a6Sdrh pEList = pSelect->pEList; 8537d10d5a6Sdrh 8547d10d5a6Sdrh /* Resolve all names in the ORDER BY term expression 8557d10d5a6Sdrh */ 8567d10d5a6Sdrh memset(&nc, 0, sizeof(nc)); 8577d10d5a6Sdrh nc.pParse = pParse; 8587d10d5a6Sdrh nc.pSrcList = pSelect->pSrc; 8597d10d5a6Sdrh nc.pEList = pEList; 860a51009b2Sdrh nc.ncFlags = NC_AllowAgg; 8617d10d5a6Sdrh nc.nErr = 0; 862a7564663Sdrh db = pParse->db; 863a7564663Sdrh savedSuppErr = db->suppressErr; 864a7564663Sdrh db->suppressErr = 1; 865a7564663Sdrh rc = sqlite3ResolveExprNames(&nc, pE); 866a7564663Sdrh db->suppressErr = savedSuppErr; 867a7564663Sdrh if( rc ) return 0; 8687d10d5a6Sdrh 8697d10d5a6Sdrh /* Try to match the ORDER BY expression against an expression 8707d10d5a6Sdrh ** in the result set. Return an 1-based index of the matching 8717d10d5a6Sdrh ** result-set entry. 8727d10d5a6Sdrh */ 8737d10d5a6Sdrh for(i=0; i<pEList->nExpr; i++){ 874619a1305Sdrh if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){ 8757d10d5a6Sdrh return i+1; 8767d10d5a6Sdrh } 8777d10d5a6Sdrh } 8787d10d5a6Sdrh 8797d10d5a6Sdrh /* If no match, return 0. */ 8807d10d5a6Sdrh return 0; 8817d10d5a6Sdrh } 8827d10d5a6Sdrh 8837d10d5a6Sdrh /* 8847d10d5a6Sdrh ** Generate an ORDER BY or GROUP BY term out-of-range error. 8857d10d5a6Sdrh */ 8867d10d5a6Sdrh static void resolveOutOfRangeError( 8877d10d5a6Sdrh Parse *pParse, /* The error context into which to write the error */ 8887d10d5a6Sdrh const char *zType, /* "ORDER" or "GROUP" */ 8897d10d5a6Sdrh int i, /* The index (1-based) of the term out of range */ 8907d10d5a6Sdrh int mx /* Largest permissible value of i */ 8917d10d5a6Sdrh ){ 8927d10d5a6Sdrh sqlite3ErrorMsg(pParse, 8937d10d5a6Sdrh "%r %s BY term out of range - should be " 8947d10d5a6Sdrh "between 1 and %d", i, zType, mx); 8957d10d5a6Sdrh } 8967d10d5a6Sdrh 8977d10d5a6Sdrh /* 8987d10d5a6Sdrh ** Analyze the ORDER BY clause in a compound SELECT statement. Modify 8997d10d5a6Sdrh ** each term of the ORDER BY clause is a constant integer between 1 9007d10d5a6Sdrh ** and N where N is the number of columns in the compound SELECT. 9017d10d5a6Sdrh ** 9027d10d5a6Sdrh ** ORDER BY terms that are already an integer between 1 and N are 9037d10d5a6Sdrh ** unmodified. ORDER BY terms that are integers outside the range of 9047d10d5a6Sdrh ** 1 through N generate an error. ORDER BY terms that are expressions 9057d10d5a6Sdrh ** are matched against result set expressions of compound SELECT 9067d10d5a6Sdrh ** beginning with the left-most SELECT and working toward the right. 9077d10d5a6Sdrh ** At the first match, the ORDER BY expression is transformed into 9087d10d5a6Sdrh ** the integer column number. 9097d10d5a6Sdrh ** 9107d10d5a6Sdrh ** Return the number of errors seen. 9117d10d5a6Sdrh */ 9127d10d5a6Sdrh static int resolveCompoundOrderBy( 9137d10d5a6Sdrh Parse *pParse, /* Parsing context. Leave error messages here */ 9147d10d5a6Sdrh Select *pSelect /* The SELECT statement containing the ORDER BY */ 9157d10d5a6Sdrh ){ 9167d10d5a6Sdrh int i; 9177d10d5a6Sdrh ExprList *pOrderBy; 9187d10d5a6Sdrh ExprList *pEList; 9197d10d5a6Sdrh sqlite3 *db; 9207d10d5a6Sdrh int moreToDo = 1; 9217d10d5a6Sdrh 9227d10d5a6Sdrh pOrderBy = pSelect->pOrderBy; 9237d10d5a6Sdrh if( pOrderBy==0 ) return 0; 9247d10d5a6Sdrh db = pParse->db; 9257d10d5a6Sdrh #if SQLITE_MAX_COLUMN 9267d10d5a6Sdrh if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ 9277d10d5a6Sdrh sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); 9287d10d5a6Sdrh return 1; 9297d10d5a6Sdrh } 9307d10d5a6Sdrh #endif 9317d10d5a6Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 9327d10d5a6Sdrh pOrderBy->a[i].done = 0; 9337d10d5a6Sdrh } 9347d10d5a6Sdrh pSelect->pNext = 0; 9357d10d5a6Sdrh while( pSelect->pPrior ){ 9367d10d5a6Sdrh pSelect->pPrior->pNext = pSelect; 9377d10d5a6Sdrh pSelect = pSelect->pPrior; 9387d10d5a6Sdrh } 9397d10d5a6Sdrh while( pSelect && moreToDo ){ 9407d10d5a6Sdrh struct ExprList_item *pItem; 9417d10d5a6Sdrh moreToDo = 0; 9427d10d5a6Sdrh pEList = pSelect->pEList; 9430a846f96Sdrh assert( pEList!=0 ); 9447d10d5a6Sdrh for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ 9457d10d5a6Sdrh int iCol = -1; 9467d10d5a6Sdrh Expr *pE, *pDup; 9477d10d5a6Sdrh if( pItem->done ) continue; 948bd13d34bSdrh pE = sqlite3ExprSkipCollate(pItem->pExpr); 9497d10d5a6Sdrh if( sqlite3ExprIsInteger(pE, &iCol) ){ 95073c0fdc7Sdrh if( iCol<=0 || iCol>pEList->nExpr ){ 9517d10d5a6Sdrh resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr); 9527d10d5a6Sdrh return 1; 9537d10d5a6Sdrh } 9547d10d5a6Sdrh }else{ 9557d10d5a6Sdrh iCol = resolveAsName(pParse, pEList, pE); 9567d10d5a6Sdrh if( iCol==0 ){ 9576ab3a2ecSdanielk1977 pDup = sqlite3ExprDup(db, pE, 0); 9587d10d5a6Sdrh if( !db->mallocFailed ){ 9597d10d5a6Sdrh assert(pDup); 9607d10d5a6Sdrh iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); 9617d10d5a6Sdrh } 9627d10d5a6Sdrh sqlite3ExprDelete(db, pDup); 9637d10d5a6Sdrh } 9647d10d5a6Sdrh } 9657d10d5a6Sdrh if( iCol>0 ){ 966bd13d34bSdrh /* Convert the ORDER BY term into an integer column number iCol, 967bd13d34bSdrh ** taking care to preserve the COLLATE clause if it exists */ 968bd13d34bSdrh Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); 969bd13d34bSdrh if( pNew==0 ) return 1; 970bd13d34bSdrh pNew->flags |= EP_IntValue; 971bd13d34bSdrh pNew->u.iValue = iCol; 972bd13d34bSdrh if( pItem->pExpr==pE ){ 973bd13d34bSdrh pItem->pExpr = pNew; 974bd13d34bSdrh }else{ 975bd13d34bSdrh assert( pItem->pExpr->op==TK_COLLATE ); 976bd13d34bSdrh assert( pItem->pExpr->pLeft==pE ); 977bd13d34bSdrh pItem->pExpr->pLeft = pNew; 978bd13d34bSdrh } 9797d10d5a6Sdrh sqlite3ExprDelete(db, pE); 980c2acc4e4Sdrh pItem->u.x.iOrderByCol = (u16)iCol; 9817d10d5a6Sdrh pItem->done = 1; 9827d10d5a6Sdrh }else{ 9837d10d5a6Sdrh moreToDo = 1; 9847d10d5a6Sdrh } 9857d10d5a6Sdrh } 9867d10d5a6Sdrh pSelect = pSelect->pNext; 9877d10d5a6Sdrh } 9887d10d5a6Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 9897d10d5a6Sdrh if( pOrderBy->a[i].done==0 ){ 9907d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any " 9917d10d5a6Sdrh "column in the result set", i+1); 9927d10d5a6Sdrh return 1; 9937d10d5a6Sdrh } 9947d10d5a6Sdrh } 9957d10d5a6Sdrh return 0; 9967d10d5a6Sdrh } 9977d10d5a6Sdrh 9987d10d5a6Sdrh /* 9997d10d5a6Sdrh ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of 10007d10d5a6Sdrh ** the SELECT statement pSelect. If any term is reference to a 1001c2acc4e4Sdrh ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol 1002c2acc4e4Sdrh ** field) then convert that term into a copy of the corresponding result set 10037d10d5a6Sdrh ** column. 10047d10d5a6Sdrh ** 10057d10d5a6Sdrh ** If any errors are detected, add an error message to pParse and 10067d10d5a6Sdrh ** return non-zero. Return zero if no errors are seen. 10077d10d5a6Sdrh */ 10087d10d5a6Sdrh int sqlite3ResolveOrderGroupBy( 10097d10d5a6Sdrh Parse *pParse, /* Parsing context. Leave error messages here */ 10107d10d5a6Sdrh Select *pSelect, /* The SELECT statement containing the clause */ 10117d10d5a6Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ 10127d10d5a6Sdrh const char *zType /* "ORDER" or "GROUP" */ 10137d10d5a6Sdrh ){ 10147d10d5a6Sdrh int i; 10157d10d5a6Sdrh sqlite3 *db = pParse->db; 10167d10d5a6Sdrh ExprList *pEList; 10177d10d5a6Sdrh struct ExprList_item *pItem; 10187d10d5a6Sdrh 10197d10d5a6Sdrh if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; 10207d10d5a6Sdrh #if SQLITE_MAX_COLUMN 10217d10d5a6Sdrh if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ 10227d10d5a6Sdrh sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); 10237d10d5a6Sdrh return 1; 10247d10d5a6Sdrh } 10257d10d5a6Sdrh #endif 10267d10d5a6Sdrh pEList = pSelect->pEList; 10270a846f96Sdrh assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ 10287d10d5a6Sdrh for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ 1029c2acc4e4Sdrh if( pItem->u.x.iOrderByCol ){ 1030c2acc4e4Sdrh if( pItem->u.x.iOrderByCol>pEList->nExpr ){ 10317d10d5a6Sdrh resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); 10327d10d5a6Sdrh return 1; 10337d10d5a6Sdrh } 1034c2acc4e4Sdrh resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0); 10357d10d5a6Sdrh } 10367d10d5a6Sdrh } 10377d10d5a6Sdrh return 0; 10387d10d5a6Sdrh } 10397d10d5a6Sdrh 10407d10d5a6Sdrh /* 10417d10d5a6Sdrh ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. 10427d10d5a6Sdrh ** The Name context of the SELECT statement is pNC. zType is either 10437d10d5a6Sdrh ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. 10447d10d5a6Sdrh ** 10457d10d5a6Sdrh ** This routine resolves each term of the clause into an expression. 10467d10d5a6Sdrh ** If the order-by term is an integer I between 1 and N (where N is the 10477d10d5a6Sdrh ** number of columns in the result set of the SELECT) then the expression 10487d10d5a6Sdrh ** in the resolution is a copy of the I-th result-set expression. If 104926080d92Sdrh ** the order-by term is an identifier that corresponds to the AS-name of 10507d10d5a6Sdrh ** a result-set expression, then the term resolves to a copy of the 10517d10d5a6Sdrh ** result-set expression. Otherwise, the expression is resolved in 10527d10d5a6Sdrh ** the usual way - using sqlite3ResolveExprNames(). 10537d10d5a6Sdrh ** 10547d10d5a6Sdrh ** This routine returns the number of errors. If errors occur, then 10557d10d5a6Sdrh ** an appropriate error message might be left in pParse. (OOM errors 10567d10d5a6Sdrh ** excepted.) 10577d10d5a6Sdrh */ 10587d10d5a6Sdrh static int resolveOrderGroupBy( 10597d10d5a6Sdrh NameContext *pNC, /* The name context of the SELECT statement */ 10607d10d5a6Sdrh Select *pSelect, /* The SELECT statement holding pOrderBy */ 10617d10d5a6Sdrh ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */ 10627d10d5a6Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */ 10637d10d5a6Sdrh ){ 106470331cd7Sdrh int i, j; /* Loop counters */ 10657d10d5a6Sdrh int iCol; /* Column number */ 10667d10d5a6Sdrh struct ExprList_item *pItem; /* A term of the ORDER BY clause */ 10677d10d5a6Sdrh Parse *pParse; /* Parsing context */ 10687d10d5a6Sdrh int nResult; /* Number of terms in the result set */ 10697d10d5a6Sdrh 10707d10d5a6Sdrh if( pOrderBy==0 ) return 0; 10717d10d5a6Sdrh nResult = pSelect->pEList->nExpr; 10727d10d5a6Sdrh pParse = pNC->pParse; 10737d10d5a6Sdrh for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ 10747d10d5a6Sdrh Expr *pE = pItem->pExpr; 1075e35463b3Sdrh Expr *pE2 = sqlite3ExprSkipCollate(pE); 10760af16ab2Sdrh if( zType[0]!='G' ){ 1077e35463b3Sdrh iCol = resolveAsName(pParse, pSelect->pEList, pE2); 10787d10d5a6Sdrh if( iCol>0 ){ 10797d10d5a6Sdrh /* If an AS-name match is found, mark this ORDER BY column as being 10807d10d5a6Sdrh ** a copy of the iCol-th result-set column. The subsequent call to 10817d10d5a6Sdrh ** sqlite3ResolveOrderGroupBy() will convert the expression to a 10827d10d5a6Sdrh ** copy of the iCol-th result-set expression. */ 1083c2acc4e4Sdrh pItem->u.x.iOrderByCol = (u16)iCol; 10847d10d5a6Sdrh continue; 10857d10d5a6Sdrh } 10860af16ab2Sdrh } 1087e35463b3Sdrh if( sqlite3ExprIsInteger(pE2, &iCol) ){ 10887d10d5a6Sdrh /* The ORDER BY term is an integer constant. Again, set the column 10897d10d5a6Sdrh ** number so that sqlite3ResolveOrderGroupBy() will convert the 10907d10d5a6Sdrh ** order-by term to a copy of the result-set expression */ 109185d641f9Sdrh if( iCol<1 || iCol>0xffff ){ 10927d10d5a6Sdrh resolveOutOfRangeError(pParse, zType, i+1, nResult); 10937d10d5a6Sdrh return 1; 10947d10d5a6Sdrh } 1095c2acc4e4Sdrh pItem->u.x.iOrderByCol = (u16)iCol; 10967d10d5a6Sdrh continue; 10977d10d5a6Sdrh } 10987d10d5a6Sdrh 10997d10d5a6Sdrh /* Otherwise, treat the ORDER BY term as an ordinary expression */ 1100c2acc4e4Sdrh pItem->u.x.iOrderByCol = 0; 11017d10d5a6Sdrh if( sqlite3ResolveExprNames(pNC, pE) ){ 11027d10d5a6Sdrh return 1; 11037d10d5a6Sdrh } 110470331cd7Sdrh for(j=0; j<pSelect->pEList->nExpr; j++){ 1105619a1305Sdrh if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){ 1106c2acc4e4Sdrh pItem->u.x.iOrderByCol = j+1; 110770331cd7Sdrh } 110870331cd7Sdrh } 11097d10d5a6Sdrh } 11107d10d5a6Sdrh return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); 11117d10d5a6Sdrh } 11127d10d5a6Sdrh 11137d10d5a6Sdrh /* 11147d10d5a6Sdrh ** Resolve names in the SELECT statement p and all of its descendents. 11157d10d5a6Sdrh */ 11167d10d5a6Sdrh static int resolveSelectStep(Walker *pWalker, Select *p){ 11177d10d5a6Sdrh NameContext *pOuterNC; /* Context that contains this SELECT */ 11187d10d5a6Sdrh NameContext sNC; /* Name context of this SELECT */ 11197d10d5a6Sdrh int isCompound; /* True if p is a compound select */ 11207d10d5a6Sdrh int nCompound; /* Number of compound terms processed so far */ 11217d10d5a6Sdrh Parse *pParse; /* Parsing context */ 11227d10d5a6Sdrh ExprList *pEList; /* Result set expression list */ 11237d10d5a6Sdrh int i; /* Loop counter */ 11247d10d5a6Sdrh ExprList *pGroupBy; /* The GROUP BY clause */ 11257d10d5a6Sdrh Select *pLeftmost; /* Left-most of SELECT of a compound */ 11267d10d5a6Sdrh sqlite3 *db; /* Database connection */ 11277d10d5a6Sdrh 11287d10d5a6Sdrh 11290a846f96Sdrh assert( p!=0 ); 11307d10d5a6Sdrh if( p->selFlags & SF_Resolved ){ 11317d10d5a6Sdrh return WRC_Prune; 11327d10d5a6Sdrh } 11337d10d5a6Sdrh pOuterNC = pWalker->u.pNC; 11347d10d5a6Sdrh pParse = pWalker->pParse; 11357d10d5a6Sdrh db = pParse->db; 11367d10d5a6Sdrh 11377d10d5a6Sdrh /* Normally sqlite3SelectExpand() will be called first and will have 11387d10d5a6Sdrh ** already expanded this SELECT. However, if this is a subquery within 11397d10d5a6Sdrh ** an expression, sqlite3ResolveExprNames() will be called without a 11407d10d5a6Sdrh ** prior call to sqlite3SelectExpand(). When that happens, let 11417d10d5a6Sdrh ** sqlite3SelectPrep() do all of the processing for this SELECT. 11427d10d5a6Sdrh ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and 11437d10d5a6Sdrh ** this routine in the correct order. 11447d10d5a6Sdrh */ 11457d10d5a6Sdrh if( (p->selFlags & SF_Expanded)==0 ){ 11467d10d5a6Sdrh sqlite3SelectPrep(pParse, p, pOuterNC); 11477d10d5a6Sdrh return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune; 11487d10d5a6Sdrh } 11497d10d5a6Sdrh 11507d10d5a6Sdrh isCompound = p->pPrior!=0; 11517d10d5a6Sdrh nCompound = 0; 11527d10d5a6Sdrh pLeftmost = p; 11537d10d5a6Sdrh while( p ){ 11547d10d5a6Sdrh assert( (p->selFlags & SF_Expanded)!=0 ); 11557d10d5a6Sdrh assert( (p->selFlags & SF_Resolved)==0 ); 11567d10d5a6Sdrh p->selFlags |= SF_Resolved; 11577d10d5a6Sdrh 11587d10d5a6Sdrh /* Resolve the expressions in the LIMIT and OFFSET clauses. These 11597d10d5a6Sdrh ** are not allowed to refer to any names, so pass an empty NameContext. 11607d10d5a6Sdrh */ 11617d10d5a6Sdrh memset(&sNC, 0, sizeof(sNC)); 11627d10d5a6Sdrh sNC.pParse = pParse; 11637d10d5a6Sdrh if( sqlite3ResolveExprNames(&sNC, p->pLimit) || 11647d10d5a6Sdrh sqlite3ResolveExprNames(&sNC, p->pOffset) ){ 11657d10d5a6Sdrh return WRC_Abort; 11667d10d5a6Sdrh } 11677d10d5a6Sdrh 11687d10d5a6Sdrh /* Recursively resolve names in all subqueries 11697d10d5a6Sdrh */ 11707d10d5a6Sdrh for(i=0; i<p->pSrc->nSrc; i++){ 11717d10d5a6Sdrh struct SrcList_item *pItem = &p->pSrc->a[i]; 11727d10d5a6Sdrh if( pItem->pSelect ){ 1173da79cf0cSdan NameContext *pNC; /* Used to iterate name contexts */ 1174da79cf0cSdan int nRef = 0; /* Refcount for pOuterNC and outer contexts */ 11757d10d5a6Sdrh const char *zSavedContext = pParse->zAuthContext; 1176da79cf0cSdan 1177da79cf0cSdan /* Count the total number of references to pOuterNC and all of its 1178da79cf0cSdan ** parent contexts. After resolving references to expressions in 1179da79cf0cSdan ** pItem->pSelect, check if this value has changed. If so, then 1180da79cf0cSdan ** SELECT statement pItem->pSelect must be correlated. Set the 1181da79cf0cSdan ** pItem->isCorrelated flag if this is the case. */ 1182da79cf0cSdan for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef; 1183da79cf0cSdan 11847d10d5a6Sdrh if( pItem->zName ) pParse->zAuthContext = pItem->zName; 1185cd2b5613Sdrh sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC); 11867d10d5a6Sdrh pParse->zAuthContext = zSavedContext; 11877d10d5a6Sdrh if( pParse->nErr || db->mallocFailed ) return WRC_Abort; 1188da79cf0cSdan 1189da79cf0cSdan for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef; 1190da79cf0cSdan assert( pItem->isCorrelated==0 && nRef<=0 ); 1191da79cf0cSdan pItem->isCorrelated = (nRef!=0); 11927d10d5a6Sdrh } 11937d10d5a6Sdrh } 11947d10d5a6Sdrh 119592689d28Sdrh /* Set up the local name-context to pass to sqlite3ResolveExprNames() to 119692689d28Sdrh ** resolve the result-set expression list. 119792689d28Sdrh */ 119892689d28Sdrh sNC.ncFlags = NC_AllowAgg; 119992689d28Sdrh sNC.pSrcList = p->pSrc; 120092689d28Sdrh sNC.pNext = pOuterNC; 120192689d28Sdrh 120292689d28Sdrh /* Resolve names in the result set. */ 120392689d28Sdrh pEList = p->pEList; 120492689d28Sdrh assert( pEList!=0 ); 120592689d28Sdrh for(i=0; i<pEList->nExpr; i++){ 120692689d28Sdrh Expr *pX = pEList->a[i].pExpr; 120792689d28Sdrh if( sqlite3ResolveExprNames(&sNC, pX) ){ 120892689d28Sdrh return WRC_Abort; 120992689d28Sdrh } 121092689d28Sdrh } 121192689d28Sdrh 12127d10d5a6Sdrh /* If there are no aggregate functions in the result-set, and no GROUP BY 12137d10d5a6Sdrh ** expression, do not allow aggregates in any of the other expressions. 12147d10d5a6Sdrh */ 12157d10d5a6Sdrh assert( (p->selFlags & SF_Aggregate)==0 ); 12167d10d5a6Sdrh pGroupBy = p->pGroupBy; 1217a51009b2Sdrh if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){ 12187d10d5a6Sdrh p->selFlags |= SF_Aggregate; 12197d10d5a6Sdrh }else{ 1220a51009b2Sdrh sNC.ncFlags &= ~NC_AllowAgg; 12217d10d5a6Sdrh } 12227d10d5a6Sdrh 12237d10d5a6Sdrh /* If a HAVING clause is present, then there must be a GROUP BY clause. 12247d10d5a6Sdrh */ 12257d10d5a6Sdrh if( p->pHaving && !pGroupBy ){ 12267d10d5a6Sdrh sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 12277d10d5a6Sdrh return WRC_Abort; 12287d10d5a6Sdrh } 12297d10d5a6Sdrh 123026080d92Sdrh /* Add the output column list to the name-context before parsing the 12317d10d5a6Sdrh ** other expressions in the SELECT statement. This is so that 12327d10d5a6Sdrh ** expressions in the WHERE clause (etc.) can refer to expressions by 12337d10d5a6Sdrh ** aliases in the result set. 12347d10d5a6Sdrh ** 12357d10d5a6Sdrh ** Minor point: If this is the case, then the expression will be 12367d10d5a6Sdrh ** re-evaluated for each reference to it. 12377d10d5a6Sdrh */ 12387d10d5a6Sdrh sNC.pEList = p->pEList; 123958a450c0Sdrh if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; 1240a3a5bd9bSdrh if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; 12417d10d5a6Sdrh 12427d10d5a6Sdrh /* The ORDER BY and GROUP BY clauses may not refer to terms in 12437d10d5a6Sdrh ** outer queries 12447d10d5a6Sdrh */ 12457d10d5a6Sdrh sNC.pNext = 0; 1246a51009b2Sdrh sNC.ncFlags |= NC_AllowAgg; 12477d10d5a6Sdrh 12487d10d5a6Sdrh /* Process the ORDER BY clause for singleton SELECT statements. 12497d10d5a6Sdrh ** The ORDER BY clause for compounds SELECT statements is handled 12507d10d5a6Sdrh ** below, after all of the result-sets for all of the elements of 12517d10d5a6Sdrh ** the compound have been resolved. 12527d10d5a6Sdrh */ 12537d10d5a6Sdrh if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){ 12547d10d5a6Sdrh return WRC_Abort; 12557d10d5a6Sdrh } 12567d10d5a6Sdrh if( db->mallocFailed ){ 12577d10d5a6Sdrh return WRC_Abort; 12587d10d5a6Sdrh } 12597d10d5a6Sdrh 12607d10d5a6Sdrh /* Resolve the GROUP BY clause. At the same time, make sure 12617d10d5a6Sdrh ** the GROUP BY clause does not contain aggregate functions. 12627d10d5a6Sdrh */ 12637d10d5a6Sdrh if( pGroupBy ){ 12647d10d5a6Sdrh struct ExprList_item *pItem; 12657d10d5a6Sdrh 12667d10d5a6Sdrh if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ 12677d10d5a6Sdrh return WRC_Abort; 12687d10d5a6Sdrh } 12697d10d5a6Sdrh for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){ 12707d10d5a6Sdrh if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ 12717d10d5a6Sdrh sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " 12727d10d5a6Sdrh "the GROUP BY clause"); 12737d10d5a6Sdrh return WRC_Abort; 12747d10d5a6Sdrh } 12757d10d5a6Sdrh } 12767d10d5a6Sdrh } 12777d10d5a6Sdrh 12787d10d5a6Sdrh /* Advance to the next term of the compound 12797d10d5a6Sdrh */ 12807d10d5a6Sdrh p = p->pPrior; 12817d10d5a6Sdrh nCompound++; 12827d10d5a6Sdrh } 12837d10d5a6Sdrh 12847d10d5a6Sdrh /* Resolve the ORDER BY on a compound SELECT after all terms of 12857d10d5a6Sdrh ** the compound have been resolved. 12867d10d5a6Sdrh */ 12877d10d5a6Sdrh if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){ 12887d10d5a6Sdrh return WRC_Abort; 12897d10d5a6Sdrh } 12907d10d5a6Sdrh 12917d10d5a6Sdrh return WRC_Prune; 12927d10d5a6Sdrh } 12937d10d5a6Sdrh 12947d10d5a6Sdrh /* 12957d10d5a6Sdrh ** This routine walks an expression tree and resolves references to 12967d10d5a6Sdrh ** table columns and result-set columns. At the same time, do error 12977d10d5a6Sdrh ** checking on function usage and set a flag if any aggregate functions 12987d10d5a6Sdrh ** are seen. 12997d10d5a6Sdrh ** 13007d10d5a6Sdrh ** To resolve table columns references we look for nodes (or subtrees) of the 13017d10d5a6Sdrh ** form X.Y.Z or Y.Z or just Z where 13027d10d5a6Sdrh ** 13037d10d5a6Sdrh ** X: The name of a database. Ex: "main" or "temp" or 13047d10d5a6Sdrh ** the symbolic name assigned to an ATTACH-ed database. 13057d10d5a6Sdrh ** 13067d10d5a6Sdrh ** Y: The name of a table in a FROM clause. Or in a trigger 13077d10d5a6Sdrh ** one of the special names "old" or "new". 13087d10d5a6Sdrh ** 13097d10d5a6Sdrh ** Z: The name of a column in table Y. 13107d10d5a6Sdrh ** 13117d10d5a6Sdrh ** The node at the root of the subtree is modified as follows: 13127d10d5a6Sdrh ** 13137d10d5a6Sdrh ** Expr.op Changed to TK_COLUMN 13147d10d5a6Sdrh ** Expr.pTab Points to the Table object for X.Y 13157d10d5a6Sdrh ** Expr.iColumn The column index in X.Y. -1 for the rowid. 13167d10d5a6Sdrh ** Expr.iTable The VDBE cursor number for X.Y 13177d10d5a6Sdrh ** 13187d10d5a6Sdrh ** 13197d10d5a6Sdrh ** To resolve result-set references, look for expression nodes of the 13207d10d5a6Sdrh ** form Z (with no X and Y prefix) where the Z matches the right-hand 13217d10d5a6Sdrh ** size of an AS clause in the result-set of a SELECT. The Z expression 13227d10d5a6Sdrh ** is replaced by a copy of the left-hand side of the result-set expression. 13237d10d5a6Sdrh ** Table-name and function resolution occurs on the substituted expression 13247d10d5a6Sdrh ** tree. For example, in: 13257d10d5a6Sdrh ** 13267d10d5a6Sdrh ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x; 13277d10d5a6Sdrh ** 13287d10d5a6Sdrh ** The "x" term of the order by is replaced by "a+b" to render: 13297d10d5a6Sdrh ** 13307d10d5a6Sdrh ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; 13317d10d5a6Sdrh ** 13327d10d5a6Sdrh ** Function calls are checked to make sure that the function is 13337d10d5a6Sdrh ** defined and that the correct number of arguments are specified. 1334a51009b2Sdrh ** If the function is an aggregate function, then the NC_HasAgg flag is 13357d10d5a6Sdrh ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. 13367d10d5a6Sdrh ** If an expression contains aggregate functions then the EP_Agg 13377d10d5a6Sdrh ** property on the expression is set. 13387d10d5a6Sdrh ** 13397d10d5a6Sdrh ** An error message is left in pParse if anything is amiss. The number 13407d10d5a6Sdrh ** if errors is returned. 13417d10d5a6Sdrh */ 13427d10d5a6Sdrh int sqlite3ResolveExprNames( 13437d10d5a6Sdrh NameContext *pNC, /* Namespace to resolve expressions in. */ 13447d10d5a6Sdrh Expr *pExpr /* The expression to be analyzed. */ 13457d10d5a6Sdrh ){ 1346a51009b2Sdrh u8 savedHasAgg; 13477d10d5a6Sdrh Walker w; 13487d10d5a6Sdrh 13497d10d5a6Sdrh if( pExpr==0 ) return 0; 13507d10d5a6Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 13517d10d5a6Sdrh { 13527d10d5a6Sdrh Parse *pParse = pNC->pParse; 13537d10d5a6Sdrh if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){ 13547d10d5a6Sdrh return 1; 13557d10d5a6Sdrh } 13567d10d5a6Sdrh pParse->nHeight += pExpr->nHeight; 13577d10d5a6Sdrh } 13587d10d5a6Sdrh #endif 1359a51009b2Sdrh savedHasAgg = pNC->ncFlags & NC_HasAgg; 1360a51009b2Sdrh pNC->ncFlags &= ~NC_HasAgg; 1361aa87f9a6Sdrh memset(&w, 0, sizeof(w)); 13627d10d5a6Sdrh w.xExprCallback = resolveExprStep; 13637d10d5a6Sdrh w.xSelectCallback = resolveSelectStep; 13647d10d5a6Sdrh w.pParse = pNC->pParse; 13657d10d5a6Sdrh w.u.pNC = pNC; 13667d10d5a6Sdrh sqlite3WalkExpr(&w, pExpr); 13677d10d5a6Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 13687d10d5a6Sdrh pNC->pParse->nHeight -= pExpr->nHeight; 13697d10d5a6Sdrh #endif 1370fd773cf9Sdrh if( pNC->nErr>0 || w.pParse->nErr>0 ){ 13717d10d5a6Sdrh ExprSetProperty(pExpr, EP_Error); 13727d10d5a6Sdrh } 1373a51009b2Sdrh if( pNC->ncFlags & NC_HasAgg ){ 13747d10d5a6Sdrh ExprSetProperty(pExpr, EP_Agg); 13757d10d5a6Sdrh }else if( savedHasAgg ){ 1376a51009b2Sdrh pNC->ncFlags |= NC_HasAgg; 13777d10d5a6Sdrh } 13787d10d5a6Sdrh return ExprHasProperty(pExpr, EP_Error); 13797d10d5a6Sdrh } 13807d10d5a6Sdrh 13817d10d5a6Sdrh 13827d10d5a6Sdrh /* 13837d10d5a6Sdrh ** Resolve all names in all expressions of a SELECT and in all 13847d10d5a6Sdrh ** decendents of the SELECT, including compounds off of p->pPrior, 13857d10d5a6Sdrh ** subqueries in expressions, and subqueries used as FROM clause 13867d10d5a6Sdrh ** terms. 13877d10d5a6Sdrh ** 13887d10d5a6Sdrh ** See sqlite3ResolveExprNames() for a description of the kinds of 13897d10d5a6Sdrh ** transformations that occur. 13907d10d5a6Sdrh ** 13917d10d5a6Sdrh ** All SELECT statements should have been expanded using 13927d10d5a6Sdrh ** sqlite3SelectExpand() prior to invoking this routine. 13937d10d5a6Sdrh */ 13947d10d5a6Sdrh void sqlite3ResolveSelectNames( 13957d10d5a6Sdrh Parse *pParse, /* The parser context */ 13967d10d5a6Sdrh Select *p, /* The SELECT statement being coded. */ 13977d10d5a6Sdrh NameContext *pOuterNC /* Name context for parent SELECT statement */ 13987d10d5a6Sdrh ){ 13997d10d5a6Sdrh Walker w; 14007d10d5a6Sdrh 14010a846f96Sdrh assert( p!=0 ); 1402aa87f9a6Sdrh memset(&w, 0, sizeof(w)); 14037d10d5a6Sdrh w.xExprCallback = resolveExprStep; 14047d10d5a6Sdrh w.xSelectCallback = resolveSelectStep; 14057d10d5a6Sdrh w.pParse = pParse; 14067d10d5a6Sdrh w.u.pNC = pOuterNC; 14077d10d5a6Sdrh sqlite3WalkSelect(&w, p); 14087d10d5a6Sdrh } 14093780be11Sdrh 14103780be11Sdrh /* 14113780be11Sdrh ** Resolve names in expressions that can only reference a single table: 14123780be11Sdrh ** 14133780be11Sdrh ** * CHECK constraints 14143780be11Sdrh ** * WHERE clauses on partial indices 14153780be11Sdrh ** 14163780be11Sdrh ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression 14173780be11Sdrh ** is set to -1 and the Expr.iColumn value is set to the column number. 14183780be11Sdrh ** 14193780be11Sdrh ** Any errors cause an error message to be set in pParse. 14203780be11Sdrh */ 14213780be11Sdrh void sqlite3ResolveSelfReference( 14223780be11Sdrh Parse *pParse, /* Parsing context */ 14233780be11Sdrh Table *pTab, /* The table being referenced */ 14243780be11Sdrh int type, /* NC_IsCheck or NC_PartIdx */ 14253780be11Sdrh Expr *pExpr, /* Expression to resolve. May be NULL. */ 14263780be11Sdrh ExprList *pList /* Expression list to resolve. May be NUL. */ 14273780be11Sdrh ){ 14283780be11Sdrh SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ 14293780be11Sdrh NameContext sNC; /* Name context for pParse->pNewTable */ 14303780be11Sdrh int i; /* Loop counter */ 14313780be11Sdrh 14323780be11Sdrh assert( type==NC_IsCheck || type==NC_PartIdx ); 14333780be11Sdrh memset(&sNC, 0, sizeof(sNC)); 14343780be11Sdrh memset(&sSrc, 0, sizeof(sSrc)); 14353780be11Sdrh sSrc.nSrc = 1; 14363780be11Sdrh sSrc.a[0].zName = pTab->zName; 14373780be11Sdrh sSrc.a[0].pTab = pTab; 14383780be11Sdrh sSrc.a[0].iCursor = -1; 14393780be11Sdrh sNC.pParse = pParse; 14403780be11Sdrh sNC.pSrcList = &sSrc; 14413780be11Sdrh sNC.ncFlags = type; 14423780be11Sdrh if( sqlite3ResolveExprNames(&sNC, pExpr) ) return; 14433780be11Sdrh if( pList ){ 14443780be11Sdrh for(i=0; i<pList->nExpr; i++){ 14453780be11Sdrh if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){ 14463780be11Sdrh return; 14473780be11Sdrh } 14483780be11Sdrh } 14493780be11Sdrh } 14503780be11Sdrh } 1451