From 7825a1b01e400434835253b4ff38dd96d823d454 Mon Sep 17 00:00:00 2001 From: Amit Langote Date: Thu, 20 Jul 2023 16:23:49 +0900 Subject: [PATCH] Pass constructName to transformJsonValueExpr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows it to pass to coerce_to_specific_type() the actual name corresponding to the specific JSON_* function expression being transformed, instead of the currently hardcoded string. Backpatched to 16 from the development branch to keep the code in sync across branches. Reviewed-by: Álvaro Herrera Discussion: https://postgr.es/m/CA+HiwqHu58pO3cJ7rB6ZLwUztVdG1J66xSjDdjfan5uT5NhESw@mail.gmail.com --- src/backend/parser/parse_expr.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 346fd272b6..286e85d726 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -3222,8 +3222,8 @@ makeCaseTestExpr(Node *expr) * default format otherwise. */ static Node * -transformJsonValueExpr(ParseState *pstate, JsonValueExpr *ve, - JsonFormatType default_format) +transformJsonValueExpr(ParseState *pstate, const char *constructName, + JsonValueExpr *ve, JsonFormatType default_format) { Node *expr = transformExprRecurse(pstate, (Node *) ve->raw_expr); Node *rawexpr; @@ -3233,12 +3233,8 @@ transformJsonValueExpr(ParseState *pstate, JsonValueExpr *ve, char typcategory; bool typispreferred; - /* - * Using JSON_VALUE here is slightly bogus: perhaps we need to be passed a - * JsonConstructorType so that we can use one of JSON_OBJECTAGG, etc. - */ if (exprType(expr) == UNKNOWNOID) - expr = coerce_to_specific_type(pstate, expr, TEXTOID, "JSON_VALUE"); + expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName); rawexpr = expr; exprtype = exprType(expr); @@ -3588,7 +3584,8 @@ transformJsonObjectConstructor(ParseState *pstate, JsonObjectConstructor *ctor) { JsonKeyValue *kv = castNode(JsonKeyValue, lfirst(lc)); Node *key = transformExprRecurse(pstate, (Node *) kv->key); - Node *val = transformJsonValueExpr(pstate, kv->value, + Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()", + kv->value, JS_FORMAT_DEFAULT); args = lappend(args, key); @@ -3768,7 +3765,9 @@ transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg) Oid aggtype; key = transformExprRecurse(pstate, (Node *) agg->arg->key); - val = transformJsonValueExpr(pstate, agg->arg->value, JS_FORMAT_DEFAULT); + val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()", + agg->arg->value, + JS_FORMAT_DEFAULT); args = list_make2(key, val); returning = transformJsonConstructorOutput(pstate, agg->constructor->output, @@ -3824,7 +3823,9 @@ transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg) Oid aggfnoid; Oid aggtype; - arg = transformJsonValueExpr(pstate, agg->arg, JS_FORMAT_DEFAULT); + arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", + agg->arg, + JS_FORMAT_DEFAULT); returning = transformJsonConstructorOutput(pstate, agg->constructor->output, list_make1(arg)); @@ -3870,7 +3871,8 @@ transformJsonArrayConstructor(ParseState *pstate, JsonArrayConstructor *ctor) foreach(lc, ctor->exprs) { JsonValueExpr *jsval = castNode(JsonValueExpr, lfirst(lc)); - Node *val = transformJsonValueExpr(pstate, jsval, + Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()", + jsval, JS_FORMAT_DEFAULT); args = lappend(args, val);