drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 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 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Create a table and some data to work with. |
| 21 | # |
| 22 | do_test null-1.0 { |
| 23 | execsql { |
| 24 | begin; |
| 25 | create table t1(a,b,c); |
| 26 | insert into t1 values(1,0,0); |
| 27 | insert into t1 values(2,0,1); |
| 28 | insert into t1 values(3,1,0); |
| 29 | insert into t1 values(4,1,1); |
| 30 | insert into t1 values(5,null,0); |
| 31 | insert into t1 values(6,null,1); |
| 32 | insert into t1 values(7,null,null); |
| 33 | commit; |
| 34 | select * from t1; |
| 35 | } |
| 36 | } {1 0 0 2 0 1 3 1 0 4 1 1 5 {} 0 6 {} 1 7 {} {}} |
| 37 | |
| 38 | # Check for how arithmetic expressions handle NULL |
| 39 | # |
| 40 | do_test null-1.1 { |
| 41 | execsql { |
| 42 | select ifnull(a+b,99) from t1; |
| 43 | } |
| 44 | } {1 2 4 5 99 99 99} |
| 45 | do_test null-1.2 { |
| 46 | execsql { |
| 47 | select ifnull(b*c,99) from t1; |
| 48 | } |
drh | 1288c95 | 2002-06-01 21:41:10 +0000 | [diff] [blame] | 49 | } {0 0 0 1 99 99 99} |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 50 | |
| 51 | # Check to see how the CASE expression handles NULL values. The |
| 52 | # first WHEN for which the test expression is TRUE is selected. |
| 53 | # FALSE and UNKNOWN test expressions are skipped. |
| 54 | # |
| 55 | do_test null-2.1 { |
| 56 | execsql { |
| 57 | select ifnull(case when b<>0 then 1 else 0 end, 99) from t1; |
| 58 | } |
| 59 | } {0 0 1 1 0 0 0} |
| 60 | do_test null-2.2 { |
| 61 | execsql { |
| 62 | select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1; |
| 63 | } |
| 64 | } {1 1 0 0 0 0 0} |
| 65 | do_test null-2.3 { |
| 66 | execsql { |
| 67 | select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1; |
| 68 | } |
| 69 | } {0 0 0 1 0 0 0} |
| 70 | do_test null-2.4 { |
| 71 | execsql { |
| 72 | select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1; |
| 73 | } |
| 74 | } {1 1 1 0 1 0 0} |
| 75 | do_test null-2.5 { |
| 76 | execsql { |
| 77 | select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1; |
| 78 | } |
| 79 | } {0 1 1 1 0 1 0} |
| 80 | do_test null-2.6 { |
| 81 | execsql { |
| 82 | select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1; |
| 83 | } |
| 84 | } {1 0 0 0 0 0 0} |
| 85 | do_test null-2.7 { |
| 86 | execsql { |
| 87 | select ifnull(case b when c then 1 else 0 end, 99) from t1; |
| 88 | } |
| 89 | } {1 0 0 1 0 0 0} |
| 90 | do_test null-2.8 { |
| 91 | execsql { |
| 92 | select ifnull(case c when b then 1 else 0 end, 99) from t1; |
| 93 | } |
| 94 | } {1 0 0 1 0 0 0} |
| 95 | |
| 96 | # Check to see that NULL values are ignored in aggregate functions. |
| 97 | # |
| 98 | do_test null-3.1 { |
| 99 | execsql { |
| 100 | select count(*), count(b), count(c), sum(b), sum(c), |
| 101 | avg(b), avg(c), min(b), max(b) from t1; |
| 102 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 103 | } {7 4 6 2.0 3.0 0.5 0.5 0 1} |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 104 | |
| 105 | # Check to see how WHERE clauses handle NULL values. A NULL value |
| 106 | # is the same as UNKNOWN. The WHERE clause should only select those |
| 107 | # rows that are TRUE. FALSE and UNKNOWN rows are rejected. |
| 108 | # |
| 109 | do_test null-4.1 { |
| 110 | execsql { |
| 111 | select a from t1 where b<10 |
| 112 | } |
| 113 | } {1 2 3 4} |
| 114 | do_test null-4.2 { |
| 115 | execsql { |
| 116 | select a from t1 where not b>10 |
| 117 | } |
| 118 | } {1 2 3 4} |
| 119 | do_test null-4.3 { |
| 120 | execsql { |
| 121 | select a from t1 where b<10 or c=1; |
| 122 | } |
| 123 | } {1 2 3 4 6} |
| 124 | do_test null-4.4 { |
| 125 | execsql { |
| 126 | select a from t1 where b<10 and c=1; |
| 127 | } |
| 128 | } {2 4} |
| 129 | do_test null-4.5 { |
| 130 | execsql { |
| 131 | select a from t1 where not (b<10 and c=1); |
| 132 | } |
| 133 | } {1 3 5} |
| 134 | |
| 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 | |
| 144 | # A UNION to two queries should treat NULL values |
| 145 | # as distinct |
| 146 | # |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 147 | ifcapable compound { |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 148 | do_test null-6.1 { |
| 149 | execsql { |
| 150 | select b from t1 union select c from t1 order by c; |
| 151 | } |
| 152 | } {{} 0 1} |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 153 | } ;# ifcapable compound |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 154 | |
| 155 | # The UNIQUE constraint only applies to non-null values |
| 156 | # |
| 157 | do_test null-7.1 { |
| 158 | execsql { |
| 159 | create table t2(a, b unique on conflict ignore); |
| 160 | insert into t2 values(1,1); |
| 161 | insert into t2 values(2,null); |
| 162 | insert into t2 values(3,null); |
| 163 | insert into t2 values(4,1); |
| 164 | select a from t2; |
| 165 | } |
| 166 | } {1 2 3} |
| 167 | do_test null-7.2 { |
| 168 | execsql { |
| 169 | create table t3(a, b, c, unique(b,c) on conflict ignore); |
| 170 | insert into t3 values(1,1,1); |
| 171 | insert into t3 values(2,null,1); |
| 172 | insert into t3 values(3,null,1); |
| 173 | insert into t3 values(4,1,1); |
| 174 | select a from t3; |
| 175 | } |
| 176 | } {1 2 3} |
| 177 | |
drh | 562528c | 2003-09-27 00:41:27 +0000 | [diff] [blame] | 178 | # Ticket #461 - Make sure nulls are handled correctly when doing a |
| 179 | # lookup using an index. |
| 180 | # |
| 181 | do_test null-8.1 { |
| 182 | execsql { |
| 183 | CREATE TABLE t4(x,y); |
| 184 | INSERT INTO t4 VALUES(1,11); |
| 185 | INSERT INTO t4 VALUES(2,NULL); |
| 186 | SELECT x FROM t4 WHERE y=NULL; |
| 187 | } |
| 188 | } {} |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame^] | 189 | ifcapable subquery { |
| 190 | do_test null-8.2 { |
| 191 | execsql { |
| 192 | SELECT x FROM t4 WHERE y IN (33,NULL); |
| 193 | } |
| 194 | } {} |
| 195 | } |
drh | 562528c | 2003-09-27 00:41:27 +0000 | [diff] [blame] | 196 | do_test null-8.3 { |
| 197 | execsql { |
| 198 | SELECT x FROM t4 WHERE y<33 ORDER BY x; |
| 199 | } |
| 200 | } {1} |
| 201 | do_test null-8.4 { |
| 202 | execsql { |
| 203 | SELECT x FROM t4 WHERE y>6 ORDER BY x; |
| 204 | } |
| 205 | } {1} |
| 206 | do_test null-8.5 { |
| 207 | execsql { |
| 208 | SELECT x FROM t4 WHERE y!=33 ORDER BY x; |
| 209 | } |
| 210 | } {1} |
| 211 | do_test null-8.11 { |
| 212 | execsql { |
| 213 | CREATE INDEX t4i1 ON t4(y); |
| 214 | SELECT x FROM t4 WHERE y=NULL; |
| 215 | } |
| 216 | } {} |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame^] | 217 | ifcapable subquery { |
| 218 | do_test null-8.12 { |
| 219 | execsql { |
| 220 | SELECT x FROM t4 WHERE y IN (33,NULL); |
| 221 | } |
| 222 | } {} |
| 223 | } |
drh | 562528c | 2003-09-27 00:41:27 +0000 | [diff] [blame] | 224 | do_test null-8.13 { |
| 225 | execsql { |
| 226 | SELECT x FROM t4 WHERE y<33 ORDER BY x; |
| 227 | } |
| 228 | } {1} |
| 229 | do_test null-8.14 { |
| 230 | execsql { |
| 231 | SELECT x FROM t4 WHERE y>6 ORDER BY x; |
| 232 | } |
| 233 | } {1} |
| 234 | do_test null-8.15 { |
| 235 | execsql { |
| 236 | SELECT x FROM t4 WHERE y!=33 ORDER BY x; |
| 237 | } |
| 238 | } {1} |
| 239 | |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 240 | |
| 241 | |
| 242 | finish_test |