diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 1fb9ae217f726a47a2f1433fdf6f2a993cf840fc..2d42b1e1743358cfcf9e096b44808696a0771848 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -10309,8 +10309,9 @@ table2-mapping <row> <entry><literal>-</literal></entry> <entry><type>integer</type></entry> - <entry>Delete the field or element with specified index (Negative - integers count from the end)</entry> + <entry>Delete the array element with specified index (Negative + 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> </row> <row> diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index f87ba77e3ebdb69de55cfdc7a67e34f57c9bb713..c14d3f73fca51aceb0e5a4858993d9225eb0edb7 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -3400,6 +3400,11 @@ jsonb_delete_idx(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_PARAMETER_VALUE), 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) PG_RETURN_JSONB(in); diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index 412bf97b83f0461fa84dcc8a3ec9a33506711f10..e6654d47158e2a17d01c2712bb7eca1e171b5de9 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -3031,54 +3031,6 @@ select '["a","b","c"]'::jsonb - -4; ["a", "b", "c"] (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]'); jsonb_set -------------------------------------------------------------------------- @@ -3192,12 +3144,8 @@ select '[]'::jsonb - 'a'; select '"a"'::jsonb - 1; -- error ERROR: cannot delete from scalar -select '{}'::jsonb - 1 ; - ?column? ----------- - {} -(1 row) - +select '{}'::jsonb - 1; -- error +ERROR: cannot delete from object using integer subscript select '[]'::jsonb - 1; ?column? ---------- diff --git a/src/test/regress/expected/jsonb_1.out b/src/test/regress/expected/jsonb_1.out index 4ead74b572672ad31da2fc2c181a86d3873c012b..0a1ec93456915f2a4d5b59435ef35578694a7d78 100644 --- a/src/test/regress/expected/jsonb_1.out +++ b/src/test/regress/expected/jsonb_1.out @@ -3031,54 +3031,6 @@ select '["a","b","c"]'::jsonb - -4; ["a", "b", "c"] (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]'); jsonb_set -------------------------------------------------------------------------- @@ -3192,12 +3144,8 @@ select '[]'::jsonb - 'a'; select '"a"'::jsonb - 1; -- error ERROR: cannot delete from scalar -select '{}'::jsonb - 1 ; - ?column? ----------- - {} -(1 row) - +select '{}'::jsonb - 1; -- error +ERROR: cannot delete from object using integer subscript select '[]'::jsonb - 1; ?column? ---------- diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql index 2abec221b43bd714415742b8e7765fd15d01663b..29c82a2a62cfea38db9b254ea29489cce66e1a7b 100644 --- a/src/test/regress/sql/jsonb.sql +++ b/src/test/regress/sql/jsonb.sql @@ -738,15 +738,6 @@ select '["a","b","c"]'::jsonb - -2; select '["a","b","c"]'::jsonb - -3; 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, '{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]'); @@ -775,7 +766,7 @@ select '"a"'::jsonb - 'a'; -- error select '{}'::jsonb - 'a'; select '[]'::jsonb - 'a'; select '"a"'::jsonb - 1; -- error -select '{}'::jsonb - 1 ; +select '{}'::jsonb - 1; -- error select '[]'::jsonb - 1; select '"a"'::jsonb - '{a}'::text[]; -- error select '{}'::jsonb - '{a}'::text[];