@@ -3974,6 +3974,46 @@ coerceJsonFuncExpr(ParseState *pstate, Node *expr,
39743974 return res ;
39753975}
39763976
3977+ static Node *
3978+ makeJsonCtorExpr (ParseState * pstate , JsonCtorType type , List * args , Expr * fexpr ,
3979+ JsonReturning * returning , bool unique , bool absent_on_null ,
3980+ int location )
3981+ {
3982+ Node * placeholder ;
3983+ Node * coercion ;
3984+ JsonCtorExpr * jsctor = makeNode (JsonCtorExpr );
3985+ Oid intermediate_typid =
3986+ returning -> format -> format == JS_FORMAT_JSONB ? JSONBOID : JSONOID ;
3987+
3988+ jsctor -> args = args ;
3989+ jsctor -> func = fexpr ;
3990+ jsctor -> type = type ;
3991+ jsctor -> returning = returning ;
3992+ jsctor -> unique = unique ;
3993+ jsctor -> absent_on_null = absent_on_null ;
3994+ jsctor -> location = location ;
3995+
3996+ if (fexpr )
3997+ placeholder = makeCaseTestExpr ((Node * ) fexpr );
3998+ else
3999+ {
4000+ CaseTestExpr * cte = makeNode (CaseTestExpr );
4001+
4002+ cte -> typeId = intermediate_typid ;
4003+ cte -> typeMod = -1 ;
4004+ cte -> collation = InvalidOid ;
4005+
4006+ placeholder = (Node * ) cte ;
4007+ }
4008+
4009+ coercion = coerceJsonFuncExpr (pstate , placeholder , returning , true);
4010+
4011+ if (coercion != placeholder )
4012+ jsctor -> coercion = (Expr * ) coercion ;
4013+
4014+ return (Node * ) jsctor ;
4015+ }
4016+
39774017/*
39784018 * Transform JSON_OBJECT() constructor.
39794019 *
@@ -3986,7 +4026,7 @@ coerceJsonFuncExpr(ParseState *pstate, Node *expr,
39864026static Node *
39874027transformJsonObjectCtor (ParseState * pstate , JsonObjectCtor * ctor )
39884028{
3989- JsonCtorExpr * jsctor ;
4029+ JsonReturning * returning ;
39904030 List * args = NIL ;
39914031
39924032 /* transform key-value pairs, if any */
@@ -4007,15 +4047,11 @@ transformJsonObjectCtor(ParseState *pstate, JsonObjectCtor *ctor)
40074047 }
40084048 }
40094049
4010- jsctor = makeNode (JsonCtorExpr );
4011- jsctor -> args = args ;
4012- jsctor -> type = JSCTOR_JSON_OBJECT ;
4013- jsctor -> returning = transformJsonCtorOutput (pstate , ctor -> output , args );
4014- jsctor -> unique = ctor -> unique ;
4015- jsctor -> absent_on_null = ctor -> absent_on_null ;
4016- jsctor -> location = ctor -> location ;
4050+ returning = transformJsonCtorOutput (pstate , ctor -> output , args );
40174051
4018- return coerceJsonFuncExpr (pstate , (Node * ) jsctor , jsctor -> returning , true);
4052+ return makeJsonCtorExpr (pstate , JSCTOR_JSON_OBJECT , args , NULL ,
4053+ returning , ctor -> unique , ctor -> absent_on_null ,
4054+ ctor -> location );
40194055}
40204056
40214057/*
@@ -4093,7 +4129,6 @@ transformJsonAggCtor(ParseState *pstate, JsonAggCtor *agg_ctor,
40934129 bool unique , bool absent_on_null )
40944130{
40954131 Oid aggfnoid ;
4096- JsonCtorExpr * jsctor ;
40974132 Node * node ;
40984133 Expr * aggfilter = agg_ctor -> agg_filter ? (Expr * )
40994134 transformWhereClause (pstate , agg_ctor -> agg_filter ,
@@ -4156,15 +4191,8 @@ transformJsonAggCtor(ParseState *pstate, JsonAggCtor *agg_ctor,
41564191 node = (Node * ) aggref ;
41574192 }
41584193
4159- jsctor = makeNode (JsonCtorExpr );
4160- jsctor -> func = (Expr * ) node ;
4161- jsctor -> type = ctor_type ;
4162- jsctor -> returning = returning ;
4163- jsctor -> unique = unique ;
4164- jsctor -> absent_on_null = absent_on_null ;
4165- jsctor -> location = agg_ctor -> location ;
4166-
4167- return coerceJsonFuncExpr (pstate , (Node * ) jsctor , returning , true);
4194+ return makeJsonCtorExpr (pstate , ctor_type , NIL , (Expr * ) node , returning ,
4195+ unique , absent_on_null , agg_ctor -> location );
41684196}
41694197
41704198/*
@@ -4277,7 +4305,7 @@ transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg)
42774305static Node *
42784306transformJsonArrayCtor (ParseState * pstate , JsonArrayCtor * ctor )
42794307{
4280- JsonCtorExpr * jsctor ;
4308+ JsonReturning * returning ;
42814309 List * args = NIL ;
42824310
42834311 /* transform element expressions, if any */
@@ -4296,13 +4324,8 @@ transformJsonArrayCtor(ParseState *pstate, JsonArrayCtor *ctor)
42964324 }
42974325 }
42984326
4299- jsctor = makeNode (JsonCtorExpr );
4300- jsctor -> args = args ;
4301- jsctor -> type = JSCTOR_JSON_ARRAY ;
4302- jsctor -> returning = transformJsonCtorOutput (pstate , ctor -> output , args );
4303- jsctor -> unique = false;
4304- jsctor -> absent_on_null = ctor -> absent_on_null ;
4305- jsctor -> location = ctor -> location ;
4327+ returning = transformJsonCtorOutput (pstate , ctor -> output , args );
43064328
4307- return coerceJsonFuncExpr (pstate , (Node * ) jsctor , jsctor -> returning , true);
4329+ return makeJsonCtorExpr (pstate , JSCTOR_JSON_ARRAY , args , NULL , returning ,
4330+ false, ctor -> absent_on_null , ctor -> location );
43084331}
0 commit comments