blob: 1f3fe7212d2e6e70ca3f78ef771e38f572a3510e [file] [log] [blame]
drhf570f012002-05-31 15:51:25 +00001# 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
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Create a table and some data to work with.
21#
22do_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#
40do_test null-1.1 {
41 execsql {
42 select ifnull(a+b,99) from t1;
43 }
44} {1 2 4 5 99 99 99}
45do_test null-1.2 {
46 execsql {
47 select ifnull(b*c,99) from t1;
48 }
49} {0 0 0 1 0 99 99}
50do_test null-1.2.1 {
51 execsql {
52 select ifnull(c*b,99) from t1;
53 }
54} {0 0 0 1 0 99 99}
55
56# Check to see how the CASE expression handles NULL values. The
57# first WHEN for which the test expression is TRUE is selected.
58# FALSE and UNKNOWN test expressions are skipped.
59#
60do_test null-2.1 {
61 execsql {
62 select ifnull(case when b<>0 then 1 else 0 end, 99) from t1;
63 }
64} {0 0 1 1 0 0 0}
65do_test null-2.2 {
66 execsql {
67 select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1;
68 }
69} {1 1 0 0 0 0 0}
70do_test null-2.3 {
71 execsql {
72 select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1;
73 }
74} {0 0 0 1 0 0 0}
75do_test null-2.4 {
76 execsql {
77 select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1;
78 }
79} {1 1 1 0 1 0 0}
80do_test null-2.5 {
81 execsql {
82 select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1;
83 }
84} {0 1 1 1 0 1 0}
85do_test null-2.6 {
86 execsql {
87 select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1;
88 }
89} {1 0 0 0 0 0 0}
90do_test null-2.7 {
91 execsql {
92 select ifnull(case b when c then 1 else 0 end, 99) from t1;
93 }
94} {1 0 0 1 0 0 0}
95do_test null-2.8 {
96 execsql {
97 select ifnull(case c when b then 1 else 0 end, 99) from t1;
98 }
99} {1 0 0 1 0 0 0}
100
101# Check to see that NULL values are ignored in aggregate functions.
102#
103do_test null-3.1 {
104 execsql {
105 select count(*), count(b), count(c), sum(b), sum(c),
106 avg(b), avg(c), min(b), max(b) from t1;
107 }
108} {7 4 6 2 3 0.5 0.5 0 1}
109
110# Check to see how WHERE clauses handle NULL values. A NULL value
111# is the same as UNKNOWN. The WHERE clause should only select those
112# rows that are TRUE. FALSE and UNKNOWN rows are rejected.
113#
114do_test null-4.1 {
115 execsql {
116 select a from t1 where b<10
117 }
118} {1 2 3 4}
119do_test null-4.2 {
120 execsql {
121 select a from t1 where not b>10
122 }
123} {1 2 3 4}
124do_test null-4.3 {
125 execsql {
126 select a from t1 where b<10 or c=1;
127 }
128} {1 2 3 4 6}
129do_test null-4.4 {
130 execsql {
131 select a from t1 where b<10 and c=1;
132 }
133} {2 4}
134do_test null-4.5 {
135 execsql {
136 select a from t1 where not (b<10 and c=1);
137 }
138} {1 3 5}
139
140# The DISTINCT keyword on a SELECT statement should treat NULL values
141# as distinct
142#
143do_test null-5.1 {
144 execsql {
145 select distinct b from t1 order by b;
146 }
147} {{} 0 1}
148
149# A UNION to two queries should treat NULL values
150# as distinct
151#
152do_test null-6.1 {
153 execsql {
154 select b from t1 union select c from t1 order by c;
155 }
156} {{} 0 1}
157
158# The UNIQUE constraint only applies to non-null values
159#
160do_test null-7.1 {
161 execsql {
162 create table t2(a, b unique on conflict ignore);
163 insert into t2 values(1,1);
164 insert into t2 values(2,null);
165 insert into t2 values(3,null);
166 insert into t2 values(4,1);
167 select a from t2;
168 }
169} {1 2 3}
170do_test null-7.2 {
171 execsql {
172 create table t3(a, b, c, unique(b,c) on conflict ignore);
173 insert into t3 values(1,1,1);
174 insert into t3 values(2,null,1);
175 insert into t3 values(3,null,1);
176 insert into t3 values(4,1,1);
177 select a from t3;
178 }
179} {1 2 3}
180
181
182
183finish_test