3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the LIMIT in combination with ORDER BY
13 # and in particular, the optimizations in the inner loop that cause an
14 # early exit of the inner loop when the LIMIT is reached and the inner
15 # loop is emitting rows in ORDER BY order.
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 do_execsql_test limit2-100 {
23 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
24 INSERT INTO t1(a,b) SELECT 1, (x*17)%1000 + 1000 FROM c;
25 INSERT INTO t1(a,b) VALUES(2,2),(3,1006),(4,4),(5,9999);
26 CREATE INDEX t1ab ON t1(a,b);
28 set sqlite_search_count 0
29 do_execsql_test limit2-100.1 {
30 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b LIMIT 5;
31 } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |}
32 set fast_count $sqlite_search_count
33 set sqlite_search_count 0
34 do_execsql_test limit2-100.2 {
35 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b LIMIT 5;
36 } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |}
37 do_test limit2-100.3 {
38 set slow_count $sqlite_search_count
39 expr {$fast_count < 0.02*$slow_count}
42 do_execsql_test limit2-110 {
44 INSERT INTO t2(x,y) VALUES('a',1),('a',2),('a',3),('a',4);
45 INSERT INTO t2(x,y) VALUES('b',1),('c',2),('d',3),('e',4);
46 CREATE INDEX t2xy ON t2(x,y);
48 set sqlite_search_count 0
49 do_execsql_test limit2-110.1 {
50 SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY t1.b LIMIT 5;
51 } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |}
52 set fast_count $sqlite_search_count
53 set sqlite_search_count 0
54 do_execsql_test limit2-110.2 {
55 SELECT a, b, '|' FROM t2, t1 WHERE t2.x='a' AND t1.a=t2.y ORDER BY +t1.b LIMIT 5;
56 } {2 2 | 4 4 | 1 1000 | 1 1001 | 1 1002 |}
57 set slow_count $sqlite_search_count
58 do_test limit2-110.3 {
59 expr {$fast_count < 0.02*$slow_count}
62 do_execsql_test limit2-120 {
64 CREATE INDEX t1ab ON t1(a,b DESC);
66 set sqlite_search_count 0
67 do_execsql_test limit2-120.1 {
68 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY b DESC LIMIT 5;
69 } {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |}
70 set fast_count $sqlite_search_count
71 set sqlite_search_count 0
72 do_execsql_test limit2-120.2 {
73 SELECT a, b, '|' FROM t1 WHERE a IN (2,4,5,3,1) ORDER BY +b DESC LIMIT 5;
74 } {5 9999 | 1 1999 | 1 1998 | 1 1997 | 1 1996 |}
75 do_test limit2-120.3 {
76 set slow_count $sqlite_search_count
77 expr {$fast_count < 0.02*$slow_count}