agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
pgsql: Fix const-folding of JSON constructors inside a simple CASE
5+ messages / 1 participants
[nested] [flat]

* pgsql: Fix const-folding of JSON constructors inside a simple CASE
@ 2026-09-14 08:09 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 08:09 UTC (permalink / raw)
  To: pgsql-committers@lists.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 <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs48A=VCFbteTkuCoknO1_0-Cu0aMBT0M07dm7vj1QyixDg@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(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix const-folding of JSON constructors inside a simple CASE
@ 2026-09-14 08:09 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 08:09 UTC (permalink / raw)
  To: pgsql-committers@lists.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 <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs48A=VCFbteTkuCoknO1_0-Cu0aMBT0M07dm7vj1QyixDg@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_19_STABLE

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

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(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix const-folding of JSON constructors inside a simple CASE
@ 2026-09-14 08:09 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 08:09 UTC (permalink / raw)
  To: pgsql-committers@lists.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 <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAMbWs48A=VCFbteTkuCoknO1_0-Cu0aMBT0M07dm7vj1QyixDg@mail.gmail.com
Backpatch-through: 16

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/29aef0f68ac89770d8623a48d33af96530deb011

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(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix const-folding of JSON constructors inside a simple CASE
@ 2026-09-14 08:09 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 08:09 UTC (permalink / raw)
  To: pgsql-committers@lists.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 <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(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* pgsql: Fix const-folding of JSON constructors inside a simple CASE
@ 2026-09-14 08:09 Richard Guo <rguo@postgresql.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Richard Guo @ 2026-09-14 08:09 UTC (permalink / raw)
  To: pgsql-committers@lists.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 <guofenglinux@gmail.com>
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(-)



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2026-09-14 08:09 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-14 08:09 pgsql: Fix const-folding of JSON constructors inside a simple CASE Richard Guo <rguo@postgresql.org>
2026-09-14 08:09 pgsql: Fix const-folding of JSON constructors inside a simple CASE Richard Guo <rguo@postgresql.org>
2026-09-14 08:09 pgsql: Fix const-folding of JSON constructors inside a simple CASE Richard Guo <rguo@postgresql.org>
2026-09-14 08:09 pgsql: Fix const-folding of JSON constructors inside a simple CASE Richard Guo <rguo@postgresql.org>
2026-09-14 08:09 pgsql: Fix const-folding of JSON constructors inside a simple CASE Richard Guo <rguo@postgresql.org>

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