OLD | NEW |
1 # 2006 October 27 | 1 # 2006 October 27 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # This file implements regression tests for SQLite library. The | 11 # This file implements regression tests for SQLite library. The |
12 # focus of this file is testing the use of indices in WHERE clauses. | 12 # focus of this file is testing the use of indices in WHERE clauses. |
13 # This file was created when support for optimizing IS NULL phrases | 13 # This file was created when support for optimizing IS NULL phrases |
14 # was added. And so the principle purpose of this file is to test | 14 # was added. And so the principle purpose of this file is to test |
15 # that IS NULL phrases are correctly optimized. But you can never | 15 # that IS NULL phrases are correctly optimized. But you can never |
16 # have too many tests, so some other tests are thrown in as well. | 16 # have too many tests, so some other tests are thrown in as well. |
17 # | 17 # |
18 # $Id: where4.test,v 1.6 2007/12/10 05:03:48 danielk1977 Exp $ | 18 # $Id: where4.test,v 1.6 2007/12/10 05:03:48 danielk1977 Exp $ |
19 | 19 |
20 set testdir [file dirname $argv0] | 20 set testdir [file dirname $argv0] |
21 source $testdir/tester.tcl | 21 source $testdir/tester.tcl |
| 22 set testprefix where4 |
22 | 23 |
23 ifcapable !tclvar||!bloblit { | 24 ifcapable !tclvar||!bloblit { |
24 finish_test | 25 finish_test |
25 return | 26 return |
26 } | 27 } |
27 | 28 |
28 # Build some test data | 29 # Build some test data |
29 # | 30 # |
30 do_test where4-1.0 { | 31 do_test where4-1.0 { |
31 execsql { | 32 execsql { |
(...skipping 18 matching lines...) Expand all Loading... |
50 } | 51 } |
51 | 52 |
52 # Verify that queries use an index. We are using the special variable | 53 # Verify that queries use an index. We are using the special variable |
53 # "sqlite_search_count" which tallys the number of executions of MoveTo | 54 # "sqlite_search_count" which tallys the number of executions of MoveTo |
54 # and Next operators in the VDBE. By verifing that the search count is | 55 # and Next operators in the VDBE. By verifing that the search count is |
55 # small we can be assured that indices are being used properly. | 56 # small we can be assured that indices are being used properly. |
56 # | 57 # |
57 do_test where4-1.1 { | 58 do_test where4-1.1 { |
58 count {SELECT rowid FROM t1 WHERE w IS NULL} | 59 count {SELECT rowid FROM t1 WHERE w IS NULL} |
59 } {7 2} | 60 } {7 2} |
| 61 do_test where4-1.1b { |
| 62 unset -nocomplain null |
| 63 count {SELECT rowid FROM t1 WHERE w IS $null} |
| 64 } {7 2} |
60 do_test where4-1.2 { | 65 do_test where4-1.2 { |
61 count {SELECT rowid FROM t1 WHERE +w IS NULL} | 66 count {SELECT rowid FROM t1 WHERE +w IS NULL} |
62 } {7 6} | 67 } {7 6} |
63 do_test where4-1.3 { | 68 do_test where4-1.3 { |
64 count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL} | 69 count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL} |
65 } {2 2} | 70 } {2 2} |
66 do_test where4-1.4 { | 71 do_test where4-1.4 { |
67 count {SELECT rowid FROM t1 WHERE w=1 AND +x IS NULL} | 72 count {SELECT rowid FROM t1 WHERE w=1 AND +x IS NULL} |
68 } {2 3} | 73 } {2 3} |
69 do_test where4-1.5 { | 74 do_test where4-1.5 { |
70 count {SELECT rowid FROM t1 WHERE w=1 AND x>0} | 75 count {SELECT rowid FROM t1 WHERE w=1 AND x>0} |
71 } {1 2} | 76 } {1 2} |
72 do_test where4-1.6 { | 77 do_test where4-1.6 { |
73 count {SELECT rowid FROM t1 WHERE w=1 AND x<9} | 78 count {SELECT rowid FROM t1 WHERE w=1 AND x<9} |
74 } {1 2} | 79 } {1 2} |
75 do_test where4-1.7 { | 80 do_test where4-1.7 { |
76 count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y=3} | 81 count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y=3} |
77 } {2 2} | 82 } {2 2} |
78 do_test where4-1.8 { | 83 do_test where4-1.8 { |
79 count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y>2} | 84 count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y>2} |
80 } {2 2} | 85 } {2 2} |
81 do_test where4-1.9 { | 86 do_test where4-1.9 { |
82 count {SELECT rowid FROM t1 WHERE w='a' AND x IS NULL AND y='c'} | 87 count {SELECT rowid FROM t1 WHERE w='a' AND x IS NULL AND y='c'} |
83 } {4 2} | 88 } {4 2} |
84 do_test where4-1.10 { | 89 do_test where4-1.10 { |
85 count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL} | 90 count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL} |
86 } {6 2} | 91 } {6 2} |
87 do_test where4-1.11 { | 92 do_test where4-1.11 { |
88 count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=123} | 93 count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=123} |
89 } {1} | 94 } {0} |
90 do_test where4-1.12 { | 95 do_test where4-1.12 { |
91 count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=x'7A'} | 96 count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=x'7A'} |
92 } {6 2} | 97 } {6 2} |
93 do_test where4-1.13 { | 98 do_test where4-1.13 { |
94 count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL} | 99 count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL} |
95 } {7 2} | 100 } {7 2} |
96 do_test where4-1.14 { | 101 do_test where4-1.14 { |
97 count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y IS NULL} | 102 count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y IS NULL} |
98 } {7 2} | 103 } {7 2} |
99 do_test where4-1.15 { | 104 do_test where4-1.15 { |
(...skipping 24 matching lines...) Expand all Loading... |
124 # optimize the query because the second column might be NULL because | 129 # optimize the query because the second column might be NULL because |
125 # the right table did not match - something the index does not know | 130 # the right table did not match - something the index does not know |
126 # about. | 131 # about. |
127 # | 132 # |
128 do_test where4-3.1 { | 133 do_test where4-3.1 { |
129 execsql { | 134 execsql { |
130 CREATE TABLE t2(a); | 135 CREATE TABLE t2(a); |
131 INSERT INTO t2 VALUES(1); | 136 INSERT INTO t2 VALUES(1); |
132 INSERT INTO t2 VALUES(2); | 137 INSERT INTO t2 VALUES(2); |
133 INSERT INTO t2 VALUES(3); | 138 INSERT INTO t2 VALUES(3); |
134 CREATE TABLE t3(x,y,UNIQUE(x,y)); | 139 CREATE TABLE t3(x,y,UNIQUE("x",'y' ASC)); -- Goofy syntax allowed |
135 INSERT INTO t3 VALUES(1,11); | 140 INSERT INTO t3 VALUES(1,11); |
136 INSERT INTO t3 VALUES(2,NULL); | 141 INSERT INTO t3 VALUES(2,NULL); |
137 | 142 |
138 SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE +y IS NULL; | 143 SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE +y IS NULL; |
139 } | 144 } |
140 } {2 2 {} 3 {} {}} | 145 } {2 2 {} 3 {} {}} |
141 do_test where4-3.2 { | 146 do_test where4-3.2 { |
142 execsql { | 147 execsql { |
143 SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE y IS NULL; | 148 SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE y IS NULL; |
144 } | 149 } |
145 } {2 2 {} 3 {} {}} | 150 } {2 2 {} 3 {} {}} |
| 151 do_test where4-3.3 { |
| 152 execsql { |
| 153 SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE NULL is y; |
| 154 } |
| 155 } {2 2 {} 3 {} {}} |
| 156 do_test where4-3.4 { |
| 157 unset -nocomplain null |
| 158 execsql { |
| 159 SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE y IS $null; |
| 160 } |
| 161 } {2 2 {} 3 {} {}} |
146 | 162 |
147 # Ticket #2189. Probably the same bug as #2177. | 163 # Ticket #2189. Probably the same bug as #2177. |
148 # | 164 # |
149 do_test where4-4.1 { | 165 do_test where4-4.1 { |
150 execsql { | 166 execsql { |
151 CREATE TABLE test(col1 TEXT PRIMARY KEY); | 167 CREATE TABLE test(col1 TEXT PRIMARY KEY); |
152 INSERT INTO test(col1) values('a'); | 168 INSERT INTO test(col1) values('a'); |
153 INSERT INTO test(col1) values('b'); | 169 INSERT INTO test(col1) values('b'); |
154 INSERT INTO test(col1) values('c'); | 170 INSERT INTO test(col1) values('c'); |
155 CREATE TABLE test2(col1 TEXT PRIMARY KEY); | 171 CREATE TABLE test2(col1 TEXT PRIMARY KEY); |
(...skipping 21 matching lines...) Expand all Loading... |
177 SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1 | 193 SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1 |
178 WHERE t1.col1 IS NULL; | 194 WHERE t1.col1 IS NULL; |
179 } | 195 } |
180 } {} | 196 } {} |
181 | 197 |
182 # Ticket #2273. Problems with IN operators and NULLs. | 198 # Ticket #2273. Problems with IN operators and NULLs. |
183 # | 199 # |
184 ifcapable subquery { | 200 ifcapable subquery { |
185 do_test where4-5.1 { | 201 do_test where4-5.1 { |
186 execsql { | 202 execsql { |
187 CREATE TABLE t4(x,y,z,PRIMARY KEY(x,y)); | 203 -- Allow the 'x' syntax for backwards compatibility |
| 204 CREATE TABLE t4(x,y,z,PRIMARY KEY('x' ASC, "y" ASC)); |
188 } | 205 } |
189 execsql { | 206 execsql { |
190 SELECT * | 207 SELECT * |
191 FROM t2 LEFT JOIN t4 b1 | 208 FROM t2 LEFT JOIN t4 b1 |
192 LEFT JOIN t4 b2 ON b2.x=b1.x AND b2.y IN (b1.y); | 209 LEFT JOIN t4 b2 ON b2.x=b1.x AND b2.y IN (b1.y); |
193 } | 210 } |
194 } {1 {} {} {} {} {} {} 2 {} {} {} {} {} {} 3 {} {} {} {} {} {}} | 211 } {1 {} {} {} {} {} {} 2 {} {} {} {} {} {} 3 {} {} {} {} {} {}} |
195 do_test where4-5.2 { | 212 do_test where4-5.2 { |
196 execsql { | 213 execsql { |
197 INSERT INTO t4 VALUES(1,1,11); | 214 INSERT INTO t4 VALUES(1,1,11); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 do_test where4-7.2 { | 280 do_test where4-7.2 { |
264 execsql { | 281 execsql { |
265 SELECT sum(( | 282 SELECT sum(( |
266 SELECT d FROM t8 WHERE a = i AND b = i AND c < NULL | 283 SELECT d FROM t8 WHERE a = i AND b = i AND c < NULL |
267 )) FROM t7; | 284 )) FROM t7; |
268 } | 285 } |
269 } {{}} | 286 } {{}} |
270 | 287 |
271 }; #ifcapable subquery | 288 }; #ifcapable subquery |
272 | 289 |
| 290 #------------------------------------------------------------------------- |
| 291 # Verify that "IS ?" with a NULL bound to the variable also functions |
| 292 # correctly. |
| 293 |
| 294 unset -nocomplain null |
| 295 |
| 296 do_execsql_test 8.1 { |
| 297 CREATE TABLE u9(a UNIQUE, b); |
| 298 INSERT INTO u9 VALUES(NULL, 1); |
| 299 INSERT INTO u9 VALUES(NULL, 2); |
| 300 } |
| 301 do_execsql_test 8.2 { SELECT * FROM u9 WHERE a IS NULL } {{} 1 {} 2} |
| 302 do_execsql_test 8.2 { SELECT * FROM u9 WHERE a IS $null } {{} 1 {} 2} |
| 303 |
| 304 |
| 305 |
| 306 |
273 finish_test | 307 finish_test |
OLD | NEW |