]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Enforce RETURNING typmod for empty-set JSON_ARRAY(query)
authorRichard Guo <rguo@postgresql.org>
Fri, 8 May 2026 08:21:48 +0000 (17:21 +0900)
committerRichard Guo <rguo@postgresql.org>
Fri, 8 May 2026 08:21:48 +0000 (17:21 +0900)
commit9d124a14b3d4803afee5efc4f794dfb72016cb88
treee3a45747ec45206cedac7c93a7424676241a512c
parenta49b9cfd72d89a8fd68c90e84c38aa7e2ada756b
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
src/backend/parser/parse_expr.c
src/test/regress/expected/sqljson.out
src/test/regress/sql/sqljson.sql