Re: LEAST and GREATEST functions?

From: Joe Conway <mail(at)joeconway(dot)com>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: Greg Stark <gsstark(at)mit(dot)edu>, Stefan Bill <sjb26(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: LEAST and GREATEST functions?
Date: 2003-07-02 01:42:18
Message-ID: 3F02387A.4070401@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Josh Berkus wrote:
>>create or replace function greatest(anyelement, anyelement) returns
>>anyelement as 'select case when $1 > $2 then $1 else $2 end' language
>>'sql';
>
> Way cool. I'd have to imagine that it would blow up if you did this, though:
>
> select greatest ( 512, now() );
>
> With an "Operator is not defined" error, hey?

It errors out with a type mismatch error:

regression=# select greatest (512, now());
ERROR: Function greatest(integer, timestamp with time zone) does not exist
Unable to identify a function that satisfies the given argument types
You may need to add explicit typecasts

Of course none of this is documented yet (because I still owe the
documentation ;-), but that can be done during feature freeze/beta), but
the concept of the anyelement data type is that, although it can mean
literally any data type, any arguments (or return type) so defined have
to match each other at function call time. So with:
greatest(anyelement, anyelement) returns anyelement
when it gets called, the two arguments *must* be the same data type, and
the function will return the same type. Any arguments declared with a
specific datatype (say integer) don't participate in the runtime
resolution of the polymorphic arguments.

Similarly there is an anyarray data type that is constrained at runtime
to be an array of anything that was defined as anyelement; e.g.:

create or replace function myelement(anyarray, int) returns anyelement
as 'select $1[$2]' language 'sql';

regression=# select myelement(array[11,22,33,44,55], 2);
myelement
-----------
22
(1 row)

Joe

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Joe Conway 2003-07-02 02:25:37 Re: passing a record as a function argument in pl/pgsql
Previous Message Josh Berkus 2003-07-02 00:04:26 Re: LEAST and GREATEST functions?