Treat JsonConstructorExpr as non-strict

JsonConstructorExpr can produce non-NULL output with a NULL input, so
it should be treated as a non-strict construct.  Failing to do so can
lead to incorrect query behavior.

For example, in the reported case, when pulling up a subquery that is
under an outer join, if the subquery's target list contains a
JsonConstructorExpr that uses subquery variables and it is mistakenly
treated as strict, it will be pulled up without being wrapped in a
PlaceHolderVar.  As a result, the expression will be evaluated at the
wrong place and will not be forced to null when the outer join should
do so.

Back-patch to v16 where JsonConstructorExpr was introduced.

Bug: #19046
Reported-by: Runyuan He <runyuan@berkeley.edu>
Author: Tender Wang <tndrwang@gmail.com>
Co-authored-by: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/19046-765b6602b0a8cfdf@postgresql.org
Backpatch-through: 16
This commit is contained in:
Richard Guo
2025-09-16 18:42:20 +09:00
parent cfa6cd2927
commit b63a822452
3 changed files with 47 additions and 0 deletions

View File

@ -996,6 +996,23 @@ fetch backward all in c1;
commit;
--
-- Check that JsonConstructorExpr is treated as non-strict, and thus can be
-- wrapped in a PlaceHolderVar
--
begin;
create temp table json_tab (a int);
insert into json_tab values (1);
explain (verbose, costs off)
select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
rollback;
--
-- Verify that we correctly flatten cases involving a subquery output
-- expression that doesn't need to be wrapped in a PlaceHolderVar