select2.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.18 2002/04/02 13:26:11 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. set f [open ./testdata1.txt w]
  21. for {set i 0} {$i<=30} {incr i} {
  22. puts $f "[expr {$i%9}]\t[expr {$i%10}]"
  23. }
  24. close $f
  25. execsql {COPY tbl1 FROM './testdata1.txt'}
  26. file delete -force ./testdata1.txt
  27. catch {unset data}
  28. # Do a second query inside a first.
  29. #
  30. do_test select2-1.1 {
  31. set sql {SELECT DISTINCT f1 FROM tbl1 ORDER BY f1}
  32. set r {}
  33. db eval $sql data {
  34. set f1 $data(f1)
  35. lappend r $f1:
  36. set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
  37. db eval $sql2 d2 {
  38. lappend r $d2(f2)
  39. }
  40. }
  41. set r
  42. } {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}
  43. do_test select2-1.2 {
  44. set sql {SELECT DISTINCT f1 FROM tbl1 WHERE f1>3 AND f1<5}
  45. set r {}
  46. db eval $sql data {
  47. set f1 $data(f1)
  48. lappend r $f1:
  49. set sql2 "SELECT f2 FROM tbl1 WHERE f1=$f1 ORDER BY f2"
  50. db eval $sql2 d2 {
  51. lappend r $d2(f2)
  52. }
  53. }
  54. set r
  55. } {4: 2 3 4}
  56. # Create a largish table
  57. #
  58. do_test select2-2.0 {
  59. execsql {CREATE TABLE tbl2(f1 int, f2 int, f3 int)}
  60. set f [open ./testdata1.txt w]
  61. for {set i 1} {$i<=30000} {incr i} {
  62. puts $f "$i\t[expr {$i*2}]\t[expr {$i*3}]"
  63. }
  64. close $f
  65. # execsql {--vdbe-trace-on--}
  66. execsql {COPY tbl2 FROM './testdata1.txt'}
  67. file delete -force ./testdata1.txt
  68. } {}
  69. do_test select2-2.1 {
  70. execsql {SELECT count(*) FROM tbl2}
  71. } {30000}
  72. do_test select2-2.2 {
  73. execsql {SELECT count(*) FROM tbl2 WHERE f2>1000}
  74. } {29500}
  75. do_test select2-3.1 {
  76. execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
  77. } {500}
  78. do_test select2-3.2a {
  79. execsql {CREATE INDEX idx1 ON tbl2(f2)}
  80. } {}
  81. do_test select2-3.2b {
  82. execsql {SELECT f1 FROM tbl2 WHERE 1000=f2}
  83. } {500}
  84. do_test select2-3.2c {
  85. execsql {SELECT f1 FROM tbl2 WHERE f2=1000}
  86. } {500}
  87. do_test select2-3.2d {
  88. set sqlite_search_count 0
  89. execsql {SELECT * FROM tbl2 WHERE 1000=f2}
  90. set sqlite_search_count
  91. } {3}
  92. do_test select2-3.2e {
  93. set sqlite_search_count 0
  94. execsql {SELECT * FROM tbl2 WHERE f2=1000}
  95. set sqlite_search_count
  96. } {3}
  97. # Make sure queries run faster with an index than without
  98. #
  99. do_test select2-3.3 {
  100. execsql {DROP INDEX idx1}
  101. set sqlite_search_count 0
  102. execsql {SELECT f1 FROM tbl2 WHERE f2==2000}
  103. set sqlite_search_count
  104. } {29999}
  105. # Make sure we can optimize functions in the WHERE clause that
  106. # use fields from two or more different table. (Bug #6)
  107. #
  108. do_test select2-4.1 {
  109. execsql {
  110. CREATE TABLE aa(a);
  111. CREATE TABLE bb(b);
  112. INSERT INTO aa VALUES(1);
  113. INSERT INTO aa VALUES(3);
  114. INSERT INTO bb VALUES(2);
  115. INSERT INTO bb VALUES(4);
  116. SELECT * FROM aa, bb WHERE max(a,b)>2;
  117. }
  118. } {1 4 3 2 3 4}
  119. do_test select2-4.2 {
  120. execsql {
  121. INSERT INTO bb VALUES(0);
  122. SELECT * FROM aa, bb WHERE b;
  123. }
  124. } {1 2 1 4 3 2 3 4}
  125. do_test select2-4.3 {
  126. execsql {
  127. SELECT * FROM aa, bb WHERE NOT b;
  128. }
  129. } {1 0 3 0}
  130. do_test select2-4.4 {
  131. execsql {
  132. SELECT * FROM aa, bb WHERE min(a,b);
  133. }
  134. } {1 2 1 4 3 2 3 4}
  135. do_test select2-4.5 {
  136. execsql {
  137. SELECT * FROM aa, bb WHERE NOT min(a,b);
  138. }
  139. } {1 0 3 0}
  140. do_test select2-4.6 {
  141. execsql {
  142. SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 1 END;
  143. }
  144. } {1 2 3 4}
  145. do_test select2-4.7 {
  146. execsql {
  147. SELECT * FROM aa, bb WHERE CASE WHEN a=b-1 THEN 0 ELSE 1 END;
  148. }
  149. } {1 4 1 0 3 2 3 0}
  150. finish_test