pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ondřej Bouda <obouda@email.cz>
To: Ken Tanzer <ken.tanzer@gmail.com>
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Largest & Smallest Functions
Date: Wed, 7 Nov 2018 23:46:46 +0100
Message-ID: <be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz> (raw)
In-Reply-To: <CAD3a31WVxY7ARuQ=c0t6dDfjskeHd1Ymv+fNyuz=xYaG4GCXnQ@mail.gmail.com>
References: <CAD3a31WVxY7ARuQ=c0t6dDfjskeHd1Ymv+fNyuz=xYaG4GCXnQ@mail.gmail.com>

Hi,

 > 2) Is there any particular reason functions like that aren't built
 > into Postgres?  They seem like they would be useful.  (Or maybe I
 > missed them?)

LEAST() and GREATEST() expressions do the same thing as yours smallest() 
and largest(). See 
https://www.postgresql.org/docs/current/functions-conditional.html

That might also answer the first question - just drop smallest() and 
largest() and you will get two functions instead of four :-)

Now to be a little more serious, if you want a single function to both 
support variadic number of arguments AND all of them in a single array, 
how could the function decide whether smallest(ARRAY[1,2,3]) shall 
return 1 or ARRAY[1,2,3] (which is the smallest out of all arguments)?
I would suggest not to declare such overloaded function even if it was 
possible, as it might confuse the reader easily. Instead, I would go for
SELECT min(u) FROM unnest(ARRAY[1,2,3]) u
or just define a separate least_array() / greatest_array() variant.

Regards,
Ondřej Bouda




view thread (13+ messages)  latest in thread

Message-ID: <be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz>
Permalink:  ../be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz/
Also on:    postgresql.org/message-id/be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: obouda@email.cz, ken.tanzer@gmail.com
  Subject: Re: Largest & Smallest Functions
  In-Reply-To: <be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz>
  References: <CAD3a31WVxY7ARuQ=c0t6dDfjskeHd1Ymv+fNyuz=xYaG4GCXnQ@mail.gmail.com> <be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz>

* 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