sl@0
|
1 |
# 2008 January 5
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
# $Id: minmax3.test,v 1.5 2008/07/12 14:52:20 drh Exp $
|
sl@0
|
12 |
|
sl@0
|
13 |
set testdir [file dirname $argv0]
|
sl@0
|
14 |
source $testdir/tester.tcl
|
sl@0
|
15 |
|
sl@0
|
16 |
# Do an SQL statement. Append the search count to the end of the result.
|
sl@0
|
17 |
#
|
sl@0
|
18 |
proc count sql {
|
sl@0
|
19 |
set ::sqlite_search_count 0
|
sl@0
|
20 |
return [concat [execsql $sql] $::sqlite_search_count]
|
sl@0
|
21 |
}
|
sl@0
|
22 |
|
sl@0
|
23 |
# This procedure sets the value of the file-format in file 'test.db'
|
sl@0
|
24 |
# to $newval. Also, the schema cookie is incremented.
|
sl@0
|
25 |
#
|
sl@0
|
26 |
proc set_file_format {newval} {
|
sl@0
|
27 |
hexio_write test.db 44 [hexio_render_int32 $newval]
|
sl@0
|
28 |
set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
|
sl@0
|
29 |
incr schemacookie
|
sl@0
|
30 |
hexio_write test.db 40 [hexio_render_int32 $schemacookie]
|
sl@0
|
31 |
return {}
|
sl@0
|
32 |
}
|
sl@0
|
33 |
|
sl@0
|
34 |
do_test minmax3-1.0 {
|
sl@0
|
35 |
execsql {
|
sl@0
|
36 |
CREATE TABLE t1(x, y, z);
|
sl@0
|
37 |
}
|
sl@0
|
38 |
db close
|
sl@0
|
39 |
set_file_format 4
|
sl@0
|
40 |
sqlite3 db test.db
|
sl@0
|
41 |
execsql {
|
sl@0
|
42 |
BEGIN;
|
sl@0
|
43 |
INSERT INTO t1 VALUES('1', 'I', 'one');
|
sl@0
|
44 |
INSERT INTO t1 VALUES('2', 'IV', 'four');
|
sl@0
|
45 |
INSERT INTO t1 VALUES('2', NULL, 'three');
|
sl@0
|
46 |
INSERT INTO t1 VALUES('2', 'II', 'two');
|
sl@0
|
47 |
INSERT INTO t1 VALUES('2', 'V', 'five');
|
sl@0
|
48 |
INSERT INTO t1 VALUES('3', 'VI', 'six');
|
sl@0
|
49 |
COMMIT;
|
sl@0
|
50 |
}
|
sl@0
|
51 |
} {}
|
sl@0
|
52 |
do_test minmax3-1.1.1 {
|
sl@0
|
53 |
# Linear scan.
|
sl@0
|
54 |
count { SELECT max(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
55 |
} {V 5}
|
sl@0
|
56 |
do_test minmax3-1.1.2 {
|
sl@0
|
57 |
# Index optimizes the WHERE x='2' constraint.
|
sl@0
|
58 |
execsql { CREATE INDEX i1 ON t1(x) }
|
sl@0
|
59 |
count { SELECT max(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
60 |
} {V 9}
|
sl@0
|
61 |
do_test minmax3-1.1.3 {
|
sl@0
|
62 |
# Index optimizes the WHERE x='2' constraint and the MAX(y).
|
sl@0
|
63 |
execsql { CREATE INDEX i2 ON t1(x,y) }
|
sl@0
|
64 |
count { SELECT max(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
65 |
} {V 1}
|
sl@0
|
66 |
do_test minmax3-1.1.4 {
|
sl@0
|
67 |
# Index optimizes the WHERE x='2' constraint and the MAX(y).
|
sl@0
|
68 |
execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
|
sl@0
|
69 |
count { SELECT max(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
70 |
} {V 1}
|
sl@0
|
71 |
do_test minmax3-1.1.5 {
|
sl@0
|
72 |
count { SELECT max(y) FROM t1 WHERE x = '2' AND y != 'V'; }
|
sl@0
|
73 |
} {IV 2}
|
sl@0
|
74 |
do_test minmax3-1.1.6 {
|
sl@0
|
75 |
count { SELECT max(y) FROM t1 WHERE x = '2' AND y < 'V'; }
|
sl@0
|
76 |
} {IV 1}
|
sl@0
|
77 |
do_test minmax3-1.1.6 {
|
sl@0
|
78 |
count { SELECT max(y) FROM t1 WHERE x = '2' AND z != 'five'; }
|
sl@0
|
79 |
} {IV 4}
|
sl@0
|
80 |
|
sl@0
|
81 |
do_test minmax3-1.2.1 {
|
sl@0
|
82 |
# Linear scan of t1.
|
sl@0
|
83 |
execsql { DROP INDEX i1 ; DROP INDEX i2 }
|
sl@0
|
84 |
count { SELECT min(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
85 |
} {II 5}
|
sl@0
|
86 |
do_test minmax3-1.2.2 {
|
sl@0
|
87 |
# Index i1 optimizes the WHERE x='2' constraint.
|
sl@0
|
88 |
execsql { CREATE INDEX i1 ON t1(x) }
|
sl@0
|
89 |
count { SELECT min(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
90 |
} {II 9}
|
sl@0
|
91 |
do_test minmax3-1.2.3 {
|
sl@0
|
92 |
# Index i2 optimizes the WHERE x='2' constraint and the min(y).
|
sl@0
|
93 |
execsql { CREATE INDEX i2 ON t1(x,y) }
|
sl@0
|
94 |
count { SELECT min(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
95 |
} {II 1}
|
sl@0
|
96 |
do_test minmax3-1.2.4 {
|
sl@0
|
97 |
# Index optimizes the WHERE x='2' constraint and the MAX(y).
|
sl@0
|
98 |
execsql { DROP INDEX i2 ; CREATE INDEX i2 ON t1(x, y DESC) }
|
sl@0
|
99 |
count { SELECT min(y) FROM t1 WHERE x = '2'; }
|
sl@0
|
100 |
} {II 1}
|
sl@0
|
101 |
|
sl@0
|
102 |
do_test minmax3-1.3.1 {
|
sl@0
|
103 |
# Linear scan
|
sl@0
|
104 |
execsql { DROP INDEX i1 ; DROP INDEX i2 }
|
sl@0
|
105 |
count { SELECT min(y) FROM t1; }
|
sl@0
|
106 |
} {I 5}
|
sl@0
|
107 |
do_test minmax3-1.3.2 {
|
sl@0
|
108 |
# Index i1 optimizes the min(y)
|
sl@0
|
109 |
execsql { CREATE INDEX i1 ON t1(y) }
|
sl@0
|
110 |
count { SELECT min(y) FROM t1; }
|
sl@0
|
111 |
} {I 1}
|
sl@0
|
112 |
do_test minmax3-1.3.3 {
|
sl@0
|
113 |
# Index i1 optimizes the min(y)
|
sl@0
|
114 |
execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
|
sl@0
|
115 |
count { SELECT min(y) FROM t1; }
|
sl@0
|
116 |
} {I 1}
|
sl@0
|
117 |
|
sl@0
|
118 |
do_test minmax3-1.4.1 {
|
sl@0
|
119 |
# Linear scan
|
sl@0
|
120 |
execsql { DROP INDEX i1 }
|
sl@0
|
121 |
count { SELECT max(y) FROM t1; }
|
sl@0
|
122 |
} {VI 5}
|
sl@0
|
123 |
do_test minmax3-1.4.2 {
|
sl@0
|
124 |
# Index i1 optimizes the max(y)
|
sl@0
|
125 |
execsql { CREATE INDEX i1 ON t1(y) }
|
sl@0
|
126 |
count { SELECT max(y) FROM t1; }
|
sl@0
|
127 |
} {VI 0}
|
sl@0
|
128 |
do_test minmax3-1.4.3 {
|
sl@0
|
129 |
# Index i1 optimizes the max(y)
|
sl@0
|
130 |
execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(y DESC) }
|
sl@0
|
131 |
execsql { SELECT y from t1}
|
sl@0
|
132 |
count { SELECT max(y) FROM t1; }
|
sl@0
|
133 |
} {VI 0}
|
sl@0
|
134 |
do_test minmax3-1.4.4 {
|
sl@0
|
135 |
execsql { DROP INDEX i1 }
|
sl@0
|
136 |
} {}
|
sl@0
|
137 |
|
sl@0
|
138 |
do_test minmax3-2.1 {
|
sl@0
|
139 |
execsql {
|
sl@0
|
140 |
CREATE TABLE t2(a, b);
|
sl@0
|
141 |
CREATE INDEX i3 ON t2(a, b);
|
sl@0
|
142 |
INSERT INTO t2 VALUES(1, NULL);
|
sl@0
|
143 |
INSERT INTO t2 VALUES(1, 1);
|
sl@0
|
144 |
INSERT INTO t2 VALUES(1, 2);
|
sl@0
|
145 |
INSERT INTO t2 VALUES(1, 3);
|
sl@0
|
146 |
INSERT INTO t2 VALUES(2, NULL);
|
sl@0
|
147 |
INSERT INTO t2 VALUES(2, 1);
|
sl@0
|
148 |
INSERT INTO t2 VALUES(2, 2);
|
sl@0
|
149 |
INSERT INTO t2 VALUES(2, 3);
|
sl@0
|
150 |
INSERT INTO t2 VALUES(3, 1);
|
sl@0
|
151 |
INSERT INTO t2 VALUES(3, 2);
|
sl@0
|
152 |
INSERT INTO t2 VALUES(3, 3);
|
sl@0
|
153 |
}
|
sl@0
|
154 |
} {}
|
sl@0
|
155 |
do_test minmax3-2.2 {
|
sl@0
|
156 |
execsql { SELECT min(b) FROM t2 WHERE a = 1; }
|
sl@0
|
157 |
} {1}
|
sl@0
|
158 |
do_test minmax3-2.3 {
|
sl@0
|
159 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
|
sl@0
|
160 |
} {2}
|
sl@0
|
161 |
do_test minmax3-2.4 {
|
sl@0
|
162 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
|
sl@0
|
163 |
} {1}
|
sl@0
|
164 |
do_test minmax3-2.5 {
|
sl@0
|
165 |
execsql { SELECT min(b) FROM t2 WHERE a = 1; }
|
sl@0
|
166 |
} {1}
|
sl@0
|
167 |
do_test minmax3-2.6 {
|
sl@0
|
168 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
|
sl@0
|
169 |
} {1}
|
sl@0
|
170 |
do_test minmax3-2.7 {
|
sl@0
|
171 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
|
sl@0
|
172 |
} {{}}
|
sl@0
|
173 |
do_test minmax3-2.8 {
|
sl@0
|
174 |
execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }
|
sl@0
|
175 |
} {{}}
|
sl@0
|
176 |
|
sl@0
|
177 |
do_test minmax3-2.1 {
|
sl@0
|
178 |
execsql {
|
sl@0
|
179 |
DROP TABLE t2;
|
sl@0
|
180 |
CREATE TABLE t2(a, b);
|
sl@0
|
181 |
CREATE INDEX i3 ON t2(a, b DESC);
|
sl@0
|
182 |
INSERT INTO t2 VALUES(1, NULL);
|
sl@0
|
183 |
INSERT INTO t2 VALUES(1, 1);
|
sl@0
|
184 |
INSERT INTO t2 VALUES(1, 2);
|
sl@0
|
185 |
INSERT INTO t2 VALUES(1, 3);
|
sl@0
|
186 |
INSERT INTO t2 VALUES(2, NULL);
|
sl@0
|
187 |
INSERT INTO t2 VALUES(2, 1);
|
sl@0
|
188 |
INSERT INTO t2 VALUES(2, 2);
|
sl@0
|
189 |
INSERT INTO t2 VALUES(2, 3);
|
sl@0
|
190 |
INSERT INTO t2 VALUES(3, 1);
|
sl@0
|
191 |
INSERT INTO t2 VALUES(3, 2);
|
sl@0
|
192 |
INSERT INTO t2 VALUES(3, 3);
|
sl@0
|
193 |
}
|
sl@0
|
194 |
} {}
|
sl@0
|
195 |
do_test minmax3-2.2 {
|
sl@0
|
196 |
execsql { SELECT min(b) FROM t2 WHERE a = 1; }
|
sl@0
|
197 |
} {1}
|
sl@0
|
198 |
do_test minmax3-2.3 {
|
sl@0
|
199 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>1; }
|
sl@0
|
200 |
} {2}
|
sl@0
|
201 |
do_test minmax3-2.4 {
|
sl@0
|
202 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b>-1; }
|
sl@0
|
203 |
} {1}
|
sl@0
|
204 |
do_test minmax3-2.5 {
|
sl@0
|
205 |
execsql { SELECT min(b) FROM t2 WHERE a = 1; }
|
sl@0
|
206 |
} {1}
|
sl@0
|
207 |
do_test minmax3-2.6 {
|
sl@0
|
208 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<2; }
|
sl@0
|
209 |
} {1}
|
sl@0
|
210 |
do_test minmax3-2.7 {
|
sl@0
|
211 |
execsql { SELECT min(b) FROM t2 WHERE a = 1 AND b<1; }
|
sl@0
|
212 |
} {{}}
|
sl@0
|
213 |
do_test minmax3-2.8 {
|
sl@0
|
214 |
execsql { SELECT min(b) FROM t2 WHERE a = 3 AND b<1; }
|
sl@0
|
215 |
} {{}}
|
sl@0
|
216 |
|
sl@0
|
217 |
finish_test
|