Re: CAST function for user defined type

From: Ron Peterson <ron(dot)peterson(at)yellowbank(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: CAST function for user defined type
Date: 2007-01-22 16:21:22
Message-ID: 20070122162122.GA19861@yellowbank.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Jan 22, 2007 at 04:36:20PM +0100, Martijn van Oosterhout wrote:
> On Mon, Jan 22, 2007 at 09:44:52AM -0500, Ron Peterson wrote:
> > I've created my own type: y_octet_16. Now I'm trying to create a CAST
> > function for this type, but I'm not quite getting it.
>
> Quick question: do you mean:
>
> > val
> > y_octet_16
> ^^^^^^^^^^
> > VALUES
> > ( 'aaa', encode( y_uuid_generate(), 'hex' )::y_byte_16 );
> or ^^^^^^^^^
>
> I'm not sure from your example of the difference? byte vs octet.

Phghtht, I got tripped up by my own pedantry. I'm using the specific
term 'octet' to refer to eight bits, rather than the colloquial 'byte',
but instinctively typed 'byte' in my cast. So that's just a typo.

That cleared one hurdle, but I'm still not there yet.

% select encode( y_uuid_generate(), 'hex' );
encode
----------------------------------
fe43d07c0c624786bebfcb3357a2a13a
(1 row)

% select 'fe43d07c0c624786bebfcb3357a2a13a'::y_octet_16;
y_octet_16
----------------------------------
fe43d07c0c624786bebfcb3357a2a13a

That works, but if I cast the output of 'encode' directly as below, my
cast function doesn't work. The error message indicates that it's not
seeing a hex string as input. When things are connected up this way,
what should the cast function expect to see as input?

% select encode( y_uuid_generate(), 'hex' )::y_octet_16;
ERROR: input string contains invalid characters

Best.

--
Ron Peterson
https://www.yellowbank.com/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-01-22 16:21:35 Re: Enable/Disable Triggers
Previous Message Tom Lane 2007-01-22 16:16:43 Re: Is there an equivalent of the W3c HTML checker for SQL?