Re: Largest & Smallest Functions

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
Cc: ken(dot)tanzer(at)gmail(dot)com, "pgsql-general(at)postgresql(dot)org >> PG-General Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Largest & Smallest Functions
Date: 2018-11-08 06:21:48
Message-ID: CAFj8pRB-FsrYZ=BCjwzzf4-ui0TG1Zqbkd-iTi6LxL_bPZVA0Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

čt 8. 11. 2018 v 7:11 odesílatel Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
napsal:

> >>>>> "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
>

It is work with custom functions, not with buildin least, greatest

Regards

Pavel

> 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)
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ken Tanzer 2018-11-08 06:32:56 Re: Largest & Smallest Functions
Previous Message Andrew Gierth 2018-11-08 06:10:50 Re: Largest & Smallest Functions