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 #***********************************************************************
11 # Make sure the rtreenode() testing function can handle entries with
15 if {![info exists testdir]} {
16 set testdir [file join [file dirname [info script]] .. .. test]
18 source [file join [file dirname [info script]] rtree_util.tcl]
19 source $testdir/tester.tcl
20 ifcapable !rtree { finish_test ; return }
24 CREATE VIRTUAL TABLE r_tree USING rtree(id, min_x, max_x, min_y, max_y);
29 SELECT * FROM r_tree, t
30 WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
34 `--SCAN r_tree VIRTUAL TABLE INDEX 2:D3B2D1B0
38 SELECT * FROM t, r_tree
39 WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
43 `--SCAN r_tree VIRTUAL TABLE INDEX 2:D3B2D1B0
47 SELECT * FROM t, r_tree
48 WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND ?<=max_y
52 `--SCAN r_tree VIRTUAL TABLE INDEX 2:D3B2D1B0
56 SELECT * FROM t, r_tree
59 |--SCAN r_tree VIRTUAL TABLE INDEX 2:
64 INSERT INTO t VALUES(0, 0);
65 INSERT INTO t VALUES(0, 1);
66 INSERT INTO t VALUES(0, 2);
67 INSERT INTO t VALUES(0, 3);
68 INSERT INTO t VALUES(0, 4);
69 INSERT INTO t VALUES(0, 5);
70 INSERT INTO t VALUES(0, 6);
71 INSERT INTO t VALUES(0, 7);
72 INSERT INTO t VALUES(0, 8);
73 INSERT INTO t VALUES(0, 9);
75 INSERT INTO t SELECT x+1, y FROM t;
76 INSERT INTO t SELECT x+2, y FROM t;
77 INSERT INTO t SELECT x+4, y FROM t;
78 INSERT INTO r_tree SELECT NULL, x-1, x+1, y-1, y+1 FROM t;
86 SELECT * FROM r_tree, t
87 WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
91 `--SCAN r_tree VIRTUAL TABLE INDEX 2:D3B2D1B0
95 SELECT * FROM t, r_tree
96 WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND t.x<=max_y
100 `--SCAN r_tree VIRTUAL TABLE INDEX 2:D3B2D1B0
104 SELECT * FROM t, r_tree
105 WHERE t.x>=min_x AND t.x<=max_x AND t.y>=min_y AND ?<=max_y
109 `--SCAN r_tree VIRTUAL TABLE INDEX 2:D3B2D1B0
113 SELECT * FROM t, r_tree
116 |--SCAN r_tree VIRTUAL TABLE INDEX 2:
120 #-------------------------------------------------------------------------
121 # Test that the special CROSS JOIN handling works with rtree tables.
123 do_execsql_test 3.1 {
126 CREATE VIRTUAL TABLE t3 USING rtree(z, x1,x2, y1,y2);
129 do_eqp_test 3.2.1 { SELECT * FROM t1 CROSS JOIN t2 } {
134 do_eqp_test 3.2.2 { SELECT * FROM t2 CROSS JOIN t1 } {
140 do_eqp_test 3.3.1 { SELECT * FROM t1 CROSS JOIN t3 } {
143 `--SCAN t3 VIRTUAL TABLE INDEX 2:
145 do_eqp_test 3.3.2 { SELECT * FROM t3 CROSS JOIN t1 } {
147 |--SCAN t3 VIRTUAL TABLE INDEX 2:
151 #--------------------------------------------------------------------
152 # Test that LEFT JOINs are not reordered if the right-hand-side is
156 do_execsql_test 4.1 {
158 CREATE VIRTUAL TABLE t2 USING rtree(b, x1,x2);
160 INSERT INTO t1 VALUES(1);
161 INSERT INTO t1 VALUES(2);
163 INSERT INTO t2 VALUES(1, 0.0, 0.1);
164 INSERT INTO t2 VALUES(3, 0.0, 0.1);
167 do_execsql_test 4.2 {
168 SELECT a, b FROM t1 LEFT JOIN t2 ON (+a = +b);
171 do_execsql_test 4.3 {
172 SELECT b, a FROM t2 LEFT JOIN t1 ON (+a = +b);
175 #--------------------------------------------------------------------
176 # Test that the sqlite_stat1 data is used correctly.
179 do_execsql_test 5.1 {
180 CREATE TABLE t1(x INT PRIMARY KEY, y);
181 CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2, +d1);
183 INSERT INTO t1(x) VALUES(1);
184 INSERT INTO t1(x) SELECT x+1 FROM t1; -- 2
185 INSERT INTO t1(x) SELECT x+2 FROM t1; -- 4
186 INSERT INTO t1(x) SELECT x+4 FROM t1; -- 8
187 INSERT INTO t1(x) SELECT x+8 FROM t1; -- 16
188 INSERT INTO t1(x) SELECT x+16 FROM t1; -- 32
189 INSERT INTO t1(x) SELECT x+32 FROM t1; -- 64
190 INSERT INTO t1(x) SELECT x+64 FROM t1; -- 128
191 INSERT INTO t1(x) SELECT x+128 FROM t1; -- 256
192 INSERT INTO t1(x) SELECT x+256 FROM t1; -- 512
193 INSERT INTO t1(x) SELECT x+512 FROM t1; --1024
195 INSERT INTO rt SELECT x, x, x+1, printf('x%04xy',x) FROM t1 WHERE x<=5;
197 do_rtree_integrity_test 5.1.1 rt
199 # First test a query with no ANALYZE data at all. The outer loop is
203 SELECT * FROM t1, rt WHERE x==id;
207 `--SCAN rt VIRTUAL TABLE INDEX 1:
210 # Now create enough ANALYZE data to tell SQLite that virtual table "rt"
211 # contains very few rows. This causes it to move "rt" to the outer loop.
213 do_execsql_test 5.3 {
215 DELETE FROM sqlite_stat1 WHERE tbl='t1';
220 SELECT * FROM t1, rt WHERE x==id;
223 |--SCAN rt VIRTUAL TABLE INDEX 2:
224 `--SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (x=?)
227 # Delete the ANALYZE data. "t1" should be the outer loop again.
229 do_execsql_test 5.5 { DROP TABLE sqlite_stat1; }
233 SELECT * FROM t1, rt WHERE x==id;
237 `--SCAN rt VIRTUAL TABLE INDEX 1:
240 # This time create and attach a database that contains ANALYZE data for
241 # tables of the same names as those used internally by virtual table
242 # "rt". Check that the rtree module is not fooled into using this data.
243 # Table "t1" should remain the outer loop.
250 DELETE FROM sqlite_stat1 WHERE tbl='t1';
255 execsql { ATTACH 'test.db2' AS aux; }
258 SELECT * FROM t1, rt WHERE x==id;
262 `--SCAN rt VIRTUAL TABLE INDEX 1:
265 #--------------------------------------------------------------------
266 # Test that having a second connection drop the sqlite_stat1 table
267 # before it is required by rtreeConnect() does not cause problems.
271 do_execsql_test 6.1 {
273 CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2);
274 INSERT INTO t1 VALUES(1);
275 INSERT INTO rt VALUES(1,2,3);
280 do_execsql_test 6.2 { SELECT * FROM t1 } {1}
284 db2 eval { DROP TABLE sqlite_stat1 }
286 execsql { SELECT * FROM rt }
291 #--------------------------------------------------------------------
292 # Test that queries featuring LEFT or CROSS JOINS are handled correctly.
293 # Handled correctly in this case means:
295 # * Terms with prereqs that appear to the left of a LEFT JOIN against
296 # the virtual table are always available to xBestIndex.
298 # * Terms with prereqs that appear to the right of a LEFT JOIN against
299 # the virtual table are never available to xBestIndex.
301 # And the same behaviour for CROSS joins.
304 do_execsql_test 7.0 {
305 CREATE TABLE xdir(x1);
306 CREATE TABLE ydir(y1);
307 CREATE VIRTUAL TABLE rt USING rtree_i32(id, xmin, xmax, ymin, ymax);
309 INSERT INTO xdir VALUES(5);
310 INSERT INTO ydir VALUES(10);
312 INSERT INTO rt VALUES(1, 2, 7, 12, 14); -- Not a hit
313 INSERT INTO rt VALUES(2, 2, 7, 8, 12); -- A hit!
314 INSERT INTO rt VALUES(3, 7, 11, 8, 12); -- Not a hit!
315 INSERT INTO rt VALUES(4, 5, 5, 10, 10); -- A hit!
319 proc do_eqp_execsql_test {tn sql res1 res2} {
320 do_eqp_test $tn.1 $sql $res1
321 do_execsql_test $tn.2 $sql $res2
324 do_eqp_execsql_test 7.1 {
325 SELECT id FROM xdir, rt, ydir
326 ON (y1 BETWEEN ymin AND ymax)
327 WHERE (x1 BETWEEN xmin AND xmax);
332 `--SCAN rt VIRTUAL TABLE INDEX 2:B2D3B0D1
337 do_eqp_execsql_test 7.2 {
338 SELECT * FROM xdir, rt LEFT JOIN ydir
339 ON (y1 BETWEEN ymin AND ymax)
340 WHERE (x1 BETWEEN xmin AND xmax);
344 |--SCAN rt VIRTUAL TABLE INDEX 2:B0D1
345 `--SCAN ydir LEFT-JOIN
352 do_eqp_execsql_test 7.3 {
353 SELECT id FROM xdir, rt CROSS JOIN ydir
354 ON (y1 BETWEEN ymin AND ymax)
355 WHERE (x1 BETWEEN xmin AND xmax);
359 |--SCAN rt VIRTUAL TABLE INDEX 2:B0D1
365 do_eqp_execsql_test 7.4 {
366 SELECT id FROM rt, xdir CROSS JOIN ydir
367 ON (y1 BETWEEN ymin AND ymax)
368 WHERE (x1 BETWEEN xmin AND xmax);
372 |--SCAN rt VIRTUAL TABLE INDEX 2:B0D1