Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3Vwu-0003VJ-Ng for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 23:02:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d3Vwt-0001fU-Js for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 23:02:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d3VwA-0000SZ-Sv for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 23:01:31 +0000 Received: from mail-wr0-x229.google.com ([2a00:1450:400c:c0c::229]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1d3Vw6-0005Ul-QE for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 23:01:30 +0000 Received: by mail-wr0-x229.google.com with SMTP id l50so7768457wrc.3 for ; Wed, 26 Apr 2017 16:01:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc:message-id:references :to; bh=V2Oj8DCDxPXY8i5DC+TxjfeXocToGYej+r9+5ZsiSmg=; b=VAQxIrtm4tFCxUhTQ6BlWtwPu83RR85oaMjhXX7RZnJr0Wj4vZg4LWgo6BJWVYnsPI Evi51YZ5F6RzW+Zryz2Sisy8svCkoMKSVZyFHpxOCaSygF8ZmSzBrBI0h93AF3fJEojc pSYSV5LdQ5BWn9OAZnewhSqAs2f2YxH5EwA+hKN3i7mFACFPw1MFBEkOQOgo5i0xWX8t 2IqsHIrvmuSefRAeDlbLu8al8B/ZV3PviLrsVKwPdmeQM4sg4X2hFfpweedxseftekFZ R6h9PEY1JxZTBEmapohJe31cEZIwMPPHmqBT0DL5B1PLUcn54uCmQxx7mr+SWMDQQ5yZ RKlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :message-id:references:to; bh=V2Oj8DCDxPXY8i5DC+TxjfeXocToGYej+r9+5ZsiSmg=; b=DkFyeuGoGg1Ej3qQpTI2QCRs6iOtfck1/sezjsSmSZUQgPrkwiFBJpnf8iX2Arz2JJ 72Al+8Kk8dm+aKQf2fwjaA8EcoGNg0rmDLTiDbeZJsYpEkMR/1nAp3Xb7tl0UWp79srC OjpTElWMZ6P/iVZHwCbaxJYe3DzYZV8qfOgNJOjzyuwdHXH6mipqWpCI+iBq/UGc106d jdyRwvCnTaai0GADwHsddHulEUcb/q4HMK2Mtmkglq+hoI0YPS1pJx94CabAxyqeb/Cr jEE6oZysTqpIyOSbLL+48mSyRtmd4nLeDIloP3Ocjf83gLWSHnOnTtPy834PD+25vaxC PiNg== X-Gm-Message-State: AN3rC/4Ra9K2z4A5aIICNs468kjgh4myrY+EDqMvvLG4zdqXJfGrmO0W 45Gx+KZZzq7+RQ== X-Received: by 10.223.129.229 with SMTP id 92mr1390814wra.57.1493247685297; Wed, 26 Apr 2017 16:01:25 -0700 (PDT) Received: from [192.168.2.5] (2-230-140-245.ip202.fastwebnet.it. [2.230.140.245]) by smtp.gmail.com with ESMTPSA id t85sm1165900wmt.23.2017.04.26.16.01.24 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 26 Apr 2017 16:01:24 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_4E9ED26A-CAC7-40C5-B138-CED227715ED8" Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\)) Subject: Re: BUG #14632: Plus and minus operators inconsistency with leap years and year intervals. From: Pietro Pugni In-Reply-To: Date: Thu, 27 Apr 2017 01:01:22 +0200 Cc: "pgsql-bugs@postgresql.org" Message-Id: References: <20170426182322.24371.95224@wrigleys.postgresql.org> <5752C491-B0EC-4D1E-AF97-5E1C8C793A8A@gmail.com> To: "David G. Johnston" X-Mailer: Apple Mail (2.3124) X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --Apple-Mail=_4E9ED26A-CAC7-40C5-B138-CED227715ED8 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > =E2=80=8BIn theory we could simply die trying but given few complaints = this behavior doesn't seem to bother many people if not actually please = them because at least they can get a correct result even if it is munged = a bit. Date interval math is fraught with problems.; you can bypass = most of them by manipulating days instead. Many people doesn=E2=80=99t observe data and thus don=E2=80=99t see = errors. I was simply calculating birthdays from birth dates and found = some weird results that turned out to be associated to leap years. > The =E2=80=9Clogical=E2=80=9D bug can be seen also by adding and = subtracting the same quantity: >=20 > SELECT '1912-02-29'::date - '10 years'::interval + '10 = years'::interval >=20 > It returns '1912-02-28 00:00:00' instead of '1912-02-29 00:00:00'. >=20 > Expect 1912-02-28 is a correct response. The only reason you think = the 29th comes into play here is because you remember that the starting = point was the 29th. The system has no such memory.=E2=80=8B And this is logically wrong because it leads to wrong results. I=E2=80=99m= aware that time intervals are difficult to manage but more exactness is = needed here: '10 years' must have the same meaning when added to a date = and subtracted from it, otherwise it leads to wrong results. > It=E2=80=99s an issue because there=E2=80=99s no other way to obtain = the original date (reversing the add operation). >=20 > You are correct. Given the presence of leap years what you describe = is a mathematical impossibility - not a bug. It should be advised in the official docs (I can=E2=80=99t find any = reference for this topic). > Also: > SELECT age('1922-02-28'::date, '1912-02-29'::date) =3D '10 = years'::interval > returns false, while: > SELECT ('1922-02-29'::date + '10 years'::interval) =3D = '1922-02-28'::date > returns true. >=20 > The inconsistency (or bug) resides in the non unique meaning of the = interval handling with respect of summing and subtracting the same = quantity.=20 >=20 >=20 > =E2=80=8BIt resides in the fact we apply date shifting after applying = the interval in order to come up with a valid date. That such date = shifting negates the commutative property is an unfortunate byproduct. = To my knowledge there is no promise nor requirement for date arithmetic = to be commutative. Or, more precisely, if you wish to use the = commutative property here you must operate using days. Again, it should be written in some part of the docs that intervals lead = to different results if summed/subtracted to/from leap dates. >=20 >=20 > Again, the issue can be seen this way: adding the interval returned by = =E2=80=8B=E2=80=8BSELECT age('1922-02-28'::date, '1912-02-29'::date) to = the original date. So: > =E2=80=8B=E2=80=8BSELECT '1912-02-29'::date + '9 years 11 mons 28 = days'::interval > returns: > (c) 1922-02-26 00:00:00 > Here we loose 2 days even if we add the same interval used in (b)! >=20 > =E2=80=8BOperate in days and you can do this just fine - but note you = cannot just convert the interval to a number of days. >=20 > =E2=80=8BSELECT '1922-02-28'::date - '1912-02-29'::date ; 3652 > SELECT '1922-02-28'::date - 3652 ; 1912-02-29 >=20 > This particular example leads me to suspect that some improvement in = this area might be possible... >=20 > The bug basically consists of the vague meaning of =E2=80=9Cyears=E2=80=9D= applied to leap years. It should be revised in order to be consistent = and correct. >=20 > How? Probably considering leap dates as singularities and threat them by = subtracting 1 day.=20 For example, a possible workaround to properly calculate the interval = between 1922-02-28 and 1922-02-29 is the following: SELECT age('1922-02-28'::date, '1912-02-29'::date - '1 day'::interval) age =20 ---------- 10 years King regards, Pietro Pugni --Apple-Mail=_4E9ED26A-CAC7-40C5-B138-CED227715ED8 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
=E2=80=8BIn theory we could simply die trying but given few = complaints this behavior doesn't seem to bother many people if not = actually please them because at least they can get a correct result even = if it is munged a bit.  Date interval math is fraught with = problems.; you can bypass most of them by manipulating days = instead.
Many people doesn=E2=80=99t = observe data and thus don=E2=80=99t see errors. I was simply calculating = birthdays from birth dates and found some weird results that turned out = to be associated to leap years.


The =E2=80=9Clogical=E2=80= =9D bug can be seen also by adding and subtracting the same = quantity:

SELECT '1912-02-29'::date - '10 = years'::interval + '10 years'::interval

It returns '1912-02-28 00:00:00' instead of '1912-02-29 = 00:00:00'.

Expect 1912-02-28 is a = correct response.  The only reason you think the 29th comes into = play here is because you remember that the starting point was the = 29th.  The system has no such = memory.=E2=80=8B

And this is logically wrong because it leads to = wrong results. I=E2=80=99m aware that time intervals are difficult to = manage but more exactness is needed here: '10 years' must have the same = meaning when added to a date and subtracted from it, otherwise it leads = to wrong results.


It=E2=80=99s = an issue because there=E2=80=99s no other way to obtain the original = date (reversing the add operation).

You are correct.  = Given the presence of leap years what you describe is a mathematical = impossibility - not a = bug.

It should be advised in the official docs (I = can=E2=80=99t find any reference for this topic).


Also:
SELECT age('1922-02-28'::date, = '1912-02-29'::date) =3D '10 years'::interval
returns false, = while:
SELECT ('1922-02-29'::date + '10 years'::interval) =3D = '1922-02-28'::date
returns true.

The inconsistency (or bug) resides in = the non unique meaning of the interval handling with respect of summing = and subtracting the same quantity. 


=E2=80=8BIt resides in = the fact we apply date shifting after applying the interval in order to = come up with a valid date.  That such date shifting negates the = commutative property is an unfortunate byproduct.  To my knowledge = there is no promise nor requirement for date arithmetic to be = commutative.  Or, more precisely, if you wish to use the = commutative property here you must operate using = days.

Again, it should be written in some part of the = docs that intervals lead to different results if summed/subtracted = to/from leap dates.



Again, the issue can be = seen this way: adding the interval returned by 
=E2=80=8B=E2= =80=8B
SELECT age('1922-02-28'::date, = '1912-02-29'::date)
 to the original date. So:
=E2=80=8B=E2= =80=8B
SELECT '1912-02-29'::date + '9 years 11 mons 28 = days'::interval
returns:
(c) 1922-02-26 = 00:00:00
Here we loose 2 days = even if we add the same interval used in (b)!

=E2=80=8BOperate in = days and you can do this just fine - but note you cannot just convert = the interval to a number of days.

=E2=80=8BSELECT = '1922-02-28'::date - '1912-02-29'::date ; 3652
SELECT = '1922-02-28'::date - 3652 ; 1912-02-29

This particular example = leads me to suspect that some improvement in this area might be = possible...

The bug basically consists of the vague meaning of = =E2=80=9Cyears=E2=80=9D applied to leap years. It should be revised in = order to be consistent and correct.

How?

Probably considering leap dates as singularities and threat = them by subtracting 1 day. 
For example, a possible workaround to properly calculate the = interval between 1922-02-28 and 1922-02-29 is the following:

SELECT age('1922-02-28'::date, = '1912-02-29'::date - '1 day'::interval)
  =  age    
----------
 10 years

King regards,
 Pietro= Pugni

= --Apple-Mail=_4E9ED26A-CAC7-40C5-B138-CED227715ED8--