plpgsql function with more than one array argument

From: Andre Holzner <Andre(dot)Holzner(at)cern(dot)ch>
To: pgsql-sql(at)postgresql(dot)org
Subject: plpgsql function with more than one array argument
Date: 2002-01-28 23:52:21
Message-ID: 3C55E435.280AE10C@cern.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql

Hi,

thanks for reading this message.

I have a table (in a postgres database) looking like this:

Table "zdec_bhab"
Attribute | Type | Modifier
-----------+-----------+----------
run | bigint |
evt | bigint |
...
pcha | real[] |
...

where pcha is a 2D array, i.e. the first index can go from 1 to some
number and the second is 1..3.

Now, I'd like to create a plpgsql function taking as an argument
two vectors (arrays) from pcha:

CREATE FUNCTION mytest(real[],real[]) RETURNS real AS '
DECLARE
p1 ALIAS FOR $1;
p2 ALIAS FOR $2;
begin
-- RAISE NOTICE ''xxx %'',p2;
return p2[1][1];
end;' LANGUAGE 'plpgsql';

I do the following query:

select
pcha[1:1][1:3],pcha[2:2][1:3],mytest(pcha[1:1][1:3],pcha[2:2][1:3]) from
zdec_bhab where nch>=2;

which yields:
pcha |
pcha | mytest
---------------------------------------------+---------------------------------------------+--------
{{"-21.0788","35.0317","19.2111"}} |
{{"21.0605","-34.995","-19.2111"}} |

i.e. mytest seems to return something empty... however, If I uncomment
the RAISE NOTICE
line, I see the correct values (as in the output of the select
statement).

If I do

select
pcha[1:1][1:3],pcha[2:2][1:3],mytest(pcha[2:2][1:3],pcha[1:1][1:3]) from
zdec_bhab where nch>=2;

(i.e. the arguments of mytest exchanged), I get the correct values.

Am I doing something wrong or is this a 'feature' ?
(I'm using PostgreSQL 7.1.3 on i686-pc-linux-gnu, compiled by GCC
2.95.3).

Is it possible in plpgsql to have functions with more than
one array argument ? What about plperl ?

Or do I have to convert the 2D array into three 1D arrays pcha_x, pcha_y
and pcha_z ?

best regards & thanks for the help,

André

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bill Studenmund 2002-01-29 00:27:04 Re: RFD: schemas and different kinds of Postgres objects
Previous Message Trond Eivind =?iso-8859-1?q?Glomsr=F8d?= 2002-01-28 23:02:05 Re: PostgreSQL v7.2rc2 Released

Browse pgsql-sql by date

  From Date Subject
Next Message Dedov Anton 2002-01-29 03:45:02 Again about foreign keys and inheritance
Previous Message Allan Engelhardt 2002-01-28 21:37:14 Re: export/save meta data