pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Josh Berkus <josh@agliodbs.com>
Cc: Greg Stark <gsstark@mit.edu>
Cc: Stefan Bill <sjb26@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: LEAST and GREATEST functions?
Date: Tue, 01 Jul 2003 18:42:18 -0700
Message-ID: <3F02387A.4070401@joeconway.com> (raw)
In-Reply-To: <200307011704.26768.josh@agliodbs.com>
References: <20030630201057.14507.qmail@web13905.mail.yahoo.com>
	<87d6gu2otf.fsf@stark.dyndns.tv>
	<3F01ED9F.3060704@joeconway.com>
	<200307011704.26768.josh@agliodbs.com>

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




view thread (14+ messages)  latest in thread

Message-ID: <3F02387A.4070401@joeconway.com>
Permalink:  ../3F02387A.4070401@joeconway.com/
Also on:    postgresql.org/message-id/3F02387A.4070401@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, josh@agliodbs.com, gsstark@mit.edu, sjb26@yahoo.com
  Subject: Re: LEAST and GREATEST functions?
  In-Reply-To: <3F02387A.4070401@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox