Re: Function accepting array of complex type - Mailing list pgsql-hackers

From Jim Nasby
Subject Re: Function accepting array of complex type
Date
Msg-id 55E0BD22.1060409@BlueTreble.com
Whole thread Raw
In response to Re: Function accepting array of complex type  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Function accepting array of complex type
List pgsql-hackers
On 8/25/15 6:28 PM, Tom Lane wrote:
> Jim Nasby <Jim.Nasby@BlueTreble.com> writes:
>> This works:
>> CREATE TYPE c AS (r float, i float);
>> CREATE FUNCTION mag(c c) RETURNS float LANGUAGE sql AS $$
>> SELECT sqrt(c.r^2 + c.i^2)
>> $$;
>> SELECT mag( (2.2, 2.2) );
>>          mag
>> ------------------
>>    3.11126983722081
>
>> But this doesn't:
>> CREATE FUNCTION magsum( c c[] ) RETURNS float LANGUAGE sql AS $$
>> SELECT sum(sqrt(c.r^2 + c.i^2)) FROM unnest(c) c
>> $$;
>> SELECT magsum( array[row(2.1, 2.1), row(2.2,2.2)] );
>> ERROR:  function magsum(record[]) does not exist at character 8
>
> You need to cast it to some specific record type:
>
> regression=# SELECT magsum( array[row(2.1, 2.1), row(2.2,2.2)]::c[] );

Right, I was wondering how hard it would be to improve that, but it's 
not clear to me where to look at in the code. Does the resolution happen 
as part of parsing, or is it further down the road?
-- 
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com



pgsql-hackers by date:

Previous
From: Jim Nasby
Date:
Subject: Re: psql - better support pipe line
Next
From: Jim Nasby
Date:
Subject: Fwd: Core dump with nested CREATE TEMP TABLE