Skip to content

Commit b9d2c5c

Browse files
committed
Fix loss of fractional digits for large values in cash_numeric().
Money values exceeding about 18 digits (depending on lc_monetary) could be inaccurately converted to numeric, due to select_div_scale() deciding it didn't need to compute any fractional digits. Force its hand by setting the dscale of one division input to equal the number of fractional digits we need. In passing, rearrange the logic to not do useless work in locales where money values are considered integral. Per bug #15925 from Slawomir Chodnicki. Back-patch to all supported branches. Discussion: https://postgr.es/m/15925-da9953e2674bb5c8@postgresql.org
1 parent e829337 commit b9d2c5c

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

src/backend/utils/adt/cash.c

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,36 +1032,54 @@ Datum
10321032
cash_numeric(PG_FUNCTION_ARGS)
10331033
{
10341034
Cash money = PG_GETARG_CASH(0);
1035-
Numeric result;
1035+
Datum result;
10361036
int fpoint;
1037-
int64 scale;
1038-
int i;
1039-
Datum amount;
1040-
Datum numeric_scale;
1041-
Datum quotient;
10421037
struct lconv *lconvert = PGLC_localeconv();
10431038

10441039
/* see comments about frac_digits in cash_in() */
10451040
fpoint = lconvert->frac_digits;
10461041
if (fpoint < 0 || fpoint > 10)
10471042
fpoint = 2;
10481043

1049-
/* compute required scale factor */
1050-
scale = 1;
1051-
for (i = 0; i < fpoint; i++)
1052-
scale *= 10;
1044+
/* convert the integral money value to numeric */
1045+
result = DirectFunctionCall1(int8_numeric, Int64GetDatum(money));
10531046

1054-
/* form the result as money / scale */
1055-
amount = DirectFunctionCall1(int8_numeric, Int64GetDatum(money));
1056-
numeric_scale = DirectFunctionCall1(int8_numeric, Int64GetDatum(scale));
1057-
quotient = DirectFunctionCall2(numeric_div, amount, numeric_scale);
1047+
/* scale appropriately, if needed */
1048+
if (fpoint > 0)
1049+
{
1050+
int64 scale;
1051+
int i;
1052+
Datum numeric_scale;
1053+
Datum quotient;
1054+
1055+
/* compute required scale factor */
1056+
scale = 1;
1057+
for (i = 0; i < fpoint; i++)
1058+
scale *= 10;
1059+
numeric_scale = DirectFunctionCall1(int8_numeric,
1060+
Int64GetDatum(scale));
10581061

1059-
/* forcibly round to exactly the intended number of digits */
1060-
result = DatumGetNumeric(DirectFunctionCall2(numeric_round,
1061-
quotient,
1062-
Int32GetDatum(fpoint)));
1062+
/*
1063+
* Given integral inputs approaching INT64_MAX, select_div_scale()
1064+
* might choose a result scale of zero, causing loss of fractional
1065+
* digits in the quotient. We can ensure an exact result by setting
1066+
* the dscale of either input to be at least as large as the desired
1067+
* result scale. numeric_round() will do that for us.
1068+
*/
1069+
numeric_scale = DirectFunctionCall2(numeric_round,
1070+
numeric_scale,
1071+
Int32GetDatum(fpoint));
1072+
1073+
/* Now we can safely divide ... */
1074+
quotient = DirectFunctionCall2(numeric_div, result, numeric_scale);
1075+
1076+
/* ... and forcibly round to exactly the intended number of digits */
1077+
result = DirectFunctionCall2(numeric_round,
1078+
quotient,
1079+
Int32GetDatum(fpoint));
1080+
}
10631081

1064-
PG_RETURN_NUMERIC(result);
1082+
PG_RETURN_DATUM(result);
10651083
}
10661084

10671085
/* numeric_cash()

src/test/regress/expected/money.out

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
--
22
-- MONEY
33
--
4+
-- Note that we assume lc_monetary has been set to C.
5+
--
46
CREATE TABLE money_data (m money);
57
INSERT INTO money_data VALUES ('123');
68
SELECT * FROM money_data;
@@ -476,7 +478,7 @@ SELECT (-12345678901234567)::numeric::money;
476478
-$12,345,678,901,234,567.00
477479
(1 row)
478480

479-
-- Cast from money
481+
-- Cast from money to numeric
480482
SELECT '12345678901234567'::money::numeric;
481483
numeric
482484
----------------------
@@ -489,3 +491,15 @@ SELECT '-12345678901234567'::money::numeric;
489491
-12345678901234567.00
490492
(1 row)
491493

494+
SELECT '92233720368547758.07'::money::numeric;
495+
numeric
496+
----------------------
497+
92233720368547758.07
498+
(1 row)
499+
500+
SELECT '-92233720368547758.08'::money::numeric;
501+
numeric
502+
-----------------------
503+
-92233720368547758.08
504+
(1 row)
505+

src/test/regress/sql/money.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
--
22
-- MONEY
33
--
4+
-- Note that we assume lc_monetary has been set to C.
5+
--
46

57
CREATE TABLE money_data (m money);
68

@@ -122,6 +124,8 @@ SELECT (-1234567890)::int4::money;
122124
SELECT (-12345678901234567)::int8::money;
123125
SELECT (-12345678901234567)::numeric::money;
124126

125-
-- Cast from money
127+
-- Cast from money to numeric
126128
SELECT '12345678901234567'::money::numeric;
127129
SELECT '-12345678901234567'::money::numeric;
130+
SELECT '92233720368547758.07'::money::numeric;
131+
SELECT '-92233720368547758.08'::money::numeric;

0 commit comments

Comments
 (0)