Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b81c7b4

Browse files
committed
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.
1 parent d23a3a6 commit b81c7b4

File tree

5 files changed

+13
-120
lines changed

5 files changed

+13
-120
lines changed

doc/src/sgml/func.sgml

+3-2
Original file line numberDiff line numberDiff line change
@@ -10309,8 +10309,9 @@ table2-mapping
1030910309
<row>
1031010310
<entry><literal>-</literal></entry>
1031110311
<entry><type>integer</type></entry>
10312-
<entry>Delete the field or element with specified index (Negative
10313-
integers count from the end)</entry>
10312+
<entry>Delete the array element with specified index (Negative
10313+
integers count from the end). Throws an error if top level
10314+
container is not an array.</entry>
1031410315
<entry><literal>'["a", "b"]'::jsonb - 1 </literal></entry>
1031510316
</row>
1031610317
<row>

src/backend/utils/adt/jsonfuncs.c

+5
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,11 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
34003400
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
34013401
errmsg("cannot delete from scalar")));
34023402

3403+
if (JB_ROOT_IS_OBJECT(in))
3404+
ereport(ERROR,
3405+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3406+
errmsg("cannot delete from object using integer subscript")));
3407+
34033408
if (JB_ROOT_COUNT(in) == 0)
34043409
PG_RETURN_JSONB(in);
34053410

src/test/regress/expected/jsonb.out

+2-54
Original file line numberDiff line numberDiff line change
@@ -3031,54 +3031,6 @@ select '["a","b","c"]'::jsonb - -4;
30313031
["a", "b", "c"]
30323032
(1 row)
30333033

3034-
select '{"a":1, "b":2, "c":3}'::jsonb - 3;
3035-
?column?
3036-
--------------------------
3037-
{"a": 1, "b": 2, "c": 3}
3038-
(1 row)
3039-
3040-
select '{"a":1, "b":2, "c":3}'::jsonb - 2;
3041-
?column?
3042-
------------------
3043-
{"a": 1, "b": 2}
3044-
(1 row)
3045-
3046-
select '{"a":1, "b":2, "c":3}'::jsonb - 1;
3047-
?column?
3048-
------------------
3049-
{"a": 1, "c": 3}
3050-
(1 row)
3051-
3052-
select '{"a":1, "b":2, "c":3}'::jsonb - 0;
3053-
?column?
3054-
------------------
3055-
{"b": 2, "c": 3}
3056-
(1 row)
3057-
3058-
select '{"a":1, "b":2, "c":3}'::jsonb - -1;
3059-
?column?
3060-
------------------
3061-
{"a": 1, "b": 2}
3062-
(1 row)
3063-
3064-
select '{"a":1, "b":2, "c":3}'::jsonb - -2;
3065-
?column?
3066-
------------------
3067-
{"a": 1, "c": 3}
3068-
(1 row)
3069-
3070-
select '{"a":1, "b":2, "c":3}'::jsonb - -3;
3071-
?column?
3072-
------------------
3073-
{"b": 2, "c": 3}
3074-
(1 row)
3075-
3076-
select '{"a":1, "b":2, "c":3}'::jsonb - -4;
3077-
?column?
3078-
--------------------------
3079-
{"a": 1, "b": 2, "c": 3}
3080-
(1 row)
3081-
30823034
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
30833035
jsonb_set
30843036
--------------------------------------------------------------------------
@@ -3192,12 +3144,8 @@ select '[]'::jsonb - 'a';
31923144

31933145
select '"a"'::jsonb - 1; -- error
31943146
ERROR: cannot delete from scalar
3195-
select '{}'::jsonb - 1 ;
3196-
?column?
3197-
----------
3198-
{}
3199-
(1 row)
3200-
3147+
select '{}'::jsonb - 1; -- error
3148+
ERROR: cannot delete from object using integer subscript
32013149
select '[]'::jsonb - 1;
32023150
?column?
32033151
----------

src/test/regress/expected/jsonb_1.out

+2-54
Original file line numberDiff line numberDiff line change
@@ -3031,54 +3031,6 @@ select '["a","b","c"]'::jsonb - -4;
30313031
["a", "b", "c"]
30323032
(1 row)
30333033

3034-
select '{"a":1, "b":2, "c":3}'::jsonb - 3;
3035-
?column?
3036-
--------------------------
3037-
{"a": 1, "b": 2, "c": 3}
3038-
(1 row)
3039-
3040-
select '{"a":1, "b":2, "c":3}'::jsonb - 2;
3041-
?column?
3042-
------------------
3043-
{"a": 1, "b": 2}
3044-
(1 row)
3045-
3046-
select '{"a":1, "b":2, "c":3}'::jsonb - 1;
3047-
?column?
3048-
------------------
3049-
{"a": 1, "c": 3}
3050-
(1 row)
3051-
3052-
select '{"a":1, "b":2, "c":3}'::jsonb - 0;
3053-
?column?
3054-
------------------
3055-
{"b": 2, "c": 3}
3056-
(1 row)
3057-
3058-
select '{"a":1, "b":2, "c":3}'::jsonb - -1;
3059-
?column?
3060-
------------------
3061-
{"a": 1, "b": 2}
3062-
(1 row)
3063-
3064-
select '{"a":1, "b":2, "c":3}'::jsonb - -2;
3065-
?column?
3066-
------------------
3067-
{"a": 1, "c": 3}
3068-
(1 row)
3069-
3070-
select '{"a":1, "b":2, "c":3}'::jsonb - -3;
3071-
?column?
3072-
------------------
3073-
{"b": 2, "c": 3}
3074-
(1 row)
3075-
3076-
select '{"a":1, "b":2, "c":3}'::jsonb - -4;
3077-
?column?
3078-
--------------------------
3079-
{"a": 1, "b": 2, "c": 3}
3080-
(1 row)
3081-
30823034
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
30833035
jsonb_set
30843036
--------------------------------------------------------------------------
@@ -3192,12 +3144,8 @@ select '[]'::jsonb - 'a';
31923144

31933145
select '"a"'::jsonb - 1; -- error
31943146
ERROR: cannot delete from scalar
3195-
select '{}'::jsonb - 1 ;
3196-
?column?
3197-
----------
3198-
{}
3199-
(1 row)
3200-
3147+
select '{}'::jsonb - 1; -- error
3148+
ERROR: cannot delete from object using integer subscript
32013149
select '[]'::jsonb - 1;
32023150
?column?
32033151
----------

src/test/regress/sql/jsonb.sql

+1-10
Original file line numberDiff line numberDiff line change
@@ -738,15 +738,6 @@ select '["a","b","c"]'::jsonb - -2;
738738
select '["a","b","c"]'::jsonb - -3;
739739
select '["a","b","c"]'::jsonb - -4;
740740

741-
select '{"a":1, "b":2, "c":3}'::jsonb - 3;
742-
select '{"a":1, "b":2, "c":3}'::jsonb - 2;
743-
select '{"a":1, "b":2, "c":3}'::jsonb - 1;
744-
select '{"a":1, "b":2, "c":3}'::jsonb - 0;
745-
select '{"a":1, "b":2, "c":3}'::jsonb - -1;
746-
select '{"a":1, "b":2, "c":3}'::jsonb - -2;
747-
select '{"a":1, "b":2, "c":3}'::jsonb - -3;
748-
select '{"a":1, "b":2, "c":3}'::jsonb - -4;
749-
750741
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
751742
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
752743
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
775766
select '{}'::jsonb - 'a';
776767
select '[]'::jsonb - 'a';
777768
select '"a"'::jsonb - 1; -- error
778-
select '{}'::jsonb - 1 ;
769+
select '{}'::jsonb - 1; -- error
779770
select '[]'::jsonb - 1;
780771
select '"a"'::jsonb - '{a}'::text[]; -- error
781772
select '{}'::jsonb - '{a}'::text[];

0 commit comments

Comments
 (0)