Dear PostgreSQL developer.
Although it is allowed to create a cast for a domain, it seems that there
is no way to trigger it. You can find attached an sql script to illustrate
the issue with postgresql 8.1.3. The create cast and create domain
documentations do not seem to discuss this point.
ISTM that it is a pg bug. Indeed, either
(1) the create cast should be rejected if it is not allowed for domains.
or
(2) the function should be triggered by explicit casts to the domain.
Have a nice day,
--
Fabien.
DROP DOMAIN a_year CASCADE;
-- a simple domain
CREATE DOMAIN a_year AS INTEGER
CHECK (VALUE BETWEEN 1 AND 3000);
-- ok
SELECT 1::a_year;
SELECT CAST('2000' AS a_year);
-- fails as expected
SELECT 0::a_year;
CREATE FUNCTION date2year(DATE)
RETURNS a_year IMMUTABLE STRICT AS $$
SELECT EXTRACT(YEAR FROM $1)::a_year;
$$ LANGUAGE sql;
-- ok
SELECT date2year(CURRENT_DATE);
-- fails as expected
SELECT date2year(DATE '3001-01-01');
CREATE CAST (DATE AS a_year)
WITH FUNCTION date2year(DATE);
-- fails, I would expect 1970
SELECT (DATE '1970-03-20')::a_year;
-- fails, I would expect the current year
SELECT CURRENT_DATE::a_year;
SELECT CAST(CURRENT_DATE AS a_year);
---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?
http://archives.postgresql.org