diff options
author | Alexander Korotkov <akorotkov@postgresql.org> | 2019-03-16 12:15:37 +0300 |
---|---|---|
committer | Alexander Korotkov <akorotkov@postgresql.org> | 2019-03-16 12:16:48 +0300 |
commit | 72b6460336e86ad5cafd3426af6013c7d8457367 (patch) | |
tree | 8c590577fcfac501f24d7a282b529bea9d45c6d9 /src/backend/utils/adt/jsonb.c | |
parent | 893d6f8a1f9b43da805124e93cbf0f7aea890ad4 (diff) | |
download | postgresql-72b6460336e86ad5cafd3426af6013c7d8457367.tar.gz postgresql-72b6460336e86ad5cafd3426af6013c7d8457367.zip |
Partial implementation of SQL/JSON path language
SQL 2016 standards among other things contains set of SQL/JSON features for
JSON processing inside of relational database. The core of SQL/JSON is JSON
path language, allowing access parts of JSON documents and make computations
over them. This commit implements partial support JSON path language as
separate datatype called "jsonpath". The implementation is partial because
it's lacking datetime support and suppression of numeric errors. Missing
features will be added later by separate commits.
Support of SQL/JSON features requires implementation of separate nodes, and it
will be considered in subsequent patches. This commit includes following
set of plain functions, allowing to execute jsonpath over jsonb values:
* jsonb_path_exists(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_match(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query(jsonb, jsonpath[, jsonb, bool]),
* jsonb_path_query_array(jsonb, jsonpath[, jsonb, bool]).
* jsonb_path_query_first(jsonb, jsonpath[, jsonb, bool]).
This commit also implements "jsonb @? jsonpath" and "jsonb @@ jsonpath", which
are wrappers over jsonpath_exists(jsonb, jsonpath) and jsonpath_predicate(jsonb,
jsonpath) correspondingly. These operators will have an index support
(implemented in subsequent patches).
Catversion bumped, to add new functions and operators.
Code was written by Nikita Glukhov and Teodor Sigaev, revised by me.
Documentation was written by Oleg Bartunov and Liudmila Mantrova. The work
was inspired by Oleg Bartunov.
Discussion: https://postgr.es/m/fcc6fc6a-b497-f39a-923d-aa34d0c588e8%402ndQuadrant.com
Author: Nikita Glukhov, Teodor Sigaev, Alexander Korotkov, Oleg Bartunov, Liudmila Mantrova
Reviewed-by: Tomas Vondra, Andrew Dunstan, Pavel Stehule, Alexander Korotkov
Diffstat (limited to 'src/backend/utils/adt/jsonb.c')
-rw-r--r-- | src/backend/utils/adt/jsonb.c | 91 |
1 files changed, 51 insertions, 40 deletions
diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c index c02c8569f28..7af4091200b 100644 --- a/src/backend/utils/adt/jsonb.c +++ b/src/backend/utils/adt/jsonb.c @@ -164,6 +164,55 @@ jsonb_send(PG_FUNCTION_ARGS) } /* + * Get the type name of a jsonb container. + */ +static const char * +JsonbContainerTypeName(JsonbContainer *jbc) +{ + JsonbValue scalar; + + if (JsonbExtractScalar(jbc, &scalar)) + return JsonbTypeName(&scalar); + else if (JsonContainerIsArray(jbc)) + return "array"; + else if (JsonContainerIsObject(jbc)) + return "object"; + else + { + elog(ERROR, "invalid jsonb container type: 0x%08x", jbc->header); + return "unknown"; + } +} + +/* + * Get the type name of a jsonb value. + */ +const char * +JsonbTypeName(JsonbValue *jbv) +{ + switch (jbv->type) + { + case jbvBinary: + return JsonbContainerTypeName(jbv->val.binary.data); + case jbvObject: + return "object"; + case jbvArray: + return "array"; + case jbvNumeric: + return "number"; + case jbvString: + return "string"; + case jbvBool: + return "boolean"; + case jbvNull: + return "null"; + default: + elog(ERROR, "unrecognized jsonb value type: %d", jbv->type); + return "unknown"; + } +} + +/* * SQL function jsonb_typeof(jsonb) -> text * * This function is here because the analog json function is in json.c, since @@ -173,45 +222,7 @@ Datum jsonb_typeof(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB_P(0); - JsonbIterator *it; - JsonbValue v; - char *result; - - if (JB_ROOT_IS_OBJECT(in)) - result = "object"; - else if (JB_ROOT_IS_ARRAY(in) && !JB_ROOT_IS_SCALAR(in)) - result = "array"; - else - { - Assert(JB_ROOT_IS_SCALAR(in)); - - it = JsonbIteratorInit(&in->root); - - /* - * A root scalar is stored as an array of one element, so we get the - * array and then its first (and only) member. - */ - (void) JsonbIteratorNext(&it, &v, true); - Assert(v.type == jbvArray); - (void) JsonbIteratorNext(&it, &v, true); - switch (v.type) - { - case jbvNull: - result = "null"; - break; - case jbvString: - result = "string"; - break; - case jbvNumeric: - result = "number"; - break; - case jbvBool: - result = "boolean"; - break; - default: - elog(ERROR, "unknown jsonb scalar type"); - } - } + const char *result = JsonbContainerTypeName(&in->root); PG_RETURN_TEXT_P(cstring_to_text(result)); } @@ -1857,7 +1868,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS) /* * Extract scalar value from raw-scalar pseudo-array jsonb. */ -static bool +bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res) { JsonbIterator *it; |