diff options
Diffstat (limited to 'src/select.c')
-rw-r--r-- | src/select.c | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/src/select.c b/src/select.c index 7a3ba4a6f..482d7283c 100644 --- a/src/select.c +++ b/src/select.c @@ -7285,6 +7285,14 @@ int sqlite3Select( goto select_end; } + /* Do not attempt the usual optimizations (flattening and ORDER BY + ** elimination) on a MATERIALIZED common table expression because + ** a MATERIALIZED common table expression is an optimization fence. + */ + if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){ + continue; + } + /* Do not try to flatten an aggregate subquery. ** ** Flattening an aggregate subquery is only possible if the outer query @@ -7315,21 +7323,14 @@ int sqlite3Select( ** (b) The subquery is part of a join ** See forum post 062d576715d277c8 ** - ** The above cases might get an incorrect result if ORDER BY is omitted. - ** The following constraints are not required for correct answers, but are - ** included in order to give developers more control over when a sort - ** occurs: - ** - ** (6) The subquery is really a MATERIALIZED CTE - ** (7) The OmitOrderBy optimization is disabled + ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled. */ if( pSub->pOrderBy!=0 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */ && pSub->pLimit==0 /* Condition (1) */ && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */ && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */ - && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (6) */ - && OptimizationEnabled(db, SQLITE_OmitOrderBy) /* (7) */ + && OptimizationEnabled(db, SQLITE_OmitOrderBy) ){ TREETRACE(0x800,pParse,p, ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1)); @@ -7366,11 +7367,6 @@ int sqlite3Select( continue; } - /* Do not flatten a MATERIALIZED CTE */ - if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){ - continue; - } - if( flattenSubquery(pParse, p, i, isAgg) ){ if( pParse->nErr ) goto select_end; /* This subquery can be absorbed into its parent. */ |