Re: Custom type's modifiers

From: Marthin Laubscher <postgres(at)lobeshare(dot)co(dot)za>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Custom type's modifiers
Date: 2024-06-27 17:01:10
Message-ID: 90B7091A-1B2E-4331-BF0F-7AE40F7D52BE@lobeshare.co.za
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2024/06/27, 18:13, "David G. Johnston" <mailto:david(dot)g(dot)johnston(at)gmail(dot)com> wrote:
> A cast between two types is going to accept a concrete instance of the input type, in memory, as its argument and then produces a concrete instance of the output type, in memory, as output.  If the input data is serialized the constructor for the input type will handle deserialization.

I confess to some uncertainty whether the PostgreSQL specific x::y notation and the standards based CAST(x AS y) would both be addressed by creating a cast. What you’re saying means both forms engage the same code and defining a cast would cover the :: syntax as well. Thanks for that.

If my understanding is accurate, it means that even when both values are of MyType the CAST function would still be invoked so the type logic can determine how to handle (or reject) the cast. Cast would (obviously) require the target type modifiers as well, and the good news is that it’s already there as the second parameter of the function. So that’s the other function that receives the type modifier that I was missing. It’s starting to make plenty sense.

To summarise:
- The type modifiers, encoded by the TYPMOD_IN function are passed directly as parameters to:-
- the type's input function (parameter 3), and
- any "with function" cast where the target type has type modifiers.
- Regardless of the syntax used to invoke the cast, the same function will be called.
- Depending on what casts are defined, converting from the external string format to a value of MyType will be handled either by the input function or a cast function. By default (without any casts) only values recognised by input can be converted to MyType values.

-- Thanks for your time – Marthin Laubscher

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2024-06-27 17:05:25 Re: Custom type's modifiers
Previous Message Tom Lane 2024-06-27 16:59:37 Re: JIT causes core dump during error recovery