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}}
471 # Ticket [db4d96798da8b]
472 # LIMIT does not work with nested views containing UNION ALL
477 INSERT INTO t13 VALUES(1),(2);
478 CREATE VIEW v13a AS SELECT x AS y FROM t13;
479 CREATE VIEW v13b AS SELECT y AS z FROM v13a UNION ALL SELECT y+10 FROM v13a;
480 CREATE VIEW v13c AS SELECT z FROM v13b UNION ALL SELECT z+20 FROM v13b;
484 db eval {SELECT z FROM v13c LIMIT 1}
487 db eval {SELECT z FROM v13c LIMIT 2}
490 db eval {SELECT z FROM v13c LIMIT 3}
493 db eval {SELECT z FROM v13c LIMIT 4}
496 db eval {SELECT z FROM v13c LIMIT 5}
499 db eval {SELECT z FROM v13c LIMIT 6}
502 db eval {SELECT z FROM v13c LIMIT 7}
503 } {1 2 11 12 21 22 31}
505 db eval {SELECT z FROM v13c LIMIT 8}
506 } {1 2 11 12 21 22 31 32}
507 do_test limit-13.10 {
508 db eval {SELECT z FROM v13c LIMIT 9}
509 } {1 2 11 12 21 22 31 32}
510 do_test limit-13.11 {
511 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 1}
513 do_test limit-13.12 {
514 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 1}
516 do_test limit-13.13 {
517 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 1}
519 do_test limit-13.14 {
520 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 1}
522 do_test limit-13.15 {
523 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 1}
525 do_test limit-13.16 {
526 db eval {SELECT z FROM v13c LIMIT 6 OFFSET 1}
528 do_test limit-13.17 {
529 db eval {SELECT z FROM v13c LIMIT 7 OFFSET 1}
530 } {2 11 12 21 22 31 32}
531 do_test limit-13.18 {
532 db eval {SELECT z FROM v13c LIMIT 8 OFFSET 1}
533 } {2 11 12 21 22 31 32}
534 do_test limit-13.21 {
535 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 2}
537 do_test limit-13.22 {
538 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 2}
540 do_test limit-13.23 {
541 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 2}
543 do_test limit-13.24 {
544 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 2}
546 do_test limit-13.25 {
547 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 2}
549 do_test limit-13.26 {
550 db eval {SELECT z FROM v13c LIMIT 6 OFFSET 2}
551 } {11 12 21 22 31 32}
552 do_test limit-13.27 {
553 db eval {SELECT z FROM v13c LIMIT 7 OFFSET 2}
554 } {11 12 21 22 31 32}
555 do_test limit-13.31 {
556 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 3}
558 do_test limit-13.32 {
559 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 3}
561 do_test limit-13.33 {
562 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 3}
564 do_test limit-13.34 {
565 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 3}
567 do_test limit-13.35 {
568 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 3}
570 do_test limit-13.36 {
571 db eval {SELECT z FROM v13c LIMIT 6 OFFSET 3}
573 do_test limit-13.41 {
574 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 4}
576 do_test limit-13.42 {
577 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 4}
579 do_test limit-13.43 {
580 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 4}
582 do_test limit-13.44 {
583 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 4}
585 do_test limit-13.45 {
586 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 4}
588 do_test limit-13.51 {
589 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 5}
591 do_test limit-13.52 {
592 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 5}
594 do_test limit-13.53 {
595 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 5}
597 do_test limit-13.54 {
598 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 5}
600 do_test limit-13.61 {
601 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 6}
603 do_test limit-13.62 {
604 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 6}
606 do_test limit-13.63 {
607 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 6}
609 do_test limit-13.71 {
610 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 7}
612 do_test limit-13.72 {
613 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 7}
615 do_test limit-13.81 {
616 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 8}
619 do_execsql_test limit-14.1 {
620 SELECT 123 LIMIT 1 OFFSET 0
622 do_execsql_test limit-14.2 {
623 SELECT 123 LIMIT 1 OFFSET 1
625 do_execsql_test limit-14.3 {
626 SELECT 123 LIMIT 0 OFFSET 0
628 do_execsql_test limit-14.4 {
629 SELECT 123 LIMIT 0 OFFSET 1
631 do_execsql_test limit-14.6 {
632 SELECT 123 LIMIT -1 OFFSET 0
634 do_execsql_test limit-14.7 {
635 SELECT 123 LIMIT -1 OFFSET 1