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

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, 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 09:10:42
Message-ID: CACJufxGm7Byb+=k3EKTtMR2jdGWnR2p81T8xZHpNUBnK0WNwLQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Jul 9, 2026 at 3:18 PM Peter Eisentraut <peter(at)eisentraut(dot)org> wrote:
>
> On 04.07.26 03:09, jian he wrote:
> >> I don't really understand the purpose of this feature to begin with.
> >> You can already call the formatting functions directly. Does this cast
> >> syntax offer anything on top of that?
> >>
> >
> > https://www.postgresql.org/message-id/762ae707-7fdc-43d8-a77a-3a10d12ce21d%40postgresfriends.org
> >
> > After this implemented, we can begin fully supporting:
> >
> > <cast specification> ::=
> > CAST <left paren>
> > <cast operand> AS <cast target>
> > [ FORMAT <cast template> ]
> > [ <cast error behavior> ON CONVERSION ERROR ]
> > <right paren>
>
> The ON CONVERSION ERROR feature is independent of the FORMAT feature,
> isn't it?
>

Earlier in this thread, the concern was raised that this isn't "generic" enough:
CAST ... FORMAT is just letting the parser produce a FuncExpr, which makes the
whole patch more like a syntax sugar.

In response, people suggested making it more generic. So far we've tried: using
the data type's input/output functions, using the data type's
typformatin/typformatout functions, and introducing a new pg_formatter catalog.
But we haven't agreed on a concrete definition of what a sufficiently generic
and genuinely future-proof approach would look like.

What I'm saying is: even without that generic mechanism settled, having CAST ...
FORMAT producing a FuncExpr still has value on its own. See below.

The attached v12-0001 to v12-0003 is the same as in [1].
v12-0004 is added to demo that CAST(expr AS type FORMAT fmt) is useful
for more than just being syntactic sugar for the existing to_*
functions.
Note: v12-0004 is in a rough but functional state.
With v12-0004, we can now do:

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

(1 row)

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.

[1]: https://www.postgresql.org/message-id/CACJufxGPp23XfP_V9zk7cSDJ6DCZEz4Jo3BUPtPfueNAe%3DC2Hg%40mail.gmail.com

--
jian
https://www.enterprisedb.com/

Attachment Content-Type Size
v12-0002-introduce-pg_proc.proerrorsafe.nocfbot application/octet-stream 66.2 KB
v12-0004-CAST-expr-AS-type-FORMAT-template.nocfbot application/octet-stream 71.8 KB
v12-0003-CAST-expr-AS-newtype-DEFAULT-expr-ON-CONVERSION-ERROR.nocfbot application/octet-stream 149.1 KB
v12-0001-error-safe-for-casting-text-to-other-types-per-pg_cast.nocfbot application/octet-stream 9.3 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message vignesh C 2026-07-15 09:13:59 Re: sequencesync worker race with REFRESH SEQUENCES
Previous Message jian he 2026-07-15 09:06:32 Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions