0
0
mirror of https://github.com/sqlite/sqlite.git synced 2024-11-22 03:40:55 +01:00

Fix a problem with the query optimizer for LIMIT/OFFSET queries when

underlying query is a UNION ALL and both arms of the UNION ALL are
subqueries with an ORDER BY clause.  This bug was reported at
[forum:/forumpost/6b5e9188f0657616|forum post 6b5e9188f0657616].  The
problem was introduced in 2015 (SQLite version 3.9.0) by check-in
[4b631364354068af].  See also ticket [b65cb2c8d91f6685].

FossilOrigin-Name: 6c806f64bbc9e98891bad0868575ee2ec5d0951ceb0c71d3ed417b45d5f27561
This commit is contained in:
drh 2022-08-04 17:15:00 +00:00
parent e24a6f58ae
commit bffd5c1ece
4 changed files with 54 additions and 10 deletions

View File

@ -1,5 +1,5 @@
C Fix\sa\stypo\son\sa\scomment\sused\sto\sgenerate\sbytecode\sdocumentation.
D 2022-08-04T14:02:35.852
C Fix\sa\sproblem\swith\sthe\squery\soptimizer\sfor\sLIMIT/OFFSET\squeries\swhen\nunderlying\squery\sis\sa\sUNION\sALL\sand\sboth\sarms\sof\sthe\sUNION\sALL\sare\nsubqueries\swith\san\sORDER\sBY\sclause.\s\sThis\sbug\swas\sreported\sat\n[forum:/forumpost/6b5e9188f0657616|forum\spost\s6b5e9188f0657616].\s\sThe\nproblem\swas\sintroduced\sin\s2015\s(SQLite\sversion\s3.9.0)\sby\scheck-in\n[4b631364354068af].\s\sSee\salso\sticket\s[b65cb2c8d91f6685].
D 2022-08-04T17:15:00.492
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -570,7 +570,7 @@ F src/printf.c e99ee9741e79ae3873458146f59644276657340385ade4e76a5f5d1c25793764
F src/random.c 097dc8b31b8fba5a9aca1697aeb9fd82078ec91be734c16bffda620ced7ab83c
F src/resolve.c efea4e5fbecfd6d0a9071b0be0d952620991673391b6ffaaf4c277b0bb674633
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
F src/select.c 76708e3a8cb80f011f31f1eb6fa5f433aef8f47c85c0a2709148d673a719a846
F src/select.c 4750fbe9d8ecb7236baf7a9bea4299bb87126e08c209645666a0ae8f0efbe0fc
F src/shell.c.in 29749b34bbd19d0004fdb6f61f62659096e1c0b4dfb1ad2314e7fafbe9dd8d37
F src/sqlite.h.in be265991edca9aea69986758b58ba81cbf5ae403fe0c4ea1d0c9df0cdc8f25ed
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@ -1286,7 +1286,7 @@ F test/null.test b7ff206a1c60fe01aa2abd33ef9ea83c93727d993ca8a613de86e925c9f2bc6
F test/nulls1.test 7a5e4346ee4285034100b4cd20e6784f16a9d6c927e44ecdf10034086bbee9c9
F test/numcast.test 5d126f7f581432e86a90d1e35cac625164aec4a1
F test/numindex1.test 20a5450d4b056e48cd5db30e659f13347a099823
F test/offset1.test f06b83657bcf26f9ce805e67450e189e282143b2
F test/offset1.test 72cca52482cbd5bc687cfa67aa2566c859081b5a353fd2f9da9bbd3914dea1ef
F test/openv2.test 0d3040974bf402e19b7df4b783e447289d7ab394
F test/optfuzz-db01.c 9f2fa80b8f84ebbf1f2e8b13421a4e0477fe300f6686fbd76cac1d2db66e0fdc
F test/optfuzz-db01.txt 21f6bdeadc701cf11528276e2a55c70bfcb846ba42df327f979bd9e7b6ce7041
@ -1981,8 +1981,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 597347d9c39f6ed068ac942a8c9853b1b745fce1eb5e9dd30f6c1cb312b36f53
R ce5a27ffc72e7ec12d095a7689107487
P 64348d0b665f61c3a89f51341f23ca87f939dd9e306dc58c2004e46ab04e696e
R ee3ba899316d027809db4ccd10585a40
U drh
Z 549658791a10dec284226014b5aa7e75
Z 55de9d5c0f343647378b885b0fe37df5
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
64348d0b665f61c3a89f51341f23ca87f939dd9e306dc58c2004e46ab04e696e
6c806f64bbc9e98891bad0868575ee2ec5d0951ceb0c71d3ed417b45d5f27561

View File

@ -1688,7 +1688,7 @@ static void generateSortTail(
if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
VdbeCoverage(v);
codeOffset(v, p->iOffset, addrContinue);
assert( p->iLimit==0 && p->iOffset==0 );
sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab);
bSeq = 0;
}else{
@ -1696,6 +1696,9 @@ static void generateSortTail(
codeOffset(v, p->iOffset, addrContinue);
iSortTab = iTab;
bSeq = 1;
if( p->iOffset>0 ){
sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
}
}
for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
#ifdef SQLITE_ENABLE_SORTER_REFERENCES

View File

@ -156,6 +156,47 @@ do_execsql_test offset1-1.4.9 {
LIMIT 9 OFFSET 1;
} {2 b 3 c 4 d 5 e 6 w 7 x 8 y 9 z}
# 2022-08-04
# https://sqlite.org/forum/forumpost/6b5e9188f0657616
#
do_execsql_test offset1-2.0 {
CREATE TABLE employees (
id integer primary key,
name text,
city text,
department text,
salary integer
);
INSERT INTO employees VALUES
(11,'Diane','London','hr',70),
(12,'Bob','London','hr',78),
(21,'Emma','London','it',84),
(22,'Grace','Berlin','it',90),
(23,'Henry','London','it',104),
(24,'Irene','Berlin','it',104),
(25,'Frank','Berlin','it',120),
(31,'Cindy','Berlin','sales',96),
(32,'Dave','London','sales',96),
(33,'Alice','Berlin','sales',100);
CREATE VIEW v AS
SELECT * FROM (
SELECT * FROM employees
WHERE salary < 100
ORDER BY salary desc)
UNION ALL
SELECT * FROM (
SELECT * FROM employees
WHERE salary >= 100
ORDER BY salary asc);
} {}
do_execsql_test offset1-2.1 {
SELECT * FROM v LIMIT 5 OFFSET 2;
} {
22 Grace Berlin it 90
21 Emma London it 84
12 Bob London hr 78
11 Diane London hr 70
33 Alice Berlin sales 100
}
finish_test