Wrong result from JSON constructor in a simple CASE
While working on the fix for qual pushdown past grouping through a
simple CASE [1]/messages/by-id/CAHewXNkvGTOgijRLjmudpg=wz0d-J8ChY2o7ksh3Be+Q_Bxwog@mail.gmail.com, I modeled the v2 patch's handling of CaseTestExpr on
what eval_const_expressions does. Ewan Young pointed out that v2
mishandled the CaseTestExpr in a JSON constructor's coercion
expression, which led me to notice that eval_const_expressions has the
same bug:
SELECT CASE 'x' WHEN JSON_OBJECT('a': 'b' RETURNING text)
THEN 1 ELSE 0 END;
case
------
1
(1 row)
The result should be 0, since 'x' is not equal to '{"a" : "b"}'.
A JSON constructor with a RETURNING clause uses a CaseTestExpr as the
placeholder for its result in the coercion expression. When it
appears in a WHEN clause of a simple CASE with a constant test
expression, eval_const_expressions substitutes that constant for the
placeholder too, so the coercion yields the CASE's test value instead
of the constructor's result.
The same hazard exists for the elemexpr of an ArrayCoerceExpr, and
eval_const_expressions already clears case_val while processing that.
The attached patch does the same for the coercion of a
JsonConstructorExpr.
[1]: /messages/by-id/CAHewXNkvGTOgijRLjmudpg=wz0d-J8ChY2o7ksh3Be+Q_Bxwog@mail.gmail.com
- Richard
On Fri, Aug 28, 2026 at 10:29 PM Richard Guo <guofenglinux@gmail.com> wrote:
While working on the fix for qual pushdown past grouping through a
simple CASE [1], I modeled the v2 patch's handling of CaseTestExpr on
what eval_const_expressions does. Ewan Young pointed out that v2
mishandled the CaseTestExpr in a JSON constructor's coercion
expression, which led me to notice that eval_const_expressions has the
same bug:
I found another related but separate problem by grepping all the
places that handle CaseTestExpr.
contain_context_dependent_node_walker() knows that a CaseTestExpr is
expected under a simple CaseExpr or in the elemexpr of an
ArrayCoerceExpr, but not that a JsonConstructorExpr whose RETURNING
type needs a coercion also carries one in that coercion. So
inline_function() refuses to inline any SQL function that is passed
such a constructor as an argument. As an example, consider:
create function f(text) returns text language sql immutable
as $$ select $1 || '!' $$;
explain (verbose, costs off)
select f(json_object('a': x returning text)) from generate_series(1,1) x;
QUERY PLAN
--------------------------------------------------
Function Scan on pg_catalog.generate_series x
Output: f(JSON_OBJECT('a' : x RETURNING text))
Function Call: generate_series(1, 1)
(3 rows)
The attached 0002 teaches the walker about the coercion of a
JsonConstructorExpr. It's basically just mirroring what it already
does for ArrayCoerceExpr.
0001 is a bug fix and should be back-patched to v16 where JSON
constructor was introduced. 0002 is a missed optimization so maybe we
can leave it to master-only?
- Richard
Attachments:
t253591_2v2-0001-Fix-const-folding-of-JSON-constructors-inside-a-s.patchapplication/octet-stream; name=v2-0001-Fix-const-folding-of-JSON-constructors-inside-a-s.patchDownload+46-7
v2-0002-Don-t-let-JSON-constructor-coercions-block-SQL-fu.patchapplication/octet-stream; name=v2-0002-Don-t-let-JSON-constructor-coercions-block-SQL-fu.patchDownload+48-2
On Thu, Sep 3, 2026 at 11:39 AM Richard Guo <guofenglinux@gmail.com> wrote:
0001 is a bug fix and should be back-patched to v16 where JSON
constructor was introduced. 0002 is a missed optimization so maybe we
can leave it to master-only?
I've pushed 0001 and back-patched it to v16, and pushed 0002 to master
and v19 only.
- Richard