Unix Technical Forum

Re: Interval month, week -> day

This is a discussion on Re: Interval month, week -> day within the Pgsql Patches forums, part of the PostgreSQL category; --> Michael Glaesemann <grzm@seespotcode.net> writes: > I came across some behavior that seems counterintuitive to me: > test=# select '1.5 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql Patches

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-18-2008, 08:56 AM
Tom Lane
 
Posts: n/a
Default Re: Interval month, week -> day

Michael Glaesemann <grzm@seespotcode.net> writes:
> I came across some behavior that seems counterintuitive to me:


> test=# select '1.5 mon'::interval;
> interval
> -----------------
> 1 mon 360:00:00
> (1 row)


> With the time/day/month interval struct introduced in 8.1, I'd expect
> this to return '1 mon 15 days'. The reason is that the DecodeInterval
> converts fractional months to time directly, rather than cascading
> first to days.


I agree that this seems like an oversight in the original
months/days/seconds patch, rather than behavior we want to keep.
But is DecodeInterval the only place with the problem? My recollection
is that there's a certain amount of redundancy in the datetime code ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-18-2008, 08:56 AM
Michael Glaesemann
 
Posts: n/a
Default Re: Interval month, week -> day


On Sep 1, 2006, at 9:12 , Tom Lane wrote:

> Michael Glaesemann <grzm@seespotcode.net> writes:
>> I came across some behavior that seems counterintuitive to me:

>
>> test=# select '1.5 mon'::interval;
>> interval
>> -----------------
>> 1 mon 360:00:00
>> (1 row)

>
>> With the time/day/month interval struct introduced in 8.1, I'd expect
>> this to return '1 mon 15 days'. The reason is that the DecodeInterval
>> converts fractional months to time directly, rather than cascading
>> first to days.

>
> I agree that this seems like an oversight in the original
> months/days/seconds patch, rather than behavior we want to keep.
> But is DecodeInterval the only place with the problem? My
> recollection
> is that there's a certain amount of redundancy in the datetime
> code ...


I'll check on this tonight. Any idea where I might start to look?

Michael Glaesemann
grzm seespotcode net




---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-18-2008, 08:56 AM
Tom Lane
 
Posts: n/a
Default Re: Interval month, week -> day

Michael Glaesemann <grzm@seespotcode.net> writes:
> On Sep 1, 2006, at 9:12 , Tom Lane wrote:
>> I agree that this seems like an oversight in the original
>> months/days/seconds patch, rather than behavior we want to keep.
>> But is DecodeInterval the only place with the problem?


> I'll check on this tonight. Any idea where I might start to look?


I'd look at the input routines for all the datetime types and see where
they go. It's entirely possible that DecodeInterval is the only place
with the problem, but I'd not assume that without looking.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-18-2008, 08:57 AM
Michael Glaesemann
 
Posts: n/a
Default Re: Interval month, week -> day


On Sep 1, 2006, at 9:32 , Tom Lane wrote:

> Michael Glaesemann <grzm@seespotcode.net> writes:
>> On Sep 1, 2006, at 9:12 , Tom Lane wrote:
>>> I agree that this seems like an oversight in the original
>>> months/days/seconds patch, rather than behavior we want to keep.
>>> But is DecodeInterval the only place with the problem?

>
>> I'll check on this tonight. Any idea where I might start to look?

>
> I'd look at the input routines for all the datetime types and see
> where
> they go. It's entirely possible that DecodeInterval is the only place
> with the problem, but I'd not assume that without looking.


AFAICS, DecodeInterval is the only place that needed changing. I've
looked through datetime.c, timestamp.c, date.c, and nabstime.c, and
don't see anything else. It makes sense, too, as the only place where
you could have weeks or non-integer months is during Interval input
or interval multiplication/division. The pg_tm struct, which is used
in time(stamp)?(tz)?/interval arithmetic only has integral months and
no weeks component, so that shouldn't cause any problems. So, I think
that's about it.

Michael Glaesemann
grzm seespotcode net




---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-18-2008, 08:57 AM
Michael Glaesemann
 
Posts: n/a
Default Re: Interval month, week -> day


On Sep 3, 2006, at 20:00 , Michael Glaesemann wrote:

>
> On Sep 1, 2006, at 9:32 , Tom Lane wrote:
>
>> Michael Glaesemann <grzm@seespotcode.net> writes:
>>> On Sep 1, 2006, at 9:12 , Tom Lane wrote:
>>>> I agree that this seems like an oversight in the original
>>>> months/days/seconds patch, rather than behavior we want to keep.
>>>> But is DecodeInterval the only place with the problem?

>>
>>> I'll check on this tonight. Any idea where I might start to look?

>>
>> I'd look at the input routines for all the datetime types and see
>> where
>> they go. It's entirely possible that DecodeInterval is the only
>> place
>> with the problem, but I'd not assume that without looking.

>
> AFAICS, DecodeInterval is the only place that needed changing. I've
> looked through datetime.c, timestamp.c, date.c, and nabstime.c, and
> don't see anything else. It makes sense, too, as the only place
> where you could have weeks or non-integer months is during Interval
> input or interval multiplication/division. The pg_tm struct, which
> is used in time(stamp)?(tz)?/interval arithmetic only has integral
> months and no weeks component, so that shouldn't cause any
> problems. So, I think that's about it.


I realized there might be something in ecpg, and there was. I've
updated the ecpg DecodeInterval to match. However, I haven't been
able to get ecpg make check to work, so that part's untested.

Michael Glaesemann
grzm seespotcode net


Index: src/backend/utils/adt/datetime.c
================================================== =================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/datetime.c,v
retrieving revision 1.169
diff -c -r1.169 datetime.c
*** src/backend/utils/adt/datetime.c 25 Jul 2006 03:51:21 -0000 1.169
--- src/backend/utils/adt/datetime.c 3 Sep 2006 23:55:34 -0000
***************
*** 2920,2935 ****
tm->tm_mday += val * 7;
if (fval != 0)
{
! int sec;
!
! fval *= 7 * SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
}
tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
break;
--- 2920,2942 ----
tm->tm_mday += val * 7;
if (fval != 0)
{
! int extra_days;
! fval *= 7;
! extra_days = (int32) fval;
! tm->tm_mday += extra_days;
! fval -= extra_days;
! if (fval != 0)
! {
! int sec;
! fval *= SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
+ }
}
tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
break;
***************
*** 2938,2953 ****
tm->tm_mon += val;
if (fval != 0)
{
! int sec;
!
! fval *= DAYS_PER_MONTH * SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
}
tmask = DTK_M(MONTH);
break;
--- 2945,2967 ----
tm->tm_mon += val;
if (fval != 0)
{
! int day;
! fval *= DAYS_PER_MONTH;
! day = fval;
! tm->tm_mday += day;
! fval -= day;
! if (fval != 0)
! {
! int sec;
! fval *= SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
+ }
}
tmask = DTK_M(MONTH);
break;
Index: src/interfaces/ecpg/pgtypeslib/interval.c
================================================== =================
RCS file: /projects/cvsroot/pgsql/src/interfaces/ecpg/pgtypeslib/
interval.c,v
retrieving revision 1.32
diff -c -r1.32 interval.c
*** src/interfaces/ecpg/pgtypeslib/interval.c 6 Jun 2006 11:31:55
-0000 1.32
--- src/interfaces/ecpg/pgtypeslib/interval.c 3 Sep 2006 23:55:34 -0000
***************
*** 307,322 ****
tm->tm_mday += val * 7;
if (fval != 0)
{
! int sec;
!
! fval *= 7 * SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
}
tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
break;
--- 307,329 ----
tm->tm_mday += val * 7;
if (fval != 0)
{
! int extra_days;
! fval *= 7;
! extra_days = (int32) fval;
! tm->tm_mday += extra_days;
! fval -= extra_days;
! if (fval != 0)
! {
! int sec;
! fval *= SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
+ }
}
tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
break;
***************
*** 325,340 ****
tm->tm_mon += val;
if (fval != 0)
{
! int sec;
!
! fval *= DAYS_PER_MONTH * SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
}
tmask = DTK_M(MONTH);
break;
--- 332,354 ----
tm->tm_mon += val;
if (fval != 0)
{
! int day;
! fval *= DAYS_PER_MONTH;
! day = fval;
! tm->tm_mday += day;
! fval -= day;
! if (fval != 0)
! {
! int sec;
! fval *= SECS_PER_DAY;
! sec = fval;
! tm->tm_sec += sec;
#ifdef HAVE_INT64_TIMESTAMP
! *fsec += (fval - sec) * 1000000;
#else
! *fsec += fval - sec;
#endif
+ }
}
tmask = DTK_M(MONTH);
break;
Index: src/test/regress/expected/interval.out
================================================== =================
RCS file: /projects/cvsroot/pgsql/src/test/regress/expected/
interval.out,v
retrieving revision 1.16
diff -c -r1.16 interval.out
*** src/test/regress/expected/interval.out 3 Sep 2006 03:34:04 -0000
1.16
--- src/test/regress/expected/interval.out 3 Sep 2006 23:55:35 -0000
***************
*** 39,44 ****
--- 39,56 ----
-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
+ (1 row)
+
+ SELECT INTERVAL '1.5 months' AS "One month 15 days";
+ One month 15 days
+ -------------------
+ 1 mon 15 days
+ (1 row)
+
SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
9 years...
----------------------------------
Index: src/test/regress/sql/interval.sql
================================================== =================
RCS file: /projects/cvsroot/pgsql/src/test/regress/sql/interval.sql,v
retrieving revision 1.9
diff -c -r1.9 interval.sql
*** src/test/regress/sql/interval.sql 6 Mar 2006 22:49:17 -0000 1.9
--- src/test/regress/sql/interval.sql 3 Sep 2006 23:55:35 -0000
***************
*** 11,16 ****
--- 11,18 ----
SELECT INTERVAL '-05' AS "Five 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 months' AS "One month 15 days";
SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";

CREATE TABLE INTERVAL_TBL (f1 interval);


---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-18-2008, 08:57 AM
Tom Lane
 
Posts: n/a
Default Re: Interval month, week -> day

Michael Glaesemann <grzm@seespotcode.net> writes:
> I realized there might be something in ecpg, and there was. I've
> updated the ecpg DecodeInterval to match. However, I haven't been
> able to get ecpg make check to work, so that part's untested.


This patch fails to apply --- looks like whitespace got mangled in
transit. Please resend as an attachment.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-18-2008, 08:57 AM
Michael Glaesemann
 
Posts: n/a
Default Re: Interval month, week -> day


On Sep 4, 2006, at 9:41 , Tom Lane wrote:

> This patch fails to apply --- looks like whitespace got mangled in
> transit. Please resend as an attachment.


Please let me know if you have any problems with this one.

Michael Glaesemann
grzm seespotcode net





---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-18-2008, 08:57 AM
Tom Lane
 
Posts: n/a
Default Re: [HACKERS] Interval month, week -> day

Michael Glaesemann <grzm@seespotcode.net> writes:
> On Sep 4, 2006, at 9:41 , Tom Lane wrote:
>> This patch fails to apply --- looks like whitespace got mangled in
>> transit. Please resend as an attachment.


> Please let me know if you have any problems with this one.


Ah, that one works --- applied. A few comments:

* You worried about the "tmask" coding in your original message, but
I think that's OK as-is. The point of that code, IIUC, is to reject
multiple specifications of the same field type, eg '1 day 2 days'.
If we changed it then we'd reject '1.5 month 2 days', whereas I think
least surprise would dictate adding the components to give 1 month
17 days.

* AFAICT the ecpg regression tests are not affected by this change.

* You mentioned being unable to get the ecpg tests to run on your
machine. I'm sure Michael and Joachim would like the details. The
ecpg regression tests are pretty new and some portability problems
are to be expected, but they seem to be passing on all the machines
Michael and Joachim and I have access to.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-18-2008, 08:57 AM
Andrew Dunstan
 
Posts: n/a
Default Re: [HACKERS] Interval month, week -> day



Tom Lane wrote:
> You mentioned being unable to get the ecpg tests to run on your
> machine. I'm sure Michael and Joachim would like the details. The
> ecpg regression tests are pretty new and some portability problems
> are to be expected, but they seem to be passing on all the machines
> Michael and Joachim and I have access to.
>
>
>


I have just today released a new version of the buildfarm client that
includes ECPG regression testing for HEAD (until now that was in our CVS
tip but not in a released version).

cheers

andrew

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-18-2008, 08:57 AM
Bruce Momjian
 
Posts: n/a
Default Re: [HACKERS] Interval month, week -> day

Tom Lane wrote:
> Michael Glaesemann <grzm@seespotcode.net> writes:
> > On Sep 4, 2006, at 9:41 , Tom Lane wrote:
> >> This patch fails to apply --- looks like whitespace got mangled in
> >> transit. Please resend as an attachment.

>
> > Please let me know if you have any problems with this one.

>
> Ah, that one works --- applied. A few comments:
>
> * You worried about the "tmask" coding in your original message, but
> I think that's OK as-is. The point of that code, IIUC, is to reject
> multiple specifications of the same field type, eg '1 day 2 days'.
> If we changed it then we'd reject '1.5 month 2 days', whereas I think
> least surprise would dictate adding the components to give 1 month
> 17 days.
>
> * AFAICT the ecpg regression tests are not affected by this change.
>
> * You mentioned being unable to get the ecpg tests to run on your
> machine. I'm sure Michael and Joachim would like the details. The
> ecpg regression tests are pretty new and some portability problems
> are to be expected, but they seem to be passing on all the machines
> Michael and Joachim and I have access to.


When I tried the ecpg regression tests it complained there was no
results/ directory. I created one and it worked.

--
Bruce Momjian bruce@momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:51 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com