Re: BUG #19418: SQL/JSON JSON_VALUE() does not conform to ISO/IEC 9075-2:2023(E) 6.34 <JSON value constructor>
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
To: Richard Guo <guofenglinux@gmail.com>
Cc: Melanie Plageman <melanieplageman@gmail.com>, Nathan Bossart <nathandbossart@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>, Amit Langote <amitlangote09@gmail.com>, Vik Fearing <vik@postgresfriends.org>, lukas.eder@gmail.com, pgsql-bugs@lists.postgresql.org, rmt@lists.postgresql.org, Álvaro Herrera <alvherre@kurilemu.de>, Heikki Linnakangas <hlinnaka@iki.fi>
Date: 2026-05-07T14:44:52Z
Lists: pgsql-bugs
Hi, On Fri, 1 May 2026 at 06:53, Richard Guo <guofenglinux@gmail.com> wrote: > On Fri, May 1, 2026 at 9:26 AM Richard Guo <guofenglinux@gmail.com> 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
Commits
-
Enforce RETURNING typmod for empty-set JSON_ARRAY(query)
- 9d124a14b3d4 19 (unreleased) landed
-
Fix JSON_ARRAY(query) empty set handling and view deparsing
- 8d829f5a0203 19 (unreleased) landed