1
0
mirror of https://github.com/sqlite/sqlite.git synced 2026-10-23 21:49:02 +03:00

Another fix to the order-by-subquery optimization

of check-in [7a0cdc7edb704a88]. Trouble reported by
[bugs:/info/2026-10-11T17:00:56Z|Bug 2026-10-11T17:00:56Z].

FossilOrigin-Name: 1b574df8c7df4f737ca7b7e42203a8090836a05bc808cafc8f8f70aefbe8627b
This commit is contained in:
drh
2026-10-11 18:38:57 +00:00
parent 513c19fa38
commit 50575da52c
4 changed files with 28 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
C Fix\sa\sproblem\sin\sthe\squery\sflattener\sassociated\swith\sRIGHT\sJOIN,\sas\nreported\sby\s[bugs:/info/2026-10-11T16:49:00Z|Bug\s2026-10-11T16:49:00Z].\nThis\sproblem\ssurfaced\safter\sthe\sfix\sto\n[forum:/info/2025-08-21T14:14:03Z|Forum\spost\s2025-08-21T14:14:03Z]\nat\scheck-in\s[e0beda7ffe92abf6].\s\sBefore\sthat,\sthe\squery\swould\snot\sparse.
D 2026-10-11T17:44:08.449
C Another\sfix\sto\sthe\sorder-by-subquery\soptimization\nof\scheck-in\s[7a0cdc7edb704a88].\sTrouble\sreported\sby\n[bugs:/info/2026-10-11T17:00:56Z|Bug\s2026-10-11T17:00:56Z].
D 2026-10-11T18:38:57.052
F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -883,7 +883,7 @@ F src/vxworks.h 9d18819c5235b49c2340a8a4d48195ec5d5afb637b152406de95a9436beeaeab
F src/wal.c 41438914e455bbc42ccff8545eab4c711f91233e480337e44a3d5fdf121fce74
F src/wal.h c6251cfc1950643a976625ca452e46dc9b466b4f5dd2e85f244f29f79bf68ace
F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014
F src/where.c 7b5b408a0d66ff5b7a071be8352c0920e6dcd9e7846742498460cbf0e2de3f78
F src/where.c eb55e7f12edfbbf8d72e458ec3e2acb6242d50fe936f38167cc50bf60fe80b24
F src/whereInt.h f039ad303bc2913de673e431821ef8db3b619d249e6e5548d90072a684e72285
F src/wherecode.c 1a6d2999159b28d8c0b968458b90ec183b3813cb332c11515ab60a73db5c07fd
F src/whereexpr.c 76f72a95c5cf0e8a8edb679ca4fb7afa17ba45ff4effe73c414266510109e701
@@ -1554,7 +1554,7 @@ F test/openv2.test 0d3040974bf402e19b7df4b783e447289d7ab394
F test/optfuzz-db01.c 9f2fa80b8f84ebbf1f2e8b13421a4e0477fe300f6686fbd76cac1d2db66e0fdc
F test/optfuzz-db01.txt 21f6bdeadc701cf11528276e2a55c70bfcb846ba42df327f979bd9e7b6ce7041
F test/optfuzz.c 690430a0bf0ad047d5a168bf52b05b2ee97aedaad8c14337e9eb5050faa64994
F test/orderby1.test 64d6aac62e68c289d378a18cea9b9941d812b6a26f141a18c9fbde79bd7f28db
F test/orderby1.test fe68221ce63f8f6f5807218ba7b2ede855a4010b9b3bf83c11a76ae9233e99f5
F test/orderby2.test bc11009f7cd99d96b1b11e57b199b00633eb5b04
F test/orderby3.test 64465d29ab6dabb38731ff24ead2aa0bb262bb06a7b6aa17c6f874a5f24fee94
F test/orderby4.test daff719d0eead9ae92863a0b5a2e367bfdc7efcc9dfe16d83d667f84df597eff
@@ -2283,8 +2283,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee
F tool/warnings.sh a554d13f6e5cf3760f041b87939e3d616ec6961859c3245e8ef701d1eafc2ca2
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c
P 6ebd1e69486cabc6be3316426f67341d75073e8b392de5d6e5307662dd0000dd
R 3319ccaf0c0774a4e4e39c418ec93b36
P e6e27e37eb8c3159d67529959694971f35a589da75648e207c44e317e6d160be
R d95406870443382222b8acbab92f9f0f
U drh
Z e86168cc2acce42c941a0f03a4eff586
Z 645d94f02c859d7750556ce47d6ed593
# Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
e6e27e37eb8c3159d67529959694971f35a589da75648e207c44e317e6d160be
1b574df8c7df4f737ca7b7e42203a8090836a05bc808cafc8f8f70aefbe8627b

View File

@@ -5079,13 +5079,13 @@ static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
** direction because there is no way to run a co-routine backwards.
*/
static SQLITE_NOINLINE int wherePathMatchSubqueryOB(
WhereInfo *pWInfo, /* The WHERE clause */
WhereLoop *pLoop, /* The nested loop term that is a subquery */
int iLoop, /* Which level of the nested loop. 0==outermost */
int iCur, /* Cursor used by the this loop */
ExprList *pOrderBy, /* The ORDER BY clause on the whole query */
Bitmask *pRevMask, /* When loops need to go in reverse order */
Bitmask *pOBSat /* Which terms of pOrderBy are satisfied so far */
Bitmask *pOBSat, /* Which terms of pOrderBy are satisfied so far */
u16 wctrlFlags /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
){
int iOB; /* Index into pOrderBy->a[] */
int jSub; /* Index into pSubOB->a[] */
@@ -5103,7 +5103,7 @@ static SQLITE_NOINLINE int wherePathMatchSubqueryOB(
if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) break;
if( pOBExpr->iTable!=iCur ) break;
if( pOBExpr->iColumn!=pSubOB->a[jSub].u.x.iOrderByCol-1 ) break;
if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
if( (wctrlFlags & WHERE_GROUPBY)==0 ){
u8 sfOB = pOrderBy->a[iOB].fg.sortFlags; /* sortFlags for iOB */
u8 sfSub = pSubOB->a[jSub].fg.sortFlags; /* sortFlags for jSub */
if( (sfSub & KEYINFO_ORDER_BIGNULL) != (sfOB & KEYINFO_ORDER_BIGNULL) ){
@@ -5285,8 +5285,8 @@ static i8 wherePathSatisfiesOrderBy(
if( pLoop->wsFlags & WHERE_IPK ){
if( pLoop->u.btree.pOrderBy
&& OptimizationEnabled(db, SQLITE_OrderBySubq)
&& wherePathMatchSubqueryOB(pWInfo,pLoop,iLoop,iCur,
pOrderBy,pRevMask, &obSat)
&& wherePathMatchSubqueryOB(pLoop,iLoop,iCur,
pOrderBy,pRevMask, &obSat, wctrlFlags)
){
nColumn = 0;
isOrderDistinct = 0;

View File

@@ -565,5 +565,20 @@ do_catchsql_test 11.0 {
VALUES(2) EXCEPT SELECT '' ORDER BY abc
} {1 {1st ORDER BY term does not match any column in the result set}}
# Bug 2026-10-11T17:00:56Z
do_execsql_test 12.0 {
DROP TABLE t1;
CREATE TABLE t1(a INT);
INSERT INTO t1(a) VALUES(1),(2);
SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
GROUP BY a
ORDER BY a DESC;
} {2 1}
do_execsql_test 12.1 {
SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
GROUP BY a
ORDER BY a DESC
LIMIT 1;
} {2}
finish_test