2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the LIMIT ... OFFSET ... clause
16 # of SELECT statements.
18 # $Id: limit.test,v 1.11.2.1 2004/07/19 23:33:04 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Build some test data
25 set fd [open data1.txt w]
26 for {set i 1} {$i<=32} {incr i} {
27 for {set j 0} {pow(2,$j)<$i} {incr j} {}
28 puts $fd "[expr {32-$i}]\t[expr {10-$j}]"
32 CREATE TABLE t1(x int, y int);
33 COPY t1 FROM 'data1.txt'
38 execsql {SELECT count(*) FROM t1}
41 execsql {SELECT count(*) FROM t1 LIMIT 5}
44 execsql {SELECT x FROM t1 ORDER BY x LIMIT 5}
47 execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 2}
50 execsql {SELECT x FROM t1 ORDER BY x LIMIT 2, 5}
53 execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 5}
56 execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 30}
59 execsql {SELECT x FROM t1 ORDER BY x LIMIT 30, 50}
62 execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 50}
65 execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5}
66 } {0 5 0 5 0 5 1 5 0 5 2 5 0 5 3 5 0 5 4 5}
68 execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5 OFFSET 32}
69 } {1 5 0 5 1 5 1 5 1 5 2 5 1 5 3 5 1 5 4 5}
73 CREATE VIEW v1 AS SELECT * FROM t1 LIMIT 2;
74 SELECT count(*) FROM (SELECT * FROM v1);
79 CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 2;
80 SELECT count(*) FROM t2;
85 SELECT count(*) FROM t1 WHERE rowid IN (SELECT rowid FROM t1 LIMIT 2);
91 SELECT z FROM (SELECT y*10+x AS z FROM t1 ORDER BY x LIMIT 10)
100 INSERT INTO t3 SELECT x FROM t1 ORDER BY x LIMIT 10 OFFSET 1;
101 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
102 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
103 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
104 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
105 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
106 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
107 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
108 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
109 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
110 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
112 SELECT count(*) FROM t3;
117 SELECT x FROM t3 LIMIT 2 OFFSET 10000
122 CREATE TABLE t4 AS SELECT x,
123 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
124 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
125 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
126 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
127 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x AS y
129 SELECT x FROM t4 ORDER BY y DESC LIMIT 1 OFFSET 999;
135 CREATE TABLE t5(x,y);
136 INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
138 SELECT * FROM t5 ORDER BY x;
144 INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
145 ORDER BY x DESC LIMIT 2;
146 SELECT * FROM t5 ORDER BY x;
152 INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x ORDER BY x DESC LIMIT 31;
153 SELECT * FROM t5 ORDER BY x LIMIT 2;
158 SELECT * FROM t5 ORDER BY x DESC, y DESC LIMIT 2;
164 INSERT INTO t5 SELECT a.x*100+b.x, a.y*100+b.y FROM t1 AS a, t1 AS b
165 ORDER BY 1, 2 LIMIT 1000;
166 SELECT count(*), sum(x), sum(y), min(x), max(x), min(y), max(y) FROM t5;
168 } {1000 1528204 593161 0 3107 505 1005}
170 # There is some contraversy about whether LIMIT 0 should be the same as
171 # no limit at all or if LIMIT 0 should result in zero output rows.
177 INSERT INTO t6 VALUES(1);
178 INSERT INTO t6 VALUES(2);
179 INSERT INTO t6 SELECT a+2 FROM t6;
186 SELECT * FROM t6 LIMIT -1 OFFSET -1;
191 SELECT * FROM t6 LIMIT 2 OFFSET -123;
196 SELECT * FROM t6 LIMIT -432 OFFSET 2;
201 SELECT * FROM t6 LIMIT -1
206 SELECT * FROM t6 LIMIT -1 OFFSET 1
211 SELECT * FROM t6 LIMIT 0
216 SELECT * FROM t6 LIMIT 0 OFFSET 1
220 # Make sure LIMIT works well with compound SELECT statements.
223 do_test limit-7.1.1 {
225 SELECT x FROM t2 LIMIT 5 UNION ALL SELECT a FROM t6;
227 } {1 {LIMIT clause should come after UNION ALL not before}}
228 do_test limit-7.1.2 {
230 SELECT x FROM t2 LIMIT 5 UNION SELECT a FROM t6;
232 } {1 {LIMIT clause should come after UNION not before}}
233 do_test limit-7.1.3 {
235 SELECT x FROM t2 LIMIT 5 EXCEPT SELECT a FROM t6 LIMIT 3;
237 } {1 {LIMIT clause should come after EXCEPT not before}}
238 do_test limit-7.1.4 {
240 SELECT x FROM t2 LIMIT 0,5 INTERSECT SELECT a FROM t6;
242 } {1 {LIMIT clause should come after INTERSECT not before}}
245 SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 5;
250 SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 3 OFFSET 1;
255 SELECT x FROM t2 UNION ALL SELECT a FROM t6 ORDER BY 1 LIMIT 3 OFFSET 1;
260 SELECT x FROM t2 UNION SELECT x+2 FROM t2 LIMIT 2 OFFSET 1;
265 SELECT x FROM t2 UNION SELECT x+2 FROM t2 ORDER BY 1 DESC LIMIT 2 OFFSET 1;
270 SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 LIMIT 2;
275 SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 ORDER BY 1 DESC LIMIT 2;
280 SELECT a+26 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
285 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
290 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1 OFFSET 1;
295 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2
296 ORDER BY 1 DESC LIMIT 1 OFFSET 1;
300 # Tests for limit in conjunction with distinct. The distinct should
301 # occur before both the limit and the offset. Ticket #749.
305 SELECT DISTINCT round(x/100) FROM t3 LIMIT 5;
310 SELECT DISTINCT round(x/100) FROM t3 LIMIT 5 OFFSET 5;
315 SELECT DISTINCT round(x/100) FROM t3 LIMIT 5 OFFSET 25;