Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSOBM-0000L7-U2 for pgsql-general@arkaria.postgresql.org; Fri, 02 Apr 2021 18:06:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lSOBL-0008Ei-Qj for pgsql-general@arkaria.postgresql.org; Fri, 02 Apr 2021 18:06:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSOBL-0008Eb-JG for pgsql-general@lists.postgresql.org; Fri, 02 Apr 2021 18:06:07 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lSOBJ-0007Dg-EK for pgsql-general@lists.postgresql.org; Fri, 02 Apr 2021 18:06:06 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1lSOBI-0002iQ-OC; Fri, 02 Apr 2021 14:06:04 -0400 Date: Fri, 2 Apr 2021 14:06:04 -0400 From: Bruce Momjian To: Bryn Llewellyn Cc: pgsql-general list Subject: Re: Have I found an interval arithmetic bug? Message-ID: <20210402180604.GG9270@momjian.us> References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thread moved to hackers, with a patch. --------------------------------------------------------------------------- On Thu, Apr 1, 2021 at 09:46:58PM -0700, Bryn Llewellyn wrote: > Or am I misunderstanding something? > > Try this. The result of each “select” is shown as the trailing comment on the > same line. I added whitespace by hand to line up the fields. > > select interval '-1.7 years'; -- -1 years -8 mons > > select interval '29.4 months'; -- 2 years 5 mons 12 > days > > select interval '-1.7 years 29.4 months'; -- 8 mons 12 > days << wrong > select interval '29.4 months -1.7 years'; -- 9 mons 12 > days > > select interval '-1.7 years' + interval '29.4 months'; -- 9 mons 12 > days > select interval '29.4 months' + interval '-1.7 years'; -- 9 mons 12 > days > > As I reason it, the last four “select” statements are all semantically the > same. They’re just different syntaxes to add the two intervals the the first > two “select” statements use separately. There’s one odd man out. And I reason > this one to be wrong. Is there a flaw in my reasoning? > > Further… there’s a notable asymmetry. The fractional part of “1.7 years” is 8.4 > months. But the fractional part of the months value doesn’t spread further down > into days. However, the fractional part of “29.4 months” (12 days) _does_ > spread further down into days. What’s the rationale for this asymmetry? > > I can’t see that my observations here can be explained by the difference > between calendar time and clock time. Here I’m just working with non-metric > units like feet and inches. One year is just defined as 12 months. And one > month is just defined as 30 days. All that stuff about adding a month to > 3-Feb-2020 taking you to 3-Mar-2020 (same for leap years an non-leap years) , > and that other stuff about adding one day to 23:00 on the day before the > “spring forward” moment taking you to 23:00 on the next day (i.w. when > intervals are added to timestamps) is downstream of simply adding two > intervals. > -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com If only the physical world exists, free will is an illusion.