sl@0: /* sl@0: ** 2008 August 18 sl@0: ** sl@0: ** The author disclaims copyright to this source code. In place of sl@0: ** a legal notice, here is a blessing: sl@0: ** sl@0: ** May you do good and not evil. sl@0: ** May you find forgiveness for yourself and forgive others. sl@0: ** May you share freely, never taking more than you give. sl@0: ** sl@0: ************************************************************************* sl@0: ** sl@0: ** This file contains routines used for walking the parser tree and sl@0: ** resolve all identifiers by associating them with a particular sl@0: ** table and column. sl@0: ** sl@0: ** $Id: resolve.c,v 1.5 2008/08/29 02:14:03 drh Exp $ sl@0: */ sl@0: #include "sqliteInt.h" sl@0: #include sl@0: #include sl@0: sl@0: /* sl@0: ** Turn the pExpr expression into an alias for the iCol-th column of the sl@0: ** result set in pEList. sl@0: ** sl@0: ** If the result set column is a simple column reference, then this routine sl@0: ** makes an exact copy. But for any other kind of expression, this sl@0: ** routine make a copy of the result set column as the argument to the sl@0: ** TK_AS operator. The TK_AS operator causes the expression to be sl@0: ** evaluated just once and then reused for each alias. sl@0: ** sl@0: ** The reason for suppressing the TK_AS term when the expression is a simple sl@0: ** column reference is so that the column reference will be recognized as sl@0: ** usable by indices within the WHERE clause processing logic. sl@0: ** sl@0: ** Hack: The TK_AS operator is inhibited if zType[0]=='G'. This means sl@0: ** that in a GROUP BY clause, the expression is evaluated twice. Hence: sl@0: ** sl@0: ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY x sl@0: ** sl@0: ** Is equivalent to: sl@0: ** sl@0: ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5 sl@0: ** sl@0: ** The result of random()%5 in the GROUP BY clause is probably different sl@0: ** from the result in the result-set. We might fix this someday. Or sl@0: ** then again, we might not... sl@0: */ sl@0: static void resolveAlias( sl@0: Parse *pParse, /* Parsing context */ sl@0: ExprList *pEList, /* A result set */ sl@0: int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ sl@0: Expr *pExpr, /* Transform this into an alias to the result set */ sl@0: const char *zType /* "GROUP" or "ORDER" or "" */ sl@0: ){ sl@0: Expr *pOrig; /* The iCol-th column of the result set */ sl@0: Expr *pDup; /* Copy of pOrig */ sl@0: sqlite3 *db; /* The database connection */ sl@0: sl@0: assert( iCol>=0 && iColnExpr ); sl@0: pOrig = pEList->a[iCol].pExpr; sl@0: assert( pOrig!=0 ); sl@0: assert( pOrig->flags & EP_Resolved ); sl@0: db = pParse->db; sl@0: pDup = sqlite3ExprDup(db, pOrig); sl@0: if( pDup==0 ) return; sl@0: if( pDup->op!=TK_COLUMN && zType[0]!='G' ){ sl@0: pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0); sl@0: if( pDup==0 ) return; sl@0: if( pEList->a[iCol].iAlias==0 ){ sl@0: pEList->a[iCol].iAlias = ++pParse->nAlias; sl@0: } sl@0: pDup->iTable = pEList->a[iCol].iAlias; sl@0: } sl@0: if( pExpr->flags & EP_ExpCollate ){ sl@0: pDup->pColl = pExpr->pColl; sl@0: pDup->flags |= EP_ExpCollate; sl@0: } sl@0: if( pExpr->span.dyn ) sqlite3DbFree(db, (char*)pExpr->span.z); sl@0: if( pExpr->token.dyn ) sqlite3DbFree(db, (char*)pExpr->token.z); sl@0: memcpy(pExpr, pDup, sizeof(*pExpr)); sl@0: sqlite3DbFree(db, pDup); sl@0: } sl@0: sl@0: /* sl@0: ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up sl@0: ** that name in the set of source tables in pSrcList and make the pExpr sl@0: ** expression node refer back to that source column. The following changes sl@0: ** are made to pExpr: sl@0: ** sl@0: ** pExpr->iDb Set the index in db->aDb[] of the database X sl@0: ** (even if X is implied). sl@0: ** pExpr->iTable Set to the cursor number for the table obtained sl@0: ** from pSrcList. sl@0: ** pExpr->pTab Points to the Table structure of X.Y (even if sl@0: ** X and/or Y are implied.) sl@0: ** pExpr->iColumn Set to the column number within the table. sl@0: ** pExpr->op Set to TK_COLUMN. sl@0: ** pExpr->pLeft Any expression this points to is deleted sl@0: ** pExpr->pRight Any expression this points to is deleted. sl@0: ** sl@0: ** The pDbToken is the name of the database (the "X"). This value may be sl@0: ** NULL meaning that name is of the form Y.Z or Z. Any available database sl@0: ** can be used. The pTableToken is the name of the table (the "Y"). This sl@0: ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it sl@0: ** means that the form of the name is Z and that columns from any table sl@0: ** can be used. sl@0: ** sl@0: ** If the name cannot be resolved unambiguously, leave an error message sl@0: ** in pParse and return non-zero. Return zero on success. sl@0: */ sl@0: static int lookupName( sl@0: Parse *pParse, /* The parsing context */ sl@0: Token *pDbToken, /* Name of the database containing table, or NULL */ sl@0: Token *pTableToken, /* Name of table containing column, or NULL */ sl@0: Token *pColumnToken, /* Name of the column. */ sl@0: NameContext *pNC, /* The name context used to resolve the name */ sl@0: Expr *pExpr /* Make this EXPR node point to the selected column */ sl@0: ){ sl@0: char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */ sl@0: char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */ sl@0: char *zCol = 0; /* Name of the column. The "Z" */ sl@0: int i, j; /* Loop counters */ sl@0: int cnt = 0; /* Number of matching column names */ sl@0: int cntTab = 0; /* Number of matching table names */ sl@0: sqlite3 *db = pParse->db; /* The database connection */ sl@0: struct SrcList_item *pItem; /* Use for looping over pSrcList items */ sl@0: struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ sl@0: NameContext *pTopNC = pNC; /* First namecontext in the list */ sl@0: Schema *pSchema = 0; /* Schema of the expression */ sl@0: sl@0: assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */ sl@0: sl@0: /* Dequote and zero-terminate the names */ sl@0: zDb = sqlite3NameFromToken(db, pDbToken); sl@0: zTab = sqlite3NameFromToken(db, pTableToken); sl@0: zCol = sqlite3NameFromToken(db, pColumnToken); sl@0: if( db->mallocFailed ){ sl@0: goto lookupname_end; sl@0: } sl@0: sl@0: /* Initialize the node to no-match */ sl@0: pExpr->iTable = -1; sl@0: pExpr->pTab = 0; sl@0: sl@0: /* Start at the inner-most context and move outward until a match is found */ sl@0: while( pNC && cnt==0 ){ sl@0: ExprList *pEList; sl@0: SrcList *pSrcList = pNC->pSrcList; sl@0: sl@0: if( pSrcList ){ sl@0: for(i=0, pItem=pSrcList->a; inSrc; i++, pItem++){ sl@0: Table *pTab; sl@0: int iDb; sl@0: Column *pCol; sl@0: sl@0: pTab = pItem->pTab; sl@0: assert( pTab!=0 && pTab->zName!=0 ); sl@0: iDb = sqlite3SchemaToIndex(db, pTab->pSchema); sl@0: assert( pTab->nCol>0 ); sl@0: if( zTab ){ sl@0: if( pItem->zAlias ){ sl@0: char *zTabName = pItem->zAlias; sl@0: if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; sl@0: }else{ sl@0: char *zTabName = pTab->zName; sl@0: if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; sl@0: if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ sl@0: continue; sl@0: } sl@0: } sl@0: } sl@0: if( 0==(cntTab++) ){ sl@0: pExpr->iTable = pItem->iCursor; sl@0: pExpr->pTab = pTab; sl@0: pSchema = pTab->pSchema; sl@0: pMatch = pItem; sl@0: } sl@0: for(j=0, pCol=pTab->aCol; jnCol; j++, pCol++){ sl@0: if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ sl@0: IdList *pUsing; sl@0: cnt++; sl@0: pExpr->iTable = pItem->iCursor; sl@0: pExpr->pTab = pTab; sl@0: pMatch = pItem; sl@0: pSchema = pTab->pSchema; sl@0: /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ sl@0: pExpr->iColumn = j==pTab->iPKey ? -1 : j; sl@0: if( inSrc-1 ){ sl@0: if( pItem[1].jointype & JT_NATURAL ){ sl@0: /* If this match occurred in the left table of a natural join, sl@0: ** then skip the right table to avoid a duplicate match */ sl@0: pItem++; sl@0: i++; sl@0: }else if( (pUsing = pItem[1].pUsing)!=0 ){ sl@0: /* If this match occurs on a column that is in the USING clause sl@0: ** of a join, skip the search of the right table of the join sl@0: ** to avoid a duplicate match there. */ sl@0: int k; sl@0: for(k=0; knId; k++){ sl@0: if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ sl@0: pItem++; sl@0: i++; sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: #ifndef SQLITE_OMIT_TRIGGER sl@0: /* If we have not already resolved the name, then maybe sl@0: ** it is a new.* or old.* trigger argument reference sl@0: */ sl@0: if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ sl@0: TriggerStack *pTriggerStack = pParse->trigStack; sl@0: Table *pTab = 0; sl@0: u32 *piColMask; sl@0: if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ sl@0: pExpr->iTable = pTriggerStack->newIdx; sl@0: assert( pTriggerStack->pTab ); sl@0: pTab = pTriggerStack->pTab; sl@0: piColMask = &(pTriggerStack->newColMask); sl@0: }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ sl@0: pExpr->iTable = pTriggerStack->oldIdx; sl@0: assert( pTriggerStack->pTab ); sl@0: pTab = pTriggerStack->pTab; sl@0: piColMask = &(pTriggerStack->oldColMask); sl@0: } sl@0: sl@0: if( pTab ){ sl@0: int iCol; sl@0: Column *pCol = pTab->aCol; sl@0: sl@0: pSchema = pTab->pSchema; sl@0: cntTab++; sl@0: for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { sl@0: if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ sl@0: cnt++; sl@0: pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; sl@0: pExpr->pTab = pTab; sl@0: if( iCol>=0 ){ sl@0: testcase( iCol==31 ); sl@0: testcase( iCol==32 ); sl@0: *piColMask |= ((u32)1<=32?0xffffffff:0); sl@0: } sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: #endif /* !defined(SQLITE_OMIT_TRIGGER) */ sl@0: sl@0: /* sl@0: ** Perhaps the name is a reference to the ROWID sl@0: */ sl@0: if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ sl@0: cnt = 1; sl@0: pExpr->iColumn = -1; sl@0: pExpr->affinity = SQLITE_AFF_INTEGER; sl@0: } sl@0: sl@0: /* sl@0: ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z sl@0: ** might refer to an result-set alias. This happens, for example, when sl@0: ** we are resolving names in the WHERE clause of the following command: sl@0: ** sl@0: ** SELECT a+b AS x FROM table WHERE x<10; sl@0: ** sl@0: ** In cases like this, replace pExpr with a copy of the expression that sl@0: ** forms the result set entry ("a+b" in the example) and return immediately. sl@0: ** Note that the expression in the result set should have already been sl@0: ** resolved by the time the WHERE clause is resolved. sl@0: */ sl@0: if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ sl@0: for(j=0; jnExpr; j++){ sl@0: char *zAs = pEList->a[j].zName; sl@0: if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ sl@0: Expr *pOrig; sl@0: assert( pExpr->pLeft==0 && pExpr->pRight==0 ); sl@0: assert( pExpr->pList==0 ); sl@0: assert( pExpr->pSelect==0 ); sl@0: pOrig = pEList->a[j].pExpr; sl@0: if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ sl@0: sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); sl@0: sqlite3DbFree(db, zCol); sl@0: return 2; sl@0: } sl@0: resolveAlias(pParse, pEList, j, pExpr, ""); sl@0: cnt = 1; sl@0: pMatch = 0; sl@0: assert( zTab==0 && zDb==0 ); sl@0: goto lookupname_end_2; sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* Advance to the next name context. The loop will exit when either sl@0: ** we have a match (cnt>0) or when we run out of name contexts. sl@0: */ sl@0: if( cnt==0 ){ sl@0: pNC = pNC->pNext; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** If X and Y are NULL (in other words if only the column name Z is sl@0: ** supplied) and the value of Z is enclosed in double-quotes, then sl@0: ** Z is a string literal if it doesn't match any column names. In that sl@0: ** case, we need to return right away and not make any changes to sl@0: ** pExpr. sl@0: ** sl@0: ** Because no reference was made to outer contexts, the pNC->nRef sl@0: ** fields are not changed in any context. sl@0: */ sl@0: if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){ sl@0: sqlite3DbFree(db, zCol); sl@0: pExpr->op = TK_STRING; sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** cnt==0 means there was not match. cnt>1 means there were two or sl@0: ** more matches. Either way, we have an error. sl@0: */ sl@0: if( cnt!=1 ){ sl@0: const char *zErr; sl@0: zErr = cnt==0 ? "no such column" : "ambiguous column name"; sl@0: if( zDb ){ sl@0: sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); sl@0: }else if( zTab ){ sl@0: sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); sl@0: }else{ sl@0: sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); sl@0: } sl@0: pTopNC->nErr++; sl@0: } sl@0: sl@0: /* If a column from a table in pSrcList is referenced, then record sl@0: ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes sl@0: ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the sl@0: ** column number is greater than the number of bits in the bitmask sl@0: ** then set the high-order bit of the bitmask. sl@0: */ sl@0: if( pExpr->iColumn>=0 && pMatch!=0 ){ sl@0: int n = pExpr->iColumn; sl@0: testcase( n==sizeof(Bitmask)*8-1 ); sl@0: if( n>=sizeof(Bitmask)*8 ){ sl@0: n = sizeof(Bitmask)*8-1; sl@0: } sl@0: assert( pMatch->iCursor==pExpr->iTable ); sl@0: pMatch->colUsed |= ((Bitmask)1)<pLeft); sl@0: pExpr->pLeft = 0; sl@0: sqlite3ExprDelete(db, pExpr->pRight); sl@0: pExpr->pRight = 0; sl@0: pExpr->op = TK_COLUMN; sl@0: lookupname_end_2: sl@0: sqlite3DbFree(db, zCol); sl@0: if( cnt==1 ){ sl@0: assert( pNC!=0 ); sl@0: sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); sl@0: /* Increment the nRef value on all name contexts from TopNC up to sl@0: ** the point where the name matched. */ sl@0: for(;;){ sl@0: assert( pTopNC!=0 ); sl@0: pTopNC->nRef++; sl@0: if( pTopNC==pNC ) break; sl@0: pTopNC = pTopNC->pNext; sl@0: } sl@0: return 0; sl@0: } else { sl@0: return 1; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** This routine is callback for sqlite3WalkExpr(). sl@0: ** sl@0: ** Resolve symbolic names into TK_COLUMN operators for the current sl@0: ** node in the expression tree. Return 0 to continue the search down sl@0: ** the tree or 2 to abort the tree walk. sl@0: ** sl@0: ** This routine also does error checking and name resolution for sl@0: ** function names. The operator for aggregate functions is changed sl@0: ** to TK_AGG_FUNCTION. sl@0: */ sl@0: static int resolveExprStep(Walker *pWalker, Expr *pExpr){ sl@0: NameContext *pNC; sl@0: Parse *pParse; sl@0: sl@0: pNC = pWalker->u.pNC; sl@0: assert( pNC!=0 ); sl@0: pParse = pNC->pParse; sl@0: assert( pParse==pWalker->pParse ); sl@0: sl@0: if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune; sl@0: ExprSetProperty(pExpr, EP_Resolved); sl@0: #ifndef NDEBUG sl@0: if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ sl@0: SrcList *pSrcList = pNC->pSrcList; sl@0: int i; sl@0: for(i=0; ipSrcList->nSrc; i++){ sl@0: assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursornTab); sl@0: } sl@0: } sl@0: #endif sl@0: switch( pExpr->op ){ sl@0: /* A lone identifier is the name of a column. sl@0: */ sl@0: case TK_ID: { sl@0: lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr); sl@0: return WRC_Prune; sl@0: } sl@0: sl@0: /* A table name and column name: ID.ID sl@0: ** Or a database, table and column: ID.ID.ID sl@0: */ sl@0: case TK_DOT: { sl@0: Token *pColumn; sl@0: Token *pTable; sl@0: Token *pDb; sl@0: Expr *pRight; sl@0: sl@0: /* if( pSrcList==0 ) break; */ sl@0: pRight = pExpr->pRight; sl@0: if( pRight->op==TK_ID ){ sl@0: pDb = 0; sl@0: pTable = &pExpr->pLeft->token; sl@0: pColumn = &pRight->token; sl@0: }else{ sl@0: assert( pRight->op==TK_DOT ); sl@0: pDb = &pExpr->pLeft->token; sl@0: pTable = &pRight->pLeft->token; sl@0: pColumn = &pRight->pRight->token; sl@0: } sl@0: lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr); sl@0: return WRC_Prune; sl@0: } sl@0: sl@0: /* Resolve function names sl@0: */ sl@0: case TK_CONST_FUNC: sl@0: case TK_FUNCTION: { sl@0: ExprList *pList = pExpr->pList; /* The argument list */ sl@0: int n = pList ? pList->nExpr : 0; /* Number of arguments */ sl@0: int no_such_func = 0; /* True if no such function exists */ sl@0: int wrong_num_args = 0; /* True if wrong number of arguments */ sl@0: int is_agg = 0; /* True if is an aggregate function */ sl@0: int auth; /* Authorization to use the function */ sl@0: int nId; /* Number of characters in function name */ sl@0: const char *zId; /* The function name. */ sl@0: FuncDef *pDef; /* Information about the function */ sl@0: int enc = ENC(pParse->db); /* The database encoding */ sl@0: sl@0: zId = (char*)pExpr->token.z; sl@0: nId = pExpr->token.n; sl@0: pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); sl@0: if( pDef==0 ){ sl@0: pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); sl@0: if( pDef==0 ){ sl@0: no_such_func = 1; sl@0: }else{ sl@0: wrong_num_args = 1; sl@0: } sl@0: }else{ sl@0: is_agg = pDef->xFunc==0; sl@0: } sl@0: #ifndef SQLITE_OMIT_AUTHORIZATION sl@0: if( pDef ){ sl@0: auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); sl@0: if( auth!=SQLITE_OK ){ sl@0: if( auth==SQLITE_DENY ){ sl@0: sqlite3ErrorMsg(pParse, "not authorized to use function: %s", sl@0: pDef->zName); sl@0: pNC->nErr++; sl@0: } sl@0: pExpr->op = TK_NULL; sl@0: return WRC_Prune; sl@0: } sl@0: } sl@0: #endif sl@0: if( is_agg && !pNC->allowAgg ){ sl@0: sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); sl@0: pNC->nErr++; sl@0: is_agg = 0; sl@0: }else if( no_such_func ){ sl@0: sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); sl@0: pNC->nErr++; sl@0: }else if( wrong_num_args ){ sl@0: sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", sl@0: nId, zId); sl@0: pNC->nErr++; sl@0: } sl@0: if( is_agg ){ sl@0: pExpr->op = TK_AGG_FUNCTION; sl@0: pNC->hasAgg = 1; sl@0: } sl@0: if( is_agg ) pNC->allowAgg = 0; sl@0: sqlite3WalkExprList(pWalker, pList); sl@0: if( is_agg ) pNC->allowAgg = 1; sl@0: /* FIX ME: Compute pExpr->affinity based on the expected return sl@0: ** type of the function sl@0: */ sl@0: return WRC_Prune; sl@0: } sl@0: #ifndef SQLITE_OMIT_SUBQUERY sl@0: case TK_SELECT: sl@0: case TK_EXISTS: sl@0: #endif sl@0: case TK_IN: { sl@0: if( pExpr->pSelect ){ sl@0: int nRef = pNC->nRef; sl@0: #ifndef SQLITE_OMIT_CHECK sl@0: if( pNC->isCheck ){ sl@0: sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); sl@0: } sl@0: #endif sl@0: sqlite3WalkSelect(pWalker, pExpr->pSelect); sl@0: assert( pNC->nRef>=nRef ); sl@0: if( nRef!=pNC->nRef ){ sl@0: ExprSetProperty(pExpr, EP_VarSelect); sl@0: } sl@0: } sl@0: break; sl@0: } sl@0: #ifndef SQLITE_OMIT_CHECK sl@0: case TK_VARIABLE: { sl@0: if( pNC->isCheck ){ sl@0: sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); sl@0: } sl@0: break; sl@0: } sl@0: #endif sl@0: } sl@0: return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; sl@0: } sl@0: sl@0: /* sl@0: ** pEList is a list of expressions which are really the result set of the sl@0: ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. sl@0: ** This routine checks to see if pE is a simple identifier which corresponds sl@0: ** to the AS-name of one of the terms of the expression list. If it is, sl@0: ** this routine return an integer between 1 and N where N is the number of sl@0: ** elements in pEList, corresponding to the matching entry. If there is sl@0: ** no match, or if pE is not a simple identifier, then this routine sl@0: ** return 0. sl@0: ** sl@0: ** pEList has been resolved. pE has not. sl@0: */ sl@0: static int resolveAsName( sl@0: Parse *pParse, /* Parsing context for error messages */ sl@0: ExprList *pEList, /* List of expressions to scan */ sl@0: Expr *pE /* Expression we are trying to match */ sl@0: ){ sl@0: int i; /* Loop counter */ sl@0: sl@0: if( pE->op==TK_ID || (pE->op==TK_STRING && pE->token.z[0]!='\'') ){ sl@0: sqlite3 *db = pParse->db; sl@0: char *zCol = sqlite3NameFromToken(db, &pE->token); sl@0: if( zCol==0 ){ sl@0: return -1; sl@0: } sl@0: for(i=0; inExpr; i++){ sl@0: char *zAs = pEList->a[i].zName; sl@0: if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ sl@0: sqlite3DbFree(db, zCol); sl@0: return i+1; sl@0: } sl@0: } sl@0: sqlite3DbFree(db, zCol); sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** pE is a pointer to an expression which is a single term in the sl@0: ** ORDER BY of a compound SELECT. The expression has not been sl@0: ** name resolved. sl@0: ** sl@0: ** At the point this routine is called, we already know that the sl@0: ** ORDER BY term is not an integer index into the result set. That sl@0: ** case is handled by the calling routine. sl@0: ** sl@0: ** Attempt to match pE against result set columns in the left-most sl@0: ** SELECT statement. Return the index i of the matching column, sl@0: ** as an indication to the caller that it should sort by the i-th column. sl@0: ** The left-most column is 1. In other words, the value returned is the sl@0: ** same integer value that would be used in the SQL statement to indicate sl@0: ** the column. sl@0: ** sl@0: ** If there is no match, return 0. Return -1 if an error occurs. sl@0: */ sl@0: static int resolveOrderByTermToExprList( sl@0: Parse *pParse, /* Parsing context for error messages */ sl@0: Select *pSelect, /* The SELECT statement with the ORDER BY clause */ sl@0: Expr *pE /* The specific ORDER BY term */ sl@0: ){ sl@0: int i; /* Loop counter */ sl@0: ExprList *pEList; /* The columns of the result set */ sl@0: NameContext nc; /* Name context for resolving pE */ sl@0: sl@0: assert( sqlite3ExprIsInteger(pE, &i)==0 ); sl@0: pEList = pSelect->pEList; sl@0: sl@0: /* Resolve all names in the ORDER BY term expression sl@0: */ sl@0: memset(&nc, 0, sizeof(nc)); sl@0: nc.pParse = pParse; sl@0: nc.pSrcList = pSelect->pSrc; sl@0: nc.pEList = pEList; sl@0: nc.allowAgg = 1; sl@0: nc.nErr = 0; sl@0: if( sqlite3ResolveExprNames(&nc, pE) ){ sl@0: sqlite3ErrorClear(pParse); sl@0: return 0; sl@0: } sl@0: sl@0: /* Try to match the ORDER BY expression against an expression sl@0: ** in the result set. Return an 1-based index of the matching sl@0: ** result-set entry. sl@0: */ sl@0: for(i=0; inExpr; i++){ sl@0: if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){ sl@0: return i+1; sl@0: } sl@0: } sl@0: sl@0: /* If no match, return 0. */ sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** Generate an ORDER BY or GROUP BY term out-of-range error. sl@0: */ sl@0: static void resolveOutOfRangeError( sl@0: Parse *pParse, /* The error context into which to write the error */ sl@0: const char *zType, /* "ORDER" or "GROUP" */ sl@0: int i, /* The index (1-based) of the term out of range */ sl@0: int mx /* Largest permissible value of i */ sl@0: ){ sl@0: sqlite3ErrorMsg(pParse, sl@0: "%r %s BY term out of range - should be " sl@0: "between 1 and %d", i, zType, mx); sl@0: } sl@0: sl@0: /* sl@0: ** Analyze the ORDER BY clause in a compound SELECT statement. Modify sl@0: ** each term of the ORDER BY clause is a constant integer between 1 sl@0: ** and N where N is the number of columns in the compound SELECT. sl@0: ** sl@0: ** ORDER BY terms that are already an integer between 1 and N are sl@0: ** unmodified. ORDER BY terms that are integers outside the range of sl@0: ** 1 through N generate an error. ORDER BY terms that are expressions sl@0: ** are matched against result set expressions of compound SELECT sl@0: ** beginning with the left-most SELECT and working toward the right. sl@0: ** At the first match, the ORDER BY expression is transformed into sl@0: ** the integer column number. sl@0: ** sl@0: ** Return the number of errors seen. sl@0: */ sl@0: static int resolveCompoundOrderBy( sl@0: Parse *pParse, /* Parsing context. Leave error messages here */ sl@0: Select *pSelect /* The SELECT statement containing the ORDER BY */ sl@0: ){ sl@0: int i; sl@0: ExprList *pOrderBy; sl@0: ExprList *pEList; sl@0: sqlite3 *db; sl@0: int moreToDo = 1; sl@0: sl@0: pOrderBy = pSelect->pOrderBy; sl@0: if( pOrderBy==0 ) return 0; sl@0: db = pParse->db; sl@0: #if SQLITE_MAX_COLUMN sl@0: if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ sl@0: sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); sl@0: return 1; sl@0: } sl@0: #endif sl@0: for(i=0; inExpr; i++){ sl@0: pOrderBy->a[i].done = 0; sl@0: } sl@0: pSelect->pNext = 0; sl@0: while( pSelect->pPrior ){ sl@0: pSelect->pPrior->pNext = pSelect; sl@0: pSelect = pSelect->pPrior; sl@0: } sl@0: while( pSelect && moreToDo ){ sl@0: struct ExprList_item *pItem; sl@0: moreToDo = 0; sl@0: pEList = pSelect->pEList; sl@0: assert( pEList!=0 ); sl@0: for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ sl@0: int iCol = -1; sl@0: Expr *pE, *pDup; sl@0: if( pItem->done ) continue; sl@0: pE = pItem->pExpr; sl@0: if( sqlite3ExprIsInteger(pE, &iCol) ){ sl@0: if( iCol<0 || iCol>pEList->nExpr ){ sl@0: resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr); sl@0: return 1; sl@0: } sl@0: }else{ sl@0: iCol = resolveAsName(pParse, pEList, pE); sl@0: if( iCol==0 ){ sl@0: pDup = sqlite3ExprDup(db, pE); sl@0: if( !db->mallocFailed ){ sl@0: assert(pDup); sl@0: iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); sl@0: } sl@0: sqlite3ExprDelete(db, pDup); sl@0: } sl@0: if( iCol<0 ){ sl@0: return 1; sl@0: } sl@0: } sl@0: if( iCol>0 ){ sl@0: CollSeq *pColl = pE->pColl; sl@0: int flags = pE->flags & EP_ExpCollate; sl@0: sqlite3ExprDelete(db, pE); sl@0: pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0, 0, 0); sl@0: if( pE==0 ) return 1; sl@0: pE->pColl = pColl; sl@0: pE->flags |= EP_IntValue | flags; sl@0: pE->iTable = iCol; sl@0: pItem->iCol = iCol; sl@0: pItem->done = 1; sl@0: }else{ sl@0: moreToDo = 1; sl@0: } sl@0: } sl@0: pSelect = pSelect->pNext; sl@0: } sl@0: for(i=0; inExpr; i++){ sl@0: if( pOrderBy->a[i].done==0 ){ sl@0: sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any " sl@0: "column in the result set", i+1); sl@0: return 1; sl@0: } sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of sl@0: ** the SELECT statement pSelect. If any term is reference to a sl@0: ** result set expression (as determined by the ExprList.a.iCol field) sl@0: ** then convert that term into a copy of the corresponding result set sl@0: ** column. sl@0: ** sl@0: ** If any errors are detected, add an error message to pParse and sl@0: ** return non-zero. Return zero if no errors are seen. sl@0: */ sl@0: int sqlite3ResolveOrderGroupBy( sl@0: Parse *pParse, /* Parsing context. Leave error messages here */ sl@0: Select *pSelect, /* The SELECT statement containing the clause */ sl@0: ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ sl@0: const char *zType /* "ORDER" or "GROUP" */ sl@0: ){ sl@0: int i; sl@0: sqlite3 *db = pParse->db; sl@0: ExprList *pEList; sl@0: struct ExprList_item *pItem; sl@0: sl@0: if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; sl@0: #if SQLITE_MAX_COLUMN sl@0: if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ sl@0: sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); sl@0: return 1; sl@0: } sl@0: #endif sl@0: pEList = pSelect->pEList; sl@0: assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ sl@0: for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ sl@0: if( pItem->iCol ){ sl@0: if( pItem->iCol>pEList->nExpr ){ sl@0: resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); sl@0: return 1; sl@0: } sl@0: resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType); sl@0: } sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. sl@0: ** The Name context of the SELECT statement is pNC. zType is either sl@0: ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. sl@0: ** sl@0: ** This routine resolves each term of the clause into an expression. sl@0: ** If the order-by term is an integer I between 1 and N (where N is the sl@0: ** number of columns in the result set of the SELECT) then the expression sl@0: ** in the resolution is a copy of the I-th result-set expression. If sl@0: ** the order-by term is an identify that corresponds to the AS-name of sl@0: ** a result-set expression, then the term resolves to a copy of the sl@0: ** result-set expression. Otherwise, the expression is resolved in sl@0: ** the usual way - using sqlite3ResolveExprNames(). sl@0: ** sl@0: ** This routine returns the number of errors. If errors occur, then sl@0: ** an appropriate error message might be left in pParse. (OOM errors sl@0: ** excepted.) sl@0: */ sl@0: static int resolveOrderGroupBy( sl@0: NameContext *pNC, /* The name context of the SELECT statement */ sl@0: Select *pSelect, /* The SELECT statement holding pOrderBy */ sl@0: ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */ sl@0: const char *zType /* Either "ORDER" or "GROUP", as appropriate */ sl@0: ){ sl@0: int i; /* Loop counter */ sl@0: int iCol; /* Column number */ sl@0: struct ExprList_item *pItem; /* A term of the ORDER BY clause */ sl@0: Parse *pParse; /* Parsing context */ sl@0: int nResult; /* Number of terms in the result set */ sl@0: sl@0: if( pOrderBy==0 ) return 0; sl@0: nResult = pSelect->pEList->nExpr; sl@0: pParse = pNC->pParse; sl@0: for(i=0, pItem=pOrderBy->a; inExpr; i++, pItem++){ sl@0: Expr *pE = pItem->pExpr; sl@0: iCol = resolveAsName(pParse, pSelect->pEList, pE); sl@0: if( iCol<0 ){ sl@0: return 1; /* OOM error */ sl@0: } sl@0: if( iCol>0 ){ sl@0: /* If an AS-name match is found, mark this ORDER BY column as being sl@0: ** a copy of the iCol-th result-set column. The subsequent call to sl@0: ** sqlite3ResolveOrderGroupBy() will convert the expression to a sl@0: ** copy of the iCol-th result-set expression. */ sl@0: pItem->iCol = iCol; sl@0: continue; sl@0: } sl@0: if( sqlite3ExprIsInteger(pE, &iCol) ){ sl@0: /* The ORDER BY term is an integer constant. Again, set the column sl@0: ** number so that sqlite3ResolveOrderGroupBy() will convert the sl@0: ** order-by term to a copy of the result-set expression */ sl@0: if( iCol<1 ){ sl@0: resolveOutOfRangeError(pParse, zType, i+1, nResult); sl@0: return 1; sl@0: } sl@0: pItem->iCol = iCol; sl@0: continue; sl@0: } sl@0: sl@0: /* Otherwise, treat the ORDER BY term as an ordinary expression */ sl@0: pItem->iCol = 0; sl@0: if( sqlite3ResolveExprNames(pNC, pE) ){ sl@0: return 1; sl@0: } sl@0: } sl@0: return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); sl@0: } sl@0: sl@0: /* sl@0: ** Resolve names in the SELECT statement p and all of its descendents. sl@0: */ sl@0: static int resolveSelectStep(Walker *pWalker, Select *p){ sl@0: NameContext *pOuterNC; /* Context that contains this SELECT */ sl@0: NameContext sNC; /* Name context of this SELECT */ sl@0: int isCompound; /* True if p is a compound select */ sl@0: int nCompound; /* Number of compound terms processed so far */ sl@0: Parse *pParse; /* Parsing context */ sl@0: ExprList *pEList; /* Result set expression list */ sl@0: int i; /* Loop counter */ sl@0: ExprList *pGroupBy; /* The GROUP BY clause */ sl@0: Select *pLeftmost; /* Left-most of SELECT of a compound */ sl@0: sqlite3 *db; /* Database connection */ sl@0: sl@0: sl@0: assert( p!=0 ); sl@0: if( p->selFlags & SF_Resolved ){ sl@0: return WRC_Prune; sl@0: } sl@0: pOuterNC = pWalker->u.pNC; sl@0: pParse = pWalker->pParse; sl@0: db = pParse->db; sl@0: sl@0: /* Normally sqlite3SelectExpand() will be called first and will have sl@0: ** already expanded this SELECT. However, if this is a subquery within sl@0: ** an expression, sqlite3ResolveExprNames() will be called without a sl@0: ** prior call to sqlite3SelectExpand(). When that happens, let sl@0: ** sqlite3SelectPrep() do all of the processing for this SELECT. sl@0: ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and sl@0: ** this routine in the correct order. sl@0: */ sl@0: if( (p->selFlags & SF_Expanded)==0 ){ sl@0: sqlite3SelectPrep(pParse, p, pOuterNC); sl@0: return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune; sl@0: } sl@0: sl@0: isCompound = p->pPrior!=0; sl@0: nCompound = 0; sl@0: pLeftmost = p; sl@0: while( p ){ sl@0: assert( (p->selFlags & SF_Expanded)!=0 ); sl@0: assert( (p->selFlags & SF_Resolved)==0 ); sl@0: p->selFlags |= SF_Resolved; sl@0: sl@0: /* Resolve the expressions in the LIMIT and OFFSET clauses. These sl@0: ** are not allowed to refer to any names, so pass an empty NameContext. sl@0: */ sl@0: memset(&sNC, 0, sizeof(sNC)); sl@0: sNC.pParse = pParse; sl@0: if( sqlite3ResolveExprNames(&sNC, p->pLimit) || sl@0: sqlite3ResolveExprNames(&sNC, p->pOffset) ){ sl@0: return WRC_Abort; sl@0: } sl@0: sl@0: /* Set up the local name-context to pass to sqlite3ResolveExprNames() to sl@0: ** resolve the result-set expression list. sl@0: */ sl@0: sNC.allowAgg = 1; sl@0: sNC.pSrcList = p->pSrc; sl@0: sNC.pNext = pOuterNC; sl@0: sl@0: /* Resolve names in the result set. */ sl@0: pEList = p->pEList; sl@0: assert( pEList!=0 ); sl@0: for(i=0; inExpr; i++){ sl@0: Expr *pX = pEList->a[i].pExpr; sl@0: if( sqlite3ResolveExprNames(&sNC, pX) ){ sl@0: return WRC_Abort; sl@0: } sl@0: } sl@0: sl@0: /* Recursively resolve names in all subqueries sl@0: */ sl@0: for(i=0; ipSrc->nSrc; i++){ sl@0: struct SrcList_item *pItem = &p->pSrc->a[i]; sl@0: if( pItem->pSelect ){ sl@0: const char *zSavedContext = pParse->zAuthContext; sl@0: if( pItem->zName ) pParse->zAuthContext = pItem->zName; sl@0: sqlite3ResolveSelectNames(pParse, pItem->pSelect, &sNC); sl@0: pParse->zAuthContext = zSavedContext; sl@0: if( pParse->nErr || db->mallocFailed ) return WRC_Abort; sl@0: } sl@0: } sl@0: sl@0: /* If there are no aggregate functions in the result-set, and no GROUP BY sl@0: ** expression, do not allow aggregates in any of the other expressions. sl@0: */ sl@0: assert( (p->selFlags & SF_Aggregate)==0 ); sl@0: pGroupBy = p->pGroupBy; sl@0: if( pGroupBy || sNC.hasAgg ){ sl@0: p->selFlags |= SF_Aggregate; sl@0: }else{ sl@0: sNC.allowAgg = 0; sl@0: } sl@0: sl@0: /* If a HAVING clause is present, then there must be a GROUP BY clause. sl@0: */ sl@0: if( p->pHaving && !pGroupBy ){ sl@0: sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); sl@0: return WRC_Abort; sl@0: } sl@0: sl@0: /* Add the expression list to the name-context before parsing the sl@0: ** other expressions in the SELECT statement. This is so that sl@0: ** expressions in the WHERE clause (etc.) can refer to expressions by sl@0: ** aliases in the result set. sl@0: ** sl@0: ** Minor point: If this is the case, then the expression will be sl@0: ** re-evaluated for each reference to it. sl@0: */ sl@0: sNC.pEList = p->pEList; sl@0: if( sqlite3ResolveExprNames(&sNC, p->pWhere) || sl@0: sqlite3ResolveExprNames(&sNC, p->pHaving) sl@0: ){ sl@0: return WRC_Abort; sl@0: } sl@0: sl@0: /* The ORDER BY and GROUP BY clauses may not refer to terms in sl@0: ** outer queries sl@0: */ sl@0: sNC.pNext = 0; sl@0: sNC.allowAgg = 1; sl@0: sl@0: /* Process the ORDER BY clause for singleton SELECT statements. sl@0: ** The ORDER BY clause for compounds SELECT statements is handled sl@0: ** below, after all of the result-sets for all of the elements of sl@0: ** the compound have been resolved. sl@0: */ sl@0: if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){ sl@0: return WRC_Abort; sl@0: } sl@0: if( db->mallocFailed ){ sl@0: return WRC_Abort; sl@0: } sl@0: sl@0: /* Resolve the GROUP BY clause. At the same time, make sure sl@0: ** the GROUP BY clause does not contain aggregate functions. sl@0: */ sl@0: if( pGroupBy ){ sl@0: struct ExprList_item *pItem; sl@0: sl@0: if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ sl@0: return WRC_Abort; sl@0: } sl@0: for(i=0, pItem=pGroupBy->a; inExpr; i++, pItem++){ sl@0: if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ sl@0: sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " sl@0: "the GROUP BY clause"); sl@0: return WRC_Abort; sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* Advance to the next term of the compound sl@0: */ sl@0: p = p->pPrior; sl@0: nCompound++; sl@0: } sl@0: sl@0: /* Resolve the ORDER BY on a compound SELECT after all terms of sl@0: ** the compound have been resolved. sl@0: */ sl@0: if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){ sl@0: return WRC_Abort; sl@0: } sl@0: sl@0: return WRC_Prune; sl@0: } sl@0: sl@0: /* sl@0: ** This routine walks an expression tree and resolves references to sl@0: ** table columns and result-set columns. At the same time, do error sl@0: ** checking on function usage and set a flag if any aggregate functions sl@0: ** are seen. sl@0: ** sl@0: ** To resolve table columns references we look for nodes (or subtrees) of the sl@0: ** form X.Y.Z or Y.Z or just Z where sl@0: ** sl@0: ** X: The name of a database. Ex: "main" or "temp" or sl@0: ** the symbolic name assigned to an ATTACH-ed database. sl@0: ** sl@0: ** Y: The name of a table in a FROM clause. Or in a trigger sl@0: ** one of the special names "old" or "new". sl@0: ** sl@0: ** Z: The name of a column in table Y. sl@0: ** sl@0: ** The node at the root of the subtree is modified as follows: sl@0: ** sl@0: ** Expr.op Changed to TK_COLUMN sl@0: ** Expr.pTab Points to the Table object for X.Y sl@0: ** Expr.iColumn The column index in X.Y. -1 for the rowid. sl@0: ** Expr.iTable The VDBE cursor number for X.Y sl@0: ** sl@0: ** sl@0: ** To resolve result-set references, look for expression nodes of the sl@0: ** form Z (with no X and Y prefix) where the Z matches the right-hand sl@0: ** size of an AS clause in the result-set of a SELECT. The Z expression sl@0: ** is replaced by a copy of the left-hand side of the result-set expression. sl@0: ** Table-name and function resolution occurs on the substituted expression sl@0: ** tree. For example, in: sl@0: ** sl@0: ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x; sl@0: ** sl@0: ** The "x" term of the order by is replaced by "a+b" to render: sl@0: ** sl@0: ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; sl@0: ** sl@0: ** Function calls are checked to make sure that the function is sl@0: ** defined and that the correct number of arguments are specified. sl@0: ** If the function is an aggregate function, then the pNC->hasAgg is sl@0: ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. sl@0: ** If an expression contains aggregate functions then the EP_Agg sl@0: ** property on the expression is set. sl@0: ** sl@0: ** An error message is left in pParse if anything is amiss. The number sl@0: ** if errors is returned. sl@0: */ sl@0: int sqlite3ResolveExprNames( sl@0: NameContext *pNC, /* Namespace to resolve expressions in. */ sl@0: Expr *pExpr /* The expression to be analyzed. */ sl@0: ){ sl@0: int savedHasAgg; sl@0: Walker w; sl@0: sl@0: if( pExpr==0 ) return 0; sl@0: #if SQLITE_MAX_EXPR_DEPTH>0 sl@0: { sl@0: Parse *pParse = pNC->pParse; sl@0: if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){ sl@0: return 1; sl@0: } sl@0: pParse->nHeight += pExpr->nHeight; sl@0: } sl@0: #endif sl@0: savedHasAgg = pNC->hasAgg; sl@0: pNC->hasAgg = 0; sl@0: w.xExprCallback = resolveExprStep; sl@0: w.xSelectCallback = resolveSelectStep; sl@0: w.pParse = pNC->pParse; sl@0: w.u.pNC = pNC; sl@0: sqlite3WalkExpr(&w, pExpr); sl@0: #if SQLITE_MAX_EXPR_DEPTH>0 sl@0: pNC->pParse->nHeight -= pExpr->nHeight; sl@0: #endif sl@0: if( pNC->nErr>0 ){ sl@0: ExprSetProperty(pExpr, EP_Error); sl@0: } sl@0: if( pNC->hasAgg ){ sl@0: ExprSetProperty(pExpr, EP_Agg); sl@0: }else if( savedHasAgg ){ sl@0: pNC->hasAgg = 1; sl@0: } sl@0: return ExprHasProperty(pExpr, EP_Error); sl@0: } sl@0: sl@0: sl@0: /* sl@0: ** Resolve all names in all expressions of a SELECT and in all sl@0: ** decendents of the SELECT, including compounds off of p->pPrior, sl@0: ** subqueries in expressions, and subqueries used as FROM clause sl@0: ** terms. sl@0: ** sl@0: ** See sqlite3ResolveExprNames() for a description of the kinds of sl@0: ** transformations that occur. sl@0: ** sl@0: ** All SELECT statements should have been expanded using sl@0: ** sqlite3SelectExpand() prior to invoking this routine. sl@0: */ sl@0: void sqlite3ResolveSelectNames( sl@0: Parse *pParse, /* The parser context */ sl@0: Select *p, /* The SELECT statement being coded. */ sl@0: NameContext *pOuterNC /* Name context for parent SELECT statement */ sl@0: ){ sl@0: Walker w; sl@0: sl@0: assert( p!=0 ); sl@0: w.xExprCallback = resolveExprStep; sl@0: w.xSelectCallback = resolveSelectStep; sl@0: w.pParse = pParse; sl@0: w.u.pNC = pOuterNC; sl@0: sqlite3WalkSelect(&w, p); sl@0: }