pgsql: Fix const-folding of JSON constructors inside a simple CASE

Поиск
Список
Период
Сортировка
Искать

pgsql: Fix const-folding of JSON constructors inside a simple CASE

От:
Richard Guo <rguo@postgresql.org>
Дата:
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 
Discussion: https://postgr.es/m/CAMbWs48A=VCFbteTkuCoknO1_0-Cu0aMBT0M07dm7vj1QyixDg@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/27ef9097b59bbdd059194bac30708eba518ab4d4

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      |  2 ++
4 files changed, 48 insertions(+), 6 deletions(-)

FAQ