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
23 # rtree-1.*: Creating/destroying r-tree tables.
24 # rtree-2.*: Test the implicit constraints - unique rowid and
25 # (coord[N]<=coord[N+1]) for even values of N. Also
26 # automatic assigning of rowid values.
27 # rtree-3.*: Linear scans of r-tree data.
28 # rtree-4.*: Test INSERT
29 # rtree-5.*: Test DELETE
30 # rtree-6.*: Test UPDATE
31 # rtree-7.*: Test renaming an r-tree table.
32 # rtree-8.*: Test constrained scans of r-tree data.
40 #----------------------------------------------------------------------------
41 # Test cases rtree-1.* test CREATE and DROP table statements.
44 # Test creating and dropping an rtree table.
47 execsql { CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2) }
50 execsql { SELECT name FROM sqlite_master ORDER BY name }
51 } {t1 t1_node t1_parent t1_rowid}
55 SELECT name FROM sqlite_master ORDER BY name;
59 # Test creating and dropping an rtree table with an odd name in
60 # an attached database.
63 file delete -force test2.db
65 ATTACH 'test2.db' AS aux;
66 CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
70 execsql { SELECT name FROM sqlite_master ORDER BY name }
73 execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
74 } {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
77 DROP TABLE aux.'a" "b';
78 SELECT name FROM aux.sqlite_master ORDER BY name;
82 # Test that the logic for checking the number of columns specified
83 # for an rtree table. Acceptable values are odd numbers between 3 and
86 set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
87 for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
89 set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
92 if {$nCol%2 == 0} { set X {1 {Wrong number of columns for an rtree table}} }
93 if {$nCol < 3} { set X {1 {Too few columns for an rtree table}} }
94 if {$nCol > 11} { set X {1 {Too many columns for an rtree table}} }
96 do_test rtree-1.3.$nCol {
98 CREATE VIRTUAL TABLE t1 USING rtree($columns);
102 catchsql { DROP TABLE t1 }
105 # Test that it is possible to open an existing database that contains
108 do_test rtree-1.4.1 {
110 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2);
111 INSERT INTO t1 VALUES(1, 5.0, 10.0);
112 INSERT INTO t1 VALUES(2, 15.0, 20.0);
115 do_test rtree-1.4.2 {
118 execsql { SELECT * FROM t1 ORDER BY ii }
119 } {1 5.0 10.0 2 15.0 20.0}
120 do_test rtree-1.4.3 {
121 execsql { DROP TABLE t1 }
124 # Test that it is possible to create an r-tree table with ridiculous
127 do_test rtree-1.5.1 {
129 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
130 INSERT INTO t1 VALUES(1, 2, 3);
131 SELECT "the key", "x dim.", "x2'dim" FROM t1;
134 do_test rtree-1.5.1 {
135 execsql { DROP TABLE t1 }
138 # Force the r-tree constructor to fail.
140 do_test rtree-1.6.1 {
141 execsql { CREATE TABLE t1_rowid(a); }
143 CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
145 } {1 {table "t1_rowid" already exists}}
146 do_test rtree-1.6.1 {
147 execsql { DROP TABLE t1_rowid }
150 #----------------------------------------------------------------------------
151 # Test cases rtree-2.*
153 do_test rtree-2.1.1 {
155 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
160 do_test rtree-2.1.2 {
161 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
162 execsql { SELECT * FROM t1 }
163 } {1 1.0 3.0 2.0 4.0}
164 do_test rtree-2.1.3 {
165 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
166 execsql { SELECT rowid FROM t1 ORDER BY rowid }
168 do_test rtree-2.1.3 {
169 execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
170 execsql { SELECT ii FROM t1 ORDER BY ii }
173 do_test rtree-2.2.1 {
174 catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
175 } {1 {constraint failed}}
176 do_test rtree-2.2.2 {
177 catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
178 } {1 {constraint failed}}
179 do_test rtree-2.2.3 {
180 catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
181 } {1 {constraint failed}}
182 do_test rtree-2.2.4 {
183 execsql { SELECT ii FROM t1 ORDER BY ii }
187 execsql { DROP TABLE t1 }
190 #----------------------------------------------------------------------------
191 # Test cases rtree-3.* test linear scans of r-tree table data. To test
192 # this we have to insert some data into an r-tree, but that is not the
193 # focus of these tests.
195 do_test rtree-3.1.1 {
197 CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
201 do_test rtree-3.1.2 {
203 INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
206 } {5 1.0 3.0 2.0 4.0}
207 do_test rtree-3.1.3 {
209 INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
212 } {5 1.0 3.0 2.0 4.0 6 2.0 6.0 4.0 8.0}
214 # Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
215 do_test rtree-3.2.1 {
216 catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
217 } {1 {constraint failed}}
218 do_test rtree-3.2.2 {
219 catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
222 #----------------------------------------------------------------------------
223 # Test cases rtree-5.* test DELETE operations.
225 do_test rtree-5.1.1 {
226 execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
228 do_test rtree-5.1.2 {
230 INSERT INTO t2 VALUES(1, 10, 20);
231 INSERT INTO t2 VALUES(2, 30, 40);
232 INSERT INTO t2 VALUES(3, 50, 60);
233 SELECT * FROM t2 ORDER BY ii;
235 } {1 10.0 20.0 2 30.0 40.0 3 50.0 60.0}
236 do_test rtree-5.1.3 {
238 DELETE FROM t2 WHERE ii=2;
239 SELECT * FROM t2 ORDER BY ii;
241 } {1 10.0 20.0 3 50.0 60.0}
242 do_test rtree-5.1.4 {
244 DELETE FROM t2 WHERE ii=1;
245 SELECT * FROM t2 ORDER BY ii;
248 do_test rtree-5.1.5 {
250 DELETE FROM t2 WHERE ii=3;
251 SELECT * FROM t2 ORDER BY ii;
254 do_test rtree-5.1.6 {
255 execsql { SELECT * FROM t2_rowid }
258 #----------------------------------------------------------------------------
259 # Test cases rtree-5.* test UPDATE operations.
261 do_test rtree-6.1.1 {
262 execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
264 do_test rtree-6.1.2 {
266 INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
270 } {1 2.0 5.0 4.0 5.0}
271 do_test rtree-6.1.3 {
272 execsql { UPDATE t3 SET ii = 2 }
273 execsql { SELECT * FROM t3 }
274 } {2 2.0 5.0 4.0 5.0}
276 #----------------------------------------------------------------------------
277 # Test cases rtree-7.* test rename operations.
279 do_test rtree-7.1.1 {
281 CREATE VIRTUAL TABLE t4 USING rtree(ii, x1, x2, y1, y2, z1, z2);
282 INSERT INTO t4 VALUES(1, 2, 3, 4, 5, 6, 7);
285 do_test rtree-7.1.2 {
286 execsql { ALTER TABLE t4 RENAME TO t5 }
287 execsql { SELECT * FROM t5 }
288 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
289 do_test rtree-7.1.3 {
292 execsql { SELECT * FROM t5 }
293 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
294 do_test rtree-7.1.4 {
295 execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
296 execsql { SELECT * FROM "raisara ""one""'" }
297 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
298 do_test rtree-7.1.5 {
299 execsql { SELECT * FROM 'raisara "one"''' }
300 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
301 do_test rtree-7.1.6 {
302 execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
303 execsql { SELECT * FROM "abc 123" }
304 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
305 do_test rtree-7.1.7 {
308 execsql { SELECT * FROM "abc 123" }
309 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
311 # An error midway through a rename operation.
312 do_test rtree-7.2.1 {
314 CREATE TABLE t4_node(a);
316 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
317 } {1 {SQL logic error or missing database}}
318 do_test rtree-7.2.2 {
319 execsql { SELECT * FROM "abc 123" }
320 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
321 do_test rtree-7.2.3 {
324 CREATE TABLE t4_rowid(a);
326 catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
327 } {1 {SQL logic error or missing database}}
328 do_test rtree-7.2.4 {
331 execsql { SELECT * FROM "abc 123" }
332 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
333 do_test rtree-7.2.5 {
334 execsql { DROP TABLE t4_rowid }
335 execsql { ALTER TABLE "abc 123" RENAME TO t4 }
336 execsql { SELECT * FROM t4 }
337 } {1 2.0 3.0 4.0 5.0 6.0 7.0}
340 #----------------------------------------------------------------------------
341 # Test cases rtree-8.*
344 # Test that the function to determine if a leaf cell is part of the
346 do_test rtree-8.1.1 {
348 CREATE VIRTUAL TABLE t6 USING rtree(ii, x1, x2);
349 INSERT INTO t6 VALUES(1, 3, 7);
350 INSERT INTO t6 VALUES(2, 4, 6);
353 do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } } {1 2}
354 do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } } {2}
355 do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } } {}
356 do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } } {}
357 do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1<3 } } {}
358 do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1<4 } } {1}
359 do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1<5 } } {1 2}
361 #----------------------------------------------------------------------------
362 # Test cases rtree-9.*
364 # Test that ticket #3549 is fixed.
367 CREATE TABLE foo (id INTEGER PRIMARY KEY);
368 CREATE VIRTUAL TABLE bar USING rtree (id, minX, maxX, minY, maxY);
369 INSERT INTO foo VALUES (null);
370 INSERT INTO foo SELECT null FROM foo;
371 INSERT INTO foo SELECT null FROM foo;
372 INSERT INTO foo SELECT null FROM foo;
373 INSERT INTO foo SELECT null FROM foo;
374 INSERT INTO foo SELECT null FROM foo;
375 INSERT INTO foo SELECT null FROM foo;
376 DELETE FROM foo WHERE id > 40;
377 INSERT INTO bar SELECT NULL, 0, 0, 0, 0 FROM foo;
381 # This used to crash.
384 SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
389 SELECT count(*) FROM bar b1, bar b2, foo s1
390 WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
394 #-------------------------------------------------------------------------
395 # Ticket #3970: Check that the error message is meaningful when a
396 # keyword is used as a column name.
399 catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
400 } {1 {near "index": syntax error}}
402 #-------------------------------------------------------------------------
403 # Test last_insert_rowid().
407 CREATE VIRTUAL TABLE t8 USING rtree(idx, x1, x2, y1, y2);
408 INSERT INTO t8 VALUES(1, 1.0, 1.0, 2.0, 2.0);
409 SELECT last_insert_rowid();
414 INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
415 SELECT last_insert_rowid();