Re: Re: PostgreSQL needs percentage function - Mailing list pgsql-general

From Peter J. Holzer
Subject Re: Re: PostgreSQL needs percentage function
Date
Msg-id 20171218193023.r42hiywc3cegedwy@hjp.at
Whole thread Raw
List pgsql-general
On 2017-12-18 18:13:58 +0200, Nick Dro wrote:
> I know how to implement this. It's not the issue.
> It's very easy to implement absolute value as well yet still PostgreSQL gives
> abs(x) function which is build in function.
> My claim is that if there is a build in function for absolute value why not for
> percentage? Both are very basic mathematical operations.

Apart from historic reasons, abs(x) is actually quite awkward to
write inline: You need to write x 3 times:

case when x >= 0 then x else -x end

Which is already quite a bit longer than abs(x), but it gets worse when
x is a complicated expression:

    abs((graph_width + graph_border_width) / pixels_per_character)
becomes
    case
        when ((graph_width + graph_border_width) / pixels_per_character) >= 0
        then ((graph_width + graph_border_width) / pixels_per_character)
        else -((graph_width + graph_border_width) / pixels_per_character)
    end

And the expression is evaluated twice, which is a problem if is contains
a function call with a side effekt. abs(random()*3-1) is not the same as
case random()*3-1 >= 0 then random()*3-1 else -(random()*3-1) end.

So while primitive, abs() is actually useful as a function.

percent(a, b) isn't even shorter than (a * b / 100), and it doesn't get
better for more complicated expressions. It' just less readable (does
percent(a, b) mean (a * b / 100) or (a / b * 100) or (b / a * 100)? Is
there even a factor of 100 involved? (Is should be, because that's what
"cent" means, but it's missing in percent_rank()). If such a function
existed, I would avoid it.

        hp

--
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp@hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>

Attachment

pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: reclaiming space from heavily used tables?
Next
From: "Peter J. Holzer"
Date:
Subject: Re: Re: PostgreSQL needs percentage function