Re: pl/perl example in the doc no longer works in 9.1

From: Alex Hunsaker <badalex(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "David E(dot) Wheeler" <david(at)kineticode(dot)com>, Amit Khandekar <amit(dot)khandekar(at)enterprisedb(dot)com>, hackers(at)postgresql(dot)org, Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Alexey Klyukin <alexk(at)commandprompt(dot)com>
Subject: Re: pl/perl example in the doc no longer works in 9.1
Date: 2011-10-13 04:09:36
Message-ID: CAFaPBrSqw60Waa4rLzEO4vAC-sZfG=ZSSGZMyHOnAOJa298B3g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Oct 12, 2011 at 15:33, Alex Hunsaker <badalex(at)gmail(dot)com> wrote:
> On Wed, Oct 12, 2011 at 15:00, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

>> The core of the problem seems to be that if SvROK(sv) then
>> the code assumes that it must be intended to convert that to an array or
>> composite, no matter whether the declared result type of the function is
>> compatible with such a thing.
>
> Hrm, well 9.0 and below did not get this "right" either:
> create or replace function test_hash() returns text as $$ return
> {'a'=>1}; $$ language plperl;
> select test_array();
>      test_array
> -----------------------
>  ARRAY(0x7fd92384dcb8)
> (1 row)
>
> create or replace function test_hash() returns text as $$ return
> {'a'=>1}; $$ language plperl;
> select test_hash();
>      test_hash
> ----------------------
>  HASH(0x7fd92387f848)
> (1 row)
>

> Given the output above (both pre 9.1 and post) it seems unless the
> type is a set or composite we should throw an error. Maybe "PL/Perl
> function returning type %s must not return a reference" ?
>
>>  It would be more appropriate to drive the
>> cases off the nature of the function result type, perhaps.
>
> Ill see if I can cook up something that's not too invasive.

PFA my attempt at a fix.

This gets rid of of most of the if/else chain and the has_retval crap
in plperl_handl_func(). Instead we let plperl_sv_to_datum() do most of
the lifting. It also now handles VOIDOID and checks that the request
result oid can be converted from the perl structure. For example if
you passed in a hashref with a result oid that was not an rowtype it
will error out with "PL/Perl cannot convert hash to non rowtype %s".
Arrays behave similarly.

One side effect is you can now return a composite literal where you
could not before. ( We already let you return array literals )

The comments might still be a bit sparse-- Im hoping the added errors
make things a bit more self explanatory.

A large portion of the diff is added regression tests, testing what
happens when you return various references.

Comments?

Attachment Content-Type Size
plperl_returns.patch.gz application/x-gzip 3.4 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jun Ishiduka 2011-10-13 04:31:27 Re: Online base backup from the hot-standby
Previous Message Kyotaro HORIGUCHI 2011-10-13 03:45:31 Re: [v9.2] make_greater_string() does not return a string in some cases