Re: Why does to_json take "anyelement" rather than "any"?

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Mohamed Wael Khobalatte <mkhobalatte(at)grubhub(dot)com>, Nikhil Benesch <nikhil(dot)benesch(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Why does to_json take "anyelement" rather than "any"?
Date: 2020-11-06 05:25:26
Message-ID: CAFj8pRA7hiuXS7-nGeeZcLBsVwN3Tn7BewnyA+rrNzXwJH=xPg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

pá 6. 11. 2020 v 1:39 odesílatel David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> napsal:

> On Thu, Nov 5, 2020 at 3:43 PM Mohamed Wael Khobalatte <
> mkhobalatte(at)grubhub(dot)com> wrote:
>
>> You can always cast to text yourself, of course, but I am not familiar
>> with the type hierarchy enough to tell why `to_json` can't deduce that as
>> text whereas the other function can.
>>
>
> My understanding is that "any" is defined to accept that behavior -
> allowing any pseudo-type and unknown. The "anyelement" polymorphic
> pseudo-type is defined such that only concrete known types are allowed to
> match - and then the rules of polymorphism apply when performing a lookup.
> My uninformed conclusion is that since to_json only defines a single
> parameter that changing it from "anyelement" to "any" would be reasonable
> and the hack describe probably "just works" (though I'd test it on a
> wide-range of built-in types first if I was actually going to use the hack).
>
> You only get to use "any" for a C-language function but that is indeed the
> case here.
>

Type "anyelement" can force the function's result type directly. But there
cannot be function that returns UNKNOWN.

Type "any" just accept any argument without any impact on result type.
Unfortunately, inside a function is necessary to do much more work related
to casting types, and the execution can be slower.

I checked the source code of to_json and this function can use "any"
without any change.

Regards

Pavel

> David J.
>
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2020-11-06 05:36:18 Re: default result formats setting
Previous Message Peter Smith 2020-11-06 05:24:46 Re: extension patch of CREATE OR REPLACE TRIGGER