@@ -3974,6 +3974,46 @@ coerceJsonFuncExpr(ParseState *pstate, Node *expr,
3974
3974
return res ;
3975
3975
}
3976
3976
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
+
3977
4017
/*
3978
4018
* Transform JSON_OBJECT() constructor.
3979
4019
*
@@ -3986,7 +4026,7 @@ coerceJsonFuncExpr(ParseState *pstate, Node *expr,
3986
4026
static Node *
3987
4027
transformJsonObjectCtor (ParseState * pstate , JsonObjectCtor * ctor )
3988
4028
{
3989
- JsonCtorExpr * jsctor ;
4029
+ JsonReturning * returning ;
3990
4030
List * args = NIL ;
3991
4031
3992
4032
/* transform key-value pairs, if any */
@@ -4007,15 +4047,11 @@ transformJsonObjectCtor(ParseState *pstate, JsonObjectCtor *ctor)
4007
4047
}
4008
4048
}
4009
4049
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 );
4017
4051
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 );
4019
4055
}
4020
4056
4021
4057
/*
@@ -4093,7 +4129,6 @@ transformJsonAggCtor(ParseState *pstate, JsonAggCtor *agg_ctor,
4093
4129
bool unique , bool absent_on_null )
4094
4130
{
4095
4131
Oid aggfnoid ;
4096
- JsonCtorExpr * jsctor ;
4097
4132
Node * node ;
4098
4133
Expr * aggfilter = agg_ctor -> agg_filter ? (Expr * )
4099
4134
transformWhereClause (pstate , agg_ctor -> agg_filter ,
@@ -4156,15 +4191,8 @@ transformJsonAggCtor(ParseState *pstate, JsonAggCtor *agg_ctor,
4156
4191
node = (Node * ) aggref ;
4157
4192
}
4158
4193
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 );
4168
4196
}
4169
4197
4170
4198
/*
@@ -4277,7 +4305,7 @@ transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg)
4277
4305
static Node *
4278
4306
transformJsonArrayCtor (ParseState * pstate , JsonArrayCtor * ctor )
4279
4307
{
4280
- JsonCtorExpr * jsctor ;
4308
+ JsonReturning * returning ;
4281
4309
List * args = NIL ;
4282
4310
4283
4311
/* transform element expressions, if any */
@@ -4296,13 +4324,8 @@ transformJsonArrayCtor(ParseState *pstate, JsonArrayCtor *ctor)
4296
4324
}
4297
4325
}
4298
4326
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 );
4306
4328
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 );
4308
4331
}
0 commit comments