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

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Vik Fearing <vik(at)postgresfriends(dot)org>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Corey Huinker <corey(dot)huinker(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: implement CAST(expr AS type FORMAT 'template')
Date: 2026-03-30 22:17:52
Message-ID: CAN4CZFNeCfLzt5g90vf_Dw5BKW2u-QhVKo369XbO-vXYq4ci5g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

v6 is better, but I found a few more questions:

+select pg_get_viewdef('tcast_v1', true);
+ pg_get_viewdef
+--------------------------------------------------------------------------------------------
+ SELECT CAST( col1 AS date FORMAT 'YYYY-MM-DD'::text) AS to_date,
+

Is that space after "CAST(" intentional?

+ format = coerce_to_target_type(pstate, fmt,
+ exprType(fmt), TEXTOID,
+ exprTypmod(fmt),
+ ccontext, cformat,
+ exprLocation(fmt));

Is exprTypmod(fmt) correct, shouldn't it use -1?

This one also shows a not so userfriendly error:

postgres=# SELECT CAST('hello' AS name FORMAT 'test') ;
2026-03-30 22:12:53.651 UTC [750980] ERROR: function
pg_catalog.to_char(text, text) does not exist
2026-03-30 22:12:53.651 UTC [750980] DETAIL: No function of that name
accepts the given argument types.
2026-03-30 22:12:53.651 UTC [750980] HINT: You might need to add
explicit type casts.
2026-03-30 22:12:53.651 UTC [750980] STATEMENT: SELECT CAST('hello'
AS name FORMAT 'test') ;
ERROR: function pg_catalog.to_char(text, text) does not exist
DETAIL: No function of that name accepts the given argument types.
HINT: You might need to add explicit type casts.

And varchar/bpchar adds details about binary coercible casts:

SELECT CAST('hello' AS bpchar FORMAT 'test') ;
2026-03-30 22:14:12.081 UTC [750980] ERROR: cannot cast type text to
character while using a format template at character 8
2026-03-30 22:14:12.081 UTC [750980] DETAIL: binary coercible type
cast is not supported while using a format template
2026-03-30 22:14:12.081 UTC [750980] STATEMENT: SELECT CAST('hello'
AS bpchar FORMAT 'test') ;
ERROR: cannot cast type text to character while using a format template
LINE 1: SELECT CAST('hello' AS bpchar FORMAT 'test') ;

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-03-30 22:37:09 Re: Fix race with LLVM and bison.
Previous Message Heikki Linnakangas 2026-03-30 22:02:52 Re: Shared hash table allocations