aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2012-04-17 16:38:53 +0000
committerdrh <drh@noemail.net>2012-04-17 16:38:53 +0000
commit374fdce485ab55739dfd307ac00a59ebe8ce8d25 (patch)
tree325d9cfeffa79ef18b2889c690f98acff4f5c51f /src
parentd8621b90c9caa7d7e1970e98b0c6992d414706a7 (diff)
downloadsqlite-374fdce485ab55739dfd307ac00a59ebe8ce8d25.tar.gz
sqlite-374fdce485ab55739dfd307ac00a59ebe8ce8d25.zip
Improved handling of aggregate subqueries within an aggregate query.
FossilOrigin-Name: 430bb59d798286a86c351de92c429345f016b3f0
Diffstat (limited to 'src')
-rw-r--r--src/expr.c52
-rw-r--r--src/sqliteInt.h2
2 files changed, 40 insertions, 14 deletions
diff --git a/src/expr.c b/src/expr.c
index 4751ec91b..328de4e5e 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -3778,7 +3778,7 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB){
if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
return 2;
}
- }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
+ }else if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
return 2;
@@ -3816,6 +3816,41 @@ int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
}
/*
+** This is the expression callback for sqlite3FunctionUsesOtherSrc().
+**
+** Determine if an expression references any table other than one of the
+** tables in pWalker->u.pSrcList and abort if it does.
+*/
+static int exprUsesOtherSrc(Walker *pWalker, Expr *pExpr){
+ if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
+ int i;
+ SrcList *pSrc = pWalker->u.pSrcList;
+ for(i=0; i<pSrc->nSrc; i++){
+ if( pExpr->iTable==pSrc->a[i].iCursor ) return WRC_Continue;
+ }
+ return WRC_Abort;
+ }else{
+ return WRC_Continue;
+ }
+}
+
+/*
+** Determine if any of the arguments to the pExpr Function references
+** any SrcList other than pSrcList. Return true if they do. Return
+** false if pExpr has no argument or has only constant arguments or
+** only references tables named in pSrcList.
+*/
+static int sqlite3FunctionUsesOtherSrc(Expr *pExpr, SrcList *pSrcList){
+ Walker w;
+ assert( pExpr->op==TK_AGG_FUNCTION );
+ memset(&w, 0, sizeof(w));
+ w.xExprCallback = exprUsesOtherSrc;
+ w.u.pSrcList = pSrcList;
+ if( sqlite3WalkExprList(&w, pExpr->x.pList)!=WRC_Continue ) return 1;
+ return 0;
+}
+
+/*
** Add a new element to the pAggInfo->aCol[] array. Return the index of
** the new element. Return a negative number if malloc fails.
*/
@@ -3930,9 +3965,7 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
return WRC_Prune;
}
case TK_AGG_FUNCTION: {
- /* The pNC->nDepth==0 test causes aggregate functions in subqueries
- ** to be ignored */
- if( pNC->nDepth==0 ){
+ if( !sqlite3FunctionUsesOtherSrc(pExpr, pSrcList) ){
/* Check to see if pExpr is a duplicate of another aggregate
** function that is already in the pAggInfo structure
*/
@@ -3976,15 +4009,7 @@ static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
return WRC_Continue;
}
static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
- NameContext *pNC = pWalker->u.pNC;
- if( pNC->nDepth==0 ){
- pNC->nDepth++;
- sqlite3WalkSelect(pWalker, pSelect);
- pNC->nDepth--;
- return WRC_Prune;
- }else{
- return WRC_Continue;
- }
+ return WRC_Continue;
}
/*
@@ -3997,6 +4022,7 @@ static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
*/
void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
Walker w;
+ memset(&w, 0, sizeof(w));
w.xExprCallback = analyzeAggregate;
w.xSelectCallback = analyzeAggregatesInSelect;
w.u.pNC = pNC;
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index ccffe09c2..cb178ff97 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -2011,7 +2011,6 @@ struct NameContext {
u8 allowAgg; /* Aggregate functions allowed here */
u8 hasAgg; /* True if aggregates are seen */
u8 isCheck; /* True if resolving names in a CHECK constraint */
- int nDepth; /* Depth of subquery recursion. 1 for no recursion */
AggInfo *pAggInfo; /* Information about aggregates at this level */
NameContext *pNext; /* Next outer name context. NULL for outermost */
};
@@ -2477,6 +2476,7 @@ struct Walker {
union { /* Extra data for callback */
NameContext *pNC; /* Naming context */
int i; /* Integer value */
+ SrcList *pSrcList; /* FROM clause */
} u;
};