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

From: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
To: zengman <zengman(at)halodbtech(dot)com>, suryapoondla4(at)gmail(dot)com
Cc: jian he <jian(dot)universality(at)gmail(dot)com>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Vik Fearing <vik(at)postgresfriends(dot)org>
Subject: Re: implement CAST(expr AS type FORMAT 'template')
Date: 2026-03-23 20:20:14
Message-ID: CADkLM=fm35Nuf7++P815o+qHpa3WhHg=Fw=WzRS7NCrkRF2p5g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>
>
> Typos
> - parse_expr.c:2745: "formmatted" → "formatted"
> - parse_coerce.c:156: "Cocerce" → "Coerce"
>
> In `coerce_type_with_format()` (parse_coerce.c):
>
> 1. The check `if (fmtcategory != TYPCATEGORY_STRING && fmtcategory !=
> TYPCATEGORY_UNKNOWN)`
> could be moved earlier. It doesn't depend on any other
> calculations, so failing fast here would avoid unnecessary work.
>
> 2. consider using` list_make2(node, format)` instead of `list_make1() +
> lappend()`.
>
> --
> Regards,
> Man Zeng

Surya and I did a pair-review of this. In addition to the notes above
(which we agree with), we have the following notes:

+ if (inputBaseTypeId == targetBaseTypeId)
+ ereport(ERROR,
+ errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot cast type %s to %s while
using a format template",
+ format_type_be(inputBaseTypeId),
+
format_type_be(targetBaseTypeId)),
+ errdetail("binary coercible type cast is
not supported while using a format template"),
+ parser_coercion_errposition(pstate,
location, node));

This could use a bit more explanation in a comment - is it because there is
no plausible type that can take a FORMAT and be cast to itself?

+ if (s_typcategory != TYPCATEGORY_NUMERIC &&
+ s_typcategory != TYPCATEGORY_STRING &&
+ s_typcategory != TYPCATEGORY_DATETIME &&
+ s_typcategory != TYPCATEGORY_TIMESPAN)

In situations like this, the committers have shown a strong preference for
switch() statements. Though it may make more sense to package this if into
a static function is_formattable_type() or similar.

+ if (t_typcategory == TYPCATEGORY_STRING)
+ funcname = list_make2(makeString("pg_catalog"),
+
makeString("to_char"));
...
+ else
+ elog(ERROR, "failed to find conversion function from %s to
%s while using a format template",
+ format_type_be(inputTypeId),
format_type_be(targetTypeId));

Similar thing here, committers will probably want to see a switch.

+create function imm_const() returns text as $$ begin return 'YYYY-MM-DD';
end; $$ language plpgsql immutable;;
+select cast(col1 as date format imm_const()) from tcast;

double-;

Overall, the test coverage appears complete and the close mirroring of
existing tests makes the intention very clear.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-03-23 21:01:22 Re: another autovacuum scheduling thread
Previous Message Masahiko Sawada 2026-03-23 20:14:04 Re: Add tab completion for SERVER and CONNECTION keywords in psql