3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for proper treatment of the special
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Create a table and some data to work with.
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);
36 } {1 0 0 2 0 1 3 1 0 4 1 1 5 {} 0 6 {} 1 7 {} {}}
38 # Check for how arithmetic expressions handle NULL
42 select ifnull(a+b,99) from t1;
47 select ifnull(b*c,99) from t1;
51 # Check to see how the CASE expression handles NULL values. The
52 # first WHEN for which the test expression is TRUE is selected.
53 # FALSE and UNKNOWN test expressions are skipped.
57 select ifnull(case when b<>0 then 1 else 0 end, 99) from t1;
62 select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1;
67 select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1;
72 select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1;
77 select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1;
82 select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1;
87 select ifnull(case b when c then 1 else 0 end, 99) from t1;
92 select ifnull(case c when b then 1 else 0 end, 99) from t1;
96 # Check to see that NULL values are ignored in aggregate functions.
100 select count(*), count(b), count(c), sum(b), sum(c),
101 avg(b), avg(c), min(b), max(b) from t1;
103 } {7 4 6 2 3 0.5 0.5 0 1}
105 # The sum of zero entries is a NULL, but the total of zero entries is 0.
109 SELECT sum(b), total(b) FROM t1 WHERE b<0
113 # Check to see how WHERE clauses handle NULL values. A NULL value
114 # is the same as UNKNOWN. The WHERE clause should only select those
115 # rows that are TRUE. FALSE and UNKNOWN rows are rejected.
119 select a from t1 where b<10
124 select a from t1 where not b>10
129 select a from t1 where b<10 or c=1;
134 select a from t1 where b<10 and c=1;
139 select a from t1 where not (b<10 and c=1);
143 # The DISTINCT keyword on a SELECT statement should treat NULL values
148 select distinct b from t1 order by b;
152 # A UNION to two queries should treat NULL values
155 # (Later:) We also take this opportunity to test the ability
156 # of an ORDER BY clause to bind to either SELECT of a UNION.
157 # The left-most SELECT is preferred. In standard SQL, only
158 # the left SELECT can be used. The ability to match an ORDER
159 # BY term to the right SELECT is an SQLite extension.
164 select b from t1 union select c from t1 order by b;
169 select b from t1 union select c from t1 order by 1;
174 select b from t1 union select c from t1 order by t1.b;
179 select b from t1 union select c from t1 order by main.t1.b;
184 select b from t1 union select c from t1 order by t1.a;
186 } {1 {1st ORDER BY term does not match any column in the result set}}
189 select b from t1 union select c from t1 order by main.t1.a;
191 } {1 {1st ORDER BY term does not match any column in the result set}}
192 } ;# ifcapable compound
194 # The UNIQUE constraint only applies to non-null values
199 create table t2(a, b unique on conflict ignore);
200 insert into t2 values(1,1);
201 insert into t2 values(2,null);
202 insert into t2 values(3,null);
203 insert into t2 values(4,1);
209 create table t3(a, b, c, unique(b,c) on conflict ignore);
210 insert into t3 values(1,1,1);
211 insert into t3 values(2,null,1);
212 insert into t3 values(3,null,1);
213 insert into t3 values(4,1,1);
219 # Ticket #461 - Make sure nulls are handled correctly when doing a
220 # lookup using an index.
224 CREATE TABLE t4(x,y);
225 INSERT INTO t4 VALUES(1,11);
226 INSERT INTO t4 VALUES(2,NULL);
227 SELECT x FROM t4 WHERE y=NULL;
233 SELECT x FROM t4 WHERE y IN (33,NULL);
239 SELECT x FROM t4 WHERE y<33 ORDER BY x;
244 SELECT x FROM t4 WHERE y>6 ORDER BY x;
249 SELECT x FROM t4 WHERE y!=33 ORDER BY x;
254 CREATE INDEX t4i1 ON t4(y);
255 SELECT x FROM t4 WHERE y=NULL;
261 SELECT x FROM t4 WHERE y IN (33,NULL);
267 SELECT x FROM t4 WHERE y<33 ORDER BY x;
272 SELECT x FROM t4 WHERE y>6 ORDER BY x;
277 SELECT x FROM t4 WHERE y!=33 ORDER BY x;