agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix const-folding of JSON constructors inside a simple CASE
Date: Mon, 14 Sep 2026 08:09:57 +0000
Message-ID: <E1x61lF-00000000G0t-27VF@gemulon.postgresql.org> (raw)

Fix const-folding of JSON constructors inside a simple CASE

A JSON constructor with a RETURNING clause uses a CaseTestExpr as the
placeholder for its result in the coercion expression.  When such a
constructor appears in a WHEN clause of a simple CASE whose test
expression is a constant, eval_const_expressions substituted that
constant for the placeholder, so the coercion produced the CASE's test
value instead of the constructor's result.  For instance,

    CASE 'x' WHEN JSON_OBJECT('a': 'b' RETURNING text) THEN 1 ELSE 0 END

evaluated to 1.

To fix, keep case_val out of scope while simplifying the coercion, as
is already done for the elemexpr of an ArrayCoerceExpr.

Back-patch to v16, where the SQL/JSON constructor functions were
introduced.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs48A=VCFbteTkuCoknO1_0-Cu0aMBT0M07dm7vj1QyixDg@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/23088673d311ae274069f7147cf280ee2540aeea

Modified Files
--------------
src/backend/optimizer/util/clauses.c  | 35 ++++++++++++++++++++++++++++++++++-
src/include/nodes/primnodes.h         | 10 +++++-----
src/test/regress/expected/sqljson.out |  7 +++++++
src/test/regress/sql/sqljson.sql      |  3 +++
4 files changed, 49 insertions(+), 6 deletions(-)



view thread (5+ messages)  latest in thread

Message-ID: <E1x61lF-00000000G0t-27VF@gemulon.postgresql.org>
Permalink:  ../E1x61lF-00000000G0t-27VF@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1x61lF-00000000G0t-27VF@gemulon.postgresql.org

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: rguo@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix const-folding of JSON constructors inside a simple CASE
  In-Reply-To: <E1x61lF-00000000G0t-27VF@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox