A SUM() of all NULLs returns NULL. A SUM() of nothing return 0.
A SUM() of a mixture of NULLs and numbers returns the sum of the numbers. Ticket #1413. (CVS 2677) FossilOrigin-Name: 2e6230edfd651b40481ebad8aa01a22ac92ce80c
This commit is contained in:
parent
825c662e66
commit
3f219f46fc
16
manifest
16
manifest
@ -1,5 +1,5 @@
|
||||
C Remove\sa\sC++ism\sthat\ssnuck\sin\son\sone\sof\sthe\sreason\schanges.\s(CVS\s2676)
|
||||
D 2005-09-08T19:01:06
|
||||
C A\sSUM()\sof\sall\sNULLs\sreturns\sNULL.\s\sA\sSUM()\sof\snothing\sreturn\s0.\nA\sSUM()\sof\sa\smixture\sof\sNULLs\sand\snumbers\sreturns\sthe\ssum\sof\sthe\nnumbers.\s\sTicket\s#1413.\s(CVS\s2677)
|
||||
D 2005-09-08T19:45:58
|
||||
F Makefile.in 12784cdce5ffc8dfb707300c34e4f1eb3b8a14f1
|
||||
F Makefile.linux-gcc 06be33b2a9ad4f005a5f42b22c4a19dab3cbb5c7
|
||||
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
|
||||
@ -41,7 +41,7 @@ F src/date.c 7444b0900a28da77e57e3337a636873cff0ae940
|
||||
F src/delete.c 16a0e19460b14d219f39ff5c7a9eef808aa1969c
|
||||
F src/experimental.c 50c1e3b34f752f4ac10c36f287db095c2b61766d
|
||||
F src/expr.c 38f1e135aa80dfc62e253c1e22dd6e194efd2d61
|
||||
F src/func.c c1027f4fcb4aaf7118e2b8ed7bbc33c709f2128e
|
||||
F src/func.c ebf56befdbecb6570f3fbf42e6e414baf5e2049a
|
||||
F src/hash.c 2b1b13f7400e179631c83a1be0c664608c8f021f
|
||||
F src/hash.h 1b0c445e1c89ff2aaad9b4605ba61375af001e84
|
||||
F src/insert.c 484c73bc1309f283a31baa0e114f3ee980536397
|
||||
@ -145,7 +145,7 @@ F test/enc2.test 76c13b8c00beaf95b15c152e95dab51292eb1f0d
|
||||
F test/enc3.test f6a5f0b7b7f3a88f030d3143729b87cd5c86d837
|
||||
F test/expr.test 71b8cba7fe5c228147c93e530e098144565aaa46
|
||||
F test/fkey1.test 153004438d51e6769fb1ce165f6313972d6263ce
|
||||
F test/func.test 89206eb4ec8bb2ba1853706470cc2b84147a8c78
|
||||
F test/func.test e1f9489e76a1add3ffeecd02038edca130ba759b
|
||||
F test/hook.test f8605cde4c77b2c6a4a73723bf6c507796a64dda
|
||||
F test/in.test cead6165aebbe0d451bb2263a307173acfeb6240
|
||||
F test/index.test 51e01a0928b4b61228917ddd8c6c0e2466547f6f
|
||||
@ -174,7 +174,7 @@ F test/malloc.test 666c77a878ce50f5c22b9211ed43e889cabb63a6
|
||||
F test/malloc2.test 655b972372d2754a3f6c6ed54d7cfd18fde9bd32
|
||||
F test/memdb.test 1860e060be810bf0775bc57408a5b7c4954bcaea
|
||||
F test/memleak.test df2b2b96e77f8ba159a332299535b1e5f18e49ac
|
||||
F test/minmax.test 1d6f6cd00b0818423c52e71a5d684445b03e20d2
|
||||
F test/minmax.test cad887abca5504396718e2cd5729ca40758743e8
|
||||
F test/misc1.test c991617666991e11513e46cb646fd2e45ae68599
|
||||
F test/misc2.test 5c699af2fede2694736a9f45aea7e2f052686e15
|
||||
F test/misc3.test 7bd937e2c62bcc6be71939faf068d506467b1e03
|
||||
@ -306,7 +306,7 @@ F www/tclsqlite.tcl 3df553505b6efcad08f91e9b975deb2e6c9bb955
|
||||
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
|
||||
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
|
||||
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
|
||||
P cdfe372a958fb446485913e860d52b87fffd34e4
|
||||
R 90ad4b515120e3af2e9b022d1054c0bf
|
||||
P 48f6a331efea419fe948cd366f9c60ae41edddfd
|
||||
R 37364513cd59bb3038c4f5a7cd9fbfba
|
||||
U drh
|
||||
Z b72ca3f8146ea206fe3fcf97bfef6a79
|
||||
Z 7b39a84184f0dd170c44b611c8879606
|
||||
|
@ -1 +1 @@
|
||||
48f6a331efea419fe948cd366f9c60ae41edddfd
|
||||
2e6230edfd651b40481ebad8aa01a22ac92ce80c
|
14
src/func.c
14
src/func.c
@ -16,7 +16,7 @@
|
||||
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
|
||||
** All other code has file scope.
|
||||
**
|
||||
** $Id: func.c,v 1.108 2005/09/08 10:37:01 drh Exp $
|
||||
** $Id: func.c,v 1.109 2005/09/08 19:45:58 drh Exp $
|
||||
*/
|
||||
#include "sqliteInt.h"
|
||||
#include <ctype.h>
|
||||
@ -819,7 +819,7 @@ typedef struct SumCtx SumCtx;
|
||||
struct SumCtx {
|
||||
double sum; /* Sum of terms */
|
||||
int cnt; /* Number of elements summed */
|
||||
int isFloat; /* True if there has been any floating point value */
|
||||
u8 seenFloat; /* True if there has been any floating point value */
|
||||
};
|
||||
|
||||
/*
|
||||
@ -828,13 +828,15 @@ struct SumCtx {
|
||||
static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
|
||||
SumCtx *p;
|
||||
int type;
|
||||
if( argc<1 ) return;
|
||||
assert( argc==1 );
|
||||
p = sqlite3_aggregate_context(context, sizeof(*p));
|
||||
type = sqlite3_value_type(argv[0]);
|
||||
if( p && type!=SQLITE_NULL ){
|
||||
p->sum += sqlite3_value_double(argv[0]);
|
||||
p->cnt++;
|
||||
if( type==SQLITE_FLOAT ) p->isFloat = 1;
|
||||
if( type==SQLITE_FLOAT ){
|
||||
p->seenFloat = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
static void sumFinalize(sqlite3_context *context){
|
||||
@ -842,9 +844,9 @@ static void sumFinalize(sqlite3_context *context){
|
||||
p = sqlite3_aggregate_context(context, 0);
|
||||
if( p==0 ){
|
||||
sqlite3_result_int(context, 0);
|
||||
}else if( p->isFloat ){
|
||||
}else if( p->seenFloat ){
|
||||
sqlite3_result_double(context, p->sum);
|
||||
}else{
|
||||
}else if( p->cnt>0 ){
|
||||
sqlite3_result_int64(context, (i64)p->sum);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
# This file implements regression tests for SQLite library. The
|
||||
# focus of this file is testing built-in functions.
|
||||
#
|
||||
# $Id: func.test,v 1.38 2005/09/08 10:37:01 drh Exp $
|
||||
# $Id: func.test,v 1.39 2005/09/08 19:45:58 drh Exp $
|
||||
|
||||
set testdir [file dirname $argv0]
|
||||
source $testdir/tester.tcl
|
||||
@ -513,5 +513,31 @@ do_test func-18.2 {
|
||||
}
|
||||
} {9902.0}
|
||||
|
||||
# The sum of nothing is 0. But the sum of all NULLs is NULL.
|
||||
#
|
||||
do_test func-18.3 {
|
||||
execsql {
|
||||
DELETE FROM t5;
|
||||
SELECT sum(x) FROM t5;
|
||||
}
|
||||
} {0}
|
||||
do_test func-18.4 {
|
||||
execsql {
|
||||
INSERT INTO t5 VALUES(NULL);
|
||||
SELECT sum(x) FROM t5
|
||||
}
|
||||
} {{}}
|
||||
do_test func-18.5 {
|
||||
execsql {
|
||||
INSERT INTO t5 VALUES(NULL);
|
||||
SELECT sum(x) FROM t5
|
||||
}
|
||||
} {{}}
|
||||
do_test func-18.6 {
|
||||
execsql {
|
||||
INSERT INTO t5 VALUES(123);
|
||||
SELECT sum(x) FROM t5
|
||||
}
|
||||
} {123}
|
||||
|
||||
finish_test
|
||||
|
@ -13,7 +13,7 @@
|
||||
# aggregate min() and max() functions and which are handled as
|
||||
# as a special case.
|
||||
#
|
||||
# $Id: minmax.test,v 1.17 2005/09/08 10:37:01 drh Exp $
|
||||
# $Id: minmax.test,v 1.18 2005/09/08 19:45:58 drh Exp $
|
||||
|
||||
set testdir [file dirname $argv0]
|
||||
source $testdir/tester.tcl
|
||||
@ -143,7 +143,7 @@ ifcapable {compound && subquery} {
|
||||
} {1 20}
|
||||
do_test minmax-4.2 {
|
||||
execsql {
|
||||
SELECT y, sum(x) FROM
|
||||
SELECT y, coalesce(sum(x),0) FROM
|
||||
(SELECT null, y+1 FROM t1 UNION SELECT * FROM t1)
|
||||
GROUP BY y ORDER BY y;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user