Re: Largest & Smallest Functions

Поиск
Список
Период
Сортировка
От Andrew Gierth
Тема Re: Largest & Smallest Functions
Дата
Msg-id 87a7mkyuo7.fsf@news-spur.riddles.org.uk
обсуждение исходный текст
Ответ на Largest & Smallest Functions  (Ken Tanzer <ken.tanzer@gmail.com>)
Ответы Re: Largest & Smallest Functions  (Pavel Stehule <pavel.stehule@gmail.com>)
Re: Largest & Smallest Functions  (Ken Tanzer <ken.tanzer@gmail.com>)
Список pgsql-general
>>>>> "Ken" == Ken Tanzer <ken.tanzer@gmail.com> writes:

 Ken> Hi. Building on the [type]_larger and _smaller functions (and
 Ken> lifting from the documentation), I put together a couple of
 Ken> functions that will take any number of arguments:

 Ken> CREATE FUNCTION largest(VARIADIC anyarray) RETURNS anyelement AS
 Ken> $$ SELECT max($1[i]) FROM generate_subscripts($1, 1) g(i); $$
 Ken> LANGUAGE SQL IMMUTABLE;

 Ken> So far so good. I can do smallest(2,4,7), etc. But to take this a
 Ken> convenient step further, sometimes I want the smallest or largest
 Ken> from values already in an array. So I can create these functions:

But you don't need to create more functions, because you can do this:

select largest(variadic array[1,2,3]);
 largest 
---------
       3

 Ken> So here's my questions:

 Ken> 1) Is there any way to collapse those four functions into two? (Or
 Ken> is there a better way to go about this?)

See above

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

As already pointed out, greatest() and least() exist (though they were
added before VARIADIC was, so they don't use it)

 Ken> 3) Bonus question--how come all the existing _larger and _smaller
 Ken> functions are specific to each data type, as opposed to more
 Ken> general smaller/larger functions?

Because it saves looking up the type comparison function and doing an
indirect call.

-- 
Andrew (irc:RhodiumToad)


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

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