2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the use of indices in WHERE clases.
17 # $Id: where.test,v 1.17 2003/06/15 23:42:25 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Build some test data
26 CREATE TABLE t1(w int, x int, y int);
27 CREATE TABLE t2(p int, q int, r int, s int);
29 for {set i 1} {$i<=100} {incr i} {
31 set x [expr {int(log($i)/log(2))}]
32 set y [expr {$i*$i + 2*$i + 1}]
33 execsql "INSERT INTO t1 VALUES($w,$x,$y)"
36 INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
37 CREATE INDEX i1w ON t1(w);
38 CREATE INDEX i1xy ON t1(x,y);
39 CREATE INDEX i2p ON t2(p);
40 CREATE INDEX i2r ON t2(r);
41 CREATE INDEX i2qs ON t2(q, s);
45 # Do an SQL statement. Append the search count to the end of the result.
48 set ::sqlite_search_count 0
49 return [concat [execsql $sql] $::sqlite_search_count]
52 # 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 # 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.
58 count {SELECT x, y FROM t1 WHERE w=10}
61 count {SELECT x, y FROM t1 WHERE w=11}
64 count {SELECT x, y FROM t1 WHERE 11=w}
67 count {SELECT x, y FROM t1 WHERE 11=w AND x>2}
70 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
73 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
76 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
79 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
82 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
85 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
88 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
91 # New for SQLite version 2.1: Verify that that inequality constraints
95 count {SELECT w FROM t1 WHERE x=3 AND y<100}
98 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
101 count {SELECT w FROM t1 WHERE 3=x AND y<100}
104 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
107 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
110 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
113 count {SELECT w FROM t1 WHERE x=3 AND y>225}
116 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
119 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
122 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
125 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
128 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
131 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
134 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
137 # Need to work on optimizing the BETWEEN operator.
139 # do_test where-1.26 {
140 # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
144 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
147 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
150 count {SELECT w FROM t1 WHERE y==121}
155 count {SELECT w FROM t1 WHERE w>97}
158 count {SELECT w FROM t1 WHERE w>=97}
161 count {SELECT w FROM t1 WHERE w==97}
164 count {SELECT w FROM t1 WHERE w+1==98}
167 count {SELECT w FROM t1 WHERE w<3}
170 count {SELECT w FROM t1 WHERE w<=3}
173 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
177 count {SELECT (w) FROM t1 WHERE (w)>(97)}
180 count {SELECT (w) FROM t1 WHERE (w)>=(97)}
183 count {SELECT (w) FROM t1 WHERE (w)==(97)}
186 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
190 # Do the same kind of thing except use a join as the data source.
194 SELECT w, p FROM t2, t1
195 WHERE x=q AND y=s AND r=8977
200 SELECT w, p FROM t2, t1
201 WHERE x=q AND s=y AND r=8977
206 SELECT w, p FROM t2, t1
207 WHERE x=q AND s=y AND r=8977 AND w>10
212 SELECT w, p FROM t2, t1
213 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
218 SELECT w, p FROM t2, t1
219 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
224 SELECT w, p FROM t2, t1
225 WHERE x=q AND p=77 AND s=y AND w>5
230 SELECT w, p FROM t1, t2
231 WHERE x=q AND p>77 AND s=y AND w=5
235 # Lets do a 3-way join.
239 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
240 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
245 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
246 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
251 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
252 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
256 # Test to see that the special case of a constant WHERE clause is
261 SELECT * FROM t1 WHERE 0
266 SELECT * FROM t1 WHERE 1 LIMIT 1
280 # Verify that IN operators in a WHERE clause are handled correctly.
284 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
286 } {1 0 4 2 1 9 3 1 16 0}
289 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
291 } {1 0 4 2 1 9 3 1 16 199}
294 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
296 } {1 0 4 2 1 9 3 1 16 10}
299 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
301 } {1 0 4 2 1 9 3 1 16 199}
304 SELECT * FROM t1 WHERE rowid IN
305 (select rowid from t1 where rowid IN (-1,2,4))
311 SELECT * FROM t1 WHERE rowid+0 IN
312 (select rowid from t1 where rowid IN (-1,2,4))
318 SELECT * FROM t1 WHERE w IN
319 (select rowid from t1 where rowid IN (-1,2,4))
325 SELECT * FROM t1 WHERE w+0 IN
326 (select rowid from t1 where rowid IN (-1,2,4))
332 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
337 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
342 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
344 } {79 6 6400 89 6 8100 199}
347 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
349 } {79 6 6400 89 6 8100 74}
352 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
357 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
361 # This procedure executes the SQL. Then it checks the generated program
362 # for the SQL and appends a "nosort" to the result if the program contains the
363 # SortCallback opcode. If the program does not contain the SortCallback
364 # opcode it appends "sort"
367 set data [execsql $sql]
368 set prog [execsql "EXPLAIN $sql"]
369 if {[regexp SortCallback $prog]} {set x sort} {set x nosort}
373 # Check out the logic that attempts to implement the ORDER BY clause
374 # using an index rather than by sorting.
378 CREATE TABLE t3(a,b,c);
379 CREATE INDEX t3a ON t3(a);
380 CREATE INDEX t3bc ON t3(b,c);
381 CREATE INDEX t3acb ON t3(a,c,b);
382 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
383 SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
385 } {100 5050 5050 348550}
388 SELECT * FROM t3 ORDER BY a LIMIT 3
390 } {1 100 4 2 99 9 3 98 16 nosort}
393 SELECT * FROM t3 ORDER BY a+1 LIMIT 3
395 } {1 100 4 2 99 9 3 98 16 sort}
398 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
400 } {1 100 4 2 99 9 3 98 16 nosort}
403 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
405 } {1 100 4 2 99 9 3 98 16 nosort}
408 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
410 } {1 100 4 2 99 9 3 98 16 nosort}
413 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
415 } {1 100 4 2 99 9 3 98 16 sort}
418 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
420 } {1 100 4 2 99 9 3 98 16 sort}
421 do_test where-6.9.1 {
423 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
426 do_test where-6.9.2 {
428 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
431 do_test where-6.9.3 {
433 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
436 do_test where-6.9.4 {
438 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
441 do_test where-6.9.5 {
443 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
446 do_test where-6.9.6 {
448 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
451 do_test where-6.9.7 {
453 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
456 do_test where-6.9.8 {
458 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
461 do_test where-6.9.9 {
463 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
468 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
473 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
478 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
483 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
485 } {100 1 10201 99 2 10000 98 3 9801 nosort}
486 do_test where-6.13.1 {
488 SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
490 } {100 1 10201 99 2 10000 98 3 9801 sort}
493 SELECT * FROM t3 ORDER BY b LIMIT 3
495 } {100 1 10201 99 2 10000 98 3 9801 nosort}
498 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
500 } {1 0 2 1 3 1 nosort}
503 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
508 SELECT y FROM t1 ORDER BY w COLLATE text LIMIT 3;
513 SELECT y FROM t1 ORDER BY w COLLATE numeric LIMIT 3;
518 SELECT y FROM t1 ORDER BY w LIMIT 3;
522 # Tests for reverse-order sorting.
526 SELECT w FROM t1 WHERE x=3 ORDER BY y;
528 } {8 9 10 11 12 13 14 15 nosort}
531 SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
533 } {15 14 13 12 11 10 9 8 nosort}
536 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
541 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
546 SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
548 } {15 14 13 12 11 nosort}
551 SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
553 } {15 14 13 12 11 10 nosort}
556 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
561 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
563 } {13 12 11 10 nosort}
566 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
571 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
576 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
581 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
583 } {10 11 12 13 nosort}
586 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
591 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
596 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
601 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
606 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
611 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
616 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
621 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
626 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
631 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
636 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
641 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
646 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
651 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
656 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
661 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
666 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
671 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
677 CREATE TABLE t4 AS SELECT * FROM t1;
678 CREATE INDEX i4xy ON t4(x,y);
681 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
689 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
693 # Make sure searches with an index work with an empty table.
697 CREATE TABLE t5(x PRIMARY KEY);
698 SELECT * FROM t5 WHERE x<10;
703 SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
708 SELECT * FROM t5 WHERE x=10;
714 SELECT 1 WHERE abs(random())<0
718 proc tclvar_func {vname} {return [set ::$vname]}
719 db function tclvar tclvar_func
722 SELECT count(*) FROM t1 WHERE tclvar('v1');
728 SELECT count(*) FROM t1 WHERE tclvar('v1');
733 proc tclvar_func {vname} {
739 SELECT count(*) FROM t1 WHERE tclvar('v1');
743 integrity_check {where-99.0}