1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Support JSON negative array subscripts everywhere

Previously, there was an inconsistency across json/jsonb operators that
operate on datums containing JSON arrays -- only some operators
supported negative array count-from-the-end subscripting.  Specifically,
only a new-to-9.5 jsonb deletion operator had support (the new "jsonb -
integer" operator).  This inconsistency seemed likely to be
counter-intuitive to users.  To fix, allow all places where the user can
supply an integer subscript to accept a negative subscript value,
including path-orientated operators and functions, as well as other
extraction operators.  This will need to be called out as an
incompatibility in the 9.5 release notes, since it's possible that users
are relying on certain established extraction operators changed here
yielding NULL in the event of a negative subscript.

For the json type, this requires adding a way of cheaply getting the
total JSON array element count ahead of time when parsing arrays with a
negative subscript involved, necessitating an ad-hoc lex and parse.
This is followed by a "conversion" from a negative subscript to its
equivalent positive-wise value using the count.  From there on, it's as
if a positive-wise value was originally provided.

Note that there is still a minor inconsistency here across jsonb
deletion operators.  Unlike the aforementioned new "-" deletion operator
that accepts an integer on its right hand side, the new "#-" path
orientated deletion variant does not throw an error when it appears like
an array subscript (input that could be recognized by as an integer
literal) is being used on an object, which is wrong-headed.  The reason
for not being stricter is that it could be the case that an object pair
happens to have a key value that looks like an integer; in general,
these two possibilities are impossible to differentiate with rhs path
text[] argument elements.  However, we still don't allow the "#-"
path-orientated deletion operator to perform array-style subscripting.
Rather, we just return the original left operand value in the event of a
negative subscript (which seems analogous to how the established
"jsonb/json #> text[]" path-orientated operator may yield NULL in the
event of an invalid subscript).

In passing, make SetArrayPath() stricter about not accepting cases where
there is trailing non-numeric garbage bytes rather than a clean NUL
byte.  This means, for example, that strings like "10e10" are now not
accepted as an array subscript of 10 by some new-to-9.5 path-orientated
jsonb operators (e.g. the new #- operator).  Finally, remove dead code
for jsonb subscript deletion; arguably, this should have been done in
commit b81c7b409.

Peter Geoghegan and Andrew Dunstan
This commit is contained in:
Andrew Dunstan
2015-07-17 20:56:13 -04:00
parent 9a5f369adc
commit 89ddd29bbd
10 changed files with 231 additions and 29 deletions

View File

@ -10177,7 +10177,8 @@ table2-mapping
<row>
<entry><literal>-&gt;</literal></entry>
<entry><type>int</type></entry>
<entry>Get JSON array element (indexed from zero)</entry>
<entry>Get JSON array element (indexed from zero, negative
integers count from the end)</entry>
<entry><literal>'[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json-&gt;2</literal></entry>
<entry><literal>{"c":"baz"}</literal></entry>
</row>
@ -10230,7 +10231,10 @@ table2-mapping
returning <type>text</>, which coerce the value to text.
The field/element/path extraction operators return NULL, rather than
failing, if the JSON input does not have the right structure to match
the request; for example if no such element exists.
the request; for example if no such element exists. The
field/element/path extraction operators that accept integer JSON
array subscripts all support negative subscripting from the end of
arrays.
</para>
</note>
<para>
@ -10318,7 +10322,8 @@ table2-mapping
<row>
<entry><literal>#-</literal></entry>
<entry><type>text[]</type></entry>
<entry>Delete the field or element with specified path</entry>
<entry>Delete the field or element with specified path (for
JSON arrays, negative integers count from the end)</entry>
<entry><literal>'["a", {"b":1}]'::jsonb #- '{1,b}'</literal></entry>
</row>
</tbody>
@ -10858,6 +10863,9 @@ table2-mapping
<replaceable>create_missing</replaceable> is true ( default is
<literal>true</>) and the item
designated by <replaceable>path</replaceable> does not exist.
As with the path orientated operators, negative integers that
appear in <replaceable>path</replaceable> count from the end
of JSON arrays.
</entry>
<entry><para><literal>jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)</literal>
</para><para><literal>jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]')</literal>
@ -10872,7 +10880,7 @@ table2-mapping
<entry><para><type>text</type></para></entry>
<entry>
Returns <replaceable>from_json</replaceable>
as indented json text.
as indented JSON text.
</entry>
<entry><literal>jsonb_pretty('[{"f1":1,"f2":null},2,null,3]')</literal></entry>
<entry>