Re: BUG #14849: jsonb_build_object doesn't like VARIADIC calls very much

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Marko Tiikkaja <marko(at)joh(dot)to>
Cc: PostgreSQL mailing lists <pgsql-bugs(at)postgresql(dot)org>, andrew(dot)dunstan(at)2ndquadrant(dot)com
Subject: Re: BUG #14849: jsonb_build_object doesn't like VARIADIC calls very much
Date: 2017-10-12 04:42:54
Message-ID: CAB7nPqSDHmjE-6HS-RUufZMEc6qipBDNjL1HktcMagVn7pGOcw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

On Wed, Oct 11, 2017 at 11:00 AM, <marko(at)joh(dot)to> wrote:
> This query fails with an unreasonable error message:
>
> =# SELECT jsonb_build_object(VARIADIC '{a,b}'::text[]);
> ERROR: invalid number of arguments: object must be matched key value
> pairs
>
> jsonb_object(text[]) can be used instead in this case, so perhaps
> jsonb_build_object() could simply point to that one if a variadic call like
> this is made?

It looks like a good idea to do so for this code. It seems that nobody
has actually bothered testing those functions in more fancy ways than
the documentation shows... And I think that this is not the only
problem.

I looked as well at jsonb_build_array(), which also uses VARIADIC ANY,
being surprised by that:
=# SELECT jsonb_build_array(variadic '{a,b}'::text[]);
jsonb_build_array
-------------------
[["a", "b"]]
(1 row)
But it seems to me that when a variadic call is used, then ["a", "b"]
is the correct result, no?

The json_* equivalent functions are reacting similarly than the jsonb_* ones.

It is actually possible to make the difference between a variadic and
a non-variadic call by looking at funcvariadic in
fcinfo->flinfo->fn_expr. So my suggestion of a fix would be the
following:
- refactor jsonb_object with an _internal routine that gets called as
well for variadic calls of jsonb_build_object. Something equivalent
needs to be done for the json functions.
- for json[b]_build_array, let's check if the input is a variadic
call, then fill in an intermediate structure with all the array
values, which is used with the existing processing.

More regression tests are needed as well. Andrew, you worked on most
of those items, including 7e354ab9, what is your opinion on the
matter?
--
Michael

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message KES 2017-10-12 08:00:54 Fwd: [BUGS] BUG #14850: Implement optinal additinal parameter for 'justify' date/time function
Previous Message Tom Lane 2017-10-12 02:27:05 Re: Combination of ordered-set aggregate function terminates JDBC connection on PostgreSQL 9.6.5

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2017-10-12 04:59:53 Re: [COMMITTERS] pgsql: Add configure infrastructure to detect support for C99's restric
Previous Message Dilip Kumar 2017-10-12 04:28:25 Re: Proposal: Improve bitmap costing for lossy pages