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.
254 # EVIDENCE-OF: R-13512-64012 In a compound SELECT, only the last or
255 # right-most simple SELECT may contain a LIMIT clause.
257 # EVIDENCE-OF: R-03782-50113 In a compound SELECT, the LIMIT clause
258 # applies to the entire compound, not just the final SELECT.
261 do_test limit-7.1.1 {
263 SELECT x FROM t2 LIMIT 5 UNION ALL SELECT a FROM t6;
265 } {1 {LIMIT clause should come after UNION ALL not before}}
266 do_test limit-7.1.2 {
268 SELECT x FROM t2 LIMIT 5 UNION SELECT a FROM t6;
270 } {1 {LIMIT clause should come after UNION not before}}
271 do_test limit-7.1.3 {
273 SELECT x FROM t2 LIMIT 5 EXCEPT SELECT a FROM t6 LIMIT 3;
275 } {1 {LIMIT clause should come after EXCEPT not before}}
276 do_test limit-7.1.4 {
278 SELECT x FROM t2 LIMIT 0,5 INTERSECT SELECT a FROM t6;
280 } {1 {LIMIT clause should come after INTERSECT not before}}
283 SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 5;
288 SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 3 OFFSET 1;
293 SELECT x FROM t2 UNION ALL SELECT a FROM t6 ORDER BY 1 LIMIT 3 OFFSET 1;
298 SELECT x FROM t2 UNION SELECT x+2 FROM t2 LIMIT 2 OFFSET 1;
303 SELECT x FROM t2 UNION SELECT x+2 FROM t2 ORDER BY 1 DESC LIMIT 2 OFFSET 1;
308 SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 LIMIT 2;
313 SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 ORDER BY 1 DESC LIMIT 2;
318 SELECT a+26 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
323 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
328 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1 OFFSET 1;
333 SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2
334 ORDER BY 1 DESC LIMIT 1 OFFSET 1;
337 } ;# ifcapable compound
339 # Tests for limit in conjunction with distinct. The distinct should
340 # occur before both the limit and the offset. Ticket #749.
344 SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5;
349 SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5 OFFSET 5;
354 SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5 OFFSET 25;
358 # Make sure limits on multiple subqueries work correctly.
364 SELECT * FROM (SELECT * FROM t6 LIMIT 3);
368 do_test limit-9.2.1 {
370 CREATE TABLE t7 AS SELECT * FROM t6;
374 do_test limit-9.2.2 {
376 SELECT * FROM (SELECT * FROM t7 LIMIT 3);
384 SELECT * FROM (SELECT * FROM t6 LIMIT 3)
386 SELECT * FROM (SELECT * FROM t7 LIMIT 3)
392 SELECT * FROM (SELECT * FROM t6 LIMIT 3)
394 SELECT * FROM (SELECT * FROM t7 LIMIT 3)
402 SELECT * FROM t6 LIMIT 3
404 SELECT * FROM t7 LIMIT 3
406 } {1 {LIMIT clause should come after UNION not before}}
409 # Test LIMIT and OFFSET using SQL variables.
413 SELECT x FROM t1 LIMIT :limit;
415 } {31 30 29 28 27 26 25 24 23 22}
420 SELECT x FROM t1 LIMIT :limit OFFSET :offset;
426 SELECT x FROM t1 WHERE x<10 LIMIT :limit;
428 } {9 8 7 6 5 4 3 2 1 0}
433 SELECT x FROM t1 WHERE x<10 LIMIT :limit;
436 } {1 {datatype mismatch}}
438 set limit "hello world"
441 SELECT x FROM t1 WHERE x<10 LIMIT :limit;
444 } {1 {datatype mismatch}}
449 SELECT x FROM (SELECT x FROM t1 ORDER BY x LIMIT 0) ORDER BY x
452 } ;# ifcapable subquery
454 # Test error processing.
458 SELECT * FROM t1 LIMIT replace(1)
460 } {1 {wrong number of arguments to function replace()}}
463 SELECT * FROM t1 LIMIT 5 OFFSET replace(1)
465 } {1 {wrong number of arguments to function replace()}}
468 SELECT * FROM t1 LIMIT x
470 } {1 {no such column: x}}
473 SELECT * FROM t1 LIMIT 1 OFFSET x
475 } {1 {no such column: x}}
477 # Ticket [db4d96798da8b]
478 # LIMIT does not work with nested views containing UNION ALL
483 INSERT INTO t13 VALUES(1),(2);
484 CREATE VIEW v13a AS SELECT x AS y FROM t13;
485 CREATE VIEW v13b AS SELECT y AS z FROM v13a UNION ALL SELECT y+10 FROM v13a;
486 CREATE VIEW v13c AS SELECT z FROM v13b UNION ALL SELECT z+20 FROM v13b;
490 db eval {SELECT z FROM v13c LIMIT 1}
493 db eval {SELECT z FROM v13c LIMIT 2}
496 db eval {SELECT z FROM v13c LIMIT 3}
499 db eval {SELECT z FROM v13c LIMIT 4}
502 db eval {SELECT z FROM v13c LIMIT 5}
505 db eval {SELECT z FROM v13c LIMIT 6}
508 db eval {SELECT z FROM v13c LIMIT 7}
509 } {1 2 11 12 21 22 31}
511 db eval {SELECT z FROM v13c LIMIT 8}
512 } {1 2 11 12 21 22 31 32}
513 do_test limit-13.10 {
514 db eval {SELECT z FROM v13c LIMIT 9}
515 } {1 2 11 12 21 22 31 32}
516 do_test limit-13.11 {
517 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 1}
519 do_test limit-13.12 {
520 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 1}
522 do_test limit-13.13 {
523 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 1}
525 do_test limit-13.14 {
526 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 1}
528 do_test limit-13.15 {
529 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 1}
531 do_test limit-13.16 {
532 db eval {SELECT z FROM v13c LIMIT 6 OFFSET 1}
534 do_test limit-13.17 {
535 db eval {SELECT z FROM v13c LIMIT 7 OFFSET 1}
536 } {2 11 12 21 22 31 32}
537 do_test limit-13.18 {
538 db eval {SELECT z FROM v13c LIMIT 8 OFFSET 1}
539 } {2 11 12 21 22 31 32}
540 do_test limit-13.21 {
541 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 2}
543 do_test limit-13.22 {
544 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 2}
546 do_test limit-13.23 {
547 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 2}
549 do_test limit-13.24 {
550 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 2}
552 do_test limit-13.25 {
553 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 2}
555 do_test limit-13.26 {
556 db eval {SELECT z FROM v13c LIMIT 6 OFFSET 2}
557 } {11 12 21 22 31 32}
558 do_test limit-13.27 {
559 db eval {SELECT z FROM v13c LIMIT 7 OFFSET 2}
560 } {11 12 21 22 31 32}
561 do_test limit-13.31 {
562 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 3}
564 do_test limit-13.32 {
565 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 3}
567 do_test limit-13.33 {
568 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 3}
570 do_test limit-13.34 {
571 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 3}
573 do_test limit-13.35 {
574 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 3}
576 do_test limit-13.36 {
577 db eval {SELECT z FROM v13c LIMIT 6 OFFSET 3}
579 do_test limit-13.41 {
580 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 4}
582 do_test limit-13.42 {
583 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 4}
585 do_test limit-13.43 {
586 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 4}
588 do_test limit-13.44 {
589 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 4}
591 do_test limit-13.45 {
592 db eval {SELECT z FROM v13c LIMIT 5 OFFSET 4}
594 do_test limit-13.51 {
595 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 5}
597 do_test limit-13.52 {
598 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 5}
600 do_test limit-13.53 {
601 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 5}
603 do_test limit-13.54 {
604 db eval {SELECT z FROM v13c LIMIT 4 OFFSET 5}
606 do_test limit-13.61 {
607 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 6}
609 do_test limit-13.62 {
610 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 6}
612 do_test limit-13.63 {
613 db eval {SELECT z FROM v13c LIMIT 3 OFFSET 6}
615 do_test limit-13.71 {
616 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 7}
618 do_test limit-13.72 {
619 db eval {SELECT z FROM v13c LIMIT 2 OFFSET 7}
621 do_test limit-13.81 {
622 db eval {SELECT z FROM v13c LIMIT 1 OFFSET 8}
625 do_execsql_test limit-14.1 {
626 SELECT 123 LIMIT 1 OFFSET 0
628 do_execsql_test limit-14.2 {
629 SELECT 123 LIMIT 1 OFFSET 1
631 do_execsql_test limit-14.3 {
632 SELECT 123 LIMIT 0 OFFSET 0
634 do_execsql_test limit-14.4 {
635 SELECT 123 LIMIT 0 OFFSET 1
637 do_execsql_test limit-14.6 {
638 SELECT 123 LIMIT -1 OFFSET 0
640 do_execsql_test limit-14.7 {
641 SELECT 123 LIMIT -1 OFFSET 1
644 # 2021-03-05 dbsqlfuzz crash-d811039c9f44f2d43199d5889fcf4085ef6221b9
647 do_execsql_test limit-15.1 {
648 CREATE TABLE t1(a PRIMARY KEY, b TEXT);
649 CREATE TABLE t4(c PRIMARY KEY, d);
650 CREATE TABLE t5(e PRIMARY KEY, f);
651 CREATE TABLE t6(g, h);
652 CREATE TABLE t3_a(k, v);
653 CREATE TABLE t3_b(k, v);
654 CREATE VIEW t3 AS SELECT * FROM t3_a UNION ALL SELECT * FROM t3_b;
655 INSERT INTO t5(e,f) VALUES(500000,'orange');
656 INSERT INTO t4(c,d) VALUES(300000,'blue'),(400,'green'),(8000,'grey');
657 INSERT INTO t1(a,b) VALUES(300000,'purple');
658 INSERT INTO t3_a VALUES(300000,'yellow'),(500,'pink'),(8000,'red');
659 INSERT INTO t6 default values;
662 (SELECT 200000 FROM t6 WHERE a = ( SELECT 300000 FROM t3 WHERE a ) ),
663 (SELECT 400000 FROM t5 WHERE e=500000),
664 (SELECT 600000 FROM t4 WHERE c=a)