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 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the use of indices in WHERE clauses
13 # based on recent changes to the optimizer.
15 # $Id: where2.test,v 1.15 2009/02/02 01:50:40 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Build some test data
25 CREATE TABLE t1(w int, x int, y int, z int);
27 for {set i 1} {$i<=100} {incr i} {
29 set x [expr {int(log($i)/log(2))}]
30 set y [expr {$i*$i + 2*$i + 1}]
33 execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)}
35 execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)}
39 CREATE UNIQUE INDEX i1w ON t1(w);
40 CREATE INDEX i1xy ON t1(x,y);
41 CREATE INDEX i1zyx ON t1(z,y,x);
46 # Do an SQL statement. Append the search count to the end of the result.
49 set ::sqlite_search_count 0
50 return [concat [execsql $sql] $::sqlite_search_count]
53 # This procedure executes the SQL. Then it checks to see if the OP_Sort
54 # opcode was executed. If an OP_Sort did occur, then "sort" is appended
55 # to the result. If no OP_Sort happened, then "nosort" is appended.
57 # This procedure is used to check to make sure sorting is or is not
58 # occurring as expected.
61 set data [execsql $sql]
62 if {[db status sort]} {set x sort} {set x nosort}
67 # This procedure executes the SQL. Then it appends to the result the
68 # "sort" or "nosort" keyword (as in the cksort procedure above) then
69 # it appends the ::sqlite_query_plan variable.
71 proc queryplan {sql} {
72 set ::sqlite_sort_count 0
73 set data [execsql $sql]
74 if {$::sqlite_sort_count} {set x sort} {set x nosort}
76 return [concat $data $::sqlite_query_plan]
80 # Prefer a UNIQUE index over another index.
84 SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396
86 } {85 6 7396 7402 nosort t1 i1w}
88 # Always prefer a rowid== constraint over any other index.
92 SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 AND rowid=85
94 } {85 6 7396 7402 nosort t1 *}
96 # When constrained by a UNIQUE index, the ORDER BY clause is always ignored.
100 SELECT * FROM t1 WHERE w=85 ORDER BY random();
102 } {85 6 7396 7402 nosort t1 i1w}
105 SELECT * FROM t1 WHERE x=6 AND y=7396 ORDER BY random();
107 } {85 6 7396 7402 sort t1 i1xy}
110 SELECT * FROM t1 WHERE rowid=85 AND x=6 AND y=7396 ORDER BY random();
112 } {85 6 7396 7402 nosort t1 *}
115 # Efficient handling of forward and reverse table scans.
119 SELECT * FROM t1 ORDER BY rowid LIMIT 2
121 } {1 0 4 4 2 1 9 10 nosort t1 *}
124 SELECT * FROM t1 ORDER BY rowid DESC LIMIT 2
126 } {100 6 10201 10207 99 6 10000 10006 nosort t1 *}
128 # The IN operator can be used by indices at multiple layers
133 SELECT * FROM t1 WHERE z IN (10207,10006) AND y IN (10000,10201)
137 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
140 SELECT * FROM t1 WHERE z IN (10207,10006) AND y=10000
144 } {99 6 10000 10006 sort t1 i1zyx}
147 SELECT * FROM t1 WHERE z=10006 AND y IN (10000,10201)
151 } {99 6 10000 10006 sort t1 i1zyx}
155 SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
156 AND y IN (10000,10201)
160 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
163 SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
164 AND y IN (SELECT 10000 UNION SELECT 10201)
168 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
173 WHERE x IN (1,2,3,4,5,6,7,8)
174 AND y IN (10000,10001,10002,10003,10004,10005)
177 } {99 6 10000 10006 sort t1 i1xy}
179 # Duplicate entires on the RHS of an IN operator do not cause duplicate
184 SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
187 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
191 SELECT * FROM t1 WHERE z IN (
192 SELECT 10207 UNION ALL SELECT 10006
193 UNION ALL SELECT 10006 UNION ALL SELECT 10207)
196 } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
199 } ;# ifcapable subquery
201 # The use of an IN operator disables the index as a sorter.
205 SELECT * FROM t1 WHERE w=99 ORDER BY w
207 } {99 6 10000 10006 nosort t1 i1w}
212 SELECT * FROM t1 WHERE w IN (99) ORDER BY w
214 } {99 6 10000 10006 sort t1 i1w}
217 # Verify that OR clauses get translated into IN operators.
220 ifcapable subquery {set ::idx i1w}
221 do_test where2-6.1.1 {
223 SELECT * FROM t1 WHERE w=99 OR w=100 ORDER BY +w
225 } [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
226 do_test where2-6.1.2 {
228 SELECT * FROM t1 WHERE 99=w OR 100=w ORDER BY +w
230 } [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
233 SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=w ORDER BY +w
235 } [list 6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
239 SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=+w ORDER BY +w
241 } {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}}
244 SELECT * FROM t1 WHERE w=99 OR +w=100 OR 6=w ORDER BY +w
246 } {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}}
249 ifcapable subquery {set ::idx i1zyx}
252 SELECT b.* FROM t1 a, t1 b
253 WHERE a.w=1 AND (a.y=b.z OR b.z=10)
256 } [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
259 SELECT b.* FROM t1 a, t1 b
260 WHERE a.w=1 AND (b.z=10 OR a.y=b.z OR b.z=10)
263 } [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
265 # Ticket #2249. Make sure the OR optimization is not attempted if
266 # comparisons between columns of different affinities are needed.
270 CREATE TABLE t2249a(a TEXT UNIQUE);
271 CREATE TABLE t2249b(b INTEGER);
272 INSERT INTO t2249a VALUES('0123');
273 INSERT INTO t2249b VALUES(123);
276 -- Because a is type TEXT and b is type INTEGER, both a and b
277 -- will attempt to convert to NUMERIC before the comparison.
278 -- They will thus compare equal.
280 SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b;
282 } {123 0123 nosort t2249b {} t2249a {}}
285 -- The + operator removes affinity from the rhs. No conversions
286 -- occur and the comparison is false. The result is an empty set.
288 SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b;
290 } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
291 do_test where2-6.9.2 {
292 # The same thing but with the expression flipped around.
294 SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a
296 } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
297 do_test where2-6.10 {
299 -- Use + on both sides of the comparison to disable indices
300 -- completely. Make sure we get the same result.
302 SELECT * FROM t2249b CROSS JOIN t2249a WHERE +a=+b;
304 } {nosort t2249b {} t2249a {}}
305 do_test where2-6.11 {
306 # This will not attempt the OR optimization because of the a=b
309 SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b OR a='hello';
311 } {123 0123 nosort t2249b {} t2249a {}}
312 do_test where2-6.11.2 {
313 # Permutations of the expression terms.
315 SELECT * FROM t2249b CROSS JOIN t2249a WHERE b=a OR a='hello';
317 } {123 0123 nosort t2249b {} t2249a {}}
318 do_test where2-6.11.3 {
319 # Permutations of the expression terms.
321 SELECT * FROM t2249b CROSS JOIN t2249a WHERE 'hello'=a OR b=a;
323 } {123 0123 nosort t2249b {} t2249a {}}
324 do_test where2-6.11.4 {
325 # Permutations of the expression terms.
327 SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR b=a;
329 } {123 0123 nosort t2249b {} t2249a {}}
330 ifcapable explain&&subquery {
331 # These tests are not run if subquery support is not included in the
332 # build. This is because these tests test the "a = 1 OR a = 2" to
333 # "a IN (1, 2)" optimisation transformation, which is not enabled if
334 # subqueries and the IN operator is not available.
336 do_test where2-6.12 {
337 # In this case, the +b disables the affinity conflict and allows
338 # the OR optimization to be used again. The result is now an empty
339 # set, the same as in where2-6.9.
341 SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR a='hello';
343 } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
344 do_test where2-6.12.2 {
345 # In this case, the +b disables the affinity conflict and allows
346 # the OR optimization to be used again. The result is now an empty
347 # set, the same as in where2-6.9.
349 SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR +b=a;
351 } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
352 do_test where2-6.12.3 {
353 # In this case, the +b disables the affinity conflict and allows
354 # the OR optimization to be used again. The result is now an empty
355 # set, the same as in where2-6.9.
357 SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a OR a='hello';
359 } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
360 do_test where2-6.13 {
361 # The addition of +a on the second term disabled the OR optimization.
362 # But we should still get the same empty-set result as in where2-6.9.
364 SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR +a='hello';
366 } {nosort t2249b {} t2249a {}}
369 # Variations on the order of terms in a WHERE clause in order
370 # to make sure the OR optimizer can recognize them all.
371 do_test where2-6.20 {
373 SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a
375 } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
376 ifcapable explain&&subquery {
377 # These tests are not run if subquery support is not included in the
378 # build. This is because these tests test the "a = 1 OR a = 2" to
379 # "a IN (1, 2)" optimisation transformation, which is not enabled if
380 # subqueries and the IN operator is not available.
382 do_test where2-6.21 {
384 SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a OR y.a='hello'
386 } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
387 do_test where2-6.22 {
389 SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a=x.a OR y.a='hello'
391 } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
392 do_test where2-6.23 {
394 SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a='hello' OR x.a=y.a
396 } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
399 # Unique queries (queries that are guaranteed to return only a single
400 # row of result) do not call the sorter. But all tables must give
401 # a unique result. If any one table in the join does not give a unique
402 # result then sorting is necessary.
406 create table t8(a unique, b, c);
407 insert into t8 values(1,2,3);
408 insert into t8 values(2,3,4);
409 create table t9(x,y);
410 insert into t9 values(2,4);
411 insert into t9 values(2,3);
412 select y from t8, t9 where a=1 order by a, y;
417 select * from t8 where a=1 order by b, c
422 select * from t8, t9 where a=1 and y=3 order by b, x
427 create unique index i9y on t9(y);
428 select * from t8, t9 where a=1 and y=3 order by b, x
432 # Ticket #1807. Using IN constrains on multiple columns of
433 # a multi-column index.
438 SELECT * FROM t1 WHERE x IN (20,21) AND y IN (1,2)
443 SELECT * FROM t1 WHERE x IN (1,2) AND y IN (-5,-6)
446 execsql {CREATE TABLE tx AS SELECT * FROM t1}
450 WHERE x IN (SELECT x FROM tx WHERE rowid<0)
451 AND +y IN (SELECT y FROM tx WHERE rowid=1)
457 WHERE x IN (SELECT x FROM tx WHERE rowid=1)
458 AND y IN (SELECT y FROM tx WHERE rowid<0)
461 #set sqlite_where_trace 1
464 CREATE INDEX tx_xyz ON tx(x, y, z, w);
466 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
467 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
468 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 12 AND 14)
474 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
475 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 12 AND 14)
476 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
482 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 12 AND 14)
483 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
484 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
486 } {10 11 12 13 14 15}
490 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
491 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
492 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
494 } {10 11 12 13 14 15 16 17 18 19 20}
498 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
499 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
500 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 2 AND 4)
503 do_test where2-8.10 {
506 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
507 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 2 AND 4)
508 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
511 do_test where2-8.11 {
514 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 2 AND 4)
515 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
516 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
519 do_test where2-8.12 {
522 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
523 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
524 AND z IN (SELECT z FROM t1 WHERE w BETWEEN -4 AND -2)
527 do_test where2-8.13 {
530 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
531 AND y IN (SELECT y FROM t1 WHERE w BETWEEN -4 AND -2)
532 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
535 do_test where2-8.14 {
538 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN -4 AND -2)
539 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
540 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
543 do_test where2-8.15 {
546 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
547 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
548 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 200 AND 300)
551 do_test where2-8.16 {
554 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
555 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 200 AND 300)
556 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
559 do_test where2-8.17 {
562 WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 200 AND 300)
563 AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
564 AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
567 do_test where2-8.18 {
570 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
571 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
572 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 200 AND 300)
575 do_test where2-8.19 {
578 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
579 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 200 AND 300)
580 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
583 do_test where2-8.20 {
586 WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 200 AND 300)
587 AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
588 AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
593 # Make sure WHERE clauses of the form A=1 AND (B=2 OR B=3) are optimized
594 # when we have an index on A and B.
596 ifcapable or_opt&&tclvar {
600 CREATE TABLE t10(a,b,c);
601 INSERT INTO t10 VALUES(1,1,1);
602 INSERT INTO t10 VALUES(1,2,2);
603 INSERT INTO t10 VALUES(1,3,3);
605 for {set i 4} {$i<=1000} {incr i} {
606 execsql {INSERT INTO t10 VALUES(1,$i,$i)}
609 CREATE INDEX i10 ON t10(a,b);
611 SELECT count(*) FROM t10;
617 SELECT * FROM t10 WHERE a=1 AND (b=2 OR b=3)
623 # Indices with redundant columns
625 do_test where2-11.1 {
627 CREATE TABLE t11(a,b,c,d);
628 CREATE INDEX i11aba ON t11(a,b,a,c); -- column A occurs twice.
629 INSERT INTO t11 VALUES(1,2,3,4);
630 INSERT INTO t11 VALUES(5,6,7,8);
631 INSERT INTO t11 VALUES(1,2,9,10);
632 INSERT INTO t11 VALUES(5,11,12,13);
633 SELECT c FROM t11 WHERE a=1 AND b=2 ORDER BY c;
636 do_test where2-11.2 {
638 CREATE INDEX i11cccccccc ON t11(c,c,c,c,c,c,c,c); -- repeated column
639 SELECT d FROM t11 WHERE c=9;
642 do_test where2-11.3 {
644 SELECT d FROM t11 WHERE c IN (1,2,3,4,5);
647 do_test where2-11.4 {
649 SELECT d FROM t11 WHERE c=7 OR (a=1 AND b=2) ORDER BY d;