Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1dc3a62

Browse files
committed
stddev() and variance() should return NULL when there is just one input
value, per recent discussion on pgsql-general.
1 parent f9ba0a7 commit 1dc3a62

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed

src/backend/utils/adt/float.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.84 2003/03/11 21:01:33 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.85 2003/04/21 00:22:24 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1649,12 +1649,9 @@ float8_variance(PG_FUNCTION_ARGS)
16491649
sumX = transvalues[1];
16501650
sumX2 = transvalues[2];
16511651

1652-
/* We define VARIANCE of no values to be NULL, of 1 value to be 0 */
1653-
if (N == 0.0)
1654-
PG_RETURN_NULL();
1655-
1652+
/* Sample variance is undefined when N is 0 or 1, so return NULL */
16561653
if (N <= 1.0)
1657-
PG_RETURN_FLOAT8(0.0);
1654+
PG_RETURN_NULL();
16581655

16591656
numerator = N * sumX2 - sumX * sumX;
16601657

@@ -1680,12 +1677,9 @@ float8_stddev(PG_FUNCTION_ARGS)
16801677
sumX = transvalues[1];
16811678
sumX2 = transvalues[2];
16821679

1683-
/* We define STDDEV of no values to be NULL, of 1 value to be 0 */
1684-
if (N == 0.0)
1685-
PG_RETURN_NULL();
1686-
1680+
/* Sample stddev is undefined when N is 0 or 1, so return NULL */
16871681
if (N <= 1.0)
1688-
PG_RETURN_FLOAT8(0.0);
1682+
PG_RETURN_NULL();
16891683

16901684
numerator = N * sumX2 - sumX * sumX;
16911685

src/backend/utils/adt/numeric.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Copyright (c) 1998-2003, PostgreSQL Global Development Group
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.59 2003/03/21 01:58:04 tgl Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.60 2003/04/21 00:22:24 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -1964,24 +1964,19 @@ numeric_variance(PG_FUNCTION_ARGS)
19641964
if (NUMERIC_IS_NAN(N) || NUMERIC_IS_NAN(sumX) || NUMERIC_IS_NAN(sumX2))
19651965
PG_RETURN_NUMERIC(make_result(&const_nan));
19661966

1967-
/* We define VARIANCE of no values to be NULL, of 1 value to be 0 */
1968-
/* N is zero iff no digits (cf. numeric_uminus) */
1969-
if (N->varlen == NUMERIC_HDRSZ)
1970-
PG_RETURN_NULL();
1971-
1967+
/* Sample variance is undefined when N is 0 or 1, so return NULL */
19721968
init_var(&vN);
19731969
set_var_from_num(N, &vN);
19741970

1975-
init_var(&vNminus1);
1976-
sub_var(&vN, &const_one, &vNminus1);
1977-
1978-
if (cmp_var(&vNminus1, &const_zero) <= 0)
1971+
if (cmp_var(&vN, &const_one) <= 0)
19791972
{
19801973
free_var(&vN);
1981-
free_var(&vNminus1);
1982-
PG_RETURN_NUMERIC(make_result(&const_zero));
1974+
PG_RETURN_NULL();
19831975
}
19841976

1977+
init_var(&vNminus1);
1978+
sub_var(&vN, &const_one, &vNminus1);
1979+
19851980
init_var(&vsumX);
19861981
set_var_from_num(sumX, &vsumX);
19871982
init_var(&vsumX2);
@@ -2045,24 +2040,19 @@ numeric_stddev(PG_FUNCTION_ARGS)
20452040
if (NUMERIC_IS_NAN(N) || NUMERIC_IS_NAN(sumX) || NUMERIC_IS_NAN(sumX2))
20462041
PG_RETURN_NUMERIC(make_result(&const_nan));
20472042

2048-
/* We define STDDEV of no values to be NULL, of 1 value to be 0 */
2049-
/* N is zero iff no digits (cf. numeric_uminus) */
2050-
if (N->varlen == NUMERIC_HDRSZ)
2051-
PG_RETURN_NULL();
2052-
2043+
/* Sample stddev is undefined when N is 0 or 1, so return NULL */
20532044
init_var(&vN);
20542045
set_var_from_num(N, &vN);
20552046

2056-
init_var(&vNminus1);
2057-
sub_var(&vN, &const_one, &vNminus1);
2058-
2059-
if (cmp_var(&vNminus1, &const_zero) <= 0)
2047+
if (cmp_var(&vN, &const_one) <= 0)
20602048
{
20612049
free_var(&vN);
2062-
free_var(&vNminus1);
2063-
PG_RETURN_NUMERIC(make_result(&const_zero));
2050+
PG_RETURN_NULL();
20642051
}
20652052

2053+
init_var(&vNminus1);
2054+
sub_var(&vN, &const_one, &vNminus1);
2055+
20662056
init_var(&vsumX);
20672057
set_var_from_num(sumX, &vsumX);
20682058
init_var(&vsumX2);

0 commit comments

Comments
 (0)