drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 1 | # 2001 September 15 |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 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. |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 9 | # |
| 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 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 14 | # $Id: where.test,v 1.3 2001/09/16 00:13:28 drh Exp $ |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | # Build some test data |
| 20 | # |
| 21 | do_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 | # |
| 48 | do_test where-1.1 { |
| 49 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w=10} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 50 | } {3 121 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 51 | do_test where-1.2 { |
| 52 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w=11} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 53 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 54 | do_test where-1.3 { |
| 55 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE 11=w} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 56 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 57 | do_test where-1.4 { |
| 58 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE 11=w AND x>2} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 59 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 60 | do_test where-1.5 { |
| 61 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE y<200 AND w=11 AND x>2} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 62 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 63 | do_test where-1.6 { |
| 64 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE y<200 AND x>2 AND w=11} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 65 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 66 | do_test where-1.7 { |
| 67 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w=11 AND y<200 AND x>2} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 68 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 69 | do_test where-1.8 { |
| 70 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE w>10 AND y=144 AND x=3} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 71 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 72 | do_test where-1.9 { |
| 73 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE y=144 AND w>10 AND x=3} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 74 | } {3 144 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 75 | do_test where-1.10 { |
| 76 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE x=3 AND w>=10 AND y=121} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 77 | } {3 121 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 78 | do_test where-1.11 { |
| 79 | execsql {SELECT x, y, fcnt(*) FROM t1 WHERE x=3 AND y=100 AND w<10} |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 80 | } {3 100 1} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 81 | |
| 82 | # Do the same kind of thing except use a join as the data source. |
| 83 | # |
| 84 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 89 | } {34 67 2} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 90 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 95 | } {34 67 2} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 96 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 101 | } {34 67 2} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 102 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 107 | } {34 67 2} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 108 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 113 | } {34 67 2} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 114 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 119 | } {24 77 2} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 120 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 125 | } {5 96 2} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 126 | |
| 127 | # Lets do a 3-way join. |
| 128 | # |
| 129 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 134 | } {11 90 11 3} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 135 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 140 | } {12 89 12 3} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 141 | do_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 | } |
drh | c87fa69 | 2001-08-19 18:19:46 +0000 | [diff] [blame] | 146 | } {15 86 86 3} |
drh | 6de4f4c | 2000-06-12 12:20:48 +0000 | [diff] [blame] | 147 | |
| 148 | finish_test |