xref: /sqlite-3.40.0/ext/rtree/rtreeE.test (revision 1917e92f)
165e6b0ddSdrh# 2010 August 28
265e6b0ddSdrh#
365e6b0ddSdrh# The author disclaims copyright to this source code.  In place of
465e6b0ddSdrh# a legal notice, here is a blessing:
565e6b0ddSdrh#
665e6b0ddSdrh#    May you do good and not evil.
765e6b0ddSdrh#    May you find forgiveness for yourself and forgive others.
865e6b0ddSdrh#    May you share freely, never taking more than you give.
965e6b0ddSdrh#
1065e6b0ddSdrh#***********************************************************************
1165e6b0ddSdrh# This file contains tests for the r-tree module. Specifically, it tests
1265e6b0ddSdrh# that new-style custom r-tree queries (geometry callbacks) work.
1365e6b0ddSdrh#
1465e6b0ddSdrh
1565e6b0ddSdrhif {![info exists testdir]} {
1665e6b0ddSdrh  set testdir [file join [file dirname [info script]] .. .. test]
1765e6b0ddSdrh}
18*1917e92fSdansource [file join [file dirname [info script]] rtree_util.tcl]
1965e6b0ddSdrhsource $testdir/tester.tcl
2065e6b0ddSdrhifcapable !rtree { finish_test ; return }
2165e6b0ddSdrhifcapable rtree_int_only { finish_test; return }
2265e6b0ddSdrh
2365e6b0ddSdrh
2465e6b0ddSdrh#-------------------------------------------------------------------------
2565e6b0ddSdrh# Test the example 2d "circle" geometry callback.
2665e6b0ddSdrh#
2765e6b0ddSdrhregister_circle_geom db
2865e6b0ddSdrh
29*1917e92fSdando_execsql_test rtreeE-1.0.0 {
3065e6b0ddSdrh  PRAGMA page_size=512;
3165e6b0ddSdrh  CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1);
3265e6b0ddSdrh
3365e6b0ddSdrh  /* A tight pattern of small boxes near 0,0 */
3465e6b0ddSdrh  WITH RECURSIVE
3565e6b0ddSdrh    x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
3665e6b0ddSdrh    y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
3765e6b0ddSdrh  INSERT INTO rt1 SELECT x+5*y, x, x+2, y, y+2 FROM x, y;
3865e6b0ddSdrh
3965e6b0ddSdrh  /* A looser pattern of small boxes near 100, 0 */
4065e6b0ddSdrh  WITH RECURSIVE
4165e6b0ddSdrh    x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
4265e6b0ddSdrh    y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
4365e6b0ddSdrh  INSERT INTO rt1 SELECT 100+x+5*y, x*3+100, x*3+102, y*3, y*3+2 FROM x, y;
4465e6b0ddSdrh
4565e6b0ddSdrh  /* A looser pattern of larger boxes near 0, 200 */
4665e6b0ddSdrh  WITH RECURSIVE
4765e6b0ddSdrh    x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
4865e6b0ddSdrh    y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
4965e6b0ddSdrh  INSERT INTO rt1 SELECT 200+x+5*y, x*7, x*7+15, y*7+200, y*7+215 FROM x, y;
5065e6b0ddSdrh} {}
51*1917e92fSdando_rtree_integrity_test rtreeE-1.0.1 rt1
5265e6b0ddSdrh
5365e6b0ddSdrh# Queries against each of the three clusters */
5465e6b0ddSdrhdo_execsql_test rtreeE-1.1 {
5565e6b0ddSdrh  SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 0.0, 50.0, 3) ORDER BY id;
5665e6b0ddSdrh} {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
574f03f413Sdrhdo_execsql_test rtreeE-1.1x {
584f03f413Sdrh  SELECT id FROM rt1 WHERE id MATCH Qcircle('x:0 y:0 r:50.0 e:3') ORDER BY id;
594f03f413Sdrh} {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
6065e6b0ddSdrhdo_execsql_test rtreeE-1.2 {
6165e6b0ddSdrh  SELECT id FROM rt1 WHERE id MATCH Qcircle(100.0, 0.0, 50.0, 3) ORDER BY id;
6265e6b0ddSdrh} {100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124}
6365e6b0ddSdrhdo_execsql_test rtreeE-1.3 {
6465e6b0ddSdrh  SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 200.0, 50.0, 3) ORDER BY id;
6565e6b0ddSdrh} {200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224}
6665e6b0ddSdrh
6765e6b0ddSdrh# The Qcircle geometry function gives a lower score to larger leaf-nodes.
6865e6b0ddSdrh# This causes the 200s to sort before the 100s and the 0s to sort before
6965e6b0ddSdrh# last.
7065e6b0ddSdrh#
7165e6b0ddSdrhdo_execsql_test rtreeE-1.4 {
724f03f413Sdrh  SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:3') AND id%100==0
7365e6b0ddSdrh} {200 100 0}
7465e6b0ddSdrh
7565e6b0ddSdrh# Exclude odd rowids on a depth-first search
7665e6b0ddSdrhdo_execsql_test rtreeE-1.5 {
774f03f413Sdrh  SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:4') ORDER BY +id
7865e6b0ddSdrh} {0 2 4 6 8 10 12 14 16 18 20 22 24 100 102 104 106 108 110 112 114 116 118 120 122 124 200 202 204 206 208 210 212 214 216 218 220 222 224}
7965e6b0ddSdrh
8065e6b0ddSdrh# Exclude odd rowids on a breadth-first search.
8165e6b0ddSdrhdo_execsql_test rtreeE-1.6 {
8265e6b0ddSdrh  SELECT id FROM rt1 WHERE id MATCH Qcircle(0,0,1000,5) ORDER BY +id
8365e6b0ddSdrh} {0 2 4 6 8 10 12 14 16 18 20 22 24 100 102 104 106 108 110 112 114 116 118 120 122 124 200 202 204 206 208 210 212 214 216 218 220 222 224}
8465e6b0ddSdrh
856b76418eSdan# Test that rtree prefers MATCH to lookup-by-rowid.
866b76418eSdan#
876b76418eSdando_execsql_test rtreeE-1.7 {
886b76418eSdan  SELECT id FROM rt1 WHERE id=18 AND id MATCH Qcircle(0,0,1000,5)
896b76418eSdan} {18}
906b76418eSdan
916b76418eSdan
9265e6b0ddSdrh# Construct a large 2-D RTree with thousands of random entries.
9365e6b0ddSdrh#
9465e6b0ddSdrhdo_test rtreeE-2.1 {
9565e6b0ddSdrh  db eval {
9665e6b0ddSdrh    CREATE TABLE t2(id,x0,x1,y0,y1);
9765e6b0ddSdrh    CREATE VIRTUAL TABLE rt2 USING rtree(id,x0,x1,y0,y1);
9865e6b0ddSdrh    BEGIN;
9965e6b0ddSdrh  }
10065e6b0ddSdrh  expr srand(0)
10165e6b0ddSdrh  for {set i 1} {$i<=10000} {incr i} {
10265e6b0ddSdrh    set dx [expr {int(rand()*40)+1}]
10365e6b0ddSdrh    set dy [expr {int(rand()*40)+1}]
10465e6b0ddSdrh    set x0 [expr {int(rand()*(10000 - $dx))}]
10565e6b0ddSdrh    set x1 [expr {$x0+$dx}]
10665e6b0ddSdrh    set y0 [expr {int(rand()*(10000 - $dy))}]
10765e6b0ddSdrh    set y1 [expr {$y0+$dy}]
10865e6b0ddSdrh    set id [expr {$i+10000}]
10965e6b0ddSdrh    db eval {INSERT INTO t2 VALUES($id,$x0,$x1,$y0,$y1)}
11065e6b0ddSdrh  }
11165e6b0ddSdrh  db eval {
11265e6b0ddSdrh    INSERT INTO rt2 SELECT * FROM t2;
11365e6b0ddSdrh    COMMIT;
11465e6b0ddSdrh  }
11565e6b0ddSdrh} {}
116*1917e92fSdando_rtree_integrity_test rtreeE-2.1.1 rt2
11765e6b0ddSdrh
11865e6b0ddSdrhfor {set i 1} {$i<=200} {incr i} {
11965e6b0ddSdrh  set dx [expr {int(rand()*100)}]
12065e6b0ddSdrh  set dy [expr {int(rand()*100)}]
12165e6b0ddSdrh  set x0 [expr {int(rand()*(10000 - $dx))}]
12265e6b0ddSdrh  set x1 [expr {$x0+$dx}]
12365e6b0ddSdrh  set y0 [expr {int(rand()*(10000 - $dy))}]
12465e6b0ddSdrh  set y1 [expr {$y0+$dy}]
12565e6b0ddSdrh  set ans [db eval {SELECT id FROM t2 WHERE x1>=$x0 AND x0<=$x1 AND y1>=$y0 AND y0<=$y1 ORDER BY id}]
12665e6b0ddSdrh  do_execsql_test rtreeE-2.2.$i {
12765e6b0ddSdrh    SELECT id FROM rt2 WHERE id MATCH breadthfirstsearch($x0,$x1,$y0,$y1) ORDER BY id
12865e6b0ddSdrh  } $ans
12965e6b0ddSdrh}
13065e6b0ddSdrh
13165e6b0ddSdrh# Run query that have very deep priority queues
13265e6b0ddSdrh#
13365e6b0ddSdrhset ans [db eval {SELECT id FROM t2 WHERE x1>=0 AND x0<=5000 AND y1>=0 AND y0<=5000 ORDER BY id}]
13465e6b0ddSdrhdo_execsql_test rtreeE-2.3 {
13565e6b0ddSdrh  SELECT id FROM rt2 WHERE id MATCH breadthfirstsearch(0,5000,0,5000) ORDER BY id
13665e6b0ddSdrh} $ans
13765e6b0ddSdrhset ans [db eval {SELECT id FROM t2 WHERE x1>=0 AND x0<=10000 AND y1>=0 AND y0<=10000 ORDER BY id}]
13865e6b0ddSdrhdo_execsql_test rtreeE-2.4 {
13965e6b0ddSdrh  SELECT id FROM rt2 WHERE id MATCH breadthfirstsearch(0,10000,0,10000) ORDER BY id
14065e6b0ddSdrh} $ans
14165e6b0ddSdrh
1426b76418eSdan
14365e6b0ddSdrhfinish_test
144