xref: /sqlite-3.40.0/test/limit2.test (revision 73fa4737)
1# 2016-05-20
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
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.
16
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21do_execsql_test limit2-100 {
22  CREATE TABLE t1(a,b);
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);
27}
28set sqlite_search_count 0
29do_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 |}
32set fast_count $sqlite_search_count
33set sqlite_search_count 0
34do_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 |}
37do_test limit2-100.3 {
38  set slow_count $sqlite_search_count
39  expr {$fast_count < 0.02*$slow_count}
40} {1}
41
42do_execsql_test limit2-110 {
43  CREATE TABLE t2(x,y);
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);
47}
48set sqlite_search_count 0
49do_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 |}
52set fast_count $sqlite_search_count
53set sqlite_search_count 0
54do_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 |}
57set slow_count $sqlite_search_count
58do_test limit2-110.3 {
59  expr {$fast_count < 0.02*$slow_count}
60} {1}
61
62do_execsql_test limit2-120 {
63  DROP INDEX t1ab;
64  CREATE INDEX t1ab ON t1(a,b DESC);
65}
66set sqlite_search_count 0
67do_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 |}
70set fast_count $sqlite_search_count
71set sqlite_search_count 0
72do_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 |}
75do_test limit2-120.3 {
76  set slow_count $sqlite_search_count
77  expr {$fast_count < 0.02*$slow_count}
78} {1}
79
80
81
82finish_test
83