1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-21 12:05:57 +03:00

Desupport jsonb subscript deletion on objects

Supporting deletion of JSON pairs within jsonb objects using an
array-style integer subscript allowed for surprising outcomes.  This was
mostly due to the implementation-defined ordering of pairs within
objects for jsonb.

It also seems desirable to make jsonb integer subscript deletion
consistent with the 9.4 era general purpose integer subscripting
operator for jsonb (although that operator returns NULL when an object
is encountered, while we prefer here to throw an error).

Peter Geoghegan, following discussion on -hackers.
This commit is contained in:
Andrew Dunstan 2015-06-07 20:46:00 -04:00
parent d23a3a603b
commit b81c7b4098
5 changed files with 13 additions and 120 deletions

View File

@ -10309,8 +10309,9 @@ table2-mapping
<row> <row>
<entry><literal>-</literal></entry> <entry><literal>-</literal></entry>
<entry><type>integer</type></entry> <entry><type>integer</type></entry>
<entry>Delete the field or element with specified index (Negative <entry>Delete the array element with specified index (Negative
integers count from the end)</entry> integers count from the end). Throws an error if top level
container is not an array.</entry>
<entry><literal>'["a", "b"]'::jsonb - 1 </literal></entry> <entry><literal>'["a", "b"]'::jsonb - 1 </literal></entry>
</row> </row>
<row> <row>

View File

@ -3400,6 +3400,11 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot delete from scalar"))); errmsg("cannot delete from scalar")));
if (JB_ROOT_IS_OBJECT(in))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("cannot delete from object using integer subscript")));
if (JB_ROOT_COUNT(in) == 0) if (JB_ROOT_COUNT(in) == 0)
PG_RETURN_JSONB(in); PG_RETURN_JSONB(in);

View File

@ -3031,54 +3031,6 @@ select '["a","b","c"]'::jsonb - -4;
["a", "b", "c"] ["a", "b", "c"]
(1 row) (1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 3;
?column?
--------------------------
{"a": 1, "b": 2, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 2;
?column?
------------------
{"a": 1, "b": 2}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 1;
?column?
------------------
{"a": 1, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 0;
?column?
------------------
{"b": 2, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -1;
?column?
------------------
{"a": 1, "b": 2}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -2;
?column?
------------------
{"a": 1, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -3;
?column?
------------------
{"b": 2, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -4;
?column?
--------------------------
{"a": 1, "b": 2, "c": 3}
(1 row)
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]'); select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
jsonb_set jsonb_set
-------------------------------------------------------------------------- --------------------------------------------------------------------------
@ -3192,12 +3144,8 @@ select '[]'::jsonb - 'a';
select '"a"'::jsonb - 1; -- error select '"a"'::jsonb - 1; -- error
ERROR: cannot delete from scalar ERROR: cannot delete from scalar
select '{}'::jsonb - 1 ; select '{}'::jsonb - 1; -- error
?column? ERROR: cannot delete from object using integer subscript
----------
{}
(1 row)
select '[]'::jsonb - 1; select '[]'::jsonb - 1;
?column? ?column?
---------- ----------

View File

@ -3031,54 +3031,6 @@ select '["a","b","c"]'::jsonb - -4;
["a", "b", "c"] ["a", "b", "c"]
(1 row) (1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 3;
?column?
--------------------------
{"a": 1, "b": 2, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 2;
?column?
------------------
{"a": 1, "b": 2}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 1;
?column?
------------------
{"a": 1, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - 0;
?column?
------------------
{"b": 2, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -1;
?column?
------------------
{"a": 1, "b": 2}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -2;
?column?
------------------
{"a": 1, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -3;
?column?
------------------
{"b": 2, "c": 3}
(1 row)
select '{"a":1, "b":2, "c":3}'::jsonb - -4;
?column?
--------------------------
{"a": 1, "b": 2, "c": 3}
(1 row)
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]'); select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
jsonb_set jsonb_set
-------------------------------------------------------------------------- --------------------------------------------------------------------------
@ -3192,12 +3144,8 @@ select '[]'::jsonb - 'a';
select '"a"'::jsonb - 1; -- error select '"a"'::jsonb - 1; -- error
ERROR: cannot delete from scalar ERROR: cannot delete from scalar
select '{}'::jsonb - 1 ; select '{}'::jsonb - 1; -- error
?column? ERROR: cannot delete from object using integer subscript
----------
{}
(1 row)
select '[]'::jsonb - 1; select '[]'::jsonb - 1;
?column? ?column?
---------- ----------

View File

@ -738,15 +738,6 @@ select '["a","b","c"]'::jsonb - -2;
select '["a","b","c"]'::jsonb - -3; select '["a","b","c"]'::jsonb - -3;
select '["a","b","c"]'::jsonb - -4; select '["a","b","c"]'::jsonb - -4;
select '{"a":1, "b":2, "c":3}'::jsonb - 3;
select '{"a":1, "b":2, "c":3}'::jsonb - 2;
select '{"a":1, "b":2, "c":3}'::jsonb - 1;
select '{"a":1, "b":2, "c":3}'::jsonb - 0;
select '{"a":1, "b":2, "c":3}'::jsonb - -1;
select '{"a":1, "b":2, "c":3}'::jsonb - -2;
select '{"a":1, "b":2, "c":3}'::jsonb - -3;
select '{"a":1, "b":2, "c":3}'::jsonb - -4;
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]'); select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]'); select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]'); select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
@ -775,7 +766,7 @@ select '"a"'::jsonb - 'a'; -- error
select '{}'::jsonb - 'a'; select '{}'::jsonb - 'a';
select '[]'::jsonb - 'a'; select '[]'::jsonb - 'a';
select '"a"'::jsonb - 1; -- error select '"a"'::jsonb - 1; -- error
select '{}'::jsonb - 1 ; select '{}'::jsonb - 1; -- error
select '[]'::jsonb - 1; select '[]'::jsonb - 1;
select '"a"'::jsonb - '{a}'::text[]; -- error select '"a"'::jsonb - '{a}'::text[]; -- error
select '{}'::jsonb - '{a}'::text[]; select '{}'::jsonb - '{a}'::text[];