Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa85411

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. Forceits hand by setting the dscale of one division input to equal thenumber of fractional digits we need.In passing, rearrange the logic to not do useless work in localeswhere money values are considered integral.Per bug #15925 from Slawomir Chodnicki. Back-patch to all supportedbranches.Discussion:https://postgr.es/m/15925-da9953e2674bb5c8@postgresql.org
1 parented304d1 commitfa85411

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

‎src/backend/utils/adt/cash.c

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,36 +1033,54 @@ Datum
10331033
cash_numeric(PG_FUNCTION_ARGS)
10341034
{
10351035
Cashmoney=PG_GETARG_CASH(0);
1036-
Numericresult;
1036+
Datumresult;
10371037
intfpoint;
1038-
int64scale;
1039-
inti;
1040-
Datumamount;
1041-
Datumnumeric_scale;
1042-
Datumquotient;
10431038
structlconv*lconvert=PGLC_localeconv();
10441039

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

1050-
/* compute required scale factor */
1051-
scale=1;
1052-
for (i=0;i<fpoint;i++)
1053-
scale *=10;
1054-
1055-
/* form the result as money / scale */
1056-
amount=DirectFunctionCall1(int8_numeric,Int64GetDatum(money));
1057-
numeric_scale=DirectFunctionCall1(int8_numeric,Int64GetDatum(scale));
1058-
quotient=DirectFunctionCall2(numeric_div,amount,numeric_scale);
1045+
/* convert the integral money value to numeric */
1046+
result=DirectFunctionCall1(int8_numeric,Int64GetDatum(money));
10591047

1060-
/* forcibly round to exactly the intended number of digits */
1061-
result=DatumGetNumeric(DirectFunctionCall2(numeric_round,
1062-
quotient,
1063-
Int32GetDatum(fpoint)));
1048+
/* scale appropriately, if needed */
1049+
if (fpoint>0)
1050+
{
1051+
int64scale;
1052+
inti;
1053+
Datumnumeric_scale;
1054+
Datumquotient;
1055+
1056+
/* compute required scale factor */
1057+
scale=1;
1058+
for (i=0;i<fpoint;i++)
1059+
scale *=10;
1060+
numeric_scale=DirectFunctionCall1(int8_numeric,
1061+
Int64GetDatum(scale));
1062+
1063+
/*
1064+
* Given integral inputs approaching INT64_MAX, select_div_scale()
1065+
* might choose a result scale of zero, causing loss of fractional
1066+
* digits in the quotient. We can ensure an exact result by setting
1067+
* the dscale of either input to be at least as large as the desired
1068+
* result scale. numeric_round() will do that for us.
1069+
*/
1070+
numeric_scale=DirectFunctionCall2(numeric_round,
1071+
numeric_scale,
1072+
Int32GetDatum(fpoint));
1073+
1074+
/* Now we can safely divide ... */
1075+
quotient=DirectFunctionCall2(numeric_div,result,numeric_scale);
1076+
1077+
/* ... and forcibly round to exactly the intended number of digits */
1078+
result=DirectFunctionCall2(numeric_round,
1079+
quotient,
1080+
Int32GetDatum(fpoint));
1081+
}
10641082

1065-
PG_RETURN_NUMERIC(result);
1083+
PG_RETURN_DATUM(result);
10661084
}
10671085

10681086
/* 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
CREATETABLEmoney_data (mmoney);
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)

[8]ページ先頭

©2009-2025 Movatter.jp