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
24 sqlite3_db_config db 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.0 {
288 SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5
291 do_test minmax2-9.1 {
293 SELECT max(yy) FROM (
294 SELECT max(rowid) AS yy FROM t4 UNION SELECT max(rowid) FROM t5
298 do_test minmax2-9.2 {
300 SELECT max(yy) FROM (
301 SELECT max(rowid) AS yy FROM t4 EXCEPT SELECT max(rowid) FROM t5
305 } ;# ifcapable compound&&subquery
307 # If there is a NULL in an aggregate max() or min(), ignore it. An
308 # aggregate min() or max() will only return NULL if all values are NULL.
310 do_test minmax2-10.1 {
313 INSERT INTO t6 VALUES(1);
314 INSERT INTO t6 VALUES(2);
315 INSERT INTO t6 VALUES(NULL);
316 SELECT coalesce(min(x),-1) FROM t6;
319 do_test minmax2-10.2 {
321 SELECT max(x) FROM t6;
324 do_test minmax2-10.3 {
326 CREATE INDEX i6 ON t6(x DESC);
327 SELECT coalesce(min(x),-1) FROM t6;
330 do_test minmax2-10.4 {
332 SELECT max(x) FROM t6;
335 do_test minmax2-10.5 {
337 DELETE FROM t6 WHERE x NOT NULL;
338 SELECT count(*) FROM t6;
341 do_test minmax2-10.6 {
343 SELECT count(x) FROM t6;
347 do_test minmax2-10.7 {
349 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
353 do_test minmax2-10.8 {
355 SELECT min(x), max(x) FROM t6;
358 do_test minmax2-10.9 {
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 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 SELECT count(*) FROM t6;
373 do_test minmax2-10.10 {
375 SELECT count(x) FROM t6;
379 do_test minmax2-10.11 {
381 SELECT (SELECT min(x) FROM t6), (SELECT max(x) FROM t6);
385 do_test minmax2-10.12 {
387 SELECT min(x), max(x) FROM t6;
391 # 2017-10-26. Extend the min/max optimization to indexes on expressions
393 do_execsql_test minmax2-11.100 {
394 CREATE TABLE t11(a,b,c);
395 INSERT INTO t11(a,b,c) VALUES(1,10,5),(2,8,11),(3,1,4),(4,20,1),(5,16,4);
396 CREATE INDEX t11bc ON t11(b+c);
397 SELECT max(b+c) FROM t11;
399 do_execsql_test minmax2-11.110 {
400 SELECT a, max(b+c) FROM t11;
402 do_test minmax2-11.111 {
403 db eval {SELECT max(b+c) FROM t11}
406 do_test minmax2-11.112 {
407 db eval {SELECT max(c+b) FROM t11}
410 do_execsql_test minmax2-11.120 {
411 SELECT a, min(b+c) FROM t11;
413 do_test minmax2-11.121 {
414 db eval {SELECT min(b+c) FROM t11}
417 do_test minmax2-11.122 {
418 db eval {SELECT min(c+b) FROM t11}
421 do_execsql_test minmax2-11.130 {
422 INSERT INTO t11(a,b,c) VALUES(6,NULL,0),(7,0,NULL);
423 SELECT a, min(b+c) FROM t11;