Re: cast from integer to money

Lists: pgsql-hackers
From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: cast from integer to money
Date: 2011-03-31 20:27:54
Message-ID: AANLkTi=zbyy2=cq8Wa3K3+=n2ynkR1kdTHECnoruWS_G@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On the open items list, we have:

conversion from integer literals to money type
http://archives.postgresql.org/pgsql-testers/2011-01/msg00000.php

What this is really complaining about is that we added a cast from
numeric to money, but not from integer to money. This isn't really a
bug: the fact that we added one cast doesn't oblige us to add two. On
the other hand, the change is probably harmless and straightforward,
and might reduce user confusion. Right now:

rhaas=# select 1::money;
ERROR: cannot cast type integer to money
LINE 1: select 1::money;
^
rhaas=# select 1.0::money;
money
-------
$1.00
(1 row)

Does anyone care enough about this to put in the effort to fix it, or
should we just let it go? Does anyone see a reason why we wouldn't
want to do this, if someone's motivated to code it up?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Robert Haas" <robertmhaas(at)gmail(dot)com>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-03-31 20:58:30
Message-ID: 4D94A4A6020000250003C043@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> wrote:

> On the open items list, we have:
>
> conversion from integer literals to money type
> http://archives.postgresql.org/pgsql-testers/2011-01/msg00000.php
>
> What this is really complaining about is that we added a cast from
> numeric to money, but not from integer to money. This isn't
> really a bug: the fact that we added one cast doesn't oblige us to
> add two. On the other hand, the change is probably harmless and
> straightforward, and might reduce user confusion.

There were reasonable arguments made why this could be a bad idea --
primarily around the question of whether '395' represented $3.95 or
$395.00. Going the other way has issues with truncation of
fractions and the number of digits which can be handled. I'm not
convinced it's sane, and I feel strongly it's too late in the cycle
to try to implement.

-Kevin


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: cast from integer to money
Date: 2011-03-31 21:30:26
Message-ID: AANLkTimVj+zHpSTmpAe7myvSxMxHy3w8j-bttUXyY0=S@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Mar 31, 2011 at 4:58 PM, Kevin Grittner
<Kevin(dot)Grittner(at)wicourts(dot)gov> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
>> On the open items list, we have:
>>
>> conversion from integer literals to money type
>> http://archives.postgresql.org/pgsql-testers/2011-01/msg00000.php
>>
>> What this is really complaining about is that we added a cast from
>> numeric to money, but not from integer to money.  This isn't
>> really a bug: the fact that we added one cast doesn't oblige us to
>> add two. On the other hand, the change is probably harmless and
>> straightforward, and might reduce user confusion.
>
> There were reasonable arguments made why this could be a bad idea --
> primarily around the question of whether '395' represented $3.95 or
> $395.00.

That's not too hard to figure out, right? If 1.00 means $1.00, 1 had
better not mean $0.01, or there will be riots in the streets.

> Going the other way has issues with truncation of
> fractions and the number of digits which can be handled.

Notice I didn't propose that.

> I'm not
> convinced it's sane, and I feel strongly it's too late in the cycle
> to try to implement.

Fair enough. Any contrary votes?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Robert Haas" <robertmhaas(at)gmail(dot)com>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-03-31 22:09:23
Message-ID: 4D94B543020000250003C090@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> wrote:

>> There were reasonable arguments made why this could be a bad idea
>> -- primarily around the question of whether '395' represented
>> $3.95 or $395.00.
>
> That's not too hard to figure out, right? If 1.00 means $1.00, 1
> had better not mean $0.01, or there will be riots in the streets.
>
>> Going the other way has issues with truncation of
>> fractions and the number of digits which can be handled.
>
> Notice I didn't propose that.

If you're just talking about going in the one direction, I might be
persuaded that's sane, especially because of the case of literals,
and especially since there are currencies where fractional amounts
aren't used in the conventional representation.

-Kevin


From: Stephen Frost <sfrost(at)snowman(dot)net>
To: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: cast from integer to money
Date: 2011-03-31 22:39:24
Message-ID: 20110331223924.GZ4116@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

* Kevin Grittner (Kevin(dot)Grittner(at)wicourts(dot)gov) wrote:
> If you're just talking about going in the one direction, I might be
> persuaded that's sane, especially because of the case of literals,
> and especially since there are currencies where fractional amounts
> aren't used in the conventional representation.

Going just integer->money, with the "1" -> "$1.00", seems completely
reasonable to me. As for being too late in the cycle.. if someone's
willing to do the work, I can't imagine it breaking anything, so I
wouldn't be against putting it in. It really should be before the
first beta tho.

Thanks,

Stephen


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Stephen Frost <sfrost(at)snowman(dot)net>
Cc: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-04-01 02:58:05
Message-ID: 411C71DF-861A-44DF-BC2D-B3DCE9418B20@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 31, 2011, at 6:39 PM, Stephen Frost <sfrost(at)snowman(dot)net> wrote:
> * Kevin Grittner (Kevin(dot)Grittner(at)wicourts(dot)gov) wrote:
>> If you're just talking about going in the one direction, I might be
>> persuaded that's sane, especially because of the case of literals,
>> and especially since there are currencies where fractional amounts
>> aren't used in the conventional representation.
>
> Going just integer->money, with the "1" -> "$1.00", seems completely
> reasonable to me. As for being too late in the cycle.. if someone's
> willing to do the work, I can't imagine it breaking anything, so I
> wouldn't be against putting it in. It really should be before the
> first beta tho.

Agreed, emphatically.

...Robert


From: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>
To: Stephen Frost <sfrost(at)snowman(dot)net>
Cc: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, Robert Haas <robertmhaas(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: cast from integer to money
Date: 2011-04-02 02:33:16
Message-ID: BANLkTinSm_FLWmPoxvK5uTC3yqgxOGottg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Mar 31, 2011 at 6:39 PM, Stephen Frost <sfrost(at)snowman(dot)net> wrote:
> Going just integer->money, with the "1" -> "$1.00", seems completely
> reasonable to me.  As for being too late in the cycle..  if someone's
> willing to do the work, I can't imagine it breaking anything, so I
> wouldn't be against putting it in.  It really should be before the
> first beta tho.

Attached is a patch which enables casting int2/int4/int8 to money,
with the same scaling as numeric uses. Hence, 1::money yields '$1.00'
. The only other numeric types (other than oid, cardinal_number,
etc.) that can't be casted directly to money are float4 and float8,
and I suspect this is intentional.

The patch includes tests, but does not update the documentation.
Should the docs be updated where it reads "Values of the numeric data
type can be cast to money. Other numeric types can be converted to
money by casting to numeric first" ?

Because this change adds rows to the pg_proc and pg_cast catalogs,
applying this patch for 9.1 will require alpha users to initdb again.
Is that acceptable?

Regards,
Joey Adams

Attachment Content-Type Size
cast-integer-to-money.patch text/x-patch 6.6 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: cast from integer to money
Date: 2011-04-04 03:23:48
Message-ID: BANLkTi=qzAhufDZ-oQfH-mt13i1FZw5giQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Apr 1, 2011 at 10:33 PM, Joseph Adams
<joeyadams3(dot)14159(at)gmail(dot)com> wrote:
> On Thu, Mar 31, 2011 at 6:39 PM, Stephen Frost <sfrost(at)snowman(dot)net> wrote:
>> Going just integer->money, with the "1" -> "$1.00", seems completely
>> reasonable to me.  As for being too late in the cycle..  if someone's
>> willing to do the work, I can't imagine it breaking anything, so I
>> wouldn't be against putting it in.  It really should be before the
>> first beta tho.
>
> Attached is a patch which enables casting int2/int4/int8 to money,
> with the same scaling as numeric uses.  Hence, 1::money yields '$1.00'

Thanks for the patch, but I think you forgot to worry about overflow:

rhaas=# select 9223372036854775807::money;
money
--------
-$1.00
(1 row)

> .  The only other numeric types (other than oid, cardinal_number,
> etc.) that can't be casted directly to money are float4 and float8,
> and I suspect this is intentional.

Agreed.

> The patch includes tests, but does not update the documentation.
> Should the docs be updated where it reads "Values of the numeric data
> type can be cast to money. Other numeric types can be converted to
> money by casting to numeric first" ?

Yes.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>, Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: cast from integer to money
Date: 2011-04-04 03:33:56
Message-ID: 9152.1301888036@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Fri, Apr 1, 2011 at 10:33 PM, Joseph Adams
> <joeyadams3(dot)14159(at)gmail(dot)com> wrote:
>> The only other numeric types (other than oid, cardinal_number,
>> etc.) that can't be casted directly to money are float4 and float8,
>> and I suspect this is intentional.

> Agreed.

BTW, I think inclusion of int2 in this patch is just a waste of code
space. The main argument for supporting these casts seems to be that
integer literals produced by the parser should be castable to money
without special pushups. But the parser never produces native int2
constants. So int4 and int8 will cover all the useful cases.

regards, tom lane


From: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: cast from integer to money
Date: 2011-04-04 05:46:17
Message-ID: BANLkTinwYmXMWwL3VdnHudeJs9H7QUnfVw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Apr 3, 2011 at 11:23 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> Thanks for the patch, but I think you forgot to worry about overflow:
>
> rhaas=# select 9223372036854775807::money;
>  money
> --------
>  -$1.00
> (1 row)

cash_in doesn't test for overflow, either (tested on 8.4.0, 9.0.3, and HEAD):

joey=# select '9223372036854775807'::money;
money
--------
-$1.00
(1 row)

Is this a bug?

Detail: unlike cash_in, numeric_cash does check for overflow
(implicitly, through its use of numeric_int8).

Joey Adams


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-04-04 11:02:09
Message-ID: 83C03EB9-7AB0-45F2-9D6C-26963FB6DDC6@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Apr 4, 2011, at 1:46 AM, Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com> wrote:
> On Sun, Apr 3, 2011 at 11:23 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
>> Thanks for the patch, but I think you forgot to worry about overflow:
>>
>> rhaas=# select 9223372036854775807::money;
>> money
>> --------
>> -$1.00
>> (1 row)
>
> cash_in doesn't test for overflow, either (tested on 8.4.0, 9.0.3, and HEAD):
>
> joey=# select '9223372036854775807'::money;
> money
> --------
> -$1.00
> (1 row)
>
> Is this a bug?

Seems like it. You have to feel sorry for the guy who deposits 9 quintillion dollars and then gets a note from the bank saying his account is overdrawn...

> Detail: unlike cash_in, numeric_cash does check for overflow
> (implicitly, through its use of numeric_int8).

Yeah.

...Robert


From: "Andrew Dunstan" <andrew(at)dunslane(dot)net>
To: "Robert Haas" <robertmhaas(at)gmail(dot)com>
Cc: "Joseph Adams" <joeyadams3(dot)14159(at)gmail(dot)com>, "Stephen Frost" <sfrost(at)snowman(dot)net>, "Kevin Grittner" <kevin(dot)grittner(at)wicourts(dot)gov>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-04-04 11:09:31
Message-ID: 55193.66.195.200.99.1301915371.squirrel@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, April 4, 2011 7:02 am, Robert Haas wrote:
> You have to feel sorry for the guy who deposits 9
> quintillion dollars and then gets a note from the bank saying his account
> is overdrawn...
>

Not really ...

cheers

andrew


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>, Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-04-04 14:58:16
Message-ID: 29676.1301929096@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Apr 4, 2011, at 1:46 AM, Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com> wrote:
>> On Sun, Apr 3, 2011 at 11:23 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
>>> Thanks for the patch, but I think you forgot to worry about overflow:

>> cash_in doesn't test for overflow, either (tested on 8.4.0, 9.0.3, and HEAD):
>> Is this a bug?

> Seems like it. You have to feel sorry for the guy who deposits 9 quintillion dollars and then gets a note from the bank saying his account is overdrawn...

I'm fairly sure that *none* of the money operations bother to check for
overflow; not only input, but arithmetic. That falls somewhere between
bug and missing feature. It's probably worth fixing but seems outside
the scope of the current patch.

In the meantime, I'm not sure whether the newly added functions should
be held to a higher standard than the existing ones. It might be better
to leave it be, and plan to fix them all at once in a consistent style.

regards, tom lane


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>, Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-04-04 15:01:15
Message-ID: BANLkTi=Ot7aFzpCvdLuL2rqNYEB5Nh+o0w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Apr 4, 2011 at 10:58 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Apr 4, 2011, at 1:46 AM, Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com> wrote:
>>> On Sun, Apr 3, 2011 at 11:23 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
>>>> Thanks for the patch, but I think you forgot to worry about overflow:
>
>>> cash_in doesn't test for overflow, either (tested on 8.4.0, 9.0.3, and HEAD):
>>> Is this a bug?
>
>> Seems like it. You have to feel sorry for the guy who deposits 9 quintillion dollars and then gets a note from the bank saying his account is overdrawn...
>
> I'm fairly sure that *none* of the money operations bother to check for
> overflow; not only input, but arithmetic.  That falls somewhere between
> bug and missing feature.  It's probably worth fixing but seems outside
> the scope of the current patch.

Oh. Bummer. Yeah, that sounds more like a TODO than an open item.

> In the meantime, I'm not sure whether the newly added functions should
> be held to a higher standard than the existing ones.  It might be better
> to leave it be, and plan to fix them all at once in a consistent style.

Maybe. The numeric->money cast does handle it though, so there's at
least some precedent for checking. If you don't want to worry about
it, I'm OK with just putting it in as-is, but I'd probably be inclined
to look for a way to fix it if we can do that without adding too much
complexity.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-04-05 05:10:56
Message-ID: BANLkTim6K=545L=+zow=T_u69mL8kX-JDg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Attached is an updated version of the patch to allow conversion of
int4/int8 directly to money. I added overflow checks, dropped
int2->cash, and updated the documentation.

- Joey

Attachment Content-Type Size
cast-integer-to-money-2.patch text/x-patch 7.7 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Stephen Frost <sfrost(at)snowman(dot)net>, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: cast from integer to money
Date: 2011-04-05 13:37:51
Message-ID: BANLkTinUxdO94FxO46+fVDgV=Z_AWNCf3g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Apr 5, 2011 at 1:10 AM, Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com> wrote:
> Attached is an updated version of the patch to allow conversion of
> int4/int8 directly to money.  I added overflow checks, dropped
> int2->cash, and updated the documentation.

Excellent, thanks.

My only gripe is that I don't think we should duplicate int8mul, so
I've changed your patch to use this incantation:

+ result = DatumGetInt64(DirectFunctionCall2(int8mul, Int64GetDatum(amount
+ Int64GetDatum(scale)));

...which is parallel to what the existing numeric -> money cast
already does. That results in a slightly different error message, but
I think that's OK: no one has complained about the numeric -> cash
error message, or the fact that the remaining functions in this module
do no overflow checking at all.

With that change, committed. Thanks for picking this one up.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company