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
20 set ::testprefix minmax
25 CREATE TABLE t1(x, y);
26 INSERT INTO t1 VALUES(1,1);
27 INSERT INTO t1 VALUES(2,2);
28 INSERT INTO t1 VALUES(3,2);
29 INSERT INTO t1 VALUES(4,3);
30 INSERT INTO t1 VALUES(5,3);
31 INSERT INTO t1 VALUES(6,3);
32 INSERT INTO t1 VALUES(7,3);
33 INSERT INTO t1 VALUES(8,4);
34 INSERT INTO t1 VALUES(9,4);
35 INSERT INTO t1 VALUES(10,4);
36 INSERT INTO t1 VALUES(11,4);
37 INSERT INTO t1 VALUES(12,4);
38 INSERT INTO t1 VALUES(13,4);
39 INSERT INTO t1 VALUES(14,4);
40 INSERT INTO t1 VALUES(15,4);
41 INSERT INTO t1 VALUES(16,5);
42 INSERT INTO t1 VALUES(17,5);
43 INSERT INTO t1 VALUES(18,5);
44 INSERT INTO t1 VALUES(19,5);
45 INSERT INTO t1 VALUES(20,5);
47 SELECT DISTINCT y FROM t1 ORDER BY y;
52 set sqlite_search_count 0
53 execsql {SELECT min(x) FROM t1}
56 set sqlite_search_count
59 set sqlite_search_count 0
60 execsql {SELECT max(x) FROM t1}
63 set sqlite_search_count
66 execsql {CREATE INDEX t1i1 ON t1(x)}
67 set sqlite_search_count 0
68 execsql {SELECT min(x) FROM t1}
71 set sqlite_search_count
74 set sqlite_search_count 0
75 execsql {SELECT max(x) FROM t1}
78 set sqlite_search_count
81 set sqlite_search_count 0
82 execsql {SELECT max(y) FROM t1}
85 set sqlite_search_count
89 execsql {SELECT min(x) FROM t1 WHERE x=5}
92 execsql {SELECT min(x) FROM t1 WHERE x>=5}
95 execsql {SELECT min(x) FROM t1 WHERE x>=4.5}
98 execsql {SELECT min(x) FROM t1 WHERE x<4.5}
103 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
104 INSERT INTO t2 SELECT * FROM t1;
106 set sqlite_search_count 0
107 execsql {SELECT min(a) FROM t2}
110 set sqlite_search_count
113 set sqlite_search_count 0
114 execsql {SELECT max(a) FROM t2}
117 set sqlite_search_count
122 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
124 db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
125 execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
127 set sqlite_search_count 0
128 execsql {SELECT max(a) FROM t2}
131 set sqlite_search_count
135 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
137 db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
138 execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
140 set sqlite_search_count 0
142 execsql { SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2) }
144 execsql { SELECT b FROM t2 WHERE a=max_a_t2() }
148 set sqlite_search_count
151 ifcapable {compound && subquery} {
154 SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
155 (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
160 SELECT y, coalesce(sum(x),0) FROM
161 (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
162 GROUP BY y ORDER BY y;
164 } {1 1 2 5 3 22 4 92 5 90 6 0}
167 SELECT y, count(x), count(*) FROM
168 (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
169 GROUP BY y ORDER BY y;
171 } {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
172 } ;# ifcapable compound
174 # Make sure the min(x) and max(x) optimizations work on empty tables
175 # including empty tables with indices. Ticket #296.
179 CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
180 SELECT coalesce(min(x),999) FROM t3;
185 SELECT coalesce(min(rowid),999) FROM t3;
190 SELECT coalesce(max(x),999) FROM t3;
195 SELECT coalesce(max(rowid),999) FROM t3;
200 SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
204 # Make sure the min(x) and max(x) optimizations work when there
205 # is a LIMIT clause. Ticket #396.
209 SELECT min(a) FROM t2 LIMIT 1
214 SELECT max(a) FROM t2 LIMIT 3
219 SELECT min(a) FROM t2 LIMIT 0,100
224 SELECT max(a) FROM t2 LIMIT 1,100
229 SELECT min(x) FROM t3 LIMIT 1
234 SELECT max(x) FROM t3 LIMIT 0
239 SELECT max(a) FROM t2 LIMIT 0
243 # Make sure the max(x) and min(x) optimizations work for nested
244 # queries. Ticket #587.
248 SELECT max(x) FROM t1;
254 SELECT * FROM (SELECT max(x) FROM t1);
260 SELECT min(x) FROM t1;
266 SELECT * FROM (SELECT min(x) FROM t1);
271 # Make sure min(x) and max(x) work correctly when the datatype is
272 # TEXT instead of NUMERIC. Ticket #623.
276 CREATE TABLE t4(a TEXT);
277 INSERT INTO t4 VALUES('1234');
278 INSERT INTO t4 VALUES('234');
279 INSERT INTO t4 VALUES('34');
280 SELECT min(a), max(a) FROM t4;
285 CREATE TABLE t5(a INTEGER);
286 INSERT INTO t5 VALUES('1234');
287 INSERT INTO t5 VALUES('234');
288 INSERT INTO t5 VALUES('34');
289 SELECT min(a), max(a) FROM t5;
293 # Ticket #658: Test the min()/max() optimization when the FROM clause
296 ifcapable {compound && subquery} {
299 SELECT max(rowid) FROM (
300 SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
306 SELECT max(rowid) FROM (
307 SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
311 } ;# ifcapable compound&&subquery
313 # If there is a NULL in an aggregate max() or min(), ignore it. An
314 # aggregate min() or max() will only return NULL if all values are NULL.
316 do_test minmax-10.1 {
319 INSERT INTO t6 VALUES(1);
320 INSERT INTO t6 VALUES(2);
321 INSERT INTO t6 VALUES(NULL);
322 SELECT coalesce(min(x),-1) FROM t6;
325 do_test minmax-10.2 {
327 SELECT max(x) FROM t6;
330 do_test minmax-10.3 {
332 CREATE INDEX i6 ON t6(x);
333 SELECT coalesce(min(x),-1) FROM t6;
336 do_test minmax-10.4 {
338 SELECT max(x) FROM t6;
341 do_test minmax-10.5 {
343 DELETE FROM t6 WHERE x NOT NULL;
344 SELECT count(*) FROM t6;
347 do_test minmax-10.6 {
349 SELECT count(x) FROM t6;
353 do_test minmax-10.7 {
355 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
359 do_test minmax-10.8 {
361 SELECT min(x), max(x) FROM t6;
364 do_test minmax-10.9 {
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 INSERT INTO t6 SELECT * FROM t6;
376 SELECT count(*) FROM t6;
379 do_test minmax-10.10 {
381 SELECT count(x) FROM t6;
385 do_test minmax-10.11 {
387 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
391 do_test minmax-10.12 {
393 SELECT min(x), max(x) FROM t6;
398 do_test minmax-11.1 {
400 CREATE INDEX t1i2 ON t1(y,x);
401 SELECT min(x) FROM t1 WHERE y=5;
404 do_test minmax-11.2 {
406 SELECT max(x) FROM t1 WHERE y=5;
409 do_test minmax-11.3 {
411 SELECT min(x) FROM t1 WHERE y=6;
414 do_test minmax-11.4 {
416 SELECT max(x) FROM t1 WHERE y=6;
419 do_test minmax-11.5 {
421 SELECT min(x) FROM t1 WHERE y=1;
424 do_test minmax-11.6 {
426 SELECT max(x) FROM t1 WHERE y=1;
429 do_test minmax-11.7 {
431 SELECT min(x) FROM t1 WHERE y=0;
434 do_test minmax-11.8 {
436 SELECT max(x) FROM t1 WHERE y=0;
439 do_test minmax-11.9 {
441 SELECT min(x) FROM t1 WHERE y=5 AND x>=17.5;
444 do_test minmax-11.10 {
446 SELECT max(x) FROM t1 WHERE y=5 AND x>=17.5;
450 do_test minmax-12.1 {
452 CREATE TABLE t7(a,b,c);
453 INSERT INTO t7 SELECT y, x, x*y FROM t1;
454 INSERT INTO t7 SELECT y, x, x*y+1000 FROM t1;
455 CREATE INDEX t7i1 ON t7(a,b,c);
456 SELECT min(a) FROM t7;
459 do_test minmax-12.2 {
461 SELECT max(a) FROM t7;
464 do_test minmax-12.3 {
466 SELECT max(a) FROM t7 WHERE a=5;
469 do_test minmax-12.4 {
471 SELECT min(b) FROM t7 WHERE a=5;
474 do_test minmax-12.5 {
476 SELECT max(b) FROM t7 WHERE a=5;
479 do_test minmax-12.6 {
481 SELECT min(b) FROM t7 WHERE a=4;
484 do_test minmax-12.7 {
486 SELECT max(b) FROM t7 WHERE a=4;
489 do_test minmax-12.8 {
491 SELECT min(c) FROM t7 WHERE a=4 AND b=10;
494 do_test minmax-12.9 {
496 SELECT max(c) FROM t7 WHERE a=4 AND b=10;
499 do_test minmax-12.10 {
501 SELECT min(rowid) FROM t7;
504 do_test minmax-12.11 {
506 SELECT max(rowid) FROM t7;
509 do_test minmax-12.12 {
511 SELECT min(rowid) FROM t7 WHERE a=3;
514 do_test minmax-12.13 {
516 SELECT max(rowid) FROM t7 WHERE a=3;
519 do_test minmax-12.14 {
521 SELECT min(rowid) FROM t7 WHERE a=3 AND b=5;
524 do_test minmax-12.15 {
526 SELECT max(rowid) FROM t7 WHERE a=3 AND b=5;
529 do_test minmax-12.16 {
531 SELECT min(rowid) FROM t7 WHERE a=3 AND b=5 AND c=1015;
534 do_test minmax-12.17 {
536 SELECT max(rowid) FROM t7 WHERE a=3 AND b=5 AND c=15;
540 #-------------------------------------------------------------------------
543 proc do_test_13 {op name sql1 sql2 res} {
544 set ::sqlite_search_count 0
545 uplevel [list do_execsql_test $name.1 $sql1 $res]
546 set a $::sqlite_search_count
548 set ::sqlite_search_count 0
549 uplevel [list do_execsql_test $name.2 $sql2 $res]
550 set b $::sqlite_search_count
552 uplevel [list do_test $name.3 [list expr "$a $op $b"] 1]
555 # Run a test named $name. Check that SQL statements $sql1 and $sql2 both
556 # return the same result, but that $sql2 increments the $sqlite_search_count
557 # variable more often (indicating that it is visiting more rows to determine
560 proc do_test_13_opt {name sql1 sql2 res} {
561 uplevel [list do_test_13 < $name $sql1 $sql2 $res]
564 # Like [do_test_13_noopt], except this time check that the $sqlite_search_count
565 # variable is incremented the same number of times by both SQL statements.
567 proc do_test_13_noopt {name sql1 sql2 res} {
568 uplevel [list do_test_13 == $name $sql1 $sql2 $res]
571 do_execsql_test 13.1 {
572 CREATE TABLE t1(a, b, c);
573 INSERT INTO t1 VALUES('a', 1, 1);
574 INSERT INTO t1 VALUES('b', 6, 6);
575 INSERT INTO t1 VALUES('c', 5, 5);
576 INSERT INTO t1 VALUES('a', 4, 4);
577 INSERT INTO t1 VALUES('a', 5, 5);
578 INSERT INTO t1 VALUES('c', 6, 6);
579 INSERT INTO t1 VALUES('b', 4, 4);
580 INSERT INTO t1 VALUES('c', 7, 7);
581 INSERT INTO t1 VALUES('b', 2, 2);
582 INSERT INTO t1 VALUES('b', 3, 3);
583 INSERT INTO t1 VALUES('a', 3, 3);
584 INSERT INTO t1 VALUES('b', 5, 5);
585 INSERT INTO t1 VALUES('c', 4, 4);
586 INSERT INTO t1 VALUES('c', 3, 3);
587 INSERT INTO t1 VALUES('a', 2, 2);
588 SELECT * FROM t1 ORDER BY a, b, c;
589 } {a 1 1 a 2 2 a 3 3 a 4 4 a 5 5
590 b 2 2 b 3 3 b 4 4 b 5 5 b 6 6
591 c 3 3 c 4 4 c 5 5 c 6 6 c 7 7
593 do_execsql_test 13.2 { CREATE INDEX i1 ON t1(a, b, c) }
595 do_test_13_opt 13.3 {
596 SELECT min(b) FROM t1 WHERE a='b'
598 SELECT min(c) FROM t1 WHERE a='b'
601 do_test_13_opt 13.4 {
602 SELECT a, min(b) FROM t1 WHERE a='b'
604 SELECT a, min(c) FROM t1 WHERE a='b'
607 do_test_13_opt 13.4 {
608 SELECT a||c, max(b)+4 FROM t1 WHERE a='c'
610 SELECT a||c, max(c)+4 FROM t1 WHERE a='c'
613 do_test_13_noopt 13.5 {
614 SELECT a||c, max(b+1) FROM t1 WHERE a='c'
616 SELECT a||c, max(c+1) FROM t1 WHERE a='c'
619 do_test_13_noopt 13.6 {
620 SELECT count(b) FROM t1 WHERE a='c'
622 SELECT count(c) FROM t1 WHERE a='c'
625 do_test_13_noopt 13.7 {
626 SELECT min(b), count(b) FROM t1 WHERE a='a';
628 SELECT min(c), count(c) FROM t1 WHERE a='a';