From 64065814fd30cddb12b41674f01d7e505f793e19 Mon Sep 17 00:00:00 2001 From: Amul Sul Date: Tue, 15 Feb 2022 23:57:26 -0500 Subject: [PATCH v2 1/5] Move numeric_pg_lsn to pg_lsn.c file. --- src/backend/utils/adt/numeric.c | 64 +++++++++++++++++---------------- src/backend/utils/adt/pg_lsn.c | 11 ++++++ src/include/utils/numeric.h | 1 + 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 45547f6ae7f..3157df7cd7b 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -4211,6 +4211,39 @@ int64_div_fast_to_numeric(int64 val1, int log10val2) return res; } +/* + * typeName is the user-visible type name of uint64 used for the error + * reporting. + */ +uint64 +numeric_to_uint64_type(Numeric num, char *typeName) +{ + NumericVar x; + uint64 result; + + if (NUMERIC_IS_SPECIAL(num)) + { + if (NUMERIC_IS_NAN(num)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot convert NaN to %s", typeName))); + else + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot convert infinity to %s", typeName))); + } + + /* Convert to variable format and thence to pg_lsn */ + init_var_from_num(num, &x); + + if (!numericvar_to_uint64(&x, &result)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("%s out of range", typeName))); + + return result; +} + Datum int4_numeric(PG_FUNCTION_ARGS) { @@ -4543,37 +4576,6 @@ numeric_float4(PG_FUNCTION_ARGS) } -Datum -numeric_pg_lsn(PG_FUNCTION_ARGS) -{ - Numeric num = PG_GETARG_NUMERIC(0); - NumericVar x; - XLogRecPtr result; - - if (NUMERIC_IS_SPECIAL(num)) - { - if (NUMERIC_IS_NAN(num)) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("cannot convert NaN to %s", "pg_lsn"))); - else - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("cannot convert infinity to %s", "pg_lsn"))); - } - - /* Convert to variable format and thence to pg_lsn */ - init_var_from_num(num, &x); - - if (!numericvar_to_uint64(&x, (uint64 *) &result)) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("pg_lsn out of range"))); - - PG_RETURN_LSN(result); -} - - /* ---------------------------------------------------------------------- * * Aggregate functions diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c index 45408787da9..90ecd0e7590 100644 --- a/src/backend/utils/adt/pg_lsn.c +++ b/src/backend/utils/adt/pg_lsn.c @@ -60,6 +60,17 @@ pg_lsn_in_internal(const char *str, bool *have_error) return result; } +Datum +numeric_pg_lsn(PG_FUNCTION_ARGS) +{ + Numeric num = PG_GETARG_NUMERIC(0); + XLogRecPtr result; + + result = (XLogRecPtr) numeric_to_uint64_type(num, "pg_lsn"); + + PG_RETURN_LSN(result); +} + Datum pg_lsn_in(PG_FUNCTION_ARGS) { diff --git a/src/include/utils/numeric.h b/src/include/utils/numeric.h index 21cd5a5cbf5..1b9ad3b5b58 100644 --- a/src/include/utils/numeric.h +++ b/src/include/utils/numeric.h @@ -74,6 +74,7 @@ extern char *numeric_normalize(Numeric num); extern Numeric int64_to_numeric(int64 val); extern Numeric int64_div_fast_to_numeric(int64 val1, int log10val2); +extern uint64 numeric_to_uint64_type(Numeric num, char *typeName); extern Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error); -- 2.18.0