plperl better array support

Started by Andrew Dunstanover 21 years ago4 messagespatches
Jump to latest
#1Andrew Dunstan
andrew@dunslane.net

The attached patch (submitted for comment) is somewhat adapted from one
submitted last October. This allows returning a perl array where a
postgres array is expected.

example:

andrew=# create function blurfl() returns text[] language plperl as $$
andrew$# return ['a','b','c','a"b\c'];
andrew$# $$;
CREATE FUNCTION
andrew=# select blurfl();
blurfl
-------------------
{a,b,c,"a\"b\\c"}

Unlike the patch from October, this patch does not implement ANYARRAY or
ANYELEMENT pseudotypes. However it does escape/quote array elements
where necessary. It also preserves the old behaviour (if the plperl
function returns a string it is just passed through).

I'm not happy about constructing a string which we then parse out again
into an array - that strikes me as quite inefficient. (And there are
other inelegancies that I'd like to get rid of.) Much better would be to
use some array contruction calls directly - any pointers on how to do
that would be apprciated :-)

cheers

andrew

Attachments:

plperl-array.patchtext/x-patch; name=plperl-array.patchDownload+108-25
#2David Fetter
david@fetter.org
In reply to: Andrew Dunstan (#1)
Re: plperl better array support

On Mon, Jun 20, 2005 at 05:06:32AM -0400, Andrew Dunstan wrote:

The attached patch (submitted for comment) is somewhat adapted from one
submitted last October. This allows returning a perl array where a
postgres array is expected.

example:

andrew=# create function blurfl() returns text[] language plperl as $$
andrew$# return ['a','b','c','a"b\c'];
andrew$# $$;
CREATE FUNCTION
andrew=# select blurfl();
blurfl
-------------------
{a,b,c,"a\"b\\c"}

Unlike the patch from October, this patch does not implement ANYARRAY or
ANYELEMENT pseudotypes. However it does escape/quote array elements
where necessary. It also preserves the old behaviour (if the plperl
function returns a string it is just passed through).

I'm not happy about constructing a string which we then parse out again
into an array - that strikes me as quite inefficient. (And there are
other inelegancies that I'd like to get rid of.) Much better would be to
use some array contruction calls directly - any pointers on how to do
that would be apprciated :-)

Here's some pointers I'm qualified to point out, for what that's worth. ;)

In src/backend/utils/adt/arrayfuncs.c there are direct
array-manipulation functions. In the perlapi docs for perl, there are
ways to manipulate arrays directly. It should be possible to go
between perl arrays and postgresql arrays this way, except...perl
arrays can contain undef, which usually translates as NULL. Would it
be easier to add NULL support to postgresql arrays than to handle
undefs in perl arrays automagically?

Cheers,
D
--
David Fetter david@fetter.org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

#3Andrew Dunstan
andrew@dunslane.net
In reply to: David Fetter (#2)
Re: plperl better array support

David Fetter wrote:

In src/backend/utils/adt/arrayfuncs.c there are direct
array-manipulation functions.

And in other places - I had already found that stuff :-) . But a worked
example would help. If it's not available I'll muddle through some time.

In the perlapi docs for perl, there are
ways to manipulate arrays directly.

The patch is already using them. In fact, you can't delve even mildly
into plperl.c without having the perlapi docs close at hand.

It should be possible to go
between perl arrays and postgresql arrays this way, except...perl
arrays can contain undef, which usually translates as NULL. Would it
be easier to add NULL support to postgresql arrays than to handle
undefs in perl arrays automagically?

We should have NULL support in arrays. In the absence of that I'm not
particularly inclined to make special provsion for undef in an array.

cheers

andrew

#4Andrew Dunstan
andrew@dunslane.net
In reply to: Andrew Dunstan (#1)
Re: plperl better array support

I wrote:

The attached patch (submitted for comment) is somewhat adapted from
one submitted last October. This allows returning a perl array where a
postgres array is expected.

example:

andrew=# create function blurfl() returns text[] language plperl as $$
andrew$# return ['a','b','c','a"b\c'];
andrew$# $$;
CREATE FUNCTION
andrew=# select blurfl();
blurfl -------------------
{a,b,c,"a\"b\\c"}

Unlike the patch from October, this patch does not implement ANYARRAY
or ANYELEMENT pseudotypes. However it does escape/quote array elements
where necessary. It also preserves the old behaviour (if the plperl
function returns a string it is just passed through).

I'm not happy about constructing a string which we then parse out
again into an array - that strikes me as quite inefficient. (And there
are other inelegancies that I'd like to get rid of.) Much better would
be to use some array contruction calls directly - any pointers on how
to do that would be apprciated :-)

[snip]

I have been wondering whether a better approach might be to provide a
builtin plperl function that would construct the array string for us.
The downside is that the plperl programmer would have to be aware of it.
The upside would be that it would probably be faster (no massive calling
from C->perl) and it would take better account of things like binary
nulls, which we might otherwise barf on. They aren't mutually exclusive
- we could implement what I originally intended later on if we go with
this approach.

thoughts?

cheers

andrew