Re: PL/Perl Returned Array

Lists: pgsql-hackers
From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: PL/Perl Returned Array
Date: 2011-08-13 00:00:05
Message-ID: 6242D79C-0D5F-44B8-AD1A-0E1D556E733D@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hackers,

Given this script on 9.1beta3:

BEGIN;

CREATE EXTENSION plperl;

CREATE OR REPLACE FUNCTION wtf(
) RETURNS TEXT[] LANGUAGE plperl AS $$ return []; $$;

SELECT wtf() = '{}'::TEXT[];

ROLLBACK;

The output is:

BEGIN
CREATE EXTENSION
CREATE FUNCTION
?column?
----------
f
(1 row)

ROLLBACK

Why is that? If I save the values to TEXT[] columns, they are still not the same. But if I pg_dump them and then load them up again, they *are* the same. The dump looks like this:

CREATE TABLE gtf (
have text[],
want text[]
);

COPY gtf (have, want) FROM stdin;
{} {}
\.

Is PL/Perl doing something funky under the hood to array values it returns on 9.1?

Thanks,

David


From: Alex Hunsaker <badalex(at)gmail(dot)com>
To: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/Perl Returned Array
Date: 2011-08-13 01:17:25
Message-ID: CAFaPBrTmLWVrWxk4n4OrCYwbpMkTKckwTJdoxhajXWXV4N5KZQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Aug 12, 2011 at 18:00, David E. Wheeler <david(at)kineticode(dot)com> wrote:
> Hackers,
>
> Given this script on 9.1beta3:
>
>    BEGIN;
>
>    CREATE EXTENSION plperl;
>
>    CREATE OR REPLACE FUNCTION wtf(
>    ) RETURNS TEXT[] LANGUAGE plperl AS $$ return []; $$;
>
>    SELECT wtf() = '{}'::TEXT[];

> Why is that? If I save the values to TEXT[] columns, they are still not the same. But if I pg_dump them and then load them up again, they *are* the same. The dump looks like this:

Eek.

> Is PL/Perl doing something funky under the hood to array values it returns on 9.1?

Yeah, its not handling empty arrays very well (its calling
accumArrayResult which increments the number of elements even when we
are a zero length array).

This was rewritten in 9.1 as part of the array overhaul. Previously
returned arrays were converted into a string with the perl sub
encode_array_literal(), potgres would then convert that string into
the appropriate data type. Not only was that a tad inefficient, but it
failed to handle composite types nicely. In 9.1 you can pass in arrays
with composite types and the like, we figured it would be awfully nice
if you could return them as well :-)

Anyway, the attached patch fixes it for me. That is when we don't have
an array state, just return an empty array. (Also adds some
additional comments)

I thought about doing this right after we set dims[0] in
plperl_array_to_datum(), but that would fail to handle nested empty
multidim arrays like [[]]. As long as you can do that via
ARRAY[ARRAY[]]... I figure plperl should support it to.

Thanks for the report!

Attachment Content-Type Size
plperl_empty_array.patch text/x-patch 1.2 KB

From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Alex Hunsaker <badalex(at)gmail(dot)com>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/Perl Returned Array
Date: 2011-08-13 02:49:26
Message-ID: D2396E1E-B402-42EE-BE29-861997BEAE06@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Aug 12, 2011, at 6:17 PM, Alex Hunsaker wrote:

> Anyway, the attached patch fixes it for me. That is when we don't have
> an array state, just return an empty array. (Also adds some
> additional comments)

Fix confirmed, thank you!

+1 to getting this committed before the next beta/RC.

David


From: Darren Duncan <darren(at)darrenduncan(dot)net>
To: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/Perl Returned Array
Date: 2011-08-13 03:09:49
Message-ID: 4E45EAFD.7010402@darrenduncan.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

David E. Wheeler wrote:
> On Aug 12, 2011, at 6:17 PM, Alex Hunsaker wrote:
>> Anyway, the attached patch fixes it for me. That is when we don't have
>> an array state, just return an empty array. (Also adds some
>> additional comments)
>
> Fix confirmed, thank you!
>
> +1 to getting this committed before the next beta/RC.

Policy question. If this problem hadn't been detected until after 9.1 was
declared production, would it have been considered a bug to fix in 9.1.x or
would it have been delayed to 9.2 since that fix might be considered an
incompatibility? If the latter, then I'm really glad it was found now. --
Darren Duncan


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Darren Duncan <darren(at)darrenduncan(dot)net>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/Perl Returned Array
Date: 2011-08-13 08:15:20
Message-ID: 1313223320.9614.0.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On fre, 2011-08-12 at 20:09 -0700, Darren Duncan wrote:
> David E. Wheeler wrote:
> > On Aug 12, 2011, at 6:17 PM, Alex Hunsaker wrote:
> >> Anyway, the attached patch fixes it for me. That is when we don't have
> >> an array state, just return an empty array. (Also adds some
> >> additional comments)
> >
> > Fix confirmed, thank you!
> >
> > +1 to getting this committed before the next beta/RC.
>
> Policy question. If this problem hadn't been detected until after 9.1 was
> declared production, would it have been considered a bug to fix in 9.1.x or
> would it have been delayed to 9.2 since that fix might be considered an
> incompatibility?

Surely this would be a bug fix.


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Alex Hunsaker <badalex(at)gmail(dot)com>
Cc: "David E(dot) Wheeler" <david(at)kineticode(dot)com>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/Perl Returned Array
Date: 2011-08-17 16:06:25
Message-ID: 4E4BE701.2010003@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 08/12/2011 09:17 PM, Alex Hunsaker wrote:

[empty arrays returned are not handled correctly]

>
> Anyway, the attached patch fixes it for me. That is when we don't have
> an array state, just return an empty array. (Also adds some
> additional comments)

Applied, thanks.

cheers

andrew


From: "David E(dot) Wheeler" <david(at)kineticode(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Alex Hunsaker <badalex(at)gmail(dot)com>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/Perl Returned Array
Date: 2011-08-17 17:32:37
Message-ID: 0AE0B658-A0E5-48CA-81E0-51DB910D3037@kineticode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Aug 17, 2011, at 9:06 AM, Andrew Dunstan wrote:

> [empty arrays returned are not handled correctly]
>
>> Anyway, the attached patch fixes it for me. That is when we don't have
>> an array state, just return an empty array. (Also adds some
>> additional comments)
>
> Applied, thanks.

Awesome, thanks!

David


From: Alex Hunsaker <badalex(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: "David E(dot) Wheeler" <david(at)kineticode(dot)com>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PL/Perl Returned Array
Date: 2011-08-17 20:29:32
Message-ID: CAFaPBrR0ehA69oiU8Pc8G7hTFB+tox8i7myJOB9cdAJd4Lbghg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Aug 17, 2011 at 10:06, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
>
>
> On 08/12/2011 09:17 PM, Alex Hunsaker wrote:
>
> [empty arrays returned are not handled correctly]
>
>>
>> Anyway, the attached patch fixes it for me. That is when we don't have
>> an array state, just return an empty array.  (Also adds some
>> additional comments)
>
> Applied, thanks.

Thanks for picking this up.