Fix long-standing bugs with the handling of LIMIT clausing in

compound SELECT statements with FROM clause subqueries.
Ticket [38cb5df375078d3f9].

FossilOrigin-Name: b0450120eabfe5455cea1ed04cec587a5540b6b7
This commit is contained in:
drh 2010-10-06 18:55:37 +00:00
parent 14d14607a5
commit 5e87be8705
4 changed files with 344 additions and 18 deletions

View File

@ -1,5 +1,8 @@
C Fix\sminor\stest\ssuite\sproblems\scausing\serrors\son\sOSX.
D 2010-10-06T16:42:53
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
C Fix\slong-standing\sbugs\swith\sthe\shandling\sof\sLIMIT\sclausing\sin\ncompound\sSELECT\sstatements\swith\sFROM\sclause\ssubqueries.\nTicket\s[38cb5df375078d3f9].
D 2010-10-06T18:55:37
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in c599a15d268b1db2aeadea19df2adc3bf2eb6bee
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -173,7 +176,7 @@ F src/printf.c 8ae5082dd38a1b5456030c3755ec3a392cd51506
F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
F src/resolve.c 1c0f32b64f8e3f555fe1f732f9d6f501a7f05706
F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697
F src/select.c cf3013b1953348c7c6ff9636342e87b9fda68466
F src/select.c 6a5c72fb0e8dc7f6133f5a9d7a747130ef0a00ea
F src/shell.c 8517fc1f9c59ae4007e6cc8b9af91ab231ea2056
F src/sqlite.h.in a5b50e1bc4d4d9a3cc95386747220a839d0c40f7
F src/sqlite3ext.h 69dfb8116af51b84a029cddb3b35062354270c89
@ -661,6 +664,7 @@ F test/tkt-26ff0c2d1e.test 888324e751512972c6e0d1a09df740d8f5aaf660
F test/tkt-2ea2425d34.test 1cf13e6f75d149b3209a0cb32927a82d3d79fb28
F test/tkt-31338dca7e.test 5741cd48de500347a437ba1be58c8335e83c5a5e
F test/tkt-313723c356.test c47f8a9330523e6f35698bf4489bcb29609b53ac
F test/tkt-38cb5df375.test 9e9b19857dba0896a8efdaf334d405ba423492f2
F test/tkt-3998683a16.test 6d1d04d551ed1704eb3396ca87bb9ccc8c5c1eb7
F test/tkt-3fe897352e.test 10de1a67bd5c66b238a4c96abe55531b37bb4f00
F test/tkt-4a03edc4c8.test 2865e4edbc075b954daa82f8da7cc973033ec76e
@ -872,7 +876,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
P f29da7bec68fa2cdd97c75b30f7ab25c96f8d49a
R f6ee88c33c88377138fd0e936582123a
U dan
Z 0a6a348b6e59062f1f3c2d42f48437e9
P 759c954fee215a64f21fe13491515eebebbc6d01
R 1213c6239354252d04636f6e595a524d
U drh
Z ad83a95e895864b36d1472aba689aee3
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFMrMYsoxKgR168RlERAphrAJsHgOTi3yKaSdMCDuDixyFjVCs2qACffar6
94VTw5iNsyxG/DDO3oJrUTk=
=3769
-----END PGP SIGNATURE-----

View File

@ -1 +1 @@
759c954fee215a64f21fe13491515eebebbc6d01
b0450120eabfe5455cea1ed04cec587a5540b6b7

View File

@ -442,7 +442,6 @@ static void pushOntoSorter(
sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
sqlite3VdbeJumpHere(v, addr2);
pSelect->iLimit = 0;
}
}
@ -721,11 +720,11 @@ static void selectInnerLoop(
#endif
}
/* Jump to the end of the loop if the LIMIT is reached.
/* Jump to the end of the loop if the LIMIT is reached. Except, if
** there is a sorter, in which case the sorter has already limited
** the output for us.
*/
if( p->iLimit ){
assert( pOrderBy==0 ); /* If there is an ORDER BY, the call to
** pushOntoSorter() would have cleared p->iLimit */
if( pOrderBy==0 && p->iLimit ){
sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
}
}
@ -860,10 +859,6 @@ static void generateSortTail(
sqlite3ReleaseTempReg(pParse, regRow);
sqlite3ReleaseTempReg(pParse, regRowid);
/* LIMIT has been implemented by the pushOntoSorter() routine.
*/
assert( p->iLimit==0 );
/* The bottom of the loop
*/
sqlite3VdbeResolveLabel(v, addrContinue);
@ -2181,7 +2176,6 @@ static int multiSelectOrderBy(
/* Separate the left and the right query from one another
*/
p->pPrior = 0;
pPrior->pRightmost = 0;
sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
if( pPrior->pPrior==0 ){
sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");

321
test/tkt-38cb5df375.test Normal file
View File

@ -0,0 +1,321 @@
# 2010 October 6
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. Specifically,
# it tests that ticket [38cb5df375078d3f9711482d2a1615d09f6b3f33] has
# been resolved.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
do_test tkt-38cb5df375.0 {
execsql {
CREATE TABLE t1(a);
INSERT INTO t1 VALUES(1);
INSERT INTO t1 VALUES(2);
INSERT INTO t1 SELECT a+2 FROM t1;
INSERT INTO t1 SELECT a+4 FROM t1;
}
} {}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.1.$ii {
execsql {
SELECT * FROM (SELECT * FROM t1 ORDER BY a)
UNION ALL SELECT 9 FROM (SELECT a FROM t1)
LIMIT $::ii;
}
} [lrange {1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.2.$ii {
execsql {
SELECT 9 FROM (SELECT * FROM t1)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a)
LIMIT $::ii;
}
} [lrange {9 9 9 9 9 9 9 9 1 2 3 4 5 6 7 8} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.3.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a)
LIMIT $::ii;
}
} [lrange {1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.4.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1)
UNION ALL SELECT 9 FROM (SELECT a FROM t1)
LIMIT $::ii;
}
} [lrange {0 0 0 0 0 0 0 0 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4} {
do_test tkt-38cb5df375.5.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1)
UNION SELECT 9 FROM (SELECT a FROM t1)
LIMIT $::ii;
}
} [lrange {0 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11} {
do_test tkt-38cb5df375.11.$ii {
execsql {
SELECT * FROM (SELECT * FROM t1 ORDER BY a LIMIT 3)
UNION ALL SELECT 9 FROM (SELECT a FROM t1)
LIMIT $::ii;
}
} [lrange {1 2 3 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11} {
do_test tkt-38cb5df375.12.$ii {
execsql {
SELECT 9 FROM (SELECT * FROM t1)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 3)
LIMIT $::ii;
}
} [lrange {9 9 9 9 9 9 9 9 1 2 3} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6} {
do_test tkt-38cb5df375.13.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 3)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 3)
LIMIT $::ii;
}
} [lrange {1 2 3 1 2 3} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6} {
do_test tkt-38cb5df375.14.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1 LIMIT 3)
UNION ALL SELECT 9 FROM (SELECT a FROM t1 LIMIT 3)
LIMIT $::ii;
}
} [lrange {0 0 0 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.21.$ii {
execsql {
SELECT * FROM (SELECT * FROM t1 ORDER BY a)
UNION ALL SELECT 9 FROM (SELECT a FROM t1)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.22.$ii {
execsql {
SELECT 9 FROM (SELECT * FROM t1)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.23.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a)
ORDER BY 1 DESC
LIMIT $::ii;
}
} [lrange {8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} {
do_test tkt-38cb5df375.24.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1)
UNION ALL SELECT 9 FROM (SELECT a FROM t1)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {0 0 0 0 0 0 0 0 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11} {
do_test tkt-38cb5df375.31.$ii {
execsql {
SELECT * FROM (SELECT * FROM t1 ORDER BY a LIMIT 3)
UNION ALL SELECT 9 FROM (SELECT a FROM t1)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9 10 11} {
do_test tkt-38cb5df375.32.$ii {
execsql {
SELECT 9 FROM (SELECT * FROM t1)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 3)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 9 9 9 9 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.33.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 4)
UNION ALL SELECT 90+a FROM (SELECT a FROM t1 ORDER BY a LIMIT 3)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 4 91 92 93} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.34.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 2)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 5)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 1 2 2 3 4 5} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.35.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 5)
UNION ALL SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 1 2 2 3 4 5} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.35b.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 5)
UNION ALL SELECT a+10 FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 4 5 11 12} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.35c.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 5)
UNION SELECT a+10 FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 4 5 11 12} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.35d.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 5)
INTERSECT SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.35e.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 5)
EXCEPT SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {3 4 5} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.36.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1 LIMIT 3)
UNION ALL SELECT 9 FROM (SELECT a FROM t1 LIMIT 4)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {0 0 0 9 9 9 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.37.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1 LIMIT 3)
UNION SELECT 9 FROM (SELECT a FROM t1 LIMIT 4)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {0 9} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
do_test tkt-38cb5df375.38.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1 LIMIT 3)
EXCEPT SELECT 9 FROM (SELECT a FROM t1 LIMIT 4)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {0} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9} {
do_test tkt-38cb5df375.41.$ii {
execsql {
SELECT 0 FROM (SELECT * FROM t1 LIMIT 3)
UNION ALL SELECT 9 FROM (SELECT a FROM t1 LIMIT 4)
UNION ALL SELECT 88 FROM (SELECT a FROM t1 LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {0 0 0 9 9 9 9 88 88} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9} {
do_test tkt-38cb5df375.42.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 3)
UNION ALL SELECT a+10 FROM (SELECT a FROM t1 ORDER BY a LIMIT 4)
UNION ALL SELECT a+20 FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 11 12 13 14 21 22} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7 8 9} {
do_test tkt-38cb5df375.43.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a LIMIT 3)
UNION SELECT a+10 FROM (SELECT a FROM t1 ORDER BY a LIMIT 4)
UNION SELECT a+20 FROM (SELECT a FROM t1 ORDER BY a LIMIT 2)
ORDER BY 1
LIMIT $::ii;
}
} [lrange {1 2 3 11 12 13 14 21 22} 0 [expr {$ii-1}]]
}
foreach ii {1 2 3 4 5 6 7} {
set jj [expr {7-$ii}]
do_test tkt-38cb5df375.51.$ii {
execsql {
SELECT a FROM (SELECT * FROM t1 ORDER BY a)
EXCEPT SELECT a FROM (SELECT a FROM t1 ORDER BY a LIMIT $::ii)
ORDER BY a DESC
LIMIT $::jj;
}
} [lrange {8 7 6 5 4 3 2 1} 0 [expr {$jj-1}]]
}
finish_test