null.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.
  12. #
  13. # This file implements tests for proper treatment of the special
  14. # value NULL.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Create a table and some data to work with.
  19. #
  20. do_test null-1.0 {
  21. execsql {
  22. begin;
  23. create table t1(a,b,c);
  24. insert into t1 values(1,0,0);
  25. insert into t1 values(2,0,1);
  26. insert into t1 values(3,1,0);
  27. insert into t1 values(4,1,1);
  28. insert into t1 values(5,null,0);
  29. insert into t1 values(6,null,1);
  30. insert into t1 values(7,null,null);
  31. commit;
  32. select * from t1;
  33. }
  34. } {1 0 0 2 0 1 3 1 0 4 1 1 5 {} 0 6 {} 1 7 {} {}}
  35. # Check for how arithmetic expressions handle NULL
  36. #
  37. do_test null-1.1 {
  38. execsql {
  39. select ifnull(a+b,99) from t1;
  40. }
  41. } {1 2 4 5 99 99 99}
  42. do_test null-1.2 {
  43. execsql {
  44. select ifnull(b*c,99) from t1;
  45. }
  46. } {0 0 0 1 99 99 99}
  47. # Check to see how the CASE expression handles NULL values. The
  48. # first WHEN for which the test expression is TRUE is selected.
  49. # FALSE and UNKNOWN test expressions are skipped.
  50. #
  51. do_test null-2.1 {
  52. execsql {
  53. select ifnull(case when b<>0 then 1 else 0 end, 99) from t1;
  54. }
  55. } {0 0 1 1 0 0 0}
  56. do_test null-2.2 {
  57. execsql {
  58. select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1;
  59. }
  60. } {1 1 0 0 0 0 0}
  61. do_test null-2.3 {
  62. execsql {
  63. select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1;
  64. }
  65. } {0 0 0 1 0 0 0}
  66. do_test null-2.4 {
  67. execsql {
  68. select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1;
  69. }
  70. } {1 1 1 0 1 0 0}
  71. do_test null-2.5 {
  72. execsql {
  73. select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1;
  74. }
  75. } {0 1 1 1 0 1 0}
  76. do_test null-2.6 {
  77. execsql {
  78. select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1;
  79. }
  80. } {1 0 0 0 0 0 0}
  81. do_test null-2.7 {
  82. execsql {
  83. select ifnull(case b when c then 1 else 0 end, 99) from t1;
  84. }
  85. } {1 0 0 1 0 0 0}
  86. do_test null-2.8 {
  87. execsql {
  88. select ifnull(case c when b then 1 else 0 end, 99) from t1;
  89. }
  90. } {1 0 0 1 0 0 0}
  91. # Check to see that NULL values are ignored in aggregate functions.
  92. #
  93. do_test null-3.1 {
  94. execsql {
  95. select count(*), count(b), count(c), sum(b), sum(c),
  96. avg(b), avg(c), min(b), max(b) from t1;
  97. }
  98. } {7 4 6 2 3 0.5 0.5 0 1}
  99. # The sum of zero entries is a NULL, but the total of zero entries is 0.
  100. #
  101. do_test null-3.2 {
  102. execsql {
  103. SELECT sum(b), total(b) FROM t1 WHERE b<0
  104. }
  105. } {{} 0.0}
  106. # Check to see how WHERE clauses handle NULL values. A NULL value
  107. # is the same as UNKNOWN. The WHERE clause should only select those
  108. # rows that are TRUE. FALSE and UNKNOWN rows are rejected.
  109. #
  110. do_test null-4.1 {
  111. execsql {
  112. select a from t1 where b<10
  113. }
  114. } {1 2 3 4}
  115. do_test null-4.2 {
  116. execsql {
  117. select a from t1 where not b>10
  118. }
  119. } {1 2 3 4}
  120. do_test null-4.3 {
  121. execsql {
  122. select a from t1 where b<10 or c=1;
  123. }
  124. } {1 2 3 4 6}
  125. do_test null-4.4 {
  126. execsql {
  127. select a from t1 where b<10 and c=1;
  128. }
  129. } {2 4}
  130. do_test null-4.5 {
  131. execsql {
  132. select a from t1 where not (b<10 and c=1);
  133. }
  134. } {1 3 5}
  135. # The DISTINCT keyword on a SELECT statement should treat NULL values
  136. # as distinct
  137. #
  138. do_test null-5.1 {
  139. execsql {
  140. select distinct b from t1 order by b;
  141. }
  142. } {{} 0 1}
  143. # A UNION to two queries should treat NULL values
  144. # as distinct.
  145. #
  146. # (Later:) We also take this opportunity to test the ability
  147. # of an ORDER BY clause to bind to either SELECT of a UNION.
  148. # The left-most SELECT is preferred. In standard SQL, only
  149. # the left SELECT can be used. The ability to match an ORDER
  150. # BY term to the right SELECT is an SQLite extension.
  151. #
  152. ifcapable compound {
  153. do_test null-6.1 {
  154. execsql {
  155. select b from t1 union select c from t1 order by b;
  156. }
  157. } {{} 0 1}
  158. do_test null-6.2 {
  159. execsql {
  160. select b from t1 union select c from t1 order by 1;
  161. }
  162. } {{} 0 1}
  163. do_test null-6.3 {
  164. execsql {
  165. select b from t1 union select c from t1 order by t1.b;
  166. }
  167. } {{} 0 1}
  168. do_test null-6.4 {
  169. execsql {
  170. select b from t1 union select c from t1 order by main.t1.b;
  171. }
  172. } {{} 0 1}
  173. do_test null-6.5 {
  174. catchsql {
  175. select b from t1 union select c from t1 order by t1.a;
  176. }
  177. } {1 {1st ORDER BY term does not match any column in the result set}}
  178. do_test null-6.6 {
  179. catchsql {
  180. select b from t1 union select c from t1 order by main.t1.a;
  181. }
  182. } {1 {1st ORDER BY term does not match any column in the result set}}
  183. } ;# ifcapable compound
  184. # The UNIQUE constraint only applies to non-null values
  185. #
  186. ifcapable conflict {
  187. do_test null-7.1 {
  188. execsql {
  189. create table t2(a, b unique on conflict ignore);
  190. insert into t2 values(1,1);
  191. insert into t2 values(2,null);
  192. insert into t2 values(3,null);
  193. insert into t2 values(4,1);
  194. select a from t2;
  195. }
  196. } {1 2 3}
  197. do_test null-7.2 {
  198. execsql {
  199. create table t3(a, b, c, unique(b,c) on conflict ignore);
  200. insert into t3 values(1,1,1);
  201. insert into t3 values(2,null,1);
  202. insert into t3 values(3,null,1);
  203. insert into t3 values(4,1,1);
  204. select a from t3;
  205. }
  206. } {1 2 3}
  207. }
  208. # Ticket #461 - Make sure nulls are handled correctly when doing a
  209. # lookup using an index.
  210. #
  211. do_test null-8.1 {
  212. execsql {
  213. CREATE TABLE t4(x,y);
  214. INSERT INTO t4 VALUES(1,11);
  215. INSERT INTO t4 VALUES(2,NULL);
  216. SELECT x FROM t4 WHERE y=NULL;
  217. }
  218. } {}
  219. ifcapable subquery {
  220. do_test null-8.2 {
  221. execsql {
  222. SELECT x FROM t4 WHERE y IN (33,NULL);
  223. }
  224. } {}
  225. }
  226. do_test null-8.3 {
  227. execsql {
  228. SELECT x FROM t4 WHERE y<33 ORDER BY x;
  229. }
  230. } {1}
  231. do_test null-8.4 {
  232. execsql {
  233. SELECT x FROM t4 WHERE y>6 ORDER BY x;
  234. }
  235. } {1}
  236. do_test null-8.5 {
  237. execsql {
  238. SELECT x FROM t4 WHERE y!=33 ORDER BY x;
  239. }
  240. } {1}
  241. do_test null-8.11 {
  242. execsql {
  243. CREATE INDEX t4i1 ON t4(y);
  244. SELECT x FROM t4 WHERE y=NULL;
  245. }
  246. } {}
  247. ifcapable subquery {
  248. do_test null-8.12 {
  249. execsql {
  250. SELECT x FROM t4 WHERE y IN (33,NULL);
  251. }
  252. } {}
  253. }
  254. do_test null-8.13 {
  255. execsql {
  256. SELECT x FROM t4 WHERE y<33 ORDER BY x;
  257. }
  258. } {1}
  259. do_test null-8.14 {
  260. execsql {
  261. SELECT x FROM t4 WHERE y>6 ORDER BY x;
  262. }
  263. } {1}
  264. do_test null-8.15 {
  265. execsql {
  266. SELECT x FROM t4 WHERE y!=33 ORDER BY x;
  267. }
  268. } {1}
  269. finish_test