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