Re: Function proposal to find the type of a datum

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kate F <kate(at)cats(dot)meow(dot)at>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Function proposal to find the type of a datum
Date: 2007-02-02 08:06:19
Message-ID: 26980.1170403579@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Kate F <kate(at)cats(dot)meow(dot)at> writes:
> The difference between OF and this function is that this function is
> pulling the type from the datum, rather than explicitly testing it
> against types the user suggests. If I wanted to find the type of x
> using OF, I would have to check it for all types which interest me:

> IF a IS OF (INTEGER) THEN
> t := 'INTEGER';
> ELSE IF a IS OF (TEXT) THEN
> t := 'TEXT';
> ELSE IF a IS OF (REAL) THEN
> t := 'REAL';
> ...
> and so on. Versus:

> t := pg_type_of(a);

Well, but what are you going to do with "t" after that? AFAICS the
next step is going to be something like

IF t = 'integer'::regtype THEN
...
ELSE IF t = 'text'::regtype THEN
...
etc etc

So it seems to me that this is functionally about the same, except that
it exposes two implementation-dependent concepts (pg_type OIDs and
regtype) where the first exposes neither.

Your approach would help if there were a reason to pass "t" as a
variable to someplace not having access to "a", but I don't see a
very compelling use-case for that.

> Secondly, the semantics are different: OF yields the type the datum
> currently is; pg_type_of() (perhaps it should be renamed?) returns the
> most appropiate type to which the datum may be cast, if I understand
> get_fn_expr_argtype() correctly.

You don't, I think --- there's really no such thing as a "datum of type
ANYELEMENT", real datums always have some more-specific type. But my
question upthread was directed exactly to the point of how we should
interpret IS OF applied to a polymorphic function argument. It's at
least possible to argue that it's OK to interpret it the way you need.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeremy Drake 2007-02-02 08:15:15 Re: writing new regexp functions
Previous Message Kate F 2007-02-02 07:51:08 Re: Function proposal to find the type of a datum