interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / rtree / rtreedoc.test

1594 lines · 54555 bytes Raw Blame History
1# 2021 September 13
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 rtreedoc
21
22ifcapable !rtree {
23 finish_test
24 return
25}
26
27# This command returns the number of columns in table $tbl within the
28# database opened by database handle $db
29proc column_count {db tbl} {
30 set nCol 0
31 $db eval "PRAGMA table_info = $tbl" { incr nCol }
32 return $nCol
33}
34
35proc column_name_list {db tbl} {
36 set lCol [list]
37 $db eval "PRAGMA table_info = $tbl" {
38 lappend lCol $name
39 }
40 return $lCol
41}
42unset -nocomplain res
43
44#-------------------------------------------------------------------------
45#-------------------------------------------------------------------------
46# Section 3 of documentation.
47#-------------------------------------------------------------------------
48#-------------------------------------------------------------------------
49set testprefix rtreedoc-1
50
51# EVIDENCE-OF: R-15060-13876 A 1-dimensional R*Tree thus has 3 columns.
52do_execsql_test 1.1.1 { CREATE VIRTUAL TABLE rt1 USING rtree(id, x1,x2) }
53do_test 1.1.2 { column_count db rt1 } 3
54
55# EVIDENCE-OF: R-19353-19546 A 2-dimensional R*Tree has 5 columns.
56do_execsql_test 1.2.1 { CREATE VIRTUAL TABLE rt2 USING rtree(id,x1,x2, y1,y2) }
57do_test 1.2.2 { column_count db rt2 } 5
58
59# EVIDENCE-OF: R-13615-19528 A 3-dimensional R*Tree has 7 columns.
60do_execsql_test 1.3.1 {
61 CREATE VIRTUAL TABLE rt3 USING rtree(id, x1,x2, y1,y2, z1,z2)
62}
63do_test 1.3.2 { column_count db rt3 } 7
64
65# EVIDENCE-OF: R-53479-41922 A 4-dimensional R*Tree has 9 columns.
66do_execsql_test 1.4.1 {
67 CREATE VIRTUAL TABLE rt4 USING rtree(id, x1,x2, y1,y2, z1,z2, v1,v2)
68}
69do_test 1.4.2 { column_count db rt4 } 9
70
71# EVIDENCE-OF: R-13981-28768 And a 5-dimensional R*Tree has 11 columns.
72do_execsql_test 1.5.1 {
73 CREATE VIRTUAL TABLE rt5 USING rtree(id, x1,x2, y1,y2, z1,z2, v1,v2, w1,w2)
74}
75do_test 1.5.2 { column_count db rt5 } 11
76
77
78# Attempt to create r-tree tables with 6 and 7 dimensions.
79#
80# EVIDENCE-OF: R-61533-25862 The SQLite R*Tree implementation does not
81# support R*Trees wider than 5 dimensions.
82do_catchsql_test 2.1.1 {
83 CREATE VIRTUAL TABLE rt6 USING rtree(
84 id, x1,x2, y1,y2, z1,z2, v1,v2, w1,w2, a1,a2
85 )
86} {1 {Too many columns for an rtree table}}
87do_catchsql_test 2.1.2 {
88 CREATE VIRTUAL TABLE rt6 USING rtree(
89 id, x1,x2, y1,y2, z1,z2, v1,v2, w1,w2, a1,a2, b1, b2
90 )
91} {1 {Too many columns for an rtree table}}
92
93# Attempt to create r-tree tables with no columns, a single column, or
94# an even number of columns. This and the tests above establish that:
95#
96# EVIDENCE-OF: R-16717-50504 Each R*Tree index is a virtual table with
97# an odd number of columns between 3 and 11.
98foreach {tn cols err} {
99 1 "" "Too few columns for an rtree table"
100 2 "x" "Too few columns for an rtree table"
101 3 "x,y" "Too few columns for an rtree table"
102 4 "a,b,c,d" "Wrong number of columns for an rtree table"
103 5 "a,b,c,d,e,f" "Wrong number of columns for an rtree table"
104 6 "a,b,c,d,e,f,g,h" "Wrong number of columns for an rtree table"
105 7 "a,b,c,d,e,f,g,h,i,j" "Wrong number of columns for an rtree table"
106 8 "a,b,c,d,e,f,g,h,i,j,k,l" "Too many columns for an rtree table"
107} {
108 do_catchsql_test 3.$tn "
109 CREATE VIRTUAL TABLE xyz USING rtree($cols)
110 " [list 1 $err]
111}
112
113# EVIDENCE-OF: R-17874-21123 The first column of an SQLite R*Tree is
114# similar to an integer primary key column of a normal SQLite table.
115#
116# EVIDENCE-OF: R-46619-65417 The first column is always a 64-bit signed
117# integer primary key.
118#
119# EVIDENCE-OF: R-46866-24036 It may only store a 64-bit signed integer
120# value.
121#
122# EVIDENCE-OF: R-00250-64843 If an attempt is made to insert any other
123# non-integer value into this column, the r-tree module silently
124# converts it to an integer before writing it into the database.
125#
126do_execsql_test 4.0 { CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2) }
127foreach {tn val res} {
128 1 10 10
129 2 10.6 10
130 3 10.99 10
131 4 '123' 123
132 5 X'313233' 123
133 6 -10 -10
134 7 9223372036854775807 9223372036854775807
135 8 -9223372036854775808 -9223372036854775808
136 9 '9223372036854775807' 9223372036854775807
137 10 '-9223372036854775808' -9223372036854775808
138 11 'hello+world' 0
139} {
140 do_execsql_test 4.$tn.1 "
141 DELETE FROM rt;
142 INSERT INTO rt VALUES($val, 10, 20);
143 "
144 do_execsql_test 4.$tn.2 {
145 SELECT typeof(id), id FROM rt
146 } [list integer $res]
147}
148
149# EVIDENCE-OF: R-15544-29079 Inserting a NULL value into this column
150# causes SQLite to automatically generate a new unique primary key
151# value.
152do_execsql_test 5.1 {
153 DELETE FROM rt;
154 INSERT INTO rt VALUES(100, 1, 2);
155 INSERT INTO rt VALUES(NULL, 1, 2);
156}
157do_execsql_test 5.2 { SELECT id FROM rt } {100 101}
158do_execsql_test 5.3 {
159 INSERT INTO rt VALUES(9223372036854775807, 1, 2);
160 INSERT INTO rt VALUES(NULL, 1, 2);
161}
162do_execsql_test 5.4 {
163 SELECT count(*) FROM rt;
164} 4
165do_execsql_test 5.5 {
166 SELECT id IN(100, 101, 9223372036854775807) FROM rt ORDER BY 1;
167} {0 1 1 1}
168
169
170# EVIDENCE-OF: R-64317-38978 The other columns are pairs, one pair per
171# dimension, containing the minimum and maximum values for that
172# dimension, respectively.
173#
174# Show this by observing that attempts to insert rows with max>min fail.
175#
176do_execsql_test 6.1 {
177 CREATE VIRTUAL TABLE rtF USING rtree(id, x1,x2, y1,y2);
178 CREATE VIRTUAL TABLE rtI USING rtree_i32(id, x1,x2, y1,y2, z1,z2);
179}
180foreach {tn x1 x2 y1 y2 ok} {
181 1 10.3 20.1 30.9 40.2 1
182 2 10.3 20.1 40.2 30.9 0
183 3 10.3 30.9 20.1 40.2 1
184 4 20.1 10.3 30.9 40.2 0
185} {
186 do_test 6.2.$tn {
187 catch { db eval { INSERT INTO rtF VALUES(NULL, $x1, $x2, $y1, $y2) } }
188 } [expr $ok==0]
189}
190foreach {tn x1 x2 y1 y2 z1 z2 ok} {
191 1 10 20 30 40 50 60 1
192 2 10 20 30 40 60 50 0
193 3 10 20 30 50 40 60 1
194 4 10 20 40 30 50 60 0
195 5 10 30 20 40 50 60 1
196 6 20 10 30 40 50 60 0
197} {
198 do_test 6.3.$tn {
199 catch { db eval { INSERT INTO rtI VALUES(NULL,$x1,$x2,$y1,$y2,$z1,$z2) } }
200 } [expr $ok==0]
201}
202
203# EVIDENCE-OF: R-08054-15429 The min/max-value pair columns are stored
204# as 32-bit floating point values for "rtree" virtual tables or as
205# 32-bit signed integers in "rtree_i32" virtual tables.
206#
207# Show this by showing that large values are rounded in ways consistent
208# with those two 32-bit types.
209do_execsql_test 7.1 {
210 DELETE FROM rtI;
211 INSERT INTO rtI VALUES(0,
212 -2_000_000_000, 2_000_000_000,
213 -5_000_000_000, 5_000_000_000,
214 1_000_000_000_000, 10_000_000_000_000
215 );
216 SELECT * FROM rtI;
217} {
218 0 -2000000000 2000000000 -2147483648 2147483647 2147483647 2147483647
219}
220do_execsql_test 7.2 {
221 DELETE FROM rtF;
222 INSERT INTO rtF VALUES(
223 0, -2000000000, 2000000000,
224 -1000000000000, 10000000000000
225 );
226 SELECT * FROM rtF;
227} {
228 0 -2000000000.0 2000000000.0 -1000000061440.0 10000000876544.0
229}
230
231# EVIDENCE-OF: R-47371-54529 Unlike regular SQLite tables which can
232# store data in a variety of datatypes and formats, the R*Tree rigidly
233# enforce these storage types.
234#
235# EVIDENCE-OF: R-39153-14977 If any other type of value is inserted into
236# such a column, the r-tree module silently converts it to the required
237# type before writing the new record to the database.
238do_execsql_test 8.1 {
239 DELETE FROM rtI;
240 INSERT INTO rtI VALUES(
241 1, 'hello world', X'616263', NULL, 44.5, 1000, 9999.9999
242 );
243 SELECT * FROM rtI;
244} {
245 1 0 0 0 44 1000 9999
246}
247
248do_execsql_test 8.2 {
249 SELECT
250 typeof(x1), typeof(x2), typeof(y1), typeof(y2), typeof(z1), typeof(z2)
251 FROM rtI
252} {integer integer integer integer integer integer}
253
254do_execsql_test 8.3 {
255 DELETE FROM rtF;
256 INSERT INTO rtF VALUES(
257 1, 'hello world', X'616263', NULL, 44
258 );
259 SELECT * FROM rtF;
260} {
261 1 0.0 0.0 0.0 44.0
262}
263do_execsql_test 8.4 {
264 SELECT
265 typeof(x1), typeof(x2), typeof(y1), typeof(y2)
266 FROM rtF
267} {real real real real}
268
269
270
271
272#-------------------------------------------------------------------------
273#-------------------------------------------------------------------------
274# Section 3.1 of documentation.
275#-------------------------------------------------------------------------
276#-------------------------------------------------------------------------
277set testprefix rtreedoc-2
278reset_db
279
280foreach {tn name clist} {
281 1 t1 "id x1 x2"
282 2 t2 "id x1 x2 y1 y2 z1 z2"
283} {
284# EVIDENCE-OF: R-15142-18077 A new R*Tree index is created as follows:
285# CREATE VIRTUAL TABLE <name> USING rtree(<column-names>);
286 do_execsql_test 1.$tn.1 "
287 CREATE VIRTUAL TABLE $name USING rtree([join $clist ,])
288 "
289
290# EVIDENCE-OF: R-51698-09302 The <name> is the name your
291# application chooses for the R*Tree index and <column-names> is a
292# comma separated list of between 3 and 11 columns.
293 do_test 1.$tn.2 { column_name_list db $name } [list {*}$clist]
294
295# EVIDENCE-OF: R-50130-53472 The virtual <name> table creates
296# three shadow tables to actually store its content.
297 do_execsql_test 1.$tn.3 {
298 SELECT count(*) FROM sqlite_schema
299 } [expr 1+3]
300
301# EVIDENCE-OF: R-45256-35998 The names of these shadow tables are:
302# <name>_node <name>_rowid <name>_parent
303 do_execsql_test 1.$tn.4 {
304 SELECT name FROM sqlite_schema WHERE rootpage>0 ORDER BY 1
305 } [list ${name}_node ${name}_parent ${name}_rowid]
306
307 do_execsql_test 1.$tn.5 "DROP TABLE $name"
308}
309
310# EVIDENCE-OF: R-11241-54478 As an example, consider creating a
311# two-dimensional R*Tree index for use in spatial queries: CREATE
312# VIRTUAL TABLE demo_index USING rtree( id, -- Integer primary key minX,
313# maxX, -- Minimum and maximum X coordinate minY, maxY -- Minimum and
314# maximum Y coordinate );
315do_execsql_test 2.0 {
316 CREATE VIRTUAL TABLE demo_index USING rtree(
317 id, -- Integer primary key
318 minX, maxX, -- Minimum and maximum X coordinate
319 minY, maxY -- Minimum and maximum Y coordinate
320 );
321 INSERT INTO demo_index VALUES(1,2,3,4,5);
322 INSERT INTO demo_index VALUES(6,7,8,9,10);
323}
324
325# EVIDENCE-OF: R-02287-33529 The shadow tables are ordinary SQLite data
326# tables.
327#
328# Ordinary tables. With ordinary sqlite_schema entries.
329do_execsql_test 2.1 {
330 SELECT type, name, sql FROM sqlite_schema WHERE sql NOT LIKE '%virtual%'
331} {
332 table demo_index_rowid
333 {CREATE TABLE "demo_index_rowid"(rowid INTEGER PRIMARY KEY,nodeno)}
334 table demo_index_node
335 {CREATE TABLE "demo_index_node"(nodeno INTEGER PRIMARY KEY,data)}
336 table demo_index_parent
337 {CREATE TABLE "demo_index_parent"(nodeno INTEGER PRIMARY KEY,parentnode)}
338}
339
340# EVIDENCE-OF: R-10863-13089 You can query them directly if you like,
341# though this unlikely to reveal anything particularly useful.
342#
343# Querying:
344do_execsql_test 2.2 {
345 SELECT count(*) FROM demo_index_node;
346 SELECT count(*) FROM demo_index_rowid;
347 SELECT count(*) FROM demo_index_parent;
348} {1 2 0}
349
350# EVIDENCE-OF: R-05650-46070 And you can UPDATE, DELETE, INSERT or even
351# DROP the shadow tables, though doing so will corrupt your R*Tree
352# index.
353do_execsql_test 2.3 {
354 DELETE FROM demo_index_rowid;
355 INSERT INTO demo_index_parent VALUES(2, 3);
356 UPDATE demo_index_node SET data = 'hello world'
357}
358do_catchsql_test 2.4 {
359 SELECT * FROM demo_index WHERE minX>10 AND maxX<30
360} {1 {database disk image is malformed}}
361do_execsql_test 2.5 {
362 DROP TABLE demo_index_rowid
363}
364
365#-------------------------------------------------------------------------
366#-------------------------------------------------------------------------
367# Section 3.1.1 of documentation.
368#-------------------------------------------------------------------------
369#-------------------------------------------------------------------------
370set testprefix rtreedoc-3
371reset_db
372
373# EVIDENCE-OF: R-44253-50720 In the argments to "rtree" in the CREATE
374# VIRTUAL TABLE statement, the names of the columns are taken from the
375# first token of each argument. All subsequent tokens within each
376# argument are silently ignored.
377#
378foreach {tn cols lCol} {
379 1 {(id TEXT, x1 TEXT, x2 TEXT, y1 TEXT, y2 TEXT)} {id x1 x2 y1 y2}
380 2 {(id TEXT, x1 UNIQUE, x2 TEXT, y1 NOT NULL, y2 TEXT)} {id x1 x2 y1 y2}
381 3 {(id, x1 DEFAULT 4, x2 TEXT, y1 NOT NULL, y2 TEXT)} {id x1 x2 y1 y2}
382} {
383 do_execsql_test 1.$tn.1 " CREATE VIRTUAL TABLE abc USING rtree $cols "
384 do_test 1.$tn.2 { column_name_list db abc } $lCol
385
386# EVIDENCE-OF: R-52032-06717 This means, for example, that if you try to
387# give a column a type affinity or add a constraint such as UNIQUE or
388# NOT NULL or DEFAULT to a column, those extra tokens are accepted as
389# valid, but they do not change the behavior of the rtree.
390
391 # Show there are no UNIQUE constraints
392 do_execsql_test 1.$tn.3 {
393 INSERT INTO abc VALUES(1, 10.0, 20.0, 10.0, 20.0);
394 INSERT INTO abc VALUES(2, 10.0, 20.0, 10.0, 20.0);
395 }
396
397 # Show the default values have not been modified
398 do_execsql_test 1.$tn.4 {
399 INSERT INTO abc DEFAULT VALUES;
400 SELECT * FROM abc WHERE rowid NOT IN (1,2)
401 } {3 0.0 0.0 0.0 0.0}
402
403 # Show that there are no NOT NULL constraints
404 do_execsql_test 1.$tn.5 {
405 INSERT INTO abc VALUES(NULL, NULL, NULL, NULL, NULL);
406 SELECT * FROM abc WHERE rowid NOT IN (1,2,3)
407 } {4 0.0 0.0 0.0 0.0}
408
409# EVIDENCE-OF: R-06893-30579 In an RTREE virtual table, the first column
410# always has a type affinity of INTEGER and all other data columns have
411# a type affinity of REAL.
412 do_execsql_test 1.$tn.5 {
413 INSERT INTO abc VALUES('5', '5', '5', '5', '5');
414 SELECT * FROM abc WHERE rowid NOT IN (1,2,3,4)
415 } {5 5.0 5.0 5.0 5.0}
416 do_execsql_test 1.$tn.6 {
417 SELECT type FROM pragma_table_info('abc') ORDER BY cid
418 } {INT REAL REAL REAL REAL}
419
420 do_execsql_test 1.$tn.7 " CREATE VIRTUAL TABLE abc2 USING rtree_i32 $cols "
421
422# EVIDENCE-OF: R-06224-52418 In an RTREE_I32 virtual table, all columns
423# have type affinity of INTEGER.
424 do_execsql_test 1.$tn.8 {
425 INSERT INTO abc2 VALUES('6.0', '6.0', '6.0', '6.0', '6.0');
426 SELECT * FROM abc2
427 } {6 6 6 6 6}
428 do_execsql_test 1.$tn.9 {
429 SELECT type FROM pragma_table_info('abc2') ORDER BY cid
430 } {INT INT INT INT INT}
431
432
433 do_execsql_test 1.$tn.10 {
434 DROP TABLE abc;
435 DROP TABLE abc2;
436 }
437}
438
439#-------------------------------------------------------------------------
440#-------------------------------------------------------------------------
441# Section 3.2 of documentation.
442#-------------------------------------------------------------------------
443#-------------------------------------------------------------------------
444set testprefix rtreedoc-4
445reset_db
446
447# EVIDENCE-OF: R-36195-31555 The usual INSERT, UPDATE, and DELETE
448# commands work on an R*Tree index just like on regular tables.
449#
450# Create a regular table and an rtree table. Perform INSERT, UPDATE and
451# DELETE operations, then observe that the contents of the two tables
452# are identical.
453do_execsql_test 1.0 {
454 CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2);
455 CREATE TABLE t1(id INTEGER PRIMARY KEY, x1 REAL, x2 REAL);
456}
457foreach {tn sql} {
458 1 "INSERT INTO %TBL% VALUES(5, 11,12)"
459 2 "INSERT INTO %TBL% VALUES(11, -11,14.5)"
460 3 "UPDATE %TBL% SET x1=-99 WHERE id=11"
461 4 "DELETE FROM %TBL% WHERE x2=14.5"
462 5 "DELETE FROM %TBL%"
463} {
464 set sql1 [string map {%TBL% rt} $sql]
465 set sql2 [string map {%TBL% t1} $sql]
466 do_execsql_test 1.$tn.0 $sql1
467 do_execsql_test 1.$tn.1 $sql2
468
469 set data1 [execsql {SELECT * FROM rt ORDER BY 1}]
470 set data2 [execsql {SELECT * FROM t1 ORDER BY 1}]
471
472 set res [expr {$data1==$data2}]
473 do_test 1.$tn.2 {set res} 1
474}
475
476# EVIDENCE-OF: R-56987-45305
477do_execsql_test 2.0 {
478 CREATE VIRTUAL TABLE demo_index USING rtree(
479 id, -- Integer primary key
480 minX, maxX, -- Minimum and maximum X coordinate
481 minY, maxY -- Minimum and maximum Y coordinate
482 );
483
484 INSERT INTO demo_index VALUES
485 (28215, -80.781227, -80.604706, 35.208813, 35.297367),
486 (28216, -80.957283, -80.840599, 35.235920, 35.367825),
487 (28217, -80.960869, -80.869431, 35.133682, 35.208233),
488 (28226, -80.878983, -80.778275, 35.060287, 35.154446),
489 (28227, -80.745544, -80.555382, 35.130215, 35.236916),
490 (28244, -80.844208, -80.841988, 35.223728, 35.225471),
491 (28262, -80.809074, -80.682938, 35.276207, 35.377747),
492 (28269, -80.851471, -80.735718, 35.272560, 35.407925),
493 (28270, -80.794983, -80.728966, 35.059872, 35.161823),
494 (28273, -80.994766, -80.875259, 35.074734, 35.172836),
495 (28277, -80.876793, -80.767586, 35.001709, 35.101063),
496 (28278, -81.058029, -80.956375, 35.044701, 35.223812),
497 (28280, -80.844208, -80.841972, 35.225468, 35.227203),
498 (28282, -80.846382, -80.844193, 35.223972, 35.225655);
499}
500
501#-------------------------------------------------------------------------
502#-------------------------------------------------------------------------
503# Section 3.3 of documentation.
504#-------------------------------------------------------------------------
505#-------------------------------------------------------------------------
506set testprefix rtreedoc-5
507
508do_execsql_test 1.0 {
509 INSERT INTO demo_index
510 SELECT NULL, minX, maxX, minY+0.2, maxY+0.2 FROM demo_index;
511 INSERT INTO demo_index
512 SELECT NULL, minX+0.2, maxX+0.2, minY, maxY FROM demo_index;
513 INSERT INTO demo_index
514 SELECT NULL, minX, maxX, minY+0.4, maxY+0.4 FROM demo_index;
515 INSERT INTO demo_index
516 SELECT NULL, minX+0.4, maxX+0.4, minY, maxY FROM demo_index;
517 INSERT INTO demo_index
518 SELECT NULL, minX, maxX, minY+0.8, maxY+0.8 FROM demo_index;
519 INSERT INTO demo_index
520 SELECT NULL, minX+0.8, maxX+0.8, minY, maxY FROM demo_index;
521
522 SELECT count(*) FROM demo_index;
523} {896}
524
525proc do_vmstep_test {tn sql expr} {
526 execsql $sql
527 set step [db status vmstep]
528 do_test $tn.$step "expr {[subst $expr]}" 1
529}
530
531# EVIDENCE-OF: R-45880-07724 Any valid query will work against an R*Tree
532# index.
533do_execsql_test 1.1.0 {
534 CREATE TABLE demo_tbl AS SELECT * FROM demo_index;
535}
536foreach {tn sql} {
537 1 {SELECT * FROM %TBL% ORDER BY 1}
538 2 {SELECT max(minX) FROM %TBL% ORDER BY 1}
539 3 {SELECT max(minX) FROM %TBL% GROUP BY round(minY) ORDER BY 1}
540} {
541 set sql1 [string map {%TBL% demo_index} $sql]
542 set sql2 [string map {%TBL% demo_tbl} $sql]
543
544 do_execsql_test 1.1.$tn $sql1 [execsql $sql2]
545}
546
547# EVIDENCE-OF: R-60814-18273 The R*Tree implementation just makes some
548# kinds of queries especially efficient.
549#
550# The second query is more efficient than the first.
551do_vmstep_test 1.2.1 {SELECT * FROM demo_index WHERE +rowid=28269} {$step>2000}
552do_vmstep_test 1.2.2 {SELECT * FROM demo_index WHERE rowid=28269} {$step<100}
553
554# EVIDENCE-OF: R-37800-50174 Queries against the primary key are
555# efficient: SELECT * FROM demo_index WHERE id=28269;
556do_vmstep_test 2.2 { SELECT * FROM demo_index WHERE id=28269 } {$step < 100}
557
558# EVIDENCE-OF: R-35847-18866 The big reason for using an R*Tree is so
559# that you can efficiently do range queries against the coordinate
560# ranges.
561#
562# EVIDENCE-OF: R-49927-54202
563do_vmstep_test 2.3 {
564 SELECT id FROM demo_index
565 WHERE minX<=-80.77470 AND maxX>=-80.77470
566 AND minY<=35.37785 AND maxY>=35.37785;
567} {$step < 100}
568
569# EVIDENCE-OF: R-12823-37176 The query above will quickly locate all
570# zipcodes that contain the SQLite main office in their bounding box,
571# even if the R*Tree contains many entries.
572#
573do_execsql_test 2.4 {
574 SELECT id FROM demo_index
575 WHERE minX<=-80.77470 AND maxX>=-80.77470
576 AND minY<=35.37785 AND maxY>=35.37785;
577} {
578 28322 28269
579}
580
581# EVIDENCE-OF: R-07351-00257 For example, to find all zipcode bounding
582# boxes that overlap with the 28269 zipcode: SELECT A.id FROM demo_index
583# AS A, demo_index AS B WHERE A.maxX>=B.minX AND A.minX<=B.maxX
584# AND A.maxY>=B.minY AND A.minY<=B.maxY AND B.id=28269;
585#
586# Also check that it is efficient
587#
588# EVIDENCE-OF: R-39094-01937 This second query will find both 28269
589# entry (since every bounding box overlaps with itself) and also other
590# zipcode that is close enough to 28269 that their bounding boxes
591# overlap.
592#
593# 28269 is there in the result.
594#
595do_vmstep_test 2.5.1 {
596 SELECT A.id FROM demo_index AS A, demo_index AS B
597 WHERE A.maxX>=B.minX AND A.minX<=B.maxX
598 AND A.maxY>=B.minY AND A.minY<=B.maxY
599 AND B.id=28269
600} {$step < 100}
601do_execsql_test 2.5.2 {
602 SELECT A.id FROM demo_index AS A, demo_index AS B
603 WHERE A.maxX>=B.minX AND A.minX<=B.maxX
604 AND A.maxY>=B.minY AND A.minY<=B.maxY
605 AND B.id=28269 ORDER BY +A.id;
606} {
607 28215
608 28216
609 28262
610 28269
611 28286
612 28287
613 28291
614 28293
615 28298
616 28313
617 28320
618 28322
619 28336
620}
621
622# EVIDENCE-OF: R-02723-34107 Note that it is not necessary for all
623# coordinates in an R*Tree index to be constrained in order for the
624# index search to be efficient.
625#
626# EVIDENCE-OF: R-22490-27246 One might, for example, want to query all
627# objects that overlap with the 35th parallel: SELECT id FROM demo_index
628# WHERE maxY>=35.0 AND minY<=35.0;
629do_vmstep_test 2.6.1 {
630 SELECT id FROM demo_index
631 WHERE maxY>=35.0 AND minY<=35.0;
632} {$step < 100}
633do_execsql_test 2.6.2 {
634 SELECT id FROM demo_index
635 WHERE maxY>=35.0 AND minY<=35.0;
636} {}
637
638
639#-------------------------------------------------------------------------
640#-------------------------------------------------------------------------
641# Section 3.4 of documentation.
642#-------------------------------------------------------------------------
643#-------------------------------------------------------------------------
644set testprefix rtreedoc-6
645reset_db
646
647# EVIDENCE-OF: R-08327-00674 By default, coordinates are stored in an
648# R*Tree using 32-bit floating point values.
649#
650# EVIDENCE-OF: R-22000-53613 The default virtual table ("rtree") stores
651# coordinates as single-precision (4-byte) floating point numbers.
652#
653# Show this by showing that rounding is consistent with 32-bit float
654# rounding.
655do_execsql_test 1.0 {
656 CREATE VIRTUAL TABLE rt USING rtree(id, a,b);
657}
658do_execsql_test 1.1 {
659 INSERT INTO rt VALUES(14, -1000000000000, 1000000000000);
660 SELECT * FROM rt;
661} {14 -1000000061440.0 1000000061440.0}
662
663# EVIDENCE-OF: R-39127-51288 When a coordinate cannot be exactly
664# represented by a 32-bit floating point number, the lower-bound
665# coordinates are rounded down and the upper-bound coordinates are
666# rounded up.
667foreach {tn val} {
668 1 100000000000
669 2 200000000000
670 3 300000000000
671 4 400000000000
672
673 5 -100000000000
674 6 -200000000000
675 7 -300000000000
676 8 -400000000000
677} {
678 set val [expr $val]
679 do_execsql_test 2.$tn.0 {DELETE FROM rt}
680 do_execsql_test 2.$tn.1 {INSERT INTO rt VALUES(23, $val, $val)}
681 do_execsql_test 2.$tn.2 {
682 SELECT $val>=a, $val<=b, a!=b FROM rt
683 } {1 1 1}
684}
685
686do_execsql_test 3.0 {
687 DROP TABLE rt;
688 CREATE VIRTUAL TABLE rt USING rtree(id, x1,x2, y1,y2);
689}
690
691# EVIDENCE-OF: R-45870-62834 Thus, bounding boxes might be slightly
692# larger than specified, but will never be any smaller.
693foreach {tn x1 x2 y1 y2} {
694 1 100000000000 200000000000 300000000000 400000000000
695} {
696 set val [expr $val]
697 do_execsql_test 3.$tn.0 {DELETE FROM rt}
698 do_execsql_test 3.$tn.1 {INSERT INTO rt VALUES(23, $x1, $x2, $y1, $y2)}
699 do_execsql_test 3.$tn.2 {
700 SELECT (x2-x1)*(y2-y1) >= ($x2-$x1)*($y2-$y1) FROM rt
701 } {1}
702}
703
704#-------------------------------------------------------------------------
705#-------------------------------------------------------------------------
706# Section 3.5 of documentation.
707#-------------------------------------------------------------------------
708#-------------------------------------------------------------------------
709set testprefix rtreedoc-7
710reset_db
711
712# EVIDENCE-OF: R-55979-39402 It is the nature of the Guttman R-Tree
713# algorithm that any write might radically restructure the tree, and in
714# the process change the scan order of the nodes.
715#
716# In the test below, the INSERT marked "THIS INSERT!!" does not affect
717# the results of queries with an ORDER BY, but does affect the results
718# of one without an ORDER BY. Therefore the INSERT changed the scan
719# order.
720do_execsql_test 1.0 {
721 CREATE VIRTUAL TABLE rt USING rtree(id, minX, maxX);
722 WITH s(i) AS (
723 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<51
724 )
725 INSERT INTO rt SELECT NULL, i%10, (i%10)+5 FROM s
726}
727do_execsql_test 1.1 { SELECT count(*) FROM rt_node } 1
728do_test 1.2 {
729 set res1 [db eval {SELECT * FROM rt WHERE maxX < 30}]
730 set res1o [db eval {SELECT * FROM rt WHERE maxX < 30 ORDER BY +id}]
731
732 db eval { INSERT INTO rt VALUES(NULL, 50, 50) } ;# THIS INSERT!!
733
734 set res2 [db eval {SELECT * FROM rt WHERE maxX < 30}]
735 set res2o [db eval {SELECT * FROM rt WHERE maxX < 30 ORDER BY +id}]
736 list [expr {$res1==$res2}] [expr {$res1o==$res2o}]
737} {0 1}
738
739do_execsql_test 1.3 { SELECT count(*) FROM rt_node } 3
740
741# EVIDENCE-OF: R-00683-48865 For this reason, it is not generally
742# possible to modify the R-Tree in the middle of a query of the R-Tree.
743# Attempts to do so will fail with a SQLITE_LOCKED "database table is
744# locked" error.
745#
746# SQLITE_LOCKED==6
747#
748do_test 1.4 {
749 set nCnt 3
750 db eval { SELECT * FROM rt WHERE minX>0 AND maxX<12 } {
751 incr nCnt -1
752 if {$nCnt==0} {
753 set rc [catch {db eval {
754 INSERT INTO rt VALUES(NULL, 51, 51);
755 }} msg]
756 set errorcode [db errorcode]
757 break
758 }
759 }
760
761 list $errorcode $rc $msg
762} {6 1 {database table is locked}}
763
764# EVIDENCE-OF: R-19740-29710 So, for example, suppose an application
765# runs one query against an R-Tree like this: SELECT id FROM demo_index
766# WHERE maxY>=35.0 AND minY<=35.0; Then for each "id" value
767# returned, suppose the application creates an UPDATE statement like the
768# following and binds the "id" value returned against the "?1"
769# parameter: UPDATE demo_index SET maxY=maxY+0.5 WHERE id=?1;
770#
771# EVIDENCE-OF: R-52919-32711 Then the UPDATE might fail with an
772# SQLITE_LOCKED error.
773do_execsql_test 2.0 {
774 CREATE VIRTUAL TABLE demo_index USING rtree(
775 id, -- Integer primary key
776 minX, maxX, -- Minimum and maximum X coordinate
777 minY, maxY -- Minimum and maximum Y coordinate
778 );
779 INSERT INTO demo_index VALUES
780 (28215, -80.781227, -80.604706, 35.208813, 35.297367),
781 (28216, -80.957283, -80.840599, 35.235920, 35.367825),
782 (28217, -80.960869, -80.869431, 35.133682, 35.208233),
783 (28226, -80.878983, -80.778275, 35.060287, 35.154446);
784}
785do_test 2.1 {
786 db eval { SELECT id FROM demo_index WHERE maxY>=35.0 AND minY<=35.0 } {
787 set rc [catch {
788 db eval { UPDATE demo_index SET maxY=maxY+0.5 WHERE id=$id }
789 } msg]
790 set errorcode [db errorcode]
791 break
792 }
793 list $errorcode $rc $msg
794} {6 1 {database table is locked}}
795
796# EVIDENCE-OF: R-32604-49843 Ordinary tables in SQLite are able to read
797# and write at the same time.
798#
799do_execsql_test 3.0 {
800 CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c);
801 INSERT INTO x1 VALUES(1, 1, 1);
802 INSERT INTO x1 VALUES(2, 2, 2);
803 INSERT INTO x1 VALUES(3, 3, 3);
804 INSERT INTO x1 VALUES(4, 4, 4);
805}
806do_test 3.1 {
807 unset -nocomplain res
808 set res [list]
809 db eval { SELECT * FROM x1 } {
810 lappend res $a $b $c
811 switch -- $a {
812 1 {
813 db eval { INSERT INTO x1 VALUES(5, 5, 5) }
814 }
815 2 {
816 db eval { UPDATE x1 SET c=20 WHERE a=2 }
817 }
818 3 {
819 db eval { DELETE FROM x1 WHERE c IN (3,4) }
820 }
821 }
822 }
823 set res
824} {1 1 1 2 2 2 3 3 3 5 5 5}
825do_execsql_test 3.2 {
826 SELECT * FROM x1
827} {1 1 1 2 2 20 5 5 5}
828
829# EVIDENCE-OF: R-06177-00576 And R-Tree can appear to read and write at
830# the same time in some circumstances, if it can figure out how to
831# reliably run the query to completion before starting the update.
832#
833# In 8.2, it can, it 8.1, it cannot.
834do_test 8.1 {
835 db eval { SELECT * FROM rt } {
836 set rc [catch { db eval { INSERT INTO rt VALUES(53,53,53) } } msg]
837 break;
838 }
839 list $rc $msg
840} {1 {database table is locked}}
841do_test 8.2 {
842 db eval { SELECT * FROM rt ORDER BY +id } {
843 set rc [catch { db eval { INSERT INTO rt VALUES(53,53,53) } } msg]
844 break
845 }
846 list $rc $msg
847} {0 {}}
848
849#-------------------------------------------------------------------------
850#-------------------------------------------------------------------------
851# Section 4 of documentation.
852#-------------------------------------------------------------------------
853#-------------------------------------------------------------------------
854set testprefix rtreedoc-8
855reset_db
856
857# EVIDENCE-OF: R-21062-30088 For the example above, one might create an
858# auxiliary table as follows: CREATE TABLE demo_data( id INTEGER PRIMARY
859# KEY, -- primary key objname TEXT, -- name of the object objtype TEXT,
860# -- object type boundary BLOB -- detailed boundary of object );
861#
862# One might.
863#
864do_execsql_test 1.0 {
865 CREATE TABLE demo_data(
866 id INTEGER PRIMARY KEY, -- primary key
867 objname TEXT, -- name of the object
868 objtype TEXT, -- object type
869 boundary BLOB -- detailed boundary of object
870 );
871}
872
873do_execsql_test 1.1 {
874 CREATE VIRTUAL TABLE demo_index USING rtree(
875 id, -- Integer primary key
876 minX, maxX, -- Minimum and maximum X coordinate
877 minY, maxY -- Minimum and maximum Y coordinate
878 );
879
880 INSERT INTO demo_index VALUES
881 (28215, -80.781227, -80.604706, 35.208813, 35.297367),
882 (28216, -80.957283, -80.840599, 35.235920, 35.367825),
883 (28217, -80.960869, -80.869431, 35.133682, 35.208233),
884 (28226, -80.878983, -80.778275, 35.060287, 35.154446),
885 (28227, -80.745544, -80.555382, 35.130215, 35.236916),
886 (28244, -80.844208, -80.841988, 35.223728, 35.225471),
887 (28262, -80.809074, -80.682938, 35.276207, 35.377747),
888 (28269, -80.851471, -80.735718, 35.272560, 35.407925),
889 (28270, -80.794983, -80.728966, 35.059872, 35.161823),
890 (28273, -80.994766, -80.875259, 35.074734, 35.172836),
891 (28277, -80.876793, -80.767586, 35.001709, 35.101063),
892 (28278, -81.058029, -80.956375, 35.044701, 35.223812),
893 (28280, -80.844208, -80.841972, 35.225468, 35.227203),
894 (28282, -80.846382, -80.844193, 35.223972, 35.225655);
895
896 INSERT INTO demo_index
897 SELECT NULL, minX, maxX, minY+0.2, maxY+0.2 FROM demo_index;
898 INSERT INTO demo_index
899 SELECT NULL, minX+0.2, maxX+0.2, minY, maxY FROM demo_index;
900 INSERT INTO demo_index
901 SELECT NULL, minX, maxX, minY+0.4, maxY+0.4 FROM demo_index;
902 INSERT INTO demo_index
903 SELECT NULL, minX+0.4, maxX+0.4, minY, maxY FROM demo_index;
904 INSERT INTO demo_index
905 SELECT NULL, minX, maxX, minY+0.8, maxY+0.8 FROM demo_index;
906 INSERT INTO demo_index
907 SELECT NULL, minX+0.8, maxX+0.8, minY, maxY FROM demo_index;
908
909 INSERT INTO demo_data(id) SELECT id FROM demo_index;
910
911 SELECT count(*) FROM demo_index;
912} {896}
913
914set ::contained_in 0
915proc contained_in {args} {incr ::contained_in ; return 0}
916db func contained_in contained_in
917
918# EVIDENCE-OF: R-32671-43888 Then an efficient way to find the specific
919# ZIP code for the main SQLite office would be to run a query like this:
920# SELECT objname FROM demo_data, demo_index WHERE
921# demo_data.id=demo_index.id AND contained_in(demo_data.boundary,
922# 35.37785, -80.77470) AND minX<=-80.77470 AND maxX>=-80.77470 AND
923# minY<=35.37785 AND maxY>=35.37785;
924do_vmstep_test 1.2 {
925 SELECT objname FROM demo_data, demo_index
926 WHERE demo_data.id=demo_index.id
927 AND contained_in(demo_data.boundary, 35.37785, -80.77470)
928 AND minX<=-80.77470 AND maxX>=-80.77470
929 AND minY<=35.37785 AND maxY>=35.37785;
930} {$step<100}
931set ::contained_in1 $::contained_in
932
933# EVIDENCE-OF: R-32761-23915 One would get the same answer without the
934# use of the R*Tree index using the following simpler query: SELECT
935# objname FROM demo_data WHERE contained_in(demo_data.boundary,
936# 35.37785, -80.77470);
937set ::contained_in 0
938do_vmstep_test 1.3 {
939 SELECT objname FROM demo_data
940 WHERE contained_in(demo_data.boundary, 35.37785, -80.77470);
941} {$step>3200}
942
943# EVIDENCE-OF: R-40261-32799 The problem with this latter query is that
944# it must apply the contained_in() function to all entries in the
945# demo_data table.
946#
947# 896 of them, IIRC.
948do_test 1.4 {
949 set ::contained_in
950} 896
951
952# EVIDENCE-OF: R-24212-52761 The use of the R*Tree in the penultimate
953# query reduces the number of calls to contained_in() function to a
954# small subset of the entire table.
955#
956# 2 is a small subset of 896.
957#
958# EVIDENCE-OF: R-39057-63901 The R*Tree index did not find the exact
959# answer itself, it merely limited the search space.
960#
961# contained_in() filtered out those 2 rows.
962do_test 1.5 {
963 set ::contained_in1
964} {2}
965
966
967#-------------------------------------------------------------------------
968#-------------------------------------------------------------------------
969# Section 4.1 of documentation.
970#-------------------------------------------------------------------------
971#-------------------------------------------------------------------------
972set testprefix rtreedoc-9
973reset_db
974
975# EVIDENCE-OF: R-46566-43213 Beginning with SQLite version 3.24.0
976# (2018-06-04), r-tree tables can have auxiliary columns that store
977# arbitrary data. Auxiliary columns can be used in place of secondary
978# tables such as "demo_data".
979#
980# EVIDENCE-OF: R-41287-48160 Auxiliary columns are marked with a "+"
981# symbol before the column name.
982#
983# This interface cannot conveniently be used to prove anything about
984# versions of SQLite prior to 3.24.0.
985#
986do_execsql_test 1.0 {
987 CREATE VIRTUAL TABLE rta USING rtree(
988 id, u1,u2, v1,v2, +aux
989 );
990
991 INSERT INTO rta(aux) VALUES(NULL);
992 INSERT INTO rta(aux) VALUES(45);
993 INSERT INTO rta(aux) VALUES(22.3);
994 INSERT INTO rta(aux) VALUES('hello');
995 INSERT INTO rta(aux) VALUES(X'ABCD');
996
997 SELECT typeof(aux), quote(aux) FROM rta;
998} {
999 null NULL
1000 integer 45
1001 real 22.3
1002 text 'hello'
1003 blob X'ABCD'
1004}
1005
1006# EVIDENCE-OF: R-30514-26093 Auxiliary columns must come after all of
1007# the coordinate boundary columns.
1008foreach {tn cols} {
1009 1 "id x1,x2, +extra, y1,y2"
1010 2 "extra, +id x1,x2, y1,y2"
1011 3 "id, x1,+x2, extra, y1,y2"
1012} {
1013 do_catchsql_test 2.$tn "
1014 CREATE VIRTUAL TABLE rrr USING rtree($cols)
1015 " {1 {Auxiliary rtree columns must be last}}
1016}
1017do_catchsql_test 3.0 {
1018 CREATE VIRTUAL TABLE rrr USING rtree(+id, extra, x1, x2);
1019} {1 {near "+": syntax error}}
1020
1021# EVIDENCE-OF: R-01280-03635 An RTREE table can have no more than 100
1022# columns total. In other words, the count of columns including the
1023# integer primary key column, the coordinate boundary columns, and all
1024# auxiliary columns must be 100 or less.
1025do_catchsql_test 3.1 {
1026 CREATE VIRTUAL TABLE r1 USING rtree(intid, u1,u2,
1027 +c00, +c01, +c02, +c03, +c04, +c05, +c06, +c07, +c08, +c09,
1028 +c10, +c11, +c12, +c13, +c14, +c15, +c16, +c17, +c18, +c19,
1029 +c20, +c21, +c22, +c23, +c24, +c25, +c26, +c27, +c28, +c29,
1030 +c30, +c31, +c32, +c33, +c34, +c35, +c36, +c37, +c38, +c39,
1031 +c40, +c41, +c42, +c43, +c44, +c45, +c46, +c47, +c48, +c49,
1032 +c50, +c51, +c52, +c53, +c54, +c55, +c56, +c57, +c58, +c59,
1033 +c60, +c61, +c62, +c63, +c64, +c65, +c66, +c67, +c68, +c69,
1034 +c70, +c71, +c72, +c73, +c74, +c75, +c76, +c77, +c78, +c79,
1035 +c80, +c81, +c82, +c83, +c84, +c85, +c86, +c87, +c88, +c89,
1036 +c90, +c91, +c92, +c93, +c94, +c95, +c96
1037 );
1038} {0 {}}
1039do_catchsql_test 3.2 {
1040 DROP TABLE r1;
1041 CREATE VIRTUAL TABLE r1 USING rtree(intid, u1,u2,
1042 +c00, +c01, +c02, +c03, +c04, +c05, +c06, +c07, +c08, +c09,
1043 +c10, +c11, +c12, +c13, +c14, +c15, +c16, +c17, +c18, +c19,
1044 +c20, +c21, +c22, +c23, +c24, +c25, +c26, +c27, +c28, +c29,
1045 +c30, +c31, +c32, +c33, +c34, +c35, +c36, +c37, +c38, +c39,
1046 +c40, +c41, +c42, +c43, +c44, +c45, +c46, +c47, +c48, +c49,
1047 +c50, +c51, +c52, +c53, +c54, +c55, +c56, +c57, +c58, +c59,
1048 +c60, +c61, +c62, +c63, +c64, +c65, +c66, +c67, +c68, +c69,
1049 +c70, +c71, +c72, +c73, +c74, +c75, +c76, +c77, +c78, +c79,
1050 +c80, +c81, +c82, +c83, +c84, +c85, +c86, +c87, +c88, +c89,
1051 +c90, +c91, +c92, +c93, +c94, +c95, +c96, +c97
1052 );
1053} {1 {Too many columns for an rtree table}}
1054do_catchsql_test 3.3 {
1055 CREATE VIRTUAL TABLE r1 USING rtree(intid, u1,u2, v1,v2,
1056 +c00, +c01, +c02, +c03, +c04, +c05, +c06, +c07, +c08, +c09,
1057 +c10, +c11, +c12, +c13, +c14, +c15, +c16, +c17, +c18, +c19,
1058 +c20, +c21, +c22, +c23, +c24, +c25, +c26, +c27, +c28, +c29,
1059 +c30, +c31, +c32, +c33, +c34, +c35, +c36, +c37, +c38, +c39,
1060 +c40, +c41, +c42, +c43, +c44, +c45, +c46, +c47, +c48, +c49,
1061 +c50, +c51, +c52, +c53, +c54, +c55, +c56, +c57, +c58, +c59,
1062 +c60, +c61, +c62, +c63, +c64, +c65, +c66, +c67, +c68, +c69,
1063 +c70, +c71, +c72, +c73, +c74, +c75, +c76, +c77, +c78, +c79,
1064 +c80, +c81, +c82, +c83, +c84, +c85, +c86, +c87, +c88, +c89,
1065 +c90, +c91, +c92, +c93, +c94,
1066 );
1067} {0 {}}
1068do_catchsql_test 3.4 {
1069 DROP TABLE r1;
1070 CREATE VIRTUAL TABLE r1 USING rtree(intid, u1,u2, v1,v2,
1071 +c00, +c01, +c02, +c03, +c04, +c05, +c06, +c07, +c08, +c09,
1072 +c10, +c11, +c12, +c13, +c14, +c15, +c16, +c17, +c18, +c19,
1073 +c20, +c21, +c22, +c23, +c24, +c25, +c26, +c27, +c28, +c29,
1074 +c30, +c31, +c32, +c33, +c34, +c35, +c36, +c37, +c38, +c39,
1075 +c40, +c41, +c42, +c43, +c44, +c45, +c46, +c47, +c48, +c49,
1076 +c50, +c51, +c52, +c53, +c54, +c55, +c56, +c57, +c58, +c59,
1077 +c60, +c61, +c62, +c63, +c64, +c65, +c66, +c67, +c68, +c69,
1078 +c70, +c71, +c72, +c73, +c74, +c75, +c76, +c77, +c78, +c79,
1079 +c80, +c81, +c82, +c83, +c84, +c85, +c86, +c87, +c88, +c89,
1080 +c90, +c91, +c92, +c93, +c94, +c95,
1081 );
1082} {1 {Too many columns for an rtree table}}
1083
1084# EVIDENCE-OF: R-05552-15084
1085do_execsql_test 4.0 {
1086 CREATE VIRTUAL TABLE demo_index2 USING rtree(
1087 id, -- Integer primary key
1088 minX, maxX, -- Minimum and maximum X coordinate
1089 minY, maxY, -- Minimum and maximum Y coordinate
1090 +objname TEXT, -- name of the object
1091 +objtype TEXT, -- object type
1092 +boundary BLOB -- detailed boundary of object
1093 );
1094}
1095do_execsql_test 4.1 {
1096 CREATE VIRTUAL TABLE demo_index USING rtree(
1097 id, -- Integer primary key
1098 minX, maxX, -- Minimum and maximum X coordinate
1099 minY, maxY -- Minimum and maximum Y coordinate
1100 );
1101 CREATE TABLE demo_data(
1102 id INTEGER PRIMARY KEY, -- primary key
1103 objname TEXT, -- name of the object
1104 objtype TEXT, -- object type
1105 boundary BLOB -- detailed boundary of object
1106 );
1107
1108 INSERT INTO demo_index2(id) VALUES(1);
1109 INSERT INTO demo_index(id) VALUES(1);
1110 INSERT INTO demo_data(id) VALUES(1);
1111}
1112do_test 4.2 {
1113 catch { array unset R }
1114 db eval {SELECT * FROM demo_index2} R { set r1 [array names R] }
1115 catch { array unset R }
1116 db eval {SELECT * FROM demo_index NATURAL JOIN demo_data } R {
1117 set r2 [array names R]
1118 }
1119 expr {$r1==$r2}
1120} {1}
1121
1122# EVIDENCE-OF: R-26099-32169 SELECT objname FROM demo_index2 WHERE
1123# contained_in(boundary, 35.37785, -80.77470) AND minX<=-80.77470 AND
1124# maxX>=-80.77470 AND minY<=35.37785 AND maxY>=35.37785;
1125do_execsql_test 4.3.1 {
1126 DELETE FROM demo_index2;
1127 INSERT INTO demo_index2(id,minX,maxX,minY,maxY) VALUES
1128 (28215, -80.781227, -80.604706, 35.208813, 35.297367),
1129 (28216, -80.957283, -80.840599, 35.235920, 35.367825),
1130 (28217, -80.960869, -80.869431, 35.133682, 35.208233),
1131 (28226, -80.878983, -80.778275, 35.060287, 35.154446),
1132 (28227, -80.745544, -80.555382, 35.130215, 35.236916),
1133 (28244, -80.844208, -80.841988, 35.223728, 35.225471),
1134 (28262, -80.809074, -80.682938, 35.276207, 35.377747),
1135 (28269, -80.851471, -80.735718, 35.272560, 35.407925),
1136 (28270, -80.794983, -80.728966, 35.059872, 35.161823),
1137 (28273, -80.994766, -80.875259, 35.074734, 35.172836),
1138 (28277, -80.876793, -80.767586, 35.001709, 35.101063),
1139 (28278, -81.058029, -80.956375, 35.044701, 35.223812),
1140 (28280, -80.844208, -80.841972, 35.225468, 35.227203),
1141 (28282, -80.846382, -80.844193, 35.223972, 35.225655);
1142}
1143set ::contained_in 0
1144proc contained_in {args} {
1145 incr ::contained_in
1146 return 0
1147}
1148db func contained_in contained_in
1149do_execsql_test 4.3.2 {
1150 SELECT objname FROM demo_index2
1151 WHERE contained_in(boundary, 35.37785, -80.77470)
1152 AND minX<=-80.77470 AND maxX>=-80.77470
1153 AND minY<=35.37785 AND maxY>=35.37785;
1154}
1155do_test 4.3.3 {
1156 # Function invoked only once because r-tree filtering happened first.
1157 set ::contained_in
1158} 1
1159set ::contained_in 0
1160do_execsql_test 4.3.4 {
1161 SELECT objname FROM demo_index2
1162 WHERE contained_in(boundary, 35.37785, -80.77470)
1163}
1164do_test 4.3.3 {
1165 # Function invoked 14 times because no r-tree filtering. Inefficient.
1166 set ::contained_in
1167} 14
1168
1169#-------------------------------------------------------------------------
1170#-------------------------------------------------------------------------
1171# Section 4.1.1 of documentation.
1172#-------------------------------------------------------------------------
1173#-------------------------------------------------------------------------
1174set testprefix rtreedoc-9
1175reset_db
1176
1177# EVIDENCE-OF: R-24021-02490 For auxiliary columns, only the name of the
1178# column matters. The type affinity is ignored.
1179#
1180# EVIDENCE-OF: R-39906-44154 Constraints such as NOT NULL, UNIQUE,
1181# REFERENCES, or CHECK are also ignored.
1182do_execsql_test 1.0 { PRAGMA foreign_keys = on }
1183foreach {tn auxcol nm} {
1184 1 "+extra INTEGER" extra
1185 2 "+extra TEXT" extra
1186 3 "+extra BLOB" extra
1187 4 "+extra REAL" extra
1188
1189 5 "+col NOT NULL" col
1190 6 "+col CHECK (col IS NOT NULL)" col
1191 7 "+col REFERENCES tbl(x)" col
1192} {
1193 do_execsql_test 1.$tn.1 "
1194 CREATE VIRTUAL TABLE rt USING rtree_i32(k, a,b, $auxcol)
1195 "
1196
1197 # Check that the aux column has no affinity. Or NOT NULL constraint.
1198 # And that the aux column is the child key of an FK constraint.
1199 #
1200 do_execsql_test 1.$tn.2 "
1201 INSERT INTO rt($nm) VALUES(NULL), (45), (-123.2), ('456'), (X'ABCD');
1202 SELECT typeof($nm), quote($nm) FROM rt;
1203 " {
1204 null NULL
1205 integer 45
1206 real -123.2
1207 text '456'
1208 blob X'ABCD'
1209 }
1210
1211 # Check that there is no UNIQUE constraint either.
1212 #
1213 do_execsql_test 1.$tn.3 "
1214 INSERT INTO rt($nm) VALUES('xyz'), ('xyz'), ('xyz');
1215 "
1216
1217 do_execsql_test 1.$tn.2 {
1218 DROP TABLE rt
1219 }
1220}
1221
1222#-------------------------------------------------------------------------
1223#-------------------------------------------------------------------------
1224# Section 5 of documentation.
1225#-------------------------------------------------------------------------
1226#-------------------------------------------------------------------------
1227set testprefix rtreedoc-10
1228
1229# EVIDENCE-OF: R-21011-43790 If integer coordinates are desired, declare
1230# the table using "rtree_i32" instead: CREATE VIRTUAL TABLE intrtree
1231# USING rtree_i32(id,x0,x1,y0,y1,z0,z1);
1232do_execsql_test 1.0 {
1233 CREATE VIRTUAL TABLE intrtree USING rtree_i32(id,x0,x1,y0,y1,z0,z1);
1234 INSERT INTO intrtree DEFAULT VALUES;
1235 SELECT typeof(x0) FROM intrtree;
1236} {integer}
1237
1238# EVIDENCE-OF: R-09193-49806 An rtree_i32 stores coordinates as 32-bit
1239# signed integers.
1240#
1241# Show that coordinates are cast in a way consistent with casting to
1242# a signed 32-bit integer.
1243do_execsql_test 1.1 {
1244 DELETE FROM intrtree;
1245 INSERT INTO intrtree VALUES(333,
1246 1<<44, (1<<44)+1,
1247 10000000000, 10000000001,
1248 -10000000001, -10000000000
1249 );
1250 SELECT * FROM intrtree;
1251} {
1252 333 2147483647 2147483647 2147483647 2147483647 -2147483648 -2147483648
1253}
1254
1255#-------------------------------------------------------------------------
1256#-------------------------------------------------------------------------
1257# Section 7.1 of documentation.
1258#-------------------------------------------------------------------------
1259#-------------------------------------------------------------------------
1260set testprefix rtreedoc-11
1261reset_db
1262
1263# This command assumes that the argument is a node blob for a 2 dimensional
1264# i32 r-tree table. It decodes and returns a list of cells from the node
1265# as a list. Each cell is itself a list of the following form:
1266#
1267# {$rowid $minX $maxX $minY $maxY}
1268#
1269# For internal (non-leaf) nodes, the rowid is replaced by the child node
1270# number.
1271#
1272proc rnode {aData} {
1273 set nDim 2
1274
1275 set nData [string length $aData]
1276 set nBytePerCell [expr (8 + 2*$nDim*4)]
1277 binary scan [string range $aData 2 3] S nCell
1278
1279 set res [list]
1280 for {set i 0} {$i < $nCell} {incr i} {
1281 set iOff [expr $i*$nBytePerCell+4]
1282 set cell [string range $aData $iOff [expr $iOff+$nBytePerCell-1]]
1283 binary scan $cell WIIII rowid x1 x2 y1 y2
1284 lappend res [list $rowid $x1 $x2 $y1 $y2]
1285 }
1286
1287 return $res
1288}
1289
1290# aData must be a node blob. This command returns true if the node contains
1291# rowid $rowid, or false otherwise.
1292#
1293proc rnode_contains {aData rowid} {
1294 set L [rnode $aData]
1295 foreach cell $L {
1296 set r [lindex $cell 0]
1297 if {$r==$rowid} { return 1 }
1298 }
1299 return 0
1300}
1301
1302proc rnode_replace_cell {aData iCell cell} {
1303 set aCell [binary format WIIII {*}$cell]
1304 set nDim 2
1305 set nBytePerCell [expr (8 + 2*$nDim*4)]
1306 set iOff [expr $iCell*$nBytePerCell+4]
1307
1308 set aNew [binary format a*a*a* \
1309 [string range $aData 0 $iOff-1] \
1310 $aCell \
1311 [string range $aData $iOff+$nBytePerCell end] \
1312 ]
1313 return $aNew
1314}
1315
1316db function rnode rnode
1317db function rnode_contains rnode_contains
1318db function rnode_replace_cell rnode_replace_cell
1319
1320foreach {tn nm} {
1321 1 x1
1322 2 asdfghjkl
1323 3 hello_world
1324} {
1325 do_execsql_test 1.$tn.1 "
1326 CREATE VIRTUAL TABLE $nm USING rtree(a,b,c,d,e);
1327 "
1328
1329 # EVIDENCE-OF: R-33789-46762 The content of an R*Tree index is actually
1330 # stored in three ordinary SQLite tables with names derived from the
1331 # name of the R*Tree.
1332 #
1333 # EVIDENCE-OF: R-39849-06566 This is their schema: CREATE TABLE
1334 # %_node(nodeno INTEGER PRIMARY KEY, data) CREATE TABLE %_parent(nodeno
1335 # INTEGER PRIMARY KEY, parentnode) CREATE TABLE %_rowid(rowid INTEGER
1336 # PRIMARY KEY, nodeno)
1337 #
1338 # EVIDENCE-OF: R-07489-10051 The "%" in the name of each shadow table is
1339 # replaced by the name of the R*Tree virtual table. So, if the name of
1340 # the R*Tree table is "xyz" then the three shadow tables would be
1341 # "xyz_node", "xyz_parent", and "xyz_rowid".
1342 do_execsql_test 1.$tn.2 {
1343 SELECT sql FROM sqlite_schema WHERE name!=$nm ORDER BY 1
1344 } [string map [list % $nm] "
1345 {CREATE TABLE \"%_node\"(nodeno INTEGER PRIMARY KEY,data)}
1346 {CREATE TABLE \"%_parent\"(nodeno INTEGER PRIMARY KEY,parentnode)}
1347 {CREATE TABLE \"%_rowid\"(rowid INTEGER PRIMARY KEY,nodeno)}
1348 "]
1349
1350 do_execsql_test 1.$tn "DROP TABLE $nm"
1351}
1352
1353
1354# EVIDENCE-OF: R-51070-59303 There is one entry in the %_node table for
1355# each R*Tree node.
1356#
1357# The following creates a 6 node r-tree structure.
1358#
1359do_execsql_test 2.0 {
1360 CREATE VIRTUAL TABLE r1 USING rtree_i32(i, x1,x2, y1,y2);
1361 WITH t(i) AS (
1362 VALUES(1) UNION SELECT i+1 FROM t WHERE i<110
1363 )
1364 INSERT INTO r1 SELECT i, (i%10), (i%10)+2, (i%6), (i%7)+6 FROM t;
1365}
1366do_execsql_test 2.1 {
1367 SELECT count(*) FROM r1_node;
1368} 6
1369
1370# EVIDENCE-OF: R-27261-09153 All nodes other than the root have an entry
1371# in the %_parent shadow table that identifies the parent node.
1372#
1373# In this case nodes 2-6 are the children of node 1.
1374#
1375do_execsql_test 2.3 {
1376 SELECT nodeno, parentnode FROM r1_parent
1377} {2 1 3 1 4 1 5 1 6 1}
1378
1379# EVIDENCE-OF: R-02358-35037 The %_rowid shadow table maps entry rowids
1380# to the node that contains that entry.
1381#
1382do_execsql_test 2.4 {
1383 SELECT 'failed' FROM r1_rowid WHERE 0==rnode_contains(
1384 (SELECT data FROM r1_node WHERE nodeno=r1_rowid.nodeno), rowid
1385 )
1386}
1387do_test 2.5 {
1388 db eval { SELECT nodeno, data FROM r1_node WHERE nodeno!=1 } {
1389 set L [rnode $data]
1390 foreach cell $L {
1391 set rowid [lindex $cell 0]
1392 set rowid_nodeno 0
1393 db eval {SELECT nodeno AS rowid_nodeno FROM r1_rowid WHERE rowid=$rowid} {
1394 break
1395 }
1396 if {$rowid_nodeno!=$nodeno} { error "data mismatch!" }
1397 }
1398 }
1399} {}
1400
1401# EVIDENCE-OF: R-65201-22208 Extra columns appended to the %_rowid table
1402# hold the content of auxiliary columns.
1403#
1404# EVIDENCE-OF: R-44161-28345 The names of these extra %_rowid columns
1405# are probably not the same as the actual auxiliary column names.
1406#
1407# In this case, the auxiliary columns are named "e1" and "e2". The
1408# extra %_rowid columns are named "a0" and "a1".
1409#
1410do_execsql_test 3.0 {
1411 CREATE VIRTUAL TABLE rtaux USING rtree(id, x1,x2, y1,y2, +e1, +e2);
1412 SELECT sql FROM sqlite_schema WHERE name='rtaux_rowid';
1413} {
1414 {CREATE TABLE "rtaux_rowid"(rowid INTEGER PRIMARY KEY,nodeno,a0,a1)}
1415}
1416do_execsql_test 3.1 {
1417 INSERT INTO rtaux(e1, e2) VALUES('hello', 'world'), (123, 456);
1418}
1419do_execsql_test 3.2 {
1420 SELECT a0, a1 FROM rtaux_rowid;
1421} {
1422 hello world 123 456
1423}
1424
1425#-------------------------------------------------------------------------
1426#-------------------------------------------------------------------------
1427# Section 7.2 of documentation.
1428#-------------------------------------------------------------------------
1429#-------------------------------------------------------------------------
1430set testprefix rtreedoc-12
1431reset_db
1432forcedelete test.db2
1433
1434db function rnode rnode
1435db function rnode_contains rnode_contains
1436db function rnode_replace_cell rnode_replace_cell
1437
1438# EVIDENCE-OF: R-13571-45795 The scalar SQL function rtreecheck(R) or
1439# rtreecheck(S,R) runs an integrity check on the rtree table named R
1440# contained within database S.
1441#
1442# EVIDENCE-OF: R-36011-59963 The function returns a human-language
1443# description of any problems found, or the string 'ok' if everything is
1444# ok.
1445#
1446do_execsql_test 1.0 {
1447 CREATE VIRTUAL TABLE rt1 USING rtree(id, a, b);
1448 WITH s(i) AS (
1449 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<200
1450 )
1451 INSERT INTO rt1 SELECT i, i, i FROM s;
1452
1453 ATTACH 'test.db2' AS 'aux';
1454 CREATE VIRTUAL TABLE aux.rt1 USING rtree(id, a, b);
1455 INSERT INTO aux.rt1 SELECT * FROM rt1;
1456}
1457
1458do_execsql_test 1.1.1 { SELECT rtreecheck('rt1'); } {ok}
1459do_execsql_test 1.1.2 { SELECT rtreecheck('main', 'rt1'); } {ok}
1460do_execsql_test 1.1.3 { SELECT rtreecheck('aux', 'rt1'); } {ok}
1461do_catchsql_test 1.1.4 {
1462 SELECT rtreecheck('nosuchdb', 'rt1');
1463} {1 {SQL logic error}}
1464
1465# Corrupt the table in database 'main':
1466do_execsql_test 1.2.1 { UPDATE rt1_node SET nodeno=21 WHERE nodeno=3; }
1467do_execsql_test 1.2.1 { SELECT rtreecheck('rt1')=='ok'; } {0}
1468do_execsql_test 1.2.2 { SELECT rtreecheck('main', 'rt1')=='ok'; } {0}
1469do_execsql_test 1.2.3 { SELECT rtreecheck('aux', 'rt1')=='ok'; } {1}
1470do_execsql_test 1.2.4 { UPDATE rt1_node SET nodeno=3 WHERE nodeno=21; }
1471
1472# Corrupt the table in database 'aux':
1473do_execsql_test 1.2.1 { UPDATE aux.rt1_node SET nodeno=21 WHERE nodeno=3; }
1474do_execsql_test 1.2.1 { SELECT rtreecheck('rt1')=='ok'; } {1}
1475do_execsql_test 1.2.2 { SELECT rtreecheck('main', 'rt1')=='ok'; } {1}
1476do_execsql_test 1.2.3 { SELECT rtreecheck('aux', 'rt1')=='ok'; } {0}
1477do_execsql_test 1.2.4 { UPDATE rt1_node SET nodeno=3 WHERE nodeno=21; }
1478
1479# EVIDENCE-OF: R-45759-33459 Example: To verify that an R*Tree named
1480# "demo_index" is well-formed and internally consistent, run: SELECT
1481# rtreecheck('demo_index');
1482do_execsql_test 2.0 {
1483 CREATE VIRTUAL TABLE demo_index USING rtree(id, x1,x2, y1,y2);
1484 INSERT INTO demo_index SELECT id, a, b, a, b FROM rt1;
1485}
1486do_execsql_test 2.1 { SELECT rtreecheck('demo_index') } {ok}
1487do_execsql_test 2.2 {
1488 UPDATE demo_index_rowid SET nodeno=44 WHERE rowid=44;
1489 SELECT rtreecheck('demo_index');
1490} {{Found (44 -> 44) in %_rowid table, expected (44 -> 4)}}
1491
1492
1493do_execsql_test 3.0 {
1494 CREATE VIRTUAL TABLE rt2 USING rtree_i32(id, a, b, c, d);
1495 WITH s(i) AS (
1496 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<200
1497 )
1498 INSERT INTO rt2 SELECT i, i, i+2, i, i+2 FROM s;
1499}
1500
1501# EVIDENCE-OF: R-02555-31045 for each dimension, (coord1 <= coord2).
1502#
1503execsql BEGIN
1504do_test 3.1 {
1505 set cell [
1506 lindex [execsql {SELECT rnode(data) FROM rt2_node WHERE nodeno=3}] 0 3
1507 ]
1508 set cell [list [lindex $cell 0] \
1509 [lindex $cell 2] [lindex $cell 1] \
1510 [lindex $cell 3] [lindex $cell 4] \
1511 ]
1512 execsql {
1513 UPDATE rt2_node SET data=rnode_replace_cell(data, 3, $cell) WHERE nodeno=3
1514 }
1515 execsql { SELECT rtreecheck('rt2') }
1516} {{Dimension 0 of cell 3 on node 3 is corrupt}}
1517execsql ROLLBACK
1518
1519# EVIDENCE-OF: R-13844-15873 unless the cell is on the root node, that
1520# the cell is bounded by the parent cell on the parent node.
1521#
1522execsql BEGIN
1523do_test 3.2 {
1524 set cell [
1525 lindex [execsql {SELECT rnode(data) FROM rt2_node WHERE nodeno=3}] 0 3
1526 ]
1527 lset cell 3 450
1528 lset cell 4 451
1529 execsql {
1530 UPDATE rt2_node SET data=rnode_replace_cell(data, 3, $cell) WHERE nodeno=3
1531 }
1532 execsql { SELECT rtreecheck('rt2') }
1533} {{Dimension 1 of cell 3 on node 3 is corrupt relative to parent}}
1534execsql ROLLBACK
1535
1536# EVIDENCE-OF: R-02505-03621 for leaf nodes, that there is an entry in
1537# the %_rowid table corresponding to the cell's rowid value that points
1538# to the correct node.
1539#
1540execsql BEGIN
1541do_test 3.3 {
1542 execsql {
1543 UPDATE rt2_rowid SET rowid=452 WHERE rowid=100
1544 }
1545 execsql { SELECT rtreecheck('rt2') }
1546} {{Mapping (100 -> 6) missing from %_rowid table}}
1547execsql ROLLBACK
1548
1549# EVIDENCE-OF: R-50927-02218 for cells on non-leaf nodes, that there is
1550# an entry in the %_parent table mapping from the cell's child node to
1551# the node that it resides on.
1552#
1553execsql BEGIN
1554do_test 3.4.1 {
1555 execsql {
1556 UPDATE rt2_parent SET parentnode=123 WHERE nodeno=3
1557 }
1558 execsql { SELECT rtreecheck('rt2') }
1559} {{Found (3 -> 123) in %_parent table, expected (3 -> 1)}}
1560execsql ROLLBACK
1561execsql BEGIN
1562do_test 3.4.2 {
1563 execsql {
1564 UPDATE rt2_parent SET nodeno=123 WHERE nodeno=3
1565 }
1566 execsql { SELECT rtreecheck('rt2') }
1567} {{Mapping (3 -> 1) missing from %_parent table}}
1568execsql ROLLBACK
1569
1570# EVIDENCE-OF: R-23235-09153 That there are the same number of entries
1571# in the %_rowid table as there are leaf cells in the r-tree structure,
1572# and that there is a leaf cell that corresponds to each entry in the
1573# %_rowid table.
1574execsql BEGIN
1575do_test 3.5 {
1576 execsql { INSERT INTO rt2_rowid VALUES(1000, 1000) }
1577 execsql { SELECT rtreecheck('rt2') }
1578} {{Wrong number of entries in %_rowid table - expected 200, actual 201}}
1579execsql ROLLBACK
1580
1581# EVIDENCE-OF: R-62800-43436 That there are the same number of entries
1582# in the %_parent table as there are non-leaf cells in the r-tree
1583# structure, and that there is a non-leaf cell that corresponds to each
1584# entry in the %_parent table.
1585execsql BEGIN
1586do_test 3.6 {
1587 execsql { INSERT INTO rt2_parent VALUES(1000, 1000) }
1588 execsql { SELECT rtreecheck('rt2') }
1589} {{Wrong number of entries in %_parent table - expected 10, actual 11}}
1590execsql ROLLBACK
1591
1592
1593
1594finish_test