Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit41dd50e

Browse files
committed
Fix corner-case behaviors in JSON/JSONB field extraction operators.
Cause the path extraction operators to return their lefthand input,not NULL, if the path array has no elements. This seems more consistentsince the case ought to correspond to applying the simple extractionoperator (->) zero times.Cause other corner cases in field/element/path extraction to return NULLrather than failing. This behavior is arguably more useful than throwingan error, since it allows an expression index using these operators to bebuilt even when not all values in the column are suitable for theextraction being indexed. Moreover, we already had multipleinconsistencies between the path extraction operators and the simpleextraction operators, as well as inconsistencies between the JSON andJSONB code paths. Adopt a uniform rule of returning NULL rather thanthrowing an error when the JSON input does not have a structure thatpermits the request to be satisfied.Back-patch to 9.4. Update the release notes to list this as a behaviorchange since 9.3.
1 parentebf20f6 commit41dd50e

File tree

10 files changed

+1060
-369
lines changed

10 files changed

+1060
-369
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10152,10 +10152,14 @@ table2-mapping
1015210152
<note>
1015310153
<para>
1015410154
There are parallel variants of these operators for both the
10155-
<type>json</type> and <type>jsonb</type> types. The operators
10155+
<type>json</type> and <type>jsonb</type> types.
10156+
The field/element/path extraction operators
1015610157
return the same type as their left-hand input (either <type>json</type>
1015710158
or <type>jsonb</type>), except for those specified as
1015810159
returning <type>text</>, which coerce the value to text.
10160+
The field/element/path extraction operators return NULL, rather than
10161+
failing, if the JSON input does not have the right structure to match
10162+
the request; for example if no such element exists.
1015910163
</para>
1016010164
</note>
1016110165
<para>

‎doc/src/sgml/json.sgml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,6 @@ SELECT jdoc-&gt;'guid', jdoc-&gt;'name' FROM api WHERE jdoc -&gt; 'tags' ? 'qui'
415415
the <literal>"tags"</> key is common, defining an index like this
416416
may be worthwhile:
417417
<programlisting>
418-
-- Note that the "jsonb -&gt; text" operator can only be called on a JSON
419-
-- object, so as a consequence of creating this index the root of each
420-
-- "jdoc" value must be an object. This is enforced during insertion.
421418
CREATE INDEX idxgintags ON api USING gin ((jdoc -&gt; 'tags'));
422419
</programlisting>
423420
Now, the <literal>WHERE</> clause <literal>jdoc -&gt; 'tags' ? 'qui'</>

‎doc/src/sgml/release-9.4.sgml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@
136136
</para>
137137
</listitem>
138138

139+
<listitem>
140+
<para>
141+
The <link linkend="functions-json-op-table"><type>json</type>
142+
<literal>#&gt;</> <type>text[]</> path extraction operator</link> now
143+
returns its lefthand input, not NULL, if the array is empty (Tom Lane)
144+
</para>
145+
146+
<para>
147+
This is consistent with the notion that this represents zero
148+
applications of the simple field/element extraction
149+
operator <literal>-&gt;</>. Similarly, <type>json</type>
150+
<literal>#&gt;&gt;</> <type>text[]</> with an empty array merely
151+
coerces its lefthand input to text.
152+
</para>
153+
</listitem>
154+
155+
<listitem>
156+
<para>
157+
Corner cases in
158+
the <link linkend="functions-json-op-table"><type>JSON</type>
159+
field/element/path extraction operators</link> now return NULL rather
160+
than raising an error (Tom Lane)
161+
</para>
162+
163+
<para>
164+
For example, applying field extraction to a JSON array now yields NULL
165+
not an error. This is more consistent (since some comparable cases such
166+
as no-such-field already returned NULL), and it makes it safe to create
167+
expression indexes that use these operators, since they will now not
168+
throw errors for any valid JSON input.
169+
</para>
170+
</listitem>
171+
139172
<listitem>
140173
<para>
141174
Cause consecutive whitespace in <link

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp