Re: Extended unit

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alban Hertroys <alban(at)magproductions(dot)nl>, Pailloncy Jean-Gerard <jg(at)rilk(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Extended unit
Date: 2005-01-25 20:26:39
Message-ID: 20050125202639.GM29308@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jan 25, 2005 at 02:31:40PM -0500, Tom Lane wrote:
> AFAICS this could easily be implemented as a user-defined type, along
> the lines of
>
> CREATE TYPE measurement AS (value double, units text);
>
> and if you want to constrain a particular column to contain only one
> value of units, use CHECK.

I've tried this but I can't work out how to make it work. For composite
types you can't specify input and output functions. It's all record_in
but it's not working for me:

# CREATE TYPE measurement AS (value float, units text);
CREATE TYPE
# select '(5,a)'::measurement;
ERROR: Cannot cast type "unknown" to measurement
# select measurement(5,'a');
ERROR: Function measurement(integer, "unknown") does not exist
Unable to identify a function that satisfies the given argument types
You may need to add explicit typecasts
# select cast( (5,'a') as measurement);
ERROR: parser: parse error at or near "as" at character 22
# select cast( '5' as measurement);
ERROR: Cannot cast type "unknown" to measurement

This is 7.3 though, is it better in later versions? I can't find any
examples anywhere. Composite types don't seems to be used much.
However, it appears you could just update pg_type to change the
input/output functions...

> The argument that we should extend the type system for this would become
> a lot more credible if there were a widely-used extension in existence
> for it to prove that there's sufficient demand.

I guess it's mostly syntactic sugar, but it might normalize the
varchar(n) and timestamp(n) format.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ben 2005-01-25 20:29:07 Apparently I don't understand full outer joins....
Previous Message Martijn van Oosterhout 2005-01-25 19:50:02 Re: Recursive queries