Re: domains, case statements, functions: bug?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
Cc: Joe Van Dyk <joe(at)tanga(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: domains, case statements, functions: bug?
Date: 2013-07-09 01:58:14
Message-ID: 12597.1373335094@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com> writes:
> test=> create function f(t) returns m as $$ select case when true then
> $1.c end $$ language sql;
> ERROR: return type mismatch in function declared to return m
> DETAIL: Actual return type is numeric.

pg_typeof is somewhat helpful here:

regression=# select pg_typeof(t.c) from t;
pg_typeof
-----------
m
(1 row)

regression=# select pg_typeof(case when true then t.c end) from t;
pg_typeof
-----------
numeric
(1 row)

The reason for this is that CASE uses select_common_type() to infer the
output type, and select_common_type intentionally discriminates against
domain types. The comment therein says:

* If all input types are valid and exactly the same, just pick that type.
* This is the only way that we will resolve the result as being a domain
* type; otherwise domains are smashed to their base types for comparison.

So the way to get a CASE to return a domain type is to be sure you
provide an ELSE with the same result type:

regression=# select pg_typeof(case when true then t.c else null::m end) from t;
pg_typeof
-----------
m
(1 row)

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Marcin Mańk 2013-07-09 03:42:56 Re: Longest Common Subsequence in Postgres - Algorithm Challenge
Previous Message Adrian Klaver 2013-07-09 00:52:48 Re: domains, case statements, functions: bug?