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

Commit5763ef4

Browse files
committed
Fix numeric_mul() overflow due to too many digits after decimal point.
This fixes an overflow error when using the numeric * operator if theresult has more than 16383 digits after the decimal point by roundingthe result. Overflow errors should only occur if the result has toomany digits *before* the decimal point.Discussion:https://postgr.es/m/CAEZATCUmeFWCrq2dNzZpRj5+6LfN85jYiDoqm+ucSXhb9U2TbA@mail.gmail.com
1 parent2c28c68 commit5763ef4

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ struct NumericData
203203
*/
204204

205205
#defineNUMERIC_DSCALE_MASK0x3FFF
206+
#defineNUMERIC_DSCALE_MAXNUMERIC_DSCALE_MASK
206207

207208
#defineNUMERIC_SIGN(n) \
208209
(NUMERIC_IS_SHORT(n) ? \
@@ -2488,14 +2489,21 @@ numeric_mul(PG_FUNCTION_ARGS)
24882489
* Unlike add_var() and sub_var(), mul_var() will round its result. In the
24892490
* case of numeric_mul(), which is invoked for the * operator on numerics,
24902491
* we request exact representation for the product (rscale = sum(dscale of
2491-
* arg1, dscale of arg2)).
2492+
* arg1, dscale of arg2)). If the exact result has more digits after the
2493+
* decimal point than can be stored in a numeric, we round it. Rounding
2494+
* after computing the exact result ensures that the final result is
2495+
* correctly rounded (rounding in mul_var() using a truncated product
2496+
* would not guarantee this).
24922497
*/
24932498
init_var_from_num(num1,&arg1);
24942499
init_var_from_num(num2,&arg2);
24952500

24962501
init_var(&result);
24972502
mul_var(&arg1,&arg2,&result,arg1.dscale+arg2.dscale);
24982503

2504+
if (result.dscale>NUMERIC_DSCALE_MAX)
2505+
round_var(&result,NUMERIC_DSCALE_MAX);
2506+
24992507
res=make_result(&result);
25002508

25012509
free_var(&result);

‎src/test/regress/expected/numeric.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,12 @@ select 4769999999999999999999999999999999999999999999999999999999999999999999999
14981498
47699999999999999999999999999999999999999999999999999999999999999999999999999999999999985230000000000000000000000000000000000000000000000000000000000000000000000000000000000001
14991499
(1 row)
15001500

1501+
select (0.1 - 2e-16383) * (0.1 - 3e-16383) = 0.01 as rounds_to_point_zero_one;
1502+
rounds_to_point_zero_one
1503+
--------------------------
1504+
t
1505+
(1 row)
1506+
15011507
--
15021508
-- Test some corner cases for division
15031509
--

‎src/test/regress/sql/numeric.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,8 @@ select 4770999999999999999999999999999999999999999999999999999999999999999999999
864864

865865
select4769999999999999999999999999999999999999999999999999999999999999999999999999999999999999*9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
866866

867+
select (0.1- 2e-16383)* (0.1- 3e-16383)=0.01as rounds_to_point_zero_one;
868+
867869
--
868870
-- Test some corner cases for division
869871
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp