PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/sqlite/test/where4.test

#
Unknown | 273 lines | 255 code | 18 blank | 0 comment | 0 complexity | 23b81eda54f61a213d48098192c4fc88 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # 2006 October 27
  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. # This file was created when support for optimizing IS NULL phrases
  14. # was added. And so the principle purpose of this file is to test
  15. # that IS NULL phrases are correctly optimized. But you can never
  16. # have too many tests, so some other tests are thrown in as well.
  17. #
  18. # $Id: where4.test,v 1.6 2007/12/10 05:03:48 danielk1977 Exp $
  19. set testdir [file dirname $argv0]
  20. source $testdir/tester.tcl
  21. ifcapable !tclvar||!bloblit {
  22. finish_test
  23. return
  24. }
  25. # Build some test data
  26. #
  27. do_test where4-1.0 {
  28. execsql {
  29. CREATE TABLE t1(w, x, y);
  30. CREATE INDEX i1wxy ON t1(w,x,y);
  31. INSERT INTO t1 VALUES(1,2,3);
  32. INSERT INTO t1 VALUES(1,NULL,3);
  33. INSERT INTO t1 VALUES('a','b','c');
  34. INSERT INTO t1 VALUES('a',NULL,'c');
  35. INSERT INTO t1 VALUES(X'78',x'79',x'7a');
  36. INSERT INTO t1 VALUES(X'78',NULL,X'7A');
  37. INSERT INTO t1 VALUES(NULL,NULL,NULL);
  38. SELECT count(*) FROM t1;
  39. }
  40. } {7}
  41. # Do an SQL statement. Append the search count to the end of the result.
  42. #
  43. proc count sql {
  44. set ::sqlite_search_count 0
  45. return [concat [execsql $sql] $::sqlite_search_count]
  46. }
  47. # Verify that queries use an index. We are using the special variable
  48. # "sqlite_search_count" which tallys the number of executions of MoveTo
  49. # and Next operators in the VDBE. By verifing that the search count is
  50. # small we can be assured that indices are being used properly.
  51. #
  52. do_test where4-1.1 {
  53. count {SELECT rowid FROM t1 WHERE w IS NULL}
  54. } {7 2}
  55. do_test where4-1.2 {
  56. count {SELECT rowid FROM t1 WHERE +w IS NULL}
  57. } {7 6}
  58. do_test where4-1.3 {
  59. count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL}
  60. } {2 2}
  61. do_test where4-1.4 {
  62. count {SELECT rowid FROM t1 WHERE w=1 AND +x IS NULL}
  63. } {2 3}
  64. do_test where4-1.5 {
  65. count {SELECT rowid FROM t1 WHERE w=1 AND x>0}
  66. } {1 2}
  67. do_test where4-1.6 {
  68. count {SELECT rowid FROM t1 WHERE w=1 AND x<9}
  69. } {1 3}
  70. do_test where4-1.7 {
  71. count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y=3}
  72. } {2 2}
  73. do_test where4-1.8 {
  74. count {SELECT rowid FROM t1 WHERE w=1 AND x IS NULL AND y>2}
  75. } {2 2}
  76. do_test where4-1.9 {
  77. count {SELECT rowid FROM t1 WHERE w='a' AND x IS NULL AND y='c'}
  78. } {4 2}
  79. do_test where4-1.10 {
  80. count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL}
  81. } {6 2}
  82. do_test where4-1.11 {
  83. count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=123}
  84. } {1}
  85. do_test where4-1.12 {
  86. count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=x'7A'}
  87. } {6 2}
  88. do_test where4-1.13 {
  89. count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL}
  90. } {7 2}
  91. do_test where4-1.14 {
  92. count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y IS NULL}
  93. } {7 2}
  94. do_test where4-1.15 {
  95. count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y<0}
  96. } {2}
  97. do_test where4-1.16 {
  98. count {SELECT rowid FROM t1 WHERE w IS NULL AND x IS NULL AND y>=0}
  99. } {1}
  100. do_test where4-2.1 {
  101. execsql {SELECT rowid FROM t1 ORDER BY w, x, y}
  102. } {7 2 1 4 3 6 5}
  103. do_test where4-2.2 {
  104. execsql {SELECT rowid FROM t1 ORDER BY w DESC, x, y}
  105. } {6 5 4 3 2 1 7}
  106. do_test where4-2.3 {
  107. execsql {SELECT rowid FROM t1 ORDER BY w, x DESC, y}
  108. } {7 1 2 3 4 5 6}
  109. # Ticket #2177
  110. #
  111. # Suppose you have a left join where the right table of the left
  112. # join (the one that can be NULL) has an index on two columns.
  113. # The first indexed column is used in the ON clause of the join.
  114. # The second indexed column is used in the WHERE clause with an IS NULL
  115. # constraint. It is not allowed to use the IS NULL optimization to
  116. # optimize the query because the second column might be NULL because
  117. # the right table did not match - something the index does not know
  118. # about.
  119. #
  120. do_test where4-3.1 {
  121. execsql {
  122. CREATE TABLE t2(a);
  123. INSERT INTO t2 VALUES(1);
  124. INSERT INTO t2 VALUES(2);
  125. INSERT INTO t2 VALUES(3);
  126. CREATE TABLE t3(x,y,UNIQUE(x,y));
  127. INSERT INTO t3 VALUES(1,11);
  128. INSERT INTO t3 VALUES(2,NULL);
  129. SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE +y IS NULL;
  130. }
  131. } {2 2 {} 3 {} {}}
  132. do_test where4-3.2 {
  133. execsql {
  134. SELECT * FROM t2 LEFT JOIN t3 ON a=x WHERE y IS NULL;
  135. }
  136. } {2 2 {} 3 {} {}}
  137. # Ticket #2189. Probably the same bug as #2177.
  138. #
  139. do_test where4-4.1 {
  140. execsql {
  141. CREATE TABLE test(col1 TEXT PRIMARY KEY);
  142. INSERT INTO test(col1) values('a');
  143. INSERT INTO test(col1) values('b');
  144. INSERT INTO test(col1) values('c');
  145. CREATE TABLE test2(col1 TEXT PRIMARY KEY);
  146. INSERT INTO test2(col1) values('a');
  147. INSERT INTO test2(col1) values('b');
  148. INSERT INTO test2(col1) values('c');
  149. SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
  150. WHERE +t2.col1 IS NULL;
  151. }
  152. } {}
  153. do_test where4-4.2 {
  154. execsql {
  155. SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
  156. WHERE t2.col1 IS NULL;
  157. }
  158. } {}
  159. do_test where4-4.3 {
  160. execsql {
  161. SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
  162. WHERE +t1.col1 IS NULL;
  163. }
  164. } {}
  165. do_test where4-4.4 {
  166. execsql {
  167. SELECT * FROM test t1 LEFT OUTER JOIN test2 t2 ON t1.col1 = t2.col1
  168. WHERE t1.col1 IS NULL;
  169. }
  170. } {}
  171. # Ticket #2273. Problems with IN operators and NULLs.
  172. #
  173. ifcapable subquery {
  174. do_test where4-5.1 {
  175. execsql {
  176. CREATE TABLE t4(x,y,z,PRIMARY KEY(x,y));
  177. }
  178. execsql {
  179. SELECT *
  180. FROM t2 LEFT JOIN t4 b1
  181. LEFT JOIN t4 b2 ON b2.x=b1.x AND b2.y IN (b1.y);
  182. }
  183. } {1 {} {} {} {} {} {} 2 {} {} {} {} {} {} 3 {} {} {} {} {} {}}
  184. do_test where4-5.2 {
  185. execsql {
  186. INSERT INTO t4 VALUES(1,1,11);
  187. INSERT INTO t4 VALUES(1,2,12);
  188. INSERT INTO t4 VALUES(1,3,13);
  189. INSERT INTO t4 VALUES(2,2,22);
  190. SELECT rowid FROM t4 WHERE x IN (1,9,2,5) AND y IN (1,3,NULL,2) AND z!=13;
  191. }
  192. } {1 2 4}
  193. do_test where4-5.3 {
  194. execsql {
  195. SELECT rowid FROM t4 WHERE x IN (1,9,NULL,2) AND y IN (1,3,2) AND z!=13;
  196. }
  197. } {1 2 4}
  198. do_test where4-6.1 {
  199. execsql {
  200. CREATE TABLE t5(a,b,c,d,e,f,UNIQUE(a,b,c,d,e,f));
  201. INSERT INTO t5 VALUES(1,1,1,1,1,11111);
  202. INSERT INTO t5 VALUES(2,2,2,2,2,22222);
  203. INSERT INTO t5 VALUES(1,2,3,4,5,12345);
  204. INSERT INTO t5 VALUES(2,3,4,5,6,23456);
  205. }
  206. execsql {
  207. SELECT rowid FROM t5
  208. WHERE a IN (1,9,2) AND b=2 AND c IN (1,2,3,4) AND d>0
  209. }
  210. } {3 2}
  211. do_test where4-6.2 {
  212. execsql {
  213. SELECT rowid FROM t5
  214. WHERE a IN (1,NULL,2) AND b=2 AND c IN (1,2,3,4) AND d>0
  215. }
  216. } {3 2}
  217. do_test where4-7.1 {
  218. execsql {
  219. CREATE TABLE t6(y,z,PRIMARY KEY(y,z));
  220. }
  221. execsql {
  222. SELECT * FROM t6 WHERE y=NULL AND z IN ('hello');
  223. }
  224. } {}
  225. integrity_check {where4-99.0}
  226. do_test where4-7.1 {
  227. execsql {
  228. BEGIN;
  229. CREATE TABLE t8(a, b, c, d);
  230. CREATE INDEX t8_i ON t8(a, b, c);
  231. CREATE TABLE t7(i);
  232. INSERT INTO t7 VALUES(1);
  233. INSERT INTO t7 SELECT i*2 FROM t7;
  234. INSERT INTO t7 SELECT i*2 FROM t7;
  235. INSERT INTO t7 SELECT i*2 FROM t7;
  236. INSERT INTO t7 SELECT i*2 FROM t7;
  237. INSERT INTO t7 SELECT i*2 FROM t7;
  238. INSERT INTO t7 SELECT i*2 FROM t7;
  239. COMMIT;
  240. }
  241. } {}
  242. # At one point the sub-select inside the aggregate sum() function in the
  243. # following query was leaking a couple of stack entries. This query
  244. # runs the SELECT in a loop enough times that an assert() fails. Or rather,
  245. # did fail before the bug was fixed.
  246. #
  247. do_test where4-7.2 {
  248. execsql {
  249. SELECT sum((
  250. SELECT d FROM t8 WHERE a = i AND b = i AND c < NULL
  251. )) FROM t7;
  252. }
  253. } {{}}
  254. }; #ifcapable subquery
  255. finish_test