Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7618ec5

Browse files
author
Liudmila Mantrova
committed
DOC: improved descriptions of handling data types by SQL/JSON functions based on feedback from Nikita Glukhov
1 parent 1388737 commit 7618ec5

File tree

2 files changed

+143
-33
lines changed

2 files changed

+143
-33
lines changed

doc/src/sgml/biblio.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
<pubdate>1988</pubdate>
137137
</biblioentry>
138138

139-
<biblioentry id="sqltr17">
139+
<biblioentry id="sqltr-19075-6">
140140
<title>SQL Technical Report</title>
141141
<subtitle>Part 6: SQL support for JavaScript Object
142142
Notation (JSON)</subtitle>

doc/src/sgml/func-sqljson.sgml

Lines changed: 142 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
</itemizedlist>
2525

2626
<para>
27-
To learn more about the SQL/JSON standard, see <xref linkend="sqltr17"/>.
28-
For details on JSON types supported in <productname>PostgreSQL</productname>, see <xref linkend="datatype-json"/>.
27+
To learn more about the SQL/JSON standard, see <xref linkend="sqltr-19075-6"/>.
28+
For details on JSON types supported in <productname>PostgreSQL</productname>,
29+
see <xref linkend="datatype-json"/>.
2930
</para>
3031

3132
<sect2 id="functions-sqljson">
@@ -1064,15 +1065,13 @@ SELECT JSON_EXISTS(jsonb '{"a": [1,2,3]}', 'strict $.a[5]');
10641065
</term>
10651066
<listitem>
10661067
<para>
1067-
The output clause that specifies the data type of the returned value.
1068-
Out of the box, <productname>PostgreSQL</productname>
1069-
supports the following types: <literal>json</literal>, <literal>jsonb</literal>,
1070-
<literal>bytea</literal>, and character string types (<literal>text</literal>, <literal>char</literal>,
1071-
<literal>varchar</literal>, and <literal>nchar</literal>).
1072-
The extracted value must be a single <acronym>SQL/JSON</acronym> scalar item
1073-
and have a cast to the specified type. Otherwise, an error occurs.
1068+
The output clause that specifies the SQL data type the extracted value
1069+
will be represented as. The extracted value must be a single
1070+
<acronym>SQL/JSON</acronym> scalar item and have a cast to the
1071+
specified type. Otherwise, an error occurs.
10741072
By default, <function>JSON_VALUE</function> returns a string
1075-
of the <literal>text</literal> type.
1073+
of the <literal>text</literal> type. For details, see
1074+
<xref linkend="sqljson-output-clause"/>.
10761075
</para>
10771076
</listitem>
10781077
</varlistentry>
@@ -1112,11 +1111,10 @@ SELECT JSON_EXISTS(jsonb '{"a": [1,2,3]}', 'strict $.a[5]');
11121111
<title>Examples</title>
11131112

11141113
<para>
1115-
Extract an SQL/JSON value and return it as an SQL
1116-
scalar of the specified type. Note that
1117-
<command>JSON_VALUE</command> can only return a
1118-
single scalar, and the returned value must have a
1119-
cast to the specified return type:
1114+
Extract an SQL/JSON value and return it as an SQL scalar
1115+
of the specified type. Note that <command>JSON_VALUE</command>
1116+
can only return a single scalar, and the extracted value must have
1117+
a cast to the specified return type:
11201118
</para>
11211119

11221120
<screen>
@@ -1226,7 +1224,12 @@ SELECT JSON_VALUE(jsonb '[1,2]', 'strict $[*]' DEFAULT 1 ON ERROR);
12261224
</term>
12271225
<listitem>
12281226
<para>
1229-
The output clause that specifies the data type of the returned value.
1227+
The output clause that specifies the SQL data type the extracted
1228+
SQL/JSON object or array will be represented as. By default,
1229+
<function>JSON_QUERY</function> returns a string of the
1230+
<literal>json</literal> type, unless the context item is explicitly
1231+
converted to another type. In this case, the return type corresponds
1232+
to the type of the context item.
12301233
For details, see <xref linkend="sqljson-output-clause"/>.
12311234
</para>
12321235
</listitem>
@@ -2032,19 +2035,127 @@ FROM
20322035
<listitem>
20332036
<para>
20342037
The output clause that specifies the return type of the generated
2035-
<acronym>JSON</acronym> object. Out of the box, <productname>PostgreSQL</productname>
2036-
supports the following types: <type>json</type>, <type>jsonb</type>,
2037-
<type>bytea</type>, and character string types (<type>text</type>, <type>char</type>,
2038-
<type>varchar</type>, and <type>nchar</type>).
2039-
To use other types, you must create the <literal>CAST</literal> from <type>json</type> for this type.
2040-
By default, the <type>json</type> type is returned.
2038+
<acronym>SQL/JSON</acronym> item.
20412039
</para>
2040+
20422041
<para>
2043-
The optional <literal>FORMAT</literal> clause is provided to conform to the SQL/JSON standard.
2044-
</para>
2042+
The <literal>RETURNING</literal> clause is common for both constructor
2043+
and query SQL/JSON functions. All functions rely on type casts
2044+
available in <productname>PostgreSQL</productname>, but have some
2045+
implementation specifics that determines which types can be used
2046+
by a particular function. Type casts supported by
2047+
<productname>PostgreSQL</productname> out of the box are listed
2048+
in <xref linkend="functions-jsonb-type-casts"/>.
2049+
</para>
2050+
2051+
<para>
2052+
Constructor functions support <type>json</type>, <type>jsonb</type>,
2053+
<type>bytea</type>, and any other types that have a cast from
2054+
the SQL type of the constructed item. By default,
2055+
the constructed item is represented as <type>json</type>.
2056+
</para>
2057+
20452058
<para>
2046-
The output clause is common for both constructor and query SQL/JSON functions.
2059+
For the <literal>JSON_VALUE</literal> function, you can use
2060+
<type>json</type>, <type>jsonb</type>, or any other type that has
2061+
a cast from the SQL type that corresponds to the SQL/JSON type of the
2062+
extracted value. The output to <type>json</type> and <type>jsonb</type>
2063+
types will always work regardless of the type of the extracted value,
2064+
since SQL/JSON items are converted directly to <type>json</type> or
2065+
<type>jsonb</type> without casting. With other types, the result
2066+
depends on the combination of the type of the extracted value
2067+
and the specified return type: if no cast is found, an error occurs.
2068+
By default, the <acronym>SQL/JSON</acronym> item returned by
2069+
<literal>JSON_VALUE</literal> is represented as <type>text</type>.
20472070
</para>
2071+
2072+
<para>
2073+
With the <literal>JSON_QUERY</literal> function, you can use any
2074+
return types. The implementation details are as follows:
2075+
<itemizedlist>
2076+
<listitem>
2077+
<para>
2078+
For SQL/JSON items of a scalar SQL type, a cast from <type>json</type>
2079+
or <type>jsonb</type> is searched, depending on the type of the context
2080+
item. If no cast is found, an input/output function is used for
2081+
conversion. As an exception, if you use the <type>bytea</type> return
2082+
type, the extracted SQL/JSON item is converted to the return type as
2083+
follows: <literal>convert_to(json[b]::text, 'UTF8')</literal>.
2084+
If <literal>JSON_QUERY</literal> extracts an SQL/JSON string,
2085+
which is not wrapped using the <literal>WRAPPER</literal> clause,
2086+
and the <literal>OMIT QUOTES</literal> clause is specified, the string
2087+
contents is converted to the return type using the input/output
2088+
function that corresponds to this return type.
2089+
</para>
2090+
</listitem>
2091+
<listitem>
2092+
<para>
2093+
For SQL/JSON items of non-scalar SQL types (arrays, records, and
2094+
domains), the <function>json[b]_populate_record()</function> function
2095+
is used for conversion to the specified return type. This is a
2096+
<productname>PostgreSQL</productname> extension of the SQL/JSON standard.
2097+
</para>
2098+
</listitem>
2099+
</itemizedlist>
2100+
By default, <function>JSON_QUERY</function> returns a string of the
2101+
<literal>json</literal> type, unless the context item is is explicitly
2102+
converted to another type. In this case, the return type corresponds
2103+
to the type of the context item.
2104+
</para>
2105+
2106+
<table id="functions-jsonb-type-casts">
2107+
<title>Type Casts Supported by <productname>PostgreSQL</productname></title>
2108+
<tgroup cols="2">
2109+
<thead>
2110+
<row>
2111+
<entry>SQL/JSON type</entry>
2112+
<entry>SQL Type</entry>
2113+
</row>
2114+
</thead>
2115+
<tbody>
2116+
<row>
2117+
<entry><type>string</type></entry>
2118+
<entry><type>text</type></entry>
2119+
</row>
2120+
<row>
2121+
<entry><type>number</type></entry>
2122+
<entry><type>numeric</type></entry>
2123+
</row>
2124+
<row>
2125+
<entry><type>boolean</type></entry>
2126+
<entry><type>boolean</type></entry>
2127+
</row>
2128+
<row>
2129+
<entry><type>date</type></entry>
2130+
<entry><type>date</type></entry>
2131+
</row>
2132+
<row>
2133+
<entry><type>time</type></entry>
2134+
<entry><type>time</type></entry>
2135+
</row>
2136+
<row>
2137+
<entry><type>time with tz</type></entry>
2138+
<entry><type>timetz</type></entry>
2139+
</row>
2140+
<row>
2141+
<entry><type>timestamp</type></entry>
2142+
<entry><type>timestamp</type></entry>
2143+
</row>
2144+
<row>
2145+
<entry><type>timestamp with tz</type></entry>
2146+
<entry><type>timestamptz</type></entry>
2147+
</row>
2148+
</tbody>
2149+
</tgroup>
2150+
</table>
2151+
2152+
<para>
2153+
The optional <literal>FORMAT</literal> clause is provided
2154+
to conform to the SQL/JSON standard and can only take the
2155+
<literal>json</literal> value as an argument.
2156+
<productname>PostgreSQL</productname> currently supports
2157+
only the UTF8 encoding.
2158+
</para>
20482159
</listitem>
20492160
</varlistentry>
20502161
</variablelist>
@@ -2067,18 +2178,18 @@ FROM
20672178
<para>
20682179
The SQL/JSON query functions pass the provided path expression to
20692180
the <firstterm>path engine</firstterm> for evaluation.
2070-
The path expression is evaluated from left to right.
2071-
You can use parentheses to change the order of operations.
2072-
If the evaluation is successful, an SQL/JSON sequence is produced.
2073-
The evaluation result is returned to the SQL/JSON query function
2181+
The path expression is evaluated from left to right, but
2182+
you can use parentheses to change the order of operations.
2183+
If the evaluation is successful, an SQL/JSON sequence is produced,
2184+
and the evaluation result is returned to the SQL/JSON query function
20742185
that completes the specified computation. If the query result
20752186
must be JSON text, you have to use the <command>WITH WRAPPER</command> clause
20762187
or enclose the path expression in square brackets to ensure
20772188
the evaluation result is an array.
20782189
</para>
20792190

20802191
<para>
2081-
A typical path expression has the following structure:
2192+
A path expression has the following structure:
20822193
</para>
20832194

20842195
<programlisting>
@@ -2508,7 +2619,7 @@ FROM
25082619
</row>
25092620
<row>
25102621
<entry><literal>like_regex</literal></entry>
2511-
<entry>Tests pattern matching with regular expressions</entry>
2622+
<entry>Tests pattern matching with POSIX regular expressions</entry>
25122623
<entry><literal>["abc", "abd", "aBdC", "abdacb", "babc"]</literal></entry>
25132624
<entry><literal>$[*] ? (@ like_regex "^ab.*c" flag "i")</literal></entry>
25142625
<entry><literal>"abc", "aBdC", "abdacb"</literal></entry>
@@ -3545,4 +3656,3 @@ FROM
35453656
</sect2>
35463657

35473658
</sect1>
3548-

0 commit comments

Comments
 (0)