pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Dean Rasheed <dean.a.rasheed@gmail.com>
Cc: John W Higgins <wishdev@gmail.com>
Cc: pgsql-hackers list <pgsql-hackers@lists.postgresql.org>
Subject: Re: Have I found an interval arithmetic bug?
Date: Wed, 28 Jul 2021 11:19:16 -0400
Message-ID: <20210728151916.GA15440@momjian.us> (raw)
In-Reply-To: <CAEZATCWaK0qdTiqxS=fM03VaKcfbYmSOV_JzqYDrVn2nSaMnFQ@mail.gmail.com>
References: <CALNJ-vRJUo62KNrQsiSsNAuPSBYpOXk8SPLh6r5jwHVzYai=HA@mail.gmail.com>
	<20210402234943.GB29126@momjian.us>
	<95D98B9E-CD54-4FAC-9574-D950E9741CAC@yugabyte.com>
	<20210403010229.GE29126@momjian.us>
	<20210727193614.GE19774@momjian.us>
	<456756.1627416114@sss.pgh.pa.us>
	<20210727211314.GA23040@momjian.us>
	<901454CE-2C0D-4C49-ADAB-A094C03B7CB1@yugabyte.com>
	<CAPhAwGyccJZT-mXzHU1216Q-Z_xY3q+4TixzZ2sZf3hiPFe5tg@mail.gmail.com>
	<CAEZATCWaK0qdTiqxS=fM03VaKcfbYmSOV_JzqYDrVn2nSaMnFQ@mail.gmail.com>

On Wed, Jul 28, 2021 at 08:42:31AM +0100, Dean Rasheed wrote:
> On Wed, 28 Jul 2021 at 00:08, John W Higgins <wishdev@gmail.com> wrote:
> >
> > It's nice to envision all forms of fancy calculations. But the fact is that
> >
> > '1.5 month'::interval * 2 != '3 month"::interval
> >
> 
> That's not exactly true. Even without the patch:
> 
> SELECT '1.5 month'::interval * 2 AS product,
>        '3 month'::interval AS expected,
>        justify_interval('1.5 month'::interval * 2) AS justified_product,
>        '1.5 month'::interval * 2 = '3 month'::interval AS equal;
> 
>     product     | expected | justified_product | equal
> ----------------+----------+-------------------+-------
>  2 mons 30 days | 3 mons   | 3 mons            | t
> (1 row)
> 
> So it's equal even without calling justify_interval() on the result.
> 
> FWIW, I remain of the opinion that the interval literal code should
> just spill down to lower units in all cases, just like the
> multiplication and division code, so that the results are consistent
> (barring floating point rounding errors) and explainable.

Here is a more minimal patch that doesn't change the spill-down units at
all, but merely documents it, and changes the spilldown to months to
round instead of truncate.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.

Attachments:

interval.diff (text/x-diff, 5.2K) download diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index 453115f942..bd938a675a 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -2840,15 +2840,17 @@ P <optional> <replaceable>years</replaceable>-<replaceable>months</replaceable>- </para> <para> - In the verbose input format, and in some fields of the more compact - input formats, field values can have fractional parts; for example - <literal>'1.5 week'</literal> or <literal>'01:02:03.45'</literal>. Such input is - converted to the appropriate number of months, days, and seconds - for storage. When this would result in a fractional number of - months or days, the fraction is added to the lower-order fields - using the conversion factors 1 month = 30 days and 1 day = 24 hours. - For example, <literal>'1.5 month'</literal> becomes 1 month and 15 days. - Only seconds will ever be shown as fractional on output. + Field values can have fractional parts; for example, <literal>'1.5 + weeks'</literal> or <literal>'01:02:03.45'</literal>. However, + because interval internally stores only three integer units (months, + days, seconds), fractional units must be spilled to smaller units. + For example, because months are approximated to equal 30 days, + fractional values of units greater than months is rounded to be the + nearest integer number of months. Fractional units of months or less + are computed to be an integer number of days and seconds, assuming + 24 hours per day. For example, <literal>'1.5 months'</literal> + becomes <literal>1 month 15 days</literal>. Only seconds will ever + be shown as fractional on output. </para> <para> diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index 54ae632de2..cb3fa85892 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -3306,29 +3306,25 @@ DecodeInterval(char **field, int *ftype, int nf, int range, case DTK_YEAR: tm->tm_year += val; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR); tmask = DTK_M(YEAR); break; case DTK_DECADE: tm->tm_year += val * 10; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR * 10; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 10); tmask = DTK_M(DECADE); break; case DTK_CENTURY: tm->tm_year += val * 100; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR * 100; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 100); tmask = DTK_M(CENTURY); break; case DTK_MILLENNIUM: tm->tm_year += val * 1000; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR * 1000; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 1000); tmask = DTK_M(MILLENNIUM); break; @@ -3565,7 +3561,7 @@ DecodeISO8601Interval(char *str, { case 'Y': tm->tm_year += val; - tm->tm_mon += (fval * MONTHS_PER_YEAR); + tm->tm_mon += rint(fval * MONTHS_PER_YEAR); break; case 'M': tm->tm_mon += val; @@ -3601,7 +3597,7 @@ DecodeISO8601Interval(char *str, return DTERR_BAD_FORMAT; tm->tm_year += val; - tm->tm_mon += (fval * MONTHS_PER_YEAR); + tm->tm_mon += rint(fval * MONTHS_PER_YEAR); if (unit == '\0') return 0; if (unit == 'T') diff --git a/src/interfaces/ecpg/pgtypeslib/interval.c b/src/interfaces/ecpg/pgtypeslib/interval.c index 02b3c47223..a7e530cb5d 100644 --- a/src/interfaces/ecpg/pgtypeslib/interval.c +++ b/src/interfaces/ecpg/pgtypeslib/interval.c @@ -155,7 +155,7 @@ DecodeISO8601Interval(char *str, { case 'Y': tm->tm_year += val; - tm->tm_mon += (fval * MONTHS_PER_YEAR); + tm->tm_mon += rint(fval * MONTHS_PER_YEAR); break; case 'M': tm->tm_mon += val; @@ -191,7 +191,7 @@ DecodeISO8601Interval(char *str, return DTERR_BAD_FORMAT; tm->tm_year += val; - tm->tm_mon += (fval * MONTHS_PER_YEAR); + tm->tm_mon += rint(fval * MONTHS_PER_YEAR); if (unit == '\0') return 0; if (unit == 'T') @@ -528,29 +528,25 @@ DecodeInterval(char **field, int *ftype, int nf, /* int range, */ case DTK_YEAR: tm->tm_year += val; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR); tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR); break; case DTK_DECADE: tm->tm_year += val * 10; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR * 10; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 10); tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR); break; case DTK_CENTURY: tm->tm_year += val * 100; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR * 100; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 100); tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR); break; case DTK_MILLENNIUM: tm->tm_year += val * 1000; - if (fval != 0) - tm->tm_mon += fval * MONTHS_PER_YEAR * 1000; + tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 1000); tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR); break;

view thread (95+ messages)  latest in thread

Message-ID: <20210728151916.GA15440@momjian.us>
Permalink:  ../20210728151916.GA15440@momjian.us/
Also on:    postgresql.org/message-id/20210728151916.GA15440@momjian.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: bruce@momjian.us, dean.a.rasheed@gmail.com, wishdev@gmail.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: Have I found an interval arithmetic bug?
  In-Reply-To: <20210728151916.GA15440@momjian.us>
  References: <CALNJ-vRJUo62KNrQsiSsNAuPSBYpOXk8SPLh6r5jwHVzYai=HA@mail.gmail.com> <20210402234943.GB29126@momjian.us> <95D98B9E-CD54-4FAC-9574-D950E9741CAC@yugabyte.com> <20210403010229.GE29126@momjian.us> <20210727193614.GE19774@momjian.us> <456756.1627416114@sss.pgh.pa.us> <20210727211314.GA23040@momjian.us> <901454CE-2C0D-4C49-ADAB-A094C03B7CB1@yugabyte.com> <CAPhAwGyccJZT-mXzHU1216Q-Z_xY3q+4TixzZ2sZf3hiPFe5tg@mail.gmail.com> <CAEZATCWaK0qdTiqxS=fM03VaKcfbYmSOV_JzqYDrVn2nSaMnFQ@mail.gmail.com> <20210728151916.GA15440@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox