blob: 5e9776d41dc537dcd0c9bed9263672580e4e326b [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh6de4f4c2000-06-12 12:20:48 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh6de4f4c2000-06-12 12:20:48 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh6de4f4c2000-06-12 12:20:48 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the use of indices in WHERE clases.
13#
drhb19a2bc2001-09-16 00:13:26 +000014# $Id: where.test,v 1.3 2001/09/16 00:13:28 drh Exp $
drh6de4f4c2000-06-12 12:20:48 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Build some test data
20#
21do_test where-1.0 {
22 execsql {
23 CREATE TABLE t1(w int, x int, y int);
24 CREATE TABLE t2(p int, q int, r int, s int);
25 }
26 for {set i 1} {$i<=100} {incr i} {
27 set w $i
28 set x [expr {int(log($i)/log(2))}]
29 set y [expr {$i*$i + 2*$i + 1}]
30 execsql "INSERT INTO t1 VALUES($w,$x,$y)"
31 }
32 execsql {
33 INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
34 CREATE INDEX i1w ON t1(w);
35 CREATE INDEX i1xy ON t1(x,y);
36 CREATE INDEX i2p ON t2(p);
37 CREATE INDEX i2r ON t2(r);
38 CREATE INDEX i2qs ON t2(q, s);
39 }
40} {}
41
42# Verify that queries use an index. We are using the special "fcnt(*)"
43# function to verify the results. fcnt(*) returns the number of Fetch
44# operations that have occurred up to the point where fcnt(*) is invoked.
45# By verifing that fcnt(*) returns a small number we know that an index
46# was used instead of an exhaustive search.
47#
48do_test where-1.1 {
49 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w=10}
drhc87fa692001-08-19 18:19:46 +000050} {3 121 1}
drh6de4f4c2000-06-12 12:20:48 +000051do_test where-1.2 {
52 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w=11}
drhc87fa692001-08-19 18:19:46 +000053} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000054do_test where-1.3 {
55 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE 11=w}
drhc87fa692001-08-19 18:19:46 +000056} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000057do_test where-1.4 {
58 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE 11=w AND x>2}
drhc87fa692001-08-19 18:19:46 +000059} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000060do_test where-1.5 {
61 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE y<200 AND w=11 AND x>2}
drhc87fa692001-08-19 18:19:46 +000062} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000063do_test where-1.6 {
64 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE y<200 AND x>2 AND w=11}
drhc87fa692001-08-19 18:19:46 +000065} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000066do_test where-1.7 {
67 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w=11 AND y<200 AND x>2}
drhc87fa692001-08-19 18:19:46 +000068} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000069do_test where-1.8 {
70 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w>10 AND y=144 AND x=3}
drhc87fa692001-08-19 18:19:46 +000071} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000072do_test where-1.9 {
73 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE y=144 AND w>10 AND x=3}
drhc87fa692001-08-19 18:19:46 +000074} {3 144 1}
drh6de4f4c2000-06-12 12:20:48 +000075do_test where-1.10 {
76 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE x=3 AND w>=10 AND y=121}
drhc87fa692001-08-19 18:19:46 +000077} {3 121 1}
drh6de4f4c2000-06-12 12:20:48 +000078do_test where-1.11 {
79 execsql {SELECT x, y, fcnt(*) FROM t1 WHERE x=3 AND y=100 AND w<10}
drhc87fa692001-08-19 18:19:46 +000080} {3 100 1}
drh6de4f4c2000-06-12 12:20:48 +000081
82# Do the same kind of thing except use a join as the data source.
83#
84do_test where-2.1 {
85 execsql {
86 SELECT w, p, fcnt(*) FROM t2, t1
87 WHERE x=q AND y=s AND r=8977
88 }
drhc87fa692001-08-19 18:19:46 +000089} {34 67 2}
drh6de4f4c2000-06-12 12:20:48 +000090do_test where-2.2 {
91 execsql {
92 SELECT w, p, fcnt(*) FROM t2, t1
93 WHERE x=q AND s=y AND r=8977
94 }
drhc87fa692001-08-19 18:19:46 +000095} {34 67 2}
drh6de4f4c2000-06-12 12:20:48 +000096do_test where-2.3 {
97 execsql {
98 SELECT w, p, fcnt(*) FROM t2, t1
99 WHERE x=q AND s=y AND r=8977 AND w>10
100 }
drhc87fa692001-08-19 18:19:46 +0000101} {34 67 2}
drh6de4f4c2000-06-12 12:20:48 +0000102do_test where-2.4 {
103 execsql {
104 SELECT w, p, fcnt(*) FROM t2, t1
105 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
106 }
drhc87fa692001-08-19 18:19:46 +0000107} {34 67 2}
drh6de4f4c2000-06-12 12:20:48 +0000108do_test where-2.5 {
109 execsql {
110 SELECT w, p, fcnt(*) FROM t2, t1
111 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
112 }
drhc87fa692001-08-19 18:19:46 +0000113} {34 67 2}
drh6de4f4c2000-06-12 12:20:48 +0000114do_test where-2.6 {
115 execsql {
116 SELECT w, p, fcnt(*) FROM t2, t1
117 WHERE x=q AND p=77 AND s=y AND w>5
118 }
drhc87fa692001-08-19 18:19:46 +0000119} {24 77 2}
drh6de4f4c2000-06-12 12:20:48 +0000120do_test where-2.7 {
121 execsql {
122 SELECT w, p, fcnt(*) FROM t1, t2
123 WHERE x=q AND p>77 AND s=y AND w=5
124 }
drhc87fa692001-08-19 18:19:46 +0000125} {5 96 2}
drh6de4f4c2000-06-12 12:20:48 +0000126
127# Lets do a 3-way join.
128#
129do_test where-3.1 {
130 execsql {
131 SELECT A.w, B.p, C.w, fcnt(*) FROM t1 as A, t2 as B, t1 as C
132 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
133 }
drhc87fa692001-08-19 18:19:46 +0000134} {11 90 11 3}
drh6de4f4c2000-06-12 12:20:48 +0000135do_test where-3.2 {
136 execsql {
137 SELECT A.w, B.p, C.w, fcnt(*) FROM t1 as A, t2 as B, t1 as C
138 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
139 }
drhc87fa692001-08-19 18:19:46 +0000140} {12 89 12 3}
drh6de4f4c2000-06-12 12:20:48 +0000141do_test where-3.3 {
142 execsql {
143 SELECT A.w, B.p, C.w, fcnt(*) FROM t1 as A, t2 as B, t1 as C
144 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
145 }
drhc87fa692001-08-19 18:19:46 +0000146} {15 86 86 3}
drh6de4f4c2000-06-12 12:20:48 +0000147
148finish_test