Re: [***DKIM violation***] Re: BUG #19418: SQL/JSON JSON_VALUE() does not conform to ISO/IEC 9075-2:2023(E) 6.34 <JSON value constructor>

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Staroverov Ilja <i(dot)staroverov(at)ftdata(dot)ru>
Cc: Richard Guo <guofenglinux(at)gmail(dot)com>, Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>, Melanie Plageman <melanieplageman(at)gmail(dot)com>, Nathan Bossart <nathandbossart(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Amit Langote <amitlangote09(at)gmail(dot)com>, Vik Fearing <vik(at)postgresfriends(dot)org>, "lukas(dot)eder(at)gmail(dot)com" <lukas(dot)eder(at)gmail(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>, "rmt(at)lists(dot)postgresql(dot)org" <rmt(at)lists(dot)postgresql(dot)org>, Álvaro Herrera <alvherre(at)kurilemu(dot)de>, Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Subject: Re: [***DKIM violation***] Re: BUG #19418: SQL/JSON JSON_VALUE() does not conform to ISO/IEC 9075-2:2023(E) 6.34 <JSON value constructor>
Date: 2026-10-29 03:11:10
Message-ID: CACJufxGc-5438j2vg2BzsDOM2S=e6np7X1pHdLo31279wk+gPA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Oct 16, 2026 at 8:39 PM Staroverov Ilja <i(dot)staroverov(at)ftdata(dot)ru> wrote:
>
> Hi,
>
> There's another problem with the empty-array fallback of
> JSON_ARRAY(query) from 8d829f5a0, this time with domains in the
> RETURNING clause. Reproduced on master (aa7b9cf13); REL_19_STABLE has
> the same code:
>
> CREATE DOMAIN nonempty_arr AS jsonb
> CHECK (jsonb_array_length(VALUE) > 0);
> SELECT JSON_ARRAY(SELECT 1 RETURNING nonempty_arr);
> ERROR: value for domain nonempty_arr violates check constraint "nonempty_arr_check"
>

>
> One related case is not fixed by this:
>
> CREATE DOMAIN nn AS jsonb NOT NULL;
> SELECT JSON_ARRAY(SELECT 1 WHERE false RETURNING nn);
> ERROR: domain nn does not allow null values
>
> although the result should be []. The same happens with any domain
> whose constraints reject NULL, such as CHECK (VALUE IS NOT NULL). Here
> the RETURNING coercion is applied to the NULL result of the inner
> JSON_ARRAYAGG, before the COALESCE, so this is independent of the
> patch. Fixing that too would mean computing the array in json or
> jsonb and applying the RETURNING coercion once, after the COALESCE,

> which is roughly what Ayush suggested upthread. That is a larger
> change, so I kept the patch minimal, but I can rework it that way if
> that's preferred.
>

Hi.
These two issues are related.

In transformJsonArrayQueryConstructor, if we change
from
COALESCE((SELECT JSON_ARRAYAGG(a) FROM (subquery) q(a)), '[]')
to
(COALESCE((SELECT JSON_ARRAYAGG(a) FROM (subquery) q(a)), '[]'))::returning_type

Then your patch may not be needed, because the outer CoerceToDomain
already checks the final COALESCE result, whichever branch it comes
from.

Anyway, another draft/demo patch attached.
Obviously, some of the comments need to be adjusted.

--
jian
https://www.enterprisedb.com/

Attachment Content-Type Size
v2-0001-Fix-JSON_ARRAY-query-interact-with-constrained-domain.patch text/x-patch 7.5 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message David Rowley 2026-10-29 03:23:22 Re: BUG #19803: Assertion "!parallel_aware || pathnode->path.parallel_safe" in create_append_path
Previous Message Richard Guo 2026-10-29 02:42:54 Re: BUG #19765: Wrong result from a chain of LEFT JOINs since v16