resolve.c

Go to the documentation of this file.
00001 /*
00002 ** 2008 August 18
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 **
00013 ** This file contains routines used for walking the parser tree and
00014 ** resolve all identifiers by associating them with a particular
00015 ** table and column.
00016 **
00017 ** $Id: resolve.c,v 1.10 2008/10/19 21:03:27 drh Exp $
00018 */
00019 #include "sqliteInt.h"
00020 #include <stdlib.h>
00021 #include <string.h>
00022 
00023 /*
00024 ** Turn the pExpr expression into an alias for the iCol-th column of the
00025 ** result set in pEList.
00026 **
00027 ** If the result set column is a simple column reference, then this routine
00028 ** makes an exact copy.  But for any other kind of expression, this
00029 ** routine make a copy of the result set column as the argument to the
00030 ** TK_AS operator.  The TK_AS operator causes the expression to be
00031 ** evaluated just once and then reused for each alias.
00032 **
00033 ** The reason for suppressing the TK_AS term when the expression is a simple
00034 ** column reference is so that the column reference will be recognized as
00035 ** usable by indices within the WHERE clause processing logic. 
00036 **
00037 ** Hack:  The TK_AS operator is inhibited if zType[0]=='G'.  This means
00038 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
00039 **
00040 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
00041 **
00042 ** Is equivalent to:
00043 **
00044 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
00045 **
00046 ** The result of random()%5 in the GROUP BY clause is probably different
00047 ** from the result in the result-set.  We might fix this someday.  Or
00048 ** then again, we might not...
00049 */
00050 static void resolveAlias(
00051   Parse *pParse,         /* Parsing context */
00052   ExprList *pEList,      /* A result set */
00053   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
00054   Expr *pExpr,           /* Transform this into an alias to the result set */
00055   const char *zType      /* "GROUP" or "ORDER" or "" */
00056 ){
00057   Expr *pOrig;           /* The iCol-th column of the result set */
00058   Expr *pDup;            /* Copy of pOrig */
00059   sqlite3 *db;           /* The database connection */
00060 
00061   assert( iCol>=0 && iCol<pEList->nExpr );
00062   pOrig = pEList->a[iCol].pExpr;
00063   assert( pOrig!=0 );
00064   assert( pOrig->flags & EP_Resolved );
00065   db = pParse->db;
00066   pDup = sqlite3ExprDup(db, pOrig);
00067   if( pDup==0 ) return;
00068   if( pDup->op!=TK_COLUMN && zType[0]!='G' ){
00069     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
00070     if( pDup==0 ) return;
00071     if( pEList->a[iCol].iAlias==0 ){
00072       pEList->a[iCol].iAlias = ++pParse->nAlias;
00073     }
00074     pDup->iTable = pEList->a[iCol].iAlias;
00075   }
00076   if( pExpr->flags & EP_ExpCollate ){
00077     pDup->pColl = pExpr->pColl;
00078     pDup->flags |= EP_ExpCollate;
00079   }
00080   sqlite3ExprClear(db, pExpr);
00081   memcpy(pExpr, pDup, sizeof(*pExpr));
00082   sqlite3DbFree(db, pDup);
00083 }
00084 
00085 /*
00086 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
00087 ** that name in the set of source tables in pSrcList and make the pExpr 
00088 ** expression node refer back to that source column.  The following changes
00089 ** are made to pExpr:
00090 **
00091 **    pExpr->iDb           Set the index in db->aDb[] of the database X
00092 **                         (even if X is implied).
00093 **    pExpr->iTable        Set to the cursor number for the table obtained
00094 **                         from pSrcList.
00095 **    pExpr->pTab          Points to the Table structure of X.Y (even if
00096 **                         X and/or Y are implied.)
00097 **    pExpr->iColumn       Set to the column number within the table.
00098 **    pExpr->op            Set to TK_COLUMN.
00099 **    pExpr->pLeft         Any expression this points to is deleted
00100 **    pExpr->pRight        Any expression this points to is deleted.
00101 **
00102 ** The pDbToken is the name of the database (the "X").  This value may be
00103 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
00104 ** can be used.  The pTableToken is the name of the table (the "Y").  This
00105 ** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
00106 ** means that the form of the name is Z and that columns from any table
00107 ** can be used.
00108 **
00109 ** If the name cannot be resolved unambiguously, leave an error message
00110 ** in pParse and return non-zero.  Return zero on success.
00111 */
00112 static int lookupName(
00113   Parse *pParse,       /* The parsing context */
00114   Token *pDbToken,     /* Name of the database containing table, or NULL */
00115   Token *pTableToken,  /* Name of table containing column, or NULL */
00116   Token *pColumnToken, /* Name of the column. */
00117   NameContext *pNC,    /* The name context used to resolve the name */
00118   Expr *pExpr          /* Make this EXPR node point to the selected column */
00119 ){
00120   char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
00121   char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
00122   char *zCol = 0;      /* Name of the column.  The "Z" */
00123   int i, j;            /* Loop counters */
00124   int cnt = 0;                      /* Number of matching column names */
00125   int cntTab = 0;                   /* Number of matching table names */
00126   sqlite3 *db = pParse->db;         /* The database connection */
00127   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
00128   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
00129   NameContext *pTopNC = pNC;        /* First namecontext in the list */
00130   Schema *pSchema = 0;              /* Schema of the expression */
00131 
00132   assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
00133 
00134   /* Dequote and zero-terminate the names */
00135   zDb = sqlite3NameFromToken(db, pDbToken);
00136   zTab = sqlite3NameFromToken(db, pTableToken);
00137   zCol = sqlite3NameFromToken(db, pColumnToken);
00138   if( db->mallocFailed ){
00139     goto lookupname_end;
00140   }
00141 
00142   /* Initialize the node to no-match */
00143   pExpr->iTable = -1;
00144   pExpr->pTab = 0;
00145 
00146   /* Start at the inner-most context and move outward until a match is found */
00147   while( pNC && cnt==0 ){
00148     ExprList *pEList;
00149     SrcList *pSrcList = pNC->pSrcList;
00150 
00151     if( pSrcList ){
00152       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
00153         Table *pTab;
00154         int iDb;
00155         Column *pCol;
00156   
00157         pTab = pItem->pTab;
00158         assert( pTab!=0 && pTab->zName!=0 );
00159         iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
00160         assert( pTab->nCol>0 );
00161         if( zTab ){
00162           if( pItem->zAlias ){
00163             char *zTabName = pItem->zAlias;
00164             if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
00165           }else{
00166             char *zTabName = pTab->zName;
00167             if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
00168             if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
00169               continue;
00170             }
00171           }
00172         }
00173         if( 0==(cntTab++) ){
00174           pExpr->iTable = pItem->iCursor;
00175           pExpr->pTab = pTab;
00176           pSchema = pTab->pSchema;
00177           pMatch = pItem;
00178         }
00179         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
00180           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
00181             IdList *pUsing;
00182             cnt++;
00183             pExpr->iTable = pItem->iCursor;
00184             pExpr->pTab = pTab;
00185             pMatch = pItem;
00186             pSchema = pTab->pSchema;
00187             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
00188             pExpr->iColumn = j==pTab->iPKey ? -1 : j;
00189             if( i<pSrcList->nSrc-1 ){
00190               if( pItem[1].jointype & JT_NATURAL ){
00191                 /* If this match occurred in the left table of a natural join,
00192                 ** then skip the right table to avoid a duplicate match */
00193                 pItem++;
00194                 i++;
00195               }else if( (pUsing = pItem[1].pUsing)!=0 ){
00196                 /* If this match occurs on a column that is in the USING clause
00197                 ** of a join, skip the search of the right table of the join
00198                 ** to avoid a duplicate match there. */
00199                 int k;
00200                 for(k=0; k<pUsing->nId; k++){
00201                   if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
00202                     pItem++;
00203                     i++;
00204                     break;
00205                   }
00206                 }
00207               }
00208             }
00209             break;
00210           }
00211         }
00212       }
00213     }
00214 
00215 #ifndef SQLITE_OMIT_TRIGGER
00216     /* If we have not already resolved the name, then maybe 
00217     ** it is a new.* or old.* trigger argument reference
00218     */
00219     if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
00220       TriggerStack *pTriggerStack = pParse->trigStack;
00221       Table *pTab = 0;
00222       u32 *piColMask;
00223       if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
00224         pExpr->iTable = pTriggerStack->newIdx;
00225         assert( pTriggerStack->pTab );
00226         pTab = pTriggerStack->pTab;
00227         piColMask = &(pTriggerStack->newColMask);
00228       }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
00229         pExpr->iTable = pTriggerStack->oldIdx;
00230         assert( pTriggerStack->pTab );
00231         pTab = pTriggerStack->pTab;
00232         piColMask = &(pTriggerStack->oldColMask);
00233       }
00234 
00235       if( pTab ){ 
00236         int iCol;
00237         Column *pCol = pTab->aCol;
00238 
00239         pSchema = pTab->pSchema;
00240         cntTab++;
00241         for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
00242           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
00243             cnt++;
00244             pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
00245             pExpr->pTab = pTab;
00246             if( iCol>=0 ){
00247               testcase( iCol==31 );
00248               testcase( iCol==32 );
00249               *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
00250             }
00251             break;
00252           }
00253         }
00254       }
00255     }
00256 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
00257 
00258     /*
00259     ** Perhaps the name is a reference to the ROWID
00260     */
00261     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
00262       cnt = 1;
00263       pExpr->iColumn = -1;
00264       pExpr->affinity = SQLITE_AFF_INTEGER;
00265     }
00266 
00267     /*
00268     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
00269     ** might refer to an result-set alias.  This happens, for example, when
00270     ** we are resolving names in the WHERE clause of the following command:
00271     **
00272     **     SELECT a+b AS x FROM table WHERE x<10;
00273     **
00274     ** In cases like this, replace pExpr with a copy of the expression that
00275     ** forms the result set entry ("a+b" in the example) and return immediately.
00276     ** Note that the expression in the result set should have already been
00277     ** resolved by the time the WHERE clause is resolved.
00278     */
00279     if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
00280       for(j=0; j<pEList->nExpr; j++){
00281         char *zAs = pEList->a[j].zName;
00282         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
00283           Expr *pOrig;
00284           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
00285           assert( pExpr->pList==0 );
00286           assert( pExpr->pSelect==0 );
00287           pOrig = pEList->a[j].pExpr;
00288           if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
00289             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
00290             sqlite3DbFree(db, zCol);
00291             return 2;
00292           }
00293           resolveAlias(pParse, pEList, j, pExpr, "");
00294           cnt = 1;
00295           pMatch = 0;
00296           assert( zTab==0 && zDb==0 );
00297           goto lookupname_end_2;
00298         }
00299       } 
00300     }
00301 
00302     /* Advance to the next name context.  The loop will exit when either
00303     ** we have a match (cnt>0) or when we run out of name contexts.
00304     */
00305     if( cnt==0 ){
00306       pNC = pNC->pNext;
00307     }
00308   }
00309 
00310   /*
00311   ** If X and Y are NULL (in other words if only the column name Z is
00312   ** supplied) and the value of Z is enclosed in double-quotes, then
00313   ** Z is a string literal if it doesn't match any column names.  In that
00314   ** case, we need to return right away and not make any changes to
00315   ** pExpr.
00316   **
00317   ** Because no reference was made to outer contexts, the pNC->nRef
00318   ** fields are not changed in any context.
00319   */
00320   if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
00321     sqlite3DbFree(db, zCol);
00322     pExpr->op = TK_STRING;
00323     pExpr->pTab = 0;
00324     return 0;
00325   }
00326 
00327   /*
00328   ** cnt==0 means there was not match.  cnt>1 means there were two or
00329   ** more matches.  Either way, we have an error.
00330   */
00331   if( cnt!=1 ){
00332     const char *zErr;
00333     zErr = cnt==0 ? "no such column" : "ambiguous column name";
00334     if( zDb ){
00335       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
00336     }else if( zTab ){
00337       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
00338     }else{
00339       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
00340     }
00341     pTopNC->nErr++;
00342   }
00343 
00344   /* If a column from a table in pSrcList is referenced, then record
00345   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
00346   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
00347   ** column number is greater than the number of bits in the bitmask
00348   ** then set the high-order bit of the bitmask.
00349   */
00350   if( pExpr->iColumn>=0 && pMatch!=0 ){
00351     int n = pExpr->iColumn;
00352     testcase( n==sizeof(Bitmask)*8-1 );
00353     if( n>=sizeof(Bitmask)*8 ){
00354       n = sizeof(Bitmask)*8-1;
00355     }
00356     assert( pMatch->iCursor==pExpr->iTable );
00357     pMatch->colUsed |= ((Bitmask)1)<<n;
00358   }
00359 
00360 lookupname_end:
00361   /* Clean up and return
00362   */
00363   sqlite3DbFree(db, zDb);
00364   sqlite3DbFree(db, zTab);
00365   sqlite3ExprDelete(db, pExpr->pLeft);
00366   pExpr->pLeft = 0;
00367   sqlite3ExprDelete(db, pExpr->pRight);
00368   pExpr->pRight = 0;
00369   pExpr->op = TK_COLUMN;
00370 lookupname_end_2:
00371   sqlite3DbFree(db, zCol);
00372   if( cnt==1 ){
00373     assert( pNC!=0 );
00374     sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
00375     /* Increment the nRef value on all name contexts from TopNC up to
00376     ** the point where the name matched. */
00377     for(;;){
00378       assert( pTopNC!=0 );
00379       pTopNC->nRef++;
00380       if( pTopNC==pNC ) break;
00381       pTopNC = pTopNC->pNext;
00382     }
00383     return 0;
00384   } else {
00385     return 1;
00386   }
00387 }
00388 
00389 /*
00390 ** This routine is callback for sqlite3WalkExpr().
00391 **
00392 ** Resolve symbolic names into TK_COLUMN operators for the current
00393 ** node in the expression tree.  Return 0 to continue the search down
00394 ** the tree or 2 to abort the tree walk.
00395 **
00396 ** This routine also does error checking and name resolution for
00397 ** function names.  The operator for aggregate functions is changed
00398 ** to TK_AGG_FUNCTION.
00399 */
00400 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
00401   NameContext *pNC;
00402   Parse *pParse;
00403 
00404   pNC = pWalker->u.pNC;
00405   assert( pNC!=0 );
00406   pParse = pNC->pParse;
00407   assert( pParse==pWalker->pParse );
00408 
00409   if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
00410   ExprSetProperty(pExpr, EP_Resolved);
00411 #ifndef NDEBUG
00412   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
00413     SrcList *pSrcList = pNC->pSrcList;
00414     int i;
00415     for(i=0; i<pNC->pSrcList->nSrc; i++){
00416       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
00417     }
00418   }
00419 #endif
00420   switch( pExpr->op ){
00421 
00422 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
00423     /* The special operator TK_ROW means use the rowid for the first
00424     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
00425     ** clause processing on UPDATE and DELETE statements.
00426     */
00427     case TK_ROW: {
00428       SrcList *pSrcList = pNC->pSrcList;
00429       struct SrcList_item *pItem;
00430       assert( pSrcList && pSrcList->nSrc==1 );
00431       pItem = pSrcList->a; 
00432       pExpr->op = TK_COLUMN;
00433       pExpr->pTab = pItem->pTab;
00434       pExpr->iTable = pItem->iCursor;
00435       pExpr->iColumn = -1;
00436       pExpr->affinity = SQLITE_AFF_INTEGER;
00437       break;
00438     }
00439 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
00440 
00441     /* A lone identifier is the name of a column.
00442     */
00443     case TK_ID: {
00444       lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
00445       return WRC_Prune;
00446     }
00447   
00448     /* A table name and column name:     ID.ID
00449     ** Or a database, table and column:  ID.ID.ID
00450     */
00451     case TK_DOT: {
00452       Token *pColumn;
00453       Token *pTable;
00454       Token *pDb;
00455       Expr *pRight;
00456 
00457       /* if( pSrcList==0 ) break; */
00458       pRight = pExpr->pRight;
00459       if( pRight->op==TK_ID ){
00460         pDb = 0;
00461         pTable = &pExpr->pLeft->token;
00462         pColumn = &pRight->token;
00463       }else{
00464         assert( pRight->op==TK_DOT );
00465         pDb = &pExpr->pLeft->token;
00466         pTable = &pRight->pLeft->token;
00467         pColumn = &pRight->pRight->token;
00468       }
00469       lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
00470       return WRC_Prune;
00471     }
00472 
00473     /* Resolve function names
00474     */
00475     case TK_CONST_FUNC:
00476     case TK_FUNCTION: {
00477       ExprList *pList = pExpr->pList;    /* The argument list */
00478       int n = pList ? pList->nExpr : 0;  /* Number of arguments */
00479       int no_such_func = 0;       /* True if no such function exists */
00480       int wrong_num_args = 0;     /* True if wrong number of arguments */
00481       int is_agg = 0;             /* True if is an aggregate function */
00482       int auth;                   /* Authorization to use the function */
00483       int nId;                    /* Number of characters in function name */
00484       const char *zId;            /* The function name. */
00485       FuncDef *pDef;              /* Information about the function */
00486       int enc = ENC(pParse->db);  /* The database encoding */
00487 
00488       zId = (char*)pExpr->token.z;
00489       nId = pExpr->token.n;
00490       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
00491       if( pDef==0 ){
00492         pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
00493         if( pDef==0 ){
00494           no_such_func = 1;
00495         }else{
00496           wrong_num_args = 1;
00497         }
00498       }else{
00499         is_agg = pDef->xFunc==0;
00500       }
00501 #ifndef SQLITE_OMIT_AUTHORIZATION
00502       if( pDef ){
00503         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
00504         if( auth!=SQLITE_OK ){
00505           if( auth==SQLITE_DENY ){
00506             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
00507                                     pDef->zName);
00508             pNC->nErr++;
00509           }
00510           pExpr->op = TK_NULL;
00511           return WRC_Prune;
00512         }
00513       }
00514 #endif
00515       if( is_agg && !pNC->allowAgg ){
00516         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
00517         pNC->nErr++;
00518         is_agg = 0;
00519       }else if( no_such_func ){
00520         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
00521         pNC->nErr++;
00522       }else if( wrong_num_args ){
00523         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
00524              nId, zId);
00525         pNC->nErr++;
00526       }
00527       if( is_agg ){
00528         pExpr->op = TK_AGG_FUNCTION;
00529         pNC->hasAgg = 1;
00530       }
00531       if( is_agg ) pNC->allowAgg = 0;
00532       sqlite3WalkExprList(pWalker, pList);
00533       if( is_agg ) pNC->allowAgg = 1;
00534       /* FIX ME:  Compute pExpr->affinity based on the expected return
00535       ** type of the function 
00536       */
00537       return WRC_Prune;
00538     }
00539 #ifndef SQLITE_OMIT_SUBQUERY
00540     case TK_SELECT:
00541     case TK_EXISTS:
00542 #endif
00543     case TK_IN: {
00544       if( pExpr->pSelect ){
00545         int nRef = pNC->nRef;
00546 #ifndef SQLITE_OMIT_CHECK
00547         if( pNC->isCheck ){
00548           sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
00549         }
00550 #endif
00551         sqlite3WalkSelect(pWalker, pExpr->pSelect);
00552         assert( pNC->nRef>=nRef );
00553         if( nRef!=pNC->nRef ){
00554           ExprSetProperty(pExpr, EP_VarSelect);
00555         }
00556       }
00557       break;
00558     }
00559 #ifndef SQLITE_OMIT_CHECK
00560     case TK_VARIABLE: {
00561       if( pNC->isCheck ){
00562         sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
00563       }
00564       break;
00565     }
00566 #endif
00567   }
00568   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
00569 }
00570 
00571 /*
00572 ** pEList is a list of expressions which are really the result set of the
00573 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
00574 ** This routine checks to see if pE is a simple identifier which corresponds
00575 ** to the AS-name of one of the terms of the expression list.  If it is,
00576 ** this routine return an integer between 1 and N where N is the number of
00577 ** elements in pEList, corresponding to the matching entry.  If there is
00578 ** no match, or if pE is not a simple identifier, then this routine
00579 ** return 0.
00580 **
00581 ** pEList has been resolved.  pE has not.
00582 */
00583 static int resolveAsName(
00584   Parse *pParse,     /* Parsing context for error messages */
00585   ExprList *pEList,  /* List of expressions to scan */
00586   Expr *pE           /* Expression we are trying to match */
00587 ){
00588   int i;             /* Loop counter */
00589 
00590   if( pE->op==TK_ID || (pE->op==TK_STRING && pE->token.z[0]!='\'') ){
00591     sqlite3 *db = pParse->db;
00592     char *zCol = sqlite3NameFromToken(db, &pE->token);
00593     if( zCol==0 ){
00594       return -1;
00595     }
00596     for(i=0; i<pEList->nExpr; i++){
00597       char *zAs = pEList->a[i].zName;
00598       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
00599         sqlite3DbFree(db, zCol);
00600         return i+1;
00601       }
00602     }
00603     sqlite3DbFree(db, zCol);
00604   }
00605   return 0;
00606 }
00607 
00608 /*
00609 ** pE is a pointer to an expression which is a single term in the
00610 ** ORDER BY of a compound SELECT.  The expression has not been
00611 ** name resolved.
00612 **
00613 ** At the point this routine is called, we already know that the
00614 ** ORDER BY term is not an integer index into the result set.  That
00615 ** case is handled by the calling routine.
00616 **
00617 ** Attempt to match pE against result set columns in the left-most
00618 ** SELECT statement.  Return the index i of the matching column,
00619 ** as an indication to the caller that it should sort by the i-th column.
00620 ** The left-most column is 1.  In other words, the value returned is the
00621 ** same integer value that would be used in the SQL statement to indicate
00622 ** the column.
00623 **
00624 ** If there is no match, return 0.  Return -1 if an error occurs.
00625 */
00626 static int resolveOrderByTermToExprList(
00627   Parse *pParse,     /* Parsing context for error messages */
00628   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
00629   Expr *pE           /* The specific ORDER BY term */
00630 ){
00631   int i;             /* Loop counter */
00632   ExprList *pEList;  /* The columns of the result set */
00633   NameContext nc;    /* Name context for resolving pE */
00634 
00635   assert( sqlite3ExprIsInteger(pE, &i)==0 );
00636   pEList = pSelect->pEList;
00637 
00638   /* Resolve all names in the ORDER BY term expression
00639   */
00640   memset(&nc, 0, sizeof(nc));
00641   nc.pParse = pParse;
00642   nc.pSrcList = pSelect->pSrc;
00643   nc.pEList = pEList;
00644   nc.allowAgg = 1;
00645   nc.nErr = 0;
00646   if( sqlite3ResolveExprNames(&nc, pE) ){
00647     sqlite3ErrorClear(pParse);
00648     return 0;
00649   }
00650 
00651   /* Try to match the ORDER BY expression against an expression
00652   ** in the result set.  Return an 1-based index of the matching
00653   ** result-set entry.
00654   */
00655   for(i=0; i<pEList->nExpr; i++){
00656     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){
00657       return i+1;
00658     }
00659   }
00660 
00661   /* If no match, return 0. */
00662   return 0;
00663 }
00664 
00665 /*
00666 ** Generate an ORDER BY or GROUP BY term out-of-range error.
00667 */
00668 static void resolveOutOfRangeError(
00669   Parse *pParse,         /* The error context into which to write the error */
00670   const char *zType,     /* "ORDER" or "GROUP" */
00671   int i,                 /* The index (1-based) of the term out of range */
00672   int mx                 /* Largest permissible value of i */
00673 ){
00674   sqlite3ErrorMsg(pParse, 
00675     "%r %s BY term out of range - should be "
00676     "between 1 and %d", i, zType, mx);
00677 }
00678 
00679 /*
00680 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
00681 ** each term of the ORDER BY clause is a constant integer between 1
00682 ** and N where N is the number of columns in the compound SELECT.
00683 **
00684 ** ORDER BY terms that are already an integer between 1 and N are
00685 ** unmodified.  ORDER BY terms that are integers outside the range of
00686 ** 1 through N generate an error.  ORDER BY terms that are expressions
00687 ** are matched against result set expressions of compound SELECT
00688 ** beginning with the left-most SELECT and working toward the right.
00689 ** At the first match, the ORDER BY expression is transformed into
00690 ** the integer column number.
00691 **
00692 ** Return the number of errors seen.
00693 */
00694 static int resolveCompoundOrderBy(
00695   Parse *pParse,        /* Parsing context.  Leave error messages here */
00696   Select *pSelect       /* The SELECT statement containing the ORDER BY */
00697 ){
00698   int i;
00699   ExprList *pOrderBy;
00700   ExprList *pEList;
00701   sqlite3 *db;
00702   int moreToDo = 1;
00703 
00704   pOrderBy = pSelect->pOrderBy;
00705   if( pOrderBy==0 ) return 0;
00706   db = pParse->db;
00707 #if SQLITE_MAX_COLUMN
00708   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
00709     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
00710     return 1;
00711   }
00712 #endif
00713   for(i=0; i<pOrderBy->nExpr; i++){
00714     pOrderBy->a[i].done = 0;
00715   }
00716   pSelect->pNext = 0;
00717   while( pSelect->pPrior ){
00718     pSelect->pPrior->pNext = pSelect;
00719     pSelect = pSelect->pPrior;
00720   }
00721   while( pSelect && moreToDo ){
00722     struct ExprList_item *pItem;
00723     moreToDo = 0;
00724     pEList = pSelect->pEList;
00725     assert( pEList!=0 );
00726     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
00727       int iCol = -1;
00728       Expr *pE, *pDup;
00729       if( pItem->done ) continue;
00730       pE = pItem->pExpr;
00731       if( sqlite3ExprIsInteger(pE, &iCol) ){
00732         if( iCol<0 || iCol>pEList->nExpr ){
00733           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
00734           return 1;
00735         }
00736       }else{
00737         iCol = resolveAsName(pParse, pEList, pE);
00738         if( iCol==0 ){
00739           pDup = sqlite3ExprDup(db, pE);
00740           if( !db->mallocFailed ){
00741             assert(pDup);
00742             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
00743           }
00744           sqlite3ExprDelete(db, pDup);
00745         }
00746         if( iCol<0 ){
00747           return 1;
00748         }
00749       }
00750       if( iCol>0 ){
00751         CollSeq *pColl = pE->pColl;
00752         int flags = pE->flags & EP_ExpCollate;
00753         sqlite3ExprDelete(db, pE);
00754         pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0, 0, 0);
00755         if( pE==0 ) return 1;
00756         pE->pColl = pColl;
00757         pE->flags |= EP_IntValue | flags;
00758         pE->iTable = iCol;
00759         pItem->iCol = iCol;
00760         pItem->done = 1;
00761       }else{
00762         moreToDo = 1;
00763       }
00764     }
00765     pSelect = pSelect->pNext;
00766   }
00767   for(i=0; i<pOrderBy->nExpr; i++){
00768     if( pOrderBy->a[i].done==0 ){
00769       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
00770             "column in the result set", i+1);
00771       return 1;
00772     }
00773   }
00774   return 0;
00775 }
00776 
00777 /*
00778 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
00779 ** the SELECT statement pSelect.  If any term is reference to a
00780 ** result set expression (as determined by the ExprList.a.iCol field)
00781 ** then convert that term into a copy of the corresponding result set
00782 ** column.
00783 **
00784 ** If any errors are detected, add an error message to pParse and
00785 ** return non-zero.  Return zero if no errors are seen.
00786 */
00787 int sqlite3ResolveOrderGroupBy(
00788   Parse *pParse,        /* Parsing context.  Leave error messages here */
00789   Select *pSelect,      /* The SELECT statement containing the clause */
00790   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
00791   const char *zType     /* "ORDER" or "GROUP" */
00792 ){
00793   int i;
00794   sqlite3 *db = pParse->db;
00795   ExprList *pEList;
00796   struct ExprList_item *pItem;
00797 
00798   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
00799 #if SQLITE_MAX_COLUMN
00800   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
00801     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
00802     return 1;
00803   }
00804 #endif
00805   pEList = pSelect->pEList;
00806   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
00807   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
00808     if( pItem->iCol ){
00809       if( pItem->iCol>pEList->nExpr ){
00810         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
00811         return 1;
00812       }
00813       resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
00814     }
00815   }
00816   return 0;
00817 }
00818 
00819 /*
00820 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
00821 ** The Name context of the SELECT statement is pNC.  zType is either
00822 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
00823 **
00824 ** This routine resolves each term of the clause into an expression.
00825 ** If the order-by term is an integer I between 1 and N (where N is the
00826 ** number of columns in the result set of the SELECT) then the expression
00827 ** in the resolution is a copy of the I-th result-set expression.  If
00828 ** the order-by term is an identify that corresponds to the AS-name of
00829 ** a result-set expression, then the term resolves to a copy of the
00830 ** result-set expression.  Otherwise, the expression is resolved in
00831 ** the usual way - using sqlite3ResolveExprNames().
00832 **
00833 ** This routine returns the number of errors.  If errors occur, then
00834 ** an appropriate error message might be left in pParse.  (OOM errors
00835 ** excepted.)
00836 */
00837 static int resolveOrderGroupBy(
00838   NameContext *pNC,     /* The name context of the SELECT statement */
00839   Select *pSelect,      /* The SELECT statement holding pOrderBy */
00840   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
00841   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
00842 ){
00843   int i;                         /* Loop counter */
00844   int iCol;                      /* Column number */
00845   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
00846   Parse *pParse;                 /* Parsing context */
00847   int nResult;                   /* Number of terms in the result set */
00848 
00849   if( pOrderBy==0 ) return 0;
00850   nResult = pSelect->pEList->nExpr;
00851   pParse = pNC->pParse;
00852   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
00853     Expr *pE = pItem->pExpr;
00854     iCol = resolveAsName(pParse, pSelect->pEList, pE);
00855     if( iCol<0 ){
00856       return 1;  /* OOM error */
00857     }
00858     if( iCol>0 ){
00859       /* If an AS-name match is found, mark this ORDER BY column as being
00860       ** a copy of the iCol-th result-set column.  The subsequent call to
00861       ** sqlite3ResolveOrderGroupBy() will convert the expression to a
00862       ** copy of the iCol-th result-set expression. */
00863       pItem->iCol = iCol;
00864       continue;
00865     }
00866     if( sqlite3ExprIsInteger(pE, &iCol) ){
00867       /* The ORDER BY term is an integer constant.  Again, set the column
00868       ** number so that sqlite3ResolveOrderGroupBy() will convert the
00869       ** order-by term to a copy of the result-set expression */
00870       if( iCol<1 ){
00871         resolveOutOfRangeError(pParse, zType, i+1, nResult);
00872         return 1;
00873       }
00874       pItem->iCol = iCol;
00875       continue;
00876     }
00877 
00878     /* Otherwise, treat the ORDER BY term as an ordinary expression */
00879     pItem->iCol = 0;
00880     if( sqlite3ResolveExprNames(pNC, pE) ){
00881       return 1;
00882     }
00883   }
00884   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
00885 }
00886 
00887 /*
00888 ** Resolve names in the SELECT statement p and all of its descendents.
00889 */
00890 static int resolveSelectStep(Walker *pWalker, Select *p){
00891   NameContext *pOuterNC;  /* Context that contains this SELECT */
00892   NameContext sNC;        /* Name context of this SELECT */
00893   int isCompound;         /* True if p is a compound select */
00894   int nCompound;          /* Number of compound terms processed so far */
00895   Parse *pParse;          /* Parsing context */
00896   ExprList *pEList;       /* Result set expression list */
00897   int i;                  /* Loop counter */
00898   ExprList *pGroupBy;     /* The GROUP BY clause */
00899   Select *pLeftmost;      /* Left-most of SELECT of a compound */
00900   sqlite3 *db;            /* Database connection */
00901   
00902 
00903   assert( p!=0 );
00904   if( p->selFlags & SF_Resolved ){
00905     return WRC_Prune;
00906   }
00907   pOuterNC = pWalker->u.pNC;
00908   pParse = pWalker->pParse;
00909   db = pParse->db;
00910 
00911   /* Normally sqlite3SelectExpand() will be called first and will have
00912   ** already expanded this SELECT.  However, if this is a subquery within
00913   ** an expression, sqlite3ResolveExprNames() will be called without a
00914   ** prior call to sqlite3SelectExpand().  When that happens, let
00915   ** sqlite3SelectPrep() do all of the processing for this SELECT.
00916   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
00917   ** this routine in the correct order.
00918   */
00919   if( (p->selFlags & SF_Expanded)==0 ){
00920     sqlite3SelectPrep(pParse, p, pOuterNC);
00921     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
00922   }
00923 
00924   isCompound = p->pPrior!=0;
00925   nCompound = 0;
00926   pLeftmost = p;
00927   while( p ){
00928     assert( (p->selFlags & SF_Expanded)!=0 );
00929     assert( (p->selFlags & SF_Resolved)==0 );
00930     p->selFlags |= SF_Resolved;
00931 
00932     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
00933     ** are not allowed to refer to any names, so pass an empty NameContext.
00934     */
00935     memset(&sNC, 0, sizeof(sNC));
00936     sNC.pParse = pParse;
00937     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
00938         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
00939       return WRC_Abort;
00940     }
00941   
00942     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
00943     ** resolve the result-set expression list.
00944     */
00945     sNC.allowAgg = 1;
00946     sNC.pSrcList = p->pSrc;
00947     sNC.pNext = pOuterNC;
00948   
00949     /* Resolve names in the result set. */
00950     pEList = p->pEList;
00951     assert( pEList!=0 );
00952     for(i=0; i<pEList->nExpr; i++){
00953       Expr *pX = pEList->a[i].pExpr;
00954       if( sqlite3ResolveExprNames(&sNC, pX) ){
00955         return WRC_Abort;
00956       }
00957     }
00958   
00959     /* Recursively resolve names in all subqueries
00960     */
00961     for(i=0; i<p->pSrc->nSrc; i++){
00962       struct SrcList_item *pItem = &p->pSrc->a[i];
00963       if( pItem->pSelect ){
00964         const char *zSavedContext = pParse->zAuthContext;
00965         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
00966         sqlite3ResolveSelectNames(pParse, pItem->pSelect, &sNC);
00967         pParse->zAuthContext = zSavedContext;
00968         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
00969       }
00970     }
00971   
00972     /* If there are no aggregate functions in the result-set, and no GROUP BY 
00973     ** expression, do not allow aggregates in any of the other expressions.
00974     */
00975     assert( (p->selFlags & SF_Aggregate)==0 );
00976     pGroupBy = p->pGroupBy;
00977     if( pGroupBy || sNC.hasAgg ){
00978       p->selFlags |= SF_Aggregate;
00979     }else{
00980       sNC.allowAgg = 0;
00981     }
00982   
00983     /* If a HAVING clause is present, then there must be a GROUP BY clause.
00984     */
00985     if( p->pHaving && !pGroupBy ){
00986       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
00987       return WRC_Abort;
00988     }
00989   
00990     /* Add the expression list to the name-context before parsing the
00991     ** other expressions in the SELECT statement. This is so that
00992     ** expressions in the WHERE clause (etc.) can refer to expressions by
00993     ** aliases in the result set.
00994     **
00995     ** Minor point: If this is the case, then the expression will be
00996     ** re-evaluated for each reference to it.
00997     */
00998     sNC.pEList = p->pEList;
00999     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
01000        sqlite3ResolveExprNames(&sNC, p->pHaving)
01001     ){
01002       return WRC_Abort;
01003     }
01004 
01005     /* The ORDER BY and GROUP BY clauses may not refer to terms in
01006     ** outer queries 
01007     */
01008     sNC.pNext = 0;
01009     sNC.allowAgg = 1;
01010 
01011     /* Process the ORDER BY clause for singleton SELECT statements.
01012     ** The ORDER BY clause for compounds SELECT statements is handled
01013     ** below, after all of the result-sets for all of the elements of
01014     ** the compound have been resolved.
01015     */
01016     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
01017       return WRC_Abort;
01018     }
01019     if( db->mallocFailed ){
01020       return WRC_Abort;
01021     }
01022   
01023     /* Resolve the GROUP BY clause.  At the same time, make sure 
01024     ** the GROUP BY clause does not contain aggregate functions.
01025     */
01026     if( pGroupBy ){
01027       struct ExprList_item *pItem;
01028     
01029       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
01030         return WRC_Abort;
01031       }
01032       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
01033         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
01034           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
01035               "the GROUP BY clause");
01036           return WRC_Abort;
01037         }
01038       }
01039     }
01040 
01041     /* Advance to the next term of the compound
01042     */
01043     p = p->pPrior;
01044     nCompound++;
01045   }
01046 
01047   /* Resolve the ORDER BY on a compound SELECT after all terms of
01048   ** the compound have been resolved.
01049   */
01050   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
01051     return WRC_Abort;
01052   }
01053 
01054   return WRC_Prune;
01055 }
01056 
01057 /*
01058 ** This routine walks an expression tree and resolves references to
01059 ** table columns and result-set columns.  At the same time, do error
01060 ** checking on function usage and set a flag if any aggregate functions
01061 ** are seen.
01062 **
01063 ** To resolve table columns references we look for nodes (or subtrees) of the 
01064 ** form X.Y.Z or Y.Z or just Z where
01065 **
01066 **      X:   The name of a database.  Ex:  "main" or "temp" or
01067 **           the symbolic name assigned to an ATTACH-ed database.
01068 **
01069 **      Y:   The name of a table in a FROM clause.  Or in a trigger
01070 **           one of the special names "old" or "new".
01071 **
01072 **      Z:   The name of a column in table Y.
01073 **
01074 ** The node at the root of the subtree is modified as follows:
01075 **
01076 **    Expr.op        Changed to TK_COLUMN
01077 **    Expr.pTab      Points to the Table object for X.Y
01078 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
01079 **    Expr.iTable    The VDBE cursor number for X.Y
01080 **
01081 **
01082 ** To resolve result-set references, look for expression nodes of the
01083 ** form Z (with no X and Y prefix) where the Z matches the right-hand
01084 ** size of an AS clause in the result-set of a SELECT.  The Z expression
01085 ** is replaced by a copy of the left-hand side of the result-set expression.
01086 ** Table-name and function resolution occurs on the substituted expression
01087 ** tree.  For example, in:
01088 **
01089 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
01090 **
01091 ** The "x" term of the order by is replaced by "a+b" to render:
01092 **
01093 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
01094 **
01095 ** Function calls are checked to make sure that the function is 
01096 ** defined and that the correct number of arguments are specified.
01097 ** If the function is an aggregate function, then the pNC->hasAgg is
01098 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
01099 ** If an expression contains aggregate functions then the EP_Agg
01100 ** property on the expression is set.
01101 **
01102 ** An error message is left in pParse if anything is amiss.  The number
01103 ** if errors is returned.
01104 */
01105 int sqlite3ResolveExprNames( 
01106   NameContext *pNC,       /* Namespace to resolve expressions in. */
01107   Expr *pExpr             /* The expression to be analyzed. */
01108 ){
01109   int savedHasAgg;
01110   Walker w;
01111 
01112   if( pExpr==0 ) return 0;
01113 #if SQLITE_MAX_EXPR_DEPTH>0
01114   {
01115     Parse *pParse = pNC->pParse;
01116     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
01117       return 1;
01118     }
01119     pParse->nHeight += pExpr->nHeight;
01120   }
01121 #endif
01122   savedHasAgg = pNC->hasAgg;
01123   pNC->hasAgg = 0;
01124   w.xExprCallback = resolveExprStep;
01125   w.xSelectCallback = resolveSelectStep;
01126   w.pParse = pNC->pParse;
01127   w.u.pNC = pNC;
01128   sqlite3WalkExpr(&w, pExpr);
01129 #if SQLITE_MAX_EXPR_DEPTH>0
01130   pNC->pParse->nHeight -= pExpr->nHeight;
01131 #endif
01132   if( pNC->nErr>0 ){
01133     ExprSetProperty(pExpr, EP_Error);
01134   }
01135   if( pNC->hasAgg ){
01136     ExprSetProperty(pExpr, EP_Agg);
01137   }else if( savedHasAgg ){
01138     pNC->hasAgg = 1;
01139   }
01140   return ExprHasProperty(pExpr, EP_Error);
01141 }
01142 
01143 
01144 /*
01145 ** Resolve all names in all expressions of a SELECT and in all
01146 ** decendents of the SELECT, including compounds off of p->pPrior,
01147 ** subqueries in expressions, and subqueries used as FROM clause
01148 ** terms.
01149 **
01150 ** See sqlite3ResolveExprNames() for a description of the kinds of
01151 ** transformations that occur.
01152 **
01153 ** All SELECT statements should have been expanded using
01154 ** sqlite3SelectExpand() prior to invoking this routine.
01155 */
01156 void sqlite3ResolveSelectNames(
01157   Parse *pParse,         /* The parser context */
01158   Select *p,             /* The SELECT statement being coded. */
01159   NameContext *pOuterNC  /* Name context for parent SELECT statement */
01160 ){
01161   Walker w;
01162 
01163   assert( p!=0 );
01164   w.xExprCallback = resolveExprStep;
01165   w.xSelectCallback = resolveSelectStep;
01166   w.pParse = pParse;
01167   w.u.pNC = pOuterNC;
01168   sqlite3WalkSelect(&w, p);
01169 }

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:56 2011 by Doxygen 1.6.1