Re: jsonb_set array append hack? - Mailing list pgsql-hackers

From Dmitry Dolgov
Subject Re: jsonb_set array append hack?
Date
Msg-id CA+q6zcWc9NOSvr=ZjjV8GAFhk3Awjz=McXVDTeVBeuEXHMdzbg@mail.gmail.com
Whole thread Raw
In response to Re: jsonb_set array append hack?  (Andrew Dunstan <andrew@dunslane.net>)
Responses Re: jsonb_set array append hack?  (Thom Brown <thom@linux.com>)
List pgsql-hackers
I'm sorry, but I'm not sure, what behavior is expected in this case?
Right now the following logic was implemented:
    "we trying to set an element inside an array, but we've got a non-integer path item
    ("nonsense" in this particular case), so we're going to add a new element at the end of array by default"

If it's wrong, should we refuse to perform such kind of operations, or should we replace
    "vehicle_type": ["car", "van"]
to
    "vehicle_type: {"nonsense": "motorcycle"}
?

On 15 September 2015 at 01:59, Andrew Dunstan <andrew@dunslane.net> wrote:


On 09/14/2015 01:29 PM, Thom Brown wrote:
Hi,

I've noticed that if you use a string for an element key in jsonb_set with create_missing set to true, you can use it to append to an array:

postgres=# SELECT jsonb_set(
'{"name": "Joe", "vehicle_types": ["car","van"]}'::jsonb,
           '{vehicle_types,nonsense}',
           '"motorcycle"', true);
jsonb_set
----------------------------------------------------------------
 {"name": "Joe", "vehicle_types": ["car", "van", "motorcycle"]}
(1 row)

What this really should match is a nested element inside "vehicle_types" called "nonsense".  But this seems to be a hack to get an element added to an array.  To do it properly currently requires specifying an arbitrary number in the hope that it will exceed the number of elements you have in the array.


That's a bug and we should fix it.



e.g.

postgres=# SELECT jsonb_set(
           '{"name": "Joe", "vehicle_types": ["car","van"]}'::jsonb,
           '{vehicle_types,100000}',
           '"motorcycle"', true);
jsonb_set
----------------------------------------------------------------
 {"name": "Joe", "vehicle_types": ["car", "van", "motorcycle"]}
(1 row)

But I'm guessing people shouldn't be relying on the hack in the first example.  Isn't this a bug?  If so, wouldn't this also be a bug?:

postgres=# SELECT jsonb_set(
           '{"name": "Joe", "vehicle_types": ["car","van"]}'::jsonb,
           array['vehicle_types',NULL],
           '"motorcycle"', true);



I think that's a bug too.

cheers

andrew



--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

pgsql-hackers by date:

Previous
From: Petr Jelinek
Date:
Subject: Re: creating extension including dependencies
Next
From: Thom Brown
Date:
Subject: Re: jsonb_set array append hack?