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 # $Id: minmax3.test,v 1.5 2008/07/12 14:52:20 drh Exp $
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
16 # Do not use a codec for tests in this file, as the database file is
17 # manipulated directly using tcl scripts (using the [hexio_write] command).
21 # Do an SQL statement. Append the search count to the end of the result.
24 set ::sqlite_search_count 0
25 return [concat [execsql $sql] $::sqlite_search_count]
28 # This procedure sets the value of the file-format in file 'test.db'
29 # to $newval. Also, the schema cookie is incremented.
31 proc set_file_format {newval} {
32 hexio_write test.db 44 [hexio_render_int32 $newval]
33 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
35 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
41 CREATE TABLE t1(x, y, z);
48 INSERT INTO t1 VALUES('1', 'I', 'one');
49 INSERT INTO t1 VALUES('2', 'IV', 'four');
50 INSERT INTO t1 VALUES('2', NULL, 'three');
51 INSERT INTO t1 VALUES('2', 'II', 'two');
52 INSERT INTO t1 VALUES('2', 'V', 'five');
53 INSERT INTO t1 VALUES('3', 'VI', 'six');
55 PRAGMA automatic_index=OFF;
58 do_test minmax3-1.1.1 {
60 count { SELECT max(y) FROM t1 WHERE x = '2'; }
62 do_test minmax3-1.1.2 {
63 # Index optimizes the WHERE x='2' constraint.
64 execsql { CREATE INDEX i1 ON t1(x) }
65 count { SELECT max(y) FROM t1 WHERE x = '2'; }
67 do_test minmax3-1.1.3 {
68 # Index optimizes the WHERE x='2' constraint and the MAX(y).
69 execsql { CREATE INDEX i2 ON t1(x,y) }
70 count { SELECT max(y) FROM t1 WHERE x = '2'; }
72 do_test minmax3-1.1.4 {
73 # Index optimizes the WHERE x='2' constraint and the MAX(y).
74 execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
75 count { SELECT max(y) FROM t1 WHERE x = '2'; }
77 do_test minmax3-1.1.5 {
78 count { SELECT max(y) FROM t1 WHERE x = '2' AND y != 'V'; }
80 do_test minmax3-1.1.6 {
81 count { SELECT max(y) FROM t1 WHERE x = '2' AND y < 'V'; }
83 do_test minmax3-1.1.6 {
84 count { SELECT max(y) FROM t1 WHERE x = '2' AND z != 'five'; }
87 do_test minmax3-1.2.1 {
89 execsql { DROP INDEX i1 ; DROP INDEX i2 }
90 count { SELECT min(y) FROM t1 WHERE x = '2'; }
92 do_test minmax3-1.2.2 {
93 # Index i1 optimizes the WHERE x='2' constraint.
94 execsql { CREATE INDEX i1 ON t1(x) }
95 count { SELECT min(y) FROM t1 WHERE x = '2'; }
97 do_test minmax3-1.2.3 {
98 # Index i2 optimizes the WHERE x='2' constraint and the min(y).
99 execsql { CREATE INDEX i2 ON t1(x,y) }
100 count { SELECT min(y) FROM t1 WHERE x = '2'; }
102 do_test minmax3-1.2.4 {
103 # Index optimizes the WHERE x='2' constraint and the MAX(y).
104 execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
105 count { SELECT min(y) FROM t1 WHERE x = '2'; }
108 do_test minmax3-1.3.1 {
110 execsql { DROP INDEX i1 ; DROP INDEX i2 }
111 count { SELECT min(y) FROM t1; }
113 do_test minmax3-1.3.2 {
114 # Index i1 optimizes the min(y)
115 execsql { CREATE INDEX i1 ON t1(y) }
116 count { SELECT min(y) FROM t1; }
118 do_test minmax3-1.3.3 {
119 # Index i1 optimizes the min(y)
120 execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
121 count { SELECT min(y) FROM t1; }
124 do_test minmax3-1.4.1 {
126 execsql { DROP INDEX i1 }
127 count { SELECT max(y) FROM t1; }
129 do_test minmax3-1.4.2 {
130 # Index i1 optimizes the max(y)
131 execsql { CREATE INDEX i1 ON t1(y) }
132 count { SELECT max(y) FROM t1; }
134 do_test minmax3-1.4.3 {
135 # Index i1 optimizes the max(y)
136 execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
137 execsql { SELECT y from t1}
138 count { SELECT max(y) FROM t1; }
140 do_test minmax3-1.4.4 {
141 execsql { DROP INDEX i1 }
144 do_test minmax3-2.1 {
146 CREATE TABLE t2(a, b);
147 CREATE INDEX i3 ON t2(a, b);
148 INSERT INTO t2 VALUES(1, NULL);
149 INSERT INTO t2 VALUES(1, 1);
150 INSERT INTO t2 VALUES(1, 2);
151 INSERT INTO t2 VALUES(1, 3);
152 INSERT INTO t2 VALUES(2, NULL);
153 INSERT INTO t2 VALUES(2, 1);
154 INSERT INTO t2 VALUES(2, 2);
155 INSERT INTO t2 VALUES(2, 3);
156 INSERT INTO t2 VALUES(3, 1);
157 INSERT INTO t2 VALUES(3, 2);
158 INSERT INTO t2 VALUES(3, 3);
161 do_test minmax3-2.2 {
162 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
164 do_test minmax3-2.3 {
165 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
167 do_test minmax3-2.4 {
168 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
170 do_test minmax3-2.5 {
171 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
173 do_test minmax3-2.6 {
174 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
176 do_test minmax3-2.7 {
177 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
179 do_test minmax3-2.8 {
180 execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }
183 do_test minmax3-3.1 {
186 CREATE TABLE t2(a, b);
187 CREATE INDEX i3 ON t2(a, b DESC);
188 INSERT INTO t2 VALUES(1, NULL);
189 INSERT INTO t2 VALUES(1, 1);
190 INSERT INTO t2 VALUES(1, 2);
191 INSERT INTO t2 VALUES(1, 3);
192 INSERT INTO t2 VALUES(2, NULL);
193 INSERT INTO t2 VALUES(2, 1);
194 INSERT INTO t2 VALUES(2, 2);
195 INSERT INTO t2 VALUES(2, 3);
196 INSERT INTO t2 VALUES(3, 1);
197 INSERT INTO t2 VALUES(3, 2);
198 INSERT INTO t2 VALUES(3, 3);
201 do_test minmax3-3.2 {
202 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
204 do_test minmax3-3.3 {
205 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
207 do_test minmax3-3.4 {
208 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
210 do_test minmax3-3.5 {
211 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
213 do_test minmax3-3.6 {
214 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
216 do_test minmax3-3.7 {
217 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
219 do_test minmax3-3.8 {
220 execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }
223 do_test minmax3-4.1 {
226 INSERT INTO t4 VALUES('abc');
227 INSERT INTO t4 VALUES('BCD');
228 SELECT max(x) FROM t4;
231 do_test minmax3-4.2 {
233 SELECT max(x COLLATE nocase) FROM t4;
236 do_test minmax3-4.3 {
238 SELECT max(x), max(x COLLATE nocase) FROM t4;
241 do_test minmax3-4.4 {
243 SELECT max(x COLLATE binary), max(x COLLATE nocase) FROM t4;
246 do_test minmax3-4.5 {
248 SELECT max(x COLLATE nocase), max(x COLLATE rtrim) FROM t4;
251 do_test minmax3-4.6 {
253 SELECT max(x COLLATE nocase), max(x) FROM t4;
256 do_test minmax3-4.10 {
258 SELECT min(x) FROM t4;
261 do_test minmax3-4.11 {
263 SELECT min(x COLLATE nocase) FROM t4;
266 do_test minmax3-4.12 {
268 SELECT min(x), min(x COLLATE nocase) FROM t4;
271 do_test minmax3-4.13 {
273 SELECT min(x COLLATE binary), min(x COLLATE nocase) FROM t4;
276 do_test minmax3-4.14 {
278 SELECT min(x COLLATE nocase), min(x COLLATE rtrim) FROM t4;
281 do_test minmax3-4.15 {
283 SELECT min(x COLLATE nocase), min(x) FROM t4;