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.
42 # rtree-15.*: Check that DROP TABLE works within a transaction that
43 # writes to an r-tree table.
51 #----------------------------------------------------------------------------
52 # Test cases rtree-1.* test CREATE and DROP table statements.
55 # Test creating and dropping an rtree table.
58 execsql { CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2) }
61 execsql { SELECT name FROM sqlite_master ORDER BY name }
62 } {t1 t1_node t1_parent t1_rowid}
66 SELECT name FROM sqlite_master ORDER BY name;
70 # Test creating and dropping an rtree table with an odd name in
71 # an attached database.
74 file delete -force test2.db
76 ATTACH 'test2.db' AS aux;
77 CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
81 execsql { SELECT name FROM sqlite_master ORDER BY name }
84 execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
85 } {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
88 DROP TABLE aux.'a" "b';
89 SELECT name FROM aux.sqlite_master ORDER BY name;
93 # Test that the logic for checking the number of columns specified
94 # for an rtree table. Acceptable values are odd numbers between 3 and
97 set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
98 for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
100 set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
103 if {$nCol%2 == 0} { set X {1 {Wrong number of columns for an rtree table}} }
104 if {$nCol < 3} { set X {1 {Too few columns for an rtree table}} }
105 if {$nCol > 11} { set X {1 {Too many columns for an rtree table}} }
107 do_test rtree-1.3.$nCol {
109 CREATE VIRTUAL TABLE t1 USING rtree($columns);
113 catchsql { DROP TABLE t1 }
116 # Like execsql except display output as integer where that can be
117 # done without loss of information.
119 proc execsql_intout {sql} {
121 foreach term [execsql $sql] {
122 regsub {\.0$} $term {} term
128 # Test that it is possible to open an existing database that contains
131 do_execsql_test rtree-1.4.1a {
132 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2);
133 INSERT INTO t1 VALUES(1, 5.0, 10.0);
134 SELECT substr(hex(data),1,40) FROM t1_node;
135 } {00000001000000000000000140A0000041200000}
136 do_execsql_test rtree-1.4.1b {
137 INSERT INTO t1 VALUES(2, 15.0, 20.0);
139 do_test rtree-1.4.2 {
142 execsql_intout { SELECT * FROM t1 ORDER BY ii }
144 do_test rtree-1.4.3 {
145 execsql { DROP TABLE t1 }
148 # Test that it is possible to create an r-tree table with ridiculous
151 do_test rtree-1.5.1 {
153 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
154 INSERT INTO t1 VALUES(1, 2, 3);
155 SELECT "the key", "x dim.", "x2'dim" FROM t1;
158 do_test rtree-1.5.1 {
159 execsql { DROP TABLE t1 }
162 # Force the r-tree constructor to fail.
164 do_test rtree-1.6.1 {
165 execsql { CREATE TABLE t1_rowid(a); }
167 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
169 } {1 {table "t1_rowid" already exists}}
170 do_test rtree-1.6.1 {
171 execsql { DROP TABLE t1_rowid }
174 #----------------------------------------------------------------------------
175 # Test cases rtree-2.*
177 do_test rtree-2.1.1 {
179 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
184 do_test rtree-2.1.2 {
185 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
186 execsql_intout { SELECT * FROM t1 }
188 do_test rtree-2.1.3 {
189 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
190 execsql { SELECT rowid FROM t1 ORDER BY rowid }
192 do_test rtree-2.1.3 {
193 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
194 execsql { SELECT ii FROM t1 ORDER BY ii }
197 do_test rtree-2.2.1 {
198 catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
199 } {1 {UNIQUE constraint failed: t1.ii}}
200 do_test rtree-2.2.2 {
201 catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
202 } {1 {rtree constraint failed: t1.(y1<=y2)}}
203 do_test rtree-2.2.3 {
204 catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
205 } {1 {rtree constraint failed: t1.(x1<=x2)}}
206 do_test rtree-2.2.4 {
207 execsql { SELECT ii FROM t1 ORDER BY ii }
211 execsql { DROP TABLE t1 }
214 #----------------------------------------------------------------------------
215 # Test cases rtree-3.* test linear scans of r-tree table data. To test
216 # this we have to insert some data into an r-tree, but that is not the
217 # focus of these tests.
219 do_test rtree-3.1.1 {
221 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
225 do_test rtree-3.1.2 {
227 INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
231 do_test rtree-3.1.3 {
233 INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
236 } {5 1 3 2 4 6 2 6 4 8}
238 # Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
239 do_test rtree-3.2.1 {
240 catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
241 } {1 {rtree constraint failed: t1.(y1<=y2)}}
242 do_test rtree-3.2.2 {
243 catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
246 #----------------------------------------------------------------------------
247 # Test cases rtree-5.* test DELETE operations.
249 do_test rtree-5.1.1 {
250 execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
252 do_test rtree-5.1.2 {
254 INSERT INTO t2 VALUES(1, 10, 20);
255 INSERT INTO t2 VALUES(2, 30, 40);
256 INSERT INTO t2 VALUES(3, 50, 60);
257 SELECT * FROM t2 ORDER BY ii;
259 } {1 10 20 2 30 40 3 50 60}
260 do_test rtree-5.1.3 {
262 DELETE FROM t2 WHERE ii=2;
263 SELECT * FROM t2 ORDER BY ii;
266 do_test rtree-5.1.4 {
268 DELETE FROM t2 WHERE ii=1;
269 SELECT * FROM t2 ORDER BY ii;
272 do_test rtree-5.1.5 {
274 DELETE FROM t2 WHERE ii=3;
275 SELECT * FROM t2 ORDER BY ii;
278 do_test rtree-5.1.6 {
279 execsql { SELECT * FROM t2_rowid }
282 #----------------------------------------------------------------------------
283 # Test cases rtree-5.* test UPDATE operations.
285 do_test rtree-6.1.1 {
286 execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
288 do_test rtree-6.1.2 {
290 INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
295 do_test rtree-6.1.3 {
296 execsql { UPDATE t3 SET ii = 2 }
297 execsql_intout { SELECT * FROM t3 }
300 #----------------------------------------------------------------------------
301 # Test cases rtree-7.* test rename operations.
303 do_test rtree-7.1.1 {
305 CREATE VIRTUAL TABLE t4 USING rtree(ii, x1, x2, y1, y2, z1, z2);
306 INSERT INTO t4 VALUES(1, 2, 3, 4, 5, 6, 7);
309 do_test rtree-7.1.2 {
310 execsql { ALTER TABLE t4 RENAME TO t5 }
311 execsql_intout { SELECT * FROM t5 }
313 do_test rtree-7.1.3 {
316 execsql_intout { SELECT * FROM t5 }
318 do_test rtree-7.1.4 {
319 execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
320 execsql_intout { SELECT * FROM "raisara ""one""'" }
322 do_test rtree-7.1.5 {
323 execsql_intout { SELECT * FROM 'raisara "one"''' }
325 do_test rtree-7.1.6 {
326 execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
327 execsql_intout { SELECT * FROM "abc 123" }
329 do_test rtree-7.1.7 {
332 execsql_intout { SELECT * FROM "abc 123" }
335 # An error midway through a rename operation.
336 do_test rtree-7.2.1 {
338 CREATE TABLE t4_node(a);
340 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
341 } {1 {SQL logic error}}
342 do_test rtree-7.2.2 {
343 execsql_intout { SELECT * FROM "abc 123" }
345 do_test rtree-7.2.3 {
348 CREATE TABLE t4_rowid(a);
350 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
351 } {1 {SQL logic error}}
352 do_test rtree-7.2.4 {
355 execsql_intout { SELECT * FROM "abc 123" }
357 do_test rtree-7.2.5 {
358 execsql { DROP TABLE t4_rowid }
359 execsql { ALTER TABLE "abc 123" RENAME TO t4 }
360 execsql_intout { SELECT * FROM t4 }
364 #----------------------------------------------------------------------------
365 # Test cases rtree-8.*
368 # Test that the function to determine if a leaf cell is part of the
370 do_test rtree-8.1.1 {
372 CREATE VIRTUAL TABLE t6 USING rtree(ii, x1, x2);
373 INSERT INTO t6 VALUES(1, 3, 7);
374 INSERT INTO t6 VALUES(2, 4, 6);
377 do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } } {1 2}
378 do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } } {2}
379 do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } } {}
380 do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } } {}
381 do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1<3 } } {}
382 do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1<4 } } {1}
383 do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1<5 } } {1 2}
385 #----------------------------------------------------------------------------
386 # Test cases rtree-9.*
388 # Test that ticket #3549 is fixed.
391 CREATE TABLE foo (id INTEGER PRIMARY KEY);
392 CREATE VIRTUAL TABLE bar USING rtree (id, minX, maxX, minY, maxY);
393 INSERT INTO foo VALUES (null);
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 INSERT INTO foo SELECT null FROM foo;
399 INSERT INTO foo SELECT null FROM foo;
400 DELETE FROM foo WHERE id > 40;
401 INSERT INTO bar SELECT NULL, 0, 0, 0, 0 FROM foo;
405 # This used to crash.
408 SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
413 SELECT count(*) FROM bar b1, bar b2, foo s1
414 WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
418 #-------------------------------------------------------------------------
419 # Ticket #3970: Check that the error message is meaningful when a
420 # keyword is used as a column name.
423 catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
424 } {1 {near "index": syntax error}}
426 #-------------------------------------------------------------------------
427 # Test last_insert_rowid().
431 CREATE VIRTUAL TABLE t8 USING rtree(idx, x1, x2, y1, y2);
432 INSERT INTO t8 VALUES(1, 1.0, 1.0, 2.0, 2.0);
433 SELECT last_insert_rowid();
438 INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
439 SELECT last_insert_rowid();
443 #-------------------------------------------------------------------------
444 # Test on-conflict clause handling.
447 do_execsql_test 12.0.1 {
448 CREATE VIRTUAL TABLE t1 USING rtree_i32(idx, x1, x2, y1, y2);
449 INSERT INTO t1 VALUES(1, 1, 2, 3, 4);
450 SELECT substr(hex(data),1,56) FROM t1_node;
451 } {00000001000000000000000100000001000000020000000300000004}
452 do_execsql_test 12.0.2 {
453 INSERT INTO t1 VALUES(2, 2, 3, 4, 5);
454 INSERT INTO t1 VALUES(3, 3, 4, 5, 6);
456 CREATE TABLE source(idx, x1, x2, y1, y2);
457 INSERT INTO source VALUES(5, 8, 8, 8, 8);
458 INSERT INTO source VALUES(2, 7, 7, 7, 7);
461 foreach {tn sql_template testdata} {
462 1 "INSERT %CONF% INTO t1 VALUES(2, 7, 7, 7, 7)" {
463 ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
464 ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
465 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
466 FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
467 REPLACE 0 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7}
470 2 "INSERT %CONF% INTO t1 SELECT * FROM source" {
471 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
472 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
473 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}
474 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}
475 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}
478 3 "UPDATE %CONF% t1 SET idx = 2 WHERE idx = 4" {
479 ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
480 ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
481 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
482 FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
483 REPLACE 0 0 {1 1 2 3 4 2 4 5 6 7 3 3 4 5 6}
486 3 "UPDATE %CONF% t1 SET idx = ((idx+1)%5)+1 WHERE idx > 2" {
487 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
488 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
489 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
490 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
491 REPLACE 1 0 {1 4 5 6 7 2 2 3 4 5 5 3 4 5 6}
494 4 "INSERT %CONF% INTO t1 VALUES(2, 7, 6, 7, 7)" {
495 ROLLBACK 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
496 ABORT 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
497 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
498 FAIL 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
499 REPLACE 0 2 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
503 foreach {mode uses error data} $testdata {
504 db_restore_and_reopen
506 set sql [string map [list %CONF% "OR $mode"] $sql_template]
507 set testname "12.$tn.[string tolower $mode]"
511 INSERT INTO t1 VALUES(4, 4, 5, 6, 7);
515 set res(1) {1 {UNIQUE constraint failed: t1.idx}}
516 set res(2) {1 {rtree constraint failed: t1.(x1<=x2)}}
518 do_catchsql_test $testname.1 $sql $res($error)
519 do_test $testname.2 [list sql_uses_stmt db $sql] $uses
520 do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
522 do_rtree_integrity_test $testname.4 t1
527 #-------------------------------------------------------------------------
528 # Test that bug [d2889096e7bdeac6d] has been fixed.
531 do_execsql_test 13.1 {
532 CREATE VIRTUAL TABLE t9 USING rtree(id, xmin, xmax);
533 INSERT INTO t9 VALUES(1,0,0);
534 INSERT INTO t9 VALUES(2,0,0);
535 SELECT * FROM t9 WHERE id IN (1, 2);
536 } {1 0.0 0.0 2 0.0 0.0}
538 do_execsql_test 13.2 {
544 SELECT * FROM r CROSS JOIN t9 WHERE id=x;
545 } {1 1 0.0 0.0 2 2 0.0 0.0}
547 #-------------------------------------------------------------------------
548 # Test if a non-integer is inserted into the PK column of an r-tree
549 # table, it is converted to an integer before being inserted. Also
550 # that if a non-numeric is inserted into one of the min/max dimension
551 # columns, it is converted to the required type before being inserted.
553 do_execsql_test 14.1 {
554 CREATE VIRTUAL TABLE t10 USING rtree(ii, x1, x2);
557 do_execsql_test 14.2 {
558 INSERT INTO t10 VALUES(NULL, 1, 2);
559 INSERT INTO t10 VALUES(NULL, 2, 3);
560 INSERT INTO t10 VALUES('4xxx', 3, 4);
561 INSERT INTO t10 VALUES(5.0, 4, 5);
562 INSERT INTO t10 VALUES(6.4, 5, 6);
564 do_execsql_test 14.3 {
567 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
570 do_execsql_test 14.4 {
572 INSERT INTO t10 VALUES(1, 'one', 'two');
573 INSERT INTO t10 VALUES(2, '52xyz', '81...');
575 do_execsql_test 14.5 {
582 do_execsql_test 14.4 {
584 CREATE VIRTUAL TABLE t10 USING rtree_i32(ii, x1, x2);
585 INSERT INTO t10 VALUES(1, 'one', 'two');
586 INSERT INTO t10 VALUES(2, '52xyz', '81...');
587 INSERT INTO t10 VALUES(3, 42.3, 49.9);
589 do_execsql_test 14.5 {
597 #-------------------------------------------------------------------------
599 do_execsql_test 15.0 {
600 CREATE VIRTUAL TABLE rt USING rtree(id, x1,x2, y1,y2);
601 CREATE TEMP TABLE t13(a, b, c);
603 do_execsql_test 15.1 {
605 INSERT INTO rt VALUES(1,2,3,4,5);
607 do_execsql_test 15.2 {
612 # Test cases for the new auxiliary columns feature
614 do_catchsql_test 16.100 {
615 CREATE VIRTUAL TABLE t16 USING rtree(id,x0,x1,y0,+aux1,x1);
616 } {1 {Auxiliary rtree columns must be last}}
619 CREATE VIRTUAL TABLE t16 USING rtree(
620 id, x00, x01, x10, x11, x20, x21, x30, x31, x40, x41
622 for {set i 12} {$i<=100} {incr i} {
630 CREATE VIRTUAL TABLE t16b USING rtree(
631 id, x00, x01, x10, x11, x20, x21, x30, x31, x40, x41
633 for {set i 12} {$i<=101} {incr i} {
638 } {1 {Too many columns for an rtree table}}
640 do_execsql_test 16.130 {
641 DROP TABLE IF EXISTS rt1;
642 CREATE VIRTUAL TABLE rt1 USING rtree(id, x1, x2, +aux);
643 INSERT INTO rt1 VALUES(1, 1, 2, 'aux1');
644 INSERT INTO rt1 VALUES(2, 2, 3, 'aux2');
645 INSERT INTO rt1 VALUES(3, 3, 4, 'aux3');
646 INSERT INTO rt1 VALUES(4, 4, 5, 'aux4');
647 SELECT * FROM rt1 WHERE id IN (1, 2, 3, 4);
648 } {1 1.0 2.0 aux1 2 2.0 3.0 aux2 3 3.0 4.0 aux3 4 4.0 5.0 aux4}