From f9f9334dbf698f2377727bb1f96f387e8c54d2ba Mon Sep 17 00:00:00 2001 From: zhenglong li Date: Mon, 24 Aug 2026 17:53:36 +0800 Subject: [PATCH v7 1/2] Fix planner's strictness check for JsonExpr contain_nonstrict_functions_walker() had no case for JsonExpr, so the SQL/JSON query functions JSON_EXISTS, JSON_QUERY and JSON_VALUE were implicitly treated as strict in all of their inputs. They are strict in the context item and path specification, but not in the PASSING values: a NULL PASSING value merely becomes a jsonpath variable holding a JSON null. Nor are they strict in the inputs of an ON EMPTY / ON ERROR expression, which is evaluated only when that behavior applies. Moreover, when RETURNING a domain, a NULL context item still yields a NULL that is checked against the domain's constraints; if a constraint rejects it, the result is an error or the ON ERROR value. This could lead to wrong results or spurious errors in at least two ways. First, inline_function() could inline a STRICT SQL function whose body isn't actually strict in the function's parameters, so that calls with NULL arguments returned non-NULL or failed instead of returning NULL. Second, when pulling up a subquery underneath an outer join, pullup_replace_vars() could decide that such an expression needs no PlaceHolderVar, allowing it to be evaluated above the join for unmatched rows, where the correct result is NULL. Fix by treating a JsonExpr as non-strict if it returns a domain type, or if a Var, PlaceHolderVar or Param appears in its PASSING values or in its ON EMPTY / ON ERROR expressions. Any domain is treated this way, with or without constraints, since constraints may be added after planning. Otherwise, its inputs, including the PASSING values and the behavior expressions, are checked as usual, as they may still contain other non-strict constructs such as a SubLink. That way, the common case of a STRICT SQL function applying JSON_VALUE() etc. to its argument, with constant PASSING values and behaviors, can still be inlined. Co-authored-by: Amit Langote Reviewed-by: Tomas Vondra --- src/backend/optimizer/util/clauses.c | 54 +++++++++ .../regress/expected/sqljson_queryfuncs.out | 111 ++++++++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 83 +++++++++++++ 3 files changed, 248 insertions(+) diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 4d9ff9ec27a..44394be7b21 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1057,6 +1057,23 @@ max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context) * Check clauses for nonstrict functions *****************************************************************************/ +/* + * contain_var_or_param_walker + * Does the expression contain a Var, PlaceHolderVar, or Param, that is, + * any input that could be NULL at run time? + */ +static bool +contain_var_or_param_walker(Node *node, void *context) +{ + if (node == NULL) + return false; + if (IsA(node, Var) || + IsA(node, PlaceHolderVar) || + IsA(node, Param)) + return true; + return expression_tree_walker(node, contain_var_or_param_walker, context); +} + /* * contain_nonstrict_functions * Recursively search for nonstrict functions within a clause. @@ -1201,6 +1218,43 @@ contain_nonstrict_functions_walker(Node *node, void *context) return true; else if (IsA(node, JsonConstructorExpr)) return true; + else if (IsA(node, JsonExpr)) + { + JsonExpr *jsexpr = (JsonExpr *) node; + + /* + * A NULL context item or path specification yields a NULL result + * without consulting the ON EMPTY / ON ERROR behaviors, so JsonExpr + * is strict in those inputs. The exception is a domain RETURNING + * type: the NULL is still checked against the domain's constraints, + * and if they reject it, the result is an error or the ON ERROR + * value. Since the constraints may change after planning, treat any + * domain as non-strict. + */ + if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN) + return true; + + /* + * A NULL PASSING value does not force a NULL result; it merely + * becomes a jsonpath variable holding a JSON null. + */ + if (contain_var_or_param_walker((Node *) jsexpr->passing_values, + NULL)) + return true; + } + else if (IsA(node, JsonBehavior)) + { + /* + * An ON EMPTY / ON ERROR expression is evaluated only when that + * behavior applies, so a NULL input to it does not force a NULL + * result. Otherwise fall through to check the expression as usual; + * it could still contain something we consider non-strict, such as a + * SubLink. + */ + if (contain_var_or_param_walker(((JsonBehavior *) node)->expr, + NULL)) + return true; + } else { /* Check other function-containing nodes */ diff --git a/src/test/regress/expected/sqljson_queryfuncs.out b/src/test/regress/expected/sqljson_queryfuncs.out index 19db825c8ab..9e4b44a350a 100644 --- a/src/test/regress/expected/sqljson_queryfuncs.out +++ b/src/test/regress/expected/sqljson_queryfuncs.out @@ -1842,3 +1842,114 @@ SELECT JSON_VALUE(x, '$' RETURNING jsonb) FROM (VALUES ('1'::jsonb), (NULL), ('2 2 (3 rows) +-- Test the planner's strictness check for JsonExpr. JSON_VALUE() etc. can +-- return non-NULL for a NULL PASSING value or a NULL input to an ON EMPTY / +-- ON ERROR expression, and, when RETURNING a domain, for a NULL context item. +CREATE DOMAIN queryfuncs_jsonb_nn AS jsonb NOT NULL; +CREATE DOMAIN queryfuncs_int_plain AS int; +-- When a subquery is pulled up underneath an outer join, such expressions +-- must be wrapped in PlaceHolderVars; all of these must be NULL. +SELECT ss.* FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_VALUE(jsonb '1', '$' PASSING y AS p) passing, + JSON_VALUE(jsonb '{}', '$.a' RETURNING int + DEFAULT num_nulls((SELECT y)) ON EMPTY) sublink, + JSON_QUERY(j, '$' RETURNING queryfuncs_jsonb_nn + EMPTY ARRAY ON ERROR) domain + FROM (VALUES (1, jsonb '1'), (2, NULL)) b(y, j)) ss ON false; + passing | sublink | domain +---------+---------+-------- + | | +(1 row) + +-- A STRICT SQL function must not be inlined if its body isn't strict in the +-- function's parameters; these must return NULL for the NULL row, not a +-- non-NULL value. +CREATE TEMP TABLE jsonexpr_strict_args (x int); +INSERT INTO jsonexpr_strict_args VALUES (NULL), (1); +CREATE FUNCTION jsonexpr_strict_passing(int) RETURNS text + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE(jsonb '1', '$' PASSING $1 AS p) $$; +CREATE FUNCTION jsonexpr_strict_default(int) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE(jsonb '1', '$' RETURNING int + DEFAULT $1 + 0 ON EMPTY) $$; +SELECT x, jsonexpr_strict_passing(x), jsonexpr_strict_default(x) +FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_passing | jsonexpr_strict_default +---+-------------------------+------------------------- + 1 | 1 | 1 + | | +(2 rows) + +-- With a NOT NULL domain, a NULL context item makes even the default NULL ON +-- ERROR raise an error, as the NULL then fails the domain check too; this must +-- return NULL for the NULL row, not an error. +CREATE FUNCTION jsonexpr_strict_domain(jsonb) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_nn) $$; +SELECT x, jsonexpr_strict_domain(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_domain +---+------------------------ + 1 | 1 + | +(2 rows) + +-- Any domain is treated as non-strict, even one without constraints, since +-- constraints may be added after planning. So this is not inlined. +CREATE FUNCTION jsonexpr_strict_domain_plain(text) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1::jsonb, '$' RETURNING queryfuncs_int_plain) $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_domain_plain(x::text) FROM jsonexpr_strict_args; + QUERY PLAN +--------------------------------------------------- + Seq Scan on pg_temp.jsonexpr_strict_args + Output: jsonexpr_strict_domain_plain((x)::text) +(2 rows) + +-- But constant PASSING values and ON EMPTY / ON ERROR behaviors, including +-- the implicit FALSE ON ERROR of JSON_EXISTS(), have no input that could be +-- NULL, so these are strict in the context item and can be inlined. +CREATE FUNCTION jsonexpr_strict_const_value(text) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1::jsonb, '$ ? (@ > $v)' PASSING 0 AS v + RETURNING int DEFAULT -1 ON EMPTY + DEFAULT -2 ON ERROR) $$; +CREATE FUNCTION jsonexpr_strict_const_query(text) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1::jsonb, '$.a' EMPTY ARRAY ON EMPTY) $$; +CREATE FUNCTION jsonexpr_strict_const_exists(text) RETURNS bool + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_EXISTS($1::jsonb, '$.a') $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_const_value(x::text), + jsonexpr_strict_const_query(x::text), + jsonexpr_strict_const_exists(x::text) +FROM jsonexpr_strict_args; + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Seq Scan on pg_temp.jsonexpr_strict_args + Output: JSON_VALUE(((x)::text)::jsonb, '$?(@ > $"v")' PASSING 0 AS v RETURNING integer DEFAULT '-1'::integer ON EMPTY DEFAULT '-2'::integer ON ERROR), JSON_QUERY(((x)::text)::jsonb, '$."a"' RETURNING jsonb WITHOUT WRAPPER KEEP QUOTES EMPTY ARRAY ON EMPTY), JSON_EXISTS(((x)::text)::jsonb, '$."a"') +(2 rows) + +SELECT x, jsonexpr_strict_const_value(x::text), + jsonexpr_strict_const_query(x::text), + jsonexpr_strict_const_exists(x::text) +FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_const_value | jsonexpr_strict_const_query | jsonexpr_strict_const_exists +---+-----------------------------+-----------------------------+------------------------------ + 1 | 1 | [] | f + | | | +(2 rows) + +DROP TABLE jsonexpr_strict_args; +DROP FUNCTION jsonexpr_strict_passing(int); +DROP FUNCTION jsonexpr_strict_default(int); +DROP FUNCTION jsonexpr_strict_domain(jsonb); +DROP FUNCTION jsonexpr_strict_domain_plain(text); +DROP FUNCTION jsonexpr_strict_const_value(text); +DROP FUNCTION jsonexpr_strict_const_query(text); +DROP FUNCTION jsonexpr_strict_const_exists(text); +DROP DOMAIN queryfuncs_jsonb_nn; +DROP DOMAIN queryfuncs_int_plain; diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql index 5c675d98235..a23eaa3bef7 100644 --- a/src/test/regress/sql/sqljson_queryfuncs.sql +++ b/src/test/regress/sql/sqljson_queryfuncs.sql @@ -625,3 +625,86 @@ SELECT JSON_VALUE(x, '$' RETURNING json) FROM (VALUES ('1'::json), ('null'), ('2 SELECT JSON_VALUE(x, '$.a' RETURNING jsonb) FROM (VALUES ('{"a": 1}'::jsonb), ('{}'), ('{"a": 2}')) v(x); SELECT JSON_VALUE(x, 'strict $.a' RETURNING jsonb) FROM (VALUES ('{"a": 1}'::jsonb), ('[]'), ('{"a": 2}')) v(x); SELECT JSON_VALUE(x, '$' RETURNING jsonb) FROM (VALUES ('1'::jsonb), (NULL), ('2')) v(x); + +-- Test the planner's strictness check for JsonExpr. JSON_VALUE() etc. can +-- return non-NULL for a NULL PASSING value or a NULL input to an ON EMPTY / +-- ON ERROR expression, and, when RETURNING a domain, for a NULL context item. +CREATE DOMAIN queryfuncs_jsonb_nn AS jsonb NOT NULL; +CREATE DOMAIN queryfuncs_int_plain AS int; + +-- When a subquery is pulled up underneath an outer join, such expressions +-- must be wrapped in PlaceHolderVars; all of these must be NULL. +SELECT ss.* FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_VALUE(jsonb '1', '$' PASSING y AS p) passing, + JSON_VALUE(jsonb '{}', '$.a' RETURNING int + DEFAULT num_nulls((SELECT y)) ON EMPTY) sublink, + JSON_QUERY(j, '$' RETURNING queryfuncs_jsonb_nn + EMPTY ARRAY ON ERROR) domain + FROM (VALUES (1, jsonb '1'), (2, NULL)) b(y, j)) ss ON false; + +-- A STRICT SQL function must not be inlined if its body isn't strict in the +-- function's parameters; these must return NULL for the NULL row, not a +-- non-NULL value. +CREATE TEMP TABLE jsonexpr_strict_args (x int); +INSERT INTO jsonexpr_strict_args VALUES (NULL), (1); +CREATE FUNCTION jsonexpr_strict_passing(int) RETURNS text + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE(jsonb '1', '$' PASSING $1 AS p) $$; +CREATE FUNCTION jsonexpr_strict_default(int) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE(jsonb '1', '$' RETURNING int + DEFAULT $1 + 0 ON EMPTY) $$; +SELECT x, jsonexpr_strict_passing(x), jsonexpr_strict_default(x) +FROM jsonexpr_strict_args ORDER BY x; + +-- With a NOT NULL domain, a NULL context item makes even the default NULL ON +-- ERROR raise an error, as the NULL then fails the domain check too; this must +-- return NULL for the NULL row, not an error. +CREATE FUNCTION jsonexpr_strict_domain(jsonb) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_nn) $$; +SELECT x, jsonexpr_strict_domain(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; + +-- Any domain is treated as non-strict, even one without constraints, since +-- constraints may be added after planning. So this is not inlined. +CREATE FUNCTION jsonexpr_strict_domain_plain(text) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1::jsonb, '$' RETURNING queryfuncs_int_plain) $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_domain_plain(x::text) FROM jsonexpr_strict_args; + +-- But constant PASSING values and ON EMPTY / ON ERROR behaviors, including +-- the implicit FALSE ON ERROR of JSON_EXISTS(), have no input that could be +-- NULL, so these are strict in the context item and can be inlined. +CREATE FUNCTION jsonexpr_strict_const_value(text) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1::jsonb, '$ ? (@ > $v)' PASSING 0 AS v + RETURNING int DEFAULT -1 ON EMPTY + DEFAULT -2 ON ERROR) $$; +CREATE FUNCTION jsonexpr_strict_const_query(text) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1::jsonb, '$.a' EMPTY ARRAY ON EMPTY) $$; +CREATE FUNCTION jsonexpr_strict_const_exists(text) RETURNS bool + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_EXISTS($1::jsonb, '$.a') $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_const_value(x::text), + jsonexpr_strict_const_query(x::text), + jsonexpr_strict_const_exists(x::text) +FROM jsonexpr_strict_args; +SELECT x, jsonexpr_strict_const_value(x::text), + jsonexpr_strict_const_query(x::text), + jsonexpr_strict_const_exists(x::text) +FROM jsonexpr_strict_args ORDER BY x; + +DROP TABLE jsonexpr_strict_args; +DROP FUNCTION jsonexpr_strict_passing(int); +DROP FUNCTION jsonexpr_strict_default(int); +DROP FUNCTION jsonexpr_strict_domain(jsonb); +DROP FUNCTION jsonexpr_strict_domain_plain(text); +DROP FUNCTION jsonexpr_strict_const_value(text); +DROP FUNCTION jsonexpr_strict_const_query(text); +DROP FUNCTION jsonexpr_strict_const_exists(text); +DROP DOMAIN queryfuncs_jsonb_nn; +DROP DOMAIN queryfuncs_int_plain; -- 2.50.1 (Apple Git-155)