Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wKzyN-001Sf1-31 for pgsql-bugs@arkaria.postgresql.org; Thu, 07 May 2026 14:45:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wKzyM-004bTI-2t for pgsql-bugs@arkaria.postgresql.org; Thu, 07 May 2026 14:45:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wKzyM-004bT8-1y for pgsql-bugs@lists.postgresql.org; Thu, 07 May 2026 14:45:06 +0000 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wKzyK-000000011IF-1rwa for pgsql-bugs@lists.postgresql.org; Thu, 07 May 2026 14:45:06 +0000 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-7bd5dde63dbso9306287b3.3 for ; Thu, 07 May 2026 07:45:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1778165102; cv=none; d=google.com; s=arc-20240605; b=JpFCZNEfp2PoVJ+McESGYXn31uZYNxYbCxpkYW4sy7chL3pe3Kf6NGO6VWMXr4LHYk b7ZLRnqvQYfUYzPZRawHWdeoYZYfaBx1wzGtrJ1zyKFe9UCjLABHytF/fPAirq0Y9So+ GvNDB9v/vFIBbez8EK5U/D5VW6c11YgSvtdNiB1v6XSmf282XILTcyvcrzGjo5m2/0k3 x61brle7Wj07xCtP4MuZEU5PvzYKHcd/ICGUdQP8unyYjrMi1R3Zzm23BavNLVrFmwOz m62IYKKHitnYLkVQ4Culp8j0TO+j2D9Mx6prbjvV2FstmvmUcGi97UnxPixvYJkSTeiR sMNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=TzlI0WndZexvZJ9mLQzKPPv+YseMovlwcY5UnY9z1bI=; fh=2fpSoW2a+NjTiRx3JxilZ3wz3JtGfhYZMpEMfvSEN0k=; b=eMF7XKLijtBjIT37gbyRtWBIC61OGFXfW1sjMFIw7hB00B8SEEnzwUCahJgtkkk3M0 y6f5UVpC8dy/axJX2wZeKiGM5TiIAafxXGLGCTrweGHhwSFQOmhKoYB5PXkUo744WGFz axlVYGqPdPVGt92fJizv2QEOjNzRyA0wVQ8CXpGv/qjFbo70LBdxP8KEz6aGjMomg3cE yO9yYw7tMyM1fZfblwR3ZRlo3hQHJtY+9Xj7EwZ9lS770F6i2b8FoVyYnJ08ILid/XYr ImA3Bt680FsdXkdtrAqeZslDq7IUf1FAn8l1ey2hgb7N5ZjcE7XqNsbOyUkG4IMB9j1+ 5Vbw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778165102; x=1778769902; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TzlI0WndZexvZJ9mLQzKPPv+YseMovlwcY5UnY9z1bI=; b=kubBdJgGxMzCLIQHmGLNMlmnpz0ultCtuS5OR8vJZ7OIKXU9wJXrh75LhCZIkXMSGj xfnApxp064w5YsG4KhfXkJiAK8mnhDDd/74QWgxvZXvVqvjt2dkfyjiKLwzVWR2ZQ0Oq qSH2rbk8Oo5R9DA/G086tTjzBnsUwBs408f3h1ydf6ny9UTx4I0rkoHnFQxl3w/e9g4K zkSg67nO5gbTDewtVJAIHH+l0ATF7ldQg/4UHp1NvljLPzwo9M1IH64luqbvyQGzYfAD JNcY929yCLMVk/YbCfjo5WOud5gwo7E71nZwx+DI8VAqT4FocxbcJNmepzQWnmVGyYuc p+Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778165102; x=1778769902; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=TzlI0WndZexvZJ9mLQzKPPv+YseMovlwcY5UnY9z1bI=; b=lMZcIcoJ234DuKr6LE3VOKWAEpCLTwhSgLxttNQlLVpM8srqeG7PsSLsh4+98hprjn vXKLEFODpEcDHz1p5uNkLXQVVnK3QkPkCQ3duvWB3VqwfyW0thgS2LFYvfkq/IFASXy4 uI1Wyp/LC3Pr3iMwogFUbFRTytehA+Rd60mGDLsyrEZxS9sYY1x7pZ1DIv217+GUXIgP DpjYb1HL192Ynndfkha0HigDTrDFx+3SdlBjD3MUWpOGnY4ZvIrwZzO6CGFZxDB7crZK tpKyDVhkP28M/InQY+dYKpuPNAWcG69Sy1Q/xM8lWj60fVQW8jS02BIGe4egkqjs6Poq ra5w== X-Forwarded-Encrypted: i=1; AFNElJ8uaEWiJJYF1FmRpYc/V1VmkUiXCFumsXjHDHtlpGTAzUC3VkBddAx0Iptaz+IWJy3wPP46SA+dZBD7@lists.postgresql.org X-Gm-Message-State: AOJu0YxNC7erkpFiyUatipVY5ARQc1ms7yRkhbsfdDuPSODXr1wExAoU tjYuF/EDBvZoVRSc5tRYs4u5oXcx7u49U+uTfMLoFDpfaSZ5WkX7JuF8l+zb++2yuVsLL2N6L59 ADO8h+xIpnEqqsVYmA1qFVO5kajJMsWU= X-Gm-Gg: Acq92OEl+MD7dZUrq4swZaXu6BbC826r4RcCupn05e2NTytODJjnjv6JkVNgoGfQ3eh fDiiJZ3PA9LPgNDB32jeDaB7g6MgzLuuGKeeBFMq7d8jAyEgb1Kb9bvs/pPNI2G2XoMDJ6ICQ4n IzTlAHAj6F6FGfpyQGWsfHAfqkXL/sqo/rAO2mYrbKGx2VNjGUP4v6QBAYfjiQFqRoiXVzFeNkl 6/sgCLfwdiDeTuyIjL/uKx82Im1ypS4w6LrNACczWNiGj4X12Jx/nRh1SZ5toZMVMzw7AL++fL+ rrJOBeDeVsNLMhIGFp3ICizPsfdos3RdSJEDnbgrQZyZGITWlw== X-Received: by 2002:a05:690c:113:b0:7bf:ff7:ea72 with SMTP id 00721157ae682-7bf0ff7ec09mr15726827b3.34.1778165102188; Thu, 07 May 2026 07:45:02 -0700 (PDT) MIME-Version: 1.0 References: <501040.1772433449@sss.pgh.pa.us> <485641.1776738632@sss.pgh.pa.us> In-Reply-To: From: Ayush Tiwari Date: Thu, 7 May 2026 20:14:52 +0530 X-Gm-Features: AVHnY4IS3EPrsy6Q3eJ56hUwtCWc4QRWt_dmhr6InUjfcHU4keJ2ApTuAS5xVUM Message-ID: Subject: Re: BUG #19418: SQL/JSON JSON_VALUE() does not conform to ISO/IEC 9075-2:2023(E) 6.34 To: Richard Guo Cc: Melanie Plageman , Nathan Bossart , Tom Lane , Amit Langote , Vik Fearing , lukas.eder@gmail.com, pgsql-bugs@lists.postgresql.org, rmt@lists.postgresql.org, =?UTF-8?Q?=C3=81lvaro_Herrera?= , Heikki Linnakangas Content-Type: multipart/alternative; boundary="000000000000449cdf06513b535b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000449cdf06513b535b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, On Fri, 1 May 2026 at 06:53, Richard Guo wrote: > On Fri, May 1, 2026 at 9:26=E2=80=AFAM Richard Guo wrote: > > Thank you, Nathan and Melanie. I now have two of the three RMT > > approvals, so I believe I'm good to go. Will commit this shortly. > > Committed. > While looking at the JSON_ARRAY(query) empty-set fix, I noticed what looks like a typmod issue in the new empty-array fallback. I understand from this discussion that returning [] for an empty JSON_ARRAY(query) input is intentional and required by SQL/JSON. This report is about the RETURNING typmod not being enforced on that new [] fallback. The non-empty query form enforces the RETURNING typmod: SELECT JSON_ARRAY(SELECT 1 RETURNING varchar(1)); ERROR: value too long for type character varying(1) but the empty query form returns a value that does not fit the declared type: SELECT JSON_ARRAY(SELECT 1 WHERE false RETURNING varchar(1)); json_array ------------ [] (1 row) The same inconsistency is visible through a view: the column is stored as varchar(1), and pg_get_viewdef() shows RETURNING character varying(1), but executing the view can still return the two-character value []. The issue appears to be in transformJsonArrayQueryConstructor(): the COALESCE fallback builds the empty-array constant with typmod -1, and later eval_const_expressions() replaces JSCTOR_JSON_ARRAY_QUERY with this pre-built func expression. At that point the JsonConstructorExpr wrapper's RETURNING typmod is no longer enough to enforce varchar(1). I think the right fix is probably to make the executable expression stored in func carry the RETURNING typmod coercion. This would also match the direction sketched earlier in the thread, where the fallback was described as '[]'::[RETURNING_TYPE]. For example, coerceJsonFuncExpr() could notice same-type/different-typmod cases, and transformJsonArrayQueryConstructor() could apply it to the COALESCE expression before storing that expression in the JSCTOR_JSON_ARRAY_QUERY node. Regards, Ayush --000000000000449cdf06513b535b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,


O= n Fri, 1 May 2026 at 06:53, Richard Guo <guofenglinux@gmail.com> wrote:
On Fri, May 1, 2026 at 9:26=E2=80=AFAM Ric= hard Guo <gu= ofenglinux@gmail.com> wrote:
> Thank you, Nathan and Melanie.=C2=A0 I now have two of the three RMT > approvals, so I believe I'm good to go.=C2=A0 Will commit this sho= rtly.

Committed.

While looking at the JSON_ARRAY(q= uery) empty-set fix, I noticed what
looks like a typmod issue in the new= empty-array fallback.

I understand from this discussion that return= ing [] for an empty
JSON_ARRAY(query) input is intentional and required = by SQL/JSON.=C2=A0 This report is
about the RETURNING typmod not being e= nforced on that new [] fallback.

The non-empty query form enforces t= he RETURNING typmod:

=C2=A0 SELECT JSON_ARRAY(SELECT 1 RETURNING var= char(1));
=C2=A0 ERROR: =C2=A0value too long for type character varying(= 1)

but the empty query form returns a value that does not fit the de= clared type:

=C2=A0 SELECT JSON_ARRAY(SELECT 1 WHERE false RETURNING= varchar(1));
=C2=A0 =C2=A0json_array
=C2=A0 ------------
=C2=A0 = =C2=A0[]
=C2=A0 (1 row)

The same inconsistency is visible through= a view: the column is stored as
varchar(1), and pg_get_viewdef() shows = RETURNING character varying(1), but
executing the view can still return = the two-character value [].

The issue appears to be in transformJson= ArrayQueryConstructor(): the COALESCE
fallback builds the empty-array co= nstant with typmod -1, and later
eval_const_expressions() replaces JSCTO= R_JSON_ARRAY_QUERY with this pre-built
func expression.=C2=A0 At that po= int the JsonConstructorExpr wrapper's RETURNING
typmod is no longer = enough to enforce varchar(1).

I think the right fix is probably to m= ake the executable expression stored in
func carry the RETURNING typmod = coercion.=C2=A0 This would also match the direction
sketched earlier in = the thread, where the fallback was described as
'[]'::[RETURNING= _TYPE].=C2=A0 For example, coerceJsonFuncExpr() could notice
same-type/d= ifferent-typmod cases, and transformJsonArrayQueryConstructor() could
ap= ply it to the COALESCE expression before storing that expression in the
= JSCTOR_JSON_ARRAY_QUERY node.

Regards,
Ayush=C2=A0
--000000000000449cdf06513b535b--