@@ -4725,26 +4725,44 @@ ExecEvalJsonExpr(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
4725
4725
op -> d .jsonexpr .args );
4726
4726
struct JsonCoercionState * jcstate ;
4727
4727
4728
- if (!jbv )
4728
+ if (!jbv )/* NULL or empty */
4729
4729
break ;
4730
4730
4731
+ Assert (!empty );
4732
+
4731
4733
* resnull = false;
4732
4734
4735
+ /* coerce item datum to the output type */
4736
+ if (jexpr -> returning -> typid == JSONOID ||
4737
+ jexpr -> returning -> typid == JSONBOID )
4738
+ {
4739
+ /* Use result coercion from json[b] to the output type */
4740
+ res = JsonbPGetDatum (JsonbValueToJsonb (jbv ));
4741
+ break ;
4742
+ }
4743
+
4744
+ /* Use coercion from SQL/JSON item type to the output type */
4733
4745
res = ExecPrepareJsonItemCoercion (jbv ,
4734
4746
op -> d .jsonexpr .jsexpr -> returning ,
4735
4747
& op -> d .jsonexpr .coercions ,
4736
4748
& jcstate );
4737
4749
4738
- /* coerce item datum to the output type */
4739
- if ((jcstate -> coercion &&
4750
+ if (jcstate -> coercion &&
4740
4751
(jcstate -> coercion -> via_io ||
4741
- jcstate -> coercion -> via_populate ))|| /* ignored for scalars jsons */
4742
- jexpr -> returning -> typid == JSONOID ||
4743
- jexpr -> returning -> typid == JSONBOID )
4752
+ jcstate -> coercion -> via_populate ))
4744
4753
{
4745
- /* use coercion via I/O from json[b] to the output type */
4746
- res = JsonbPGetDatum (JsonbValueToJsonb (jbv ));
4747
- res = ExecEvalJsonExprCoercion (op ,econtext ,res ,resnull );
4754
+ /*
4755
+ * Coercion via I/O means here that the cast to the target
4756
+ * type simply does not exist.
4757
+ */
4758
+ ereport (ERROR ,
4759
+ /*
4760
+ * XXX Standard says about a separate error code
4761
+ * ERRCODE_SQL_JSON_ITEM_CANNOT_BE_CAST_TO_TARGET_TYPE
4762
+ * but does not define its number.
4763
+ */
4764
+ (errcode (ERRCODE_SQL_JSON_SCALAR_REQUIRED ),
4765
+ errmsg ("SQL/JSON item cannot be cast to target type" )));
4748
4766
}
4749
4767
else if (jcstate -> estate )
4750
4768
{
@@ -4754,17 +4772,16 @@ ExecEvalJsonExpr(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
4754
4772
res = ExecEvalExpr (jcstate -> estate ,econtext ,resnull );
4755
4773
}
4756
4774
/* else no coercion */
4775
+
4776
+ return res ;
4757
4777
}
4758
- break ;
4759
4778
4760
4779
case IS_JSON_EXISTS :
4761
- res = BoolGetDatum (JsonbPathExists (item ,path ,op -> d .jsonexpr .args ));
4762
4780
* resnull = false;
4763
- break ;
4781
+ return BoolGetDatum ( JsonbPathExists ( item , path , op -> d . jsonexpr . args )) ;
4764
4782
4765
4783
default :
4766
- elog (ERROR ,"unrecognized SQL/JSON expression op %d" ,
4767
- jexpr -> op );
4784
+ elog (ERROR ,"unrecognized SQL/JSON expression op %d" ,jexpr -> op );
4768
4785
return (Datum )0 ;
4769
4786
}
4770
4787
@@ -4780,15 +4797,13 @@ ExecEvalJsonExpr(ExprState *state, ExprEvalStep *op, ExprContext *econtext,
4780
4797
/* execute ON EMPTY behavior */
4781
4798
res = ExecEvalJsonBehavior (econtext ,jexpr -> on_empty ,
4782
4799
op -> d .jsonexpr .default_on_empty ,resnull );
4783
- }
4784
4800
4785
- if (jexpr -> op != IS_JSON_EXISTS &&
4786
- (!empty ?jexpr -> op != IS_JSON_VALUE :
4787
- /* result is already coerced in DEFAULT behavior case */
4788
- jexpr -> on_empty -> btype != JSON_BEHAVIOR_DEFAULT ))
4789
- res = ExecEvalJsonExprCoercion (op ,econtext ,res ,resnull );
4801
+ /* result is already coerced in DEFAULT behavior case */
4802
+ if (jexpr -> on_empty -> btype == JSON_BEHAVIOR_DEFAULT )
4803
+ return res ;
4804
+ }
4790
4805
4791
- return res ;
4806
+ return ExecEvalJsonExprCoercion ( op , econtext , res , resnull ) ;
4792
4807
}
4793
4808
4794
4809
bool