summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2072e78)
raw | patch | inline | side by side (parent: 2072e78)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 12 Nov 2012 10:29:53 +0000 (11:29 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 12 Nov 2012 10:29:53 +0000 (11:29 +0100) |
NaN will be treated as undefined value.
src/cdata.c | patch | blob | history |
diff --git a/src/cdata.c b/src/cdata.c
index 2630e733d35d10200e9938adc5082ff6bc200ea2..418ec0afa2fe42f3068584cb0551964b70527f0e 100644 (file)
--- a/src/cdata.c
+++ b/src/cdata.c
#include <errno.h>
#include <string.h>
+#include <math.h>
#include <postgres.h>
#include <fmgr.h>
data->value = strtod(val_str, &endptr);
data->val_num = 1;
+ if (isnan(data->value)) {
+ data->undef_num = 1;
+ }
+
if ((endptr == val_str) || errno)
ereport(ERROR, (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
cdata_t *data;
cdata_t *update;
+ float8 value;
+ float8 u_value;
+
+ int32 val_num;
+ int32 u_val_num;
+
if (PG_NARGS() != 2)
ereport(ERROR, (
errmsg("cdata_update() expects two arguments"),
"consolidation function")
));
+ value = data->value;
+ u_value = update->value;
+
+ val_num = data->val_num - data->undef_num;
+ u_val_num = update->val_num - update->undef_num;
+
+ data->undef_num += update->undef_num;
+ data->val_num += update->val_num;
+
+ if (isnan(value) || isnan(u_value)) {
+ data->value = isnan(value) ? u_value : value;
+ PG_RETURN_CDATA_P(data);
+ }
+
switch (data->cf) {
case CF_AVG:
- data->value = (data->value * (data->val_num - data->undef_num))
- + (update->value * (update->val_num - update->undef_num));
- data->value /= (data->val_num - data->undef_num)
- + (update->val_num - update->undef_num);
+ data->value = ((value * val_num) + (u_value * u_val_num))
+ / (val_num + u_val_num);
break;
case CF_MIN:
- data->value = (data->value <= update->value)
- ? data->value : update->value;
+ data->value = (value < u_value) ? value : u_value;
break;
case CF_MAX:
- data->value = (data->value >= update->value)
- ? data->value : update->value;
+ data->value = (value >= u_value) ? value : u_value;
break;
default:
ereport(ERROR, (
));
break;
}
-
- data->undef_num += update->undef_num;
- data->val_num += update->val_num;
PG_RETURN_CDATA_P(data);
} /* cdata_update */