between.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # 2005 July 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 implements regression tests for SQLite library. The
  12. # focus of this file is testing the use of indices in WHERE clauses
  13. # when the WHERE clause contains the BETWEEN operator.
  14. #
  15. # $Id: between.test,v 1.2 2006/01/17 09:35:02 danielk1977 Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Build some test data
  19. #
  20. do_test between-1.0 {
  21. execsql {
  22. BEGIN;
  23. CREATE TABLE t1(w int, x int, y int, z int);
  24. }
  25. for {set i 1} {$i<=100} {incr i} {
  26. set w $i
  27. set x [expr {int(log($i)/log(2))}]
  28. set y [expr {$i*$i + 2*$i + 1}]
  29. set z [expr {$x+$y}]
  30. ifcapable tclvar {
  31. # Random unplanned test of the $varname variable syntax.
  32. execsql {INSERT INTO t1 VALUES($::w,$::x,$::y,$::z)}
  33. } else {
  34. # If the $varname syntax is not available, use the regular variable
  35. # declaration syntax.
  36. execsql {INSERT INTO t1 VALUES(:w,:x,:y,:z)}
  37. }
  38. }
  39. execsql {
  40. CREATE UNIQUE INDEX i1w ON t1(w);
  41. CREATE INDEX i1xy ON t1(x,y);
  42. CREATE INDEX i1zyx ON t1(z,y,x);
  43. COMMIT;
  44. }
  45. } {}
  46. # This procedure executes the SQL. Then it appends to the result the
  47. # "sort" or "nosort" keyword depending on whether or not any sorting
  48. # is done. Then it appends the names of the table and index used.
  49. #
  50. proc queryplan {sql} {
  51. set ::sqlite_sort_count 0
  52. set data [execsql $sql]
  53. if {$::sqlite_sort_count} {set x sort} {set x nosort}
  54. lappend data $x
  55. set eqp [execsql "EXPLAIN QUERY PLAN $sql"]
  56. # puts eqp=$eqp
  57. foreach {a b c x} $eqp {
  58. if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \
  59. $x all as tab idx]} {
  60. lappend data $tab $idx
  61. } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} {
  62. lappend data $tab *
  63. }
  64. }
  65. return $data
  66. }
  67. do_test between-1.1.1 {
  68. queryplan {
  69. SELECT * FROM t1 WHERE w BETWEEN 5 AND 6 ORDER BY +w
  70. }
  71. } {5 2 36 38 6 2 49 51 sort t1 i1w}
  72. do_test between-1.1.2 {
  73. queryplan {
  74. SELECT * FROM t1 WHERE +w BETWEEN 5 AND 6 ORDER BY +w
  75. }
  76. } {5 2 36 38 6 2 49 51 sort t1 *}
  77. do_test between-1.2.1 {
  78. queryplan {
  79. SELECT * FROM t1 WHERE w BETWEEN 5 AND 65-y ORDER BY +w
  80. }
  81. } {5 2 36 38 6 2 49 51 sort t1 i1w}
  82. do_test between-1.2.2 {
  83. queryplan {
  84. SELECT * FROM t1 WHERE +w BETWEEN 5 AND 65-y ORDER BY +w
  85. }
  86. } {5 2 36 38 6 2 49 51 sort t1 *}
  87. do_test between-1.3.1 {
  88. queryplan {
  89. SELECT * FROM t1 WHERE w BETWEEN 41-y AND 6 ORDER BY +w
  90. }
  91. } {5 2 36 38 6 2 49 51 sort t1 i1w}
  92. do_test between-1.3.2 {
  93. queryplan {
  94. SELECT * FROM t1 WHERE +w BETWEEN 41-y AND 6 ORDER BY +w
  95. }
  96. } {5 2 36 38 6 2 49 51 sort t1 *}
  97. do_test between-1.4 {
  98. queryplan {
  99. SELECT * FROM t1 WHERE w BETWEEN 41-y AND 65-y ORDER BY +w
  100. }
  101. } {5 2 36 38 6 2 49 51 sort t1 *}
  102. do_test between-1.5.1 {
  103. queryplan {
  104. SELECT * FROM t1 WHERE 26 BETWEEN y AND z ORDER BY +w
  105. }
  106. } {4 2 25 27 sort t1 i1zyx}
  107. do_test between-1.5.2 {
  108. queryplan {
  109. SELECT * FROM t1 WHERE 26 BETWEEN +y AND z ORDER BY +w
  110. }
  111. } {4 2 25 27 sort t1 i1zyx}
  112. do_test between-1.5.3 {
  113. queryplan {
  114. SELECT * FROM t1 WHERE 26 BETWEEN y AND +z ORDER BY +w
  115. }
  116. } {4 2 25 27 sort t1 *}
  117. finish_test