2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 November 6
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing the LIMIT ... OFFSET ... clause
2N/A# of SELECT statements.
2N/A#
2N/A# $Id: limit.test,v 1.11.2.1 2004/07/19 23:33:04 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Build some test data
2N/A#
2N/Aset fd [open data1.txt w]
2N/Afor {set i 1} {$i<=32} {incr i} {
2N/A for {set j 0} {pow(2,$j)<$i} {incr j} {}
2N/A puts $fd "[expr {32-$i}]\t[expr {10-$j}]"
2N/A}
2N/Aclose $fd
2N/Aexecsql {
2N/A CREATE TABLE t1(x int, y int);
2N/A COPY t1 FROM 'data1.txt'
2N/A}
2N/Afile delete data1.txt
2N/A
2N/Ado_test limit-1.0 {
2N/A execsql {SELECT count(*) FROM t1}
2N/A} {32}
2N/Ado_test limit-1.1 {
2N/A execsql {SELECT count(*) FROM t1 LIMIT 5}
2N/A} {32}
2N/Ado_test limit-1.2.1 {
2N/A execsql {SELECT x FROM t1 ORDER BY x LIMIT 5}
2N/A} {0 1 2 3 4}
2N/Ado_test limit-1.2.2 {
2N/A execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 2}
2N/A} {2 3 4 5 6}
2N/Ado_test limit-1.2.3 {
2N/A execsql {SELECT x FROM t1 ORDER BY x LIMIT 2, 5}
2N/A} {2 3 4 5 6}
2N/Ado_test limit-1.3 {
2N/A execsql {SELECT x FROM t1 ORDER BY x LIMIT 5 OFFSET 5}
2N/A} {5 6 7 8 9}
2N/Ado_test limit-1.4.1 {
2N/A execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 30}
2N/A} {30 31}
2N/Ado_test limit-1.4.2 {
2N/A execsql {SELECT x FROM t1 ORDER BY x LIMIT 30, 50}
2N/A} {30 31}
2N/Ado_test limit-1.5 {
2N/A execsql {SELECT x FROM t1 ORDER BY x LIMIT 50 OFFSET 50}
2N/A} {}
2N/Ado_test limit-1.6 {
2N/A execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5}
2N/A} {0 5 0 5 0 5 1 5 0 5 2 5 0 5 3 5 0 5 4 5}
2N/Ado_test limit-1.7 {
2N/A execsql {SELECT * FROM t1 AS a, t1 AS b ORDER BY a.x, b.x LIMIT 5 OFFSET 32}
2N/A} {1 5 0 5 1 5 1 5 1 5 2 5 1 5 3 5 1 5 4 5}
2N/A
2N/Ado_test limit-2.1 {
2N/A execsql {
2N/A CREATE VIEW v1 AS SELECT * FROM t1 LIMIT 2;
2N/A SELECT count(*) FROM (SELECT * FROM v1);
2N/A }
2N/A} 2
2N/Ado_test limit-2.2 {
2N/A execsql {
2N/A CREATE TABLE t2 AS SELECT * FROM t1 LIMIT 2;
2N/A SELECT count(*) FROM t2;
2N/A }
2N/A} 2
2N/Ado_test limit-2.3 {
2N/A execsql {
2N/A SELECT count(*) FROM t1 WHERE rowid IN (SELECT rowid FROM t1 LIMIT 2);
2N/A }
2N/A} 2
2N/A
2N/Ado_test limit-3.1 {
2N/A execsql {
2N/A SELECT z FROM (SELECT y*10+x AS z FROM t1 ORDER BY x LIMIT 10)
2N/A ORDER BY z LIMIT 5;
2N/A }
2N/A} {50 51 52 53 54}
2N/A
2N/Ado_test limit-4.1 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE t3(x);
2N/A INSERT INTO t3 SELECT x FROM t1 ORDER BY x LIMIT 10 OFFSET 1;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A INSERT INTO t3 SELECT x+(SELECT max(x) FROM t3) FROM t3;
2N/A END;
2N/A SELECT count(*) FROM t3;
2N/A }
2N/A} {10240}
2N/Ado_test limit-4.2 {
2N/A execsql {
2N/A SELECT x FROM t3 LIMIT 2 OFFSET 10000
2N/A }
2N/A} {10001 10002}
2N/Ado_test limit-4.3 {
2N/A execsql {
2N/A CREATE TABLE t4 AS SELECT x,
2N/A 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
2N/A 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
2N/A 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
2N/A 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x ||
2N/A 'abcdefghijklmnopqrstuvwyxz ABCDEFGHIJKLMNOPQRSTUVWYXZ' || x AS y
2N/A FROM t3 LIMIT 1000;
2N/A SELECT x FROM t4 ORDER BY y DESC LIMIT 1 OFFSET 999;
2N/A }
2N/A} {1000}
2N/A
2N/Ado_test limit-5.1 {
2N/A execsql {
2N/A CREATE TABLE t5(x,y);
2N/A INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
2N/A ORDER BY x LIMIT 2;
2N/A SELECT * FROM t5 ORDER BY x;
2N/A }
2N/A} {5 15 6 16}
2N/Ado_test limit-5.2 {
2N/A execsql {
2N/A DELETE FROM t5;
2N/A INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x BETWEEN 10 AND 15
2N/A ORDER BY x DESC LIMIT 2;
2N/A SELECT * FROM t5 ORDER BY x;
2N/A }
2N/A} {9 19 10 20}
2N/Ado_test limit-5.3 {
2N/A execsql {
2N/A DELETE FROM t5;
2N/A INSERT INTO t5 SELECT x-y, x+y FROM t1 WHERE x ORDER BY x DESC LIMIT 31;
2N/A SELECT * FROM t5 ORDER BY x LIMIT 2;
2N/A }
2N/A} {-4 6 -3 7}
2N/Ado_test limit-5.4 {
2N/A execsql {
2N/A SELECT * FROM t5 ORDER BY x DESC, y DESC LIMIT 2;
2N/A }
2N/A} {21 41 21 39}
2N/Ado_test limit-5.5 {
2N/A execsql {
2N/A DELETE FROM t5;
2N/A INSERT INTO t5 SELECT a.x*100+b.x, a.y*100+b.y FROM t1 AS a, t1 AS b
2N/A ORDER BY 1, 2 LIMIT 1000;
2N/A SELECT count(*), sum(x), sum(y), min(x), max(x), min(y), max(y) FROM t5;
2N/A }
2N/A} {1000 1528204 593161 0 3107 505 1005}
2N/A
2N/A# There is some contraversy about whether LIMIT 0 should be the same as
2N/A# no limit at all or if LIMIT 0 should result in zero output rows.
2N/A#
2N/Ado_test limit-6.1 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE t6(a);
2N/A INSERT INTO t6 VALUES(1);
2N/A INSERT INTO t6 VALUES(2);
2N/A INSERT INTO t6 SELECT a+2 FROM t6;
2N/A COMMIT;
2N/A SELECT * FROM t6;
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test limit-6.2 {
2N/A execsql {
2N/A SELECT * FROM t6 LIMIT -1 OFFSET -1;
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test limit-6.3 {
2N/A execsql {
2N/A SELECT * FROM t6 LIMIT 2 OFFSET -123;
2N/A }
2N/A} {1 2}
2N/Ado_test limit-6.4 {
2N/A execsql {
2N/A SELECT * FROM t6 LIMIT -432 OFFSET 2;
2N/A }
2N/A} {3 4}
2N/Ado_test limit-6.5 {
2N/A execsql {
2N/A SELECT * FROM t6 LIMIT -1
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test limit-6.6 {
2N/A execsql {
2N/A SELECT * FROM t6 LIMIT -1 OFFSET 1
2N/A }
2N/A} {2 3 4}
2N/Ado_test limit-6.7 {
2N/A execsql {
2N/A SELECT * FROM t6 LIMIT 0
2N/A }
2N/A} {}
2N/Ado_test limit-6.8 {
2N/A execsql {
2N/A SELECT * FROM t6 LIMIT 0 OFFSET 1
2N/A }
2N/A} {}
2N/A
2N/A# Make sure LIMIT works well with compound SELECT statements.
2N/A# Ticket #393
2N/A#
2N/Ado_test limit-7.1.1 {
2N/A catchsql {
2N/A SELECT x FROM t2 LIMIT 5 UNION ALL SELECT a FROM t6;
2N/A }
2N/A} {1 {LIMIT clause should come after UNION ALL not before}}
2N/Ado_test limit-7.1.2 {
2N/A catchsql {
2N/A SELECT x FROM t2 LIMIT 5 UNION SELECT a FROM t6;
2N/A }
2N/A} {1 {LIMIT clause should come after UNION not before}}
2N/Ado_test limit-7.1.3 {
2N/A catchsql {
2N/A SELECT x FROM t2 LIMIT 5 EXCEPT SELECT a FROM t6 LIMIT 3;
2N/A }
2N/A} {1 {LIMIT clause should come after EXCEPT not before}}
2N/Ado_test limit-7.1.4 {
2N/A catchsql {
2N/A SELECT x FROM t2 LIMIT 0,5 INTERSECT SELECT a FROM t6;
2N/A }
2N/A} {1 {LIMIT clause should come after INTERSECT not before}}
2N/Ado_test limit-7.2 {
2N/A execsql {
2N/A SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 5;
2N/A }
2N/A} {31 30 1 2 3}
2N/Ado_test limit-7.3 {
2N/A execsql {
2N/A SELECT x FROM t2 UNION ALL SELECT a FROM t6 LIMIT 3 OFFSET 1;
2N/A }
2N/A} {30 1 2}
2N/Ado_test limit-7.4 {
2N/A execsql {
2N/A SELECT x FROM t2 UNION ALL SELECT a FROM t6 ORDER BY 1 LIMIT 3 OFFSET 1;
2N/A }
2N/A} {2 3 4}
2N/Ado_test limit-7.5 {
2N/A execsql {
2N/A SELECT x FROM t2 UNION SELECT x+2 FROM t2 LIMIT 2 OFFSET 1;
2N/A }
2N/A} {31 32}
2N/Ado_test limit-7.6 {
2N/A execsql {
2N/A SELECT x FROM t2 UNION SELECT x+2 FROM t2 ORDER BY 1 DESC LIMIT 2 OFFSET 1;
2N/A }
2N/A} {32 31}
2N/Ado_test limit-7.7 {
2N/A execsql {
2N/A SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 LIMIT 2;
2N/A }
2N/A} {11 12}
2N/Ado_test limit-7.8 {
2N/A execsql {
2N/A SELECT a+9 FROM t6 EXCEPT SELECT y FROM t2 ORDER BY 1 DESC LIMIT 2;
2N/A }
2N/A} {13 12}
2N/Ado_test limit-7.9 {
2N/A execsql {
2N/A SELECT a+26 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
2N/A }
2N/A} {30}
2N/Ado_test limit-7.10 {
2N/A execsql {
2N/A SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1;
2N/A }
2N/A} {30}
2N/Ado_test limit-7.11 {
2N/A execsql {
2N/A SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2 LIMIT 1 OFFSET 1;
2N/A }
2N/A} {31}
2N/Ado_test limit-7.12 {
2N/A execsql {
2N/A SELECT a+27 FROM t6 INTERSECT SELECT x FROM t2
2N/A ORDER BY 1 DESC LIMIT 1 OFFSET 1;
2N/A }
2N/A} {30}
2N/A
2N/A# Tests for limit in conjunction with distinct. The distinct should
2N/A# occur before both the limit and the offset. Ticket #749.
2N/A#
2N/Ado_test limit-8.1 {
2N/A execsql {
2N/A SELECT DISTINCT round(x/100) FROM t3 LIMIT 5;
2N/A }
2N/A} {0 1 2 3 4}
2N/Ado_test limit-8.2 {
2N/A execsql {
2N/A SELECT DISTINCT round(x/100) FROM t3 LIMIT 5 OFFSET 5;
2N/A }
2N/A} {5 6 7 8 9}
2N/Ado_test limit-8.3 {
2N/A execsql {
2N/A SELECT DISTINCT round(x/100) FROM t3 LIMIT 5 OFFSET 25;
2N/A }
2N/A} {25 26 27 28 29}
2N/A
2N/A
2N/Afinish_test