Ivan <Ivan-Sun1@mail.ru> writes:
> CREATE DOMAIN test_domain
> AS varchar(64)
> NOT NULL;
> CREATE TYPE test_type AS
> ("Id" int4,
> "Data" test_domain);
> CREATE OR REPLACE FUNCTION union_test()
> RETURNS SETOF test_type AS
> $BODY$
> select 1 as "Id", 'string1'::test_domain as "Data"
> union all
> select 2 as "Id", 'string2'::test_domain as "Data"
> $BODY$
> LANGUAGE 'sql' VOLATILE;
> generates error message
> ERROR: return type mismatch in function declared to return test_type
> DETAIL: Final SELECT returns character varying instead of test_domain at column 2.
> CONTEXT: SQL function "union_test"
The reason this happens is that select_common_type() smashes all its
inputs down to base types. I'm a bit hesitant to change this behavior
without thinking about all the possible consequences. There are clearly
some cases where it's the right thing --- for instance, if the inputs
are two different domains over the same base type, selecting the base
type seems the most reasonable behavior. Also, at least some of the
routine's callers seem to be relying on the assumption that the result
won't be a domain type.
regards, tom lane