Re: any, anyelement, and anyarray

From: Thomas Hallgren <thomas(at)tada(dot)se>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: any, anyelement, and anyarray
Date: 2006-05-12 14:29:06
Message-ID: 44649BB2.50005@tada.se
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Tom Lane wrote:
> Thomas Hallgren <thomas(at)tada(dot)se> writes:
>
>> - Why does PostgreSQL declare three different generic types? Isn't one
>> enough? ISTM it would be far simpler to use constructs like 'any' and
>> 'any[]' but neither of them are permitted.
>>
>
> "any" isn't the same as "anyelement", because it doesn't have the
> property of constraining different argument positions to be the same
> type. For instance, compare(any,any) and compare(anyelement,anyelement)
> would accept different sets of input types.
>
>
I've read that the "anyelement" will constrain all parameters and the
return type to be of the same type, or an array of that type. I
understand that concept since it will give the executor an ability to
infer the return type by looking at the parameters. If "any" doesn't do
that then I understand the difference.

I've been trying to use "any" with no luck. How do I declare a function
that takes an "any" as a parameter? I know how it would be implemented
internally but the SQL for it eludes me.

I realize that it would be trickier to return an "any" since the
expected return type must somehow be derived from the context where the
function was called. But similar things are done with "record" already,
right?

>> - Why can't I write 'anyelement[]'. Shouldn't that be the same thing as
>> 'anyarray'?
>>
>
> No, you're confusing these with actual datatypes. They are pseudotypes,
> which means they're only allowed as function argument/result type
> placeholders.
>
>
I understand that. But I'd consider it an implementation detail and I
think the average SQL user finds it a bit confusing. Wouldn't it be a
good idea to let the SQL parser recognize the anyelement[] construct as
a synonym for anyarray? I.e. allow me to write:

CREATE FUNCTION makeSingleElementArray(anyelement) RETURNS anyelement[]
AS ...

Regards,
Thomas Hallgren

PS. I'm happy to announce that PL/Java now handles records, domains,
user defined types, anyelement, and anyarray without problems.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2006-05-12 14:51:23 Re: Continuing "pgstat update timeout" failures in buildfarm
Previous Message Tom Lane 2006-05-12 13:57:52 Re: Compiling on 8.1.3 on Openserver 5.05