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 # The focus of this file is testing the r-tree extension.
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
24 # rtree-1.*: Creating/destroying r-tree tables.
25 # rtree-2.*: Test the implicit constraints - unique rowid and
26 # (coord[N]<=coord[N+1]) for even values of N. Also
27 # automatic assigning of rowid values.
28 # rtree-3.*: Linear scans of r-tree data.
29 # rtree-4.*: Test INSERT
30 # rtree-5.*: Test DELETE
31 # rtree-6.*: Test UPDATE
32 # rtree-7.*: Test renaming an r-tree table.
33 # rtree-8.*: Test constrained scans of r-tree data.
35 # rtree-12.*: Test that on-conflict clauses are supported.
36 # rtree-13.*: Test that bug [d2889096e7bdeac6d] has been fixed.
37 # rtree-14.*: Test if a non-integer is inserted into the PK column of an
38 # r-tree table, it is converted to an integer before being
39 # inserted. Also that if a non-numeric is inserted into one
40 # of the min/max dimension columns, it is converted to the
41 # required type before being inserted.
49 #----------------------------------------------------------------------------
50 # Test cases rtree-1.* test CREATE and DROP table statements.
53 # Test creating and dropping an rtree table.
56 execsql { CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2) }
59 execsql { SELECT name FROM sqlite_master ORDER BY name }
60 } {t1 t1_node t1_parent t1_rowid}
64 SELECT name FROM sqlite_master ORDER BY name;
68 # Test creating and dropping an rtree table with an odd name in
69 # an attached database.
72 file delete -force test2.db
74 ATTACH 'test2.db' AS aux;
75 CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
79 execsql { SELECT name FROM sqlite_master ORDER BY name }
82 execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
83 } {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
86 DROP TABLE aux.'a" "b';
87 SELECT name FROM aux.sqlite_master ORDER BY name;
91 # Test that the logic for checking the number of columns specified
92 # for an rtree table. Acceptable values are odd numbers between 3 and
95 set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
96 for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
98 set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
101 if {$nCol%2 == 0} { set X {1 {Wrong number of columns for an rtree table}} }
102 if {$nCol < 3} { set X {1 {Too few columns for an rtree table}} }
103 if {$nCol > 11} { set X {1 {Too many columns for an rtree table}} }
105 do_test rtree-1.3.$nCol {
107 CREATE VIRTUAL TABLE t1 USING rtree($columns);
111 catchsql { DROP TABLE t1 }
114 # Like execsql except display output as integer where that can be
115 # done without loss of information.
117 proc execsql_intout {sql} {
119 foreach term [execsql $sql] {
120 regsub {\.0$} $term {} term
126 # Test that it is possible to open an existing database that contains
129 do_execsql_test rtree-1.4.1a {
130 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2);
131 INSERT INTO t1 VALUES(1, 5.0, 10.0);
132 SELECT substr(hex(data),1,40) FROM t1_node;
133 } {00000001000000000000000140A0000041200000}
134 do_execsql_test rtree-1.4.1b {
135 INSERT INTO t1 VALUES(2, 15.0, 20.0);
137 do_test rtree-1.4.2 {
140 execsql_intout { SELECT * FROM t1 ORDER BY ii }
142 do_test rtree-1.4.3 {
143 execsql { DROP TABLE t1 }
146 # Test that it is possible to create an r-tree table with ridiculous
149 do_test rtree-1.5.1 {
151 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
152 INSERT INTO t1 VALUES(1, 2, 3);
153 SELECT "the key", "x dim.", "x2'dim" FROM t1;
156 do_test rtree-1.5.1 {
157 execsql { DROP TABLE t1 }
160 # Force the r-tree constructor to fail.
162 do_test rtree-1.6.1 {
163 execsql { CREATE TABLE t1_rowid(a); }
165 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
167 } {1 {table "t1_rowid" already exists}}
168 do_test rtree-1.6.1 {
169 execsql { DROP TABLE t1_rowid }
172 #----------------------------------------------------------------------------
173 # Test cases rtree-2.*
175 do_test rtree-2.1.1 {
177 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
182 do_test rtree-2.1.2 {
183 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
184 execsql_intout { SELECT * FROM t1 }
186 do_test rtree-2.1.3 {
187 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
188 execsql { SELECT rowid FROM t1 ORDER BY rowid }
190 do_test rtree-2.1.3 {
191 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
192 execsql { SELECT ii FROM t1 ORDER BY ii }
195 do_test rtree-2.2.1 {
196 catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
197 } {1 {UNIQUE constraint failed: t1.ii}}
198 do_test rtree-2.2.2 {
199 catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
200 } {1 {rtree constraint failed: t1.(y1<=y2)}}
201 do_test rtree-2.2.3 {
202 catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
203 } {1 {rtree constraint failed: t1.(x1<=x2)}}
204 do_test rtree-2.2.4 {
205 execsql { SELECT ii FROM t1 ORDER BY ii }
209 execsql { DROP TABLE t1 }
212 #----------------------------------------------------------------------------
213 # Test cases rtree-3.* test linear scans of r-tree table data. To test
214 # this we have to insert some data into an r-tree, but that is not the
215 # focus of these tests.
217 do_test rtree-3.1.1 {
219 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
223 do_test rtree-3.1.2 {
225 INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
229 do_test rtree-3.1.3 {
231 INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
234 } {5 1 3 2 4 6 2 6 4 8}
236 # Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
237 do_test rtree-3.2.1 {
238 catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
239 } {1 {rtree constraint failed: t1.(y1<=y2)}}
240 do_test rtree-3.2.2 {
241 catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
244 #----------------------------------------------------------------------------
245 # Test cases rtree-5.* test DELETE operations.
247 do_test rtree-5.1.1 {
248 execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
250 do_test rtree-5.1.2 {
252 INSERT INTO t2 VALUES(1, 10, 20);
253 INSERT INTO t2 VALUES(2, 30, 40);
254 INSERT INTO t2 VALUES(3, 50, 60);
255 SELECT * FROM t2 ORDER BY ii;
257 } {1 10 20 2 30 40 3 50 60}
258 do_test rtree-5.1.3 {
260 DELETE FROM t2 WHERE ii=2;
261 SELECT * FROM t2 ORDER BY ii;
264 do_test rtree-5.1.4 {
266 DELETE FROM t2 WHERE ii=1;
267 SELECT * FROM t2 ORDER BY ii;
270 do_test rtree-5.1.5 {
272 DELETE FROM t2 WHERE ii=3;
273 SELECT * FROM t2 ORDER BY ii;
276 do_test rtree-5.1.6 {
277 execsql { SELECT * FROM t2_rowid }
280 #----------------------------------------------------------------------------
281 # Test cases rtree-5.* test UPDATE operations.
283 do_test rtree-6.1.1 {
284 execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
286 do_test rtree-6.1.2 {
288 INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
293 do_test rtree-6.1.3 {
294 execsql { UPDATE t3 SET ii = 2 }
295 execsql_intout { SELECT * FROM t3 }
298 #----------------------------------------------------------------------------
299 # Test cases rtree-7.* test rename operations.
301 do_test rtree-7.1.1 {
303 CREATE VIRTUAL TABLE t4 USING rtree(ii, x1, x2, y1, y2, z1, z2);
304 INSERT INTO t4 VALUES(1, 2, 3, 4, 5, 6, 7);
307 do_test rtree-7.1.2 {
308 execsql { ALTER TABLE t4 RENAME TO t5 }
309 execsql_intout { SELECT * FROM t5 }
311 do_test rtree-7.1.3 {
314 execsql_intout { SELECT * FROM t5 }
316 do_test rtree-7.1.4 {
317 execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
318 execsql_intout { SELECT * FROM "raisara ""one""'" }
320 do_test rtree-7.1.5 {
321 execsql_intout { SELECT * FROM 'raisara "one"''' }
323 do_test rtree-7.1.6 {
324 execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
325 execsql_intout { SELECT * FROM "abc 123" }
327 do_test rtree-7.1.7 {
330 execsql_intout { SELECT * FROM "abc 123" }
333 # An error midway through a rename operation.
334 do_test rtree-7.2.1 {
336 CREATE TABLE t4_node(a);
338 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
339 } {1 {SQL logic error or missing database}}
340 do_test rtree-7.2.2 {
341 execsql_intout { SELECT * FROM "abc 123" }
343 do_test rtree-7.2.3 {
346 CREATE TABLE t4_rowid(a);
348 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
349 } {1 {SQL logic error or missing database}}
350 do_test rtree-7.2.4 {
353 execsql_intout { SELECT * FROM "abc 123" }
355 do_test rtree-7.2.5 {
356 execsql { DROP TABLE t4_rowid }
357 execsql { ALTER TABLE "abc 123" RENAME TO t4 }
358 execsql_intout { SELECT * FROM t4 }
362 #----------------------------------------------------------------------------
363 # Test cases rtree-8.*
366 # Test that the function to determine if a leaf cell is part of the
368 do_test rtree-8.1.1 {
370 CREATE VIRTUAL TABLE t6 USING rtree(ii, x1, x2);
371 INSERT INTO t6 VALUES(1, 3, 7);
372 INSERT INTO t6 VALUES(2, 4, 6);
375 do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } } {1 2}
376 do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } } {2}
377 do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } } {}
378 do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } } {}
379 do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1<3 } } {}
380 do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1<4 } } {1}
381 do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1<5 } } {1 2}
383 #----------------------------------------------------------------------------
384 # Test cases rtree-9.*
386 # Test that ticket #3549 is fixed.
389 CREATE TABLE foo (id INTEGER PRIMARY KEY);
390 CREATE VIRTUAL TABLE bar USING rtree (id, minX, maxX, minY, maxY);
391 INSERT INTO foo VALUES (null);
392 INSERT INTO foo SELECT null FROM foo;
393 INSERT INTO foo SELECT null FROM foo;
394 INSERT INTO foo SELECT null FROM foo;
395 INSERT INTO foo SELECT null FROM foo;
396 INSERT INTO foo SELECT null FROM foo;
397 INSERT INTO foo SELECT null FROM foo;
398 DELETE FROM foo WHERE id > 40;
399 INSERT INTO bar SELECT NULL, 0, 0, 0, 0 FROM foo;
403 # This used to crash.
406 SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
411 SELECT count(*) FROM bar b1, bar b2, foo s1
412 WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
416 #-------------------------------------------------------------------------
417 # Ticket #3970: Check that the error message is meaningful when a
418 # keyword is used as a column name.
421 catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
422 } {1 {near "index": syntax error}}
424 #-------------------------------------------------------------------------
425 # Test last_insert_rowid().
429 CREATE VIRTUAL TABLE t8 USING rtree(idx, x1, x2, y1, y2);
430 INSERT INTO t8 VALUES(1, 1.0, 1.0, 2.0, 2.0);
431 SELECT last_insert_rowid();
436 INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
437 SELECT last_insert_rowid();
441 #-------------------------------------------------------------------------
442 # Test on-conflict clause handling.
445 do_execsql_test 12.0.1 {
446 CREATE VIRTUAL TABLE t1 USING rtree_i32(idx, x1, x2, y1, y2);
447 INSERT INTO t1 VALUES(1, 1, 2, 3, 4);
448 SELECT substr(hex(data),1,56) FROM t1_node;
449 } {00000001000000000000000100000001000000020000000300000004}
450 do_execsql_test 12.0.2 {
451 INSERT INTO t1 VALUES(2, 2, 3, 4, 5);
452 INSERT INTO t1 VALUES(3, 3, 4, 5, 6);
454 CREATE TABLE source(idx, x1, x2, y1, y2);
455 INSERT INTO source VALUES(5, 8, 8, 8, 8);
456 INSERT INTO source VALUES(2, 7, 7, 7, 7);
459 foreach {tn sql_template testdata} {
460 1 "INSERT %CONF% INTO t1 VALUES(2, 7, 7, 7, 7)" {
461 ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
462 ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
463 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
464 FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
465 REPLACE 0 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7}
468 2 "INSERT %CONF% INTO t1 SELECT * FROM source" {
469 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
470 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
471 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
472 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
473 REPLACE 1 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7 5 8 8 8 8}
476 3 "UPDATE %CONF% t1 SET idx = 2 WHERE idx = 4" {
477 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
478 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
479 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
480 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
481 REPLACE 1 0 {1 1 2 3 4 2 4 5 6 7 3 3 4 5 6}
484 3 "UPDATE %CONF% t1 SET idx = ((idx+1)%5)+1 WHERE idx > 2" {
485 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
486 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
487 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
488 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
489 REPLACE 1 0 {1 4 5 6 7 2 2 3 4 5 5 3 4 5 6}
492 4 "INSERT %CONF% INTO t1 VALUES(2, 7, 6, 7, 7)" {
493 ROLLBACK 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
494 ABORT 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
495 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
496 FAIL 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
497 REPLACE 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
501 foreach {mode uses error data} $testdata {
502 db_restore_and_reopen
504 set sql [string map [list %CONF% "OR $mode"] $sql_template]
505 set testname "12.$tn.[string tolower $mode]"
509 INSERT INTO t1 VALUES(4, 4, 5, 6, 7);
513 set res(1) {1 {UNIQUE constraint failed: t1.idx}}
514 set res(2) {1 {rtree constraint failed: t1.(x1<=x2)}}
516 do_catchsql_test $testname.1 $sql $res($error)
517 do_test $testname.2 [list sql_uses_stmt db $sql] $uses
518 do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
520 do_test $testname.4 { rtree_check db t1 } 0
525 #-------------------------------------------------------------------------
526 # Test that bug [d2889096e7bdeac6d] has been fixed.
529 do_execsql_test 13.1 {
530 CREATE VIRTUAL TABLE t9 USING rtree(id, xmin, xmax);
531 INSERT INTO t9 VALUES(1,0,0);
532 INSERT INTO t9 VALUES(2,0,0);
533 SELECT * FROM t9 WHERE id IN (1, 2);
534 } {1 0.0 0.0 2 0.0 0.0}
536 do_execsql_test 13.2 {
542 SELECT * FROM r CROSS JOIN t9 WHERE id=x;
543 } {1 1 0.0 0.0 2 2 0.0 0.0}
545 #-------------------------------------------------------------------------
546 # Test if a non-integer is inserted into the PK column of an r-tree
547 # table, it is converted to an integer before being inserted. Also
548 # that if a non-numeric is inserted into one of the min/max dimension
549 # columns, it is converted to the required type before being inserted.
551 do_execsql_test 14.1 {
552 CREATE VIRTUAL TABLE t10 USING rtree(ii, x1, x2);
555 do_execsql_test 14.2 {
556 INSERT INTO t10 VALUES(NULL, 1, 2);
557 INSERT INTO t10 VALUES(NULL, 2, 3);
558 INSERT INTO t10 VALUES('4xxx', 3, 4);
559 INSERT INTO t10 VALUES(5.0, 4, 5);
560 INSERT INTO t10 VALUES(6.4, 5, 6);
562 do_execsql_test 14.3 {
565 1 1.0 2.0 2 2.0 3.0 4 3.0 4.0 5 4.0 5.0 6 5.0 6.0
568 do_execsql_test 14.4 {
570 INSERT INTO t10 VALUES(1, 'one', 'two');
571 INSERT INTO t10 VALUES(2, '52xyz', '81...');
573 do_execsql_test 14.5 {
580 do_execsql_test 14.4 {
582 CREATE VIRTUAL TABLE t10 USING rtree_i32(ii, x1, x2);
583 INSERT INTO t10 VALUES(1, 'one', 'two');
584 INSERT INTO t10 VALUES(2, '52xyz', '81...');
585 INSERT INTO t10 VALUES(3, 42.3, 49.9);
587 do_execsql_test 14.5 {