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