1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-11 01:42:22 +03:00

Simplify the select.c logic that enforces the optimization fence around

a MATERIALIZED common table expression.  Do not allow the
reverse_unordered_selects setting to affect a fenced CTE that itself has
an ORDER BY clause.

FossilOrigin-Name: 165b9bda243777ae7fb6f51ee7ad58771c581b82ef9a70268eb9764e9e6eba6f
This commit is contained in:
drh
2023-08-02 12:39:56 +00:00
parent 5e82c06cf8
commit aa250db66a
4 changed files with 38 additions and 23 deletions

View File

@@ -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. */