Do not flatten sub-queries that contain window functions.
[sqlite.git] / test / in6.test
blobad16c4e8a8c4cc3ba66a379da0348142c1e17b67
1 # 2018-06-07
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # A multi-key index that uses an IN operator on one of the keys other
13 # than the left-most key is able to abort the IN-operator loop early
14 # if key terms further to the left do not match.
16 # Call this the "multikey-IN-operator early-out optimization" or
17 # just "IN-early-out" optimization for short.
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
22 set testprefix in6
24 do_test in6-1.1 {
25   db eval {
26     CREATE TABLE t1(a,b,c,d);
27     WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
28       INSERT INTO t1(a,b,c,d)
29         SELECT 100, 200+x/2, 300+x/5, x FROM c;
30     CREATE INDEX t1abc ON t1(a,b,c);
31   }
32   set ::sqlite_search_count 0
33   db eval {
34     SELECT d FROM t1
35      WHERE a=99
36        AND b IN (200,205,201,204)
37        AND c IN (304,302,309,308);
38   }
39 } {}
40 do_test in6-1.2 {
41   set ::sqlite_search_count
42 } {0}  ;# Without the IN-early-out optimization, this value would be 15
44 # The multikey-IN-operator early-out optimization does not apply
45 # when the IN operator is on the left-most column of the index.
47 do_test in6-1.3 {
48   db eval {
49     EXPLAIN
50     SELECT d FROM t1
51       WHERE a IN (98,99,100,101)
52         AND b=200 AND c=300;
53   }
54 } {~/(IfNoHope|SeekHit)/}
56 set sqlite_search_count 0
57 do_execsql_test in6-1.4 {
58  SELECT d FROM t1
59   WHERE a=100
60     AND b IN (200,201,202,204)
61     AND c IN (300,302,301,305)
62   ORDER BY +d;
63 } {1 2 3 4 5 8 9}
64 do_test in6-1.5 {
65   set ::sqlite_search_count
66 } {39}
68 do_execsql_test in6-2.1 {
69   CREATE TABLE t2(e INT UNIQUE, f TEXT);
70   SELECT d, f FROM t1 LEFT JOIN t2 ON (e=d)
71   WHERE a=100
72     AND b IN (200,201,202,204)
73     AND c IN (300,302,301,305)
74   ORDER BY +d;
75 } {1 {} 2 {} 3 {} 4 {} 5 {} 8 {} 9 {}}
77 finish_test