diff options
author | Amit Langote <amitlan@postgresql.org> | 2024-04-18 14:33:47 +0900 |
---|---|---|
committer | Amit Langote <amitlan@postgresql.org> | 2024-04-18 14:45:48 +0900 |
commit | b4fad46b6bc8a9bf46ff689bcb1bd4edf8f267af (patch) | |
tree | 48accd814bfb6c712699b8f26734fbf06c949b3e /src/backend/utils/adt | |
parent | 40126ac68f2ff96351cd6071350eb2d5cbd50145 (diff) | |
download | postgresql-b4fad46b6bc8a9bf46ff689bcb1bd4edf8f267af.tar.gz postgresql-b4fad46b6bc8a9bf46ff689bcb1bd4edf8f267af.zip |
SQL/JSON: Improve some error messages
This improves some error messages emitted by SQL/JSON query functions
by mentioning column name when available, such as when they are
invoked as part of evaluating JSON_TABLE() columns. To do so, a new
field column_name is added to both JsonFuncExpr and JsonExpr that is
only populated when creating those nodes for transformed JSON_TABLE()
columns.
While at it, relevant error messages are reworded for clarity.
Reported-by: Jian He <jian.universality@gmail.com>
Suggested-by: Jian He <jian.universality@gmail.com>
Discussion: https://postgr.es/m/CACJufxG_e0QLCgaELrr2ZNz7AxPeGCNKAORe3fHtFCQLsH4J4Q@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt')
-rw-r--r-- | src/backend/utils/adt/jsonpath_exec.c | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index 103572ed932..e74dc1b2d42 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -3899,7 +3899,8 @@ JsonPathExists(Datum jb, JsonPath *jp, bool *error, List *vars) */ Datum JsonPathQuery(Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty, - bool *error, List *vars) + bool *error, List *vars, + const char *column_name) { JsonbValue *singleton; bool wrap; @@ -3950,10 +3951,17 @@ JsonPathQuery(Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty, return (Datum) 0; } - ereport(ERROR, - (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM), - errmsg("JSON path expression in JSON_QUERY should return singleton item without wrapper"), - errhint("Use WITH WRAPPER clause to wrap SQL/JSON item sequence into array."))); + if (column_name) + ereport(ERROR, + (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM), + errmsg("JSON path expression for column \"%s\" should return single item without wrapper", + column_name), + errhint("Use WITH WRAPPER clause to wrap SQL/JSON items into array."))); + else + ereport(ERROR, + (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM), + errmsg("JSON path expression in JSON_QUERY should return single item without wrapper"), + errhint("Use WITH WRAPPER clause to wrap SQL/JSON items into array."))); } if (singleton) @@ -3970,7 +3978,8 @@ JsonPathQuery(Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty, * *error to true. *empty is set to true if no match is found. */ JsonbValue * -JsonPathValue(Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars) +JsonPathValue(Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars, + const char *column_name) { JsonbValue *res; JsonValueList found = {0}; @@ -4006,9 +4015,15 @@ JsonPathValue(Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars) return NULL; } - ereport(ERROR, - (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM), - errmsg("JSON path expression in JSON_VALUE should return singleton scalar item"))); + if (column_name) + ereport(ERROR, + (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM), + errmsg("JSON path expression for column \"%s\" should return single scalar item", + column_name))); + else + ereport(ERROR, + (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM), + errmsg("JSON path expression in JSON_VALUE should return single scalar item"))); } res = JsonValueListHead(&found); @@ -4024,9 +4039,15 @@ JsonPathValue(Datum jb, JsonPath *jp, bool *empty, bool *error, List *vars) return NULL; } - ereport(ERROR, - (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED), - errmsg("JSON path expression in JSON_VALUE should return singleton scalar item"))); + if (column_name) + ereport(ERROR, + (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED), + errmsg("JSON path expression for column \"%s\" should return single scalar item", + column_name))); + else + ereport(ERROR, + (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED), + errmsg("JSON path expression in JSON_VALUE should return single scalar item"))); } if (res->type == jbvNull) |