Re: BUG #15763: JSON nulls not handled properly

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jacob Crell <jacobcrell(at)gmail(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #15763: JSON nulls not handled properly
Date: 2019-04-16 20:10:52
Message-ID: 8194.1555445452@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Jacob Crell <jacobcrell(at)gmail(dot)com> writes:
> Thanks for the response. I may have been a bit off in my diagnosis of what
> was going wrong. My bug report stemmed from the fact that the below returns
> different results, the first throwing an error and the second returning no
> rows:

> SELECT json_array_elements('{"key":null}'::json->'key')
> SELECT json_array_elements(null::json)

> This seems unintuitive. Is it potentially a bug?

No, because null::json is not the same thing as 'null'::json.
They're related ideas, but not interchangeable. In your
second example, json_array_elements() never gets called at all
because it's marked strict and strict functions are not invoked
on SQL nulls. In the first example, it is called and it complains
because the JSON value it's called on isn't an array, but a
scalar null value.

You could make a case that the function should have been defined
to return zero rows for a JSON-null input ... but it'd be at best
a debatable point, so we're unlikely to change the function
definition now.

If you need that behavior, it is available in the jsonb world,
with something like

regression=# SELECT jsonb_array_elements(nullif('{"key":null}'::jsonb->'key',
'null'::jsonb));
jsonb_array_elements
----------------------
(0 rows)

But nullif() doesn't work on plain json, for lack of an equality
operator :-(

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2019-04-17 02:24:19 BUG #15764: Yum repo/key name mismatch
Previous Message Jacob Crell 2019-04-16 19:57:38 Re: BUG #15763: JSON nulls not handled properly