mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-11 01:42:22 +03:00
Fix the handling of sub-queries with LIMIT clauses by the optimization
activated by compile-time symbol SQLITE_COUNTOFVIEW_OPTIMIZATION. FossilOrigin-Name: 21235d9a41567897418aa12f7bd6dd8d6ee363147527e1d8fbca14fc83e0f2c9
This commit is contained in:
22
src/select.c
22
src/select.c
@@ -5495,6 +5495,7 @@ static struct SrcList_item *isSelfJoinView(
|
||||
** The transformation only works if all of the following are true:
|
||||
**
|
||||
** * The subquery is a UNION ALL of two or more terms
|
||||
** * The subquery does not have a LIMIT clause
|
||||
** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
|
||||
** * The outer query is a simple count(*)
|
||||
**
|
||||
@@ -5518,6 +5519,7 @@ static int countOfViewOptimization(Parse *pParse, Select *p){
|
||||
do{
|
||||
if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
|
||||
if( pSub->pWhere ) return 0; /* No WHERE clause */
|
||||
if( pSub->pLimit ) return 0; /* No LIMIT clause */
|
||||
if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
|
||||
pSub = pSub->pPrior; /* Repeat over compound */
|
||||
}while( pSub );
|
||||
@@ -5779,6 +5781,16 @@ int sqlite3Select(
|
||||
SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
|
||||
}
|
||||
|
||||
#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
|
||||
if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
|
||||
&& countOfViewOptimization(pParse, p)
|
||||
){
|
||||
if( db->mallocFailed ) goto select_end;
|
||||
pEList = p->pEList;
|
||||
pTabList = p->pSrc;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* For each term in the FROM clause, do two things:
|
||||
** (1) Authorized unreferenced tables
|
||||
** (2) Generate code for all sub-queries
|
||||
@@ -5957,16 +5969,6 @@ int sqlite3Select(
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
|
||||
if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)
|
||||
&& countOfViewOptimization(pParse, p)
|
||||
){
|
||||
if( db->mallocFailed ) goto select_end;
|
||||
pEList = p->pEList;
|
||||
pTabList = p->pSrc;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
|
||||
** if the select-list is the same as the ORDER BY list, then this query
|
||||
** can be rewritten as a GROUP BY. In other words, this:
|
||||
|
||||
Reference in New Issue
Block a user