sl@0: # 2005 July 28 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the use of indices in WHERE clauses sl@0: # based on recent changes to the optimizer. sl@0: # sl@0: # $Id: where2.test,v 1.13 2007/12/10 05:03:48 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Build some test data sl@0: # sl@0: do_test where2-1.0 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t1(w int, x int, y int, z int); sl@0: } sl@0: for {set i 1} {$i<=100} {incr i} { sl@0: set w $i sl@0: set x [expr {int(log($i)/log(2))}] sl@0: set y [expr {$i*$i + 2*$i + 1}] sl@0: set z [expr {$x+$y}] sl@0: ifcapable tclvar { sl@0: execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)} sl@0: } else { sl@0: execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)} sl@0: } sl@0: } sl@0: execsql { sl@0: CREATE UNIQUE INDEX i1w ON t1(w); sl@0: CREATE INDEX i1xy ON t1(x,y); sl@0: CREATE INDEX i1zyx ON t1(z,y,x); sl@0: COMMIT; sl@0: } sl@0: } {} sl@0: sl@0: # Do an SQL statement. Append the search count to the end of the result. sl@0: # sl@0: proc count sql { sl@0: set ::sqlite_search_count 0 sl@0: return [concat [execsql $sql] $::sqlite_search_count] sl@0: } sl@0: sl@0: # This procedure executes the SQL. Then it checks to see if the OP_Sort sl@0: # opcode was executed. If an OP_Sort did occur, then "sort" is appended sl@0: # to the result. If no OP_Sort happened, then "nosort" is appended. sl@0: # sl@0: # This procedure is used to check to make sure sorting is or is not sl@0: # occurring as expected. sl@0: # sl@0: proc cksort {sql} { sl@0: set ::sqlite_sort_count 0 sl@0: set data [execsql $sql] sl@0: if {$::sqlite_sort_count} {set x sort} {set x nosort} sl@0: lappend data $x sl@0: return $data sl@0: } sl@0: sl@0: # This procedure executes the SQL. Then it appends to the result the sl@0: # "sort" or "nosort" keyword (as in the cksort procedure above) then sl@0: # it appends the ::sqlite_query_plan variable. sl@0: # sl@0: proc queryplan {sql} { sl@0: set ::sqlite_sort_count 0 sl@0: set data [execsql $sql] sl@0: if {$::sqlite_sort_count} {set x sort} {set x nosort} sl@0: lappend data $x sl@0: return [concat $data $::sqlite_query_plan] sl@0: } sl@0: sl@0: sl@0: # Prefer a UNIQUE index over another index. sl@0: # sl@0: do_test where2-1.1 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 sl@0: } sl@0: } {85 6 7396 7402 nosort t1 i1w} sl@0: sl@0: # Always prefer a rowid== constraint over any other index. sl@0: # sl@0: do_test where2-1.3 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 AND rowid=85 sl@0: } sl@0: } {85 6 7396 7402 nosort t1 *} sl@0: sl@0: # When constrained by a UNIQUE index, the ORDER BY clause is always ignored. sl@0: # sl@0: do_test where2-2.1 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=85 ORDER BY random(5); sl@0: } sl@0: } {85 6 7396 7402 nosort t1 i1w} sl@0: do_test where2-2.2 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE x=6 AND y=7396 ORDER BY random(5); sl@0: } sl@0: } {85 6 7396 7402 sort t1 i1xy} sl@0: do_test where2-2.3 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE rowid=85 AND x=6 AND y=7396 ORDER BY random(5); sl@0: } sl@0: } {85 6 7396 7402 nosort t1 *} sl@0: sl@0: sl@0: # Efficient handling of forward and reverse table scans. sl@0: # sl@0: do_test where2-3.1 { sl@0: queryplan { sl@0: SELECT * FROM t1 ORDER BY rowid LIMIT 2 sl@0: } sl@0: } {1 0 4 4 2 1 9 10 nosort t1 *} sl@0: do_test where2-3.2 { sl@0: queryplan { sl@0: SELECT * FROM t1 ORDER BY rowid DESC LIMIT 2 sl@0: } sl@0: } {100 6 10201 10207 99 6 10000 10006 nosort t1 *} sl@0: sl@0: # The IN operator can be used by indices at multiple layers sl@0: # sl@0: ifcapable subquery { sl@0: do_test where2-4.1 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE z IN (10207,10006) AND y IN (10000,10201) sl@0: AND x>0 AND x<10 sl@0: ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} sl@0: do_test where2-4.2 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE z IN (10207,10006) AND y=10000 sl@0: AND x>0 AND x<10 sl@0: ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 sort t1 i1zyx} sl@0: do_test where2-4.3 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE z=10006 AND y IN (10000,10201) sl@0: AND x>0 AND x<10 sl@0: ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 sort t1 i1zyx} sl@0: ifcapable compound { sl@0: do_test where2-4.4 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006) sl@0: AND y IN (10000,10201) sl@0: AND x>0 AND x<10 sl@0: ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} sl@0: do_test where2-4.5 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006) sl@0: AND y IN (SELECT 10000 UNION SELECT 10201) sl@0: AND x>0 AND x<10 sl@0: ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} sl@0: } sl@0: do_test where2-4.6 { sl@0: queryplan { sl@0: SELECT * FROM t1 sl@0: WHERE x IN (1,2,3,4,5,6,7,8) sl@0: AND y IN (10000,10001,10002,10003,10004,10005) sl@0: ORDER BY 2 sl@0: } sl@0: } {99 6 10000 10006 sort t1 i1xy} sl@0: sl@0: # Duplicate entires on the RHS of an IN operator do not cause duplicate sl@0: # output rows. sl@0: # sl@0: do_test where2-4.6 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207) sl@0: ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} sl@0: ifcapable compound { sl@0: do_test where2-4.7 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE z IN ( sl@0: SELECT 10207 UNION ALL SELECT 10006 sl@0: UNION ALL SELECT 10006 UNION ALL SELECT 10207) sl@0: ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx} sl@0: } sl@0: sl@0: } ;# ifcapable subquery sl@0: sl@0: # The use of an IN operator disables the index as a sorter. sl@0: # sl@0: do_test where2-5.1 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=99 ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 nosort t1 i1w} sl@0: sl@0: ifcapable subquery { sl@0: do_test where2-5.2 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w IN (99) ORDER BY w sl@0: } sl@0: } {99 6 10000 10006 sort t1 i1w} sl@0: } sl@0: sl@0: # Verify that OR clauses get translated into IN operators. sl@0: # sl@0: set ::idx {} sl@0: ifcapable subquery {set ::idx i1w} sl@0: do_test where2-6.1.1 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=99 OR w=100 ORDER BY +w sl@0: } sl@0: } [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx] sl@0: do_test where2-6.1.2 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE 99=w OR 100=w ORDER BY +w sl@0: } sl@0: } [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx] sl@0: do_test where2-6.2 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=w ORDER BY +w sl@0: } sl@0: } [list 6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx] sl@0: sl@0: do_test where2-6.3 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=+w ORDER BY +w sl@0: } sl@0: } {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}} sl@0: do_test where2-6.4 { sl@0: queryplan { sl@0: SELECT * FROM t1 WHERE w=99 OR +w=100 OR 6=w ORDER BY +w sl@0: } sl@0: } {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}} sl@0: sl@0: set ::idx {} sl@0: ifcapable subquery {set ::idx i1zyx} sl@0: do_test where2-6.5 { sl@0: queryplan { sl@0: SELECT b.* FROM t1 a, t1 b sl@0: WHERE a.w=1 AND (a.y=b.z OR b.z=10) sl@0: ORDER BY +b.w sl@0: } sl@0: } [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx] sl@0: do_test where2-6.6 { sl@0: queryplan { sl@0: SELECT b.* FROM t1 a, t1 b sl@0: WHERE a.w=1 AND (b.z=10 OR a.y=b.z OR b.z=10) sl@0: ORDER BY +b.w sl@0: } sl@0: } [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx] sl@0: sl@0: # Ticket #2249. Make sure the OR optimization is not attempted if sl@0: # comparisons between columns of different affinities are needed. sl@0: # sl@0: do_test where2-6.7 { sl@0: execsql { sl@0: CREATE TABLE t2249a(a TEXT UNIQUE); sl@0: CREATE TABLE t2249b(b INTEGER); sl@0: INSERT INTO t2249a VALUES('0123'); sl@0: INSERT INTO t2249b VALUES(123); sl@0: } sl@0: queryplan { sl@0: -- Because a is type TEXT and b is type INTEGER, both a and b sl@0: -- will attempt to convert to NUMERIC before the comparison. sl@0: -- They will thus compare equal. sl@0: -- sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b; sl@0: } sl@0: } {123 0123 nosort t2249b {} t2249a {}} sl@0: do_test where2-6.9 { sl@0: queryplan { sl@0: -- The + operator removes affinity from the rhs. No conversions sl@0: -- occur and the comparison is false. The result is an empty set. sl@0: -- sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b; sl@0: } sl@0: } {nosort t2249b {} {} sqlite_autoindex_t2249a_1} sl@0: do_test where2-6.9.2 { sl@0: # The same thing but with the expression flipped around. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a sl@0: } sl@0: } {nosort t2249b {} {} sqlite_autoindex_t2249a_1} sl@0: do_test where2-6.10 { sl@0: queryplan { sl@0: -- Use + on both sides of the comparison to disable indices sl@0: -- completely. Make sure we get the same result. sl@0: -- sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE +a=+b; sl@0: } sl@0: } {nosort t2249b {} t2249a {}} sl@0: do_test where2-6.11 { sl@0: # This will not attempt the OR optimization because of the a=b sl@0: # comparison. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b OR a='hello'; sl@0: } sl@0: } {123 0123 nosort t2249b {} t2249a {}} sl@0: do_test where2-6.11.2 { sl@0: # Permutations of the expression terms. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE b=a OR a='hello'; sl@0: } sl@0: } {123 0123 nosort t2249b {} t2249a {}} sl@0: do_test where2-6.11.3 { sl@0: # Permutations of the expression terms. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE 'hello'=a OR b=a; sl@0: } sl@0: } {123 0123 nosort t2249b {} t2249a {}} sl@0: do_test where2-6.11.4 { sl@0: # Permutations of the expression terms. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR b=a; sl@0: } sl@0: } {123 0123 nosort t2249b {} t2249a {}} sl@0: ifcapable explain&&subquery { sl@0: # These tests are not run if subquery support is not included in the sl@0: # build. This is because these tests test the "a = 1 OR a = 2" to sl@0: # "a IN (1, 2)" optimisation transformation, which is not enabled if sl@0: # subqueries and the IN operator is not available. sl@0: # sl@0: do_test where2-6.12 { sl@0: # In this case, the +b disables the affinity conflict and allows sl@0: # the OR optimization to be used again. The result is now an empty sl@0: # set, the same as in where2-6.9. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR a='hello'; sl@0: } sl@0: } {nosort t2249b {} {} sqlite_autoindex_t2249a_1} sl@0: do_test where2-6.12.2 { sl@0: # In this case, the +b disables the affinity conflict and allows sl@0: # the OR optimization to be used again. The result is now an empty sl@0: # set, the same as in where2-6.9. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR +b=a; sl@0: } sl@0: } {nosort t2249b {} {} sqlite_autoindex_t2249a_1} sl@0: do_test where2-6.12.3 { sl@0: # In this case, the +b disables the affinity conflict and allows sl@0: # the OR optimization to be used again. The result is now an empty sl@0: # set, the same as in where2-6.9. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a OR a='hello'; sl@0: } sl@0: } {nosort t2249b {} {} sqlite_autoindex_t2249a_1} sl@0: do_test where2-6.13 { sl@0: # The addition of +a on the second term disabled the OR optimization. sl@0: # But we should still get the same empty-set result as in where2-6.9. sl@0: queryplan { sl@0: SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR +a='hello'; sl@0: } sl@0: } {nosort t2249b {} t2249a {}} sl@0: } sl@0: sl@0: # Variations on the order of terms in a WHERE clause in order sl@0: # to make sure the OR optimizer can recognize them all. sl@0: do_test where2-6.20 { sl@0: queryplan { sl@0: SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a sl@0: } sl@0: } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1} sl@0: ifcapable explain&&subquery { sl@0: # These tests are not run if subquery support is not included in the sl@0: # build. This is because these tests test the "a = 1 OR a = 2" to sl@0: # "a IN (1, 2)" optimisation transformation, which is not enabled if sl@0: # subqueries and the IN operator is not available. sl@0: # sl@0: do_test where2-6.21 { sl@0: queryplan { sl@0: SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a OR y.a='hello' sl@0: } sl@0: } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1} sl@0: do_test where2-6.22 { sl@0: queryplan { sl@0: SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a=x.a OR y.a='hello' sl@0: } sl@0: } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1} sl@0: do_test where2-6.23 { sl@0: queryplan { sl@0: SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a='hello' OR x.a=y.a sl@0: } sl@0: } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1} sl@0: } sl@0: sl@0: # Unique queries (queries that are guaranteed to return only a single sl@0: # row of result) do not call the sorter. But all tables must give sl@0: # a unique result. If any one table in the join does not give a unique sl@0: # result then sorting is necessary. sl@0: # sl@0: do_test where2-7.1 { sl@0: cksort { sl@0: create table t8(a unique, b, c); sl@0: insert into t8 values(1,2,3); sl@0: insert into t8 values(2,3,4); sl@0: create table t9(x,y); sl@0: insert into t9 values(2,4); sl@0: insert into t9 values(2,3); sl@0: select y from t8, t9 where a=1 order by a, y; sl@0: } sl@0: } {3 4 sort} sl@0: do_test where2-7.2 { sl@0: cksort { sl@0: select * from t8 where a=1 order by b, c sl@0: } sl@0: } {1 2 3 nosort} sl@0: do_test where2-7.3 { sl@0: cksort { sl@0: select * from t8, t9 where a=1 and y=3 order by b, x sl@0: } sl@0: } {1 2 3 2 3 sort} sl@0: do_test where2-7.4 { sl@0: cksort { sl@0: create unique index i9y on t9(y); sl@0: select * from t8, t9 where a=1 and y=3 order by b, x sl@0: } sl@0: } {1 2 3 2 3 nosort} sl@0: sl@0: # Ticket #1807. Using IN constrains on multiple columns of sl@0: # a multi-column index. sl@0: # sl@0: ifcapable subquery { sl@0: do_test where2-8.1 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE x IN (20,21) AND y IN (1,2) sl@0: } sl@0: } {} sl@0: do_test where2-8.2 { sl@0: execsql { sl@0: SELECT * FROM t1 WHERE x IN (1,2) AND y IN (-5,-6) sl@0: } sl@0: } {} sl@0: execsql {CREATE TABLE tx AS SELECT * FROM t1} sl@0: do_test where2-8.3 { sl@0: execsql { sl@0: SELECT w FROM t1 sl@0: WHERE x IN (SELECT x FROM tx WHERE rowid<0) sl@0: AND +y IN (SELECT y FROM tx WHERE rowid=1) sl@0: } sl@0: } {} sl@0: do_test where2-8.4 { sl@0: execsql { sl@0: SELECT w FROM t1 sl@0: WHERE x IN (SELECT x FROM tx WHERE rowid=1) sl@0: AND y IN (SELECT y FROM tx WHERE rowid<0) sl@0: } sl@0: } {} sl@0: #set sqlite_where_trace 1 sl@0: do_test where2-8.5 { sl@0: execsql { sl@0: CREATE INDEX tx_xyz ON tx(x, y, z, w); sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 12 AND 14) sl@0: } sl@0: } {12 13 14} sl@0: do_test where2-8.6 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 12 AND 14) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {12 13 14} sl@0: do_test where2-8.7 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 12 AND 14) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {10 11 12 13 14 15} sl@0: do_test where2-8.8 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {10 11 12 13 14 15 16 17 18 19 20} sl@0: do_test where2-8.9 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 2 AND 4) sl@0: } sl@0: } {} sl@0: do_test where2-8.10 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 2 AND 4) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: do_test where2-8.11 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 2 AND 4) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: do_test where2-8.12 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN -4 AND -2) sl@0: } sl@0: } {} sl@0: do_test where2-8.13 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN -4 AND -2) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: do_test where2-8.14 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN -4 AND -2) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: do_test where2-8.15 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 200 AND 300) sl@0: } sl@0: } {} sl@0: do_test where2-8.16 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 200 AND 300) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: do_test where2-8.17 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 200 AND 300) sl@0: AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: do_test where2-8.18 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 200 AND 300) sl@0: } sl@0: } {} sl@0: do_test where2-8.19 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20) sl@0: AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 200 AND 300) sl@0: AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: do_test where2-8.20 { sl@0: execsql { sl@0: SELECT w FROM tx sl@0: WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 200 AND 300) sl@0: AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20) sl@0: AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20) sl@0: } sl@0: } {} sl@0: } sl@0: sl@0: # Make sure WHERE clauses of the form A=1 AND (B=2 OR B=3) are optimized sl@0: # when we have an index on A and B. sl@0: # sl@0: ifcapable or_opt&&tclvar { sl@0: do_test where2-9.1 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t10(a,b,c); sl@0: INSERT INTO t10 VALUES(1,1,1); sl@0: INSERT INTO t10 VALUES(1,2,2); sl@0: INSERT INTO t10 VALUES(1,3,3); sl@0: } sl@0: for {set i 4} {$i<=1000} {incr i} { sl@0: execsql {INSERT INTO t10 VALUES(1,$i,$i)} sl@0: } sl@0: execsql { sl@0: CREATE INDEX i10 ON t10(a,b); sl@0: COMMIT; sl@0: SELECT count(*) FROM t10; sl@0: } sl@0: } 1000 sl@0: ifcapable subquery { sl@0: do_test where2-9.2 { sl@0: count { sl@0: SELECT * FROM t10 WHERE a=1 AND (b=2 OR b=3) sl@0: } sl@0: } {1 2 2 1 3 3 7} sl@0: } sl@0: } sl@0: sl@0: finish_test