diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/select.c | 4 | ||||
-rw-r--r-- | src/sqliteInt.h | 2 | ||||
-rw-r--r-- | src/where.c | 7 |
3 files changed, 6 insertions, 7 deletions
diff --git a/src/select.c b/src/select.c index c6b36b683..7c84bd7d6 100644 --- a/src/select.c +++ b/src/select.c @@ -6856,7 +6856,7 @@ int sqlite3Select( pExpr = sqlite3ExprDup(db, pExpr, 0); pDistinct = sqlite3ExprListDup(db, pGroupBy, 0); pDistinct = sqlite3ExprListAppend(pParse, pDistinct, pExpr); - distFlag = pDistinct ? WHERE_WANT_DISTINCT : 0; + distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0; } /* If there is a GROUP BY clause we might need a sorting index to @@ -7169,7 +7169,7 @@ int sqlite3Select( } }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){ pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList; - distFlag = pDistinct ? WHERE_WANT_DISTINCT : 0; + distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0; } /* This case runs if the aggregate has no GROUP BY clause. The diff --git a/src/sqliteInt.h b/src/sqliteInt.h index fcc50e157..783950fb0 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -3019,7 +3019,7 @@ struct SrcList { #define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */ #define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */ #define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */ - /* 0x0400 not currently used */ +#define WHERE_AGG_DISTINCT 0x0400 /* Query is "SELECT agg(DISTINCT ...)" */ #define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */ /* 0x1000 not currently used */ /* 0x2000 not currently used */ diff --git a/src/where.c b/src/where.c index ff6220b83..22ad55846 100644 --- a/src/where.c +++ b/src/where.c @@ -5025,9 +5025,7 @@ WhereInfo *sqlite3WhereBegin( /* Attempt to omit tables from the join that do not affect the result. ** For a table to not affect the result, the following must be true: ** - ** 1) The query must not be an aggregate. Or it must be an aggregate - ** that contains only one aggregate function with the DISTINCT - ** qualifier. e.g. "SELECT count(DISTINCT ...) FROM". + ** 1) The query must not be an aggregate. ** 2) The table must be the RHS of a LEFT JOIN. ** 3) Either the query must be DISTINCT, or else the ON or USING clause ** must contain a constraint that limits the scan of the table to @@ -5055,7 +5053,8 @@ WhereInfo *sqlite3WhereBegin( */ notReady = ~(Bitmask)0; if( pWInfo->nLevel>=2 - && pResultSet!=0 /* guarantees condition (1) above */ + && pResultSet!=0 /* these two combine to guarantee */ + && 0==(wctrlFlags & WHERE_AGG_DISTINCT) /* condition (1) above */ && OptimizationEnabled(db, SQLITE_OmitNoopJoin) ){ int i; |