Skip to content

Commit abb1733

Browse files
committed
Add scale(numeric)
Author: Marko Tiikkaja
1 parent 419400c commit abb1733

File tree

7 files changed

+105
-1
lines changed

7 files changed

+105
-1
lines changed

doc/src/sgml/func.sgml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,19 @@
849849
<entry><literal>42.44</literal></entry>
850850
</row>
851851

852+
<row>
853+
<entry>
854+
<indexterm>
855+
<primary>scale</primary>
856+
</indexterm>
857+
<literal><function>scale(<type>numeric</type>)</function></literal>
858+
</entry>
859+
<entry><type>numeric</type></entry>
860+
<entry>scale of the argument (the number of decimal digits in the fractional part)</entry>
861+
<entry><literal>scale(8.41)</literal></entry>
862+
<entry><literal>2</literal></entry>
863+
</row>
864+
852865
<row>
853866
<entry>
854867
<indexterm>

src/backend/utils/adt/numeric.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,23 @@ numeric_power(PG_FUNCTION_ARGS)
28252825
PG_RETURN_NUMERIC(res);
28262826
}
28272827

2828+
/*
2829+
* numeric_scale() -
2830+
*
2831+
* Returns the scale, i.e. the count of decimal digits in the fractional part
2832+
*/
2833+
Datum
2834+
numeric_scale(PG_FUNCTION_ARGS)
2835+
{
2836+
Numeric num = PG_GETARG_NUMERIC(0);
2837+
2838+
if (NUMERIC_IS_NAN(num))
2839+
PG_RETURN_NULL();
2840+
2841+
PG_RETURN_INT32(NUMERIC_DSCALE(num));
2842+
}
2843+
2844+
28282845

28292846
/* ----------------------------------------------------------------------
28302847
*

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201601051
56+
#define CATALOG_VERSION_NO 201601052
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,6 +2361,8 @@ DESCR("exponentiation");
23612361
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_ ));
23622362
DESCR("exponentiation");
23632363
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_ ));
2364+
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_ ));
2365+
DESCR("number of decimal digits in the fractional part");
23642366
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_ ));
23652367
DESCR("convert int4 to numeric");
23662368
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_ ));

src/include/utils/builtins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ extern Datum numeric_exp(PG_FUNCTION_ARGS);
10221022
extern Datum numeric_ln(PG_FUNCTION_ARGS);
10231023
extern Datum numeric_log(PG_FUNCTION_ARGS);
10241024
extern Datum numeric_power(PG_FUNCTION_ARGS);
1025+
extern Datum numeric_scale(PG_FUNCTION_ARGS);
10251026
extern Datum int4_numeric(PG_FUNCTION_ARGS);
10261027
extern Datum numeric_int4(PG_FUNCTION_ARGS);
10271028
extern Datum int8_numeric(PG_FUNCTION_ARGS);

src/test/regress/expected/numeric.out

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,3 +1852,60 @@ select log(3.1954752e47, 9.4792021e-73);
18521852
-1.51613372350688302142917386143459361608600157692779164475351842333265418126982165
18531853
(1 row)
18541854

1855+
--
1856+
-- Tests for scale()
1857+
--
1858+
select scale(numeric 'NaN');
1859+
scale
1860+
-------
1861+
1862+
(1 row)
1863+
1864+
select scale(NULL::numeric);
1865+
scale
1866+
-------
1867+
1868+
(1 row)
1869+
1870+
select scale(1.12);
1871+
scale
1872+
-------
1873+
2
1874+
(1 row)
1875+
1876+
select scale(0);
1877+
scale
1878+
-------
1879+
0
1880+
(1 row)
1881+
1882+
select scale(0.00);
1883+
scale
1884+
-------
1885+
2
1886+
(1 row)
1887+
1888+
select scale(1.12345);
1889+
scale
1890+
-------
1891+
5
1892+
(1 row)
1893+
1894+
select scale(110123.12475871856128);
1895+
scale
1896+
-------
1897+
14
1898+
(1 row)
1899+
1900+
select scale(-1123.12471856128);
1901+
scale
1902+
-------
1903+
11
1904+
(1 row)
1905+
1906+
select scale(-13.000000000000000);
1907+
scale
1908+
-------
1909+
15
1910+
(1 row)
1911+

src/test/regress/sql/numeric.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,3 +983,17 @@ select log(1.23e-89, 6.4689e45);
983983
select log(0.99923, 4.58934e34);
984984
select log(1.000016, 8.452010e18);
985985
select log(3.1954752e47, 9.4792021e-73);
986+
987+
--
988+
-- Tests for scale()
989+
--
990+
991+
select scale(numeric 'NaN');
992+
select scale(NULL::numeric);
993+
select scale(1.12);
994+
select scale(0);
995+
select scale(0.00);
996+
select scale(1.12345);
997+
select scale(110123.12475871856128);
998+
select scale(-1123.12471856128);
999+
select scale(-13.000000000000000);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy