sqlite/test/limit.test
drh a88dc3f629 Make LIMIT 0 return no rows. LIMIT -1 still returns all rows. Ticket #346. (CVS 1053)
FossilOrigin-Name: a31d0bd90af7cc95f8e36ca8ece21515f872235e
2003-07-16 11:51:35 +00:00

217 lines
5.6 KiB
Plaintext

# 2001 November 6
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the LIMIT ... OFFSET ... clause
# of SELECT statements.
#
# $Id: limit.test,v 1.9 2003/07/16 11:51:36 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Build some test data
#
set fd [open data1.txt w]
for {set i 1} {$i<=32} {incr i} {
for {set j 0} {pow(2,$j)<$i} {incr j} {}
puts $fd "[expr {32-$i}]\t[expr {10-$j}]"
}
close $fd
execsql {
CREATE TABLE t1(x int, y int);
COPY t1 FROM 'data1.txt'
}
file delete data1.txt
do_test limit-1.0 {
execsql {SELECT count(*) FROM t1}
} {32}
do_test limit-1.1 {
execsql {SELECT count(*) FROM t1 LIMIT 5}
} {32}
do_test limit-1.2.1 {
execsql {SELECT x FROM t1 ORDER BY x LIMIT 5}
} {0 1 2 3 4}
do_test limit-1.2.2 {
execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 2}
} {2 3 4 5 6}
do_test limit-1.2.3 {
execsql {SELECT x FROM t1 ORDER BY x LIMIT 2, 5}
} {2 3 4 5 6}
do_test limit-1.3 {
execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 5}
} {5 6 7 8 9}
do_test limit-1.4.1 {
execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 30}
} {30 31}
do_test limit-1.4.2 {
execsql {SELECT x FROM t1 ORDER BY x LIMIT 30, 50}
} {30 31}
do_test limit-1.5 {
execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 50}
} {}
do_test limit-1.6 {
execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5}
} {0 5 0 5 0 5 1 5 0 5 2 5 0 5 3 5 0 5 4 5}
do_test limit-1.7 {
execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5 OFFSET 32}
} {1 5 0 5 1 5 1 5 1 5 2 5 1 5 3 5 1 5 4 5}
do_test limit-2.1 {
execsql {
CREATE VIEW v1 AS SELECT * FROM t1 LIMIT 2;
SELECT count(*) FROM (SELECT * FROM v1);
}
} 2
do_test limit-2.2 {
execsql {
CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 2;
SELECT count(*) FROM t2;
}
} 2
do_test limit-2.3 {
execsql {
SELECT count(*) FROM t1 WHERE rowid IN (SELECT rowid FROM t1 LIMIT 2);
}
} 2
do_test limit-3.1 {
execsql {
SELECT z FROM (SELECT y*10+x AS z FROM t1 ORDER BY x LIMIT 10)
ORDER BY z LIMIT 5;
}
} {50 51 52 53 54}
do_test limit-4.1 {
execsql {
BEGIN;
CREATE TABLE t3 AS SELECT x FROM t1 ORDER BY x LIMIT 10 OFFSET 1;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
END;
SELECT count(*) FROM t3;
}
} {10240}
do_test limit-4.2 {
execsql {
SELECT x FROM t3 LIMIT 2 OFFSET 10000
}
} {10001 10002}
do_test limit-4.3 {
execsql {
CREATE TABLE t4 AS SELECT x,
'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x AS y
FROM t3 LIMIT 1000;
SELECT x FROM t4 ORDER BY y DESC LIMIT 1 OFFSET 999;
}
} {1000}
do_test limit-5.1 {
execsql {
CREATE TABLE t5(x,y);
INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
ORDER BY x LIMIT 2;
SELECT * FROM t5 ORDER BY x;
}
} {5 15 6 16}
do_test limit-5.2 {
execsql {
DELETE FROM t5;
INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
ORDER BY x DESC LIMIT 2;
SELECT * FROM t5 ORDER BY x;
}
} {9 19 10 20}
do_test limit-5.3 {
execsql {
DELETE FROM t5;
INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x ORDER BY x DESC LIMIT 31;
SELECT * FROM t5 ORDER BY x LIMIT 2;
}
} {-4 6 -3 7}
do_test limit-5.4 {
execsql {
SELECT * FROM t5 ORDER BY x DESC, y DESC LIMIT 2;
}
} {21 41 21 39}
do_test limit-5.5 {
execsql {
DELETE FROM t5;
INSERT INTO t5 SELECT a.x*100+b.x, a.y*100+b.y FROM t1 AS a, t1 AS b
ORDER BY 1, 2 LIMIT 1000;
SELECT count(*), sum(x), sum(y), min(x), max(x), min(y), max(y) FROM t5;
}
} {1000 1528204 593161 0 3107 505 1005}
# There is some contraversy about whether LIMIT 0 should be the same as
# no limit at all or if LIMIT 0 should result in zero output rows.
#
do_test limit-6.1 {
execsql {
BEGIN;
CREATE TABLE t6(a);
INSERT INTO t6 VALUES(1);
INSERT INTO t6 VALUES(2);
INSERT INTO t6 SELECT a+2 FROM t6;
COMMIT;
SELECT * FROM t6;
}
} {1 2 3 4}
do_test limit-6.2 {
execsql {
SELECT * FROM t6 LIMIT -1 OFFSET -1;
}
} {1 2 3 4}
do_test limit-6.3 {
execsql {
SELECT * FROM t6 LIMIT 2 OFFSET -123;
}
} {1 2}
do_test limit-6.4 {
execsql {
SELECT * FROM t6 LIMIT -432 OFFSET 2;
}
} {3 4}
do_test limit-6.5 {
execsql {
SELECT * FROM t6 LIMIT -1
}
} {1 2 3 4}
do_test limit-6.6 {
execsql {
SELECT * FROM t6 LIMIT -1 OFFSET 1
}
} {2 3 4}
do_test limit-6.7 {
execsql {
SELECT * FROM t6 LIMIT 0
}
} {}
do_test limit-6.8 {
execsql {
SELECT * FROM t6 LIMIT 0 OFFSET 1
}
} {}
finish_test