First public contribution.
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 an SQL statement. Append the search count to the end of the result.
19 set ::sqlite_search_count 0
20 return [concat [execsql $sql] $::sqlite_search_count]
23 # This procedure sets the value of the file-format in file 'test.db'
24 # to $newval. Also, the schema cookie is incremented.
26 proc set_file_format {newval} {
27 hexio_write test.db 44 [hexio_render_int32 $newval]
28 set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
30 hexio_write test.db 40 [hexio_render_int32 $schemacookie]
36 CREATE TABLE t1(x, y, z);
43 INSERT INTO t1 VALUES('1', 'I', 'one');
44 INSERT INTO t1 VALUES('2', 'IV', 'four');
45 INSERT INTO t1 VALUES('2', NULL, 'three');
46 INSERT INTO t1 VALUES('2', 'II', 'two');
47 INSERT INTO t1 VALUES('2', 'V', 'five');
48 INSERT INTO t1 VALUES('3', 'VI', 'six');
52 do_test minmax3-1.1.1 {
54 count { SELECT max(y) FROM t1 WHERE x = '2'; }
56 do_test minmax3-1.1.2 {
57 # Index optimizes the WHERE x='2' constraint.
58 execsql { CREATE INDEX i1 ON t1(x) }
59 count { SELECT max(y) FROM t1 WHERE x = '2'; }
61 do_test minmax3-1.1.3 {
62 # Index optimizes the WHERE x='2' constraint and the MAX(y).
63 execsql { CREATE INDEX i2 ON t1(x,y) }
64 count { SELECT max(y) FROM t1 WHERE x = '2'; }
66 do_test minmax3-1.1.4 {
67 # Index optimizes the WHERE x='2' constraint and the MAX(y).
68 execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
69 count { SELECT max(y) FROM t1 WHERE x = '2'; }
71 do_test minmax3-1.1.5 {
72 count { SELECT max(y) FROM t1 WHERE x = '2' AND y != 'V'; }
74 do_test minmax3-1.1.6 {
75 count { SELECT max(y) FROM t1 WHERE x = '2' AND y < 'V'; }
77 do_test minmax3-1.1.6 {
78 count { SELECT max(y) FROM t1 WHERE x = '2' AND z != 'five'; }
81 do_test minmax3-1.2.1 {
83 execsql { DROP INDEX i1 ; DROP INDEX i2 }
84 count { SELECT min(y) FROM t1 WHERE x = '2'; }
86 do_test minmax3-1.2.2 {
87 # Index i1 optimizes the WHERE x='2' constraint.
88 execsql { CREATE INDEX i1 ON t1(x) }
89 count { SELECT min(y) FROM t1 WHERE x = '2'; }
91 do_test minmax3-1.2.3 {
92 # Index i2 optimizes the WHERE x='2' constraint and the min(y).
93 execsql { CREATE INDEX i2 ON t1(x,y) }
94 count { SELECT min(y) FROM t1 WHERE x = '2'; }
96 do_test minmax3-1.2.4 {
97 # Index optimizes the WHERE x='2' constraint and the MAX(y).
98 execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
99 count { SELECT min(y) FROM t1 WHERE x = '2'; }
102 do_test minmax3-1.3.1 {
104 execsql { DROP INDEX i1 ; DROP INDEX i2 }
105 count { SELECT min(y) FROM t1; }
107 do_test minmax3-1.3.2 {
108 # Index i1 optimizes the min(y)
109 execsql { CREATE INDEX i1 ON t1(y) }
110 count { SELECT min(y) FROM t1; }
112 do_test minmax3-1.3.3 {
113 # Index i1 optimizes the min(y)
114 execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
115 count { SELECT min(y) FROM t1; }
118 do_test minmax3-1.4.1 {
120 execsql { DROP INDEX i1 }
121 count { SELECT max(y) FROM t1; }
123 do_test minmax3-1.4.2 {
124 # Index i1 optimizes the max(y)
125 execsql { CREATE INDEX i1 ON t1(y) }
126 count { SELECT max(y) FROM t1; }
128 do_test minmax3-1.4.3 {
129 # Index i1 optimizes the max(y)
130 execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
131 execsql { SELECT y from t1}
132 count { SELECT max(y) FROM t1; }
134 do_test minmax3-1.4.4 {
135 execsql { DROP INDEX i1 }
138 do_test minmax3-2.1 {
140 CREATE TABLE t2(a, b);
141 CREATE INDEX i3 ON t2(a, b);
142 INSERT INTO t2 VALUES(1, NULL);
143 INSERT INTO t2 VALUES(1, 1);
144 INSERT INTO t2 VALUES(1, 2);
145 INSERT INTO t2 VALUES(1, 3);
146 INSERT INTO t2 VALUES(2, NULL);
147 INSERT INTO t2 VALUES(2, 1);
148 INSERT INTO t2 VALUES(2, 2);
149 INSERT INTO t2 VALUES(2, 3);
150 INSERT INTO t2 VALUES(3, 1);
151 INSERT INTO t2 VALUES(3, 2);
152 INSERT INTO t2 VALUES(3, 3);
155 do_test minmax3-2.2 {
156 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
158 do_test minmax3-2.3 {
159 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
161 do_test minmax3-2.4 {
162 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
164 do_test minmax3-2.5 {
165 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
167 do_test minmax3-2.6 {
168 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
170 do_test minmax3-2.7 {
171 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
173 do_test minmax3-2.8 {
174 execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }
177 do_test minmax3-2.1 {
180 CREATE TABLE t2(a, b);
181 CREATE INDEX i3 ON t2(a, b DESC);
182 INSERT INTO t2 VALUES(1, NULL);
183 INSERT INTO t2 VALUES(1, 1);
184 INSERT INTO t2 VALUES(1, 2);
185 INSERT INTO t2 VALUES(1, 3);
186 INSERT INTO t2 VALUES(2, NULL);
187 INSERT INTO t2 VALUES(2, 1);
188 INSERT INTO t2 VALUES(2, 2);
189 INSERT INTO t2 VALUES(2, 3);
190 INSERT INTO t2 VALUES(3, 1);
191 INSERT INTO t2 VALUES(3, 2);
192 INSERT INTO t2 VALUES(3, 3);
195 do_test minmax3-2.2 {
196 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
198 do_test minmax3-2.3 {
199 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
201 do_test minmax3-2.4 {
202 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
204 do_test minmax3-2.5 {
205 execsql { SELECT min(b) FROM t2 WHERE a = 1; }
207 do_test minmax3-2.6 {
208 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
210 do_test minmax3-2.7 {
211 execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
213 do_test minmax3-2.8 {
214 execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }