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

Never flatten a CTE that is labeled MATERIALIZED. This is really something

of a bug fix because the documentation says that a MATERIALIZED CTE is an
optimization barrier.

FossilOrigin-Name: b7ef9796f548ce43e06673f86bbec68157aae5e4ee8451e1d87ee5f70af1bb27
This commit is contained in:
drh
2023-08-02 11:06:27 +00:00
parent 4178849736
commit 5e82c06cf8
3 changed files with 20 additions and 11 deletions

View File

@@ -7314,7 +7314,14 @@ int sqlite3Select(
** (a) The outer query has a different ORDER BY clause
** (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
*/
if( pSub->pOrderBy!=0
&& (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
@@ -7322,7 +7329,7 @@ int sqlite3Select(
&& (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)
&& OptimizationEnabled(db, SQLITE_OmitOrderBy) /* (7) */
){
TREETRACE(0x800,pParse,p,
("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
@@ -7359,6 +7366,11 @@ 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. */