blob: 317ca3d81b06d93370e61564ac41c6dd852423e6 [file] [log] [blame]
drh7d10d5a2008-08-20 16:35:10 +00001/*
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.
drh7d10d5a2008-08-20 16:35:10 +000016*/
17#include "sqliteInt.h"
18#include <stdlib.h>
19#include <string.h>
20
21/*
drhed551b92012-08-23 19:46:11 +000022** Walk the expression tree pExpr and increase the aggregate function
23** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
24** This needs to occur when copying a TK_AGG_FUNCTION node from an
25** outer query into an inner subquery.
26**
27** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
28** is a helper function - a callback for the tree walker.
29*/
30static int incrAggDepth(Walker *pWalker, Expr *pExpr){
31 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
32 return WRC_Continue;
33}
34static void incrAggFunctionDepth(Expr *pExpr, int N){
35 if( N>0 ){
36 Walker w;
37 memset(&w, 0, sizeof(w));
38 w.xExprCallback = incrAggDepth;
39 w.u.i = N;
40 sqlite3WalkExpr(&w, pExpr);
41 }
42}
43
44/*
drh8b213892008-08-29 02:14:02 +000045** Turn the pExpr expression into an alias for the iCol-th column of the
46** result set in pEList.
47**
48** If the result set column is a simple column reference, then this routine
49** makes an exact copy. But for any other kind of expression, this
50** routine make a copy of the result set column as the argument to the
51** TK_AS operator. The TK_AS operator causes the expression to be
52** evaluated just once and then reused for each alias.
53**
54** The reason for suppressing the TK_AS term when the expression is a simple
55** column reference is so that the column reference will be recognized as
56** usable by indices within the WHERE clause processing logic.
57**
58** Hack: The TK_AS operator is inhibited if zType[0]=='G'. This means
59** that in a GROUP BY clause, the expression is evaluated twice. Hence:
60**
61** SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
62**
63** Is equivalent to:
64**
65** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
66**
67** The result of random()%5 in the GROUP BY clause is probably different
68** from the result in the result-set. We might fix this someday. Or
69** then again, we might not...
drhed551b92012-08-23 19:46:11 +000070**
drh0a8a4062012-12-07 18:38:16 +000071** If the reference is followed by a COLLATE operator, then make sure
72** the COLLATE operator is preserved. For example:
73**
74** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
75**
76** Should be transformed into:
77**
78** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
79**
drhed551b92012-08-23 19:46:11 +000080** The nSubquery parameter specifies how many levels of subquery the
81** alias is removed from the original expression. The usually value is
82** zero but it might be more if the alias is contained within a subquery
83** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
84** structures must be increased by the nSubquery amount.
drh8b213892008-08-29 02:14:02 +000085*/
86static void resolveAlias(
87 Parse *pParse, /* Parsing context */
88 ExprList *pEList, /* A result set */
89 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
90 Expr *pExpr, /* Transform this into an alias to the result set */
drhed551b92012-08-23 19:46:11 +000091 const char *zType, /* "GROUP" or "ORDER" or "" */
92 int nSubquery /* Number of subqueries that the label is moving */
drh8b213892008-08-29 02:14:02 +000093){
94 Expr *pOrig; /* The iCol-th column of the result set */
95 Expr *pDup; /* Copy of pOrig */
96 sqlite3 *db; /* The database connection */
97
98 assert( iCol>=0 && iCol<pEList->nExpr );
99 pOrig = pEList->a[iCol].pExpr;
100 assert( pOrig!=0 );
101 assert( pOrig->flags & EP_Resolved );
102 db = pParse->db;
drh0a8a4062012-12-07 18:38:16 +0000103 pDup = sqlite3ExprDup(db, pOrig, 0);
104 if( pDup==0 ) return;
drhb7916a72009-05-27 10:31:29 +0000105 if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
drhed551b92012-08-23 19:46:11 +0000106 incrAggFunctionDepth(pDup, nSubquery);
drh8b213892008-08-29 02:14:02 +0000107 pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
108 if( pDup==0 ) return;
109 if( pEList->a[iCol].iAlias==0 ){
drhea678832008-12-10 19:26:22 +0000110 pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
drh8b213892008-08-29 02:14:02 +0000111 }
112 pDup->iTable = pEList->a[iCol].iAlias;
113 }
drh0a8a4062012-12-07 18:38:16 +0000114 if( pExpr->op==TK_COLLATE ){
115 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
116 }
danf6963f92009-11-23 14:39:14 +0000117
118 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
119 ** prevents ExprDelete() from deleting the Expr structure itself,
120 ** allowing it to be repopulated by the memcpy() on the following line.
drhbd13d342012-12-07 21:02:47 +0000121 ** The pExpr->u.zToken might point into memory that will be freed by the
122 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
123 ** make a copy of the token before doing the sqlite3DbFree().
danf6963f92009-11-23 14:39:14 +0000124 */
125 ExprSetProperty(pExpr, EP_Static);
126 sqlite3ExprDelete(db, pExpr);
drh8b213892008-08-29 02:14:02 +0000127 memcpy(pExpr, pDup, sizeof(*pExpr));
drh0a8a4062012-12-07 18:38:16 +0000128 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
129 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
130 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
131 pExpr->flags2 |= EP2_MallocedToken;
132 }
drh8b213892008-08-29 02:14:02 +0000133 sqlite3DbFree(db, pDup);
134}
135
drhe802c5d2011-10-18 18:10:40 +0000136
137/*
138** Return TRUE if the name zCol occurs anywhere in the USING clause.
139**
140** Return FALSE if the USING clause is NULL or if it does not contain
141** zCol.
142*/
143static int nameInUsingClause(IdList *pUsing, const char *zCol){
144 if( pUsing ){
145 int k;
146 for(k=0; k<pUsing->nId; k++){
147 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
148 }
149 }
150 return 0;
151}
152
153
drh8b213892008-08-29 02:14:02 +0000154/*
drh7d10d5a2008-08-20 16:35:10 +0000155** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
156** that name in the set of source tables in pSrcList and make the pExpr
157** expression node refer back to that source column. The following changes
158** are made to pExpr:
159**
160** pExpr->iDb Set the index in db->aDb[] of the database X
161** (even if X is implied).
162** pExpr->iTable Set to the cursor number for the table obtained
163** from pSrcList.
164** pExpr->pTab Points to the Table structure of X.Y (even if
165** X and/or Y are implied.)
166** pExpr->iColumn Set to the column number within the table.
167** pExpr->op Set to TK_COLUMN.
168** pExpr->pLeft Any expression this points to is deleted
169** pExpr->pRight Any expression this points to is deleted.
170**
drhb7916a72009-05-27 10:31:29 +0000171** The zDb variable is the name of the database (the "X"). This value may be
drh7d10d5a2008-08-20 16:35:10 +0000172** NULL meaning that name is of the form Y.Z or Z. Any available database
drhb7916a72009-05-27 10:31:29 +0000173** can be used. The zTable variable is the name of the table (the "Y"). This
174** value can be NULL if zDb is also NULL. If zTable is NULL it
drh7d10d5a2008-08-20 16:35:10 +0000175** means that the form of the name is Z and that columns from any table
176** can be used.
177**
178** If the name cannot be resolved unambiguously, leave an error message
drhf7828b52009-06-15 23:15:59 +0000179** in pParse and return WRC_Abort. Return WRC_Prune on success.
drh7d10d5a2008-08-20 16:35:10 +0000180*/
181static int lookupName(
182 Parse *pParse, /* The parsing context */
drhb7916a72009-05-27 10:31:29 +0000183 const char *zDb, /* Name of the database containing table, or NULL */
184 const char *zTab, /* Name of table containing column, or NULL */
185 const char *zCol, /* Name of the column. */
drh7d10d5a2008-08-20 16:35:10 +0000186 NameContext *pNC, /* The name context used to resolve the name */
187 Expr *pExpr /* Make this EXPR node point to the selected column */
188){
drhed551b92012-08-23 19:46:11 +0000189 int i, j; /* Loop counters */
drh7d10d5a2008-08-20 16:35:10 +0000190 int cnt = 0; /* Number of matching column names */
191 int cntTab = 0; /* Number of matching table names */
drhed551b92012-08-23 19:46:11 +0000192 int nSubquery = 0; /* How many levels of subquery */
drh7d10d5a2008-08-20 16:35:10 +0000193 sqlite3 *db = pParse->db; /* The database connection */
194 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
195 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
196 NameContext *pTopNC = pNC; /* First namecontext in the list */
197 Schema *pSchema = 0; /* Schema of the expression */
dan2bd93512009-08-31 08:22:46 +0000198 int isTrigger = 0;
drh7d10d5a2008-08-20 16:35:10 +0000199
drhb7916a72009-05-27 10:31:29 +0000200 assert( pNC ); /* the name context cannot be NULL. */
201 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
drh5a05be12012-10-09 18:51:44 +0000202 assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drh7d10d5a2008-08-20 16:35:10 +0000203
204 /* Initialize the node to no-match */
205 pExpr->iTable = -1;
206 pExpr->pTab = 0;
drh33e619f2009-05-28 01:00:55 +0000207 ExprSetIrreducible(pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000208
drh8f25d182012-12-19 02:36:45 +0000209 /* Translate the schema name in zDb into a pointer to the corresponding
210 ** schema. If not found, pSchema will remain NULL and nothing will match
211 ** resulting in an appropriate error message toward the end of this routine
212 */
213 if( zDb ){
214 for(i=0; i<db->nDb; i++){
215 assert( db->aDb[i].zName );
216 if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
217 pSchema = db->aDb[i].pSchema;
218 break;
219 }
220 }
221 }
222
drh7d10d5a2008-08-20 16:35:10 +0000223 /* Start at the inner-most context and move outward until a match is found */
224 while( pNC && cnt==0 ){
225 ExprList *pEList;
226 SrcList *pSrcList = pNC->pSrcList;
227
228 if( pSrcList ){
229 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
230 Table *pTab;
drh7d10d5a2008-08-20 16:35:10 +0000231 Column *pCol;
232
233 pTab = pItem->pTab;
drhf4366202008-08-25 12:14:08 +0000234 assert( pTab!=0 && pTab->zName!=0 );
drh7d10d5a2008-08-20 16:35:10 +0000235 assert( pTab->nCol>0 );
drh8f25d182012-12-19 02:36:45 +0000236 if( zDb && pTab->pSchema!=pSchema ){
237 continue;
238 }
239 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
240 ExprList *pEList = pItem->pSelect->pEList;
241 int hit = 0;
242 for(j=0; j<pEList->nExpr; j++){
243 if( zTab && sqlite3StrICmp(pEList->a[j].zSpan, zTab)!=0 ) continue;
244 if( sqlite3StrICmp(pEList->a[j].zName, zCol)==0 ){
245 cnt++;
246 cntTab = 2;
247 pMatch = pItem;
248 pExpr->iColumn = j;
249 }
250 }
251 if( hit || zTab==0 ) continue;
252 }
drh7d10d5a2008-08-20 16:35:10 +0000253 if( zTab ){
drh8f25d182012-12-19 02:36:45 +0000254 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
255 assert( zTabName!=0 );
256 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
257 continue;
drh7d10d5a2008-08-20 16:35:10 +0000258 }
259 }
260 if( 0==(cntTab++) ){
drh7d10d5a2008-08-20 16:35:10 +0000261 pMatch = pItem;
262 }
263 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
264 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drhe802c5d2011-10-18 18:10:40 +0000265 /* If there has been exactly one prior match and this match
266 ** is for the right-hand table of a NATURAL JOIN or is in a
267 ** USING clause, then skip this match.
268 */
269 if( cnt==1 ){
270 if( pItem->jointype & JT_NATURAL ) continue;
271 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
272 }
drh7d10d5a2008-08-20 16:35:10 +0000273 cnt++;
drh7d10d5a2008-08-20 16:35:10 +0000274 pMatch = pItem;
drh7d10d5a2008-08-20 16:35:10 +0000275 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
shanecf697392009-06-01 16:53:09 +0000276 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
drh7d10d5a2008-08-20 16:35:10 +0000277 break;
278 }
279 }
280 }
drh8f25d182012-12-19 02:36:45 +0000281 if( pMatch ){
282 pExpr->iTable = pMatch->iCursor;
283 pExpr->pTab = pMatch->pTab;
284 pSchema = pExpr->pTab->pSchema;
285 }
286 } /* if( pSrcList ) */
drh7d10d5a2008-08-20 16:35:10 +0000287
288#ifndef SQLITE_OMIT_TRIGGER
289 /* If we have not already resolved the name, then maybe
290 ** it is a new.* or old.* trigger argument reference
291 */
dan165921a2009-08-28 18:53:45 +0000292 if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
dan65a7cd12009-09-01 12:16:01 +0000293 int op = pParse->eTriggerOp;
drh7d10d5a2008-08-20 16:35:10 +0000294 Table *pTab = 0;
dan65a7cd12009-09-01 12:16:01 +0000295 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
296 if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
dan165921a2009-08-28 18:53:45 +0000297 pExpr->iTable = 1;
298 pTab = pParse->pTriggerTab;
dan65a7cd12009-09-01 12:16:01 +0000299 }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
dan165921a2009-08-28 18:53:45 +0000300 pExpr->iTable = 0;
301 pTab = pParse->pTriggerTab;
drh7d10d5a2008-08-20 16:35:10 +0000302 }
303
304 if( pTab ){
305 int iCol;
drh7d10d5a2008-08-20 16:35:10 +0000306 pSchema = pTab->pSchema;
307 cntTab++;
drh25e978d2009-12-29 23:39:04 +0000308 for(iCol=0; iCol<pTab->nCol; iCol++){
309 Column *pCol = &pTab->aCol[iCol];
310 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
311 if( iCol==pTab->iPKey ){
312 iCol = -1;
drh7d10d5a2008-08-20 16:35:10 +0000313 }
drh25e978d2009-12-29 23:39:04 +0000314 break;
drh7d10d5a2008-08-20 16:35:10 +0000315 }
316 }
drh25e978d2009-12-29 23:39:04 +0000317 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
drhc79c7612010-01-01 18:57:48 +0000318 iCol = -1; /* IMP: R-44911-55124 */
drh25e978d2009-12-29 23:39:04 +0000319 }
dan2bd93512009-08-31 08:22:46 +0000320 if( iCol<pTab->nCol ){
321 cnt++;
322 if( iCol<0 ){
323 pExpr->affinity = SQLITE_AFF_INTEGER;
dan2832ad42009-08-31 15:27:27 +0000324 }else if( pExpr->iTable==0 ){
325 testcase( iCol==31 );
326 testcase( iCol==32 );
327 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
danbb5f1682009-11-27 12:12:34 +0000328 }else{
329 testcase( iCol==31 );
330 testcase( iCol==32 );
331 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
dan2bd93512009-08-31 08:22:46 +0000332 }
shanecea72b22009-09-07 04:38:36 +0000333 pExpr->iColumn = (i16)iCol;
dan2bd93512009-08-31 08:22:46 +0000334 pExpr->pTab = pTab;
335 isTrigger = 1;
336 }
drh7d10d5a2008-08-20 16:35:10 +0000337 }
338 }
339#endif /* !defined(SQLITE_OMIT_TRIGGER) */
340
341 /*
342 ** Perhaps the name is a reference to the ROWID
343 */
344 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
345 cnt = 1;
drhc79c7612010-01-01 18:57:48 +0000346 pExpr->iColumn = -1; /* IMP: R-44911-55124 */
drh7d10d5a2008-08-20 16:35:10 +0000347 pExpr->affinity = SQLITE_AFF_INTEGER;
348 }
349
350 /*
351 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
352 ** might refer to an result-set alias. This happens, for example, when
353 ** we are resolving names in the WHERE clause of the following command:
354 **
355 ** SELECT a+b AS x FROM table WHERE x<10;
356 **
357 ** In cases like this, replace pExpr with a copy of the expression that
358 ** forms the result set entry ("a+b" in the example) and return immediately.
359 ** Note that the expression in the result set should have already been
360 ** resolved by the time the WHERE clause is resolved.
361 */
362 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
363 for(j=0; j<pEList->nExpr; j++){
364 char *zAs = pEList->a[j].zName;
365 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8b213892008-08-29 02:14:02 +0000366 Expr *pOrig;
drh7d10d5a2008-08-20 16:35:10 +0000367 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
danielk19776ab3a2e2009-02-19 14:39:25 +0000368 assert( pExpr->x.pList==0 );
369 assert( pExpr->x.pSelect==0 );
drh7d10d5a2008-08-20 16:35:10 +0000370 pOrig = pEList->a[j].pExpr;
drha51009b2012-05-21 19:11:25 +0000371 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
drh7d10d5a2008-08-20 16:35:10 +0000372 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drhf7828b52009-06-15 23:15:59 +0000373 return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +0000374 }
drhed551b92012-08-23 19:46:11 +0000375 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
drh7d10d5a2008-08-20 16:35:10 +0000376 cnt = 1;
377 pMatch = 0;
378 assert( zTab==0 && zDb==0 );
drhb7916a72009-05-27 10:31:29 +0000379 goto lookupname_end;
drh7d10d5a2008-08-20 16:35:10 +0000380 }
381 }
382 }
383
384 /* Advance to the next name context. The loop will exit when either
385 ** we have a match (cnt>0) or when we run out of name contexts.
386 */
387 if( cnt==0 ){
388 pNC = pNC->pNext;
drhed551b92012-08-23 19:46:11 +0000389 nSubquery++;
drh7d10d5a2008-08-20 16:35:10 +0000390 }
391 }
392
393 /*
394 ** If X and Y are NULL (in other words if only the column name Z is
395 ** supplied) and the value of Z is enclosed in double-quotes, then
396 ** Z is a string literal if it doesn't match any column names. In that
397 ** case, we need to return right away and not make any changes to
398 ** pExpr.
399 **
400 ** Because no reference was made to outer contexts, the pNC->nRef
401 ** fields are not changed in any context.
402 */
drh24fb6272009-05-01 21:13:36 +0000403 if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
drh7d10d5a2008-08-20 16:35:10 +0000404 pExpr->op = TK_STRING;
drh1885d1c2008-10-19 21:03:27 +0000405 pExpr->pTab = 0;
drhf7828b52009-06-15 23:15:59 +0000406 return WRC_Prune;
drh7d10d5a2008-08-20 16:35:10 +0000407 }
408
409 /*
410 ** cnt==0 means there was not match. cnt>1 means there were two or
411 ** more matches. Either way, we have an error.
412 */
413 if( cnt!=1 ){
414 const char *zErr;
415 zErr = cnt==0 ? "no such column" : "ambiguous column name";
416 if( zDb ){
417 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
418 }else if( zTab ){
419 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
420 }else{
421 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
422 }
dan1db95102010-06-28 10:15:19 +0000423 pParse->checkSchema = 1;
drh7d10d5a2008-08-20 16:35:10 +0000424 pTopNC->nErr++;
425 }
426
427 /* If a column from a table in pSrcList is referenced, then record
428 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
429 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
430 ** column number is greater than the number of bits in the bitmask
431 ** then set the high-order bit of the bitmask.
432 */
danielk19772d2e7bd2009-02-24 10:14:40 +0000433 if( pExpr->iColumn>=0 && pMatch!=0 ){
434 int n = pExpr->iColumn;
435 testcase( n==BMS-1 );
436 if( n>=BMS ){
437 n = BMS-1;
drh7d10d5a2008-08-20 16:35:10 +0000438 }
danielk19772d2e7bd2009-02-24 10:14:40 +0000439 assert( pMatch->iCursor==pExpr->iTable );
440 pMatch->colUsed |= ((Bitmask)1)<<n;
drh7d10d5a2008-08-20 16:35:10 +0000441 }
442
drh7d10d5a2008-08-20 16:35:10 +0000443 /* Clean up and return
444 */
drh7d10d5a2008-08-20 16:35:10 +0000445 sqlite3ExprDelete(db, pExpr->pLeft);
446 pExpr->pLeft = 0;
447 sqlite3ExprDelete(db, pExpr->pRight);
448 pExpr->pRight = 0;
dan2bd93512009-08-31 08:22:46 +0000449 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
drhb7916a72009-05-27 10:31:29 +0000450lookupname_end:
drh7d10d5a2008-08-20 16:35:10 +0000451 if( cnt==1 ){
452 assert( pNC!=0 );
453 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
454 /* Increment the nRef value on all name contexts from TopNC up to
455 ** the point where the name matched. */
456 for(;;){
457 assert( pTopNC!=0 );
458 pTopNC->nRef++;
459 if( pTopNC==pNC ) break;
460 pTopNC = pTopNC->pNext;
461 }
drhf7828b52009-06-15 23:15:59 +0000462 return WRC_Prune;
drh7d10d5a2008-08-20 16:35:10 +0000463 } else {
drhf7828b52009-06-15 23:15:59 +0000464 return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +0000465 }
466}
467
468/*
danf7b0b0a2009-10-19 15:52:32 +0000469** Allocate and return a pointer to an expression to load the column iCol
drh9e481652010-04-08 17:35:34 +0000470** from datasource iSrc in SrcList pSrc.
danf7b0b0a2009-10-19 15:52:32 +0000471*/
472Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
473 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
474 if( p ){
475 struct SrcList_item *pItem = &pSrc->a[iSrc];
476 p->pTab = pItem->pTab;
477 p->iTable = pItem->iCursor;
478 if( p->pTab->iPKey==iCol ){
479 p->iColumn = -1;
480 }else{
drh8677d302009-11-04 13:17:14 +0000481 p->iColumn = (ynVar)iCol;
drh7caba662010-04-08 15:01:44 +0000482 testcase( iCol==BMS );
483 testcase( iCol==BMS-1 );
danf7b0b0a2009-10-19 15:52:32 +0000484 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
485 }
486 ExprSetProperty(p, EP_Resolved);
487 }
488 return p;
489}
490
491/*
drh7d10d5a2008-08-20 16:35:10 +0000492** This routine is callback for sqlite3WalkExpr().
493**
494** Resolve symbolic names into TK_COLUMN operators for the current
495** node in the expression tree. Return 0 to continue the search down
496** the tree or 2 to abort the tree walk.
497**
498** This routine also does error checking and name resolution for
499** function names. The operator for aggregate functions is changed
500** to TK_AGG_FUNCTION.
501*/
502static int resolveExprStep(Walker *pWalker, Expr *pExpr){
503 NameContext *pNC;
504 Parse *pParse;
505
drh7d10d5a2008-08-20 16:35:10 +0000506 pNC = pWalker->u.pNC;
507 assert( pNC!=0 );
508 pParse = pNC->pParse;
509 assert( pParse==pWalker->pParse );
510
511 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
512 ExprSetProperty(pExpr, EP_Resolved);
513#ifndef NDEBUG
514 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
515 SrcList *pSrcList = pNC->pSrcList;
516 int i;
517 for(i=0; i<pNC->pSrcList->nSrc; i++){
518 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
519 }
520 }
521#endif
522 switch( pExpr->op ){
drh41204f12008-10-06 13:54:35 +0000523
shane273f6192008-10-10 04:34:16 +0000524#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
drh41204f12008-10-06 13:54:35 +0000525 /* The special operator TK_ROW means use the rowid for the first
526 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
527 ** clause processing on UPDATE and DELETE statements.
528 */
529 case TK_ROW: {
530 SrcList *pSrcList = pNC->pSrcList;
531 struct SrcList_item *pItem;
532 assert( pSrcList && pSrcList->nSrc==1 );
533 pItem = pSrcList->a;
534 pExpr->op = TK_COLUMN;
535 pExpr->pTab = pItem->pTab;
536 pExpr->iTable = pItem->iCursor;
537 pExpr->iColumn = -1;
538 pExpr->affinity = SQLITE_AFF_INTEGER;
539 break;
540 }
shane273f6192008-10-10 04:34:16 +0000541#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
drh41204f12008-10-06 13:54:35 +0000542
drh7d10d5a2008-08-20 16:35:10 +0000543 /* A lone identifier is the name of a column.
544 */
545 case TK_ID: {
drhf7828b52009-06-15 23:15:59 +0000546 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000547 }
548
549 /* A table name and column name: ID.ID
550 ** Or a database, table and column: ID.ID.ID
551 */
552 case TK_DOT: {
drhb7916a72009-05-27 10:31:29 +0000553 const char *zColumn;
554 const char *zTable;
555 const char *zDb;
drh7d10d5a2008-08-20 16:35:10 +0000556 Expr *pRight;
557
558 /* if( pSrcList==0 ) break; */
559 pRight = pExpr->pRight;
560 if( pRight->op==TK_ID ){
drhb7916a72009-05-27 10:31:29 +0000561 zDb = 0;
drh33e619f2009-05-28 01:00:55 +0000562 zTable = pExpr->pLeft->u.zToken;
563 zColumn = pRight->u.zToken;
drh7d10d5a2008-08-20 16:35:10 +0000564 }else{
565 assert( pRight->op==TK_DOT );
drh33e619f2009-05-28 01:00:55 +0000566 zDb = pExpr->pLeft->u.zToken;
567 zTable = pRight->pLeft->u.zToken;
568 zColumn = pRight->pRight->u.zToken;
drh7d10d5a2008-08-20 16:35:10 +0000569 }
drhf7828b52009-06-15 23:15:59 +0000570 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000571 }
572
573 /* Resolve function names
574 */
575 case TK_CONST_FUNC:
576 case TK_FUNCTION: {
danielk19776ab3a2e2009-02-19 14:39:25 +0000577 ExprList *pList = pExpr->x.pList; /* The argument list */
578 int n = pList ? pList->nExpr : 0; /* Number of arguments */
drh7d10d5a2008-08-20 16:35:10 +0000579 int no_such_func = 0; /* True if no such function exists */
580 int wrong_num_args = 0; /* True if wrong number of arguments */
581 int is_agg = 0; /* True if is an aggregate function */
582 int auth; /* Authorization to use the function */
583 int nId; /* Number of characters in function name */
584 const char *zId; /* The function name. */
585 FuncDef *pDef; /* Information about the function */
drhea678832008-12-10 19:26:22 +0000586 u8 enc = ENC(pParse->db); /* The database encoding */
drh7d10d5a2008-08-20 16:35:10 +0000587
drh73c0fdc2009-06-15 18:32:36 +0000588 testcase( pExpr->op==TK_CONST_FUNC );
danielk19776ab3a2e2009-02-19 14:39:25 +0000589 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drh33e619f2009-05-28 01:00:55 +0000590 zId = pExpr->u.zToken;
drhb7916a72009-05-27 10:31:29 +0000591 nId = sqlite3Strlen30(zId);
drh7d10d5a2008-08-20 16:35:10 +0000592 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
593 if( pDef==0 ){
drh89d5d6a2012-04-07 00:09:21 +0000594 pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
drh7d10d5a2008-08-20 16:35:10 +0000595 if( pDef==0 ){
596 no_such_func = 1;
597 }else{
598 wrong_num_args = 1;
599 }
600 }else{
601 is_agg = pDef->xFunc==0;
602 }
603#ifndef SQLITE_OMIT_AUTHORIZATION
604 if( pDef ){
605 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
606 if( auth!=SQLITE_OK ){
607 if( auth==SQLITE_DENY ){
608 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
609 pDef->zName);
610 pNC->nErr++;
611 }
612 pExpr->op = TK_NULL;
613 return WRC_Prune;
614 }
615 }
616#endif
drha51009b2012-05-21 19:11:25 +0000617 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
drh7d10d5a2008-08-20 16:35:10 +0000618 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
619 pNC->nErr++;
620 is_agg = 0;
621 }else if( no_such_func ){
622 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
623 pNC->nErr++;
624 }else if( wrong_num_args ){
625 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
626 nId, zId);
627 pNC->nErr++;
628 }
drha51009b2012-05-21 19:11:25 +0000629 if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +0000630 sqlite3WalkExprList(pWalker, pList);
drh030796d2012-08-23 16:18:10 +0000631 if( is_agg ){
632 NameContext *pNC2 = pNC;
633 pExpr->op = TK_AGG_FUNCTION;
634 pExpr->op2 = 0;
635 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
636 pExpr->op2++;
637 pNC2 = pNC2->pNext;
638 }
639 if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
640 pNC->ncFlags |= NC_AllowAgg;
641 }
drh7d10d5a2008-08-20 16:35:10 +0000642 /* FIX ME: Compute pExpr->affinity based on the expected return
643 ** type of the function
644 */
645 return WRC_Prune;
646 }
647#ifndef SQLITE_OMIT_SUBQUERY
648 case TK_SELECT:
drh73c0fdc2009-06-15 18:32:36 +0000649 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
drh7d10d5a2008-08-20 16:35:10 +0000650#endif
651 case TK_IN: {
drh73c0fdc2009-06-15 18:32:36 +0000652 testcase( pExpr->op==TK_IN );
danielk19776ab3a2e2009-02-19 14:39:25 +0000653 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
drh7d10d5a2008-08-20 16:35:10 +0000654 int nRef = pNC->nRef;
655#ifndef SQLITE_OMIT_CHECK
drha51009b2012-05-21 19:11:25 +0000656 if( (pNC->ncFlags & NC_IsCheck)!=0 ){
drh7d10d5a2008-08-20 16:35:10 +0000657 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
658 }
659#endif
danielk19776ab3a2e2009-02-19 14:39:25 +0000660 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
drh7d10d5a2008-08-20 16:35:10 +0000661 assert( pNC->nRef>=nRef );
662 if( nRef!=pNC->nRef ){
663 ExprSetProperty(pExpr, EP_VarSelect);
664 }
665 }
666 break;
667 }
668#ifndef SQLITE_OMIT_CHECK
669 case TK_VARIABLE: {
drha51009b2012-05-21 19:11:25 +0000670 if( (pNC->ncFlags & NC_IsCheck)!=0 ){
drh7d10d5a2008-08-20 16:35:10 +0000671 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
672 }
673 break;
674 }
675#endif
676 }
677 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
678}
679
680/*
681** pEList is a list of expressions which are really the result set of the
682** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
683** This routine checks to see if pE is a simple identifier which corresponds
684** to the AS-name of one of the terms of the expression list. If it is,
685** this routine return an integer between 1 and N where N is the number of
686** elements in pEList, corresponding to the matching entry. If there is
687** no match, or if pE is not a simple identifier, then this routine
688** return 0.
689**
690** pEList has been resolved. pE has not.
691*/
692static int resolveAsName(
693 Parse *pParse, /* Parsing context for error messages */
694 ExprList *pEList, /* List of expressions to scan */
695 Expr *pE /* Expression we are trying to match */
696){
697 int i; /* Loop counter */
698
shanecf697392009-06-01 16:53:09 +0000699 UNUSED_PARAMETER(pParse);
700
drh73c0fdc2009-06-15 18:32:36 +0000701 if( pE->op==TK_ID ){
drh33e619f2009-05-28 01:00:55 +0000702 char *zCol = pE->u.zToken;
drh7d10d5a2008-08-20 16:35:10 +0000703 for(i=0; i<pEList->nExpr; i++){
704 char *zAs = pEList->a[i].zName;
705 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh7d10d5a2008-08-20 16:35:10 +0000706 return i+1;
707 }
708 }
drh7d10d5a2008-08-20 16:35:10 +0000709 }
710 return 0;
711}
712
713/*
714** pE is a pointer to an expression which is a single term in the
715** ORDER BY of a compound SELECT. The expression has not been
716** name resolved.
717**
718** At the point this routine is called, we already know that the
719** ORDER BY term is not an integer index into the result set. That
720** case is handled by the calling routine.
721**
722** Attempt to match pE against result set columns in the left-most
723** SELECT statement. Return the index i of the matching column,
724** as an indication to the caller that it should sort by the i-th column.
725** The left-most column is 1. In other words, the value returned is the
726** same integer value that would be used in the SQL statement to indicate
727** the column.
728**
729** If there is no match, return 0. Return -1 if an error occurs.
730*/
731static int resolveOrderByTermToExprList(
732 Parse *pParse, /* Parsing context for error messages */
733 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
734 Expr *pE /* The specific ORDER BY term */
735){
736 int i; /* Loop counter */
737 ExprList *pEList; /* The columns of the result set */
738 NameContext nc; /* Name context for resolving pE */
drha7564662010-02-22 19:32:31 +0000739 sqlite3 *db; /* Database connection */
740 int rc; /* Return code from subprocedures */
741 u8 savedSuppErr; /* Saved value of db->suppressErr */
drh7d10d5a2008-08-20 16:35:10 +0000742
743 assert( sqlite3ExprIsInteger(pE, &i)==0 );
744 pEList = pSelect->pEList;
745
746 /* Resolve all names in the ORDER BY term expression
747 */
748 memset(&nc, 0, sizeof(nc));
749 nc.pParse = pParse;
750 nc.pSrcList = pSelect->pSrc;
751 nc.pEList = pEList;
drha51009b2012-05-21 19:11:25 +0000752 nc.ncFlags = NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +0000753 nc.nErr = 0;
drha7564662010-02-22 19:32:31 +0000754 db = pParse->db;
755 savedSuppErr = db->suppressErr;
756 db->suppressErr = 1;
757 rc = sqlite3ResolveExprNames(&nc, pE);
758 db->suppressErr = savedSuppErr;
759 if( rc ) return 0;
drh7d10d5a2008-08-20 16:35:10 +0000760
761 /* Try to match the ORDER BY expression against an expression
762 ** in the result set. Return an 1-based index of the matching
763 ** result-set entry.
764 */
765 for(i=0; i<pEList->nExpr; i++){
drh1d9da702010-01-07 15:17:02 +0000766 if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
drh7d10d5a2008-08-20 16:35:10 +0000767 return i+1;
768 }
769 }
770
771 /* If no match, return 0. */
772 return 0;
773}
774
775/*
776** Generate an ORDER BY or GROUP BY term out-of-range error.
777*/
778static void resolveOutOfRangeError(
779 Parse *pParse, /* The error context into which to write the error */
780 const char *zType, /* "ORDER" or "GROUP" */
781 int i, /* The index (1-based) of the term out of range */
782 int mx /* Largest permissible value of i */
783){
784 sqlite3ErrorMsg(pParse,
785 "%r %s BY term out of range - should be "
786 "between 1 and %d", i, zType, mx);
787}
788
789/*
790** Analyze the ORDER BY clause in a compound SELECT statement. Modify
791** each term of the ORDER BY clause is a constant integer between 1
792** and N where N is the number of columns in the compound SELECT.
793**
794** ORDER BY terms that are already an integer between 1 and N are
795** unmodified. ORDER BY terms that are integers outside the range of
796** 1 through N generate an error. ORDER BY terms that are expressions
797** are matched against result set expressions of compound SELECT
798** beginning with the left-most SELECT and working toward the right.
799** At the first match, the ORDER BY expression is transformed into
800** the integer column number.
801**
802** Return the number of errors seen.
803*/
804static int resolveCompoundOrderBy(
805 Parse *pParse, /* Parsing context. Leave error messages here */
806 Select *pSelect /* The SELECT statement containing the ORDER BY */
807){
808 int i;
809 ExprList *pOrderBy;
810 ExprList *pEList;
811 sqlite3 *db;
812 int moreToDo = 1;
813
814 pOrderBy = pSelect->pOrderBy;
815 if( pOrderBy==0 ) return 0;
816 db = pParse->db;
817#if SQLITE_MAX_COLUMN
818 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
819 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
820 return 1;
821 }
822#endif
823 for(i=0; i<pOrderBy->nExpr; i++){
824 pOrderBy->a[i].done = 0;
825 }
826 pSelect->pNext = 0;
827 while( pSelect->pPrior ){
828 pSelect->pPrior->pNext = pSelect;
829 pSelect = pSelect->pPrior;
830 }
831 while( pSelect && moreToDo ){
832 struct ExprList_item *pItem;
833 moreToDo = 0;
834 pEList = pSelect->pEList;
drh0a846f92008-08-25 17:23:29 +0000835 assert( pEList!=0 );
drh7d10d5a2008-08-20 16:35:10 +0000836 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
837 int iCol = -1;
838 Expr *pE, *pDup;
839 if( pItem->done ) continue;
drhbd13d342012-12-07 21:02:47 +0000840 pE = sqlite3ExprSkipCollate(pItem->pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000841 if( sqlite3ExprIsInteger(pE, &iCol) ){
drh73c0fdc2009-06-15 18:32:36 +0000842 if( iCol<=0 || iCol>pEList->nExpr ){
drh7d10d5a2008-08-20 16:35:10 +0000843 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
844 return 1;
845 }
846 }else{
847 iCol = resolveAsName(pParse, pEList, pE);
848 if( iCol==0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000849 pDup = sqlite3ExprDup(db, pE, 0);
drh7d10d5a2008-08-20 16:35:10 +0000850 if( !db->mallocFailed ){
851 assert(pDup);
852 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
853 }
854 sqlite3ExprDelete(db, pDup);
855 }
drh7d10d5a2008-08-20 16:35:10 +0000856 }
857 if( iCol>0 ){
drhbd13d342012-12-07 21:02:47 +0000858 /* Convert the ORDER BY term into an integer column number iCol,
859 ** taking care to preserve the COLLATE clause if it exists */
860 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
861 if( pNew==0 ) return 1;
862 pNew->flags |= EP_IntValue;
863 pNew->u.iValue = iCol;
864 if( pItem->pExpr==pE ){
865 pItem->pExpr = pNew;
866 }else{
867 assert( pItem->pExpr->op==TK_COLLATE );
868 assert( pItem->pExpr->pLeft==pE );
869 pItem->pExpr->pLeft = pNew;
870 }
drh7d10d5a2008-08-20 16:35:10 +0000871 sqlite3ExprDelete(db, pE);
drh4b3ac732011-12-10 23:18:32 +0000872 pItem->iOrderByCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +0000873 pItem->done = 1;
874 }else{
875 moreToDo = 1;
876 }
877 }
878 pSelect = pSelect->pNext;
879 }
880 for(i=0; i<pOrderBy->nExpr; i++){
881 if( pOrderBy->a[i].done==0 ){
882 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
883 "column in the result set", i+1);
884 return 1;
885 }
886 }
887 return 0;
888}
889
890/*
891** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
892** the SELECT statement pSelect. If any term is reference to a
893** result set expression (as determined by the ExprList.a.iCol field)
894** then convert that term into a copy of the corresponding result set
895** column.
896**
897** If any errors are detected, add an error message to pParse and
898** return non-zero. Return zero if no errors are seen.
899*/
900int sqlite3ResolveOrderGroupBy(
901 Parse *pParse, /* Parsing context. Leave error messages here */
902 Select *pSelect, /* The SELECT statement containing the clause */
903 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
904 const char *zType /* "ORDER" or "GROUP" */
905){
906 int i;
907 sqlite3 *db = pParse->db;
908 ExprList *pEList;
909 struct ExprList_item *pItem;
910
911 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
912#if SQLITE_MAX_COLUMN
913 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
914 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
915 return 1;
916 }
917#endif
918 pEList = pSelect->pEList;
drh0a846f92008-08-25 17:23:29 +0000919 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
drh7d10d5a2008-08-20 16:35:10 +0000920 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
drh4b3ac732011-12-10 23:18:32 +0000921 if( pItem->iOrderByCol ){
922 if( pItem->iOrderByCol>pEList->nExpr ){
drh7d10d5a2008-08-20 16:35:10 +0000923 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
924 return 1;
925 }
drhed551b92012-08-23 19:46:11 +0000926 resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
drh7d10d5a2008-08-20 16:35:10 +0000927 }
928 }
929 return 0;
930}
931
932/*
933** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
934** The Name context of the SELECT statement is pNC. zType is either
935** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
936**
937** This routine resolves each term of the clause into an expression.
938** If the order-by term is an integer I between 1 and N (where N is the
939** number of columns in the result set of the SELECT) then the expression
940** in the resolution is a copy of the I-th result-set expression. If
941** the order-by term is an identify that corresponds to the AS-name of
942** a result-set expression, then the term resolves to a copy of the
943** result-set expression. Otherwise, the expression is resolved in
944** the usual way - using sqlite3ResolveExprNames().
945**
946** This routine returns the number of errors. If errors occur, then
947** an appropriate error message might be left in pParse. (OOM errors
948** excepted.)
949*/
950static int resolveOrderGroupBy(
951 NameContext *pNC, /* The name context of the SELECT statement */
952 Select *pSelect, /* The SELECT statement holding pOrderBy */
953 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
954 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
955){
drh70331cd2012-04-27 01:09:06 +0000956 int i, j; /* Loop counters */
drh7d10d5a2008-08-20 16:35:10 +0000957 int iCol; /* Column number */
958 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
959 Parse *pParse; /* Parsing context */
960 int nResult; /* Number of terms in the result set */
961
962 if( pOrderBy==0 ) return 0;
963 nResult = pSelect->pEList->nExpr;
964 pParse = pNC->pParse;
965 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
966 Expr *pE = pItem->pExpr;
967 iCol = resolveAsName(pParse, pSelect->pEList, pE);
drh7d10d5a2008-08-20 16:35:10 +0000968 if( iCol>0 ){
969 /* If an AS-name match is found, mark this ORDER BY column as being
970 ** a copy of the iCol-th result-set column. The subsequent call to
971 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
972 ** copy of the iCol-th result-set expression. */
drh4b3ac732011-12-10 23:18:32 +0000973 pItem->iOrderByCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +0000974 continue;
975 }
drh0a8a4062012-12-07 18:38:16 +0000976 if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
drh7d10d5a2008-08-20 16:35:10 +0000977 /* The ORDER BY term is an integer constant. Again, set the column
978 ** number so that sqlite3ResolveOrderGroupBy() will convert the
979 ** order-by term to a copy of the result-set expression */
drh85d641f2012-12-07 23:23:53 +0000980 if( iCol<1 || iCol>0xffff ){
drh7d10d5a2008-08-20 16:35:10 +0000981 resolveOutOfRangeError(pParse, zType, i+1, nResult);
982 return 1;
983 }
drh4b3ac732011-12-10 23:18:32 +0000984 pItem->iOrderByCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +0000985 continue;
986 }
987
988 /* Otherwise, treat the ORDER BY term as an ordinary expression */
drh4b3ac732011-12-10 23:18:32 +0000989 pItem->iOrderByCol = 0;
drh7d10d5a2008-08-20 16:35:10 +0000990 if( sqlite3ResolveExprNames(pNC, pE) ){
991 return 1;
992 }
drh70331cd2012-04-27 01:09:06 +0000993 for(j=0; j<pSelect->pEList->nExpr; j++){
994 if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr)==0 ){
995 pItem->iOrderByCol = j+1;
996 }
997 }
drh7d10d5a2008-08-20 16:35:10 +0000998 }
999 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
1000}
1001
1002/*
1003** Resolve names in the SELECT statement p and all of its descendents.
1004*/
1005static int resolveSelectStep(Walker *pWalker, Select *p){
1006 NameContext *pOuterNC; /* Context that contains this SELECT */
1007 NameContext sNC; /* Name context of this SELECT */
1008 int isCompound; /* True if p is a compound select */
1009 int nCompound; /* Number of compound terms processed so far */
1010 Parse *pParse; /* Parsing context */
1011 ExprList *pEList; /* Result set expression list */
1012 int i; /* Loop counter */
1013 ExprList *pGroupBy; /* The GROUP BY clause */
1014 Select *pLeftmost; /* Left-most of SELECT of a compound */
1015 sqlite3 *db; /* Database connection */
1016
1017
drh0a846f92008-08-25 17:23:29 +00001018 assert( p!=0 );
drh7d10d5a2008-08-20 16:35:10 +00001019 if( p->selFlags & SF_Resolved ){
1020 return WRC_Prune;
1021 }
1022 pOuterNC = pWalker->u.pNC;
1023 pParse = pWalker->pParse;
1024 db = pParse->db;
1025
1026 /* Normally sqlite3SelectExpand() will be called first and will have
1027 ** already expanded this SELECT. However, if this is a subquery within
1028 ** an expression, sqlite3ResolveExprNames() will be called without a
1029 ** prior call to sqlite3SelectExpand(). When that happens, let
1030 ** sqlite3SelectPrep() do all of the processing for this SELECT.
1031 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
1032 ** this routine in the correct order.
1033 */
1034 if( (p->selFlags & SF_Expanded)==0 ){
1035 sqlite3SelectPrep(pParse, p, pOuterNC);
1036 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
1037 }
1038
1039 isCompound = p->pPrior!=0;
1040 nCompound = 0;
1041 pLeftmost = p;
1042 while( p ){
1043 assert( (p->selFlags & SF_Expanded)!=0 );
1044 assert( (p->selFlags & SF_Resolved)==0 );
1045 p->selFlags |= SF_Resolved;
1046
1047 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
1048 ** are not allowed to refer to any names, so pass an empty NameContext.
1049 */
1050 memset(&sNC, 0, sizeof(sNC));
1051 sNC.pParse = pParse;
1052 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
1053 sqlite3ResolveExprNames(&sNC, p->pOffset) ){
1054 return WRC_Abort;
1055 }
1056
drh7d10d5a2008-08-20 16:35:10 +00001057 /* Recursively resolve names in all subqueries
1058 */
1059 for(i=0; i<p->pSrc->nSrc; i++){
1060 struct SrcList_item *pItem = &p->pSrc->a[i];
1061 if( pItem->pSelect ){
danda79cf02011-07-08 16:10:54 +00001062 NameContext *pNC; /* Used to iterate name contexts */
1063 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
drh7d10d5a2008-08-20 16:35:10 +00001064 const char *zSavedContext = pParse->zAuthContext;
danda79cf02011-07-08 16:10:54 +00001065
1066 /* Count the total number of references to pOuterNC and all of its
1067 ** parent contexts. After resolving references to expressions in
1068 ** pItem->pSelect, check if this value has changed. If so, then
1069 ** SELECT statement pItem->pSelect must be correlated. Set the
1070 ** pItem->isCorrelated flag if this is the case. */
1071 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
1072
drh7d10d5a2008-08-20 16:35:10 +00001073 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
drhcd2b5612008-12-09 14:03:22 +00001074 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
drh7d10d5a2008-08-20 16:35:10 +00001075 pParse->zAuthContext = zSavedContext;
1076 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
danda79cf02011-07-08 16:10:54 +00001077
1078 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
1079 assert( pItem->isCorrelated==0 && nRef<=0 );
1080 pItem->isCorrelated = (nRef!=0);
drh7d10d5a2008-08-20 16:35:10 +00001081 }
1082 }
1083
drh92689d22012-12-18 16:07:08 +00001084 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
1085 ** resolve the result-set expression list.
1086 */
1087 sNC.ncFlags = NC_AllowAgg;
1088 sNC.pSrcList = p->pSrc;
1089 sNC.pNext = pOuterNC;
1090
1091 /* Resolve names in the result set. */
1092 pEList = p->pEList;
1093 assert( pEList!=0 );
1094 for(i=0; i<pEList->nExpr; i++){
1095 Expr *pX = pEList->a[i].pExpr;
1096 if( sqlite3ResolveExprNames(&sNC, pX) ){
1097 return WRC_Abort;
1098 }
1099 }
1100
drh7d10d5a2008-08-20 16:35:10 +00001101 /* If there are no aggregate functions in the result-set, and no GROUP BY
1102 ** expression, do not allow aggregates in any of the other expressions.
1103 */
1104 assert( (p->selFlags & SF_Aggregate)==0 );
1105 pGroupBy = p->pGroupBy;
drha51009b2012-05-21 19:11:25 +00001106 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
drh7d10d5a2008-08-20 16:35:10 +00001107 p->selFlags |= SF_Aggregate;
1108 }else{
drha51009b2012-05-21 19:11:25 +00001109 sNC.ncFlags &= ~NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +00001110 }
1111
1112 /* If a HAVING clause is present, then there must be a GROUP BY clause.
1113 */
1114 if( p->pHaving && !pGroupBy ){
1115 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
1116 return WRC_Abort;
1117 }
1118
1119 /* Add the expression list to the name-context before parsing the
1120 ** other expressions in the SELECT statement. This is so that
1121 ** expressions in the WHERE clause (etc.) can refer to expressions by
1122 ** aliases in the result set.
1123 **
1124 ** Minor point: If this is the case, then the expression will be
1125 ** re-evaluated for each reference to it.
1126 */
1127 sNC.pEList = p->pEList;
1128 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
1129 sqlite3ResolveExprNames(&sNC, p->pHaving)
1130 ){
1131 return WRC_Abort;
1132 }
1133
1134 /* The ORDER BY and GROUP BY clauses may not refer to terms in
1135 ** outer queries
1136 */
1137 sNC.pNext = 0;
drha51009b2012-05-21 19:11:25 +00001138 sNC.ncFlags |= NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +00001139
1140 /* Process the ORDER BY clause for singleton SELECT statements.
1141 ** The ORDER BY clause for compounds SELECT statements is handled
1142 ** below, after all of the result-sets for all of the elements of
1143 ** the compound have been resolved.
1144 */
1145 if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
1146 return WRC_Abort;
1147 }
1148 if( db->mallocFailed ){
1149 return WRC_Abort;
1150 }
1151
1152 /* Resolve the GROUP BY clause. At the same time, make sure
1153 ** the GROUP BY clause does not contain aggregate functions.
1154 */
1155 if( pGroupBy ){
1156 struct ExprList_item *pItem;
1157
1158 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
1159 return WRC_Abort;
1160 }
1161 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
1162 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
1163 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
1164 "the GROUP BY clause");
1165 return WRC_Abort;
1166 }
1167 }
1168 }
1169
1170 /* Advance to the next term of the compound
1171 */
1172 p = p->pPrior;
1173 nCompound++;
1174 }
1175
1176 /* Resolve the ORDER BY on a compound SELECT after all terms of
1177 ** the compound have been resolved.
1178 */
1179 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
1180 return WRC_Abort;
1181 }
1182
1183 return WRC_Prune;
1184}
1185
1186/*
1187** This routine walks an expression tree and resolves references to
1188** table columns and result-set columns. At the same time, do error
1189** checking on function usage and set a flag if any aggregate functions
1190** are seen.
1191**
1192** To resolve table columns references we look for nodes (or subtrees) of the
1193** form X.Y.Z or Y.Z or just Z where
1194**
1195** X: The name of a database. Ex: "main" or "temp" or
1196** the symbolic name assigned to an ATTACH-ed database.
1197**
1198** Y: The name of a table in a FROM clause. Or in a trigger
1199** one of the special names "old" or "new".
1200**
1201** Z: The name of a column in table Y.
1202**
1203** The node at the root of the subtree is modified as follows:
1204**
1205** Expr.op Changed to TK_COLUMN
1206** Expr.pTab Points to the Table object for X.Y
1207** Expr.iColumn The column index in X.Y. -1 for the rowid.
1208** Expr.iTable The VDBE cursor number for X.Y
1209**
1210**
1211** To resolve result-set references, look for expression nodes of the
1212** form Z (with no X and Y prefix) where the Z matches the right-hand
1213** size of an AS clause in the result-set of a SELECT. The Z expression
1214** is replaced by a copy of the left-hand side of the result-set expression.
1215** Table-name and function resolution occurs on the substituted expression
1216** tree. For example, in:
1217**
1218** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
1219**
1220** The "x" term of the order by is replaced by "a+b" to render:
1221**
1222** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
1223**
1224** Function calls are checked to make sure that the function is
1225** defined and that the correct number of arguments are specified.
drha51009b2012-05-21 19:11:25 +00001226** If the function is an aggregate function, then the NC_HasAgg flag is
drh7d10d5a2008-08-20 16:35:10 +00001227** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
1228** If an expression contains aggregate functions then the EP_Agg
1229** property on the expression is set.
1230**
1231** An error message is left in pParse if anything is amiss. The number
1232** if errors is returned.
1233*/
1234int sqlite3ResolveExprNames(
1235 NameContext *pNC, /* Namespace to resolve expressions in. */
1236 Expr *pExpr /* The expression to be analyzed. */
1237){
drha51009b2012-05-21 19:11:25 +00001238 u8 savedHasAgg;
drh7d10d5a2008-08-20 16:35:10 +00001239 Walker w;
1240
1241 if( pExpr==0 ) return 0;
1242#if SQLITE_MAX_EXPR_DEPTH>0
1243 {
1244 Parse *pParse = pNC->pParse;
1245 if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
1246 return 1;
1247 }
1248 pParse->nHeight += pExpr->nHeight;
1249 }
1250#endif
drha51009b2012-05-21 19:11:25 +00001251 savedHasAgg = pNC->ncFlags & NC_HasAgg;
1252 pNC->ncFlags &= ~NC_HasAgg;
drh7d10d5a2008-08-20 16:35:10 +00001253 w.xExprCallback = resolveExprStep;
1254 w.xSelectCallback = resolveSelectStep;
1255 w.pParse = pNC->pParse;
1256 w.u.pNC = pNC;
1257 sqlite3WalkExpr(&w, pExpr);
1258#if SQLITE_MAX_EXPR_DEPTH>0
1259 pNC->pParse->nHeight -= pExpr->nHeight;
1260#endif
drhfd773cf2009-05-29 14:39:07 +00001261 if( pNC->nErr>0 || w.pParse->nErr>0 ){
drh7d10d5a2008-08-20 16:35:10 +00001262 ExprSetProperty(pExpr, EP_Error);
1263 }
drha51009b2012-05-21 19:11:25 +00001264 if( pNC->ncFlags & NC_HasAgg ){
drh7d10d5a2008-08-20 16:35:10 +00001265 ExprSetProperty(pExpr, EP_Agg);
1266 }else if( savedHasAgg ){
drha51009b2012-05-21 19:11:25 +00001267 pNC->ncFlags |= NC_HasAgg;
drh7d10d5a2008-08-20 16:35:10 +00001268 }
1269 return ExprHasProperty(pExpr, EP_Error);
1270}
drh7d10d5a2008-08-20 16:35:10 +00001271
1272
1273/*
1274** Resolve all names in all expressions of a SELECT and in all
1275** decendents of the SELECT, including compounds off of p->pPrior,
1276** subqueries in expressions, and subqueries used as FROM clause
1277** terms.
1278**
1279** See sqlite3ResolveExprNames() for a description of the kinds of
1280** transformations that occur.
1281**
1282** All SELECT statements should have been expanded using
1283** sqlite3SelectExpand() prior to invoking this routine.
1284*/
1285void sqlite3ResolveSelectNames(
1286 Parse *pParse, /* The parser context */
1287 Select *p, /* The SELECT statement being coded. */
1288 NameContext *pOuterNC /* Name context for parent SELECT statement */
1289){
1290 Walker w;
1291
drh0a846f92008-08-25 17:23:29 +00001292 assert( p!=0 );
1293 w.xExprCallback = resolveExprStep;
1294 w.xSelectCallback = resolveSelectStep;
1295 w.pParse = pParse;
1296 w.u.pNC = pOuterNC;
1297 sqlite3WalkSelect(&w, p);
drh7d10d5a2008-08-20 16:35:10 +00001298}