Re: implement CAST(expr AS type FORMAT 'template') - Mailing list pgsql-hackers

From jian he
Subject Re: implement CAST(expr AS type FORMAT 'template')
Date
Msg-id CACJufxH4ELUjdGNi7vkHEA6L=G0SfE5abnFs5vjvEm3G-Jqsbw@mail.gmail.com
Whole thread
In response to Re: implement CAST(expr AS type FORMAT 'template')  (Zsolt Parragi <zsolt.parragi@percona.com>)
Responses Re: implement CAST(expr AS type FORMAT 'template')
List pgsql-hackers
On Tue, Mar 31, 2026 at 6:18 AM Zsolt Parragi <zsolt.parragi@percona.com> wrote:
>
> 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?
>
I have removed this white spce.

> + 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?
>
yes. it should be -1.

> This one also shows a not so userfriendly error:
>
> postgres=# SELECT CAST('hello' AS name FORMAT 'test') ;
>
> SELECT CAST('hello' AS bpchar FORMAT 'test') ;

now:
SELECT CAST('hello' AS name FORMAT 'test') ;
ERROR:  cannot cast type text to name using formatted template

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') ;
               ^
DETAIL:  binary coercible type cast is not supported while using a
format template

type Text to type name is not binary coercible, see outpout of:

select *, castfunc::regproc from pg_cast
where castsource = 'text'::regtype and casttarget = 'name'::regtype;



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

Attachment

pgsql-hackers by date:

Previous
From: SATYANARAYANA NARLAPURAM
Date:
Subject: Re: POC: Parallel processing of indexes in autovacuum
Next
From: Chao Li
Date:
Subject: Re: Bug: wrong relname in RemoveSubscriptionRel() error detail