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 1m7jJB-0007U5-UU for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Jul 2021 18:57:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m7jJA-0003iS-VM for pgsql-hackers@arkaria.postgresql.org; Sun, 25 Jul 2021 18:57:04 +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 1m7jJA-0003iD-Jv for pgsql-hackers@lists.postgresql.org; Sun, 25 Jul 2021 18:57:04 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m7jJ4-0001rf-Rm for pgsql-hackers@postgresql.org; Sun, 25 Jul 2021 18:57:01 +0000 Received: by mail-pj1-x102e.google.com with SMTP id pf12-20020a17090b1d8cb0290175c085e7a5so16595973pjb.0 for ; Sun, 25 Jul 2021 11:56:58 -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=8LgM6W6ON9BKPrFJwvKdQv35Cl2esTYMONCWBCC4uko=; b=Fw2Eo5wGERieT+eaQGDjKFoxnT0mgiNx/vX6HyUQMSsjE2a1KmTup/+kdiRp77AeZv SaYfWZSdMjYMdYBwBumVrk7FCCa4ok3oWB8yRC+GHLycguF8f5ieNIuob7lQ7Xfl9cbl jAXcsL5LVMB5rJrpAdEVLjh66zahpjRdETV9Y= 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=8LgM6W6ON9BKPrFJwvKdQv35Cl2esTYMONCWBCC4uko=; b=Q9AEQhup/Fd9NZHDrWuv0tJ/pXc3m0+rvjAIpMTR02knJK6bNoIRX9uoaAaJYN1TvW ttS0RIxn6Ezzme7ja9Y7gdSq9vEGLTcckFIGJOl1Lage1BfnrAeC7fKI4AA7nh/9oOS/ su+Nuq+q09YLyBm0WQt2V+hfRvhCnzmSao/42lJAfil4oHdtunTtQaoSU9yCP1fLZxIk 2SE4/H1YIQMamdL+rOI+fGqoBwWpz1V5dXL7pweEmz5xkQcdBm9S0P8V2Dhl8eGGVwmH L5BxLg7pg4ejKMo0BB/oEy8fFJk8M0DjtP+ukM0bS8Ob/oQiWzpg5s9MUUxvefDuI+5D mHxA== X-Gm-Message-State: AOAM5307RnXDmwjnnQSOe78f1SYYeLkb08ntotoMSD12QZvmwMX6vW+k peNSF7Z2SSOSgtQ6NCedlMU/BQ== X-Google-Smtp-Source: ABdhPJx5ne30b+ET20lhk0GmaKfdJz6DHsN7cgeRyf8eqgGhZk7EKGDk1uUR2F25a2PkEAtW6Lcn2g== X-Received: by 2002:a63:fc02:: with SMTP id j2mr14917518pgi.235.1627239416618; Sun, 25 Jul 2021 11:56:56 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:f592:b6fd:e5ca:c232]) by smtp.gmail.com with ESMTPSA id y7sm37816987pfi.204.2021.07.25.11.56.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 25 Jul 2021 11:56:56 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_8FBBAF99-82C4-41B4-86FB-C53776C8A1B1" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Have I found an interval arithmetic bug? Date: Sun, 25 Jul 2021 11:56:54 -0700 In-Reply-To: <20210723202738.GC8025@momjian.us> Cc: Zhihong Yu , Tom Lane PostgreSQL , Dean Rasheed , Daniel Gustafsson , Justin Pryzby , Isaac Morland , John W Higgins , PostgreSQL-development To: Bruce Momjian References: <677937.1626861539@sss.pgh.pa.us> <711398.1626888589@sss.pgh.pa.us> <20210722000713.GA19630@momjian.us> <654FEA5F-79CF-414A-9801-F1482EEBCD88@yugabyte.com> <20210722014331.GB19630@momjian.us> <20210723150551.GA8025@momjian.us> <20210723202738.GC8025@momjian.us> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_8FBBAF99-82C4-41B4-86FB-C53776C8A1B1 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 23-Jul-2021, bruce@momjian.us wrote: >=20 > On Fri, Jul 23, 2021 at 10:55:11AM -0700, Bryn Llewellyn wrote: >> SELECT >> '1.2345 months 1.2345 days 1.2345 seconds'::interval =3D=20 >> '1 month 1 day 1 second'::interval*1.2345; >>=20 >> In 13.3, the result is TRUE. (I know that this doesn=E2=80=99t = guarantee that the >> internal representations of the two compared interval values are the = same. But >> it=E2=80=99s a necessary condition for the outcome that I=E2=80=99m = referring to and serves to >> indecate the pont I=E2=80=99m making. A more careful test can be = made. >=20 > So you are saying fractional unit output should match multiplication > output? It doesn't now for all units: >=20 > SELECT interval '1.3443 years'; > interval > --------------- > 1 year 4 mons > =09 > SELECT interval '1 years' * 1.3443; > ?column? > --------------------------------- > 1 year 4 mons 3 days 22:45:07.2 >=20 > It is true this patch is further reducing that matching. Do people > think I should make them match as part of this patch? Summary: -------- It seems to me that the best thing to do is fix the coarse bug about = which there is unanimous agreement and to leave everything else (quirks = and all) untouched. Detail: ------- My previous email (to which Bruce replies here) was muddled. Sorry. The = challenge is that are a number of mechanisms at work. Their effects are = conflated. And it=E2=80=99s very hard to unscramble them. The underlying problem is that the internal representation of an = interval value is a [mm, dd, ss] tuple. This fact is documented. The = representation is key to understanding the definitions of these = operations: =E2=80=94 defining an interval value from a text literal that uses real = number values for its various fields. =E2=80=94 defining an interval value from make_interval(). (This one is = easy because the API requires integral values for all but the seconds = argument. It would be interesting to know why this asymmetrical = definition was implemented. It seems to imply that somebody though that = spilldown was a bad idea and should be prevented before it might = happen.) =E2=80=94 creating the text typecast of an extant interval value. =E2=80=94 creating an interval value by adding/subtracting an extant = interval value to/from another =E2=80=94 creating an interval value by multiplying or dividing an = extant interval value by a (real) number =E2=80=94 creating an interval value by subtracting a pair of moments of = the same data type (timestamptz, plain timestamp, or time) =E2=80=94 creating a new moment value by adding or subtracting an extant = interval value to an extant moment value of the same data type. =E2=80=94 creating an interval value by applying justify_hours(i), = justify_days(i), and justify_interval(i) to an extant interval value, i. =E2=80=94 creating a double precision value by applying extract(epoch = from i)=20 to an extant interval value, i. =E2=80=94 evaluating inequality and equality tests to compare two extant = interval values. Notice that, for example, this test: select interval '1.3443 years' as i1, interval '1 years' * 1.3443 as i2; conflates three things: converting an interval literal to a [mm, dd, ss] = tuple; multiplying a [mm, dd, ss] tuple by a real number; and converting = a [mm, dd, ss] tuple to a text representation. Similarly, this test: select interval '1.3443 years' < interval '1 years' * 1.3443; conflates three things: converting an interval literal to a [mm, dd, ss] = tuple; multiplying a [mm, dd, ss] tuple by a real number; and inequality = comparison of two [mm, dd, ss] two [mm, dd, ss] tuples. As far as I=E2=80=99ve been able, the PG documentation doesn=E2=80=99t = do a good job of defining the semantics of any of these operations. Some = (like the =E2=80=9Cjustify=E2=80=9D functions=E2=80=9D are sketched = reasonably well. Others, like interval multiplication, are entirely = undefined. This makes discussion of simple test like the two I showed immediately = above hard. It also makes any discussion of correctness, possible bugs, = and proposed implementation changes very difficult. Further, it also makes it hard to see how tests for application code = that uses any of these operations can be designed. The normal approach = relies on testing that you get what you expect. But here, you don't know = what to expect=E2=80=94unless (as I=E2=80=99ve done) you first assert = hypotheses for the undefined operations and test them with programmed = simulations. Of course, this is, in general, an unreliable approach. The = only way to know what code is intended to do is to read the prose = specification that informs the implementation. I had forgotten one piece of the long history of this thread. Soon after = I presented the testcase that folks agree shows a clear bug, I asked = about the rules for creating the internal [mm, dd, ss] tuple from a text = literal that uses real numbers for the fields. My tests showed two = things: (1) an intuitively clear model for the spilldown of nonintegral = months to days and then, in turn, of nonintegral days to seconds; and = (2) a quirky rule for deriving intermediate months from fractional years = and fractional months before then using the more obvious rules to spill = to days. (This defies terse expression in prose. I copied my PL/pgSQL = implementation below.) There was initially some discussion about changing implementation o the = spill-down from [years, months] in the interval literal to the ultimate = [mm, dd, ss] representation. This is what's Bruces is asking about. And = it's what I was muddled about. As I=E2=80=99ve said, my conclusion is that the only safe approach is to = create and use only =E2=80=9Cpure=E2=80=9D interval values (where just = one of the internal fields is non-zero). For this reason (and having = seen what I decided was the impossibly unmemorable rules that my modeled = implementation uses) I didn=E2=80=99t look at the rules for the other = fields that the interval literal allows (weeks, centuries, millennia, = and so on). = --------------------------------------------------------------------------= ------ mm_trunc constant int not null :=3D = trunc(p.mm); mm_remainder constant double precision not null :=3D p.mm - = mm_trunc::double precision; -- This is a quirk. mm_out constant int not null :=3D = trunc(p.yy*mm_per_yy) + mm_trunc; dd_real_from_mm constant double precision not null :=3D = mm_remainder*dd_per_mm; dd_int_from_mm constant int not null :=3D = trunc(dd_real_from_mm); dd_remainder_from_mm constant double precision not null :=3D = dd_real_from_mm - dd_int_from_mm::double precision; dd_int_from_user constant int not null :=3D = trunc(p.dd); dd_remainder_from_user constant double precision not null :=3D p.dd - = dd_int_from_user::double precision; dd_out constant int not null :=3D = dd_int_from_mm + dd_int_from_user; d_remainder constant double precision not null :=3D = dd_remainder_from_mm + dd_remainder_from_user; ss_out constant double precision not null :=3D = d_remainder*ss_per_dd + = p.hh*ss_per_hh + = p.mi*ss_per_mi + p.ss; = --------------------------------------------------------------------------= ------ --Apple-Mail=_8FBBAF99-82C4-41B4-86FB-C53776C8A1B1 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On 23-Jul-2021, bruce@momjian.us = wrote:

On Fri, Jul 23, 2021 at 10:55:11AM -0700, = Bryn Llewellyn wrote:
SELECT
 '1.2345 months 1.2345 days 1.2345 = seconds'::interval =3D
 '1 month 1 day 1 = second'::interval*1.2345;

In 13.3, the = result is TRUE. (I know that this doesn=E2=80=99t guarantee that the
internal representations of the two compared interval values = are the same. But
it=E2=80=99s a necessary condition for = the outcome that I=E2=80=99m referring to and serves to
indecate the pont I=E2=80=99m making. A more careful test can = be made.

So you are saying = fractional unit output should match multiplication
output? =  It doesn't now for all units:

SELECT = interval '1.3443 years';
  interval
= ---------------
1 year 4 mons

= SELECT interval '1 years' * 1.3443;
=            ?column?=
= ---------------------------------
1 year 4 = mons 3 days 22:45:07.2

It is true this = patch is further reducing that matching.  Do people
think I should make them match as part of this = patch?

Summary:
--------

It seems to me that the best thing to do is = fix the coarse bug about which there is unanimous agreement and to leave = everything else (quirks and all) untouched.

Detail:
-------

My = previous email (to which Bruce replies here) was muddled. Sorry. The = challenge is that are a number of mechanisms at work. Their effects are = conflated. And it=E2=80=99s very hard to unscramble them.

The = underlying problem is that the internal representation of an interval = value is a [mm, dd, ss] tuple. This fact is documented. The = representation is key to understanding the definitions of these = operations:

=E2=80= =94 defining an interval value from a text literal that uses real number = values for its various fields.

=E2=80=94 defining an interval value = from make_interval(). (This one is easy because the API requires = integral values for all but the seconds argument. It would be = interesting to know why this asymmetrical definition was implemented. It = seems to imply that somebody though that spilldown was a bad idea and = should be prevented before it might happen.)

=E2=80=94 creating the text typecast of = an extant interval value.

=E2=80=94 creating an interval = value by adding/subtracting an extant interval value to/from = another

=E2=80=94 creating an interval value by multiplying = or dividing an extant interval value by a (real) number

=E2=80=94 creating an interval value by = subtracting a pair of moments of the same data type (timestamptz, plain = timestamp, or time)

=E2=80=94 creating a new moment value by adding or = subtracting an extant interval value to an extant moment value of the = same data type.

=E2=80=94 creating an interval value by = applying  justify_hours(i), justify_days(i), and = justify_interval(i) to an extant interval value, i.

=E2=80=94 creating a double precision value by applying = extract(epoch from i) 
to an extant interval = value, i.

=E2=80= =94 evaluating inequality and equality tests to compare two extant = interval values.

Notice that, for example, this test:

select
  interval  '1.3443 years' as i1,
  interval '1 years' * 1.3443 as i2;

conflates three things: converting an interval = literal to a [mm, dd, ss] tuple; multiplying a [mm, dd, ss] = tuple by a real number; and converting a [mm, dd, ss] tuple to a = text representation. Similarly, this test:

select
  interval  '1.3443 years' <
  interval '1 years' * 1.3443;

conflates three things: converting an interval = literal to a [mm, dd, ss] tuple; multiplying a [mm, dd, ss] = tuple by a real number; and inequality comparison of two [mm, dd, = ss] two [mm, dd, ss] tuples.

As far as I=E2=80=99ve been able, the = PG documentation doesn=E2=80=99t do a good job of defining the semantics = of any of these operations. Some (like the =E2=80=9Cjustify=E2=80=9D = functions=E2=80=9D are sketched reasonably well. Others, like interval = multiplication, are entirely undefined.

This makes discussion of simple test = like the two I showed immediately above hard. It also makes any = discussion of correctness, possible bugs, and proposed implementation = changes very difficult.

Further, it also makes it hard to see how tests for = application code that uses any of these operations can be designed. The = normal approach relies on testing that you get what you expect. But = here, you don't know what to expect=E2=80=94unless (as I=E2=80=99ve = done) you first assert hypotheses for the undefined operations and test = them with programmed simulations. Of course, this is, in general, an = unreliable approach. The only way to know what code is intended to do is = to read the prose specification that informs the = implementation.

I had forgotten one piece of the long history of this thread. = Soon after I presented the testcase that folks agree shows a clear bug, = I asked about the rules for creating the internal [mm, dd, ss] tuple = from a text literal that uses real numbers for the fields. My tests = showed two things: (1) an intuitively clear model for the spilldown of = nonintegral months to days and then, in turn, of nonintegral days to = seconds; and (2) a quirky rule for deriving intermediate months from = fractional years and fractional months before then using the more = obvious rules to spill to days. (This defies terse expression in prose. = I copied my PL/pgSQL implementation below.)

There was initially some discussion = about changing implementation o the spill-down from [years, months] in = the interval literal to the ultimate [mm, dd, ss] representation. This = is what's Bruces is asking about. And it's what I was muddled = about.

As I=E2=80=99ve said, my conclusion is that the only safe = approach is to create and use only =E2=80=9Cpure=E2=80=9D interval = values (where just one of the internal fields is non-zero). For this = reason (and having seen what I decided was the impossibly unmemorable = rules that my modeled implementation uses) I didn=E2=80=99t look at the = rules for the other fields that the interval literal allows (weeks, = centuries, millennia, and so on).

---------------------------------------------------------------= -----------------
mm_trunc   =              constant int   =            not null :=3D = trunc(p.mm);
mm_remainder       =      constant double precision not null :=3D p.mm - = mm_trunc::double precision;

-- This is a quirk.
mm_out   =                constant int =              not null :=3D = trunc(p.yy*mm_per_yy) + mm_trunc;

dd_real_from_mm       =   constant double precision not null :=3D = mm_remainder*dd_per_mm;

dd_int_from_mm          constant int =              not null :=3D = trunc(dd_real_from_mm);
dd_remainder_from_mm   =  constant double precision not null :=3D dd_real_from_mm - = dd_int_from_mm::double precision;

dd_int_from_user       =  constant int              not = null :=3D trunc(p.dd);
dd_remainder_from_user =  constant double precision not null :=3D p.dd - = dd_int_from_user::double precision;

dd_out         =          constant int       =        not null :=3D dd_int_from_mm + = dd_int_from_user;

d_remainder             = constant double precision not null :=3D dd_remainder_from_mm + = dd_remainder_from_user;

ss_out               =    constant double precision not null :=3D = d_remainder*ss_per_dd +
        =                     =                     =               p.hh*ss_per_hh = +
              =                     =                     =         p.mi*ss_per_mi +
  =                     =                     =                     = p.ss;
---------------------------------------------------------------= -----------------












= --Apple-Mail=_8FBBAF99-82C4-41B4-86FB-C53776C8A1B1--