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

@ -1,5 +1,5 @@
C When\sa\squery\sis\sinside\san\sAS\sMATERIALIZED\sCTE,\sdo\snot\sattempt\sthe\nomit-ORDER-BY\soptimization.\s\sIf\sthe\sdeveloper\sspecifies\sMATERIALIZED,\nthat\smeans\she\swants\sthe\ssort\sto\sactually\shappen.
D 2023-08-02T00:09:15.460
C Never\sflatten\sa\sCTE\sthat\sis\slabeled\sMATERIALIZED.\s\sThis\sis\sreally\ssomething\nof\sa\sbug\sfix\sbecause\sthe\sdocumentation\ssays\sthat\sa\sMATERIALIZED\sCTE\sis\san\noptimization\sbarrier.
D 2023-08-02T11:06:27.030
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -643,7 +643,7 @@ F src/printf.c e3ba080e2f409f9bfcc8d34724e6fc160e9c718dc92d0548f6b71b8b6f860ce2
F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
F src/resolve.c 37953a5f36c60bea413c3c04efcd433b6177009f508ef2ace0494728912fe2e9
F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
F src/select.c 0ea15e379b2eb09d1a803499438b957c59af7264996903ec9b9a4aeb0402293b
F src/select.c 10ff496138ffa5bf5c3d9c7808b7558cb9b398d12062c464037167748075a13f
F src/shell.c.in 694aaf751f00610381533d4a31c83d142cfc83ef91ef65e2aa6912ace7c39b40
F src/sqlite.h.in 7b07a33d2af82ee974aa91e6294abce0282b2f4c5934b291d2fff961810dd867
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@ -2049,11 +2049,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P e989a37ff9d5b52e0090d59be077ad2260c8df5d4c2c2d8088b1160de64dffd4
R 85471d4376eb004f17191da107252ebb
T *branch * materialize-order-by
T *sym-materialize-order-by *
T -sym-trunk *
P aa769ee7477171f26a091cadff98aa4f48da7752ade20b405a95c4525456f62e
R 5d3359f6c6d5e2c6f797651e2c52272a
U drh
Z 34b88ebfe26392ded7967d5b47a75b8d
Z d3d190ae3372df83f9be614d98609288
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
aa769ee7477171f26a091cadff98aa4f48da7752ade20b405a95c4525456f62e
b7ef9796f548ce43e06673f86bbec68157aae5e4ee8451e1d87ee5f70af1bb27

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