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.
44 #----------------------------------------------------------------------------
45 # Test cases rtree-1.* test CREATE and DROP table statements.
48 # Test creating and dropping an rtree table.
51 execsql { CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2) }
54 execsql { SELECT name FROM sqlite_master ORDER BY name }
55 } {t1 t1_node t1_parent t1_rowid}
59 SELECT name FROM sqlite_master ORDER BY name;
63 # Test creating and dropping an rtree table with an odd name in
64 # an attached database.
67 file delete -force test2.db
69 ATTACH 'test2.db' AS aux;
70 CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
74 execsql { SELECT name FROM sqlite_master ORDER BY name }
77 execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
78 } {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
81 DROP TABLE aux.'a" "b';
82 SELECT name FROM aux.sqlite_master ORDER BY name;
86 # Test that the logic for checking the number of columns specified
87 # for an rtree table. Acceptable values are odd numbers between 3 and
90 set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
91 for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
93 set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
96 if {$nCol%2 == 0} { set X {1 {Wrong number of columns for an rtree table}} }
97 if {$nCol < 3} { set X {1 {Too few columns for an rtree table}} }
98 if {$nCol > 11} { set X {1 {Too many columns for an rtree table}} }
100 do_test rtree-1.3.$nCol {
102 CREATE VIRTUAL TABLE t1 USING rtree($columns);
106 catchsql { DROP TABLE t1 }
109 # Like execsql except display output as integer where that can be
110 # done without loss of information.
112 proc execsql_intout {sql} {
114 foreach term [execsql $sql] {
115 regsub {\.0$} $term {} term
121 # Test that it is possible to open an existing database that contains
124 do_execsql_test rtree-1.4.1a {
125 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2);
126 INSERT INTO t1 VALUES(1, 5.0, 10.0);
127 SELECT substr(hex(data),1,40) FROM t1_node;
128 } {00000001000000000000000140A0000041200000}
129 do_execsql_test rtree-1.4.1b {
130 INSERT INTO t1 VALUES(2, 15.0, 20.0);
132 do_test rtree-1.4.2 {
135 execsql_intout { SELECT * FROM t1 ORDER BY ii }
137 do_test rtree-1.4.3 {
138 execsql { DROP TABLE t1 }
141 # Test that it is possible to create an r-tree table with ridiculous
144 do_test rtree-1.5.1 {
146 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
147 INSERT INTO t1 VALUES(1, 2, 3);
148 SELECT "the key", "x dim.", "x2'dim" FROM t1;
151 do_test rtree-1.5.1 {
152 execsql { DROP TABLE t1 }
155 # Force the r-tree constructor to fail.
157 do_test rtree-1.6.1 {
158 execsql { CREATE TABLE t1_rowid(a); }
160 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
162 } {1 {table "t1_rowid" already exists}}
163 do_test rtree-1.6.1 {
164 execsql { DROP TABLE t1_rowid }
167 #----------------------------------------------------------------------------
168 # Test cases rtree-2.*
170 do_test rtree-2.1.1 {
172 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
177 do_test rtree-2.1.2 {
178 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
179 execsql_intout { SELECT * FROM t1 }
181 do_test rtree-2.1.3 {
182 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
183 execsql { SELECT rowid FROM t1 ORDER BY rowid }
185 do_test rtree-2.1.3 {
186 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
187 execsql { SELECT ii FROM t1 ORDER BY ii }
190 do_test rtree-2.2.1 {
191 catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
192 } {1 {constraint failed}}
193 do_test rtree-2.2.2 {
194 catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
195 } {1 {constraint failed}}
196 do_test rtree-2.2.3 {
197 catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
198 } {1 {constraint failed}}
199 do_test rtree-2.2.4 {
200 execsql { SELECT ii FROM t1 ORDER BY ii }
204 execsql { DROP TABLE t1 }
207 #----------------------------------------------------------------------------
208 # Test cases rtree-3.* test linear scans of r-tree table data. To test
209 # this we have to insert some data into an r-tree, but that is not the
210 # focus of these tests.
212 do_test rtree-3.1.1 {
214 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
218 do_test rtree-3.1.2 {
220 INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
224 do_test rtree-3.1.3 {
226 INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
229 } {5 1 3 2 4 6 2 6 4 8}
231 # Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
232 do_test rtree-3.2.1 {
233 catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
234 } {1 {constraint failed}}
235 do_test rtree-3.2.2 {
236 catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
239 #----------------------------------------------------------------------------
240 # Test cases rtree-5.* test DELETE operations.
242 do_test rtree-5.1.1 {
243 execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
245 do_test rtree-5.1.2 {
247 INSERT INTO t2 VALUES(1, 10, 20);
248 INSERT INTO t2 VALUES(2, 30, 40);
249 INSERT INTO t2 VALUES(3, 50, 60);
250 SELECT * FROM t2 ORDER BY ii;
252 } {1 10 20 2 30 40 3 50 60}
253 do_test rtree-5.1.3 {
255 DELETE FROM t2 WHERE ii=2;
256 SELECT * FROM t2 ORDER BY ii;
259 do_test rtree-5.1.4 {
261 DELETE FROM t2 WHERE ii=1;
262 SELECT * FROM t2 ORDER BY ii;
265 do_test rtree-5.1.5 {
267 DELETE FROM t2 WHERE ii=3;
268 SELECT * FROM t2 ORDER BY ii;
271 do_test rtree-5.1.6 {
272 execsql { SELECT * FROM t2_rowid }
275 #----------------------------------------------------------------------------
276 # Test cases rtree-5.* test UPDATE operations.
278 do_test rtree-6.1.1 {
279 execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
281 do_test rtree-6.1.2 {
283 INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
288 do_test rtree-6.1.3 {
289 execsql { UPDATE t3 SET ii = 2 }
290 execsql_intout { SELECT * FROM t3 }
293 #----------------------------------------------------------------------------
294 # Test cases rtree-7.* test rename operations.
296 do_test rtree-7.1.1 {
298 CREATE VIRTUAL TABLE t4 USING rtree(ii, x1, x2, y1, y2, z1, z2);
299 INSERT INTO t4 VALUES(1, 2, 3, 4, 5, 6, 7);
302 do_test rtree-7.1.2 {
303 execsql { ALTER TABLE t4 RENAME TO t5 }
304 execsql_intout { SELECT * FROM t5 }
306 do_test rtree-7.1.3 {
309 execsql_intout { SELECT * FROM t5 }
311 do_test rtree-7.1.4 {
312 execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
313 execsql_intout { SELECT * FROM "raisara ""one""'" }
315 do_test rtree-7.1.5 {
316 execsql_intout { SELECT * FROM 'raisara "one"''' }
318 do_test rtree-7.1.6 {
319 execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
320 execsql_intout { SELECT * FROM "abc 123" }
322 do_test rtree-7.1.7 {
325 execsql_intout { SELECT * FROM "abc 123" }
328 # An error midway through a rename operation.
329 do_test rtree-7.2.1 {
331 CREATE TABLE t4_node(a);
333 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
334 } {1 {SQL logic error or missing database}}
335 do_test rtree-7.2.2 {
336 execsql_intout { SELECT * FROM "abc 123" }
338 do_test rtree-7.2.3 {
341 CREATE TABLE t4_rowid(a);
343 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
344 } {1 {SQL logic error or missing database}}
345 do_test rtree-7.2.4 {
348 execsql_intout { SELECT * FROM "abc 123" }
350 do_test rtree-7.2.5 {
351 execsql { DROP TABLE t4_rowid }
352 execsql { ALTER TABLE "abc 123" RENAME TO t4 }
353 execsql_intout { SELECT * FROM t4 }
357 #----------------------------------------------------------------------------
358 # Test cases rtree-8.*
361 # Test that the function to determine if a leaf cell is part of the
363 do_test rtree-8.1.1 {
365 CREATE VIRTUAL TABLE t6 USING rtree(ii, x1, x2);
366 INSERT INTO t6 VALUES(1, 3, 7);
367 INSERT INTO t6 VALUES(2, 4, 6);
370 do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } } {1 2}
371 do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } } {2}
372 do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } } {}
373 do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } } {}
374 do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1<3 } } {}
375 do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1<4 } } {1}
376 do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1<5 } } {1 2}
378 #----------------------------------------------------------------------------
379 # Test cases rtree-9.*
381 # Test that ticket #3549 is fixed.
384 CREATE TABLE foo (id INTEGER PRIMARY KEY);
385 CREATE VIRTUAL TABLE bar USING rtree (id, minX, maxX, minY, maxY);
386 INSERT INTO foo VALUES (null);
387 INSERT INTO foo SELECT null FROM foo;
388 INSERT INTO foo SELECT null FROM foo;
389 INSERT INTO foo SELECT null FROM foo;
390 INSERT INTO foo SELECT null FROM foo;
391 INSERT INTO foo SELECT null FROM foo;
392 INSERT INTO foo SELECT null FROM foo;
393 DELETE FROM foo WHERE id > 40;
394 INSERT INTO bar SELECT NULL, 0, 0, 0, 0 FROM foo;
398 # This used to crash.
401 SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
406 SELECT count(*) FROM bar b1, bar b2, foo s1
407 WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
411 #-------------------------------------------------------------------------
412 # Ticket #3970: Check that the error message is meaningful when a
413 # keyword is used as a column name.
416 catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
417 } {1 {near "index": syntax error}}
419 #-------------------------------------------------------------------------
420 # Test last_insert_rowid().
424 CREATE VIRTUAL TABLE t8 USING rtree(idx, x1, x2, y1, y2);
425 INSERT INTO t8 VALUES(1, 1.0, 1.0, 2.0, 2.0);
426 SELECT last_insert_rowid();
431 INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
432 SELECT last_insert_rowid();
436 #-------------------------------------------------------------------------
437 # Test on-conflict clause handling.
440 do_execsql_test 12.0.1 {
441 CREATE VIRTUAL TABLE t1 USING rtree_i32(idx, x1, x2, y1, y2);
442 INSERT INTO t1 VALUES(1, 1, 2, 3, 4);
443 SELECT substr(hex(data),1,56) FROM t1_node;
444 } {00000001000000000000000100000001000000020000000300000004}
445 do_execsql_test 12.0.2 {
446 INSERT INTO t1 VALUES(2, 2, 3, 4, 5);
447 INSERT INTO t1 VALUES(3, 3, 4, 5, 6);
449 CREATE TABLE source(idx, x1, x2, y1, y2);
450 INSERT INTO source VALUES(5, 8, 8, 8, 8);
451 INSERT INTO source VALUES(2, 7, 7, 7, 7);
454 foreach {tn sql_template testdata} {
455 1 "INSERT %CONF% INTO t1 VALUES(2, 7, 7, 7, 7)" {
456 ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
457 ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
458 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
459 FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
460 REPLACE 0 0 {1 1 2 3 4 2 7 7 7 7 3 3 4 5 6 4 4 5 6 7}
463 2 "INSERT %CONF% INTO t1 SELECT * FROM source" {
464 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
465 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
466 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}
467 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}
468 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}
471 3 "UPDATE %CONF% t1 SET idx = 2 WHERE idx = 4" {
472 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
473 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
474 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
475 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
476 REPLACE 1 0 {1 1 2 3 4 2 4 5 6 7 3 3 4 5 6}
479 3 "UPDATE %CONF% t1 SET idx = ((idx+1)%5)+1 WHERE idx > 2" {
480 ROLLBACK 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
481 ABORT 1 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
482 IGNORE 1 0 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
483 FAIL 1 1 {1 1 2 3 4 2 2 3 4 5 4 4 5 6 7 5 3 4 5 6}
484 REPLACE 1 0 {1 4 5 6 7 2 2 3 4 5 5 3 4 5 6}
487 4 "INSERT %CONF% INTO t1 VALUES(2, 7, 6, 7, 7)" {
488 ROLLBACK 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6}
489 ABORT 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
490 IGNORE 0 0 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
491 FAIL 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
492 REPLACE 0 1 {1 1 2 3 4 2 2 3 4 5 3 3 4 5 6 4 4 5 6 7}
496 foreach {mode uses error data} $testdata {
497 db_restore_and_reopen
499 set sql [string map [list %CONF% "OR $mode"] $sql_template]
500 set testname "12.$tn.[string tolower $mode]"
504 INSERT INTO t1 VALUES(4, 4, 5, 6, 7);
508 set res(1) {1 {constraint failed}}
509 do_catchsql_test $testname.1 $sql $res($error)
510 do_test $testname.2 [list sql_uses_stmt db $sql] $uses
511 do_execsql_test $testname.3 { SELECT * FROM t1 ORDER BY idx } $data
513 do_test $testname.4 { rtree_check db t1 } 0
518 #-------------------------------------------------------------------------
519 # Test that bug [d2889096e7bdeac6d] has been fixed.
522 do_execsql_test 13.1 {
523 CREATE VIRTUAL TABLE t9 USING rtree(id, xmin, xmax);
524 INSERT INTO t9 VALUES(1,0,0);
525 INSERT INTO t9 VALUES(2,0,0);
526 SELECT * FROM t9 WHERE id IN (1, 2);
527 } {1 0.0 0.0 2 0.0 0.0}
529 do_execsql_test 13.2 {
535 SELECT * FROM r CROSS JOIN t9 WHERE id=x;
536 } {1 1 0.0 0.0 2 2 0.0 0.0}