Enforce RETURNING typmod for empty-set JSON_ARRAY(query)
Richard Guo <rguo@postgresql.org>
Enforce RETURNING typmod for empty-set JSON_ARRAY(query) Commit 8d829f5a0 introduced a COALESCE wrapper around the JSON_ARRAYAGG subquery so that JSON_ARRAY(query) returns '[]' rather than NULL when the subquery yields no rows, per the SQL/JSON standard. The empty-array Const used as the COALESCE fallback was, however, built with typmod -1 and the type input function was likewise invoked with typmod -1. As a result, any length restriction from the RETURNING clause was silently bypassed on the empty-set path, while the non-empty path enforced it via the JSON_ARRAYAGG coercion. Build the empty-array Const using the typmod of the COALESCE's non-empty argument, and pass that typmod to OidInputFunctionCall as well so the value is length-checked at parse time. This makes the empty-set and non-empty-set paths behave consistently. Reported-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com> Author: Richard Guo <guofenglinux@gmail.com> Discussion: https://postgr.es/m/CAJTYsWXPYqa58YXrU+SQMVonsAhjLS46HNUMU=wO5zm9MgY3_g@mail.gmail.com
Files
| Path | Change | +/− |
|---|---|---|
| src/backend/parser/parse_expr.c | modified | +8 −4 |
| src/test/regress/expected/sqljson.out | modified | +19 −0 |
| src/test/regress/sql/sqljson.sql | modified | +8 −0 |
Discussion
- BUG #19418: SQL/JSON JSON_VALUE() does not conform to ISO/IEC 9075-2:2023(E) 6.34 <JSON value constructor> 29 messages · 2026-02-26 → 2026-05-08