Index: src/func.c ================================================================== --- src/func.c +++ src/func.c @@ -14,11 +14,11 @@ ** ** There is only one exported symbol in this file - the function ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: func.c,v 1.75 2004/06/21 06:50:28 danielk1977 Exp $ +** $Id: func.c,v 1.76 2004/06/24 00:20:05 danielk1977 Exp $ */ #include #include #include #include @@ -969,12 +969,12 @@ static void minmaxStep(sqlite3_context *context, int argc, sqlite3_value **argv){ int max = 0; int cmp = 0; Mem *pArg = (Mem *)argv[0]; Mem *pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); + if( !pBest ) return; - if( !pBest || SQLITE_NULL==sqlite3_value_type(argv[0]) ) return; if( pBest->flags ){ CollSeq *pColl = sqlite3GetFuncCollSeq(context); /* This step function is used for both the min() and max() aggregates, ** the only difference between the two being that the sense of the ** comparison is inverted. For the max() aggregate, the Index: test/func.test ================================================================== --- test/func.test +++ test/func.test @@ -9,11 +9,11 @@ # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing built-in functions. # -# $Id: func.test,v 1.25 2004/06/23 01:05:27 danielk1977 Exp $ +# $Id: func.test,v 1.26 2004/06/24 00:20:05 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Create a table to work with. @@ -249,11 +249,11 @@ # do_test func-8.1 { execsql { SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2; } -} {68236 3 22745.33 1 67890 5} +} {68236 3 22745.33 {} 67890 5} do_test func-8.2 { execsql { SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2; } } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} @@ -260,11 +260,16 @@ do_test func-8.3 { execsql { CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC; SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; } -} {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} +} {{}} +do_test func-8.4 { + execsql { + SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; + } +} {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} # How do you test the random() function in a meaningful, deterministic way? # do_test func-9.1 { execsql { Index: test/minmax.test ================================================================== --- test/minmax.test +++ test/minmax.test @@ -11,11 +11,11 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing SELECT statements that contain # aggregate min() and max() functions and which are handled as # as a special case. # -# $Id: minmax.test,v 1.9 2004/03/13 14:00:37 drh Exp $ +# $Id: minmax.test,v 1.10 2004/06/24 00:20:05 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl do_test minmax-1.0 { @@ -125,11 +125,11 @@ do_test minmax-4.1 { execsql { SELECT coalesce(min(x+0),-1), coalesce(max(x+0),-1) FROM (SELECT * FROM t1 UNION SELECT NULL as 'x', NULL as 'y') } -} {1 20} +} {-1 20} do_test minmax-4.2 { execsql { SELECT y, sum(x) FROM (SELECT null, y+1 FROM t1 UNION SELECT * FROM t1) GROUP BY y ORDER BY y; @@ -273,7 +273,37 @@ SELECT max(rowid) FROM ( SELECT max(rowid) FROM t4 EXCEPT SELECT max(rowid) FROM t5 ) } } {{}} + +# If there is a NULL in an aggregate max(), ignore it. If a NULL +# occurs in an aggregate min(), then the result will be NULL because +# NULL compares less than all other values. +# +do_test minmax-10.1 { + execsql { + CREATE TABLE t6(x); + INSERT INTO t6 VALUES(1); + INSERT INTO t6 VALUES(2); + INSERT INTO t6 VALUES(NULL); + SELECT coalesce(min(x),-1) FROM t6; + } +} {-1} +do_test minmax-10.2 { + execsql { + SELECT max(x) FROM t6; + } +} {2} +do_test minmax-10.3 { + execsql { + CREATE INDEX i6 ON t6(x); + SELECT coalesce(min(x),-1) FROM t6; + } +} {-1} +do_test minmax-10.4 { + execsql { + SELECT max(x) FROM t6; + } +} {2} finish_test Index: test/null.test ================================================================== --- test/null.test +++ test/null.test @@ -98,11 +98,11 @@ do_test null-3.1 { execsql { select count(*), count(b), count(c), sum(b), sum(c), avg(b), avg(c), min(b), max(b) from t1; } -} {7 4 6 2 3 0.5 0.5 0 1} +} {7 4 6 2 3 0.5 0.5 {} 1} # Check to see how WHERE clauses handle NULL values. A NULL value # is the same as UNKNOWN. The WHERE clause should only select those # rows that are TRUE. FALSE and UNKNOWN rows are rejected. # Index: test/select1.test ================================================================== --- test/select1.test +++ test/select1.test @@ -9,11 +9,11 @@ # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing the SELECT statement. # -# $Id: select1.test,v 1.32 2004/05/28 11:37:29 danielk1977 Exp $ +# $Id: select1.test,v 1.33 2004/06/24 00:20:05 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Try to select on a non-existant table. @@ -157,11 +157,11 @@ set v [catch {execsql {SELECT MIN(f1,f2) FROM test1}} msg] lappend v [lsort $msg] } {0 {11 33}} do_test select1-2.8.1 { execsql {SELECT coalesce(min(a),'xyzzy') FROM t3} -} {11} +} {xyzzy} do_test select1-2.8.2 { execsql {SELECT min(coalesce(a,'xyzzy')) FROM t3} } {11} do_test select1-2.8.3 { execsql {SELECT min(b), min(b) FROM t4}