sl@0
|
1 |
# 2006 October 27
|
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 |
# This file implements regression tests for SQLite library. The
|
sl@0
|
12 |
# focus of this file is testing the use of indices in WHERE clauses.
|
sl@0
|
13 |
# This file was created when support for optimizing IS NULL phrases
|
sl@0
|
14 |
# was added. And so the principle purpose of this file is to test
|
sl@0
|
15 |
# that IS NULL phrases are correctly optimized. But you can never
|
sl@0
|
16 |
# have too many tests, so some other tests are thrown in as well.
|
sl@0
|
17 |
#
|
sl@0
|
18 |
# $Id: where4.test,v 1.6 2007/12/10 05:03:48 danielk1977 Exp $
|
sl@0
|
19 |
|
sl@0
|
20 |
set testdir [file dirname $argv0]
|
sl@0
|
21 |
source $testdir/tester.tcl
|
sl@0
|
22 |
|
sl@0
|
23 |
ifcapable !tclvar||!bloblit {
|
sl@0
|
24 |
finish_test
|
sl@0
|
25 |
return
|
sl@0
|
26 |
}
|
sl@0
|
27 |
|
sl@0
|
28 |
# Build some test data
|
sl@0
|
29 |
#
|
sl@0
|
30 |
do_test where4-1.0 {
|
sl@0
|
31 |
execsql {
|
sl@0
|
32 |
CREATE TABLE t1(w, x, y);
|
sl@0
|
33 |
CREATE INDEX i1wxy ON t1(w,x,y);
|
sl@0
|
34 |
INSERT INTO t1 VALUES(1,2,3);
|
sl@0
|
35 |
INSERT INTO t1 VALUES(1,NULL,3);
|
sl@0
|
36 |
INSERT INTO t1 VALUES('a','b','c');
|
sl@0
|
37 |
INSERT INTO t1 VALUES('a',NULL,'c');
|
sl@0
|
38 |
INSERT INTO t1 VALUES(X'78',x'79',x'7a');
|
sl@0
|
39 |
INSERT INTO t1 VALUES(X'78',NULL,X'7A');
|
sl@0
|
40 |
INSERT INTO t1 VALUES(NULL,NULL,NULL);
|
sl@0
|
41 |
SELECT count(*) FROM t1;
|
sl@0
|
42 |
}
|
sl@0
|
43 |
} {7}
|
sl@0
|
44 |
|
sl@0
|
45 |
# Do an SQL statement. Append the search count to the end of the result.
|
sl@0
|
46 |
#
|
sl@0
|
47 |
proc count sql {
|
sl@0
|
48 |
set ::sqlite_search_count 0
|
sl@0
|
49 |
return [concat [execsql $sql] $::sqlite_search_count]
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
# Verify that queries use an index. We are using the special variable
|
sl@0
|
53 |
# "sqlite_search_count" which tallys the number of executions of MoveTo
|
sl@0
|
54 |
# and Next operators in the VDBE. By verifing that the search count is
|
sl@0
|
55 |
# small we can be assured that indices are being used properly.
|
sl@0
|
56 |
#
|
sl@0
|
57 |
do_test where4-1.1 {
|
sl@0
|
58 |
count {SELECT rowid FROM t1 WHERE w IS NULL}
|
sl@0
|
59 |
} {7 2}
|
sl@0
|
60 |
do_test where4-1.2 {
|
sl@0
|
61 |
count {SELECT rowid FROM t1 WHERE +w IS NULL}
|
sl@0
|
62 |
} {7 6}
|
sl@0
|
63 |
do_test where4-1.3 {
|
sl@0
|
64 |
count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL}
|
sl@0
|
65 |
} {2 2}
|
sl@0
|
66 |
do_test where4-1.4 {
|
sl@0
|
67 |
count {SELECT rowid FROM t1 WHERE w=1 AND +x IS NULL}
|
sl@0
|
68 |
} {2 3}
|
sl@0
|
69 |
do_test where4-1.5 {
|
sl@0
|
70 |
count {SELECT rowid FROM t1 WHERE w=1 AND x>0}
|
sl@0
|
71 |
} {1 2}
|
sl@0
|
72 |
do_test where4-1.6 {
|
sl@0
|
73 |
count {SELECT rowid FROM t1 WHERE w=1 AND x<9}
|
sl@0
|
74 |
} {1 3}
|
sl@0
|
75 |
do_test where4-1.7 {
|
sl@0
|
76 |
count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y=3}
|
sl@0
|
77 |
} {2 2}
|
sl@0
|
78 |
do_test where4-1.8 {
|
sl@0
|
79 |
count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y>2}
|
sl@0
|
80 |
} {2 2}
|
sl@0
|
81 |
do_test where4-1.9 {
|
sl@0
|
82 |
count {SELECT rowid FROM t1 WHERE w='a' AND x IS NULL AND y='c'}
|
sl@0
|
83 |
} {4 2}
|
sl@0
|
84 |
do_test where4-1.10 {
|
sl@0
|
85 |
count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL}
|
sl@0
|
86 |
} {6 2}
|
sl@0
|
87 |
do_test where4-1.11 {
|
sl@0
|
88 |
count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=123}
|
sl@0
|
89 |
} {1}
|
sl@0
|
90 |
do_test where4-1.12 {
|
sl@0
|
91 |
count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=x'7A'}
|
sl@0
|
92 |
} {6 2}
|
sl@0
|
93 |
do_test where4-1.13 {
|
sl@0
|
94 |
count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL}
|
sl@0
|
95 |
} {7 2}
|
sl@0
|
96 |
do_test where4-1.14 {
|
sl@0
|
97 |
count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y IS NULL}
|
sl@0
|
98 |
} {7 2}
|
sl@0
|
99 |
do_test where4-1.15 {
|
sl@0
|
100 |
count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y<0}
|
sl@0
|
101 |
} {2}
|
sl@0
|
102 |
do_test where4-1.16 {
|
sl@0
|
103 |
count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y>=0}
|
sl@0
|
104 |
} {1}
|
sl@0
|
105 |
|
sl@0
|
106 |
do_test where4-2.1 {
|
sl@0
|
107 |
execsql {SELECT rowid FROM t1 ORDER BY w, x, y}
|
sl@0
|
108 |
} {7 2 1 4 3 6 5}
|
sl@0
|
109 |
do_test where4-2.2 {
|
sl@0
|
110 |
execsql {SELECT rowid FROM t1 ORDER BY w DESC, x, y}
|
sl@0
|
111 |
} {6 5 4 3 2 1 7}
|
sl@0
|
112 |
do_test where4-2.3 {
|
sl@0
|
113 |
execsql {SELECT rowid FROM t1 ORDER BY w, x DESC, y}
|
sl@0
|
114 |
} {7 1 2 3 4 5 6}
|
sl@0
|
115 |
|
sl@0
|
116 |
|
sl@0
|
117 |
# Ticket #2177
|
sl@0
|
118 |
#
|
sl@0
|
119 |
# Suppose you have a left join where the right table of the left
|
sl@0
|
120 |
# join (the one that can be NULL) has an index on two columns.
|
sl@0
|
121 |
# The first indexed column is used in the ON clause of the join.
|
sl@0
|
122 |
# The second indexed column is used in the WHERE clause with an IS NULL
|
sl@0
|
123 |
# constraint. It is not allowed to use the IS NULL optimization to
|
sl@0
|
124 |
# optimize the query because the second column might be NULL because
|
sl@0
|
125 |
# the right table did not match - something the index does not know
|
sl@0
|
126 |
# about.
|
sl@0
|
127 |
#
|
sl@0
|
128 |
do_test where4-3.1 {
|
sl@0
|
129 |
execsql {
|
sl@0
|
130 |
CREATE TABLE t2(a);
|
sl@0
|
131 |
INSERT INTO t2 VALUES(1);
|
sl@0
|
132 |
INSERT INTO t2 VALUES(2);
|
sl@0
|
133 |
INSERT INTO t2 VALUES(3);
|
sl@0
|
134 |
CREATE TABLE t3(x,y,UNIQUE(x,y));
|
sl@0
|
135 |
INSERT INTO t3 VALUES(1,11);
|
sl@0
|
136 |
INSERT INTO t3 VALUES(2,NULL);
|
sl@0
|
137 |
|
sl@0
|
138 |
SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE +y IS NULL;
|
sl@0
|
139 |
}
|
sl@0
|
140 |
} {2 2 {} 3 {} {}}
|
sl@0
|
141 |
do_test where4-3.2 {
|
sl@0
|
142 |
execsql {
|
sl@0
|
143 |
SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE y IS NULL;
|
sl@0
|
144 |
}
|
sl@0
|
145 |
} {2 2 {} 3 {} {}}
|
sl@0
|
146 |
|
sl@0
|
147 |
# Ticket #2189. Probably the same bug as #2177.
|
sl@0
|
148 |
#
|
sl@0
|
149 |
do_test where4-4.1 {
|
sl@0
|
150 |
execsql {
|
sl@0
|
151 |
CREATE TABLE test(col1 TEXT PRIMARY KEY);
|
sl@0
|
152 |
INSERT INTO test(col1) values('a');
|
sl@0
|
153 |
INSERT INTO test(col1) values('b');
|
sl@0
|
154 |
INSERT INTO test(col1) values('c');
|
sl@0
|
155 |
CREATE TABLE test2(col1 TEXT PRIMARY KEY);
|
sl@0
|
156 |
INSERT INTO test2(col1) values('a');
|
sl@0
|
157 |
INSERT INTO test2(col1) values('b');
|
sl@0
|
158 |
INSERT INTO test2(col1) values('c');
|
sl@0
|
159 |
SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
|
sl@0
|
160 |
WHERE +t2.col1 IS NULL;
|
sl@0
|
161 |
}
|
sl@0
|
162 |
} {}
|
sl@0
|
163 |
do_test where4-4.2 {
|
sl@0
|
164 |
execsql {
|
sl@0
|
165 |
SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
|
sl@0
|
166 |
WHERE t2.col1 IS NULL;
|
sl@0
|
167 |
}
|
sl@0
|
168 |
} {}
|
sl@0
|
169 |
do_test where4-4.3 {
|
sl@0
|
170 |
execsql {
|
sl@0
|
171 |
SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
|
sl@0
|
172 |
WHERE +t1.col1 IS NULL;
|
sl@0
|
173 |
}
|
sl@0
|
174 |
} {}
|
sl@0
|
175 |
do_test where4-4.4 {
|
sl@0
|
176 |
execsql {
|
sl@0
|
177 |
SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
|
sl@0
|
178 |
WHERE t1.col1 IS NULL;
|
sl@0
|
179 |
}
|
sl@0
|
180 |
} {}
|
sl@0
|
181 |
|
sl@0
|
182 |
# Ticket #2273. Problems with IN operators and NULLs.
|
sl@0
|
183 |
#
|
sl@0
|
184 |
ifcapable subquery {
|
sl@0
|
185 |
do_test where4-5.1 {
|
sl@0
|
186 |
execsql {
|
sl@0
|
187 |
CREATE TABLE t4(x,y,z,PRIMARY KEY(x,y));
|
sl@0
|
188 |
}
|
sl@0
|
189 |
execsql {
|
sl@0
|
190 |
SELECT *
|
sl@0
|
191 |
FROM t2 LEFT JOIN t4 b1
|
sl@0
|
192 |
LEFT JOIN t4 b2 ON b2.x=b1.x AND b2.y IN (b1.y);
|
sl@0
|
193 |
}
|
sl@0
|
194 |
} {1 {} {} {} {} {} {} 2 {} {} {} {} {} {} 3 {} {} {} {} {} {}}
|
sl@0
|
195 |
do_test where4-5.2 {
|
sl@0
|
196 |
execsql {
|
sl@0
|
197 |
INSERT INTO t4 VALUES(1,1,11);
|
sl@0
|
198 |
INSERT INTO t4 VALUES(1,2,12);
|
sl@0
|
199 |
INSERT INTO t4 VALUES(1,3,13);
|
sl@0
|
200 |
INSERT INTO t4 VALUES(2,2,22);
|
sl@0
|
201 |
SELECT rowid FROM t4 WHERE x IN (1,9,2,5) AND y IN (1,3,NULL,2) AND z!=13;
|
sl@0
|
202 |
}
|
sl@0
|
203 |
} {1 2 4}
|
sl@0
|
204 |
do_test where4-5.3 {
|
sl@0
|
205 |
execsql {
|
sl@0
|
206 |
SELECT rowid FROM t4 WHERE x IN (1,9,NULL,2) AND y IN (1,3,2) AND z!=13;
|
sl@0
|
207 |
}
|
sl@0
|
208 |
} {1 2 4}
|
sl@0
|
209 |
do_test where4-6.1 {
|
sl@0
|
210 |
execsql {
|
sl@0
|
211 |
CREATE TABLE t5(a,b,c,d,e,f,UNIQUE(a,b,c,d,e,f));
|
sl@0
|
212 |
INSERT INTO t5 VALUES(1,1,1,1,1,11111);
|
sl@0
|
213 |
INSERT INTO t5 VALUES(2,2,2,2,2,22222);
|
sl@0
|
214 |
INSERT INTO t5 VALUES(1,2,3,4,5,12345);
|
sl@0
|
215 |
INSERT INTO t5 VALUES(2,3,4,5,6,23456);
|
sl@0
|
216 |
}
|
sl@0
|
217 |
execsql {
|
sl@0
|
218 |
SELECT rowid FROM t5
|
sl@0
|
219 |
WHERE a IN (1,9,2) AND b=2 AND c IN (1,2,3,4) AND d>0
|
sl@0
|
220 |
}
|
sl@0
|
221 |
} {3 2}
|
sl@0
|
222 |
do_test where4-6.2 {
|
sl@0
|
223 |
execsql {
|
sl@0
|
224 |
SELECT rowid FROM t5
|
sl@0
|
225 |
WHERE a IN (1,NULL,2) AND b=2 AND c IN (1,2,3,4) AND d>0
|
sl@0
|
226 |
}
|
sl@0
|
227 |
} {3 2}
|
sl@0
|
228 |
do_test where4-7.1 {
|
sl@0
|
229 |
execsql {
|
sl@0
|
230 |
CREATE TABLE t6(y,z,PRIMARY KEY(y,z));
|
sl@0
|
231 |
}
|
sl@0
|
232 |
execsql {
|
sl@0
|
233 |
SELECT * FROM t6 WHERE y=NULL AND z IN ('hello');
|
sl@0
|
234 |
}
|
sl@0
|
235 |
} {}
|
sl@0
|
236 |
|
sl@0
|
237 |
integrity_check {where4-99.0}
|
sl@0
|
238 |
|
sl@0
|
239 |
do_test where4-7.1 {
|
sl@0
|
240 |
execsql {
|
sl@0
|
241 |
BEGIN;
|
sl@0
|
242 |
CREATE TABLE t8(a, b, c, d);
|
sl@0
|
243 |
CREATE INDEX t8_i ON t8(a, b, c);
|
sl@0
|
244 |
CREATE TABLE t7(i);
|
sl@0
|
245 |
|
sl@0
|
246 |
INSERT INTO t7 VALUES(1);
|
sl@0
|
247 |
INSERT INTO t7 SELECT i*2 FROM t7;
|
sl@0
|
248 |
INSERT INTO t7 SELECT i*2 FROM t7;
|
sl@0
|
249 |
INSERT INTO t7 SELECT i*2 FROM t7;
|
sl@0
|
250 |
INSERT INTO t7 SELECT i*2 FROM t7;
|
sl@0
|
251 |
INSERT INTO t7 SELECT i*2 FROM t7;
|
sl@0
|
252 |
INSERT INTO t7 SELECT i*2 FROM t7;
|
sl@0
|
253 |
|
sl@0
|
254 |
COMMIT;
|
sl@0
|
255 |
}
|
sl@0
|
256 |
} {}
|
sl@0
|
257 |
|
sl@0
|
258 |
# At one point the sub-select inside the aggregate sum() function in the
|
sl@0
|
259 |
# following query was leaking a couple of stack entries. This query
|
sl@0
|
260 |
# runs the SELECT in a loop enough times that an assert() fails. Or rather,
|
sl@0
|
261 |
# did fail before the bug was fixed.
|
sl@0
|
262 |
#
|
sl@0
|
263 |
do_test where4-7.2 {
|
sl@0
|
264 |
execsql {
|
sl@0
|
265 |
SELECT sum((
|
sl@0
|
266 |
SELECT d FROM t8 WHERE a = i AND b = i AND c < NULL
|
sl@0
|
267 |
)) FROM t7;
|
sl@0
|
268 |
}
|
sl@0
|
269 |
} {{}}
|
sl@0
|
270 |
|
sl@0
|
271 |
}; #ifcapable subquery
|
sl@0
|
272 |
|
sl@0
|
273 |
finish_test
|