aboutsummaryrefslogtreecommitdiff
path: root/src/select.c
diff options
context:
space:
mode:
authordrh <>2023-08-02 12:39:56 +0000
committerdrh <>2023-08-02 12:39:56 +0000
commitaa250db66aed4ed8f969d61f05bc374aa8c2c2e6 (patch)
treecf73d938b125cfed442ae210b7233a51efe7a284 /src/select.c
parent5e82c06cf89a5d3d57f8cbb206a7142c9c4c4a0c (diff)
downloadsqlite-aa250db66aed4ed8f969d61f05bc374aa8c2c2e6.tar.gz
sqlite-aa250db66aed4ed8f969d61f05bc374aa8c2c2e6.zip
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
Diffstat (limited to 'src/select.c')
-rw-r--r--src/select.c24
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. */