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

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Zsolt Parragi <zsolt(dot)parragi(at)percona(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-31 07:47:53
Message-ID: CACJufxH4ELUjdGNi7vkHEA6L=G0SfE5abnFs5vjvEm3G-Jqsbw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Mar 31, 2026 at 6:18 AM Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)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 Content-Type Size
v7-0001-CAST-expr-AS-type-FORMAT-template.patch text/x-patch 74.5 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-03-31 07:53:46 Re: Bug: wrong relname in RemoveSubscriptionRel() error detail
Previous Message SATYANARAYANA NARLAPURAM 2026-03-31 07:46:20 Re: POC: Parallel processing of indexes in autovacuum