Re: Largest & Smallest Functions

From: Ondřej Bouda <obouda(at)email(dot)cz>
To: Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>
Cc: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Largest & Smallest Functions
Date: 2018-11-07 22:46:46
Message-ID: be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ken Tanzer 2018-11-07 23:19:12 Re: Largest & Smallest Functions
Previous Message Ken Tanzer 2018-11-07 21:37:09 Largest & Smallest Functions