Permalink
Browse files

Proper handling of NULL values (the aggregates ignore them from now on).

  • Loading branch information...
1 parent e5c102e commit bece98615a52f7ebc0552a9ed6f828d098427938 @tvondra committed Nov 5, 2011
Showing with 67 additions and 49 deletions.
  1. +67 −49 src/quantile.c
View
@@ -127,8 +127,6 @@ quantile_append_double(PG_FUNCTION_ARGS)
struct_double * data;
- double element;
-
MemoryContext oldcontext = MemoryContextSwitchTo(CurrentMemoryContext->parent);
if (PG_ARGISNULL(0)) {
@@ -144,15 +142,20 @@ quantile_append_double(PG_FUNCTION_ARGS)
data = (struct_double*)PG_GETARG_INT64(0);
}
- element = PG_GETARG_FLOAT8(1);
+ /* ignore NULL values */
+ if (! PG_ARGISNULL(1)) {
- if (data->next > data->nelements-1) {
- data->elements = (double*)repalloc(data->elements, sizeof(double)*(data->nelements + SLICE_SIZE));
- data->nelements = data->nelements + SLICE_SIZE;
+ double element = PG_GETARG_FLOAT8(1);
+
+ if (data->next > data->nelements-1) {
+ data->elements = (double*)repalloc(data->elements, sizeof(double)*(data->nelements + SLICE_SIZE));
+ data->nelements = data->nelements + SLICE_SIZE;
+ }
+
+ data->elements[data->next++] = element;
+
}
- data->elements[data->next++] = element;
-
MemoryContextSwitchTo(oldcontext);
PG_RETURN_INT64((int64)data);
@@ -165,8 +168,6 @@ quantile_append_double_array(PG_FUNCTION_ARGS)
struct_double * data;
- double element;
-
MemoryContext oldcontext = MemoryContextSwitchTo(CurrentMemoryContext->parent);
if (PG_ARGISNULL(0)) {
@@ -182,15 +183,20 @@ quantile_append_double_array(PG_FUNCTION_ARGS)
data = (struct_double*)PG_GETARG_INT64(0);
}
- element = PG_GETARG_FLOAT8(1);
+ /* ignore NULL values */
+ if (! PG_ARGISNULL(1)) {
- if (data->next > data->nelements-1) {
- data->elements = (double*)repalloc(data->elements, sizeof(double)*(data->nelements + SLICE_SIZE));
- data->nelements = data->nelements + SLICE_SIZE;
+ double element = PG_GETARG_FLOAT8(1);
+
+ if (data->next > data->nelements-1) {
+ data->elements = (double*)repalloc(data->elements, sizeof(double)*(data->nelements + SLICE_SIZE));
+ data->nelements = data->nelements + SLICE_SIZE;
+ }
+
+ data->elements[data->next++] = element;
+
}
- data->elements[data->next++] = element;
-
MemoryContextSwitchTo(oldcontext);
PG_RETURN_INT64((int64)data);
@@ -203,8 +209,6 @@ quantile_append_int32(PG_FUNCTION_ARGS)
struct_int32 * data;
- int32 element;
-
MemoryContext oldcontext = MemoryContextSwitchTo(CurrentMemoryContext->parent);
if (PG_ARGISNULL(0)) {
@@ -219,15 +223,20 @@ quantile_append_int32(PG_FUNCTION_ARGS)
data = (struct_int32*)PG_GETARG_INT64(0);
}
- element = PG_GETARG_INT32(1);
+ /* ignore NULL values */
+ if (! PG_ARGISNULL(1)) {
- if (data->next > data->nelements-1) {
- data->elements = (int32*)repalloc(data->elements, sizeof(int32)*(data->nelements + SLICE_SIZE));
- data->nelements = data->nelements + SLICE_SIZE;
+ int32 element = PG_GETARG_INT32(1);
+
+ if (data->next > data->nelements-1) {
+ data->elements = (int32*)repalloc(data->elements, sizeof(int32)*(data->nelements + SLICE_SIZE));
+ data->nelements = data->nelements + SLICE_SIZE;
+ }
+
+ data->elements[data->next++] = element;
+
}
- data->elements[data->next++] = element;
-
MemoryContextSwitchTo(oldcontext);
PG_RETURN_INT64((int64)data);
@@ -240,8 +249,6 @@ quantile_append_int32_array(PG_FUNCTION_ARGS)
struct_int32 * data;
- int32 element;
-
MemoryContext oldcontext = MemoryContextSwitchTo(CurrentMemoryContext->parent);
if (PG_ARGISNULL(0)) {
@@ -257,15 +264,20 @@ quantile_append_int32_array(PG_FUNCTION_ARGS)
data = (struct_int32*)PG_GETARG_INT64(0);
}
- element = PG_GETARG_INT32(1);
-
- if (data->next > data->nelements-1) {
- data->elements = (int32*)repalloc(data->elements, sizeof(int32)*(data->nelements + SLICE_SIZE));
- data->nelements = data->nelements + SLICE_SIZE;
+ /* ignore NULL values */
+ if (! PG_ARGISNULL(1)) {
+
+ int32 element = PG_GETARG_INT32(1);
+
+ if (data->next > data->nelements-1) {
+ data->elements = (int32*)repalloc(data->elements, sizeof(int32)*(data->nelements + SLICE_SIZE));
+ data->nelements = data->nelements + SLICE_SIZE;
+ }
+
+ data->elements[data->next++] = element;
+
}
- data->elements[data->next++] = element;
-
MemoryContextSwitchTo(oldcontext);
PG_RETURN_INT64((int64)data);
@@ -278,8 +290,6 @@ quantile_append_int64(PG_FUNCTION_ARGS)
struct_int64 * data;
- int64 element;
-
MemoryContext oldcontext = MemoryContextSwitchTo(CurrentMemoryContext->parent);
if (PG_ARGISNULL(0)) {
@@ -294,15 +304,20 @@ quantile_append_int64(PG_FUNCTION_ARGS)
data = (struct_int64*)PG_GETARG_INT64(0);
}
- element = PG_GETARG_INT64(1);
+ /* ignore NULL values */
+ if (! PG_ARGISNULL(1)) {
- if (data->next > data->nelements-1) {
- data->elements = (int64*)repalloc(data->elements, sizeof(int64)*(data->nelements + SLICE_SIZE));
- data->nelements = data->nelements + SLICE_SIZE;
+ int64 element = PG_GETARG_INT64(1);
+
+ if (data->next > data->nelements-1) {
+ data->elements = (int64*)repalloc(data->elements, sizeof(int64)*(data->nelements + SLICE_SIZE));
+ data->nelements = data->nelements + SLICE_SIZE;
+ }
+
+ data->elements[data->next++] = element;
+
}
- data->elements[data->next++] = element;
-
MemoryContextSwitchTo(oldcontext);
PG_RETURN_INT64((int64)data);
@@ -315,8 +330,6 @@ quantile_append_int64_array(PG_FUNCTION_ARGS)
struct_int64 * data;
- int64 element;
-
MemoryContext oldcontext = MemoryContextSwitchTo(CurrentMemoryContext->parent);
if (PG_ARGISNULL(0)) {
@@ -332,15 +345,20 @@ quantile_append_int64_array(PG_FUNCTION_ARGS)
data = (struct_int64*)PG_GETARG_INT64(0);
}
- element = PG_GETARG_INT64(1);
+ /* ignore NULL values */
+ if (! PG_ARGISNULL(1)) {
- if (data->next > data->nelements-1) {
- data->elements = (int64*)repalloc(data->elements, sizeof(int64)*(data->nelements + SLICE_SIZE));
- data->nelements = data->nelements + SLICE_SIZE;
+ int64 element = PG_GETARG_INT64(1);
+
+ if (data->next > data->nelements-1) {
+ data->elements = (int64*)repalloc(data->elements, sizeof(int64)*(data->nelements + SLICE_SIZE));
+ data->nelements = data->nelements + SLICE_SIZE;
+ }
+
+ data->elements[data->next++] = element;
+
}
- data->elements[data->next++] = element;
-
MemoryContextSwitchTo(oldcontext);
PG_RETURN_INT64((int64)data);

0 comments on commit bece986

Please sign in to comment.