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
14 # as a special case. This file makes sure that the min/max
15 # optimization works right in the presence of descending
16 # indices. Ticket #2514.
18 # $Id: minmax2.test,v 1.2 2008/01/05 17:39:30 danielk1977 Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
25 PRAGMA legacy_file_format=0;
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 DESC)}
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}
86 do_test minmax2-1.10 {
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
101 do_test minmax2-2.2 {
102 set sqlite_search_count 0
103 execsql {SELECT max(a) FROM t2}
105 do_test minmax2-2.3 {
106 set sqlite_search_count
109 do_test minmax2-3.0 {
111 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
113 db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
114 execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
116 set sqlite_search_count 0
117 execsql {SELECT max(a) FROM t2}
119 do_test minmax2-3.1 {
120 set sqlite_search_count
122 do_test minmax2-3.2 {
124 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)}
126 db function max_a_t2 {execsql {SELECT max(a) FROM t2}}
127 execsql {INSERT INTO t2 VALUES(max_a_t2()+1,999)}
129 set sqlite_search_count 0
131 execsql { SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2) }
133 execsql { SELECT b FROM t2 WHERE a=max_a_t2() }
136 do_test minmax2-3.3 {
137 set sqlite_search_count
140 ifcapable {compound && subquery} {
141 do_test minmax2-4.1 {
143 SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM
144 (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y')
147 do_test minmax2-4.2 {
149 SELECT y, coalesce(sum(x),0) FROM
150 (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
151 GROUP BY y ORDER BY y;
153 } {1 1 2 5 3 22 4 92 5 90 6 0}
154 do_test minmax2-4.3 {
156 SELECT y, count(x), count(*) FROM
157 (SELECT null AS x, y+1 AS y FROM t1 UNION SELECT * FROM t1)
158 GROUP BY y ORDER BY y;
160 } {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1}
161 } ;# ifcapable compound
163 # Make sure the min(x) and max(x) optimizations work on empty tables
164 # including empty tables with indices. Ticket #296.
166 do_test minmax2-5.1 {
168 CREATE TABLE t3(x INTEGER UNIQUE NOT NULL);
169 SELECT coalesce(min(x),999) FROM t3;
172 do_test minmax2-5.2 {
174 SELECT coalesce(min(rowid),999) FROM t3;
177 do_test minmax2-5.3 {
179 SELECT coalesce(max(x),999) FROM t3;
182 do_test minmax2-5.4 {
184 SELECT coalesce(max(rowid),999) FROM t3;
187 do_test minmax2-5.5 {
189 SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25;
193 # Make sure the min(x) and max(x) optimizations work when there
194 # is a LIMIT clause. Ticket #396.
196 do_test minmax2-6.1 {
198 SELECT min(a) FROM t2 LIMIT 1
201 do_test minmax2-6.2 {
203 SELECT max(a) FROM t2 LIMIT 3
206 do_test minmax2-6.3 {
208 SELECT min(a) FROM t2 LIMIT 0,100
211 do_test minmax2-6.4 {
213 SELECT max(a) FROM t2 LIMIT 1,100
216 do_test minmax2-6.5 {
218 SELECT min(x) FROM t3 LIMIT 1
221 do_test minmax2-6.6 {
223 SELECT max(x) FROM t3 LIMIT 0
226 do_test minmax2-6.7 {
228 SELECT max(a) FROM t2 LIMIT 0
232 # Make sure the max(x) and min(x) optimizations work for nested
233 # queries. Ticket #587.
235 do_test minmax2-7.1 {
237 SELECT max(x) FROM t1;
241 do_test minmax2-7.2 {
243 SELECT * FROM (SELECT max(x) FROM t1);
247 do_test minmax2-7.3 {
249 SELECT min(x) FROM t1;
253 do_test minmax2-7.4 {
255 SELECT * FROM (SELECT min(x) FROM t1);
260 # Make sure min(x) and max(x) work correctly when the datatype is
261 # TEXT instead of NUMERIC. Ticket #623.
263 do_test minmax2-8.1 {
265 CREATE TABLE t4(a TEXT);
266 INSERT INTO t4 VALUES('1234');
267 INSERT INTO t4 VALUES('234');
268 INSERT INTO t4 VALUES('34');
269 SELECT min(a), max(a) FROM t4;
272 do_test minmax2-8.2 {
274 CREATE TABLE t5(a INTEGER);
275 INSERT INTO t5 VALUES('1234');
276 INSERT INTO t5 VALUES('234');
277 INSERT INTO t5 VALUES('34');
278 SELECT min(a), max(a) FROM t5;
282 # Ticket #658: Test the min()/max() optimization when the FROM clause
285 ifcapable {compound && subquery} {
286 do_test minmax2-9.1 {
288 SELECT max(rowid) FROM (
289 SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
293 do_test minmax2-9.2 {
295 SELECT max(rowid) FROM (
296 SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5
300 } ;# ifcapable compound&&subquery
302 # If there is a NULL in an aggregate max() or min(), ignore it. An
303 # aggregate min() or max() will only return NULL if all values are NULL.
305 do_test minmax2-10.1 {
308 INSERT INTO t6 VALUES(1);
309 INSERT INTO t6 VALUES(2);
310 INSERT INTO t6 VALUES(NULL);
311 SELECT coalesce(min(x),-1) FROM t6;
314 do_test minmax2-10.2 {
316 SELECT max(x) FROM t6;
319 do_test minmax2-10.3 {
321 CREATE INDEX i6 ON t6(x DESC);
322 SELECT coalesce(min(x),-1) FROM t6;
325 do_test minmax2-10.4 {
327 SELECT max(x) FROM t6;
330 do_test minmax2-10.5 {
332 DELETE FROM t6 WHERE x NOT NULL;
333 SELECT count(*) FROM t6;
336 do_test minmax2-10.6 {
338 SELECT count(x) FROM t6;
342 do_test minmax2-10.7 {
344 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
348 do_test minmax2-10.8 {
350 SELECT min(x), max(x) FROM t6;
353 do_test minmax2-10.9 {
355 INSERT INTO t6 SELECT * FROM t6;
356 INSERT INTO t6 SELECT * FROM t6;
357 INSERT INTO t6 SELECT * FROM t6;
358 INSERT INTO t6 SELECT * FROM t6;
359 INSERT INTO t6 SELECT * FROM t6;
360 INSERT INTO t6 SELECT * FROM t6;
361 INSERT INTO t6 SELECT * FROM t6;
362 INSERT INTO t6 SELECT * FROM t6;
363 INSERT INTO t6 SELECT * FROM t6;
364 INSERT INTO t6 SELECT * FROM t6;
365 SELECT count(*) FROM t6;
368 do_test minmax2-10.10 {
370 SELECT count(x) FROM t6;
374 do_test minmax2-10.11 {
376 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
380 do_test minmax2-10.12 {
382 SELECT min(x), max(x) FROM t6;
386 # 2017-10-26. Extend the min/max optimization to indexes on expressions
388 do_execsql_test minmax2-11.100 {
389 CREATE TABLE t11(a,b,c);
390 INSERT INTO t11(a,b,c) VALUES(1,10,5),(2,8,11),(3,1,4),(4,20,1),(5,16,4);
391 CREATE INDEX t11bc ON t11(b+c);
392 SELECT max(b+c) FROM t11;
394 do_execsql_test minmax2-11.110 {
395 SELECT a, max(b+c) FROM t11;
397 do_test minmax2-11.111 {
398 db eval {SELECT max(b+c) FROM t11}
401 do_test minmax2-11.112 {
402 db eval {SELECT max(c+b) FROM t11}
405 do_execsql_test minmax2-11.120 {
406 SELECT a, min(b+c) FROM t11;
408 do_test minmax2-11.121 {
409 db eval {SELECT min(b+c) FROM t11}
412 do_test minmax2-11.122 {
413 db eval {SELECT min(c+b) FROM t11}
416 do_execsql_test minmax2-11.130 {
417 INSERT INTO t11(a,b,c) VALUES(6,NULL,0),(7,0,NULL);
418 SELECT a, min(b+c) FROM t11;