2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing SELECT statements that contain
16 # aggregate min() and max() functions and which are handled as
19 # $Id: minmax.test,v 1.9.2.2 2004/07/18 21:14:05 drh Exp $
21 set testdir [file dirname $argv0]
22 source $testdir/tester.tcl
27 CREATE TABLE t1(x, y);
28 INSERT INTO t1 VALUES(1,1);
29 INSERT INTO t1 VALUES(2,2);
30 INSERT INTO t1 VALUES(3,2);
31 INSERT INTO t1 VALUES(4,3);
32 INSERT INTO t1 VALUES(5,3);
33 INSERT INTO t1 VALUES(6,3);
34 INSERT INTO t1 VALUES(7,3);
35 INSERT INTO t1 VALUES(8,4);
36 INSERT INTO t1 VALUES(9,4);
37 INSERT INTO t1 VALUES(10,4);
38 INSERT INTO t1 VALUES(11,4);
39 INSERT INTO t1 VALUES(12,4);
40 INSERT INTO t1 VALUES(13,4);
41 INSERT INTO t1 VALUES(14,4);
42 INSERT INTO t1 VALUES(15,4);
43 INSERT INTO t1 VALUES(16,5);
44 INSERT INTO t1 VALUES(17,5);
45 INSERT INTO t1 VALUES(18,5);
46 INSERT INTO t1 VALUES(19,5);
47 INSERT INTO t1 VALUES(20,5);
49 SELECT DISTINCT y FROM t1 ORDER BY y;
54 set sqlite_search_count 0
55 execsql {SELECT min(x) FROM t1}
58 set sqlite_search_count
61 set sqlite_search_count 0
62 execsql {SELECT max(x) FROM t1}
65 set sqlite_search_count
68 execsql {CREATE INDEX t1i1 ON t1(x)}
69 set sqlite_search_count 0
70 execsql {SELECT min(x) FROM t1}
73 set sqlite_search_count
76 set sqlite_search_count 0
77 execsql {SELECT max(x) FROM t1}
80 set sqlite_search_count
83 set sqlite_search_count 0
84 execsql {SELECT max(y) FROM t1}
87 set sqlite_search_count
92 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
93 INSERT INTO t2 SELECT * FROM t1;
95 set sqlite_search_count 0
96 execsql {SELECT min(a) FROM t2}
99 set sqlite_search_count
102 set sqlite_search_count 0
103 execsql {SELECT max(a) FROM t2}
106 set sqlite_search_count
110 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
111 set sqlite_search_count 0
112 execsql {SELECT max(a) FROM t2}
115 set sqlite_search_count
118 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
119 set sqlite_search_count 0
121 SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2)
125 set sqlite_search_count
130 SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
131 (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
136 SELECT y, sum(x) FROM
137 (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
138 GROUP BY y ORDER BY y;
140 } {1 1 2 5 3 22 4 92 5 90 6 0}
143 SELECT y, count(x), count(*) FROM
144 (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
145 GROUP BY y ORDER BY y;
147 } {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
149 # Make sure the min(x) and max(x) optimizations work on empty tables
150 # including empty tables with indices. Ticket #296.
154 CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
155 SELECT coalesce(min(x),999) FROM t3;
160 SELECT coalesce(min(rowid),999) FROM t3;
165 SELECT coalesce(max(x),999) FROM t3;
170 SELECT coalesce(max(rowid),999) FROM t3;
175 SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
179 # Make sure the min(x) and max(x) optimizations work when there
180 # is a LIMIT clause. Ticket #396.
184 SELECT min(a) FROM t2 LIMIT 1
189 SELECT max(a) FROM t2 LIMIT 3
194 SELECT min(a) FROM t2 LIMIT 0,100
199 SELECT max(a) FROM t2 LIMIT 1,100
204 SELECT min(x) FROM t3 LIMIT 1
209 SELECT max(x) FROM t3 LIMIT 0
214 SELECT max(a) FROM t2 LIMIT 0
218 # Make sure the max(x) and min(x) optimizations work for nested
219 # queries. Ticket #587.
223 SELECT max(x) FROM t1;
228 SELECT * FROM (SELECT max(x) FROM t1);
233 SELECT min(x) FROM t1;
238 SELECT * FROM (SELECT min(x) FROM t1);
242 # Make sure min(x) and max(x) work correctly when the datatype is
243 # TEXT instead of NUMERIC. Ticket #623.
247 CREATE TABLE t4(a TEXT);
248 INSERT INTO t4 VALUES('1234');
249 INSERT INTO t4 VALUES('234');
250 INSERT INTO t4 VALUES('34');
251 SELECT min(a), max(a) FROM t4;
256 CREATE TABLE t5(a INTEGER);
257 INSERT INTO t5 VALUES('1234');
258 INSERT INTO t5 VALUES('234');
259 INSERT INTO t5 VALUES('34');
260 SELECT min(a), max(a) FROM t5;
264 # Ticket #658: Test the min()/max() optimization when the FROM clause
269 SELECT max(rowid) FROM (
270 SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
276 SELECT max(rowid) FROM (
277 SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
282 # If there is a NULL in an aggregate max() or min(), ignore it. An
283 # aggregate min() or max() will only return NULL if all values are NULL.
285 do_test minmax-10.1 {
288 INSERT INTO t6 VALUES(1);
289 INSERT INTO t6 VALUES(2);
290 INSERT INTO t6 VALUES(NULL);
291 SELECT coalesce(min(x),-1) FROM t6;
294 do_test minmax-10.2 {
296 SELECT max(x) FROM t6;
299 do_test minmax-10.3 {
301 CREATE INDEX i6 ON t6(x);
302 SELECT coalesce(min(x),-1) FROM t6;
305 do_test minmax-10.4 {
307 SELECT max(x) FROM t6;
310 do_test minmax-10.5 {
312 DELETE FROM t6 WHERE x NOT NULL;
313 SELECT count(*) FROM t6;
316 do_test minmax-10.6 {
318 SELECT count(x) FROM t6;
321 do_test minmax-10.7 {
323 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
326 do_test minmax-10.8 {
328 SELECT min(x), max(x) FROM t6;
331 do_test minmax-10.9 {
333 INSERT INTO t6 SELECT * FROM t6;
334 INSERT INTO t6 SELECT * FROM t6;
335 INSERT INTO t6 SELECT * FROM t6;
336 INSERT INTO t6 SELECT * FROM t6;
337 INSERT INTO t6 SELECT * FROM t6;
338 INSERT INTO t6 SELECT * FROM t6;
339 INSERT INTO t6 SELECT * FROM t6;
340 INSERT INTO t6 SELECT * FROM t6;
341 INSERT INTO t6 SELECT * FROM t6;
342 INSERT INTO t6 SELECT * FROM t6;
343 SELECT count(*) FROM t6;
346 do_test minmax-10.10 {
348 SELECT count(x) FROM t6;
351 do_test minmax-10.11 {
353 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
356 do_test minmax-10.12 {
358 SELECT min(x), max(x) FROM t6;