Re: BUG #7620: array_to_json doesn't support heterogeneous arrays

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
Cc: ghazel(at)gmail(dot)com, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #7620: array_to_json doesn't support heterogeneous arrays
Date: 2012-10-24 13:39:14
Message-ID: CAHyXU0ykN_fK5Sy7FXSJhj3W8sCRPMRYJKgEX8LK=hwfv_zXAw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, Oct 24, 2012 at 2:21 AM, Craig Ringer <ringerc(at)ringerc(dot)id(dot)au> wrote:
> On 10/24/2012 07:32 AM, ghazel(at)gmail(dot)com wrote:
>> The following bug has been logged on the website:
>>
>> Bug reference: 7620
>> Logged by: Greg Hazel
>> Email address: ghazel(at)gmail(dot)com
>> PostgreSQL version: 9.2.1
>> Operating system: Amazon Linux
>> Description:
>>
>> array_to_json(ARRAY['foo', 100, true]) complains because arrays can't have
>> mixed types, but json arrays can.
>
> The issue here isn't array_to_json, it's PostgreSQL arrays.
>
> What you appear to want is a way to call row_to_json so that it produces
> a json array instead of a json object as it currently does. That way you
> could pass it a ROW() construct, composite type, or record, and have it
> output a heterogeneous JSON array.
>
> This isn't a bug, but it's a perfectly reasonable feature request if
> re-interpreted a little. It will never work with PostgreSQL arrays,
> though, because the arrays themselves cannot contain mixed types:
>
> regress=# SELECT ARRAY[1,'test'];
> ERROR: invalid input syntax for integer: "test"
> LINE 1: SELECT ARRAY[1,'test'];
> ^
> Instead you want a way to take this:
>
> regress=# SELECT ROW(1,'test');
> row
> ----------
> (1,test)
> (1 row)
>
> and output the json:
>
> [1,"test"]
>
> instead of a json object:
>
> regress=# SELECT row_to_json(ROW(1,'test'));
> row_to_json
> ----------------------
> {"f1":1,"f2":"test"}
> (1 row)
>
>
> Would a version of `row_to_json` that output a json array satisfy your
> needs?

That's an interesting idea, but I'd like to see the OP make a
convincing case why the data must be returned as an array. In
javascript there isn't much difference...but maybe there's an
important point I'm missing.

merlin

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Merlin Moncure 2012-10-24 15:06:51 Introducing floating point cast into filter drastically changes row estimate
Previous Message Xiong He 2012-10-24 08:16:56 Re: BUG #7613: INTIDB