From d9cde96423f1f7dcb5c15eb51b352c47cca3cd7d Mon Sep 17 00:00:00 2001 From: Jeevan Chalke Date: Thu, 7 Dec 2023 18:21:12 +0530 Subject: [PATCH v4 1/5] Add numeric_int8_opt_error() to optionally suppress errors. Jsonpath methods report errors in their way and thus want the internal functions to not throw any error. So, add this version for numeric_int8() conversion. We already did such error suppression in commit 16d489b0fe058e527619f5e9d92fd7ca3c6c2994. Jeevan Chalke --- src/backend/utils/adt/numeric.c | 59 ++++++++++++++++++++++++++++++----------- src/include/utils/numeric.h | 1 + 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index bf61fd7..2a29690 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -4430,35 +4430,62 @@ int8_numeric(PG_FUNCTION_ARGS) PG_RETURN_NUMERIC(int64_to_numeric(val)); } - -Datum -numeric_int8(PG_FUNCTION_ARGS) +int64 +numeric_int8_opt_error(Numeric num, bool *have_error) { - Numeric num = PG_GETARG_NUMERIC(0); NumericVar x; int64 result; + if (have_error) + *have_error = false; + if (NUMERIC_IS_SPECIAL(num)) { - if (NUMERIC_IS_NAN(num)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("cannot convert NaN to %s", "bigint"))); + if (have_error) + { + *have_error = true; + return 0; + } else - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("cannot convert infinity to %s", "bigint"))); + { + if (NUMERIC_IS_NAN(num)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot convert NaN to %s", "bigint"))); + else + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot convert infinity to %s", "bigint"))); + } } - /* Convert to variable format and thence to int8 */ + /* Convert to variable format, then convert to int8 */ init_var_from_num(num, &x); if (!numericvar_to_int64(&x, &result)) - ereport(ERROR, - (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), - errmsg("bigint out of range"))); + { + if (have_error) + { + *have_error = true; + return 0; + } + else + { + ereport(ERROR, + (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), + errmsg("bigint out of range"))); + } + } + + return result; +} + +Datum +numeric_int8(PG_FUNCTION_ARGS) +{ + Numeric num = PG_GETARG_NUMERIC(0); - PG_RETURN_INT64(result); + PG_RETURN_INT64(numeric_int8_opt_error(num, NULL)); } diff --git a/src/include/utils/numeric.h b/src/include/utils/numeric.h index 08e4f8c..0780b52 100644 --- a/src/include/utils/numeric.h +++ b/src/include/utils/numeric.h @@ -101,5 +101,6 @@ extern Numeric numeric_div_opt_error(Numeric num1, Numeric num2, extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error); extern int32 numeric_int4_opt_error(Numeric num, bool *have_error); +extern int64 numeric_int8_opt_error(Numeric num, bool *have_error); #endif /* _PG_NUMERIC_H_ */ -- 1.8.3.1