Re: Have I found an interval arithmetic bug?

From: Justin Pryzby <pryzby(at)telsasoft(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>, Bryn Llewellyn <bryn(at)yugabyte(dot)com>
Cc: Zhihong Yu <zyu(at)yugabyte(dot)com>, Isaac Morland <isaac(dot)morland(at)gmail(dot)com>, John W Higgins <wishdev(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Have I found an interval arithmetic bug?
Date: 2021-04-05 16:33:10
Message-ID: 20210405163310.GP6592@telsasoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Fri, Apr 02, 2021 at 10:21:26PM -0400, Bruce Momjian wrote:
> I wish I could figure out how to improve it any futher. What is odd is
> that I have never seen this reported as a problem before. I plan to
> apply this early next week for PG 14.

On Fri, Apr 02, 2021 at 01:05:42PM -0700, Bryn Llewellyn wrote:
> bruce(at)momjian(dot)us wrote:
> > Yes, looking at the code, it seems we only spill down to one unit, not more. I think we need to have a discussion if we want to change that.

If this is a bug, then there's no deadline - and if you're proposing a behavior
change, then I don't think it's a good time to begin the discussion.

> https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT
> « …field values can have fractional parts; for example '1.5 week' or '01:02:03.45'. Such input is converted to the appropriate number of months, days, and seconds for storage. When this would result in a fractional number of months or days, the fraction is added to the lower-order fields using the conversion factors 1 month = 30 days and 1 day = 24 hours. For example, '1.5 month' becomes 1 month and 15 days. Only seconds will ever be shown as fractional on output. »

Your patch changes what seems to be the intended behavior, with the test case
added by:

|commit 57bfb27e60055c10e42b87e68a894720c2f78e70
|Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
|Date: Mon Sep 4 01:26:28 2006 +0000
|
| Fix interval input parser so that fractional weeks and months are
| cascaded first to days and only what is leftover into seconds. This

And documented by:

|commit dbf57d31f8d7bf5c058a9fab2a1ccb4a336864ce
|Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
|Date: Sun Nov 9 17:09:48 2008 +0000
|
| Add some documentation about handling of fractions in interval input.
| (It's always worked like this, but we never documented it before.)

If you were to change the behavior, I think you'd have to update the
documentation, too - but I think that's not a desirable change.

I *am* curious why the YEAR, DECADE, CENTURY, AND MILLENIUM cases only handle
fractional intervals down to the next smaller unit, and not down to
seconds/milliseconds. I wrote a patch to handle that by adding
AdjustFractMons(), if we agree that it's desirable to change.

--
Justin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2021-04-05 18:01:58 Re: Have I found an interval arithmetic bug?
Previous Message Stephan Knauss 2021-04-05 07:23:23 Re: Debugging leaking memory in Postgresql 13.2/Postgis 3.1

Browse pgsql-hackers by date

  From Date Subject
Next Message Jacob Champion 2021-04-05 16:40:41 Re: Proposal: Save user's original authenticated identity for logging
Previous Message Tom Lane 2021-04-05 16:21:36 Re: ALTER TABLE ADD COLUMN fast default