Re: json casts

Lists: pgsql-hackers
From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: json casts
Date: 2014-05-27 19:53:53
Message-ID: 5384ED51.9090704@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I've been on the receiving end of a couple of mumbles about the fact
that the JSON rendering code ignores casts of builtin types to JSON.
This was originally done as an optimization to avoid doing cache lookups
for casts for things we knew quite well how to turn into JSON values
(unlike, say, hstore). However, there is at least one concrete case
where this has some possibly undesirable consequences, namely
timestamps. Many JSON processors, especially JavaScript/ECMAScript
processors, require timestamp values to be in ISO 8601 format, with a
'T' between the date part and the time part, and thus they barf on the
output we produce for such values.

I'm therefore wondering if we should just remove this optimization. I
don't have any performance figures, but it seems unlikely to be terribly
expensive. Perhaps in 9.5 we should look at caching a lot of the info
the json rendering code gathers, but that's something we can't look at now.

cheers

andrew


From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 19:57:00
Message-ID: 5384EE0C.3000605@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 05/27/2014 10:53 PM, Andrew Dunstan wrote:
> I've been on the receiving end of a couple of mumbles about the fact
> that the JSON rendering code ignores casts of builtin types to JSON.
> This was originally done as an optimization to avoid doing cache lookups
> for casts for things we knew quite well how to turn into JSON values
> (unlike, say, hstore). However, there is at least one concrete case
> where this has some possibly undesirable consequences, namely
> timestamps. Many JSON processors, especially JavaScript/ECMAScript
> processors, require timestamp values to be in ISO 8601 format, with a
> 'T' between the date part and the time part, and thus they barf on the
> output we produce for such values.

I don't understand what ignoring casts of builtin types to JSON means.
Can you give an example?

- Heikki


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 20:19:02
Message-ID: 5384F336.3000804@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 05/27/2014 03:57 PM, Heikki Linnakangas wrote:
> On 05/27/2014 10:53 PM, Andrew Dunstan wrote:
>> I've been on the receiving end of a couple of mumbles about the fact
>> that the JSON rendering code ignores casts of builtin types to JSON.
>> This was originally done as an optimization to avoid doing cache lookups
>> for casts for things we knew quite well how to turn into JSON values
>> (unlike, say, hstore). However, there is at least one concrete case
>> where this has some possibly undesirable consequences, namely
>> timestamps. Many JSON processors, especially JavaScript/ECMAScript
>> processors, require timestamp values to be in ISO 8601 format, with a
>> 'T' between the date part and the time part, and thus they barf on the
>> output we produce for such values.
>
> I don't understand what ignoring casts of builtin types to JSON means.
> Can you give an example?

See src/backend/utils/adt/json.c:json_categorize_type() lines 1280-1300.

When rendering some value as part of a json string, if a cast exists
from the data type to json, then the cast function is used to render the
json instead of the type's normal output function, but only if it's not
a builtin type.

cheers

andrew


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 21:00:12
Message-ID: 18174.1401224412@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
> On 05/27/2014 03:57 PM, Heikki Linnakangas wrote:
>> On 05/27/2014 10:53 PM, Andrew Dunstan wrote:
>>> I've been on the receiving end of a couple of mumbles about the fact
>>> that the JSON rendering code ignores casts of builtin types to JSON.
>>> This was originally done as an optimization to avoid doing cache lookups
>>> for casts for things we knew quite well how to turn into JSON values
>>> (unlike, say, hstore). However, there is at least one concrete case
>>> where this has some possibly undesirable consequences, namely
>>> timestamps. Many JSON processors, especially JavaScript/ECMAScript
>>> processors, require timestamp values to be in ISO 8601 format, with a
>>> 'T' between the date part and the time part, and thus they barf on the
>>> output we produce for such values.

>> I don't understand what ignoring casts of builtin types to JSON means.
>> Can you give an example?

> See src/backend/utils/adt/json.c:json_categorize_type() lines 1280-1300.

> When rendering some value as part of a json string, if a cast exists
> from the data type to json, then the cast function is used to render the
> json instead of the type's normal output function, but only if it's not
> a builtin type.

How exactly would disabling that code have any effect on timestamp
rendering? There's no cast to json from timestamps (nor any other
builtin type, except jsonb).

I'd be inclined to think a more useful answer to this issue would be to
make json.c special-case timestamps, as it already does for numerics.

regards, tom lane


From: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 21:43:40
Message-ID: 5385070C.7030803@2ndQuadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 05/27/2014 11:00 PM, Tom Lane wrote:
> Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
>> On 05/27/2014 03:57 PM, Heikki Linnakangas wrote:
>>> On 05/27/2014 10:53 PM, Andrew Dunstan wrote:
>>>> I've been on the receiving end of a couple of mumbles about the fact
>>>> that the JSON rendering code ignores casts of builtin types to JSON.
>>>> This was originally done as an optimization to avoid doing cache lookups
>>>> for casts for things we knew quite well how to turn into JSON values
>>>> (unlike, say, hstore). However, there is at least one concrete case
>>>> where this has some possibly undesirable consequences, namely
>>>> timestamps. Many JSON processors, especially JavaScript/ECMAScript
>>>> processors, require timestamp values to be in ISO 8601 format, with a
>>>> 'T' between the date part and the time part, and thus they barf on the
>>>> output we produce for such values.
>>> I don't understand what ignoring casts of builtin types to JSON means.
>>> Can you give an example?
>> See src/backend/utils/adt/json.c:json_categorize_type() lines 1280-1300.
>> When rendering some value as part of a json string, if a cast exists
>> from the data type to json, then the cast function is used to render the
>> json instead of the type's normal output function, but only if it's not
>> a builtin type.
> How exactly would disabling that code have any effect on timestamp
> rendering? There's no cast to json from timestamps (nor any other
> builtin type, except jsonb).
I think Andrews idea was, that if cast were used, one could fix the above
problem by defining a correct cast.

>
> I'd be inclined to think a more useful answer to this issue would be to
> make json.c special-case timestamps, as it already does for numerics.
>
> regards, tom lane
But I agree that special-casing the code to use the de-facto json standard
of using ISO 8601 date representation is a better solution.

Just make sure you get the TZ part right - this is another place where
PostgreSQL often differs from other systems' understanding of ISO
timestamps.

Cheers

--
Hannu Krosing
PostgreSQL Consultant
Performance, Scalability and High Availability
2ndQuadrant Nordic OÜ


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 22:34:58
Message-ID: 53851312.9070803@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 05/27/2014 05:43 PM, Hannu Krosing wrote:
> On 05/27/2014 11:00 PM, Tom Lane wrote:
>>>
>>> See src/backend/utils/adt/json.c:json_categorize_type() lines 1280-1300.
>>> When rendering some value as part of a json string, if a cast exists
>>> from the data type to json, then the cast function is used to render the
>>> json instead of the type's normal output function, but only if it's not
>>> a builtin type.
>> How exactly would disabling that code have any effect on timestamp
>> rendering? There's no cast to json from timestamps (nor any other
>> builtin type, except jsonb).
> I think Andrews idea was, that if cast were used, one could fix the above
> problem by defining a correct cast.

Right.

>
>
>> I'd be inclined to think a more useful answer to this issue would be to
>> make json.c special-case timestamps, as it already does for numerics.
>>
>>

OK, that's another approach.

> But I agree that special-casing the code to use the de-facto json standard
> of using ISO 8601 date representation is a better solution.
>
> Just make sure you get the TZ part right - this is another place where
> PostgreSQL often differs from other systems' understanding of ISO
> timestamps.

The target would be something like:

to_char($1,'\"YYYY-MM-DD"T"HH:MI:SS.USOF\"')

AIUI that should be legal ISO 8601. But I'm happy to defer to experts.

Given that this would be a hard coded behaviour change, is it too late
to do this for 9.4?

cheers

andrew


From: Stephen Frost <sfrost(at)snowman(dot)net>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 23:10:03
Message-ID: 20140527231003.GV2556@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

* Andrew Dunstan (andrew(at)dunslane(dot)net) wrote:
> >>I'd be inclined to think a more useful answer to this issue would be to
> >>make json.c special-case timestamps, as it already does for numerics.
> >>
> >>
>
> OK, that's another approach.

I'm all for doing this for JSON, but it'd sure be nice to have the
option to get actual ISO 8601 from the built-ins too...

> Given that this would be a hard coded behaviour change, is it too
> late to do this for 9.4?

No, for my 2c.

Thanks,

Stephen


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Stephen Frost <sfrost(at)snowman(dot)net>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 23:17:43
Message-ID: 5252.1401232663@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Stephen Frost <sfrost(at)snowman(dot)net> writes:
> * Andrew Dunstan (andrew(at)dunslane(dot)net) wrote:
>> Given that this would be a hard coded behaviour change, is it too
>> late to do this for 9.4?

> No, for my 2c.

If we do it by adding casts then it'd require an initdb, so I'd vote
against that for 9.4. If we just change behavior in json.c then that
objection doesn't apply, so I wouldn't complain.

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-27 23:25:46
Message-ID: 53851EFA.6010100@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 05/27/2014 07:17 PM, Tom Lane wrote:
> Stephen Frost <sfrost(at)snowman(dot)net> writes:
>> * Andrew Dunstan (andrew(at)dunslane(dot)net) wrote:
>>> Given that this would be a hard coded behaviour change, is it too
>>> late to do this for 9.4?
>> No, for my 2c.
> If we do it by adding casts then it'd require an initdb, so I'd vote
> against that for 9.4. If we just change behavior in json.c then that
> objection doesn't apply, so I wouldn't complain.
>
>

I wasn't proposing to add a cast, just to allow users to add one if they
wanted. But I'm quite happy to go with special-casing timestamps, and
leave the larger question for another time.

cheers

andrew


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-28 03:55:43
Message-ID: CA+TgmoYrmQAC12tBAwtTia+KbG_irCXat25NFoocqi0U1tMsbA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, May 27, 2014 at 5:00 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> I'd be inclined to think a more useful answer to this issue would be to
> make json.c special-case timestamps, as it already does for numerics.

I wonder if anyone besides me is nervous about changing the semantics
here. It seems like the sort of backward-compatibility break we
normally avoid. If we do make such a compatibility break, it should
certainly be release-noted.

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


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-28 06:35:16
Message-ID: 538583A4.6000109@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 05/27/2014 11:55 PM, Robert Haas wrote:
> On Tue, May 27, 2014 at 5:00 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> I'd be inclined to think a more useful answer to this issue would be to
>> make json.c special-case timestamps, as it already does for numerics.
> I wonder if anyone besides me is nervous about changing the semantics
> here. It seems like the sort of backward-compatibility break we
> normally avoid. If we do make such a compatibility break, it should
> certainly be release-noted.
>

Yes, certainly it should be.

cheers

andrew


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-05-28 22:48:09
Message-ID: 538667A9.9050901@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 05/27/2014 07:25 PM, Andrew Dunstan wrote:
>
> On 05/27/2014 07:17 PM, Tom Lane wrote:
>> Stephen Frost <sfrost(at)snowman(dot)net> writes:
>>> * Andrew Dunstan (andrew(at)dunslane(dot)net) wrote:
>>>> Given that this would be a hard coded behaviour change, is it too
>>>> late to do this for 9.4?
>>> No, for my 2c.
>> If we do it by adding casts then it'd require an initdb, so I'd vote
>> against that for 9.4. If we just change behavior in json.c then that
>> objection doesn't apply, so I wouldn't complain.
>>
>>
>
>
> I wasn't proposing to add a cast, just to allow users to add one if
> they wanted. But I'm quite happy to go with special-casing timestamps,
> and leave the larger question for another time.
>
>

Here's a draft patch. I'm still checking to see if there are other
places that need to be fixed, but I think this has the main one.

cheers

andrew

Attachment Content-Type Size
jsonts.patch text/x-patch 5.4 KB

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Stephen Frost <sfrost(at)snowman(dot)net>, Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-06-03 20:39:34
Message-ID: 538E3286.3040300@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 5/28/14, 6:48 PM, Andrew Dunstan wrote:
>
> On 05/27/2014 07:25 PM, Andrew Dunstan wrote:
>>
>> On 05/27/2014 07:17 PM, Tom Lane wrote:
>>> Stephen Frost <sfrost(at)snowman(dot)net> writes:
>>>> * Andrew Dunstan (andrew(at)dunslane(dot)net) wrote:
>>>>> Given that this would be a hard coded behaviour change, is it too
>>>>> late to do this for 9.4?
>>>> No, for my 2c.
>>> If we do it by adding casts then it'd require an initdb, so I'd vote
>>> against that for 9.4. If we just change behavior in json.c then that
>>> objection doesn't apply, so I wouldn't complain.
>>>
>>>
>>
>>
>> I wasn't proposing to add a cast, just to allow users to add one if
>> they wanted. But I'm quite happy to go with special-casing timestamps,
>> and leave the larger question for another time.
>>
>>
>
>
> Here's a draft patch. I'm still checking to see if there are other
> places that need to be fixed, but I think this has the main one.

This was solved back in the day for the xml type, which has essentially
the same requirement, by adding an ISO-8601-compatible output option to
EncodeDateTime(). See map_sql_value_to_xml_value() in xml.c. You ought
to be able to reuse that. Seems easier than routing through to_char().


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Stephen Frost <sfrost(at)snowman(dot)net>, Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-06-03 20:45:52
Message-ID: 28714.1401828352@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> This was solved back in the day for the xml type, which has essentially
> the same requirement, by adding an ISO-8601-compatible output option to
> EncodeDateTime(). See map_sql_value_to_xml_value() in xml.c. You ought
> to be able to reuse that. Seems easier than routing through to_char().

I was wondering if we didn't have another way to do that. to_char() is
squirrely enough that I really hate having any other core functionality
depending on it. +1 for changing this.

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, Stephen Frost <sfrost(at)snowman(dot)net>, Hannu Krosing <hannu(at)2ndQuadrant(dot)com>, Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json casts
Date: 2014-06-03 21:55:37
Message-ID: 538E4459.1040602@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 06/03/2014 04:45 PM, Tom Lane wrote:
> Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
>> This was solved back in the day for the xml type, which has essentially
>> the same requirement, by adding an ISO-8601-compatible output option to
>> EncodeDateTime(). See map_sql_value_to_xml_value() in xml.c. You ought
>> to be able to reuse that. Seems easier than routing through to_char().
> I was wondering if we didn't have another way to do that. to_char() is
> squirrely enough that I really hate having any other core functionality
> depending on it. +1 for changing this.
>
>

It's a bit of a pity neither of you spoke up in the 6 days since I
published the draft patch. And honestly, some of the other code invoked
by the XML code looks a bit squirrely too. But, OK, I will look at it. I
guess I can assume that the output won't contain anything that needs
escaping, so I can just add the leading and trailing quote marks without
needing to call escape_json().

cheers

andrew