24
24
</itemizedlist>
25
25
26
26
<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"/>.
29
30
</para>
30
31
31
32
<sect2 id="functions-sqljson">
@@ -1064,15 +1065,13 @@ SELECT JSON_EXISTS(jsonb '{"a": [1,2,3]}', 'strict $.a[5]');
1064
1065
</term>
1065
1066
<listitem>
1066
1067
<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.
1074
1072
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"/>.
1076
1075
</para>
1077
1076
</listitem>
1078
1077
</varlistentry>
@@ -1112,11 +1111,10 @@ SELECT JSON_EXISTS(jsonb '{"a": [1,2,3]}', 'strict $.a[5]');
1112
1111
<title>Examples</title>
1113
1112
1114
1113
<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:
1120
1118
</para>
1121
1119
1122
1120
<screen>
@@ -1226,7 +1224,12 @@ SELECT JSON_VALUE(jsonb '[1,2]', 'strict $[*]' DEFAULT 1 ON ERROR);
1226
1224
</term>
1227
1225
<listitem>
1228
1226
<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.
1230
1233
For details, see <xref linkend="sqljson-output-clause"/>.
1231
1234
</para>
1232
1235
</listitem>
@@ -2032,19 +2035,127 @@ FROM
2032
2035
<listitem>
2033
2036
<para>
2034
2037
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.
2041
2039
</para>
2040
+
2042
2041
<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
+
2045
2058
<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>.
2047
2070
</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>
2048
2159
</listitem>
2049
2160
</varlistentry>
2050
2161
</variablelist>
@@ -2067,18 +2178,18 @@ FROM
2067
2178
<para>
2068
2179
The SQL/JSON query functions pass the provided path expression to
2069
2180
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
2074
2185
that completes the specified computation. If the query result
2075
2186
must be JSON text, you have to use the <command>WITH WRAPPER</command> clause
2076
2187
or enclose the path expression in square brackets to ensure
2077
2188
the evaluation result is an array.
2078
2189
</para>
2079
2190
2080
2191
<para>
2081
- A typical path expression has the following structure:
2192
+ A path expression has the following structure:
2082
2193
</para>
2083
2194
2084
2195
<programlisting>
@@ -2508,7 +2619,7 @@ FROM
2508
2619
</row>
2509
2620
<row>
2510
2621
<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>
2512
2623
<entry><literal>["abc", "abd", "aBdC", "abdacb", "babc"]</literal></entry>
2513
2624
<entry><literal>$[*] ? (@ like_regex "^ab.*c" flag "i")</literal></entry>
2514
2625
<entry><literal>"abc", "aBdC", "abdacb"</literal></entry>
@@ -3545,4 +3656,3 @@ FROM
3545
3656
</sect2>
3546
3657
3547
3658
</sect1>
3548
-
0 commit comments