dbTalk Databases Forums  

[BUGS] timestamp arithmetic (a possible bug?)

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] timestamp arithmetic (a possible bug?) in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ilir Gashi
 
Posts: n/a

Default [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 06:04 AM






Hi,

I saw this behaviour in PostgreSQL 7.2. (Once again, I know this is an old
release but I do not have a newer version installed, and I am only using
the server for research purposes). If you execute the following statement

SELECT (CAST('01.01.2004 10:01:00' AS TIMESTAMP) - CAST('01.01.2004
10:00:00' AS TIMESTAMP)) + CAST('01.01.2004 10:00:00' AS TIMESTAMP);

The result returned is:

?column?
---------------------
2004-01-01 00:01:00
(1 row)

I was expecting: 2004-01-01 10:01:00.

Tried it on Oracle 8.0.5:

SELECT TO_DATE('01.01.2004 10:01:00', 'DD.MM.YYYY HH:MI:SS') -
TO_DATE('01.01.2004 10:00:00', 'DD.MM.YYYY HH:MI:SS') + TO_DATE('01.01.2004
10:00:00', 'DD.MM.YYYY HH:MI:SS') FROM DUAL;

---------------------------
2004-01-01 10:01:00
(1 row selected)


And MSSQL 7:

SELECT (CAST('01.01.2004 10:01:00' AS DATETIME) - CAST('01.01.2004
10:00:00' AS DATETIME) + CAST('01.01.2004 10:00:00' AS DATETIME));


---------------------------

2004-01-01 10:01:00.000

(1 row(s) affected)


Is this a bug? Same thing happens if I use TimestampTZ rather than
Timestamp.

Best regards,

Ilir

____________________________________________

Ilir Gashi
PhD Student
Centre for Software Reliability
City University
Northampton Square, London EC1V 0HB
email: i.gashi (AT) city (DOT) ac.uk
website: http://www.csr.city.ac.uk/csr_city/staff/gashi/
____________________________________________


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org

Reply With Quote
  #2  
Old   
Theodore Petrosky
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 06:36 AM






Interesting....

If I reverse the order it works...

agencysacks=# SELECT CAST('01.01.2004 10:00:00' AS
TIMESTAMP) + (CAST('01.01.2004 10:01:00' AS TIMESTAMP)
- CAST('01.01.2004 10:00:00' AS TIMESTAMP)) as answer;
answer
---------------------
2004-01-01 10:01:00
(1 row)

However your original...


agencysacks=# SELECT (CAST('01.01.2004 10:01:00' AS
TIMESTAMP) - CAST('01.01.2004 10:00:00' AS TIMESTAMP))
+ CAST('01.01.2004 10:00:00' AS TIMESTAMP);
ERROR: operator does not exist: interval + timestamp
without time zone
HINT: No operator matches the given name and argument
type(s). You may need to add explicit type casts.


agencysacks=# select version();

version

-------------------------------------------------------------------------------------------------------------------------
PostgreSQL 7.4.2 on powerpc-apple-darwin7.2.0,
compiled by GCC gcc (GCC) 3.3 20030304 (Apple
Computer, Inc. build 1495)
(1 row)

Looks like postgresql demands the order to be
timestamp +- interval.

Ted


--- Ilir Gashi <I.Gashi (AT) city (DOT) ac.uk> wrote:
Quote:
Hi,

I saw this behaviour in PostgreSQL 7.2. (Once again,
I know this is an old
release but I do not have a newer version installed,
and I am only using
the server for research purposes). If you execute
the following statement

SELECT (CAST('01.01.2004 10:01:00' AS TIMESTAMP) -
CAST('01.01.2004
10:00:00' AS TIMESTAMP)) + CAST('01.01.2004
10:00:00' AS TIMESTAMP);

The result returned is:

?column?
---------------------
2004-01-01 00:01:00
(1 row)

I was expecting: 2004-01-01 10:01:00.

Tried it on Oracle 8.0.5:

SELECT TO_DATE('01.01.2004 10:01:00', 'DD.MM.YYYY
HH:MI:SS') -
TO_DATE('01.01.2004 10:00:00', 'DD.MM.YYYY
HH:MI:SS') + TO_DATE('01.01.2004
10:00:00', 'DD.MM.YYYY HH:MI:SS') FROM DUAL;

---------------------------
2004-01-01 10:01:00
(1 row selected)


And MSSQL 7:

SELECT (CAST('01.01.2004 10:01:00' AS DATETIME) -
CAST('01.01.2004
10:00:00' AS DATETIME) + CAST('01.01.2004 10:00:00'
AS DATETIME));


---------------------------

2004-01-01 10:01:00.000

(1 row(s) affected)


Is this a bug? Same thing happens if I use
TimestampTZ rather than
Timestamp.

Best regards,

Ilir

____________________________________________

Ilir Gashi
PhD Student
Centre for Software Reliability
City University
Northampton Square, London EC1V 0HB
email: i.gashi (AT) city (DOT) ac.uk
website:
http://www.csr.city.ac.uk/csr_city/staff/gashi/
____________________________________________


---------------------------(end of
broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to
majordomo (AT) postgresql (DOT) org



__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

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


Reply With Quote
  #3  
Old   
Achilleus Mantzios
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 06:45 AM



O kyrios Ilir Gashi egrapse stis Jul 2, 2004 :

SELECT '01.01.2004 10:00:00'::timestamp + ('01.01.2004
10:01:00'::timestamp - '01.01.2004 10:00:00'::timestamp)::interval;
?column?
---------------------
2004-01-01 10:01:00
(1 row)

works fine in my 7.4.2

Quote:
Hi,

I saw this behaviour in PostgreSQL 7.2. (Once again, I know this is an old
release but I do not have a newer version installed, and I am only using
the server for research purposes). If you execute the following statement

SELECT (CAST('01.01.2004 10:01:00' AS TIMESTAMP) - CAST('01.01.2004
10:00:00' AS TIMESTAMP)) + CAST('01.01.2004 10:00:00' AS TIMESTAMP);

The result returned is:

?column?
---------------------
2004-01-01 00:01:00
(1 row)

I was expecting: 2004-01-01 10:01:00.

Tried it on Oracle 8.0.5:

SELECT TO_DATE('01.01.2004 10:01:00', 'DD.MM.YYYY HH:MI:SS') -
TO_DATE('01.01.2004 10:00:00', 'DD.MM.YYYY HH:MI:SS') + TO_DATE('01.01.2004
10:00:00', 'DD.MM.YYYY HH:MI:SS') FROM DUAL;

---------------------------
2004-01-01 10:01:00
(1 row selected)


And MSSQL 7:

SELECT (CAST('01.01.2004 10:01:00' AS DATETIME) - CAST('01.01.2004
10:00:00' AS DATETIME) + CAST('01.01.2004 10:00:00' AS DATETIME));


---------------------------

2004-01-01 10:01:00.000

(1 row(s) affected)


Is this a bug? Same thing happens if I use TimestampTZ rather than
Timestamp.

Best regards,

Ilir

____________________________________________

Ilir Gashi
PhD Student
Centre for Software Reliability
City University
Northampton Square, London EC1V 0HB
email: i.gashi (AT) city (DOT) ac.uk
website: http://www.csr.city.ac.uk/csr_city/staff/gashi/
____________________________________________


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org

--
-Achilleus


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

http://archives.postgresql.org


Reply With Quote
  #4  
Old   
Ilir Gashi
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 08:31 AM



On Jul 2 2004, Achilleus Mantzios wrote:

Quote:
SELECT '01.01.2004 10:00:00'::timestamp + ('01.01.2004
10:01:00'::timestamp - '01.01.2004 10:00:00'::timestamp)::interval;
?column?
---------------------
2004-01-01 10:01:00
(1 row)

works fine in my 7.4.2
Yes. I've just tried it in PostgreSQL 7.2 and it works fine there as well.



On Jul 2 2004, Theodore Petrosky wrote:

Quote:
Interesting....

If I reverse the order it works...

agencysacks=# SELECT CAST('01.01.2004 10:00:00' AS
TIMESTAMP) + (CAST('01.01.2004 10:01:00' AS TIMESTAMP)
- CAST('01.01.2004 10:00:00' AS TIMESTAMP)) as answer;
answer
---------------------
2004-01-01 10:01:00
(1 row)
Yes. The above works for me as well in 7.2.


Quote:
However your original...


agencysacks=# SELECT (CAST('01.01.2004 10:01:00' AS
TIMESTAMP) - CAST('01.01.2004 10:00:00' AS TIMESTAMP))
+ CAST('01.01.2004 10:00:00' AS TIMESTAMP);
ERROR: operator does not exist: interval + timestamp
without time zone
HINT: No operator matches the given name and argument
type(s). You may need to add explicit type casts.


agencysacks=# select version();

version



-------------------------------------------------------------------------------------------------------------------------
PostgreSQL 7.4.2 on powerpc-apple-darwin7.2.0,
compiled by GCC gcc (GCC) 3.3 20030304 (Apple
Computer, Inc. build 1495)
(1 row)

Looks like postgresql demands the order to be
timestamp +- interval.

Ted
I am not getting the error above. I get the incorrect result as reported in
my original report. Does this mean that this was a buggy behaviour in 7.2
and then was changed in the latter releases to give the error message that
7.4.2 is giving?

Thanks for the quick responses.

Best regards,

Ilir
____________________________________________

Ilir Gashi
PhD Student
Centre for Software Reliability
City University
Northampton Square, London EC1V 0HB
email: i.gashi (AT) city (DOT) ac.uk
website: http://www.csr.city.ac.uk/csr_city/staff/gashi/
____________________________________________


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


Reply With Quote
  #5  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 09:04 AM



Ilir Gashi <I.Gashi (AT) city (DOT) ac.uk> writes:
Quote:
I saw this behaviour in PostgreSQL 7.2. (Once again, I know this is an old
release but I do not have a newer version installed, and I am only using
the server for research purposes). If you execute the following statement

SELECT (CAST('01.01.2004 10:01:00' AS TIMESTAMP) - CAST('01.01.2004
10:00:00' AS TIMESTAMP)) + CAST('01.01.2004 10:00:00' AS TIMESTAMP);
More recent releases fail with

regression=# SELECT (CAST('01.01.2004 10:01:00' AS TIMESTAMP) - CAST('01.01.2004 10:00:00' AS TIMESTAMP)) + CAST('01.01.2004 10:00:00' AS TIMESTAMP);
ERROR: operator does not exist: interval + timestamp without time zone
HINT: No operator matches the given name and argument type(s). You may need to add explicit type casts.

(There is a timestamp + interval operator, so you could make it work by
flipping around the outer addition.)

It looks like what is happening in 7.2 is the parser is choosing the
closest available operator, which happens to be time + date, and
then coercing the interval and timestamp to time and date respectively.
The former isn't so bad but the latter loses your 10AM data...

Since then (I think in 7.3) we tightened the rules so that
information-losing coercions, such as timestamp to date, won't be
invoked implicitly.

regards, tom lane

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

http://archives.postgresql.org


Reply With Quote
  #6  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 09:18 AM



On Fri, 2 Jul 2004, Tom Lane wrote:

Quote:
Ilir Gashi <I.Gashi (AT) city (DOT) ac.uk> writes:
I saw this behaviour in PostgreSQL 7.2. (Once again, I know this is an old
release but I do not have a newer version installed, and I am only using
the server for research purposes). If you execute the following statement

SELECT (CAST('01.01.2004 10:01:00' AS TIMESTAMP) - CAST('01.01.2004
10:00:00' AS TIMESTAMP)) + CAST('01.01.2004 10:00:00' AS TIMESTAMP);

(There is a timestamp + interval operator, so you could make it work by
flipping around the outer addition.)
Should we be providing an interval + timestamp operator as well since it
looks like the spec implies both orderings should work?

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
  #7  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 09:37 AM



Stephan Szabo <sszabo (AT) megazone (DOT) bigpanda.com> writes:
Quote:
On Fri, 2 Jul 2004, Tom Lane wrote:
(There is a timestamp + interval operator, so you could make it work by
flipping around the outer addition.)

Should we be providing an interval + timestamp operator as well since it
looks like the spec implies both orderings should work?
If you see spec support for it, then yes ... where do you read that
exactly?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)


Reply With Quote
  #8  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 09:44 AM




On Fri, 2 Jul 2004, Tom Lane wrote:

Quote:
Stephan Szabo <sszabo (AT) megazone (DOT) bigpanda.com> writes:
On Fri, 2 Jul 2004, Tom Lane wrote:
(There is a timestamp + interval operator, so you could make it work by
flipping around the outer addition.)

Should we be providing an interval + timestamp operator as well since it
looks like the spec implies both orderings should work?

If you see spec support for it, then yes ... where do you read that
exactly?
SQL92 (draft) 4.5.3 Operators involving datetimes and intervals (the table
appears to be the same in SQL99 4.7.3)

The list given there is:
datetime - datetime -> interval
datetime +,- interval -> datetime
interval + datetime -> datetime
interval +,- interval -> interval
interval *,/ numeric -> interval
numeric * interval -> interval


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
  #9  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] timestamp arithmetic (a possible bug?) - 07-02-2004 , 11:42 AM



Stephan Szabo <sszabo (AT) megazone (DOT) bigpanda.com> writes:
Quote:
Should we be providing an interval + timestamp operator as well since it
looks like the spec implies both orderings should work?

If you see spec support for it, then yes ... where do you read that
exactly?

SQL92 (draft) 4.5.3 Operators involving datetimes and intervals (the table
appears to be the same in SQL99 4.7.3)
Yeah. It looks like we have most of these, but would need to add
interval + date
interval + timetz
interval + timestamp
interval + timestamptz
and for consistency
integer + date
Curiously, we do have interval + time without time zone ... I guess Tom
Lockhart overlooked these when he was working in the area.

I notice also that date - date yields an integer (ie, number of days)
where I think that strict spec compliance would mandate yielding an
interval instead. I'm uneager to change this though.

regards, tom lane

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


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.