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