Re: How do I create an array? - Mailing list pgsql-general

From Joe Conway
Subject Re: How do I create an array?
Date
Msg-id 3E41F7E0.7020003@joeconway.com
Whole thread Raw
In response to Re: How do I create an array?  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: How do I create an array?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Tom Lane wrote:
> Also, I'll bet that Joe Conway's upcoming plr makes it just as easy as pie
> (once you learn R, anyway).  But plain SQL and plpgsql don't really have
> much to fall back on to support such things.
>

You mean something like this?

create or replace function vec(float, float) returns _float8 as 'c(arg1,arg2)'
language 'plr';
select vec(1.23, 1.32);
      vec
-------------
  {1.23,1.32}
(1 row)

Actually, while I was at it I also wrote a C function called "array" which can
be declared to take as many arguments (to the max allowed) and return a
corresponding array. It is useful since R likes to work with arrays. E.g:

CREATE OR REPLACE FUNCTION array (float8, float8) RETURNS float8[] AS
'$libdir/plr','array' LANGUAGE 'C' WITH (isstrict);

regression=# select array(1.24,2.35);
     array
-------------
  {1.24,2.35}
(1 row)

CREATE OR REPLACE FUNCTION array (float8, float8, float8) RETURNS float8[] AS
'$libdir/plr','array' LANGUAGE 'C' WITH (isstrict);

regression=# select array(1.24,2.35,4.57);
       array
------------------
  {1.24,2.35,4.57}
(1 row)

I'm still working out the kinks with PL/R, and I've just started the
documentation, but it's pretty much feature complete, at least as far as the
first release goes.

Per previous discussion it won't be usable for trigger functions, and it
doesn't handle greater than 2d arrays as arguments or return types. But it
does allow scalar, 1d and 2d array, and composite type arguments. And it can
return scalar, 1d and 2d arrays, and composite types (i.e. supports table
functions).

I've been developing against cvs, but I tested today against 7.3.2 and it
passed its regression test. If you're feeling adventurous, let me know and
I'll send a copy directly.

Joe


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: databases limit
Next
From: jerome
Date:
Subject: Re: Backup scheme...