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
197d10d5a6Sdrh /*
20ec43d804Sdrh ** Magic table number to mean the EXCLUDED table in an UPSERT statement.
21ec43d804Sdrh */
22ec43d804Sdrh #define EXCLUDED_TABLE_NUMBER 2
23ec43d804Sdrh
24ec43d804Sdrh /*
25ed551b95Sdrh ** Walk the expression tree pExpr and increase the aggregate function
26ed551b95Sdrh ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
27ed551b95Sdrh ** This needs to occur when copying a TK_AGG_FUNCTION node from an
28ed551b95Sdrh ** outer query into an inner subquery.
29ed551b95Sdrh **
30ed551b95Sdrh ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
31ed551b95Sdrh ** is a helper function - a callback for the tree walker.
32c37577bbSdrh **
33c37577bbSdrh ** See also the sqlite3WindowExtraAggFuncDepth() routine in window.c
34ed551b95Sdrh */
incrAggDepth(Walker * pWalker,Expr * pExpr)35ed551b95Sdrh static int incrAggDepth(Walker *pWalker, Expr *pExpr){
36059b2d50Sdrh if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
37ed551b95Sdrh return WRC_Continue;
38ed551b95Sdrh }
incrAggFunctionDepth(Expr * pExpr,int N)39ed551b95Sdrh static void incrAggFunctionDepth(Expr *pExpr, int N){
40ed551b95Sdrh if( N>0 ){
41ed551b95Sdrh Walker w;
42ed551b95Sdrh memset(&w, 0, sizeof(w));
43ed551b95Sdrh w.xExprCallback = incrAggDepth;
44059b2d50Sdrh w.u.n = N;
45ed551b95Sdrh sqlite3WalkExpr(&w, pExpr);
46ed551b95Sdrh }
47ed551b95Sdrh }
48ed551b95Sdrh
49ed551b95Sdrh /*
508b213899Sdrh ** Turn the pExpr expression into an alias for the iCol-th column of the
518b213899Sdrh ** result set in pEList.
528b213899Sdrh **
530a8a406eSdrh ** If the reference is followed by a COLLATE operator, then make sure
540a8a406eSdrh ** the COLLATE operator is preserved. For example:
550a8a406eSdrh **
560a8a406eSdrh ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
570a8a406eSdrh **
580a8a406eSdrh ** Should be transformed into:
590a8a406eSdrh **
600a8a406eSdrh ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
610a8a406eSdrh **
62ed551b95Sdrh ** The nSubquery parameter specifies how many levels of subquery the
6341148f83Sdrh ** alias is removed from the original expression. The usual value is
64ed551b95Sdrh ** zero but it might be more if the alias is contained within a subquery
65ed551b95Sdrh ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
66ed551b95Sdrh ** structures must be increased by the nSubquery amount.
678b213899Sdrh */
resolveAlias(Parse * pParse,ExprList * pEList,int iCol,Expr * pExpr,int nSubquery)688b213899Sdrh static void resolveAlias(
698b213899Sdrh Parse *pParse, /* Parsing context */
708b213899Sdrh ExprList *pEList, /* A result set */
718b213899Sdrh int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
728b213899Sdrh Expr *pExpr, /* Transform this into an alias to the result set */
73ed551b95Sdrh int nSubquery /* Number of subqueries that the label is moving */
748b213899Sdrh ){
758b213899Sdrh Expr *pOrig; /* The iCol-th column of the result set */
768b213899Sdrh Expr *pDup; /* Copy of pOrig */
778b213899Sdrh sqlite3 *db; /* The database connection */
788b213899Sdrh
798b213899Sdrh assert( iCol>=0 && iCol<pEList->nExpr );
808b213899Sdrh pOrig = pEList->a[iCol].pExpr;
818b213899Sdrh assert( pOrig!=0 );
828b213899Sdrh db = pParse->db;
836ab3a2ecSdanielk1977 pDup = sqlite3ExprDup(db, pOrig, 0);
843c6edc8aSdrh if( db->mallocFailed ){
853c6edc8aSdrh sqlite3ExprDelete(db, pDup);
863c6edc8aSdrh pDup = 0;
873c6edc8aSdrh }else{
883245f3beSdrh Expr temp;
89c7e93f58Sdrh incrAggFunctionDepth(pDup, nSubquery);
900a8a406eSdrh if( pExpr->op==TK_COLLATE ){
91f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue) );
920a8a406eSdrh pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
930a8a406eSdrh }
943245f3beSdrh memcpy(&temp, pDup, sizeof(Expr));
953245f3beSdrh memcpy(pDup, pExpr, sizeof(Expr));
963245f3beSdrh memcpy(pExpr, &temp, sizeof(Expr));
97a51ddb1eSdan if( ExprHasProperty(pExpr, EP_WinFunc) ){
98feef4472Sdrh if( ALWAYS(pExpr->y.pWin!=0) ){
99a51ddb1eSdan pExpr->y.pWin->pOwner = pExpr;
100d79cd92bSdrh }
101a51ddb1eSdan }
102*95530163Sdrh sqlite3ExprDeferredDelete(pParse, pDup);
1038b213899Sdrh }
1044344dbd3Sdrh }
1058b213899Sdrh
1063e3f1a5bSdrh /*
1073e3f1a5bSdrh ** Subqueries stores the original database, table and column names for their
1083e3f1a5bSdrh ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
1093e3f1a5bSdrh ** Check to see if the zSpan given to this routine matches the zDb, zTab,
1103e3f1a5bSdrh ** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
1113e3f1a5bSdrh ** match anything.
1123e3f1a5bSdrh */
sqlite3MatchEName(const struct ExprList_item * pItem,const char * zCol,const char * zTab,const char * zDb)113c4938ea2Sdrh int sqlite3MatchEName(
114c4938ea2Sdrh const struct ExprList_item *pItem,
1153e3f1a5bSdrh const char *zCol,
1163e3f1a5bSdrh const char *zTab,
1173e3f1a5bSdrh const char *zDb
1183e3f1a5bSdrh ){
1193e3f1a5bSdrh int n;
120c4938ea2Sdrh const char *zSpan;
121d88fd539Sdrh if( pItem->fg.eEName!=ENAME_TAB ) return 0;
122c4938ea2Sdrh zSpan = pItem->zEName;
1233e3f1a5bSdrh for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
124dd1dd489Sdrh if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
1253e3f1a5bSdrh return 0;
1263e3f1a5bSdrh }
1273e3f1a5bSdrh zSpan += n+1;
1283e3f1a5bSdrh for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
129dd1dd489Sdrh if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
1303e3f1a5bSdrh return 0;
1313e3f1a5bSdrh }
1323e3f1a5bSdrh zSpan += n+1;
1333e3f1a5bSdrh if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
1343e3f1a5bSdrh return 0;
1353e3f1a5bSdrh }
1363e3f1a5bSdrh return 1;
1373e3f1a5bSdrh }
138e802c5daSdrh
1398b213899Sdrh /*
140d0ff601cSdrh ** Return TRUE if the double-quoted string mis-feature should be supported.
141d0ff601cSdrh */
areDoubleQuotedStringsEnabled(sqlite3 * db,NameContext * pTopNC)142d0ff601cSdrh static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
143d0ff601cSdrh if( db->init.busy ) return 1; /* Always support for legacy schemas */
144d0ff601cSdrh if( pTopNC->ncFlags & NC_IsDDL ){
145d0ff601cSdrh /* Currently parsing a DDL statement */
146d0ff601cSdrh if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
147d0ff601cSdrh return 1;
148d0ff601cSdrh }
149d0ff601cSdrh return (db->flags & SQLITE_DqsDDL)!=0;
150d0ff601cSdrh }else{
151d0ff601cSdrh /* Currently parsing a DML statement */
152d0ff601cSdrh return (db->flags & SQLITE_DqsDML)!=0;
153d0ff601cSdrh }
154d0ff601cSdrh }
155d0ff601cSdrh
156d0ff601cSdrh /*
15774a07986Sdrh ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
15874a07986Sdrh ** return the appropriate colUsed mask.
15974a07986Sdrh */
sqlite3ExprColUsed(Expr * pExpr)16074a07986Sdrh Bitmask sqlite3ExprColUsed(Expr *pExpr){
16174a07986Sdrh int n;
16274a07986Sdrh Table *pExTab;
16374a07986Sdrh
16474a07986Sdrh n = pExpr->iColumn;
165477572b9Sdrh assert( ExprUseYTab(pExpr) );
16674a07986Sdrh pExTab = pExpr->y.pTab;
16774a07986Sdrh assert( pExTab!=0 );
16874a07986Sdrh if( (pExTab->tabFlags & TF_HasGenerated)!=0
16974a07986Sdrh && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
17074a07986Sdrh ){
17174a07986Sdrh testcase( pExTab->nCol==BMS-1 );
17274a07986Sdrh testcase( pExTab->nCol==BMS );
17374a07986Sdrh return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
17474a07986Sdrh }else{
17574a07986Sdrh testcase( n==BMS-1 );
17674a07986Sdrh testcase( n==BMS );
17774a07986Sdrh if( n>=BMS ) n = BMS-1;
17874a07986Sdrh return ((Bitmask)1)<<n;
17974a07986Sdrh }
18074a07986Sdrh }
18174a07986Sdrh
18274a07986Sdrh /*
183bb3c62a7Sdrh ** Create a new expression term for the column specified by pMatch and
184bb3c62a7Sdrh ** iColumn. Append this new expression term to the FULL JOIN Match set
185bb3c62a7Sdrh ** in *ppList. Create a new *ppList if this is the first term in the
186bb3c62a7Sdrh ** set.
187bb3c62a7Sdrh */
extendFJMatch(Parse * pParse,ExprList ** ppList,SrcItem * pMatch,i16 iColumn)188bb3c62a7Sdrh static void extendFJMatch(
189bb3c62a7Sdrh Parse *pParse, /* Parsing context */
190bb3c62a7Sdrh ExprList **ppList, /* ExprList to extend */
191bb3c62a7Sdrh SrcItem *pMatch, /* Source table containing the column */
192bb3c62a7Sdrh i16 iColumn /* The column number */
193bb3c62a7Sdrh ){
194bb3c62a7Sdrh Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
195bb3c62a7Sdrh if( pNew ){
196bb3c62a7Sdrh pNew->iTable = pMatch->iCursor;
19779a25ee0Sdrh pNew->iColumn = iColumn;
19879a25ee0Sdrh pNew->y.pTab = pMatch->pTab;
199bb3c62a7Sdrh assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
200bb3c62a7Sdrh ExprSetProperty(pNew, EP_CanBeNull);
201bb3c62a7Sdrh *ppList = sqlite3ExprListAppend(pParse, *ppList, pNew);
202bb3c62a7Sdrh }
203bb3c62a7Sdrh }
204bb3c62a7Sdrh
205bb3c62a7Sdrh /*
2067d10d5a6Sdrh ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
2077d10d5a6Sdrh ** that name in the set of source tables in pSrcList and make the pExpr
2087d10d5a6Sdrh ** expression node refer back to that source column. The following changes
2097d10d5a6Sdrh ** are made to pExpr:
2107d10d5a6Sdrh **
2117d10d5a6Sdrh ** pExpr->iDb Set the index in db->aDb[] of the database X
2127d10d5a6Sdrh ** (even if X is implied).
2137d10d5a6Sdrh ** pExpr->iTable Set to the cursor number for the table obtained
2147d10d5a6Sdrh ** from pSrcList.
215eda079cdSdrh ** pExpr->y.pTab Points to the Table structure of X.Y (even if
2167d10d5a6Sdrh ** X and/or Y are implied.)
2177d10d5a6Sdrh ** pExpr->iColumn Set to the column number within the table.
2187d10d5a6Sdrh ** pExpr->op Set to TK_COLUMN.
2197d10d5a6Sdrh ** pExpr->pLeft Any expression this points to is deleted
2207d10d5a6Sdrh ** pExpr->pRight Any expression this points to is deleted.
2217d10d5a6Sdrh **
222b7916a78Sdrh ** The zDb variable is the name of the database (the "X"). This value may be
2237d10d5a6Sdrh ** NULL meaning that name is of the form Y.Z or Z. Any available database
224b7916a78Sdrh ** can be used. The zTable variable is the name of the table (the "Y"). This
225b7916a78Sdrh ** value can be NULL if zDb is also NULL. If zTable is NULL it
2267d10d5a6Sdrh ** means that the form of the name is Z and that columns from any table
2277d10d5a6Sdrh ** can be used.
2287d10d5a6Sdrh **
2297d10d5a6Sdrh ** If the name cannot be resolved unambiguously, leave an error message
230f7828b5cSdrh ** in pParse and return WRC_Abort. Return WRC_Prune on success.
2317d10d5a6Sdrh */
lookupName(Parse * pParse,const char * zDb,const char * zTab,const char * zCol,NameContext * pNC,Expr * pExpr)2327d10d5a6Sdrh static int lookupName(
2337d10d5a6Sdrh Parse *pParse, /* The parsing context */
234b7916a78Sdrh const char *zDb, /* Name of the database containing table, or NULL */
235b7916a78Sdrh const char *zTab, /* Name of table containing column, or NULL */
236b7916a78Sdrh const char *zCol, /* Name of the column. */
2377d10d5a6Sdrh NameContext *pNC, /* The name context used to resolve the name */
2387d10d5a6Sdrh Expr *pExpr /* Make this EXPR node point to the selected column */
2397d10d5a6Sdrh ){
2407d10d5a6Sdrh int i, j; /* Loop counters */
2417d10d5a6Sdrh int cnt = 0; /* Number of matching column names */
2427d10d5a6Sdrh int cntTab = 0; /* Number of matching table names */
243ed551b95Sdrh int nSubquery = 0; /* How many levels of subquery */
2447d10d5a6Sdrh sqlite3 *db = pParse->db; /* The database connection */
2457601294aSdrh SrcItem *pItem; /* Use for looping over pSrcList items */
2467601294aSdrh SrcItem *pMatch = 0; /* The matching pSrcList item */
2477d10d5a6Sdrh NameContext *pTopNC = pNC; /* First namecontext in the list */
2487d10d5a6Sdrh Schema *pSchema = 0; /* Schema of the expression */
249eac9fabbSdrh int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
250bb3c62a7Sdrh Table *pTab = 0; /* Table holding the row */
25111f9b033Sdrh Column *pCol; /* A column of pTab */
252bb3c62a7Sdrh ExprList *pFJMatch = 0; /* Matches for FULL JOIN .. USING */
2537d10d5a6Sdrh
254e34c647eSshane assert( pNC ); /* the name context cannot be NULL. */
255b7916a78Sdrh assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
25618f8600fSdrh assert( zDb==0 || zTab!=0 );
257c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
2587d10d5a6Sdrh
2597d10d5a6Sdrh /* Initialize the node to no-match */
2607d10d5a6Sdrh pExpr->iTable = -1;
261ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce);
2627d10d5a6Sdrh
2638f25d18bSdrh /* Translate the schema name in zDb into a pointer to the corresponding
2648f25d18bSdrh ** schema. If not found, pSchema will remain NULL and nothing will match
2658f25d18bSdrh ** resulting in an appropriate error message toward the end of this routine
2668f25d18bSdrh */
2678f25d18bSdrh if( zDb ){
2681e7d43c9Sdrh testcase( pNC->ncFlags & NC_PartIdx );
2691e7d43c9Sdrh testcase( pNC->ncFlags & NC_IsCheck );
2701e7d43c9Sdrh if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
27131e7147dSdrh /* Silently ignore database qualifiers inside CHECK constraints and
27231e7147dSdrh ** partial indices. Do not raise errors because that might break
27331e7147dSdrh ** legacy and because it does not hurt anything to just ignore the
27431e7147dSdrh ** database name. */
2751e7d43c9Sdrh zDb = 0;
2761e7d43c9Sdrh }else{
2778f25d18bSdrh for(i=0; i<db->nDb; i++){
27869c33826Sdrh assert( db->aDb[i].zDbSName );
27969c33826Sdrh if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
2808f25d18bSdrh pSchema = db->aDb[i].pSchema;
2818f25d18bSdrh break;
2828f25d18bSdrh }
2838f25d18bSdrh }
28400bd55e1Sdan if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
28500bd55e1Sdan /* This branch is taken when the main database has been renamed
28600bd55e1Sdan ** using SQLITE_DBCONFIG_MAINDBNAME. */
28700bd55e1Sdan pSchema = db->aDb[0].pSchema;
28800bd55e1Sdan zDb = db->aDb[0].zDbSName;
28900bd55e1Sdan }
2908f25d18bSdrh }
2911e7d43c9Sdrh }
2928f25d18bSdrh
2937d10d5a6Sdrh /* Start at the inner-most context and move outward until a match is found */
294a92b81f4Sdrh assert( pNC && cnt==0 );
295a92b81f4Sdrh do{
2967d10d5a6Sdrh ExprList *pEList;
2977d10d5a6Sdrh SrcList *pSrcList = pNC->pSrcList;
2987d10d5a6Sdrh
2997d10d5a6Sdrh if( pSrcList ){
3007d10d5a6Sdrh for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
301d44390c8Sdrh u8 hCol;
3027d10d5a6Sdrh pTab = pItem->pTab;
303f436620eSdrh assert( pTab!=0 && pTab->zName!=0 );
3044d466698Sdrh assert( pTab->nCol>0 || pParse->nErr );
30507fae32dSmistachkin assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
306815b782eSdrh if( pItem->fg.isNestedFrom ){
3072627cbd4Sdrh /* In this case, pItem is a subquery that has been formed from a
3082627cbd4Sdrh ** parenthesized subset of the FROM clause terms. Example:
3092627cbd4Sdrh ** .... FROM t1 LEFT JOIN (t2 RIGHT JOIN t3 USING(x)) USING(y) ...
3102627cbd4Sdrh ** \_________________________/
3112627cbd4Sdrh ** This pItem -------------^
3122627cbd4Sdrh */
3138f25d18bSdrh int hit = 0;
314815b782eSdrh assert( pItem->pSelect!=0 );
315928d9c62Sdrh pEList = pItem->pSelect->pEList;
316815b782eSdrh assert( pEList!=0 );
317815b782eSdrh assert( pEList->nExpr==pTab->nCol );
3188f25d18bSdrh for(j=0; j<pEList->nExpr; j++){
31918f8600fSdrh if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
32018f8600fSdrh continue;
32118f8600fSdrh }
322bb3c62a7Sdrh if( cnt>0 ){
323bb3c62a7Sdrh if( pItem->fg.isUsing==0
324fe146997Sdrh || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
325bb3c62a7Sdrh ){
3262e615889Sdrh /* Two or more tables have the same column name which is
3272e615889Sdrh ** not joined by USING. This is an error. Signal as much
3282e615889Sdrh ** by clearing pFJMatch and letting cnt go above 1. */
329bb3c62a7Sdrh sqlite3ExprListDelete(db, pFJMatch);
330bb3c62a7Sdrh pFJMatch = 0;
331bb3c62a7Sdrh }else
332bb3c62a7Sdrh if( (pItem->fg.jointype & JT_RIGHT)==0 ){
333bb3c62a7Sdrh /* An INNER or LEFT JOIN. Use the left-most table */
334bb3c62a7Sdrh continue;
335bb3c62a7Sdrh }else
336bb3c62a7Sdrh if( (pItem->fg.jointype & JT_LEFT)==0 ){
337bb3c62a7Sdrh /* A RIGHT JOIN. Use the right-most table */
338bb3c62a7Sdrh cnt = 0;
339bb3c62a7Sdrh sqlite3ExprListDelete(db, pFJMatch);
340bb3c62a7Sdrh pFJMatch = 0;
341bb3c62a7Sdrh }else{
342bb3c62a7Sdrh /* For a FULL JOIN, we must construct a coalesce() func */
34379a25ee0Sdrh extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
344bb3c62a7Sdrh }
345bb3c62a7Sdrh }
3468f25d18bSdrh cnt++;
3478f25d18bSdrh cntTab = 2;
3488f25d18bSdrh pMatch = pItem;
3498f25d18bSdrh pExpr->iColumn = j;
350d88fd539Sdrh pEList->a[j].fg.bUsed = 1;
35118f8600fSdrh hit = 1;
352d88fd539Sdrh if( pEList->a[j].fg.bUsingTerm ) break;
3538f25d18bSdrh }
3548f25d18bSdrh if( hit || zTab==0 ) continue;
3558f25d18bSdrh }
35618f8600fSdrh assert( zDb==0 || zTab!=0 );
35718f8600fSdrh if( zTab ){
35818f8600fSdrh const char *zTabName;
359ceeb04bfSdrh if( zDb ){
360ceeb04bfSdrh if( pTab->pSchema!=pSchema ) continue;
361ceeb04bfSdrh if( pSchema==0 && strcmp(zDb,"*")!=0 ) continue;
362c75e09c7Sdrh }
36318f8600fSdrh zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
3648f25d18bSdrh assert( zTabName!=0 );
3658f25d18bSdrh if( sqlite3StrICmp(zTabName, zTab)!=0 ){
36673c0fdc7Sdrh continue;
36773c0fdc7Sdrh }
368477572b9Sdrh assert( ExprUseYTab(pExpr) );
369c9461eccSdan if( IN_RENAME_OBJECT && pItem->zAlias ){
370eda079cdSdrh sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
371c9461eccSdan }
3727d10d5a6Sdrh }
373d44390c8Sdrh hCol = sqlite3StrIHash(zCol);
3747d10d5a6Sdrh for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
375cf9d36d1Sdrh if( pCol->hName==hCol
376cf9d36d1Sdrh && sqlite3StrICmp(pCol->zCnName, zCol)==0
377cf9d36d1Sdrh ){
378bb3c62a7Sdrh if( cnt>0 ){
379bb3c62a7Sdrh if( pItem->fg.isUsing==0
380fe146997Sdrh || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
381d44f8b23Sdrh ){
3822e615889Sdrh /* Two or more tables have the same column name which is
3832e615889Sdrh ** not joined by USING. This is an error. Signal as much
3842e615889Sdrh ** by clearing pFJMatch and letting cnt go above 1. */
385bb3c62a7Sdrh sqlite3ExprListDelete(db, pFJMatch);
386bb3c62a7Sdrh pFJMatch = 0;
387bb3c62a7Sdrh }else
388bb3c62a7Sdrh if( (pItem->fg.jointype & JT_RIGHT)==0 ){
389bb3c62a7Sdrh /* An INNER or LEFT JOIN. Use the left-most table */
390d44f8b23Sdrh continue;
391bb3c62a7Sdrh }else
392bb3c62a7Sdrh if( (pItem->fg.jointype & JT_LEFT)==0 ){
393bb3c62a7Sdrh /* A RIGHT JOIN. Use the right-most table */
394bb3c62a7Sdrh cnt = 0;
395bb3c62a7Sdrh sqlite3ExprListDelete(db, pFJMatch);
396bb3c62a7Sdrh pFJMatch = 0;
397bb3c62a7Sdrh }else{
398bb3c62a7Sdrh /* For a FULL JOIN, we must construct a coalesce() func */
39979a25ee0Sdrh extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
400d44f8b23Sdrh }
401e802c5daSdrh }
4027d10d5a6Sdrh cnt++;
4037d10d5a6Sdrh pMatch = pItem;
4047d10d5a6Sdrh /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
405cf697396Sshane pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
40618f8600fSdrh if( pItem->fg.isNestedFrom ){
40718f8600fSdrh sqlite3SrcItemColumnUsed(pItem, j);
40818f8600fSdrh }
4097d10d5a6Sdrh break;
4107d10d5a6Sdrh }
4117d10d5a6Sdrh }
412b9248ef5Sdan if( 0==cnt && VisibleRowid(pTab) ){
413b9248ef5Sdan cntTab++;
414b9248ef5Sdan pMatch = pItem;
415b9248ef5Sdan }
4167d10d5a6Sdrh }
4178f25d18bSdrh if( pMatch ){
4188f25d18bSdrh pExpr->iTable = pMatch->iCursor;
419477572b9Sdrh assert( ExprUseYTab(pExpr) );
420eda079cdSdrh pExpr->y.pTab = pMatch->pTab;
421a76ac88aSdrh if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
42272673a24Sdrh ExprSetProperty(pExpr, EP_CanBeNull);
42372673a24Sdrh }
424eda079cdSdrh pSchema = pExpr->y.pTab->pSchema;
4257d10d5a6Sdrh }
4268f25d18bSdrh } /* if( pSrcList ) */
4277d10d5a6Sdrh
428eac9fabbSdrh #if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
4297d10d5a6Sdrh /* If we have not already resolved the name, then maybe
430eac9fabbSdrh ** it is a new.* or old.* trigger argument reference. Or
431d5101972Sdrh ** maybe it is an excluded.* from an upsert. Or maybe it is
432d5101972Sdrh ** a reference in the RETURNING clause to a table being modified.
4337d10d5a6Sdrh */
434d5101972Sdrh if( cnt==0 && zDb==0 ){
435eac9fabbSdrh pTab = 0;
436eac9fabbSdrh #ifndef SQLITE_OMIT_TRIGGER
437eac9fabbSdrh if( pParse->pTriggerTab!=0 ){
43865a7cd16Sdan int op = pParse->eTriggerOp;
43965a7cd16Sdan assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
44022af584eSdrh if( pParse->bReturning ){
44122af584eSdrh if( (pNC->ncFlags & NC_UBaseReg)!=0
44222af584eSdrh && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
44322af584eSdrh ){
444d75aeee5Sdrh pExpr->iTable = op!=TK_DELETE;
445d75aeee5Sdrh pTab = pParse->pTriggerTab;
446d75aeee5Sdrh }
447d75aeee5Sdrh }else if( op!=TK_DELETE && zTab && sqlite3StrICmp("new",zTab) == 0 ){
448165921a7Sdan pExpr->iTable = 1;
449165921a7Sdan pTab = pParse->pTriggerTab;
450b8352479Sdrh }else if( op!=TK_INSERT && zTab && sqlite3StrICmp("old",zTab)==0 ){
451165921a7Sdan pExpr->iTable = 0;
452165921a7Sdan pTab = pParse->pTriggerTab;
4537d10d5a6Sdrh }
454eac9fabbSdrh }
455eac9fabbSdrh #endif /* SQLITE_OMIT_TRIGGER */
456eac9fabbSdrh #ifndef SQLITE_OMIT_UPSERT
457d5101972Sdrh if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
458eac9fabbSdrh Upsert *pUpsert = pNC->uNC.pUpsert;
459eac9fabbSdrh if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
460eac9fabbSdrh pTab = pUpsert->pUpsertSrc->a[0].pTab;
461ec43d804Sdrh pExpr->iTable = EXCLUDED_TABLE_NUMBER;
462eac9fabbSdrh }
463eac9fabbSdrh }
464eac9fabbSdrh #endif /* SQLITE_OMIT_UPSERT */
4657d10d5a6Sdrh
4667d10d5a6Sdrh if( pTab ){
4677d10d5a6Sdrh int iCol;
468d44390c8Sdrh u8 hCol = sqlite3StrIHash(zCol);
4697d10d5a6Sdrh pSchema = pTab->pSchema;
4707d10d5a6Sdrh cntTab++;
471511717c6Sdrh for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
472cf9d36d1Sdrh if( pCol->hName==hCol
473cf9d36d1Sdrh && sqlite3StrICmp(pCol->zCnName, zCol)==0
474cf9d36d1Sdrh ){
4752bd93516Sdan if( iCol==pTab->iPKey ){
4762bd93516Sdan iCol = -1;
4772bd93516Sdan }
4787d10d5a6Sdrh break;
4797d10d5a6Sdrh }
4807d10d5a6Sdrh }
481fccda8a1Sdrh if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
482e8a537eeSdrh /* IMP: R-51414-32910 */
483bbbb0e80Sdrh iCol = -1;
4847d10d5a6Sdrh }
4852bd93516Sdan if( iCol<pTab->nCol ){
4862bd93516Sdan cnt++;
487a7e16a2fSdan pMatch = 0;
488eac9fabbSdrh #ifndef SQLITE_OMIT_UPSERT
489ec43d804Sdrh if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
4909916048bSdrh testcase( iCol==(-1) );
491477572b9Sdrh assert( ExprUseYTab(pExpr) );
492c9461eccSdan if( IN_RENAME_OBJECT ){
49385a9d508Sdan pExpr->iColumn = iCol;
494eda079cdSdrh pExpr->y.pTab = pTab;
49585a9d508Sdan eNewExprOp = TK_COLUMN;
49685a9d508Sdan }else{
497b8fec219Sdrh pExpr->iTable = pNC->uNC.pUpsert->regData +
498b8fec219Sdrh sqlite3TableColumnToStorage(pTab, iCol);
499eac9fabbSdrh eNewExprOp = TK_REGISTER;
50085a9d508Sdan }
501eac9fabbSdrh }else
5020a6259f5Sdrh #endif /* SQLITE_OMIT_UPSERT */
503eac9fabbSdrh {
504477572b9Sdrh assert( ExprUseYTab(pExpr) );
505552562c4Sdrh pExpr->y.pTab = pTab;
506552562c4Sdrh if( pParse->bReturning ){
507552562c4Sdrh eNewExprOp = TK_REGISTER;
50841584df5Sdrh pExpr->op2 = TK_COLUMN;
5090d08072bSdan pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
5100d08072bSdan sqlite3TableColumnToStorage(pTab, iCol) + 1;
511552562c4Sdrh }else{
512552562c4Sdrh pExpr->iColumn = (i16)iCol;
513552562c4Sdrh eNewExprOp = TK_TRIGGER;
514eac9fabbSdrh #ifndef SQLITE_OMIT_TRIGGER
5158873aea3Sdrh if( iCol<0 ){
5168873aea3Sdrh pExpr->affExpr = SQLITE_AFF_INTEGER;
5178873aea3Sdrh }else if( pExpr->iTable==0 ){
5182832ad42Sdan testcase( iCol==31 );
5192832ad42Sdan testcase( iCol==32 );
5202832ad42Sdan pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
521bb5f168fSdan }else{
522bb5f168fSdan testcase( iCol==31 );
523bb5f168fSdan testcase( iCol==32 );
524bb5f168fSdan pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
5252bd93516Sdan }
5260a6259f5Sdrh #endif /* SQLITE_OMIT_TRIGGER */
5272bd93516Sdan }
5282bd93516Sdan }
5297d10d5a6Sdrh }
530eac9fabbSdrh }
531552562c4Sdrh }
532eac9fabbSdrh #endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
5337d10d5a6Sdrh
5347d10d5a6Sdrh /*
5357d10d5a6Sdrh ** Perhaps the name is a reference to the ROWID
5367d10d5a6Sdrh */
5374cbc54b0Sdrh if( cnt==0
5384cbc54b0Sdrh && cntTab==1
5394cbc54b0Sdrh && pMatch
54081f7b372Sdrh && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
5414cbc54b0Sdrh && sqlite3IsRowid(zCol)
542dd513654Sdrh && ALWAYS(VisibleRowid(pMatch->pTab))
5434cbc54b0Sdrh ){
5447d10d5a6Sdrh cnt = 1;
54515427279Sdrh pExpr->iColumn = -1;
5461194904bSdrh pExpr->affExpr = SQLITE_AFF_INTEGER;
5477d10d5a6Sdrh }
5487d10d5a6Sdrh
5497d10d5a6Sdrh /*
5507d10d5a6Sdrh ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
5517d10d5a6Sdrh ** might refer to an result-set alias. This happens, for example, when
5527d10d5a6Sdrh ** we are resolving names in the WHERE clause of the following command:
5537d10d5a6Sdrh **
5547d10d5a6Sdrh ** SELECT a+b AS x FROM table WHERE x<10;
5557d10d5a6Sdrh **
5567d10d5a6Sdrh ** In cases like this, replace pExpr with a copy of the expression that
5577d10d5a6Sdrh ** forms the result set entry ("a+b" in the example) and return immediately.
5587d10d5a6Sdrh ** Note that the expression in the result set should have already been
5597d10d5a6Sdrh ** resolved by the time the WHERE clause is resolved.
560e35463b3Sdrh **
561e35463b3Sdrh ** The ability to use an output result-set column in the WHERE, GROUP BY,
5625579d59fSdrh ** or HAVING clauses, or as part of a larger expression in the ORDER BY
563e35463b3Sdrh ** clause is not standard SQL. This is a (goofy) SQLite extension, that
5645579d59fSdrh ** is supported for backwards compatibility only. Hence, we issue a warning
565e35463b3Sdrh ** on sqlite3_log() whenever the capability is used.
5667d10d5a6Sdrh */
567de0e1b15Sdrh if( cnt==0
568de0e1b15Sdrh && (pNC->ncFlags & NC_UEList)!=0
56925c3b8caSdrh && zTab==0
570a3a5bd9bSdrh ){
57125c3b8caSdrh pEList = pNC->uNC.pEList;
57225c3b8caSdrh assert( pEList!=0 );
5737d10d5a6Sdrh for(j=0; j<pEList->nExpr; j++){
5749fc1b9afSdrh char *zAs = pEList->a[j].zEName;
575d88fd539Sdrh if( pEList->a[j].fg.eEName==ENAME_NAME
576607dd6e6Sdan && sqlite3_stricmp(zAs, zCol)==0
577c4938ea2Sdrh ){
5788b213899Sdrh Expr *pOrig;
5797d10d5a6Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 );
580a4eeccdfSdrh assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
581a4eeccdfSdrh assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
5827d10d5a6Sdrh pOrig = pEList->a[j].pExpr;
583a51009b2Sdrh if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
5847d10d5a6Sdrh sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
585f7828b5cSdrh return WRC_Abort;
5867d10d5a6Sdrh }
587e3735bf4Sdan if( ExprHasProperty(pOrig, EP_Win)
588e3735bf4Sdan && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
589e3735bf4Sdan ){
5904ded26a5Sdan sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
5914ded26a5Sdan return WRC_Abort;
5924ded26a5Sdan }
5933bafdedeSdan if( sqlite3ExprVectorSize(pOrig)!=1 ){
5943bafdedeSdan sqlite3ErrorMsg(pParse, "row value misused");
5953bafdedeSdan return WRC_Abort;
5963bafdedeSdan }
5976b37b762Sdrh resolveAlias(pParse, pEList, j, pExpr, nSubquery);
5987d10d5a6Sdrh cnt = 1;
5997d10d5a6Sdrh pMatch = 0;
6007d10d5a6Sdrh assert( zTab==0 && zDb==0 );
601c9461eccSdan if( IN_RENAME_OBJECT ){
6021b0c5de4Sdan sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
6031b0c5de4Sdan }
604b7916a78Sdrh goto lookupname_end;
6057d10d5a6Sdrh }
6067d10d5a6Sdrh }
6077d10d5a6Sdrh }
6087d10d5a6Sdrh
6097d10d5a6Sdrh /* Advance to the next name context. The loop will exit when either
6107d10d5a6Sdrh ** we have a match (cnt>0) or when we run out of name contexts.
6117d10d5a6Sdrh */
612a92b81f4Sdrh if( cnt ) break;
6137d10d5a6Sdrh pNC = pNC->pNext;
614ed551b95Sdrh nSubquery++;
615a92b81f4Sdrh }while( pNC );
616a92b81f4Sdrh
6177d10d5a6Sdrh
6187d10d5a6Sdrh /*
6197d10d5a6Sdrh ** If X and Y are NULL (in other words if only the column name Z is
6207d10d5a6Sdrh ** supplied) and the value of Z is enclosed in double-quotes, then
6217d10d5a6Sdrh ** Z is a string literal if it doesn't match any column names. In that
6227d10d5a6Sdrh ** case, we need to return right away and not make any changes to
6237d10d5a6Sdrh ** pExpr.
6247d10d5a6Sdrh **
6257d10d5a6Sdrh ** Because no reference was made to outer contexts, the pNC->nRef
6267d10d5a6Sdrh ** fields are not changed in any context.
6277d10d5a6Sdrh */
628007c843bSdrh if( cnt==0 && zTab==0 ){
629171d16bbSdrh assert( pExpr->op==TK_ID );
6300d92571dSdan if( ExprHasProperty(pExpr,EP_DblQuoted)
631d0ff601cSdrh && areDoubleQuotedStringsEnabled(db, pTopNC)
6320d92571dSdan ){
633ec8fc62cSdrh /* If a double-quoted identifier does not match any known column name,
634ec8fc62cSdrh ** then treat it as a string.
635ec8fc62cSdrh **
636ec8fc62cSdrh ** This hack was added in the early days of SQLite in a misguided attempt
637ec8fc62cSdrh ** to be compatible with MySQL 3.x, which used double-quotes for strings.
638ec8fc62cSdrh ** I now sorely regret putting in this hack. The effect of this hack is
639ec8fc62cSdrh ** that misspelled identifier names are silently converted into strings
640ec8fc62cSdrh ** rather than causing an error, to the frustration of countless
641ec8fc62cSdrh ** programmers. To all those frustrated programmers, my apologies.
642ec8fc62cSdrh **
643ec8fc62cSdrh ** Someday, I hope to get rid of this hack. Unfortunately there is
644ec8fc62cSdrh ** a huge amount of legacy SQL that uses it. So for now, we just
645ec8fc62cSdrh ** issue a warning.
646ec8fc62cSdrh */
647ec8fc62cSdrh sqlite3_log(SQLITE_WARNING,
648ec8fc62cSdrh "double-quoted string literal: \"%w\"", zCol);
649893bd375Sdrh #ifdef SQLITE_ENABLE_NORMALIZE
650893bd375Sdrh sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
651893bd375Sdrh #endif
6527d10d5a6Sdrh pExpr->op = TK_STRING;
653477572b9Sdrh memset(&pExpr->y, 0, sizeof(pExpr->y));
654f7828b5cSdrh return WRC_Prune;
6557d10d5a6Sdrh }
656171d16bbSdrh if( sqlite3ExprIdToTrueFalse(pExpr) ){
657007c843bSdrh return WRC_Prune;
658007c843bSdrh }
659007c843bSdrh }
6607d10d5a6Sdrh
661bb3c62a7Sdrh /*
662bb3c62a7Sdrh ** cnt==0 means there was not match.
663bb3c62a7Sdrh ** cnt>1 means there were two or more matches.
664bb3c62a7Sdrh **
665bb3c62a7Sdrh ** cnt==0 is always an error. cnt>1 is often an error, but might
666bb3c62a7Sdrh ** be multiple matches for a NATURAL LEFT JOIN or a LEFT JOIN USING.
667bb3c62a7Sdrh */
668bb3c62a7Sdrh assert( pFJMatch==0 || cnt>0 );
669bdbda1ebSdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );
6707d10d5a6Sdrh if( cnt!=1 ){
6717d10d5a6Sdrh const char *zErr;
672bb3c62a7Sdrh if( pFJMatch ){
673bb3c62a7Sdrh if( pFJMatch->nExpr==cnt-1 ){
674977eef6cSdrh if( ExprHasProperty(pExpr,EP_Leaf) ){
675977eef6cSdrh ExprClearProperty(pExpr,EP_Leaf);
676977eef6cSdrh }else{
677bdbda1ebSdrh sqlite3ExprDelete(db, pExpr->pLeft);
678bdbda1ebSdrh pExpr->pLeft = 0;
679bdbda1ebSdrh sqlite3ExprDelete(db, pExpr->pRight);
680bdbda1ebSdrh pExpr->pRight = 0;
681bdbda1ebSdrh }
682bb3c62a7Sdrh extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
683bb3c62a7Sdrh pExpr->op = TK_FUNCTION;
684bb3c62a7Sdrh pExpr->u.zToken = "coalesce";
685bb3c62a7Sdrh pExpr->x.pList = pFJMatch;
686a3e2518bSdrh cnt = 1;
687bb3c62a7Sdrh goto lookupname_end;
688bb3c62a7Sdrh }else{
689bb3c62a7Sdrh sqlite3ExprListDelete(db, pFJMatch);
690bb3c62a7Sdrh pFJMatch = 0;
691bb3c62a7Sdrh }
692bb3c62a7Sdrh }
6937d10d5a6Sdrh zErr = cnt==0 ? "no such column" : "ambiguous column name";
6947d10d5a6Sdrh if( zDb ){
6957d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
6967d10d5a6Sdrh }else if( zTab ){
6977d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
6987d10d5a6Sdrh }else{
6997d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
7007d10d5a6Sdrh }
7014f77c920Sdrh sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
7021db95106Sdan pParse->checkSchema = 1;
703050611a7Sdrh pTopNC->nNcErr++;
7047d10d5a6Sdrh }
705bb3c62a7Sdrh assert( pFJMatch==0 );
7067d10d5a6Sdrh
707bdbda1ebSdrh /* Remove all substructure from pExpr */
708bdbda1ebSdrh if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
709bdbda1ebSdrh sqlite3ExprDelete(db, pExpr->pLeft);
710bdbda1ebSdrh pExpr->pLeft = 0;
711bdbda1ebSdrh sqlite3ExprDelete(db, pExpr->pRight);
712bdbda1ebSdrh pExpr->pRight = 0;
713bdbda1ebSdrh ExprSetProperty(pExpr, EP_Leaf);
714bdbda1ebSdrh }
715bdbda1ebSdrh
7167d10d5a6Sdrh /* If a column from a table in pSrcList is referenced, then record
7177d10d5a6Sdrh ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
718bf9d0996Sdrh ** bit 0 to be set. Column 1 sets bit 1. And so forth. Bit 63 is
719bf9d0996Sdrh ** set if the 63rd or any subsequent column is used.
720522ebfa7Sdrh **
721522ebfa7Sdrh ** The colUsed mask is an optimization used to help determine if an
722522ebfa7Sdrh ** index is a covering index. The correct answer is still obtained
723bf9d0996Sdrh ** if the mask contains extra set bits. However, it is important to
724bf9d0996Sdrh ** avoid setting bits beyond the maximum column number of the table.
725bf9d0996Sdrh ** (See ticket [b92e5e8ec2cdbaa1]).
726522ebfa7Sdrh **
727bf9d0996Sdrh ** If a generated column is referenced, set bits for every column
728bf9d0996Sdrh ** of the table.
7297d10d5a6Sdrh */
7302d2e7bd3Sdanielk1977 if( pExpr->iColumn>=0 && pMatch!=0 ){
73174a07986Sdrh pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
7320824d5b9Sdrh }
7337d10d5a6Sdrh
734eac9fabbSdrh pExpr->op = eNewExprOp;
735b7916a78Sdrh lookupname_end:
7367d10d5a6Sdrh if( cnt==1 ){
7377d10d5a6Sdrh assert( pNC!=0 );
7382d7a691fSdrh #ifndef SQLITE_OMIT_AUTHORIZATION
739552562c4Sdrh if( pParse->db->xAuth
74029f6a365Sdrh && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
741552562c4Sdrh ){
7427d10d5a6Sdrh sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
743a3a5bd9bSdrh }
7442d7a691fSdrh #endif
7457d10d5a6Sdrh /* Increment the nRef value on all name contexts from TopNC up to
7467d10d5a6Sdrh ** the point where the name matched. */
7477d10d5a6Sdrh for(;;){
7487d10d5a6Sdrh assert( pTopNC!=0 );
7497d10d5a6Sdrh pTopNC->nRef++;
7507d10d5a6Sdrh if( pTopNC==pNC ) break;
7517d10d5a6Sdrh pTopNC = pTopNC->pNext;
7527d10d5a6Sdrh }
753f7828b5cSdrh return WRC_Prune;
7547d10d5a6Sdrh } else {
755f7828b5cSdrh return WRC_Abort;
7567d10d5a6Sdrh }
7577d10d5a6Sdrh }
7587d10d5a6Sdrh
7597d10d5a6Sdrh /*
760f7b0b0adSdan ** Allocate and return a pointer to an expression to load the column iCol
7619e48165bSdrh ** from datasource iSrc in SrcList pSrc.
762f7b0b0adSdan */
sqlite3CreateColumnExpr(sqlite3 * db,SrcList * pSrc,int iSrc,int iCol)763f7b0b0adSdan Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
764f7b0b0adSdan Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
765f7b0b0adSdan if( p ){
7667601294aSdrh SrcItem *pItem = &pSrc->a[iSrc];
767477572b9Sdrh Table *pTab;
768477572b9Sdrh assert( ExprUseYTab(p) );
769477572b9Sdrh pTab = p->y.pTab = pItem->pTab;
770f7b0b0adSdan p->iTable = pItem->iCursor;
771eda079cdSdrh if( p->y.pTab->iPKey==iCol ){
772f7b0b0adSdan p->iColumn = -1;
773f7b0b0adSdan }else{
7748677d308Sdrh p->iColumn = (ynVar)iCol;
7750824d5b9Sdrh if( (pTab->tabFlags & TF_HasGenerated)!=0
7760824d5b9Sdrh && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
7770824d5b9Sdrh ){
778926f796eSdrh testcase( pTab->nCol==63 );
779926f796eSdrh testcase( pTab->nCol==64 );
7800824d5b9Sdrh pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
781926f796eSdrh }else{
7827caba669Sdrh testcase( iCol==BMS );
7837caba669Sdrh testcase( iCol==BMS-1 );
784f7b0b0adSdan pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
785f7b0b0adSdan }
786f7b0b0adSdan }
787926f796eSdrh }
788f7b0b0adSdan return p;
789f7b0b0adSdan }
790f7b0b0adSdan
791f7b0b0adSdan /*
792a514b8ebSdrh ** Report an error that an expression is not valid for some set of
79303bf26d9Sdrh ** pNC->ncFlags values determined by validMask.
79477318a3cSdrh **
79577318a3cSdrh ** static void notValid(
79677318a3cSdrh ** Parse *pParse, // Leave error message here
79777318a3cSdrh ** NameContext *pNC, // The name context
79877318a3cSdrh ** const char *zMsg, // Type of error
79977318a3cSdrh ** int validMask, // Set of contexts for which prohibited
80077318a3cSdrh ** Expr *pExpr // Invalidate this expression on error
80177318a3cSdrh ** ){...}
80277318a3cSdrh **
80377318a3cSdrh ** As an optimization, since the conditional is almost always false
80477318a3cSdrh ** (because errors are rare), the conditional is moved outside of the
80577318a3cSdrh ** function call using a macro.
8063780be11Sdrh */
notValidImpl(Parse * pParse,NameContext * pNC,const char * zMsg,Expr * pExpr,Expr * pError)80777318a3cSdrh static void notValidImpl(
8083780be11Sdrh Parse *pParse, /* Leave error message here */
8093780be11Sdrh NameContext *pNC, /* The name context */
810a514b8ebSdrh const char *zMsg, /* Type of error */
81162fc069eSdrh Expr *pExpr, /* Invalidate this expression on error */
81262fc069eSdrh Expr *pError /* Associate error with this expression */
8133780be11Sdrh ){
814a514b8ebSdrh const char *zIn = "partial index WHERE clauses";
815a514b8ebSdrh if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
8163780be11Sdrh #ifndef SQLITE_OMIT_CHECK
817a514b8ebSdrh else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
8183780be11Sdrh #endif
81981f7b372Sdrh #ifndef SQLITE_OMIT_GENERATED_COLUMNS
8206ab61d70Sdrh else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
82181f7b372Sdrh #endif
822a514b8ebSdrh sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
82377318a3cSdrh if( pExpr ) pExpr->op = TK_NULL;
82462fc069eSdrh sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
825a514b8ebSdrh }
82662fc069eSdrh #define sqlite3ResolveNotValid(P,N,M,X,E,R) \
82777318a3cSdrh assert( ((X)&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol))==0 ); \
82862fc069eSdrh if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
8293780be11Sdrh
830cca9f3d2Sdrh /*
831cca9f3d2Sdrh ** Expression p should encode a floating point value between 1.0 and 0.0.
832cca9f3d2Sdrh ** Return 1024 times this value. Or return -1 if p is not a floating point
833cca9f3d2Sdrh ** value between 1.0 and 0.0.
834cca9f3d2Sdrh */
exprProbability(Expr * p)835cca9f3d2Sdrh static int exprProbability(Expr *p){
836cca9f3d2Sdrh double r = -1.0;
837cca9f3d2Sdrh if( p->op!=TK_FLOAT ) return -1;
838f9751074Sdrh assert( !ExprHasProperty(p, EP_IntValue) );
839cca9f3d2Sdrh sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
84009328c00Sdrh assert( r>=0.0 );
84109328c00Sdrh if( r>1.0 ) return -1;
842d05ab6aaSdrh return (int)(r*134217728.0);
843cca9f3d2Sdrh }
8443780be11Sdrh
8453780be11Sdrh /*
8467d10d5a6Sdrh ** This routine is callback for sqlite3WalkExpr().
8477d10d5a6Sdrh **
8487d10d5a6Sdrh ** Resolve symbolic names into TK_COLUMN operators for the current
8497d10d5a6Sdrh ** node in the expression tree. Return 0 to continue the search down
8507d10d5a6Sdrh ** the tree or 2 to abort the tree walk.
8517d10d5a6Sdrh **
8527d10d5a6Sdrh ** This routine also does error checking and name resolution for
8537d10d5a6Sdrh ** function names. The operator for aggregate functions is changed
8547d10d5a6Sdrh ** to TK_AGG_FUNCTION.
8557d10d5a6Sdrh */
resolveExprStep(Walker * pWalker,Expr * pExpr)8567d10d5a6Sdrh static int resolveExprStep(Walker *pWalker, Expr *pExpr){
8577d10d5a6Sdrh NameContext *pNC;
8587d10d5a6Sdrh Parse *pParse;
8597d10d5a6Sdrh
8607d10d5a6Sdrh pNC = pWalker->u.pNC;
8617d10d5a6Sdrh assert( pNC!=0 );
8627d10d5a6Sdrh pParse = pNC->pParse;
8637d10d5a6Sdrh assert( pParse==pWalker->pParse );
8647d10d5a6Sdrh
8657d10d5a6Sdrh #ifndef NDEBUG
8667d10d5a6Sdrh if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
8677d10d5a6Sdrh SrcList *pSrcList = pNC->pSrcList;
8687d10d5a6Sdrh int i;
8697d10d5a6Sdrh for(i=0; i<pNC->pSrcList->nSrc; i++){
8707d10d5a6Sdrh assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
8717d10d5a6Sdrh }
8727d10d5a6Sdrh }
8737d10d5a6Sdrh #endif
8747d10d5a6Sdrh switch( pExpr->op ){
87541204f1fSdrh
87641204f1fSdrh /* The special operator TK_ROW means use the rowid for the first
87741204f1fSdrh ** column in the FROM clause. This is used by the LIMIT and ORDER BY
878be952c11Sdan ** clause processing on UPDATE and DELETE statements, and by
879be952c11Sdan ** UPDATE ... FROM statement processing.
88041204f1fSdrh */
88141204f1fSdrh case TK_ROW: {
88241204f1fSdrh SrcList *pSrcList = pNC->pSrcList;
8837601294aSdrh SrcItem *pItem;
884f2972b60Sdan assert( pSrcList && pSrcList->nSrc>=1 );
88541204f1fSdrh pItem = pSrcList->a;
88641204f1fSdrh pExpr->op = TK_COLUMN;
887477572b9Sdrh assert( ExprUseYTab(pExpr) );
888eda079cdSdrh pExpr->y.pTab = pItem->pTab;
88941204f1fSdrh pExpr->iTable = pItem->iCursor;
890576d5a86Sdan pExpr->iColumn--;
891af97c3f3Sdrh pExpr->affExpr = SQLITE_AFF_INTEGER;
89241204f1fSdrh break;
89341204f1fSdrh }
89441204f1fSdrh
89545e4cb8eSdrh /* An optimization: Attempt to convert
8968ddf6862Sdan **
89745e4cb8eSdrh ** "expr IS NOT NULL" --> "TRUE"
89845e4cb8eSdrh ** "expr IS NULL" --> "FALSE"
89945e4cb8eSdrh **
90045e4cb8eSdrh ** if we can prove that "expr" is never NULL. Call this the
90145e4cb8eSdrh ** "NOT NULL strength reduction optimization".
90245e4cb8eSdrh **
90345e4cb8eSdrh ** If this optimization occurs, also restore the NameContext ref-counts
90445e4cb8eSdrh ** to the state they where in before the "column" LHS expression was
90545e4cb8eSdrh ** resolved. This prevents "column" from being counted as having been
90645e4cb8eSdrh ** referenced, which might prevent a SELECT from being erroneously
90745e4cb8eSdrh ** marked as correlated.
9088ddf6862Sdan */
9098ddf6862Sdan case TK_NOTNULL:
9108ddf6862Sdan case TK_ISNULL: {
9118ddf6862Sdan int anRef[8];
9128ddf6862Sdan NameContext *p;
9138ddf6862Sdan int i;
9148ddf6862Sdan for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
9158ddf6862Sdan anRef[i] = p->nRef;
9168ddf6862Sdan }
9178ddf6862Sdan sqlite3WalkExpr(pWalker, pExpr->pLeft);
9183083d5f5Sdan if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
91967a99dbeSdrh testcase( ExprHasProperty(pExpr, EP_OuterON) );
920f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue) );
9218ddf6862Sdan if( pExpr->op==TK_NOTNULL ){
9228ddf6862Sdan pExpr->u.zToken = "true";
9238ddf6862Sdan ExprSetProperty(pExpr, EP_IsTrue);
9248ddf6862Sdan }else{
9258ddf6862Sdan pExpr->u.zToken = "false";
9268ddf6862Sdan ExprSetProperty(pExpr, EP_IsFalse);
9278ddf6862Sdan }
9288ddf6862Sdan pExpr->op = TK_TRUEFALSE;
9298ddf6862Sdan for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
9308ddf6862Sdan p->nRef = anRef[i];
9318ddf6862Sdan }
9328ddf6862Sdan sqlite3ExprDelete(pParse->db, pExpr->pLeft);
9338ddf6862Sdan pExpr->pLeft = 0;
9348ddf6862Sdan }
9358ddf6862Sdan return WRC_Prune;
9368ddf6862Sdan }
9378ddf6862Sdan
9383cf48e3eSdrh /* A column name: ID
9393cf48e3eSdrh ** Or table name and column name: ID.ID
9407d10d5a6Sdrh ** Or a database, table and column: ID.ID.ID
9413cf48e3eSdrh **
9423cf48e3eSdrh ** The TK_ID and TK_OUT cases are combined so that there will only
9433cf48e3eSdrh ** be one call to lookupName(). Then the compiler will in-line
9443cf48e3eSdrh ** lookupName() for a size reduction and performance increase.
9457d10d5a6Sdrh */
9463cf48e3eSdrh case TK_ID:
9477d10d5a6Sdrh case TK_DOT: {
948b7916a78Sdrh const char *zColumn;
949b7916a78Sdrh const char *zTable;
950b7916a78Sdrh const char *zDb;
9517d10d5a6Sdrh Expr *pRight;
9527d10d5a6Sdrh
9533cf48e3eSdrh if( pExpr->op==TK_ID ){
9543cf48e3eSdrh zDb = 0;
9553cf48e3eSdrh zTable = 0;
956f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue) );
9573cf48e3eSdrh zColumn = pExpr->u.zToken;
9583cf48e3eSdrh }else{
959c9461eccSdan Expr *pLeft = pExpr->pLeft;
96077318a3cSdrh testcase( pNC->ncFlags & NC_IdxExpr );
96177318a3cSdrh testcase( pNC->ncFlags & NC_GenCol );
96277318a3cSdrh sqlite3ResolveNotValid(pParse, pNC, "the \".\" operator",
96362fc069eSdrh NC_IdxExpr|NC_GenCol, 0, pExpr);
9647d10d5a6Sdrh pRight = pExpr->pRight;
9657d10d5a6Sdrh if( pRight->op==TK_ID ){
966b7916a78Sdrh zDb = 0;
9677d10d5a6Sdrh }else{
9687d10d5a6Sdrh assert( pRight->op==TK_DOT );
969f9751074Sdrh assert( !ExprHasProperty(pRight, EP_IntValue) );
970c9461eccSdan zDb = pLeft->u.zToken;
971c9461eccSdan pLeft = pRight->pLeft;
972cf8f2895Sdan pRight = pRight->pRight;
973cf8f2895Sdan }
974477572b9Sdrh assert( ExprUseUToken(pLeft) && ExprUseUToken(pRight) );
975c9461eccSdan zTable = pLeft->u.zToken;
976cf8f2895Sdan zColumn = pRight->u.zToken;
977477572b9Sdrh assert( ExprUseYTab(pExpr) );
978c9461eccSdan if( IN_RENAME_OBJECT ){
97907e95233Sdan sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
980eda079cdSdrh sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
981c9461eccSdan }
9823cf48e3eSdrh }
983f7828b5cSdrh return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
9847d10d5a6Sdrh }
9857d10d5a6Sdrh
9867d10d5a6Sdrh /* Resolve function names
9877d10d5a6Sdrh */
9887d10d5a6Sdrh case TK_FUNCTION: {
9896ab3a2ecSdanielk1977 ExprList *pList = pExpr->x.pList; /* The argument list */
9907d10d5a6Sdrh int n = pList ? pList->nExpr : 0; /* Number of arguments */
9917d10d5a6Sdrh int no_such_func = 0; /* True if no such function exists */
9927d10d5a6Sdrh int wrong_num_args = 0; /* True if wrong number of arguments */
9937d10d5a6Sdrh int is_agg = 0; /* True if is an aggregate function */
9947d10d5a6Sdrh const char *zId; /* The function name. */
9957d10d5a6Sdrh FuncDef *pDef; /* Information about the function */
996ea678832Sdrh u8 enc = ENC(pParse->db); /* The database encoding */
9974ded26a5Sdan int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
9984f9adee2Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
9994f9adee2Sdan Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
10004f9adee2Sdan #endif
1001f9751074Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect|EP_IntValue) );
100233e619fcSdrh zId = pExpr->u.zToken;
100380738d9cSdrh pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
10047d10d5a6Sdrh if( pDef==0 ){
100580738d9cSdrh pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
10067d10d5a6Sdrh if( pDef==0 ){
10077d10d5a6Sdrh no_such_func = 1;
10087d10d5a6Sdrh }else{
10097d10d5a6Sdrh wrong_num_args = 1;
10107d10d5a6Sdrh }
10117d10d5a6Sdrh }else{
10122d80151fSdrh is_agg = pDef->xFinalize!=0;
1013cca9f3d2Sdrh if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
1014a7d6db6aSdrh ExprSetProperty(pExpr, EP_Unlikely);
1015cca9f3d2Sdrh if( n==2 ){
1016cca9f3d2Sdrh pExpr->iTable = exprProbability(pList->a[1].pExpr);
1017cca9f3d2Sdrh if( pExpr->iTable<0 ){
101831e7147dSdrh sqlite3ErrorMsg(pParse,
101962fc069eSdrh "second argument to %#T() must be a "
102062fc069eSdrh "constant between 0.0 and 1.0", pExpr);
1021050611a7Sdrh pNC->nNcErr++;
1022cca9f3d2Sdrh }
1023cca9f3d2Sdrh }else{
102431e7147dSdrh /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
102531e7147dSdrh ** equivalent to likelihood(X, 0.0625).
102631e7147dSdrh ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
102731e7147dSdrh ** short-hand for likelihood(X,0.0625).
102831e7147dSdrh ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
102931e7147dSdrh ** for likelihood(X,0.9375).
103031e7147dSdrh ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
103131e7147dSdrh ** to likelihood(X,0.9375). */
103203202a97Sdrh /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
1033d05ab6aaSdrh pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
1034cca9f3d2Sdrh }
1035cca9f3d2Sdrh }
10367d10d5a6Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION
1037a0daa751Sdrh {
1038a0daa751Sdrh int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
10397d10d5a6Sdrh if( auth!=SQLITE_OK ){
10407d10d5a6Sdrh if( auth==SQLITE_DENY ){
104162fc069eSdrh sqlite3ErrorMsg(pParse, "not authorized to use function: %#T",
104262fc069eSdrh pExpr);
1043050611a7Sdrh pNC->nNcErr++;
10447d10d5a6Sdrh }
10457d10d5a6Sdrh pExpr->op = TK_NULL;
10467d10d5a6Sdrh return WRC_Prune;
10477d10d5a6Sdrh }
1048a0daa751Sdrh }
10499588ad95Sdrh #endif
1050a7f910b5Sdrh if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
10511d85e405Sdrh /* For the purposes of the EP_ConstFunc flag, date and time
105203bf26d9Sdrh ** functions and other functions that change slowly are considered
105320cee7d0Sdrh ** constant because they are constant for the duration of one query.
105420cee7d0Sdrh ** This allows them to be factored out of inner loops. */
105563f84573Sdrh ExprSetProperty(pExpr,EP_ConstFunc);
10561d85e405Sdrh }
10571d85e405Sdrh if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
1058fe704604Sdrh /* Clearly non-deterministic functions like random(), but also
1059fe704604Sdrh ** date/time functions that use 'now', and other functions like
1060a7f910b5Sdrh ** sqlite_version() that might change over time cannot be used
1061fe704604Sdrh ** in an index or generated column. Curiously, they can be used
1062fe704604Sdrh ** in a CHECK constraint. SQLServer, MySQL, and PostgreSQL all
1063fe704604Sdrh ** all this. */
106477318a3cSdrh sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
106562fc069eSdrh NC_IdxExpr|NC_PartIdx|NC_GenCol, 0, pExpr);
106620cee7d0Sdrh }else{
106720cee7d0Sdrh assert( (NC_SelfRef & 0xff)==NC_SelfRef ); /* Must fit in 8 bits */
106820cee7d0Sdrh pExpr->op2 = pNC->ncFlags & NC_SelfRef;
106905b32ee3Sdrh if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
107031e7147dSdrh }
1071eea8eb6dSdrh if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
1072eea8eb6dSdrh && pParse->nested==0
1073171c50ecSdrh && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
1074eea8eb6dSdrh ){
1075eea8eb6dSdrh /* Internal-use-only functions are disallowed unless the
10760dfa5255Sdrh ** SQL is being compiled using sqlite3NestedParse() or
10770dfa5255Sdrh ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
10787f1c1119Sdrh ** used to activate internal functions for testing purposes */
1079eea8eb6dSdrh no_such_func = 1;
1080eea8eb6dSdrh pDef = 0;
10812eeca204Sdrh }else
10822eeca204Sdrh if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
10832eeca204Sdrh && !IN_RENAME_OBJECT
10842eeca204Sdrh ){
10852eeca204Sdrh sqlite3ExprFunctionUsable(pParse, pExpr, pDef);
10867d10d5a6Sdrh }
1087c4ad8499Sdrh }
108826522d1cSdan
1089c9461eccSdan if( 0==IN_RENAME_OBJECT ){
109067a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC
10917262ca94Sdan assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
10927262ca94Sdan || (pDef->xValue==0 && pDef->xInverse==0)
10937262ca94Sdan || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
10947262ca94Sdan );
10958117f113Sdan if( pDef && pDef->xValue==0 && pWin ){
109626522d1cSdan sqlite3ErrorMsg(pParse,
109762fc069eSdrh "%#T() may not be used as a window function", pExpr
109826522d1cSdan );
1099050611a7Sdrh pNC->nNcErr++;
110026522d1cSdan }else if(
110126522d1cSdan (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
11028117f113Sdan || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
11038117f113Sdan || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
110486fb6e17Sdan ){
110526522d1cSdan const char *zType;
11068117f113Sdan if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
110726522d1cSdan zType = "window";
110826522d1cSdan }else{
110926522d1cSdan zType = "aggregate";
111026522d1cSdan }
111162fc069eSdrh sqlite3ErrorMsg(pParse, "misuse of %s function %#T()",zType,pExpr);
1112050611a7Sdrh pNC->nNcErr++;
11137d10d5a6Sdrh is_agg = 0;
1114c3163073Sdan }
1115c3163073Sdan #else
1116c3163073Sdan if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
111762fc069eSdrh sqlite3ErrorMsg(pParse,"misuse of aggregate function %#T()",pExpr);
1118050611a7Sdrh pNC->nNcErr++;
1119c3163073Sdan is_agg = 0;
1120c3163073Sdan }
1121c3163073Sdan #endif
11223a843f52Sdrh else if( no_such_func && pParse->db->init.busy==0
1123cc15313cSdrh #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
1124cc15313cSdrh && pParse->explain==0
1125cc15313cSdrh #endif
1126cc15313cSdrh ){
112762fc069eSdrh sqlite3ErrorMsg(pParse, "no such function: %#T", pExpr);
1128050611a7Sdrh pNC->nNcErr++;
11297d10d5a6Sdrh }else if( wrong_num_args ){
113062fc069eSdrh sqlite3ErrorMsg(pParse,"wrong number of arguments to function %#T()",
113162fc069eSdrh pExpr);
1132050611a7Sdrh pNC->nNcErr++;
11337d10d5a6Sdrh }
11345e61c1b7Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
11355e61c1b7Sdan else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
11365e61c1b7Sdan sqlite3ErrorMsg(pParse,
113762fc069eSdrh "FILTER may not be used with non-aggregate %#T()",
113862fc069eSdrh pExpr
11395e61c1b7Sdan );
1140050611a7Sdrh pNC->nNcErr++;
11415e61c1b7Sdan }
11425e61c1b7Sdan #endif
1143c3163073Sdan if( is_agg ){
11444ded26a5Sdan /* Window functions may not be arguments of aggregate functions.
11454ded26a5Sdan ** Or arguments of other window functions. But aggregate functions
11464ded26a5Sdan ** may be arguments for window functions. */
114734a7d790Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
11488117f113Sdan pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
114934a7d790Sdan #else
115034a7d790Sdan pNC->ncFlags &= ~NC_AllowAgg;
115134a7d790Sdan #endif
1152c3163073Sdan }
1153dabc268fSdan }
11549bf022d5Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
11551e60261cSdan else if( ExprHasProperty(pExpr, EP_WinFunc) ){
11569bf022d5Sdan is_agg = 1;
11579bf022d5Sdan }
11589bf022d5Sdan #endif
11597d10d5a6Sdrh sqlite3WalkExprList(pWalker, pList);
1160030796dfSdrh if( is_agg ){
116167a9b8edSdan #ifndef SQLITE_OMIT_WINDOWFUNC
11624f9adee2Sdan if( pWin ){
1163e3bf632cSdan Select *pSel = pNC->pWinSelect;
1164477572b9Sdrh assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
1165490e6f25Sdan if( IN_RENAME_OBJECT==0 ){
11662e2c8819Sdrh sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
1167e8dd6a4eSdan if( pParse->db->mallocFailed ) break;
1168490e6f25Sdan }
11694f9adee2Sdan sqlite3WalkExprList(pWalker, pWin->pPartition);
11704f9adee2Sdan sqlite3WalkExprList(pWalker, pWin->pOrderBy);
11714f9adee2Sdan sqlite3WalkExpr(pWalker, pWin->pFilter);
1172a3fcc000Sdan sqlite3WindowLink(pSel, pWin);
11734ded26a5Sdan pNC->ncFlags |= NC_HasWin;
117467a9b8edSdan }else
117567a9b8edSdan #endif /* SQLITE_OMIT_WINDOWFUNC */
117667a9b8edSdan {
117790cf38beSdrh NameContext *pNC2; /* For looping up thru outer contexts */
1178030796dfSdrh pExpr->op = TK_AGG_FUNCTION;
1179030796dfSdrh pExpr->op2 = 0;
11806ba7ab0dSdan #ifndef SQLITE_OMIT_WINDOWFUNC
11814f9adee2Sdan if( ExprHasProperty(pExpr, EP_WinFunc) ){
11824f9adee2Sdan sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
11834f9adee2Sdan }
11846ba7ab0dSdan #endif
118590cf38beSdrh pNC2 = pNC;
118690cf38beSdrh while( pNC2
118790cf38beSdrh && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
118890cf38beSdrh ){
1189030796dfSdrh pExpr->op2++;
1190030796dfSdrh pNC2 = pNC2->pNext;
1191030796dfSdrh }
119272d1eac6Sdan assert( pDef!=0 || IN_RENAME_OBJECT );
119372d1eac6Sdan if( pNC2 && pDef ){
11949588ad95Sdrh assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
1195bb301231Sdrh assert( SQLITE_FUNC_ANYORDER==NC_OrderAgg );
11969588ad95Sdrh testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
1197bb301231Sdrh testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
1198bb301231Sdrh pNC2->ncFlags |= NC_HasAgg
1199bb301231Sdrh | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
1200bb301231Sdrh & (SQLITE_FUNC_MINMAX|SQLITE_FUNC_ANYORDER));
12019588ad95Sdrh }
1202030796dfSdrh }
12034ded26a5Sdan pNC->ncFlags |= savedAllowFlags;
1204c3163073Sdan }
12057d10d5a6Sdrh /* FIX ME: Compute pExpr->affinity based on the expected return
12067d10d5a6Sdrh ** type of the function
12077d10d5a6Sdrh */
12087d10d5a6Sdrh return WRC_Prune;
12097d10d5a6Sdrh }
12107d10d5a6Sdrh #ifndef SQLITE_OMIT_SUBQUERY
12117d10d5a6Sdrh case TK_SELECT:
121273c0fdc7Sdrh case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
12137d10d5a6Sdrh #endif
12147d10d5a6Sdrh case TK_IN: {
121573c0fdc7Sdrh testcase( pExpr->op==TK_IN );
1216a4eeccdfSdrh if( ExprUseXSelect(pExpr) ){
12177d10d5a6Sdrh int nRef = pNC->nRef;
121877318a3cSdrh testcase( pNC->ncFlags & NC_IsCheck );
121977318a3cSdrh testcase( pNC->ncFlags & NC_PartIdx );
122077318a3cSdrh testcase( pNC->ncFlags & NC_IdxExpr );
122177318a3cSdrh testcase( pNC->ncFlags & NC_GenCol );
122284d90319Sdrh if( pNC->ncFlags & NC_SelfRef ){
122362fc069eSdrh notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr);
122484d90319Sdrh }else{
12256ab3a2ecSdanielk1977 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
122684d90319Sdrh }
12277d10d5a6Sdrh assert( pNC->nRef>=nRef );
12287d10d5a6Sdrh if( nRef!=pNC->nRef ){
12297d10d5a6Sdrh ExprSetProperty(pExpr, EP_VarSelect);
123091da7072Sdrh pNC->ncFlags |= NC_VarSelect;
12317d10d5a6Sdrh }
12327d10d5a6Sdrh }
12337d10d5a6Sdrh break;
12347d10d5a6Sdrh }
12357d10d5a6Sdrh case TK_VARIABLE: {
123677318a3cSdrh testcase( pNC->ncFlags & NC_IsCheck );
123777318a3cSdrh testcase( pNC->ncFlags & NC_PartIdx );
123877318a3cSdrh testcase( pNC->ncFlags & NC_IdxExpr );
123977318a3cSdrh testcase( pNC->ncFlags & NC_GenCol );
124077318a3cSdrh sqlite3ResolveNotValid(pParse, pNC, "parameters",
124162fc069eSdrh NC_IsCheck|NC_PartIdx|NC_IdxExpr|NC_GenCol, pExpr, pExpr);
12427d10d5a6Sdrh break;
12437d10d5a6Sdrh }
1244007c843bSdrh case TK_IS:
12458abed7b9Sdrh case TK_ISNOT: {
12460d950af3Sdrh Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
12478abed7b9Sdrh assert( !ExprHasProperty(pExpr, EP_Reduced) );
12488abed7b9Sdrh /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
12498abed7b9Sdrh ** and "x IS NOT FALSE". */
125091be05a9Sdrh if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
12518abed7b9Sdrh int rc = resolveExprStep(pWalker, pRight);
1252007c843bSdrh if( rc==WRC_Abort ) return WRC_Abort;
12538abed7b9Sdrh if( pRight->op==TK_TRUEFALSE ){
12548abed7b9Sdrh pExpr->op2 = pExpr->op;
12558abed7b9Sdrh pExpr->op = TK_TRUTH;
1256007c843bSdrh return WRC_Continue;
1257007c843bSdrh }
1258007c843bSdrh }
125908b92086Sdrh /* no break */ deliberate_fall_through
12608abed7b9Sdrh }
12613bafdedeSdan case TK_BETWEEN:
1262b29e60c4Sdrh case TK_EQ:
1263b29e60c4Sdrh case TK_NE:
1264b29e60c4Sdrh case TK_LT:
1265b29e60c4Sdrh case TK_LE:
1266b29e60c4Sdrh case TK_GT:
12678abed7b9Sdrh case TK_GE: {
1268b29e60c4Sdrh int nLeft, nRight;
1269b29e60c4Sdrh if( pParse->db->mallocFailed ) break;
1270b29e60c4Sdrh assert( pExpr->pLeft!=0 );
1271b29e60c4Sdrh nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
12723bafdedeSdan if( pExpr->op==TK_BETWEEN ){
1273a4eeccdfSdrh assert( ExprUseXList(pExpr) );
12743bafdedeSdan nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
12753bafdedeSdan if( nRight==nLeft ){
12763bafdedeSdan nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
12773bafdedeSdan }
12783bafdedeSdan }else{
12793bafdedeSdan assert( pExpr->pRight!=0 );
1280b29e60c4Sdrh nRight = sqlite3ExprVectorSize(pExpr->pRight);
12813bafdedeSdan }
1282b29e60c4Sdrh if( nLeft!=nRight ){
1283b29e60c4Sdrh testcase( pExpr->op==TK_EQ );
1284b29e60c4Sdrh testcase( pExpr->op==TK_NE );
1285b29e60c4Sdrh testcase( pExpr->op==TK_LT );
1286b29e60c4Sdrh testcase( pExpr->op==TK_LE );
1287b29e60c4Sdrh testcase( pExpr->op==TK_GT );
1288b29e60c4Sdrh testcase( pExpr->op==TK_GE );
1289b29e60c4Sdrh testcase( pExpr->op==TK_IS );
1290b29e60c4Sdrh testcase( pExpr->op==TK_ISNOT );
12913bafdedeSdan testcase( pExpr->op==TK_BETWEEN );
1292b29e60c4Sdrh sqlite3ErrorMsg(pParse, "row value misused");
129362fc069eSdrh sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
1294b29e60c4Sdrh }
1295b29e60c4Sdrh break;
1296b29e60c4Sdrh }
12977d10d5a6Sdrh }
12980c7d3d39Sdrh assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
12990c7d3d39Sdrh return pParse->nErr ? WRC_Abort : WRC_Continue;
13007d10d5a6Sdrh }
13017d10d5a6Sdrh
13027d10d5a6Sdrh /*
13037d10d5a6Sdrh ** pEList is a list of expressions which are really the result set of the
13047d10d5a6Sdrh ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
13057d10d5a6Sdrh ** This routine checks to see if pE is a simple identifier which corresponds
13067d10d5a6Sdrh ** to the AS-name of one of the terms of the expression list. If it is,
13077d10d5a6Sdrh ** this routine return an integer between 1 and N where N is the number of
13087d10d5a6Sdrh ** elements in pEList, corresponding to the matching entry. If there is
13097d10d5a6Sdrh ** no match, or if pE is not a simple identifier, then this routine
13107d10d5a6Sdrh ** return 0.
13117d10d5a6Sdrh **
13127d10d5a6Sdrh ** pEList has been resolved. pE has not.
13137d10d5a6Sdrh */
resolveAsName(Parse * pParse,ExprList * pEList,Expr * pE)13147d10d5a6Sdrh static int resolveAsName(
13157d10d5a6Sdrh Parse *pParse, /* Parsing context for error messages */
13167d10d5a6Sdrh ExprList *pEList, /* List of expressions to scan */
13177d10d5a6Sdrh Expr *pE /* Expression we are trying to match */
13187d10d5a6Sdrh ){
13197d10d5a6Sdrh int i; /* Loop counter */
13207d10d5a6Sdrh
1321cf697396Sshane UNUSED_PARAMETER(pParse);
1322cf697396Sshane
132373c0fdc7Sdrh if( pE->op==TK_ID ){
1324f9751074Sdrh const char *zCol;
1325f9751074Sdrh assert( !ExprHasProperty(pE, EP_IntValue) );
1326f9751074Sdrh zCol = pE->u.zToken;
13277d10d5a6Sdrh for(i=0; i<pEList->nExpr; i++){
1328d88fd539Sdrh if( pEList->a[i].fg.eEName==ENAME_NAME
1329607dd6e6Sdan && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
1330c4938ea2Sdrh ){
13317d10d5a6Sdrh return i+1;
13327d10d5a6Sdrh }
13337d10d5a6Sdrh }
13347d10d5a6Sdrh }
13357d10d5a6Sdrh return 0;
13367d10d5a6Sdrh }
13377d10d5a6Sdrh
13387d10d5a6Sdrh /*
13397d10d5a6Sdrh ** pE is a pointer to an expression which is a single term in the
13407d10d5a6Sdrh ** ORDER BY of a compound SELECT. The expression has not been
13417d10d5a6Sdrh ** name resolved.
13427d10d5a6Sdrh **
13437d10d5a6Sdrh ** At the point this routine is called, we already know that the
13447d10d5a6Sdrh ** ORDER BY term is not an integer index into the result set. That
13457d10d5a6Sdrh ** case is handled by the calling routine.
13467d10d5a6Sdrh **
13477d10d5a6Sdrh ** Attempt to match pE against result set columns in the left-most
13487d10d5a6Sdrh ** SELECT statement. Return the index i of the matching column,
13497d10d5a6Sdrh ** as an indication to the caller that it should sort by the i-th column.
13507d10d5a6Sdrh ** The left-most column is 1. In other words, the value returned is the
13517d10d5a6Sdrh ** same integer value that would be used in the SQL statement to indicate
13527d10d5a6Sdrh ** the column.
13537d10d5a6Sdrh **
13547d10d5a6Sdrh ** If there is no match, return 0. Return -1 if an error occurs.
13557d10d5a6Sdrh */
resolveOrderByTermToExprList(Parse * pParse,Select * pSelect,Expr * pE)13567d10d5a6Sdrh static int resolveOrderByTermToExprList(
13577d10d5a6Sdrh Parse *pParse, /* Parsing context for error messages */
13587d10d5a6Sdrh Select *pSelect, /* The SELECT statement with the ORDER BY clause */
13597d10d5a6Sdrh Expr *pE /* The specific ORDER BY term */
13607d10d5a6Sdrh ){
13617d10d5a6Sdrh int i; /* Loop counter */
13627d10d5a6Sdrh ExprList *pEList; /* The columns of the result set */
13637d10d5a6Sdrh NameContext nc; /* Name context for resolving pE */
1364a7564663Sdrh sqlite3 *db; /* Database connection */
1365a7564663Sdrh int rc; /* Return code from subprocedures */
1366a7564663Sdrh u8 savedSuppErr; /* Saved value of db->suppressErr */
13677d10d5a6Sdrh
13687d10d5a6Sdrh assert( sqlite3ExprIsInteger(pE, &i)==0 );
13697d10d5a6Sdrh pEList = pSelect->pEList;
13707d10d5a6Sdrh
13717d10d5a6Sdrh /* Resolve all names in the ORDER BY term expression
13727d10d5a6Sdrh */
13737d10d5a6Sdrh memset(&nc, 0, sizeof(nc));
13747d10d5a6Sdrh nc.pParse = pParse;
13757d10d5a6Sdrh nc.pSrcList = pSelect->pSrc;
137625c3b8caSdrh nc.uNC.pEList = pEList;
1377d487e373Sdan nc.ncFlags = NC_AllowAgg|NC_UEList|NC_NoSelect;
1378050611a7Sdrh nc.nNcErr = 0;
1379a7564663Sdrh db = pParse->db;
1380a7564663Sdrh savedSuppErr = db->suppressErr;
1381b56a0907Sdan db->suppressErr = 1;
1382a7564663Sdrh rc = sqlite3ResolveExprNames(&nc, pE);
1383a7564663Sdrh db->suppressErr = savedSuppErr;
1384a7564663Sdrh if( rc ) return 0;
13857d10d5a6Sdrh
13867d10d5a6Sdrh /* Try to match the ORDER BY expression against an expression
13877d10d5a6Sdrh ** in the result set. Return an 1-based index of the matching
13887d10d5a6Sdrh ** result-set entry.
13897d10d5a6Sdrh */
13907d10d5a6Sdrh for(i=0; i<pEList->nExpr; i++){
13915aa550cfSdan if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
13927d10d5a6Sdrh return i+1;
13937d10d5a6Sdrh }
13947d10d5a6Sdrh }
13957d10d5a6Sdrh
13967d10d5a6Sdrh /* If no match, return 0. */
13977d10d5a6Sdrh return 0;
13987d10d5a6Sdrh }
13997d10d5a6Sdrh
14007d10d5a6Sdrh /*
14017d10d5a6Sdrh ** Generate an ORDER BY or GROUP BY term out-of-range error.
14027d10d5a6Sdrh */
resolveOutOfRangeError(Parse * pParse,const char * zType,int i,int mx,Expr * pError)14037d10d5a6Sdrh static void resolveOutOfRangeError(
14047d10d5a6Sdrh Parse *pParse, /* The error context into which to write the error */
14057d10d5a6Sdrh const char *zType, /* "ORDER" or "GROUP" */
14067d10d5a6Sdrh int i, /* The index (1-based) of the term out of range */
140762fc069eSdrh int mx, /* Largest permissible value of i */
140862fc069eSdrh Expr *pError /* Associate the error with the expression */
14097d10d5a6Sdrh ){
14107d10d5a6Sdrh sqlite3ErrorMsg(pParse,
14117d10d5a6Sdrh "%r %s BY term out of range - should be "
14127d10d5a6Sdrh "between 1 and %d", i, zType, mx);
141362fc069eSdrh sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
14147d10d5a6Sdrh }
14157d10d5a6Sdrh
14167d10d5a6Sdrh /*
14177d10d5a6Sdrh ** Analyze the ORDER BY clause in a compound SELECT statement. Modify
14187d10d5a6Sdrh ** each term of the ORDER BY clause is a constant integer between 1
14197d10d5a6Sdrh ** and N where N is the number of columns in the compound SELECT.
14207d10d5a6Sdrh **
14217d10d5a6Sdrh ** ORDER BY terms that are already an integer between 1 and N are
14227d10d5a6Sdrh ** unmodified. ORDER BY terms that are integers outside the range of
14237d10d5a6Sdrh ** 1 through N generate an error. ORDER BY terms that are expressions
14247d10d5a6Sdrh ** are matched against result set expressions of compound SELECT
14257d10d5a6Sdrh ** beginning with the left-most SELECT and working toward the right.
14267d10d5a6Sdrh ** At the first match, the ORDER BY expression is transformed into
14277d10d5a6Sdrh ** the integer column number.
14287d10d5a6Sdrh **
14297d10d5a6Sdrh ** Return the number of errors seen.
14307d10d5a6Sdrh */
resolveCompoundOrderBy(Parse * pParse,Select * pSelect)14317d10d5a6Sdrh static int resolveCompoundOrderBy(
14327d10d5a6Sdrh Parse *pParse, /* Parsing context. Leave error messages here */
14337d10d5a6Sdrh Select *pSelect /* The SELECT statement containing the ORDER BY */
14347d10d5a6Sdrh ){
14357d10d5a6Sdrh int i;
14367d10d5a6Sdrh ExprList *pOrderBy;
14377d10d5a6Sdrh ExprList *pEList;
14387d10d5a6Sdrh sqlite3 *db;
14397d10d5a6Sdrh int moreToDo = 1;
14407d10d5a6Sdrh
14417d10d5a6Sdrh pOrderBy = pSelect->pOrderBy;
14427d10d5a6Sdrh if( pOrderBy==0 ) return 0;
14437d10d5a6Sdrh db = pParse->db;
14447d10d5a6Sdrh if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
14457d10d5a6Sdrh sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
14467d10d5a6Sdrh return 1;
14477d10d5a6Sdrh }
14487d10d5a6Sdrh for(i=0; i<pOrderBy->nExpr; i++){
1449d88fd539Sdrh pOrderBy->a[i].fg.done = 0;
14507d10d5a6Sdrh }
14517d10d5a6Sdrh pSelect->pNext = 0;
14527d10d5a6Sdrh while( pSelect->pPrior ){
14537d10d5a6Sdrh pSelect->pPrior->pNext = pSelect;
14547d10d5a6Sdrh pSelect = pSelect->pPrior;
14557d10d5a6Sdrh }
14567d10d5a6Sdrh while( pSelect && moreToDo ){
14577d10d5a6Sdrh struct ExprList_item *pItem;
14587d10d5a6Sdrh moreToDo = 0;
14597d10d5a6Sdrh pEList = pSelect->pEList;
14600a846f96Sdrh assert( pEList!=0 );
14617d10d5a6Sdrh for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
14627d10d5a6Sdrh int iCol = -1;
14637d10d5a6Sdrh Expr *pE, *pDup;
1464d88fd539Sdrh if( pItem->fg.done ) continue;
14650d950af3Sdrh pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
1466235667a8Sdrh if( NEVER(pE==0) ) continue;
14677d10d5a6Sdrh if( sqlite3ExprIsInteger(pE, &iCol) ){
146873c0fdc7Sdrh if( iCol<=0 || iCol>pEList->nExpr ){
146962fc069eSdrh resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
14707d10d5a6Sdrh return 1;
14717d10d5a6Sdrh }
14727d10d5a6Sdrh }else{
14737d10d5a6Sdrh iCol = resolveAsName(pParse, pEList, pE);
14747d10d5a6Sdrh if( iCol==0 ){
14755e970a8fSdan /* Now test if expression pE matches one of the values returned
14765e970a8fSdan ** by pSelect. In the usual case this is done by duplicating the
14775e970a8fSdan ** expression, resolving any symbols in it, and then comparing
14785e970a8fSdan ** it against each expression returned by the SELECT statement.
14795e970a8fSdan ** Once the comparisons are finished, the duplicate expression
14805e970a8fSdan ** is deleted.
14815e970a8fSdan **
1482b56a0907Sdan ** If this is running as part of an ALTER TABLE operation and
1483b56a0907Sdan ** the symbols resolve successfully, also resolve the symbols in the
1484b56a0907Sdan ** actual expression. This allows the code in alter.c to modify
1485b56a0907Sdan ** column references within the ORDER BY expression as required. */
14866ab3a2ecSdanielk1977 pDup = sqlite3ExprDup(db, pE, 0);
14877d10d5a6Sdrh if( !db->mallocFailed ){
14887d10d5a6Sdrh assert(pDup);
14897d10d5a6Sdrh iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
1490b56a0907Sdan if( IN_RENAME_OBJECT && iCol>0 ){
1491b56a0907Sdan resolveOrderByTermToExprList(pParse, pSelect, pE);
14927d10d5a6Sdrh }
1493b56a0907Sdan }
14947d10d5a6Sdrh sqlite3ExprDelete(db, pDup);
14957d10d5a6Sdrh }
14967d10d5a6Sdrh }
14977d10d5a6Sdrh if( iCol>0 ){
1498bd13d34bSdrh /* Convert the ORDER BY term into an integer column number iCol,
1499b56a0907Sdan ** taking care to preserve the COLLATE clause if it exists. */
1500a5f9f42aSdan if( !IN_RENAME_OBJECT ){
1501bd13d34bSdrh Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
1502bd13d34bSdrh if( pNew==0 ) return 1;
1503bd13d34bSdrh pNew->flags |= EP_IntValue;
1504bd13d34bSdrh pNew->u.iValue = iCol;
1505bd13d34bSdrh if( pItem->pExpr==pE ){
1506bd13d34bSdrh pItem->pExpr = pNew;
1507bd13d34bSdrh }else{
15086456b771Sdrh Expr *pParent = pItem->pExpr;
15096456b771Sdrh assert( pParent->op==TK_COLLATE );
15106456b771Sdrh while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
15116456b771Sdrh assert( pParent->pLeft==pE );
15126456b771Sdrh pParent->pLeft = pNew;
1513bd13d34bSdrh }
15147d10d5a6Sdrh sqlite3ExprDelete(db, pE);
1515c2acc4e4Sdrh pItem->u.x.iOrderByCol = (u16)iCol;
1516a5f9f42aSdan }
1517d88fd539Sdrh pItem->fg.done = 1;
15187d10d5a6Sdrh }else{
15197d10d5a6Sdrh moreToDo = 1;
15207d10d5a6Sdrh }
15217d10d5a6Sdrh }
15227d10d5a6Sdrh pSelect = pSelect->pNext;
15237d10d5a6Sdrh }
15247d10d5a6Sdrh for(i=0; i<pOrderBy->nExpr; i++){
1525d88fd539Sdrh if( pOrderBy->a[i].fg.done==0 ){
15267d10d5a6Sdrh sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
15277d10d5a6Sdrh "column in the result set", i+1);
15287d10d5a6Sdrh return 1;
15297d10d5a6Sdrh }
15307d10d5a6Sdrh }
15317d10d5a6Sdrh return 0;
15327d10d5a6Sdrh }
15337d10d5a6Sdrh
15347d10d5a6Sdrh /*
15357d10d5a6Sdrh ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
15367d10d5a6Sdrh ** the SELECT statement pSelect. If any term is reference to a
1537c2acc4e4Sdrh ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
1538c2acc4e4Sdrh ** field) then convert that term into a copy of the corresponding result set
15397d10d5a6Sdrh ** column.
15407d10d5a6Sdrh **
15417d10d5a6Sdrh ** If any errors are detected, add an error message to pParse and
15427d10d5a6Sdrh ** return non-zero. Return zero if no errors are seen.
15437d10d5a6Sdrh */
sqlite3ResolveOrderGroupBy(Parse * pParse,Select * pSelect,ExprList * pOrderBy,const char * zType)15447d10d5a6Sdrh int sqlite3ResolveOrderGroupBy(
15457d10d5a6Sdrh Parse *pParse, /* Parsing context. Leave error messages here */
15467d10d5a6Sdrh Select *pSelect, /* The SELECT statement containing the clause */
15477d10d5a6Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
15487d10d5a6Sdrh const char *zType /* "ORDER" or "GROUP" */
15497d10d5a6Sdrh ){
15507d10d5a6Sdrh int i;
15517d10d5a6Sdrh sqlite3 *db = pParse->db;
15527d10d5a6Sdrh ExprList *pEList;
15537d10d5a6Sdrh struct ExprList_item *pItem;
15547d10d5a6Sdrh
155571f059c8Sdan if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
15567d10d5a6Sdrh if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
15577d10d5a6Sdrh sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
15587d10d5a6Sdrh return 1;
15597d10d5a6Sdrh }
15607d10d5a6Sdrh pEList = pSelect->pEList;
15610a846f96Sdrh assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
15627d10d5a6Sdrh for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
1563c2acc4e4Sdrh if( pItem->u.x.iOrderByCol ){
1564c2acc4e4Sdrh if( pItem->u.x.iOrderByCol>pEList->nExpr ){
156562fc069eSdrh resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
15667d10d5a6Sdrh return 1;
15677d10d5a6Sdrh }
15686b37b762Sdrh resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
15697d10d5a6Sdrh }
15707d10d5a6Sdrh }
15717d10d5a6Sdrh return 0;
15727d10d5a6Sdrh }
15737d10d5a6Sdrh
1574f030b376Sdan #ifndef SQLITE_OMIT_WINDOWFUNC
1575f030b376Sdan /*
157675b0821eSdan ** Walker callback for windowRemoveExprFromSelect().
1577f030b376Sdan */
resolveRemoveWindowsCb(Walker * pWalker,Expr * pExpr)1578f030b376Sdan static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
157951755a78Sdrh UNUSED_PARAMETER(pWalker);
1580f030b376Sdan if( ExprHasProperty(pExpr, EP_WinFunc) ){
158175b0821eSdan Window *pWin = pExpr->y.pWin;
1582e2094572Sdrh sqlite3WindowUnlinkFromSelect(pWin);
1583f030b376Sdan }
1584f030b376Sdan return WRC_Continue;
1585f030b376Sdan }
1586f030b376Sdan
1587f030b376Sdan /*
1588f030b376Sdan ** Remove any Window objects owned by the expression pExpr from the
1589f030b376Sdan ** Select.pWin list of Select object pSelect.
1590f030b376Sdan */
windowRemoveExprFromSelect(Select * pSelect,Expr * pExpr)159175b0821eSdan static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
1592fd15e18dSdrh if( pSelect->pWin ){
1593f030b376Sdan Walker sWalker;
1594f030b376Sdan memset(&sWalker, 0, sizeof(Walker));
1595f030b376Sdan sWalker.xExprCallback = resolveRemoveWindowsCb;
1596f030b376Sdan sWalker.u.pSelect = pSelect;
1597f030b376Sdan sqlite3WalkExpr(&sWalker, pExpr);
1598f030b376Sdan }
1599fd15e18dSdrh }
1600f030b376Sdan #else
160175b0821eSdan # define windowRemoveExprFromSelect(a, b)
1602fd15e18dSdrh #endif /* SQLITE_OMIT_WINDOWFUNC */
1603f030b376Sdan
16047d10d5a6Sdrh /*
16057d10d5a6Sdrh ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
16067d10d5a6Sdrh ** The Name context of the SELECT statement is pNC. zType is either
16077d10d5a6Sdrh ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
16087d10d5a6Sdrh **
16097d10d5a6Sdrh ** This routine resolves each term of the clause into an expression.
16107d10d5a6Sdrh ** If the order-by term is an integer I between 1 and N (where N is the
16117d10d5a6Sdrh ** number of columns in the result set of the SELECT) then the expression
16127d10d5a6Sdrh ** in the resolution is a copy of the I-th result-set expression. If
161326080d92Sdrh ** the order-by term is an identifier that corresponds to the AS-name of
16147d10d5a6Sdrh ** a result-set expression, then the term resolves to a copy of the
16157d10d5a6Sdrh ** result-set expression. Otherwise, the expression is resolved in
16167d10d5a6Sdrh ** the usual way - using sqlite3ResolveExprNames().
16177d10d5a6Sdrh **
16187d10d5a6Sdrh ** This routine returns the number of errors. If errors occur, then
16197d10d5a6Sdrh ** an appropriate error message might be left in pParse. (OOM errors
16207d10d5a6Sdrh ** excepted.)
16217d10d5a6Sdrh */
resolveOrderGroupBy(NameContext * pNC,Select * pSelect,ExprList * pOrderBy,const char * zType)16227d10d5a6Sdrh static int resolveOrderGroupBy(
16237d10d5a6Sdrh NameContext *pNC, /* The name context of the SELECT statement */
16247d10d5a6Sdrh Select *pSelect, /* The SELECT statement holding pOrderBy */
16257d10d5a6Sdrh ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
16267d10d5a6Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */
16277d10d5a6Sdrh ){
162870331cd7Sdrh int i, j; /* Loop counters */
16297d10d5a6Sdrh int iCol; /* Column number */
16307d10d5a6Sdrh struct ExprList_item *pItem; /* A term of the ORDER BY clause */
16317d10d5a6Sdrh Parse *pParse; /* Parsing context */
16327d10d5a6Sdrh int nResult; /* Number of terms in the result set */
16337d10d5a6Sdrh
1634de0e1b15Sdrh assert( pOrderBy!=0 );
16357d10d5a6Sdrh nResult = pSelect->pEList->nExpr;
16367d10d5a6Sdrh pParse = pNC->pParse;
16377d10d5a6Sdrh for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
16387d10d5a6Sdrh Expr *pE = pItem->pExpr;
16390d950af3Sdrh Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
1640235667a8Sdrh if( NEVER(pE2==0) ) continue;
16410af16ab2Sdrh if( zType[0]!='G' ){
1642e35463b3Sdrh iCol = resolveAsName(pParse, pSelect->pEList, pE2);
16437d10d5a6Sdrh if( iCol>0 ){
16447d10d5a6Sdrh /* If an AS-name match is found, mark this ORDER BY column as being
16457d10d5a6Sdrh ** a copy of the iCol-th result-set column. The subsequent call to
16467d10d5a6Sdrh ** sqlite3ResolveOrderGroupBy() will convert the expression to a
16477d10d5a6Sdrh ** copy of the iCol-th result-set expression. */
1648c2acc4e4Sdrh pItem->u.x.iOrderByCol = (u16)iCol;
16497d10d5a6Sdrh continue;
16507d10d5a6Sdrh }
16510af16ab2Sdrh }
1652e35463b3Sdrh if( sqlite3ExprIsInteger(pE2, &iCol) ){
16537d10d5a6Sdrh /* The ORDER BY term is an integer constant. Again, set the column
16547d10d5a6Sdrh ** number so that sqlite3ResolveOrderGroupBy() will convert the
16557d10d5a6Sdrh ** order-by term to a copy of the result-set expression */
165685d641f9Sdrh if( iCol<1 || iCol>0xffff ){
165762fc069eSdrh resolveOutOfRangeError(pParse, zType, i+1, nResult, pE2);
16587d10d5a6Sdrh return 1;
16597d10d5a6Sdrh }
1660c2acc4e4Sdrh pItem->u.x.iOrderByCol = (u16)iCol;
16617d10d5a6Sdrh continue;
16627d10d5a6Sdrh }
16637d10d5a6Sdrh
16647d10d5a6Sdrh /* Otherwise, treat the ORDER BY term as an ordinary expression */
1665c2acc4e4Sdrh pItem->u.x.iOrderByCol = 0;
16667d10d5a6Sdrh if( sqlite3ResolveExprNames(pNC, pE) ){
16677d10d5a6Sdrh return 1;
16687d10d5a6Sdrh }
166970331cd7Sdrh for(j=0; j<pSelect->pEList->nExpr; j++){
16705aa550cfSdan if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
1671f030b376Sdan /* Since this expresion is being changed into a reference
1672f030b376Sdan ** to an identical expression in the result set, remove all Window
1673f030b376Sdan ** objects belonging to the expression from the Select.pWin list. */
167475b0821eSdan windowRemoveExprFromSelect(pSelect, pE);
1675c2acc4e4Sdrh pItem->u.x.iOrderByCol = j+1;
167670331cd7Sdrh }
167770331cd7Sdrh }
16787d10d5a6Sdrh }
16797d10d5a6Sdrh return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
16807d10d5a6Sdrh }
16817d10d5a6Sdrh
16827d10d5a6Sdrh /*
168360ec914cSpeter.d.reid ** Resolve names in the SELECT statement p and all of its descendants.
16847d10d5a6Sdrh */
resolveSelectStep(Walker * pWalker,Select * p)16857d10d5a6Sdrh static int resolveSelectStep(Walker *pWalker, Select *p){
16867d10d5a6Sdrh NameContext *pOuterNC; /* Context that contains this SELECT */
16877d10d5a6Sdrh NameContext sNC; /* Name context of this SELECT */
16887d10d5a6Sdrh int isCompound; /* True if p is a compound select */
16897d10d5a6Sdrh int nCompound; /* Number of compound terms processed so far */
16907d10d5a6Sdrh Parse *pParse; /* Parsing context */
16917d10d5a6Sdrh int i; /* Loop counter */
16927d10d5a6Sdrh ExprList *pGroupBy; /* The GROUP BY clause */
16937d10d5a6Sdrh Select *pLeftmost; /* Left-most of SELECT of a compound */
16947d10d5a6Sdrh sqlite3 *db; /* Database connection */
16957d10d5a6Sdrh
16967d10d5a6Sdrh
16970a846f96Sdrh assert( p!=0 );
16987d10d5a6Sdrh if( p->selFlags & SF_Resolved ){
16997d10d5a6Sdrh return WRC_Prune;
17007d10d5a6Sdrh }
17017d10d5a6Sdrh pOuterNC = pWalker->u.pNC;
17027d10d5a6Sdrh pParse = pWalker->pParse;
17037d10d5a6Sdrh db = pParse->db;
17047d10d5a6Sdrh
17057d10d5a6Sdrh /* Normally sqlite3SelectExpand() will be called first and will have
17067d10d5a6Sdrh ** already expanded this SELECT. However, if this is a subquery within
17077d10d5a6Sdrh ** an expression, sqlite3ResolveExprNames() will be called without a
17087d10d5a6Sdrh ** prior call to sqlite3SelectExpand(). When that happens, let
17097d10d5a6Sdrh ** sqlite3SelectPrep() do all of the processing for this SELECT.
17107d10d5a6Sdrh ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
17117d10d5a6Sdrh ** this routine in the correct order.
17127d10d5a6Sdrh */
17137d10d5a6Sdrh if( (p->selFlags & SF_Expanded)==0 ){
17147d10d5a6Sdrh sqlite3SelectPrep(pParse, p, pOuterNC);
17150c7d3d39Sdrh return pParse->nErr ? WRC_Abort : WRC_Prune;
17167d10d5a6Sdrh }
17177d10d5a6Sdrh
17187d10d5a6Sdrh isCompound = p->pPrior!=0;
17197d10d5a6Sdrh nCompound = 0;
17207d10d5a6Sdrh pLeftmost = p;
17217d10d5a6Sdrh while( p ){
17227d10d5a6Sdrh assert( (p->selFlags & SF_Expanded)!=0 );
17237d10d5a6Sdrh assert( (p->selFlags & SF_Resolved)==0 );
17249920bf97Sdrh assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
17257d10d5a6Sdrh p->selFlags |= SF_Resolved;
17267d10d5a6Sdrh
17279920bf97Sdrh
17287d10d5a6Sdrh /* Resolve the expressions in the LIMIT and OFFSET clauses. These
17297d10d5a6Sdrh ** are not allowed to refer to any names, so pass an empty NameContext.
17307d10d5a6Sdrh */
17317d10d5a6Sdrh memset(&sNC, 0, sizeof(sNC));
17327d10d5a6Sdrh sNC.pParse = pParse;
1733e3bf632cSdan sNC.pWinSelect = p;
17348c0833fbSdrh if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
17357d10d5a6Sdrh return WRC_Abort;
17367d10d5a6Sdrh }
17377d10d5a6Sdrh
1738b33c50f2Sdan /* If the SF_Converted flags is set, then this Select object was
1739b33c50f2Sdan ** was created by the convertCompoundSelectToSubquery() function.
1740b33c50f2Sdan ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
1741b33c50f2Sdan ** as if it were part of the sub-query, not the parent. This block
1742b33c50f2Sdan ** moves the pOrderBy down to the sub-query. It will be moved back
1743b33c50f2Sdan ** after the names have been resolved. */
1744b33c50f2Sdan if( p->selFlags & SF_Converted ){
1745b33c50f2Sdan Select *pSub = p->pSrc->a[0].pSelect;
1746a43f02efSdrh assert( p->pSrc->nSrc==1 && p->pOrderBy );
1747b33c50f2Sdan assert( pSub->pPrior && pSub->pOrderBy==0 );
1748b33c50f2Sdan pSub->pOrderBy = p->pOrderBy;
1749b33c50f2Sdan p->pOrderBy = 0;
1750b33c50f2Sdan }
1751b33c50f2Sdan
1752ee612e2aSdrh /* Recursively resolve names in all subqueries in the FROM clause
17537d10d5a6Sdrh */
17547d10d5a6Sdrh for(i=0; i<p->pSrc->nSrc; i++){
17557601294aSdrh SrcItem *pItem = &p->pSrc->a[i];
1756781b7ac3Sdan if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
17574b17455aSdan int nRef = pOuterNC ? pOuterNC->nRef : 0;
17587d10d5a6Sdrh const char *zSavedContext = pParse->zAuthContext;
1759da79cf0cSdan
17607d10d5a6Sdrh if( pItem->zName ) pParse->zAuthContext = pItem->zName;
1761cd2b5613Sdrh sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
17627d10d5a6Sdrh pParse->zAuthContext = zSavedContext;
17630c7d3d39Sdrh if( pParse->nErr ) return WRC_Abort;
17640c7d3d39Sdrh assert( db->mallocFailed==0 );
1765da79cf0cSdan
17664b17455aSdan /* If the number of references to the outer context changed when
17674b17455aSdan ** expressions in the sub-select were resolved, the sub-select
17684b17455aSdan ** is correlated. It is not required to check the refcount on any
17694b17455aSdan ** but the innermost outer context object, as lookupName() increments
17704b17455aSdan ** the refcount on all contexts between the current one and the
17714b17455aSdan ** context containing the column when it resolves a name. */
17724b17455aSdan if( pOuterNC ){
17734b17455aSdan assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
17744b17455aSdan pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
17754b17455aSdan }
17767d10d5a6Sdrh }
17777d10d5a6Sdrh }
17787d10d5a6Sdrh
177992689d28Sdrh /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
178092689d28Sdrh ** resolve the result-set expression list.
178192689d28Sdrh */
178286fb6e17Sdan sNC.ncFlags = NC_AllowAgg|NC_AllowWin;
178392689d28Sdrh sNC.pSrcList = p->pSrc;
178492689d28Sdrh sNC.pNext = pOuterNC;
178592689d28Sdrh
178692689d28Sdrh /* Resolve names in the result set. */
178701d230ceSdrh if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
178886fb6e17Sdan sNC.ncFlags &= ~NC_AllowWin;
178992689d28Sdrh
17907d10d5a6Sdrh /* If there are no aggregate functions in the result-set, and no GROUP BY
17917d10d5a6Sdrh ** expression, do not allow aggregates in any of the other expressions.
17927d10d5a6Sdrh */
17937d10d5a6Sdrh assert( (p->selFlags & SF_Aggregate)==0 );
17947d10d5a6Sdrh pGroupBy = p->pGroupBy;
1795a51009b2Sdrh if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
17969588ad95Sdrh assert( NC_MinMaxAgg==SF_MinMaxAgg );
1797bb301231Sdrh assert( NC_OrderAgg==SF_OrderByReqd );
1798bb301231Sdrh p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
17997d10d5a6Sdrh }else{
1800a51009b2Sdrh sNC.ncFlags &= ~NC_AllowAgg;
18017d10d5a6Sdrh }
18027d10d5a6Sdrh
180326080d92Sdrh /* Add the output column list to the name-context before parsing the
18047d10d5a6Sdrh ** other expressions in the SELECT statement. This is so that
18057d10d5a6Sdrh ** expressions in the WHERE clause (etc.) can refer to expressions by
18067d10d5a6Sdrh ** aliases in the result set.
18077d10d5a6Sdrh **
18087d10d5a6Sdrh ** Minor point: If this is the case, then the expression will be
18097d10d5a6Sdrh ** re-evaluated for each reference to it.
18107d10d5a6Sdrh */
1811d5101972Sdrh assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert|NC_UBaseReg))==0 );
181225c3b8caSdrh sNC.uNC.pEList = p->pEList;
181325c3b8caSdrh sNC.ncFlags |= NC_UEList;
1814de0e1b15Sdrh if( p->pHaving ){
1815b9294de1Sdrh if( (p->selFlags & SF_Aggregate)==0 ){
1816b9294de1Sdrh sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
1817de0e1b15Sdrh return WRC_Abort;
1818de0e1b15Sdrh }
181958a450c0Sdrh if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
1820de0e1b15Sdrh }
1821a3a5bd9bSdrh if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
18227d10d5a6Sdrh
182301d230ceSdrh /* Resolve names in table-valued-function arguments */
182401d230ceSdrh for(i=0; i<p->pSrc->nSrc; i++){
18257601294aSdrh SrcItem *pItem = &p->pSrc->a[i];
182601d230ceSdrh if( pItem->fg.isTabFunc
182701d230ceSdrh && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
182801d230ceSdrh ){
182901d230ceSdrh return WRC_Abort;
183001d230ceSdrh }
183101d230ceSdrh }
183201d230ceSdrh
1833be12083bSdan #ifndef SQLITE_OMIT_WINDOWFUNC
1834be12083bSdan if( IN_RENAME_OBJECT ){
1835be12083bSdan Window *pWin;
1836be12083bSdan for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
1837be12083bSdan if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
1838be12083bSdan || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
1839be12083bSdan ){
1840be12083bSdan return WRC_Abort;
1841be12083bSdan }
1842be12083bSdan }
1843be12083bSdan }
1844be12083bSdan #endif
1845be12083bSdan
18467d10d5a6Sdrh /* The ORDER BY and GROUP BY clauses may not refer to terms in
18477d10d5a6Sdrh ** outer queries
18487d10d5a6Sdrh */
18497d10d5a6Sdrh sNC.pNext = 0;
185086fb6e17Sdan sNC.ncFlags |= NC_AllowAgg|NC_AllowWin;
18517d10d5a6Sdrh
1852b33c50f2Sdan /* If this is a converted compound query, move the ORDER BY clause from
1853b33c50f2Sdan ** the sub-query back to the parent query. At this point each term
1854b33c50f2Sdan ** within the ORDER BY clause has been transformed to an integer value.
1855b33c50f2Sdan ** These integers will be replaced by copies of the corresponding result
1856b33c50f2Sdan ** set expressions by the call to resolveOrderGroupBy() below. */
1857b33c50f2Sdan if( p->selFlags & SF_Converted ){
1858b33c50f2Sdan Select *pSub = p->pSrc->a[0].pSelect;
1859b33c50f2Sdan p->pOrderBy = pSub->pOrderBy;
1860b33c50f2Sdan pSub->pOrderBy = 0;
1861b33c50f2Sdan }
1862b33c50f2Sdan
18637d10d5a6Sdrh /* Process the ORDER BY clause for singleton SELECT statements.
18647d10d5a6Sdrh ** The ORDER BY clause for compounds SELECT statements is handled
18657d10d5a6Sdrh ** below, after all of the result-sets for all of the elements of
18667d10d5a6Sdrh ** the compound have been resolved.
18677b4da150Sdrh **
18687b4da150Sdrh ** If there is an ORDER BY clause on a term of a compound-select other
18697b4da150Sdrh ** than the right-most term, then that is a syntax error. But the error
18707b4da150Sdrh ** is not detected until much later, and so we need to go ahead and
18717b4da150Sdrh ** resolve those symbols on the incorrect ORDER BY for consistency.
18727d10d5a6Sdrh */
1873de0e1b15Sdrh if( p->pOrderBy!=0
1874de0e1b15Sdrh && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
18757b4da150Sdrh && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
18767b4da150Sdrh ){
18777d10d5a6Sdrh return WRC_Abort;
18787d10d5a6Sdrh }
18797d10d5a6Sdrh if( db->mallocFailed ){
18807d10d5a6Sdrh return WRC_Abort;
18817d10d5a6Sdrh }
188286fb6e17Sdan sNC.ncFlags &= ~NC_AllowWin;
18837d10d5a6Sdrh
18847d10d5a6Sdrh /* Resolve the GROUP BY clause. At the same time, make sure
18857d10d5a6Sdrh ** the GROUP BY clause does not contain aggregate functions.
18867d10d5a6Sdrh */
18877d10d5a6Sdrh if( pGroupBy ){
18887d10d5a6Sdrh struct ExprList_item *pItem;
18897d10d5a6Sdrh
18907d10d5a6Sdrh if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
18917d10d5a6Sdrh return WRC_Abort;
18927d10d5a6Sdrh }
18937d10d5a6Sdrh for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
18947d10d5a6Sdrh if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
18957d10d5a6Sdrh sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
18967d10d5a6Sdrh "the GROUP BY clause");
18977d10d5a6Sdrh return WRC_Abort;
18987d10d5a6Sdrh }
18997d10d5a6Sdrh }
19007d10d5a6Sdrh }
19017d10d5a6Sdrh
1902923cadb1Sdan /* If this is part of a compound SELECT, check that it has the right
1903923cadb1Sdan ** number of expressions in the select list. */
1904923cadb1Sdan if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
1905923cadb1Sdan sqlite3SelectWrongNumTermsError(pParse, p->pNext);
1906923cadb1Sdan return WRC_Abort;
1907923cadb1Sdan }
1908923cadb1Sdan
19097d10d5a6Sdrh /* Advance to the next term of the compound
19107d10d5a6Sdrh */
19117d10d5a6Sdrh p = p->pPrior;
19127d10d5a6Sdrh nCompound++;
19137d10d5a6Sdrh }
19147d10d5a6Sdrh
19157d10d5a6Sdrh /* Resolve the ORDER BY on a compound SELECT after all terms of
19167d10d5a6Sdrh ** the compound have been resolved.
19177d10d5a6Sdrh */
19187d10d5a6Sdrh if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
19197d10d5a6Sdrh return WRC_Abort;
19207d10d5a6Sdrh }
19217d10d5a6Sdrh
19227d10d5a6Sdrh return WRC_Prune;
19237d10d5a6Sdrh }
19247d10d5a6Sdrh
19257d10d5a6Sdrh /*
19267d10d5a6Sdrh ** This routine walks an expression tree and resolves references to
19277d10d5a6Sdrh ** table columns and result-set columns. At the same time, do error
19287d10d5a6Sdrh ** checking on function usage and set a flag if any aggregate functions
19297d10d5a6Sdrh ** are seen.
19307d10d5a6Sdrh **
19317d10d5a6Sdrh ** To resolve table columns references we look for nodes (or subtrees) of the
19327d10d5a6Sdrh ** form X.Y.Z or Y.Z or just Z where
19337d10d5a6Sdrh **
19347d10d5a6Sdrh ** X: The name of a database. Ex: "main" or "temp" or
19357d10d5a6Sdrh ** the symbolic name assigned to an ATTACH-ed database.
19367d10d5a6Sdrh **
19377d10d5a6Sdrh ** Y: The name of a table in a FROM clause. Or in a trigger
19387d10d5a6Sdrh ** one of the special names "old" or "new".
19397d10d5a6Sdrh **
19407d10d5a6Sdrh ** Z: The name of a column in table Y.
19417d10d5a6Sdrh **
19427d10d5a6Sdrh ** The node at the root of the subtree is modified as follows:
19437d10d5a6Sdrh **
19447d10d5a6Sdrh ** Expr.op Changed to TK_COLUMN
19457d10d5a6Sdrh ** Expr.pTab Points to the Table object for X.Y
19467d10d5a6Sdrh ** Expr.iColumn The column index in X.Y. -1 for the rowid.
19477d10d5a6Sdrh ** Expr.iTable The VDBE cursor number for X.Y
19487d10d5a6Sdrh **
19497d10d5a6Sdrh **
19507d10d5a6Sdrh ** To resolve result-set references, look for expression nodes of the
19517d10d5a6Sdrh ** form Z (with no X and Y prefix) where the Z matches the right-hand
19527d10d5a6Sdrh ** size of an AS clause in the result-set of a SELECT. The Z expression
19537d10d5a6Sdrh ** is replaced by a copy of the left-hand side of the result-set expression.
19547d10d5a6Sdrh ** Table-name and function resolution occurs on the substituted expression
19557d10d5a6Sdrh ** tree. For example, in:
19567d10d5a6Sdrh **
19577d10d5a6Sdrh ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
19587d10d5a6Sdrh **
19597d10d5a6Sdrh ** The "x" term of the order by is replaced by "a+b" to render:
19607d10d5a6Sdrh **
19617d10d5a6Sdrh ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
19627d10d5a6Sdrh **
19637d10d5a6Sdrh ** Function calls are checked to make sure that the function is
19647d10d5a6Sdrh ** defined and that the correct number of arguments are specified.
1965a51009b2Sdrh ** If the function is an aggregate function, then the NC_HasAgg flag is
19667d10d5a6Sdrh ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
19677d10d5a6Sdrh ** If an expression contains aggregate functions then the EP_Agg
19687d10d5a6Sdrh ** property on the expression is set.
19697d10d5a6Sdrh **
19707d10d5a6Sdrh ** An error message is left in pParse if anything is amiss. The number
19717d10d5a6Sdrh ** if errors is returned.
19727d10d5a6Sdrh */
sqlite3ResolveExprNames(NameContext * pNC,Expr * pExpr)19737d10d5a6Sdrh int sqlite3ResolveExprNames(
19747d10d5a6Sdrh NameContext *pNC, /* Namespace to resolve expressions in. */
19757d10d5a6Sdrh Expr *pExpr /* The expression to be analyzed. */
19767d10d5a6Sdrh ){
19770d92571dSdan int savedHasAgg;
19787d10d5a6Sdrh Walker w;
19797d10d5a6Sdrh
1980d03257c1Sdrh if( pExpr==0 ) return SQLITE_OK;
1981bb301231Sdrh savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
1982bb301231Sdrh pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
19839bfb024dSdrh w.pParse = pNC->pParse;
19847d10d5a6Sdrh w.xExprCallback = resolveExprStep;
1985d487e373Sdan w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
19869bfb024dSdrh w.xSelectCallback2 = 0;
19877d10d5a6Sdrh w.u.pNC = pNC;
1988d03257c1Sdrh #if SQLITE_MAX_EXPR_DEPTH>0
1989d03257c1Sdrh w.pParse->nHeight += pExpr->nHeight;
1990d03257c1Sdrh if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
1991d03257c1Sdrh return SQLITE_ERROR;
1992d03257c1Sdrh }
1993d03257c1Sdrh #endif
19947d10d5a6Sdrh sqlite3WalkExpr(&w, pExpr);
19957d10d5a6Sdrh #if SQLITE_MAX_EXPR_DEPTH>0
1996d03257c1Sdrh w.pParse->nHeight -= pExpr->nHeight;
19977d10d5a6Sdrh #endif
1998d137f4e6Sdrh assert( EP_Agg==NC_HasAgg );
1999d137f4e6Sdrh assert( EP_Win==NC_HasWin );
2000d137f4e6Sdrh testcase( pNC->ncFlags & NC_HasAgg );
2001d137f4e6Sdrh testcase( pNC->ncFlags & NC_HasWin );
2002d137f4e6Sdrh ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
20039588ad95Sdrh pNC->ncFlags |= savedHasAgg;
2004050611a7Sdrh return pNC->nNcErr>0 || w.pParse->nErr>0;
20057d10d5a6Sdrh }
20067d10d5a6Sdrh
200701d230ceSdrh /*
200801d230ceSdrh ** Resolve all names for all expression in an expression list. This is
200901d230ceSdrh ** just like sqlite3ResolveExprNames() except that it works for an expression
201001d230ceSdrh ** list rather than a single expression.
201101d230ceSdrh */
sqlite3ResolveExprListNames(NameContext * pNC,ExprList * pList)201201d230ceSdrh int sqlite3ResolveExprListNames(
201301d230ceSdrh NameContext *pNC, /* Namespace to resolve expressions in. */
201401d230ceSdrh ExprList *pList /* The expression list to be analyzed. */
201501d230ceSdrh ){
201601d230ceSdrh int i;
20174047bdfdSdrh int savedHasAgg = 0;
20184047bdfdSdrh Walker w;
20194047bdfdSdrh if( pList==0 ) return WRC_Continue;
20204047bdfdSdrh w.pParse = pNC->pParse;
20214047bdfdSdrh w.xExprCallback = resolveExprStep;
20224047bdfdSdrh w.xSelectCallback = resolveSelectStep;
20234047bdfdSdrh w.xSelectCallback2 = 0;
20244047bdfdSdrh w.u.pNC = pNC;
2025bb301231Sdrh savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
2026bb301231Sdrh pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
202701d230ceSdrh for(i=0; i<pList->nExpr; i++){
20284047bdfdSdrh Expr *pExpr = pList->a[i].pExpr;
20294047bdfdSdrh if( pExpr==0 ) continue;
20304047bdfdSdrh #if SQLITE_MAX_EXPR_DEPTH>0
20314047bdfdSdrh w.pParse->nHeight += pExpr->nHeight;
20324047bdfdSdrh if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
20334047bdfdSdrh return WRC_Abort;
203401d230ceSdrh }
20354047bdfdSdrh #endif
20364047bdfdSdrh sqlite3WalkExpr(&w, pExpr);
20374047bdfdSdrh #if SQLITE_MAX_EXPR_DEPTH>0
20384047bdfdSdrh w.pParse->nHeight -= pExpr->nHeight;
20394047bdfdSdrh #endif
20404047bdfdSdrh assert( EP_Agg==NC_HasAgg );
20414047bdfdSdrh assert( EP_Win==NC_HasWin );
20424047bdfdSdrh testcase( pNC->ncFlags & NC_HasAgg );
20434047bdfdSdrh testcase( pNC->ncFlags & NC_HasWin );
2044bb301231Sdrh if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
20454047bdfdSdrh ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
2046bb301231Sdrh savedHasAgg |= pNC->ncFlags &
2047bb301231Sdrh (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
2048bb301231Sdrh pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
204920292310Sdrh }
2050050611a7Sdrh if( w.pParse->nErr>0 ) return WRC_Abort;
20514047bdfdSdrh }
20524047bdfdSdrh pNC->ncFlags |= savedHasAgg;
205301d230ceSdrh return WRC_Continue;
205401d230ceSdrh }
20557d10d5a6Sdrh
20567d10d5a6Sdrh /*
20577d10d5a6Sdrh ** Resolve all names in all expressions of a SELECT and in all
20587d10d5a6Sdrh ** decendents of the SELECT, including compounds off of p->pPrior,
20597d10d5a6Sdrh ** subqueries in expressions, and subqueries used as FROM clause
20607d10d5a6Sdrh ** terms.
20617d10d5a6Sdrh **
20627d10d5a6Sdrh ** See sqlite3ResolveExprNames() for a description of the kinds of
20637d10d5a6Sdrh ** transformations that occur.
20647d10d5a6Sdrh **
20657d10d5a6Sdrh ** All SELECT statements should have been expanded using
20667d10d5a6Sdrh ** sqlite3SelectExpand() prior to invoking this routine.
20677d10d5a6Sdrh */
sqlite3ResolveSelectNames(Parse * pParse,Select * p,NameContext * pOuterNC)20687d10d5a6Sdrh void sqlite3ResolveSelectNames(
20697d10d5a6Sdrh Parse *pParse, /* The parser context */
20707d10d5a6Sdrh Select *p, /* The SELECT statement being coded. */
20717d10d5a6Sdrh NameContext *pOuterNC /* Name context for parent SELECT statement */
20727d10d5a6Sdrh ){
20737d10d5a6Sdrh Walker w;
20747d10d5a6Sdrh
20750a846f96Sdrh assert( p!=0 );
20767d10d5a6Sdrh w.xExprCallback = resolveExprStep;
20777d10d5a6Sdrh w.xSelectCallback = resolveSelectStep;
2078979dd1beSdrh w.xSelectCallback2 = 0;
20797d10d5a6Sdrh w.pParse = pParse;
20807d10d5a6Sdrh w.u.pNC = pOuterNC;
20817d10d5a6Sdrh sqlite3WalkSelect(&w, p);
20827d10d5a6Sdrh }
20833780be11Sdrh
20843780be11Sdrh /*
2085ee751fabSdrh ** Resolve names in expressions that can only reference a single table
2086ee751fabSdrh ** or which cannot reference any tables at all. Examples:
20873780be11Sdrh **
208820cee7d0Sdrh ** "type" flag
208920cee7d0Sdrh ** ------------
209020cee7d0Sdrh ** (1) CHECK constraints NC_IsCheck
209120cee7d0Sdrh ** (2) WHERE clauses on partial indices NC_PartIdx
209220cee7d0Sdrh ** (3) Expressions in indexes on expressions NC_IdxExpr
209320cee7d0Sdrh ** (4) Expression arguments to VACUUM INTO. 0
209420cee7d0Sdrh ** (5) GENERATED ALWAYS as expressions NC_GenCol
20953780be11Sdrh **
2096ee751fabSdrh ** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
2097ee751fabSdrh ** nodes of the expression is set to -1 and the Expr.iColumn value is
2098ee751fabSdrh ** set to the column number. In case (4), TK_COLUMN nodes cause an error.
20993780be11Sdrh **
21003780be11Sdrh ** Any errors cause an error message to be set in pParse.
21013780be11Sdrh */
sqlite3ResolveSelfReference(Parse * pParse,Table * pTab,int type,Expr * pExpr,ExprList * pList)2102ee751fabSdrh int sqlite3ResolveSelfReference(
21033780be11Sdrh Parse *pParse, /* Parsing context */
2104ee751fabSdrh Table *pTab, /* The table being referenced, or NULL */
210581f7b372Sdrh int type, /* NC_IsCheck, NC_PartIdx, NC_IdxExpr, NC_GenCol, or 0 */
21063780be11Sdrh Expr *pExpr, /* Expression to resolve. May be NULL. */
21074357e226Sdrh ExprList *pList /* Expression list to resolve. May be NULL. */
21083780be11Sdrh ){
21093780be11Sdrh SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
21103780be11Sdrh NameContext sNC; /* Name context for pParse->pNewTable */
2111ee751fabSdrh int rc;
21123780be11Sdrh
2113ee751fabSdrh assert( type==0 || pTab!=0 );
211481f7b372Sdrh assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr
211581f7b372Sdrh || type==NC_GenCol || pTab==0 );
21163780be11Sdrh memset(&sNC, 0, sizeof(sNC));
21173780be11Sdrh memset(&sSrc, 0, sizeof(sSrc));
2118ee751fabSdrh if( pTab ){
21193780be11Sdrh sSrc.nSrc = 1;
21203780be11Sdrh sSrc.a[0].zName = pTab->zName;
21213780be11Sdrh sSrc.a[0].pTab = pTab;
21223780be11Sdrh sSrc.a[0].iCursor = -1;
212305b32ee3Sdrh if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
212405b32ee3Sdrh /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
212505b32ee3Sdrh ** schema elements */
212605b32ee3Sdrh type |= NC_FromDDL;
212705b32ee3Sdrh }
2128ee751fabSdrh }
21293780be11Sdrh sNC.pParse = pParse;
21303780be11Sdrh sNC.pSrcList = &sSrc;
2131d0ff601cSdrh sNC.ncFlags = type | NC_IsDDL;
2132ee751fabSdrh if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK ) return rc;
2133ee751fabSdrh if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
2134ee751fabSdrh return rc;
21353780be11Sdrh }
2136