problem with polymorphic functions and implicit casting - Mailing list pgsql-hackers

From Pavel Stehule
Subject problem with polymorphic functions and implicit casting
Date
Msg-id 162867790905180509w3e7c8ac3w39c4a5a084aa173b@mail.gmail.com
Whole thread Raw
List 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


pgsql-hackers by date:

Previous
From: Itagaki Takahiro
Date:
Subject: pg_restore --clean vs. large object
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Fixing the libxml memory allocation situation