Re: How to check whether a data type can be cast to another

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kareem Sedki <isiscreation(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to check whether a data type can be cast to another
Date: 2009-10-23 14:00:08
Message-ID: 9888.1256306408@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Kareem Sedki <isiscreation(at)gmail(dot)com> writes:
> I am trying to write a function that takes one source and one target
> data type identifiers, each as a '/text/' variable, as arguments and
> return true if the cast can be made, false otherwise.

I think this is fundamentally the wrong approach, because it ignores the
question of whether a specific value can be cast. For example, there is
a cast from int4 to int2, but that doesn't mean that 999999::int2 will
succeed. What I think you should do is just try the cast and catch any
error that occurs.

If you really want to try to do what you're saying, look into
parser/parse_coerce.c. In particular, can_coerce_type() is the
authoritative answer.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Broersma 2009-10-23 14:40:12 pg 8.4 (Auto)-vacuumlo
Previous Message Merlin Moncure 2009-10-23 13:40:37 Re: How to check whether a data type can be cast to another