Pass constructName to transformJsonValueExpr()
authorAmit Langote <amitlan@postgresql.org>
Fri, 7 Jul 2023 03:08:58 +0000 (12:08 +0900)
committerAmit Langote <amitlan@postgresql.org>
Thu, 13 Jul 2023 03:13:49 +0000 (12:13 +0900)
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.

Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://postgr.es/m/CA+HiwqE4XTdfb1nW=Ojoy_tQSRhYt-q_kb6i5d4xcKyrLC1Nbg@mail.gmail.com

src/backend/parser/parse_expr.c

index 346fd272b6d17ad3eda86b3ff0a2041ca59763b9..5bf790cf0fe946374642092fead019c4af7216e1 100644 (file)
@@ -3222,8 +3222,8 @@ makeCaseTestExpr(Node *expr)
  * default format otherwise.
  */
 static Node *
-transformJsonValueExpr(ParseState *pstate, JsonValueExpr *ve,
-                      JsonFormatType default_format)
+transformJsonValueExpr(ParseState *pstate, 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);