@@ -4412,26 +4412,44 @@ ExecEvalJsonExpr(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
4412
4412
op -> d .jsonexpr .args );
4413
4413
struct JsonCoercionState * jcstate ;
4414
4414
4415
- if (!jbv )
4415
+ if (!jbv ) /* NULL or empty */
4416
4416
break ;
4417
4417
4418
+ Assert (!empty );
4419
+
4418
4420
* resnull = false;
4419
4421
4422
+ /* coerce item datum to the output type */
4423
+ if (jexpr -> returning .typid == JSONOID ||
4424
+ jexpr -> returning .typid == JSONBOID )
4425
+ {
4426
+ /* Use result coercion from json[b] to the output type */
4427
+ res = JsonbPGetDatum (JsonItemToJsonb (jbv ));
4428
+ break ;
4429
+ }
4430
+
4431
+ /* Use coercion from SQL/JSON item type to the output type */
4420
4432
res = ExecPrepareJsonItemCoercion (jbv ,
4421
4433
& op -> d .jsonexpr .jsexpr -> returning ,
4422
4434
& op -> d .jsonexpr .coercions ,
4423
4435
& jcstate );
4424
4436
4425
- /* coerce item datum to the output type */
4426
- if ((jcstate -> coercion &&
4437
+ if (jcstate -> coercion &&
4427
4438
(jcstate -> coercion -> via_io ||
4428
- jcstate -> coercion -> via_populate )) || /* ignored for scalars jsons */
4429
- jexpr -> returning .typid == JSONOID ||
4430
- jexpr -> returning .typid == JSONBOID )
4439
+ jcstate -> coercion -> via_populate ))
4431
4440
{
4432
- /* use coercion via I/O from json[b] to the output type */
4433
- res = JsonbPGetDatum (JsonItemToJsonb (jbv ));
4434
- res = ExecEvalJsonExprCoercion (op , econtext , res , resnull );
4441
+ /*
4442
+ * Coercion via I/O means here that the cast to the target
4443
+ * type simply does not exist.
4444
+ */
4445
+ ereport (ERROR ,
4446
+ /*
4447
+ * XXX Standard says about a separate error code
4448
+ * ERRCODE_JSON_ITEM_CANNOT_BE_CAST_TO_TARGET_TYPE
4449
+ * but does not define its number.
4450
+ */
4451
+ (errcode (ERRCODE_JSON_SCALAR_REQUIRED ),
4452
+ errmsg ("SQL/JSON item cannot be cast to target type" )));
4435
4453
}
4436
4454
else if (jcstate -> estate )
4437
4455
{
@@ -4441,17 +4459,16 @@ ExecEvalJsonExpr(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
4441
4459
res = ExecEvalExpr (jcstate -> estate , econtext , resnull );
4442
4460
}
4443
4461
/* else no coercion */
4462
+
4463
+ return res ;
4444
4464
}
4445
- break ;
4446
4465
4447
4466
case IS_JSON_EXISTS :
4448
- res = BoolGetDatum (JsonbPathExists (item , path , op -> d .jsonexpr .args ));
4449
4467
* resnull = false;
4450
- break ;
4468
+ return BoolGetDatum ( JsonbPathExists ( item , path , op -> d . jsonexpr . args )) ;
4451
4469
4452
4470
default :
4453
- elog (ERROR , "unrecognized SQL/JSON expression op %d" ,
4454
- jexpr -> op );
4471
+ elog (ERROR , "unrecognized SQL/JSON expression op %d" , jexpr -> op );
4455
4472
return (Datum ) 0 ;
4456
4473
}
4457
4474
@@ -4465,15 +4482,13 @@ ExecEvalJsonExpr(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
4465
4482
/* execute ON EMPTY behavior */
4466
4483
res = ExecEvalJsonBehavior (econtext , & jexpr -> on_empty ,
4467
4484
op -> d .jsonexpr .default_on_empty , resnull );
4468
- }
4469
4485
4470
- if (jexpr -> op != IS_JSON_EXISTS &&
4471
- (!empty ? jexpr -> op != IS_JSON_VALUE :
4472
- /* result is already coerced in DEFAULT behavior case */
4473
- jexpr -> on_empty .btype != JSON_BEHAVIOR_DEFAULT ))
4474
- res = ExecEvalJsonExprCoercion (op , econtext , res , resnull );
4486
+ /* result is already coerced in DEFAULT behavior case */
4487
+ if (jexpr -> on_empty .btype == JSON_BEHAVIOR_DEFAULT )
4488
+ return res ;
4489
+ }
4475
4490
4476
- return res ;
4491
+ return ExecEvalJsonExprCoercion ( op , econtext , res , resnull ) ;
4477
4492
}
4478
4493
4479
4494
bool
0 commit comments