pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRe: Have I found an interval arithmetic bug?
4+ messages / 2 participants
[nested] [flat]
* Re: Have I found an interval arithmetic bug?
@ 2021-05-08 02:23 Bruce Momjian <bruce@momjian.us>
parent: Zhihong Yu <zyu@yugabyte.com>
0 siblings, 1 reply; 4+ messages in thread
From: Bruce Momjian @ 2021-05-08 02:23 UTC (permalink / raw)
To: Zhihong Yu <zyu@yugabyte.com>; +Cc: Bryn Llewellyn <bryn@yugabyte.com>; Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>; Justin Pryzby <pryzby@telsasoft.com>; Isaac Morland <isaac.morland@gmail.com>; John W Higgins <wishdev@gmail.com>; PostgreSQL-development <pgsql-hackers@postgresql.org>
On Fri, May 7, 2021 at 07:23:42PM -0700, Zhihong Yu wrote:
> On Tue, Apr 13, 2021 at 10:55 AM Bryn Llewellyn <bryn@yugabyte.com> wrote:
> There’s no shipped function that does this, and this makes me suspect that
> I’d prefer to roll my own for any serious purpose.
>
> The existence of the three “justify” functions is, therefore, harmless.
>
> Bruce / Tom:
> Can we revisit this topic ?
I thought we agreed that the attached patch will be applied to PG 15.
--
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.5K) download
diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml
index 0e8ef958a9..0830128013 100644
--- a/doc/src/sgml/datatype.sgml
+++ b/doc/src/sgml/datatype.sgml
@@ -2770,15 +2770,13 @@ 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>. The fractional
+ parts are used to compute appropriate values for the next lower-order
+ internal fields (months, days, seconds). For example, <literal>'1.5
+ months'</literal> becomes <literal>1 month 15 days</literal>.
+ The fractional conversion factors used are 1 month = 30 days and 1 day
+ = 24 hours. 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..fb9a753223 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 02b3c47223..0b2fccbbe6 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 e4b1246f45..e8bc7a2337 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 days +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 6d532398bd..58bc06178c 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...";
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Have I found an interval arithmetic bug?
@ 2021-05-08 02:23 Zhihong Yu <zyu@yugabyte.com>
0 siblings, 1 reply; 4+ messages in thread
From: Zhihong Yu @ 2021-05-08 02:23 UTC (permalink / raw)
To: Bryn Llewellyn <bryn@yugabyte.com>; +Cc: Bruce Momjian <bruce@momjian.us>; Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>; Justin Pryzby <pryzby@telsasoft.com>; Isaac Morland <isaac.morland@gmail.com>; John W Higgins <wishdev@gmail.com>; PostgreSQL-development <pgsql-hackers@postgresql.org>
On Tue, Apr 13, 2021 at 10:55 AM Bryn Llewellyn <bryn@yugabyte.com> wrote:
> On 12-Apr-2021, at 17:25, Bruce Momjian <bruce@momjian.us> wrote:
>
> On Mon, Apr 12, 2021 at 05:20:43PM -0700, Bryn Llewellyn wrote:
>
> I’d argue that the fact that this:
>
> ('0.3 months'::interval) + ('0.7 months'::interval)
>
> Is reported as '30 days' and not '1 month' is yet another
> bug—precisely because of what I said in my previous email (sorry
> that I forked the thread) where I referred to the fact that, in the
> right test, adding 1 month gets a different answer than adding 30
> days.
>
>
> Flowing _up_ is what these functions do:
>
> \df *justify*
> List of functions
> Schema | Name | Result data type | Argument data types |
> Type
>
> ------------+------------------+------------------+---------------------+------
> pg_catalog | justify_days | interval | interval |
> func
> pg_catalog | justify_hours | interval | interval |
> func
> pg_catalog | justify_interval | interval | interval |
> func
>
> Yet another convincing reason to get rid of this flow down
> business altogether.
>
>
> We can certainly get rid of all downflow, which in the current patch is
> only when fractional internal units are specified.
>
> If some application wants to model flow-down, then it can do so with
> trivial programming and full control over its own definition of the
> rules.
>
>
> *“Yes please!” re Bruce’s “We can certainly get rid of all downflow, which
> in the current patch is only when fractional internal units are specified.”*
>
> Notice that a user who creates interval values explicitly only by using
> “make_interval()” will see no behavior change.
>
> There’s another case of up-flow. When you subtract one timestamp value
> from another, and when they’re far enough apart, then the (internal
> representation of the) resulting interval value has both a seconds
> component and a days component. (But never, in my tests, a months
> component.) I assume that the implementation first gets the difference
> between the two timestamp values in seconds using (the moral equivalent of)
> “extract epoch”. And then, if this is greater than 24*60*60, it implements
> up-flow using the “rule-of-24”—never mind that this means that if you add
> the answer back to the timestamp value that you subtracted, then you might
> not get the timestamp value from which you subtracted. (This happens around
> a DST change and has been discussed earlier in the thread.)
>
> The purpose of these three “justify” functions is dubious. I think that
> it’s best to think of the 3-component interval vector like an [x, y, z]
> vector in 3d geometric space, where the three coordinates are not mutually
> convertible because each has unique semantics. This point has been
> rehearsed many times in this long thread.
>
> Having said this, it isn’t hard to understand the rules that the functions
> implement. And, most importantly, their use is voluntary. They are, though,
> no more than shipped and documented wrappers for a few special cases. A
> user could so easily write their own function like this:
>
> 1. Compute the values of the three components of the internal
> representation of the passed-in interval value using the “extract” feature
> and some simple arithmetic.
>
> 2. Derive the [minutes, days, seconds] values of a new representation
> using whatever rules you feel for.
>
> 3. Use these new values to create the return interval value.
>
> For example, I might follow a discipline to use interval values that have
> only one of the three components of the internal representation non-zero.
> It’s easy to use the “domain” feature for this. (I can use these in any
> context where I can use the shipped interval.) I could then write a
> function to convert a “pure seconds” value of my_interval to a “pure years”
> value. And I could implement my own rules:
>
> — Makes sense only for a large number of seconds that comes out to at
> least five years (else assert failure).
>
> — Converts seconds to years using the rule that 1 year is, on
> average, 365.25*24*60*60 seconds, and then truncates it.
>
> There’s no shipped function that does this, and this makes me suspect that
> I’d prefer to roll my own for any serious purpose.
>
> The existence of the three “justify” functions is, therefore, harmless.
>
>
Bruce / Tom:
Can we revisit this topic ?
Cheers
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Have I found an interval arithmetic bug?
@ 2021-05-08 02:39 Zhihong Yu <zyu@yugabyte.com>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 4+ messages in thread
From: Zhihong Yu @ 2021-05-08 02:39 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Bryn Llewellyn <bryn@yugabyte.com>; Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>; Justin Pryzby <pryzby@telsasoft.com>; Isaac Morland <isaac.morland@gmail.com>; John W Higgins <wishdev@gmail.com>; PostgreSQL-development <pgsql-hackers@postgresql.org>
On Fri, May 7, 2021 at 7:23 PM Bruce Momjian <bruce@momjian.us> wrote:
> On Fri, May 7, 2021 at 07:23:42PM -0700, Zhihong Yu wrote:
> > On Tue, Apr 13, 2021 at 10:55 AM Bryn Llewellyn <bryn@yugabyte.com>
> wrote:
> > There’s no shipped function that does this, and this makes me
> suspect that
> > I’d prefer to roll my own for any serious purpose.
> >
> > The existence of the three “justify” functions is, therefore,
> harmless.
> >
> > Bruce / Tom:
> > Can we revisit this topic ?
>
> I thought we agreed that the attached patch will be applied to PG 15.
>
Good to know.
Hopefully it lands soon.
> --
> Bruce Momjian <bruce@momjian.us> https://momjian.us
> EDB https://enterprisedb.com
>
> If only the physical world exists, free will is an illusion.
>
>
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Have I found an interval arithmetic bug?
@ 2021-05-08 02:42 Bruce Momjian <bruce@momjian.us>
parent: Zhihong Yu <zyu@yugabyte.com>
0 siblings, 0 replies; 4+ messages in thread
From: Bruce Momjian @ 2021-05-08 02:42 UTC (permalink / raw)
To: Zhihong Yu <zyu@yugabyte.com>; +Cc: Bryn Llewellyn <bryn@yugabyte.com>; Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>; Justin Pryzby <pryzby@telsasoft.com>; Isaac Morland <isaac.morland@gmail.com>; John W Higgins <wishdev@gmail.com>; PostgreSQL-development <pgsql-hackers@postgresql.org>
On Fri, May 7, 2021 at 07:39:31PM -0700, Zhihong Yu wrote:
>
>
> On Fri, May 7, 2021 at 7:23 PM Bruce Momjian <bruce@momjian.us> wrote:
>
> On Fri, May 7, 2021 at 07:23:42PM -0700, Zhihong Yu wrote:
> > On Tue, Apr 13, 2021 at 10:55 AM Bryn Llewellyn <bryn@yugabyte.com>
> wrote:
> > There’s no shipped function that does this, and this makes me suspect
> that
> > I’d prefer to roll my own for any serious purpose.
> >
> > The existence of the three “justify” functions is, therefore,
> harmless.
> >
> > Bruce / Tom:
> > Can we revisit this topic ?
>
> I thought we agreed that the attached patch will be applied to PG 15.
>
>
> Good to know.
>
> Hopefully it lands soon.
It will be applied in June/July, but will not appear in a release until
Sept/Oct, 2022. Sorry.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2021-05-08 02:42 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-05-08 02:23 Re: Have I found an interval arithmetic bug? Zhihong Yu <zyu@yugabyte.com>
2021-05-08 02:23 ` Bruce Momjian <bruce@momjian.us>
2021-05-08 02:39 ` Zhihong Yu <zyu@yugabyte.com>
2021-05-08 02:42 ` Bruce Momjian <bruce@momjian.us>
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