Re: to_json(NULL) should to return JSON null instead NULL

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: "Shulgin, Oleksandr" <oleksandr(dot)shulgin(at)zalando(dot)de>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: to_json(NULL) should to return JSON null instead NULL
Date: 2015-08-29 17:29:39
Message-ID: CAFj8pRArunZEiE4WHA4_Ok5ZiqD_oxwBWtKf7qUvCyZeaqV+mA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

2015-08-29 19:11 GMT+02:00 Andrew Dunstan <andrew(at)dunslane(dot)net>:

>
>
> On 08/29/2015 12:02 PM, Pavel Stehule wrote:
>
>>
>>
>> 2015-08-29 15:43 GMT+02:00 Shulgin, Oleksandr <
>> oleksandr(dot)shulgin(at)zalando(dot)de <mailto:oleksandr(dot)shulgin(at)zalando(dot)de>>:
>>
>> On Sat, Aug 29, 2015 at 3:39 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us
>> <mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us>> wrote:
>>
>> Andrew Dunstan <andrew(at)dunslane(dot)net
>> <mailto:andrew(at)dunslane(dot)net>> writes:
>> > On 08/29/2015 08:47 AM, Shulgin, Oleksandr wrote:
>> >> Given there were no loud complaints about this, the current
>> behavior
>> >> is appropriate for most users, the rest can still work
>> around using
>> >> coalesce(to_json(...), json 'null').
>>
>> > I don't think it's necessarily more correct. But I do agree
>> that it's
>> > not a good idea to change the behaviour unless there is major
>> > unhappiness with it.
>>
>> I'm not entirely convinced that JSON NULL and SQL NULL should
>> be treated
>> as the same concept, so I would say that the current behavior
>> is fine ---
>> at least when you think about it in isolation. However,
>> haven't we
>> already bought into that equivalence in these examples?
>>
>> regression=# select row_to_json(row(1,null,2));
>> row_to_json
>> ---------------------------
>> {"f1":1,"f2":null,"f3":2}
>> (1 row)
>>
>> regression=# select array_to_json(array[1,null,2]);
>> array_to_json
>> ---------------
>> [1,null,2]
>> (1 row)
>>
>> or even in to_json itself:
>>
>> regression=# select to_json(array[1,null,2]);
>> to_json
>> ------------
>> [1,null,2]
>> (1 row)
>>
>> The scalar case is definitely failing to be consistent with these.
>>
>>
>> Yes, that's my argument for correctness also: to_json() on a
>> composite object should behave like distribution of to_json()
>> calls over object/array elements.
>>
>> Is consistency a sufficient reason to change it?
>>
>>
>> Not for me.
>>
>>
>> It is bug - and it should be fixed. I agree, so this change is too strong
>> for fixing in minor version - but we can change it in unreleased major
>> versions - 9.5 and master.
>>
>
>
>
>
> No, frankly that's being far too free with the word bug. It's not even
> unambiguously incorrect.
>
> Note that all the to_json functions are strict. In this sense it's quite
> consistent. If we change it to being called on null input, what should we
> return if a null non-scalar is passed in?
>

what is correct from JSON perspective? All fields with NULL

Pavel

>
> cheers
>
> andrew
>
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2015-08-29 17:31:04 Re: On-demand running query plans using auto_explain and signals
Previous Message Andrew Dunstan 2015-08-29 17:11:46 Re: to_json(NULL) should to return JSON null instead NULL