Re: Largest & Smallest Functions

Поиск
Список
Период
Сортировка
От Ondřej Bouda
Тема Re: Largest & Smallest Functions
Дата
Msg-id be3b67fd-3854-bebb-c444-4ff8ea4d338a@email.cz
обсуждение исходный текст
Ответ на Largest & Smallest Functions  (Ken Tanzer <ken.tanzer@gmail.com>)
Ответы Re: Largest & Smallest Functions  (Ken Tanzer <ken.tanzer@gmail.com>)
Список 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


В списке pgsql-general по дате отправления:

Предыдущее
От: Ken Tanzer
Дата:
Сообщение: Largest & Smallest Functions
Следующее
От: Ken Tanzer
Дата:
Сообщение: Re: Largest & Smallest Functions