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

From David G. Johnston
Subject Re: implement CAST(expr AS type FORMAT 'template')
Date
Msg-id CAKFQuwaG00tsXc2oSQwjdSnPQ=JuH+T8Oeejb3sO5TeqUHsfUQ@mail.gmail.com
Whole thread Raw
In response to Re: implement CAST(expr AS type FORMAT 'template')  (jian he <jian.universality@gmail.com>)
Responses Re: implement CAST(expr AS type FORMAT 'template')
List pgsql-hackers
On Sun, Aug 3, 2025 at 8:10 PM jian he <jian.universality@gmail.com> wrote:
hi.
one more question:

For binary coercible type casts, no formatted related function for it,
should we error out?
For example, should the following error out or return text '1'.

select cast('1'::text as text format 'YYYY'::text);

I'm hoping the standard says (or allows us to) error out here.

text as a type has no semantics on which to associate a format so it should be an error to attempt to do so.  Not a silent no-op.

I was under the impression that for format to be allowed in the expression one of the two data types involved has to be text and the other must not be text.

IME we are actually implementing a formatting option for text serialization and deserialization here, not a cast (we are just borrowing existing syntax that is serviceable).  Hence the absence of these entries in pg_cast and why the fit into pg_type seems so reasonable.

The existence of the various "to_char" and "to_date" functions reflects the historical lack of a dedicated syntax for this kind of (de-)serialization.  But it seems unwise to bias ourselves to how the new syntax/feature should be implemented just because these functions exist.  At least one design should be done pretending they don't and see what comes out of it.  Their code can always be moved or reused in whatever we come up with; forcing them to be used directly, as-is, within the new solution adds an unnecessary constraint.

David J.

pgsql-hackers by date:

Previous
From: Dilip Kumar
Date:
Subject: Re: Conflict detection for update_deleted in logical replication
Next
From: Tender Wang
Date:
Subject: Re: A little cosmetic to convert_VALUES_to_ANY()