Re: [PATCH] Handle SQL NULL results from casts in JSON generation

From: Neil Chen <carpenter(dot)nail(dot)cz(at)gmail(dot)com>
To: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Handle SQL NULL results from casts in JSON generation
Date: 2026-11-20 06:09:33
Message-ID: CAA3qoJknBrQWz3ObHrMsyqmW-oZrtiU_V1iRyGV57qhdjrHj9Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Fri, Nov 20, 2026 at 11:36 AM Neil Chen <carpenter(dot)nail(dot)cz(at)gmail(dot)com>
wrote:

>
> With the patch, to_json('unset'::json_cast_flag) IS NULL is false.
> Adding ::json to the argument makes the same test true. Should
> to_json() and to_jsonb() return SQL NULL for this top-level case?
>
>
I lean toward returning SQL NULL here, so that's what v2 does.
It gives the same result as casting to json first, and callers can
still distinguish SQL NULL from JSON null with IS NULL or COALESCE.

Using the enum and cast from the original reproducer:

-- [null] with either patch
SELECT jsonb_build_array('unset'::json_cast_flag);

-- v1 returns f | f. v2 returns t | t.
SELECT to_json('unset'::json_cast_flag) IS NULL,
to_jsonb('unset'::json_cast_flag) IS NULL;

Would you prefer SQL NULL or JSON null for the direct calls?

make check passed with v2 on master (d94964f14f3, 243 tests).

--
Ze Chen (Neil)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v2-0001-Handle-SQL-NULL-results-from-casts-in-JSON-generation.patch text/x-patch 10.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2026-11-20 06:16:50 Re: psql: avoid over-reading unterminated prompt escapes
Previous Message Sergey Tatarintsev 2026-11-20 06:08:41 [PATCH] Clamp auto-tuned transaction_buffers to at least 8 SLRU banks