From 1d2e2603bce881dc70ef31ec93d0a670bc3a9f36 Mon Sep 17 00:00:00 2001 From: zhenglong li Date: Mon, 23 Nov 2026 10:05:14 +0800 Subject: [PATCH v7 2/2] Handle JsonExpr in FULL JOIN null-preservation check Commit 72b480e55d7 added full_join_expr_nonstrict_walker(), which mirrors contain_nonstrict_functions_walker() to decide whether a FULL JOIN merged-column operand is null-preserving. Like the latter before the previous commit, it had no case for JsonExpr, so an operand such as JSON_VALUE(..., '$' PASSING b.x AS p) was considered null-preserving because it contains a level-zero Var, although it yields a non-NULL value when b is null-extended. The same is true of a JsonExpr whose context item is NULL but which returns a domain that rejects the NULL and supplies an ON ERROR value instead. Deriving input restrictions from COALESCE(left, right) = constant could then filter out unmatched rows. Fix by applying the same JsonExpr and JsonBehavior rules as in contain_nonstrict_functions_walker(), so that the two walkers stay in sync. --- src/backend/optimizer/util/clauses.c | 20 +++++++++ src/test/regress/expected/join.out | 61 ++++++++++++++++++++++++++++ src/test/regress/sql/join.sql | 32 +++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 44394be7b21..8bf36550ca8 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1448,6 +1448,26 @@ full_join_expr_nonstrict_walker(Node *node, bool *found_nullable) return true; else if (IsA(node, JsonConstructorExpr)) return true; + else if (IsA(node, JsonExpr)) + { + JsonExpr *jsexpr = (JsonExpr *) node; + + /* see contain_nonstrict_functions_walker() */ + if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN) + return true; + if (contain_var_or_param_walker((Node *) jsexpr->passing_values, + NULL)) + return true; + /* else fall through to check the context item, path and behaviors */ + } + else if (IsA(node, JsonBehavior)) + { + /* see contain_nonstrict_functions_walker() */ + if (contain_var_or_param_walker(((JsonBehavior *) node)->expr, + NULL)) + return true; + /* else fall through to check the expression */ + } else { /* Check other function-containing nodes */ diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index 440511d21b5..fc2c2b296bb 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -6691,6 +6691,67 @@ select * from (select coalesce(x, '{0}') as x from (values (null::int[]), ('{1}' {0} | (1 row) +-- a JsonExpr isn't null-preserving in its PASSING values, nor in its context +-- item if it returns a domain that could reject the NULL +explain (costs off) +select * from (values (null::int), (1)) a(x) + full join (values (0), (1)) b(x) + on a.x = json_value(jsonb '0', '$' passing b.x as p returning int) + where coalesce(a.x, json_value(jsonb '0', '$' passing b.x as p returning int)) = 0 + order by a.x, b.x; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------- + Sort + Sort Key: "*VALUES*".column1, "*VALUES*_1".column1 + -> Hash Full Join + Hash Cond: ("*VALUES*".column1 = JSON_VALUE('0'::jsonb, '$' PASSING "*VALUES*_1".column1 AS p RETURNING integer)) + Filter: (COALESCE("*VALUES*".column1, JSON_VALUE('0'::jsonb, '$' PASSING "*VALUES*_1".column1 AS p RETURNING integer)) = 0) + -> Values Scan on "*VALUES*" + -> Hash + -> Values Scan on "*VALUES*_1" +(8 rows) + +select * from (values (null::int), (1)) a(x) + full join (values (0), (1)) b(x) + on a.x = json_value(jsonb '0', '$' passing b.x as p returning int) + where coalesce(a.x, json_value(jsonb '0', '$' passing b.x as p returning int)) = 0 + order by a.x, b.x; + x | x +---+--- + | 0 + | 1 + | +(3 rows) + +create domain fj_int_nn as int not null; +select * from (values (null::int), (1)) a(x) + full join (values ('0'::jsonb), ('1')) b(j) + on a.x = json_value(b.j, '$' returning fj_int_nn default 0 on error) + where coalesce(a.x, json_value(b.j, '$' returning fj_int_nn default 0 on error)) = 0 + order by a.x, b.j; + x | j +---+--- + | 0 + | +(2 rows) + +drop domain fj_int_nn; +-- but one depending only on its context item is null-preserving +explain (costs off) +select * from (values (null::int), (1)) a(x) + full join (values ('0'::jsonb), ('1')) b(j) + on a.x = json_value(b.j, '$' returning int) + where coalesce(a.x, json_value(b.j, '$' returning int)) = 0; + QUERY PLAN +------------------------------------------------------------------------ + Merge Full Join + -> Values Scan on "*VALUES*" + Filter: (column1 = 0) + -> Materialize + -> Values Scan on "*VALUES*_1" + Filter: (JSON_VALUE(column1, '$' RETURNING integer) = 0) +(6 rows) + -- -- check deductions from FULL JOIN clauses and COALESCE() = constant: the -- join clause's operands may appear in either order, but must come from diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 45dcd48d2b3..2376171e6b6 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -2336,6 +2336,38 @@ select * from (select coalesce(x, '{0}') as x from (values (null::int[]), ('{1}' full join (values (1), (2)) b(x) on a.x[1] = b.x where coalesce(a.x[1], b.x) = 0; +-- a JsonExpr isn't null-preserving in its PASSING values, nor in its context +-- item if it returns a domain that could reject the NULL +explain (costs off) +select * from (values (null::int), (1)) a(x) + full join (values (0), (1)) b(x) + on a.x = json_value(jsonb '0', '$' passing b.x as p returning int) + where coalesce(a.x, json_value(jsonb '0', '$' passing b.x as p returning int)) = 0 + order by a.x, b.x; + +select * from (values (null::int), (1)) a(x) + full join (values (0), (1)) b(x) + on a.x = json_value(jsonb '0', '$' passing b.x as p returning int) + where coalesce(a.x, json_value(jsonb '0', '$' passing b.x as p returning int)) = 0 + order by a.x, b.x; + +create domain fj_int_nn as int not null; + +select * from (values (null::int), (1)) a(x) + full join (values ('0'::jsonb), ('1')) b(j) + on a.x = json_value(b.j, '$' returning fj_int_nn default 0 on error) + where coalesce(a.x, json_value(b.j, '$' returning fj_int_nn default 0 on error)) = 0 + order by a.x, b.j; + +drop domain fj_int_nn; + +-- but one depending only on its context item is null-preserving +explain (costs off) +select * from (values (null::int), (1)) a(x) + full join (values ('0'::jsonb), ('1')) b(j) + on a.x = json_value(b.j, '$' returning int) + where coalesce(a.x, json_value(b.j, '$' returning int)) = 0; + -- -- check deductions from FULL JOIN clauses and COALESCE() = constant: the -- join clause's operands may appear in either order, but must come from -- 2.50.1 (Apple Git-155)