select2.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # 2001 September 15
  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 SELECT statement.
  13. #
  14. # $Id: select2.test,v 1.28 2009/01/15 15:23:59 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create a table with some data
  18. #
  19. execsql {CREATE TABLE tbl1(f1 int, f2 int)}
  20. execsql {BEGIN}
  21. for {set i 0} {$i<=30} {incr i} {
  22. execsql "INSERT INTO tbl1 VALUES([expr {$i%9}],[expr {$i%10}])"
  23. }
  24. execsql {COMMIT}
  25. # Do a second query inside a first.
  26. #
  27. do_test select2-1.1 {
  28. set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
  29. set r {}
  30. catch {unset data}
  31. db eval $sql data {
  32. set f1 $data(f1)
  33. lappend r $f1:
  34. set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
  35. db eval $sql2 d2 {
  36. lappend r $d2(f2)
  37. }
  38. }
  39. set r
  40. } {0: 0 7 8 9 1: 0 1 8 9 2: 0 1 2 9 3: 0 1 2 3 4: 2 3 4 5: 3 4 5 6: 4 5 6 7: 5 6 7 8: 6 7 8}
  41. do_test select2-1.2 {
  42. set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
  43. set r {}
  44. db eval $sql data {
  45. set f1 $data(f1)
  46. lappend r $f1:
  47. set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
  48. db eval $sql2 d2 {
  49. lappend r $d2(f2)
  50. }
  51. }
  52. set r
  53. } {4: 2 3 4}
  54. unset data
  55. # Create a largish table. Do this twice, once using the TCL cache and once
  56. # without. Compare the performance to make sure things go faster with the
  57. # cache turned on.
  58. #
  59. ifcapable tclvar {
  60. do_test select2-2.0.1 {
  61. set t1 [time {
  62. execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
  63. for {set i 1} {$i<=30000} {incr i} {
  64. set i2 [expr {$i*2}]
  65. set i3 [expr {$i*3}]
  66. db eval {INSERT INTO tbl2 VALUES($i,$i2,$i3)}
  67. }
  68. execsql {COMMIT}
  69. }]
  70. list
  71. } {}
  72. puts "time with cache: $::t1"
  73. }
  74. catch {execsql {DROP TABLE tbl2}}
  75. do_test select2-2.0.2 {
  76. set t2 [time {
  77. execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int); BEGIN;}
  78. for {set i 1} {$i<=30000} {incr i} {
  79. set i2 [expr {$i*2}]
  80. set i3 [expr {$i*3}]
  81. execsql "INSERT INTO tbl2 VALUES($i,$i2,$i3)"
  82. }
  83. execsql {COMMIT}
  84. }]
  85. list
  86. } {}
  87. puts "time without cache: $t2"
  88. #ifcapable tclvar {
  89. # do_test select2-2.0.3 {
  90. # expr {[lindex $t1 0]<[lindex $t2 0]}
  91. # } 1
  92. #}
  93. do_test select2-2.1 {
  94. execsql {SELECT count(*) FROM tbl2}
  95. } {30000}
  96. do_test select2-2.2 {
  97. execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
  98. } {29500}
  99. do_test select2-3.1 {
  100. execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
  101. } {500}
  102. do_test select2-3.2a {
  103. execsql {CREATE INDEX idx1 ON tbl2(f2)}
  104. } {}
  105. do_test select2-3.2b {
  106. execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
  107. } {500}
  108. do_test select2-3.2c {
  109. execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
  110. } {500}
  111. do_test select2-3.2d {
  112. set sqlite_search_count 0
  113. execsql {SELECT * FROM tbl2 WHERE 1000=f2}
  114. set sqlite_search_count
  115. } {3}
  116. do_test select2-3.2e {
  117. set sqlite_search_count 0
  118. execsql {SELECT * FROM tbl2 WHERE f2=1000}
  119. set sqlite_search_count
  120. } {3}
  121. # Make sure queries run faster with an index than without
  122. #
  123. do_test select2-3.3 {
  124. execsql {DROP INDEX idx1}
  125. set sqlite_search_count 0
  126. execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
  127. set sqlite_search_count
  128. } {29999}
  129. # Make sure we can optimize functions in the WHERE clause that
  130. # use fields from two or more different table. (Bug #6)
  131. #
  132. do_test select2-4.1 {
  133. execsql {
  134. CREATE TABLE aa(a);
  135. CREATE TABLE bb(b);
  136. INSERT INTO aa VALUES(1);
  137. INSERT INTO aa VALUES(3);
  138. INSERT INTO bb VALUES(2);
  139. INSERT INTO bb VALUES(4);
  140. SELECT * FROM aa, bb WHERE max(a,b)>2;
  141. }
  142. } {1 4 3 2 3 4}
  143. do_test select2-4.2 {
  144. execsql {
  145. INSERT INTO bb VALUES(0);
  146. SELECT * FROM aa CROSS JOIN bb WHERE b;
  147. }
  148. } {1 2 1 4 3 2 3 4}
  149. do_test select2-4.3 {
  150. execsql {
  151. SELECT * FROM aa CROSS JOIN bb WHERE NOT b;
  152. }
  153. } {1 0 3 0}
  154. do_test select2-4.4 {
  155. execsql {
  156. SELECT * FROM aa, bb WHERE min(a,b);
  157. }
  158. } {1 2 1 4 3 2 3 4}
  159. do_test select2-4.5 {
  160. execsql {
  161. SELECT * FROM aa, bb WHERE NOT min(a,b);
  162. }
  163. } {1 0 3 0}
  164. do_test select2-4.6 {
  165. execsql {
  166. SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
  167. }
  168. } {1 2 3 4}
  169. do_test select2-4.7 {
  170. execsql {
  171. SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;
  172. }
  173. } {1 4 1 0 3 2 3 0}
  174. finish_test