Re: BUG #13936: jsonb_object() -> ERROR: unknown type of jsonb container

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Peter Geoghegan <pg(at)heroku(dot)com>
Cc: xtracoder(at)gmail(dot)com, pgsql-bugs <pgsql-bugs(at)postgresql(dot)org>, Andrew Dunstan <andrew(at)dunslane(dot)net>
Subject: Re: BUG #13936: jsonb_object() -> ERROR: unknown type of jsonb container
Date: 2016-02-09 11:57:13
Message-ID: CAB7nPqQEiWopsEHdarbn6FbaEG7QTzR9gU82G9Nrr-_yDEy-ZA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, Feb 9, 2016 at 10:45 AM, Peter Geoghegan <pg(at)heroku(dot)com> wrote:
> On Mon, Feb 8, 2016 at 11:52 AM, <xtracoder(at)gmail(dot)com> wrote:
>> Here is the simple code to reproduce:
>>
>> select jsonb_object('{}'::text[], '{}'::text[])
>>
>> Result of execution is
>>
>> ERROR: unknown type of jsonb container
>> ********** Error **********
>>
>> ERROR: unknown type of jsonb container
>> SQL state: XX000
>>
>> Expected result - jsonb object with no attributes, i.e.: '{}'
>
> I agree that this is a bug.
>
> Looks like it's coming from the jsonb output function, which is an
> additional concern:

Yep, this comes from a copy-paste error in jsonb_object_two_arg from
json_object_two_arg, caused by this bit particularly:
if (nkdims == 0)
PG_RETURN_DATUM(CStringGetTextDatum("{}"));
json is represented as an equivalent of a text data type, but that's
not the case of jsonb. So while this will work for json, that's really
broken for jsonb.

One way to address this issue is to call jsonb_from_cstring() when
nkdims == 0. Another method, a bit more complex, is to build an empty
object and then return it as a result, like more or less that for
example:
pushJsonbValue(&result.parseState, WJB_BEGIN_OBJECT, NULL);
result = pushJsonbValue(&result.parseState, WJB_END_OBJECT, NULL);
return result;

The patch attached uses jsonb_from_cstring(), with new regression
tests, and it fixes the issue for me. That seems more simple, but the
other method would work as well, though I am not sure this is worth
the complication.
Regards,
--
Michael

Attachment Content-Type Size
jsonb-objs-empty.patch text/x-patch 2.8 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2016-02-09 12:03:36 Re: BUG #13916: Only the install user can be defined in the new cluster
Previous Message Valeriy A. 2016-02-09 09:01:24 Re: BUG #13920: pg_try_advisory_xact_lock bigint trouble