pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Zhihong Yu <zyu@yugabyte.com>
Cc: Isaac Morland <isaac.morland@gmail.com>
Cc: John W Higgins <wishdev@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: Have I found an interval arithmetic bug?
Date: Fri, 2 Apr 2021 23:00:12 -0400
Message-ID: <20210403030012.GH29126@momjian.us> (raw)
In-Reply-To: <CALNJ-vTCW_Ot1jpz+o-HXYRso1XwOZHmOMYSD6AeWqObszFtNw@mail.gmail.com>
References: <BDAE4B56-3337-45A2-AC8A-30593849D6C0@yugabyte.com>
	<20210402180549.GF9270@momjian.us>
	<CAPhAwGy9X_fqPz0ekcq0+PwUZZsitcwtuNt-qhajC5pnYtjK+g@mail.gmail.com>
	<20210402235815.GC29126@momjian.us>
	<CALNJ-vRZDWgcevmjoECOPshNbtvMbTPK09NJ7J8FSHJzdySiJA@mail.gmail.com>
	<CALNJ-vTwCbt+dTeC+5GcrR7CCcBWnz+zSeLGkViEyJSg=UdCXQ@mail.gmail.com>
	<CAMsGm5errRXqOOt2r-uSDYqn301s_tQMQdJHLPmTXcc+EOLEfQ@mail.gmail.com>
	<CALNJ-vRF71-c8oubm96wx4qBz1TQr2Dm5ocQAYbHmtmf_gBTmg@mail.gmail.com>
	<20210403022126.GG29126@momjian.us>
	<CALNJ-vTCW_Ot1jpz+o-HXYRso1XwOZHmOMYSD6AeWqObszFtNw@mail.gmail.com>

On Fri, Apr  2, 2021 at 07:53:35PM -0700, Zhihong Yu wrote:
> Bruce:
> In src/interfaces/ecpg/pgtypeslib/interval.c, how about the following places ?
> 
> Around line 158:
>                 case 'Y':
>                     tm->tm_year += val;
>                     tm->tm_mon += (fval * MONTHS_PER_YEAR);
> 
> Around line 194:
>                     tm->tm_year += val;
>                     tm->tm_mon += (fval * MONTHS_PER_YEAR);
> 
> Is rint() needed for these two cases ?

Ah, yes, good point.  Updated patch attached.

-- 
  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.0K) download diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index 889077f55c..d5b3705145 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -526,7 +526,6 @@ AdjustFractDays(double frac, struct pg_tm *tm, fsec_t *fsec, int scale) extra_days = (int) frac; tm->tm_mday += extra_days; frac -= extra_days; - AdjustFractSeconds(frac, tm, fsec, SECS_PER_DAY); } /* Fetch a fractional-second value with suitable error checking */ @@ -3307,28 +3306,28 @@ 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 +3564,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 +3600,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 4245016c8e..fccb9765ae 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') @@ -529,28 +529,28 @@ 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; diff --git a/src/test/regress/expected/interval.out b/src/test/regress/expected/interval.out index c5ffa9f2cc..bd35c930b0 100644 --- a/src/test/regress/expected/interval.out +++ b/src/test/regress/expected/interval.out @@ -34,10 +34,10 @@ SELECT INTERVAL '-1 days +02:03' AS "22 hours ago..."; -1 day +02:03:00 (1 row) -SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours"; - Ten days twelve hours ------------------------ - 10 days 12:00:00 +SELECT INTERVAL '1.5 weeks' AS "Ten days"; + Ten days +---------- + 10 days (1 row) SELECT INTERVAL '1.5 months' AS "One month 15 days"; diff --git a/src/test/regress/sql/interval.sql b/src/test/regress/sql/interval.sql index 11c1929bef..bd141e7b52 100644 --- a/src/test/regress/sql/interval.sql +++ b/src/test/regress/sql/interval.sql @@ -11,7 +11,7 @@ SELECT INTERVAL '+02:00' AS "Two hours"; SELECT INTERVAL '-08:00' AS "Eight hours"; SELECT INTERVAL '-1 +02:03' AS "22 hours ago..."; SELECT INTERVAL '-1 days +02:03' AS "22 hours ago..."; -SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours"; +SELECT INTERVAL '1.5 weeks' AS "Ten days"; SELECT INTERVAL '1.5 months' AS "One month 15 days"; SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";

view thread (95+ messages)  latest in thread

Message-ID: <20210403030012.GH29126@momjian.us>
Permalink:  ../20210403030012.GH29126@momjian.us/
Also on:    postgresql.org/message-id/20210403030012.GH29126@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, zyu@yugabyte.com, isaac.morland@gmail.com, wishdev@gmail.com, pgsql-hackers@postgresql.org
  Subject: Re: Have I found an interval arithmetic bug?
  In-Reply-To: <20210403030012.GH29126@momjian.us>
  References: <BDAE4B56-3337-45A2-AC8A-30593849D6C0@yugabyte.com> <20210402180549.GF9270@momjian.us> <CAPhAwGy9X_fqPz0ekcq0+PwUZZsitcwtuNt-qhajC5pnYtjK+g@mail.gmail.com> <20210402235815.GC29126@momjian.us> <CALNJ-vRZDWgcevmjoECOPshNbtvMbTPK09NJ7J8FSHJzdySiJA@mail.gmail.com> <CALNJ-vTwCbt+dTeC+5GcrR7CCcBWnz+zSeLGkViEyJSg=UdCXQ@mail.gmail.com> <CAMsGm5errRXqOOt2r-uSDYqn301s_tQMQdJHLPmTXcc+EOLEfQ@mail.gmail.com> <CALNJ-vRF71-c8oubm96wx4qBz1TQr2Dm5ocQAYbHmtmf_gBTmg@mail.gmail.com> <20210403022126.GG29126@momjian.us> <CALNJ-vTCW_Ot1jpz+o-HXYRso1XwOZHmOMYSD6AeWqObszFtNw@mail.gmail.com> <20210403030012.GH29126@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