diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2019-07-26 11:59:00 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2019-07-26 11:59:00 -0400 |
commit | 13e493cf68a4ed8a54e974c41470fab43676e67c (patch) | |
tree | d4aaccf91b844987e2d22a1c882c0d2c1fa2cdaf /src/test | |
parent | 4583605e6d1f2b95a61dfe0c1fdfba99a88e3c4a (diff) |
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
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/regress/expected/money.out | 41 | ||||
-rw-r--r-- | src/test/regress/sql/money.sql | 12 |
2 files changed, 51 insertions, 2 deletions
diff --git a/src/test/regress/expected/money.out b/src/test/regress/expected/money.out index efb48f1acd6..3911da65a23 100644 --- a/src/test/regress/expected/money.out +++ b/src/test/regress/expected/money.out @@ -1,6 +1,8 @@ -- -- MONEY -- +-- Note that we assume lc_monetary has been set to C. +-- CREATE TABLE money_data (m money); INSERT INTO money_data VALUES ('123'); SELECT * FROM money_data; @@ -223,7 +225,7 @@ SELECT '90000000000000099.00'::money / 10::smallint; $9,000,000,000,000,009.90 (1 row) --- Cast int4/int8 to money +-- Cast int4/int8/numeric to money SELECT 1234567890::money; money ------------------- @@ -274,6 +276,12 @@ SELECT 12345678901234567::int8::money; $12,345,678,901,234,567.00 (1 row) +SELECT 12345678901234567::numeric::money; + money +---------------------------- + $12,345,678,901,234,567.00 +(1 row) + SELECT (-1234567890)::int4::money; money -------------------- @@ -286,3 +294,34 @@ SELECT (-12345678901234567)::int8::money; -$12,345,678,901,234,567.00 (1 row) +SELECT (-12345678901234567)::numeric::money; + money +----------------------------- + -$12,345,678,901,234,567.00 +(1 row) + +-- Cast from money to numeric +SELECT '12345678901234567'::money::numeric; + numeric +---------------------- + 12345678901234567.00 +(1 row) + +SELECT '-12345678901234567'::money::numeric; + numeric +----------------------- + -12345678901234567.00 +(1 row) + +SELECT '92233720368547758.07'::money::numeric; + numeric +---------------------- + 92233720368547758.07 +(1 row) + +SELECT '-92233720368547758.08'::money::numeric; + numeric +----------------------- + -92233720368547758.08 +(1 row) + diff --git a/src/test/regress/sql/money.sql b/src/test/regress/sql/money.sql index f0db5fa4322..f49db0d21df 100644 --- a/src/test/regress/sql/money.sql +++ b/src/test/regress/sql/money.sql @@ -1,6 +1,8 @@ -- -- MONEY -- +-- Note that we assume lc_monetary has been set to C. +-- CREATE TABLE money_data (m money); @@ -67,7 +69,7 @@ SELECT '878.08'::money / 11::smallint; SELECT '90000000000000099.00'::money / 10::int; SELECT '90000000000000099.00'::money / 10::smallint; --- Cast int4/int8 to money +-- Cast int4/int8/numeric to money SELECT 1234567890::money; SELECT 12345678901234567::money; SELECT 123456789012345678::money; @@ -79,5 +81,13 @@ SELECT (-123456789012345678)::money; SELECT (-9223372036854775808)::money; SELECT 1234567890::int4::money; SELECT 12345678901234567::int8::money; +SELECT 12345678901234567::numeric::money; SELECT (-1234567890)::int4::money; SELECT (-12345678901234567)::int8::money; +SELECT (-12345678901234567)::numeric::money; + +-- Cast from money to numeric +SELECT '12345678901234567'::money::numeric; +SELECT '-12345678901234567'::money::numeric; +SELECT '92233720368547758.07'::money::numeric; +SELECT '-92233720368547758.08'::money::numeric; |