minmax.test revision 2
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 15
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 SELECT statements that contain
2N/A# aggregate min() and max() functions and which are handled as
2N/A# as a special case.
2N/A#
2N/A# $Id: minmax.test,v 1.9.2.2 2004/07/18 21:14:05 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/Ado_test minmax-1.0 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE t1(x, y);
2N/A INSERT INTO t1 VALUES(1,1);
2N/A INSERT INTO t1 VALUES(2,2);
2N/A INSERT INTO t1 VALUES(3,2);
2N/A INSERT INTO t1 VALUES(4,3);
2N/A INSERT INTO t1 VALUES(5,3);
2N/A INSERT INTO t1 VALUES(6,3);
2N/A INSERT INTO t1 VALUES(7,3);
2N/A INSERT INTO t1 VALUES(8,4);
2N/A INSERT INTO t1 VALUES(9,4);
2N/A INSERT INTO t1 VALUES(10,4);
2N/A INSERT INTO t1 VALUES(11,4);
2N/A INSERT INTO t1 VALUES(12,4);
2N/A INSERT INTO t1 VALUES(13,4);
2N/A INSERT INTO t1 VALUES(14,4);
2N/A INSERT INTO t1 VALUES(15,4);
2N/A INSERT INTO t1 VALUES(16,5);
2N/A INSERT INTO t1 VALUES(17,5);
2N/A INSERT INTO t1 VALUES(18,5);
2N/A INSERT INTO t1 VALUES(19,5);
2N/A INSERT INTO t1 VALUES(20,5);
2N/A COMMIT;
2N/A SELECT DISTINCT y FROM t1 ORDER BY y;
2N/A }
2N/A} {1 2 3 4 5}
2N/A
2N/Ado_test minmax-1.1 {
2N/A set sqlite_search_count 0
2N/A execsql {SELECT min(x) FROM t1}
2N/A} {1}
2N/Ado_test minmax-1.2 {
2N/A set sqlite_search_count
2N/A} {19}
2N/Ado_test minmax-1.3 {
2N/A set sqlite_search_count 0
2N/A execsql {SELECT max(x) FROM t1}
2N/A} {20}
2N/Ado_test minmax-1.4 {
2N/A set sqlite_search_count
2N/A} {19}
2N/Ado_test minmax-1.5 {
2N/A execsql {CREATE INDEX t1i1 ON t1(x)}
2N/A set sqlite_search_count 0
2N/A execsql {SELECT min(x) FROM t1}
2N/A} {1}
2N/Ado_test minmax-1.6 {
2N/A set sqlite_search_count
2N/A} {2}
2N/Ado_test minmax-1.7 {
2N/A set sqlite_search_count 0
2N/A execsql {SELECT max(x) FROM t1}
2N/A} {20}
2N/Ado_test minmax-1.8 {
2N/A set sqlite_search_count
2N/A} {1}
2N/Ado_test minmax-1.9 {
2N/A set sqlite_search_count 0
2N/A execsql {SELECT max(y) FROM t1}
2N/A} {5}
2N/Ado_test minmax-1.10 {
2N/A set sqlite_search_count
2N/A} {19}
2N/A
2N/Ado_test minmax-2.0 {
2N/A execsql {
2N/A CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
2N/A INSERT INTO t2 SELECT * FROM t1;
2N/A }
2N/A set sqlite_search_count 0
2N/A execsql {SELECT min(a) FROM t2}
2N/A} {1}
2N/Ado_test minmax-2.1 {
2N/A set sqlite_search_count
2N/A} {0}
2N/Ado_test minmax-2.2 {
2N/A set sqlite_search_count 0
2N/A execsql {SELECT max(a) FROM t2}
2N/A} {20}
2N/Ado_test minmax-2.3 {
2N/A set sqlite_search_count
2N/A} {0}
2N/A
2N/Ado_test minmax-3.0 {
2N/A execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
2N/A set sqlite_search_count 0
2N/A execsql {SELECT max(a) FROM t2}
2N/A} {21}
2N/Ado_test minmax-3.1 {
2N/A set sqlite_search_count
2N/A} {0}
2N/Ado_test minmax-3.2 {
2N/A execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
2N/A set sqlite_search_count 0
2N/A execsql {
2N/A SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2)
2N/A }
2N/A} {999}
2N/Ado_test minmax-3.3 {
2N/A set sqlite_search_count
2N/A} {0}
2N/A
2N/Ado_test minmax-4.1 {
2N/A execsql {
2N/A SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
2N/A (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
2N/A }
2N/A} {1 20}
2N/Ado_test minmax-4.2 {
2N/A execsql {
2N/A SELECT y, sum(x) FROM
2N/A (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
2N/A GROUP BY y ORDER BY y;
2N/A }
2N/A} {1 1 2 5 3 22 4 92 5 90 6 0}
2N/Ado_test minmax-4.3 {
2N/A execsql {
2N/A SELECT y, count(x), count(*) FROM
2N/A (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
2N/A GROUP BY y ORDER BY y;
2N/A }
2N/A} {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
2N/A
2N/A# Make sure the min(x) and max(x) optimizations work on empty tables
2N/A# including empty tables with indices. Ticket #296.
2N/A#
2N/Ado_test minmax-5.1 {
2N/A execsql {
2N/A CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
2N/A SELECT coalesce(min(x),999) FROM t3;
2N/A }
2N/A} {999}
2N/Ado_test minmax-5.2 {
2N/A execsql {
2N/A SELECT coalesce(min(rowid),999) FROM t3;
2N/A }
2N/A} {999}
2N/Ado_test minmax-5.3 {
2N/A execsql {
2N/A SELECT coalesce(max(x),999) FROM t3;
2N/A }
2N/A} {999}
2N/Ado_test minmax-5.4 {
2N/A execsql {
2N/A SELECT coalesce(max(rowid),999) FROM t3;
2N/A }
2N/A} {999}
2N/Ado_test minmax-5.5 {
2N/A execsql {
2N/A SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
2N/A }
2N/A} {999}
2N/A
2N/A# Make sure the min(x) and max(x) optimizations work when there
2N/A# is a LIMIT clause. Ticket #396.
2N/A#
2N/Ado_test minmax-6.1 {
2N/A execsql {
2N/A SELECT min(a) FROM t2 LIMIT 1
2N/A }
2N/A} {1}
2N/Ado_test minmax-6.2 {
2N/A execsql {
2N/A SELECT max(a) FROM t2 LIMIT 3
2N/A }
2N/A} {22}
2N/Ado_test minmax-6.3 {
2N/A execsql {
2N/A SELECT min(a) FROM t2 LIMIT 0,100
2N/A }
2N/A} {1}
2N/Ado_test minmax-6.4 {
2N/A execsql {
2N/A SELECT max(a) FROM t2 LIMIT 1,100
2N/A }
2N/A} {}
2N/Ado_test minmax-6.5 {
2N/A execsql {
2N/A SELECT min(x) FROM t3 LIMIT 1
2N/A }
2N/A} {{}}
2N/Ado_test minmax-6.6 {
2N/A execsql {
2N/A SELECT max(x) FROM t3 LIMIT 0
2N/A }
2N/A} {}
2N/Ado_test minmax-6.7 {
2N/A execsql {
2N/A SELECT max(a) FROM t2 LIMIT 0
2N/A }
2N/A} {}
2N/A
2N/A# Make sure the max(x) and min(x) optimizations work for nested
2N/A# queries. Ticket #587.
2N/A#
2N/Ado_test minmax-7.1 {
2N/A execsql {
2N/A SELECT max(x) FROM t1;
2N/A }
2N/A} 20
2N/Ado_test minmax-7.2 {
2N/A execsql {
2N/A SELECT * FROM (SELECT max(x) FROM t1);
2N/A }
2N/A} 20
2N/Ado_test minmax-7.3 {
2N/A execsql {
2N/A SELECT min(x) FROM t1;
2N/A }
2N/A} 1
2N/Ado_test minmax-7.4 {
2N/A execsql {
2N/A SELECT * FROM (SELECT min(x) FROM t1);
2N/A }
2N/A} 1
2N/A
2N/A# Make sure min(x) and max(x) work correctly when the datatype is
2N/A# TEXT instead of NUMERIC. Ticket #623.
2N/A#
2N/Ado_test minmax-8.1 {
2N/A execsql {
2N/A CREATE TABLE t4(a TEXT);
2N/A INSERT INTO t4 VALUES('1234');
2N/A INSERT INTO t4 VALUES('234');
2N/A INSERT INTO t4 VALUES('34');
2N/A SELECT min(a), max(a) FROM t4;
2N/A }
2N/A} {1234 34}
2N/Ado_test minmax-8.2 {
2N/A execsql {
2N/A CREATE TABLE t5(a INTEGER);
2N/A INSERT INTO t5 VALUES('1234');
2N/A INSERT INTO t5 VALUES('234');
2N/A INSERT INTO t5 VALUES('34');
2N/A SELECT min(a), max(a) FROM t5;
2N/A }
2N/A} {34 1234}
2N/A
2N/A# Ticket #658: Test the min()/max() optimization when the FROM clause
2N/A# is a subquery.
2N/A#
2N/Ado_test minmax-9.1 {
2N/A execsql {
2N/A SELECT max(rowid) FROM (
2N/A SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
2N/A )
2N/A }
2N/A} {1}
2N/Ado_test minmax-9.2 {
2N/A execsql {
2N/A SELECT max(rowid) FROM (
2N/A SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
2N/A )
2N/A }
2N/A} {{}}
2N/A
2N/A# If there is a NULL in an aggregate max() or min(), ignore it. An
2N/A# aggregate min() or max() will only return NULL if all values are NULL.
2N/A#
2N/Ado_test minmax-10.1 {
2N/A execsql {
2N/A CREATE TABLE t6(x);
2N/A INSERT INTO t6 VALUES(1);
2N/A INSERT INTO t6 VALUES(2);
2N/A INSERT INTO t6 VALUES(NULL);
2N/A SELECT coalesce(min(x),-1) FROM t6;
2N/A }
2N/A} {1}
2N/Ado_test minmax-10.2 {
2N/A execsql {
2N/A SELECT max(x) FROM t6;
2N/A }
2N/A} {2}
2N/Ado_test minmax-10.3 {
2N/A execsql {
2N/A CREATE INDEX i6 ON t6(x);
2N/A SELECT coalesce(min(x),-1) FROM t6;
2N/A }
2N/A} {1}
2N/Ado_test minmax-10.4 {
2N/A execsql {
2N/A SELECT max(x) FROM t6;
2N/A }
2N/A} {2}
2N/Ado_test minmax-10.5 {
2N/A execsql {
2N/A DELETE FROM t6 WHERE x NOT NULL;
2N/A SELECT count(*) FROM t6;
2N/A }
2N/A} 1
2N/Ado_test minmax-10.6 {
2N/A execsql {
2N/A SELECT count(x) FROM t6;
2N/A }
2N/A} 0
2N/Ado_test minmax-10.7 {
2N/A execsql {
2N/A SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
2N/A }
2N/A} {{} {}}
2N/Ado_test minmax-10.8 {
2N/A execsql {
2N/A SELECT min(x), max(x) FROM t6;
2N/A }
2N/A} {{} {}}
2N/Ado_test minmax-10.9 {
2N/A execsql {
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A SELECT count(*) FROM t6;
2N/A }
2N/A} 1024
2N/Ado_test minmax-10.10 {
2N/A execsql {
2N/A SELECT count(x) FROM t6;
2N/A }
2N/A} 0
2N/Ado_test minmax-10.11 {
2N/A execsql {
2N/A SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
2N/A }
2N/A} {{} {}}
2N/Ado_test minmax-10.12 {
2N/A execsql {
2N/A SELECT min(x), max(x) FROM t6;
2N/A }
2N/A} {{} {}}
2N/A
2N/Afinish_test