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 1m6Nh2-00018r-AO for pgsql-hackers@arkaria.postgresql.org; Thu, 22 Jul 2021 01:40: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 1m6NgU-0001wA-EF for pgsql-hackers@arkaria.postgresql.org; Thu, 22 Jul 2021 01:39:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m6NgU-0001w2-2x for pgsql-hackers@lists.postgresql.org; Thu, 22 Jul 2021 01:39:34 +0000 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m6NgR-00074O-0b for pgsql-hackers@postgresql.org; Thu, 22 Jul 2021 01:39:33 +0000 Received: by mail-pj1-x102b.google.com with SMTP id my10so3972891pjb.1 for ; Wed, 21 Jul 2021 18:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=nf7izrKNLZOCufYWLxToSFeMzl7sTeZN0UQOow1oJU0=; b=gLnP+iU5ignXD7QdKZAWjvrPCBy/UFBM+SwYb6wZnkQ9dm6xi/XQiL+E/+B6oy6a7b FGKJ8O7fL14PY7MfoHYL3Tz049ygptE3lCdNcGVZCiVuujpFnqixps+v4ZP4Tu6rkSk2 ctL4AdBVEgta5AAWfEYToqzbNy2Y4J/MPYi+E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=nf7izrKNLZOCufYWLxToSFeMzl7sTeZN0UQOow1oJU0=; b=LULCyvSyQ7ylCJIAh2fm/X92bv9lyW3zPRFmJkJFjdmWPT5/UtyMSqjhmtUYX3A5/M +20Kf1R4stsVBPGF9Xa266GdFpSXO8asSZ64V6GQlNi1DvJrNVejw3IB0tI+afAbc2xE 6UmHpzpu43JproKDEk/wx3gL1QX3efoZWBUD4tN2cR5s7X9QkckC88u9LdvB+eUMyt34 z9OKd2AnaNAN8VXoXJOThG7Qe8CI3sIyYmRZFFi1e+5ntc29ZTB2yhjcTWU26oyDBVgB 5BVCenf6jJaX05SueoDnCDRxyrscu1SZbCvjqlXf/LBcSNws7o5gHB5rXc4/RdsbSFJW ObLg== X-Gm-Message-State: AOAM532+Grr13uqyMoT4XNZ2iZCTkcwXBl2fmVtreEYTl9GeR3USMIHB WS5LuS+3/+jN10FCQozypPL92g== X-Google-Smtp-Source: ABdhPJwzk2gGkcbHpjUElw5/uB3ESRcNyN6okvpvbb3KATNw6cMsSRXetMzi6its7ENbwoY9zfD1mA== X-Received: by 2002:a17:90a:5141:: with SMTP id k1mr6278074pjm.185.1626917969148; Wed, 21 Jul 2021 18:39:29 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:3c3a:ecf2:66f:d412]) by smtp.gmail.com with ESMTPSA id y28sm21914129pff.137.2021.07.21.18.39.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Jul 2021 18:39:28 -0700 (PDT) From: Bryn Llewellyn Message-Id: <654FEA5F-79CF-414A-9801-F1482EEBCD88@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_954CB134-1073-497B-9000-4954DFBF951E" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.100.0.2.22\)) Subject: Re: Have I found an interval arithmetic bug? Date: Wed, 21 Jul 2021 18:39:26 -0700 In-Reply-To: <20210722000713.GA19630@momjian.us> Cc: Tom Lane PostgreSQL , Dean Rasheed , Zhihong Yu , Daniel Gustafsson , Justin Pryzby , Isaac Morland , John W Higgins , PostgreSQL-development To: Bruce Momjian References: <20210720041419.GA5882@momjian.us> <20210720225350.GB27025@momjian.us> <20210721024834.GA8395@momjian.us> <677937.1626861539@sss.pgh.pa.us> <711398.1626888589@sss.pgh.pa.us> <20210722000713.GA19630@momjian.us> X-Mailer: Apple Mail (2.3654.100.0.2.22) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_954CB134-1073-497B-9000-4954DFBF951E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 21-Jul-2021, at 17:07, Bruce Momjian wrote: >=20 > On Wed, Jul 21, 2021 at 01:29:49PM -0400, Tom Lane wrote: >> Bryn Llewellyn writes: >>> It was me that started the enormous thread with the title =E2=80=9CHav= e I found an interval arithmetic bug?=E2=80=9D on 01-Apr-2021. I = presented this testcase: >>=20 >>>> select interval '-1.7 years'; -- -1 years = -8 mons >>>>=20 >>>> select interval '29.4 months'; -- 2 years = 5 mons 12 days >>>>=20 >>>> 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 >>>>=20 >>>> 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 >>=20 >>> The consensus was that the outcome that I flagged with =E2=80=9Cwrong=E2= =80=9D does indeed have that status. >>=20 >> Yeah, I think it's self-evident that your last four cases should >> produce the same results. Whether '9 mons 12 days' is the best >> possible result is debatable --- in a perfect world, maybe we'd >> produce '9 mons' exactly --- but given that the first two cases >> produce what they do, that does seem self-consistent. I think >> we should be setting out to fix that outlier without causing >> any of the other five results to change. >=20 > OK, I decided to reverse some of the changes I was proposing once I > started to think about the inaccuracy of not spilling down from = 'weeks' > to seconds when hours also appear. The fundamental issue is that the > months-to-days conversion is almost always an approximation, while the > days to seconds conversion is almost always accurate. This means we = are > never going to have consistent spill-down that is useful. >=20 > Therefore, I went ahead and accepted that years and larger units spill > only to months, months spill only to days, and weeks and lower spill = all > the way down to seconds. I also spelled this out in the docs, and > explained why we have this behavior. >=20 > Also, with my patch, the last four queries return the same result > because of the proper rounding also added by the patch, attached. Your statement =E2=80=9Cmonths-to-days conversion is almost always an approximation, = while the days to seconds conversion is almost always accurate.=E2=80=9D=20= is misleading. Any conversion like these (and also the =E2=80=9Cspill = up=E2=80=9D conversions that the justify_hours(), justify_days(), and = justify_interval() built-in functions bring) are semantically dangerous = because of the different rules for adding a pure months, a pure days, or = a pure seconds interval to a timestamptz value. Unless you avoid mixed interval values, then it=E2=80=99s so hard (even = though it is possible) to predict the outcomes of interval arithmetic. = Rather, all you get is emergent behavior that I fail to see can be = relied upon in deliberately designed application code. Here=E2=80=99s a = telling example: set timezone =3D 'America/Los_Angeles'; with c as ( select '2021-03-13 19:00:00 America/Los_Angeles'::timestamptz as d, '25 hours'::interval as i) select d + i as "d + i", d + justify_hours(i) as "d + justify_hours(i)" from c; This is the result: d + i | d + justify_hours(i) =20 ------------------------+------------------------ 2021-03-14 21:00:00-07 | 2021-03-14 20:00:00-07 The two results are different, even though the native equality test = shows that the two different interval values are the same: with c as (select '25 hours'::interval as i) select (i =3D justify_hours(i))::text from c; The result is TRUE. The only route to sanity is to use only pure interval values (i.e. where = only one of the fields of the internal [mm, dd, ss] representation is = non-zero. I mentioned that you can use a set of three domain types to enforce your = intended practice here. In other words, by programming application code defensively, it=E2=80=99s = possible to insulate oneself entirely from the emergent behavior of the = decades old PG code that implements the unconstrained native interval = functionality and that brings what can only be considered to be = unpredictable results. Moreover, this defensive approach insulates you from any changes that = Bruce=E2=80=99s patch might make. --Apple-Mail=_954CB134-1073-497B-9000-4954DFBF951E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On 21-Jul-2021, at = 17:07, Bruce Momjian <bruce@momjian.us> wrote:

On = Wed, Jul 21, 2021 at 01:29:49PM -0400, Tom Lane wrote:
Bryn Llewellyn <bryn@yugabyte.com> = writes:
It was me = that started the enormous thread with the title =E2=80=9CHave I found an = interval arithmetic bug?=E2=80=9D on 01-Apr-2021. I presented this = testcase:

select = interval '-1.7 years'; =             &n= bsp;           &nbs= p;-- -1 years -8 mons

select interval '29.4 = months'; =             &n= bsp;           -- =  2 years  5 mons 12 days

select = interval '-1.7 years 29.4 months'; =             &n= bsp;--           8 = mons 12 days << wrong
select interval '29.4 months = -1.7 years'; =             &n= bsp;--           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

The consensus was that = the outcome that I flagged with =E2=80=9Cwrong=E2=80=9D does indeed have = that status.

Yeah, I think = it's self-evident that your last four cases should
produce = the same results.  Whether '9 mons 12 days' is the best
possible result is debatable --- in a perfect world, maybe = we'd
produce '9 mons' exactly --- but given that the first = two cases
produce what they do, that does seem = self-consistent.  I think
we should be setting out to = fix that outlier without causing
any of the other five = results to change.

OK, I = decided to reverse some of the changes I was proposing once I
started to think about the inaccuracy of not spilling down = from 'weeks'
to seconds when hours also appear.  The = fundamental issue is that the
months-to-days conversion is = almost always an approximation, while the
days to seconds = conversion is almost always accurate.  This means we are
never going to have consistent spill-down that is useful.

Therefore, I went ahead and accepted that = years and larger units spill
only to months, months spill = only to days, and weeks and lower spill all
the way down = to seconds.  I also spelled this out in the docs, and
explained why we have this behavior.

Also, with my patch, the last four queries return the same = result
because of the proper rounding also added by the = patch, attached.

Your= statement

=E2=80=9Cmonths-to-days conversion is = almost always an approximation, while the days to seconds conversion is = almost always accurate.=E2=80=9D 

is misleading. Any = conversion like these (and also the =E2=80=9Cspill up=E2=80=9D = conversions that the justify_hours(), justify_days(), and = justify_interval() built-in functions bring) are semantically dangerous = because of the different rules for adding a pure months, a pure days, or = a pure seconds interval to a timestamptz value.

Unless you avoid mixed interval values, = then it=E2=80=99s so hard (even though it is possible) to predict the = outcomes of interval arithmetic. Rather, all you get is emergent = behavior that I fail to see can be relied upon in deliberately designed = application code. Here=E2=80=99s a telling example:

set timezone =3D 'America/Los_Angeles';
with
  c as (
    select
    =   '2021-03-13 19:00:00 = America/Los_Angeles'::timestamptz as d,
  =     '25 hours'::interval        =                     =        as i)
select
  d +            =    i  as "d + = i",
  d + justify_hours(i) as "d + = justify_hours(i)"
from c;

This is the result:

      =    d + i          |  d = + justify_hours(i)  
------------------------+------------------------
 2021-03-14 21:00:00-07 | 2021-03-14 = 20:00:00-07

The = two results are different, even though the native equality test shows = that the two different interval values are the same:

with
  c as = (select '25 hours'::interval as i)
select (i =3D = justify_hours(i))::text
from c;

The result is TRUE.

The only route to sanity is to use only = pure interval values (i.e. where only one of the fields of the internal = [mm, dd, ss] representation is non-zero.

I mentioned that you can use a set of = three domain types to enforce your intended practice here.

In other words, by = programming application code defensively, it=E2=80=99s possible to = insulate oneself entirely from the emergent behavior of the decades old = PG code that implements the unconstrained native interval functionality = and that brings what can only be considered to be unpredictable = results.

Moreover, this defensive approach insulates you from any = changes that Bruce=E2=80=99s patch might make.

= --Apple-Mail=_954CB134-1073-497B-9000-4954DFBF951E--