diff options
author | drh <> | 2023-08-02 13:38:02 +0000 |
---|---|---|
committer | drh <> | 2023-08-02 13:38:02 +0000 |
commit | b1dcebfea397d777fc7cd35fe809b0911547f5ef (patch) | |
tree | 0da7bf6672a2151cf7b060970c16c530444590be /src/select.c | |
parent | cbaef88980f48c963da8788faadd429b64d72dca (diff) | |
parent | 26cf4af5d79775af5ebfb19c46ea9a71655916ef (diff) | |
download | sqlite-b1dcebfea397d777fc7cd35fe809b0911547f5ef.tar.gz sqlite-b1dcebfea397d777fc7cd35fe809b0911547f5ef.zip |
Stricter enforcement of the idea that a MATERIALIZED common table expression
is an optimization fence.
FossilOrigin-Name: 354425f8d97437bd156265a6914f98cce91b42abc9e773453ef4d817e308dc9a
Diffstat (limited to 'src/select.c')
-rw-r--r-- | src/select.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/select.c b/src/select.c index 098b74584..41e42b680 100644 --- a/src/select.c +++ b/src/select.c @@ -4292,7 +4292,8 @@ static int compoundHasDifferentAffinities(Select *p){ ** (27b) the subquery is a compound query and the RIGHT JOIN occurs ** in any arm of the compound query. (See also (17g).) ** -** (28) The subquery is not a MATERIALIZED CTE. +** (28) The subquery is not a MATERIALIZED CTE. (This is handled +** in the caller before ever reaching this routine.) ** ** ** In this routine, the "p" parameter is a pointer to the outer query. @@ -4402,9 +4403,9 @@ static int flattenSubquery( if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){ return 0; /* Restriction (27a) */ } - if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){ - return 0; /* (28) */ - } + + /* Condition (28) is blocked by the caller */ + assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes ); /* Restriction (17): If the sub-query is a compound SELECT, then it must ** use only the UNION ALL operator. And none of the simple select queries @@ -7285,6 +7286,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 @@ -7314,6 +7323,8 @@ int sqlite3Select( ** (a) The outer query has a different ORDER BY clause ** (b) The subquery is part of a join ** See forum post 062d576715d277c8 + ** + ** Also retain the ORDER BY if the OmitOrderBy optimization is disabled. */ if( pSub->pOrderBy!=0 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */ |