mirror of https://github.com/postgres/postgres
The new format accepts exactly the same data as the json type. However, it is stored in a format that does not require reparsing the orgiginal text in order to process it, making it much more suitable for indexing and other operations. Insignificant whitespace is discarded, and the order of object keys is not preserved. Neither are duplicate object keys kept - the later value for a given key is the only one stored. The new type has all the functions and operators that the json type has, with the exception of the json generation functions (to_json, json_agg etc.) and with identical semantics. In addition, there are operator classes for hash and btree indexing, and two classes for GIN indexing, that have no equivalent in the json type. This feature grew out of previous work by Oleg Bartunov and Teodor Sigaev, which was intended to provide similar facilities to a nested hstore type, but which in the end proved to have some significant compatibility issues. Authors: Oleg Bartunov, Teodor Sigaev, Peter Geoghegan and Andrew Dunstan. Review: Andres Freundpull/6/head
parent
b2b2491b06
commit
d9134d0a35
@ -0,0 +1,17 @@ |
||||
/* contrib/hstore/hstore--1.2--1.3.sql */ |
||||
|
||||
-- complain if script is sourced in psql, rather than via ALTER EXTENSION |
||||
\echo Use "ALTER EXTENSION hstore UPDATE TO '1.3'" to load this file. \quit |
||||
|
||||
CREATE FUNCTION hstore_to_jsonb(hstore) |
||||
RETURNS jsonb |
||||
AS 'MODULE_PATHNAME', 'hstore_to_jsonb' |
||||
LANGUAGE C IMMUTABLE STRICT; |
||||
|
||||
CREATE CAST (hstore AS jsonb) |
||||
WITH FUNCTION hstore_to_jsonb(hstore); |
||||
|
||||
CREATE FUNCTION hstore_to_jsonb_loose(hstore) |
||||
RETURNS jsonb |
||||
AS 'MODULE_PATHNAME', 'hstore_to_jsonb_loose' |
||||
LANGUAGE C IMMUTABLE STRICT; |
||||
@ -1,5 +1,5 @@ |
||||
# hstore extension |
||||
comment = 'data type for storing sets of (key, value) pairs' |
||||
default_version = '1.2' |
||||
default_version = '1.3' |
||||
module_pathname = '$libdir/hstore' |
||||
relocatable = true |
||||
|
||||
@ -0,0 +1,413 @@ |
||||
<!-- doc/src/sgml/json.sgml --> |
||||
|
||||
<sect1 id="datatype-json"> |
||||
<title><acronym>JSON</> Types</title> |
||||
|
||||
<indexterm zone="datatype-json"> |
||||
<primary>JSON</primary> |
||||
</indexterm> |
||||
|
||||
<indexterm zone="datatype-json"> |
||||
<primary>JSONB</primary> |
||||
</indexterm> |
||||
|
||||
<para> |
||||
JSON data types are for storing JSON (JavaScript Object Notation) |
||||
data, as specified in <ulink url="http://rfc7159.net/rfc7159">RFC |
||||
7159</ulink>. Such data can also be stored as <type>text</type>, but |
||||
both JSON data types have the advantage of enforcing that each |
||||
stored value is a valid JSON value. There are also related support |
||||
functions available; see <xref linkend="functions-json">. |
||||
</para> |
||||
|
||||
<para> |
||||
There are two JSON data types: <type>json</> and <type>jsonb</>. |
||||
Both accept <emphasis>almost</emphasis> identical sets of values as |
||||
input. The major practical difference is one of efficiency. The |
||||
<type>json</> data type stores an exact copy of the the input text, |
||||
which processing functions must continually reparse, while |
||||
<type>jsonb</> data is stored in a decomposed binary format that |
||||
makes it slightly less efficient to input due to added serialization |
||||
overhead, but significantly faster to process, since it never needs |
||||
reparsing. <type>jsonb</> also supports advanced |
||||
<acronym>GIN</acronym> indexing, which is a further significant |
||||
advantage. |
||||
</para> |
||||
|
||||
<para> |
||||
The other difference between the types is that the <type>json</> |
||||
type is guaranteed to contain an exact copy of the input, including |
||||
preservation of semantically insignificant white space, and the |
||||
order of keys within JSON objects (although <type>jsonb</> will |
||||
preserve trailing zeros within a JSON number). Also, because the |
||||
exact text is kept, if a JSON object within the value contains the |
||||
same key more than once, and has been stored using the <type>json</> |
||||
type, all the key/value pairs are kept. In that case, the |
||||
processing functions consider the last value as the operative one. |
||||
By contrast, <type>jsonb</> does not preserve white space, does not |
||||
preserve the order of object keys, and does not keep duplicate |
||||
object keys. Only the last value for a key specified in the input |
||||
is kept. |
||||
</para> |
||||
|
||||
<para> |
||||
In general, most applications will prefer to store JSON data as |
||||
<type>jsonb</>, unless there are quite specialized needs. |
||||
</para> |
||||
|
||||
<para> |
||||
<productname>PostgreSQL</productname> allows only one server |
||||
encoding per database. It is therefore not possible for the JSON |
||||
types to conform rigidly to the specification unless the server |
||||
encoding is UTF-8. Attempts to directly include characters which |
||||
cannot be represented in the server encoding will fail; conversely, |
||||
characters which can be represented in the server encoding but not |
||||
in UTF-8 will be allowed. <literal>\uXXXX</literal> escapes are |
||||
allowed regardless of the server encoding, and are checked only for |
||||
syntactic correctness. |
||||
</para> |
||||
|
||||
<sect2 id="json-types"> |
||||
<title>Mapping of RFC-7159/JSON Primitive Types to <productname>PostgreSQL</productname> Types</title> |
||||
<table id="json-type-mapping-table"> |
||||
<title>Mapping of type correspondence, notes</title> |
||||
<tgroup cols="3"> |
||||
<thead> |
||||
<row> |
||||
<entry><productname>PostgreSQL</productname> type</entry> |
||||
<entry>RFC-7159/JSON primitive type</entry> |
||||
<entry>Notes</entry> |
||||
</row> |
||||
</thead> |
||||
<tbody> |
||||
<row> |
||||
<entry><type>text</></entry> |
||||
<entry><type>string</></entry> |
||||
<entry>See general introductory notes on encoding and JSON</entry> |
||||
</row> |
||||
<row> |
||||
<entry><type>numeric</></entry> |
||||
<entry><type>number</></entry> |
||||
<entry><literal>NaN</literal> and <literal>infinity</literal> values are disallowed</entry> |
||||
</row> |
||||
<row> |
||||
<entry><type>boolean</></entry> |
||||
<entry><type>boolean</></entry> |
||||
<entry>Only lowercase <literal>true</literal> and <literal>false</literal> values are accepted</entry> |
||||
</row> |
||||
<row> |
||||
<entry><type>unknown</></entry> |
||||
<entry><type>null</></entry> |
||||
<entry>SQL <literal>NULL</literal> is orthogonal. NULL semantics do not apply.</entry> |
||||
</row> |
||||
</tbody> |
||||
</tgroup> |
||||
</table> |
||||
<para> |
||||
Primitive types described by <acronym>RFC</> 7159 are effectively |
||||
internally mapped onto native |
||||
<productname>PostgreSQL</productname> types. Therefore, there are |
||||
some very minor additional constraints on what constitutes valid |
||||
<type>jsonb</type> that do not apply to the <type>json</type> |
||||
type, or to JSON in the abstract, that pertain to limits on what |
||||
can be represented by the underlying type system. These |
||||
implementation-defined restrictions are permitted by |
||||
<acronym>RFC</> 7159. However, in practice problems are far more |
||||
likely to occur in other implementations which internally |
||||
represent the <type>number</> JSON primitive type as IEEE 754 |
||||
double precision floating point values, which <acronym>RFC</> 7159 |
||||
explicitly anticipates and allows for. When using JSON as an |
||||
interchange format with such systems, the danger of losing numeric |
||||
precision in respect of data originally stored by |
||||
<productname>PostgreSQL</productname> should be considered. |
||||
</para> |
||||
<para> |
||||
Conversely, as noted above there are some minor restrictions on |
||||
the input format of JSON primitive types that do not apply to |
||||
corresponding <productname>PostgreSQL</productname> types. |
||||
</para> |
||||
|
||||
</sect2> |
||||
|
||||
<sect2 id="json-querying"> |
||||
<title>Querying <type>jsonb</type> documents effectively</title> |
||||
<para> |
||||
Representing data as JSON can be considerably more flexible than |
||||
the traditional relational data model, which is compelling in |
||||
environments where requirements are fluid. It is quite possible |
||||
for both approaches to co-exist and complement each other within |
||||
the same application. However, even for applications where maximal |
||||
flexibility is desired, it is still recommended that JSON documents |
||||
have a somewhat fixed structure. This structure is typically |
||||
unenforced (though enforcing some business rules declaratively is |
||||
possible), but makes it easier to write queries that usefully |
||||
summarize a set of <quote>documents</> (datums) in a table. |
||||
</para> |
||||
<para> |
||||
<type>jsonb</> data is subject to the same concurrency control |
||||
considerations as any other datatype when stored in a table. |
||||
Although storing large documents is practicable, in order to ensure |
||||
correct behavior row-level locks are, quite naturally, aquired as |
||||
rows are updated. Consider keeping <type>jsonb</> documents at a |
||||
manageable size in order to decrease lock contention among updating |
||||
transactions. Ideally, <type>jsonb</> documents should each |
||||
represent an atomic datum that business rules dictate cannot |
||||
reasonably be further subdivided into smaller atomic datums that |
||||
can be independently modified. |
||||
</para> |
||||
</sect2> |
||||
<sect2 id="json-keys-elements"> |
||||
<title><type>jsonb</> Input and Output Syntax</title> |
||||
<para> |
||||
In effect, <type>jsonb</> has an internal type system whose |
||||
implementation is defined in terms of several particular ordinary |
||||
<productname>PostgreSQL</productname> types. The SQL parser does |
||||
not have direct knowledge of the internal types that constitute a |
||||
<type>jsonb</>. |
||||
</para> |
||||
<para> |
||||
The following are all valid <type>jsonb</> expressions: |
||||
<programlisting> |
||||
-- Simple scalar/primitive value (explicitly required by RFC-7159) |
||||
SELECT '5'::jsonb; |
||||
|
||||
-- Array of heterogeneous, primitive-typed elements |
||||
SELECT '[1, 2, "foo", null]'::jsonb; |
||||
|
||||
-- Object of heterogeneous key/value pairs of primitive types |
||||
-- Note that key values are always strings |
||||
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb; |
||||
</programlisting> |
||||
</para> |
||||
<para> |
||||
Note the distinction between scalar/primitive values as elements, |
||||
keys and values. |
||||
</para> |
||||
</sect2> |
||||
<sect2 id="json-containment"> |
||||
<title><type>jsonb</> containment</title> |
||||
<indexterm> |
||||
<primary>jsonb</primary> |
||||
<secondary>containment</secondary> |
||||
</indexterm> |
||||
<para> |
||||
Testing <quote>containment</> is an important capability of |
||||
<type>jsonb</>. There is no parallel set of facilities for the |
||||
<type>json</> type. Containment is the ability to determine if |
||||
one <type>jsonb</> document has contained within it another one. |
||||
<type>jsonb</> is nested, and so containment semantics are nested; |
||||
technically, top-down, unordered <emphasis>subtree isomorphism</> |
||||
may be tested. Containment is conventionally tested using the |
||||
<literal>@></> operator, which is made indexable by various |
||||
operator classes discussed later in this section. |
||||
</para> |
||||
<programlisting> |
||||
-- Simple scalar/primitive values may contain only each other: |
||||
SELECT '"foo"'::jsonb @> '"foo"'::jsonb; |
||||
|
||||
-- The array on the right hand side is contained within the one on the |
||||
-- left hand side: |
||||
SELECT '[1, 2, 3]'::jsonb @> '[1, 3]'::jsonb; |
||||
|
||||
-- The object with a single pair on the right hand side is contained |
||||
-- within the object on the left hand side: |
||||
SELECT '{"product": "PostgreSQL", "version": 9.4, "jsonb":true}'::jsonb @> '{"version":9.4}'::jsonb; |
||||
|
||||
-- The array on the right hand side is not contained within the array |
||||
-- containing a nested array on the left hand side: |
||||
SELECT '[1, 2, [1, 3]]'::jsonb @> '[1, 3]'::jsonb; |
||||
|
||||
-- But with a layer of nesting, it is: |
||||
SELECT '[1, 2, [1, 3]]'::jsonb @> '[[1, 3]]'::jsonb; |
||||
</programlisting> |
||||
<para> |
||||
It is both a sufficient and a necessary condition for nesting |
||||
levels to <quote>line up</> for one <type>jsonb</> to contain |
||||
within it another. Under this definition, objects and arrays |
||||
cannot <quote>line up</>, not least because objects contain |
||||
key/value pairs, while arrays contain elements. |
||||
</para> |
||||
<para> |
||||
As a special exception to the general principle that nesting |
||||
levels should <quote>line up</>, an array may contain a raw scalar: |
||||
</para> |
||||
<programlisting> |
||||
-- This array contains the raw scalar value: |
||||
SELECT '["foo", "bar"]'::jsonb @> '"bar"'::jsonb; |
||||
-- The special exception is not reciprocated -- non-containment is indicated here: |
||||
SELECT '"bar"'::jsonb @> '["bar"]'::jsonb; |
||||
</programlisting> |
||||
<para> |
||||
Objects are better suited for testing containment when there is a |
||||
great deal of nesting involved, because unlike arrays they are |
||||
internally optimized for searching, and do not need to be searched |
||||
linearly within a single <type>jsonb</> document. |
||||
</para> |
||||
<programlisting> |
||||
-- The right-hand side object is contained in this example: |
||||
SELECT '{"p":1, "a":{"b":3, "q":11}, "i":77}'::jsonb @> '{"a":{"b":3}}'::jsonb; |
||||
</programlisting> |
||||
<para> |
||||
The various containment operators, along with all other JSON |
||||
operators and support functions are documented fully within <xref |
||||
linkend="functions-json">, <xref |
||||
linkend="functions-jsonb-op-table">. |
||||
</para> |
||||
</sect2> |
||||
<sect2 id="json-indexing"> |
||||
<title><type>jsonb</> GIN Indexing</title> |
||||
<indexterm> |
||||
<primary>jsonb</primary> |
||||
<secondary>indexes on</secondary> |
||||
</indexterm> |
||||
<para> |
||||
<type>jsonb</> GIN indexes can be used to efficiently search among |
||||
more than one possible key/value pair within a single |
||||
<type>jsonb</> datum/document, among a large number of such |
||||
documents within a column in a table (i.e. among many rows). |
||||
</para> |
||||
<para> |
||||
<type>jsonb</> has GIN index support for the <literal>@></>, |
||||
<literal>?</>, <literal>?&</> and <literal>?|</> operators. |
||||
The default GIN operator class makes all these operators |
||||
indexable: |
||||
</para> |
||||
<programlisting> |
||||
-- GIN index (default opclass) |
||||
CREATE INDEX idxgin ON api USING GIN (jdoc); |
||||
|
||||
-- GIN jsonb_hash_ops index |
||||
CREATE INDEX idxginh ON api USING GIN (jdoc jsonb_hash_ops); |
||||
</programlisting> |
||||
<para> |
||||
The non-default GIN operator class <literal>jsonb_hash_ops</> |
||||
supports indexing the <literal>@></> operator only. |
||||
</para> |
||||
<para> |
||||
Consider the example of a table that stores JSON documents |
||||
retrieved from a third-party web service, with a documented schema |
||||
definition. An example of a document retrieved from this web |
||||
service is as follows: |
||||
<programlisting> |
||||
{ |
||||
"guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a", |
||||
"name": "Angela Barton", |
||||
"is_active": true, |
||||
"company": "Magnafone", |
||||
"address": "178 Howard Place, Gulf, Washington, 702", |
||||
"registered": "2009-11-07T08:53:22 +08:00", |
||||
"latitude": 19.793713, |
||||
"longitude": 86.513373, |
||||
"tags": [ |
||||
"enim", |
||||
"aliquip", |
||||
"qui" |
||||
] |
||||
} |
||||
</programlisting> |
||||
If a GIN index is created on the table that stores these |
||||
documents, <literal>api</literal>, on its <literal>jdoc</> |
||||
<type>jsonb</> column, we can expect that queries like the |
||||
following may make use of the index: |
||||
<programlisting> |
||||
-- Note that both key and value have been specified |
||||
SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"company": "Magnafone"}'; |
||||
</programlisting> |
||||
However, the index could not be used for queries like the |
||||
following, due to the aforementioned nesting restriction: |
||||
<programlisting> |
||||
SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc -> 'tags' ? 'qui'; |
||||
</programlisting> |
||||
Still, with judicious use of expressional indexing, the above |
||||
query can use an index scan. If there is a requirement to find |
||||
those records with a particular tag quickly, and the tags have a |
||||
high cardinality across all documents, defining an index as |
||||
follows is an effective approach to indexing: |
||||
<programlisting> |
||||
-- Note that the "jsonb -> text" operator can only be called on an |
||||
-- object, so as a consequence of creating this index the root "jdoc" |
||||
-- datum must be an object. This is enforced during insertion. |
||||
CREATE INDEX idxgin ON api USING GIN ((jdoc -> 'tags')); |
||||
</programlisting> |
||||
</para> |
||||
<para> |
||||
Expressional indexes are discussed in <xref |
||||
linkend="indexes-expressional">. |
||||
</para> |
||||
<para> |
||||
For the most flexible approach in terms of what may be indexed, |
||||
sophisticated querying on nested structures is possible by |
||||
exploiting containment. At the cost of having to create an index |
||||
on the entire structure for each row, and not just a nested |
||||
subset, we may exploit containment semantics to get an equivalent |
||||
result with a non-expressional index on the entire <quote>jdoc</> |
||||
column, <emphasis>without</> ever having to create additional |
||||
expressional indexes against the document (provided only |
||||
containment will be tested). While the index will be considerably |
||||
larger than our expression index, it will also be much more |
||||
flexible, allowing arbitrary structured searching. Such an index |
||||
can generally be expected to help with a query like the following: |
||||
</para> |
||||
<programlisting> |
||||
SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"tags": ["qui"]}'; |
||||
</programlisting> |
||||
<para> |
||||
For full details of the semantics that these indexable operators |
||||
implement, see <xref linkend="functions-json">, <xref |
||||
linkend="functions-jsonb-op-table">. |
||||
</para> |
||||
</sect2> |
||||
<sect2 id="json-opclass"> |
||||
<title><type>jsonb</> non-default GIN operator class</title> |
||||
<indexterm> |
||||
<primary>jsonb</primary> |
||||
<secondary>indexes on</secondary> |
||||
</indexterm> |
||||
<para> |
||||
Although only the <literal>@></> operator is made indexable, a |
||||
<literal>jsonb_hash_ops</literal> operator class GIN index has |
||||
some notable advantages over an equivalent GIN index of the |
||||
default GIN operator class for <type>jsonb</type>. Search |
||||
operations typically perform considerably better, and the on-disk |
||||
size of a <literal>jsonb_hash_ops</literal> operator class GIN |
||||
index can be much smaller. |
||||
</para> |
||||
</sect2> |
||||
<sect2 id="json-btree-indexing"> |
||||
<title><type>jsonb</> B-Tree and hash indexing</title> |
||||
<para> |
||||
<type>jsonb</type> comparisons and related operations are |
||||
<emphasis>type-wise</>, in that the underlying |
||||
<productname>PostgreSQL</productname> datatype comparators are |
||||
invoked recursively, much like a traditional composite type. |
||||
</para> |
||||
<para> |
||||
<type>jsonb</> also supports <type>btree</> and <type>hash</> |
||||
indexes. Ordering between <type>jsonb</> datums is: |
||||
<synopsis> |
||||
<replaceable>Object</replaceable> > <replaceable>Array</replaceable> > <replaceable>Boolean</replaceable> > <replaceable>Number</replaceable> > <replaceable>String</replaceable> > <replaceable>Null</replaceable> |
||||
|
||||
<replaceable>Object with n pairs</replaceable> > <replaceable>object with n - 1 pairs</replaceable> |
||||
|
||||
<replaceable>Array with n elements</replaceable> > <replaceable>array with n - 1 elements</replaceable> |
||||
</synopsis> |
||||
Subsequently, individual primitive type comparators are invoked. |
||||
All comparisons of JSON primitive types occurs using the same |
||||
comparison rules as the underlying |
||||
<productname>PostgreSQL</productname> types. Strings are |
||||
compared lexically, using the default database collation. |
||||
Objects with equal numbers of pairs are compared: |
||||
<synopsis> |
||||
<replaceable>key-1</replaceable>, <replaceable>value-1</replaceable>, <replaceable>key-2</replaceable> ... |
||||
</synopsis> |
||||
Note however that object keys are compared in their storage order, and in particular, |
||||
since shorter keys are stored before longer keys, this can lead to results that might be |
||||
unintuitive, such as: |
||||
<programlisting>{ "aa": 1, "c": 1} > {"b": 1, "d": 1}</programlisting> |
||||
Similarly, arrays with equal numbers of elements are compared: |
||||
<synopsis> |
||||
<replaceable>element-1</replaceable>, <replaceable>element-2</replaceable> ... |
||||
</synopsis> |
||||
</para> |
||||
</sect2> |
||||
</sect1> |
||||
@ -0,0 +1,468 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* jsonb.c |
||||
* I/O routines for jsonb type |
||||
* |
||||
* Copyright (c) 2014, PostgreSQL Global Development Group |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/utils/adt/jsonb.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "libpq/pqformat.h" |
||||
#include "utils/builtins.h" |
||||
#include "utils/json.h" |
||||
#include "utils/jsonapi.h" |
||||
#include "utils/jsonb.h" |
||||
|
||||
typedef struct JsonbInState |
||||
{ |
||||
JsonbParseState *parseState; |
||||
JsonbValue *res; |
||||
} JsonbInState; |
||||
|
||||
static inline Datum jsonb_from_cstring(char *json, int len); |
||||
static size_t checkStringLen(size_t len); |
||||
static void jsonb_in_object_start(void *pstate); |
||||
static void jsonb_in_object_end(void *pstate); |
||||
static void jsonb_in_array_start(void *pstate); |
||||
static void jsonb_in_array_end(void *pstate); |
||||
static void jsonb_in_object_field_start(void *pstate, char *fname, bool isnull); |
||||
static void jsonb_put_escaped_value(StringInfo out, JsonbValue * scalarVal); |
||||
static void jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype); |
||||
char *JsonbToCString(StringInfo out, char *in, int estimated_len); |
||||
|
||||
/*
|
||||
* jsonb type input function |
||||
*/ |
||||
Datum |
||||
jsonb_in(PG_FUNCTION_ARGS) |
||||
{ |
||||
char *json = PG_GETARG_CSTRING(0); |
||||
|
||||
return jsonb_from_cstring(json, strlen(json)); |
||||
} |
||||
|
||||
/*
|
||||
* jsonb type recv function |
||||
* |
||||
* The type is sent as text in binary mode, so this is almost the same |
||||
* as the input function, but it's prefixed with a version number so we |
||||
* can change the binary format sent in future if necessary. For now, |
||||
* only version 1 is supported. |
||||
*/ |
||||
Datum |
||||
jsonb_recv(PG_FUNCTION_ARGS) |
||||
{ |
||||
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); |
||||
int version = pq_getmsgint(buf, 1); |
||||
char *str; |
||||
int nbytes; |
||||
|
||||
if (version == 1) |
||||
str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); |
||||
else |
||||
elog(ERROR, "Unsupported jsonb version number %d", version); |
||||
|
||||
return jsonb_from_cstring(str, nbytes); |
||||
} |
||||
|
||||
/*
|
||||
* jsonb type output function |
||||
*/ |
||||
Datum |
||||
jsonb_out(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = PG_GETARG_JSONB(0); |
||||
char *out; |
||||
|
||||
out = JsonbToCString(NULL, VARDATA(jb), VARSIZE(jb)); |
||||
|
||||
PG_RETURN_CSTRING(out); |
||||
} |
||||
|
||||
/*
|
||||
* jsonb type send function |
||||
* |
||||
* Just send jsonb as a version number, then a string of text |
||||
*/ |
||||
Datum |
||||
jsonb_send(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = PG_GETARG_JSONB(0); |
||||
StringInfoData buf; |
||||
StringInfo jtext = makeStringInfo(); |
||||
int version = 1; |
||||
|
||||
(void) JsonbToCString(jtext, VARDATA(jb), VARSIZE(jb)); |
||||
|
||||
pq_begintypsend(&buf); |
||||
pq_sendint(&buf, version, 1); |
||||
pq_sendtext(&buf, jtext->data, jtext->len); |
||||
pfree(jtext->data); |
||||
pfree(jtext); |
||||
|
||||
PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); |
||||
} |
||||
|
||||
/*
|
||||
* SQL function jsonb_typeof(jsonb) -> text |
||||
* |
||||
* This function is here because the analog json function is in json.c, since |
||||
* it uses the json parser internals not exposed elsewhere. |
||||
*/ |
||||
Datum |
||||
jsonb_typeof(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *in = PG_GETARG_JSONB(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(VARDATA_ANY(in)); |
||||
|
||||
/*
|
||||
* 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)); |
||||
} |
||||
|
||||
/*
|
||||
* jsonb_from_cstring |
||||
* |
||||
* Turns json string into a jsonb Datum. |
||||
* |
||||
* Uses the json parser (with hooks) to construct a jsonb. |
||||
*/ |
||||
static inline Datum |
||||
jsonb_from_cstring(char *json, int len) |
||||
{ |
||||
JsonLexContext *lex; |
||||
JsonbInState state; |
||||
JsonSemAction sem; |
||||
|
||||
memset(&state, 0, sizeof(state)); |
||||
memset(&sem, 0, sizeof(sem)); |
||||
lex = makeJsonLexContextCstringLen(json, len, true); |
||||
|
||||
sem.semstate = (void *) &state; |
||||
|
||||
sem.object_start = jsonb_in_object_start; |
||||
sem.array_start = jsonb_in_array_start; |
||||
sem.object_end = jsonb_in_object_end; |
||||
sem.array_end = jsonb_in_array_end; |
||||
sem.scalar = jsonb_in_scalar; |
||||
sem.object_field_start = jsonb_in_object_field_start; |
||||
|
||||
pg_parse_json(lex, &sem); |
||||
|
||||
/* after parsing, the item member has the composed jsonb structure */ |
||||
PG_RETURN_POINTER(JsonbValueToJsonb(state.res)); |
||||
} |
||||
|
||||
static size_t |
||||
checkStringLen(size_t len) |
||||
{ |
||||
if (len > JENTRY_POSMASK) |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
||||
errmsg("string too long to represent as jsonb string"), |
||||
errdetail("Due to an implementation restriction, jsonb strings cannot exceed %d bytes.", |
||||
JENTRY_POSMASK))); |
||||
|
||||
return len; |
||||
} |
||||
|
||||
static void |
||||
jsonb_in_object_start(void *pstate) |
||||
{ |
||||
JsonbInState *_state = (JsonbInState *) pstate; |
||||
|
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_BEGIN_OBJECT, NULL); |
||||
} |
||||
|
||||
static void |
||||
jsonb_in_object_end(void *pstate) |
||||
{ |
||||
JsonbInState *_state = (JsonbInState *) pstate; |
||||
|
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_END_OBJECT, NULL); |
||||
} |
||||
|
||||
static void |
||||
jsonb_in_array_start(void *pstate) |
||||
{ |
||||
JsonbInState *_state = (JsonbInState *) pstate; |
||||
|
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_BEGIN_ARRAY, NULL); |
||||
} |
||||
|
||||
static void |
||||
jsonb_in_array_end(void *pstate) |
||||
{ |
||||
JsonbInState *_state = (JsonbInState *) pstate; |
||||
|
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_END_ARRAY, NULL); |
||||
} |
||||
|
||||
static void |
||||
jsonb_in_object_field_start(void *pstate, char *fname, bool isnull) |
||||
{ |
||||
JsonbInState *_state = (JsonbInState *) pstate; |
||||
JsonbValue v; |
||||
|
||||
Assert (fname != NULL); |
||||
v.type = jbvString; |
||||
v.string.len = checkStringLen(strlen(fname)); |
||||
v.string.val = pnstrdup(fname, v.string.len); |
||||
v.estSize = sizeof(JEntry) + v.string.len; |
||||
|
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_KEY, &v); |
||||
} |
||||
|
||||
static void |
||||
jsonb_put_escaped_value(StringInfo out, JsonbValue * scalarVal) |
||||
{ |
||||
switch (scalarVal->type) |
||||
{ |
||||
case jbvNull: |
||||
appendBinaryStringInfo(out, "null", 4); |
||||
break; |
||||
case jbvString: |
||||
escape_json(out, pnstrdup(scalarVal->string.val, scalarVal->string.len)); |
||||
break; |
||||
case jbvNumeric: |
||||
appendStringInfoString(out, |
||||
DatumGetCString(DirectFunctionCall1(numeric_out, |
||||
PointerGetDatum(scalarVal->numeric)))); |
||||
break; |
||||
case jbvBool: |
||||
if (scalarVal->boolean) |
||||
appendBinaryStringInfo(out, "true", 4); |
||||
else |
||||
appendBinaryStringInfo(out, "false", 5); |
||||
break; |
||||
default: |
||||
elog(ERROR, "unknown jsonb scalar type"); |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
* For jsonb we always want the de-escaped value - that's what's in token |
||||
*/ |
||||
static void |
||||
jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype) |
||||
{ |
||||
JsonbInState *_state = (JsonbInState *) pstate; |
||||
JsonbValue v; |
||||
|
||||
v.estSize = sizeof(JEntry); |
||||
|
||||
switch (tokentype) |
||||
{ |
||||
|
||||
case JSON_TOKEN_STRING: |
||||
Assert (token != NULL); |
||||
v.type = jbvString; |
||||
v.string.len = checkStringLen(strlen(token)); |
||||
v.string.val = pnstrdup(token, v.string.len); |
||||
v.estSize += v.string.len; |
||||
break; |
||||
case JSON_TOKEN_NUMBER: |
||||
/*
|
||||
* No need to check size of numeric values, because maximum numeric |
||||
* size is well below the JsonbValue restriction |
||||
*/ |
||||
Assert (token != NULL); |
||||
v.type = jbvNumeric; |
||||
v.numeric = DatumGetNumeric(DirectFunctionCall3(numeric_in, CStringGetDatum(token), 0, -1)); |
||||
v.estSize += VARSIZE_ANY(v.numeric) + sizeof(JEntry) /* alignment */ ; |
||||
break; |
||||
case JSON_TOKEN_TRUE: |
||||
v.type = jbvBool; |
||||
v.boolean = true; |
||||
break; |
||||
case JSON_TOKEN_FALSE: |
||||
v.type = jbvBool; |
||||
v.boolean = false; |
||||
break; |
||||
case JSON_TOKEN_NULL: |
||||
v.type = jbvNull; |
||||
break; |
||||
default: |
||||
/* should not be possible */ |
||||
elog(ERROR, "invalid json token type"); |
||||
break; |
||||
} |
||||
|
||||
if (_state->parseState == NULL) |
||||
{ |
||||
/* single scalar */ |
||||
JsonbValue va; |
||||
|
||||
va.type = jbvArray; |
||||
va.array.rawScalar = true; |
||||
va.array.nElems = 1; |
||||
|
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_BEGIN_ARRAY, &va); |
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_ELEM, &v); |
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_END_ARRAY, NULL); |
||||
} |
||||
else |
||||
{ |
||||
JsonbValue *o = &_state->parseState->contVal; |
||||
|
||||
switch (o->type) |
||||
{ |
||||
case jbvArray: |
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_ELEM, &v); |
||||
break; |
||||
case jbvObject: |
||||
_state->res = pushJsonbValue(&_state->parseState, WJB_VALUE, &v); |
||||
break; |
||||
default: |
||||
elog(ERROR, "unexpected parent of nested structure"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
* JsonbToCString |
||||
* Converts jsonb value to a C-string. |
||||
* |
||||
* If 'out' argument is non-null, the resulting C-string is stored inside the |
||||
* StringBuffer. The resulting string is always returned. |
||||
* |
||||
* A typical case for passing the StringInfo in rather than NULL is where the |
||||
* caller wants access to the len attribute without having to call strlen, e.g. |
||||
* if they are converting it to a text* object. |
||||
*/ |
||||
char * |
||||
JsonbToCString(StringInfo out, JsonbSuperHeader in, int estimated_len) |
||||
{ |
||||
bool first = true; |
||||
JsonbIterator *it; |
||||
int type = 0; |
||||
JsonbValue v; |
||||
int level = 0; |
||||
bool redo_switch = false; |
||||
|
||||
if (out == NULL) |
||||
out = makeStringInfo(); |
||||
|
||||
enlargeStringInfo(out, (estimated_len >= 0) ? estimated_len : 64); |
||||
|
||||
it = JsonbIteratorInit(in); |
||||
|
||||
while (redo_switch || |
||||
((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)) |
||||
{ |
||||
redo_switch = false; |
||||
switch (type) |
||||
{ |
||||
case WJB_BEGIN_ARRAY: |
||||
if (!first) |
||||
appendBinaryStringInfo(out, ", ", 2); |
||||
first = true; |
||||
|
||||
if (!v.array.rawScalar) |
||||
appendStringInfoChar(out, '['); |
||||
level++; |
||||
break; |
||||
case WJB_BEGIN_OBJECT: |
||||
if (!first) |
||||
appendBinaryStringInfo(out, ", ", 2); |
||||
first = true; |
||||
appendStringInfoCharMacro(out, '{'); |
||||
|
||||
level++; |
||||
break; |
||||
case WJB_KEY: |
||||
if (!first) |
||||
appendBinaryStringInfo(out, ", ", 2); |
||||
first = true; |
||||
|
||||
/* json rules guarantee this is a string */ |
||||
jsonb_put_escaped_value(out, &v); |
||||
appendBinaryStringInfo(out, ": ", 2); |
||||
|
||||
type = JsonbIteratorNext(&it, &v, false); |
||||
if (type == WJB_VALUE) |
||||
{ |
||||
first = false; |
||||
jsonb_put_escaped_value(out, &v); |
||||
} |
||||
else |
||||
{ |
||||
Assert(type == WJB_BEGIN_OBJECT || type == WJB_BEGIN_ARRAY); |
||||
|
||||
/*
|
||||
* We need to rerun the current switch() since we need to |
||||
* output the object which we just got from the iterator |
||||
* before calling the iterator again. |
||||
*/ |
||||
redo_switch = true; |
||||
} |
||||
break; |
||||
case WJB_ELEM: |
||||
if (!first) |
||||
appendBinaryStringInfo(out, ", ", 2); |
||||
else |
||||
first = false; |
||||
|
||||
jsonb_put_escaped_value(out, &v); |
||||
break; |
||||
case WJB_END_ARRAY: |
||||
level--; |
||||
if (!v.array.rawScalar) |
||||
appendStringInfoChar(out, ']'); |
||||
first = false; |
||||
break; |
||||
case WJB_END_OBJECT: |
||||
level--; |
||||
appendStringInfoCharMacro(out, '}'); |
||||
first = false; |
||||
break; |
||||
default: |
||||
elog(ERROR, "unknown flag of jsonb iterator"); |
||||
} |
||||
} |
||||
|
||||
Assert(level == 0); |
||||
|
||||
return out->data; |
||||
} |
||||
@ -0,0 +1,646 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* jsonb_gin.c |
||||
* GIN support functions for jsonb |
||||
* |
||||
* Copyright (c) 2014, PostgreSQL Global Development Group |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/utils/adt/jsonb_gin.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "access/gin.h" |
||||
#include "access/skey.h" |
||||
#include "catalog/pg_collation.h" |
||||
#include "catalog/pg_type.h" |
||||
#include "utils/builtins.h" |
||||
#include "utils/jsonb.h" |
||||
|
||||
typedef struct PathHashStack |
||||
{ |
||||
uint32 hash; |
||||
struct PathHashStack *parent; |
||||
} PathHashStack; |
||||
|
||||
static text *make_text_key(const char *str, int len, char flag); |
||||
static text *make_scalar_key(const JsonbValue * scalarVal, char flag); |
||||
|
||||
/*
|
||||
* |
||||
* jsonb_ops GIN opclass support functions |
||||
* |
||||
*/ |
||||
Datum |
||||
gin_compare_jsonb(PG_FUNCTION_ARGS) |
||||
{ |
||||
text *arg1 = PG_GETARG_TEXT_PP(0); |
||||
text *arg2 = PG_GETARG_TEXT_PP(1); |
||||
int32 result; |
||||
char *a1p, |
||||
*a2p; |
||||
int len1, |
||||
len2; |
||||
|
||||
a1p = VARDATA_ANY(arg1); |
||||
a2p = VARDATA_ANY(arg2); |
||||
|
||||
len1 = VARSIZE_ANY_EXHDR(arg1); |
||||
len2 = VARSIZE_ANY_EXHDR(arg2); |
||||
|
||||
/* Compare text as bttextcmp does, but always using C collation */ |
||||
result = varstr_cmp(a1p, len1, a2p, len2, C_COLLATION_OID); |
||||
|
||||
PG_FREE_IF_COPY(arg1, 0); |
||||
PG_FREE_IF_COPY(arg2, 1); |
||||
|
||||
PG_RETURN_INT32(result); |
||||
} |
||||
|
||||
Datum |
||||
gin_extract_jsonb(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = (Jsonb *) PG_GETARG_JSONB(0); |
||||
int32 *nentries = (int32 *) PG_GETARG_POINTER(1); |
||||
Datum *entries = NULL; |
||||
int total = 2 * JB_ROOT_COUNT(jb); |
||||
int i = 0, |
||||
r; |
||||
JsonbIterator *it; |
||||
JsonbValue v; |
||||
|
||||
if (total == 0) |
||||
{ |
||||
*nentries = 0; |
||||
PG_RETURN_POINTER(NULL); |
||||
} |
||||
|
||||
entries = (Datum *) palloc(sizeof(Datum) * total); |
||||
|
||||
it = JsonbIteratorInit(VARDATA(jb)); |
||||
|
||||
while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) |
||||
{ |
||||
if (i >= total) |
||||
{ |
||||
total *= 2; |
||||
entries = (Datum *) repalloc(entries, sizeof(Datum) * total); |
||||
} |
||||
|
||||
/*
|
||||
* Serialize keys and elements equivalently, but only when elements |
||||
* are Jsonb strings. Otherwise, serialize elements as values. Array |
||||
* elements are indexed as keys, for the benefit of |
||||
* JsonbExistsStrategyNumber. Our definition of existence does not |
||||
* allow for checking the existence of a non-jbvString element (just |
||||
* like the definition of the underlying operator), because the |
||||
* operator takes a text rhs argument (which is taken as a proxy for an |
||||
* equivalent Jsonb string). |
||||
* |
||||
* The way existence is represented does not preclude an alternative |
||||
* existence operator, that takes as its rhs value an arbitrarily |
||||
* internally-typed Jsonb. The only reason that isn't the case here is |
||||
* that the existence operator is only really intended to determine if |
||||
* an object has a certain key (object pair keys are of course |
||||
* invariably strings), which is extended to jsonb arrays. You could |
||||
* think of the default Jsonb definition of existence as being |
||||
* equivalent to a definition where all types of scalar array elements |
||||
* are keys that we can check the existence of, while just forbidding |
||||
* non-string notation. This inflexibility prevents the user from |
||||
* having to qualify that the rhs string is a raw scalar string (that |
||||
* is, naturally no internal string quoting in required for the text |
||||
* argument), and allows us to not set the reset flag for |
||||
* JsonbExistsStrategyNumber, since we know that keys are strings for |
||||
* both objects and arrays, and don't have to further account for type |
||||
* mismatch. Not having to set the reset flag makes it less than |
||||
* tempting to tighten up the definition of existence to preclude array |
||||
* elements entirely, which would arguably be a simpler alternative. |
||||
* In any case the infrastructure used to implement the existence |
||||
* operator could trivially support this hypothetical, slightly |
||||
* distinct definition of existence. |
||||
*/ |
||||
switch (r) |
||||
{ |
||||
case WJB_KEY: |
||||
/* Serialize key separately, for existence strategies */ |
||||
entries[i++] = PointerGetDatum(make_scalar_key(&v, JKEYELEM)); |
||||
break; |
||||
case WJB_ELEM: |
||||
if (v.type == jbvString) |
||||
entries[i++] = PointerGetDatum(make_scalar_key(&v, JKEYELEM)); |
||||
else |
||||
entries[i++] = PointerGetDatum(make_scalar_key(&v, JVAL)); |
||||
break; |
||||
case WJB_VALUE: |
||||
entries[i++] = PointerGetDatum(make_scalar_key(&v, JVAL)); |
||||
break; |
||||
default: |
||||
continue; |
||||
} |
||||
} |
||||
|
||||
*nentries = i; |
||||
|
||||
PG_RETURN_POINTER(entries); |
||||
} |
||||
|
||||
Datum |
||||
gin_extract_jsonb_query(PG_FUNCTION_ARGS) |
||||
{ |
||||
int32 *nentries = (int32 *) PG_GETARG_POINTER(1); |
||||
StrategyNumber strategy = PG_GETARG_UINT16(2); |
||||
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6); |
||||
Datum *entries; |
||||
|
||||
if (strategy == JsonbContainsStrategyNumber) |
||||
{ |
||||
/* Query is a jsonb, so just apply gin_extract_jsonb... */ |
||||
entries = (Datum *) |
||||
DatumGetPointer(DirectFunctionCall2(gin_extract_jsonb, |
||||
PG_GETARG_DATUM(0), |
||||
PointerGetDatum(nentries))); |
||||
/* ...although "contains {}" requires a full index scan */ |
||||
if (entries == NULL) |
||||
*searchMode = GIN_SEARCH_MODE_ALL; |
||||
} |
||||
else if (strategy == JsonbExistsStrategyNumber) |
||||
{ |
||||
text *query = PG_GETARG_TEXT_PP(0); |
||||
text *item; |
||||
|
||||
*nentries = 1; |
||||
entries = (Datum *) palloc(sizeof(Datum)); |
||||
item = make_text_key(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query), |
||||
JKEYELEM); |
||||
entries[0] = PointerGetDatum(item); |
||||
} |
||||
else if (strategy == JsonbExistsAnyStrategyNumber || |
||||
strategy == JsonbExistsAllStrategyNumber) |
||||
{ |
||||
ArrayType *query = PG_GETARG_ARRAYTYPE_P(0); |
||||
Datum *key_datums; |
||||
bool *key_nulls; |
||||
int key_count; |
||||
int i, |
||||
j; |
||||
text *item; |
||||
|
||||
deconstruct_array(query, |
||||
TEXTOID, -1, false, 'i', |
||||
&key_datums, &key_nulls, &key_count); |
||||
|
||||
entries = (Datum *) palloc(sizeof(Datum) * key_count); |
||||
|
||||
for (i = 0, j = 0; i < key_count; ++i) |
||||
{ |
||||
/* Nulls in the array are ignored */ |
||||
if (key_nulls[i]) |
||||
continue; |
||||
item = make_text_key(VARDATA(key_datums[i]), |
||||
VARSIZE(key_datums[i]) - VARHDRSZ, |
||||
JKEYELEM); |
||||
entries[j++] = PointerGetDatum(item); |
||||
} |
||||
|
||||
*nentries = j; |
||||
/* ExistsAll with no keys should match everything */ |
||||
if (j == 0 && strategy == JsonbExistsAllStrategyNumber) |
||||
*searchMode = GIN_SEARCH_MODE_ALL; |
||||
} |
||||
else |
||||
{ |
||||
elog(ERROR, "unrecognized strategy number: %d", strategy); |
||||
entries = NULL; /* keep compiler quiet */ |
||||
} |
||||
|
||||
PG_RETURN_POINTER(entries); |
||||
} |
||||
|
||||
Datum |
||||
gin_consistent_jsonb(PG_FUNCTION_ARGS) |
||||
{ |
||||
bool *check = (bool *) PG_GETARG_POINTER(0); |
||||
StrategyNumber strategy = PG_GETARG_UINT16(1); |
||||
|
||||
/* Jsonb *query = PG_GETARG_JSONB(2); */ |
||||
int32 nkeys = PG_GETARG_INT32(3); |
||||
|
||||
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */ |
||||
bool *recheck = (bool *) PG_GETARG_POINTER(5); |
||||
bool res = true; |
||||
int32 i; |
||||
|
||||
if (strategy == JsonbContainsStrategyNumber) |
||||
{ |
||||
/*
|
||||
* Index doesn't have information about correspondence of Jsonb keys |
||||
* and values (as distinct from GIN keys, which a key/value pair is |
||||
* stored as), so invariably we recheck. Besides, there are some |
||||
* special rules around the containment of raw scalar arrays and |
||||
* regular arrays that are not represented here. However, if all of |
||||
* the keys are not present, that's sufficient reason to return false |
||||
* and finish immediately. |
||||
*/ |
||||
*recheck = true; |
||||
for (i = 0; i < nkeys; i++) |
||||
{ |
||||
if (!check[i]) |
||||
{ |
||||
res = false; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
else if (strategy == JsonbExistsStrategyNumber) |
||||
{ |
||||
/* Existence of key guaranteed in default search mode */ |
||||
*recheck = false; |
||||
res = true; |
||||
} |
||||
else if (strategy == JsonbExistsAnyStrategyNumber) |
||||
{ |
||||
/* Existence of key guaranteed in default search mode */ |
||||
*recheck = false; |
||||
res = true; |
||||
} |
||||
else if (strategy == JsonbExistsAllStrategyNumber) |
||||
{ |
||||
/* Testing for the presence of all keys gives an exact result */ |
||||
*recheck = false; |
||||
for (i = 0; i < nkeys; i++) |
||||
{ |
||||
if (!check[i]) |
||||
{ |
||||
res = false; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
else |
||||
elog(ERROR, "unrecognized strategy number: %d", strategy); |
||||
|
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
Datum |
||||
gin_triconsistent_jsonb(PG_FUNCTION_ARGS) |
||||
{ |
||||
GinLogicValue *check = (GinLogicValue *) PG_GETARG_POINTER(0); |
||||
StrategyNumber strategy = PG_GETARG_UINT16(1); |
||||
/* Jsonb *query = PG_GETARG_JSONB(2); */ |
||||
int32 nkeys = PG_GETARG_INT32(3); |
||||
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */ |
||||
GinLogicValue res = GIN_TRUE; |
||||
|
||||
int32 i; |
||||
|
||||
if (strategy == JsonbContainsStrategyNumber) |
||||
{ |
||||
bool has_maybe = false; |
||||
|
||||
/*
|
||||
* All extracted keys must be present. Combination of GIN_MAYBE and |
||||
* GIN_TRUE gives GIN_MAYBE result because then all keys may be |
||||
* present. |
||||
*/ |
||||
for (i = 0; i < nkeys; i++) |
||||
{ |
||||
if (check[i] == GIN_FALSE) |
||||
{ |
||||
res = GIN_FALSE; |
||||
break; |
||||
} |
||||
if (check[i] == GIN_MAYBE) |
||||
{ |
||||
res = GIN_MAYBE; |
||||
has_maybe = true; |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
* Index doesn't have information about correspondence of Jsonb keys |
||||
* and values (as distinct from GIN keys, which a key/value pair is |
||||
* stored as), so invariably we recheck. This is also reflected in how |
||||
* GIN_MAYBE is given in response to there being no GIN_MAYBE input. |
||||
*/ |
||||
if (!has_maybe && res == GIN_TRUE) |
||||
res = GIN_MAYBE; |
||||
} |
||||
else if (strategy == JsonbExistsStrategyNumber || |
||||
strategy == JsonbExistsAnyStrategyNumber) |
||||
{ |
||||
/* Existence of key guaranteed in default search mode */ |
||||
res = GIN_FALSE; |
||||
for (i = 0; i < nkeys; i++) |
||||
{ |
||||
if (check[i] == GIN_TRUE) |
||||
{ |
||||
res = GIN_TRUE; |
||||
break; |
||||
} |
||||
if (check[i] == GIN_MAYBE) |
||||
{ |
||||
res = GIN_MAYBE; |
||||
} |
||||
} |
||||
} |
||||
else if (strategy == JsonbExistsAllStrategyNumber) |
||||
{ |
||||
/* Testing for the presence of all keys gives an exact result */ |
||||
for (i = 0; i < nkeys; i++) |
||||
{ |
||||
if (check[i] == GIN_FALSE) |
||||
{ |
||||
res = GIN_FALSE; |
||||
break; |
||||
} |
||||
if (check[i] == GIN_MAYBE) |
||||
{ |
||||
res = GIN_MAYBE; |
||||
} |
||||
} |
||||
} |
||||
else |
||||
elog(ERROR, "unrecognized strategy number: %d", strategy); |
||||
|
||||
PG_RETURN_GIN_LOGIC_VALUE(res); |
||||
} |
||||
|
||||
/*
|
||||
* |
||||
* jsonb_hash_ops GIN opclass support functions |
||||
* |
||||
*/ |
||||
Datum |
||||
gin_consistent_jsonb_hash(PG_FUNCTION_ARGS) |
||||
{ |
||||
bool *check = (bool *) PG_GETARG_POINTER(0); |
||||
StrategyNumber strategy = PG_GETARG_UINT16(1); |
||||
/* Jsonb *query = PG_GETARG_JSONB(2); */ |
||||
int32 nkeys = PG_GETARG_INT32(3); |
||||
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */ |
||||
bool *recheck = (bool *) PG_GETARG_POINTER(5); |
||||
bool res = true; |
||||
int32 i; |
||||
|
||||
if (strategy != JsonbContainsStrategyNumber) |
||||
elog(ERROR, "unrecognized strategy number: %d", strategy); |
||||
|
||||
/*
|
||||
* jsonb_hash_ops index doesn't have information about correspondence |
||||
* of Jsonb keys and values (as distinct from GIN keys, which a |
||||
* key/value pair is stored as), so invariably we recheck. Besides, |
||||
* there are some special rules around the containment of raw scalar |
||||
* arrays and regular arrays that are not represented here. However, |
||||
* if all of the keys are not present, that's sufficient reason to |
||||
* return false and finish immediately. |
||||
*/ |
||||
*recheck = true; |
||||
for (i = 0; i < nkeys; i++) |
||||
{ |
||||
if (!check[i]) |
||||
{ |
||||
res = false; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
Datum |
||||
gin_triconsistent_jsonb_hash(PG_FUNCTION_ARGS) |
||||
{ |
||||
GinLogicValue *check = (GinLogicValue *) PG_GETARG_POINTER(0); |
||||
StrategyNumber strategy = PG_GETARG_UINT16(1); |
||||
/* Jsonb *query = PG_GETARG_JSONB(2); */ |
||||
int32 nkeys = PG_GETARG_INT32(3); |
||||
/* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */ |
||||
GinLogicValue res = GIN_TRUE; |
||||
int32 i; |
||||
bool has_maybe = false; |
||||
|
||||
if (strategy != JsonbContainsStrategyNumber) |
||||
elog(ERROR, "unrecognized strategy number: %d", strategy); |
||||
|
||||
/*
|
||||
* All extracted keys must be present. A combination of GIN_MAYBE and |
||||
* GIN_TRUE induces a GIN_MAYBE result, because then all keys may be |
||||
* present. |
||||
*/ |
||||
for (i = 0; i < nkeys; i++) |
||||
{ |
||||
if (check[i] == GIN_FALSE) |
||||
{ |
||||
res = GIN_FALSE; |
||||
break; |
||||
} |
||||
if (check[i] == GIN_MAYBE) |
||||
{ |
||||
res = GIN_MAYBE; |
||||
has_maybe = true; |
||||
} |
||||
} |
||||
|
||||
/*
|
||||
* jsonb_hash_ops index doesn't have information about correspondence of |
||||
* Jsonb keys and values (as distinct from GIN keys, which for this opclass |
||||
* are a hash of a pair, or a hash of just an element), so invariably we |
||||
* recheck. This is also reflected in how GIN_MAYBE is given in response |
||||
* to there being no GIN_MAYBE input. |
||||
*/ |
||||
if (!has_maybe && res == GIN_TRUE) |
||||
res = GIN_MAYBE; |
||||
|
||||
PG_RETURN_GIN_LOGIC_VALUE(res); |
||||
} |
||||
|
||||
Datum |
||||
gin_extract_jsonb_hash(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = PG_GETARG_JSONB(0); |
||||
int32 *nentries = (int32 *) PG_GETARG_POINTER(1); |
||||
int total = 2 * JB_ROOT_COUNT(jb); |
||||
JsonbIterator *it; |
||||
JsonbValue v; |
||||
PathHashStack tail; |
||||
PathHashStack *stack; |
||||
int i = 0, |
||||
r; |
||||
Datum *entries = NULL; |
||||
|
||||
if (total == 0) |
||||
{ |
||||
*nentries = 0; |
||||
PG_RETURN_POINTER(NULL); |
||||
} |
||||
|
||||
entries = (Datum *) palloc(sizeof(Datum) * total); |
||||
|
||||
it = JsonbIteratorInit(VARDATA(jb)); |
||||
|
||||
tail.parent = NULL; |
||||
tail.hash = 0; |
||||
stack = &tail; |
||||
|
||||
while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) |
||||
{ |
||||
PathHashStack *tmp; |
||||
|
||||
if (i >= total) |
||||
{ |
||||
total *= 2; |
||||
entries = (Datum *) repalloc(entries, sizeof(Datum) * total); |
||||
} |
||||
|
||||
switch (r) |
||||
{ |
||||
case WJB_BEGIN_ARRAY: |
||||
case WJB_BEGIN_OBJECT: |
||||
tmp = stack; |
||||
stack = (PathHashStack *) palloc(sizeof(PathHashStack)); |
||||
|
||||
/*
|
||||
* Nesting an array within another array will not alter |
||||
* innermost scalar element hash values, but that seems |
||||
* inconsequential |
||||
*/ |
||||
if (tmp->parent) |
||||
{ |
||||
/*
|
||||
* We pass forward hashes from previous container nesting |
||||
* levels so that nested arrays with an outermost nested |
||||
* object will have element hashes mixed with the outermost |
||||
* key. It's also somewhat useful to have nested objects |
||||
* innermost values have hashes that are a function of not |
||||
* just their own key, but outer keys too. |
||||
*/ |
||||
stack->hash = tmp->hash; |
||||
} |
||||
else |
||||
{ |
||||
/*
|
||||
* At least nested level, initialize with stable container |
||||
* type proxy value |
||||
*/ |
||||
stack->hash = (r == WJB_BEGIN_ARRAY)? JB_FARRAY:JB_FOBJECT; |
||||
} |
||||
stack->parent = tmp; |
||||
break; |
||||
case WJB_KEY: |
||||
/* Initialize hash from parent */ |
||||
stack->hash = stack->parent->hash; |
||||
JsonbHashScalarValue(&v, &stack->hash); |
||||
break; |
||||
case WJB_ELEM: |
||||
/* Elements have parent hash mixed in separately */ |
||||
stack->hash = stack->parent->hash; |
||||
case WJB_VALUE: |
||||
/* Element/value case */ |
||||
JsonbHashScalarValue(&v, &stack->hash); |
||||
entries[i++] = stack->hash; |
||||
break; |
||||
case WJB_END_ARRAY: |
||||
case WJB_END_OBJECT: |
||||
/* Pop the stack */ |
||||
tmp = stack->parent; |
||||
pfree(stack); |
||||
stack = tmp; |
||||
break; |
||||
default: |
||||
elog(ERROR, "invalid JsonbIteratorNext rc: %d", r); |
||||
} |
||||
} |
||||
|
||||
*nentries = i; |
||||
|
||||
PG_RETURN_POINTER(entries); |
||||
} |
||||
|
||||
Datum |
||||
gin_extract_jsonb_query_hash(PG_FUNCTION_ARGS) |
||||
{ |
||||
int32 *nentries = (int32 *) PG_GETARG_POINTER(1); |
||||
StrategyNumber strategy = PG_GETARG_UINT16(2); |
||||
int32 *searchMode = (int32 *) PG_GETARG_POINTER(6); |
||||
Datum *entries; |
||||
|
||||
if (strategy != JsonbContainsStrategyNumber) |
||||
elog(ERROR, "unrecognized strategy number: %d", strategy); |
||||
|
||||
/* Query is a jsonb, so just apply gin_extract_jsonb... */ |
||||
entries = (Datum *) |
||||
DatumGetPointer(DirectFunctionCall2(gin_extract_jsonb_hash, |
||||
PG_GETARG_DATUM(0), |
||||
PointerGetDatum(nentries))); |
||||
|
||||
/* ...although "contains {}" requires a full index scan */ |
||||
if (entries == NULL) |
||||
*searchMode = GIN_SEARCH_MODE_ALL; |
||||
|
||||
PG_RETURN_POINTER(entries); |
||||
} |
||||
|
||||
/*
|
||||
* Build a text value from a cstring and flag suitable for storage as a key |
||||
* value |
||||
*/ |
||||
static text * |
||||
make_text_key(const char *str, int len, char flag) |
||||
{ |
||||
text *item; |
||||
|
||||
item = (text *) palloc(VARHDRSZ + len + 1); |
||||
SET_VARSIZE(item, VARHDRSZ + len + 1); |
||||
|
||||
*VARDATA(item) = flag; |
||||
|
||||
memcpy(VARDATA(item) + 1, str, len); |
||||
|
||||
return item; |
||||
} |
||||
|
||||
/*
|
||||
* Create a textual representation of a jsonbValue for GIN storage. |
||||
*/ |
||||
static text * |
||||
make_scalar_key(const JsonbValue * scalarVal, char flag) |
||||
{ |
||||
text *item; |
||||
char *cstr; |
||||
|
||||
switch (scalarVal->type) |
||||
{ |
||||
case jbvNull: |
||||
item = make_text_key("n", 1, flag); |
||||
break; |
||||
case jbvBool: |
||||
item = make_text_key(scalarVal->boolean ? "t" : "f", 1, flag); |
||||
break; |
||||
case jbvNumeric: |
||||
/*
|
||||
* A normalized textual representation, free of trailing zeroes is |
||||
* is required. |
||||
* |
||||
* It isn't ideal that numerics are stored in a relatively bulky |
||||
* textual format. However, it's a notationally convenient way of |
||||
* storing a "union" type in the GIN B-Tree, and indexing Jsonb |
||||
* strings takes precedence. |
||||
*/ |
||||
cstr = numeric_normalize(scalarVal->numeric); |
||||
item = make_text_key(cstr, strlen(cstr), flag); |
||||
pfree(cstr); |
||||
break; |
||||
case jbvString: |
||||
item = make_text_key(scalarVal->string.val, scalarVal->string.len, |
||||
flag); |
||||
break; |
||||
default: |
||||
elog(ERROR, "invalid jsonb scalar type"); |
||||
} |
||||
|
||||
return item; |
||||
} |
||||
@ -0,0 +1,295 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* jsonb_op.c |
||||
* Special operators for jsonb only, used by various index access methods |
||||
* |
||||
* Copyright (c) 2014, PostgreSQL Global Development Group |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/utils/adt/jsonb_op.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "miscadmin.h" |
||||
#include "utils/jsonb.h" |
||||
|
||||
Datum |
||||
jsonb_exists(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = PG_GETARG_JSONB(0); |
||||
text *key = PG_GETARG_TEXT_PP(1); |
||||
JsonbValue kval; |
||||
JsonbValue *v = NULL; |
||||
|
||||
/*
|
||||
* We only match Object keys (which are naturally always Strings), or |
||||
* string elements in arrays. In particular, we do not match non-string |
||||
* scalar elements. Existence of a key/element is only considered at the |
||||
* top level. No recursion occurs. |
||||
*/ |
||||
kval.type = jbvString; |
||||
kval.string.val = VARDATA_ANY(key); |
||||
kval.string.len = VARSIZE_ANY_EXHDR(key); |
||||
|
||||
v = findJsonbValueFromSuperHeader(VARDATA(jb), |
||||
JB_FOBJECT | JB_FARRAY, |
||||
NULL, |
||||
&kval); |
||||
|
||||
PG_RETURN_BOOL(v != NULL); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_exists_any(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = PG_GETARG_JSONB(0); |
||||
ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1); |
||||
JsonbValue *arrKey = arrayToJsonbSortedArray(keys); |
||||
uint32 *plowbound = NULL, |
||||
lowbound = 0; |
||||
int i; |
||||
|
||||
if (arrKey == NULL || arrKey->object.nPairs == 0) |
||||
PG_RETURN_BOOL(false); |
||||
|
||||
if (JB_ROOT_IS_OBJECT(jb)) |
||||
plowbound = &lowbound; |
||||
|
||||
/*
|
||||
* We exploit the fact that the pairs list is already sorted into strictly |
||||
* increasing order to narrow the findJsonbValueFromSuperHeader search; |
||||
* each search can start one entry past the previous "found" entry, or at |
||||
* the lower bound of the last search. |
||||
*/ |
||||
for (i = 0; i < arrKey->array.nElems; i++) |
||||
{ |
||||
if (findJsonbValueFromSuperHeader(VARDATA(jb), |
||||
JB_FOBJECT | JB_FARRAY, |
||||
plowbound, |
||||
arrKey->array.elems + i) != NULL) |
||||
PG_RETURN_BOOL(true); |
||||
} |
||||
|
||||
PG_RETURN_BOOL(false); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_exists_all(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = PG_GETARG_JSONB(0); |
||||
ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1); |
||||
JsonbValue *arrKey = arrayToJsonbSortedArray(keys); |
||||
uint32 *plowbound = NULL; |
||||
uint32 lowbound = 0; |
||||
int i; |
||||
|
||||
if (arrKey == NULL || arrKey->array.nElems == 0) |
||||
PG_RETURN_BOOL(true); |
||||
|
||||
if (JB_ROOT_IS_OBJECT(jb)) |
||||
plowbound = &lowbound; |
||||
|
||||
/*
|
||||
* We exploit the fact that the pairs list is already sorted into strictly |
||||
* increasing order to narrow the findJsonbValueFromSuperHeader search; |
||||
* each search can start one entry past the previous "found" entry, or at |
||||
* the lower bound of the last search. |
||||
*/ |
||||
for (i = 0; i < arrKey->array.nElems; i++) |
||||
{ |
||||
if (findJsonbValueFromSuperHeader(VARDATA(jb), |
||||
JB_FOBJECT | JB_FARRAY, |
||||
plowbound, |
||||
arrKey->array.elems + i) == NULL) |
||||
PG_RETURN_BOOL(false); |
||||
} |
||||
|
||||
PG_RETURN_BOOL(true); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_contains(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *val = PG_GETARG_JSONB(0); |
||||
Jsonb *tmpl = PG_GETARG_JSONB(1); |
||||
|
||||
JsonbIterator *it1, *it2; |
||||
|
||||
if (JB_ROOT_COUNT(val) < JB_ROOT_COUNT(tmpl) || |
||||
JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl)) |
||||
PG_RETURN_BOOL(false); |
||||
|
||||
it1 = JsonbIteratorInit(VARDATA(val)); |
||||
it2 = JsonbIteratorInit(VARDATA(tmpl)); |
||||
|
||||
PG_RETURN_BOOL(JsonbDeepContains(&it1, &it2)); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_contained(PG_FUNCTION_ARGS) |
||||
{ |
||||
/* Commutator of "contains" */ |
||||
Jsonb *tmpl = PG_GETARG_JSONB(0); |
||||
Jsonb *val = PG_GETARG_JSONB(1); |
||||
|
||||
JsonbIterator *it1, *it2; |
||||
|
||||
if (JB_ROOT_COUNT(val) < JB_ROOT_COUNT(tmpl) || |
||||
JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl)) |
||||
PG_RETURN_BOOL(false); |
||||
|
||||
it1 = JsonbIteratorInit(VARDATA(val)); |
||||
it2 = JsonbIteratorInit(VARDATA(tmpl)); |
||||
|
||||
PG_RETURN_BOOL(JsonbDeepContains(&it1, &it2)); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_ne(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jba = PG_GETARG_JSONB(0); |
||||
Jsonb *jbb = PG_GETARG_JSONB(1); |
||||
bool res; |
||||
|
||||
res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) != 0); |
||||
|
||||
PG_FREE_IF_COPY(jba, 0); |
||||
PG_FREE_IF_COPY(jbb, 1); |
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
/*
|
||||
* B-Tree operator class operators, support function |
||||
*/ |
||||
Datum |
||||
jsonb_lt(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jba = PG_GETARG_JSONB(0); |
||||
Jsonb *jbb = PG_GETARG_JSONB(1); |
||||
bool res; |
||||
|
||||
res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) < 0); |
||||
|
||||
PG_FREE_IF_COPY(jba, 0); |
||||
PG_FREE_IF_COPY(jbb, 1); |
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_gt(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jba = PG_GETARG_JSONB(0); |
||||
Jsonb *jbb = PG_GETARG_JSONB(1); |
||||
bool res; |
||||
|
||||
res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) > 0); |
||||
|
||||
PG_FREE_IF_COPY(jba, 0); |
||||
PG_FREE_IF_COPY(jbb, 1); |
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_le(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jba = PG_GETARG_JSONB(0); |
||||
Jsonb *jbb = PG_GETARG_JSONB(1); |
||||
bool res; |
||||
|
||||
res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) <= 0); |
||||
|
||||
PG_FREE_IF_COPY(jba, 0); |
||||
PG_FREE_IF_COPY(jbb, 1); |
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_ge(PG_FUNCTION_ARGS) |
||||
{ |
||||
|
||||
Jsonb *jba = PG_GETARG_JSONB(0); |
||||
Jsonb *jbb = PG_GETARG_JSONB(1); |
||||
bool res; |
||||
|
||||
res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) >= 0); |
||||
|
||||
PG_FREE_IF_COPY(jba, 0); |
||||
PG_FREE_IF_COPY(jbb, 1); |
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_eq(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jba = PG_GETARG_JSONB(0); |
||||
Jsonb *jbb = PG_GETARG_JSONB(1); |
||||
bool res; |
||||
|
||||
res = (compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)) == 0); |
||||
|
||||
PG_FREE_IF_COPY(jba, 0); |
||||
PG_FREE_IF_COPY(jbb, 1); |
||||
PG_RETURN_BOOL(res); |
||||
} |
||||
|
||||
Datum |
||||
jsonb_cmp(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jba = PG_GETARG_JSONB(0); |
||||
Jsonb *jbb = PG_GETARG_JSONB(1); |
||||
int res; |
||||
|
||||
res = compareJsonbSuperHeaderValue(VARDATA(jba), VARDATA(jbb)); |
||||
|
||||
PG_FREE_IF_COPY(jba, 0); |
||||
PG_FREE_IF_COPY(jbb, 1); |
||||
PG_RETURN_INT32(res); |
||||
} |
||||
|
||||
/*
|
||||
* Hash operator class jsonb hashing function |
||||
*/ |
||||
Datum |
||||
jsonb_hash(PG_FUNCTION_ARGS) |
||||
{ |
||||
Jsonb *jb = PG_GETARG_JSONB(0); |
||||
JsonbIterator *it; |
||||
int32 r; |
||||
JsonbValue v; |
||||
uint32 hash = 0; |
||||
|
||||
if (JB_ROOT_COUNT(jb) == 0) |
||||
PG_RETURN_INT32(0); |
||||
|
||||
it = JsonbIteratorInit(VARDATA(jb)); |
||||
|
||||
while ((r = JsonbIteratorNext(&it, &v, false)) != WJB_DONE) |
||||
{ |
||||
switch (r) |
||||
{ |
||||
/* Rotation is left to JsonbHashScalarValue() */ |
||||
case WJB_BEGIN_ARRAY: |
||||
hash ^= JB_FARRAY; |
||||
break; |
||||
case WJB_BEGIN_OBJECT: |
||||
hash ^= JB_FOBJECT; |
||||
break; |
||||
case WJB_KEY: |
||||
case WJB_VALUE: |
||||
case WJB_ELEM: |
||||
JsonbHashScalarValue(&v, &hash); |
||||
break; |
||||
case WJB_END_ARRAY: |
||||
case WJB_END_OBJECT: |
||||
break; |
||||
default: |
||||
elog(ERROR, "invalid JsonbIteratorNext rc: %d", r); |
||||
} |
||||
} |
||||
|
||||
PG_FREE_IF_COPY(jb, 0); |
||||
PG_RETURN_INT32(hash); |
||||
} |
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,320 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* jsonb.h |
||||
* Declarations for jsonb data type support. |
||||
* |
||||
* Copyright (c) 1996-2014, PostgreSQL Global Development Group |
||||
* |
||||
* src/include/utils/jsonb.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef __JSONB_H__ |
||||
#define __JSONB_H__ |
||||
|
||||
#include "lib/stringinfo.h" |
||||
#include "utils/array.h" |
||||
#include "utils/numeric.h" |
||||
|
||||
/*
|
||||
* JB_CMASK is used to extract count of items |
||||
* |
||||
* It's not possible to get more than 2^28 items into an Jsonb. |
||||
*/ |
||||
#define JB_CMASK 0x0FFFFFFF |
||||
|
||||
#define JB_FSCALAR 0x10000000 |
||||
#define JB_FOBJECT 0x20000000 |
||||
#define JB_FARRAY 0x40000000 |
||||
|
||||
/* Get information on varlena Jsonb */ |
||||
#define JB_ROOT_COUNT(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_CMASK) |
||||
#define JB_ROOT_IS_SCALAR(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_FSCALAR) |
||||
#define JB_ROOT_IS_OBJECT(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_FOBJECT) |
||||
#define JB_ROOT_IS_ARRAY(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_FARRAY) |
||||
|
||||
/* Jentry macros */ |
||||
#define JENTRY_POSMASK 0x0FFFFFFF |
||||
#define JENTRY_ISFIRST 0x80000000 |
||||
#define JENTRY_TYPEMASK (~(JENTRY_POSMASK | JENTRY_ISFIRST)) |
||||
#define JENTRY_ISSTRING 0x00000000 |
||||
#define JENTRY_ISNUMERIC 0x10000000 |
||||
#define JENTRY_ISNEST 0x20000000 |
||||
#define JENTRY_ISNULL 0x40000000 |
||||
#define JENTRY_ISBOOL (JENTRY_ISNUMERIC | JENTRY_ISNEST) |
||||
#define JENTRY_ISFALSE JENTRY_ISBOOL |
||||
#define JENTRY_ISTRUE (JENTRY_ISBOOL | 0x40000000) |
||||
/* Note possible multiple evaluations, also access to prior array element */ |
||||
#define JBE_ISFIRST(je_) (((je_).header & JENTRY_ISFIRST) != 0) |
||||
#define JBE_ISSTRING(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISSTRING) |
||||
#define JBE_ISNUMERIC(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISNUMERIC) |
||||
#define JBE_ISNEST(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISNEST) |
||||
#define JBE_ISNULL(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISNULL) |
||||
#define JBE_ISBOOL(je_) (((je_).header & JENTRY_TYPEMASK & JENTRY_ISBOOL) == JENTRY_ISBOOL) |
||||
#define JBE_ISBOOL_TRUE(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISTRUE) |
||||
#define JBE_ISBOOL_FALSE(je_) (JBE_ISBOOL(je_) && !JBE_ISBOOL_TRUE(je_)) |
||||
|
||||
/* Get offset for Jentry */ |
||||
#define JBE_ENDPOS(je_) ((je_).header & JENTRY_POSMASK) |
||||
#define JBE_OFF(je_) (JBE_ISFIRST(je_) ? 0 : JBE_ENDPOS((&(je_))[-1])) |
||||
#define JBE_LEN(je_) (JBE_ISFIRST(je_) ? \ |
||||
JBE_ENDPOS(je_) \
|
||||
: JBE_ENDPOS(je_) - JBE_ENDPOS((&(je_))[-1])) |
||||
|
||||
/* Flags indicating a stage of sequential Jsonb processing */ |
||||
#define WJB_DONE 0x000 |
||||
#define WJB_KEY 0x001 |
||||
#define WJB_VALUE 0x002 |
||||
#define WJB_ELEM 0x004 |
||||
#define WJB_BEGIN_ARRAY 0x008 |
||||
#define WJB_END_ARRAY 0x010 |
||||
#define WJB_BEGIN_OBJECT 0x020 |
||||
#define WJB_END_OBJECT 0x040 |
||||
|
||||
/*
|
||||
* When using a GIN index for jsonb, we choose to index both keys and values. |
||||
* The storage format is text, with K, or V prepended to the string to indicate |
||||
* key/element or value/element. |
||||
* |
||||
* Jsonb Keys and string array elements are treated equivalently when |
||||
* serialized to text index storage. One day we may wish to create an opclass |
||||
* that only indexes values, but for now keys and values are stored in GIN |
||||
* indexes in a way that doesn't really consider their relationship to each |
||||
* other. |
||||
*/ |
||||
#define JKEYELEM 'K' |
||||
#define JVAL 'V' |
||||
|
||||
#define JsonbContainsStrategyNumber 7 |
||||
#define JsonbExistsStrategyNumber 9 |
||||
#define JsonbExistsAnyStrategyNumber 10 |
||||
#define JsonbExistsAllStrategyNumber 11 |
||||
|
||||
/* Convenience macros */ |
||||
#define DatumGetJsonb(d) ((Jsonb *) PG_DETOAST_DATUM(d)) |
||||
#define JsonbGetDatum(p) PointerGetDatum(p) |
||||
#define PG_GETARG_JSONB(x) DatumGetJsonb(PG_GETARG_DATUM(x)) |
||||
#define PG_RETURN_JSONB(x) PG_RETURN_POINTER(x) |
||||
|
||||
typedef struct JsonbPair JsonbPair; |
||||
typedef struct JsonbValue JsonbValue; |
||||
typedef char* JsonbSuperHeader; |
||||
|
||||
/*
|
||||
* Jsonbs are varlena objects, so must meet the varlena convention that the |
||||
* first int32 of the object contains the total object size in bytes. Be sure |
||||
* to use VARSIZE() and SET_VARSIZE() to access it, though! |
||||
* |
||||
* Jsonb is the on-disk representation, in contrast to the in-memory JsonbValue |
||||
* representation. Often, JsonbValues are just shims through which a Jsonb |
||||
* buffer is accessed, but they can also be deep copied and passed around. |
||||
* |
||||
* We have an abstraction called a "superheader". This is a pointer that |
||||
* conventionally points to the first item after our 4-byte uncompressed |
||||
* varlena header, from which we can read flags using bitwise operations. |
||||
* |
||||
* Frequently, we pass a superheader reference to a function, and it doesn't |
||||
* matter if it points to just after the start of a Jsonb, or to a temp buffer. |
||||
*/ |
||||
typedef struct |
||||
{ |
||||
int32 vl_len_; /* varlena header (do not touch directly!) */ |
||||
uint32 superheader; |
||||
/* (array of JEntry follows, size determined using uint32 superheader) */ |
||||
} Jsonb; |
||||
|
||||
/*
|
||||
* JEntry: there is one of these for each key _and_ value for objects. Arrays |
||||
* have one per element. |
||||
* |
||||
* The position offset points to the _end_ so that we can get the length by |
||||
* subtraction from the previous entry. The JENTRY_ISFIRST flag indicates if |
||||
* there is a previous entry. |
||||
*/ |
||||
typedef struct |
||||
{ |
||||
uint32 header; /* Shares some flags with superheader */ |
||||
} JEntry; |
||||
|
||||
#define IsAJsonbScalar(jsonbval) ((jsonbval)->type >= jbvNull && \ |
||||
(jsonbval)->type <= jbvBool) |
||||
|
||||
/*
|
||||
* JsonbValue: In-memory representation of Jsonb. This is a convenient |
||||
* deserialized representation, that can easily support using the anonymous |
||||
* union across underlying types during manipulation. The Jsonb on-disk |
||||
* representation has various alignment considerations. |
||||
*/ |
||||
struct JsonbValue |
||||
{ |
||||
enum |
||||
{ |
||||
/* Scalar types */ |
||||
jbvNull = 0x0, |
||||
jbvString, |
||||
jbvNumeric, |
||||
jbvBool, |
||||
/* Composite types */ |
||||
jbvArray = 0x10, |
||||
jbvObject, |
||||
/* Binary (i.e. struct Jsonb) jbvArray/jbvObject */ |
||||
jbvBinary |
||||
} type; /* Influences sort order */ |
||||
|
||||
int estSize; /* Estimated size of node (including
|
||||
* subnodes) */ |
||||
|
||||
union |
||||
{ |
||||
Numeric numeric; |
||||
bool boolean; |
||||
struct |
||||
{ |
||||
int len; |
||||
char *val; /* Not necessarily null-terminated */ |
||||
} string; /* String primitive type */ |
||||
|
||||
struct |
||||
{ |
||||
int nElems; |
||||
JsonbValue *elems; |
||||
bool rawScalar; /* Top-level "raw scalar" array? */ |
||||
} array; /* Array container type */ |
||||
|
||||
struct |
||||
{ |
||||
int nPairs; /* 1 pair, 2 elements */ |
||||
JsonbPair *pairs; |
||||
} object; /* Associative container type */ |
||||
|
||||
struct |
||||
{ |
||||
int len; |
||||
char *data; |
||||
} binary; |
||||
}; |
||||
}; |
||||
|
||||
/*
|
||||
* Pair within an Object. |
||||
* |
||||
* Pairs with duplicate keys are de-duplicated. We store the order for the |
||||
* benefit of doing so in a well-defined way with respect to the original |
||||
* observed order (which is "last observed wins"). This is only used briefly |
||||
* when originally constructing a Jsonb. |
||||
*/ |
||||
struct JsonbPair |
||||
{ |
||||
JsonbValue key; /* Must be a jbvString */ |
||||
JsonbValue value; /* May be of any type */ |
||||
uint32 order; /* preserves order of pairs with equal keys */ |
||||
}; |
||||
|
||||
/* Conversion state used when parsing Jsonb from text, or for type coercion */ |
||||
typedef struct JsonbParseState |
||||
{ |
||||
JsonbValue contVal; |
||||
Size size; |
||||
struct JsonbParseState *next; |
||||
} JsonbParseState; |
||||
|
||||
/*
|
||||
* JsonbIterator holds details of the type for each iteration. It also stores a |
||||
* Jsonb varlena buffer, which can be directly accessed in some contexts. |
||||
*/ |
||||
typedef enum |
||||
{ |
||||
jbi_start = 0x0, |
||||
jbi_key, |
||||
jbi_value, |
||||
jbi_elem |
||||
} JsonbIterState; |
||||
|
||||
typedef struct JsonbIterator |
||||
{ |
||||
/* Jsonb varlena buffer (may or may not be root) */ |
||||
char *buffer; |
||||
|
||||
/* Current value */ |
||||
uint32 containerType; /* Never of value JB_FSCALAR, since
|
||||
* scalars will appear in pseudo-arrays */ |
||||
uint32 nElems; /* Number of elements in metaArray
|
||||
* (will be nPairs for objects) */ |
||||
bool isScalar; /* Pseudo-array scalar value? */ |
||||
JEntry *meta; |
||||
|
||||
/* Current item in buffer (up to nElems, but must * 2 for objects) */ |
||||
int i; |
||||
|
||||
/*
|
||||
* Data proper. Note that this points just past end of "meta" array. We |
||||
* use its metadata (Jentrys) with JBE_OFF() macro to find appropriate |
||||
* offsets into this array. |
||||
*/ |
||||
char *dataProper; |
||||
|
||||
/* Private state */ |
||||
JsonbIterState state; |
||||
|
||||
struct JsonbIterator *parent; |
||||
} JsonbIterator; |
||||
|
||||
/* I/O routines */ |
||||
extern Datum jsonb_in(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_out(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_recv(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_send(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_typeof(PG_FUNCTION_ARGS); |
||||
|
||||
/* Indexing-related ops */ |
||||
extern Datum jsonb_exists(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_exists_any(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_exists_all(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_contains(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_contained(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_ne(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_lt(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_gt(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_le(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_ge(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_eq(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_cmp(PG_FUNCTION_ARGS); |
||||
extern Datum jsonb_hash(PG_FUNCTION_ARGS); |
||||
|
||||
/* GIN support functions */ |
||||
extern Datum gin_compare_jsonb(PG_FUNCTION_ARGS); |
||||
extern Datum gin_extract_jsonb(PG_FUNCTION_ARGS); |
||||
extern Datum gin_extract_jsonb_query(PG_FUNCTION_ARGS); |
||||
extern Datum gin_consistent_jsonb(PG_FUNCTION_ARGS); |
||||
extern Datum gin_triconsistent_jsonb(PG_FUNCTION_ARGS); |
||||
/* GIN hash opclass functions */ |
||||
extern Datum gin_extract_jsonb_hash(PG_FUNCTION_ARGS); |
||||
extern Datum gin_extract_jsonb_query_hash(PG_FUNCTION_ARGS); |
||||
extern Datum gin_consistent_jsonb_hash(PG_FUNCTION_ARGS); |
||||
extern Datum gin_triconsistent_jsonb_hash(PG_FUNCTION_ARGS); |
||||
|
||||
/* Support functions */ |
||||
extern int compareJsonbSuperHeaderValue(JsonbSuperHeader a, |
||||
JsonbSuperHeader b); |
||||
extern JsonbValue *findJsonbValueFromSuperHeader(JsonbSuperHeader sheader, |
||||
uint32 flags, |
||||
uint32 *lowbound, |
||||
JsonbValue *key); |
||||
extern JsonbValue *getIthJsonbValueFromSuperHeader(JsonbSuperHeader sheader, |
||||
uint32 i); |
||||
extern JsonbValue *pushJsonbValue(JsonbParseState ** pstate, int seq, |
||||
JsonbValue *scalarVal); |
||||
extern JsonbIterator *JsonbIteratorInit(JsonbSuperHeader buffer); |
||||
extern int JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, |
||||
bool skipNested); |
||||
extern Jsonb *JsonbValueToJsonb(JsonbValue *val); |
||||
extern bool JsonbDeepContains(JsonbIterator ** val, |
||||
JsonbIterator ** mContained); |
||||
extern JsonbValue *arrayToJsonbSortedArray(ArrayType *a); |
||||
extern void JsonbHashScalarValue(const JsonbValue * scalarVal, uint32 * hash); |
||||
|
||||
/* jsonb.c support function */ |
||||
extern char *JsonbToCString(StringInfo out, JsonbSuperHeader in, |
||||
int estimated_len); |
||||
|
||||
#endif /* __JSONB_H__ */ |
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,479 @@ |
||||
-- Strings. |
||||
SELECT '""'::jsonb; -- OK. |
||||
SELECT $$''$$::jsonb; -- ERROR, single quotes are not allowed |
||||
SELECT '"abc"'::jsonb; -- OK |
||||
SELECT '"abc'::jsonb; -- ERROR, quotes not closed |
||||
SELECT '"abc |
||||
def"'::jsonb; -- ERROR, unescaped newline in string constant |
||||
SELECT '"\n\"\\"'::jsonb; -- OK, legal escapes |
||||
SELECT '"\v"'::jsonb; -- ERROR, not a valid JSON escape |
||||
SELECT '"\u"'::jsonb; -- ERROR, incomplete escape |
||||
SELECT '"\u00"'::jsonb; -- ERROR, incomplete escape |
||||
SELECT '"\u000g"'::jsonb; -- ERROR, g is not a hex digit |
||||
SELECT '"\u0000"'::jsonb; -- OK, legal escape |
||||
-- use octet_length here so we don't get an odd unicode char in the |
||||
-- output |
||||
SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK |
||||
|
||||
-- Numbers. |
||||
SELECT '1'::jsonb; -- OK |
||||
SELECT '0'::jsonb; -- OK |
||||
SELECT '01'::jsonb; -- ERROR, not valid according to JSON spec |
||||
SELECT '0.1'::jsonb; -- OK |
||||
SELECT '9223372036854775808'::jsonb; -- OK, even though it's too large for int8 |
||||
SELECT '1e100'::jsonb; -- OK |
||||
SELECT '1.3e100'::jsonb; -- OK |
||||
SELECT '1f2'::jsonb; -- ERROR |
||||
SELECT '0.x1'::jsonb; -- ERROR |
||||
SELECT '1.3ex100'::jsonb; -- ERROR |
||||
|
||||
-- Arrays. |
||||
SELECT '[]'::jsonb; -- OK |
||||
SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb; -- OK |
||||
SELECT '[1,2]'::jsonb; -- OK |
||||
SELECT '[1,2,]'::jsonb; -- ERROR, trailing comma |
||||
SELECT '[1,2'::jsonb; -- ERROR, no closing bracket |
||||
SELECT '[1,[2]'::jsonb; -- ERROR, no closing bracket |
||||
|
||||
-- Objects. |
||||
SELECT '{}'::jsonb; -- OK |
||||
SELECT '{"abc"}'::jsonb; -- ERROR, no value |
||||
SELECT '{"abc":1}'::jsonb; -- OK |
||||
SELECT '{1:"abc"}'::jsonb; -- ERROR, keys must be strings |
||||
SELECT '{"abc",1}'::jsonb; -- ERROR, wrong separator |
||||
SELECT '{"abc"=1}'::jsonb; -- ERROR, totally wrong separator |
||||
SELECT '{"abc"::1}'::jsonb; -- ERROR, another wrong separator |
||||
SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK |
||||
SELECT '{"abc":1:2}'::jsonb; -- ERROR, colon in wrong spot |
||||
SELECT '{"abc":1,3}'::jsonb; -- ERROR, no value |
||||
|
||||
-- Miscellaneous stuff. |
||||
SELECT 'true'::jsonb; -- OK |
||||
SELECT 'false'::jsonb; -- OK |
||||
SELECT 'null'::jsonb; -- OK |
||||
SELECT ' true '::jsonb; -- OK, even with extra whitespace |
||||
SELECT 'true false'::jsonb; -- ERROR, too many values |
||||
SELECT 'true, false'::jsonb; -- ERROR, too many values |
||||
SELECT 'truf'::jsonb; -- ERROR, not a keyword |
||||
SELECT 'trues'::jsonb; -- ERROR, not a keyword |
||||
SELECT ''::jsonb; -- ERROR, no value |
||||
SELECT ' '::jsonb; -- ERROR, no value |
||||
|
||||
-- make sure jsonb is passed through json generators without being escaped |
||||
SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']); |
||||
|
||||
-- jsonb extraction functions |
||||
CREATE TEMP TABLE test_jsonb ( |
||||
json_type text, |
||||
test_json jsonb |
||||
); |
||||
|
||||
INSERT INTO test_jsonb VALUES |
||||
('scalar','"a scalar"'), |
||||
('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'), |
||||
('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}'); |
||||
|
||||
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar'; |
||||
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object'; |
||||
SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object'; |
||||
|
||||
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar'; |
||||
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object'; |
||||
|
||||
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar'; |
||||
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object'; |
||||
|
||||
SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array'; |
||||
|
||||
SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object'; |
||||
SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object'; |
||||
SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object'; |
||||
|
||||
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar'; |
||||
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object'; |
||||
|
||||
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar'; |
||||
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'object'; |
||||
|
||||
-- nulls |
||||
SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object'; |
||||
SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object'; |
||||
SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array'; |
||||
SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array'; |
||||
|
||||
-- equality and inequality |
||||
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb; |
||||
SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb; |
||||
|
||||
SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb; |
||||
SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb; |
||||
|
||||
CREATE TABLE testjsonb (j jsonb); |
||||
\copy testjsonb FROM 'data/jsonb.data' |
||||
|
||||
-- containment |
||||
SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}'); |
||||
SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}'); |
||||
SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}'); |
||||
SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}'); |
||||
SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}'); |
||||
SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}'); |
||||
SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}'); |
||||
SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}'; |
||||
SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}'; |
||||
SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}'; |
||||
SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}'; |
||||
SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}'; |
||||
SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}'; |
||||
SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}'; |
||||
|
||||
SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}'); |
||||
SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}'); |
||||
SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}'); |
||||
SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}'); |
||||
SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}'); |
||||
SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}'); |
||||
SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}'); |
||||
SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}'; |
||||
SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}'; |
||||
SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}'; |
||||
SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}'; |
||||
SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}'; |
||||
SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}'; |
||||
SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}'; |
||||
-- Raw scalar may contain another raw scalar, array may contain a raw scalar |
||||
SELECT '[5]'::jsonb @> '[5]'; |
||||
SELECT '5'::jsonb @> '5'; |
||||
SELECT '[5]'::jsonb @> '5'; |
||||
-- But a raw scalar cannot contain an array |
||||
SELECT '5'::jsonb @> '[5]'; |
||||
-- In general, one thing should always contain itself. Test array containment: |
||||
SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb; |
||||
SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb; |
||||
-- array containment string matching confusion bug |
||||
SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}'; |
||||
|
||||
-- array length |
||||
SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]'); |
||||
SELECT jsonb_array_length('[]'); |
||||
SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}'); |
||||
SELECT jsonb_array_length('4'); |
||||
|
||||
-- each |
||||
SELECT jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}'); |
||||
SELECT jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q; |
||||
SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q; |
||||
SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q; |
||||
|
||||
SELECT jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}'); |
||||
SELECT jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q; |
||||
SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q; |
||||
SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q; |
||||
|
||||
-- exists |
||||
SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a'); |
||||
SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b'); |
||||
SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c'); |
||||
SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a'); |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ? 'a'; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ? 'b'; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ? 'c'; |
||||
SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a'; |
||||
-- array exists - array elements should behave as keys |
||||
SELECT count(*) from testjsonb WHERE j->'array' ? 'bar'; |
||||
-- type sensitive array exists - should return no rows (since "exists" only |
||||
-- matches strings that are either object keys or array elements) |
||||
SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text; |
||||
-- However, a raw scalar is *contained* within the array |
||||
SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb; |
||||
|
||||
SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']); |
||||
SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']); |
||||
SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']); |
||||
SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']); |
||||
SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]); |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[]; |
||||
|
||||
SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']); |
||||
SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']); |
||||
SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']); |
||||
SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']); |
||||
SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]); |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b']; |
||||
SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[]; |
||||
|
||||
-- typeof |
||||
SELECT jsonb_typeof('{}') AS object; |
||||
SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object; |
||||
SELECT jsonb_typeof('[]') AS array; |
||||
SELECT jsonb_typeof('["a", 1]') AS array; |
||||
SELECT jsonb_typeof('null') AS "null"; |
||||
SELECT jsonb_typeof('1') AS number; |
||||
SELECT jsonb_typeof('-1') AS number; |
||||
SELECT jsonb_typeof('1.0') AS number; |
||||
SELECT jsonb_typeof('1e2') AS number; |
||||
SELECT jsonb_typeof('-1.0') AS number; |
||||
SELECT jsonb_typeof('true') AS boolean; |
||||
SELECT jsonb_typeof('false') AS boolean; |
||||
SELECT jsonb_typeof('"hello"') AS string; |
||||
SELECT jsonb_typeof('"true"') AS string; |
||||
SELECT jsonb_typeof('"1.0"') AS string; |
||||
|
||||
-- extract_path, extract_path_as_text |
||||
SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6'); |
||||
SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2'); |
||||
SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text); |
||||
SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text); |
||||
SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6'); |
||||
SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2'); |
||||
SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text); |
||||
SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text); |
||||
|
||||
-- extract_path nulls |
||||
SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false; |
||||
SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true; |
||||
SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false; |
||||
SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true; |
||||
|
||||
-- extract_path operators |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f4','f6']; |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2']; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','0']; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','1']; |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f4','f6']; |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2']; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0']; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1']; |
||||
|
||||
-- same using array literals |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>'{f4,f6}'; |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>'{f2}'; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>'{f2,0}'; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>'{f2,1}'; |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>'{f4,f6}'; |
||||
SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>'{f2}'; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>'{f2,0}'; |
||||
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>'{f2,1}'; |
||||
|
||||
-- same on jsonb scalars (expecting errors) |
||||
SELECT '42'::jsonb#>array['f2']; |
||||
SELECT '42'::jsonb#>array['0']; |
||||
SELECT '42'::jsonb#>>array['f2']; |
||||
SELECT '42'::jsonb#>>array['0']; |
||||
|
||||
-- array_elements |
||||
SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]'); |
||||
SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q; |
||||
SELECT jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]'); |
||||
SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q; |
||||
|
||||
-- populate_record |
||||
CREATE TYPE jbpop AS (a text, b int, c timestamp); |
||||
|
||||
SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q; |
||||
SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q; |
||||
|
||||
SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}', true) q; |
||||
SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}', true) q; |
||||
|
||||
SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":[100,200,false],"x":43.2}', true) q; |
||||
SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":[100,200,false],"x":43.2}', true) q; |
||||
SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"c":[100,200,false],"x":43.2}', true) q; |
||||
|
||||
-- populate_recordset |
||||
SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',false) q; |
||||
SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',false) q; |
||||
SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',true) q; |
||||
SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]',true) q; |
||||
SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]',true) q; |
||||
SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]',true) q; |
||||
|
||||
-- using the default use_json_as_text argument |
||||
SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; |
||||
SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; |
||||
SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q; |
||||
SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q; |
||||
|
||||
|
||||
-- handling of unicode surrogate pairs |
||||
SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8; |
||||
SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row |
||||
SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order |
||||
SELECT jsonb '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate |
||||
SELECT jsonb '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate |
||||
|
||||
-- handling of simple unicode escapes |
||||
SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' AS correct_in_utf8; |
||||
SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' AS correct_everyWHERE; |
||||
SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' AS not_unescaped; |
||||
|
||||
-- indexing |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}'; |
||||
SELECT count(*) FROM testjsonb WHERE j ? 'public'; |
||||
SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled']; |
||||
SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled']; |
||||
|
||||
CREATE INDEX jidx ON testjsonb USING gin (j); |
||||
SET enable_seqscan = off; |
||||
|
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}'; |
||||
-- excercise GIN_SEARCH_MODE_ALL |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{}'; |
||||
SELECT count(*) FROM testjsonb WHERE j ? 'public'; |
||||
SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled']; |
||||
SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled']; |
||||
|
||||
-- array exists - array elements should behave as keys (for GIN index scans too) |
||||
CREATE INDEX jidx_array ON testjsonb USING gin((j->'array')); |
||||
SELECT count(*) from testjsonb WHERE j->'array' ? 'bar'; |
||||
-- type sensitive array exists - should return no rows (since "exists" only |
||||
-- matches strings that are either object keys or array elements) |
||||
SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text; |
||||
-- However, a raw scalar is *contained* within the array |
||||
SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb; |
||||
|
||||
RESET enable_seqscan; |
||||
|
||||
SELECT count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow; |
||||
SELECT key, count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow GROUP BY key ORDER BY count DESC, key; |
||||
|
||||
-- sort/hash |
||||
SELECT count(distinct j) FROM testjsonb; |
||||
SET enable_hashagg = off; |
||||
SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2; |
||||
SET enable_hashagg = on; |
||||
SET enable_sort = off; |
||||
SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2; |
||||
SELECT distinct * FROM (values (jsonb '{}' || ''),('{}')) v(j); |
||||
SET enable_sort = on; |
||||
|
||||
RESET enable_hashagg; |
||||
RESET enable_sort; |
||||
|
||||
DROP INDEX jidx; |
||||
DROP INDEX jidx_array; |
||||
-- btree |
||||
CREATE INDEX jidx ON testjsonb USING btree (j); |
||||
SET enable_seqscan = off; |
||||
|
||||
SELECT count(*) FROM testjsonb WHERE j > '{"p":1}'; |
||||
SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}'; |
||||
|
||||
--gin hash |
||||
DROP INDEX jidx; |
||||
CREATE INDEX jidx ON testjsonb USING gin (j jsonb_hash_ops); |
||||
SET enable_seqscan = off; |
||||
|
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}'; |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}'; |
||||
-- excercise GIN_SEARCH_MODE_ALL |
||||
SELECT count(*) FROM testjsonb WHERE j @> '{}'; |
||||
|
||||
RESET enable_seqscan; |
||||
DROP INDEX jidx; |
||||
|
||||
-- nested tests |
||||
SELECT '{"ff":{"a":12,"b":16}}'::jsonb; |
||||
SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb; |
||||
SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb; |
||||
SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2"],"d":{"d1":"d1","d2":"d2"}}}'::jsonb; |
||||
SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2",["c3"],{"c4":4}],"d":{"d1":"d1","d2":"d2"}}}'::jsonb; |
||||
SELECT '{"ff":["a","aaa"]}'::jsonb; |
||||
|
||||
SELECT |
||||
'{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff', |
||||
'{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq', |
||||
('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f, |
||||
('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t, |
||||
'{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x'; |
||||
|
||||
-- nested containment |
||||
SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}'; |
||||
SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}'; |
||||
SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}'; |
||||
SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}'; |
||||
SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}'; |
||||
SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}'; |
||||
SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]'; |
||||
SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]'; |
||||
SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]'; |
||||
SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]'; |
||||
|
||||
SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}'; |
||||
SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}'; |
||||
SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}'; |
||||
|
||||
SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}'; |
||||
SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}'; |
||||
SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}'; |
||||
SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}'; |
||||
|
||||
-- nested object field / array index lookup |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error |
||||
|
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> 0; |
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> 1; |
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> 2; |
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> 3; |
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1; |
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> 4; |
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> 5; |
||||
SELECT '["a","b","c",[1,2],null]'::jsonb -> -1; |
||||
|
||||
--nested path extraction |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{0}'; |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{a}'; |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c}'; |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,0}'; |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,1}'; |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,2}'; |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,3}'; |
||||
SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-1}'; |
||||
|
||||
SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{0}'; |
||||
SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{3}'; |
||||
SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4}'; |
||||
SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4,5}'; |
||||
|
||||
--nested exists |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd'; |
||||
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e'; |
||||
Loading…
Reference in new issue