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) AS yy FROM t4 UNION SELECT max(rowid) FROM t5
304 SELECT max(yy) FROM (
305 SELECT max(rowid) AS yy FROM t4 UNION SELECT max(rowid) FROM t5
311 SELECT max(yy) FROM (
312 SELECT max(rowid) AS yy FROM t4 EXCEPT SELECT max(rowid) FROM t5
316 } ;# ifcapable compound&&subquery
318 # If there is a NULL in an aggregate max() or min(), ignore it. An
319 # aggregate min() or max() will only return NULL if all values are NULL.
321 do_test minmax-10.1 {
324 INSERT INTO t6 VALUES(1);
325 INSERT INTO t6 VALUES(2);
326 INSERT INTO t6 VALUES(NULL);
327 SELECT coalesce(min(x),-1) FROM t6;
330 do_test minmax-10.2 {
332 SELECT max(x) FROM t6;
335 do_test minmax-10.3 {
337 CREATE INDEX i6 ON t6(x);
338 SELECT coalesce(min(x),-1) FROM t6;
341 do_test minmax-10.4 {
343 SELECT max(x) FROM t6;
346 do_test minmax-10.5 {
348 DELETE FROM t6 WHERE x NOT NULL;
349 SELECT count(*) FROM t6;
352 do_test minmax-10.6 {
354 SELECT count(x) FROM t6;
358 do_test minmax-10.7 {
360 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
364 do_test minmax-10.8 {
366 SELECT min(x), max(x) FROM t6;
369 do_test minmax-10.9 {
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 INSERT INTO t6 SELECT * FROM t6;
377 INSERT INTO t6 SELECT * FROM t6;
378 INSERT INTO t6 SELECT * FROM t6;
379 INSERT INTO t6 SELECT * FROM t6;
380 INSERT INTO t6 SELECT * FROM t6;
381 SELECT count(*) FROM t6;
384 do_test minmax-10.10 {
386 SELECT count(x) FROM t6;
390 do_test minmax-10.11 {
392 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
396 do_test minmax-10.12 {
398 SELECT min(x), max(x) FROM t6;
403 do_test minmax-11.1 {
405 CREATE INDEX t1i2 ON t1(y,x);
406 SELECT min(x) FROM t1 WHERE y=5;
409 do_test minmax-11.2 {
411 SELECT max(x) FROM t1 WHERE y=5;
414 do_test minmax-11.3 {
416 SELECT min(x) FROM t1 WHERE y=6;
419 do_test minmax-11.4 {
421 SELECT max(x) FROM t1 WHERE y=6;
424 do_test minmax-11.5 {
426 SELECT min(x) FROM t1 WHERE y=1;
429 do_test minmax-11.6 {
431 SELECT max(x) FROM t1 WHERE y=1;
434 do_test minmax-11.7 {
436 SELECT min(x) FROM t1 WHERE y=0;
439 do_test minmax-11.8 {
441 SELECT max(x) FROM t1 WHERE y=0;
444 do_test minmax-11.9 {
446 SELECT min(x) FROM t1 WHERE y=5 AND x>=17.5;
449 do_test minmax-11.10 {
451 SELECT max(x) FROM t1 WHERE y=5 AND x>=17.5;
455 do_test minmax-12.1 {
457 CREATE TABLE t7(a,b,c);
458 INSERT INTO t7 SELECT y, x, x*y FROM t1;
459 INSERT INTO t7 SELECT y, x, x*y+1000 FROM t1;
460 CREATE INDEX t7i1 ON t7(a,b,c);
461 SELECT min(a) FROM t7;
464 do_test minmax-12.2 {
466 SELECT max(a) FROM t7;
469 do_test minmax-12.3 {
471 SELECT max(a) FROM t7 WHERE a=5;
474 do_test minmax-12.4 {
476 SELECT min(b) FROM t7 WHERE a=5;
479 do_test minmax-12.5 {
481 SELECT max(b) FROM t7 WHERE a=5;
484 do_test minmax-12.6 {
486 SELECT min(b) FROM t7 WHERE a=4;
489 do_test minmax-12.7 {
491 SELECT max(b) FROM t7 WHERE a=4;
494 do_test minmax-12.8 {
496 SELECT min(c) FROM t7 WHERE a=4 AND b=10;
499 do_test minmax-12.9 {
501 SELECT max(c) FROM t7 WHERE a=4 AND b=10;
504 do_test minmax-12.10 {
506 SELECT min(rowid) FROM t7;
509 do_test minmax-12.11 {
511 SELECT max(rowid) FROM t7;
514 do_test minmax-12.12 {
516 SELECT min(rowid) FROM t7 WHERE a=3;
519 do_test minmax-12.13 {
521 SELECT max(rowid) FROM t7 WHERE a=3;
524 do_test minmax-12.14 {
526 SELECT min(rowid) FROM t7 WHERE a=3 AND b=5;
529 do_test minmax-12.15 {
531 SELECT max(rowid) FROM t7 WHERE a=3 AND b=5;
534 do_test minmax-12.16 {
536 SELECT min(rowid) FROM t7 WHERE a=3 AND b=5 AND c=1015;
539 do_test minmax-12.17 {
541 SELECT max(rowid) FROM t7 WHERE a=3 AND b=5 AND c=15;
545 #-------------------------------------------------------------------------
548 proc do_test_13 {op name sql1 sql2 res} {
549 set ::sqlite_search_count 0
550 uplevel [list do_execsql_test $name.1 $sql1 $res]
551 set a $::sqlite_search_count
553 set ::sqlite_search_count 0
554 uplevel [list do_execsql_test $name.2 $sql2 $res]
555 set b $::sqlite_search_count
557 uplevel [list do_test $name.3 [list expr "$a $op $b"] 1]
560 # Run a test named $name. Check that SQL statements $sql1 and $sql2 both
561 # return the same result, but that $sql2 increments the $sqlite_search_count
562 # variable more often (indicating that it is visiting more rows to determine
565 proc do_test_13_opt {name sql1 sql2 res} {
566 uplevel [list do_test_13 < $name $sql1 $sql2 $res]
569 # Like [do_test_13_noopt], except this time check that the $sqlite_search_count
570 # variable is incremented the same number of times by both SQL statements.
572 proc do_test_13_noopt {name sql1 sql2 res} {
573 uplevel [list do_test_13 == $name $sql1 $sql2 $res]
576 do_execsql_test 13.1 {
577 CREATE TABLE t1(a, b, c);
578 INSERT INTO t1 VALUES('a', 1, 1);
579 INSERT INTO t1 VALUES('b', 6, 6);
580 INSERT INTO t1 VALUES('c', 5, 5);
581 INSERT INTO t1 VALUES('a', 4, 4);
582 INSERT INTO t1 VALUES('a', 5, 5);
583 INSERT INTO t1 VALUES('c', 6, 6);
584 INSERT INTO t1 VALUES('b', 4, 4);
585 INSERT INTO t1 VALUES('c', 7, 7);
586 INSERT INTO t1 VALUES('b', 2, 2);
587 INSERT INTO t1 VALUES('b', 3, 3);
588 INSERT INTO t1 VALUES('a', 3, 3);
589 INSERT INTO t1 VALUES('b', 5, 5);
590 INSERT INTO t1 VALUES('c', 4, 4);
591 INSERT INTO t1 VALUES('c', 3, 3);
592 INSERT INTO t1 VALUES('a', 2, 2);
593 SELECT * FROM t1 ORDER BY a, b, c;
594 } {a 1 1 a 2 2 a 3 3 a 4 4 a 5 5
595 b 2 2 b 3 3 b 4 4 b 5 5 b 6 6
596 c 3 3 c 4 4 c 5 5 c 6 6 c 7 7
598 do_execsql_test 13.2 { CREATE INDEX i1 ON t1(a, b, c) }
600 do_test_13_opt 13.3 {
601 SELECT min(b) FROM t1 WHERE a='b'
603 SELECT min(c) FROM t1 WHERE a='b'
606 do_test_13_opt 13.4 {
607 SELECT a, min(b) FROM t1 WHERE a='b'
609 SELECT a, min(c) FROM t1 WHERE a='b'
612 do_test_13_opt 13.4 {
613 SELECT a||c, max(b)+4 FROM t1 WHERE a='c'
615 SELECT a||c, max(c)+4 FROM t1 WHERE a='c'
618 do_test_13_noopt 13.5 {
619 SELECT a||c, max(b+1) FROM t1 WHERE a='c'
621 SELECT a||c, max(c+1) FROM t1 WHERE a='c'
624 do_test_13_noopt 13.6 {
625 SELECT count(b) FROM t1 WHERE a='c'
627 SELECT count(c) FROM t1 WHERE a='c'
630 do_test_13_noopt 13.7 {
631 SELECT min(b), count(b) FROM t1 WHERE a='a';
633 SELECT min(c), count(c) FROM t1 WHERE a='a';
636 # 2016-07-26. https://www.sqlite.org/src/info/a0bac8b3c3d1bb75
637 # Incorrect result on a min() query after a CREATE INDEX.
639 do_execsql_test 14.1 {
640 CREATE TABLE t14(a INTEGER, b INTEGER);
641 INSERT INTO t14(a,b) VALUES(100,2),(200,2),(300,2),(400,1),(500,2);
642 SELECT min(a) FROM t14 WHERE b='2' AND a>'50';
644 do_execsql_test 14.2 {
645 CREATE INDEX t14ba ON t14(b,a);
646 SELECT min(a) FROM t14 WHERE b='2' AND a>'50';
649 # 2021-08-21. https://sqlite.org/forum/forumpost/cfcb4b461d
652 do_execsql_test 15.1 {
656 INSERT INTO t1 VALUES(0);
657 INSERT INTO t2 VALUES(5);
658 SELECT MIN((SELECT b FROM t2 UNION SELECT x FROM (SELECT x FROM (SELECT 1 AS x WHERE t1.a=1) UNION ALL SELECT c FROM t3))) FROM t1;