Re: Have I found an interval arithmetic bug?

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Bryn Llewellyn <bryn(at)yugabyte(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Have I found an interval arithmetic bug?
Date: 2021-04-03 00:36:27
Message-ID: 20210403003627.GD29126@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Fri, Apr 2, 2021 at 07:47:32PM -0400, Bruce Momjian wrote:
> I have modified the patch to prevent partial months from creating
> partial hours/minutes/seconds, so the output is now at least consistent
> based on the three units:
>
> SELECT ('6.54321 years'::interval)::text as i;
> i
> ----------------
> 6 years 7 mons
>
> SELECT ('6.54321 months'::interval)::text as i;
> i
> ----------------
> 6 mons 16 days
>
> SELECT ('876.54321 days'::interval)::text as i;
> i
> -----------------------
> 876 days 13:02:13.344
>
> Partial years keeps it in months, partial months takes it to days, and
> partial days take it to hours/minutes/seconds. This seems like an
> improvement.
>
> This also changes the regression test output, I think for the better:
>
> SELECT INTERVAL '1.5 weeks';
> i
> ------------------
> - 10 days 12:00:00
> + 10 days
>
> The new output is less precise, but probably closer to what the user
> wanted.

Thinking some more about this, the connection between months and days is
very inaccurate, 30 days/month, but the connection between days and
hours/minutes/seconds is pretty accurate, except for leap days.
Therefore, returning "10 days 12:00:00" is in many ways better, but
returning hours/minutes/seconds for fractional months is very arbitrary
and suggests an accuracy that doesn't exist. However, I am afraid that
trying to enforce that distinction in the Postgres behavior would appear
very arbitrary, so what I did above is proabably the best I can do.
Here is another example of what we have:

SELECT INTERVAL '1.5 years';
interval
---------------
1 year 6 mons

SELECT INTERVAL '1.5 months';
interval
---------------
1 mon 15 days

SELECT INTERVAL '1.5 weeks';
interval
----------
10 days

SELECT INTERVAL '1.5 days';
interval
----------------
1 day 12:00:00

SELECT INTERVAL '1.5 hours';
interval
----------
01:30:00

--
Bruce Momjian <bruce(at)momjian(dot)us> https://momjian.us
EDB https://enterprisedb.com

If only the physical world exists, free will is an illusion.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Benedict Holland 2021-04-03 00:46:33 Re: How to install PostgreSQL binaries on a different directory than the default one
Previous Message rob stone 2021-04-03 00:25:29 Re: How to install PostgreSQL binaries on a different directory than the default one

Browse pgsql-hackers by date

  From Date Subject
Next Message Bryn Llewellyn 2021-04-03 00:50:59 Re: Have I found an interval arithmetic bug?
Previous Message Zhihong Yu 2021-04-03 00:07:27 Re: Have I found an interval arithmetic bug?