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 # Tests for queries that use bloom filters
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 source $testdir/lock_common.tcl
17 source $testdir/malloc_common.tcl
21 # Tests 1.* verify that the bloom filter code correctly handles the
22 # case where the RHS of an (<ipk-column> = ?) expression must be coerced
23 # to an integer before the comparison made.
26 CREATE TABLE t1(a, b);
27 CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
31 INSERT INTO t1 VALUES('hello', 'world');
32 INSERT INTO t2 VALUES(14, 'fourteen');
36 ANALYZE sqlite_schema;
37 INSERT INTO sqlite_stat1 VALUES('t2','idx1','6 6');
38 ANALYZE sqlite_schema;
42 SELECT 'affinity!' FROM t1 CROSS JOIN t2 WHERE t2.c = '14';
48 CREATE TABLE t1(a, b TEXT);
49 CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
50 CREATE TABLE t3(e INTEGER PRIMARY KEY, f);
52 ANALYZE sqlite_schema;
53 INSERT INTO sqlite_stat1 VALUES('t1','idx1','600 6');
54 INSERT INTO sqlite_stat1 VALUES('t2','idx1','6 6');
55 INSERT INTO sqlite_stat1 VALUES('t3','idx2','6 6');
56 ANALYZE sqlite_schema;
58 INSERT INTO t1 VALUES(1, '123');
59 INSERT INTO t2 VALUES(123, 'one');
60 INSERT INTO t3 VALUES(123, 'two');
64 SELECT 'result' FROM t1, t2, t3
65 WHERE t2.c=t1.b AND t2.d!='silly'
66 AND t3.e=t1.b AND t3.f!='silly'
70 # https://sqlite.org/forum/forumpost/56de336385
72 # Do not employ a Bloom filter if the table being filtered or any table
73 # wo the left of the table being filtered lacks STAT1 data, since we
74 # cannot make a good Bloom filter usefulness determination without STAT1
79 CREATE TABLE objs(c INTEGER, s INTEGER, p INTEGER, o INTEGER);
80 CREATE UNIQUE INDEX objs_cspo ON objs(o,p,c,s);
82 DELETE FROM sqlite_stat1;
83 INSERT INTO sqlite_stat1 VALUES('objs','objs_cspo','520138 21 20 19 1');
84 ANALYZE sqlite_schema;
87 WITH RECURSIVE transit(x) AS (
88 SELECT s FROM objs WHERE p=9 AND o=32805
90 SELECT objs.s FROM objs, transit WHERE objs.p=9 AND objs.o=transit.x
92 SELECT x FROM transit;
97 | | `--SEARCH objs USING COVERING INDEX objs_cspo (o=? AND p=?)
100 | `--SEARCH objs USING COVERING INDEX objs_cspo (o=? AND p=?)
105 # https://sqlite.org/forum/forumpost/0846211821
107 # Bloom filter gives an incorrect result if the collating sequence is
108 # anything other than binary.
111 do_execsql_test 3.1 {
112 CREATE TABLE t0(x TEXT COLLATE rtrim);
113 INSERT INTO t0(x) VALUES ('a'), ('b'), ('c');
114 CREATE VIEW v0(y) AS SELECT DISTINCT x FROM t0;
115 SELECT count(*) FROM t0, v0 WHERE x='b ';
118 SELECT count(*) FROM t0, v0 WHERE x='b ';
123 | `--USE TEMP B-TREE FOR DISTINCT
125 `--SEARCH t0 USING AUTOMATIC PARTIAL COVERING INDEX (x=?)
127 # ^^^^^--- The key feature in the previous result is that no Bloom filter
128 # is used. In the following, a Bloom filter is used because the data type
129 # is INT instead of TEXT.
130 do_execsql_test 3.3 {
131 CREATE TABLE t1(x INT COLLATE rtrim);
132 INSERT INTO t1(x) VALUES ('a'), ('b'), ('c');
133 CREATE VIEW v1(y) AS SELECT DISTINCT x FROM t1;
134 SELECT count(*) FROM t1, v1 WHERE x='b ';
137 SELECT count(*) FROM t1, v1 WHERE x='b ';
142 | `--USE TEMP B-TREE FOR DISTINCT
144 |--BLOOM FILTER ON t1 (x=?)
145 `--SEARCH t1 USING AUTOMATIC PARTIAL COVERING INDEX (x=?)
149 # https://sqlite.org/forum/forumpost/d47a0e8e3a
150 # https://sqlite.org/forum/forumpost/2e427099d5
152 # Both reports are for the same problem - using a Bloom filter on an
153 # expression index can cause issues.
156 do_execsql_test 4.1 {
157 CREATE TABLE t1(x TEXT, y INT, z TEXT);
158 INSERT INTO t1(rowid,x,y,z) VALUES(12,'aa','bb','aa');
159 CREATE INDEX i1x ON t1(1 IS true,z);
160 CREATE TABLE t0(x TEXT);
161 INSERT INTO t0(rowid,x) VALUES(4,'aa');
162 ANALYZE sqlite_schema;
163 INSERT INTO sqlite_stat1 VALUES('t0',NULL,'20');
164 INSERT INTO sqlite_stat1 VALUES('t1','i1x','18 18 2');
165 ANALYZE sqlite_schema;
167 do_execsql_test 4.2 {
168 SELECT * FROM t0 NATURAL JOIN t1 WHERE z=t1.x;
170 do_execsql_test 4.3 {
172 CREATE TABLE t0(a TEXT);
173 INSERT INTO t0 VALUES ('xyz');
174 CREATE INDEX t0x ON t0(a IS FALSE) WHERE false;
176 CREATE TABLE t1(b INT);
177 INSERT INTO t1 VALUES('aaa'),('bbb'),('ccc'),('ddd'),(NULL);
178 CREATE TABLE t2(c REAL);
179 INSERT INTO t2 VALUES(7);
181 CREATE INDEX t2x ON t2(true IN ());
183 do_execsql_test 4.4 {
184 SELECT * FROM t0 LEFT JOIN t1 LEFT JOIN t2 ON (b NOTNULL)==(c IN ()) WHERE c;