rtreeE.test 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # 2010 August 28
  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. # This file contains tests for the r-tree module. Specifically, it tests
  12. # that new-style custom r-tree queries (geometry callbacks) work.
  13. #
  14. if {![info exists testdir]} {
  15. set testdir [file join [file dirname [info script]] .. .. test]
  16. }
  17. source [file join [file dirname [info script]] rtree_util.tcl]
  18. source $testdir/tester.tcl
  19. ifcapable !rtree { finish_test ; return }
  20. ifcapable rtree_int_only { finish_test; return }
  21. #-------------------------------------------------------------------------
  22. # Test the example 2d "circle" geometry callback.
  23. #
  24. register_circle_geom db
  25. do_execsql_test rtreeE-1.0.0 {
  26. PRAGMA page_size=512;
  27. CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1);
  28. /* A tight pattern of small boxes near 0,0 */
  29. WITH RECURSIVE
  30. x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
  31. y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
  32. INSERT INTO rt1 SELECT x+5*y, x, x+2, y, y+2 FROM x, y;
  33. /* A looser pattern of small boxes near 100, 0 */
  34. WITH RECURSIVE
  35. x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
  36. y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
  37. INSERT INTO rt1 SELECT 100+x+5*y, x*3+100, x*3+102, y*3, y*3+2 FROM x, y;
  38. /* A looser pattern of larger boxes near 0, 200 */
  39. WITH RECURSIVE
  40. x(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM x WHERE x<4),
  41. y(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM y WHERE y<4)
  42. INSERT INTO rt1 SELECT 200+x+5*y, x*7, x*7+15, y*7+200, y*7+215 FROM x, y;
  43. } {}
  44. do_rtree_integrity_test rtreeE-1.0.1 rt1
  45. # Queries against each of the three clusters */
  46. do_execsql_test rtreeE-1.1 {
  47. SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 0.0, 50.0, 3) ORDER BY id;
  48. } {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}
  49. do_execsql_test rtreeE-1.1x {
  50. SELECT id FROM rt1 WHERE id MATCH Qcircle('x:0 y:0 r:50.0 e:3') ORDER BY id;
  51. } {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}
  52. do_execsql_test rtreeE-1.2 {
  53. SELECT id FROM rt1 WHERE id MATCH Qcircle(100.0, 0.0, 50.0, 3) ORDER BY id;
  54. } {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}
  55. do_execsql_test rtreeE-1.3 {
  56. SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 200.0, 50.0, 3) ORDER BY id;
  57. } {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}
  58. # The Qcircle geometry function gives a lower score to larger leaf-nodes.
  59. # This causes the 200s to sort before the 100s and the 0s to sort before
  60. # last.
  61. #
  62. do_execsql_test rtreeE-1.4 {
  63. SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:3') AND id%100==0
  64. } {200 100 0}
  65. # Exclude odd rowids on a depth-first search
  66. do_execsql_test rtreeE-1.5 {
  67. SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:4') ORDER BY +id
  68. } {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}
  69. # Exclude odd rowids on a breadth-first search.
  70. do_execsql_test rtreeE-1.6 {
  71. SELECT id FROM rt1 WHERE id MATCH Qcircle(0,0,1000,5) ORDER BY +id
  72. } {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}
  73. # Test that rtree prefers MATCH to lookup-by-rowid.
  74. #
  75. do_execsql_test rtreeE-1.7 {
  76. SELECT id FROM rt1 WHERE id=18 AND id MATCH Qcircle(0,0,1000,5)
  77. } {18}
  78. # Construct a large 2-D RTree with thousands of random entries.
  79. #
  80. do_test rtreeE-2.1 {
  81. db eval {
  82. CREATE TABLE t2(id,x0,x1,y0,y1);
  83. CREATE VIRTUAL TABLE rt2 USING rtree(id,x0,x1,y0,y1);
  84. BEGIN;
  85. }
  86. expr srand(0)
  87. for {set i 1} {$i<=10000} {incr i} {
  88. set dx [expr {int(rand()*40)+1}]
  89. set dy [expr {int(rand()*40)+1}]
  90. set x0 [expr {int(rand()*(10000 - $dx))}]
  91. set x1 [expr {$x0+$dx}]
  92. set y0 [expr {int(rand()*(10000 - $dy))}]
  93. set y1 [expr {$y0+$dy}]
  94. set id [expr {$i+10000}]
  95. db eval {INSERT INTO t2 VALUES($id,$x0,$x1,$y0,$y1)}
  96. }
  97. db eval {
  98. INSERT INTO rt2 SELECT * FROM t2;
  99. COMMIT;
  100. }
  101. } {}
  102. do_rtree_integrity_test rtreeE-2.1.1 rt2
  103. for {set i 1} {$i<=200} {incr i} {
  104. set dx [expr {int(rand()*100)}]
  105. set dy [expr {int(rand()*100)}]
  106. set x0 [expr {int(rand()*(10000 - $dx))}]
  107. set x1 [expr {$x0+$dx}]
  108. set y0 [expr {int(rand()*(10000 - $dy))}]
  109. set y1 [expr {$y0+$dy}]
  110. set ans [db eval {SELECT id FROM t2 WHERE x1>=$x0 AND x0<=$x1 AND y1>=$y0 AND y0<=$y1 ORDER BY id}]
  111. do_execsql_test rtreeE-2.2.$i {
  112. SELECT id FROM rt2 WHERE id MATCH breadthfirstsearch($x0,$x1,$y0,$y1) ORDER BY id
  113. } $ans
  114. }
  115. # Run query that have very deep priority queues
  116. #
  117. set ans [db eval {SELECT id FROM t2 WHERE x1>=0 AND x0<=5000 AND y1>=0 AND y0<=5000 ORDER BY id}]
  118. do_execsql_test rtreeE-2.3 {
  119. SELECT id FROM rt2 WHERE id MATCH breadthfirstsearch(0,5000,0,5000) ORDER BY id
  120. } $ans
  121. set ans [db eval {SELECT id FROM t2 WHERE x1>=0 AND x0<=10000 AND y1>=0 AND y0<=10000 ORDER BY id}]
  122. do_execsql_test rtreeE-2.4 {
  123. SELECT id FROM rt2 WHERE id MATCH breadthfirstsearch(0,10000,0,10000) ORDER BY id
  124. } $ans
  125. finish_test