where.test 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 use of indices in WHERE clases.
  13. #
  14. # $Id: where.test,v 1.8 2002/06/14 22:38:43 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Build some test data
  18. #
  19. do_test where-1.0 {
  20. execsql {
  21. CREATE TABLE t1(w int, x int, y int);
  22. CREATE TABLE t2(p int, q int, r int, s int);
  23. }
  24. for {set i 1} {$i<=100} {incr i} {
  25. set w $i
  26. set x [expr {int(log($i)/log(2))}]
  27. set y [expr {$i*$i + 2*$i + 1}]
  28. execsql "INSERT INTO t1 VALUES($w,$x,$y)"
  29. }
  30. execsql {
  31. INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
  32. CREATE INDEX i1w ON t1(w);
  33. CREATE INDEX i1xy ON t1(x,y);
  34. CREATE INDEX i2p ON t2(p);
  35. CREATE INDEX i2r ON t2(r);
  36. CREATE INDEX i2qs ON t2(q, s);
  37. }
  38. } {}
  39. # Do an SQL statement. Append the search count to the end of the result.
  40. #
  41. proc count sql {
  42. set ::sqlite_search_count 0
  43. return [concat [execsql $sql] $::sqlite_search_count]
  44. }
  45. # Verify that queries use an index. We are using the special variable
  46. # "sqlite_search_count" which tallys the number of executions of MoveTo
  47. # and Next operators in the VDBE. By verifing that the search count is
  48. # small we can be assured that indices are being used properly.
  49. #
  50. do_test where-1.1 {
  51. count {SELECT x, y FROM t1 WHERE w=10}
  52. } {3 121 3}
  53. do_test where-1.2 {
  54. count {SELECT x, y FROM t1 WHERE w=11}
  55. } {3 144 3}
  56. do_test where-1.3 {
  57. count {SELECT x, y FROM t1 WHERE 11=w}
  58. } {3 144 3}
  59. do_test where-1.4 {
  60. count {SELECT x, y FROM t1 WHERE 11=w AND x>2}
  61. } {3 144 3}
  62. do_test where-1.5 {
  63. count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
  64. } {3 144 3}
  65. do_test where-1.6 {
  66. count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
  67. } {3 144 3}
  68. do_test where-1.7 {
  69. count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
  70. } {3 144 3}
  71. do_test where-1.8 {
  72. count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
  73. } {3 144 3}
  74. do_test where-1.9 {
  75. count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
  76. } {3 144 3}
  77. do_test where-1.10 {
  78. count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
  79. } {3 121 3}
  80. do_test where-1.11 {
  81. count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
  82. } {3 100 3}
  83. # New for SQLite version 2.1: Verify that that inequality constraints
  84. # are used correctly.
  85. #
  86. do_test where-1.12 {
  87. count {SELECT w FROM t1 WHERE x=3 AND y<100}
  88. } {8 3}
  89. do_test where-1.13 {
  90. count {SELECT w FROM t1 WHERE x=3 AND 100>y}
  91. } {8 3}
  92. do_test where-1.14 {
  93. count {SELECT w FROM t1 WHERE 3=x AND y<100}
  94. } {8 3}
  95. do_test where-1.15 {
  96. count {SELECT w FROM t1 WHERE 3=x AND 100>y}
  97. } {8 3}
  98. do_test where-1.16 {
  99. count {SELECT w FROM t1 WHERE x=3 AND y<=100}
  100. } {8 9 5}
  101. do_test where-1.17 {
  102. count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
  103. } {8 9 5}
  104. do_test where-1.18 {
  105. count {SELECT w FROM t1 WHERE x=3 AND y>225}
  106. } {15 3}
  107. do_test where-1.19 {
  108. count {SELECT w FROM t1 WHERE x=3 AND 225<y}
  109. } {15 3}
  110. do_test where-1.20 {
  111. count {SELECT w FROM t1 WHERE x=3 AND y>=225}
  112. } {14 15 5}
  113. do_test where-1.21 {
  114. count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
  115. } {14 15 5}
  116. do_test where-1.22 {
  117. count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
  118. } {11 12 5}
  119. do_test where-1.23 {
  120. count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
  121. } {10 11 12 13 9}
  122. do_test where-1.24 {
  123. count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
  124. } {11 12 5}
  125. do_test where-1.25 {
  126. count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
  127. } {10 11 12 13 9}
  128. # Need to work on optimizing the BETWEEN operator.
  129. #
  130. # do_test where-1.26 {
  131. # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
  132. # } {10 11 12 13 9}
  133. do_test where-1.27 {
  134. count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
  135. } {10 17}
  136. do_test where-1.28 {
  137. count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
  138. } {10 99}
  139. do_test where-1.29 {
  140. count {SELECT w FROM t1 WHERE y==121}
  141. } {10 99}
  142. do_test where-1.30 {
  143. count {SELECT w FROM t1 WHERE w>97}
  144. } {98 99 100 6}
  145. do_test where-1.31 {
  146. count {SELECT w FROM t1 WHERE w>=97}
  147. } {97 98 99 100 8}
  148. do_test where-1.33 {
  149. count {SELECT w FROM t1 WHERE w==97}
  150. } {97 3}
  151. do_test where-1.34 {
  152. count {SELECT w FROM t1 WHERE w+1==98}
  153. } {97 99}
  154. do_test where-1.35 {
  155. count {SELECT w FROM t1 WHERE w<3}
  156. } {1 2 4}
  157. do_test where-1.36 {
  158. count {SELECT w FROM t1 WHERE w<=3}
  159. } {1 2 3 6}
  160. do_test where-1.37 {
  161. count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
  162. } {1 2 3 99}
  163. # Do the same kind of thing except use a join as the data source.
  164. #
  165. do_test where-2.1 {
  166. count {
  167. SELECT w, p FROM t2, t1
  168. WHERE x=q AND y=s AND r=8977
  169. }
  170. } {34 67 6}
  171. do_test where-2.2 {
  172. count {
  173. SELECT w, p FROM t2, t1
  174. WHERE x=q AND s=y AND r=8977
  175. }
  176. } {34 67 6}
  177. do_test where-2.3 {
  178. count {
  179. SELECT w, p FROM t2, t1
  180. WHERE x=q AND s=y AND r=8977 AND w>10
  181. }
  182. } {34 67 6}
  183. do_test where-2.4 {
  184. count {
  185. SELECT w, p FROM t2, t1
  186. WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
  187. }
  188. } {34 67 6}
  189. do_test where-2.5 {
  190. count {
  191. SELECT w, p FROM t2, t1
  192. WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
  193. }
  194. } {34 67 6}
  195. do_test where-2.6 {
  196. count {
  197. SELECT w, p FROM t2, t1
  198. WHERE x=q AND p=77 AND s=y AND w>5
  199. }
  200. } {24 77 6}
  201. do_test where-2.7 {
  202. count {
  203. SELECT w, p FROM t1, t2
  204. WHERE x=q AND p>77 AND s=y AND w=5
  205. }
  206. } {5 96 6}
  207. # Lets do a 3-way join.
  208. #
  209. do_test where-3.1 {
  210. count {
  211. SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
  212. WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
  213. }
  214. } {11 90 11 9}
  215. do_test where-3.2 {
  216. count {
  217. SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
  218. WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
  219. }
  220. } {12 89 12 9}
  221. do_test where-3.3 {
  222. count {
  223. SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
  224. WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
  225. }
  226. } {15 86 86 9}
  227. # Test to see that the special case of a constant WHERE clause is
  228. # handled.
  229. #
  230. do_test where-4.1 {
  231. count {
  232. SELECT * FROM t1 WHERE 0
  233. }
  234. } {0}
  235. do_test where-4.2 {
  236. count {
  237. SELECT * FROM t1 WHERE 1 LIMIT 1
  238. }
  239. } {1 0 4 1}
  240. do_test where-4.3 {
  241. execsql {
  242. SELECT 99 WHERE 0
  243. }
  244. } {}
  245. do_test where-4.4 {
  246. execsql {
  247. SELECT 99 WHERE 1
  248. }
  249. } {99}
  250. # Verify that IN operators in a WHERE clause are handled correctly.
  251. #
  252. do_test where-5.1 {
  253. count {
  254. SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
  255. }
  256. } {1 0 4 2 1 9 3 1 16 0}
  257. do_test where-5.2 {
  258. count {
  259. SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
  260. }
  261. } {1 0 4 2 1 9 3 1 16 99}
  262. do_test where-5.3 {
  263. count {
  264. SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
  265. }
  266. } {1 0 4 2 1 9 3 1 16 10}
  267. do_test where-5.4 {
  268. count {
  269. SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
  270. }
  271. } {1 0 4 2 1 9 3 1 16 99}
  272. do_test where-5.5 {
  273. count {
  274. SELECT * FROM t1 WHERE rowid IN
  275. (select rowid from t1 where rowid IN (-1,2,4))
  276. ORDER BY 1;
  277. }
  278. } {2 1 9 4 2 25 1}
  279. do_test where-5.6 {
  280. count {
  281. SELECT * FROM t1 WHERE rowid+0 IN
  282. (select rowid from t1 where rowid IN (-1,2,4))
  283. ORDER BY 1;
  284. }
  285. } {2 1 9 4 2 25 99}
  286. do_test where-5.7 {
  287. count {
  288. SELECT * FROM t1 WHERE w IN
  289. (select rowid from t1 where rowid IN (-1,2,4))
  290. ORDER BY 1;
  291. }
  292. } {2 1 9 4 2 25 7}
  293. do_test where-5.8 {
  294. count {
  295. SELECT * FROM t1 WHERE w+0 IN
  296. (select rowid from t1 where rowid IN (-1,2,4))
  297. ORDER BY 1;
  298. }
  299. } {2 1 9 4 2 25 99}
  300. do_test where-5.9 {
  301. count {
  302. SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
  303. }
  304. } {2 1 9 3 1 16 6}
  305. do_test where-5.10 {
  306. count {
  307. SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
  308. }
  309. } {2 1 9 3 1 16 99}
  310. do_test where-5.11 {
  311. count {
  312. SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
  313. }
  314. } {79 6 6400 89 6 8100 99}
  315. do_test where-5.12 {
  316. count {
  317. SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
  318. }
  319. } {79 6 6400 89 6 8100 74}
  320. do_test where-5.13 {
  321. count {
  322. SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
  323. }
  324. } {2 1 9 3 1 16 6}
  325. do_test where-5.14 {
  326. count {
  327. SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
  328. }
  329. } {2 1 9 6}
  330. finish_test