Skip to content

Commit d29a3f4

Browse files
Richard Guoguofengrichard
andcommitted
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
1 parent 409543d commit d29a3f4

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,8 @@ contain_nonstrict_functions_walker(Node *node, void *context)
11121112
return true;
11131113
if (IsA(node, BooleanTest))
11141114
return true;
1115+
if (IsA(node, JsonConstructorExpr))
1116+
return true;
11151117

11161118
/* Check other function-containing nodes */
11171119
if (check_functions_in_node(node, contain_nonstrict_functions_checker,

src/test/regress/expected/subselect.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,34 @@ fetch backward all in c1;
17831783

17841784
commit;
17851785
--
1786+
-- Check that JsonConstructorExpr is treated as non-strict, and thus can be
1787+
-- wrapped in a PlaceHolderVar
1788+
--
1789+
begin;
1790+
create temp table json_tab (a int);
1791+
insert into json_tab values (1);
1792+
explain (verbose, costs off)
1793+
select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
1794+
QUERY PLAN
1795+
---------------------------------------------------
1796+
Nested Loop Left Join
1797+
Output: t1.a, (JSON_ARRAY(1, a RETURNING json))
1798+
Join Filter: false
1799+
-> Seq Scan on pg_temp.json_tab t1
1800+
Output: t1.a
1801+
-> Result
1802+
Output: JSON_ARRAY(1, a RETURNING json)
1803+
One-Time Filter: false
1804+
(8 rows)
1805+
1806+
select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
1807+
a | json_array
1808+
---+------------
1809+
1 |
1810+
(1 row)
1811+
1812+
rollback;
1813+
--
17861814
-- Verify that we correctly flatten cases involving a subquery output
17871815
-- expression that doesn't need to be wrapped in a PlaceHolderVar
17881816
--

src/test/regress/sql/subselect.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,23 @@ fetch backward all in c1;
929929

930930
commit;
931931

932+
--
933+
-- Check that JsonConstructorExpr is treated as non-strict, and thus can be
934+
-- wrapped in a PlaceHolderVar
935+
--
936+
937+
begin;
938+
939+
create temp table json_tab (a int);
940+
insert into json_tab values (1);
941+
942+
explain (verbose, costs off)
943+
select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
944+
945+
select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
946+
947+
rollback;
948+
932949
--
933950
-- Verify that we correctly flatten cases involving a subquery output
934951
-- expression that doesn't need to be wrapped in a PlaceHolderVar

0 commit comments

Comments
 (0)