I'm facing a strange behaviour when using a function to cast to composite type in Postgres 9.6.
I have declared a composite type "vector" as x,y,z - each of double precision as well as a cast as follows:
create type vector as(
x double precision,
y double precision,
z double precision
);
create cast (text as vector)
with function vector_from_text(text) as implicit;
Function vector_from_text looks like this:
create or replace function vector_from_text(text, out result vector) strict immutable language plpgsql as $$
declare
d double precision[];
begin
result := null;
if ($1 is null) then
return;
end if;
begin
with c1 as (
select $1::jsonb src
)
select row((src->>'x')::double precision, (src->>'y')::double precision, (src->>'z')::double precision)::vector
**into result** -- see below
from c1;
exception
when others then
d := translate($1, '()', '{}')::double precision[];
result := row(d[1], d[2], d[3])::vector;
end;
end$$
;
The function returns null on null, or a vector type for both input formats either a json-string like '{"x":0, "y":0, "z":0}' or a constructor expression like '(0,0,0)'.
The Problem:
For json-like inputs the functions returns the error
invalid input syntax for type double precision: "(0,0,0)"
as soon the select statement contains the line into result. If I remove this line or change the output variable from result to something of type text the functions works as expected.
Why is it not possible to assign an already to type vector casted value into a vector? Don't get it!