Re: Add missing function abs (interval) - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Add missing function abs (interval)
Date
Msg-id 2631121.1632678161@sss.pgh.pa.us
Whole thread Raw
In response to Re: Add missing function abs (interval)  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Add missing function abs (interval)
List pgsql-hackers
I wrote:
> Isaac Morland <isaac.morland@gmail.com> writes:
>> I've attached a patch for this. Turns out there was a comment in the source
>> explaining that there is no interval_abs because it's not clear what to
>> return; but I think it's clear that if i is an interval the larger of i and
>> -i should be considered to be the absolute value, the same as would be done
>> for any type; essentially, if the type is orderable and has a meaningful
>> definition of unary minus, the definition of abs follows from those.

> The problem with that blithe summary is the hidden assumption that
> values that compare "equal" aren't interesting to distinguish.

After thinking about this some more, it seems to me that it's a lot
clearer that the definition of abs(interval) is forced by our comparison
rules if you define it as

    CASE WHEN x < '0'::interval THEN -x ELSE x END

In particular, this makes it clear what happens and why for values
that compare equal to zero.  The thing that is bothering me about
the formulation GREATEST(x, -x) is exactly that whether you get x
or -x in such a case depends on a probably-unspecified implementation
detail inside GREATEST().

BTW, you could implement this by something along the lines of
(cf generate_series_timestamp()):

        MemSet(&interval_zero, 0, sizeof(Interval));
        if (interval_cmp_internal(interval, &interval_zero) < 0)
            return interval_um(fcinfo);
        else
            PG_RETURN_INTERVAL_P(interval);

which would avoid the need to refactor interval_um().

            regards, tom lane



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Useless division(s) in interval_cmp_value
Next
From: Isaac Morland
Date:
Subject: Re: Add missing function abs (interval)