Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3Vgq-0001rU-Nm for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 22:45:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d3Vgp-0008U7-GX for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 22:45:39 +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 1d3Vg6-0007CN-Vm for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 22:44:55 +0000 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1d3Vg1-00054V-QC for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 22:44:54 +0000 Received: by mail-wm0-x229.google.com with SMTP id w64so2465497wma.0 for ; Wed, 26 Apr 2017 15:44:49 -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=HAFZSqTqB7jlJpI8vPya0egJhNCvuO008jT+aRiUaW0=; b=fVlJwJLjwEeJFSPLXNWsQC3BvleosKsf4nIQiz8LlFX0B31JDC05KfN+dTK3wjUIRu +LBVfKIM2VSwLPjGfmIj/P47CWNfNQwkZWxJFmLGRX47lvLRmUOTkJxlKvaJgUEZ3vxC EZTUvuDSBd17YxBdMufVQm7uavq/jc7AeZYTEa1Zp5VQYC4X4v+dhQSyBl2MJ4wrOk/F Rnzrv1jzbiYVv6YJ5MK4UOXjQBPXZw1MhXtB0MIg/JuZdd+KKcrweF6VRVr8Fa6o3P/X 7k/KUjpy11jl7nT+nqk03iOttFa4zJK/EpgK9qUfdIspLk7uMaPJuWGYn6t/qvd37KsF Taew== 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=HAFZSqTqB7jlJpI8vPya0egJhNCvuO008jT+aRiUaW0=; b=RBRtsqwhsMglED6cT0/WT9Hy3beQV1QjVhgQ+ME+ZGdZICa4VkrMDHhcRfpJhjjDqA +twJq4CeUNl6FT7nx9sGKfABcDdLtIJQmhPdPJ50p6Ch8xBzCJGtlFrX50ywVsuwrxM1 9QvNa6RKQeWjbdh6V+zjwQuwhFM4Mlajyaq5yNvEXQKn6cOcx9viuC2/FohDyJl2LkiH 3T2ZbMO81VyG4u6RuCRodL4XZ/IfFVf+sd0ncf4+hH70AsGtHLA6xqBvdAU/YJM8N3sc pgL9Srxyt4zIeV5/GkA/TYXVJBOX/GLsnPCb6al1B55O/t73JzadvDHuDy1eExbGS4aQ ax2g== X-Gm-Message-State: AN3rC/4hgIzULe/sOzhkM4xeDXIHqkV0bLiprNWB7n2uJ5iQKbtVoH/+ thkZqIz5RJoMug== X-Received: by 10.28.23.198 with SMTP id 189mr109178wmx.38.1493246686864; Wed, 26 Apr 2017 15:44:46 -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 m201sm1106564wmd.15.2017.04.26.15.44.45 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 26 Apr 2017 15:44:45 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_4F10908D-4500-4FD5-B844-7DC5436CA4CB" 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: <9066.1493241918@sss.pgh.pa.us> Date: Thu, 27 Apr 2017 00:44:40 +0200 Cc: "David G. Johnston" , "pgsql-bugs@postgresql.org" Message-Id: References: <20170426182322.24371.95224@wrigleys.postgresql.org> <5752C491-B0EC-4D1E-AF97-5E1C8C793A8A@gmail.com> <9066.1493241918@sss.pgh.pa.us> To: Tom Lane 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=_4F10908D-4500-4FD5-B844-7DC5436CA4CB Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Pietro Pugni writes: >> Adding 10 years to 1912-02-29 returns 1922-02-29, as expected. >=20 > No, it doesn't, and it would be wrong to do so because there is no > such date; 1922 wasn't a leap year. I messed up 28 with 29 in that statement, sorry. Look at the queries = instead, they are correct. I=E2=80=99ll try to shed some light on the inconsistency of age() versus = intervals used in conjunction with leap years. The whole point (for leap years) is: - inconsistency (adding and subtracting the same quantity to a date = should provide the date itself, but it doesn't); - wrong results (we loose days) - ambiguity (we know that the meaning of an interval is relative to the = referred date, but we have two different meanings if we add an interval = to a date and obtain x and then subtract the same interval and obtain = y=3Dx-1) The day loss can be easily seen by recursing the addition of the result = returned by age: SELECT age(age('1922-02-28'::date, '1912-02-29'::date) + = '1912-02-29'::date, '1912-02-29'::date) + '1912-02-29'::date ?column? =20 --------------------- 1922-02-24 00:00:00 This is totally wrong from a semantic point of view and it should return = '1922-02-28 00:00:00'. Instead we get a total loss of 4 days. As reported in my previous mail, adding 10 years to a leap date returns = the expected value: SELECT '1912-02-29'::date + '10 years'::interval ?column? =20 --------------------- 1922-02-28 00:00:00 while subtracting 10 years from the returned date has a totally = different meaning for Postgres and misses 1 day: SELECT '1922-02-28'::date - '10 years'::interval ?column? =20 --------------------- 1902-02-28 00:00:00 This is inconsistent (and wrong) against the result provided by this = query: select age('1922-02-28'::date, '1912-02-29'::date) age =20 ------------------------- 9 years 11 mons 28 days I expect it to return =E2=80=9910 years=E2=80=99 instead. It=E2=80=99s = inconsistent also with this query: SELECT '1912-02-29'::date + age('1922-02-28'::date, '1912-02-29'::date) ?column? =20 --------------------- 1922-02-26 00:00:00 because it returns 1922-02-26 instead of 1922-02-28! age() returns the = correct value if we look at the docs but it=E2=80=99s wrong from a = logical point of view if we consider it for what it should be: a = function returning age between dates and not the number of days. The only workaround I see is subtracting 1 day from the =E2=80=9Cborn=E2=80= =9D date: SELECT age('1922-02-28'::date, '1912-02-29'::date - '1 day'::interval) age =20 ---------- 10 years but I=E2=80=99ll apply this caveat only to leap years, so I=E2=80=99ll = need a function or a case statement. >> 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 > As remarked somewhere in our documentation, you'd need to take that up > with the Pope, not with us database hackers. We didn't invent the > calendar rules. Postgres does a great job with dates and this is the first bug I find, = but it=E2=80=99s very frustrating because it leads to errors, especially = when dealing with a lot of records. The docs aren=E2=80=99t exhaustive = on this topic IMHO. Kind regards Pietro Pugni --Apple-Mail=_4F10908D-4500-4FD5-B844-7DC5436CA4CB Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Pietro Pugni <pietro.pugni@gmail.com> writes:
Adding = 10 years to 1912-02-29 returns 1922-02-29, as expected.

No, it doesn't, and it would be = wrong to do so because there is no
such date; 1922 wasn't = a leap year.

I messed up 28 = with 29 in that statement, sorry. Look at the queries instead, they are = correct.
I=E2=80=99= ll try to shed some light on the inconsistency of age() versus intervals = used in conjunction with leap years.

The whole point (for = leap years) is:
 - = inconsistency (adding and subtracting the same quantity to a date should = provide the date itself, but it doesn't);
 - wrong results (we loose days)
 - ambiguity (we know that the meaning of an interval is = relative to the referred date, but we have two different meanings if we = add an interval to a date and obtain x and then subtract the same = interval and obtain y=3Dx-1)

The = day loss can be easily seen by recursing the addition of the result = returned by age:
SELECT age(age('1922-02-28'::date, = '1912-02-29'::date) + '1912-02-29'::date, '1912-02-29'::date) + = '1912-02-29'::date
  =    ?column?       
---------------------
 1922-02-24 00:00:00

This = is totally wrong from a semantic point of view and it should = return '1922-02-28 00:00:00'. Instead we get a total = loss of 4 days.


As reported = in my previous mail, adding 10 years  to a leap date returns the = expected value:
SELECT = '1912-02-29'::date + '10 years'::interval
     ?column?     =   
---------------------
 1922-02-28 00:00:00

while subtracting 10 years from the returned date has a totally = different meaning for Postgres and misses 1 day:
SELECT '1922-02-28'::date - '10 = years'::interval
     ?column? =       
---------------------
 1902-02-28 = 00:00:00

This is = inconsistent (and wrong) against the result provided by this = query:
select age('1922-02-28'::date, = '1912-02-29'::date)
      =      age         =   
-------------------------
 9 years 11 mons 28 = days

I expect it to return = =E2=80=9910 years=E2=80=99 instead. It=E2=80=99s inconsistent also with = this query:
SELECT '1912-02-29'::date + = age('1922-02-28'::date, '1912-02-29'::date)
      ?column?   =     
---------------------
 1922-02-26 = 00:00:00

because it returns = 1922-02-26 instead of 1922-02-28! age() returns the correct value if we = look at the docs but it=E2=80=99s wrong from a logical point of view if = we consider it for what it should be: a function = returning age between dates and not the number of days.

The only workaround I see is subtracting 1 day from the = =E2=80=9Cborn=E2=80=9D date:
SELECT = age('1922-02-28'::date, '1912-02-29'::date - '1 = day'::interval)
   age   =  
----------
 10 = years

but I=E2=80=99ll apply = this caveat only to leap years, so I=E2=80=99ll need a function or a = case statement.


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.

As remarked = somewhere in our documentation, you'd need to take that up
with the Pope, not with us database hackers.  We didn't = invent the
calendar rules.

Postgres does a great job with = dates and this is the first bug I find, but it=E2=80=99s very = frustrating because it leads to errors, especially when dealing with a = lot of records. The docs aren=E2=80=99t exhaustive on this topic = IMHO.

Kind = regards
 Pietro = Pugni


= --Apple-Mail=_4F10908D-4500-4FD5-B844-7DC5436CA4CB--