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

From Corey Huinker
Subject Re: implement CAST(expr AS type FORMAT 'template')
Date
Msg-id CADkLM=dGxwVCp3y8=LU+8hQUPw61JELx2jh02i729YCex8UqWg@mail.gmail.com
Whole thread
In response to Re: implement CAST(expr AS type FORMAT 'template')  (jian he <jian.universality@gmail.com>)
List pgsql-hackers


On Tue, Mar 31, 2026 at 3:48 AM jian he <jian.universality@gmail.com> wrote:
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/

Everything's passing, moving the tests out of citext, etc into cast.sql is good.

I think the next step it to bring each TODO and FIXME into the thread, and explain the factors that prevent you from being certain about what to do in those situations.

pgsql-hackers by date:

Previous
From: Nathan Bossart
Date:
Subject: Re: Fixes inconsistent behavior in vacuum when it processes multiple relations
Next
From: Lev Nikolaev
Date:
Subject: Re: [PATCH] analyze: move elevel calculation into do_analyze_rel()