pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dmitry Dolgov <9erthalion6@gmail.com>
To: 민영재 <dudwo990413@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Cc: theshallow27@gmail.com
Subject: Re: BUG #19736: `JSON_OBJECT ... ABSENT ON NULL` accepts invalid keys when the value is NULL
Date: Fri, 16 Oct 2026 22:41:27 +0200
Message-ID: <atKHv08UIzPGHv91@ddolgov-thinkpadt14sgen1.rmtde.csb> (raw)
In-Reply-To: <CAP=tSTAi4iECc=5Cr0zYxQU=E-zQyQF7wrVF2MGQ=R7LmiMwkw@mail.gmail.com>
References: <19736-3a44617ea5534c77@postgresql.org>
	<CAP=tSTAi4iECc=5Cr0zYxQU=E-zQyQF7wrVF2MGQ=R7LmiMwkw@mail.gmail.com>

> On Sun, Oct 11, 2026 at 07:26:02PM +0900, 민영재 wrote:
> I reproduced both examples on master at 9e7d152488f1.
> 
> In json_build_object_worker(), the ABSENT ON NULL skip happens before
> NULL-key validation. Both json and jsonb paths also skip the scalar-key
> checks when omitting an entry without a uniqueness check.
> 
> The attached patch moves the json NULL-key check before the skip and
> validates the key type before omitting an entry in either output path.
> It avoids calling output or cast functions for omitted keys and preserves
> the existing uniqueness checks.
> 
> Regression tests cover NULL, array, composite, json and jsonb keys with
> NULL values. The new tests fail without the fix; with the patch, make
> check passes all 239 tests on macOS/arm64 with assertions enabled.

Thanks for investigating. I think it's worth to clarify the
documentation a bit -- one can argue that we don't need to verify keys
if the pair is going to be skipped, and only the wording happen to
insist on it. Currently it says:

         <replaceable>key_expression</replaceable> is a scalar expression
         defining the <acronym>JSON</acronym> key, which is
         converted to the <type>text</type> type.
         It cannot be <literal>NULL</literal> nor can it
         belong to a type that has a cast to the <type>json</type> type.

Maybe extending it to "It cannot be ..., even if the pair is going to be
eventually omitted due to ABSENT ON NULL".

It also probably makes sense to verify if there are more such examples.
E.g. looks like jsonb_object_agg_strict exhibit something similar:

    -- jsonb_object_agg_transfn_worker tests if the key is NULL
    select jsonb_object_agg_strict(NULL, NULL);
    ERROR:  22023: field name must not be null

    -- it also checks if the key is not a composite or json, if the
    -- value is not NULL
    select jsonb_object_agg_strict('{"a": 1}'::jsonb, 'value');
    ERROR:  22023: key value must be scalar, not array, composite, or json

    -- but it doesn't do so if the value is NULL
    select jsonb_object_agg_strict('{"a": 1}'::jsonb, NULL);
     jsonb_object_agg_strict
    -------------------------
     {}
    (1 row)

Regarding the proposed patch, maybe the condition on tcategory could be
factored out to not repeat it few times (including one in the
datum_to_jsonb_internal). It's also not clear to me why JSONTYPE_JSONB
is not included in json_build_object_worker, shouldn't it be included
since it has a cast to json?






view thread (10+ messages)  latest in thread

Message-ID: <atKHv08UIzPGHv91@ddolgov-thinkpadt14sgen1.rmtde.csb>
Permalink:  ../atKHv08UIzPGHv91@ddolgov-thinkpadt14sgen1.rmtde.csb/
Also on:    postgresql.org/message-id/atKHv08UIzPGHv91@ddolgov-thinkpadt14sgen1.rmtde.csb

 · 

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: 9erthalion6@gmail.com, dudwo990413@gmail.com, theshallow27@gmail.com
  Subject: Re: BUG #19736: `JSON_OBJECT ... ABSENT ON NULL` accepts invalid keys when the value is NULL
  In-Reply-To: <atKHv08UIzPGHv91@ddolgov-thinkpadt14sgen1.rmtde.csb>
  References: <19736-3a44617ea5534c77@postgresql.org> <CAP=tSTAi4iECc=5Cr0zYxQU=E-zQyQF7wrVF2MGQ=R7LmiMwkw@mail.gmail.com> <atKHv08UIzPGHv91@ddolgov-thinkpadt14sgen1.rmtde.csb>

* 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