Re: Function accepting array of complex type

From: Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Function accepting array of complex type
Date: 2015-08-28 19:57:22
Message-ID: 55E0BD22.1060409@BlueTreble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 8/25/15 6:28 PM, Tom Lane wrote:
> Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jim Nasby 2015-08-28 20:02:58 Fwd: Core dump with nested CREATE TEMP TABLE
Previous Message Jim Nasby 2015-08-28 19:52:16 Re: psql - better support pipe line