Fix for interval division/multiplication - Mailing list pgsql-patches

From Bruce Momjian
Subject Fix for interval division/multiplication
Date
Msg-id 200507200349.j6K3nCT05147@candle.pha.pa.us
Whole thread Raw
List pgsql-patches
I working on the 'date' interval patch, I found a bug in handling of
interval division and multiplication.  Look at this:

    test=> select '4 months'::interval / 5;
       ?column?
    ---------------
     1 mon -6 days
    (1 row)

With the attached fix, it returns the right value:

    test=> select '4 months'::interval / 5;
     ?column?
    ----------
     24 days
    (1 row)

The bug is the use of rint() to round the division, but then using the
rounded value to find the remainder to pass down to the time field.

The attached patch has been applied to current CVS.  Should I backpatch
this fix?

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/utils/adt/timestamp.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v
retrieving revision 1.132
diff -c -c -r1.132 timestamp.c
*** src/backend/utils/adt/timestamp.c    12 Jul 2005 16:04:58 -0000    1.132
--- src/backend/utils/adt/timestamp.c    20 Jul 2005 03:38:52 -0000
***************
*** 2201,2207 ****
      result->time += (months - result->month) * INT64CONST(30) *
                      USECS_PER_DAY;
  #else
!     result->month = rint(months);
      result->time = JROUND(span1->time * factor);
      /* evaluate fractional months as 30 days */
      result->time += JROUND((months - result->month) * 30 * SECS_PER_DAY);
--- 2201,2207 ----
      result->time += (months - result->month) * INT64CONST(30) *
                      USECS_PER_DAY;
  #else
!     result->month = (int)months;
      result->time = JROUND(span1->time * factor);
      /* evaluate fractional months as 30 days */
      result->time += JROUND((months - result->month) * 30 * SECS_PER_DAY);
***************
*** 2246,2252 ****
                      INT64CONST(30) * USECS_PER_DAY) / factor;
  #else
      months = span->month / factor;
!     result->month = rint(months);
      result->time = JROUND(span->time / factor);
      /* evaluate fractional months as 30 days */
      result->time += JROUND((months - result->month) * 30 * SECS_PER_DAY);
--- 2246,2252 ----
                      INT64CONST(30) * USECS_PER_DAY) / factor;
  #else
      months = span->month / factor;
!     result->month = (int)months;
      result->time = JROUND(span->time / factor);
      /* evaluate fractional months as 30 days */
      result->time += JROUND((months - result->month) * 30 * SECS_PER_DAY);

pgsql-patches by date:

Previous
From: Tom Lane
Date:
Subject: Re: Writing Commit Status hint bits (was Re: [HACKERS] Constant WAL replay)
Next
From: Simon Riggs
Date:
Subject: Re: Writing Commit Status hint bits (was Re: [HACKERS] Constant