xref: /illumos-gate/usr/src/lib/libsqlite/test/null.test (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
1#
2# 2001 September 15
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.
13#
14# This file implements tests for proper treatment of the special
15# value NULL.
16#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Create a table and some data to work with.
22#
23do_test null-1.0 {
24  execsql {
25    begin;
26    create table t1(a,b,c);
27    insert into t1 values(1,0,0);
28    insert into t1 values(2,0,1);
29    insert into t1 values(3,1,0);
30    insert into t1 values(4,1,1);
31    insert into t1 values(5,null,0);
32    insert into t1 values(6,null,1);
33    insert into t1 values(7,null,null);
34    commit;
35    select * from t1;
36  }
37} {1 0 0 2 0 1 3 1 0 4 1 1 5 {} 0 6 {} 1 7 {} {}}
38
39# Check for how arithmetic expressions handle NULL
40#
41do_test null-1.1 {
42  execsql {
43    select ifnull(a+b,99) from t1;
44  }
45} {1 2 4 5 99 99 99}
46do_test null-1.2 {
47  execsql {
48    select ifnull(b*c,99) from t1;
49  }
50} {0 0 0 1 99 99 99}
51
52# Check to see how the CASE expression handles NULL values.  The
53# first WHEN for which the test expression is TRUE is selected.
54# FALSE and UNKNOWN test expressions are skipped.
55#
56do_test null-2.1 {
57  execsql {
58    select ifnull(case when b<>0 then 1 else 0 end, 99) from t1;
59  }
60} {0 0 1 1 0 0 0}
61do_test null-2.2 {
62  execsql {
63    select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1;
64  }
65} {1 1 0 0 0 0 0}
66do_test null-2.3 {
67  execsql {
68    select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1;
69  }
70} {0 0 0 1 0 0 0}
71do_test null-2.4 {
72  execsql {
73    select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1;
74  }
75} {1 1 1 0 1 0 0}
76do_test null-2.5 {
77  execsql {
78    select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1;
79  }
80} {0 1 1 1 0 1 0}
81do_test null-2.6 {
82  execsql {
83    select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1;
84  }
85} {1 0 0 0 0 0 0}
86do_test null-2.7 {
87  execsql {
88    select ifnull(case b when c then 1 else 0 end, 99) from t1;
89  }
90} {1 0 0 1 0 0 0}
91do_test null-2.8 {
92  execsql {
93    select ifnull(case c when b then 1 else 0 end, 99) from t1;
94  }
95} {1 0 0 1 0 0 0}
96
97# Check to see that NULL values are ignored in aggregate functions.
98# (except for min().)
99#
100do_test null-3.1 {
101  execsql {
102    select count(*), count(b), count(c), sum(b), sum(c),
103           avg(b), avg(c), min(b), max(b) from t1;
104  }
105} {7 4 6 2 3 0.5 0.5 0 1}
106
107# Check to see how WHERE clauses handle NULL values.  A NULL value
108# is the same as UNKNOWN.  The WHERE clause should only select those
109# rows that are TRUE.  FALSE and UNKNOWN rows are rejected.
110#
111do_test null-4.1 {
112  execsql {
113    select a from t1 where b<10
114  }
115} {1 2 3 4}
116do_test null-4.2 {
117  execsql {
118    select a from t1 where not b>10
119  }
120} {1 2 3 4}
121do_test null-4.3 {
122  execsql {
123    select a from t1 where b<10 or c=1;
124  }
125} {1 2 3 4 6}
126do_test null-4.4 {
127  execsql {
128    select a from t1 where b<10 and c=1;
129  }
130} {2 4}
131do_test null-4.5 {
132  execsql {
133    select a from t1 where not (b<10 and c=1);
134  }
135} {1 3 5}
136
137# The DISTINCT keyword on a SELECT statement should treat NULL values
138# as distinct
139#
140do_test null-5.1 {
141  execsql {
142    select distinct b from t1 order by b;
143  }
144} {{} 0 1}
145
146# A UNION to two queries should treat NULL values
147# as distinct
148#
149do_test null-6.1 {
150  execsql {
151    select b from t1 union select c from t1 order by c;
152  }
153} {{} 0 1}
154
155# The UNIQUE constraint only applies to non-null values
156#
157do_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}
167do_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
178# Ticket #461 - Make sure nulls are handled correctly when doing a
179# lookup using an index.
180#
181do_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} {}
189do_test null-8.2 {
190  execsql {
191    SELECT x FROM t4 WHERE y IN (33,NULL);
192  }
193} {}
194do_test null-8.3 {
195  execsql {
196    SELECT x FROM t4 WHERE y<33 ORDER BY x;
197  }
198} {1}
199do_test null-8.4 {
200  execsql {
201    SELECT x FROM t4 WHERE y>6 ORDER BY x;
202  }
203} {1}
204do_test null-8.5 {
205  execsql {
206    SELECT x FROM t4 WHERE y!=33 ORDER BY x;
207  }
208} {1}
209do_test null-8.11 {
210  execsql {
211    CREATE INDEX t4i1 ON t4(y);
212    SELECT x FROM t4 WHERE y=NULL;
213  }
214} {}
215do_test null-8.12 {
216  execsql {
217    SELECT x FROM t4 WHERE y IN (33,NULL);
218  }
219} {}
220do_test null-8.13 {
221  execsql {
222    SELECT x FROM t4 WHERE y<33 ORDER BY x;
223  }
224} {1}
225do_test null-8.14 {
226  execsql {
227    SELECT x FROM t4 WHERE y>6 ORDER BY x;
228  }
229} {1}
230do_test null-8.15 {
231  execsql {
232    SELECT x FROM t4 WHERE y!=33 ORDER BY x;
233  }
234} {1}
235
236
237
238finish_test
239