Re: Typmod for user-defined types

Lists: pgsql-hackers
From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Typmod for user-defined types
Date: 2005-08-27 11:18:50
Message-ID: 20050827111846.GB27443@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

[Please CC any replies, thanks]

Hi,

I've got a situation were I'd really like to be able to have a typmod
for a user-defined type. In particular, I'd like to make use of the
coerce_to_target_type()/coerce_type_typmod() chain. This only works if
you have a typmod != -1.

Even if you set the typmod in pg_type, when you create a table it's not
copied to the attribute but set back to -1.

This has been discussed before[1] and I notice Tom Lane posted a
message about this back in June last year. I was wondering if it would
be possible to allow user-defined types to declare a typmod function.

As an experiment I added arguments to GenericType but that gave
reduce/reduce conflicts. OTOH if you just add to SimpleTypename it just
works. I imagine this limits the places it can work.

Tom Lanes patch[2] looks like it may work, but would a mechanism to
allow user-defined types to have a typmod function be accepted?

Have a nice day,

[1] http://archives.postgresql.org/pgsql-hackers/2004-06/msg00923.php
[2] http://archives.postgresql.org/pgsql-hackers/2004-06/msg00932.php
--
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.


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Typmod for user-defined types
Date: 2005-08-27 13:52:05
Message-ID: 200508271352.j7RDq5j19606@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


We definitely need to allow user-types to use typmod. If we don't
support this, it needs to be a TODO. Added to TODO:

* Allow user-defined types to specify a type modifier at table creation
time

---------------------------------------------------------------------------

Martijn van Oosterhout wrote:
-- Start of PGP signed section.
> [Please CC any replies, thanks]
>
> Hi,
>
> I've got a situation were I'd really like to be able to have a typmod
> for a user-defined type. In particular, I'd like to make use of the
> coerce_to_target_type()/coerce_type_typmod() chain. This only works if
> you have a typmod != -1.
>
> Even if you set the typmod in pg_type, when you create a table it's not
> copied to the attribute but set back to -1.
>
> This has been discussed before[1] and I notice Tom Lane posted a
> message about this back in June last year. I was wondering if it would
> be possible to allow user-defined types to declare a typmod function.
>
> As an experiment I added arguments to GenericType but that gave
> reduce/reduce conflicts. OTOH if you just add to SimpleTypename it just
> works. I imagine this limits the places it can work.
>
> Tom Lanes patch[2] looks like it may work, but would a mechanism to
> allow user-defined types to have a typmod function be accepted?
>
> Have a nice day,
>
> [1] http://archives.postgresql.org/pgsql-hackers/2004-06/msg00923.php
> [2] http://archives.postgresql.org/pgsql-hackers/2004-06/msg00932.php
> --
> 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.
-- End of PGP section, PGP failed!

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Typmod for user-defined types
Date: 2005-08-27 15:09:09
Message-ID: 1433.1125155349@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:
> Tom Lanes patch[2] looks like it may work, but would a mechanism to
> allow user-defined types to have a typmod function be accepted?
> [2] http://archives.postgresql.org/pgsql-hackers/2004-06/msg00932.php

Well, the question still stands: are we going to paint ourselves into
a corner by requiring type and function names to be equivalent?

regards, tom lane


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Typmod for user-defined types
Date: 2005-08-28 13:38:35
Message-ID: 20050828133830.GB12498@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

AIUI, the issue is statements like:

# select numeric(6,3) '5.6';

Bison can't look far enough ahead to determine whether the numeric is a
type or a function. However, the desired grammer itself is not
ambgiuous, it's just that Bison is not smart enough to make it work. It
works now only because we hardwire the types that are allowed to have
argument.

Wouldn't a way around this be that to have grammer allow a type to look
like a function call but convert the FuncExpr node to a TypeName node
if the grammer determines it is a type after all. Something like:

SimpleTypename: type_name { $$ = $1 }
| func_expr { $$ = MakeTypeFromFuncCall( $1 ) }

That function could then check that only constant integer arguments have been
used, and then dispatch to a type specific function to turn the arg
list into a typmod. Among other things, this would allow us to produce
better error messages for things like bit(6,3) and numeric(l) instead
of just "syntax error".

I was thinking of adding to each type a typmod_in and a typmod_out
function. The first takes an array of integer in and returns an int32,
the latter does the reverse.

This is however quite a dramatic overhaul of the grammer and I'm not
sure it's actually conflict free. But "numeric", "float", "decimal",
etc would no longer need to be matched by the lexer and you could call
the timestamp function without double quotes.

At least in preliminary tests it seems OUT as a function name would be
a casualty due to the new IO/OUT parameters:

function ( field IN OUT type )

It can't determine soon enough that OUT can't be a type in this
context. And with this change type/function names would be the same
list. I wonder if there is a way around this.

Anyway, I'll try out some more stuff.

Have a nice day,

On Sat, Aug 27, 2005 at 11:09:09AM -0400, Tom Lane wrote:
> Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:
> > Tom Lanes patch[2] looks like it may work, but would a mechanism to
> > allow user-defined types to have a typmod function be accepted?
> > [2] http://archives.postgresql.org/pgsql-hackers/2004-06/msg00932.php
>
> Well, the question still stands: are we going to paint ourselves into
> a corner by requiring type and function names to be equivalent?
>
> regards, tom lane

--
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.