pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Money type does not detect over/underflow unlike int/int8
5+ messages / 4 participants
[nested] [flat]

* Money type does not detect over/underflow unlike int/int8
@ 2017-03-31 23:03  Yasuo Ohgaki <yohgaki@ohgaki.net>
  0 siblings, 1 reply; 5+ messages in thread

From: Yasuo Ohgaki @ 2017-03-31 23:03 UTC (permalink / raw)
  To: pgsql-bugs

Hi developers.

int/int8 detects over/underflow.

====================
yohgaki@[local] ~=> SELECT '999999999999999999999'::int8;
ERROR:  22003: value "999999999999999999999" is out of range for type bigint
行 1: SELECT '999999999999999999999'::int8;
             ^
LOCATION:  scanint8, int8.c:115
時間: 0.176 ms
====================

However, money type does not detect over/underflow and results in strange
results.

Works within range (Tested with 9.5 and 9.6)
====================
yohgaki@[local] ~=> SELECT '9999999'::money;
     money
---------------
 $9,999,999.00
(1 行)

時間: 0.209 ms
====================

Strange results with out of range (Tested with 9.5 and 9.6)
====================
yohgaki@[local] ~=> SELECT '999999999999999999999'::money;
           money
---------------------------
 $2,003,764,205,206,895.64
(1 行)

時間: 0.149 ms
yohgaki@[local] ~=> SELECT '9999999999999999999999999999999999999'::money;
           money
----------------------------
 $68,739,955,140,067,327.00
(1 行)

時間: 0.197 ms
yohgaki@[local] ~=> SELECT
'9999999999999999999999999999999999999999999999999999999'::money;
           money
----------------------------
 $53,322,619,588,066,671.64
(1 行)

時間: 0.144 ms
yohgaki@[local] ~=> SELECT
'99999999999999999999999999999999999999999999999999'::money;
            money
-----------------------------
 -$26,616,273,797,759,632.36
(1 行)

時間: 0.226 ms
====================


--
Yasuo Ohgaki
yohgaki@ohgaki.net

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Money type does not detect over/underflow unlike int/int8
@ 2017-04-04 13:56  Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  parent: Yasuo Ohgaki <yohgaki@ohgaki.net>
  0 siblings, 1 reply; 5+ messages in thread

From: Peter Eisentraut @ 2017-04-04 13:56 UTC (permalink / raw)
  To: Yasuo Ohgaki <yohgaki@ohgaki.net>; pgsql-bugs

On 3/31/17 19:03, Yasuo Ohgaki wrote:
> However, money type does not detect over/underflow and results in
> strange results.

This has been fixed in PostgreSQL 10.  Please test.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Money type does not detect over/underflow unlike int/int8
@ 2017-04-04 16:52  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2017-04-04 16:52 UTC (permalink / raw)
  To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: Yasuo Ohgaki <yohgaki@ohgaki.net>; pgsql-bugs

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> On 3/31/17 19:03, Yasuo Ohgaki wrote:
>> However, money type does not detect over/underflow and results in
>> strange results.

> This has been fixed in PostgreSQL 10.  Please test.

I think the only thing that's actually gotten done is fixing cash_in
to reject out-of-range input values.  Somebody ought to fix all the
money arithmetic functions to notice overflow, too; though I'm not
very clear what would be a good strategy in places like cash_mul_flt8,
where you couldn't count on an exact result.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Money type does not detect over/underflow unlike int/int8
@ 2017-04-06 08:56  Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Kyotaro HORIGUCHI @ 2017-04-06 08:56 UTC (permalink / raw)
  To: tgl@sss.pgh.pa.us; +Cc: peter.eisentraut@2ndquadrant.com; yohgaki@ohgaki.net; pgsql-bugs

At Tue, 04 Apr 2017 12:52:47 -0400, Tom Lane <tgl@sss.pgh.pa.us> wrote in <18086.1491324767@sss.pgh.pa.us>
> Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:
> > On 3/31/17 19:03, Yasuo Ohgaki wrote:
> >> However, money type does not detect over/underflow and results in
> >> strange results.
> 
> > This has been fixed in PostgreSQL 10.  Please test.
> 
> I think the only thing that's actually gotten done is fixing cash_in
> to reject out-of-range input values.  Somebody ought to fix all the
> money arithmetic functions to notice overflow, too; though I'm not
> very clear what would be a good strategy in places like cash_mul_flt8,
> where you couldn't count on an exact result.

I suppose that the result of them is not precisely defined, so
just converting the float into int64 will be enough, of course
after some range check. Anyway the result also cannot exceed the
limit of int64.  After that, cheking overflow is a simple job.

# than handling signed values split into two parts... maybe.

Roughly something like this.

cash_mul_flt8(PG_FUNCTION_ARGS)
{
	Cash		c = PG_GETARG_CASH(0);
	float8		f = PG_GETARG_FLOAT8(1);
	Cash		result;

	if (f >= CASH_MAX / 100 || f <= CASH_MIN / 100)
	   ereport("out of range");

	result = (Cash)f * 100;
	if ((c > 0 && CASH_MAX / c <= result) ||
	    (c < 0 && CASH_MIN / c >= result))
	   ereport("overflow");

	result *= c;
	PG_RETURN_CASH(result);
}

regards,

-- 
Kyotaro Horiguchi
NTT Open Source Software Center



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Money type does not detect over/underflow unlike int/int8
@ 2017-04-06 12:48  Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
  parent: Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
  0 siblings, 0 replies; 5+ messages in thread

From: Kyotaro HORIGUCHI @ 2017-04-06 12:48 UTC (permalink / raw)
  To: Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs; pgsql-bugs; yohgaki@ohgaki.net

 i wrote:
>       result = (Cash)f * 100;
 >       if ((c > 0 && CASH_MAX / c <= result) ||
 >           (c < 0 && CASH_MIN / c >= result))
 >          ereport("overflow");

ah... this is still wrong. Will be different not so much, though.

regards,

--
Kyotaro Horiguchi
NTT Open Source Software Center

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2017-04-06 12:48 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-03-31 23:03 Money type does not detect over/underflow unlike int/int8 Yasuo Ohgaki <yohgaki@ohgaki.net>
2017-04-04 13:56 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-04-04 16:52   ` Tom Lane <tgl@sss.pgh.pa.us>
2017-04-06 08:56     ` Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
2017-04-06 12:48       ` Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox