xref: /sqlite-3.40.0/src/resolve.c (revision d05ab6aa)
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){
31059b2d50Sdrh   if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
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;
39059b2d50Sdrh     w.u.n = 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;
3409d42cc99Smistachkin       }else{
3419d42cc99Smistachkin         pTab = 0;
3427d10d5a6Sdrh       }
3437d10d5a6Sdrh 
3447d10d5a6Sdrh       if( pTab ){
3457d10d5a6Sdrh         int iCol;
3467d10d5a6Sdrh         pSchema = pTab->pSchema;
3477d10d5a6Sdrh         cntTab++;
348511717c6Sdrh         for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
3497d10d5a6Sdrh           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
3502bd93516Sdan             if( iCol==pTab->iPKey ){
3512bd93516Sdan               iCol = -1;
3522bd93516Sdan             }
3537d10d5a6Sdrh             break;
3547d10d5a6Sdrh           }
3557d10d5a6Sdrh         }
35611f9b033Sdrh         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
357e8a537eeSdrh           /* IMP: R-51414-32910 */
358bbbb0e80Sdrh           /* IMP: R-44911-55124 */
359bbbb0e80Sdrh           iCol = -1;
3607d10d5a6Sdrh         }
3612bd93516Sdan         if( iCol<pTab->nCol ){
3622bd93516Sdan           cnt++;
3632bd93516Sdan           if( iCol<0 ){
3642bd93516Sdan             pExpr->affinity = SQLITE_AFF_INTEGER;
3652832ad42Sdan           }else if( pExpr->iTable==0 ){
3662832ad42Sdan             testcase( iCol==31 );
3672832ad42Sdan             testcase( iCol==32 );
3682832ad42Sdan             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
369bb5f168fSdan           }else{
370bb5f168fSdan             testcase( iCol==31 );
371bb5f168fSdan             testcase( iCol==32 );
372bb5f168fSdan             pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
3732bd93516Sdan           }
374cea72b2dSshane           pExpr->iColumn = (i16)iCol;
3752bd93516Sdan           pExpr->pTab = pTab;
3762bd93516Sdan           isTrigger = 1;
3772bd93516Sdan         }
3782bd93516Sdan       }
3797d10d5a6Sdrh     }
3807d10d5a6Sdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) */
3817d10d5a6Sdrh 
3827d10d5a6Sdrh     /*
3837d10d5a6Sdrh     ** Perhaps the name is a reference to the ROWID
3847d10d5a6Sdrh     */
385784156f8Sdrh     if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
386784156f8Sdrh      && HasRowid(pMatch->pTab) ){
3877d10d5a6Sdrh       cnt = 1;
388c79c761fSdrh       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
3897d10d5a6Sdrh       pExpr->affinity = SQLITE_AFF_INTEGER;
3907d10d5a6Sdrh     }
3917d10d5a6Sdrh 
3927d10d5a6Sdrh     /*
3937d10d5a6Sdrh     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
3947d10d5a6Sdrh     ** might refer to an result-set alias.  This happens, for example, when
3957d10d5a6Sdrh     ** we are resolving names in the WHERE clause of the following command:
3967d10d5a6Sdrh     **
3977d10d5a6Sdrh     **     SELECT a+b AS x FROM table WHERE x<10;
3987d10d5a6Sdrh     **
3997d10d5a6Sdrh     ** In cases like this, replace pExpr with a copy of the expression that
4007d10d5a6Sdrh     ** forms the result set entry ("a+b" in the example) and return immediately.
4017d10d5a6Sdrh     ** Note that the expression in the result set should have already been
4027d10d5a6Sdrh     ** resolved by the time the WHERE clause is resolved.
403e35463b3Sdrh     **
404e35463b3Sdrh     ** The ability to use an output result-set column in the WHERE, GROUP BY,
405e35463b3Sdrh     ** or HAVING clauses, or as part of a larger expression in the ORDRE BY
406e35463b3Sdrh     ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
407e35463b3Sdrh     ** is supported for backwards compatibility only.  TO DO: Issue a warning
408e35463b3Sdrh     ** on sqlite3_log() whenever the capability is used.
4097d10d5a6Sdrh     */
410a3a5bd9bSdrh     if( (pEList = pNC->pEList)!=0
411a3a5bd9bSdrh      && zTab==0
412e35463b3Sdrh      && cnt==0
413a3a5bd9bSdrh     ){
4147d10d5a6Sdrh       for(j=0; j<pEList->nExpr; j++){
4157d10d5a6Sdrh         char *zAs = pEList->a[j].zName;
4167d10d5a6Sdrh         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
4178b213899Sdrh           Expr *pOrig;
4187d10d5a6Sdrh           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
4196ab3a2ecSdanielk1977           assert( pExpr->x.pList==0 );
4206ab3a2ecSdanielk1977           assert( pExpr->x.pSelect==0 );
4217d10d5a6Sdrh           pOrig = pEList->a[j].pExpr;
422a51009b2Sdrh           if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
4237d10d5a6Sdrh             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
424f7828b5cSdrh             return WRC_Abort;
4257d10d5a6Sdrh           }
426ed551b95Sdrh           resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
4277d10d5a6Sdrh           cnt = 1;
4287d10d5a6Sdrh           pMatch = 0;
4297d10d5a6Sdrh           assert( zTab==0 && zDb==0 );
430b7916a78Sdrh           goto lookupname_end;
4317d10d5a6Sdrh         }
4327d10d5a6Sdrh       }
4337d10d5a6Sdrh     }
4347d10d5a6Sdrh 
4357d10d5a6Sdrh     /* Advance to the next name context.  The loop will exit when either
4367d10d5a6Sdrh     ** we have a match (cnt>0) or when we run out of name contexts.
4377d10d5a6Sdrh     */
4387d10d5a6Sdrh     if( cnt==0 ){
4397d10d5a6Sdrh       pNC = pNC->pNext;
440ed551b95Sdrh       nSubquery++;
4417d10d5a6Sdrh     }
4427d10d5a6Sdrh   }
4437d10d5a6Sdrh 
4447d10d5a6Sdrh   /*
4457d10d5a6Sdrh   ** If X and Y are NULL (in other words if only the column name Z is
4467d10d5a6Sdrh   ** supplied) and the value of Z is enclosed in double-quotes, then
4477d10d5a6Sdrh   ** Z is a string literal if it doesn't match any column names.  In that
4487d10d5a6Sdrh   ** case, we need to return right away and not make any changes to
4497d10d5a6Sdrh   ** pExpr.
4507d10d5a6Sdrh   **
4517d10d5a6Sdrh   ** Because no reference was made to outer contexts, the pNC->nRef
4527d10d5a6Sdrh   ** fields are not changed in any context.
4537d10d5a6Sdrh   */
45424fb627aSdrh   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
4557d10d5a6Sdrh     pExpr->op = TK_STRING;
4561885d1c2Sdrh     pExpr->pTab = 0;
457f7828b5cSdrh     return WRC_Prune;
4587d10d5a6Sdrh   }
4597d10d5a6Sdrh 
4607d10d5a6Sdrh   /*
4617d10d5a6Sdrh   ** cnt==0 means there was not match.  cnt>1 means there were two or
4627d10d5a6Sdrh   ** more matches.  Either way, we have an error.
4637d10d5a6Sdrh   */
4647d10d5a6Sdrh   if( cnt!=1 ){
4657d10d5a6Sdrh     const char *zErr;
4667d10d5a6Sdrh     zErr = cnt==0 ? "no such column" : "ambiguous column name";
4677d10d5a6Sdrh     if( zDb ){
4687d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
4697d10d5a6Sdrh     }else if( zTab ){
4707d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
4717d10d5a6Sdrh     }else{
4727d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
4737d10d5a6Sdrh     }
4741db95106Sdan     pParse->checkSchema = 1;
4757d10d5a6Sdrh     pTopNC->nErr++;
4767d10d5a6Sdrh   }
4777d10d5a6Sdrh 
4787d10d5a6Sdrh   /* If a column from a table in pSrcList is referenced, then record
4797d10d5a6Sdrh   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
4807d10d5a6Sdrh   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
4817d10d5a6Sdrh   ** column number is greater than the number of bits in the bitmask
4827d10d5a6Sdrh   ** then set the high-order bit of the bitmask.
4837d10d5a6Sdrh   */
4842d2e7bd3Sdanielk1977   if( pExpr->iColumn>=0 && pMatch!=0 ){
4857d10d5a6Sdrh     int n = pExpr->iColumn;
48600e13613Sdanielk1977     testcase( n==BMS-1 );
48700e13613Sdanielk1977     if( n>=BMS ){
48800e13613Sdanielk1977       n = BMS-1;
4897d10d5a6Sdrh     }
4907d10d5a6Sdrh     assert( pMatch->iCursor==pExpr->iTable );
4917d10d5a6Sdrh     pMatch->colUsed |= ((Bitmask)1)<<n;
4927d10d5a6Sdrh   }
4937d10d5a6Sdrh 
4947d10d5a6Sdrh   /* Clean up and return
4957d10d5a6Sdrh   */
4967d10d5a6Sdrh   sqlite3ExprDelete(db, pExpr->pLeft);
4977d10d5a6Sdrh   pExpr->pLeft = 0;
4987d10d5a6Sdrh   sqlite3ExprDelete(db, pExpr->pRight);
4997d10d5a6Sdrh   pExpr->pRight = 0;
5002bd93516Sdan   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
501b7916a78Sdrh lookupname_end:
5027d10d5a6Sdrh   if( cnt==1 ){
5037d10d5a6Sdrh     assert( pNC!=0 );
504a3a5bd9bSdrh     if( pExpr->op!=TK_AS ){
5057d10d5a6Sdrh       sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
506a3a5bd9bSdrh     }
5077d10d5a6Sdrh     /* Increment the nRef value on all name contexts from TopNC up to
5087d10d5a6Sdrh     ** the point where the name matched. */
5097d10d5a6Sdrh     for(;;){
5107d10d5a6Sdrh       assert( pTopNC!=0 );
5117d10d5a6Sdrh       pTopNC->nRef++;
5127d10d5a6Sdrh       if( pTopNC==pNC ) break;
5137d10d5a6Sdrh       pTopNC = pTopNC->pNext;
5147d10d5a6Sdrh     }
515f7828b5cSdrh     return WRC_Prune;
5167d10d5a6Sdrh   } else {
517f7828b5cSdrh     return WRC_Abort;
5187d10d5a6Sdrh   }
5197d10d5a6Sdrh }
5207d10d5a6Sdrh 
5217d10d5a6Sdrh /*
522f7b0b0adSdan ** Allocate and return a pointer to an expression to load the column iCol
5239e48165bSdrh ** from datasource iSrc in SrcList pSrc.
524f7b0b0adSdan */
525f7b0b0adSdan Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
526f7b0b0adSdan   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
527f7b0b0adSdan   if( p ){
528f7b0b0adSdan     struct SrcList_item *pItem = &pSrc->a[iSrc];
529f7b0b0adSdan     p->pTab = pItem->pTab;
530f7b0b0adSdan     p->iTable = pItem->iCursor;
531f7b0b0adSdan     if( p->pTab->iPKey==iCol ){
532f7b0b0adSdan       p->iColumn = -1;
533f7b0b0adSdan     }else{
5348677d308Sdrh       p->iColumn = (ynVar)iCol;
5357caba669Sdrh       testcase( iCol==BMS );
5367caba669Sdrh       testcase( iCol==BMS-1 );
537f7b0b0adSdan       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
538f7b0b0adSdan     }
539f7b0b0adSdan     ExprSetProperty(p, EP_Resolved);
540f7b0b0adSdan   }
541f7b0b0adSdan   return p;
542f7b0b0adSdan }
543f7b0b0adSdan 
544f7b0b0adSdan /*
5453780be11Sdrh ** Report an error that an expression is not valid for a partial index WHERE
5463780be11Sdrh ** clause.
5473780be11Sdrh */
5483780be11Sdrh static void notValidPartIdxWhere(
5493780be11Sdrh   Parse *pParse,       /* Leave error message here */
5503780be11Sdrh   NameContext *pNC,    /* The name context */
5513780be11Sdrh   const char *zMsg     /* Type of error */
5523780be11Sdrh ){
5533780be11Sdrh   if( (pNC->ncFlags & NC_PartIdx)!=0 ){
5543780be11Sdrh     sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
5553780be11Sdrh                     zMsg);
5563780be11Sdrh   }
5573780be11Sdrh }
5583780be11Sdrh 
5593780be11Sdrh #ifndef SQLITE_OMIT_CHECK
5603780be11Sdrh /*
5613780be11Sdrh ** Report an error that an expression is not valid for a CHECK constraint.
5623780be11Sdrh */
5633780be11Sdrh static void notValidCheckConstraint(
5643780be11Sdrh   Parse *pParse,       /* Leave error message here */
5653780be11Sdrh   NameContext *pNC,    /* The name context */
5663780be11Sdrh   const char *zMsg     /* Type of error */
5673780be11Sdrh ){
5683780be11Sdrh   if( (pNC->ncFlags & NC_IsCheck)!=0 ){
5693780be11Sdrh     sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
5703780be11Sdrh   }
5713780be11Sdrh }
5723780be11Sdrh #else
5733780be11Sdrh # define notValidCheckConstraint(P,N,M)
5743780be11Sdrh #endif
5753780be11Sdrh 
576cca9f3d2Sdrh /*
577cca9f3d2Sdrh ** Expression p should encode a floating point value between 1.0 and 0.0.
578cca9f3d2Sdrh ** Return 1024 times this value.  Or return -1 if p is not a floating point
579cca9f3d2Sdrh ** value between 1.0 and 0.0.
580cca9f3d2Sdrh */
581cca9f3d2Sdrh static int exprProbability(Expr *p){
582cca9f3d2Sdrh   double r = -1.0;
583cca9f3d2Sdrh   if( p->op!=TK_FLOAT ) return -1;
584cca9f3d2Sdrh   sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
58509328c00Sdrh   assert( r>=0.0 );
58609328c00Sdrh   if( r>1.0 ) return -1;
587*d05ab6aaSdrh   return (int)(r*134217728.0);
588cca9f3d2Sdrh }
5893780be11Sdrh 
5903780be11Sdrh /*
5917d10d5a6Sdrh ** This routine is callback for sqlite3WalkExpr().
5927d10d5a6Sdrh **
5937d10d5a6Sdrh ** Resolve symbolic names into TK_COLUMN operators for the current
5947d10d5a6Sdrh ** node in the expression tree.  Return 0 to continue the search down
5957d10d5a6Sdrh ** the tree or 2 to abort the tree walk.
5967d10d5a6Sdrh **
5977d10d5a6Sdrh ** This routine also does error checking and name resolution for
5987d10d5a6Sdrh ** function names.  The operator for aggregate functions is changed
5997d10d5a6Sdrh ** to TK_AGG_FUNCTION.
6007d10d5a6Sdrh */
6017d10d5a6Sdrh static int resolveExprStep(Walker *pWalker, Expr *pExpr){
6027d10d5a6Sdrh   NameContext *pNC;
6037d10d5a6Sdrh   Parse *pParse;
6047d10d5a6Sdrh 
6057d10d5a6Sdrh   pNC = pWalker->u.pNC;
6067d10d5a6Sdrh   assert( pNC!=0 );
6077d10d5a6Sdrh   pParse = pNC->pParse;
6087d10d5a6Sdrh   assert( pParse==pWalker->pParse );
6097d10d5a6Sdrh 
610c5cd1249Sdrh   if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
6117d10d5a6Sdrh   ExprSetProperty(pExpr, EP_Resolved);
6127d10d5a6Sdrh #ifndef NDEBUG
6137d10d5a6Sdrh   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
6147d10d5a6Sdrh     SrcList *pSrcList = pNC->pSrcList;
6157d10d5a6Sdrh     int i;
6167d10d5a6Sdrh     for(i=0; i<pNC->pSrcList->nSrc; i++){
6177d10d5a6Sdrh       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
6187d10d5a6Sdrh     }
6197d10d5a6Sdrh   }
6207d10d5a6Sdrh #endif
6217d10d5a6Sdrh   switch( pExpr->op ){
62241204f1fSdrh 
623273f619bSshane #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
62441204f1fSdrh     /* The special operator TK_ROW means use the rowid for the first
62541204f1fSdrh     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
62641204f1fSdrh     ** clause processing on UPDATE and DELETE statements.
62741204f1fSdrh     */
62841204f1fSdrh     case TK_ROW: {
62941204f1fSdrh       SrcList *pSrcList = pNC->pSrcList;
63041204f1fSdrh       struct SrcList_item *pItem;
63141204f1fSdrh       assert( pSrcList && pSrcList->nSrc==1 );
63241204f1fSdrh       pItem = pSrcList->a;
63341204f1fSdrh       pExpr->op = TK_COLUMN;
63441204f1fSdrh       pExpr->pTab = pItem->pTab;
63541204f1fSdrh       pExpr->iTable = pItem->iCursor;
63641204f1fSdrh       pExpr->iColumn = -1;
63741204f1fSdrh       pExpr->affinity = SQLITE_AFF_INTEGER;
63841204f1fSdrh       break;
63941204f1fSdrh     }
640273f619bSshane #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
64141204f1fSdrh 
6427d10d5a6Sdrh     /* A lone identifier is the name of a column.
6437d10d5a6Sdrh     */
6447d10d5a6Sdrh     case TK_ID: {
645f7828b5cSdrh       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
6467d10d5a6Sdrh     }
6477d10d5a6Sdrh 
6487d10d5a6Sdrh     /* A table name and column name:     ID.ID
6497d10d5a6Sdrh     ** Or a database, table and column:  ID.ID.ID
6507d10d5a6Sdrh     */
6517d10d5a6Sdrh     case TK_DOT: {
652b7916a78Sdrh       const char *zColumn;
653b7916a78Sdrh       const char *zTable;
654b7916a78Sdrh       const char *zDb;
6557d10d5a6Sdrh       Expr *pRight;
6567d10d5a6Sdrh 
6577d10d5a6Sdrh       /* if( pSrcList==0 ) break; */
6587d10d5a6Sdrh       pRight = pExpr->pRight;
6597d10d5a6Sdrh       if( pRight->op==TK_ID ){
660b7916a78Sdrh         zDb = 0;
66133e619fcSdrh         zTable = pExpr->pLeft->u.zToken;
66233e619fcSdrh         zColumn = pRight->u.zToken;
6637d10d5a6Sdrh       }else{
6647d10d5a6Sdrh         assert( pRight->op==TK_DOT );
66533e619fcSdrh         zDb = pExpr->pLeft->u.zToken;
66633e619fcSdrh         zTable = pRight->pLeft->u.zToken;
66733e619fcSdrh         zColumn = pRight->pRight->u.zToken;
6687d10d5a6Sdrh       }
669f7828b5cSdrh       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
6707d10d5a6Sdrh     }
6717d10d5a6Sdrh 
6727d10d5a6Sdrh     /* Resolve function names
6737d10d5a6Sdrh     */
6747d10d5a6Sdrh     case TK_FUNCTION: {
6756ab3a2ecSdanielk1977       ExprList *pList = pExpr->x.pList;    /* The argument list */
6767d10d5a6Sdrh       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
6777d10d5a6Sdrh       int no_such_func = 0;       /* True if no such function exists */
6787d10d5a6Sdrh       int wrong_num_args = 0;     /* True if wrong number of arguments */
6797d10d5a6Sdrh       int is_agg = 0;             /* True if is an aggregate function */
6807d10d5a6Sdrh       int auth;                   /* Authorization to use the function */
6817d10d5a6Sdrh       int nId;                    /* Number of characters in function name */
6827d10d5a6Sdrh       const char *zId;            /* The function name. */
6837d10d5a6Sdrh       FuncDef *pDef;              /* Information about the function */
684ea678832Sdrh       u8 enc = ENC(pParse->db);   /* The database encoding */
6857d10d5a6Sdrh 
6866ab3a2ecSdanielk1977       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
6873780be11Sdrh       notValidPartIdxWhere(pParse, pNC, "functions");
68833e619fcSdrh       zId = pExpr->u.zToken;
689b7916a78Sdrh       nId = sqlite3Strlen30(zId);
6907d10d5a6Sdrh       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
6917d10d5a6Sdrh       if( pDef==0 ){
69289d5d6a2Sdrh         pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
6937d10d5a6Sdrh         if( pDef==0 ){
6947d10d5a6Sdrh           no_such_func = 1;
6957d10d5a6Sdrh         }else{
6967d10d5a6Sdrh           wrong_num_args = 1;
6977d10d5a6Sdrh         }
6987d10d5a6Sdrh       }else{
6997d10d5a6Sdrh         is_agg = pDef->xFunc==0;
700cca9f3d2Sdrh         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
701a4c3c87eSdrh           ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
702cca9f3d2Sdrh           if( n==2 ){
703cca9f3d2Sdrh             pExpr->iTable = exprProbability(pList->a[1].pExpr);
704cca9f3d2Sdrh             if( pExpr->iTable<0 ){
705aae0f9e4Sdrh               sqlite3ErrorMsg(pParse, "second argument to likelihood() must be a "
706aae0f9e4Sdrh                                       "constant between 0.0 and 1.0");
707cca9f3d2Sdrh               pNC->nErr++;
708cca9f3d2Sdrh             }
709cca9f3d2Sdrh           }else{
7103432daa8Sdrh             /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
7113432daa8Sdrh             ** likelihood(X, 0.0625).
712b3265260Sdrh             ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
713ddb17caeSdrh             ** likelihood(X,0.0625).
714ddb17caeSdrh             ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand for
715ddb17caeSdrh             ** likelihood(X,0.9375).
716ddb17caeSdrh             ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent to
717ddb17caeSdrh             ** likelihood(X,0.9375). */
71803202a97Sdrh             /* TUNING: unlikely() probability is 0.0625.  likely() is 0.9375 */
719*d05ab6aaSdrh             pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
720cca9f3d2Sdrh           }
721cca9f3d2Sdrh         }
7227d10d5a6Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION
7237d10d5a6Sdrh         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
7247d10d5a6Sdrh         if( auth!=SQLITE_OK ){
7257d10d5a6Sdrh           if( auth==SQLITE_DENY ){
7267d10d5a6Sdrh             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
7277d10d5a6Sdrh                                     pDef->zName);
7287d10d5a6Sdrh             pNC->nErr++;
7297d10d5a6Sdrh           }
7307d10d5a6Sdrh           pExpr->op = TK_NULL;
7317d10d5a6Sdrh           return WRC_Prune;
7327d10d5a6Sdrh         }
7339588ad95Sdrh #endif
734b1fba286Sdrh         if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Constant);
7357d10d5a6Sdrh       }
736a51009b2Sdrh       if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
7377d10d5a6Sdrh         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
7387d10d5a6Sdrh         pNC->nErr++;
7397d10d5a6Sdrh         is_agg = 0;
740ddd1fc72Sdrh       }else if( no_such_func && pParse->db->init.busy==0 ){
7417d10d5a6Sdrh         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
7427d10d5a6Sdrh         pNC->nErr++;
7437d10d5a6Sdrh       }else if( wrong_num_args ){
7447d10d5a6Sdrh         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
7457d10d5a6Sdrh              nId, zId);
7467d10d5a6Sdrh         pNC->nErr++;
7477d10d5a6Sdrh       }
748a51009b2Sdrh       if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
7497d10d5a6Sdrh       sqlite3WalkExprList(pWalker, pList);
750030796dfSdrh       if( is_agg ){
751030796dfSdrh         NameContext *pNC2 = pNC;
752030796dfSdrh         pExpr->op = TK_AGG_FUNCTION;
753030796dfSdrh         pExpr->op2 = 0;
754030796dfSdrh         while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
755030796dfSdrh           pExpr->op2++;
756030796dfSdrh           pNC2 = pNC2->pNext;
757030796dfSdrh         }
7589588ad95Sdrh         assert( pDef!=0 );
7599588ad95Sdrh         if( pNC2 ){
7609588ad95Sdrh           assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
7619588ad95Sdrh           testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
7629588ad95Sdrh           pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
7639588ad95Sdrh 
7649588ad95Sdrh         }
765030796dfSdrh         pNC->ncFlags |= NC_AllowAgg;
766030796dfSdrh       }
7677d10d5a6Sdrh       /* FIX ME:  Compute pExpr->affinity based on the expected return
7687d10d5a6Sdrh       ** type of the function
7697d10d5a6Sdrh       */
7707d10d5a6Sdrh       return WRC_Prune;
7717d10d5a6Sdrh     }
7727d10d5a6Sdrh #ifndef SQLITE_OMIT_SUBQUERY
7737d10d5a6Sdrh     case TK_SELECT:
77473c0fdc7Sdrh     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
7757d10d5a6Sdrh #endif
7767d10d5a6Sdrh     case TK_IN: {
77773c0fdc7Sdrh       testcase( pExpr->op==TK_IN );
7786ab3a2ecSdanielk1977       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
7797d10d5a6Sdrh         int nRef = pNC->nRef;
7803780be11Sdrh         notValidCheckConstraint(pParse, pNC, "subqueries");
7813780be11Sdrh         notValidPartIdxWhere(pParse, pNC, "subqueries");
7826ab3a2ecSdanielk1977         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
7837d10d5a6Sdrh         assert( pNC->nRef>=nRef );
7847d10d5a6Sdrh         if( nRef!=pNC->nRef ){
7857d10d5a6Sdrh           ExprSetProperty(pExpr, EP_VarSelect);
7867d10d5a6Sdrh         }
7877d10d5a6Sdrh       }
7887d10d5a6Sdrh       break;
7897d10d5a6Sdrh     }
7907d10d5a6Sdrh     case TK_VARIABLE: {
7913780be11Sdrh       notValidCheckConstraint(pParse, pNC, "parameters");
7923780be11Sdrh       notValidPartIdxWhere(pParse, pNC, "parameters");
7937d10d5a6Sdrh       break;
7947d10d5a6Sdrh     }
7957d10d5a6Sdrh   }
7967d10d5a6Sdrh   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
7977d10d5a6Sdrh }
7987d10d5a6Sdrh 
7997d10d5a6Sdrh /*
8007d10d5a6Sdrh ** pEList is a list of expressions which are really the result set of the
8017d10d5a6Sdrh ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
8027d10d5a6Sdrh ** This routine checks to see if pE is a simple identifier which corresponds
8037d10d5a6Sdrh ** to the AS-name of one of the terms of the expression list.  If it is,
8047d10d5a6Sdrh ** this routine return an integer between 1 and N where N is the number of
8057d10d5a6Sdrh ** elements in pEList, corresponding to the matching entry.  If there is
8067d10d5a6Sdrh ** no match, or if pE is not a simple identifier, then this routine
8077d10d5a6Sdrh ** return 0.
8087d10d5a6Sdrh **
8097d10d5a6Sdrh ** pEList has been resolved.  pE has not.
8107d10d5a6Sdrh */
8117d10d5a6Sdrh static int resolveAsName(
8127d10d5a6Sdrh   Parse *pParse,     /* Parsing context for error messages */
8137d10d5a6Sdrh   ExprList *pEList,  /* List of expressions to scan */
8147d10d5a6Sdrh   Expr *pE           /* Expression we are trying to match */
8157d10d5a6Sdrh ){
8167d10d5a6Sdrh   int i;             /* Loop counter */
8177d10d5a6Sdrh 
818cf697396Sshane   UNUSED_PARAMETER(pParse);
819cf697396Sshane 
82073c0fdc7Sdrh   if( pE->op==TK_ID ){
82133e619fcSdrh     char *zCol = pE->u.zToken;
8227d10d5a6Sdrh     for(i=0; i<pEList->nExpr; i++){
8237d10d5a6Sdrh       char *zAs = pEList->a[i].zName;
8247d10d5a6Sdrh       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
8257d10d5a6Sdrh         return i+1;
8267d10d5a6Sdrh       }
8277d10d5a6Sdrh     }
8287d10d5a6Sdrh   }
8297d10d5a6Sdrh   return 0;
8307d10d5a6Sdrh }
8317d10d5a6Sdrh 
8327d10d5a6Sdrh /*
8337d10d5a6Sdrh ** pE is a pointer to an expression which is a single term in the
8347d10d5a6Sdrh ** ORDER BY of a compound SELECT.  The expression has not been
8357d10d5a6Sdrh ** name resolved.
8367d10d5a6Sdrh **
8377d10d5a6Sdrh ** At the point this routine is called, we already know that the
8387d10d5a6Sdrh ** ORDER BY term is not an integer index into the result set.  That
8397d10d5a6Sdrh ** case is handled by the calling routine.
8407d10d5a6Sdrh **
8417d10d5a6Sdrh ** Attempt to match pE against result set columns in the left-most
8427d10d5a6Sdrh ** SELECT statement.  Return the index i of the matching column,
8437d10d5a6Sdrh ** as an indication to the caller that it should sort by the i-th column.
8447d10d5a6Sdrh ** The left-most column is 1.  In other words, the value returned is the
8457d10d5a6Sdrh ** same integer value that would be used in the SQL statement to indicate
8467d10d5a6Sdrh ** the column.
8477d10d5a6Sdrh **
8487d10d5a6Sdrh ** If there is no match, return 0.  Return -1 if an error occurs.
8497d10d5a6Sdrh */
8507d10d5a6Sdrh static int resolveOrderByTermToExprList(
8517d10d5a6Sdrh   Parse *pParse,     /* Parsing context for error messages */
8527d10d5a6Sdrh   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
8537d10d5a6Sdrh   Expr *pE           /* The specific ORDER BY term */
8547d10d5a6Sdrh ){
8557d10d5a6Sdrh   int i;             /* Loop counter */
8567d10d5a6Sdrh   ExprList *pEList;  /* The columns of the result set */
8577d10d5a6Sdrh   NameContext nc;    /* Name context for resolving pE */
858a7564663Sdrh   sqlite3 *db;       /* Database connection */
859a7564663Sdrh   int rc;            /* Return code from subprocedures */
860a7564663Sdrh   u8 savedSuppErr;   /* Saved value of db->suppressErr */
8617d10d5a6Sdrh 
8627d10d5a6Sdrh   assert( sqlite3ExprIsInteger(pE, &i)==0 );
8637d10d5a6Sdrh   pEList = pSelect->pEList;
8647d10d5a6Sdrh 
8657d10d5a6Sdrh   /* Resolve all names in the ORDER BY term expression
8667d10d5a6Sdrh   */
8677d10d5a6Sdrh   memset(&nc, 0, sizeof(nc));
8687d10d5a6Sdrh   nc.pParse = pParse;
8697d10d5a6Sdrh   nc.pSrcList = pSelect->pSrc;
8707d10d5a6Sdrh   nc.pEList = pEList;
871a51009b2Sdrh   nc.ncFlags = NC_AllowAgg;
8727d10d5a6Sdrh   nc.nErr = 0;
873a7564663Sdrh   db = pParse->db;
874a7564663Sdrh   savedSuppErr = db->suppressErr;
875a7564663Sdrh   db->suppressErr = 1;
876a7564663Sdrh   rc = sqlite3ResolveExprNames(&nc, pE);
877a7564663Sdrh   db->suppressErr = savedSuppErr;
878a7564663Sdrh   if( rc ) return 0;
8797d10d5a6Sdrh 
8807d10d5a6Sdrh   /* Try to match the ORDER BY expression against an expression
8817d10d5a6Sdrh   ** in the result set.  Return an 1-based index of the matching
8827d10d5a6Sdrh   ** result-set entry.
8837d10d5a6Sdrh   */
8847d10d5a6Sdrh   for(i=0; i<pEList->nExpr; i++){
885619a1305Sdrh     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
8867d10d5a6Sdrh       return i+1;
8877d10d5a6Sdrh     }
8887d10d5a6Sdrh   }
8897d10d5a6Sdrh 
8907d10d5a6Sdrh   /* If no match, return 0. */
8917d10d5a6Sdrh   return 0;
8927d10d5a6Sdrh }
8937d10d5a6Sdrh 
8947d10d5a6Sdrh /*
8957d10d5a6Sdrh ** Generate an ORDER BY or GROUP BY term out-of-range error.
8967d10d5a6Sdrh */
8977d10d5a6Sdrh static void resolveOutOfRangeError(
8987d10d5a6Sdrh   Parse *pParse,         /* The error context into which to write the error */
8997d10d5a6Sdrh   const char *zType,     /* "ORDER" or "GROUP" */
9007d10d5a6Sdrh   int i,                 /* The index (1-based) of the term out of range */
9017d10d5a6Sdrh   int mx                 /* Largest permissible value of i */
9027d10d5a6Sdrh ){
9037d10d5a6Sdrh   sqlite3ErrorMsg(pParse,
9047d10d5a6Sdrh     "%r %s BY term out of range - should be "
9057d10d5a6Sdrh     "between 1 and %d", i, zType, mx);
9067d10d5a6Sdrh }
9077d10d5a6Sdrh 
9087d10d5a6Sdrh /*
9097d10d5a6Sdrh ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
9107d10d5a6Sdrh ** each term of the ORDER BY clause is a constant integer between 1
9117d10d5a6Sdrh ** and N where N is the number of columns in the compound SELECT.
9127d10d5a6Sdrh **
9137d10d5a6Sdrh ** ORDER BY terms that are already an integer between 1 and N are
9147d10d5a6Sdrh ** unmodified.  ORDER BY terms that are integers outside the range of
9157d10d5a6Sdrh ** 1 through N generate an error.  ORDER BY terms that are expressions
9167d10d5a6Sdrh ** are matched against result set expressions of compound SELECT
9177d10d5a6Sdrh ** beginning with the left-most SELECT and working toward the right.
9187d10d5a6Sdrh ** At the first match, the ORDER BY expression is transformed into
9197d10d5a6Sdrh ** the integer column number.
9207d10d5a6Sdrh **
9217d10d5a6Sdrh ** Return the number of errors seen.
9227d10d5a6Sdrh */
9237d10d5a6Sdrh static int resolveCompoundOrderBy(
9247d10d5a6Sdrh   Parse *pParse,        /* Parsing context.  Leave error messages here */
9257d10d5a6Sdrh   Select *pSelect       /* The SELECT statement containing the ORDER BY */
9267d10d5a6Sdrh ){
9277d10d5a6Sdrh   int i;
9287d10d5a6Sdrh   ExprList *pOrderBy;
9297d10d5a6Sdrh   ExprList *pEList;
9307d10d5a6Sdrh   sqlite3 *db;
9317d10d5a6Sdrh   int moreToDo = 1;
9327d10d5a6Sdrh 
9337d10d5a6Sdrh   pOrderBy = pSelect->pOrderBy;
9347d10d5a6Sdrh   if( pOrderBy==0 ) return 0;
9357d10d5a6Sdrh   db = pParse->db;
9367d10d5a6Sdrh #if SQLITE_MAX_COLUMN
9377d10d5a6Sdrh   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
9387d10d5a6Sdrh     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
9397d10d5a6Sdrh     return 1;
9407d10d5a6Sdrh   }
9417d10d5a6Sdrh #endif
9427d10d5a6Sdrh   for(i=0; i<pOrderBy->nExpr; i++){
9437d10d5a6Sdrh     pOrderBy->a[i].done = 0;
9447d10d5a6Sdrh   }
9457d10d5a6Sdrh   pSelect->pNext = 0;
9467d10d5a6Sdrh   while( pSelect->pPrior ){
9477d10d5a6Sdrh     pSelect->pPrior->pNext = pSelect;
9487d10d5a6Sdrh     pSelect = pSelect->pPrior;
9497d10d5a6Sdrh   }
9507d10d5a6Sdrh   while( pSelect && moreToDo ){
9517d10d5a6Sdrh     struct ExprList_item *pItem;
9527d10d5a6Sdrh     moreToDo = 0;
9537d10d5a6Sdrh     pEList = pSelect->pEList;
9540a846f96Sdrh     assert( pEList!=0 );
9557d10d5a6Sdrh     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
9567d10d5a6Sdrh       int iCol = -1;
9577d10d5a6Sdrh       Expr *pE, *pDup;
9587d10d5a6Sdrh       if( pItem->done ) continue;
959bd13d34bSdrh       pE = sqlite3ExprSkipCollate(pItem->pExpr);
9607d10d5a6Sdrh       if( sqlite3ExprIsInteger(pE, &iCol) ){
96173c0fdc7Sdrh         if( iCol<=0 || iCol>pEList->nExpr ){
9627d10d5a6Sdrh           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
9637d10d5a6Sdrh           return 1;
9647d10d5a6Sdrh         }
9657d10d5a6Sdrh       }else{
9667d10d5a6Sdrh         iCol = resolveAsName(pParse, pEList, pE);
9677d10d5a6Sdrh         if( iCol==0 ){
9686ab3a2ecSdanielk1977           pDup = sqlite3ExprDup(db, pE, 0);
9697d10d5a6Sdrh           if( !db->mallocFailed ){
9707d10d5a6Sdrh             assert(pDup);
9717d10d5a6Sdrh             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
9727d10d5a6Sdrh           }
9737d10d5a6Sdrh           sqlite3ExprDelete(db, pDup);
9747d10d5a6Sdrh         }
9757d10d5a6Sdrh       }
9767d10d5a6Sdrh       if( iCol>0 ){
977bd13d34bSdrh         /* Convert the ORDER BY term into an integer column number iCol,
978bd13d34bSdrh         ** taking care to preserve the COLLATE clause if it exists */
979bd13d34bSdrh         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
980bd13d34bSdrh         if( pNew==0 ) return 1;
981bd13d34bSdrh         pNew->flags |= EP_IntValue;
982bd13d34bSdrh         pNew->u.iValue = iCol;
983bd13d34bSdrh         if( pItem->pExpr==pE ){
984bd13d34bSdrh           pItem->pExpr = pNew;
985bd13d34bSdrh         }else{
986bd13d34bSdrh           assert( pItem->pExpr->op==TK_COLLATE );
987bd13d34bSdrh           assert( pItem->pExpr->pLeft==pE );
988bd13d34bSdrh           pItem->pExpr->pLeft = pNew;
989bd13d34bSdrh         }
9907d10d5a6Sdrh         sqlite3ExprDelete(db, pE);
991c2acc4e4Sdrh         pItem->u.x.iOrderByCol = (u16)iCol;
9927d10d5a6Sdrh         pItem->done = 1;
9937d10d5a6Sdrh       }else{
9947d10d5a6Sdrh         moreToDo = 1;
9957d10d5a6Sdrh       }
9967d10d5a6Sdrh     }
9977d10d5a6Sdrh     pSelect = pSelect->pNext;
9987d10d5a6Sdrh   }
9997d10d5a6Sdrh   for(i=0; i<pOrderBy->nExpr; i++){
10007d10d5a6Sdrh     if( pOrderBy->a[i].done==0 ){
10017d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
10027d10d5a6Sdrh             "column in the result set", i+1);
10037d10d5a6Sdrh       return 1;
10047d10d5a6Sdrh     }
10057d10d5a6Sdrh   }
10067d10d5a6Sdrh   return 0;
10077d10d5a6Sdrh }
10087d10d5a6Sdrh 
10097d10d5a6Sdrh /*
10107d10d5a6Sdrh ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
10117d10d5a6Sdrh ** the SELECT statement pSelect.  If any term is reference to a
1012c2acc4e4Sdrh ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
1013c2acc4e4Sdrh ** field) then convert that term into a copy of the corresponding result set
10147d10d5a6Sdrh ** column.
10157d10d5a6Sdrh **
10167d10d5a6Sdrh ** If any errors are detected, add an error message to pParse and
10177d10d5a6Sdrh ** return non-zero.  Return zero if no errors are seen.
10187d10d5a6Sdrh */
10197d10d5a6Sdrh int sqlite3ResolveOrderGroupBy(
10207d10d5a6Sdrh   Parse *pParse,        /* Parsing context.  Leave error messages here */
10217d10d5a6Sdrh   Select *pSelect,      /* The SELECT statement containing the clause */
10227d10d5a6Sdrh   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
10237d10d5a6Sdrh   const char *zType     /* "ORDER" or "GROUP" */
10247d10d5a6Sdrh ){
10257d10d5a6Sdrh   int i;
10267d10d5a6Sdrh   sqlite3 *db = pParse->db;
10277d10d5a6Sdrh   ExprList *pEList;
10287d10d5a6Sdrh   struct ExprList_item *pItem;
10297d10d5a6Sdrh 
10307d10d5a6Sdrh   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
10317d10d5a6Sdrh #if SQLITE_MAX_COLUMN
10327d10d5a6Sdrh   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
10337d10d5a6Sdrh     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
10347d10d5a6Sdrh     return 1;
10357d10d5a6Sdrh   }
10367d10d5a6Sdrh #endif
10377d10d5a6Sdrh   pEList = pSelect->pEList;
10380a846f96Sdrh   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
10397d10d5a6Sdrh   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
1040c2acc4e4Sdrh     if( pItem->u.x.iOrderByCol ){
1041c2acc4e4Sdrh       if( pItem->u.x.iOrderByCol>pEList->nExpr ){
10427d10d5a6Sdrh         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
10437d10d5a6Sdrh         return 1;
10447d10d5a6Sdrh       }
1045c2acc4e4Sdrh       resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
10467d10d5a6Sdrh     }
10477d10d5a6Sdrh   }
10487d10d5a6Sdrh   return 0;
10497d10d5a6Sdrh }
10507d10d5a6Sdrh 
10517d10d5a6Sdrh /*
10527d10d5a6Sdrh ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
10537d10d5a6Sdrh ** The Name context of the SELECT statement is pNC.  zType is either
10547d10d5a6Sdrh ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
10557d10d5a6Sdrh **
10567d10d5a6Sdrh ** This routine resolves each term of the clause into an expression.
10577d10d5a6Sdrh ** If the order-by term is an integer I between 1 and N (where N is the
10587d10d5a6Sdrh ** number of columns in the result set of the SELECT) then the expression
10597d10d5a6Sdrh ** in the resolution is a copy of the I-th result-set expression.  If
106026080d92Sdrh ** the order-by term is an identifier that corresponds to the AS-name of
10617d10d5a6Sdrh ** a result-set expression, then the term resolves to a copy of the
10627d10d5a6Sdrh ** result-set expression.  Otherwise, the expression is resolved in
10637d10d5a6Sdrh ** the usual way - using sqlite3ResolveExprNames().
10647d10d5a6Sdrh **
10657d10d5a6Sdrh ** This routine returns the number of errors.  If errors occur, then
10667d10d5a6Sdrh ** an appropriate error message might be left in pParse.  (OOM errors
10677d10d5a6Sdrh ** excepted.)
10687d10d5a6Sdrh */
10697d10d5a6Sdrh static int resolveOrderGroupBy(
10707d10d5a6Sdrh   NameContext *pNC,     /* The name context of the SELECT statement */
10717d10d5a6Sdrh   Select *pSelect,      /* The SELECT statement holding pOrderBy */
10727d10d5a6Sdrh   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
10737d10d5a6Sdrh   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
10747d10d5a6Sdrh ){
107570331cd7Sdrh   int i, j;                      /* Loop counters */
10767d10d5a6Sdrh   int iCol;                      /* Column number */
10777d10d5a6Sdrh   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
10787d10d5a6Sdrh   Parse *pParse;                 /* Parsing context */
10797d10d5a6Sdrh   int nResult;                   /* Number of terms in the result set */
10807d10d5a6Sdrh 
10817d10d5a6Sdrh   if( pOrderBy==0 ) return 0;
10827d10d5a6Sdrh   nResult = pSelect->pEList->nExpr;
10837d10d5a6Sdrh   pParse = pNC->pParse;
10847d10d5a6Sdrh   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
10857d10d5a6Sdrh     Expr *pE = pItem->pExpr;
1086e35463b3Sdrh     Expr *pE2 = sqlite3ExprSkipCollate(pE);
10870af16ab2Sdrh     if( zType[0]!='G' ){
1088e35463b3Sdrh       iCol = resolveAsName(pParse, pSelect->pEList, pE2);
10897d10d5a6Sdrh       if( iCol>0 ){
10907d10d5a6Sdrh         /* If an AS-name match is found, mark this ORDER BY column as being
10917d10d5a6Sdrh         ** a copy of the iCol-th result-set column.  The subsequent call to
10927d10d5a6Sdrh         ** sqlite3ResolveOrderGroupBy() will convert the expression to a
10937d10d5a6Sdrh         ** copy of the iCol-th result-set expression. */
1094c2acc4e4Sdrh         pItem->u.x.iOrderByCol = (u16)iCol;
10957d10d5a6Sdrh         continue;
10967d10d5a6Sdrh       }
10970af16ab2Sdrh     }
1098e35463b3Sdrh     if( sqlite3ExprIsInteger(pE2, &iCol) ){
10997d10d5a6Sdrh       /* The ORDER BY term is an integer constant.  Again, set the column
11007d10d5a6Sdrh       ** number so that sqlite3ResolveOrderGroupBy() will convert the
11017d10d5a6Sdrh       ** order-by term to a copy of the result-set expression */
110285d641f9Sdrh       if( iCol<1 || iCol>0xffff ){
11037d10d5a6Sdrh         resolveOutOfRangeError(pParse, zType, i+1, nResult);
11047d10d5a6Sdrh         return 1;
11057d10d5a6Sdrh       }
1106c2acc4e4Sdrh       pItem->u.x.iOrderByCol = (u16)iCol;
11077d10d5a6Sdrh       continue;
11087d10d5a6Sdrh     }
11097d10d5a6Sdrh 
11107d10d5a6Sdrh     /* Otherwise, treat the ORDER BY term as an ordinary expression */
1111c2acc4e4Sdrh     pItem->u.x.iOrderByCol = 0;
11127d10d5a6Sdrh     if( sqlite3ResolveExprNames(pNC, pE) ){
11137d10d5a6Sdrh       return 1;
11147d10d5a6Sdrh     }
111570331cd7Sdrh     for(j=0; j<pSelect->pEList->nExpr; j++){
1116619a1305Sdrh       if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
1117c2acc4e4Sdrh         pItem->u.x.iOrderByCol = j+1;
111870331cd7Sdrh       }
111970331cd7Sdrh     }
11207d10d5a6Sdrh   }
11217d10d5a6Sdrh   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
11227d10d5a6Sdrh }
11237d10d5a6Sdrh 
11247d10d5a6Sdrh /*
112560ec914cSpeter.d.reid ** Resolve names in the SELECT statement p and all of its descendants.
11267d10d5a6Sdrh */
11277d10d5a6Sdrh static int resolveSelectStep(Walker *pWalker, Select *p){
11287d10d5a6Sdrh   NameContext *pOuterNC;  /* Context that contains this SELECT */
11297d10d5a6Sdrh   NameContext sNC;        /* Name context of this SELECT */
11307d10d5a6Sdrh   int isCompound;         /* True if p is a compound select */
11317d10d5a6Sdrh   int nCompound;          /* Number of compound terms processed so far */
11327d10d5a6Sdrh   Parse *pParse;          /* Parsing context */
11337d10d5a6Sdrh   ExprList *pEList;       /* Result set expression list */
11347d10d5a6Sdrh   int i;                  /* Loop counter */
11357d10d5a6Sdrh   ExprList *pGroupBy;     /* The GROUP BY clause */
11367d10d5a6Sdrh   Select *pLeftmost;      /* Left-most of SELECT of a compound */
11377d10d5a6Sdrh   sqlite3 *db;            /* Database connection */
11387d10d5a6Sdrh 
11397d10d5a6Sdrh 
11400a846f96Sdrh   assert( p!=0 );
11417d10d5a6Sdrh   if( p->selFlags & SF_Resolved ){
11427d10d5a6Sdrh     return WRC_Prune;
11437d10d5a6Sdrh   }
11447d10d5a6Sdrh   pOuterNC = pWalker->u.pNC;
11457d10d5a6Sdrh   pParse = pWalker->pParse;
11467d10d5a6Sdrh   db = pParse->db;
11477d10d5a6Sdrh 
11487d10d5a6Sdrh   /* Normally sqlite3SelectExpand() will be called first and will have
11497d10d5a6Sdrh   ** already expanded this SELECT.  However, if this is a subquery within
11507d10d5a6Sdrh   ** an expression, sqlite3ResolveExprNames() will be called without a
11517d10d5a6Sdrh   ** prior call to sqlite3SelectExpand().  When that happens, let
11527d10d5a6Sdrh   ** sqlite3SelectPrep() do all of the processing for this SELECT.
11537d10d5a6Sdrh   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
11547d10d5a6Sdrh   ** this routine in the correct order.
11557d10d5a6Sdrh   */
11567d10d5a6Sdrh   if( (p->selFlags & SF_Expanded)==0 ){
11577d10d5a6Sdrh     sqlite3SelectPrep(pParse, p, pOuterNC);
11587d10d5a6Sdrh     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
11597d10d5a6Sdrh   }
11607d10d5a6Sdrh 
11617d10d5a6Sdrh   isCompound = p->pPrior!=0;
11627d10d5a6Sdrh   nCompound = 0;
11637d10d5a6Sdrh   pLeftmost = p;
11647d10d5a6Sdrh   while( p ){
11657d10d5a6Sdrh     assert( (p->selFlags & SF_Expanded)!=0 );
11667d10d5a6Sdrh     assert( (p->selFlags & SF_Resolved)==0 );
11677d10d5a6Sdrh     p->selFlags |= SF_Resolved;
11687d10d5a6Sdrh 
11697d10d5a6Sdrh     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
11707d10d5a6Sdrh     ** are not allowed to refer to any names, so pass an empty NameContext.
11717d10d5a6Sdrh     */
11727d10d5a6Sdrh     memset(&sNC, 0, sizeof(sNC));
11737d10d5a6Sdrh     sNC.pParse = pParse;
11747d10d5a6Sdrh     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
11757d10d5a6Sdrh         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
11767d10d5a6Sdrh       return WRC_Abort;
11777d10d5a6Sdrh     }
11787d10d5a6Sdrh 
11797d10d5a6Sdrh     /* Recursively resolve names in all subqueries
11807d10d5a6Sdrh     */
11817d10d5a6Sdrh     for(i=0; i<p->pSrc->nSrc; i++){
11827d10d5a6Sdrh       struct SrcList_item *pItem = &p->pSrc->a[i];
11837d10d5a6Sdrh       if( pItem->pSelect ){
1184da79cf0cSdan         NameContext *pNC;         /* Used to iterate name contexts */
1185da79cf0cSdan         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
11867d10d5a6Sdrh         const char *zSavedContext = pParse->zAuthContext;
1187da79cf0cSdan 
1188da79cf0cSdan         /* Count the total number of references to pOuterNC and all of its
1189da79cf0cSdan         ** parent contexts. After resolving references to expressions in
1190da79cf0cSdan         ** pItem->pSelect, check if this value has changed. If so, then
1191da79cf0cSdan         ** SELECT statement pItem->pSelect must be correlated. Set the
1192da79cf0cSdan         ** pItem->isCorrelated flag if this is the case. */
1193da79cf0cSdan         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
1194da79cf0cSdan 
11957d10d5a6Sdrh         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
1196cd2b5613Sdrh         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
11977d10d5a6Sdrh         pParse->zAuthContext = zSavedContext;
11987d10d5a6Sdrh         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
1199da79cf0cSdan 
1200da79cf0cSdan         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
1201da79cf0cSdan         assert( pItem->isCorrelated==0 && nRef<=0 );
1202da79cf0cSdan         pItem->isCorrelated = (nRef!=0);
12037d10d5a6Sdrh       }
12047d10d5a6Sdrh     }
12057d10d5a6Sdrh 
120692689d28Sdrh     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
120792689d28Sdrh     ** resolve the result-set expression list.
120892689d28Sdrh     */
120992689d28Sdrh     sNC.ncFlags = NC_AllowAgg;
121092689d28Sdrh     sNC.pSrcList = p->pSrc;
121192689d28Sdrh     sNC.pNext = pOuterNC;
121292689d28Sdrh 
121392689d28Sdrh     /* Resolve names in the result set. */
121492689d28Sdrh     pEList = p->pEList;
121592689d28Sdrh     assert( pEList!=0 );
121692689d28Sdrh     for(i=0; i<pEList->nExpr; i++){
121792689d28Sdrh       Expr *pX = pEList->a[i].pExpr;
121892689d28Sdrh       if( sqlite3ResolveExprNames(&sNC, pX) ){
121992689d28Sdrh         return WRC_Abort;
122092689d28Sdrh       }
122192689d28Sdrh     }
122292689d28Sdrh 
12237d10d5a6Sdrh     /* If there are no aggregate functions in the result-set, and no GROUP BY
12247d10d5a6Sdrh     ** expression, do not allow aggregates in any of the other expressions.
12257d10d5a6Sdrh     */
12267d10d5a6Sdrh     assert( (p->selFlags & SF_Aggregate)==0 );
12277d10d5a6Sdrh     pGroupBy = p->pGroupBy;
1228a51009b2Sdrh     if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
12299588ad95Sdrh       assert( NC_MinMaxAgg==SF_MinMaxAgg );
12309588ad95Sdrh       p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
12317d10d5a6Sdrh     }else{
1232a51009b2Sdrh       sNC.ncFlags &= ~NC_AllowAgg;
12337d10d5a6Sdrh     }
12347d10d5a6Sdrh 
12357d10d5a6Sdrh     /* If a HAVING clause is present, then there must be a GROUP BY clause.
12367d10d5a6Sdrh     */
12377d10d5a6Sdrh     if( p->pHaving && !pGroupBy ){
12387d10d5a6Sdrh       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
12397d10d5a6Sdrh       return WRC_Abort;
12407d10d5a6Sdrh     }
12417d10d5a6Sdrh 
124226080d92Sdrh     /* Add the output column list to the name-context before parsing the
12437d10d5a6Sdrh     ** other expressions in the SELECT statement. This is so that
12447d10d5a6Sdrh     ** expressions in the WHERE clause (etc.) can refer to expressions by
12457d10d5a6Sdrh     ** aliases in the result set.
12467d10d5a6Sdrh     **
12477d10d5a6Sdrh     ** Minor point: If this is the case, then the expression will be
12487d10d5a6Sdrh     ** re-evaluated for each reference to it.
12497d10d5a6Sdrh     */
12507d10d5a6Sdrh     sNC.pEList = p->pEList;
125158a450c0Sdrh     if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
1252a3a5bd9bSdrh     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
12537d10d5a6Sdrh 
12547d10d5a6Sdrh     /* The ORDER BY and GROUP BY clauses may not refer to terms in
12557d10d5a6Sdrh     ** outer queries
12567d10d5a6Sdrh     */
12577d10d5a6Sdrh     sNC.pNext = 0;
1258a51009b2Sdrh     sNC.ncFlags |= NC_AllowAgg;
12597d10d5a6Sdrh 
12607d10d5a6Sdrh     /* Process the ORDER BY clause for singleton SELECT statements.
12617d10d5a6Sdrh     ** The ORDER BY clause for compounds SELECT statements is handled
12627d10d5a6Sdrh     ** below, after all of the result-sets for all of the elements of
12637d10d5a6Sdrh     ** the compound have been resolved.
12647d10d5a6Sdrh     */
12657d10d5a6Sdrh     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
12667d10d5a6Sdrh       return WRC_Abort;
12677d10d5a6Sdrh     }
12687d10d5a6Sdrh     if( db->mallocFailed ){
12697d10d5a6Sdrh       return WRC_Abort;
12707d10d5a6Sdrh     }
12717d10d5a6Sdrh 
12727d10d5a6Sdrh     /* Resolve the GROUP BY clause.  At the same time, make sure
12737d10d5a6Sdrh     ** the GROUP BY clause does not contain aggregate functions.
12747d10d5a6Sdrh     */
12757d10d5a6Sdrh     if( pGroupBy ){
12767d10d5a6Sdrh       struct ExprList_item *pItem;
12777d10d5a6Sdrh 
12787d10d5a6Sdrh       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
12797d10d5a6Sdrh         return WRC_Abort;
12807d10d5a6Sdrh       }
12817d10d5a6Sdrh       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
12827d10d5a6Sdrh         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
12837d10d5a6Sdrh           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
12847d10d5a6Sdrh               "the GROUP BY clause");
12857d10d5a6Sdrh           return WRC_Abort;
12867d10d5a6Sdrh         }
12877d10d5a6Sdrh       }
12887d10d5a6Sdrh     }
12897d10d5a6Sdrh 
12907d10d5a6Sdrh     /* Advance to the next term of the compound
12917d10d5a6Sdrh     */
12927d10d5a6Sdrh     p = p->pPrior;
12937d10d5a6Sdrh     nCompound++;
12947d10d5a6Sdrh   }
12957d10d5a6Sdrh 
12967d10d5a6Sdrh   /* Resolve the ORDER BY on a compound SELECT after all terms of
12977d10d5a6Sdrh   ** the compound have been resolved.
12987d10d5a6Sdrh   */
12997d10d5a6Sdrh   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
13007d10d5a6Sdrh     return WRC_Abort;
13017d10d5a6Sdrh   }
13027d10d5a6Sdrh 
13037d10d5a6Sdrh   return WRC_Prune;
13047d10d5a6Sdrh }
13057d10d5a6Sdrh 
13067d10d5a6Sdrh /*
13077d10d5a6Sdrh ** This routine walks an expression tree and resolves references to
13087d10d5a6Sdrh ** table columns and result-set columns.  At the same time, do error
13097d10d5a6Sdrh ** checking on function usage and set a flag if any aggregate functions
13107d10d5a6Sdrh ** are seen.
13117d10d5a6Sdrh **
13127d10d5a6Sdrh ** To resolve table columns references we look for nodes (or subtrees) of the
13137d10d5a6Sdrh ** form X.Y.Z or Y.Z or just Z where
13147d10d5a6Sdrh **
13157d10d5a6Sdrh **      X:   The name of a database.  Ex:  "main" or "temp" or
13167d10d5a6Sdrh **           the symbolic name assigned to an ATTACH-ed database.
13177d10d5a6Sdrh **
13187d10d5a6Sdrh **      Y:   The name of a table in a FROM clause.  Or in a trigger
13197d10d5a6Sdrh **           one of the special names "old" or "new".
13207d10d5a6Sdrh **
13217d10d5a6Sdrh **      Z:   The name of a column in table Y.
13227d10d5a6Sdrh **
13237d10d5a6Sdrh ** The node at the root of the subtree is modified as follows:
13247d10d5a6Sdrh **
13257d10d5a6Sdrh **    Expr.op        Changed to TK_COLUMN
13267d10d5a6Sdrh **    Expr.pTab      Points to the Table object for X.Y
13277d10d5a6Sdrh **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
13287d10d5a6Sdrh **    Expr.iTable    The VDBE cursor number for X.Y
13297d10d5a6Sdrh **
13307d10d5a6Sdrh **
13317d10d5a6Sdrh ** To resolve result-set references, look for expression nodes of the
13327d10d5a6Sdrh ** form Z (with no X and Y prefix) where the Z matches the right-hand
13337d10d5a6Sdrh ** size of an AS clause in the result-set of a SELECT.  The Z expression
13347d10d5a6Sdrh ** is replaced by a copy of the left-hand side of the result-set expression.
13357d10d5a6Sdrh ** Table-name and function resolution occurs on the substituted expression
13367d10d5a6Sdrh ** tree.  For example, in:
13377d10d5a6Sdrh **
13387d10d5a6Sdrh **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
13397d10d5a6Sdrh **
13407d10d5a6Sdrh ** The "x" term of the order by is replaced by "a+b" to render:
13417d10d5a6Sdrh **
13427d10d5a6Sdrh **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
13437d10d5a6Sdrh **
13447d10d5a6Sdrh ** Function calls are checked to make sure that the function is
13457d10d5a6Sdrh ** defined and that the correct number of arguments are specified.
1346a51009b2Sdrh ** If the function is an aggregate function, then the NC_HasAgg flag is
13477d10d5a6Sdrh ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
13487d10d5a6Sdrh ** If an expression contains aggregate functions then the EP_Agg
13497d10d5a6Sdrh ** property on the expression is set.
13507d10d5a6Sdrh **
13517d10d5a6Sdrh ** An error message is left in pParse if anything is amiss.  The number
13527d10d5a6Sdrh ** if errors is returned.
13537d10d5a6Sdrh */
13547d10d5a6Sdrh int sqlite3ResolveExprNames(
13557d10d5a6Sdrh   NameContext *pNC,       /* Namespace to resolve expressions in. */
13567d10d5a6Sdrh   Expr *pExpr             /* The expression to be analyzed. */
13577d10d5a6Sdrh ){
13589588ad95Sdrh   u16 savedHasAgg;
13597d10d5a6Sdrh   Walker w;
13607d10d5a6Sdrh 
13617d10d5a6Sdrh   if( pExpr==0 ) return 0;
13627d10d5a6Sdrh #if SQLITE_MAX_EXPR_DEPTH>0
13637d10d5a6Sdrh   {
13647d10d5a6Sdrh     Parse *pParse = pNC->pParse;
13657d10d5a6Sdrh     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
13667d10d5a6Sdrh       return 1;
13677d10d5a6Sdrh     }
13687d10d5a6Sdrh     pParse->nHeight += pExpr->nHeight;
13697d10d5a6Sdrh   }
13707d10d5a6Sdrh #endif
13719588ad95Sdrh   savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg);
13729588ad95Sdrh   pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg);
1373aa87f9a6Sdrh   memset(&w, 0, sizeof(w));
13747d10d5a6Sdrh   w.xExprCallback = resolveExprStep;
13757d10d5a6Sdrh   w.xSelectCallback = resolveSelectStep;
13767d10d5a6Sdrh   w.pParse = pNC->pParse;
13777d10d5a6Sdrh   w.u.pNC = pNC;
13787d10d5a6Sdrh   sqlite3WalkExpr(&w, pExpr);
13797d10d5a6Sdrh #if SQLITE_MAX_EXPR_DEPTH>0
13807d10d5a6Sdrh   pNC->pParse->nHeight -= pExpr->nHeight;
13817d10d5a6Sdrh #endif
1382fd773cf9Sdrh   if( pNC->nErr>0 || w.pParse->nErr>0 ){
13837d10d5a6Sdrh     ExprSetProperty(pExpr, EP_Error);
13847d10d5a6Sdrh   }
1385a51009b2Sdrh   if( pNC->ncFlags & NC_HasAgg ){
13867d10d5a6Sdrh     ExprSetProperty(pExpr, EP_Agg);
13877d10d5a6Sdrh   }
13889588ad95Sdrh   pNC->ncFlags |= savedHasAgg;
13897d10d5a6Sdrh   return ExprHasProperty(pExpr, EP_Error);
13907d10d5a6Sdrh }
13917d10d5a6Sdrh 
13927d10d5a6Sdrh 
13937d10d5a6Sdrh /*
13947d10d5a6Sdrh ** Resolve all names in all expressions of a SELECT and in all
13957d10d5a6Sdrh ** decendents of the SELECT, including compounds off of p->pPrior,
13967d10d5a6Sdrh ** subqueries in expressions, and subqueries used as FROM clause
13977d10d5a6Sdrh ** terms.
13987d10d5a6Sdrh **
13997d10d5a6Sdrh ** See sqlite3ResolveExprNames() for a description of the kinds of
14007d10d5a6Sdrh ** transformations that occur.
14017d10d5a6Sdrh **
14027d10d5a6Sdrh ** All SELECT statements should have been expanded using
14037d10d5a6Sdrh ** sqlite3SelectExpand() prior to invoking this routine.
14047d10d5a6Sdrh */
14057d10d5a6Sdrh void sqlite3ResolveSelectNames(
14067d10d5a6Sdrh   Parse *pParse,         /* The parser context */
14077d10d5a6Sdrh   Select *p,             /* The SELECT statement being coded. */
14087d10d5a6Sdrh   NameContext *pOuterNC  /* Name context for parent SELECT statement */
14097d10d5a6Sdrh ){
14107d10d5a6Sdrh   Walker w;
14117d10d5a6Sdrh 
14120a846f96Sdrh   assert( p!=0 );
1413aa87f9a6Sdrh   memset(&w, 0, sizeof(w));
14147d10d5a6Sdrh   w.xExprCallback = resolveExprStep;
14157d10d5a6Sdrh   w.xSelectCallback = resolveSelectStep;
14167d10d5a6Sdrh   w.pParse = pParse;
14177d10d5a6Sdrh   w.u.pNC = pOuterNC;
14187d10d5a6Sdrh   sqlite3WalkSelect(&w, p);
14197d10d5a6Sdrh }
14203780be11Sdrh 
14213780be11Sdrh /*
14223780be11Sdrh ** Resolve names in expressions that can only reference a single table:
14233780be11Sdrh **
14243780be11Sdrh **    *   CHECK constraints
14253780be11Sdrh **    *   WHERE clauses on partial indices
14263780be11Sdrh **
14273780be11Sdrh ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
14283780be11Sdrh ** is set to -1 and the Expr.iColumn value is set to the column number.
14293780be11Sdrh **
14303780be11Sdrh ** Any errors cause an error message to be set in pParse.
14313780be11Sdrh */
14323780be11Sdrh void sqlite3ResolveSelfReference(
14333780be11Sdrh   Parse *pParse,      /* Parsing context */
14343780be11Sdrh   Table *pTab,        /* The table being referenced */
14353780be11Sdrh   int type,           /* NC_IsCheck or NC_PartIdx */
14363780be11Sdrh   Expr *pExpr,        /* Expression to resolve.  May be NULL. */
14373780be11Sdrh   ExprList *pList     /* Expression list to resolve.  May be NUL. */
14383780be11Sdrh ){
14393780be11Sdrh   SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
14403780be11Sdrh   NameContext sNC;                /* Name context for pParse->pNewTable */
14413780be11Sdrh   int i;                          /* Loop counter */
14423780be11Sdrh 
14433780be11Sdrh   assert( type==NC_IsCheck || type==NC_PartIdx );
14443780be11Sdrh   memset(&sNC, 0, sizeof(sNC));
14453780be11Sdrh   memset(&sSrc, 0, sizeof(sSrc));
14463780be11Sdrh   sSrc.nSrc = 1;
14473780be11Sdrh   sSrc.a[0].zName = pTab->zName;
14483780be11Sdrh   sSrc.a[0].pTab = pTab;
14493780be11Sdrh   sSrc.a[0].iCursor = -1;
14503780be11Sdrh   sNC.pParse = pParse;
14513780be11Sdrh   sNC.pSrcList = &sSrc;
14523780be11Sdrh   sNC.ncFlags = type;
14533780be11Sdrh   if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
14543780be11Sdrh   if( pList ){
14553780be11Sdrh     for(i=0; i<pList->nExpr; i++){
14563780be11Sdrh       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
14573780be11Sdrh         return;
14583780be11Sdrh       }
14593780be11Sdrh     }
14603780be11Sdrh   }
14613780be11Sdrh }
1462