Thread: Help with plperl

Help with plperl

From
Alex
Date:
Hi,
could anyone help me with a problem I am having creating a plperl function.

The return always complains with the following error:
composite-returning Perl function must return reference to hash

Thanks
Alex

CREATE TYPE sometype AS
   ( jid           INTEGER,
     start_time     TEXT,
     descr        TEXT
   );

CREATE OR REPLACE FUNCTION testq() RETURNS sometype AS $$

  my $results = [];
  my $query = "SELECT jid, start_time FROM schedule LIMIT 10";

  $rv = spi_exec_query($query);
  elog(INFO, "QUERY: $query");
  elog(INFO, "ROWS: $rv->{processed}");

  for ( $i = 0; $i < $rv->{processed}; $i++ ) {
    my $row = $rv->{rows}[$i];
    push @$results, { jid        => $row->{jid},
                  start_time => $row->{start_time},
                      descr     => 'Test' };
  }
  return $results;

$$ LANGUAGE plperl;



Re: Help with plperl

From
Michael Fuhr
Date:
On Mon, Aug 22, 2005 at 03:58:25AM +1000, Alex wrote:
> could anyone help me with a problem I am having creating a plperl function.
>
> The return always complains with the following error:
> composite-returning Perl function must return reference to hash

The above error tells you what's wrong: PL/Perl expects you to
return a hash reference because the return type is "sometype"; the
code returns an array reference so you get an error.  Since you're
pushing hash references onto the array, I'm guessing you meant to
return "SETOF sometype".

--
Michael Fuhr

Re: Help with plperl

From
Martijn van Oosterhout
Date:
On Mon, Aug 22, 2005 at 03:58:25AM +1000, Alex wrote:
> Hi,
> could anyone help me with a problem I am having creating a plperl function.
>
> The return always complains with the following error:
> composite-returning Perl function must return reference to hash

Well, since you're returning a reference to an array, I imagine it's
complaining about that.

Looking at your function though, it appears to be building a set of
"sometype" rather than a single one. Perhaps you meant your function to
be defined as: RETURNS SETOF sometype.

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment