1# 2001 September 15 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 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. 15# 16# $Id: minmax.test,v 1.9 2004/03/13 14:00:37 drh Exp $ 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21do_test minmax-1.0 { 22 execsql { 23 BEGIN; 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); 45 COMMIT; 46 SELECT DISTINCT y FROM t1 ORDER BY y; 47 } 48} {1 2 3 4 5} 49 50do_test minmax-1.1 { 51 set sqlite_search_count 0 52 execsql {SELECT min(x) FROM t1} 53} {1} 54do_test minmax-1.2 { 55 set sqlite_search_count 56} {19} 57do_test minmax-1.3 { 58 set sqlite_search_count 0 59 execsql {SELECT max(x) FROM t1} 60} {20} 61do_test minmax-1.4 { 62 set sqlite_search_count 63} {19} 64do_test minmax-1.5 { 65 execsql {CREATE INDEX t1i1 ON t1(x)} 66 set sqlite_search_count 0 67 execsql {SELECT min(x) FROM t1} 68} {1} 69do_test minmax-1.6 { 70 set sqlite_search_count 71} {1} 72do_test minmax-1.7 { 73 set sqlite_search_count 0 74 execsql {SELECT max(x) FROM t1} 75} {20} 76do_test minmax-1.8 { 77 set sqlite_search_count 78} {1} 79do_test minmax-1.9 { 80 set sqlite_search_count 0 81 execsql {SELECT max(y) FROM t1} 82} {5} 83do_test minmax-1.10 { 84 set sqlite_search_count 85} {19} 86 87do_test minmax-2.0 { 88 execsql { 89 CREATE TABLE t2(a INTEGER PRIMARY KEY, b); 90 INSERT INTO t2 SELECT * FROM t1; 91 } 92 set sqlite_search_count 0 93 execsql {SELECT min(a) FROM t2} 94} {1} 95do_test minmax-2.1 { 96 set sqlite_search_count 97} {0} 98do_test minmax-2.2 { 99 set sqlite_search_count 0 100 execsql {SELECT max(a) FROM t2} 101} {20} 102do_test minmax-2.3 { 103 set sqlite_search_count 104} {0} 105 106do_test minmax-3.0 { 107 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)} 108 set sqlite_search_count 0 109 execsql {SELECT max(a) FROM t2} 110} {21} 111do_test minmax-3.1 { 112 set sqlite_search_count 113} {0} 114do_test minmax-3.2 { 115 execsql {INSERT INTO t2 VALUES((SELECT max(a) FROM t2)+1,999)} 116 set sqlite_search_count 0 117 execsql { 118 SELECT b FROM t2 WHERE a=(SELECT max(a) FROM t2) 119 } 120} {999} 121do_test minmax-3.3 { 122 set sqlite_search_count 123} {0} 124 125do_test minmax-4.1 { 126 execsql { 127 SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM 128 (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y') 129 } 130} {1 20} 131do_test minmax-4.2 { 132 execsql { 133 SELECT y, sum(x) FROM 134 (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1) 135 GROUP BY y ORDER BY y; 136 } 137} {1 1 2 5 3 22 4 92 5 90 6 0} 138do_test minmax-4.3 { 139 execsql { 140 SELECT y, count(x), count(*) FROM 141 (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1) 142 GROUP BY y ORDER BY y; 143 } 144} {1 1 1 2 2 3 3 4 5 4 8 9 5 5 6 6 0 1} 145 146# Make sure the min(x) and max(x) optimizations work on empty tables 147# including empty tables with indices. Ticket #296. 148# 149do_test minmax-5.1 { 150 execsql { 151 CREATE TABLE t3(x INTEGER UNIQUE NOT NULL); 152 SELECT coalesce(min(x),999) FROM t3; 153 } 154} {999} 155do_test minmax-5.2 { 156 execsql { 157 SELECT coalesce(min(rowid),999) FROM t3; 158 } 159} {999} 160do_test minmax-5.3 { 161 execsql { 162 SELECT coalesce(max(x),999) FROM t3; 163 } 164} {999} 165do_test minmax-5.4 { 166 execsql { 167 SELECT coalesce(max(rowid),999) FROM t3; 168 } 169} {999} 170do_test minmax-5.5 { 171 execsql { 172 SELECT coalesce(max(rowid),999) FROM t3 WHERE rowid<25; 173 } 174} {999} 175 176# Make sure the min(x) and max(x) optimizations work when there 177# is a LIMIT clause. Ticket #396. 178# 179do_test minmax-6.1 { 180 execsql { 181 SELECT min(a) FROM t2 LIMIT 1 182 } 183} {1} 184do_test minmax-6.2 { 185 execsql { 186 SELECT max(a) FROM t2 LIMIT 3 187 } 188} {22} 189do_test minmax-6.3 { 190 execsql { 191 SELECT min(a) FROM t2 LIMIT 0,100 192 } 193} {1} 194do_test minmax-6.4 { 195 execsql { 196 SELECT max(a) FROM t2 LIMIT 1,100 197 } 198} {} 199do_test minmax-6.5 { 200 execsql { 201 SELECT min(x) FROM t3 LIMIT 1 202 } 203} {{}} 204do_test minmax-6.6 { 205 execsql { 206 SELECT max(x) FROM t3 LIMIT 0 207 } 208} {} 209do_test minmax-6.7 { 210 execsql { 211 SELECT max(a) FROM t2 LIMIT 0 212 } 213} {} 214 215# Make sure the max(x) and min(x) optimizations work for nested 216# queries. Ticket #587. 217# 218do_test minmax-7.1 { 219 execsql { 220 SELECT max(x) FROM t1; 221 } 222} 20 223do_test minmax-7.2 { 224 execsql { 225 SELECT * FROM (SELECT max(x) FROM t1); 226 } 227} 20 228do_test minmax-7.3 { 229 execsql { 230 SELECT min(x) FROM t1; 231 } 232} 1 233do_test minmax-7.4 { 234 execsql { 235 SELECT * FROM (SELECT min(x) FROM t1); 236 } 237} 1 238 239# Make sure min(x) and max(x) work correctly when the datatype is 240# TEXT instead of NUMERIC. Ticket #623. 241# 242do_test minmax-8.1 { 243 execsql { 244 CREATE TABLE t4(a TEXT); 245 INSERT INTO t4 VALUES('1234'); 246 INSERT INTO t4 VALUES('234'); 247 INSERT INTO t4 VALUES('34'); 248 SELECT min(a), max(a) FROM t4; 249 } 250} {1234 34} 251do_test minmax-8.2 { 252 execsql { 253 CREATE TABLE t5(a INTEGER); 254 INSERT INTO t5 VALUES('1234'); 255 INSERT INTO t5 VALUES('234'); 256 INSERT INTO t5 VALUES('34'); 257 SELECT min(a), max(a) FROM t5; 258 } 259} {34 1234} 260 261# Ticket #658: Test the min()/max() optimization when the FROM clause 262# is a subquery. 263# 264do_test minmax-9.1 { 265 execsql { 266 SELECT max(rowid) FROM ( 267 SELECT max(rowid) FROM t4 UNION SELECT max(rowid) FROM t5 268 ) 269 } 270} {1} 271do_test minmax-9.2 { 272 execsql { 273 SELECT max(rowid) FROM ( 274 SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5 275 ) 276 } 277} {{}} 278 279finish_test 280