xref: /sqlite-3.40.0/src/resolve.c (revision abfa6d52)
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;
1108b213899Sdrh     if( pEList->a[iCol].iAlias==0 ){
111ea678832Sdrh       pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
1128b213899Sdrh     }
1138b213899Sdrh     pDup->iTable = pEList->a[iCol].iAlias;
114b7916a78Sdrh   }
1150a8a406eSdrh   if( pExpr->op==TK_COLLATE ){
1160a8a406eSdrh     pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
1170a8a406eSdrh   }
118f6963f99Sdan 
119f6963f99Sdan   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
120f6963f99Sdan   ** prevents ExprDelete() from deleting the Expr structure itself,
121f6963f99Sdan   ** allowing it to be repopulated by the memcpy() on the following line.
122bd13d34bSdrh   ** The pExpr->u.zToken might point into memory that will be freed by the
123bd13d34bSdrh   ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
124bd13d34bSdrh   ** make a copy of the token before doing the sqlite3DbFree().
125f6963f99Sdan   */
126f6963f99Sdan   ExprSetProperty(pExpr, EP_Static);
127f6963f99Sdan   sqlite3ExprDelete(db, pExpr);
1288b213899Sdrh   memcpy(pExpr, pDup, sizeof(*pExpr));
1290a8a406eSdrh   if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
1300a8a406eSdrh     assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
1310a8a406eSdrh     pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
1320a8a406eSdrh     pExpr->flags2 |= EP2_MallocedToken;
1330a8a406eSdrh   }
1348b213899Sdrh   sqlite3DbFree(db, pDup);
1358b213899Sdrh }
1368b213899Sdrh 
137e802c5daSdrh 
138e802c5daSdrh /*
139e802c5daSdrh ** Return TRUE if the name zCol occurs anywhere in the USING clause.
140e802c5daSdrh **
141e802c5daSdrh ** Return FALSE if the USING clause is NULL or if it does not contain
142e802c5daSdrh ** zCol.
143e802c5daSdrh */
144e802c5daSdrh static int nameInUsingClause(IdList *pUsing, const char *zCol){
145e802c5daSdrh   if( pUsing ){
146e802c5daSdrh     int k;
147e802c5daSdrh     for(k=0; k<pUsing->nId; k++){
148e802c5daSdrh       if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
149e802c5daSdrh     }
150e802c5daSdrh   }
151e802c5daSdrh   return 0;
152e802c5daSdrh }
153e802c5daSdrh 
1543e3f1a5bSdrh /*
1553e3f1a5bSdrh ** Subqueries stores the original database, table and column names for their
1563e3f1a5bSdrh ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
1573e3f1a5bSdrh ** Check to see if the zSpan given to this routine matches the zDb, zTab,
1583e3f1a5bSdrh ** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
1593e3f1a5bSdrh ** match anything.
1603e3f1a5bSdrh */
1613e3f1a5bSdrh int sqlite3MatchSpanName(
1623e3f1a5bSdrh   const char *zSpan,
1633e3f1a5bSdrh   const char *zCol,
1643e3f1a5bSdrh   const char *zTab,
1653e3f1a5bSdrh   const char *zDb
1663e3f1a5bSdrh ){
1673e3f1a5bSdrh   int n;
1683e3f1a5bSdrh   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
169dd1dd489Sdrh   if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
1703e3f1a5bSdrh     return 0;
1713e3f1a5bSdrh   }
1723e3f1a5bSdrh   zSpan += n+1;
1733e3f1a5bSdrh   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
174dd1dd489Sdrh   if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
1753e3f1a5bSdrh     return 0;
1763e3f1a5bSdrh   }
1773e3f1a5bSdrh   zSpan += n+1;
1783e3f1a5bSdrh   if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
1793e3f1a5bSdrh     return 0;
1803e3f1a5bSdrh   }
1813e3f1a5bSdrh   return 1;
1823e3f1a5bSdrh }
183e802c5daSdrh 
1848b213899Sdrh /*
1857d10d5a6Sdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1867d10d5a6Sdrh ** that name in the set of source tables in pSrcList and make the pExpr
1877d10d5a6Sdrh ** expression node refer back to that source column.  The following changes
1887d10d5a6Sdrh ** are made to pExpr:
1897d10d5a6Sdrh **
1907d10d5a6Sdrh **    pExpr->iDb           Set the index in db->aDb[] of the database X
1917d10d5a6Sdrh **                         (even if X is implied).
1927d10d5a6Sdrh **    pExpr->iTable        Set to the cursor number for the table obtained
1937d10d5a6Sdrh **                         from pSrcList.
1947d10d5a6Sdrh **    pExpr->pTab          Points to the Table structure of X.Y (even if
1957d10d5a6Sdrh **                         X and/or Y are implied.)
1967d10d5a6Sdrh **    pExpr->iColumn       Set to the column number within the table.
1977d10d5a6Sdrh **    pExpr->op            Set to TK_COLUMN.
1987d10d5a6Sdrh **    pExpr->pLeft         Any expression this points to is deleted
1997d10d5a6Sdrh **    pExpr->pRight        Any expression this points to is deleted.
2007d10d5a6Sdrh **
201b7916a78Sdrh ** The zDb variable is the name of the database (the "X").  This value may be
2027d10d5a6Sdrh ** NULL meaning that name is of the form Y.Z or Z.  Any available database
203b7916a78Sdrh ** can be used.  The zTable variable is the name of the table (the "Y").  This
204b7916a78Sdrh ** value can be NULL if zDb is also NULL.  If zTable is NULL it
2057d10d5a6Sdrh ** means that the form of the name is Z and that columns from any table
2067d10d5a6Sdrh ** can be used.
2077d10d5a6Sdrh **
2087d10d5a6Sdrh ** If the name cannot be resolved unambiguously, leave an error message
209f7828b5cSdrh ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
2107d10d5a6Sdrh */
2117d10d5a6Sdrh static int lookupName(
2127d10d5a6Sdrh   Parse *pParse,       /* The parsing context */
213b7916a78Sdrh   const char *zDb,     /* Name of the database containing table, or NULL */
214b7916a78Sdrh   const char *zTab,    /* Name of table containing column, or NULL */
215b7916a78Sdrh   const char *zCol,    /* Name of the column. */
2167d10d5a6Sdrh   NameContext *pNC,    /* The name context used to resolve the name */
2177d10d5a6Sdrh   Expr *pExpr          /* Make this EXPR node point to the selected column */
2187d10d5a6Sdrh ){
2197d10d5a6Sdrh   int i, j;                         /* Loop counters */
2207d10d5a6Sdrh   int cnt = 0;                      /* Number of matching column names */
2217d10d5a6Sdrh   int cntTab = 0;                   /* Number of matching table names */
222ed551b95Sdrh   int nSubquery = 0;                /* How many levels of subquery */
2237d10d5a6Sdrh   sqlite3 *db = pParse->db;         /* The database connection */
2247d10d5a6Sdrh   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
2257d10d5a6Sdrh   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
2267d10d5a6Sdrh   NameContext *pTopNC = pNC;        /* First namecontext in the list */
2277d10d5a6Sdrh   Schema *pSchema = 0;              /* Schema of the expression */
2282bd93516Sdan   int isTrigger = 0;
2297d10d5a6Sdrh 
230e34c647eSshane   assert( pNC );     /* the name context cannot be NULL. */
231b7916a78Sdrh   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
2325a05be1bSdrh   assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
2337d10d5a6Sdrh 
2347d10d5a6Sdrh   /* Initialize the node to no-match */
2357d10d5a6Sdrh   pExpr->iTable = -1;
2367d10d5a6Sdrh   pExpr->pTab = 0;
23733e619fcSdrh   ExprSetIrreducible(pExpr);
2387d10d5a6Sdrh 
2398f25d18bSdrh   /* Translate the schema name in zDb into a pointer to the corresponding
2408f25d18bSdrh   ** schema.  If not found, pSchema will remain NULL and nothing will match
2418f25d18bSdrh   ** resulting in an appropriate error message toward the end of this routine
2428f25d18bSdrh   */
2438f25d18bSdrh   if( zDb ){
2441e7d43c9Sdrh     testcase( pNC->ncFlags & NC_PartIdx );
2451e7d43c9Sdrh     testcase( pNC->ncFlags & NC_IsCheck );
2461e7d43c9Sdrh     if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
2471e7d43c9Sdrh       /* Silently ignore database qualifiers inside CHECK constraints and partial
2481e7d43c9Sdrh       ** indices.  Do not raise errors because that might break legacy and
2491e7d43c9Sdrh       ** because it does not hurt anything to just ignore the database name. */
2501e7d43c9Sdrh       zDb = 0;
2511e7d43c9Sdrh     }else{
2528f25d18bSdrh       for(i=0; i<db->nDb; i++){
2538f25d18bSdrh         assert( db->aDb[i].zName );
2548f25d18bSdrh         if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
2558f25d18bSdrh           pSchema = db->aDb[i].pSchema;
2568f25d18bSdrh           break;
2578f25d18bSdrh         }
2588f25d18bSdrh       }
2598f25d18bSdrh     }
2601e7d43c9Sdrh   }
2618f25d18bSdrh 
2627d10d5a6Sdrh   /* Start at the inner-most context and move outward until a match is found */
2637d10d5a6Sdrh   while( pNC && cnt==0 ){
2647d10d5a6Sdrh     ExprList *pEList;
2657d10d5a6Sdrh     SrcList *pSrcList = pNC->pSrcList;
2667d10d5a6Sdrh 
2677d10d5a6Sdrh     if( pSrcList ){
2687d10d5a6Sdrh       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
2697d10d5a6Sdrh         Table *pTab;
2707d10d5a6Sdrh         Column *pCol;
2717d10d5a6Sdrh 
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     */
331165921a7Sdan     if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
33265a7cd16Sdan       int op = pParse->eTriggerOp;
3337d10d5a6Sdrh       Table *pTab = 0;
33465a7cd16Sdan       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
33565a7cd16Sdan       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
336165921a7Sdan         pExpr->iTable = 1;
337165921a7Sdan         pTab = pParse->pTriggerTab;
33865a7cd16Sdan       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
339165921a7Sdan         pExpr->iTable = 0;
340165921a7Sdan         pTab = pParse->pTriggerTab;
3417d10d5a6Sdrh       }
3427d10d5a6Sdrh 
3437d10d5a6Sdrh       if( pTab ){
3447d10d5a6Sdrh         int iCol;
3457d10d5a6Sdrh         pSchema = pTab->pSchema;
3467d10d5a6Sdrh         cntTab++;
347165921a7Sdan         for(iCol=0; iCol<pTab->nCol; iCol++){
348165921a7Sdan           Column *pCol = &pTab->aCol[iCol];
3497d10d5a6Sdrh           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
3502bd93516Sdan             if( iCol==pTab->iPKey ){
3512bd93516Sdan               iCol = -1;
3522bd93516Sdan             }
3537d10d5a6Sdrh             break;
3547d10d5a6Sdrh           }
3557d10d5a6Sdrh         }
35625e978dfSdrh         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
357c79c761fSdrh           iCol = -1;        /* IMP: R-44911-55124 */
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     */
3837d10d5a6Sdrh     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
3847d10d5a6Sdrh       cnt = 1;
385c79c761fSdrh       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
3867d10d5a6Sdrh       pExpr->affinity = SQLITE_AFF_INTEGER;
3877d10d5a6Sdrh     }
3887d10d5a6Sdrh 
3897d10d5a6Sdrh     /*
3907d10d5a6Sdrh     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
3917d10d5a6Sdrh     ** might refer to an result-set alias.  This happens, for example, when
3927d10d5a6Sdrh     ** we are resolving names in the WHERE clause of the following command:
3937d10d5a6Sdrh     **
3947d10d5a6Sdrh     **     SELECT a+b AS x FROM table WHERE x<10;
3957d10d5a6Sdrh     **
3967d10d5a6Sdrh     ** In cases like this, replace pExpr with a copy of the expression that
3977d10d5a6Sdrh     ** forms the result set entry ("a+b" in the example) and return immediately.
3987d10d5a6Sdrh     ** Note that the expression in the result set should have already been
3997d10d5a6Sdrh     ** resolved by the time the WHERE clause is resolved.
400e35463b3Sdrh     **
401e35463b3Sdrh     ** The ability to use an output result-set column in the WHERE, GROUP BY,
402e35463b3Sdrh     ** or HAVING clauses, or as part of a larger expression in the ORDRE BY
403e35463b3Sdrh     ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
404e35463b3Sdrh     ** is supported for backwards compatibility only.  TO DO: Issue a warning
405e35463b3Sdrh     ** on sqlite3_log() whenever the capability is used.
4067d10d5a6Sdrh     */
407a3a5bd9bSdrh     if( (pEList = pNC->pEList)!=0
408a3a5bd9bSdrh      && zTab==0
409e35463b3Sdrh      && cnt==0
410a3a5bd9bSdrh     ){
4117d10d5a6Sdrh       for(j=0; j<pEList->nExpr; j++){
4127d10d5a6Sdrh         char *zAs = pEList->a[j].zName;
4137d10d5a6Sdrh         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
4148b213899Sdrh           Expr *pOrig;
4157d10d5a6Sdrh           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
4166ab3a2ecSdanielk1977           assert( pExpr->x.pList==0 );
4176ab3a2ecSdanielk1977           assert( pExpr->x.pSelect==0 );
4187d10d5a6Sdrh           pOrig = pEList->a[j].pExpr;
419a51009b2Sdrh           if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
4207d10d5a6Sdrh             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
421f7828b5cSdrh             return WRC_Abort;
4227d10d5a6Sdrh           }
423ed551b95Sdrh           resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
4247d10d5a6Sdrh           cnt = 1;
4257d10d5a6Sdrh           pMatch = 0;
4267d10d5a6Sdrh           assert( zTab==0 && zDb==0 );
427b7916a78Sdrh           goto lookupname_end;
4287d10d5a6Sdrh         }
4297d10d5a6Sdrh       }
4307d10d5a6Sdrh     }
4317d10d5a6Sdrh 
4327d10d5a6Sdrh     /* Advance to the next name context.  The loop will exit when either
4337d10d5a6Sdrh     ** we have a match (cnt>0) or when we run out of name contexts.
4347d10d5a6Sdrh     */
4357d10d5a6Sdrh     if( cnt==0 ){
4367d10d5a6Sdrh       pNC = pNC->pNext;
437ed551b95Sdrh       nSubquery++;
4387d10d5a6Sdrh     }
4397d10d5a6Sdrh   }
4407d10d5a6Sdrh 
4417d10d5a6Sdrh   /*
4427d10d5a6Sdrh   ** If X and Y are NULL (in other words if only the column name Z is
4437d10d5a6Sdrh   ** supplied) and the value of Z is enclosed in double-quotes, then
4447d10d5a6Sdrh   ** Z is a string literal if it doesn't match any column names.  In that
4457d10d5a6Sdrh   ** case, we need to return right away and not make any changes to
4467d10d5a6Sdrh   ** pExpr.
4477d10d5a6Sdrh   **
4487d10d5a6Sdrh   ** Because no reference was made to outer contexts, the pNC->nRef
4497d10d5a6Sdrh   ** fields are not changed in any context.
4507d10d5a6Sdrh   */
45124fb627aSdrh   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
4527d10d5a6Sdrh     pExpr->op = TK_STRING;
4531885d1c2Sdrh     pExpr->pTab = 0;
454f7828b5cSdrh     return WRC_Prune;
4557d10d5a6Sdrh   }
4567d10d5a6Sdrh 
4577d10d5a6Sdrh   /*
4587d10d5a6Sdrh   ** cnt==0 means there was not match.  cnt>1 means there were two or
4597d10d5a6Sdrh   ** more matches.  Either way, we have an error.
4607d10d5a6Sdrh   */
4617d10d5a6Sdrh   if( cnt!=1 ){
4627d10d5a6Sdrh     const char *zErr;
4637d10d5a6Sdrh     zErr = cnt==0 ? "no such column" : "ambiguous column name";
4647d10d5a6Sdrh     if( zDb ){
4657d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
4667d10d5a6Sdrh     }else if( zTab ){
4677d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
4687d10d5a6Sdrh     }else{
4697d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
4707d10d5a6Sdrh     }
4711db95106Sdan     pParse->checkSchema = 1;
4727d10d5a6Sdrh     pTopNC->nErr++;
4737d10d5a6Sdrh   }
4747d10d5a6Sdrh 
4757d10d5a6Sdrh   /* If a column from a table in pSrcList is referenced, then record
4767d10d5a6Sdrh   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
4777d10d5a6Sdrh   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
4787d10d5a6Sdrh   ** column number is greater than the number of bits in the bitmask
4797d10d5a6Sdrh   ** then set the high-order bit of the bitmask.
4807d10d5a6Sdrh   */
4812d2e7bd3Sdanielk1977   if( pExpr->iColumn>=0 && pMatch!=0 ){
4827d10d5a6Sdrh     int n = pExpr->iColumn;
48300e13613Sdanielk1977     testcase( n==BMS-1 );
48400e13613Sdanielk1977     if( n>=BMS ){
48500e13613Sdanielk1977       n = BMS-1;
4867d10d5a6Sdrh     }
4877d10d5a6Sdrh     assert( pMatch->iCursor==pExpr->iTable );
4887d10d5a6Sdrh     pMatch->colUsed |= ((Bitmask)1)<<n;
4897d10d5a6Sdrh   }
4907d10d5a6Sdrh 
4917d10d5a6Sdrh   /* Clean up and return
4927d10d5a6Sdrh   */
4937d10d5a6Sdrh   sqlite3ExprDelete(db, pExpr->pLeft);
4947d10d5a6Sdrh   pExpr->pLeft = 0;
4957d10d5a6Sdrh   sqlite3ExprDelete(db, pExpr->pRight);
4967d10d5a6Sdrh   pExpr->pRight = 0;
4972bd93516Sdan   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
498b7916a78Sdrh lookupname_end:
4997d10d5a6Sdrh   if( cnt==1 ){
5007d10d5a6Sdrh     assert( pNC!=0 );
501a3a5bd9bSdrh     if( pExpr->op!=TK_AS ){
5027d10d5a6Sdrh       sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
503a3a5bd9bSdrh     }
5047d10d5a6Sdrh     /* Increment the nRef value on all name contexts from TopNC up to
5057d10d5a6Sdrh     ** the point where the name matched. */
5067d10d5a6Sdrh     for(;;){
5077d10d5a6Sdrh       assert( pTopNC!=0 );
5087d10d5a6Sdrh       pTopNC->nRef++;
5097d10d5a6Sdrh       if( pTopNC==pNC ) break;
5107d10d5a6Sdrh       pTopNC = pTopNC->pNext;
5117d10d5a6Sdrh     }
512f7828b5cSdrh     return WRC_Prune;
5137d10d5a6Sdrh   } else {
514f7828b5cSdrh     return WRC_Abort;
5157d10d5a6Sdrh   }
5167d10d5a6Sdrh }
5177d10d5a6Sdrh 
5187d10d5a6Sdrh /*
519f7b0b0adSdan ** Allocate and return a pointer to an expression to load the column iCol
5209e48165bSdrh ** from datasource iSrc in SrcList pSrc.
521f7b0b0adSdan */
522f7b0b0adSdan Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
523f7b0b0adSdan   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
524f7b0b0adSdan   if( p ){
525f7b0b0adSdan     struct SrcList_item *pItem = &pSrc->a[iSrc];
526f7b0b0adSdan     p->pTab = pItem->pTab;
527f7b0b0adSdan     p->iTable = pItem->iCursor;
528f7b0b0adSdan     if( p->pTab->iPKey==iCol ){
529f7b0b0adSdan       p->iColumn = -1;
530f7b0b0adSdan     }else{
5318677d308Sdrh       p->iColumn = (ynVar)iCol;
5327caba669Sdrh       testcase( iCol==BMS );
5337caba669Sdrh       testcase( iCol==BMS-1 );
534f7b0b0adSdan       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
535f7b0b0adSdan     }
536f7b0b0adSdan     ExprSetProperty(p, EP_Resolved);
537f7b0b0adSdan   }
538f7b0b0adSdan   return p;
539f7b0b0adSdan }
540f7b0b0adSdan 
541f7b0b0adSdan /*
5423780be11Sdrh ** Report an error that an expression is not valid for a partial index WHERE
5433780be11Sdrh ** clause.
5443780be11Sdrh */
5453780be11Sdrh static void notValidPartIdxWhere(
5463780be11Sdrh   Parse *pParse,       /* Leave error message here */
5473780be11Sdrh   NameContext *pNC,    /* The name context */
5483780be11Sdrh   const char *zMsg     /* Type of error */
5493780be11Sdrh ){
5503780be11Sdrh   if( (pNC->ncFlags & NC_PartIdx)!=0 ){
5513780be11Sdrh     sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
5523780be11Sdrh                     zMsg);
5533780be11Sdrh   }
5543780be11Sdrh }
5553780be11Sdrh 
5563780be11Sdrh #ifndef SQLITE_OMIT_CHECK
5573780be11Sdrh /*
5583780be11Sdrh ** Report an error that an expression is not valid for a CHECK constraint.
5593780be11Sdrh */
5603780be11Sdrh static void notValidCheckConstraint(
5613780be11Sdrh   Parse *pParse,       /* Leave error message here */
5623780be11Sdrh   NameContext *pNC,    /* The name context */
5633780be11Sdrh   const char *zMsg     /* Type of error */
5643780be11Sdrh ){
5653780be11Sdrh   if( (pNC->ncFlags & NC_IsCheck)!=0 ){
5663780be11Sdrh     sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
5673780be11Sdrh   }
5683780be11Sdrh }
5693780be11Sdrh #else
5703780be11Sdrh # define notValidCheckConstraint(P,N,M)
5713780be11Sdrh #endif
5723780be11Sdrh 
573cca9f3d2Sdrh /*
574cca9f3d2Sdrh ** Expression p should encode a floating point value between 1.0 and 0.0.
575cca9f3d2Sdrh ** Return 1024 times this value.  Or return -1 if p is not a floating point
576cca9f3d2Sdrh ** value between 1.0 and 0.0.
577cca9f3d2Sdrh */
578cca9f3d2Sdrh static int exprProbability(Expr *p){
579cca9f3d2Sdrh   double r = -1.0;
580cca9f3d2Sdrh   if( p->op!=TK_FLOAT ) return -1;
581cca9f3d2Sdrh   sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
582cca9f3d2Sdrh   if( r<0.0 || r>1.0 ) return -1;
583cca9f3d2Sdrh   return (int)(r*1000.0);
584cca9f3d2Sdrh }
5853780be11Sdrh 
5863780be11Sdrh /*
5877d10d5a6Sdrh ** This routine is callback for sqlite3WalkExpr().
5887d10d5a6Sdrh **
5897d10d5a6Sdrh ** Resolve symbolic names into TK_COLUMN operators for the current
5907d10d5a6Sdrh ** node in the expression tree.  Return 0 to continue the search down
5917d10d5a6Sdrh ** the tree or 2 to abort the tree walk.
5927d10d5a6Sdrh **
5937d10d5a6Sdrh ** This routine also does error checking and name resolution for
5947d10d5a6Sdrh ** function names.  The operator for aggregate functions is changed
5957d10d5a6Sdrh ** to TK_AGG_FUNCTION.
5967d10d5a6Sdrh */
5977d10d5a6Sdrh static int resolveExprStep(Walker *pWalker, Expr *pExpr){
5987d10d5a6Sdrh   NameContext *pNC;
5997d10d5a6Sdrh   Parse *pParse;
6007d10d5a6Sdrh 
6017d10d5a6Sdrh   pNC = pWalker->u.pNC;
6027d10d5a6Sdrh   assert( pNC!=0 );
6037d10d5a6Sdrh   pParse = pNC->pParse;
6047d10d5a6Sdrh   assert( pParse==pWalker->pParse );
6057d10d5a6Sdrh 
6067d10d5a6Sdrh   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
6077d10d5a6Sdrh   ExprSetProperty(pExpr, EP_Resolved);
6087d10d5a6Sdrh #ifndef NDEBUG
6097d10d5a6Sdrh   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
6107d10d5a6Sdrh     SrcList *pSrcList = pNC->pSrcList;
6117d10d5a6Sdrh     int i;
6127d10d5a6Sdrh     for(i=0; i<pNC->pSrcList->nSrc; i++){
6137d10d5a6Sdrh       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
6147d10d5a6Sdrh     }
6157d10d5a6Sdrh   }
6167d10d5a6Sdrh #endif
6177d10d5a6Sdrh   switch( pExpr->op ){
61841204f1fSdrh 
619273f619bSshane #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
62041204f1fSdrh     /* The special operator TK_ROW means use the rowid for the first
62141204f1fSdrh     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
62241204f1fSdrh     ** clause processing on UPDATE and DELETE statements.
62341204f1fSdrh     */
62441204f1fSdrh     case TK_ROW: {
62541204f1fSdrh       SrcList *pSrcList = pNC->pSrcList;
62641204f1fSdrh       struct SrcList_item *pItem;
62741204f1fSdrh       assert( pSrcList && pSrcList->nSrc==1 );
62841204f1fSdrh       pItem = pSrcList->a;
62941204f1fSdrh       pExpr->op = TK_COLUMN;
63041204f1fSdrh       pExpr->pTab = pItem->pTab;
63141204f1fSdrh       pExpr->iTable = pItem->iCursor;
63241204f1fSdrh       pExpr->iColumn = -1;
63341204f1fSdrh       pExpr->affinity = SQLITE_AFF_INTEGER;
63441204f1fSdrh       break;
63541204f1fSdrh     }
636273f619bSshane #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
63741204f1fSdrh 
6387d10d5a6Sdrh     /* A lone identifier is the name of a column.
6397d10d5a6Sdrh     */
6407d10d5a6Sdrh     case TK_ID: {
641f7828b5cSdrh       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
6427d10d5a6Sdrh     }
6437d10d5a6Sdrh 
6447d10d5a6Sdrh     /* A table name and column name:     ID.ID
6457d10d5a6Sdrh     ** Or a database, table and column:  ID.ID.ID
6467d10d5a6Sdrh     */
6477d10d5a6Sdrh     case TK_DOT: {
648b7916a78Sdrh       const char *zColumn;
649b7916a78Sdrh       const char *zTable;
650b7916a78Sdrh       const char *zDb;
6517d10d5a6Sdrh       Expr *pRight;
6527d10d5a6Sdrh 
6537d10d5a6Sdrh       /* if( pSrcList==0 ) break; */
6547d10d5a6Sdrh       pRight = pExpr->pRight;
6557d10d5a6Sdrh       if( pRight->op==TK_ID ){
656b7916a78Sdrh         zDb = 0;
65733e619fcSdrh         zTable = pExpr->pLeft->u.zToken;
65833e619fcSdrh         zColumn = pRight->u.zToken;
6597d10d5a6Sdrh       }else{
6607d10d5a6Sdrh         assert( pRight->op==TK_DOT );
66133e619fcSdrh         zDb = pExpr->pLeft->u.zToken;
66233e619fcSdrh         zTable = pRight->pLeft->u.zToken;
66333e619fcSdrh         zColumn = pRight->pRight->u.zToken;
6647d10d5a6Sdrh       }
665f7828b5cSdrh       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
6667d10d5a6Sdrh     }
6677d10d5a6Sdrh 
6687d10d5a6Sdrh     /* Resolve function names
6697d10d5a6Sdrh     */
6707d10d5a6Sdrh     case TK_CONST_FUNC:
6717d10d5a6Sdrh     case TK_FUNCTION: {
6726ab3a2ecSdanielk1977       ExprList *pList = pExpr->x.pList;    /* The argument list */
6737d10d5a6Sdrh       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
6747d10d5a6Sdrh       int no_such_func = 0;       /* True if no such function exists */
6757d10d5a6Sdrh       int wrong_num_args = 0;     /* True if wrong number of arguments */
6767d10d5a6Sdrh       int is_agg = 0;             /* True if is an aggregate function */
6777d10d5a6Sdrh       int auth;                   /* Authorization to use the function */
6787d10d5a6Sdrh       int nId;                    /* Number of characters in function name */
6797d10d5a6Sdrh       const char *zId;            /* The function name. */
6807d10d5a6Sdrh       FuncDef *pDef;              /* Information about the function */
681ea678832Sdrh       u8 enc = ENC(pParse->db);   /* The database encoding */
6827d10d5a6Sdrh 
68373c0fdc7Sdrh       testcase( pExpr->op==TK_CONST_FUNC );
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 ){
699cca9f3d2Sdrh           ExprSetProperty(pExpr, EP_Hint);
700cca9f3d2Sdrh           if( n==2 ){
701cca9f3d2Sdrh             pExpr->iTable = exprProbability(pList->a[1].pExpr);
702cca9f3d2Sdrh             if( pExpr->iTable<0 ){
703cca9f3d2Sdrh               sqlite3ErrorMsg(pParse, "second parameter to unlikely() must be "
704cca9f3d2Sdrh                                       "between 0.0 and 1.0");
705cca9f3d2Sdrh               pNC->nErr++;
706cca9f3d2Sdrh             }
707cca9f3d2Sdrh           }else{
708*abfa6d52Sdrh             pExpr->iTable = 62;  /* TUNING:  Default 2nd arg to unlikely() is 0.0625 */
709cca9f3d2Sdrh           }
710cca9f3d2Sdrh         }
7117d10d5a6Sdrh       }
7127d10d5a6Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION
7137d10d5a6Sdrh       if( pDef ){
7147d10d5a6Sdrh         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
7157d10d5a6Sdrh         if( auth!=SQLITE_OK ){
7167d10d5a6Sdrh           if( auth==SQLITE_DENY ){
7177d10d5a6Sdrh             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
7187d10d5a6Sdrh                                     pDef->zName);
7197d10d5a6Sdrh             pNC->nErr++;
7207d10d5a6Sdrh           }
7217d10d5a6Sdrh           pExpr->op = TK_NULL;
7227d10d5a6Sdrh           return WRC_Prune;
7237d10d5a6Sdrh         }
7247d10d5a6Sdrh       }
7257d10d5a6Sdrh #endif
726a51009b2Sdrh       if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
7277d10d5a6Sdrh         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
7287d10d5a6Sdrh         pNC->nErr++;
7297d10d5a6Sdrh         is_agg = 0;
730ddd1fc72Sdrh       }else if( no_such_func && pParse->db->init.busy==0 ){
7317d10d5a6Sdrh         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
7327d10d5a6Sdrh         pNC->nErr++;
7337d10d5a6Sdrh       }else if( wrong_num_args ){
7347d10d5a6Sdrh         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
7357d10d5a6Sdrh              nId, zId);
7367d10d5a6Sdrh         pNC->nErr++;
7377d10d5a6Sdrh       }
738a51009b2Sdrh       if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
7397d10d5a6Sdrh       sqlite3WalkExprList(pWalker, pList);
740030796dfSdrh       if( is_agg ){
741030796dfSdrh         NameContext *pNC2 = pNC;
742030796dfSdrh         pExpr->op = TK_AGG_FUNCTION;
743030796dfSdrh         pExpr->op2 = 0;
744030796dfSdrh         while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
745030796dfSdrh           pExpr->op2++;
746030796dfSdrh           pNC2 = pNC2->pNext;
747030796dfSdrh         }
748030796dfSdrh         if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
749030796dfSdrh         pNC->ncFlags |= NC_AllowAgg;
750030796dfSdrh       }
7517d10d5a6Sdrh       /* FIX ME:  Compute pExpr->affinity based on the expected return
7527d10d5a6Sdrh       ** type of the function
7537d10d5a6Sdrh       */
7547d10d5a6Sdrh       return WRC_Prune;
7557d10d5a6Sdrh     }
7567d10d5a6Sdrh #ifndef SQLITE_OMIT_SUBQUERY
7577d10d5a6Sdrh     case TK_SELECT:
75873c0fdc7Sdrh     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
7597d10d5a6Sdrh #endif
7607d10d5a6Sdrh     case TK_IN: {
76173c0fdc7Sdrh       testcase( pExpr->op==TK_IN );
7626ab3a2ecSdanielk1977       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
7637d10d5a6Sdrh         int nRef = pNC->nRef;
7643780be11Sdrh         notValidCheckConstraint(pParse, pNC, "subqueries");
7653780be11Sdrh         notValidPartIdxWhere(pParse, pNC, "subqueries");
7666ab3a2ecSdanielk1977         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
7677d10d5a6Sdrh         assert( pNC->nRef>=nRef );
7687d10d5a6Sdrh         if( nRef!=pNC->nRef ){
7697d10d5a6Sdrh           ExprSetProperty(pExpr, EP_VarSelect);
7707d10d5a6Sdrh         }
7717d10d5a6Sdrh       }
7727d10d5a6Sdrh       break;
7737d10d5a6Sdrh     }
7747d10d5a6Sdrh     case TK_VARIABLE: {
7753780be11Sdrh       notValidCheckConstraint(pParse, pNC, "parameters");
7763780be11Sdrh       notValidPartIdxWhere(pParse, pNC, "parameters");
7777d10d5a6Sdrh       break;
7787d10d5a6Sdrh     }
7797d10d5a6Sdrh   }
7807d10d5a6Sdrh   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
7817d10d5a6Sdrh }
7827d10d5a6Sdrh 
7837d10d5a6Sdrh /*
7847d10d5a6Sdrh ** pEList is a list of expressions which are really the result set of the
7857d10d5a6Sdrh ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
7867d10d5a6Sdrh ** This routine checks to see if pE is a simple identifier which corresponds
7877d10d5a6Sdrh ** to the AS-name of one of the terms of the expression list.  If it is,
7887d10d5a6Sdrh ** this routine return an integer between 1 and N where N is the number of
7897d10d5a6Sdrh ** elements in pEList, corresponding to the matching entry.  If there is
7907d10d5a6Sdrh ** no match, or if pE is not a simple identifier, then this routine
7917d10d5a6Sdrh ** return 0.
7927d10d5a6Sdrh **
7937d10d5a6Sdrh ** pEList has been resolved.  pE has not.
7947d10d5a6Sdrh */
7957d10d5a6Sdrh static int resolveAsName(
7967d10d5a6Sdrh   Parse *pParse,     /* Parsing context for error messages */
7977d10d5a6Sdrh   ExprList *pEList,  /* List of expressions to scan */
7987d10d5a6Sdrh   Expr *pE           /* Expression we are trying to match */
7997d10d5a6Sdrh ){
8007d10d5a6Sdrh   int i;             /* Loop counter */
8017d10d5a6Sdrh 
802cf697396Sshane   UNUSED_PARAMETER(pParse);
803cf697396Sshane 
80473c0fdc7Sdrh   if( pE->op==TK_ID ){
80533e619fcSdrh     char *zCol = pE->u.zToken;
8067d10d5a6Sdrh     for(i=0; i<pEList->nExpr; i++){
8077d10d5a6Sdrh       char *zAs = pEList->a[i].zName;
8087d10d5a6Sdrh       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
8097d10d5a6Sdrh         return i+1;
8107d10d5a6Sdrh       }
8117d10d5a6Sdrh     }
8127d10d5a6Sdrh   }
8137d10d5a6Sdrh   return 0;
8147d10d5a6Sdrh }
8157d10d5a6Sdrh 
8167d10d5a6Sdrh /*
8177d10d5a6Sdrh ** pE is a pointer to an expression which is a single term in the
8187d10d5a6Sdrh ** ORDER BY of a compound SELECT.  The expression has not been
8197d10d5a6Sdrh ** name resolved.
8207d10d5a6Sdrh **
8217d10d5a6Sdrh ** At the point this routine is called, we already know that the
8227d10d5a6Sdrh ** ORDER BY term is not an integer index into the result set.  That
8237d10d5a6Sdrh ** case is handled by the calling routine.
8247d10d5a6Sdrh **
8257d10d5a6Sdrh ** Attempt to match pE against result set columns in the left-most
8267d10d5a6Sdrh ** SELECT statement.  Return the index i of the matching column,
8277d10d5a6Sdrh ** as an indication to the caller that it should sort by the i-th column.
8287d10d5a6Sdrh ** The left-most column is 1.  In other words, the value returned is the
8297d10d5a6Sdrh ** same integer value that would be used in the SQL statement to indicate
8307d10d5a6Sdrh ** the column.
8317d10d5a6Sdrh **
8327d10d5a6Sdrh ** If there is no match, return 0.  Return -1 if an error occurs.
8337d10d5a6Sdrh */
8347d10d5a6Sdrh static int resolveOrderByTermToExprList(
8357d10d5a6Sdrh   Parse *pParse,     /* Parsing context for error messages */
8367d10d5a6Sdrh   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
8377d10d5a6Sdrh   Expr *pE           /* The specific ORDER BY term */
8387d10d5a6Sdrh ){
8397d10d5a6Sdrh   int i;             /* Loop counter */
8407d10d5a6Sdrh   ExprList *pEList;  /* The columns of the result set */
8417d10d5a6Sdrh   NameContext nc;    /* Name context for resolving pE */
842a7564663Sdrh   sqlite3 *db;       /* Database connection */
843a7564663Sdrh   int rc;            /* Return code from subprocedures */
844a7564663Sdrh   u8 savedSuppErr;   /* Saved value of db->suppressErr */
8457d10d5a6Sdrh 
8467d10d5a6Sdrh   assert( sqlite3ExprIsInteger(pE, &i)==0 );
8477d10d5a6Sdrh   pEList = pSelect->pEList;
8487d10d5a6Sdrh 
8497d10d5a6Sdrh   /* Resolve all names in the ORDER BY term expression
8507d10d5a6Sdrh   */
8517d10d5a6Sdrh   memset(&nc, 0, sizeof(nc));
8527d10d5a6Sdrh   nc.pParse = pParse;
8537d10d5a6Sdrh   nc.pSrcList = pSelect->pSrc;
8547d10d5a6Sdrh   nc.pEList = pEList;
855a51009b2Sdrh   nc.ncFlags = NC_AllowAgg;
8567d10d5a6Sdrh   nc.nErr = 0;
857a7564663Sdrh   db = pParse->db;
858a7564663Sdrh   savedSuppErr = db->suppressErr;
859a7564663Sdrh   db->suppressErr = 1;
860a7564663Sdrh   rc = sqlite3ResolveExprNames(&nc, pE);
861a7564663Sdrh   db->suppressErr = savedSuppErr;
862a7564663Sdrh   if( rc ) return 0;
8637d10d5a6Sdrh 
8647d10d5a6Sdrh   /* Try to match the ORDER BY expression against an expression
8657d10d5a6Sdrh   ** in the result set.  Return an 1-based index of the matching
8667d10d5a6Sdrh   ** result-set entry.
8677d10d5a6Sdrh   */
8687d10d5a6Sdrh   for(i=0; i<pEList->nExpr; i++){
869619a1305Sdrh     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
8707d10d5a6Sdrh       return i+1;
8717d10d5a6Sdrh     }
8727d10d5a6Sdrh   }
8737d10d5a6Sdrh 
8747d10d5a6Sdrh   /* If no match, return 0. */
8757d10d5a6Sdrh   return 0;
8767d10d5a6Sdrh }
8777d10d5a6Sdrh 
8787d10d5a6Sdrh /*
8797d10d5a6Sdrh ** Generate an ORDER BY or GROUP BY term out-of-range error.
8807d10d5a6Sdrh */
8817d10d5a6Sdrh static void resolveOutOfRangeError(
8827d10d5a6Sdrh   Parse *pParse,         /* The error context into which to write the error */
8837d10d5a6Sdrh   const char *zType,     /* "ORDER" or "GROUP" */
8847d10d5a6Sdrh   int i,                 /* The index (1-based) of the term out of range */
8857d10d5a6Sdrh   int mx                 /* Largest permissible value of i */
8867d10d5a6Sdrh ){
8877d10d5a6Sdrh   sqlite3ErrorMsg(pParse,
8887d10d5a6Sdrh     "%r %s BY term out of range - should be "
8897d10d5a6Sdrh     "between 1 and %d", i, zType, mx);
8907d10d5a6Sdrh }
8917d10d5a6Sdrh 
8927d10d5a6Sdrh /*
8937d10d5a6Sdrh ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
8947d10d5a6Sdrh ** each term of the ORDER BY clause is a constant integer between 1
8957d10d5a6Sdrh ** and N where N is the number of columns in the compound SELECT.
8967d10d5a6Sdrh **
8977d10d5a6Sdrh ** ORDER BY terms that are already an integer between 1 and N are
8987d10d5a6Sdrh ** unmodified.  ORDER BY terms that are integers outside the range of
8997d10d5a6Sdrh ** 1 through N generate an error.  ORDER BY terms that are expressions
9007d10d5a6Sdrh ** are matched against result set expressions of compound SELECT
9017d10d5a6Sdrh ** beginning with the left-most SELECT and working toward the right.
9027d10d5a6Sdrh ** At the first match, the ORDER BY expression is transformed into
9037d10d5a6Sdrh ** the integer column number.
9047d10d5a6Sdrh **
9057d10d5a6Sdrh ** Return the number of errors seen.
9067d10d5a6Sdrh */
9077d10d5a6Sdrh static int resolveCompoundOrderBy(
9087d10d5a6Sdrh   Parse *pParse,        /* Parsing context.  Leave error messages here */
9097d10d5a6Sdrh   Select *pSelect       /* The SELECT statement containing the ORDER BY */
9107d10d5a6Sdrh ){
9117d10d5a6Sdrh   int i;
9127d10d5a6Sdrh   ExprList *pOrderBy;
9137d10d5a6Sdrh   ExprList *pEList;
9147d10d5a6Sdrh   sqlite3 *db;
9157d10d5a6Sdrh   int moreToDo = 1;
9167d10d5a6Sdrh 
9177d10d5a6Sdrh   pOrderBy = pSelect->pOrderBy;
9187d10d5a6Sdrh   if( pOrderBy==0 ) return 0;
9197d10d5a6Sdrh   db = pParse->db;
9207d10d5a6Sdrh #if SQLITE_MAX_COLUMN
9217d10d5a6Sdrh   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
9227d10d5a6Sdrh     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
9237d10d5a6Sdrh     return 1;
9247d10d5a6Sdrh   }
9257d10d5a6Sdrh #endif
9267d10d5a6Sdrh   for(i=0; i<pOrderBy->nExpr; i++){
9277d10d5a6Sdrh     pOrderBy->a[i].done = 0;
9287d10d5a6Sdrh   }
9297d10d5a6Sdrh   pSelect->pNext = 0;
9307d10d5a6Sdrh   while( pSelect->pPrior ){
9317d10d5a6Sdrh     pSelect->pPrior->pNext = pSelect;
9327d10d5a6Sdrh     pSelect = pSelect->pPrior;
9337d10d5a6Sdrh   }
9347d10d5a6Sdrh   while( pSelect && moreToDo ){
9357d10d5a6Sdrh     struct ExprList_item *pItem;
9367d10d5a6Sdrh     moreToDo = 0;
9377d10d5a6Sdrh     pEList = pSelect->pEList;
9380a846f96Sdrh     assert( pEList!=0 );
9397d10d5a6Sdrh     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
9407d10d5a6Sdrh       int iCol = -1;
9417d10d5a6Sdrh       Expr *pE, *pDup;
9427d10d5a6Sdrh       if( pItem->done ) continue;
943bd13d34bSdrh       pE = sqlite3ExprSkipCollate(pItem->pExpr);
9447d10d5a6Sdrh       if( sqlite3ExprIsInteger(pE, &iCol) ){
94573c0fdc7Sdrh         if( iCol<=0 || iCol>pEList->nExpr ){
9467d10d5a6Sdrh           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
9477d10d5a6Sdrh           return 1;
9487d10d5a6Sdrh         }
9497d10d5a6Sdrh       }else{
9507d10d5a6Sdrh         iCol = resolveAsName(pParse, pEList, pE);
9517d10d5a6Sdrh         if( iCol==0 ){
9526ab3a2ecSdanielk1977           pDup = sqlite3ExprDup(db, pE, 0);
9537d10d5a6Sdrh           if( !db->mallocFailed ){
9547d10d5a6Sdrh             assert(pDup);
9557d10d5a6Sdrh             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
9567d10d5a6Sdrh           }
9577d10d5a6Sdrh           sqlite3ExprDelete(db, pDup);
9587d10d5a6Sdrh         }
9597d10d5a6Sdrh       }
9607d10d5a6Sdrh       if( iCol>0 ){
961bd13d34bSdrh         /* Convert the ORDER BY term into an integer column number iCol,
962bd13d34bSdrh         ** taking care to preserve the COLLATE clause if it exists */
963bd13d34bSdrh         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
964bd13d34bSdrh         if( pNew==0 ) return 1;
965bd13d34bSdrh         pNew->flags |= EP_IntValue;
966bd13d34bSdrh         pNew->u.iValue = iCol;
967bd13d34bSdrh         if( pItem->pExpr==pE ){
968bd13d34bSdrh           pItem->pExpr = pNew;
969bd13d34bSdrh         }else{
970bd13d34bSdrh           assert( pItem->pExpr->op==TK_COLLATE );
971bd13d34bSdrh           assert( pItem->pExpr->pLeft==pE );
972bd13d34bSdrh           pItem->pExpr->pLeft = pNew;
973bd13d34bSdrh         }
9747d10d5a6Sdrh         sqlite3ExprDelete(db, pE);
9754b3ac73cSdrh         pItem->iOrderByCol = (u16)iCol;
9767d10d5a6Sdrh         pItem->done = 1;
9777d10d5a6Sdrh       }else{
9787d10d5a6Sdrh         moreToDo = 1;
9797d10d5a6Sdrh       }
9807d10d5a6Sdrh     }
9817d10d5a6Sdrh     pSelect = pSelect->pNext;
9827d10d5a6Sdrh   }
9837d10d5a6Sdrh   for(i=0; i<pOrderBy->nExpr; i++){
9847d10d5a6Sdrh     if( pOrderBy->a[i].done==0 ){
9857d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
9867d10d5a6Sdrh             "column in the result set", i+1);
9877d10d5a6Sdrh       return 1;
9887d10d5a6Sdrh     }
9897d10d5a6Sdrh   }
9907d10d5a6Sdrh   return 0;
9917d10d5a6Sdrh }
9927d10d5a6Sdrh 
9937d10d5a6Sdrh /*
9947d10d5a6Sdrh ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
9957d10d5a6Sdrh ** the SELECT statement pSelect.  If any term is reference to a
996e35463b3Sdrh ** result set expression (as determined by the ExprList.a.iOrderByCol field)
9977d10d5a6Sdrh ** then convert that term into a copy of the corresponding result set
9987d10d5a6Sdrh ** column.
9997d10d5a6Sdrh **
10007d10d5a6Sdrh ** If any errors are detected, add an error message to pParse and
10017d10d5a6Sdrh ** return non-zero.  Return zero if no errors are seen.
10027d10d5a6Sdrh */
10037d10d5a6Sdrh int sqlite3ResolveOrderGroupBy(
10047d10d5a6Sdrh   Parse *pParse,        /* Parsing context.  Leave error messages here */
10057d10d5a6Sdrh   Select *pSelect,      /* The SELECT statement containing the clause */
10067d10d5a6Sdrh   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
10077d10d5a6Sdrh   const char *zType     /* "ORDER" or "GROUP" */
10087d10d5a6Sdrh ){
10097d10d5a6Sdrh   int i;
10107d10d5a6Sdrh   sqlite3 *db = pParse->db;
10117d10d5a6Sdrh   ExprList *pEList;
10127d10d5a6Sdrh   struct ExprList_item *pItem;
10137d10d5a6Sdrh 
10147d10d5a6Sdrh   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
10157d10d5a6Sdrh #if SQLITE_MAX_COLUMN
10167d10d5a6Sdrh   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
10177d10d5a6Sdrh     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
10187d10d5a6Sdrh     return 1;
10197d10d5a6Sdrh   }
10207d10d5a6Sdrh #endif
10217d10d5a6Sdrh   pEList = pSelect->pEList;
10220a846f96Sdrh   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
10237d10d5a6Sdrh   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
10244b3ac73cSdrh     if( pItem->iOrderByCol ){
10254b3ac73cSdrh       if( pItem->iOrderByCol>pEList->nExpr ){
10267d10d5a6Sdrh         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
10277d10d5a6Sdrh         return 1;
10287d10d5a6Sdrh       }
1029ed551b95Sdrh       resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
10307d10d5a6Sdrh     }
10317d10d5a6Sdrh   }
10327d10d5a6Sdrh   return 0;
10337d10d5a6Sdrh }
10347d10d5a6Sdrh 
10357d10d5a6Sdrh /*
10367d10d5a6Sdrh ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
10377d10d5a6Sdrh ** The Name context of the SELECT statement is pNC.  zType is either
10387d10d5a6Sdrh ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
10397d10d5a6Sdrh **
10407d10d5a6Sdrh ** This routine resolves each term of the clause into an expression.
10417d10d5a6Sdrh ** If the order-by term is an integer I between 1 and N (where N is the
10427d10d5a6Sdrh ** number of columns in the result set of the SELECT) then the expression
10437d10d5a6Sdrh ** in the resolution is a copy of the I-th result-set expression.  If
104426080d92Sdrh ** the order-by term is an identifier that corresponds to the AS-name of
10457d10d5a6Sdrh ** a result-set expression, then the term resolves to a copy of the
10467d10d5a6Sdrh ** result-set expression.  Otherwise, the expression is resolved in
10477d10d5a6Sdrh ** the usual way - using sqlite3ResolveExprNames().
10487d10d5a6Sdrh **
10497d10d5a6Sdrh ** This routine returns the number of errors.  If errors occur, then
10507d10d5a6Sdrh ** an appropriate error message might be left in pParse.  (OOM errors
10517d10d5a6Sdrh ** excepted.)
10527d10d5a6Sdrh */
10537d10d5a6Sdrh static int resolveOrderGroupBy(
10547d10d5a6Sdrh   NameContext *pNC,     /* The name context of the SELECT statement */
10557d10d5a6Sdrh   Select *pSelect,      /* The SELECT statement holding pOrderBy */
10567d10d5a6Sdrh   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
10577d10d5a6Sdrh   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
10587d10d5a6Sdrh ){
105970331cd7Sdrh   int i, j;                      /* Loop counters */
10607d10d5a6Sdrh   int iCol;                      /* Column number */
10617d10d5a6Sdrh   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
10627d10d5a6Sdrh   Parse *pParse;                 /* Parsing context */
10637d10d5a6Sdrh   int nResult;                   /* Number of terms in the result set */
10647d10d5a6Sdrh 
10657d10d5a6Sdrh   if( pOrderBy==0 ) return 0;
10667d10d5a6Sdrh   nResult = pSelect->pEList->nExpr;
10677d10d5a6Sdrh   pParse = pNC->pParse;
10687d10d5a6Sdrh   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
10697d10d5a6Sdrh     Expr *pE = pItem->pExpr;
1070e35463b3Sdrh     Expr *pE2 = sqlite3ExprSkipCollate(pE);
10710af16ab2Sdrh     if( zType[0]!='G' ){
1072e35463b3Sdrh       iCol = resolveAsName(pParse, pSelect->pEList, pE2);
10737d10d5a6Sdrh       if( iCol>0 ){
10747d10d5a6Sdrh         /* If an AS-name match is found, mark this ORDER BY column as being
10757d10d5a6Sdrh         ** a copy of the iCol-th result-set column.  The subsequent call to
10767d10d5a6Sdrh         ** sqlite3ResolveOrderGroupBy() will convert the expression to a
10777d10d5a6Sdrh         ** copy of the iCol-th result-set expression. */
10784b3ac73cSdrh         pItem->iOrderByCol = (u16)iCol;
10797d10d5a6Sdrh         continue;
10807d10d5a6Sdrh       }
10810af16ab2Sdrh     }
1082e35463b3Sdrh     if( sqlite3ExprIsInteger(pE2, &iCol) ){
10837d10d5a6Sdrh       /* The ORDER BY term is an integer constant.  Again, set the column
10847d10d5a6Sdrh       ** number so that sqlite3ResolveOrderGroupBy() will convert the
10857d10d5a6Sdrh       ** order-by term to a copy of the result-set expression */
108685d641f9Sdrh       if( iCol<1 || iCol>0xffff ){
10877d10d5a6Sdrh         resolveOutOfRangeError(pParse, zType, i+1, nResult);
10887d10d5a6Sdrh         return 1;
10897d10d5a6Sdrh       }
10904b3ac73cSdrh       pItem->iOrderByCol = (u16)iCol;
10917d10d5a6Sdrh       continue;
10927d10d5a6Sdrh     }
10937d10d5a6Sdrh 
10947d10d5a6Sdrh     /* Otherwise, treat the ORDER BY term as an ordinary expression */
10954b3ac73cSdrh     pItem->iOrderByCol = 0;
10967d10d5a6Sdrh     if( sqlite3ResolveExprNames(pNC, pE) ){
10977d10d5a6Sdrh       return 1;
10987d10d5a6Sdrh     }
109970331cd7Sdrh     for(j=0; j<pSelect->pEList->nExpr; j++){
1100619a1305Sdrh       if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
110170331cd7Sdrh         pItem->iOrderByCol = j+1;
110270331cd7Sdrh       }
110370331cd7Sdrh     }
11047d10d5a6Sdrh   }
11057d10d5a6Sdrh   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
11067d10d5a6Sdrh }
11077d10d5a6Sdrh 
11087d10d5a6Sdrh /*
11097d10d5a6Sdrh ** Resolve names in the SELECT statement p and all of its descendents.
11107d10d5a6Sdrh */
11117d10d5a6Sdrh static int resolveSelectStep(Walker *pWalker, Select *p){
11127d10d5a6Sdrh   NameContext *pOuterNC;  /* Context that contains this SELECT */
11137d10d5a6Sdrh   NameContext sNC;        /* Name context of this SELECT */
11147d10d5a6Sdrh   int isCompound;         /* True if p is a compound select */
11157d10d5a6Sdrh   int nCompound;          /* Number of compound terms processed so far */
11167d10d5a6Sdrh   Parse *pParse;          /* Parsing context */
11177d10d5a6Sdrh   ExprList *pEList;       /* Result set expression list */
11187d10d5a6Sdrh   int i;                  /* Loop counter */
11197d10d5a6Sdrh   ExprList *pGroupBy;     /* The GROUP BY clause */
11207d10d5a6Sdrh   Select *pLeftmost;      /* Left-most of SELECT of a compound */
11217d10d5a6Sdrh   sqlite3 *db;            /* Database connection */
11227d10d5a6Sdrh 
11237d10d5a6Sdrh 
11240a846f96Sdrh   assert( p!=0 );
11257d10d5a6Sdrh   if( p->selFlags & SF_Resolved ){
11267d10d5a6Sdrh     return WRC_Prune;
11277d10d5a6Sdrh   }
11287d10d5a6Sdrh   pOuterNC = pWalker->u.pNC;
11297d10d5a6Sdrh   pParse = pWalker->pParse;
11307d10d5a6Sdrh   db = pParse->db;
11317d10d5a6Sdrh 
11327d10d5a6Sdrh   /* Normally sqlite3SelectExpand() will be called first and will have
11337d10d5a6Sdrh   ** already expanded this SELECT.  However, if this is a subquery within
11347d10d5a6Sdrh   ** an expression, sqlite3ResolveExprNames() will be called without a
11357d10d5a6Sdrh   ** prior call to sqlite3SelectExpand().  When that happens, let
11367d10d5a6Sdrh   ** sqlite3SelectPrep() do all of the processing for this SELECT.
11377d10d5a6Sdrh   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
11387d10d5a6Sdrh   ** this routine in the correct order.
11397d10d5a6Sdrh   */
11407d10d5a6Sdrh   if( (p->selFlags & SF_Expanded)==0 ){
11417d10d5a6Sdrh     sqlite3SelectPrep(pParse, p, pOuterNC);
11427d10d5a6Sdrh     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
11437d10d5a6Sdrh   }
11447d10d5a6Sdrh 
11457d10d5a6Sdrh   isCompound = p->pPrior!=0;
11467d10d5a6Sdrh   nCompound = 0;
11477d10d5a6Sdrh   pLeftmost = p;
11487d10d5a6Sdrh   while( p ){
11497d10d5a6Sdrh     assert( (p->selFlags & SF_Expanded)!=0 );
11507d10d5a6Sdrh     assert( (p->selFlags & SF_Resolved)==0 );
11517d10d5a6Sdrh     p->selFlags |= SF_Resolved;
11527d10d5a6Sdrh 
11537d10d5a6Sdrh     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
11547d10d5a6Sdrh     ** are not allowed to refer to any names, so pass an empty NameContext.
11557d10d5a6Sdrh     */
11567d10d5a6Sdrh     memset(&sNC, 0, sizeof(sNC));
11577d10d5a6Sdrh     sNC.pParse = pParse;
11587d10d5a6Sdrh     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
11597d10d5a6Sdrh         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
11607d10d5a6Sdrh       return WRC_Abort;
11617d10d5a6Sdrh     }
11627d10d5a6Sdrh 
11637d10d5a6Sdrh     /* Recursively resolve names in all subqueries
11647d10d5a6Sdrh     */
11657d10d5a6Sdrh     for(i=0; i<p->pSrc->nSrc; i++){
11667d10d5a6Sdrh       struct SrcList_item *pItem = &p->pSrc->a[i];
11677d10d5a6Sdrh       if( pItem->pSelect ){
1168da79cf0cSdan         NameContext *pNC;         /* Used to iterate name contexts */
1169da79cf0cSdan         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
11707d10d5a6Sdrh         const char *zSavedContext = pParse->zAuthContext;
1171da79cf0cSdan 
1172da79cf0cSdan         /* Count the total number of references to pOuterNC and all of its
1173da79cf0cSdan         ** parent contexts. After resolving references to expressions in
1174da79cf0cSdan         ** pItem->pSelect, check if this value has changed. If so, then
1175da79cf0cSdan         ** SELECT statement pItem->pSelect must be correlated. Set the
1176da79cf0cSdan         ** pItem->isCorrelated flag if this is the case. */
1177da79cf0cSdan         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
1178da79cf0cSdan 
11797d10d5a6Sdrh         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
1180cd2b5613Sdrh         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
11817d10d5a6Sdrh         pParse->zAuthContext = zSavedContext;
11827d10d5a6Sdrh         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
1183da79cf0cSdan 
1184da79cf0cSdan         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
1185da79cf0cSdan         assert( pItem->isCorrelated==0 && nRef<=0 );
1186da79cf0cSdan         pItem->isCorrelated = (nRef!=0);
11877d10d5a6Sdrh       }
11887d10d5a6Sdrh     }
11897d10d5a6Sdrh 
119092689d28Sdrh     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
119192689d28Sdrh     ** resolve the result-set expression list.
119292689d28Sdrh     */
119392689d28Sdrh     sNC.ncFlags = NC_AllowAgg;
119492689d28Sdrh     sNC.pSrcList = p->pSrc;
119592689d28Sdrh     sNC.pNext = pOuterNC;
119692689d28Sdrh 
119792689d28Sdrh     /* Resolve names in the result set. */
119892689d28Sdrh     pEList = p->pEList;
119992689d28Sdrh     assert( pEList!=0 );
120092689d28Sdrh     for(i=0; i<pEList->nExpr; i++){
120192689d28Sdrh       Expr *pX = pEList->a[i].pExpr;
120292689d28Sdrh       if( sqlite3ResolveExprNames(&sNC, pX) ){
120392689d28Sdrh         return WRC_Abort;
120492689d28Sdrh       }
120592689d28Sdrh     }
120692689d28Sdrh 
12077d10d5a6Sdrh     /* If there are no aggregate functions in the result-set, and no GROUP BY
12087d10d5a6Sdrh     ** expression, do not allow aggregates in any of the other expressions.
12097d10d5a6Sdrh     */
12107d10d5a6Sdrh     assert( (p->selFlags & SF_Aggregate)==0 );
12117d10d5a6Sdrh     pGroupBy = p->pGroupBy;
1212a51009b2Sdrh     if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
12137d10d5a6Sdrh       p->selFlags |= SF_Aggregate;
12147d10d5a6Sdrh     }else{
1215a51009b2Sdrh       sNC.ncFlags &= ~NC_AllowAgg;
12167d10d5a6Sdrh     }
12177d10d5a6Sdrh 
12187d10d5a6Sdrh     /* If a HAVING clause is present, then there must be a GROUP BY clause.
12197d10d5a6Sdrh     */
12207d10d5a6Sdrh     if( p->pHaving && !pGroupBy ){
12217d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
12227d10d5a6Sdrh       return WRC_Abort;
12237d10d5a6Sdrh     }
12247d10d5a6Sdrh 
122526080d92Sdrh     /* Add the output column list to the name-context before parsing the
12267d10d5a6Sdrh     ** other expressions in the SELECT statement. This is so that
12277d10d5a6Sdrh     ** expressions in the WHERE clause (etc.) can refer to expressions by
12287d10d5a6Sdrh     ** aliases in the result set.
12297d10d5a6Sdrh     **
12307d10d5a6Sdrh     ** Minor point: If this is the case, then the expression will be
12317d10d5a6Sdrh     ** re-evaluated for each reference to it.
12327d10d5a6Sdrh     */
12337d10d5a6Sdrh     sNC.pEList = p->pEList;
123458a450c0Sdrh     if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
1235a3a5bd9bSdrh     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
12367d10d5a6Sdrh 
12377d10d5a6Sdrh     /* The ORDER BY and GROUP BY clauses may not refer to terms in
12387d10d5a6Sdrh     ** outer queries
12397d10d5a6Sdrh     */
12407d10d5a6Sdrh     sNC.pNext = 0;
1241a51009b2Sdrh     sNC.ncFlags |= NC_AllowAgg;
12427d10d5a6Sdrh 
12437d10d5a6Sdrh     /* Process the ORDER BY clause for singleton SELECT statements.
12447d10d5a6Sdrh     ** The ORDER BY clause for compounds SELECT statements is handled
12457d10d5a6Sdrh     ** below, after all of the result-sets for all of the elements of
12467d10d5a6Sdrh     ** the compound have been resolved.
12477d10d5a6Sdrh     */
12487d10d5a6Sdrh     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
12497d10d5a6Sdrh       return WRC_Abort;
12507d10d5a6Sdrh     }
12517d10d5a6Sdrh     if( db->mallocFailed ){
12527d10d5a6Sdrh       return WRC_Abort;
12537d10d5a6Sdrh     }
12547d10d5a6Sdrh 
12557d10d5a6Sdrh     /* Resolve the GROUP BY clause.  At the same time, make sure
12567d10d5a6Sdrh     ** the GROUP BY clause does not contain aggregate functions.
12577d10d5a6Sdrh     */
12587d10d5a6Sdrh     if( pGroupBy ){
12597d10d5a6Sdrh       struct ExprList_item *pItem;
12607d10d5a6Sdrh 
12617d10d5a6Sdrh       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
12627d10d5a6Sdrh         return WRC_Abort;
12637d10d5a6Sdrh       }
12647d10d5a6Sdrh       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
12657d10d5a6Sdrh         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
12667d10d5a6Sdrh           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
12677d10d5a6Sdrh               "the GROUP BY clause");
12687d10d5a6Sdrh           return WRC_Abort;
12697d10d5a6Sdrh         }
12707d10d5a6Sdrh       }
12717d10d5a6Sdrh     }
12727d10d5a6Sdrh 
12737d10d5a6Sdrh     /* Advance to the next term of the compound
12747d10d5a6Sdrh     */
12757d10d5a6Sdrh     p = p->pPrior;
12767d10d5a6Sdrh     nCompound++;
12777d10d5a6Sdrh   }
12787d10d5a6Sdrh 
12797d10d5a6Sdrh   /* Resolve the ORDER BY on a compound SELECT after all terms of
12807d10d5a6Sdrh   ** the compound have been resolved.
12817d10d5a6Sdrh   */
12827d10d5a6Sdrh   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
12837d10d5a6Sdrh     return WRC_Abort;
12847d10d5a6Sdrh   }
12857d10d5a6Sdrh 
12867d10d5a6Sdrh   return WRC_Prune;
12877d10d5a6Sdrh }
12887d10d5a6Sdrh 
12897d10d5a6Sdrh /*
12907d10d5a6Sdrh ** This routine walks an expression tree and resolves references to
12917d10d5a6Sdrh ** table columns and result-set columns.  At the same time, do error
12927d10d5a6Sdrh ** checking on function usage and set a flag if any aggregate functions
12937d10d5a6Sdrh ** are seen.
12947d10d5a6Sdrh **
12957d10d5a6Sdrh ** To resolve table columns references we look for nodes (or subtrees) of the
12967d10d5a6Sdrh ** form X.Y.Z or Y.Z or just Z where
12977d10d5a6Sdrh **
12987d10d5a6Sdrh **      X:   The name of a database.  Ex:  "main" or "temp" or
12997d10d5a6Sdrh **           the symbolic name assigned to an ATTACH-ed database.
13007d10d5a6Sdrh **
13017d10d5a6Sdrh **      Y:   The name of a table in a FROM clause.  Or in a trigger
13027d10d5a6Sdrh **           one of the special names "old" or "new".
13037d10d5a6Sdrh **
13047d10d5a6Sdrh **      Z:   The name of a column in table Y.
13057d10d5a6Sdrh **
13067d10d5a6Sdrh ** The node at the root of the subtree is modified as follows:
13077d10d5a6Sdrh **
13087d10d5a6Sdrh **    Expr.op        Changed to TK_COLUMN
13097d10d5a6Sdrh **    Expr.pTab      Points to the Table object for X.Y
13107d10d5a6Sdrh **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
13117d10d5a6Sdrh **    Expr.iTable    The VDBE cursor number for X.Y
13127d10d5a6Sdrh **
13137d10d5a6Sdrh **
13147d10d5a6Sdrh ** To resolve result-set references, look for expression nodes of the
13157d10d5a6Sdrh ** form Z (with no X and Y prefix) where the Z matches the right-hand
13167d10d5a6Sdrh ** size of an AS clause in the result-set of a SELECT.  The Z expression
13177d10d5a6Sdrh ** is replaced by a copy of the left-hand side of the result-set expression.
13187d10d5a6Sdrh ** Table-name and function resolution occurs on the substituted expression
13197d10d5a6Sdrh ** tree.  For example, in:
13207d10d5a6Sdrh **
13217d10d5a6Sdrh **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
13227d10d5a6Sdrh **
13237d10d5a6Sdrh ** The "x" term of the order by is replaced by "a+b" to render:
13247d10d5a6Sdrh **
13257d10d5a6Sdrh **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
13267d10d5a6Sdrh **
13277d10d5a6Sdrh ** Function calls are checked to make sure that the function is
13287d10d5a6Sdrh ** defined and that the correct number of arguments are specified.
1329a51009b2Sdrh ** If the function is an aggregate function, then the NC_HasAgg flag is
13307d10d5a6Sdrh ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
13317d10d5a6Sdrh ** If an expression contains aggregate functions then the EP_Agg
13327d10d5a6Sdrh ** property on the expression is set.
13337d10d5a6Sdrh **
13347d10d5a6Sdrh ** An error message is left in pParse if anything is amiss.  The number
13357d10d5a6Sdrh ** if errors is returned.
13367d10d5a6Sdrh */
13377d10d5a6Sdrh int sqlite3ResolveExprNames(
13387d10d5a6Sdrh   NameContext *pNC,       /* Namespace to resolve expressions in. */
13397d10d5a6Sdrh   Expr *pExpr             /* The expression to be analyzed. */
13407d10d5a6Sdrh ){
1341a51009b2Sdrh   u8 savedHasAgg;
13427d10d5a6Sdrh   Walker w;
13437d10d5a6Sdrh 
13447d10d5a6Sdrh   if( pExpr==0 ) return 0;
13457d10d5a6Sdrh #if SQLITE_MAX_EXPR_DEPTH>0
13467d10d5a6Sdrh   {
13477d10d5a6Sdrh     Parse *pParse = pNC->pParse;
13487d10d5a6Sdrh     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
13497d10d5a6Sdrh       return 1;
13507d10d5a6Sdrh     }
13517d10d5a6Sdrh     pParse->nHeight += pExpr->nHeight;
13527d10d5a6Sdrh   }
13537d10d5a6Sdrh #endif
1354a51009b2Sdrh   savedHasAgg = pNC->ncFlags & NC_HasAgg;
1355a51009b2Sdrh   pNC->ncFlags &= ~NC_HasAgg;
1356aa87f9a6Sdrh   memset(&w, 0, sizeof(w));
13577d10d5a6Sdrh   w.xExprCallback = resolveExprStep;
13587d10d5a6Sdrh   w.xSelectCallback = resolveSelectStep;
13597d10d5a6Sdrh   w.pParse = pNC->pParse;
13607d10d5a6Sdrh   w.u.pNC = pNC;
13617d10d5a6Sdrh   sqlite3WalkExpr(&w, pExpr);
13627d10d5a6Sdrh #if SQLITE_MAX_EXPR_DEPTH>0
13637d10d5a6Sdrh   pNC->pParse->nHeight -= pExpr->nHeight;
13647d10d5a6Sdrh #endif
1365fd773cf9Sdrh   if( pNC->nErr>0 || w.pParse->nErr>0 ){
13667d10d5a6Sdrh     ExprSetProperty(pExpr, EP_Error);
13677d10d5a6Sdrh   }
1368a51009b2Sdrh   if( pNC->ncFlags & NC_HasAgg ){
13697d10d5a6Sdrh     ExprSetProperty(pExpr, EP_Agg);
13707d10d5a6Sdrh   }else if( savedHasAgg ){
1371a51009b2Sdrh     pNC->ncFlags |= NC_HasAgg;
13727d10d5a6Sdrh   }
13737d10d5a6Sdrh   return ExprHasProperty(pExpr, EP_Error);
13747d10d5a6Sdrh }
13757d10d5a6Sdrh 
13767d10d5a6Sdrh 
13777d10d5a6Sdrh /*
13787d10d5a6Sdrh ** Resolve all names in all expressions of a SELECT and in all
13797d10d5a6Sdrh ** decendents of the SELECT, including compounds off of p->pPrior,
13807d10d5a6Sdrh ** subqueries in expressions, and subqueries used as FROM clause
13817d10d5a6Sdrh ** terms.
13827d10d5a6Sdrh **
13837d10d5a6Sdrh ** See sqlite3ResolveExprNames() for a description of the kinds of
13847d10d5a6Sdrh ** transformations that occur.
13857d10d5a6Sdrh **
13867d10d5a6Sdrh ** All SELECT statements should have been expanded using
13877d10d5a6Sdrh ** sqlite3SelectExpand() prior to invoking this routine.
13887d10d5a6Sdrh */
13897d10d5a6Sdrh void sqlite3ResolveSelectNames(
13907d10d5a6Sdrh   Parse *pParse,         /* The parser context */
13917d10d5a6Sdrh   Select *p,             /* The SELECT statement being coded. */
13927d10d5a6Sdrh   NameContext *pOuterNC  /* Name context for parent SELECT statement */
13937d10d5a6Sdrh ){
13947d10d5a6Sdrh   Walker w;
13957d10d5a6Sdrh 
13960a846f96Sdrh   assert( p!=0 );
1397aa87f9a6Sdrh   memset(&w, 0, sizeof(w));
13987d10d5a6Sdrh   w.xExprCallback = resolveExprStep;
13997d10d5a6Sdrh   w.xSelectCallback = resolveSelectStep;
14007d10d5a6Sdrh   w.pParse = pParse;
14017d10d5a6Sdrh   w.u.pNC = pOuterNC;
14027d10d5a6Sdrh   sqlite3WalkSelect(&w, p);
14037d10d5a6Sdrh }
14043780be11Sdrh 
14053780be11Sdrh /*
14063780be11Sdrh ** Resolve names in expressions that can only reference a single table:
14073780be11Sdrh **
14083780be11Sdrh **    *   CHECK constraints
14093780be11Sdrh **    *   WHERE clauses on partial indices
14103780be11Sdrh **
14113780be11Sdrh ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
14123780be11Sdrh ** is set to -1 and the Expr.iColumn value is set to the column number.
14133780be11Sdrh **
14143780be11Sdrh ** Any errors cause an error message to be set in pParse.
14153780be11Sdrh */
14163780be11Sdrh void sqlite3ResolveSelfReference(
14173780be11Sdrh   Parse *pParse,      /* Parsing context */
14183780be11Sdrh   Table *pTab,        /* The table being referenced */
14193780be11Sdrh   int type,           /* NC_IsCheck or NC_PartIdx */
14203780be11Sdrh   Expr *pExpr,        /* Expression to resolve.  May be NULL. */
14213780be11Sdrh   ExprList *pList     /* Expression list to resolve.  May be NUL. */
14223780be11Sdrh ){
14233780be11Sdrh   SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
14243780be11Sdrh   NameContext sNC;                /* Name context for pParse->pNewTable */
14253780be11Sdrh   int i;                          /* Loop counter */
14263780be11Sdrh 
14273780be11Sdrh   assert( type==NC_IsCheck || type==NC_PartIdx );
14283780be11Sdrh   memset(&sNC, 0, sizeof(sNC));
14293780be11Sdrh   memset(&sSrc, 0, sizeof(sSrc));
14303780be11Sdrh   sSrc.nSrc = 1;
14313780be11Sdrh   sSrc.a[0].zName = pTab->zName;
14323780be11Sdrh   sSrc.a[0].pTab = pTab;
14333780be11Sdrh   sSrc.a[0].iCursor = -1;
14343780be11Sdrh   sNC.pParse = pParse;
14353780be11Sdrh   sNC.pSrcList = &sSrc;
14363780be11Sdrh   sNC.ncFlags = type;
14373780be11Sdrh   if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
14383780be11Sdrh   if( pList ){
14393780be11Sdrh     for(i=0; i<pList->nExpr; i++){
14403780be11Sdrh       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
14413780be11Sdrh         return;
14423780be11Sdrh       }
14433780be11Sdrh     }
14443780be11Sdrh   }
14453780be11Sdrh }
1446