From: Bruce Momjian <bruce@momjian.us>
To: Zhihong Yu <zyu@yugabyte.com>
Cc: Bryn Llewellyn <bryn@yugabyte.com>
Cc: Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>
Cc: Dean Rasheed <dean.a.rasheed@gmail.com>
Cc: Daniel Gustafsson <daniel@yesql.se>
Cc: Justin Pryzby <pryzby@telsasoft.com>
Cc: Isaac Morland <isaac.morland@gmail.com>
Cc: John W Higgins <wishdev@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: Have I found an interval arithmetic bug?
Date: Fri, 23 Jul 2021 11:05:51 -0400
Message-ID: <20210723150551.GA8025@momjian.us> (raw)
In-Reply-To: <CALNJ-vQMtXJ325fjrXBXKkpN+jHQqYJssLjHeBaXNO5ENP4Odw@mail.gmail.com>
References: <20210721024834.GA8395@momjian.us>
<CAEZATCVMXYR4-GEr4UXzZVdS1CLwFweO7P=vZrepcMu6VrQMUQ@mail.gmail.com>
<677937.1626861539@sss.pgh.pa.us>
<CEA45CB6-7C0B-4960-9913-BD009DD04533@yugabyte.com>
<711398.1626888589@sss.pgh.pa.us>
<20210722000713.GA19630@momjian.us>
<654FEA5F-79CF-414A-9801-F1482EEBCD88@yugabyte.com>
<20210722014331.GB19630@momjian.us>
<CALNJ-vSDZOp_25LHYQ4A_U=szX7EyTcSiE0c2Hd2ZZwAszRa5Q@mail.gmail.com>
<CALNJ-vQMtXJ325fjrXBXKkpN+jHQqYJssLjHeBaXNO5ENP4Odw@mail.gmail.com>
On Thu, Jul 22, 2021 at 03:17:52PM -0700, Zhihong Yu wrote:
> On Thu, Jul 22, 2021 at 2:59 PM Zhihong Yu <zyu@yugabyte.com> wrote:
> Hi,
>
> - tm->tm_mon += (fval * MONTHS_PER_YEAR);
> + tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
>
> Should the handling for year use the same check as that for month ?
>
> - AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
> + /* round to a full month? */
> + if (rint(fval * DAYS_PER_MONTH) == DAYS_PER_MONTH)
>
> Cheers
>
> Hi,
> I guess the reason for current patch was that year to months conversion is
> accurate.
Our internal units are hours/days/seconds, so the spill _up_ from months
to years happens automatically:
SELECT INTERVAL '23.99 months';
interval
----------
2 years
> On the new test:
>
> +SELECT INTERVAL '1.16 months 01:00:00' AS "One mon 5 days one hour";
>
> 0.16 * 31 = 4.96 < 5
>
> I wonder why 5 days were chosen in the test output.
We use 30 days/month, not 31. However, I think you are missing the
changes in the patch and I am just understanding them fully now. There
are two big changes:
1. The amount of spill from months only to days
2. The _rounding_ of the result once we stop spilling at months or days
#2 is the part I think you missed.
One thing missing from my previous patch was the handling of negative
units, which is now handled properly in the attached patch:
SELECT INTERVAL '-1.99 years';
interval
----------
-2 years
SELECT INTERVAL '-1.99 months';
interval
----------
-2 mons
I ended up creating a function to handle this, which allowed me to
simplify some of the surrounding code.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
Attachments:
interval.diff.gz (application/gzip, 2.5K) download
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: bruce@momjian.us, zyu@yugabyte.com, bryn@yugabyte.com, tgl@sss.pgh.pa.us, dean.a.rasheed@gmail.com, daniel@yesql.se, pryzby@telsasoft.com, isaac.morland@gmail.com, wishdev@gmail.com, pgsql-hackers@postgresql.org
Subject: Re: Have I found an interval arithmetic bug?
In-Reply-To: <20210723150551.GA8025@momjian.us>
References: <20210721024834.GA8395@momjian.us> <CAEZATCVMXYR4-GEr4UXzZVdS1CLwFweO7P=vZrepcMu6VrQMUQ@mail.gmail.com> <677937.1626861539@sss.pgh.pa.us> <CEA45CB6-7C0B-4960-9913-BD009DD04533@yugabyte.com> <711398.1626888589@sss.pgh.pa.us> <20210722000713.GA19630@momjian.us> <654FEA5F-79CF-414A-9801-F1482EEBCD88@yugabyte.com> <20210722014331.GB19630@momjian.us> <CALNJ-vSDZOp_25LHYQ4A_U=szX7EyTcSiE0c2Hd2ZZwAszRa5Q@mail.gmail.com> <CALNJ-vQMtXJ325fjrXBXKkpN+jHQqYJssLjHeBaXNO5ENP4Odw@mail.gmail.com> <20210723150551.GA8025@momjian.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox