Re: Proposal: Division operator for (interval / interval => double precision) - Mailing list pgsql-hackers

From David G. Johnston
Subject Re: Proposal: Division operator for (interval / interval => double precision)
Date
Msg-id CAKFQuwZyGzXH8OvoYwQB995vA7Xe4G5t0H-1ECPmLJAXO3xGvw@mail.gmail.com
Whole thread Raw
In response to Proposal: Division operator for (interval / interval => double precision)  (Gurjeet Singh <gurjeet@singh.im>)
List pgsql-hackers
On Sun, Jun 23, 2024 at 5:57 PM Gurjeet Singh <gurjeet@singh.im> wrote:
Is there a desire to have a division operator / that takes dividend
and divisor of types interval, and results in a quotient of type
double precision.
[...]
('365 days'::interval / '3 days'::interval) => 121
('365 days'::interval % '3 days'::interval) => 2


Is it double or biginteger that your operation is producing?

How about making the % operator output an interval?  What is the answer to:

'1 day 12 hours 59 min 10 sec' / '3 hours 22 min 6 sec'?

Though I'd rather add functions to produce numbers from intervals then let the existing math operations be used on those.  These seem independently useful though like this feature I've not really seen demand for them from others.

in_years(interval) -> numeric
in_days(interval) -> numeric
in_hours(interval) -> numeric
in_microseconds(interval) -> numeric
etc...

That said, implementing the inverse of the existing interval/double->interval operator has a nice symmetry.  Though the 4 examples are trivial, single unit, single scale, divisions, so exactly how that translates into support for a possibly messy example like above I'm uncertain.

There is no precedence, but why not add a new composite type, (whole bigint, remainder bigint) that, for you example #2, would be (121,2*24*60*60*1000*1000), the second field being 2 days in microseconds?  Possibly under a different operator so those who just want integer division can have it more cheaply and easily.

David J.

pgsql-hackers by date:

Previous
From: Alexander Lakhin
Date:
Subject: Buildfarm animal caiman showing a plperl test issue with newer Perl versions
Next
From: Li Japin
Date:
Subject: Re: Support "Right Semi Join" plan shapes