From: Bruce Momjian <bruce@momjian.us>
To: Robert Haas <robertmhaas@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: 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: Fri, 30 Jul 2021 15:55:05 -0400
Message-ID: <20210730195505.GE9600@momjian.us> (raw)
In-Reply-To: <CA+TgmoYAdZgreJTpEhzE-QS59iVMZkTjLY+2Da63K6-C8uuAJw@mail.gmail.com>
References: <CAPhAwGyccJZT-mXzHU1216Q-Z_xY3q+4TixzZ2sZf3hiPFe5tg@mail.gmail.com>
<CAEZATCWaK0qdTiqxS=fM03VaKcfbYmSOV_JzqYDrVn2nSaMnFQ@mail.gmail.com>
<20210728151916.GA15440@momjian.us>
<20210730160439.GZ9600@momjian.us>
<20210730160854.GA9600@momjian.us>
<1008623.1627663774@sss.pgh.pa.us>
<20210730190313.GC9600@momjian.us>
<CA+TgmobqWU=eS1DGs-MomKJTwY9agfMF5NBN20tHBsyMWKuAmA@mail.gmail.com>
<20210730192010.GD9600@momjian.us>
<CA+TgmoYAdZgreJTpEhzE-QS59iVMZkTjLY+2Da63K6-C8uuAJw@mail.gmail.com>
On Fri, Jul 30, 2021 at 03:47:53PM -0400, Robert Haas wrote:
> On Fri, Jul 30, 2021 at 3:20 PM Bruce Momjian <bruce@momjian.us> wrote:
> > Uh, what do you suggest then? You wanted the years/months fixed, and
> > rounding at spill stop time makes sense, and fixes the problem.
>
> Hmm, maybe I misunderstood. Are you saying that you think the patch
> will fix cases like interval '-1.7 years 29.4 months' and interval
> '29.4 months -1.7 years' to produce the same answer without changing
> any other cases? I had the impression that you were proposing a bigger
Yes, tests from the oringal email:
SELECT interval '-1.7 years 29.4 months';
interval
----------------
9 mons 12 days
(1 row)
SELECT interval '29.4 months -1.7 years';
interval
----------------
9 mons 12 days
(1 row)
SELECT interval '-1.7 years' + interval '29.4 months';
?column?
----------------
9 mons 12 days
(1 row)
SELECT interval '29.4 months' + interval '-1.7 years';
?column?
----------------
9 mons 12 days
> change to the rules for converting fractional units to units of lower
> type, particularly because Tom called it an "API change".
The API change is to _round_ units greater than months to integeral
month values; we currently truncate. Changing the spill behavior has
been rejected.
> For some reason I can't apply the patch locally.
>
> [rhaas pgsql]$ patch -p1 < ~/Downloads/interval.diff
> (Stripping trailing CRs from patch.)
> patching file doc/src/sgml/datatype.sgml
> (Stripping trailing CRs from patch.)
> patching file src/backend/utils/adt/datetime.c
> patch: **** malformed patch at line 90: @@ -3601,7 +3597,7 @@
> DecodeISO8601Interval(char *str,
Uh, here is the patch again, in case that helps.
--
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, 6.1K) downloaddiff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgmlindex 453115f942..50a2c8e5f1 100644--- a/doc/src/sgml/datatype.sgml+++ b/doc/src/sgml/datatype.sgml@@ -2840,15 +2840,18 @@ 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, microseconds), fractional units must be spilled to smaller+ units. Fractional parts of units greater than months is rounded to+ be an integer number of months, e.g. <literal>'1.5 years'</literal>+ becomes <literal>'1 year 6 mons'</literal>. Fractional parts of+ weeks and days are computed to be an integer number of days and+ microseconds, assuming 30 days per month and 24 hours per day, e.g.,+ <literal>'1.75 months'</literal> becomes <literal>1 mon 22 days+ 12:00:00</literal>. Only seconds will ever be shown as fractional+ on output.
</para>
<para>
@@ -2892,10 +2895,10 @@ P <optional> <replaceable>years</replaceable>-<replaceable>months</replaceable>-
<para>
Internally <type>interval</type> values are stored as months, days,
- and seconds. This is done because the number of days in a month+ and microseconds. This is done because the number of days in a month
varies, and a day can have 23 or 25 hours if a daylight savings
time adjustment is involved. The months and days fields are integers
- while the seconds field can store fractions. Because intervals are+ while the microseconds field can store fractional seconds. Because intervals are
usually created from constant strings or <type>timestamp</type> subtraction,
this storage method works well in most cases, but can cause unexpected
results:
diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.cindex 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.cindex 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;
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, robertmhaas@gmail.com, tgl@sss.pgh.pa.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: <20210730195505.GE9600@momjian.us>
References: <CAPhAwGyccJZT-mXzHU1216Q-Z_xY3q+4TixzZ2sZf3hiPFe5tg@mail.gmail.com> <CAEZATCWaK0qdTiqxS=fM03VaKcfbYmSOV_JzqYDrVn2nSaMnFQ@mail.gmail.com> <20210728151916.GA15440@momjian.us> <20210730160439.GZ9600@momjian.us> <20210730160854.GA9600@momjian.us> <1008623.1627663774@sss.pgh.pa.us> <20210730190313.GC9600@momjian.us> <CA+TgmobqWU=eS1DGs-MomKJTwY9agfMF5NBN20tHBsyMWKuAmA@mail.gmail.com> <20210730192010.GD9600@momjian.us> <CA+TgmoYAdZgreJTpEhzE-QS59iVMZkTjLY+2Da63K6-C8uuAJw@mail.gmail.com> <20210730195505.GE9600@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