diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 20aca51..46b8606 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -1055,6 +1055,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 any Var, PlaceHolderVar, or Param, that is, + * anything whose value could turn out to 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. @@ -1203,35 +1220,39 @@ contain_nonstrict_functions_walker(Node *node, void *context) /* * 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. + * JSON_QUERY, and JSON_VALUE return NULL without consulting the ON + * EMPTY / ON ERROR behaviors, so the construct is strict in those two + * inputs, which are checked by falling through. * - * 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. + * There is one exception: if the RETURNING type is a domain, the NULL + * can still be checked against the domain's constraints. If the + * domain rejects it, the result is either an error or whatever the ON + * ERROR behavior supplies, neither of which is a NULL result. We + * can't know here whether the domain will reject NULLs when the + * expression is evaluated, so treat any domain as non-strict. */ - if (jsexpr->use_json_coercion && - get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN) + if (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) + + /* + * A NULL PASSING argument doesn't force a NULL result; it merely + * becomes a jsonpath variable containing a JSON null. So the + * construct is not strict in any variable used there. + */ + if (contain_var_or_param_walker((Node *) jsexpr->passing_values, NULL)) 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) + } + else if (IsA(node, JsonBehavior)) + { + /* + * The expression of an ON EMPTY / ON ERROR behavior is evaluated only + * if that behavior applies, so a NULL in one of its inputs doesn't + * force the enclosing JsonExpr's result to be NULL. Hence it is not + * strict in any variable used there. 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