Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3Taa-0004Ub-Cm for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 20:31:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d3TaZ-0006hf-VT for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 20:31:04 +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 1d3TaZ-0006gm-4R for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 20:31:03 +0000 Received: from mail-wr0-x22c.google.com ([2a00:1450:400c:c0c::22c]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1d3TaV-0002ID-Lu for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 20:31:02 +0000 Received: by mail-wr0-x22c.google.com with SMTP id l50so6281328wrc.3 for ; Wed, 26 Apr 2017 13:30:59 -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=rBtEBUZHxmoeuQdNqgP4sDVtm6g8WLuG+aUJBgmYFnY=; b=cQRCyqa2rV11E4ZKor5elBGk3mZSasSdfCrYNC7Q7Kh2dC745kfZrYJs/Fg9u4hgTg wusA9lMiMWPl/apyC4Tdi7FlavBOT/VhjuOzNCIIN4OnWblee9dTmUiCPbeZNULLSrBR 3FpCiKFdMtqUk5ZP5p/f2TjxJF7+vckVUOq8q+UckYcRK3fua3Z+KBQmhDrTEYHr09Dg 5SzbhOqIoDnM3OZASiPaks7fDVztjuORkoIN+NQpPOCmKwM8RDSXpw+lxfponJDmz3jT 5Cgbja2LoXUg7uA66SkvFWUmRaFLyu3to8pNLI+IIyT8WGm2UljHUV2zZZoPlxhfOYBe J/Ig== 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=rBtEBUZHxmoeuQdNqgP4sDVtm6g8WLuG+aUJBgmYFnY=; b=m+tTaDefU70E11JDkWPkyNMb55/Zhg29+sNp6uGmyljzMgyTTqxPjBs5o9ArWsO32C MxVr6eQLgfb8uWoLHHdUzmJwXTYMN2M5Xk6TbtmV2BF0CoSZGfr1wrsvVyVjCbAAkAQz MdvW/ti81JY+G8dcmkkSC3c/skr6jg8ZIViB4hurW4ujZPUE6Wf+6vt353qTkHO9uzy7 Z8umoKh69qi6gLwr10l0qJ5ZLEL+GoZ1fgCfR30xvgguKXJeNCXthaY/PHURmdEu771X ygixMAB5jTB0ul7WbQZpE/sRIy2CmR+08rUug7EuFLTiteJp5AWw0Hf+/kizqth1vwli rUXw== X-Gm-Message-State: AN3rC/5XC0ooMTPMcnVlGHSubKEecdx6iBP7FOA60mwij8hs57mpcl2L 7BbwpZ9dbK5CbqY1ogQ= X-Received: by 10.223.172.48 with SMTP id v45mr1193535wrc.112.1493238657269; Wed, 26 Apr 2017 13:30:57 -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 m201sm728090wmd.15.2017.04.26.13.30.55 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 26 Apr 2017 13:30:56 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_E9B0556D-456F-452F-BF91-848A1DF09478" 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: Wed, 26 Apr 2017 22:30:50 +0200 Cc: "pgsql-bugs@postgresql.org" Message-Id: <5752C491-B0EC-4D1E-AF97-5E1C8C793A8A@gmail.com> References: <20170426182322.24371.95224@wrigleys.postgresql.org> 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=_E9B0556D-456F-452F-BF91-848A1DF09478 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I=E2=80=99ll try to reformulate better. Adding 10 years to 1912-02-29 returns 1922-02-29, as expected. I would like to apply the reverse operation. To do so, I subtract 10 = years from 1922-02-29 but I obtain 1912-02-28, so the math is actually = wrong. 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'. It=E2=80=99s an issue because there=E2=80=99s no other way to obtain the = original date (reversing the add operation). The function age(timestamp, = timestamp), for example, behave exactly as adding and subtracting the = interval: SELECT age('1922-02-28'::date, '1912-02-29'::date) returns: 9 years 11 mons 28 days which is inconsistent against the result returned by adding 10 years to = 1912-02-29: SELECT '1912-02-29'::date + '10 years'::interval returns: (a) '1922-02-28 00:00:00' while SELECT '1922-02-28'::date - '9 years 11 mons 28 days'::interval returns: (b) '1912-02-29 00:00:00' Please, note the difference of 1 day between (a) and (b).=20 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.=20 Again, the issue can be seen this way: adding the interval returned by = SELECT age('1922-02-28'::date, '1912-02-29'::date) to the original date. = So: 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)! 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. Kind regards, Pietro Pugni > Il giorno 26 apr 2017, alle ore 20:29, David G. Johnston = ha scritto: >=20 > On Wed, Apr 26, 2017 at 11:23 AM, > wrote: > The following bug has been logged on the website: >=20 > Bug reference: 14632 > Logged by: Pietro Pugni > Email address: pietro.pugni@gmail.com = > PostgreSQL version: 9.6.2 > Operating system: Ubuntu 16.04.2 LTS > Description: >=20 > The following query: > SELECT '1912-02-29'::date + '10 years'::interval >=20 > returns: > '1922-02-28 00:00:00' >=20 > while the reverse operation: > SELECT '1922-02-28'::date - '10 years'::interval >=20 > returns: > '1912-02-28 00:00:00' >=20 > instead of '1912-02-29 00:00:00'. >=20 > =E2=80=8BWhat part of this do you consider to be a bug - and what = should it do instead? >=20 > David J.=E2=80=8B --Apple-Mail=_E9B0556D-456F-452F-BF91-848A1DF09478 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
I=E2=80=99ll try to reformulate = better.

Adding = 10 years to 1912-02-29 returns 1922-02-29, as expected.
I would like to apply the reverse = operation. To do so, I subtract 10 years from 1922-02-29 but I obtain = 1912-02-28, so the math is actually wrong.

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'.


It=E2=80=99s an issue because there=E2=80=99s no other way to = obtain the original date (reversing the add operation). The function = age(timestamp, timestamp), = for example, behave exactly as adding and subtracting the = interval:
SELECT age('1922-02-28'::date, = '1912-02-29'::date)
returns:
9 = years 11 mons 28 days
which is inconsistent against the result returned by adding 10 = years to 1912-02-29:
SELECT '1912-02-29'::date + '10 = years'::interval
returns:
(a) = '1922-02-28 00:00:00'
while
SELECT = '1922-02-28'::date - '9 years 11 mons 28 = days'::interval
returns:
(b) '1912-02-29 = 00:00:00'
Please, note the difference of 1 = day between (a) and (b). 

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. 


Again, the issue can be seen this way: adding the interval = returned by SELECT age('1922-02-28'::date, = '1912-02-29'::date) to the original date. So:
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)!

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.


Kind= regards,
 Pietro Pugni

Il giorno 26 apr 2017, alle ore 20:29, David = G. Johnston <david.g.johnston@gmail.com> ha scritto:

On Wed, Apr 26, 2017 = at 11:23 AM, <pietro.pugni@gmail.com> wrote:
The following bug = has been logged on the website:

Bug reference:      14632
Logged by:          Pietro Pugni
Email address:      pietro.pugni@gmail.com
PostgreSQL version: 9.6.2
Operating system:   Ubuntu 16.04.2 LTS
Description:

The following query:
 SELECT '1912-02-29'::date + '10 years'::interval

returns:
 '1922-02-28 00:00:00'

while the reverse operation:
 SELECT '1922-02-28'::date - '10 years'::interval

returns:
 '1912-02-28 00:00:00'

instead of '1912-02-29 00:00:00'.

=E2=80=8BWhat part of = this do you consider to be a bug - and what should it do = instead?

David = J.=E2=80=8B

= --Apple-Mail=_E9B0556D-456F-452F-BF91-848A1DF09478--