2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library.
16 # This file implements tests for proper treatment of the special
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Create a table and some data to work with.
28 create table t1(a,b,c);
29 insert into t1 values(1,0,0);
30 insert into t1 values(2,0,1);
31 insert into t1 values(3,1,0);
32 insert into t1 values(4,1,1);
33 insert into t1 values(5,null,0);
34 insert into t1 values(6,null,1);
35 insert into t1 values(7,null,null);
39 } {1 0 0 2 0 1 3 1 0 4 1 1 5 {} 0 6 {} 1 7 {} {}}
41 # Check for how arithmetic expressions handle NULL
45 select ifnull(a+b,99) from t1;
50 select ifnull(b*c,99) from t1;
54 # Check to see how the CASE expression handles NULL values. The
55 # first WHEN for which the test expression is TRUE is selected.
56 # FALSE and UNKNOWN test expressions are skipped.
60 select ifnull(case when b<>0 then 1 else 0 end, 99) from t1;
65 select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1;
70 select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1;
75 select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1;
80 select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1;
85 select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1;
90 select ifnull(case b when c then 1 else 0 end, 99) from t1;
95 select ifnull(case c when b then 1 else 0 end, 99) from t1;
99 # Check to see that NULL values are ignored in aggregate functions.
100 # (except for min().)
104 select count(*), count(b), count(c), sum(b), sum(c),
105 avg(b), avg(c), min(b), max(b) from t1;
107 } {7 4 6 2 3 0.5 0.5 0 1}
109 # Check to see how WHERE clauses handle NULL values. A NULL value
110 # is the same as UNKNOWN. The WHERE clause should only select those
111 # rows that are TRUE. FALSE and UNKNOWN rows are rejected.
115 select a from t1 where b<10
120 select a from t1 where not b>10
125 select a from t1 where b<10 or c=1;
130 select a from t1 where b<10 and c=1;
135 select a from t1 where not (b<10 and c=1);
139 # The DISTINCT keyword on a SELECT statement should treat NULL values
144 select distinct b from t1 order by b;
148 # A UNION to two queries should treat NULL values
153 select b from t1 union select c from t1 order by c;
157 # The UNIQUE constraint only applies to non-null values
161 create table t2(a, b unique on conflict ignore);
162 insert into t2 values(1,1);
163 insert into t2 values(2,null);
164 insert into t2 values(3,null);
165 insert into t2 values(4,1);
171 create table t3(a, b, c, unique(b,c) on conflict ignore);
172 insert into t3 values(1,1,1);
173 insert into t3 values(2,null,1);
174 insert into t3 values(3,null,1);
175 insert into t3 values(4,1,1);
180 # Ticket #461 - Make sure nulls are handled correctly when doing a
181 # lookup using an index.
185 CREATE TABLE t4(x,y);
186 INSERT INTO t4 VALUES(1,11);
187 INSERT INTO t4 VALUES(2,NULL);
188 SELECT x FROM t4 WHERE y=NULL;
193 SELECT x FROM t4 WHERE y IN (33,NULL);
198 SELECT x FROM t4 WHERE y<33 ORDER BY x;
203 SELECT x FROM t4 WHERE y>6 ORDER BY x;
208 SELECT x FROM t4 WHERE y!=33 ORDER BY x;
213 CREATE INDEX t4i1 ON t4(y);
214 SELECT x FROM t4 WHERE y=NULL;
219 SELECT x FROM t4 WHERE y IN (33,NULL);
224 SELECT x FROM t4 WHERE y<33 ORDER BY x;
229 SELECT x FROM t4 WHERE y>6 ORDER BY x;
234 SELECT x FROM t4 WHERE y!=33 ORDER BY x;