null.test 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. # Check to see how WHERE clauses handle NULL values. A NULL value
  100. # is the same as UNKNOWN. The WHERE clause should only select those
  101. # rows that are TRUE. FALSE and UNKNOWN rows are rejected.
  102. #
  103. do_test null-4.1 {
  104. execsql {
  105. select a from t1 where b<10
  106. }
  107. } {1 2 3 4}
  108. do_test null-4.2 {
  109. execsql {
  110. select a from t1 where not b>10
  111. }
  112. } {1 2 3 4}
  113. do_test null-4.3 {
  114. execsql {
  115. select a from t1 where b<10 or c=1;
  116. }
  117. } {1 2 3 4 6}
  118. do_test null-4.4 {
  119. execsql {
  120. select a from t1 where b<10 and c=1;
  121. }
  122. } {2 4}
  123. do_test null-4.5 {
  124. execsql {
  125. select a from t1 where not (b<10 and c=1);
  126. }
  127. } {1 3 5}
  128. # The DISTINCT keyword on a SELECT statement should treat NULL values
  129. # as distinct
  130. #
  131. do_test null-5.1 {
  132. execsql {
  133. select distinct b from t1 order by b;
  134. }
  135. } {{} 0 1}
  136. # A UNION to two queries should treat NULL values
  137. # as distinct
  138. #
  139. do_test null-6.1 {
  140. execsql {
  141. select b from t1 union select c from t1 order by c;
  142. }
  143. } {{} 0 1}
  144. # The UNIQUE constraint only applies to non-null values
  145. #
  146. do_test null-7.1 {
  147. execsql {
  148. create table t2(a, b unique on conflict ignore);
  149. insert into t2 values(1,1);
  150. insert into t2 values(2,null);
  151. insert into t2 values(3,null);
  152. insert into t2 values(4,1);
  153. select a from t2;
  154. }
  155. } {1 2 3}
  156. do_test null-7.2 {
  157. execsql {
  158. create table t3(a, b, c, unique(b,c) on conflict ignore);
  159. insert into t3 values(1,1,1);
  160. insert into t3 values(2,null,1);
  161. insert into t3 values(3,null,1);
  162. insert into t3 values(4,1,1);
  163. select a from t3;
  164. }
  165. } {1 2 3}
  166. finish_test