Pass constructName to transformJsonValueExpr()

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 <alvherre@alvh.no-ip.org>
Discussion: https://postgr.es/m/CA+HiwqHu58pO3cJ7rB6ZLwUztVdG1J66xSjDdjfan5uT5NhESw@mail.gmail.com
This commit is contained in:
Amit Langote 2023-07-20 16:23:49 +09:00
parent 0a1d2a7df8
commit 7825a1b01e
1 changed files with 13 additions and 11 deletions

View File

@ -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);