From abb1733922f3ff17a514499883a549f8bd03af44 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 5 Jan 2016 19:02:13 -0300 Subject: [PATCH] Add scale(numeric) Author: Marko Tiikkaja --- doc/src/sgml/func.sgml | 13 ++++++ src/backend/utils/adt/numeric.c | 17 ++++++++ src/include/catalog/catversion.h | 2 +- src/include/catalog/pg_proc.h | 2 + src/include/utils/builtins.h | 1 + src/test/regress/expected/numeric.out | 57 +++++++++++++++++++++++++++ src/test/regress/sql/numeric.sql | 14 +++++++ 7 files changed, 105 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index a0b42c2ddb..8591aa3e5a 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -849,6 +849,19 @@ 42.44 + + + + scale + + scale(numeric) + + numeric + scale of the argument (the number of decimal digits in the fractional part) + scale(8.41) + 2 + + diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index ea79b48a8f..07b264572d 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -2825,6 +2825,23 @@ numeric_power(PG_FUNCTION_ARGS) PG_RETURN_NUMERIC(res); } +/* + * numeric_scale() - + * + * Returns the scale, i.e. the count of decimal digits in the fractional part + */ +Datum +numeric_scale(PG_FUNCTION_ARGS) +{ + Numeric num = PG_GETARG_NUMERIC(0); + + if (NUMERIC_IS_NAN(num)) + PG_RETURN_NULL(); + + PG_RETURN_INT32(NUMERIC_DSCALE(num)); +} + + /* ---------------------------------------------------------------------- * diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 2c6aa45063..31a132ea98 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 201601051 +#define CATALOG_VERSION_NO 201601052 #endif diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 22d9386ada..9250545d3b 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -2361,6 +2361,8 @@ DESCR("exponentiation"); DATA(insert OID = 2169 ( power PGNSP PGUID 12 1 0 0 0 f f f f t f i s 2 0 1700 "1700 1700" _null_ _null_ _null_ _null_ _null_ numeric_power _null_ _null_ _null_ )); DESCR("exponentiation"); DATA(insert OID = 1739 ( numeric_power PGNSP PGUID 12 1 0 0 0 f f f f t f i s 2 0 1700 "1700 1700" _null_ _null_ _null_ _null_ _null_ numeric_power _null_ _null_ _null_ )); +DATA(insert OID = 8888 ( scale PGNSP PGUID 12 1 0 0 0 f f f f t f i s 1 0 23 "1700" _null_ _null_ _null_ _null_ _null_ numeric_scale _null_ _null_ _null_ )); +DESCR("number of decimal digits in the fractional part"); DATA(insert OID = 1740 ( numeric PGNSP PGUID 12 1 0 0 0 f f f f t f i s 1 0 1700 "23" _null_ _null_ _null_ _null_ _null_ int4_numeric _null_ _null_ _null_ )); DESCR("convert int4 to numeric"); DATA(insert OID = 1741 ( log PGNSP PGUID 14 1 0 0 0 f f f f t f i s 1 0 1700 "1700" _null_ _null_ _null_ _null_ _null_ "select pg_catalog.log(10, $1)" _null_ _null_ _null_ )); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index bbaa2cea7e..b35d20626b 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -1022,6 +1022,7 @@ extern Datum numeric_exp(PG_FUNCTION_ARGS); extern Datum numeric_ln(PG_FUNCTION_ARGS); extern Datum numeric_log(PG_FUNCTION_ARGS); extern Datum numeric_power(PG_FUNCTION_ARGS); +extern Datum numeric_scale(PG_FUNCTION_ARGS); extern Datum int4_numeric(PG_FUNCTION_ARGS); extern Datum numeric_int4(PG_FUNCTION_ARGS); extern Datum int8_numeric(PG_FUNCTION_ARGS); diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out index caac424744..f1f50560ee 100644 --- a/src/test/regress/expected/numeric.out +++ b/src/test/regress/expected/numeric.out @@ -1852,3 +1852,60 @@ select log(3.1954752e47, 9.4792021e-73); -1.51613372350688302142917386143459361608600157692779164475351842333265418126982165 (1 row) +-- +-- Tests for scale() +-- +select scale(numeric 'NaN'); + scale +------- + +(1 row) + +select scale(NULL::numeric); + scale +------- + +(1 row) + +select scale(1.12); + scale +------- + 2 +(1 row) + +select scale(0); + scale +------- + 0 +(1 row) + +select scale(0.00); + scale +------- + 2 +(1 row) + +select scale(1.12345); + scale +------- + 5 +(1 row) + +select scale(110123.12475871856128); + scale +------- + 14 +(1 row) + +select scale(-1123.12471856128); + scale +------- + 11 +(1 row) + +select scale(-13.000000000000000); + scale +------- + 15 +(1 row) + diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql index 0a4d1cb1d2..fc472187d8 100644 --- a/src/test/regress/sql/numeric.sql +++ b/src/test/regress/sql/numeric.sql @@ -983,3 +983,17 @@ select log(1.23e-89, 6.4689e45); select log(0.99923, 4.58934e34); select log(1.000016, 8.452010e18); select log(3.1954752e47, 9.4792021e-73); + +-- +-- Tests for scale() +-- + +select scale(numeric 'NaN'); +select scale(NULL::numeric); +select scale(1.12); +select scale(0); +select scale(0.00); +select scale(1.12345); +select scale(110123.12475871856128); +select scale(-1123.12471856128); +select scale(-13.000000000000000);