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 ... OFFSET ... clause
13 # of SELECT statements.
15 # $Id: limit.test,v 1.32 2008/08/02 03:50:39 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Build some test data
23 CREATE TABLE t1(x int, y int);
26 for {set i 1} {$i<=32} {incr i} {
27 for {set j 0} {(1<<$j)<$i} {incr j} {}
28 execsql "INSERT INTO t1 VALUES([expr {32-$i}],[expr {10-$j}])"
35 execsql {SELECT count(*) FROM t1}
38 execsql {SELECT count(*) FROM t1 LIMIT 5}
41 execsql {SELECT x FROM t1 ORDER BY x LIMIT 5}
44 execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 2}
47 execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT 5 OFFSET -2}
50 execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT 2, -5}
51 } {2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
53 execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT -2, 5}
56 execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT -2, -5}
57 } {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
59 execsql {SELECT x FROM t1 ORDER BY x LIMIT 2, 5}
62 execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 5}
65 execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 30}
68 execsql {SELECT x FROM t1 ORDER BY x LIMIT 30, 50}
71 execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 50}
74 execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5}
75 } {0 5 0 5 0 5 1 5 0 5 2 5 0 5 3 5 0 5 4 5}
77 execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5 OFFSET 32}
78 } {1 5 0 5 1 5 1 5 1 5 2 5 1 5 3 5 1 5 4 5}
80 ifcapable {view && subquery} {
83 CREATE VIEW v1 AS SELECT * FROM t1 LIMIT 2;
84 SELECT count(*) FROM (SELECT * FROM v1);
90 CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 2;
91 SELECT count(*) FROM t2;
97 SELECT count(*) FROM t1 WHERE rowid IN (SELECT rowid FROM t1 LIMIT 2);
105 SELECT z FROM (SELECT y*10+x AS z FROM t1 ORDER BY x LIMIT 10)
116 INSERT INTO t3 SELECT x FROM t1 ORDER BY x LIMIT 10 OFFSET 1;
117 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
118 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
119 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
120 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
121 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
122 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
123 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
124 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
125 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
126 INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
128 SELECT count(*) FROM t3;
134 INSERT INTO t3 SELECT x FROM t1 ORDER BY x LIMIT 10 OFFSET 1;
136 for {set i 0} {$i<10} {incr i} {
137 set max_x_t3 [execsql {SELECT max(x) FROM t3}]
138 execsql "INSERT INTO t3 SELECT x+$max_x_t3 FROM t3;"
142 SELECT count(*) FROM t3;
148 SELECT x FROM t3 LIMIT 2 OFFSET 10000
153 CREATE TABLE t4 AS SELECT x,
154 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
155 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
156 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
157 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
158 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x AS y
160 SELECT x FROM t4 ORDER BY y DESC LIMIT 1 OFFSET 999;
166 CREATE TABLE t5(x,y);
167 INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
169 SELECT * FROM t5 ORDER BY x;
175 INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
176 ORDER BY x DESC LIMIT 2;
177 SELECT * FROM t5 ORDER BY x;
183 INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x ORDER BY x DESC LIMIT 31;
184 SELECT * FROM t5 ORDER BY x LIMIT 2;
189 SELECT * FROM t5 ORDER BY x DESC, y DESC LIMIT 2;
195 INSERT INTO t5 SELECT a.x*100+b.x, a.y*100+b.y FROM t1 AS a, t1 AS b
196 ORDER BY 1, 2 LIMIT 1000;
197 SELECT count(*), sum(x), sum(y), min(x), max(x), min(y), max(y) FROM t5;
199 } {1000 1528204 593161 0 3107 505 1005}
201 # There is some contraversy about whether LIMIT 0 should be the same as
202 # no limit at all or if LIMIT 0 should result in zero output rows.
208 INSERT INTO t6 VALUES(1);
209 INSERT INTO t6 VALUES(2);
210 INSERT INTO t6 SELECT a+2 FROM t6;
217 SELECT * FROM t6 LIMIT -1 OFFSET -1;
222 SELECT * FROM t6 LIMIT 2 OFFSET -123;
227 SELECT * FROM t6 LIMIT -432 OFFSET 2;
232 SELECT * FROM t6 LIMIT -1
237 SELECT * FROM t6 LIMIT -1 OFFSET 1
242 SELECT * FROM t6 LIMIT 0
247 SELECT * FROM t6 LIMIT 0 OFFSET 1
251 # Make sure LIMIT works well with compound SELECT statements.
255 do_test limit-7.1.1 {
257 SELECT x FROM t2 LIMIT 5 UNION ALL SELECT a FROM t6;
259 } {1 {LIMIT clause should come after UNION ALL not before}}
260 do_test limit-7.1.2 {
262 SELECT x FROM t2 LIMIT 5 UNION SELECT a FROM t6;
264 } {1 {LIMIT clause should come after UNION not before}}
265 do_test limit-7.1.3 {
267 SELECT x FROM t2 LIMIT 5 EXCEPT SELECT a FROM t6 LIMIT 3;
269 } {1 {LIMIT clause should come after EXCEPT not before}}
270 do_test limit-7.1.4 {
272 SELECT x FROM t2 LIMIT 0,5 INTERSECT SELECT a FROM t6;
274 } {1 {LIMIT clause should come after INTERSECT not before}}
277 SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 5;
282 SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 3 OFFSET 1;
287 SELECT x FROM t2 UNION ALL SELECT a FROM t6 ORDER BY 1 LIMIT 3 OFFSET 1;
292 SELECT x FROM t2 UNION SELECT x+2 FROM t2 LIMIT 2 OFFSET 1;
297 SELECT x FROM t2 UNION SELECT x+2 FROM t2 ORDER BY 1 DESC LIMIT 2 OFFSET 1;
302 SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 LIMIT 2;
307 SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 ORDER BY 1 DESC LIMIT 2;
312 SELECT a+26 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
317 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
322 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1 OFFSET 1;
327 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2
328 ORDER BY 1 DESC LIMIT 1 OFFSET 1;
331 } ;# ifcapable compound
333 # Tests for limit in conjunction with distinct. The distinct should
334 # occur before both the limit and the offset. Ticket #749.
338 SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5;
343 SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5 OFFSET 5;
348 SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5 OFFSET 25;
352 # Make sure limits on multiple subqueries work correctly.
358 SELECT * FROM (SELECT * FROM t6 LIMIT 3);
362 do_test limit-9.2.1 {
364 CREATE TABLE t7 AS SELECT * FROM t6;
368 do_test limit-9.2.2 {
370 SELECT * FROM (SELECT * FROM t7 LIMIT 3);
378 SELECT * FROM (SELECT * FROM t6 LIMIT 3)
380 SELECT * FROM (SELECT * FROM t7 LIMIT 3)
386 SELECT * FROM (SELECT * FROM t6 LIMIT 3)
388 SELECT * FROM (SELECT * FROM t7 LIMIT 3)
396 SELECT * FROM t6 LIMIT 3
398 SELECT * FROM t7 LIMIT 3
400 } {1 {LIMIT clause should come after UNION not before}}
403 # Test LIMIT and OFFSET using SQL variables.
407 SELECT x FROM t1 LIMIT :limit;
409 } {31 30 29 28 27 26 25 24 23 22}
414 SELECT x FROM t1 LIMIT :limit OFFSET :offset;
420 SELECT x FROM t1 WHERE x<10 LIMIT :limit;
422 } {9 8 7 6 5 4 3 2 1 0}
427 SELECT x FROM t1 WHERE x<10 LIMIT :limit;
430 } {1 {datatype mismatch}}
432 set limit "hello world"
435 SELECT x FROM t1 WHERE x<10 LIMIT :limit;
438 } {1 {datatype mismatch}}
443 SELECT x FROM (SELECT x FROM t1 ORDER BY x LIMIT 0) ORDER BY x
446 } ;# ifcapable subquery
448 # Test error processing.
452 SELECT * FROM t1 LIMIT replace(1)
454 } {1 {wrong number of arguments to function replace()}}
457 SELECT * FROM t1 LIMIT 5 OFFSET replace(1)
459 } {1 {wrong number of arguments to function replace()}}
462 SELECT * FROM t1 LIMIT x
464 } {1 {no such column: x}}
467 SELECT * FROM t1 LIMIT 1 OFFSET x
469 } {1 {no such column: x}}