sl@0: # 2001 November 6 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the LIMIT ... OFFSET ... clause sl@0: # of SELECT statements. sl@0: # sl@0: # $Id: limit.test,v 1.32 2008/08/02 03:50:39 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Build some test data sl@0: # sl@0: execsql { sl@0: CREATE TABLE t1(x int, y int); sl@0: BEGIN; sl@0: } sl@0: for {set i 1} {$i<=32} {incr i} { sl@0: for {set j 0} {(1<<$j)<$i} {incr j} {} sl@0: execsql "INSERT INTO t1 VALUES([expr {32-$i}],[expr {10-$j}])" sl@0: } sl@0: execsql { sl@0: COMMIT; sl@0: } sl@0: sl@0: do_test limit-1.0 { sl@0: execsql {SELECT count(*) FROM t1} sl@0: } {32} sl@0: do_test limit-1.1 { sl@0: execsql {SELECT count(*) FROM t1 LIMIT 5} sl@0: } {32} sl@0: do_test limit-1.2.1 { sl@0: execsql {SELECT x FROM t1 ORDER BY x LIMIT 5} sl@0: } {0 1 2 3 4} sl@0: do_test limit-1.2.2 { sl@0: execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 2} sl@0: } {2 3 4 5 6} sl@0: do_test limit-1.2.3 { sl@0: execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT 5 OFFSET -2} sl@0: } {0 1 2 3 4} sl@0: do_test limit-1.2.4 { sl@0: execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT 2, -5} sl@0: } {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} sl@0: do_test limit-1.2.5 { sl@0: execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT -2, 5} sl@0: } {0 1 2 3 4} sl@0: do_test limit-1.2.6 { sl@0: execsql {SELECT x FROM t1 ORDER BY x+1 LIMIT -2, -5} sl@0: } {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} sl@0: do_test limit-1.2.7 { sl@0: execsql {SELECT x FROM t1 ORDER BY x LIMIT 2, 5} sl@0: } {2 3 4 5 6} sl@0: do_test limit-1.3 { sl@0: execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 5} sl@0: } {5 6 7 8 9} sl@0: do_test limit-1.4.1 { sl@0: execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 30} sl@0: } {30 31} sl@0: do_test limit-1.4.2 { sl@0: execsql {SELECT x FROM t1 ORDER BY x LIMIT 30, 50} sl@0: } {30 31} sl@0: do_test limit-1.5 { sl@0: execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 50} sl@0: } {} sl@0: do_test limit-1.6 { sl@0: execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5} sl@0: } {0 5 0 5 0 5 1 5 0 5 2 5 0 5 3 5 0 5 4 5} sl@0: do_test limit-1.7 { sl@0: execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5 OFFSET 32} sl@0: } {1 5 0 5 1 5 1 5 1 5 2 5 1 5 3 5 1 5 4 5} sl@0: sl@0: ifcapable {view && subquery} { sl@0: do_test limit-2.1 { sl@0: execsql { sl@0: CREATE VIEW v1 AS SELECT * FROM t1 LIMIT 2; sl@0: SELECT count(*) FROM (SELECT * FROM v1); sl@0: } sl@0: } 2 sl@0: } ;# ifcapable view sl@0: do_test limit-2.2 { sl@0: execsql { sl@0: CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 2; sl@0: SELECT count(*) FROM t2; sl@0: } sl@0: } 2 sl@0: ifcapable subquery { sl@0: do_test limit-2.3 { sl@0: execsql { sl@0: SELECT count(*) FROM t1 WHERE rowid IN (SELECT rowid FROM t1 LIMIT 2); sl@0: } sl@0: } 2 sl@0: } sl@0: sl@0: ifcapable subquery { sl@0: do_test limit-3.1 { sl@0: execsql { sl@0: SELECT z FROM (SELECT y*10+x AS z FROM t1 ORDER BY x LIMIT 10) sl@0: ORDER BY z LIMIT 5; sl@0: } sl@0: } {50 51 52 53 54} sl@0: } sl@0: sl@0: do_test limit-4.1 { sl@0: ifcapable subquery { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t3(x); sl@0: INSERT INTO t3 SELECT x FROM t1 ORDER BY x LIMIT 10 OFFSET 1; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3; sl@0: END; sl@0: SELECT count(*) FROM t3; sl@0: } sl@0: } else { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t3(x); sl@0: INSERT INTO t3 SELECT x FROM t1 ORDER BY x LIMIT 10 OFFSET 1; sl@0: } sl@0: for {set i 0} {$i<10} {incr i} { sl@0: set max_x_t3 [execsql {SELECT max(x) FROM t3}] sl@0: execsql "INSERT INTO t3 SELECT x+$max_x_t3 FROM t3;" sl@0: } sl@0: execsql { sl@0: END; sl@0: SELECT count(*) FROM t3; sl@0: } sl@0: } sl@0: } {10240} sl@0: do_test limit-4.2 { sl@0: execsql { sl@0: SELECT x FROM t3 LIMIT 2 OFFSET 10000 sl@0: } sl@0: } {10001 10002} sl@0: do_test limit-4.3 { sl@0: execsql { sl@0: CREATE TABLE t4 AS SELECT x, sl@0: 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x || sl@0: 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x || sl@0: 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x || sl@0: 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x || sl@0: 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x AS y sl@0: FROM t3 LIMIT 1000; sl@0: SELECT x FROM t4 ORDER BY y DESC LIMIT 1 OFFSET 999; sl@0: } sl@0: } {1000} sl@0: sl@0: do_test limit-5.1 { sl@0: execsql { sl@0: CREATE TABLE t5(x,y); sl@0: INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15 sl@0: ORDER BY x LIMIT 2; sl@0: SELECT * FROM t5 ORDER BY x; sl@0: } sl@0: } {5 15 6 16} sl@0: do_test limit-5.2 { sl@0: execsql { sl@0: DELETE FROM t5; sl@0: INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15 sl@0: ORDER BY x DESC LIMIT 2; sl@0: SELECT * FROM t5 ORDER BY x; sl@0: } sl@0: } {9 19 10 20} sl@0: do_test limit-5.3 { sl@0: execsql { sl@0: DELETE FROM t5; sl@0: INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x ORDER BY x DESC LIMIT 31; sl@0: SELECT * FROM t5 ORDER BY x LIMIT 2; sl@0: } sl@0: } {-4 6 -3 7} sl@0: do_test limit-5.4 { sl@0: execsql { sl@0: SELECT * FROM t5 ORDER BY x DESC, y DESC LIMIT 2; sl@0: } sl@0: } {21 41 21 39} sl@0: do_test limit-5.5 { sl@0: execsql { sl@0: DELETE FROM t5; sl@0: INSERT INTO t5 SELECT a.x*100+b.x, a.y*100+b.y FROM t1 AS a, t1 AS b sl@0: ORDER BY 1, 2 LIMIT 1000; sl@0: SELECT count(*), sum(x), sum(y), min(x), max(x), min(y), max(y) FROM t5; sl@0: } sl@0: } {1000 1528204 593161 0 3107 505 1005} sl@0: sl@0: # There is some contraversy about whether LIMIT 0 should be the same as sl@0: # no limit at all or if LIMIT 0 should result in zero output rows. sl@0: # sl@0: do_test limit-6.1 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t6(a); sl@0: INSERT INTO t6 VALUES(1); sl@0: INSERT INTO t6 VALUES(2); sl@0: INSERT INTO t6 SELECT a+2 FROM t6; sl@0: COMMIT; sl@0: SELECT * FROM t6; sl@0: } sl@0: } {1 2 3 4} sl@0: do_test limit-6.2 { sl@0: execsql { sl@0: SELECT * FROM t6 LIMIT -1 OFFSET -1; sl@0: } sl@0: } {1 2 3 4} sl@0: do_test limit-6.3 { sl@0: execsql { sl@0: SELECT * FROM t6 LIMIT 2 OFFSET -123; sl@0: } sl@0: } {1 2} sl@0: do_test limit-6.4 { sl@0: execsql { sl@0: SELECT * FROM t6 LIMIT -432 OFFSET 2; sl@0: } sl@0: } {3 4} sl@0: do_test limit-6.5 { sl@0: execsql { sl@0: SELECT * FROM t6 LIMIT -1 sl@0: } sl@0: } {1 2 3 4} sl@0: do_test limit-6.6 { sl@0: execsql { sl@0: SELECT * FROM t6 LIMIT -1 OFFSET 1 sl@0: } sl@0: } {2 3 4} sl@0: do_test limit-6.7 { sl@0: execsql { sl@0: SELECT * FROM t6 LIMIT 0 sl@0: } sl@0: } {} sl@0: do_test limit-6.8 { sl@0: execsql { sl@0: SELECT * FROM t6 LIMIT 0 OFFSET 1 sl@0: } sl@0: } {} sl@0: sl@0: # Make sure LIMIT works well with compound SELECT statements. sl@0: # Ticket #393 sl@0: # sl@0: ifcapable compound { sl@0: do_test limit-7.1.1 { sl@0: catchsql { sl@0: SELECT x FROM t2 LIMIT 5 UNION ALL SELECT a FROM t6; sl@0: } sl@0: } {1 {LIMIT clause should come after UNION ALL not before}} sl@0: do_test limit-7.1.2 { sl@0: catchsql { sl@0: SELECT x FROM t2 LIMIT 5 UNION SELECT a FROM t6; sl@0: } sl@0: } {1 {LIMIT clause should come after UNION not before}} sl@0: do_test limit-7.1.3 { sl@0: catchsql { sl@0: SELECT x FROM t2 LIMIT 5 EXCEPT SELECT a FROM t6 LIMIT 3; sl@0: } sl@0: } {1 {LIMIT clause should come after EXCEPT not before}} sl@0: do_test limit-7.1.4 { sl@0: catchsql { sl@0: SELECT x FROM t2 LIMIT 0,5 INTERSECT SELECT a FROM t6; sl@0: } sl@0: } {1 {LIMIT clause should come after INTERSECT not before}} sl@0: do_test limit-7.2 { sl@0: execsql { sl@0: SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 5; sl@0: } sl@0: } {31 30 1 2 3} sl@0: do_test limit-7.3 { sl@0: execsql { sl@0: SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 3 OFFSET 1; sl@0: } sl@0: } {30 1 2} sl@0: do_test limit-7.4 { sl@0: execsql { sl@0: SELECT x FROM t2 UNION ALL SELECT a FROM t6 ORDER BY 1 LIMIT 3 OFFSET 1; sl@0: } sl@0: } {2 3 4} sl@0: do_test limit-7.5 { sl@0: execsql { sl@0: SELECT x FROM t2 UNION SELECT x+2 FROM t2 LIMIT 2 OFFSET 1; sl@0: } sl@0: } {31 32} sl@0: do_test limit-7.6 { sl@0: execsql { sl@0: SELECT x FROM t2 UNION SELECT x+2 FROM t2 ORDER BY 1 DESC LIMIT 2 OFFSET 1; sl@0: } sl@0: } {32 31} sl@0: do_test limit-7.7 { sl@0: execsql { sl@0: SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 LIMIT 2; sl@0: } sl@0: } {11 12} sl@0: do_test limit-7.8 { sl@0: execsql { sl@0: SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 ORDER BY 1 DESC LIMIT 2; sl@0: } sl@0: } {13 12} sl@0: do_test limit-7.9 { sl@0: execsql { sl@0: SELECT a+26 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1; sl@0: } sl@0: } {30} sl@0: do_test limit-7.10 { sl@0: execsql { sl@0: SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1; sl@0: } sl@0: } {30} sl@0: do_test limit-7.11 { sl@0: execsql { sl@0: SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1 OFFSET 1; sl@0: } sl@0: } {31} sl@0: do_test limit-7.12 { sl@0: execsql { sl@0: SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 sl@0: ORDER BY 1 DESC LIMIT 1 OFFSET 1; sl@0: } sl@0: } {30} sl@0: } ;# ifcapable compound sl@0: sl@0: # Tests for limit in conjunction with distinct. The distinct should sl@0: # occur before both the limit and the offset. Ticket #749. sl@0: # sl@0: do_test limit-8.1 { sl@0: execsql { sl@0: SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5; sl@0: } sl@0: } {0 1 2 3 4} sl@0: do_test limit-8.2 { sl@0: execsql { sl@0: SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5 OFFSET 5; sl@0: } sl@0: } {5 6 7 8 9} sl@0: do_test limit-8.3 { sl@0: execsql { sl@0: SELECT DISTINCT cast(round(x/100) as integer) FROM t3 LIMIT 5 OFFSET 25; sl@0: } sl@0: } {25 26 27 28 29} sl@0: sl@0: # Make sure limits on multiple subqueries work correctly. sl@0: # Ticket #1035 sl@0: # sl@0: ifcapable subquery { sl@0: do_test limit-9.1 { sl@0: execsql { sl@0: SELECT * FROM (SELECT * FROM t6 LIMIT 3); sl@0: } sl@0: } {1 2 3} sl@0: } sl@0: do_test limit-9.2.1 { sl@0: execsql { sl@0: CREATE TABLE t7 AS SELECT * FROM t6; sl@0: } sl@0: } {} sl@0: ifcapable subquery { sl@0: do_test limit-9.2.2 { sl@0: execsql { sl@0: SELECT * FROM (SELECT * FROM t7 LIMIT 3); sl@0: } sl@0: } {1 2 3} sl@0: } sl@0: ifcapable compound { sl@0: ifcapable subquery { sl@0: do_test limit-9.3 { sl@0: execsql { sl@0: SELECT * FROM (SELECT * FROM t6 LIMIT 3) sl@0: UNION sl@0: SELECT * FROM (SELECT * FROM t7 LIMIT 3) sl@0: ORDER BY 1 sl@0: } sl@0: } {1 2 3} sl@0: do_test limit-9.4 { sl@0: execsql { sl@0: SELECT * FROM (SELECT * FROM t6 LIMIT 3) sl@0: UNION sl@0: SELECT * FROM (SELECT * FROM t7 LIMIT 3) sl@0: ORDER BY 1 sl@0: LIMIT 2 sl@0: } sl@0: } {1 2} sl@0: } sl@0: do_test limit-9.5 { sl@0: catchsql { sl@0: SELECT * FROM t6 LIMIT 3 sl@0: UNION sl@0: SELECT * FROM t7 LIMIT 3 sl@0: } sl@0: } {1 {LIMIT clause should come after UNION not before}} sl@0: } sl@0: sl@0: # Test LIMIT and OFFSET using SQL variables. sl@0: do_test limit-10.1 { sl@0: set limit 10 sl@0: db eval { sl@0: SELECT x FROM t1 LIMIT :limit; sl@0: } sl@0: } {31 30 29 28 27 26 25 24 23 22} sl@0: do_test limit-10.2 { sl@0: set limit 5 sl@0: set offset 5 sl@0: db eval { sl@0: SELECT x FROM t1 LIMIT :limit OFFSET :offset; sl@0: } sl@0: } {26 25 24 23 22} sl@0: do_test limit-10.3 { sl@0: set limit -1 sl@0: db eval { sl@0: SELECT x FROM t1 WHERE x<10 LIMIT :limit; sl@0: } sl@0: } {9 8 7 6 5 4 3 2 1 0} sl@0: do_test limit-10.4 { sl@0: set limit 1.5 sl@0: set rc [catch { sl@0: db eval { sl@0: SELECT x FROM t1 WHERE x<10 LIMIT :limit; sl@0: } } msg] sl@0: list $rc $msg sl@0: } {1 {datatype mismatch}} sl@0: do_test limit-10.5 { sl@0: set limit "hello world" sl@0: set rc [catch { sl@0: db eval { sl@0: SELECT x FROM t1 WHERE x<10 LIMIT :limit; sl@0: } } msg] sl@0: list $rc $msg sl@0: } {1 {datatype mismatch}} sl@0: sl@0: ifcapable subquery { sl@0: do_test limit-11.1 { sl@0: db eval { sl@0: SELECT x FROM (SELECT x FROM t1 ORDER BY x LIMIT 0) ORDER BY x sl@0: } sl@0: } {} sl@0: } ;# ifcapable subquery sl@0: sl@0: # Test error processing. sl@0: # sl@0: do_test limit-12.1 { sl@0: catchsql { sl@0: SELECT * FROM t1 LIMIT replace(1) sl@0: } sl@0: } {1 {wrong number of arguments to function replace()}} sl@0: do_test limit-12.2 { sl@0: catchsql { sl@0: SELECT * FROM t1 LIMIT 5 OFFSET replace(1) sl@0: } sl@0: } {1 {wrong number of arguments to function replace()}} sl@0: do_test limit-12.3 { sl@0: catchsql { sl@0: SELECT * FROM t1 LIMIT x sl@0: } sl@0: } {1 {no such column: x}} sl@0: do_test limit-12.4 { sl@0: catchsql { sl@0: SELECT * FROM t1 LIMIT 1 OFFSET x sl@0: } sl@0: } {1 {no such column: x}} sl@0: sl@0: sl@0: finish_test