]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Treat JsonConstructorExpr as non-strict
authorRichard Guo <rguo@postgresql.org>
Tue, 16 Sep 2025 09:42:20 +0000 (18:42 +0900)
committerRichard Guo <rguo@postgresql.org>
Tue, 16 Sep 2025 09:43:57 +0000 (18:43 +0900)
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

src/backend/optimizer/util/clauses.c
src/test/regress/expected/subselect.out
src/test/regress/sql/subselect.sql

index 26a3e0500866cff85a87037fa39a2c6c796b1c62..593d91454ab4639519844b6fa204b345f73dd4f9 100644 (file)
@@ -1112,6 +1112,8 @@ contain_nonstrict_functions_walker(Node *node, void *context)
                return true;
        if (IsA(node, BooleanTest))
                return true;
+       if (IsA(node, JsonConstructorExpr))
+               return true;
 
        /* Check other function-containing nodes */
        if (check_functions_in_node(node, contain_nonstrict_functions_checker,
index 1f5f159fbce3268e94ceb091924a808ce3198ce2..ff667bec8ba2f0c3b21f7cd815cfc4e6f1b485d7 100644 (file)
@@ -1783,6 +1783,34 @@ 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;
+                    QUERY PLAN                     
+---------------------------------------------------
+ Nested Loop Left Join
+   Output: t1.a, (JSON_ARRAY(1, a RETURNING json))
+   Join Filter: false
+   ->  Seq Scan on pg_temp.json_tab t1
+         Output: t1.a
+   ->  Result
+         Output: JSON_ARRAY(1, a RETURNING json)
+         One-Time Filter: false
+(8 rows)
+
+select * from json_tab t1 left join (select json_array(1, a) from json_tab t2) s on false;
+ a | json_array 
+---+------------
+ 1 | 
+(1 row)
+
+rollback;
+--
 -- Verify that we correctly flatten cases involving a subquery output
 -- expression that doesn't need to be wrapped in a PlaceHolderVar
 --
index 4a3e32c2147dcf8cf42e5fe3290713ca12de6575..d77588ea91f8d3832a2b5f5f40cee2f3830d6785 100644 (file)
@@ -929,6 +929,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