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
1988
137137
138138

139-
sqltr17">
139+
sqltr-19075-6">
140140
SQL Technical Report
141141
Part 6: SQL support for JavaScript Object
142142
Notation (JSON)

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
2525

2626
27-
To learn more about the SQL/JSON standard, see .
28-
For details on JSON types supported in PostgreSQL, see .
27+
To learn more about the SQL/JSON standard, see .
28+
For details on JSON types supported in PostgreSQL,
29+
see .
2930
3031

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

11141113
1115-
Extract an SQL/JSON value and return it as an SQL
1116-
scalar of the specified type. Note that
1117-
JSON_VALUE 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 JSON_VALUE
1116+
can only return a single scalar, and the extracted value must have
1117+
a cast to the specified return type:
11201118
11211119

11221120
@@ -1226,7 +1224,12 @@ SELECT JSON_VALUE(jsonb '[1,2]', 'strict $[*]' DEFAULT 1 ON ERROR);
12261224
12271225
12281226
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+
JSON_QUERY returns a string of the
1230+
json 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 .
12311234
12321235
@@ -2032,19 +2035,127 @@ FROM
20322035
20332036
20342037
The output clause that specifies the return type of the generated
2035-
JSON object. Out of the box, PostgreSQL
2036-
supports the following types: json, jsonb,
2037-
bytea, and character string types (text, char,
2038-
varchar, and nchar).
2039-
To use other types, you must create the CAST from json for this type.
2040-
By default, the json type is returned.
2038+
SQL/JSON item.
20412039
2040+
20422041
2043-
The optional FORMAT clause is provided to conform to the SQL/JSON standard.
2044-
2042+
The RETURNING clause is common for both constructor
2043+
and query SQL/JSON functions. All functions rely on type casts
2044+
available in PostgreSQL, but have some
2045+
implementation specifics that determines which types can be used
2046+
by a particular function. Type casts supported by
2047+
PostgreSQL out of the box are listed
2048+
in .
2049+
2050+
2051+
2052+
Constructor functions support json, jsonb,
2053+
bytea, 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 json.
2056+
2057+
20452058
2046-
The output clause is common for both constructor and query SQL/JSON functions.
2059+
For the JSON_VALUE function, you can use
2060+
json, jsonb, 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 json and jsonb
2063+
types will always work regardless of the type of the extracted value,
2064+
since SQL/JSON items are converted directly to json or
2065+
jsonb 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 SQL/JSON item returned by
2069+
JSON_VALUE is represented as text.
20472070
2071+
2072+
2073+
With the JSON_QUERY function, you can use any
2074+
return types. The implementation details are as follows:
2075+
2076+
2077+
2078+
For SQL/JSON items of a scalar SQL type, a cast from json
2079+
or jsonb 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 bytea return
2082+
type, the extracted SQL/JSON item is converted to the return type as
2083+
follows: convert_to(json[b]::text, 'UTF8').
2084+
If JSON_QUERY extracts an SQL/JSON string,
2085+
which is not wrapped using the WRAPPER clause,
2086+
and the OMIT QUOTES 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+
2090+
2091+
2092+
2093+
For SQL/JSON items of non-scalar SQL types (arrays, records, and
2094+
domains), the json[b]_populate_record() function
2095+
is used for conversion to the specified return type. This is a
2096+
PostgreSQL extension of the SQL/JSON standard.
2097+
2098+
2099+
2100+
By default, JSON_QUERY returns a string of the
2101+
json 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+
2105+
2106+
2107+
Type Casts Supported by <productname>PostgreSQL</productname>
2108+
2109+
2110+
2111+
SQL/JSON type
2112+
SQL Type
2113+
2114+
2115+
2116+
2117+
string
2118+
text
2119+
2120+
2121+
number
2122+
numeric
2123+
2124+
2125+
boolean
2126+
boolean
2127+
2128+
2129+
date
2130+
date
2131+
2132+
2133+
time
2134+
time
2135+
2136+
2137+
time with tz
2138+
timetz
2139+
2140+
2141+
timestamp
2142+
timestamp
2143+
2144+
2145+
timestamp with tz
2146+
timestamptz
2147+
2148+
2149+
2150+
2151+
2152+
2153+
The optional FORMAT clause is provided
2154+
to conform to the SQL/JSON standard and can only take the
2155+
json value as an argument.
2156+
PostgreSQL currently supports
2157+
only the UTF8 encoding.
2158+
20482159
20492160
20502161
@@ -2067,18 +2178,18 @@ FROM
20672178
20682179
The SQL/JSON query functions pass the provided path expression to
20692180
the path engine 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 WITH WRAPPER clause
20762187
or enclose the path expression in square brackets to ensure
20772188
the evaluation result is an array.
20782189
20792190

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

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

35473658
3548-

0 commit comments

Comments
 (0)