Re: Proposal: is_castable - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Proposal: is_castable
Date
Msg-id 23154.1585923560@sss.pgh.pa.us
Whole thread Raw
In response to Proposal: is_castable  (Michał Wadas <michalwadas@gmail.com>)
Responses Re: Proposal: is_castable  (Adam Brusselback <adambrusselback@gmail.com>)
List pgsql-hackers
=?UTF-8?Q?Micha=C5=82_Wadas?= <michalwadas@gmail.com> writes:
> Currently there is no way to check if CAST will succeed.
> Therefore I propose adding new function: is_castable

> SELECT is_castable('foo' as time) // false

What would you actually do with it?

> Similar features are implemented in:
> - SQL Server (as TRY_CONVERT)
> - Oracle (as CONVERT([val] DEFAULT [expr] ON CONVERSION ERROR)

Somehow, I don't think those have the semantics of what you suggest here.

I suspect you are imagining that you could write something like

CASE WHEN is_castable(x as y) THEN cast(x as y) ELSE ...

but that will not work.  The THEN condition has to pass parse analysis
whether or not execution will ever reach it.

            regards, tom lane



pgsql-hackers by date:

Previous
From: Amit Kapila
Date:
Subject: Re: WAL usage calculation patch
Next
From: Robert Haas
Date:
Subject: where should I stick that backup?