Commit b9d2c5c7 authored by Tom Lane's avatar Tom Lane

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
parent e829337d
...@@ -1032,13 +1032,8 @@ Datum ...@@ -1032,13 +1032,8 @@ Datum
cash_numeric(PG_FUNCTION_ARGS) cash_numeric(PG_FUNCTION_ARGS)
{ {
Cash money = PG_GETARG_CASH(0); Cash money = PG_GETARG_CASH(0);
Numeric result; Datum result;
int fpoint; int fpoint;
int64 scale;
int i;
Datum amount;
Datum numeric_scale;
Datum quotient;
struct lconv *lconvert = PGLC_localeconv(); struct lconv *lconvert = PGLC_localeconv();
/* see comments about frac_digits in cash_in() */ /* see comments about frac_digits in cash_in() */
...@@ -1046,22 +1041,45 @@ cash_numeric(PG_FUNCTION_ARGS) ...@@ -1046,22 +1041,45 @@ cash_numeric(PG_FUNCTION_ARGS)
if (fpoint < 0 || fpoint > 10) if (fpoint < 0 || fpoint > 10)
fpoint = 2; fpoint = 2;
/* compute required scale factor */ /* convert the integral money value to numeric */
scale = 1; result = DirectFunctionCall1(int8_numeric, Int64GetDatum(money));
for (i = 0; i < fpoint; i++)
scale *= 10;
/* form the result as money / scale */ /* scale appropriately, if needed */
amount = DirectFunctionCall1(int8_numeric, Int64GetDatum(money)); if (fpoint > 0)
numeric_scale = DirectFunctionCall1(int8_numeric, Int64GetDatum(scale)); {
quotient = DirectFunctionCall2(numeric_div, amount, numeric_scale); int64 scale;
int i;
Datum numeric_scale;
Datum quotient;
/* compute required scale factor */
scale = 1;
for (i = 0; i < fpoint; i++)
scale *= 10;
numeric_scale = DirectFunctionCall1(int8_numeric,
Int64GetDatum(scale));
/* forcibly round to exactly the intended number of digits */ /*
result = DatumGetNumeric(DirectFunctionCall2(numeric_round, * Given integral inputs approaching INT64_MAX, select_div_scale()
quotient, * might choose a result scale of zero, causing loss of fractional
Int32GetDatum(fpoint))); * digits in the quotient. We can ensure an exact result by setting
* the dscale of either input to be at least as large as the desired
* result scale. numeric_round() will do that for us.
*/
numeric_scale = DirectFunctionCall2(numeric_round,
numeric_scale,
Int32GetDatum(fpoint));
/* Now we can safely divide ... */
quotient = DirectFunctionCall2(numeric_div, result, numeric_scale);
/* ... and forcibly round to exactly the intended number of digits */
result = DirectFunctionCall2(numeric_round,
quotient,
Int32GetDatum(fpoint));
}
PG_RETURN_NUMERIC(result); PG_RETURN_DATUM(result);
} }
/* numeric_cash() /* numeric_cash()
......
-- --
-- MONEY -- MONEY
-- --
-- Note that we assume lc_monetary has been set to C.
--
CREATE TABLE money_data (m money); CREATE TABLE money_data (m money);
INSERT INTO money_data VALUES ('123'); INSERT INTO money_data VALUES ('123');
SELECT * FROM money_data; SELECT * FROM money_data;
...@@ -476,7 +478,7 @@ SELECT (-12345678901234567)::numeric::money; ...@@ -476,7 +478,7 @@ SELECT (-12345678901234567)::numeric::money;
-$12,345,678,901,234,567.00 -$12,345,678,901,234,567.00
(1 row) (1 row)
-- Cast from money -- Cast from money to numeric
SELECT '12345678901234567'::money::numeric; SELECT '12345678901234567'::money::numeric;
numeric numeric
---------------------- ----------------------
...@@ -489,3 +491,15 @@ SELECT '-12345678901234567'::money::numeric; ...@@ -489,3 +491,15 @@ SELECT '-12345678901234567'::money::numeric;
-12345678901234567.00 -12345678901234567.00
(1 row) (1 row)
SELECT '92233720368547758.07'::money::numeric;
numeric
----------------------
92233720368547758.07
(1 row)
SELECT '-92233720368547758.08'::money::numeric;
numeric
-----------------------
-92233720368547758.08
(1 row)
-- --
-- MONEY -- MONEY
-- --
-- Note that we assume lc_monetary has been set to C.
--
CREATE TABLE money_data (m money); CREATE TABLE money_data (m money);
...@@ -122,6 +124,8 @@ SELECT (-1234567890)::int4::money; ...@@ -122,6 +124,8 @@ SELECT (-1234567890)::int4::money;
SELECT (-12345678901234567)::int8::money; SELECT (-12345678901234567)::int8::money;
SELECT (-12345678901234567)::numeric::money; SELECT (-12345678901234567)::numeric::money;
-- Cast from money -- Cast from money to numeric
SELECT '12345678901234567'::money::numeric; SELECT '12345678901234567'::money::numeric;
SELECT '-12345678901234567'::money::numeric; SELECT '-12345678901234567'::money::numeric;
SELECT '92233720368547758.07'::money::numeric;
SELECT '-92233720368547758.08'::money::numeric;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment