From d3bb9a3a93accafc786b5cd6e41d8ad963565dcf Mon Sep 17 00:00:00 2001 From: Amit Langote Date: Thu, 18 Jan 2024 17:57:20 +0900 Subject: [PATCH v37 4/7] Add jsonpath_exec APIs to use in SQL/JSON query functions This adds JsonPathExists(), JsonPathQuery(), JsonPathValue() that are wrappers over executeJsonPath() to implement SQL/JSON functions JSON_EXISTS(), JSON_QUERY(), and JSON_VALUE(), respectively. Those functions themselves will be added in a subsequent commit along with the necessary parser/planner/executor support. This also introduces a new struct JsonPathVariable for the executor implementation of those functions to be able to pass the values of the variables used in jsonpath that are separately evaluated by the executor. Discussion: https://postgr.es/m/CA+HiwqE4XTdfb1nW=Ojoy_tQSRhYt-q_kb6i5d4xcKyrLC1Nbg@mail.gmail.com --- src/backend/utils/adt/jsonpath_exec.c | 322 ++++++++++++++++++++++++++ src/include/nodes/primnodes.h | 11 + src/include/utils/jsonpath.h | 23 ++ src/tools/pgindent/typedefs.list | 2 + 4 files changed, 358 insertions(+) diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index cb2ea048c3..a2218bf0bc 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -234,6 +234,12 @@ static JsonPathExecResult appendBoolResult(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonValueList *found, JsonPathBool res); static void getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item, JsonbValue *value); +static JsonbValue *GetJsonPathVar(void *cxt, char *varName, int varNameLen, + JsonbValue *baseObject, int *baseObjectId); +static int CountJsonPathVars(void *cxt); +static void JsonItemFromDatum(Datum val, Oid typid, int32 typmod, + JsonbValue *res); +static void JsonbValueInitNumericDatum(JsonbValue *jbv, Datum num); static void getJsonPathVariable(JsonPathExecContext *cxt, JsonPathItem *variable, JsonbValue *value); static int countVariablesFromJsonb(void *varsJsonb); @@ -2138,6 +2144,155 @@ getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item, } } +/* + * Returns the computed value of a JSON path variable with given name. + */ +static JsonbValue * +GetJsonPathVar(void *cxt, char *varName, int varNameLen, + JsonbValue *baseObject, int *baseObjectId) +{ + JsonPathVariable *var = NULL; + List *vars = cxt; + ListCell *lc; + JsonbValue *result; + int id = 1; + + foreach(lc, vars) + { + JsonPathVariable *curvar = lfirst(lc); + + if (!strncmp(curvar->name, varName, varNameLen)) + { + var = curvar; + break; + } + + id++; + } + + if (var == NULL) + { + *baseObjectId = -1; + return NULL; + } + + result = palloc(sizeof(JsonbValue)); + if (var->isnull) + { + *baseObjectId = 0; + result->type = jbvNull; + } + else + JsonItemFromDatum(var->value, var->typid, var->typmod, result); + + *baseObject = *result; + *baseObjectId = id; + + return result; +} + +static int +CountJsonPathVars(void *cxt) +{ + List *vars = (List *) cxt; + + return list_length(vars); +} + + +/* + * Initialize JsonbValue to pass to jsonpath executor from given + * datum value of the specified type. + */ +static void +JsonItemFromDatum(Datum val, Oid typid, int32 typmod, JsonbValue *res) +{ + switch (typid) + { + case BOOLOID: + res->type = jbvBool; + res->val.boolean = DatumGetBool(val); + break; + case NUMERICOID: + JsonbValueInitNumericDatum(res, val); + break; + case INT2OID: + JsonbValueInitNumericDatum(res, DirectFunctionCall1(int2_numeric, val)); + break; + case INT4OID: + JsonbValueInitNumericDatum(res, DirectFunctionCall1(int4_numeric, val)); + break; + case INT8OID: + JsonbValueInitNumericDatum(res, DirectFunctionCall1(int8_numeric, val)); + break; + case FLOAT4OID: + JsonbValueInitNumericDatum(res, DirectFunctionCall1(float4_numeric, val)); + break; + case FLOAT8OID: + JsonbValueInitNumericDatum(res, DirectFunctionCall1(float8_numeric, val)); + break; + case TEXTOID: + case VARCHAROID: + res->type = jbvString; + res->val.string.val = VARDATA_ANY(val); + res->val.string.len = VARSIZE_ANY_EXHDR(val); + break; + case DATEOID: + case TIMEOID: + case TIMETZOID: + case TIMESTAMPOID: + case TIMESTAMPTZOID: + res->type = jbvDatetime; + res->val.datetime.value = val; + res->val.datetime.typid = typid; + res->val.datetime.typmod = typmod; + res->val.datetime.tz = 0; + break; + case JSONBOID: + { + JsonbValue *jbv = res; + Jsonb *jb = DatumGetJsonbP(val); + + if (JsonContainerIsScalar(&jb->root)) + { + bool result PG_USED_FOR_ASSERTS_ONLY; + + result = JsonbExtractScalar(&jb->root, jbv); + Assert(result); + } + else + JsonbInitBinary(jbv, jb); + break; + } + case JSONOID: + { + text *txt = DatumGetTextP(val); + char *str = text_to_cstring(txt); + Jsonb *jb; + + jb = DatumGetJsonbP(DirectFunctionCall1(jsonb_in, + CStringGetDatum(str))); + pfree(str); + + JsonItemFromDatum(JsonbPGetDatum(jb), JSONBOID, -1, res); + break; + } + default: + ereport(ERROR, + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("could not convert value of type %s to jsonpath", + format_type_be(typid))); + } +} + +/* Initialize numeric value from the given datum */ +static void +JsonbValueInitNumericDatum(JsonbValue *jbv, Datum num) +{ + jbv->type = jbvNumeric; + jbv->val.numeric = DatumGetNumeric(num); +} + /* * Get the value of variable passed to jsonpath executor */ @@ -2874,3 +3029,170 @@ compareDatetime(Datum val1, Oid typid1, Datum val2, Oid typid2, return DatumGetInt32(DirectFunctionCall2(cmpfunc, val1, val2)); } + +/* + * Executor-callable JSON_EXISTS implementation + * + * Returns NULL instead of throwing errors if 'error' is not NULL, setting + * *error to true. + */ +bool +JsonPathExists(Datum jb, JsonPath *jp, bool *error, List *vars) +{ + JsonPathExecResult res; + + res = executeJsonPath(jp, vars, + GetJsonPathVar, CountJsonPathVars, + DatumGetJsonbP(jb), !error, NULL, true); + + Assert(error || !jperIsError(res)); + + if (error && jperIsError(res)) + *error = true; + + return res == jperOk; +} + +/* + * Executor-callable JSON_QUERY implementation + * + * Returns NULL instead of throwing errors if 'error' is not NULL, setting + * *error to true. *empty is set to true if no match is found. + */ +Datum +JsonPathQuery(Datum jb, JsonPath *jp, JsonWrapper wrapper, bool *empty, + bool *error, List *vars) +{ + JsonbValue *singleton; + bool wrap; + JsonValueList found = {0}; + JsonPathExecResult res; + int count; + + res = executeJsonPath(jp, vars, + GetJsonPathVar, CountJsonPathVars, + DatumGetJsonbP(jb), !error, &found, true); + Assert(error || !jperIsError(res)); + if (error && jperIsError(res)) + { + *error = true; + *empty = false; + return (Datum) 0; + } + + /* WRAP or not? */ + count = JsonValueListLength(&found); + singleton = count > 0 ? JsonValueListHead(&found) : NULL; + if (singleton == NULL) + wrap = false; + else if (wrapper == JSW_NONE) + wrap = false; + else if (wrapper == JSW_UNCONDITIONAL) + wrap = true; + else if (wrapper == JSW_CONDITIONAL) + wrap = count > 1 || + IsAJsonbScalar(singleton) || + (singleton->type == jbvBinary && + JsonContainerIsScalar(singleton->val.binary.data)); + else + { + elog(ERROR, "unrecognized json wrapper %d", wrapper); + wrap = false; + } + + if (wrap) + return JsonbPGetDatum(JsonbValueToJsonb(wrapItemsInArray(&found))); + + /* No wrapping means only one item is expected. */ + if (count > 1) + { + if (error) + { + *error = true; + 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 (singleton) + return JsonbPGetDatum(JsonbValueToJsonb(singleton)); + + *empty = true; + return PointerGetDatum(NULL); +} + +/* + * Executor-callable JSON_VALUE implementation + * + * Returns NULL instead of throwing errors if 'error' is not NULL, setting + * *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) +{ + JsonbValue *res; + JsonValueList found = {0}; + JsonPathExecResult jper PG_USED_FOR_ASSERTS_ONLY; + int count; + + jper = executeJsonPath(jp, vars, GetJsonPathVar, CountJsonPathVars, + DatumGetJsonbP(jb), + !error, &found, true); + + Assert(error || !jperIsError(jper)); + + if (error && jperIsError(jper)) + { + *error = true; + *empty = false; + return NULL; + } + + count = JsonValueListLength(&found); + + *empty = (count == 0); + + if (*empty) + return NULL; + + /* JSON_VALUE expects to get only singletons. */ + if (count > 1) + { + if (error) + { + *error = true; + return NULL; + } + + ereport(ERROR, + (errcode(ERRCODE_MORE_THAN_ONE_SQL_JSON_ITEM), + errmsg("JSON path expression in JSON_VALUE should return singleton scalar item"))); + } + + res = JsonValueListHead(&found); + if (res->type == jbvBinary && JsonContainerIsScalar(res->val.binary.data)) + JsonbExtractScalar(res->val.binary.data, res); + + /* JSON_VALUE expects to get only scalars. */ + if (!IsAJsonbScalar(res)) + { + if (error) + { + *error = true; + return NULL; + } + + ereport(ERROR, + (errcode(ERRCODE_SQL_JSON_SCALAR_REQUIRED), + errmsg("JSON path expression in JSON_VALUE should return singleton scalar item"))); + } + + if (res->type == jbvNull) + return NULL; + + return res; +} diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 4a154606d2..61289d8124 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1576,6 +1576,17 @@ typedef enum JsonFormatType * jsonb */ } JsonFormatType; +/* + * JsonWrapper - + * representation of WRAPPER clause for JsonPathQuery() + */ +typedef enum JsonWrapper +{ + JSW_NONE, + JSW_CONDITIONAL, + JSW_UNCONDITIONAL, +} JsonWrapper; + /* * JsonFormat - * representation of JSON FORMAT clause diff --git a/src/include/utils/jsonpath.h b/src/include/utils/jsonpath.h index 9d55c25ebc..6eabdcfb75 100644 --- a/src/include/utils/jsonpath.h +++ b/src/include/utils/jsonpath.h @@ -16,6 +16,7 @@ #include "fmgr.h" #include "nodes/pg_list.h" +#include "nodes/primnodes.h" #include "utils/jsonb.h" typedef struct @@ -268,4 +269,26 @@ extern bool jspConvertRegexFlags(uint32 xflags, int *result, struct Node *escontext); +/* + * Evaluation of jsonpath + */ + +/* External variable passed into jsonpath. */ +typedef struct JsonPathVariable +{ + char *name; + Oid typid; + int32 typmod; + Datum value; + bool isnull; +} JsonPathVariable; + + +/* SQL/JSON item */ +extern bool JsonPathExists(Datum jb, JsonPath *path, bool *error, List *vars); +extern Datum JsonPathQuery(Datum jb, JsonPath *jp, JsonWrapper wrapper, + bool *empty, bool *error, List *vars); +extern JsonbValue *JsonPathValue(Datum jb, JsonPath *jp, bool *empty, + bool *error, List *vars); + #endif diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 7e866e3c3d..897845374f 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -1311,6 +1311,7 @@ JsonPathParseItem JsonPathParseResult JsonPathPredicateCallback JsonPathString +JsonPathVariable JsonReturning JsonScalarExpr JsonSemAction @@ -1327,6 +1328,7 @@ JsonValueExpr JsonValueList JsonValueListIterator JsonValueType +JsonWrapper Jsonb JsonbAggState JsonbContainer -- 2.35.3