problem with polymorphic functions and implicit casting

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: problem with polymorphic functions and implicit casting
Date: 2009-05-18 12:09:50
Message-ID: 162867790905180509w3e7c8ac3w39c4a5a084aa173b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I found following problem:

postgres=# create or replace function sum_items(anyarray) returns
anyelement as $$ select sum($1[i]) from
generate_series(array_lower($1,1), array_upper($1,1)) g(i)$$ language
sql immutable;
CREATE FUNCTION
Time: 1,983 ms
postgres=# select sum_items(array[1,2,3]);
ERROR: return type mismatch in function declared to return integer
DETAIL: Actual return type is bigint.
CONTEXT: SQL function "sum_items" during startup
postgres=#

so in this case we need explicit casting to result type - like

create or replace function sum_items(anyarray) returns anyelement as $$
select sum($1[i])::anyelement -- <<<<<<
from generate_series(array_lower($1,1), array_upper($1,1)) g(i)
$$ language sql immutable;

or some flag that ensure explicit casting.

Regards
Pavel Stehule

Browse pgsql-hackers by date

  From Date Subject
Next Message Martijn van Oosterhout 2009-05-18 14:45:52 Re: Fixing the libxml memory allocation situation
Previous Message Itagaki Takahiro 2009-05-18 08:10:13 pg_restore --clean vs. large object