Re: implement CAST(expr AS type FORMAT 'template')

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>, Corey Huinker <corey(dot)huinker(at)gmail(dot)com>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>, Vik Fearing <vik(at)postgresfriends(dot)org>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: implement CAST(expr AS type FORMAT 'template')
Date: 2026-07-15 15:40:33
Message-ID: CACJufxF+BX8+zaiisqzg34QnBjH6kjAgu0GH+6gvvYpmPsMovA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Jul 15, 2026 at 10:44 PM Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
>
> On Wed, Jul 15, 2026 at 5:11 AM jian he <jian(dot)universality(at)gmail(dot)com> wrote:
> > src8=# select cast('2012-13-12' as date format 'YYYY-MM-DD' );
> > ERROR: date/time field value out of range: "2012-13-12"
> >
> > With this example, I hope I've convinced you that it's still worthwhile to have
> > the parser build a FuncExpr for CAST ... FORMAT, even though that same FuncExpr
> > could otherwise be produced by calling the to_* function directly.
>
> How does this show that? Why not just call the to_* function directly?
>

you are right.

select cast('2012-13-12' as date format 'YYYY-MM-DD' default null on
conversion error);

We can do the same with:

select cast(to_date('2012-13-12', 'YYYY-MM-DD') as date default null
on conversion error);

However, this seems a bit strange.
Since to_date already returns a DATE, it's unlikely that anyone would
explicitly cast a DATE to DATE.
We can document that this approach provides an error-safe way to
evaluate these to_* functions.
(After we refactored these to_* function to be error safe).

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-07-15 15:42:25 Re: analyze-in-stages post upgrade questions
Previous Message Daniel Gustafsson 2026-07-15 15:40:22 Re: blkreftable.c needs more sanity checks