Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ug69o-00Fuxv-26 for pgsql-hackers@arkaria.postgresql.org; Sun, 27 Jul 2025 18:31:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ug69l-0011Tj-5p for pgsql-hackers@arkaria.postgresql.org; Sun, 27 Jul 2025 18:31:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ug69k-0011Tb-NJ for pgsql-hackers@lists.postgresql.org; Sun, 27 Jul 2025 18:31:33 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1ug69h-0014nc-1A for pgsql-hackers@postgresql.org; Sun, 27 Jul 2025 18:31:30 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 1870A433B6; Sun, 27 Jul 2025 18:31:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=postgresfriends.org; s=gm1; t=1753641085; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tS+zR0iaaclnjhKTgo0gO/rEKy9WbKZIzq45b9IJohU=; b=lZIcuOIYlLYZsJrqRuG9peiY9CHdzIt3ScNENLlDRVIezyTsz0qf1RUlWNVtd3hMFOxHG0 6JDv8iLEK+AxcLd49O/1+qqj1Rx8TsWXawQEbBY2tnqYcON7hUf6TIqPeZowdU8ohtxycV uaHDS2L9+sX/yHIhajAIk2zItUjLtHCcMlApVpe+iQrPOkS+WR5hUpNHnVwRbG72GSM/X5 9mhhVo+kW3ol1ZQe+Yh1AQAKQaBBWwv592znYmH29ucu/PXq4tkYgRbY2TEgIcHqP2vunp RrxlGELwNbEdu98MfcMJ8PyMIwphYfSiEMPszBuM6gUrAvRpyxD56Y5bfPFHuw== Message-ID: <4c358bf8-87e8-4760-83d5-a6f9fcd88907@postgresfriends.org> Date: Sun, 27 Jul 2025 20:31:24 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: implement CAST(expr AS type FORMAT 'template') To: jian he , PostgreSQL-development References: Content-Language: en-US From: Vik Fearing In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-State: clean X-GND-Score: 0 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdeltddtudcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeggihhkucfhvggrrhhinhhguceovhhikhesphhoshhtghhrvghsfhhrihgvnhgushdrohhrgheqnecuggftrfgrthhtvghrnhepveeivdeileekveeghfeuheegkeffiefgueejgfeljeeiieevhefgfeetgfffheelnecukfhppedvrgdtvdemkeegvdekmeduudgvtdemvdejtddumegtudgskeemvdelkeehmegrrgeiieemsggutdefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepvdgrtddvmeekgedvkeemudduvgdtmedvjedtudemtgdusgekmedvleekheemrggrieeimegsugdtfedphhgvlhhopeglkffrggeimedvrgdtvdemkeegvdekmeduudgvtdemvdejtddumegtudgskeemvdelkeehmegrrgeiieemsggutdefngdpmhgrihhlfhhrohhmpehvihhksehpohhsthhgrhgvshhfrhhivghnughsrdhorhhgpdhnsggprhgtphhtthhopedvpdhrtghpthhtohepjhhirghnrdhunhhivhgvrhhsrghlihhthiesghhmrghilhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhhrggtkhgvrhhssehpohhsthhgrhgvshhqlhdrohhrgh X-GND-Sasl: vik@postgresfriends.org List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 27/07/2025 17:43, jian he wrote: > hi. > > while working on CAST(... DEFAULT ON ERROR), I came across link[1]. I don't > have access to the SQL standard, but based on the information in link[1], for > CAST(val AS type FORMAT 'template'), I make the as an A_Const > node in gram.y. Why does it have to be an A_const?  Shouldn't any a_expr work there? > so the attached patch is to implement > CAST > AS > [ FORMAT ] > This is correct syntax.  Thanks for working on it! > The implementation is pretty straightforward. > CAST(val AS type FORMAT 'template') > internally, it will be transformed into a FuncExpr node whose funcid > corresponds to > function name as one of (to_number, to_date, to_timestamp, to_char). > template as a Const node will make life easier. This doesn't seem very postgres-y to me.  Wouldn't it be better to add something like castformatfuncid to pg_cast?  That way any types that have that would just call that.  It would allow extensions to add formatted casting to their types, for example. > select proname, prosrc, proallargtypes, proargtypes, > prorettype::regtype, proargnames > from pg_proc > where proname in ('to_number', 'to_date', 'to_timestamp', 'to_char'); > > based on the query results, only a limited set of type casts are supported with > formatted casts. so error out early if the source or target type doesn't meet > these conditions. for example, if the source or target is a composite, array, > or polymorphic type. The standard is strict on what types can be cast to another, but I see no reason not to be more generic. -- Vik Fearing