From: | Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk> |
---|---|
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-08 06:10:50 |
Message-ID: | 87a7mkyuo7.fsf@news-spur.riddles.org.uk |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
>>>>> "Ken" == Ken Tanzer <ken(dot)tanzer(at)gmail(dot)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)
From | Date | Subject | |
---|---|---|---|
Next Message | Pavel Stehule | 2018-11-08 06:21:48 | Re: Largest & Smallest Functions |
Previous Message | Pavel Stehule | 2018-11-08 06:10:09 | Re: Largest & Smallest Functions |