Stricter enforcement of the idea that a MATERIALIZED common table expression

is an optimization fence.

FossilOrigin-Name: 354425f8d97437bd156265a6914f98cce91b42abc9e773453ef4d817e308dc9a
This commit is contained in:
drh 2023-08-02 13:38:02 +00:00
commit b1dcebfea3
4 changed files with 48 additions and 13 deletions

View File

@ -1,5 +1,5 @@
C Avoid\sASAN\swarnings\swhen\sconverting\sover-sized\slong\sdouble\svalues\sinto\sdouble. C Stricter\senforcement\sof\sthe\sidea\sthat\sa\sMATERIALIZED\scommon\stable\sexpression\nis\san\soptimization\sfence.
D 2023-08-01T19:10:30.602 D 2023-08-02T13:38:02.621
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -643,7 +643,7 @@ F src/printf.c e3ba080e2f409f9bfcc8d34724e6fc160e9c718dc92d0548f6b71b8b6f860ce2
F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
F src/resolve.c 37953a5f36c60bea413c3c04efcd433b6177009f508ef2ace0494728912fe2e9 F src/resolve.c 37953a5f36c60bea413c3c04efcd433b6177009f508ef2ace0494728912fe2e9
F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
F src/select.c 3328b8c758016b400a4fea053ab1f5927929083a49d0c3921802ce99eea73cde F src/select.c abf48be202d35c4f450325b61992e98ac4aa81ed1e29709069432877d3b555d3
F src/shell.c.in 694aaf751f00610381533d4a31c83d142cfc83ef91ef65e2aa6912ace7c39b40 F src/shell.c.in 694aaf751f00610381533d4a31c83d142cfc83ef91ef65e2aa6912ace7c39b40
F src/sqlite.h.in 7b07a33d2af82ee974aa91e6294abce0282b2f4c5934b291d2fff961810dd867 F src/sqlite.h.in 7b07a33d2af82ee974aa91e6294abce0282b2f4c5934b291d2fff961810dd867
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@ -728,7 +728,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
F src/wal.c dd843f619ac60d5dadab7109cf402432ba74dde0c301505fd1c202add07659e3 F src/wal.c dd843f619ac60d5dadab7109cf402432ba74dde0c301505fd1c202add07659e3
F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
F src/walker.c 7c7ea0115345851c3da4e04e2e239a29983b61fb5b038b94eede6aba462640e2 F src/walker.c 7c7ea0115345851c3da4e04e2e239a29983b61fb5b038b94eede6aba462640e2
F src/where.c dbbd8588329e211fc797ee3d48cde21ad7a3ba0f9b135db9f10e5ba44690cf43 F src/where.c b8917792f1e0dbfa28fb29e6cd3d560060d69667be0ba4c491cbc772363264f5
F src/whereInt.h c7d19902863beadec1d04e66aca39c0bcd60b74f05f0eaa7422c7005dfc5d51a F src/whereInt.h c7d19902863beadec1d04e66aca39c0bcd60b74f05f0eaa7422c7005dfc5d51a
F src/wherecode.c 5d77db30a2a3dd532492ae882de114edba2fae672622056b1c7fd61f5917a8f1 F src/wherecode.c 5d77db30a2a3dd532492ae882de114edba2fae672622056b1c7fd61f5917a8f1
F src/whereexpr.c dc5096eca5ed503999be3bdee8a90c51361289a678d396a220912e9cb73b3c00 F src/whereexpr.c dc5096eca5ed503999be3bdee8a90c51361289a678d396a220912e9cb73b3c00
@ -2049,8 +2049,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P ef7c121049f77200ecc4da260a90776264af0e84122d0133b4986959aedc653a P e989a37ff9d5b52e0090d59be077ad2260c8df5d4c2c2d8088b1160de64dffd4 f068f105fb158634321bf6401f0774c81059932d213a18b627ae98bcffc10912
R c483adf60f57a6109b239d1712365b26 R 2044877763bac67c01e0c206bcedee44
T +closed f068f105fb158634321bf6401f0774c81059932d213a18b627ae98bcffc10912
U drh U drh
Z 2befc2bb54d1f8208d12f5236dacb8de Z eda869de5d4dc94a571480d5ab12d294
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
e989a37ff9d5b52e0090d59be077ad2260c8df5d4c2c2d8088b1160de64dffd4 354425f8d97437bd156265a6914f98cce91b42abc9e773453ef4d817e308dc9a

View File

@ -4292,7 +4292,8 @@ static int compoundHasDifferentAffinities(Select *p){
** (27b) the subquery is a compound query and the RIGHT JOIN occurs ** (27b) the subquery is a compound query and the RIGHT JOIN occurs
** in any arm of the compound query. (See also (17g).) ** 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. ** 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 ){ if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
return 0; /* Restriction (27a) */ 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 /* 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 ** use only the UNION ALL operator. And none of the simple select queries
@ -7285,6 +7286,14 @@ int sqlite3Select(
goto select_end; 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. /* Do not try to flatten an aggregate subquery.
** **
** Flattening an aggregate subquery is only possible if the outer query ** 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 ** (a) The outer query has a different ORDER BY clause
** (b) The subquery is part of a join ** (b) The subquery is part of a join
** See forum post 062d576715d277c8 ** See forum post 062d576715d277c8
**
** Also retain the ORDER BY if the OmitOrderBy optimization is disabled.
*/ */
if( pSub->pOrderBy!=0 if( pSub->pOrderBy!=0
&& (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */ && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */

View File

@ -5758,6 +5758,28 @@ static SQLITE_NOINLINE void whereAddIndexedExpr(
} }
} }
/*
** Set the reverse-scan order mask to one for all tables in the query
** with the exception of MATERIALIZED common table expressions that have
** their own internal ORDER BY clauses.
**
** This implements the PRAGMA reverse_unordered_selects=ON setting.
** (Also SQLITE_DBCONFIG_REVERSE_SCANORDER).
*/
static SQLITE_NOINLINE void whereReverseScanOrder(WhereInfo *pWInfo){
int ii;
for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
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);
}
}
}
/* /*
** Generate the beginning of the loop used for WHERE clause processing. ** Generate the beginning of the loop used for WHERE clause processing.
** The return value is a pointer to an opaque structure that contains ** The return value is a pointer to an opaque structure that contains
@ -6122,8 +6144,9 @@ WhereInfo *sqlite3WhereBegin(
if( db->mallocFailed ) goto whereBeginError; if( db->mallocFailed ) goto whereBeginError;
} }
} }
assert( pWInfo->pTabList!=0 );
if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
pWInfo->revMask = ALLBITS; whereReverseScanOrder(pWInfo);
} }
if( pParse->nErr ){ if( pParse->nErr ){
goto whereBeginError; goto whereBeginError;