mirror of
https://github.com/postgres/postgres.git
synced 2025-07-05 07:21:24 +03:00
Throw error in jsonb_path_match() when result is not single boolean
jsonb_path_match() checks if jsonb document matches jsonpath query. Therefore, jsonpath query should return single boolean. Currently, if result of jsonpath is not a single boolean, NULL is returned independently whether silent mode is on or off. But that appears to be wrong when silent mode is off. This commit makes jsonb_path_match() throw an error in this case. Author: Nikita Glukhov
This commit is contained in:
@ -320,7 +320,6 @@ jsonb_path_match(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Jsonb *jb = PG_GETARG_JSONB_P(0);
|
||||
JsonPath *jp = PG_GETARG_JSONPATH_P(1);
|
||||
JsonbValue *jbv;
|
||||
JsonValueList found = {0};
|
||||
Jsonb *vars = NULL;
|
||||
bool silent = true;
|
||||
@ -333,18 +332,27 @@ jsonb_path_match(PG_FUNCTION_ARGS)
|
||||
|
||||
(void) executeJsonPath(jp, vars, jb, !silent, &found);
|
||||
|
||||
if (JsonValueListLength(&found) < 1)
|
||||
PG_RETURN_NULL();
|
||||
|
||||
jbv = JsonValueListHead(&found);
|
||||
|
||||
PG_FREE_IF_COPY(jb, 0);
|
||||
PG_FREE_IF_COPY(jp, 1);
|
||||
|
||||
if (jbv->type != jbvBool)
|
||||
PG_RETURN_NULL();
|
||||
if (JsonValueListLength(&found) == 1)
|
||||
{
|
||||
JsonbValue *jbv = JsonValueListHead(&found);
|
||||
|
||||
PG_RETURN_BOOL(jbv->val.boolean);
|
||||
if (jbv->type == jbvBool)
|
||||
PG_RETURN_BOOL(jbv->val.boolean);
|
||||
|
||||
if (jbv->type == jbvNull)
|
||||
PG_RETURN_NULL();
|
||||
}
|
||||
|
||||
if (!silent)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_SINGLETON_JSON_ITEM_REQUIRED),
|
||||
errmsg(ERRMSG_SINGLETON_JSON_ITEM_REQUIRED),
|
||||
errdetail("expression should return a singleton boolean")));
|
||||
|
||||
PG_RETURN_NULL();
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user