xref: /sqlite-3.40.0/test/where2.test (revision 8a4380d7)
1# 2005 July 28
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
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 clauses
13# based on recent changes to the optimizer.
14#
15# $Id: where2.test,v 1.15 2009/02/02 01:50:40 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
22do_test where2-1.0 {
23  execsql {
24    BEGIN;
25    CREATE TABLE t1(w int, x int, y int, z int);
26  }
27  for {set i 1} {$i<=100} {incr i} {
28    set w $i
29    set x [expr {int(log($i)/log(2))}]
30    set y [expr {$i*$i + 2*$i + 1}]
31    set z [expr {$x+$y}]
32    ifcapable tclvar {
33      execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)}
34    } else {
35      execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)}
36    }
37  }
38  execsql {
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);
42    COMMIT;
43  }
44} {}
45
46# Do an SQL statement.  Append the search count to the end of the result.
47#
48proc count sql {
49  set ::sqlite_search_count 0
50  return [concat [execsql $sql] $::sqlite_search_count]
51}
52
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.
56#
57# This procedure is used to check to make sure sorting is or is not
58# occurring as expected.
59#
60proc cksort {sql} {
61  set data [execsql $sql]
62  if {[db status sort]} {set x sort} {set x nosort}
63  lappend data $x
64  return $data
65}
66
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 name of the table and index used.
70#
71proc queryplan {sql} {
72  set ::sqlite_sort_count 0
73  set data [execsql $sql]
74  if {$::sqlite_sort_count} {set x sort} {set x nosort}
75  lappend data $x
76  set eqp [execsql "EXPLAIN QUERY PLAN $sql"]
77  # puts eqp=$eqp
78  foreach {a b c x} $eqp {
79    if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \
80        $x all as tab idx]} {
81      lappend data $tab $idx
82    } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} {
83      lappend data $tab *
84    }
85  }
86  return $data
87}
88
89
90# Prefer a UNIQUE index over another index.
91#
92do_test where2-1.1 {
93  queryplan {
94    SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396
95  }
96} {85 6 7396 7402 nosort t1 i1w}
97
98# Always prefer a rowid== constraint over any other index.
99#
100do_test where2-1.3 {
101  queryplan {
102    SELECT * FROM t1 WHERE w=85 AND x=6 AND y=7396 AND rowid=85
103  }
104} {85 6 7396 7402 nosort t1 *}
105
106# When constrained by a UNIQUE index, the ORDER BY clause is always ignored.
107#
108do_test where2-2.1 {
109  queryplan {
110    SELECT * FROM t1 WHERE w=85 ORDER BY random();
111  }
112} {85 6 7396 7402 nosort t1 i1w}
113do_test where2-2.2 {
114  queryplan {
115    SELECT * FROM t1 WHERE x=6 AND y=7396 ORDER BY random();
116  }
117} {85 6 7396 7402 sort t1 i1xy}
118do_test where2-2.3 {
119  queryplan {
120    SELECT * FROM t1 WHERE rowid=85 AND x=6 AND y=7396 ORDER BY random();
121  }
122} {85 6 7396 7402 nosort t1 *}
123
124
125# Efficient handling of forward and reverse table scans.
126#
127do_test where2-3.1 {
128  queryplan {
129    SELECT * FROM t1 ORDER BY rowid LIMIT 2
130  }
131} {1 0 4 4 2 1 9 10 nosort t1 *}
132do_test where2-3.2 {
133  queryplan {
134    SELECT * FROM t1 ORDER BY rowid DESC LIMIT 2
135  }
136} {100 6 10201 10207 99 6 10000 10006 nosort t1 *}
137
138# The IN operator can be used by indices at multiple layers
139#
140ifcapable subquery {
141  do_test where2-4.1 {
142    queryplan {
143      SELECT * FROM t1 WHERE z IN (10207,10006) AND y IN (10000,10201)
144                       AND x>0 AND x<10
145      ORDER BY w
146    }
147  } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
148  do_test where2-4.2 {
149    queryplan {
150      SELECT * FROM t1 WHERE z IN (10207,10006) AND y=10000
151                       AND x>0 AND x<10
152      ORDER BY w
153    }
154  } {99 6 10000 10006 sort t1 i1zyx}
155  do_test where2-4.3 {
156    queryplan {
157      SELECT * FROM t1 WHERE z=10006 AND y IN (10000,10201)
158                       AND x>0 AND x<10
159      ORDER BY w
160    }
161  } {99 6 10000 10006 sort t1 i1zyx}
162  ifcapable compound {
163    do_test where2-4.4 {
164      queryplan {
165        SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
166                         AND y IN (10000,10201)
167                         AND x>0 AND x<10
168        ORDER BY w
169      }
170    } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
171    do_test where2-4.5 {
172      queryplan {
173        SELECT * FROM t1 WHERE z IN (SELECT 10207 UNION SELECT 10006)
174                         AND y IN (SELECT 10000 UNION SELECT 10201)
175                         AND x>0 AND x<10
176        ORDER BY w
177      }
178    } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
179  }
180  do_test where2-4.6a {
181    queryplan {
182      SELECT * FROM t1
183       WHERE x IN (1,2,3,4,5,6,7,8)
184         AND y IN (10000,10001,10002,10003,10004,10005)
185       ORDER BY x
186    }
187  } {99 6 10000 10006 nosort t1 i1xy}
188  do_test where2-4.6b {
189    queryplan {
190      SELECT * FROM t1
191       WHERE x IN (1,2,3,4,5,6,7,8)
192         AND y IN (10000,10001,10002,10003,10004,10005)
193       ORDER BY x DESC
194    }
195  } {99 6 10000 10006 nosort t1 i1xy}
196  do_test where2-4.6c {
197    queryplan {
198      SELECT * FROM t1
199       WHERE x IN (1,2,3,4,5,6,7,8)
200         AND y IN (10000,10001,10002,10003,10004,10005)
201       ORDER BY x, y
202    }
203  } {99 6 10000 10006 nosort t1 i1xy}
204  do_test where2-4.6d {
205    queryplan {
206      SELECT * FROM t1
207       WHERE x IN (1,2,3,4,5,6,7,8)
208         AND y IN (10000,10001,10002,10003,10004,10005)
209       ORDER BY x, y DESC
210    }
211  } {99 6 10000 10006 sort t1 i1xy}
212
213  # Duplicate entires on the RHS of an IN operator do not cause duplicate
214  # output rows.
215  #
216  do_test where2-4.6x {
217    queryplan {
218      SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
219      ORDER BY w
220    }
221  } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
222  do_test where2-4.6y {
223    queryplan {
224      SELECT * FROM t1 WHERE z IN (10207,10006,10006,10207)
225      ORDER BY w DESC
226    }
227  } {100 6 10201 10207 99 6 10000 10006 sort t1 i1zyx}
228  ifcapable compound {
229    do_test where2-4.7 {
230      queryplan {
231        SELECT * FROM t1 WHERE z IN (
232           SELECT 10207 UNION ALL SELECT 10006
233           UNION ALL SELECT 10006 UNION ALL SELECT 10207)
234        ORDER BY w
235      }
236    } {99 6 10000 10006 100 6 10201 10207 sort t1 i1zyx}
237  }
238
239} ;# ifcapable subquery
240
241# The use of an IN operator disables the index as a sorter.
242#
243do_test where2-5.1 {
244  queryplan {
245    SELECT * FROM t1 WHERE w=99 ORDER BY w
246  }
247} {99 6 10000 10006 nosort t1 i1w}
248
249ifcapable subquery {
250  do_test where2-5.2a {
251    queryplan {
252      SELECT * FROM t1 WHERE w IN (99) ORDER BY w
253    }
254  } {99 6 10000 10006 nosort t1 i1w}
255  do_test where2-5.2b {
256    queryplan {
257      SELECT * FROM t1 WHERE w IN (99) ORDER BY w DESC
258    }
259  } {99 6 10000 10006 nosort t1 i1w}
260}
261
262# Verify that OR clauses get translated into IN operators.
263#
264set ::idx {}
265ifcapable subquery {set ::idx i1w}
266do_test where2-6.1.1 {
267  queryplan {
268    SELECT * FROM t1 WHERE w=99 OR w=100 ORDER BY +w
269  }
270} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
271do_test where2-6.1.2 {
272  queryplan {
273    SELECT * FROM t1 WHERE 99=w OR 100=w ORDER BY +w
274  }
275} [list 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
276do_test where2-6.2 {
277  queryplan {
278    SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=w ORDER BY +w
279  }
280} [list 6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 $::idx]
281
282do_test where2-6.3 {
283  queryplan {
284    SELECT * FROM t1 WHERE w=99 OR w=100 OR 6=+w ORDER BY +w
285  }
286} {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}}
287do_test where2-6.4 {
288  queryplan {
289    SELECT * FROM t1 WHERE w=99 OR +w=100 OR 6=w ORDER BY +w
290  }
291} {6 2 49 51 99 6 10000 10006 100 6 10201 10207 sort t1 {}}
292
293set ::idx {}
294ifcapable subquery {set ::idx i1zyx}
295do_test where2-6.5 {
296  queryplan {
297    SELECT b.* FROM t1 a, t1 b
298     WHERE a.w=1 AND (a.y=b.z OR b.z=10)
299     ORDER BY +b.w
300  }
301} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
302do_test where2-6.6 {
303  queryplan {
304    SELECT b.* FROM t1 a, t1 b
305     WHERE a.w=1 AND (b.z=10 OR a.y=b.z OR b.z=10)
306     ORDER BY +b.w
307  }
308} [list 1 0 4 4 2 1 9 10 sort a i1w b $::idx]
309
310# Ticket #2249.  Make sure the OR optimization is not attempted if
311# comparisons between columns of different affinities are needed.
312#
313do_test where2-6.7 {
314  execsql {
315    CREATE TABLE t2249a(a TEXT UNIQUE);
316    CREATE TABLE t2249b(b INTEGER);
317    INSERT INTO t2249a VALUES('0123');
318    INSERT INTO t2249b VALUES(123);
319  }
320  queryplan {
321    -- Because a is type TEXT and b is type INTEGER, both a and b
322    -- will attempt to convert to NUMERIC before the comparison.
323    -- They will thus compare equal.
324    --
325    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b;
326  }
327} {123 0123 nosort t2249b {} t2249a {}}
328do_test where2-6.9 {
329  queryplan {
330    -- The + operator removes affinity from the rhs.  No conversions
331    -- occur and the comparison is false.  The result is an empty set.
332    --
333    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b;
334  }
335} {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
336do_test where2-6.9.2 {
337  # The same thing but with the expression flipped around.
338  queryplan {
339    SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a
340  }
341} {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
342do_test where2-6.10 {
343  queryplan {
344    -- Use + on both sides of the comparison to disable indices
345    -- completely.  Make sure we get the same result.
346    --
347    SELECT * FROM t2249b CROSS JOIN t2249a WHERE +a=+b;
348  }
349} {nosort t2249b {} t2249a {}}
350do_test where2-6.11 {
351  # This will not attempt the OR optimization because of the a=b
352  # comparison.
353  queryplan {
354    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=b OR a='hello';
355  }
356} {123 0123 nosort t2249b {} t2249a {}}
357do_test where2-6.11.2 {
358  # Permutations of the expression terms.
359  queryplan {
360    SELECT * FROM t2249b CROSS JOIN t2249a WHERE b=a OR a='hello';
361  }
362} {123 0123 nosort t2249b {} t2249a {}}
363do_test where2-6.11.3 {
364  # Permutations of the expression terms.
365  queryplan {
366    SELECT * FROM t2249b CROSS JOIN t2249a WHERE 'hello'=a OR b=a;
367  }
368} {123 0123 nosort t2249b {} t2249a {}}
369do_test where2-6.11.4 {
370  # Permutations of the expression terms.
371  queryplan {
372    SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR b=a;
373  }
374} {123 0123 nosort t2249b {} t2249a {}}
375ifcapable explain&&subquery {
376  # These tests are not run if subquery support is not included in the
377  # build. This is because these tests test the "a = 1 OR a = 2" to
378  # "a IN (1, 2)" optimisation transformation, which is not enabled if
379  # subqueries and the IN operator is not available.
380  #
381  do_test where2-6.12 {
382    # In this case, the +b disables the affinity conflict and allows
383    # the OR optimization to be used again.  The result is now an empty
384    # set, the same as in where2-6.9.
385    queryplan {
386      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR a='hello';
387    }
388  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
389  do_test where2-6.12.2 {
390    # In this case, the +b disables the affinity conflict and allows
391    # the OR optimization to be used again.  The result is now an empty
392    # set, the same as in where2-6.9.
393    queryplan {
394      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a='hello' OR +b=a;
395    }
396  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
397  do_test where2-6.12.3 {
398    # In this case, the +b disables the affinity conflict and allows
399    # the OR optimization to be used again.  The result is now an empty
400    # set, the same as in where2-6.9.
401    queryplan {
402      SELECT * FROM t2249b CROSS JOIN t2249a WHERE +b=a OR a='hello';
403    }
404  } {nosort t2249b {} {} sqlite_autoindex_t2249a_1}
405  do_test where2-6.13 {
406    # The addition of +a on the second term disabled the OR optimization.
407    # But we should still get the same empty-set result as in where2-6.9.
408    queryplan {
409      SELECT * FROM t2249b CROSS JOIN t2249a WHERE a=+b OR +a='hello';
410    }
411  } {nosort t2249b {} t2249a {}}
412}
413
414# Variations on the order of terms in a WHERE clause in order
415# to make sure the OR optimizer can recognize them all.
416do_test where2-6.20 {
417  queryplan {
418    SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a
419  }
420} {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
421ifcapable explain&&subquery {
422  # These tests are not run if subquery support is not included in the
423  # build. This is because these tests test the "a = 1 OR a = 2" to
424  # "a IN (1, 2)" optimisation transformation, which is not enabled if
425  # subqueries and the IN operator is not available.
426  #
427  do_test where2-6.21 {
428    queryplan {
429      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE x.a=y.a OR y.a='hello'
430    }
431  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
432  do_test where2-6.22 {
433    queryplan {
434      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a=x.a OR y.a='hello'
435    }
436  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
437  do_test where2-6.23 {
438    queryplan {
439      SELECT * FROM t2249a x CROSS JOIN t2249a y WHERE y.a='hello' OR x.a=y.a
440    }
441  } {0123 0123 nosort x {} {} sqlite_autoindex_t2249a_1}
442}
443
444# Unique queries (queries that are guaranteed to return only a single
445# row of result) do not call the sorter.  But all tables must give
446# a unique result.  If any one table in the join does not give a unique
447# result then sorting is necessary.
448#
449do_test where2-7.1 {
450  cksort {
451    create table t8(a unique, b, c);
452    insert into t8 values(1,2,3);
453    insert into t8 values(2,3,4);
454    create table t9(x,y);
455    insert into t9 values(2,4);
456    insert into t9 values(2,3);
457    select y from t8, t9 where a=1 order by a, y;
458  }
459} {3 4 sort}
460do_test where2-7.2 {
461  cksort {
462    select * from t8 where a=1 order by b, c
463  }
464} {1 2 3 nosort}
465do_test where2-7.3 {
466  cksort {
467    select * from t8, t9 where a=1 and y=3 order by b, x
468  }
469} {1 2 3 2 3 sort}
470do_test where2-7.4 {
471  cksort {
472    create unique index i9y on t9(y);
473    select * from t8, t9 where a=1 and y=3 order by b, x
474  }
475} {1 2 3 2 3 nosort}
476
477# Ticket #1807.  Using IN constrains on multiple columns of
478# a multi-column index.
479#
480ifcapable subquery {
481  do_test where2-8.1 {
482    execsql {
483      SELECT * FROM t1 WHERE x IN (20,21) AND y IN (1,2)
484    }
485  } {}
486  do_test where2-8.2 {
487    execsql {
488      SELECT * FROM t1 WHERE x IN (1,2) AND y IN (-5,-6)
489    }
490  } {}
491  execsql {CREATE TABLE tx AS SELECT * FROM t1}
492  do_test where2-8.3 {
493    execsql {
494      SELECT w FROM t1
495       WHERE x IN (SELECT x FROM tx WHERE rowid<0)
496         AND +y IN (SELECT y FROM tx WHERE rowid=1)
497    }
498  } {}
499  do_test where2-8.4 {
500    execsql {
501      SELECT w FROM t1
502       WHERE x IN (SELECT x FROM tx WHERE rowid=1)
503         AND y IN (SELECT y FROM tx WHERE rowid<0)
504    }
505  } {}
506  #set sqlite_where_trace 1
507  do_test where2-8.5 {
508    execsql {
509      CREATE INDEX tx_xyz ON tx(x, y, z, w);
510      SELECT w FROM tx
511       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
512         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
513         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 12 AND 14)
514    }
515  } {12 13 14}
516  do_test where2-8.6 {
517    execsql {
518      SELECT w FROM tx
519       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
520         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 12 AND 14)
521         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
522    }
523  } {12 13 14}
524  do_test where2-8.7 {
525    execsql {
526      SELECT w FROM tx
527       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 12 AND 14)
528         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
529         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
530    }
531  } {10 11 12 13 14 15}
532  do_test where2-8.8 {
533    execsql {
534      SELECT w FROM tx
535       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
536         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
537         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
538    }
539  } {10 11 12 13 14 15 16 17 18 19 20}
540  do_test where2-8.9 {
541    execsql {
542      SELECT w FROM tx
543       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
544         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
545         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 2 AND 4)
546    }
547  } {}
548  do_test where2-8.10 {
549    execsql {
550      SELECT w FROM tx
551       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
552         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 2 AND 4)
553         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
554    }
555  } {}
556  do_test where2-8.11 {
557    execsql {
558      SELECT w FROM tx
559       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 2 AND 4)
560         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
561         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
562    }
563  } {}
564  do_test where2-8.12 {
565    execsql {
566      SELECT w FROM tx
567       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
568         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
569         AND z IN (SELECT z FROM t1 WHERE w BETWEEN -4 AND -2)
570    }
571  } {}
572  do_test where2-8.13 {
573    execsql {
574      SELECT w FROM tx
575       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
576         AND y IN (SELECT y FROM t1 WHERE w BETWEEN -4 AND -2)
577         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
578    }
579  } {}
580  do_test where2-8.14 {
581    execsql {
582      SELECT w FROM tx
583       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN -4 AND -2)
584         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
585         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
586    }
587  } {}
588  do_test where2-8.15 {
589    execsql {
590      SELECT w FROM tx
591       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
592         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
593         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 200 AND 300)
594    }
595  } {}
596  do_test where2-8.16 {
597    execsql {
598      SELECT w FROM tx
599       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 10 AND 20)
600         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 200 AND 300)
601         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
602    }
603  } {}
604  do_test where2-8.17 {
605    execsql {
606      SELECT w FROM tx
607       WHERE x IN (SELECT x FROM t1 WHERE w BETWEEN 200 AND 300)
608         AND y IN (SELECT y FROM t1 WHERE w BETWEEN 10 AND 20)
609         AND z IN (SELECT z FROM t1 WHERE w BETWEEN 10 AND 20)
610    }
611  } {}
612  do_test where2-8.18 {
613    execsql {
614      SELECT w FROM tx
615       WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
616         AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
617         AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 200 AND 300)
618    }
619  } {}
620  do_test where2-8.19 {
621    execsql {
622      SELECT w FROM tx
623       WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 10 AND 20)
624         AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 200 AND 300)
625         AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
626    }
627  } {}
628  do_test where2-8.20 {
629    execsql {
630      SELECT w FROM tx
631       WHERE x IN (SELECT x FROM t1 WHERE +w BETWEEN 200 AND 300)
632         AND y IN (SELECT y FROM t1 WHERE +w BETWEEN 10 AND 20)
633         AND z IN (SELECT z FROM t1 WHERE +w BETWEEN 10 AND 20)
634    }
635  } {}
636}
637
638# Make sure WHERE clauses of the form A=1 AND (B=2 OR B=3) are optimized
639# when we have an index on A and B.
640#
641ifcapable or_opt&&tclvar {
642  do_test where2-9.1 {
643    execsql {
644      BEGIN;
645      CREATE TABLE t10(a,b,c);
646      INSERT INTO t10 VALUES(1,1,1);
647      INSERT INTO t10 VALUES(1,2,2);
648      INSERT INTO t10 VALUES(1,3,3);
649    }
650    for {set i 4} {$i<=1000} {incr i} {
651      execsql {INSERT INTO t10 VALUES(1,$i,$i)}
652    }
653    execsql {
654      CREATE INDEX i10 ON t10(a,b);
655      COMMIT;
656      SELECT count(*) FROM t10;
657    }
658  } 1000
659  ifcapable subquery {
660    do_test where2-9.2 {
661      count {
662        SELECT * FROM t10 WHERE a=1 AND (b=2 OR b=3)
663      }
664    } {1 2 2 1 3 3 7}
665  }
666}
667
668# Indices with redundant columns
669#
670do_test where2-11.1 {
671  execsql {
672    CREATE TABLE t11(a,b,c,d);
673    CREATE INDEX i11aba ON t11(a,b,a,c); -- column A occurs twice.
674    INSERT INTO t11 VALUES(1,2,3,4);
675    INSERT INTO t11 VALUES(5,6,7,8);
676    INSERT INTO t11 VALUES(1,2,9,10);
677    INSERT INTO t11 VALUES(5,11,12,13);
678    SELECT c FROM t11 WHERE a=1 AND b=2 ORDER BY c;
679  }
680} {3 9}
681do_test where2-11.2 {
682  execsql {
683    CREATE INDEX i11cccccccc ON t11(c,c,c,c,c,c,c,c); -- repeated column
684    SELECT d FROM t11 WHERE c=9;
685  }
686} {10}
687do_test where2-11.3 {
688  execsql {
689    SELECT d FROM t11 WHERE c IN (1,2,3,4,5);
690  }
691} {4}
692do_test where2-11.4 {
693  execsql {
694    SELECT d FROM t11 WHERE c=7 OR (a=1 AND b=2) ORDER BY d;
695  }
696} {4 8 10}
697
698
699finish_test
700