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 <[email protected]>
Discussion: 
https://postgr.es/m/CAMbWs48A=vcfbtetkucokno1_0-cu0ambt0m07dm7vj1qyi...@mail.gmail.com
Backpatch-through: 16

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/e17df18a811e082a47d68263c154ae0385cd1494

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

Reply via email to