Casting from a domain

Started by Jim Nasbyabout 21 years ago4 messagesgeneral
Jump to latest
#1Jim Nasby
Jim.Nasby@BlueTreble.com

I'm trying to create a seconds domain that is an interval 'mashed to
seconds' as I once saw Tom describe it.

decibel=# create domain rrs.seconds as double precision;
CREATE DOMAIN
decibel=# create cast (double precision as rrs.seconds) WITHOUT FUNCTION AS IMPLICIT;
CREATE CAST
decibel=# create function rrs.interval_to_seconds(interval) returns seconds as 'SELECT extract( EPOCH FROM $1 );' LANGUAGE SQL;
CREATE FUNCTION
decibel=# create cast (interval as rrs.seconds) WITH FUNCTION rrs.interval_to_seconds(interval) AS IMPLICIT;
CREATE CAST
decibel=# select cast('1 month'::interval AS seconds);
ERROR: cannot cast type interval to seconds

\dC shows that the cast is there, and rrs.interval_to_seconds works as
expected, and according to \df does return seconds.

Version is 7.4.5.
--
Jim C. Nasby, Database Consultant decibel@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

#2Richard Huxton
dev@archonet.com
In reply to: Jim Nasby (#1)
Re: Casting from a domain

Jim C. Nasby wrote:

decibel=# create cast (interval as rrs.seconds) WITH FUNCTION rrs.interval_to_seconds(interval) AS IMPLICIT;

^^^

decibel=# select cast('1 month'::interval AS seconds);
ERROR: cannot cast type interval to seconds

It's not a schema issue is it? I.E. the lack of "rrs."?

--
Richard Huxton
Archonet Ltd

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jim Nasby (#1)
Re: Casting from a domain

"Jim C. Nasby" <decibel@decibel.org> writes:

decibel=# create cast (interval as rrs.seconds) WITH FUNCTION rrs.interval_to_seconds(interval) AS IMPLICIT;
CREATE CAST
decibel=# select cast('1 month'::interval AS seconds);
ERROR: cannot cast type interval to seconds

Given the current coercion rules, we should probably disallow attempts
to define casts that involve domains. Casts are on base types. The
down-cast from a domain to its base type is hardwired, and the up-cast
from base type to domain is too (with invocation of any constraints
that may apply). Adding random user-defined casts to this would
probably just create confusion and ambiguity. In particular, this
was already meaningless:

decibel=# create domain rrs.seconds as double precision;
CREATE DOMAIN
decibel=# create cast (double precision as rrs.seconds) WITHOUT FUNCTION AS IMPLICIT;
CREATE CAST

since the presence of the cast might be thought to justify coercing
floats to "seconds" without invoking domain constraints.

regards, tom lane

#4Jim Nasby
Jim.Nasby@BlueTreble.com
In reply to: Tom Lane (#3)
Re: Casting from a domain

On Fri, Mar 04, 2005 at 03:40:17PM -0500, Tom Lane wrote:

"Jim C. Nasby" <decibel@decibel.org> writes:

decibel=# create cast (interval as rrs.seconds) WITH FUNCTION rrs.interval_to_seconds(interval) AS IMPLICIT;
CREATE CAST
decibel=# select cast('1 month'::interval AS seconds);
ERROR: cannot cast type interval to seconds

Given the current coercion rules, we should probably disallow attempts
to define casts that involve domains. Casts are on base types. The
down-cast from a domain to its base type is hardwired, and the up-cast
from base type to domain is too (with invocation of any constraints
that may apply). Adding random user-defined casts to this would
probably just create confusion and ambiguity. In particular, this
was already meaningless:

decibel=# create domain rrs.seconds as double precision;
CREATE DOMAIN
decibel=# create cast (double precision as rrs.seconds) WITHOUT FUNCTION AS IMPLICIT;
CREATE CAST

since the presence of the cast might be thought to justify coercing
floats to "seconds" without invoking domain constraints.

Yeah, I figured it was probably an issue with it being a domain. I was
looking for a way to do this without the extra work involved in defining
a full-blown type. I guess the good news is I should be able to re-use
all the double-precision functions and what-not for 'seconds'.

BTW, is there a reason 'double' isn't an alias for 'double precision'?
I'm pretty sure every other database I've used (other than oracle of
course) supports 'double'. " precision" is just too much extra typing
after all... :P
--
Jim C. Nasby, Database Consultant decibel@decibel.org
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"