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

@ -1,5 +1,5 @@
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
C Simplify\sthe\sselect.c\slogic\sthat\senforces\sthe\soptimization\sfence\saround\na\sMATERIALIZED\scommon\stable\sexpression.\s\sDo\snot\sallow\sthe\nreverse_unordered_selects\ssetting\sto\saffect\sa\sfenced\sCTE\sthat\sitself\shas\nan\sORDER\sBY\sclause.
D 2023-08-02T12:39:56.675
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 10ff496138ffa5bf5c3d9c7808b7558cb9b398d12062c464037167748075a13f
F src/select.c 8a8b7117bed5b7ce8122b611a9a7ff9504c523de3793d3a26661bf8ad81577ef
F src/shell.c.in 694aaf751f00610381533d4a31c83d142cfc83ef91ef65e2aa6912ace7c39b40
F src/sqlite.h.in 7b07a33d2af82ee974aa91e6294abce0282b2f4c5934b291d2fff961810dd867
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@ -728,7 +728,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
F src/wal.c dd843f619ac60d5dadab7109cf402432ba74dde0c301505fd1c202add07659e3
F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
F src/walker.c 7c7ea0115345851c3da4e04e2e239a29983b61fb5b038b94eede6aba462640e2
F src/where.c dbbd8588329e211fc797ee3d48cde21ad7a3ba0f9b135db9f10e5ba44690cf43
F src/where.c d4240edf4c78b94e705f774509f0552ebcf08a1d2ee169fe14d66eccb4dc910a
F src/whereInt.h c7d19902863beadec1d04e66aca39c0bcd60b74f05f0eaa7422c7005dfc5d51a
F src/wherecode.c 5d77db30a2a3dd532492ae882de114edba2fae672622056b1c7fd61f5917a8f1
F src/whereexpr.c dc5096eca5ed503999be3bdee8a90c51361289a678d396a220912e9cb73b3c00
@ -2049,8 +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 aa769ee7477171f26a091cadff98aa4f48da7752ade20b405a95c4525456f62e
R 5d3359f6c6d5e2c6f797651e2c52272a
P b7ef9796f548ce43e06673f86bbec68157aae5e4ee8451e1d87ee5f70af1bb27
R b2e3c3d0a4d53532b578742c3846e988
U drh
Z d3d190ae3372df83f9be614d98609288
Z 94c05c39e8f919e5e81448014dcce915
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
b7ef9796f548ce43e06673f86bbec68157aae5e4ee8451e1d87ee5f70af1bb27
165b9bda243777ae7fb6f51ee7ad58771c581b82ef9a70268eb9764e9e6eba6f

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

View File

@ -6122,8 +6122,27 @@ WhereInfo *sqlite3WhereBegin(
if( db->mallocFailed ) goto whereBeginError;
}
}
assert( pWInfo->pTabList!=0 );
if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
pWInfo->revMask = ALLBITS;
for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
/* The PRAGMA reverse_unordered_selects=ON setting (also accessible
** using SQLITE_DBCONFIG_REVERSE_SCANORDER) means to reverse the scan
** order for any table that is part of a query that does not have an
** ORDER BY clause.
**
** Except, do not reverse the output from MATERIALIZED common table
** expression that has an internal ORDER BY clause, because a
** MATERIALIZED common table expression is an optimization fence.
*/
SrcItem *pItem = &pWInfo->pTabList->a[ii];
if( !pItem->fg.isCte
|| pItem->u2.pCteUse->eM10d!=M10d_Yes
|| NEVER(pItem->pSelect==0)
|| pItem->pSelect->pOrderBy==0
){
pWInfo->revMask |= MASKBIT(ii);
}
}
}
if( pParse->nErr ){
goto whereBeginError;