Commit 72b64603 authored by Alexander Korotkov's avatar Alexander Korotkov

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
parent 893d6f8a
...@@ -136,6 +136,17 @@ ...@@ -136,6 +136,17 @@
<pubdate>1988</pubdate> <pubdate>1988</pubdate>
</biblioentry> </biblioentry>
<biblioentry id="sqltr-19075-6">
<title>SQL Technical Report</title>
<subtitle>Part 6: SQL support for JavaScript Object
Notation (JSON)</subtitle>
<edition>First Edition.</edition>
<biblioid>
<ulink url="http://standards.iso.org/ittf/PubliclyAvailableStandards/c067367_ISO_IEC_TR_19075-6_2017.zip"></ulink>.
</biblioid>
<pubdate>2017.</pubdate>
</biblioentry>
</bibliodiv> </bibliodiv>
<bibliodiv> <bibliodiv>
......
This diff is collapsed.
...@@ -22,8 +22,16 @@ ...@@ -22,8 +22,16 @@
</para> </para>
<para> <para>
There are two JSON data types: <type>json</type> and <type>jsonb</type>. <productname>PostgreSQL</productname> offers two types for storing JSON
They accept <emphasis>almost</emphasis> identical sets of values as data: <type>json</type> and <type>jsonb</type>. To implement effective query
mechanisms for these data types, <productname>PostgreSQL</productname>
also provides the <type>jsonpath</type> data type described in
<xref linkend="datatype-jsonpath"/>.
</para>
<para>
The <type>json</type> and <type>jsonb</type> data types
accept <emphasis>almost</emphasis> identical sets of values as
input. The major practical difference is one of efficiency. The input. The major practical difference is one of efficiency. The
<type>json</type> data type stores an exact copy of the input text, <type>json</type> data type stores an exact copy of the input text,
which processing functions must reparse on each execution; while which processing functions must reparse on each execution; while
...@@ -217,6 +225,11 @@ SELECT '{"reading": 1.230e-5}'::json, '{"reading": 1.230e-5}'::jsonb; ...@@ -217,6 +225,11 @@ SELECT '{"reading": 1.230e-5}'::json, '{"reading": 1.230e-5}'::jsonb;
in this example, even though those are semantically insignificant for in this example, even though those are semantically insignificant for
purposes such as equality checks. purposes such as equality checks.
</para> </para>
<para>
For the list of built-in functions and operators available for
constructing and processing JSON values, see <xref linkend="functions-json"/>.
</para>
</sect2> </sect2>
<sect2 id="json-doc-design"> <sect2 id="json-doc-design">
...@@ -593,4 +606,224 @@ SELECT jdoc-&gt;'guid', jdoc-&gt;'name' FROM api WHERE jdoc @&gt; '{"tags": ["qu ...@@ -593,4 +606,224 @@ SELECT jdoc-&gt;'guid', jdoc-&gt;'name' FROM api WHERE jdoc @&gt; '{"tags": ["qu
lists, and scalars, as appropriate. lists, and scalars, as appropriate.
</para> </para>
</sect2> </sect2>
<sect2 id="datatype-jsonpath">
<title>jsonpath Type</title>
<indexterm zone="datatype-jsonpath">
<primary>jsonpath</primary>
</indexterm>
<para>
The <type>jsonpath</type> type implements support for the SQL/JSON path language
in <productname>PostgreSQL</productname> to effectively query JSON data.
It provides a binary representation of the parsed SQL/JSON path
expression that specifies the items to be retrieved by the path
engine from the JSON data for further processing with the
SQL/JSON query functions.
</para>
<para>
The SQL/JSON path language is fully integrated into the SQL engine:
the semantics of its predicates and operators generally follow SQL.
At the same time, to provide a most natural way of working with JSON data,
SQL/JSON path syntax uses some of the JavaScript conventions:
</para>
<itemizedlist>
<listitem>
<para>
Dot <literal>.</literal> is used for member access.
</para>
</listitem>
<listitem>
<para>
Square brackets <literal>[]</literal> are used for array access.
</para>
</listitem>
<listitem>
<para>
SQL/JSON arrays are 0-relative, unlike regular SQL arrays that start from 1.
</para>
</listitem>
</itemizedlist>
<para>
An SQL/JSON path expression is an SQL character string literal,
so it must be enclosed in single quotes when passed to an SQL/JSON
query function. Following the JavaScript
conventions, character string literals within the path expression
must be enclosed in double quotes. Any single quotes within this
character string literal must be escaped with a single quote
by the SQL convention.
</para>
<para>
A path expression consists of a sequence of path elements,
which can be the following:
<itemizedlist>
<listitem>
<para>
Path literals of JSON primitive types:
Unicode text, numeric, true, false, or null.
</para>
</listitem>
<listitem>
<para>
Path variables listed in <xref linkend="type-jsonpath-variables"/>.
</para>
</listitem>
<listitem>
<para>
Accessor operators listed in <xref linkend="type-jsonpath-accessors"/>.
</para>
</listitem>
<listitem>
<para>
<type>jsonpath</type> operators and methods listed
in <xref linkend="functions-sqljson-path-operators"/>
</para>
</listitem>
<listitem>
<para>
Parentheses, which can be used to provide filter expressions
or define the order of path evaluation.
</para>
</listitem>
</itemizedlist>
</para>
<para>
For details on using <type>jsonpath</type> expressions with SQL/JSON
query functions, see <xref linkend="functions-sqljson-path"/>.
</para>
<table id="type-jsonpath-variables">
<title><type>jsonpath</type> Variables</title>
<tgroup cols="2">
<thead>
<row>
<entry>Variable</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><literal>$</literal></entry>
<entry>A variable representing the JSON text to be queried
(the <firstterm>context item</firstterm>).
</entry>
</row>
<row>
<entry><literal>$varname</literal></entry>
<entry>A named variable. Its value must be set in the
<command>PASSING</command> clause of an SQL/JSON query function.
<!-- TBD: See <xref linkend="sqljson-input-clause"/> -->
for details.
</entry>
</row>
<row>
<entry><literal>@</literal></entry>
<entry>A variable representing the result of path evaluation
in filter expressions.
</entry>
</row>
</tbody>
</tgroup>
</table>
<table id="type-jsonpath-accessors">
<title><type>jsonpath</type> Accessors</title>
<tgroup cols="2">
<thead>
<row>
<entry>Accessor Operator</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<para>
<literal>.<replaceable>key</replaceable></literal>
</para>
<para>
<literal>."$<replaceable>varname</replaceable>"</literal>
</para>
</entry>
<entry>
<para>
Member accessor that returns an object member with
the specified key. If the key name is a named variable
starting with <literal>$</literal> or does not meet the
JavaScript rules of an identifier, it must be enclosed in
double quotes as a character string literal.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>.*</literal>
</para>
</entry>
<entry>
<para>
Wildcard member accessor that returns the values of all
members located at the top level of the current object.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>.**</literal>
</para>
</entry>
<entry>
<para>
Recursive wildcard member accessor that processes all levels
of the JSON hierarchy of the current object and returns all
the member values, regardless of their nesting level. This
is a <productname>PostgreSQL</productname> extension of
the SQL/JSON standard.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>[<replaceable>subscript</replaceable>, ...]</literal>
</para>
<para>
<literal>[<replaceable>subscript</replaceable> to last]</literal>
</para>
</entry>
<entry>
<para>
Array element accessor. The provided numeric subscripts return the
corresponding array elements. The first element in an array is
accessed with [0]. The <literal>last</literal> keyword denotes
the last subscript in an array and can be used to handle arrays
of unknown length.
</para>
</entry>
</row>
<row>
<entry>
<para>
<literal>[*]</literal>
</para>
</entry>
<entry>
<para>
Wildcard array element accessor that returns all array elements.
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
</sect2>
</sect1> </sect1>
...@@ -136,6 +136,9 @@ parser/gram.h: parser/gram.y ...@@ -136,6 +136,9 @@ parser/gram.h: parser/gram.y
storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lwlocknames.txt storage/lmgr/lwlocknames.h: storage/lmgr/generate-lwlocknames.pl storage/lmgr/lwlocknames.txt
$(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c
utils/adt/jsonpath_gram.h: utils/adt/jsonpath_gram.y
$(MAKE) -C utils/adt jsonpath_gram.h
# run this unconditionally to avoid needing to know its dependencies here: # run this unconditionally to avoid needing to know its dependencies here:
submake-catalog-headers: submake-catalog-headers:
$(MAKE) -C catalog distprep generated-header-symlinks $(MAKE) -C catalog distprep generated-header-symlinks
...@@ -159,7 +162,7 @@ submake-utils-headers: ...@@ -159,7 +162,7 @@ submake-utils-headers:
.PHONY: generated-headers .PHONY: generated-headers
generated-headers: $(top_builddir)/src/include/parser/gram.h $(top_builddir)/src/include/storage/lwlocknames.h submake-catalog-headers submake-utils-headers generated-headers: $(top_builddir)/src/include/parser/gram.h $(top_builddir)/src/include/storage/lwlocknames.h $(top_builddir)/src/include/utils/jsonpath_gram.h submake-catalog-headers submake-utils-headers
$(top_builddir)/src/include/parser/gram.h: parser/gram.h $(top_builddir)/src/include/parser/gram.h: parser/gram.h
prereqdir=`cd '$(dir $<)' >/dev/null && pwd` && \ prereqdir=`cd '$(dir $<)' >/dev/null && pwd` && \
...@@ -171,6 +174,10 @@ $(top_builddir)/src/include/storage/lwlocknames.h: storage/lmgr/lwlocknames.h ...@@ -171,6 +174,10 @@ $(top_builddir)/src/include/storage/lwlocknames.h: storage/lmgr/lwlocknames.h
cd '$(dir $@)' && rm -f $(notdir $@) && \ cd '$(dir $@)' && rm -f $(notdir $@) && \
$(LN_S) "$$prereqdir/$(notdir $<)" . $(LN_S) "$$prereqdir/$(notdir $<)" .
$(top_builddir)/src/include/utils/jsonpath_gram.h: utils/adt/jsonpath_gram.h
prereqdir=`cd '$(dir $<)' >/dev/null && pwd` && \
cd '$(dir $@)' && rm -f $(notdir $@) && \
$(LN_S) "$$prereqdir/$(notdir $<)" .
utils/probes.o: utils/probes.d $(SUBDIROBJS) utils/probes.o: utils/probes.d $(SUBDIROBJS)
$(DTRACE) $(DTRACEFLAGS) -C -G -s $(call expand_subsys,$^) -o $@ $(DTRACE) $(DTRACEFLAGS) -C -G -s $(call expand_subsys,$^) -o $@
...@@ -186,6 +193,7 @@ distprep: ...@@ -186,6 +193,7 @@ distprep:
$(MAKE) -C replication repl_gram.c repl_scanner.c syncrep_gram.c syncrep_scanner.c $(MAKE) -C replication repl_gram.c repl_scanner.c syncrep_gram.c syncrep_scanner.c
$(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c $(MAKE) -C storage/lmgr lwlocknames.h lwlocknames.c
$(MAKE) -C utils distprep $(MAKE) -C utils distprep
$(MAKE) -C utils/adt jsonpath_gram.c jsonpath_gram.h jsonpath_scan.c
$(MAKE) -C utils/misc guc-file.c $(MAKE) -C utils/misc guc-file.c
$(MAKE) -C utils/sort qsort_tuple.c $(MAKE) -C utils/sort qsort_tuple.c
...@@ -308,6 +316,7 @@ maintainer-clean: distclean ...@@ -308,6 +316,7 @@ maintainer-clean: distclean
storage/lmgr/lwlocknames.c \ storage/lmgr/lwlocknames.c \
storage/lmgr/lwlocknames.h \ storage/lmgr/lwlocknames.h \
utils/misc/guc-file.c \ utils/misc/guc-file.c \
utils/adt/jsonpath_gram.h \
utils/sort/qsort_tuple.c utils/sort/qsort_tuple.c
......
...@@ -1128,6 +1128,46 @@ LANGUAGE INTERNAL ...@@ -1128,6 +1128,46 @@ LANGUAGE INTERNAL
STRICT IMMUTABLE PARALLEL SAFE STRICT IMMUTABLE PARALLEL SAFE
AS 'jsonb_insert'; AS 'jsonb_insert';
CREATE OR REPLACE FUNCTION
jsonb_path_exists(target jsonb, path jsonpath, vars jsonb DEFAULT '{}',
silent boolean DEFAULT false)
RETURNS boolean
LANGUAGE INTERNAL
STRICT IMMUTABLE PARALLEL SAFE
AS 'jsonb_path_exists';
CREATE OR REPLACE FUNCTION
jsonb_path_match(target jsonb, path jsonpath, vars jsonb DEFAULT '{}',
silent boolean DEFAULT false)
RETURNS boolean
LANGUAGE INTERNAL
STRICT IMMUTABLE PARALLEL SAFE
AS 'jsonb_path_match';
CREATE OR REPLACE FUNCTION
jsonb_path_query(target jsonb, path jsonpath, vars jsonb DEFAULT '{}',
silent boolean DEFAULT false)
RETURNS SETOF jsonb
LANGUAGE INTERNAL
STRICT IMMUTABLE PARALLEL SAFE
AS 'jsonb_path_query';
CREATE OR REPLACE FUNCTION
jsonb_path_query_array(target jsonb, path jsonpath, vars jsonb DEFAULT '{}',
silent boolean DEFAULT false)
RETURNS jsonb
LANGUAGE INTERNAL
STRICT IMMUTABLE PARALLEL SAFE
AS 'jsonb_path_query_array';
CREATE OR REPLACE FUNCTION
jsonb_path_query_first(target jsonb, path jsonpath, vars jsonb DEFAULT '{}',
silent boolean DEFAULT false)
RETURNS jsonb
LANGUAGE INTERNAL
STRICT IMMUTABLE PARALLEL SAFE
AS 'jsonb_path_query_first';
-- --
-- The default permissions for functions mean that anyone can execute them. -- The default permissions for functions mean that anyone can execute them.
-- A number of functions shouldn't be executable by just anyone, but rather -- A number of functions shouldn't be executable by just anyone, but rather
......
/jsonpath_gram.h
/jsonpath_gram.c
/jsonpath_scan.c
...@@ -17,8 +17,8 @@ OBJS = acl.o amutils.o arrayfuncs.o array_expanded.o array_selfuncs.o \ ...@@ -17,8 +17,8 @@ OBJS = acl.o amutils.o arrayfuncs.o array_expanded.o array_selfuncs.o \
float.o format_type.o formatting.o genfile.o \ float.o format_type.o formatting.o genfile.o \
geo_ops.o geo_selfuncs.o geo_spgist.o inet_cidr_ntop.o inet_net_pton.o \ geo_ops.o geo_selfuncs.o geo_spgist.o inet_cidr_ntop.o inet_net_pton.o \
int.o int8.o json.o jsonb.o jsonb_gin.o jsonb_op.o jsonb_util.o \ int.o int8.o json.o jsonb.o jsonb_gin.o jsonb_op.o jsonb_util.o \
jsonfuncs.o like.o like_support.o lockfuncs.o \ jsonfuncs.o jsonpath_gram.o jsonpath_scan.o jsonpath.o jsonpath_exec.o \
mac.o mac8.o misc.o name.o \ like.o like_support.o lockfuncs.o mac.o mac8.o misc.o name.o \
network.o network_gist.o network_selfuncs.o network_spgist.o \ network.o network_gist.o network_selfuncs.o network_spgist.o \
numeric.o numutils.o oid.o oracle_compat.o \ numeric.o numutils.o oid.o oracle_compat.o \
orderedsetaggs.o partitionfuncs.o pg_locale.o pg_lsn.o \ orderedsetaggs.o partitionfuncs.o pg_locale.o pg_lsn.o \
...@@ -33,6 +33,21 @@ OBJS = acl.o amutils.o arrayfuncs.o array_expanded.o array_selfuncs.o \ ...@@ -33,6 +33,21 @@ OBJS = acl.o amutils.o arrayfuncs.o array_expanded.o array_selfuncs.o \
txid.o uuid.o varbit.o varchar.o varlena.o version.o \ txid.o uuid.o varbit.o varchar.o varlena.o version.o \
windowfuncs.o xid.o xml.o windowfuncs.o xid.o xml.o
jsonpath_gram.c: BISONFLAGS += -d
jsonpath_scan.c: FLEXFLAGS = -CF -p -p
jsonpath_gram.h: jsonpath_gram.c ;
# Force these dependencies to be known even without dependency info built:
jsonpath_gram.o jsonpath_scan.o jsonpath_parser.o: jsonpath_gram.h
# jsonpath_gram.c, jsonpath_gram.h, and jsonpath_scan.c are in the
# distribution tarball, so they are not cleaned here.
clean distclean maintainer-clean:
rm -f lex.backup
like.o: like.c like_match.c like.o: like.c like_match.c
varlena.o: varlena.c levenshtein.c varlena.o: varlena.c levenshtein.c
......
...@@ -163,6 +163,55 @@ jsonb_send(PG_FUNCTION_ARGS) ...@@ -163,6 +163,55 @@ jsonb_send(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
} }
/*
* 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 * SQL function jsonb_typeof(jsonb) -> text
* *
...@@ -173,45 +222,7 @@ Datum ...@@ -173,45 +222,7 @@ Datum
jsonb_typeof(PG_FUNCTION_ARGS) jsonb_typeof(PG_FUNCTION_ARGS)
{ {
Jsonb *in = PG_GETARG_JSONB_P(0); Jsonb *in = PG_GETARG_JSONB_P(0);
JsonbIterator *it; const char *result = JsonbContainerTypeName(&in->root);
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");
}
}
PG_RETURN_TEXT_P(cstring_to_text(result)); PG_RETURN_TEXT_P(cstring_to_text(result));
} }
...@@ -1857,7 +1868,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS) ...@@ -1857,7 +1868,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
/* /*
* Extract scalar value from raw-scalar pseudo-array jsonb. * Extract scalar value from raw-scalar pseudo-array jsonb.
*/ */
static bool bool
JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res) JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res)
{ {
JsonbIterator *it; JsonbIterator *it;
......
...@@ -1728,6 +1728,14 @@ convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal) ...@@ -1728,6 +1728,14 @@ convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
break; break;
case jbvNumeric: case jbvNumeric:
/* replace numeric NaN with string "NaN" */
if (numeric_is_nan(scalarVal->val.numeric))
{
appendToBuffer(buffer, "NaN", 3);
*jentry = 3;
break;
}
numlen = VARSIZE_ANY(scalarVal->val.numeric); numlen = VARSIZE_ANY(scalarVal->val.numeric);
padlen = padBufferToInt(buffer); padlen = padBufferToInt(buffer);
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -133,7 +133,7 @@ static Datum build_regexp_split_result(regexp_matches_ctx *splitctx); ...@@ -133,7 +133,7 @@ static Datum build_regexp_split_result(regexp_matches_ctx *splitctx);
* Pattern is given in the database encoding. We internally convert to * Pattern is given in the database encoding. We internally convert to
* an array of pg_wchar, which is what Spencer's regex package wants. * an array of pg_wchar, which is what Spencer's regex package wants.
*/ */
static regex_t * regex_t *
RE_compile_and_cache(text *text_re, int cflags, Oid collation) RE_compile_and_cache(text *text_re, int cflags, Oid collation)
{ {
int text_re_len = VARSIZE_ANY_EXHDR(text_re); int text_re_len = VARSIZE_ANY_EXHDR(text_re);
...@@ -339,7 +339,7 @@ RE_execute(regex_t *re, char *dat, int dat_len, ...@@ -339,7 +339,7 @@ RE_execute(regex_t *re, char *dat, int dat_len,
* Both pattern and data are given in the database encoding. We internally * Both pattern and data are given in the database encoding. We internally
* convert to array of pg_wchar which is what Spencer's regex package wants. * convert to array of pg_wchar which is what Spencer's regex package wants.
*/ */
static bool bool
RE_compile_and_execute(text *text_re, char *dat, int dat_len, RE_compile_and_execute(text *text_re, char *dat, int dat_len,
int cflags, Oid collation, int cflags, Oid collation,
int nmatch, regmatch_t *pmatch) int nmatch, regmatch_t *pmatch)
......
...@@ -206,6 +206,21 @@ Section: Class 22 - Data Exception ...@@ -206,6 +206,21 @@ Section: Class 22 - Data Exception
2200N E ERRCODE_INVALID_XML_CONTENT invalid_xml_content 2200N E ERRCODE_INVALID_XML_CONTENT invalid_xml_content
2200S E ERRCODE_INVALID_XML_COMMENT invalid_xml_comment 2200S E ERRCODE_INVALID_XML_COMMENT invalid_xml_comment
2200T E ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION invalid_xml_processing_instruction 2200T E ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION invalid_xml_processing_instruction
22030 E ERRCODE_DUPLICATE_JSON_OBJECT_KEY_VALUE duplicate_json_object_key_value
22032 E ERRCODE_INVALID_JSON_TEXT invalid_json_text
22033 E ERRCODE_INVALID_JSON_SUBSCRIPT invalid_json_subscript
22034 E ERRCODE_MORE_THAN_ONE_JSON_ITEM more_than_one_json_item
22035 E ERRCODE_NO_JSON_ITEM no_json_item
22036 E ERRCODE_NON_NUMERIC_JSON_ITEM non_numeric_json_item
22037 E ERRCODE_NON_UNIQUE_KEYS_IN_JSON_OBJECT non_unique_keys_in_json_object
22038 E ERRCODE_SINGLETON_JSON_ITEM_REQUIRED singleton_json_item_required
22039 E ERRCODE_JSON_ARRAY_NOT_FOUND json_array_not_found
2203A E ERRCODE_JSON_MEMBER_NOT_FOUND json_member_not_found
2203B E ERRCODE_JSON_NUMBER_NOT_FOUND json_number_not_found
2203C E ERRCODE_JSON_OBJECT_NOT_FOUND object_not_found
2203F E ERRCODE_JSON_SCALAR_REQUIRED json_scalar_required
2203D E ERRCODE_TOO_MANY_JSON_ARRAY_ELEMENTS too_many_json_array_elements
2203E E ERRCODE_TOO_MANY_JSON_OBJECT_MEMBERS too_many_json_object_members
Section: Class 23 - Integrity Constraint Violation Section: Class 23 - Integrity Constraint Violation
......
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 201903121 #define CATALOG_VERSION_NO 201903161
#endif #endif
...@@ -3255,5 +3255,13 @@ ...@@ -3255,5 +3255,13 @@
{ oid => '3287', descr => 'delete path', { oid => '3287', descr => 'delete path',
oprname => '#-', oprleft => 'jsonb', oprright => '_text', oprname => '#-', oprleft => 'jsonb', oprright => '_text',
oprresult => 'jsonb', oprcode => 'jsonb_delete_path' }, oprresult => 'jsonb', oprcode => 'jsonb_delete_path' },
{ oid => '4012', descr => 'jsonpath exists',
oprname => '@?', oprleft => 'jsonb', oprright => 'jsonpath',
oprresult => 'bool', oprcode => 'jsonb_path_exists(jsonb,jsonpath)',
oprrest => 'contsel', oprjoin => 'contjoinsel' },
{ oid => '4013', descr => 'jsonpath match',
oprname => '@@', oprleft => 'jsonb', oprright => 'jsonpath',
oprresult => 'bool', oprcode => 'jsonb_path_match(jsonb,jsonpath)',
oprrest => 'contsel', oprjoin => 'contjoinsel' },
] ]
...@@ -9216,6 +9216,45 @@ ...@@ -9216,6 +9216,45 @@
proname => 'jsonb_insert', prorettype => 'jsonb', proname => 'jsonb_insert', prorettype => 'jsonb',
proargtypes => 'jsonb _text jsonb bool', prosrc => 'jsonb_insert' }, proargtypes => 'jsonb _text jsonb bool', prosrc => 'jsonb_insert' },
# jsonpath
{ oid => '4001', descr => 'I/O',
proname => 'jsonpath_in', prorettype => 'jsonpath', proargtypes => 'cstring',
prosrc => 'jsonpath_in' },
{ oid => '4002', descr => 'I/O',
proname => 'jsonpath_recv', prorettype => 'jsonpath', proargtypes => 'internal',
prosrc => 'jsonpath_recv' },
{ oid => '4003', descr => 'I/O',
proname => 'jsonpath_out', prorettype => 'cstring', proargtypes => 'jsonpath',
prosrc => 'jsonpath_out' },
{ oid => '4004', descr => 'I/O',
proname => 'jsonpath_send', prorettype => 'bytea', proargtypes => 'jsonpath',
prosrc => 'jsonpath_send' },
{ oid => '4005', descr => 'jsonpath exists test',
proname => 'jsonb_path_exists', prorettype => 'bool',
proargtypes => 'jsonb jsonpath jsonb bool', prosrc => 'jsonb_path_exists' },
{ oid => '4006', descr => 'jsonpath query',
proname => 'jsonb_path_query', prorows => '1000', proretset => 't',
prorettype => 'jsonb', proargtypes => 'jsonb jsonpath jsonb bool',
prosrc => 'jsonb_path_query' },
{ oid => '4007', descr => 'jsonpath query wrapped into array',
proname => 'jsonb_path_query_array', prorettype => 'jsonb',
proargtypes => 'jsonb jsonpath jsonb bool',
prosrc => 'jsonb_path_query_array' },
{ oid => '4008', descr => 'jsonpath query first item',
proname => 'jsonb_path_query_first', prorettype => 'jsonb',
proargtypes => 'jsonb jsonpath jsonb bool', prosrc => 'jsonb_path_query_first' },
{ oid => '4009', descr => 'jsonpath match', proname => 'jsonb_path_match',
prorettype => 'bool', proargtypes => 'jsonb jsonpath jsonb bool',
prosrc => 'jsonb_path_match' },
{ oid => '4010', descr => 'implementation of @? operator',
proname => 'jsonb_path_exists', prorettype => 'bool',
proargtypes => 'jsonb jsonpath', prosrc => 'jsonb_path_exists_opr' },
{ oid => '4011', descr => 'implementation of @@ operator',
proname => 'jsonb_path_match', prorettype => 'bool',
proargtypes => 'jsonb jsonpath', prosrc => 'jsonb_path_match_opr' },
# txid # txid
{ oid => '2939', descr => 'I/O', { oid => '2939', descr => 'I/O',
proname => 'txid_snapshot_in', prorettype => 'txid_snapshot', proname => 'txid_snapshot_in', prorettype => 'txid_snapshot',
......
...@@ -434,6 +434,11 @@ ...@@ -434,6 +434,11 @@
typname => 'jsonb', typlen => '-1', typbyval => 'f', typcategory => 'U', typname => 'jsonb', typlen => '-1', typbyval => 'f', typcategory => 'U',
typinput => 'jsonb_in', typoutput => 'jsonb_out', typreceive => 'jsonb_recv', typinput => 'jsonb_in', typoutput => 'jsonb_out', typreceive => 'jsonb_recv',
typsend => 'jsonb_send', typalign => 'i', typstorage => 'x' }, typsend => 'jsonb_send', typalign => 'i', typstorage => 'x' },
{ oid => '4072', array_type_oid => '4073', descr => 'JSON path',
typname => 'jsonpath', typlen => '-1', typbyval => 'f', typcategory => 'U',
typarray => '_jsonpath', typinput => 'jsonpath_in',
typoutput => 'jsonpath_out', typreceive => '-', typsend => '-',
typalign => 'i', typstorage => 'x' },
{ oid => '2970', array_type_oid => '2949', descr => 'txid snapshot', { oid => '2970', array_type_oid => '2949', descr => 'txid snapshot',
typname => 'txid_snapshot', typlen => '-1', typbyval => 'f', typname => 'txid_snapshot', typlen => '-1', typbyval => 'f',
......
...@@ -167,10 +167,18 @@ typedef struct ...@@ -167,10 +167,18 @@ typedef struct
/* /*
* the prototypes for exported functions * the prototypes for exported functions
*/ */
/* regcomp.c */
extern int pg_regcomp(regex_t *, const pg_wchar *, size_t, int, Oid); extern int pg_regcomp(regex_t *, const pg_wchar *, size_t, int, Oid);
extern int pg_regexec(regex_t *, const pg_wchar *, size_t, size_t, rm_detail_t *, size_t, regmatch_t[], int); extern int pg_regexec(regex_t *, const pg_wchar *, size_t, size_t, rm_detail_t *, size_t, regmatch_t[], int);
extern int pg_regprefix(regex_t *, pg_wchar **, size_t *); extern int pg_regprefix(regex_t *, pg_wchar **, size_t *);
extern void pg_regfree(regex_t *); extern void pg_regfree(regex_t *);
extern size_t pg_regerror(int, const regex_t *, char *, size_t); extern size_t pg_regerror(int, const regex_t *, char *, size_t);
/* regexp.c */
extern regex_t *RE_compile_and_cache(text *text_re, int cflags, Oid collation);
extern bool RE_compile_and_execute(text *text_re, char *dat, int dat_len,
int cflags, Oid collation,
int nmatch, regmatch_t *pmatch);
#endif /* _REGEX_H_ */ #endif /* _REGEX_H_ */
...@@ -3,3 +3,4 @@ ...@@ -3,3 +3,4 @@
/probes.h /probes.h
/errcodes.h /errcodes.h
/header-stamp /header-stamp
/jsonpath_gram.h
...@@ -66,8 +66,10 @@ typedef enum ...@@ -66,8 +66,10 @@ typedef enum
/* Convenience macros */ /* Convenience macros */
#define DatumGetJsonbP(d) ((Jsonb *) PG_DETOAST_DATUM(d)) #define DatumGetJsonbP(d) ((Jsonb *) PG_DETOAST_DATUM(d))
#define DatumGetJsonbPCopy(d) ((Jsonb *) PG_DETOAST_DATUM_COPY(d))
#define JsonbPGetDatum(p) PointerGetDatum(p) #define JsonbPGetDatum(p) PointerGetDatum(p)
#define PG_GETARG_JSONB_P(x) DatumGetJsonbP(PG_GETARG_DATUM(x)) #define PG_GETARG_JSONB_P(x) DatumGetJsonbP(PG_GETARG_DATUM(x))
#define PG_GETARG_JSONB_P_COPY(x) DatumGetJsonbPCopy(PG_GETARG_DATUM(x))
#define PG_RETURN_JSONB_P(x) PG_RETURN_POINTER(x) #define PG_RETURN_JSONB_P(x) PG_RETURN_POINTER(x)
typedef struct JsonbPair JsonbPair; typedef struct JsonbPair JsonbPair;
...@@ -378,6 +380,8 @@ extern char *JsonbToCString(StringInfo out, JsonbContainer *in, ...@@ -378,6 +380,8 @@ extern char *JsonbToCString(StringInfo out, JsonbContainer *in,
int estimated_len); int estimated_len);
extern char *JsonbToCStringIndent(StringInfo out, JsonbContainer *in, extern char *JsonbToCStringIndent(StringInfo out, JsonbContainer *in,
int estimated_len); int estimated_len);
extern bool JsonbExtractScalar(JsonbContainer *jbc, JsonbValue *res);
extern const char *JsonbTypeName(JsonbValue *jb);
#endif /* __JSONB_H__ */ #endif /* __JSONB_H__ */
This diff is collapsed.
/*-------------------------------------------------------------------------
*
* jsonpath_scanner.h
* Definitions for jsonpath scanner & parser
*
* Portions Copyright (c) 2019, PostgreSQL Global Development Group
*
* src/include/utils/jsonpath_scanner.h
*
*-------------------------------------------------------------------------
*/
#ifndef JSONPATH_SCANNER_H
#define JSONPATH_SCANNER_H
/* struct string is shared between scan and gram */
typedef struct string
{
char *val;
int len;
int total;
} string;
#include "utils/jsonpath.h"
#include "utils/jsonpath_gram.h"
/* flex 2.5.4 doesn't bother with a decl for this */
extern int jsonpath_yylex(YYSTYPE *yylval_param);
extern int jsonpath_yyparse(JsonPathParseResult **result);
extern void jsonpath_yyerror(JsonPathParseResult **result, const char *message);
#endif
This diff is collapsed.
This diff is collapsed.
...@@ -104,7 +104,12 @@ test: publication subscription ...@@ -104,7 +104,12 @@ test: publication subscription
# ---------- # ----------
# Another group of parallel tests # Another group of parallel tests
# ---------- # ----------
test: select_views portals_p2 foreign_key cluster dependency guc bitmapops combocid tsearch tsdicts foreign_data window xmlmap functional_deps advisory_lock json jsonb json_encoding indirect_toast equivclass test: select_views portals_p2 foreign_key cluster dependency guc bitmapops combocid tsearch tsdicts foreign_data window xmlmap functional_deps advisory_lock indirect_toast equivclass
# ----------
# Another group of parallel tests (JSON related)
# ----------
test: json jsonb json_encoding jsonpath jsonb_jsonpath
# ---------- # ----------
# Another group of parallel tests # Another group of parallel tests
......
...@@ -159,6 +159,8 @@ test: advisory_lock ...@@ -159,6 +159,8 @@ test: advisory_lock
test: json test: json
test: jsonb test: jsonb
test: json_encoding test: json_encoding
test: jsonpath
test: jsonb_jsonpath
test: indirect_toast test: indirect_toast
test: equivclass test: equivclass
test: plancache test: plancache
......
This diff is collapsed.
This diff is collapsed.
...@@ -179,6 +179,8 @@ sub mkvcbuild ...@@ -179,6 +179,8 @@ sub mkvcbuild
'src/backend/replication', 'repl_scanner.l', 'src/backend/replication', 'repl_scanner.l',
'repl_gram.y', 'syncrep_scanner.l', 'repl_gram.y', 'syncrep_scanner.l',
'syncrep_gram.y'); 'syncrep_gram.y');
$postgres->AddFiles('src/backend/utils/adt', 'jsonpath_scan.l',
'jsonpath_gram.y');
$postgres->AddDefine('BUILDING_DLL'); $postgres->AddDefine('BUILDING_DLL');
$postgres->AddLibrary('secur32.lib'); $postgres->AddLibrary('secur32.lib');
$postgres->AddLibrary('ws2_32.lib'); $postgres->AddLibrary('ws2_32.lib');
......
...@@ -327,6 +327,24 @@ sub GenerateFiles ...@@ -327,6 +327,24 @@ sub GenerateFiles
); );
} }
if (IsNewer(
'src/backend/utils/adt/jsonpath_gram.h',
'src/backend/utils/adt/jsonpath_gram.y'))
{
print "Generating jsonpath_gram.h...\n";
chdir('src/backend/utils/adt');
system('perl ../../../tools/msvc/pgbison.pl jsonpath_gram.y');
chdir('../../../..');
}
if (IsNewer(
'src/include/utils/jsonpath_gram.h',
'src/backend/utils/adt/jsonpath_gram.h'))
{
copyFile('src/backend/utils/adt/jsonpath_gram.h',
'src/include/utils/jsonpath_gram.h');
}
if ($self->{options}->{python} if ($self->{options}->{python}
&& IsNewer( && IsNewer(
'src/pl/plpython/spiexceptions.h', 'src/pl/plpython/spiexceptions.h',
......
...@@ -1097,14 +1097,27 @@ JoinType ...@@ -1097,14 +1097,27 @@ JoinType
JsObject JsObject
JsValue JsValue
JsonAggState JsonAggState
JsonBaseObjectInfo
JsonHashEntry JsonHashEntry
JsonIterateStringValuesAction JsonIterateStringValuesAction
JsonLexContext JsonLexContext
JsonLikeRegexContext
JsonParseContext JsonParseContext
JsonPath
JsonPathBool
JsonPathExecContext
JsonPathExecResult
JsonPathItem
JsonPathItemType
JsonPathParseItem
JsonPathParseResult
JsonPathPredicateCallback
JsonSemAction JsonSemAction
JsonTokenType JsonTokenType
JsonTransformStringValuesAction JsonTransformStringValuesAction
JsonTypeCategory JsonTypeCategory
JsonValueList
JsonValueListIterator
Jsonb Jsonb
JsonbAggState JsonbAggState
JsonbContainer JsonbContainer
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment