@@ -4300,14 +4300,124 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
43004300 }
43014301
43024302 /* OMIT QUOTES is meaningless when strings are wrapped. */
4303- if (func -> op == JSON_QUERY_OP &&
4304- func -> quotes == JS_QUOTES_OMIT &&
4305- (func -> wrapper == JSW_CONDITIONAL ||
4306- func -> wrapper == JSW_UNCONDITIONAL ))
4307- ereport (ERROR ,
4308- errcode (ERRCODE_SYNTAX_ERROR ),
4309- errmsg ("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used" ),
4310- parser_errposition (pstate , func -> location ));
4303+ if (func -> op == JSON_QUERY_OP )
4304+ {
4305+ if (func -> quotes == JS_QUOTES_OMIT &&
4306+ (func -> wrapper == JSW_CONDITIONAL ||
4307+ func -> wrapper == JSW_UNCONDITIONAL ))
4308+ ereport (ERROR ,
4309+ errcode (ERRCODE_SYNTAX_ERROR ),
4310+ errmsg ("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used" ),
4311+ parser_errposition (pstate , func -> location ));
4312+ if (func -> on_empty != NULL &&
4313+ func -> on_empty -> btype != JSON_BEHAVIOR_ERROR &&
4314+ func -> on_empty -> btype != JSON_BEHAVIOR_NULL &&
4315+ func -> on_empty -> btype != JSON_BEHAVIOR_EMPTY &&
4316+ func -> on_empty -> btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4317+ func -> on_empty -> btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4318+ func -> on_empty -> btype != JSON_BEHAVIOR_DEFAULT )
4319+ {
4320+ if (func -> column_name == NULL )
4321+ ereport (ERROR ,
4322+ errcode (ERRCODE_SYNTAX_ERROR ),
4323+ errmsg ("invalid ON EMPTY behavior" ),
4324+ errdetail ("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON EMPTY for JSON_QUERY()." ),
4325+ parser_errposition (pstate , func -> on_empty -> location ));
4326+ else
4327+ ereport (ERROR ,
4328+ errcode (ERRCODE_SYNTAX_ERROR ),
4329+ errmsg ("invalid ON EMPTY behavior for column \"%s\"" ,
4330+ func -> column_name ),
4331+ errdetail ("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON EMPTY for formatted columns." ),
4332+ parser_errposition (pstate , func -> on_empty -> location ));
4333+ }
4334+ if (func -> on_error != NULL &&
4335+ func -> on_error -> btype != JSON_BEHAVIOR_ERROR &&
4336+ func -> on_error -> btype != JSON_BEHAVIOR_NULL &&
4337+ func -> on_error -> btype != JSON_BEHAVIOR_EMPTY &&
4338+ func -> on_error -> btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4339+ func -> on_error -> btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4340+ func -> on_error -> btype != JSON_BEHAVIOR_DEFAULT )
4341+ {
4342+ if (func -> column_name == NULL )
4343+ ereport (ERROR ,
4344+ errcode (ERRCODE_SYNTAX_ERROR ),
4345+ errmsg ("invalid ON ERROR behavior" ),
4346+ errdetail ("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for JSON_QUERY()." ),
4347+ parser_errposition (pstate , func -> on_error -> location ));
4348+ else
4349+ ereport (ERROR ,
4350+ errcode (ERRCODE_SYNTAX_ERROR ),
4351+ errmsg ("invalid ON ERROR behavior for column \"%s\"" ,
4352+ func -> column_name ),
4353+ errdetail ("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for formatted columns." ),
4354+ parser_errposition (pstate , func -> on_error -> location ));
4355+ }
4356+ }
4357+
4358+ /* Check that ON ERROR/EMPTY behavior values are valid for the function. */
4359+ if (func -> op == JSON_EXISTS_OP &&
4360+ func -> on_error != NULL &&
4361+ func -> on_error -> btype != JSON_BEHAVIOR_ERROR &&
4362+ func -> on_error -> btype != JSON_BEHAVIOR_TRUE &&
4363+ func -> on_error -> btype != JSON_BEHAVIOR_FALSE &&
4364+ func -> on_error -> btype != JSON_BEHAVIOR_UNKNOWN )
4365+ {
4366+ if (func -> column_name == NULL )
4367+ ereport (ERROR ,
4368+ errcode (ERRCODE_SYNTAX_ERROR ),
4369+ errmsg ("invalid ON ERROR behavior" ),
4370+ errdetail ("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in ON ERROR for JSON_EXISTS()." ),
4371+ parser_errposition (pstate , func -> on_error -> location ));
4372+ else
4373+ ereport (ERROR ,
4374+ errcode (ERRCODE_SYNTAX_ERROR ),
4375+ errmsg ("invalid ON ERROR behavior for column \"%s\"" ,
4376+ func -> column_name ),
4377+ errdetail ("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in ON ERROR for EXISTS columns." ),
4378+ parser_errposition (pstate , func -> on_error -> location ));
4379+ }
4380+ if (func -> op == JSON_VALUE_OP )
4381+ {
4382+ if (func -> on_empty != NULL &&
4383+ func -> on_empty -> btype != JSON_BEHAVIOR_ERROR &&
4384+ func -> on_empty -> btype != JSON_BEHAVIOR_NULL &&
4385+ func -> on_empty -> btype != JSON_BEHAVIOR_DEFAULT )
4386+ {
4387+ if (func -> column_name == NULL )
4388+ ereport (ERROR ,
4389+ errcode (ERRCODE_SYNTAX_ERROR ),
4390+ errmsg ("invalid ON EMPTY behavior" ),
4391+ errdetail ("Only ERROR, NULL, or DEFAULT expression is allowed in ON EMPTY for JSON_VALUE()." ),
4392+ parser_errposition (pstate , func -> on_empty -> location ));
4393+ else
4394+ ereport (ERROR ,
4395+ errcode (ERRCODE_SYNTAX_ERROR ),
4396+ errmsg ("invalid ON EMPTY behavior for column \"%s\"" ,
4397+ func -> column_name ),
4398+ errdetail ("Only ERROR, NULL, or DEFAULT expression is allowed in ON EMPTY for scalar columns." ),
4399+ parser_errposition (pstate , func -> on_empty -> location ));
4400+ }
4401+ if (func -> on_error != NULL &&
4402+ func -> on_error -> btype != JSON_BEHAVIOR_ERROR &&
4403+ func -> on_error -> btype != JSON_BEHAVIOR_NULL &&
4404+ func -> on_error -> btype != JSON_BEHAVIOR_DEFAULT )
4405+ {
4406+ if (func -> column_name == NULL )
4407+ ereport (ERROR ,
4408+ errcode (ERRCODE_SYNTAX_ERROR ),
4409+ errmsg ("invalid ON ERROR behavior" ),
4410+ errdetail ("Only ERROR, NULL, or DEFAULT expression is allowed in ON ERROR for JSON_VALUE()." ),
4411+ parser_errposition (pstate , func -> on_error -> location ));
4412+ else
4413+ ereport (ERROR ,
4414+ errcode (ERRCODE_SYNTAX_ERROR ),
4415+ errmsg ("invalid ON ERROR behavior for column \"%s\"" ,
4416+ func -> column_name ),
4417+ errdetail ("Only ERROR, NULL, or DEFAULT expression is allowed in ON ERROR for scalar columns." ),
4418+ parser_errposition (pstate , func -> on_error -> location ));
4419+ }
4420+ }
43114421
43124422 jsexpr = makeNode (JsonExpr );
43134423 jsexpr -> location = func -> location ;
0 commit comments