Re: Anyone want to fix plperl for null array elements?

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Anyone want to fix plperl for null array elements?
Date: 2005-11-18 13:24:41
Message-ID: 437DD619.7000902@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Michael Fuhr wrote:

>On Thu, Nov 17, 2005 at 08:41:51PM -0500, Tom Lane wrote:
>
>
>>I think plperl should be fixed to translate undef to NULL when returning
>>an array, but currently it translates to an empty string:
>>
>>
>
>I'll take a look at this if nobody else steps up. It might just
>be a minor change to this part of plperl.c:
>
>210 " else " \
>211 " { " \
>212 " my $str = qq($elem); " \
>213 " $str =~ s/([\"\\\\])/\\\\$1/g; " \
>214 " $res .= qq(\"$str\"); " \
>215 " } " \
>
>
>

Yeah.

I have the fix below which I will apply shortly. Demo:

perltest=# CREATE OR REPLACE function returns_array() returns text[] as $$
perltest$# return ['a,b','c"d',undef,'e-f']; $$ LANGUAGE plperl;
CREATE FUNCTION
perltest=# select returns_array();
returns_array
------------------------
{"a,b","c\"d",NULL,e-f}

>>There might be some problems going in the other direction, too;
>>I haven't tried. Anybody feeling eager to fix this?
>>
>>
>
>Does the current implementation provide automatic conversion to a
>Perl array for inbound values? Unless I'm missing something that
>entire problem might still need to be solved.
>
>
>

Correct, we get the string representation, which is ugly. We certainly
want to change that for 8.2 so we get an arrayref. And this makes it
more urgent. demo:

perltest=# create or replace function dump_array(text[]) returns text
language plperlu as $$ use Data::Dumper; return Dumper(\(at)_); $$;
CREATE FUNCTION
perltest=# select dump_array(ARRAY(select datname from pg_database));
dump_array
---------------------------------------------------------------------------
$VAR1 = [
'{postgres,perltest,template1,template0}'
];

cheers

andrew

Index: plperl.c
===================================================================
RCS file: /cvsroot/pgsql/src/pl/plperl/plperl.c,v
retrieving revision 1.94
diff -c -r1.94 plperl.c
*** plperl.c 18 Oct 2005 17:13:14 -0000 1.94
--- plperl.c 18 Nov 2005 13:13:31 -0000
***************
*** 207,218 ****
" { " \
" $res .= _plperl_to_pg_array($elem); " \
" } " \
! " else " \
" { " \
" my $str = qq($elem); " \
" $str =~ s/([\"\\\\])/\\\\$1/g; " \
" $res .= qq(\"$str\"); " \
" } " \
" } " \
" return qq({$res}); " \
"} "
--- 207,222 ----
" { " \
" $res .= _plperl_to_pg_array($elem); " \
" } " \
! " elsif (defined($elem)) " \
" { " \
" my $str = qq($elem); " \
" $str =~ s/([\"\\\\])/\\\\$1/g; " \
" $res .= qq(\"$str\"); " \
" } " \
+ " else " \
+ " { "\
+ " $res .= 'NULL' ; " \
+ " } "\
" } " \
" return qq({$res}); " \
"} "

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexey Slynko 2005-11-18 13:44:26 Optimizer bug in 8.1.0?
Previous Message Andrew Dunstan 2005-11-18 13:04:21 Re: Optional postgres database not so optional in 8.1