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