Casts from jsonb to other types should cope with json null

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Casts from jsonb to other types should cope with json null
Date: 2024-08-01 22:51:57
Message-ID: 3851203.1722552717@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I complained in the discussion of bug #18564 [1] that it's quite
inconsistent that you can cast a jsonb null to text and get
a SQL NULL:

=# select ('{"a": null}'::jsonb)->>'a';
?column?
----------

(1 row)

but if you cast it to any other type it's an error:

=# select (('{"a": null}'::jsonb)->'a')::float8;
ERROR: cannot cast jsonb null to type double precision

I think this should be allowed and should produce a SQL NULL.
It doesn't look hard: the attached POC patch fixes this for
the float8 case only. If there's not conceptual objections
I can flesh this out to cover the other jsonb-to-XXX
cast functions.

regards, tom lane

[1] https://www.postgresql.org/message-id/flat/18564-5985f90678ed7512%40postgresql.org

Attachment Content-Type Size
poc-allow-casting-jsonb-nulls.patch text/x-diff 652 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2024-08-01 22:54:02 Re: Why is citext/regress failing on hamerkop?
Previous Message Alexander Korotkov 2024-08-01 22:51:11 Re: Asymmetric partition-wise JOIN