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

Started by Neil Chen9 days ago2 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Neil Chen
carpenter.nail.cz@gmail.com

Hi,

I hit an error on 19.0 and master (a20d50a00ed) with a cast to json
that returns SQL NULL for a non-NULL input.

CREATE TYPE json_cast_flag AS ENUM ('unset');

CREATE FUNCTION json_cast_flag_to_json(json_cast_flag) RETURNS json
LANGUAGE sql AS $$ SELECT NULL::json $$;

CREATE CAST (json_cast_flag AS json)
WITH FUNCTION json_cast_flag_to_json(json_cast_flag);

SELECT json_build_array(NULL); -- [null]
SELECT ('unset'::json_cast_flag::json) IS NULL; -- t
SELECT json_build_array('unset'::json_cast_flag::json); -- [null]

SELECT json_build_array('unset'::json_cast_flag); -- error
SELECT jsonb_build_array('unset'::json_cast_flag); -- error

The last two queries fail with "function returned NULL".

I'd expect both to return [null], just like the explicit-cast query
above. The constructors already handle a SQL NULL argument this way,
and both paths use the same cast function [1]https://www.postgresql.org/docs/19/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE.

OidFunctionCall1() treats a NULL result as an error. The patch calls
the cast with FunctionCallInvoke(), checks fcinfo->isnull, and writes
a JSON null for that result. The EXECUTE check and hook still run.

COPY TO (FORMAT json) hits the same error and can leave an unfinished
array with FORCE_ARRAY. COPY already writes JSON null for SQL NULL
fields [2]https://www.postgresql.org/docs/19/sql-copy.html#SQL-COPY-PARAMS-FORMAT.

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?

The patch extends the enum cast test in sqljson.sql. make check
passed on patched master (243 tests).

Thanks,

[1]: https://www.postgresql.org/docs/19/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE
https://www.postgresql.org/docs/19/functions-json.html#FUNCTIONS-JSON-CREATION-TABLE
[2]: https://www.postgresql.org/docs/19/sql-copy.html#SQL-COPY-PARAMS-FORMAT

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

Attachments:

t259520_1
v1-0001-Handle-SQL-NULL-results-from-casts-in-JSON-generation.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Handle-SQL-NULL-results-from-casts-in-JSON-generation.patchDownload+60-24
#2Neil Chen
carpenter.nail.cz@gmail.com
In reply to: Neil Chen (#1)
Re: [PATCH] Handle SQL NULL results from casts in JSON generation

Hi,

On Fri, Nov 20, 2026 at 11:36 AM Neil Chen <carpenter.nail.cz@gmail.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/

Attachments:

t259520_2
v2-0001-Handle-SQL-NULL-results-from-casts-in-JSON-generation.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Handle-SQL-NULL-results-from-casts-in-JSON-generation.patchDownload+107-24