BUG #8676: Bug Money JSON

Lists: pgsql-bugspgsql-hackers
From: email(at)andersonloyola(dot)com(dot)br
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #8676: Bug Money JSON
Date: 2013-12-11 14:30:04
Message-ID: E1Vqknc-0007nO-5S@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-hackers

The following bug has been logged on the website:

Bug reference: 8676
Logged by: Anderson Cristian da Silva
Email address: email(at)andersonloyola(dot)com(dot)br
PostgreSQL version: 9.3.2
Operating system: Centos 6.3 (Final)
Description:

Failed to convert money to json EX.:

postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario);
to_json
-----------------------
{"salario":$1,000.00}
(1 row)

postgres=# SELECT to_json(a)->'salario' FROM (VALUES(1000::money))
a(salario);
ERROR: invalid input syntax for type json
DETAIL: Token "$" is invalid.
CONTEXT: JSON data, line 1: {"salario":$...


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: email(at)andersonloyola(dot)com(dot)br
Cc: pgsql-bugs(at)postgresql(dot)org, Andrew Dunstan <andrew(at)dunslane(dot)net>
Subject: Re: BUG #8676: Bug Money JSON
Date: 2013-12-17 14:10:21
Message-ID: 20131217141021.GA19059@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-hackers

On Wed, Dec 11, 2013 at 02:30:04PM +0000, email(at)andersonloyola(dot)com(dot)br wrote:
> The following bug has been logged on the website:
>
> Bug reference: 8676
> Logged by: Anderson Cristian da Silva
> Email address: email(at)andersonloyola(dot)com(dot)br
> PostgreSQL version: 9.3.2
> Operating system: Centos 6.3 (Final)
> Description:
>
> Failed to convert money to json EX.:
>
>
> postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario);
> to_json
> -----------------------
> {"salario":$1,000.00}
> (1 row)
>
>
> postgres=# SELECT to_json(a)->'salario' FROM (VALUES(1000::money))
> a(salario);
> ERROR: invalid input syntax for type json
> DETAIL: Token "$" is invalid.
> CONTEXT: JSON data, line 1: {"salario":$...

Well, that is very interesting. It works for integer and text, and it
works if you ask for the entire relation, but if you ask just for that
field _and_ it is money, it fails. Sure looks like a bug to me.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ Everyone has their own god. +


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: email(at)andersonloyola(dot)com(dot)br, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #8676: Bug Money JSON
Date: 2013-12-17 14:44:00
Message-ID: 52B06330.1010503@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-hackers


On 12/17/2013 09:10 AM, Bruce Momjian wrote:
> On Wed, Dec 11, 2013 at 02:30:04PM +0000, email(at)andersonloyola(dot)com(dot)br wrote:
>> The following bug has been logged on the website:
>>
>> Bug reference: 8676
>> Logged by: Anderson Cristian da Silva
>> Email address: email(at)andersonloyola(dot)com(dot)br
>> PostgreSQL version: 9.3.2
>> Operating system: Centos 6.3 (Final)
>> Description:
>>
>> Failed to convert money to json EX.:
>>
>>
>> postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario);
>> to_json
>> -----------------------
>> {"salario":$1,000.00}
>> (1 row)
>>
>>
>> postgres=# SELECT to_json(a)->'salario' FROM (VALUES(1000::money))
>> a(salario);
>> ERROR: invalid input syntax for type json
>> DETAIL: Token "$" is invalid.
>> CONTEXT: JSON data, line 1: {"salario":$...
> Well, that is very interesting. It works for integer and text, and it
> works if you ask for the entire relation, but if you ask just for that
> field _and_ it is money, it fails. Sure looks like a bug to me.

Yeah. I'll have a look. In fact this looks like it's possibly a couple
of bugs. The JSON produced by the first query is not valid. It looks
like we might need to force money to text unconditionally.

cheers

andrew


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, email(at)andersonloyola(dot)com(dot)br, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #8676: Bug Money JSON
Date: 2013-12-17 15:31:08
Message-ID: 10585.1387294268@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-hackers

Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>> On Wed, Dec 11, 2013 at 02:30:04PM +0000, email(at)andersonloyola(dot)com(dot)br wrote:
>>> postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario);
>>> to_json
>>> -----------------------
>>> {"salario":$1,000.00}
>>> (1 row)

> Yeah. I'll have a look. In fact this looks like it's possibly a couple
> of bugs. The JSON produced by the first query is not valid. It looks
> like we might need to force money to text unconditionally.

Isn't this simply failure to quote the string properly? What drives
to_json's choice of whether to quote or not, anyway?

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, email(at)andersonloyola(dot)com(dot)br, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #8676: Bug Money JSON
Date: 2013-12-17 16:16:40
Message-ID: 52B078E8.5020707@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-hackers


On 12/17/2013 10:31 AM, Tom Lane wrote:
> Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>>> On Wed, Dec 11, 2013 at 02:30:04PM +0000, email(at)andersonloyola(dot)com(dot)br wrote:
>>>> postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario);
>>>> to_json
>>>> -----------------------
>>>> {"salario":$1,000.00}
>>>> (1 row)
>> Yeah. I'll have a look. In fact this looks like it's possibly a couple
>> of bugs. The JSON produced by the first query is not valid. It looks
>> like we might need to force money to text unconditionally.
> Isn't this simply failure to quote the string properly? What drives
> to_json's choice of whether to quote or not, anyway?
>
>

If it's numeric, it only quotes if it sees a non-numeric character,
defined thus:

/* letters appearing in numeric output that aren't valid in a JSON
number */
#define NON_NUMERIC_LETTER "NnAaIiFfTtYy"

I forgot about money when I did that - some of this dates back to 9.2.

I'm about to test the attached patch which should force money to be
quoted always.

cheers

andrew

Attachment Content-Type Size
json_money.patch text/x-patch 1.1 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Bruce Momjian <bruce(at)momjian(dot)us>, email(at)andersonloyola(dot)com(dot)br, pgsql-bugs(at)postgresql(dot)org, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: BUG #8676: Bug Money JSON
Date: 2013-12-26 18:17:10
Message-ID: 52BC72A6.2040808@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-hackers


On 12/17/2013 11:16 AM, Andrew Dunstan wrote:
>
> On 12/17/2013 10:31 AM, Tom Lane wrote:
>> Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>>>> On Wed, Dec 11, 2013 at 02:30:04PM +0000,
>>>> email(at)andersonloyola(dot)com(dot)br wrote:
>>>>> postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario);
>>>>> to_json
>>>>> -----------------------
>>>>> {"salario":$1,000.00}
>>>>> (1 row)
>>> Yeah. I'll have a look. In fact this looks like it's possibly a couple
>>> of bugs. The JSON produced by the first query is not valid. It looks
>>> like we might need to force money to text unconditionally.
>> Isn't this simply failure to quote the string properly? What drives
>> to_json's choice of whether to quote or not, anyway?
>>
>>
>
>
> If it's numeric, it only quotes if it sees a non-numeric character,
> defined thus:
>
> /* letters appearing in numeric output that aren't valid in a JSON
> number */
> #define NON_NUMERIC_LETTER "NnAaIiFfTtYy"
>
>
> I forgot about money when I did that - some of this dates back to 9.2.
>
> I'm about to test the attached patch which should force money to be
> quoted always.
>
>

This turned out to be not such a good idea. Quite apart from anything
else it doesn't handle domains over money at all well.

The attached patch abandons the test described above, and instead passes
the string from the output function to the json number lexer to see if
it's a valid json number. A small adjustment to the API of that
function was required to make it suitable for this use. This seems like
a much more robust approach.

cheers

andrew

Attachment Content-Type Size
json_money2.patch text/x-patch 3.4 KB

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, email(at)andersonloyola(dot)com(dot)br, pgsql-bugs(at)postgresql(dot)org, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: BUG #8676: Bug Money JSON
Date: 2013-12-27 22:23:38
Message-ID: 52BDFDEA.3000304@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-hackers


On 12/26/2013 01:17 PM, Andrew Dunstan wrote:
>
> On 12/17/2013 11:16 AM, Andrew Dunstan wrote:
>>
>> On 12/17/2013 10:31 AM, Tom Lane wrote:
>>> Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>>>>> On Wed, Dec 11, 2013 at 02:30:04PM +0000,
>>>>> email(at)andersonloyola(dot)com(dot)br wrote:
>>>>>> postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario);
>>>>>> to_json
>>>>>> -----------------------
>>>>>> {"salario":$1,000.00}
>>>>>> (1 row)
>>>> Yeah. I'll have a look. In fact this looks like it's possibly a couple
>>>> of bugs. The JSON produced by the first query is not valid. It looks
>>>> like we might need to force money to text unconditionally.
>>> Isn't this simply failure to quote the string properly? What drives
>>> to_json's choice of whether to quote or not, anyway?
>>>
>>>
>>
>>
>> If it's numeric, it only quotes if it sees a non-numeric character,
>> defined thus:
>>
>> /* letters appearing in numeric output that aren't valid in a JSON
>> number */
>> #define NON_NUMERIC_LETTER "NnAaIiFfTtYy"
>>
>>
>> I forgot about money when I did that - some of this dates back to 9.2.
>>
>> I'm about to test the attached patch which should force money to be
>> quoted always.
>>
>>
>
>
> This turned out to be not such a good idea. Quite apart from anything
> else it doesn't handle domains over money at all well.
>
> The attached patch abandons the test described above, and instead
> passes the string from the output function to the json number lexer to
> see if it's a valid json number. A small adjustment to the API of
> that function was required to make it suitable for this use. This
> seems like a much more robust approach.
>
>

Applied.

cheers

andrew