From 03ec203164119f11f0eab4c83c97a8527e2b108d Mon Sep 17 00:00:00 2001 From: Amit Langote Date: Wed, 19 Jun 2024 15:22:59 +0900 Subject: SQL/JSON: Correctly enforce the default ON EMPTY behavior Currently, when the ON EMPTY clause is not present, the ON ERROR clause (implicit or explicit) dictates the behavior when jsonpath evaluation in ExecEvalJsonExprPath() results in an empty sequence. That is an oversight in the commit 6185c9737c. This commit fixes things so that a NULL is returned instead in that case which is the default behavior when the ON EMPTY clause is not present. Reported-by: Markus Winand Discussion: https://postgr.es/m/F7DD1442-265C-4220-A603-CB0DEB77E91D%40winand.at --- src/backend/parser/parse_expr.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/backend/parser/parse_expr.c') diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 4c95986c312..00cd7358ebb 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -4418,11 +4418,11 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func) coerceJsonExprOutput(pstate, jsexpr); - if (func->on_empty) - jsexpr->on_empty = transformJsonBehavior(pstate, - func->on_empty, - JSON_BEHAVIOR_NULL, - jsexpr->returning); + /* Assume NULL ON EMPTY when ON EMPTY is not specified. */ + jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty, + JSON_BEHAVIOR_NULL, + jsexpr->returning); + /* Assume NULL ON ERROR when ON ERROR is not specified. */ jsexpr->on_error = transformJsonBehavior(pstate, func->on_error, JSON_BEHAVIOR_NULL, jsexpr->returning); @@ -4448,11 +4448,11 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func) coerceJsonExprOutput(pstate, jsexpr); - if (func->on_empty) - jsexpr->on_empty = transformJsonBehavior(pstate, - func->on_empty, - JSON_BEHAVIOR_NULL, - jsexpr->returning); + /* Assume NULL ON EMPTY when ON EMPTY is not specified. */ + jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty, + JSON_BEHAVIOR_NULL, + jsexpr->returning); + /* Assume NULL ON ERROR when ON ERROR is not specified. */ jsexpr->on_error = transformJsonBehavior(pstate, func->on_error, JSON_BEHAVIOR_NULL, jsexpr->returning); @@ -4464,6 +4464,13 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func) jsexpr->returning->typid = exprType(jsexpr->formatted_expr); jsexpr->returning->typmod = -1; } + + /* + * Assume EMPTY ON ERROR when ON ERROR is not specified. + * + * ON EMPTY cannot be specified at the top level but it can be for + * the individual columns. + */ jsexpr->on_error = transformJsonBehavior(pstate, func->on_error, JSON_BEHAVIOR_EMPTY, jsexpr->returning); -- cgit v1.2.3