X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 81E6E310C06 for ; Wed, 2 Jul 2003 01:42:51 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 67111-09 for ; Tue, 1 Jul 2003 22:42:41 -0300 (ADT) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id B371E310B91 for ; Tue, 1 Jul 2003 22:42:40 -0300 (ADT) Received: from [192.168.5.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.0.6) with ESMTP-TLS id 1816993; Tue, 01 Jul 2003 18:12:41 -0700 Message-ID: <3F02387A.4070401@joeconway.com> Date: Tue, 01 Jul 2003 18:42:18 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Josh Berkus Cc: Greg Stark , Stefan Bill , pgsql-sql@postgresql.org Subject: Re: LEAST and GREATEST functions? References: <20030630201057.14507.qmail@web13905.mail.yahoo.com> <87d6gu2otf.fsf@stark.dyndns.tv> <3F01ED9F.3060704@joeconway.com> <200307011704.26768.josh@agliodbs.com> In-Reply-To: <200307011704.26768.josh@agliodbs.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200307/29 X-Sequence-Number: 14143 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