sl@0: # 2001 September 15
sl@0: #
sl@0: # The author disclaims copyright to this source code.  In place of
sl@0: # a legal notice, here is a blessing:
sl@0: #
sl@0: #    May you do good and not evil.
sl@0: #    May you find forgiveness for yourself and forgive others.
sl@0: #    May you share freely, never taking more than you give.
sl@0: #
sl@0: #***********************************************************************
sl@0: # This file implements regression tests for SQLite library.
sl@0: #
sl@0: # This file implements tests for proper treatment of the special
sl@0: # value NULL.
sl@0: #
sl@0: 
sl@0: set testdir [file dirname $argv0]
sl@0: source $testdir/tester.tcl
sl@0: 
sl@0: # Create a table and some data to work with.
sl@0: #
sl@0: do_test null-1.0 {
sl@0:   execsql {
sl@0:     begin;
sl@0:     create table t1(a,b,c);
sl@0:     insert into t1 values(1,0,0);
sl@0:     insert into t1 values(2,0,1);
sl@0:     insert into t1 values(3,1,0);
sl@0:     insert into t1 values(4,1,1);
sl@0:     insert into t1 values(5,null,0);
sl@0:     insert into t1 values(6,null,1);
sl@0:     insert into t1 values(7,null,null);
sl@0:     commit;
sl@0:     select * from t1;
sl@0:   }
sl@0: } {1 0 0 2 0 1 3 1 0 4 1 1 5 {} 0 6 {} 1 7 {} {}}
sl@0: 
sl@0: # Check for how arithmetic expressions handle NULL
sl@0: #
sl@0: do_test null-1.1 {
sl@0:   execsql {
sl@0:     select ifnull(a+b,99) from t1;
sl@0:   }
sl@0: } {1 2 4 5 99 99 99}
sl@0: do_test null-1.2 {
sl@0:   execsql {
sl@0:     select ifnull(b*c,99) from t1;
sl@0:   }
sl@0: } {0 0 0 1 99 99 99}
sl@0: 
sl@0: # Check to see how the CASE expression handles NULL values.  The
sl@0: # first WHEN for which the test expression is TRUE is selected.
sl@0: # FALSE and UNKNOWN test expressions are skipped.
sl@0: #
sl@0: do_test null-2.1 {
sl@0:   execsql {
sl@0:     select ifnull(case when b<>0 then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {0 0 1 1 0 0 0}
sl@0: do_test null-2.2 {
sl@0:   execsql {
sl@0:     select ifnull(case when not b<>0 then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {1 1 0 0 0 0 0}
sl@0: do_test null-2.3 {
sl@0:   execsql {
sl@0:     select ifnull(case when b<>0 and c<>0 then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {0 0 0 1 0 0 0}
sl@0: do_test null-2.4 {
sl@0:   execsql {
sl@0:     select ifnull(case when not (b<>0 and c<>0) then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {1 1 1 0 1 0 0}
sl@0: do_test null-2.5 {
sl@0:   execsql {
sl@0:     select ifnull(case when b<>0 or c<>0 then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {0 1 1 1 0 1 0}
sl@0: do_test null-2.6 {
sl@0:   execsql {
sl@0:     select ifnull(case when not (b<>0 or c<>0) then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {1 0 0 0 0 0 0}
sl@0: do_test null-2.7 {
sl@0:   execsql {
sl@0:     select ifnull(case b when c then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {1 0 0 1 0 0 0}
sl@0: do_test null-2.8 {
sl@0:   execsql {
sl@0:     select ifnull(case c when b then 1 else 0 end, 99) from t1;
sl@0:   }
sl@0: } {1 0 0 1 0 0 0}
sl@0: 
sl@0: # Check to see that NULL values are ignored in aggregate functions.
sl@0: #
sl@0: do_test null-3.1 {
sl@0:   execsql {
sl@0:     select count(*), count(b), count(c), sum(b), sum(c), 
sl@0:            avg(b), avg(c), min(b), max(b) from t1;
sl@0:   }
sl@0: } {7 4 6 2 3 0.5 0.5 0 1}
sl@0: 
sl@0: # The sum of zero entries is a NULL, but the total of zero entries is 0.
sl@0: #
sl@0: do_test null-3.2 {
sl@0:   execsql {
sl@0:     SELECT sum(b), total(b) FROM t1 WHERE b<0
sl@0:   }
sl@0: } {{} 0.0}
sl@0: 
sl@0: # Check to see how WHERE clauses handle NULL values.  A NULL value
sl@0: # is the same as UNKNOWN.  The WHERE clause should only select those
sl@0: # rows that are TRUE.  FALSE and UNKNOWN rows are rejected.
sl@0: #
sl@0: do_test null-4.1 {
sl@0:   execsql {
sl@0:     select a from t1 where b<10
sl@0:   }
sl@0: } {1 2 3 4}
sl@0: do_test null-4.2 {
sl@0:   execsql {
sl@0:     select a from t1 where not b>10
sl@0:   }
sl@0: } {1 2 3 4}
sl@0: do_test null-4.3 {
sl@0:   execsql {
sl@0:     select a from t1 where b<10 or c=1;
sl@0:   }
sl@0: } {1 2 3 4 6}
sl@0: do_test null-4.4 {
sl@0:   execsql {
sl@0:     select a from t1 where b<10 and c=1;
sl@0:   }
sl@0: } {2 4}
sl@0: do_test null-4.5 {
sl@0:   execsql {
sl@0:     select a from t1 where not (b<10 and c=1);
sl@0:   }
sl@0: } {1 3 5}
sl@0: 
sl@0: # The DISTINCT keyword on a SELECT statement should treat NULL values
sl@0: # as distinct
sl@0: #
sl@0: do_test null-5.1 {
sl@0:   execsql {
sl@0:     select distinct b from t1 order by b;
sl@0:   }
sl@0: } {{} 0 1}
sl@0: 
sl@0: # A UNION to two queries should treat NULL values
sl@0: # as distinct.
sl@0: #
sl@0: # (Later:)  We also take this opportunity to test the ability
sl@0: # of an ORDER BY clause to bind to either SELECT of a UNION.
sl@0: # The left-most SELECT is preferred.  In standard SQL, only
sl@0: # the left SELECT can be used.  The ability to match an ORDER
sl@0: # BY term to the right SELECT is an SQLite extension.
sl@0: #
sl@0: ifcapable compound {
sl@0:   do_test null-6.1 {
sl@0:     execsql {
sl@0:       select b from t1 union select c from t1 order by b;
sl@0:     }
sl@0:   } {{} 0 1}
sl@0:   do_test null-6.2 {
sl@0:     execsql {
sl@0:       select b from t1 union select c from t1 order by 1;
sl@0:     }
sl@0:   } {{} 0 1}
sl@0:   do_test null-6.3 {
sl@0:     execsql {
sl@0:       select b from t1 union select c from t1 order by t1.b;
sl@0:     }
sl@0:   } {{} 0 1}
sl@0:   do_test null-6.4 {
sl@0:     execsql {
sl@0:       select b from t1 union select c from t1 order by main.t1.b;
sl@0:     }
sl@0:   } {{} 0 1}
sl@0:   do_test null-6.5 {
sl@0:     catchsql {
sl@0:       select b from t1 union select c from t1 order by t1.a;
sl@0:     }
sl@0:   } {1 {1st ORDER BY term does not match any column in the result set}}
sl@0:   do_test null-6.6 {
sl@0:     catchsql {
sl@0:       select b from t1 union select c from t1 order by main.t1.a;
sl@0:     }
sl@0:   } {1 {1st ORDER BY term does not match any column in the result set}}
sl@0: } ;# ifcapable compound
sl@0: 
sl@0: # The UNIQUE constraint only applies to non-null values
sl@0: #
sl@0: ifcapable conflict {
sl@0: do_test null-7.1 {
sl@0:     execsql {
sl@0:       create table t2(a, b unique on conflict ignore);
sl@0:       insert into t2 values(1,1);
sl@0:       insert into t2 values(2,null);
sl@0:       insert into t2 values(3,null);
sl@0:       insert into t2 values(4,1);
sl@0:       select a from t2;
sl@0:     }
sl@0:   } {1 2 3}
sl@0:   do_test null-7.2 {
sl@0:     execsql {
sl@0:       create table t3(a, b, c, unique(b,c) on conflict ignore);
sl@0:       insert into t3 values(1,1,1);
sl@0:       insert into t3 values(2,null,1);
sl@0:       insert into t3 values(3,null,1);
sl@0:       insert into t3 values(4,1,1);
sl@0:       select a from t3;
sl@0:     }
sl@0:   } {1 2 3}
sl@0: }
sl@0: 
sl@0: # Ticket #461 - Make sure nulls are handled correctly when doing a
sl@0: # lookup using an index.
sl@0: #
sl@0: do_test null-8.1 {
sl@0:   execsql {
sl@0:     CREATE TABLE t4(x,y);
sl@0:     INSERT INTO t4 VALUES(1,11);
sl@0:     INSERT INTO t4 VALUES(2,NULL);
sl@0:     SELECT x FROM t4 WHERE y=NULL;
sl@0:   }
sl@0: } {}
sl@0: ifcapable subquery {
sl@0:   do_test null-8.2 {
sl@0:     execsql {
sl@0:       SELECT x FROM t4 WHERE y IN (33,NULL);
sl@0:     }
sl@0:   } {}
sl@0: }
sl@0: do_test null-8.3 {
sl@0:   execsql {
sl@0:     SELECT x FROM t4 WHERE y<33 ORDER BY x;
sl@0:   }
sl@0: } {1}
sl@0: do_test null-8.4 {
sl@0:   execsql {
sl@0:     SELECT x FROM t4 WHERE y>6 ORDER BY x;
sl@0:   }
sl@0: } {1}
sl@0: do_test null-8.5 {
sl@0:   execsql {
sl@0:     SELECT x FROM t4 WHERE y!=33 ORDER BY x;
sl@0:   }
sl@0: } {1}
sl@0: do_test null-8.11 {
sl@0:   execsql {
sl@0:     CREATE INDEX t4i1 ON t4(y);
sl@0:     SELECT x FROM t4 WHERE y=NULL;
sl@0:   }
sl@0: } {}
sl@0: ifcapable subquery {
sl@0:   do_test null-8.12 {
sl@0:     execsql {
sl@0:       SELECT x FROM t4 WHERE y IN (33,NULL);
sl@0:     }
sl@0:   } {}
sl@0: }
sl@0: do_test null-8.13 {
sl@0:   execsql {
sl@0:     SELECT x FROM t4 WHERE y<33 ORDER BY x;
sl@0:   }
sl@0: } {1}
sl@0: do_test null-8.14 {
sl@0:   execsql {
sl@0:     SELECT x FROM t4 WHERE y>6 ORDER BY x;
sl@0:   }
sl@0: } {1}
sl@0: do_test null-8.15 {
sl@0:   execsql {
sl@0:     SELECT x FROM t4 WHERE y!=33 ORDER BY x;
sl@0:   }
sl@0: } {1}
sl@0: 
sl@0: 
sl@0: 
sl@0: finish_test