xref: /sqlite-3.40.0/ext/rtree/rtree1.test (revision e8f2c9dc)
1# 2008 Feb 19
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12# The focus of this file is testing the r-tree extension.
13#
14
15if {![info exists testdir]} {
16  set testdir [file join [file dirname [info script]] .. .. test]
17}
18source [file join [file dirname [info script]] rtree_util.tcl]
19source $testdir/tester.tcl
20set testprefix rtree1
21
22# Test plan:
23#
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.
34#
35#   rtree-12.*: Test that on-conflict clauses are supported.
36#   rtree-13.*: Test that bug [d2889096e7bdeac6d] has been fixed.
37#
38
39ifcapable !rtree {
40  finish_test
41  return
42}
43
44#----------------------------------------------------------------------------
45# Test cases rtree-1.* test CREATE and DROP table statements.
46#
47
48# Test creating and dropping an rtree table.
49#
50do_test rtree-1.1.1 {
51  execsql { CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2) }
52} {}
53do_test rtree-1.1.2 {
54  execsql { SELECT name FROM sqlite_master ORDER BY name }
55} {t1 t1_node t1_parent t1_rowid}
56do_test rtree-1.1.3 {
57  execsql {
58    DROP TABLE t1;
59    SELECT name FROM sqlite_master ORDER BY name;
60  }
61} {}
62
63# Test creating and dropping an rtree table with an odd name in
64# an attached database.
65#
66do_test rtree-1.2.1 {
67  file delete -force test2.db
68  execsql {
69    ATTACH 'test2.db' AS aux;
70    CREATE VIRTUAL TABLE aux.'a" "b' USING rtree(ii, x1, x2, y1, y2);
71  }
72} {}
73do_test rtree-1.2.2 {
74  execsql { SELECT name FROM sqlite_master ORDER BY name }
75} {}
76do_test rtree-1.2.3 {
77  execsql { SELECT name FROM aux.sqlite_master ORDER BY name }
78} {{a" "b} {a" "b_node} {a" "b_parent} {a" "b_rowid}}
79do_test rtree-1.2.4 {
80  execsql {
81    DROP TABLE aux.'a" "b';
82    SELECT name FROM aux.sqlite_master ORDER BY name;
83  }
84} {}
85
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
88# 11, inclusive.
89#
90set cols [list i1 i2 i3 i4 i5 i6 i7 i8 i9 iA iB iC iD iE iF iG iH iI iJ iK]
91for {set nCol 1} {$nCol<[llength $cols]} {incr nCol} {
92
93  set columns [join [lrange $cols 0 [expr {$nCol-1}]] ,]
94
95  set X {0 {}}
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}} }
99
100  do_test rtree-1.3.$nCol {
101    catchsql "
102      CREATE VIRTUAL TABLE t1 USING rtree($columns);
103    "
104  } $X
105
106  catchsql { DROP TABLE t1 }
107}
108
109# Like execsql except display output as integer where that can be
110# done without loss of information.
111#
112proc execsql_intout {sql} {
113  set out {}
114  foreach term [execsql $sql] {
115    regsub {\.0$} $term {} term
116    lappend out $term
117  }
118  return $out
119}
120
121# Test that it is possible to open an existing database that contains
122# r-tree tables.
123#
124do_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}
129do_execsql_test rtree-1.4.1b {
130  INSERT INTO t1 VALUES(2, 15.0, 20.0);
131} {}
132do_test rtree-1.4.2 {
133  db close
134  sqlite3 db test.db
135  execsql_intout { SELECT * FROM t1 ORDER BY ii }
136} {1 5 10 2 15 20}
137do_test rtree-1.4.3 {
138  execsql { DROP TABLE t1 }
139} {}
140
141# Test that it is possible to create an r-tree table with ridiculous
142# column names.
143#
144do_test rtree-1.5.1 {
145  execsql_intout {
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;
149  }
150} {1 2 3}
151do_test rtree-1.5.1 {
152  execsql { DROP TABLE t1 }
153} {}
154
155# Force the r-tree constructor to fail.
156#
157do_test rtree-1.6.1 {
158  execsql { CREATE TABLE t1_rowid(a); }
159  catchsql {
160    CREATE VIRTUAL TABLE t1 USING rtree("the key", "x dim.", "x2'dim");
161  }
162} {1 {table "t1_rowid" already exists}}
163do_test rtree-1.6.1 {
164  execsql { DROP TABLE t1_rowid }
165} {}
166
167#----------------------------------------------------------------------------
168# Test cases rtree-2.*
169#
170do_test rtree-2.1.1 {
171  execsql {
172    CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
173    SELECT * FROM t1;
174  }
175} {}
176
177do_test rtree-2.1.2 {
178  execsql { INSERT INTO t1 VALUES(NULL, 1, 3, 2, 4) }
179  execsql_intout { SELECT * FROM t1 }
180} {1 1 3 2 4}
181do_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 }
184} {1 2}
185do_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 }
188} {1 2 3}
189
190do_test rtree-2.2.1 {
191  catchsql { INSERT INTO t1 VALUES(2, 1, 3, 2, 4) }
192} {1 {constraint failed}}
193do_test rtree-2.2.2 {
194  catchsql { INSERT INTO t1 VALUES(4, 1, 3, 4, 2) }
195} {1 {constraint failed}}
196do_test rtree-2.2.3 {
197  catchsql { INSERT INTO t1 VALUES(4, 3, 1, 2, 4) }
198} {1 {constraint failed}}
199do_test rtree-2.2.4 {
200  execsql { SELECT ii FROM t1 ORDER BY ii }
201} {1 2 3}
202
203do_test rtree-2.X {
204  execsql { DROP TABLE t1 }
205} {}
206
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.
211#
212do_test rtree-3.1.1 {
213  execsql {
214    CREATE VIRTUAL TABLE t1 USING rtree(ii, x1, x2, y1, y2);
215    SELECT * FROM t1;
216  }
217} {}
218do_test rtree-3.1.2 {
219  execsql_intout {
220    INSERT INTO t1 VALUES(5, 1, 3, 2, 4);
221    SELECT * FROM t1;
222  }
223} {5 1 3 2 4}
224do_test rtree-3.1.3 {
225  execsql_intout {
226    INSERT INTO t1 VALUES(6, 2, 6, 4, 8);
227    SELECT * FROM t1;
228  }
229} {5 1 3 2 4 6 2 6 4 8}
230
231# Test the constraint on the coordinates (c[i]<=c[i+1] where (i%2==0)):
232do_test rtree-3.2.1 {
233  catchsql { INSERT INTO t1 VALUES(7, 2, 6, 4, 3) }
234} {1 {constraint failed}}
235do_test rtree-3.2.2 {
236  catchsql { INSERT INTO t1 VALUES(8, 2, 6, 3, 3) }
237} {0 {}}
238
239#----------------------------------------------------------------------------
240# Test cases rtree-5.* test DELETE operations.
241#
242do_test rtree-5.1.1 {
243  execsql { CREATE VIRTUAL TABLE t2 USING rtree(ii, x1, x2) }
244} {}
245do_test rtree-5.1.2 {
246  execsql_intout {
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;
251  }
252} {1 10 20 2 30 40 3 50 60}
253do_test rtree-5.1.3 {
254  execsql_intout {
255    DELETE FROM t2 WHERE ii=2;
256    SELECT * FROM t2 ORDER BY ii;
257  }
258} {1 10 20 3 50 60}
259do_test rtree-5.1.4 {
260  execsql_intout {
261    DELETE FROM t2 WHERE ii=1;
262    SELECT * FROM t2 ORDER BY ii;
263  }
264} {3 50 60}
265do_test rtree-5.1.5 {
266  execsql {
267    DELETE FROM t2 WHERE ii=3;
268    SELECT * FROM t2 ORDER BY ii;
269  }
270} {}
271do_test rtree-5.1.6 {
272  execsql { SELECT * FROM t2_rowid }
273} {}
274
275#----------------------------------------------------------------------------
276# Test cases rtree-5.* test UPDATE operations.
277#
278do_test rtree-6.1.1 {
279  execsql { CREATE VIRTUAL TABLE t3 USING rtree(ii, x1, x2, y1, y2) }
280} {}
281do_test rtree-6.1.2 {
282  execsql_intout {
283    INSERT INTO t3 VALUES(1, 2, 3, 4, 5);
284    UPDATE t3 SET x2=5;
285    SELECT * FROM t3;
286  }
287} {1 2 5 4 5}
288do_test rtree-6.1.3 {
289  execsql { UPDATE t3 SET ii = 2 }
290  execsql_intout { SELECT * FROM t3 }
291} {2 2 5 4 5}
292
293#----------------------------------------------------------------------------
294# Test cases rtree-7.* test rename operations.
295#
296do_test rtree-7.1.1 {
297  execsql {
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);
300  }
301} {}
302do_test rtree-7.1.2 {
303  execsql { ALTER TABLE t4 RENAME TO t5 }
304  execsql_intout { SELECT * FROM t5 }
305} {1 2 3 4 5 6 7}
306do_test rtree-7.1.3 {
307  db close
308  sqlite3 db test.db
309  execsql_intout { SELECT * FROM t5 }
310} {1 2 3 4 5 6 7}
311do_test rtree-7.1.4 {
312  execsql { ALTER TABLE t5 RENAME TO 'raisara "one"'''}
313  execsql_intout { SELECT * FROM "raisara ""one""'" }
314} {1 2 3 4 5 6 7}
315do_test rtree-7.1.5 {
316  execsql_intout { SELECT * FROM 'raisara "one"''' }
317} {1 2 3 4 5 6 7}
318do_test rtree-7.1.6 {
319  execsql { ALTER TABLE "raisara ""one""'" RENAME TO "abc 123" }
320  execsql_intout { SELECT * FROM "abc 123" }
321} {1 2 3 4 5 6 7}
322do_test rtree-7.1.7 {
323  db close
324  sqlite3 db test.db
325  execsql_intout { SELECT * FROM "abc 123" }
326} {1 2 3 4 5 6 7}
327
328# An error midway through a rename operation.
329do_test rtree-7.2.1 {
330  execsql {
331    CREATE TABLE t4_node(a);
332  }
333  catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
334} {1 {SQL logic error or missing database}}
335do_test rtree-7.2.2 {
336  execsql_intout { SELECT * FROM "abc 123" }
337} {1 2 3 4 5 6 7}
338do_test rtree-7.2.3 {
339  execsql {
340    DROP TABLE t4_node;
341    CREATE TABLE t4_rowid(a);
342  }
343  catchsql { ALTER TABLE "abc 123" RENAME TO t4 }
344} {1 {SQL logic error or missing database}}
345do_test rtree-7.2.4 {
346  db close
347  sqlite3 db test.db
348  execsql_intout { SELECT * FROM "abc 123" }
349} {1 2 3 4 5 6 7}
350do_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 }
354} {1 2 3 4 5 6 7}
355
356
357#----------------------------------------------------------------------------
358# Test cases rtree-8.*
359#
360
361# Test that the function to determine if a leaf cell is part of the
362# result set works.
363do_test rtree-8.1.1 {
364  execsql {
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);
368  }
369} {}
370do_test rtree-8.1.2 { execsql { SELECT ii FROM t6 WHERE x1>2 } } {1 2}
371do_test rtree-8.1.3 { execsql { SELECT ii FROM t6 WHERE x1>3 } } {2}
372do_test rtree-8.1.4 { execsql { SELECT ii FROM t6 WHERE x1>4 } } {}
373do_test rtree-8.1.5 { execsql { SELECT ii FROM t6 WHERE x1>5 } } {}
374do_test rtree-8.1.6 { execsql { SELECT ii FROM t6 WHERE x1<3 } } {}
375do_test rtree-8.1.7 { execsql { SELECT ii FROM t6 WHERE x1<4 } } {1}
376do_test rtree-8.1.8 { execsql { SELECT ii FROM t6 WHERE x1<5 } } {1 2}
377
378#----------------------------------------------------------------------------
379# Test cases rtree-9.*
380#
381# Test that ticket #3549 is fixed.
382do_test rtree-9.1 {
383  execsql {
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;
395  }
396} {}
397
398# This used to crash.
399do_test rtree-9.2 {
400  execsql {
401    SELECT count(*) FROM bar b1, bar b2, foo s1 WHERE s1.id = b1.id;
402  }
403} {1600}
404do_test rtree-9.3 {
405  execsql {
406    SELECT count(*) FROM bar b1, bar b2, foo s1
407    WHERE b1.minX <= b2.maxX AND s1.id = b1.id;
408  }
409} {1600}
410
411#-------------------------------------------------------------------------
412# Ticket #3970: Check that the error message is meaningful when a
413# keyword is used as a column name.
414#
415do_test rtree-10.1 {
416  catchsql { CREATE VIRTUAL TABLE t7 USING rtree(index, x1, y1, x2, y2) }
417} {1 {near "index": syntax error}}
418
419#-------------------------------------------------------------------------
420# Test last_insert_rowid().
421#
422do_test rtree-11.1 {
423  execsql {
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();
427  }
428} {1}
429do_test rtree-11.2 {
430  execsql {
431    INSERT INTO t8 VALUES(NULL, 1.0, 1.0, 2.0, 2.0);
432    SELECT last_insert_rowid();
433  }
434} {2}
435
436#-------------------------------------------------------------------------
437# Test on-conflict clause handling.
438#
439db_delete_and_reopen
440do_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}
445do_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);
448
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);
452}
453db_save_and_close
454foreach {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}
461  }
462
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}
469  }
470
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}
477  }
478
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}
485  }
486
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}
493  }
494
495} {
496  foreach {mode uses error data} $testdata {
497    db_restore_and_reopen
498
499    set sql [string map [list %CONF% "OR $mode"] $sql_template]
500    set testname "12.$tn.[string tolower $mode]"
501
502    execsql {
503      BEGIN;
504        INSERT INTO t1 VALUES(4,   4, 5, 6, 7);
505    }
506
507    set res(0) {0 {}}
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
512
513    do_test $testname.4 { rtree_check db t1 } 0
514    db close
515  }
516}
517
518#-------------------------------------------------------------------------
519# Test that bug [d2889096e7bdeac6d] has been fixed.
520#
521reset_db
522do_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}
528
529do_execsql_test 13.2 {
530  WITH r(x) AS (
531    SELECT 1 UNION ALL
532    SELECT 2 UNION ALL
533    SELECT 3
534  )
535  SELECT * FROM r CROSS JOIN t9 WHERE id=x;
536} {1 1 0.0 0.0 2 2 0.0 0.0}
537
538finish_test
539