problem with polymorphic functions and implicit casting

Поиск
Список
Период
Сортировка
От Pavel Stehule
Тема problem with polymorphic functions and implicit casting
Дата
Msg-id 162867790905180509w3e7c8ac3w39c4a5a084aa173b@mail.gmail.com
обсуждение исходный текст
Список 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 по дате отправления:

Предыдущее
От: Itagaki Takahiro
Дата:
Сообщение: pg_restore --clean vs. large object
Следующее
От: Martijn van Oosterhout
Дата:
Сообщение: Re: Fixing the libxml memory allocation situation