blob: 749e473d45efec8254755f4bd56a507019b2e064 [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**
danielk19772d2e7bd2009-02-24 10:14:40 +000017** $Id: resolve.c,v 1.18 2009/02/24 10:14:40 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 */
danielk19772d2e7bd2009-02-24 10:14:40 +0000350 if( pExpr->iColumn>=0 && pMatch!=0 ){
351 int n = pExpr->iColumn;
352 testcase( n==BMS-1 );
353 if( n>=BMS ){
354 n = BMS-1;
drh7d10d5a2008-08-20 16:35:10 +0000355 }
danielk19772d2e7bd2009-02-24 10:14:40 +0000356 assert( pMatch->iCursor==pExpr->iTable );
357 pMatch->colUsed |= ((Bitmask)1)<<n;
drh7d10d5a2008-08-20 16:35:10 +0000358 }
359
360lookupname_end:
361 /* Clean up and return
362 */
363 sqlite3DbFree(db, zDb);
364 sqlite3DbFree(db, zTab);
365 sqlite3ExprDelete(db, pExpr->pLeft);
366 pExpr->pLeft = 0;
367 sqlite3ExprDelete(db, pExpr->pRight);
368 pExpr->pRight = 0;
369 pExpr->op = TK_COLUMN;
370lookupname_end_2:
371 sqlite3DbFree(db, zCol);
372 if( cnt==1 ){
373 assert( pNC!=0 );
374 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
375 /* Increment the nRef value on all name contexts from TopNC up to
376 ** the point where the name matched. */
377 for(;;){
378 assert( pTopNC!=0 );
379 pTopNC->nRef++;
380 if( pTopNC==pNC ) break;
381 pTopNC = pTopNC->pNext;
382 }
383 return 0;
384 } else {
385 return 1;
386 }
387}
388
389/*
390** This routine is callback for sqlite3WalkExpr().
391**
392** Resolve symbolic names into TK_COLUMN operators for the current
393** node in the expression tree. Return 0 to continue the search down
394** the tree or 2 to abort the tree walk.
395**
396** This routine also does error checking and name resolution for
397** function names. The operator for aggregate functions is changed
398** to TK_AGG_FUNCTION.
399*/
400static int resolveExprStep(Walker *pWalker, Expr *pExpr){
401 NameContext *pNC;
402 Parse *pParse;
403
drh7d10d5a2008-08-20 16:35:10 +0000404 pNC = pWalker->u.pNC;
405 assert( pNC!=0 );
406 pParse = pNC->pParse;
407 assert( pParse==pWalker->pParse );
408
409 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
410 ExprSetProperty(pExpr, EP_Resolved);
411#ifndef NDEBUG
412 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
413 SrcList *pSrcList = pNC->pSrcList;
414 int i;
415 for(i=0; i<pNC->pSrcList->nSrc; i++){
416 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
417 }
418 }
419#endif
420 switch( pExpr->op ){
drh41204f12008-10-06 13:54:35 +0000421
shane273f6192008-10-10 04:34:16 +0000422#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
drh41204f12008-10-06 13:54:35 +0000423 /* The special operator TK_ROW means use the rowid for the first
424 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
425 ** clause processing on UPDATE and DELETE statements.
426 */
427 case TK_ROW: {
428 SrcList *pSrcList = pNC->pSrcList;
429 struct SrcList_item *pItem;
430 assert( pSrcList && pSrcList->nSrc==1 );
431 pItem = pSrcList->a;
432 pExpr->op = TK_COLUMN;
433 pExpr->pTab = pItem->pTab;
434 pExpr->iTable = pItem->iCursor;
435 pExpr->iColumn = -1;
436 pExpr->affinity = SQLITE_AFF_INTEGER;
437 break;
438 }
shane273f6192008-10-10 04:34:16 +0000439#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
drh41204f12008-10-06 13:54:35 +0000440
drh7d10d5a2008-08-20 16:35:10 +0000441 /* A lone identifier is the name of a column.
442 */
443 case TK_ID: {
444 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
445 return WRC_Prune;
446 }
447
448 /* A table name and column name: ID.ID
449 ** Or a database, table and column: ID.ID.ID
450 */
451 case TK_DOT: {
452 Token *pColumn;
453 Token *pTable;
454 Token *pDb;
455 Expr *pRight;
456
457 /* if( pSrcList==0 ) break; */
458 pRight = pExpr->pRight;
459 if( pRight->op==TK_ID ){
460 pDb = 0;
461 pTable = &pExpr->pLeft->token;
462 pColumn = &pRight->token;
463 }else{
464 assert( pRight->op==TK_DOT );
465 pDb = &pExpr->pLeft->token;
466 pTable = &pRight->pLeft->token;
467 pColumn = &pRight->pRight->token;
468 }
469 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
470 return WRC_Prune;
471 }
472
473 /* Resolve function names
474 */
475 case TK_CONST_FUNC:
476 case TK_FUNCTION: {
danielk19776ab3a2e2009-02-19 14:39:25 +0000477 ExprList *pList = pExpr->x.pList; /* The argument list */
478 int n = pList ? pList->nExpr : 0; /* Number of arguments */
drh7d10d5a2008-08-20 16:35:10 +0000479 int no_such_func = 0; /* True if no such function exists */
480 int wrong_num_args = 0; /* True if wrong number of arguments */
481 int is_agg = 0; /* True if is an aggregate function */
482 int auth; /* Authorization to use the function */
483 int nId; /* Number of characters in function name */
484 const char *zId; /* The function name. */
485 FuncDef *pDef; /* Information about the function */
drhea678832008-12-10 19:26:22 +0000486 u8 enc = ENC(pParse->db); /* The database encoding */
drh7d10d5a2008-08-20 16:35:10 +0000487
danielk19776ab3a2e2009-02-19 14:39:25 +0000488 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drh7d10d5a2008-08-20 16:35:10 +0000489 zId = (char*)pExpr->token.z;
490 nId = pExpr->token.n;
491 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
492 if( pDef==0 ){
493 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
494 if( pDef==0 ){
495 no_such_func = 1;
496 }else{
497 wrong_num_args = 1;
498 }
499 }else{
500 is_agg = pDef->xFunc==0;
501 }
502#ifndef SQLITE_OMIT_AUTHORIZATION
503 if( pDef ){
504 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
505 if( auth!=SQLITE_OK ){
506 if( auth==SQLITE_DENY ){
507 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
508 pDef->zName);
509 pNC->nErr++;
510 }
511 pExpr->op = TK_NULL;
512 return WRC_Prune;
513 }
514 }
515#endif
516 if( is_agg && !pNC->allowAgg ){
517 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
518 pNC->nErr++;
519 is_agg = 0;
520 }else if( no_such_func ){
521 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
522 pNC->nErr++;
523 }else if( wrong_num_args ){
524 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
525 nId, zId);
526 pNC->nErr++;
527 }
528 if( is_agg ){
529 pExpr->op = TK_AGG_FUNCTION;
530 pNC->hasAgg = 1;
531 }
532 if( is_agg ) pNC->allowAgg = 0;
533 sqlite3WalkExprList(pWalker, pList);
534 if( is_agg ) pNC->allowAgg = 1;
535 /* FIX ME: Compute pExpr->affinity based on the expected return
536 ** type of the function
537 */
538 return WRC_Prune;
539 }
540#ifndef SQLITE_OMIT_SUBQUERY
541 case TK_SELECT:
542 case TK_EXISTS:
543#endif
544 case TK_IN: {
danielk19776ab3a2e2009-02-19 14:39:25 +0000545 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
drh7d10d5a2008-08-20 16:35:10 +0000546 int nRef = pNC->nRef;
547#ifndef SQLITE_OMIT_CHECK
548 if( pNC->isCheck ){
549 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
550 }
551#endif
danielk19776ab3a2e2009-02-19 14:39:25 +0000552 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
drh7d10d5a2008-08-20 16:35:10 +0000553 assert( pNC->nRef>=nRef );
554 if( nRef!=pNC->nRef ){
555 ExprSetProperty(pExpr, EP_VarSelect);
556 }
557 }
558 break;
559 }
560#ifndef SQLITE_OMIT_CHECK
561 case TK_VARIABLE: {
562 if( pNC->isCheck ){
563 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
564 }
565 break;
566 }
567#endif
568 }
569 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
570}
571
572/*
573** pEList is a list of expressions which are really the result set of the
574** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
575** This routine checks to see if pE is a simple identifier which corresponds
576** to the AS-name of one of the terms of the expression list. If it is,
577** this routine return an integer between 1 and N where N is the number of
578** elements in pEList, corresponding to the matching entry. If there is
579** no match, or if pE is not a simple identifier, then this routine
580** return 0.
581**
582** pEList has been resolved. pE has not.
583*/
584static int resolveAsName(
585 Parse *pParse, /* Parsing context for error messages */
586 ExprList *pEList, /* List of expressions to scan */
587 Expr *pE /* Expression we are trying to match */
588){
589 int i; /* Loop counter */
590
591 if( pE->op==TK_ID || (pE->op==TK_STRING && pE->token.z[0]!='\'') ){
592 sqlite3 *db = pParse->db;
593 char *zCol = sqlite3NameFromToken(db, &pE->token);
594 if( zCol==0 ){
595 return -1;
596 }
597 for(i=0; i<pEList->nExpr; i++){
598 char *zAs = pEList->a[i].zName;
599 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
600 sqlite3DbFree(db, zCol);
601 return i+1;
602 }
603 }
604 sqlite3DbFree(db, zCol);
605 }
606 return 0;
607}
608
609/*
610** pE is a pointer to an expression which is a single term in the
611** ORDER BY of a compound SELECT. The expression has not been
612** name resolved.
613**
614** At the point this routine is called, we already know that the
615** ORDER BY term is not an integer index into the result set. That
616** case is handled by the calling routine.
617**
618** Attempt to match pE against result set columns in the left-most
619** SELECT statement. Return the index i of the matching column,
620** as an indication to the caller that it should sort by the i-th column.
621** The left-most column is 1. In other words, the value returned is the
622** same integer value that would be used in the SQL statement to indicate
623** the column.
624**
625** If there is no match, return 0. Return -1 if an error occurs.
626*/
627static int resolveOrderByTermToExprList(
628 Parse *pParse, /* Parsing context for error messages */
629 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
630 Expr *pE /* The specific ORDER BY term */
631){
632 int i; /* Loop counter */
633 ExprList *pEList; /* The columns of the result set */
634 NameContext nc; /* Name context for resolving pE */
635
636 assert( sqlite3ExprIsInteger(pE, &i)==0 );
637 pEList = pSelect->pEList;
638
639 /* Resolve all names in the ORDER BY term expression
640 */
641 memset(&nc, 0, sizeof(nc));
642 nc.pParse = pParse;
643 nc.pSrcList = pSelect->pSrc;
644 nc.pEList = pEList;
645 nc.allowAgg = 1;
646 nc.nErr = 0;
647 if( sqlite3ResolveExprNames(&nc, pE) ){
648 sqlite3ErrorClear(pParse);
649 return 0;
650 }
651
652 /* Try to match the ORDER BY expression against an expression
653 ** in the result set. Return an 1-based index of the matching
654 ** result-set entry.
655 */
656 for(i=0; i<pEList->nExpr; i++){
657 if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){
658 return i+1;
659 }
660 }
661
662 /* If no match, return 0. */
663 return 0;
664}
665
666/*
667** Generate an ORDER BY or GROUP BY term out-of-range error.
668*/
669static void resolveOutOfRangeError(
670 Parse *pParse, /* The error context into which to write the error */
671 const char *zType, /* "ORDER" or "GROUP" */
672 int i, /* The index (1-based) of the term out of range */
673 int mx /* Largest permissible value of i */
674){
675 sqlite3ErrorMsg(pParse,
676 "%r %s BY term out of range - should be "
677 "between 1 and %d", i, zType, mx);
678}
679
680/*
681** Analyze the ORDER BY clause in a compound SELECT statement. Modify
682** each term of the ORDER BY clause is a constant integer between 1
683** and N where N is the number of columns in the compound SELECT.
684**
685** ORDER BY terms that are already an integer between 1 and N are
686** unmodified. ORDER BY terms that are integers outside the range of
687** 1 through N generate an error. ORDER BY terms that are expressions
688** are matched against result set expressions of compound SELECT
689** beginning with the left-most SELECT and working toward the right.
690** At the first match, the ORDER BY expression is transformed into
691** the integer column number.
692**
693** Return the number of errors seen.
694*/
695static int resolveCompoundOrderBy(
696 Parse *pParse, /* Parsing context. Leave error messages here */
697 Select *pSelect /* The SELECT statement containing the ORDER BY */
698){
699 int i;
700 ExprList *pOrderBy;
701 ExprList *pEList;
702 sqlite3 *db;
703 int moreToDo = 1;
704
705 pOrderBy = pSelect->pOrderBy;
706 if( pOrderBy==0 ) return 0;
707 db = pParse->db;
708#if SQLITE_MAX_COLUMN
709 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
710 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
711 return 1;
712 }
713#endif
714 for(i=0; i<pOrderBy->nExpr; i++){
715 pOrderBy->a[i].done = 0;
716 }
717 pSelect->pNext = 0;
718 while( pSelect->pPrior ){
719 pSelect->pPrior->pNext = pSelect;
720 pSelect = pSelect->pPrior;
721 }
722 while( pSelect && moreToDo ){
723 struct ExprList_item *pItem;
724 moreToDo = 0;
725 pEList = pSelect->pEList;
drh0a846f92008-08-25 17:23:29 +0000726 assert( pEList!=0 );
drh7d10d5a2008-08-20 16:35:10 +0000727 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
728 int iCol = -1;
729 Expr *pE, *pDup;
730 if( pItem->done ) continue;
731 pE = pItem->pExpr;
732 if( sqlite3ExprIsInteger(pE, &iCol) ){
733 if( iCol<0 || iCol>pEList->nExpr ){
734 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
735 return 1;
736 }
737 }else{
738 iCol = resolveAsName(pParse, pEList, pE);
739 if( iCol==0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000740 pDup = sqlite3ExprDup(db, pE, 0);
drh7d10d5a2008-08-20 16:35:10 +0000741 if( !db->mallocFailed ){
742 assert(pDup);
743 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
744 }
745 sqlite3ExprDelete(db, pDup);
746 }
747 if( iCol<0 ){
748 return 1;
749 }
750 }
751 if( iCol>0 ){
752 CollSeq *pColl = pE->pColl;
753 int flags = pE->flags & EP_ExpCollate;
754 sqlite3ExprDelete(db, pE);
755 pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0, 0, 0);
756 if( pE==0 ) return 1;
757 pE->pColl = pColl;
758 pE->flags |= EP_IntValue | flags;
759 pE->iTable = iCol;
drhea678832008-12-10 19:26:22 +0000760 pItem->iCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +0000761 pItem->done = 1;
762 }else{
763 moreToDo = 1;
764 }
765 }
766 pSelect = pSelect->pNext;
767 }
768 for(i=0; i<pOrderBy->nExpr; i++){
769 if( pOrderBy->a[i].done==0 ){
770 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
771 "column in the result set", i+1);
772 return 1;
773 }
774 }
775 return 0;
776}
777
778/*
779** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
780** the SELECT statement pSelect. If any term is reference to a
781** result set expression (as determined by the ExprList.a.iCol field)
782** then convert that term into a copy of the corresponding result set
783** column.
784**
785** If any errors are detected, add an error message to pParse and
786** return non-zero. Return zero if no errors are seen.
787*/
788int sqlite3ResolveOrderGroupBy(
789 Parse *pParse, /* Parsing context. Leave error messages here */
790 Select *pSelect, /* The SELECT statement containing the clause */
791 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
792 const char *zType /* "ORDER" or "GROUP" */
793){
794 int i;
795 sqlite3 *db = pParse->db;
796 ExprList *pEList;
797 struct ExprList_item *pItem;
798
799 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
800#if SQLITE_MAX_COLUMN
801 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
802 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
803 return 1;
804 }
805#endif
806 pEList = pSelect->pEList;
drh0a846f92008-08-25 17:23:29 +0000807 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
drh7d10d5a2008-08-20 16:35:10 +0000808 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
809 if( pItem->iCol ){
drh7d10d5a2008-08-20 16:35:10 +0000810 if( pItem->iCol>pEList->nExpr ){
811 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
812 return 1;
813 }
drh8b213892008-08-29 02:14:02 +0000814 resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
drh7d10d5a2008-08-20 16:35:10 +0000815 }
816 }
817 return 0;
818}
819
820/*
821** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
822** The Name context of the SELECT statement is pNC. zType is either
823** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
824**
825** This routine resolves each term of the clause into an expression.
826** If the order-by term is an integer I between 1 and N (where N is the
827** number of columns in the result set of the SELECT) then the expression
828** in the resolution is a copy of the I-th result-set expression. If
829** the order-by term is an identify that corresponds to the AS-name of
830** a result-set expression, then the term resolves to a copy of the
831** result-set expression. Otherwise, the expression is resolved in
832** the usual way - using sqlite3ResolveExprNames().
833**
834** This routine returns the number of errors. If errors occur, then
835** an appropriate error message might be left in pParse. (OOM errors
836** excepted.)
837*/
838static int resolveOrderGroupBy(
839 NameContext *pNC, /* The name context of the SELECT statement */
840 Select *pSelect, /* The SELECT statement holding pOrderBy */
841 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
842 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
843){
844 int i; /* Loop counter */
845 int iCol; /* Column number */
846 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
847 Parse *pParse; /* Parsing context */
848 int nResult; /* Number of terms in the result set */
849
850 if( pOrderBy==0 ) return 0;
851 nResult = pSelect->pEList->nExpr;
852 pParse = pNC->pParse;
853 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
854 Expr *pE = pItem->pExpr;
855 iCol = resolveAsName(pParse, pSelect->pEList, pE);
856 if( iCol<0 ){
857 return 1; /* OOM error */
858 }
859 if( iCol>0 ){
860 /* If an AS-name match is found, mark this ORDER BY column as being
861 ** a copy of the iCol-th result-set column. The subsequent call to
862 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
863 ** copy of the iCol-th result-set expression. */
drhea678832008-12-10 19:26:22 +0000864 pItem->iCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +0000865 continue;
866 }
867 if( sqlite3ExprIsInteger(pE, &iCol) ){
868 /* The ORDER BY term is an integer constant. Again, set the column
869 ** number so that sqlite3ResolveOrderGroupBy() will convert the
870 ** order-by term to a copy of the result-set expression */
drh0a846f92008-08-25 17:23:29 +0000871 if( iCol<1 ){
drh7d10d5a2008-08-20 16:35:10 +0000872 resolveOutOfRangeError(pParse, zType, i+1, nResult);
873 return 1;
874 }
drhea678832008-12-10 19:26:22 +0000875 pItem->iCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +0000876 continue;
877 }
878
879 /* Otherwise, treat the ORDER BY term as an ordinary expression */
880 pItem->iCol = 0;
881 if( sqlite3ResolveExprNames(pNC, pE) ){
882 return 1;
883 }
884 }
885 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
886}
887
888/*
889** Resolve names in the SELECT statement p and all of its descendents.
890*/
891static int resolveSelectStep(Walker *pWalker, Select *p){
892 NameContext *pOuterNC; /* Context that contains this SELECT */
893 NameContext sNC; /* Name context of this SELECT */
894 int isCompound; /* True if p is a compound select */
895 int nCompound; /* Number of compound terms processed so far */
896 Parse *pParse; /* Parsing context */
897 ExprList *pEList; /* Result set expression list */
898 int i; /* Loop counter */
899 ExprList *pGroupBy; /* The GROUP BY clause */
900 Select *pLeftmost; /* Left-most of SELECT of a compound */
901 sqlite3 *db; /* Database connection */
902
903
drh0a846f92008-08-25 17:23:29 +0000904 assert( p!=0 );
drh7d10d5a2008-08-20 16:35:10 +0000905 if( p->selFlags & SF_Resolved ){
906 return WRC_Prune;
907 }
908 pOuterNC = pWalker->u.pNC;
909 pParse = pWalker->pParse;
910 db = pParse->db;
911
912 /* Normally sqlite3SelectExpand() will be called first and will have
913 ** already expanded this SELECT. However, if this is a subquery within
914 ** an expression, sqlite3ResolveExprNames() will be called without a
915 ** prior call to sqlite3SelectExpand(). When that happens, let
916 ** sqlite3SelectPrep() do all of the processing for this SELECT.
917 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
918 ** this routine in the correct order.
919 */
920 if( (p->selFlags & SF_Expanded)==0 ){
921 sqlite3SelectPrep(pParse, p, pOuterNC);
922 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
923 }
924
925 isCompound = p->pPrior!=0;
926 nCompound = 0;
927 pLeftmost = p;
928 while( p ){
929 assert( (p->selFlags & SF_Expanded)!=0 );
930 assert( (p->selFlags & SF_Resolved)==0 );
931 p->selFlags |= SF_Resolved;
932
933 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
934 ** are not allowed to refer to any names, so pass an empty NameContext.
935 */
936 memset(&sNC, 0, sizeof(sNC));
937 sNC.pParse = pParse;
938 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
939 sqlite3ResolveExprNames(&sNC, p->pOffset) ){
940 return WRC_Abort;
941 }
942
943 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
944 ** resolve the result-set expression list.
945 */
946 sNC.allowAgg = 1;
947 sNC.pSrcList = p->pSrc;
948 sNC.pNext = pOuterNC;
949
950 /* Resolve names in the result set. */
951 pEList = p->pEList;
drh0a846f92008-08-25 17:23:29 +0000952 assert( pEList!=0 );
drh7d10d5a2008-08-20 16:35:10 +0000953 for(i=0; i<pEList->nExpr; i++){
954 Expr *pX = pEList->a[i].pExpr;
955 if( sqlite3ResolveExprNames(&sNC, pX) ){
956 return WRC_Abort;
957 }
958 }
959
960 /* Recursively resolve names in all subqueries
961 */
962 for(i=0; i<p->pSrc->nSrc; i++){
963 struct SrcList_item *pItem = &p->pSrc->a[i];
964 if( pItem->pSelect ){
965 const char *zSavedContext = pParse->zAuthContext;
966 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
drhcd2b5612008-12-09 14:03:22 +0000967 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
drh7d10d5a2008-08-20 16:35:10 +0000968 pParse->zAuthContext = zSavedContext;
969 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
970 }
971 }
972
973 /* If there are no aggregate functions in the result-set, and no GROUP BY
974 ** expression, do not allow aggregates in any of the other expressions.
975 */
976 assert( (p->selFlags & SF_Aggregate)==0 );
977 pGroupBy = p->pGroupBy;
978 if( pGroupBy || sNC.hasAgg ){
979 p->selFlags |= SF_Aggregate;
980 }else{
981 sNC.allowAgg = 0;
982 }
983
984 /* If a HAVING clause is present, then there must be a GROUP BY clause.
985 */
986 if( p->pHaving && !pGroupBy ){
987 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
988 return WRC_Abort;
989 }
990
991 /* Add the expression list to the name-context before parsing the
992 ** other expressions in the SELECT statement. This is so that
993 ** expressions in the WHERE clause (etc.) can refer to expressions by
994 ** aliases in the result set.
995 **
996 ** Minor point: If this is the case, then the expression will be
997 ** re-evaluated for each reference to it.
998 */
999 sNC.pEList = p->pEList;
1000 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
1001 sqlite3ResolveExprNames(&sNC, p->pHaving)
1002 ){
1003 return WRC_Abort;
1004 }
1005
1006 /* The ORDER BY and GROUP BY clauses may not refer to terms in
1007 ** outer queries
1008 */
1009 sNC.pNext = 0;
1010 sNC.allowAgg = 1;
1011
1012 /* Process the ORDER BY clause for singleton SELECT statements.
1013 ** The ORDER BY clause for compounds SELECT statements is handled
1014 ** below, after all of the result-sets for all of the elements of
1015 ** the compound have been resolved.
1016 */
1017 if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
1018 return WRC_Abort;
1019 }
1020 if( db->mallocFailed ){
1021 return WRC_Abort;
1022 }
1023
1024 /* Resolve the GROUP BY clause. At the same time, make sure
1025 ** the GROUP BY clause does not contain aggregate functions.
1026 */
1027 if( pGroupBy ){
1028 struct ExprList_item *pItem;
1029
1030 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
1031 return WRC_Abort;
1032 }
1033 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
1034 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
1035 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
1036 "the GROUP BY clause");
1037 return WRC_Abort;
1038 }
1039 }
1040 }
1041
1042 /* Advance to the next term of the compound
1043 */
1044 p = p->pPrior;
1045 nCompound++;
1046 }
1047
1048 /* Resolve the ORDER BY on a compound SELECT after all terms of
1049 ** the compound have been resolved.
1050 */
1051 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
1052 return WRC_Abort;
1053 }
1054
1055 return WRC_Prune;
1056}
1057
1058/*
1059** This routine walks an expression tree and resolves references to
1060** table columns and result-set columns. At the same time, do error
1061** checking on function usage and set a flag if any aggregate functions
1062** are seen.
1063**
1064** To resolve table columns references we look for nodes (or subtrees) of the
1065** form X.Y.Z or Y.Z or just Z where
1066**
1067** X: The name of a database. Ex: "main" or "temp" or
1068** the symbolic name assigned to an ATTACH-ed database.
1069**
1070** Y: The name of a table in a FROM clause. Or in a trigger
1071** one of the special names "old" or "new".
1072**
1073** Z: The name of a column in table Y.
1074**
1075** The node at the root of the subtree is modified as follows:
1076**
1077** Expr.op Changed to TK_COLUMN
1078** Expr.pTab Points to the Table object for X.Y
1079** Expr.iColumn The column index in X.Y. -1 for the rowid.
1080** Expr.iTable The VDBE cursor number for X.Y
1081**
1082**
1083** To resolve result-set references, look for expression nodes of the
1084** form Z (with no X and Y prefix) where the Z matches the right-hand
1085** size of an AS clause in the result-set of a SELECT. The Z expression
1086** is replaced by a copy of the left-hand side of the result-set expression.
1087** Table-name and function resolution occurs on the substituted expression
1088** tree. For example, in:
1089**
1090** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
1091**
1092** The "x" term of the order by is replaced by "a+b" to render:
1093**
1094** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
1095**
1096** Function calls are checked to make sure that the function is
1097** defined and that the correct number of arguments are specified.
1098** If the function is an aggregate function, then the pNC->hasAgg is
1099** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
1100** If an expression contains aggregate functions then the EP_Agg
1101** property on the expression is set.
1102**
1103** An error message is left in pParse if anything is amiss. The number
1104** if errors is returned.
1105*/
1106int sqlite3ResolveExprNames(
1107 NameContext *pNC, /* Namespace to resolve expressions in. */
1108 Expr *pExpr /* The expression to be analyzed. */
1109){
1110 int savedHasAgg;
1111 Walker w;
1112
1113 if( pExpr==0 ) return 0;
1114#if SQLITE_MAX_EXPR_DEPTH>0
1115 {
1116 Parse *pParse = pNC->pParse;
1117 if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
1118 return 1;
1119 }
1120 pParse->nHeight += pExpr->nHeight;
1121 }
1122#endif
1123 savedHasAgg = pNC->hasAgg;
1124 pNC->hasAgg = 0;
1125 w.xExprCallback = resolveExprStep;
1126 w.xSelectCallback = resolveSelectStep;
1127 w.pParse = pNC->pParse;
1128 w.u.pNC = pNC;
1129 sqlite3WalkExpr(&w, pExpr);
1130#if SQLITE_MAX_EXPR_DEPTH>0
1131 pNC->pParse->nHeight -= pExpr->nHeight;
1132#endif
1133 if( pNC->nErr>0 ){
1134 ExprSetProperty(pExpr, EP_Error);
1135 }
1136 if( pNC->hasAgg ){
1137 ExprSetProperty(pExpr, EP_Agg);
1138 }else if( savedHasAgg ){
1139 pNC->hasAgg = 1;
1140 }
1141 return ExprHasProperty(pExpr, EP_Error);
1142}
drh7d10d5a2008-08-20 16:35:10 +00001143
1144
1145/*
1146** Resolve all names in all expressions of a SELECT and in all
1147** decendents of the SELECT, including compounds off of p->pPrior,
1148** subqueries in expressions, and subqueries used as FROM clause
1149** terms.
1150**
1151** See sqlite3ResolveExprNames() for a description of the kinds of
1152** transformations that occur.
1153**
1154** All SELECT statements should have been expanded using
1155** sqlite3SelectExpand() prior to invoking this routine.
1156*/
1157void sqlite3ResolveSelectNames(
1158 Parse *pParse, /* The parser context */
1159 Select *p, /* The SELECT statement being coded. */
1160 NameContext *pOuterNC /* Name context for parent SELECT statement */
1161){
1162 Walker w;
1163
drh0a846f92008-08-25 17:23:29 +00001164 assert( p!=0 );
1165 w.xExprCallback = resolveExprStep;
1166 w.xSelectCallback = resolveSelectStep;
1167 w.pParse = pParse;
1168 w.u.pNC = pOuterNC;
1169 sqlite3WalkSelect(&w, p);
drh7d10d5a2008-08-20 16:35:10 +00001170}