xref: /sqlite-3.40.0/src/resolve.c (revision be217793)
1 /*
2 ** 2008 August 18
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 ** This file contains routines used for walking the parser tree and
14 ** resolve all identifiers by associating them with a particular
15 ** table and column.
16 **
17 ** $Id: resolve.c,v 1.19 2009/03/02 01:22:40 drh Exp $
18 */
19 #include "sqliteInt.h"
20 #include <stdlib.h>
21 #include <string.h>
22 
23 /*
24 ** Turn the pExpr expression into an alias for the iCol-th column of the
25 ** result set in pEList.
26 **
27 ** If the result set column is a simple column reference, then this routine
28 ** makes an exact copy.  But for any other kind of expression, this
29 ** routine make a copy of the result set column as the argument to the
30 ** TK_AS operator.  The TK_AS operator causes the expression to be
31 ** evaluated just once and then reused for each alias.
32 **
33 ** The reason for suppressing the TK_AS term when the expression is a simple
34 ** column reference is so that the column reference will be recognized as
35 ** usable by indices within the WHERE clause processing logic.
36 **
37 ** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
38 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
39 **
40 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
41 **
42 ** Is equivalent to:
43 **
44 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
45 **
46 ** The result of random()%5 in the GROUP BY clause is probably different
47 ** from the result in the result-set.  We might fix this someday.  Or
48 ** then again, we might not...
49 */
50 static void resolveAlias(
51   Parse *pParse,         /* Parsing context */
52   ExprList *pEList,      /* A result set */
53   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
54   Expr *pExpr,           /* Transform this into an alias to the result set */
55   const char *zType      /* "GROUP" or "ORDER" or "" */
56 ){
57   Expr *pOrig;           /* The iCol-th column of the result set */
58   Expr *pDup;            /* Copy of pOrig */
59   sqlite3 *db;           /* The database connection */
60 
61   assert( iCol>=0 && iCol<pEList->nExpr );
62   pOrig = pEList->a[iCol].pExpr;
63   assert( pOrig!=0 );
64   assert( pOrig->flags & EP_Resolved );
65   db = pParse->db;
66   pDup = sqlite3ExprDup(db, pOrig, 0);
67   if( pDup==0 ) return;
68   sqlite3TokenCopy(db, &pDup->token, &pOrig->token);
69   if( pDup->op!=TK_COLUMN && zType[0]!='G' ){
70     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
71     if( pDup==0 ) return;
72     if( pEList->a[iCol].iAlias==0 ){
73       pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
74     }
75     pDup->iTable = pEList->a[iCol].iAlias;
76   }
77   if( pExpr->flags & EP_ExpCollate ){
78     pDup->pColl = pExpr->pColl;
79     pDup->flags |= EP_ExpCollate;
80   }
81   sqlite3ExprClear(db, pExpr);
82   memcpy(pExpr, pDup, sizeof(*pExpr));
83   sqlite3DbFree(db, pDup);
84 }
85 
86 /*
87 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
88 ** that name in the set of source tables in pSrcList and make the pExpr
89 ** expression node refer back to that source column.  The following changes
90 ** are made to pExpr:
91 **
92 **    pExpr->iDb           Set the index in db->aDb[] of the database X
93 **                         (even if X is implied).
94 **    pExpr->iTable        Set to the cursor number for the table obtained
95 **                         from pSrcList.
96 **    pExpr->pTab          Points to the Table structure of X.Y (even if
97 **                         X and/or Y are implied.)
98 **    pExpr->iColumn       Set to the column number within the table.
99 **    pExpr->op            Set to TK_COLUMN.
100 **    pExpr->pLeft         Any expression this points to is deleted
101 **    pExpr->pRight        Any expression this points to is deleted.
102 **
103 ** The pDbToken is the name of the database (the "X").  This value may be
104 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
105 ** can be used.  The pTableToken is the name of the table (the "Y").  This
106 ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
107 ** means that the form of the name is Z and that columns from any table
108 ** can be used.
109 **
110 ** If the name cannot be resolved unambiguously, leave an error message
111 ** in pParse and return non-zero.  Return zero on success.
112 */
113 static int lookupName(
114   Parse *pParse,       /* The parsing context */
115   Token *pDbToken,     /* Name of the database containing table, or NULL */
116   Token *pTableToken,  /* Name of table containing column, or NULL */
117   Token *pColumnToken, /* Name of the column. */
118   NameContext *pNC,    /* The name context used to resolve the name */
119   Expr *pExpr          /* Make this EXPR node point to the selected column */
120 ){
121   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
122   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
123   char *zCol = 0;      /* Name of the column.  The "Z" */
124   int i, j;            /* Loop counters */
125   int cnt = 0;                      /* Number of matching column names */
126   int cntTab = 0;                   /* Number of matching table names */
127   sqlite3 *db = pParse->db;         /* The database connection */
128   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
129   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
130   NameContext *pTopNC = pNC;        /* First namecontext in the list */
131   Schema *pSchema = 0;              /* Schema of the expression */
132 
133   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
134 
135   /* Dequote and zero-terminate the names */
136   zDb = sqlite3NameFromToken(db, pDbToken);
137   zTab = sqlite3NameFromToken(db, pTableToken);
138   zCol = sqlite3NameFromToken(db, pColumnToken);
139   if( db->mallocFailed ){
140     goto lookupname_end;
141   }
142 
143   /* Initialize the node to no-match */
144   pExpr->iTable = -1;
145   pExpr->pTab = 0;
146 
147   /* Start at the inner-most context and move outward until a match is found */
148   while( pNC && cnt==0 ){
149     ExprList *pEList;
150     SrcList *pSrcList = pNC->pSrcList;
151 
152     if( pSrcList ){
153       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
154         Table *pTab;
155         int iDb;
156         Column *pCol;
157 
158         pTab = pItem->pTab;
159         assert( pTab!=0 && pTab->zName!=0 );
160         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
161         assert( pTab->nCol>0 );
162         if( zTab ){
163           if( pItem->zAlias ){
164             char *zTabName = pItem->zAlias;
165             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
166           }else{
167             char *zTabName = pTab->zName;
168             if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
169             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
170               continue;
171             }
172           }
173         }
174         if( 0==(cntTab++) ){
175           pExpr->iTable = pItem->iCursor;
176           pExpr->pTab = pTab;
177           pSchema = pTab->pSchema;
178           pMatch = pItem;
179         }
180         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
181           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
182             IdList *pUsing;
183             cnt++;
184             pExpr->iTable = pItem->iCursor;
185             pExpr->pTab = pTab;
186             pMatch = pItem;
187             pSchema = pTab->pSchema;
188             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
189             pExpr->iColumn = j==pTab->iPKey ? -1 : j;
190             if( i<pSrcList->nSrc-1 ){
191               if( pItem[1].jointype & JT_NATURAL ){
192                 /* If this match occurred in the left table of a natural join,
193                 ** then skip the right table to avoid a duplicate match */
194                 pItem++;
195                 i++;
196               }else if( (pUsing = pItem[1].pUsing)!=0 ){
197                 /* If this match occurs on a column that is in the USING clause
198                 ** of a join, skip the search of the right table of the join
199                 ** to avoid a duplicate match there. */
200                 int k;
201                 for(k=0; k<pUsing->nId; k++){
202                   if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
203                     pItem++;
204                     i++;
205                     break;
206                   }
207                 }
208               }
209             }
210             break;
211           }
212         }
213       }
214     }
215 
216 #ifndef SQLITE_OMIT_TRIGGER
217     /* If we have not already resolved the name, then maybe
218     ** it is a new.* or old.* trigger argument reference
219     */
220     if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
221       TriggerStack *pTriggerStack = pParse->trigStack;
222       Table *pTab = 0;
223       u32 *piColMask = 0;
224       if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
225         pExpr->iTable = pTriggerStack->newIdx;
226         assert( pTriggerStack->pTab );
227         pTab = pTriggerStack->pTab;
228         piColMask = &(pTriggerStack->newColMask);
229       }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
230         pExpr->iTable = pTriggerStack->oldIdx;
231         assert( pTriggerStack->pTab );
232         pTab = pTriggerStack->pTab;
233         piColMask = &(pTriggerStack->oldColMask);
234       }
235 
236       if( pTab ){
237         int iCol;
238         Column *pCol = pTab->aCol;
239 
240         pSchema = pTab->pSchema;
241         cntTab++;
242         for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
243           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
244             cnt++;
245             pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
246             pExpr->pTab = pTab;
247             if( iCol>=0 ){
248               testcase( iCol==31 );
249               testcase( iCol==32 );
250               *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
251             }
252             break;
253           }
254         }
255       }
256     }
257 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
258 
259     /*
260     ** Perhaps the name is a reference to the ROWID
261     */
262     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
263       cnt = 1;
264       pExpr->iColumn = -1;
265       pExpr->affinity = SQLITE_AFF_INTEGER;
266     }
267 
268     /*
269     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
270     ** might refer to an result-set alias.  This happens, for example, when
271     ** we are resolving names in the WHERE clause of the following command:
272     **
273     **     SELECT a+b AS x FROM table WHERE x<10;
274     **
275     ** In cases like this, replace pExpr with a copy of the expression that
276     ** forms the result set entry ("a+b" in the example) and return immediately.
277     ** Note that the expression in the result set should have already been
278     ** resolved by the time the WHERE clause is resolved.
279     */
280     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
281       for(j=0; j<pEList->nExpr; j++){
282         char *zAs = pEList->a[j].zName;
283         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
284           Expr *pOrig;
285           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
286           assert( pExpr->x.pList==0 );
287           assert( pExpr->x.pSelect==0 );
288           pOrig = pEList->a[j].pExpr;
289           if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
290             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
291             sqlite3DbFree(db, zCol);
292             return 2;
293           }
294           resolveAlias(pParse, pEList, j, pExpr, "");
295           cnt = 1;
296           pMatch = 0;
297           assert( zTab==0 && zDb==0 );
298           goto lookupname_end_2;
299         }
300       }
301     }
302 
303     /* Advance to the next name context.  The loop will exit when either
304     ** we have a match (cnt>0) or when we run out of name contexts.
305     */
306     if( cnt==0 ){
307       pNC = pNC->pNext;
308     }
309   }
310 
311   /*
312   ** If X and Y are NULL (in other words if only the column name Z is
313   ** supplied) and the value of Z is enclosed in double-quotes, then
314   ** Z is a string literal if it doesn't match any column names.  In that
315   ** case, we need to return right away and not make any changes to
316   ** pExpr.
317   **
318   ** Because no reference was made to outer contexts, the pNC->nRef
319   ** fields are not changed in any context.
320   */
321   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
322     sqlite3DbFree(db, zCol);
323     pExpr->op = TK_STRING;
324     pExpr->pTab = 0;
325     return 0;
326   }
327 
328   /*
329   ** cnt==0 means there was not match.  cnt>1 means there were two or
330   ** more matches.  Either way, we have an error.
331   */
332   if( cnt!=1 ){
333     const char *zErr;
334     zErr = cnt==0 ? "no such column" : "ambiguous column name";
335     if( zDb ){
336       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
337     }else if( zTab ){
338       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
339     }else{
340       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
341     }
342     pTopNC->nErr++;
343   }
344 
345   /* If a column from a table in pSrcList is referenced, then record
346   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
347   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
348   ** column number is greater than the number of bits in the bitmask
349   ** then set the high-order bit of the bitmask.
350   */
351   if( pExpr->iColumn>=0 && pMatch!=0 ){
352     int n = pExpr->iColumn;
353     testcase( n==BMS-1 );
354     if( n>=BMS ){
355       n = BMS-1;
356     }
357     assert( pMatch->iCursor==pExpr->iTable );
358     pMatch->colUsed |= ((Bitmask)1)<<n;
359   }
360 
361 lookupname_end:
362   /* Clean up and return
363   */
364   sqlite3DbFree(db, zDb);
365   sqlite3DbFree(db, zTab);
366   sqlite3ExprDelete(db, pExpr->pLeft);
367   pExpr->pLeft = 0;
368   sqlite3ExprDelete(db, pExpr->pRight);
369   pExpr->pRight = 0;
370   pExpr->op = TK_COLUMN;
371 lookupname_end_2:
372   sqlite3DbFree(db, zCol);
373   if( cnt==1 ){
374     assert( pNC!=0 );
375     sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
376     /* Increment the nRef value on all name contexts from TopNC up to
377     ** the point where the name matched. */
378     for(;;){
379       assert( pTopNC!=0 );
380       pTopNC->nRef++;
381       if( pTopNC==pNC ) break;
382       pTopNC = pTopNC->pNext;
383     }
384     return 0;
385   } else {
386     return 1;
387   }
388 }
389 
390 /*
391 ** This routine is callback for sqlite3WalkExpr().
392 **
393 ** Resolve symbolic names into TK_COLUMN operators for the current
394 ** node in the expression tree.  Return 0 to continue the search down
395 ** the tree or 2 to abort the tree walk.
396 **
397 ** This routine also does error checking and name resolution for
398 ** function names.  The operator for aggregate functions is changed
399 ** to TK_AGG_FUNCTION.
400 */
401 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
402   NameContext *pNC;
403   Parse *pParse;
404 
405   pNC = pWalker->u.pNC;
406   assert( pNC!=0 );
407   pParse = pNC->pParse;
408   assert( pParse==pWalker->pParse );
409 
410   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
411   ExprSetProperty(pExpr, EP_Resolved);
412 #ifndef NDEBUG
413   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
414     SrcList *pSrcList = pNC->pSrcList;
415     int i;
416     for(i=0; i<pNC->pSrcList->nSrc; i++){
417       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
418     }
419   }
420 #endif
421   switch( pExpr->op ){
422 
423 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
424     /* The special operator TK_ROW means use the rowid for the first
425     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
426     ** clause processing on UPDATE and DELETE statements.
427     */
428     case TK_ROW: {
429       SrcList *pSrcList = pNC->pSrcList;
430       struct SrcList_item *pItem;
431       assert( pSrcList && pSrcList->nSrc==1 );
432       pItem = pSrcList->a;
433       pExpr->op = TK_COLUMN;
434       pExpr->pTab = pItem->pTab;
435       pExpr->iTable = pItem->iCursor;
436       pExpr->iColumn = -1;
437       pExpr->affinity = SQLITE_AFF_INTEGER;
438       break;
439     }
440 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
441 
442     /* A lone identifier is the name of a column.
443     */
444     case TK_ID: {
445       lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
446       return WRC_Prune;
447     }
448 
449     /* A table name and column name:     ID.ID
450     ** Or a database, table and column:  ID.ID.ID
451     */
452     case TK_DOT: {
453       Token *pColumn;
454       Token *pTable;
455       Token *pDb;
456       Expr *pRight;
457 
458       /* if( pSrcList==0 ) break; */
459       pRight = pExpr->pRight;
460       if( pRight->op==TK_ID ){
461         pDb = 0;
462         pTable = &pExpr->pLeft->token;
463         pColumn = &pRight->token;
464       }else{
465         assert( pRight->op==TK_DOT );
466         pDb = &pExpr->pLeft->token;
467         pTable = &pRight->pLeft->token;
468         pColumn = &pRight->pRight->token;
469       }
470       lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
471       return WRC_Prune;
472     }
473 
474     /* Resolve function names
475     */
476     case TK_CONST_FUNC:
477     case TK_FUNCTION: {
478       ExprList *pList = pExpr->x.pList;    /* The argument list */
479       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
480       int no_such_func = 0;       /* True if no such function exists */
481       int wrong_num_args = 0;     /* True if wrong number of arguments */
482       int is_agg = 0;             /* True if is an aggregate function */
483       int auth;                   /* Authorization to use the function */
484       int nId;                    /* Number of characters in function name */
485       const char *zId;            /* The function name. */
486       FuncDef *pDef;              /* Information about the function */
487       u8 enc = ENC(pParse->db);   /* The database encoding */
488 
489       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
490       zId = (char*)pExpr->token.z;
491       nId = pExpr->token.n;
492       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
493       if( pDef==0 ){
494         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
495         if( pDef==0 ){
496           no_such_func = 1;
497         }else{
498           wrong_num_args = 1;
499         }
500       }else{
501         is_agg = pDef->xFunc==0;
502       }
503 #ifndef SQLITE_OMIT_AUTHORIZATION
504       if( pDef ){
505         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
506         if( auth!=SQLITE_OK ){
507           if( auth==SQLITE_DENY ){
508             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
509                                     pDef->zName);
510             pNC->nErr++;
511           }
512           pExpr->op = TK_NULL;
513           return WRC_Prune;
514         }
515       }
516 #endif
517       if( is_agg && !pNC->allowAgg ){
518         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
519         pNC->nErr++;
520         is_agg = 0;
521       }else if( no_such_func ){
522         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
523         pNC->nErr++;
524       }else if( wrong_num_args ){
525         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
526              nId, zId);
527         pNC->nErr++;
528       }
529       if( is_agg ){
530         pExpr->op = TK_AGG_FUNCTION;
531         pNC->hasAgg = 1;
532       }
533       if( is_agg ) pNC->allowAgg = 0;
534       sqlite3WalkExprList(pWalker, pList);
535       if( is_agg ) pNC->allowAgg = 1;
536       /* FIX ME:  Compute pExpr->affinity based on the expected return
537       ** type of the function
538       */
539       return WRC_Prune;
540     }
541 #ifndef SQLITE_OMIT_SUBQUERY
542     case TK_SELECT:
543     case TK_EXISTS:
544 #endif
545     case TK_IN: {
546       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
547         int nRef = pNC->nRef;
548 #ifndef SQLITE_OMIT_CHECK
549         if( pNC->isCheck ){
550           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
551         }
552 #endif
553         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
554         assert( pNC->nRef>=nRef );
555         if( nRef!=pNC->nRef ){
556           ExprSetProperty(pExpr, EP_VarSelect);
557         }
558       }
559       break;
560     }
561 #ifndef SQLITE_OMIT_CHECK
562     case TK_VARIABLE: {
563       if( pNC->isCheck ){
564         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
565       }
566       break;
567     }
568 #endif
569   }
570   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
571 }
572 
573 /*
574 ** pEList is a list of expressions which are really the result set of the
575 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
576 ** This routine checks to see if pE is a simple identifier which corresponds
577 ** to the AS-name of one of the terms of the expression list.  If it is,
578 ** this routine return an integer between 1 and N where N is the number of
579 ** elements in pEList, corresponding to the matching entry.  If there is
580 ** no match, or if pE is not a simple identifier, then this routine
581 ** return 0.
582 **
583 ** pEList has been resolved.  pE has not.
584 */
585 static int resolveAsName(
586   Parse *pParse,     /* Parsing context for error messages */
587   ExprList *pEList,  /* List of expressions to scan */
588   Expr *pE           /* Expression we are trying to match */
589 ){
590   int i;             /* Loop counter */
591 
592   if( pE->op==TK_ID || (pE->op==TK_STRING && pE->token.z[0]!='\'') ){
593     sqlite3 *db = pParse->db;
594     char *zCol = sqlite3NameFromToken(db, &pE->token);
595     if( zCol==0 ){
596       return -1;
597     }
598     for(i=0; i<pEList->nExpr; i++){
599       char *zAs = pEList->a[i].zName;
600       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
601         sqlite3DbFree(db, zCol);
602         return i+1;
603       }
604     }
605     sqlite3DbFree(db, zCol);
606   }
607   return 0;
608 }
609 
610 /*
611 ** pE is a pointer to an expression which is a single term in the
612 ** ORDER BY of a compound SELECT.  The expression has not been
613 ** name resolved.
614 **
615 ** At the point this routine is called, we already know that the
616 ** ORDER BY term is not an integer index into the result set.  That
617 ** case is handled by the calling routine.
618 **
619 ** Attempt to match pE against result set columns in the left-most
620 ** SELECT statement.  Return the index i of the matching column,
621 ** as an indication to the caller that it should sort by the i-th column.
622 ** The left-most column is 1.  In other words, the value returned is the
623 ** same integer value that would be used in the SQL statement to indicate
624 ** the column.
625 **
626 ** If there is no match, return 0.  Return -1 if an error occurs.
627 */
628 static int resolveOrderByTermToExprList(
629   Parse *pParse,     /* Parsing context for error messages */
630   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
631   Expr *pE           /* The specific ORDER BY term */
632 ){
633   int i;             /* Loop counter */
634   ExprList *pEList;  /* The columns of the result set */
635   NameContext nc;    /* Name context for resolving pE */
636 
637   assert( sqlite3ExprIsInteger(pE, &i)==0 );
638   pEList = pSelect->pEList;
639 
640   /* Resolve all names in the ORDER BY term expression
641   */
642   memset(&nc, 0, sizeof(nc));
643   nc.pParse = pParse;
644   nc.pSrcList = pSelect->pSrc;
645   nc.pEList = pEList;
646   nc.allowAgg = 1;
647   nc.nErr = 0;
648   if( sqlite3ResolveExprNames(&nc, pE) ){
649     sqlite3ErrorClear(pParse);
650     return 0;
651   }
652 
653   /* Try to match the ORDER BY expression against an expression
654   ** in the result set.  Return an 1-based index of the matching
655   ** result-set entry.
656   */
657   for(i=0; i<pEList->nExpr; i++){
658     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){
659       return i+1;
660     }
661   }
662 
663   /* If no match, return 0. */
664   return 0;
665 }
666 
667 /*
668 ** Generate an ORDER BY or GROUP BY term out-of-range error.
669 */
670 static void resolveOutOfRangeError(
671   Parse *pParse,         /* The error context into which to write the error */
672   const char *zType,     /* "ORDER" or "GROUP" */
673   int i,                 /* The index (1-based) of the term out of range */
674   int mx                 /* Largest permissible value of i */
675 ){
676   sqlite3ErrorMsg(pParse,
677     "%r %s BY term out of range - should be "
678     "between 1 and %d", i, zType, mx);
679 }
680 
681 /*
682 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
683 ** each term of the ORDER BY clause is a constant integer between 1
684 ** and N where N is the number of columns in the compound SELECT.
685 **
686 ** ORDER BY terms that are already an integer between 1 and N are
687 ** unmodified.  ORDER BY terms that are integers outside the range of
688 ** 1 through N generate an error.  ORDER BY terms that are expressions
689 ** are matched against result set expressions of compound SELECT
690 ** beginning with the left-most SELECT and working toward the right.
691 ** At the first match, the ORDER BY expression is transformed into
692 ** the integer column number.
693 **
694 ** Return the number of errors seen.
695 */
696 static int resolveCompoundOrderBy(
697   Parse *pParse,        /* Parsing context.  Leave error messages here */
698   Select *pSelect       /* The SELECT statement containing the ORDER BY */
699 ){
700   int i;
701   ExprList *pOrderBy;
702   ExprList *pEList;
703   sqlite3 *db;
704   int moreToDo = 1;
705 
706   pOrderBy = pSelect->pOrderBy;
707   if( pOrderBy==0 ) return 0;
708   db = pParse->db;
709 #if SQLITE_MAX_COLUMN
710   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
711     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
712     return 1;
713   }
714 #endif
715   for(i=0; i<pOrderBy->nExpr; i++){
716     pOrderBy->a[i].done = 0;
717   }
718   pSelect->pNext = 0;
719   while( pSelect->pPrior ){
720     pSelect->pPrior->pNext = pSelect;
721     pSelect = pSelect->pPrior;
722   }
723   while( pSelect && moreToDo ){
724     struct ExprList_item *pItem;
725     moreToDo = 0;
726     pEList = pSelect->pEList;
727     assert( pEList!=0 );
728     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
729       int iCol = -1;
730       Expr *pE, *pDup;
731       if( pItem->done ) continue;
732       pE = pItem->pExpr;
733       if( sqlite3ExprIsInteger(pE, &iCol) ){
734         if( iCol<0 || iCol>pEList->nExpr ){
735           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
736           return 1;
737         }
738       }else{
739         iCol = resolveAsName(pParse, pEList, pE);
740         if( iCol==0 ){
741           pDup = sqlite3ExprDup(db, pE, 0);
742           if( !db->mallocFailed ){
743             assert(pDup);
744             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
745           }
746           sqlite3ExprDelete(db, pDup);
747         }
748         if( iCol<0 ){
749           return 1;
750         }
751       }
752       if( iCol>0 ){
753         CollSeq *pColl = pE->pColl;
754         int flags = pE->flags & EP_ExpCollate;
755         sqlite3ExprDelete(db, pE);
756         pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0, 0, 0);
757         if( pE==0 ) return 1;
758         pE->pColl = pColl;
759         pE->flags |= EP_IntValue | flags;
760         pE->iTable = iCol;
761         pItem->iCol = (u16)iCol;
762         pItem->done = 1;
763       }else{
764         moreToDo = 1;
765       }
766     }
767     pSelect = pSelect->pNext;
768   }
769   for(i=0; i<pOrderBy->nExpr; i++){
770     if( pOrderBy->a[i].done==0 ){
771       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
772             "column in the result set", i+1);
773       return 1;
774     }
775   }
776   return 0;
777 }
778 
779 /*
780 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
781 ** the SELECT statement pSelect.  If any term is reference to a
782 ** result set expression (as determined by the ExprList.a.iCol field)
783 ** then convert that term into a copy of the corresponding result set
784 ** column.
785 **
786 ** If any errors are detected, add an error message to pParse and
787 ** return non-zero.  Return zero if no errors are seen.
788 */
789 int sqlite3ResolveOrderGroupBy(
790   Parse *pParse,        /* Parsing context.  Leave error messages here */
791   Select *pSelect,      /* The SELECT statement containing the clause */
792   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
793   const char *zType     /* "ORDER" or "GROUP" */
794 ){
795   int i;
796   sqlite3 *db = pParse->db;
797   ExprList *pEList;
798   struct ExprList_item *pItem;
799 
800   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
801 #if SQLITE_MAX_COLUMN
802   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
803     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
804     return 1;
805   }
806 #endif
807   pEList = pSelect->pEList;
808   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
809   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
810     if( pItem->iCol ){
811       if( pItem->iCol>pEList->nExpr ){
812         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
813         return 1;
814       }
815       resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
816     }
817   }
818   return 0;
819 }
820 
821 /*
822 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
823 ** The Name context of the SELECT statement is pNC.  zType is either
824 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
825 **
826 ** This routine resolves each term of the clause into an expression.
827 ** If the order-by term is an integer I between 1 and N (where N is the
828 ** number of columns in the result set of the SELECT) then the expression
829 ** in the resolution is a copy of the I-th result-set expression.  If
830 ** the order-by term is an identify that corresponds to the AS-name of
831 ** a result-set expression, then the term resolves to a copy of the
832 ** result-set expression.  Otherwise, the expression is resolved in
833 ** the usual way - using sqlite3ResolveExprNames().
834 **
835 ** This routine returns the number of errors.  If errors occur, then
836 ** an appropriate error message might be left in pParse.  (OOM errors
837 ** excepted.)
838 */
839 static int resolveOrderGroupBy(
840   NameContext *pNC,     /* The name context of the SELECT statement */
841   Select *pSelect,      /* The SELECT statement holding pOrderBy */
842   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
843   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
844 ){
845   int i;                         /* Loop counter */
846   int iCol;                      /* Column number */
847   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
848   Parse *pParse;                 /* Parsing context */
849   int nResult;                   /* Number of terms in the result set */
850 
851   if( pOrderBy==0 ) return 0;
852   nResult = pSelect->pEList->nExpr;
853   pParse = pNC->pParse;
854   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
855     Expr *pE = pItem->pExpr;
856     iCol = resolveAsName(pParse, pSelect->pEList, pE);
857     if( iCol<0 ){
858       return 1;  /* OOM error */
859     }
860     if( iCol>0 ){
861       /* If an AS-name match is found, mark this ORDER BY column as being
862       ** a copy of the iCol-th result-set column.  The subsequent call to
863       ** sqlite3ResolveOrderGroupBy() will convert the expression to a
864       ** copy of the iCol-th result-set expression. */
865       pItem->iCol = (u16)iCol;
866       continue;
867     }
868     if( sqlite3ExprIsInteger(pE, &iCol) ){
869       /* The ORDER BY term is an integer constant.  Again, set the column
870       ** number so that sqlite3ResolveOrderGroupBy() will convert the
871       ** order-by term to a copy of the result-set expression */
872       if( iCol<1 ){
873         resolveOutOfRangeError(pParse, zType, i+1, nResult);
874         return 1;
875       }
876       pItem->iCol = (u16)iCol;
877       continue;
878     }
879 
880     /* Otherwise, treat the ORDER BY term as an ordinary expression */
881     pItem->iCol = 0;
882     if( sqlite3ResolveExprNames(pNC, pE) ){
883       return 1;
884     }
885   }
886   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
887 }
888 
889 /*
890 ** Resolve names in the SELECT statement p and all of its descendents.
891 */
892 static int resolveSelectStep(Walker *pWalker, Select *p){
893   NameContext *pOuterNC;  /* Context that contains this SELECT */
894   NameContext sNC;        /* Name context of this SELECT */
895   int isCompound;         /* True if p is a compound select */
896   int nCompound;          /* Number of compound terms processed so far */
897   Parse *pParse;          /* Parsing context */
898   ExprList *pEList;       /* Result set expression list */
899   int i;                  /* Loop counter */
900   ExprList *pGroupBy;     /* The GROUP BY clause */
901   Select *pLeftmost;      /* Left-most of SELECT of a compound */
902   sqlite3 *db;            /* Database connection */
903 
904 
905   assert( p!=0 );
906   if( p->selFlags & SF_Resolved ){
907     return WRC_Prune;
908   }
909   pOuterNC = pWalker->u.pNC;
910   pParse = pWalker->pParse;
911   db = pParse->db;
912 
913   /* Normally sqlite3SelectExpand() will be called first and will have
914   ** already expanded this SELECT.  However, if this is a subquery within
915   ** an expression, sqlite3ResolveExprNames() will be called without a
916   ** prior call to sqlite3SelectExpand().  When that happens, let
917   ** sqlite3SelectPrep() do all of the processing for this SELECT.
918   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
919   ** this routine in the correct order.
920   */
921   if( (p->selFlags & SF_Expanded)==0 ){
922     sqlite3SelectPrep(pParse, p, pOuterNC);
923     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
924   }
925 
926   isCompound = p->pPrior!=0;
927   nCompound = 0;
928   pLeftmost = p;
929   while( p ){
930     assert( (p->selFlags & SF_Expanded)!=0 );
931     assert( (p->selFlags & SF_Resolved)==0 );
932     p->selFlags |= SF_Resolved;
933 
934     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
935     ** are not allowed to refer to any names, so pass an empty NameContext.
936     */
937     memset(&sNC, 0, sizeof(sNC));
938     sNC.pParse = pParse;
939     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
940         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
941       return WRC_Abort;
942     }
943 
944     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
945     ** resolve the result-set expression list.
946     */
947     sNC.allowAgg = 1;
948     sNC.pSrcList = p->pSrc;
949     sNC.pNext = pOuterNC;
950 
951     /* Resolve names in the result set. */
952     pEList = p->pEList;
953     assert( pEList!=0 );
954     for(i=0; i<pEList->nExpr; i++){
955       Expr *pX = pEList->a[i].pExpr;
956       if( sqlite3ResolveExprNames(&sNC, pX) ){
957         return WRC_Abort;
958       }
959     }
960 
961     /* Recursively resolve names in all subqueries
962     */
963     for(i=0; i<p->pSrc->nSrc; i++){
964       struct SrcList_item *pItem = &p->pSrc->a[i];
965       if( pItem->pSelect ){
966         const char *zSavedContext = pParse->zAuthContext;
967         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
968         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
969         pParse->zAuthContext = zSavedContext;
970         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
971       }
972     }
973 
974     /* If there are no aggregate functions in the result-set, and no GROUP BY
975     ** expression, do not allow aggregates in any of the other expressions.
976     */
977     assert( (p->selFlags & SF_Aggregate)==0 );
978     pGroupBy = p->pGroupBy;
979     if( pGroupBy || sNC.hasAgg ){
980       p->selFlags |= SF_Aggregate;
981     }else{
982       sNC.allowAgg = 0;
983     }
984 
985     /* If a HAVING clause is present, then there must be a GROUP BY clause.
986     */
987     if( p->pHaving && !pGroupBy ){
988       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
989       return WRC_Abort;
990     }
991 
992     /* Add the expression list to the name-context before parsing the
993     ** other expressions in the SELECT statement. This is so that
994     ** expressions in the WHERE clause (etc.) can refer to expressions by
995     ** aliases in the result set.
996     **
997     ** Minor point: If this is the case, then the expression will be
998     ** re-evaluated for each reference to it.
999     */
1000     sNC.pEList = p->pEList;
1001     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
1002        sqlite3ResolveExprNames(&sNC, p->pHaving)
1003     ){
1004       return WRC_Abort;
1005     }
1006 
1007     /* The ORDER BY and GROUP BY clauses may not refer to terms in
1008     ** outer queries
1009     */
1010     sNC.pNext = 0;
1011     sNC.allowAgg = 1;
1012 
1013     /* Process the ORDER BY clause for singleton SELECT statements.
1014     ** The ORDER BY clause for compounds SELECT statements is handled
1015     ** below, after all of the result-sets for all of the elements of
1016     ** the compound have been resolved.
1017     */
1018     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
1019       return WRC_Abort;
1020     }
1021     if( db->mallocFailed ){
1022       return WRC_Abort;
1023     }
1024 
1025     /* Resolve the GROUP BY clause.  At the same time, make sure
1026     ** the GROUP BY clause does not contain aggregate functions.
1027     */
1028     if( pGroupBy ){
1029       struct ExprList_item *pItem;
1030 
1031       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
1032         return WRC_Abort;
1033       }
1034       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
1035         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
1036           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
1037               "the GROUP BY clause");
1038           return WRC_Abort;
1039         }
1040       }
1041     }
1042 
1043     /* Advance to the next term of the compound
1044     */
1045     p = p->pPrior;
1046     nCompound++;
1047   }
1048 
1049   /* Resolve the ORDER BY on a compound SELECT after all terms of
1050   ** the compound have been resolved.
1051   */
1052   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
1053     return WRC_Abort;
1054   }
1055 
1056   return WRC_Prune;
1057 }
1058 
1059 /*
1060 ** This routine walks an expression tree and resolves references to
1061 ** table columns and result-set columns.  At the same time, do error
1062 ** checking on function usage and set a flag if any aggregate functions
1063 ** are seen.
1064 **
1065 ** To resolve table columns references we look for nodes (or subtrees) of the
1066 ** form X.Y.Z or Y.Z or just Z where
1067 **
1068 **      X:   The name of a database.  Ex:  "main" or "temp" or
1069 **           the symbolic name assigned to an ATTACH-ed database.
1070 **
1071 **      Y:   The name of a table in a FROM clause.  Or in a trigger
1072 **           one of the special names "old" or "new".
1073 **
1074 **      Z:   The name of a column in table Y.
1075 **
1076 ** The node at the root of the subtree is modified as follows:
1077 **
1078 **    Expr.op        Changed to TK_COLUMN
1079 **    Expr.pTab      Points to the Table object for X.Y
1080 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
1081 **    Expr.iTable    The VDBE cursor number for X.Y
1082 **
1083 **
1084 ** To resolve result-set references, look for expression nodes of the
1085 ** form Z (with no X and Y prefix) where the Z matches the right-hand
1086 ** size of an AS clause in the result-set of a SELECT.  The Z expression
1087 ** is replaced by a copy of the left-hand side of the result-set expression.
1088 ** Table-name and function resolution occurs on the substituted expression
1089 ** tree.  For example, in:
1090 **
1091 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
1092 **
1093 ** The "x" term of the order by is replaced by "a+b" to render:
1094 **
1095 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
1096 **
1097 ** Function calls are checked to make sure that the function is
1098 ** defined and that the correct number of arguments are specified.
1099 ** If the function is an aggregate function, then the pNC->hasAgg is
1100 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
1101 ** If an expression contains aggregate functions then the EP_Agg
1102 ** property on the expression is set.
1103 **
1104 ** An error message is left in pParse if anything is amiss.  The number
1105 ** if errors is returned.
1106 */
1107 int sqlite3ResolveExprNames(
1108   NameContext *pNC,       /* Namespace to resolve expressions in. */
1109   Expr *pExpr             /* The expression to be analyzed. */
1110 ){
1111   int savedHasAgg;
1112   Walker w;
1113 
1114   if( pExpr==0 ) return 0;
1115 #if SQLITE_MAX_EXPR_DEPTH>0
1116   {
1117     Parse *pParse = pNC->pParse;
1118     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
1119       return 1;
1120     }
1121     pParse->nHeight += pExpr->nHeight;
1122   }
1123 #endif
1124   savedHasAgg = pNC->hasAgg;
1125   pNC->hasAgg = 0;
1126   w.xExprCallback = resolveExprStep;
1127   w.xSelectCallback = resolveSelectStep;
1128   w.pParse = pNC->pParse;
1129   w.u.pNC = pNC;
1130   sqlite3WalkExpr(&w, pExpr);
1131 #if SQLITE_MAX_EXPR_DEPTH>0
1132   pNC->pParse->nHeight -= pExpr->nHeight;
1133 #endif
1134   if( pNC->nErr>0 ){
1135     ExprSetProperty(pExpr, EP_Error);
1136   }
1137   if( pNC->hasAgg ){
1138     ExprSetProperty(pExpr, EP_Agg);
1139   }else if( savedHasAgg ){
1140     pNC->hasAgg = 1;
1141   }
1142   return ExprHasProperty(pExpr, EP_Error);
1143 }
1144 
1145 
1146 /*
1147 ** Resolve all names in all expressions of a SELECT and in all
1148 ** decendents of the SELECT, including compounds off of p->pPrior,
1149 ** subqueries in expressions, and subqueries used as FROM clause
1150 ** terms.
1151 **
1152 ** See sqlite3ResolveExprNames() for a description of the kinds of
1153 ** transformations that occur.
1154 **
1155 ** All SELECT statements should have been expanded using
1156 ** sqlite3SelectExpand() prior to invoking this routine.
1157 */
1158 void sqlite3ResolveSelectNames(
1159   Parse *pParse,         /* The parser context */
1160   Select *p,             /* The SELECT statement being coded. */
1161   NameContext *pOuterNC  /* Name context for parent SELECT statement */
1162 ){
1163   Walker w;
1164 
1165   assert( p!=0 );
1166   w.xExprCallback = resolveExprStep;
1167   w.xSelectCallback = resolveSelectStep;
1168   w.pParse = pParse;
1169   w.u.pNC = pOuterNC;
1170   sqlite3WalkSelect(&w, p);
1171 }
1172