pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jacob Crell <jacobcrell@gmail.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #15763: JSON nulls not handled properly
Date: Tue, 16 Apr 2019 16:10:52 -0400
Message-ID: <8194.1555445452@sss.pgh.pa.us> (raw)
In-Reply-To: <CADvKwnbAX2w1_HjvRALF34ydwpc5zXnzOrDHtVZ44cTSQEVykg@mail.gmail.com>
References: <15763-200acd20c49eeff5@postgresql.org>
	<CAKFQuwafyUXaqmaNg8qht_MXwiTEAJMi3ByshMUbXdBF0uFs5g@mail.gmail.com>
	<CADvKwnbAX2w1_HjvRALF34ydwpc5zXnzOrDHtVZ44cTSQEVykg@mail.gmail.com>

Jacob Crell <jacobcrell@gmail.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





view thread (5+ messages)

Message-ID: <8194.1555445452@sss.pgh.pa.us>
Permalink:  ../8194.1555445452@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/8194.1555445452@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jacobcrell@gmail.com, david.g.johnston@gmail.com
  Subject: Re: BUG #15763: JSON nulls not handled properly
  In-Reply-To: <8194.1555445452@sss.pgh.pa.us>
  References: <15763-200acd20c49eeff5@postgresql.org> <CAKFQuwafyUXaqmaNg8qht_MXwiTEAJMi3ByshMUbXdBF0uFs5g@mail.gmail.com> <CADvKwnbAX2w1_HjvRALF34ydwpc5zXnzOrDHtVZ44cTSQEVykg@mail.gmail.com> <8194.1555445452@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox