diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 37f24e33857..79295d43ddf 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -6378,22 +6378,15 @@ int4_sum(PG_FUNCTION_ARGS) int64 oldsum; int64 newval; + /* Return the left input unchanged if right input is null. */ + if (PG_ARGISNULL(1)) + PG_RETURN_INPUT(0); + if (PG_ARGISNULL(0)) - { - /* No non-null input seen so far... */ - if (PG_ARGISNULL(1)) - PG_RETURN_NULL(); /* still no non-null */ - /* This is the first non-null input. */ - newval = (int64) PG_GETARG_INT32(1); - PG_RETURN_INT64(newval); - } + PG_RETURN_INT64((int64) PG_GETARG_INT32(1)); oldsum = PG_GETARG_INT64(0); - /* Leave sum unchanged if new input is null. */ - if (PG_ARGISNULL(1)) - PG_RETURN_INT64(oldsum); - /* OK to do the addition. */ newval = oldsum + (int64) PG_GETARG_INT32(1); diff --git a/src/include/fmgr.h b/src/include/fmgr.h index 04b7914095f..e3c2b3ac686 100644 --- a/src/include/fmgr.h +++ b/src/include/fmgr.h @@ -349,6 +349,16 @@ extern varlena *pg_detoast_datum_packed(varlena *datum); /* A few internal functions return void (which is not the same as NULL!) */ #define PG_RETURN_VOID() return (Datum) 0 +/* + * A shortcut to allow functions to return the value of the give input + * parameter, NULL if that parameter was NULL and the value of the parameter + * otherwise. The caller is responsible for ensuring the types match. + */ +#define PG_RETURN_INPUT(n) do { \ + fcinfo->isnull = fcinfo->args[n].isnull; \ + return fcinfo->args[n].value; \ + } while (0) + /* Macros for returning results of standard types */ #define PG_RETURN_DATUM(x) return (x)