3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing SELECT statements that contain
13 # aggregate min() and max() functions and which are handled as
16 # $Id: minmax.test,v 1.21 2008/07/08 18:05:26 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
24 CREATE TABLE t1(x, y);
25 INSERT INTO t1 VALUES(1,1);
26 INSERT INTO t1 VALUES(2,2);
27 INSERT INTO t1 VALUES(3,2);
28 INSERT INTO t1 VALUES(4,3);
29 INSERT INTO t1 VALUES(5,3);
30 INSERT INTO t1 VALUES(6,3);
31 INSERT INTO t1 VALUES(7,3);
32 INSERT INTO t1 VALUES(8,4);
33 INSERT INTO t1 VALUES(9,4);
34 INSERT INTO t1 VALUES(10,4);
35 INSERT INTO t1 VALUES(11,4);
36 INSERT INTO t1 VALUES(12,4);
37 INSERT INTO t1 VALUES(13,4);
38 INSERT INTO t1 VALUES(14,4);
39 INSERT INTO t1 VALUES(15,4);
40 INSERT INTO t1 VALUES(16,5);
41 INSERT INTO t1 VALUES(17,5);
42 INSERT INTO t1 VALUES(18,5);
43 INSERT INTO t1 VALUES(19,5);
44 INSERT INTO t1 VALUES(20,5);
46 SELECT DISTINCT y FROM t1 ORDER BY y;
51 set sqlite_search_count 0
52 execsql {SELECT min(x) FROM t1}
55 set sqlite_search_count
58 set sqlite_search_count 0
59 execsql {SELECT max(x) FROM t1}
62 set sqlite_search_count
65 execsql {CREATE INDEX t1i1 ON t1(x)}
66 set sqlite_search_count 0
67 execsql {SELECT min(x) FROM t1}
70 set sqlite_search_count
73 set sqlite_search_count 0
74 execsql {SELECT max(x) FROM t1}
77 set sqlite_search_count
80 set sqlite_search_count 0
81 execsql {SELECT max(y) FROM t1}
84 set sqlite_search_count
88 execsql {SELECT min(x) FROM t1 WHERE x=5}
91 execsql {SELECT min(x) FROM t1 WHERE x>=5}
94 execsql {SELECT min(x) FROM t1 WHERE x>=4.5}
97 execsql {SELECT min(x) FROM t1 WHERE x<4.5}
102 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
103 INSERT INTO t2 SELECT * FROM t1;
105 set sqlite_search_count 0
106 execsql {SELECT min(a) FROM t2}
109 set sqlite_search_count
112 set sqlite_search_count 0
113 execsql {SELECT max(a) FROM t2}
116 set sqlite_search_count
121 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
123 db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
124 execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
126 set sqlite_search_count 0
127 execsql {SELECT max(a) FROM t2}
130 set sqlite_search_count
134 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
136 db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
137 execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
139 set sqlite_search_count 0
141 execsql { SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2) }
143 execsql { SELECT b FROM t2 WHERE a=max_a_t2() }
147 set sqlite_search_count
150 ifcapable {compound && subquery} {
153 SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
154 (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
159 SELECT y, coalesce(sum(x),0) FROM
160 (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
161 GROUP BY y ORDER BY y;
163 } {1 1 2 5 3 22 4 92 5 90 6 0}
166 SELECT y, count(x), count(*) FROM
167 (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
168 GROUP BY y ORDER BY y;
170 } {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
171 } ;# ifcapable compound
173 # Make sure the min(x) and max(x) optimizations work on empty tables
174 # including empty tables with indices. Ticket #296.
178 CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
179 SELECT coalesce(min(x),999) FROM t3;
184 SELECT coalesce(min(rowid),999) FROM t3;
189 SELECT coalesce(max(x),999) FROM t3;
194 SELECT coalesce(max(rowid),999) FROM t3;
199 SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
203 # Make sure the min(x) and max(x) optimizations work when there
204 # is a LIMIT clause. Ticket #396.
208 SELECT min(a) FROM t2 LIMIT 1
213 SELECT max(a) FROM t2 LIMIT 3
218 SELECT min(a) FROM t2 LIMIT 0,100
223 SELECT max(a) FROM t2 LIMIT 1,100
228 SELECT min(x) FROM t3 LIMIT 1
233 SELECT max(x) FROM t3 LIMIT 0
238 SELECT max(a) FROM t2 LIMIT 0
242 # Make sure the max(x) and min(x) optimizations work for nested
243 # queries. Ticket #587.
247 SELECT max(x) FROM t1;
253 SELECT * FROM (SELECT max(x) FROM t1);
259 SELECT min(x) FROM t1;
265 SELECT * FROM (SELECT min(x) FROM t1);
270 # Make sure min(x) and max(x) work correctly when the datatype is
271 # TEXT instead of NUMERIC. Ticket #623.
275 CREATE TABLE t4(a TEXT);
276 INSERT INTO t4 VALUES('1234');
277 INSERT INTO t4 VALUES('234');
278 INSERT INTO t4 VALUES('34');
279 SELECT min(a), max(a) FROM t4;
284 CREATE TABLE t5(a INTEGER);
285 INSERT INTO t5 VALUES('1234');
286 INSERT INTO t5 VALUES('234');
287 INSERT INTO t5 VALUES('34');
288 SELECT min(a), max(a) FROM t5;
292 # Ticket #658: Test the min()/max() optimization when the FROM clause
295 ifcapable {compound && subquery} {
298 SELECT max(rowid) FROM (
299 SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
305 SELECT max(rowid) FROM (
306 SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
310 } ;# ifcapable compound&&subquery
312 # If there is a NULL in an aggregate max() or min(), ignore it. An
313 # aggregate min() or max() will only return NULL if all values are NULL.
315 do_test minmax-10.1 {
318 INSERT INTO t6 VALUES(1);
319 INSERT INTO t6 VALUES(2);
320 INSERT INTO t6 VALUES(NULL);
321 SELECT coalesce(min(x),-1) FROM t6;
324 do_test minmax-10.2 {
326 SELECT max(x) FROM t6;
329 do_test minmax-10.3 {
331 CREATE INDEX i6 ON t6(x);
332 SELECT coalesce(min(x),-1) FROM t6;
335 do_test minmax-10.4 {
337 SELECT max(x) FROM t6;
340 do_test minmax-10.5 {
342 DELETE FROM t6 WHERE x NOT NULL;
343 SELECT count(*) FROM t6;
346 do_test minmax-10.6 {
348 SELECT count(x) FROM t6;
352 do_test minmax-10.7 {
354 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
358 do_test minmax-10.8 {
360 SELECT min(x), max(x) FROM t6;
363 do_test minmax-10.9 {
365 INSERT INTO t6 SELECT * FROM t6;
366 INSERT INTO t6 SELECT * FROM t6;
367 INSERT INTO t6 SELECT * FROM t6;
368 INSERT INTO t6 SELECT * FROM t6;
369 INSERT INTO t6 SELECT * FROM t6;
370 INSERT INTO t6 SELECT * FROM t6;
371 INSERT INTO t6 SELECT * FROM t6;
372 INSERT INTO t6 SELECT * FROM t6;
373 INSERT INTO t6 SELECT * FROM t6;
374 INSERT INTO t6 SELECT * FROM t6;
375 SELECT count(*) FROM t6;
378 do_test minmax-10.10 {
380 SELECT count(x) FROM t6;
384 do_test minmax-10.11 {
386 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
390 do_test minmax-10.12 {
392 SELECT min(x), max(x) FROM t6;
397 do_test minmax-11.1 {
399 CREATE INDEX t1i2 ON t1(y,x);
400 SELECT min(x) FROM t1 WHERE y=5;
403 do_test minmax-11.2 {
405 SELECT max(x) FROM t1 WHERE y=5;
408 do_test minmax-11.3 {
410 SELECT min(x) FROM t1 WHERE y=6;
413 do_test minmax-11.4 {
415 SELECT max(x) FROM t1 WHERE y=6;
418 do_test minmax-11.5 {
420 SELECT min(x) FROM t1 WHERE y=1;
423 do_test minmax-11.6 {
425 SELECT max(x) FROM t1 WHERE y=1;
428 do_test minmax-11.7 {
430 SELECT min(x) FROM t1 WHERE y=0;
433 do_test minmax-11.8 {
435 SELECT max(x) FROM t1 WHERE y=0;
438 do_test minmax-11.9 {
440 SELECT min(x) FROM t1 WHERE y=5 AND x>=17.5;
443 do_test minmax-11.10 {
445 SELECT max(x) FROM t1 WHERE y=5 AND x>=17.5;
449 do_test minmax-12.1 {
451 CREATE TABLE t7(a,b,c);
452 INSERT INTO t7 SELECT y, x, x*y FROM t1;
453 INSERT INTO t7 SELECT y, x, x*y+1000 FROM t1;
454 CREATE INDEX t7i1 ON t7(a,b,c);
455 SELECT min(a) FROM t7;
458 do_test minmax-12.2 {
460 SELECT max(a) FROM t7;
463 do_test minmax-12.3 {
465 SELECT max(a) FROM t7 WHERE a=5;
468 do_test minmax-12.4 {
470 SELECT min(b) FROM t7 WHERE a=5;
473 do_test minmax-12.5 {
475 SELECT max(b) FROM t7 WHERE a=5;
478 do_test minmax-12.6 {
480 SELECT min(b) FROM t7 WHERE a=4;
483 do_test minmax-12.7 {
485 SELECT max(b) FROM t7 WHERE a=4;
488 do_test minmax-12.8 {
490 SELECT min(c) FROM t7 WHERE a=4 AND b=10;
493 do_test minmax-12.9 {
495 SELECT max(c) FROM t7 WHERE a=4 AND b=10;
498 do_test minmax-12.10 {
500 SELECT min(rowid) FROM t7;
503 do_test minmax-12.11 {
505 SELECT max(rowid) FROM t7;
508 do_test minmax-12.12 {
510 SELECT min(rowid) FROM t7 WHERE a=3;
513 do_test minmax-12.13 {
515 SELECT max(rowid) FROM t7 WHERE a=3;
518 do_test minmax-12.14 {
520 SELECT min(rowid) FROM t7 WHERE a=3 AND b=5;
523 do_test minmax-12.15 {
525 SELECT max(rowid) FROM t7 WHERE a=3 AND b=5;
528 do_test minmax-12.16 {
530 SELECT min(rowid) FROM t7 WHERE a=3 AND b=5 AND c=1015;
533 do_test minmax-12.17 {
535 SELECT max(rowid) FROM t7 WHERE a=3 AND b=5 AND c=15;