From 61d7d22d4c1db9c7573d6d8e02e62468eed9a47f Mon Sep 17 00:00:00 2001 From: zhenglong li Date: Mon, 24 Aug 2026 17:53:36 +0800 Subject: [PATCH v5] 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 not. A NULL PASSING argument merely becomes a jsonpath variable containing a JSON null, so the result can still be non-NULL. And while a NULL context item or path specification normally yields a NULL result, the ON EMPTY / ON ERROR behavior can still supply a non-NULL value. Also, when the result is coerced via json_populate_type(), the NULL is still coerced to the RETURNING type; if that is a NOT NULL domain, the coercion fails, so the result is either an error (with ERROR ON ERROR, and also with the default NULL ON ERROR, as the NULL then fails the domain check too) or whatever ON ERROR supplies, e.g. [] for EMPTY ARRAY ON ERROR. 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 coerces its result to a domain type via json_populate_type(), if it has any PASSING arguments, or if either its ON EMPTY or ON ERROR behavior is other than NULL, UNKNOWN or ERROR. Otherwise, its inputs are checked as usual. Simply treating every JsonExpr as non-strict, as we do for CASE, COALESCE and similar constructs, would be correct too, but it would stop us from inlining the common case of a STRICT SQL function that just applies JSON_VALUE() or JSON_QUERY() to its argument with the default behaviors. Reviewed-by: Tomas Vondra --- src/backend/optimizer/util/clauses.c | 37 +++++ .../regress/expected/sqljson_queryfuncs.out | 146 ++++++++++++++++++ src/test/regress/sql/sqljson_queryfuncs.sql | 83 ++++++++++ 3 files changed, 266 insertions(+) diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 702fa0aefdd..20aca51efad 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1197,6 +1197,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 makes JSON_EXISTS, + * JSON_QUERY, and JSON_VALUE return NULL, except that when the result + * is coerced via json_populate_type() (use_json_coercion), the NULL + * is still coerced to the RETURNING type. If that type is a domain + * that rejects NULLs, the coercion fails, and the result is then + * either an error or whatever the ON ERROR behavior supplies -- + * neither of which is a NULL result. A NULL PASSING argument doesn't + * force a NULL result at all; it merely becomes a jsonpath variable + * containing a JSON null. + * + * So treat the construct as non-strict if it coerces the result to a + * domain type via json_populate_type(), if it has any PASSING + * arguments, or if either its ON EMPTY or ON ERROR behavior is other + * than NULL, UNKNOWN (which is the same as NULL), or ERROR. Otherwise + * just check its inputs. + */ + if (jsexpr->use_json_coercion && + get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN) + return true; + if (jsexpr->passing_values != NIL) + return true; + if (jsexpr->on_empty && + jsexpr->on_empty->btype != JSON_BEHAVIOR_NULL && + jsexpr->on_empty->btype != JSON_BEHAVIOR_UNKNOWN && + jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR) + return true; + if (jsexpr->on_error && + jsexpr->on_error->btype != JSON_BEHAVIOR_NULL && + jsexpr->on_error->btype != JSON_BEHAVIOR_UNKNOWN && + jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR) + 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..554745ac28d 100644 --- a/src/test/regress/expected/sqljson_queryfuncs.out +++ b/src/test/regress/expected/sqljson_queryfuncs.out @@ -1842,3 +1842,149 @@ 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 argument, and also for a NULL context +-- item if their ON EMPTY / ON ERROR behavior is anything other than NULL, +-- UNKNOWN, or ERROR. +-- When pulling up a subquery underneath an outer join, its JsonExpr output +-- must be wrapped in a PlaceHolderVar; this must return NULL, not '1'. +SELECT v FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_VALUE('1', '$' PASSING y AS p) v + FROM (VALUES (1), (2)) b(y)) ss ON false; + v +--- + +(1 row) + +-- A STRICT SQL function must not be inlined if its JsonExpr isn't strict in +-- the function's parameters; these must return NULL for the NULL row. +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) $$; +SELECT x, jsonexpr_strict_passing(x) FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_passing +---+------------------------- + 1 | 1 + | +(2 rows) + +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_default(x) FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_default +---+------------------------- + 1 | 1 + | +(2 rows) + +-- But one that only depends on the context item is strict, and can be inlined. +CREATE FUNCTION jsonexpr_strict_ctx(text) RETURNS text + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1::jsonb, '$') $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_ctx(x::text) FROM jsonexpr_strict_args; + QUERY PLAN +-------------------------------------------------------------- + Seq Scan on pg_temp.jsonexpr_strict_args + Output: JSON_VALUE(((x)::text)::jsonb, '$' RETURNING text) +(2 rows) + +SELECT x, jsonexpr_strict_ctx(x::text) FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_ctx +---+--------------------- + 1 | 1 + | +(2 rows) + +CREATE FUNCTION jsonexpr_strict_exists(text) RETURNS bool + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_EXISTS($1::jsonb, 'strict $.a' UNKNOWN ON ERROR) $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_exists(x::text) FROM jsonexpr_strict_args; + QUERY PLAN +---------------------------------------------------------------------------- + Seq Scan on pg_temp.jsonexpr_strict_args + Output: JSON_EXISTS(((x)::text)::jsonb, 'strict $."a"' UNKNOWN ON ERROR) +(2 rows) + +SELECT x, jsonexpr_strict_exists(x::text) FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_exists +---+------------------------ + 1 | + | +(2 rows) + +-- When RETURNING a domain, a NULL context item is still coerced to the +-- domain, and a NOT NULL domain makes that fail, so the result is then an +-- error or whatever the ON ERROR behavior supplies, even with the default +-- NULL ON ERROR (the NULL then fails the domain check too) or with ERROR ON +-- ERROR. These must return NULL for the NULL row, not a non-NULL value or +-- an error. +CREATE DOMAIN queryfuncs_jsonb_nn AS jsonb NOT NULL; +CREATE DOMAIN queryfuncs_int_nn AS int NOT NULL; +CREATE FUNCTION jsonexpr_strict_domain(jsonb) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_nn + EMPTY ARRAY ON ERROR) $$; +SELECT x, jsonexpr_strict_domain(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_domain +---+------------------------ + 1 | 1 + | +(2 rows) + +CREATE FUNCTION jsonexpr_strict_domain_null(jsonb) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_nn) $$; +SELECT x, jsonexpr_strict_domain_null(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_domain_null +---+----------------------------- + 1 | 1 + | +(2 rows) + +CREATE FUNCTION jsonexpr_strict_domain_error(jsonb) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1, '$' RETURNING queryfuncs_int_nn + ERROR ON ERROR) $$; +SELECT x, jsonexpr_strict_domain_error(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; + x | jsonexpr_strict_domain_error +---+------------------------------ + 1 | 1 + | +(2 rows) + +SELECT v FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_QUERY(y, '$' RETURNING queryfuncs_jsonb_nn + EMPTY ARRAY ON ERROR) v + FROM (VALUES (jsonb '1'), (NULL)) b(y)) ss ON false; + v +--- + +(1 row) + +SELECT v FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_QUERY(y, '$' RETURNING queryfuncs_jsonb_nn) v + FROM (VALUES (jsonb '1'), (NULL)) b(y)) ss ON false; + v +--- + +(1 row) + +DROP FUNCTION jsonexpr_strict_passing(int); +DROP FUNCTION jsonexpr_strict_default(int); +DROP FUNCTION jsonexpr_strict_ctx(text); +DROP FUNCTION jsonexpr_strict_exists(text); +DROP FUNCTION jsonexpr_strict_domain(jsonb); +DROP FUNCTION jsonexpr_strict_domain_null(jsonb); +DROP FUNCTION jsonexpr_strict_domain_error(jsonb); +DROP DOMAIN queryfuncs_jsonb_nn; +DROP DOMAIN queryfuncs_int_nn; +DROP TABLE jsonexpr_strict_args; diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql index 5c675d98235..def052cd78e 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 argument, and also for a NULL context +-- item if their ON EMPTY / ON ERROR behavior is anything other than NULL, +-- UNKNOWN, or ERROR. + +-- When pulling up a subquery underneath an outer join, its JsonExpr output +-- must be wrapped in a PlaceHolderVar; this must return NULL, not '1'. +SELECT v FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_VALUE('1', '$' PASSING y AS p) v + FROM (VALUES (1), (2)) b(y)) ss ON false; + +-- A STRICT SQL function must not be inlined if its JsonExpr isn't strict in +-- the function's parameters; these must return NULL for the NULL row. +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) $$; +SELECT x, jsonexpr_strict_passing(x) FROM jsonexpr_strict_args ORDER BY x; +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_default(x) FROM jsonexpr_strict_args ORDER BY x; + +-- But one that only depends on the context item is strict, and can be inlined. +CREATE FUNCTION jsonexpr_strict_ctx(text) RETURNS text + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1::jsonb, '$') $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_ctx(x::text) FROM jsonexpr_strict_args; +SELECT x, jsonexpr_strict_ctx(x::text) FROM jsonexpr_strict_args ORDER BY x; +CREATE FUNCTION jsonexpr_strict_exists(text) RETURNS bool + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_EXISTS($1::jsonb, 'strict $.a' UNKNOWN ON ERROR) $$; +EXPLAIN (VERBOSE, COSTS OFF) +SELECT jsonexpr_strict_exists(x::text) FROM jsonexpr_strict_args; +SELECT x, jsonexpr_strict_exists(x::text) FROM jsonexpr_strict_args ORDER BY x; + +-- When RETURNING a domain, a NULL context item is still coerced to the +-- domain, and a NOT NULL domain makes that fail, so the result is then an +-- error or whatever the ON ERROR behavior supplies, even with the default +-- NULL ON ERROR (the NULL then fails the domain check too) or with ERROR ON +-- ERROR. These must return NULL for the NULL row, not a non-NULL value or +-- an error. +CREATE DOMAIN queryfuncs_jsonb_nn AS jsonb NOT NULL; +CREATE DOMAIN queryfuncs_int_nn AS int NOT NULL; +CREATE FUNCTION jsonexpr_strict_domain(jsonb) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_nn + EMPTY ARRAY ON ERROR) $$; +SELECT x, jsonexpr_strict_domain(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; +CREATE FUNCTION jsonexpr_strict_domain_null(jsonb) RETURNS jsonb + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_QUERY($1, '$' RETURNING queryfuncs_jsonb_nn) $$; +SELECT x, jsonexpr_strict_domain_null(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; +CREATE FUNCTION jsonexpr_strict_domain_error(jsonb) RETURNS int + LANGUAGE sql IMMUTABLE STRICT + AS $$ SELECT JSON_VALUE($1, '$' RETURNING queryfuncs_int_nn + ERROR ON ERROR) $$; +SELECT x, jsonexpr_strict_domain_error(x::text::jsonb) +FROM jsonexpr_strict_args ORDER BY x; +SELECT v FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_QUERY(y, '$' RETURNING queryfuncs_jsonb_nn + EMPTY ARRAY ON ERROR) v + FROM (VALUES (jsonb '1'), (NULL)) b(y)) ss ON false; +SELECT v FROM (VALUES (1)) a +LEFT JOIN (SELECT JSON_QUERY(y, '$' RETURNING queryfuncs_jsonb_nn) v + FROM (VALUES (jsonb '1'), (NULL)) b(y)) ss ON false; + +DROP FUNCTION jsonexpr_strict_passing(int); +DROP FUNCTION jsonexpr_strict_default(int); +DROP FUNCTION jsonexpr_strict_ctx(text); +DROP FUNCTION jsonexpr_strict_exists(text); +DROP FUNCTION jsonexpr_strict_domain(jsonb); +DROP FUNCTION jsonexpr_strict_domain_null(jsonb); +DROP FUNCTION jsonexpr_strict_domain_error(jsonb); +DROP DOMAIN queryfuncs_jsonb_nn; +DROP DOMAIN queryfuncs_int_nn; +DROP TABLE jsonexpr_strict_args; -- 2.50.1 (Apple Git-155)