Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

an active time zone incorrectly changes interval math in UPDATE #124

Closed
timgraham opened this issue Feb 11, 2020 · 5 comments
Closed

an active time zone incorrectly changes interval math in UPDATE #124

timgraham opened this issue Feb 11, 2020 · 5 comments
Assignees

Comments

@timgraham
Copy link
Collaborator

On CockroachDB master (a regression after cockroachdb/cockroach@917005b, perhaps):

> CREATE TABLE "example" ("id" integer NOT NULL PRIMARY KEY DEFAULT unique_rowid(), "start" timestamptz NOT NULL, "end" timestamptz NOT NULL);
> INSERT INTO "example" ("start", "end") VALUES ('2010-06-25T12:15:30.747000-05:00'::timestamptz, '2010-06-25T12:15:30.747000-05:00'::timestamptz);
> UPDATE "example" SET "end" = ("end" + '104166 days 57599.999999 seconds'::interval) WHERE true; 
> SELECT ("end" - "start") AS "delta" FROM "example";
2499999:59:59.999999

That's the expected result. Now setting a time zone before the update:

> TRUNCATE "example";
> INSERT INTO "example" ("start", "end") VALUES ('2010-06-25T12:15:30.747000-05:00'::timestamptz, '2010-06-25T12:15:30.747000-05:00'::timestamptz);
> SET TIME ZONE 'America/Chicago';
> UPDATE "example" SET "end" = ("end" + '104166 days 57599.999999 seconds'::interval) WHERE true;
> SELECT ("end" - "start") AS "delta" FROM "example";
         delta
------------------------
  2500000:59:59.999999

The result is expected to stay the same.

The failing Django test:

======================================================================
FAIL: test_datetime_subtraction_microseconds (expressions.tests.FTimeDeltaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/django/test/testcases.py", line 1247, in skip_wrapper
    return test_func(*args, **kwargs)
  File "/home/tim/code/django/tests/expressions/tests.py", line 1516, in test_datetime_subtraction_microseconds
    self.assertEqual(e.delta, delta)
AssertionError: datetime.timedelta(days=104166, seconds=61199, microseconds=999999) != datetime.timedelta(days=104166, seconds=57599, microseconds=999999)
@otan
Copy link
Contributor

otan commented Feb 11, 2020

i believe this is because cockroach has different timezone data compared to postgres

example for cockroach

[email protected]:57773/defaultdb> select ('2010-06-25T12:15:30.747000-05:00'::timestamptz + '104166 days 57599.999999 seconds'::interval);
              ?column?
------------------------------------
  2295-09-06 04:15:30.746999-06:00
(1 row)

Time: 295µs

[email protected]:57882/defaultdb> select '2295-09-06 04:15:30.746999-06:00'::timestamptz;
            timestamptz
------------------------------------
  2295-09-06 04:15:30.746999-06:00
(1 row)

Time: 548µs

postgres:

otan=# select ('2010-06-25T12:15:30.747000-05:00'::timestamptz + '104166 days 57599.999999 seconds'::interval);
           ?column?
-------------------------------
 2295-09-06 04:15:30.746999-05
(1 row)

otan=# select '2295-09-06 04:15:30.746999-06:00'::timestamptz;
          timestamptz
-------------------------------
 2295-09-06 05:15:30.746999-05
(1 row)

notice the difference between -5 and -6.

my belief is that since postgres does not have timezone data that far in the future, it does not do the interval math correctly compared to cockroach, hence the result. note that for 180 days, this behaviour is the same between both cockroach and psql (and the interval is an hour longer):

cockroach

[email protected]:57882/defaultdb> select '2010-06-25T12:15:30.747000-05:00'::timestamptz + '180 days 57599.999999 seconds'::interval - '2010-06-25T12:15:30.747000-05:00'::timestamptz;
      ?column?
---------------------
  4336:59:59.999999
(1 row)

Time: 296µs

psql

otan=# select '2010-06-25T12:15:30.747000-05:00'::timestamptz + '180 days 57599.999999 seconds'::interval - '2010-06-25T12:15:30.747000-05:00'::timestamptz;
         ?column?
--------------------------
 180 days 16:59:59.999999
(1 row)

@otan
Copy link
Contributor

otan commented Feb 11, 2020

interestingly enough, cockroach stops propagating DST after 2037:

[email protected]:57882/defaultdb> select '2038-09-06 04:15:30.746999-06:00'::timestamptz;
            timestamptz
------------------------------------
  2038-09-06 04:15:30.746999-06:00
(1 row)

Time: 321µs

[email protected]:57882/defaultdb> select '2037-09-06 04:15:30.746999-06:00'::timestamptz;
            timestamptz
------------------------------------
  2037-09-06 05:15:30.746999-05:00
(1 row)

Time: 399µs

@otan
Copy link
Contributor

otan commented Feb 11, 2020

(edited - wrong)

@otan
Copy link
Contributor

otan commented Feb 11, 2020

because this is language/os related (and not something that I can immediately address), i'm going to close this in favour of cockroachdb/cockroach#31978 and cockroachdb/cockroach#36864

@otan
Copy link
Contributor

otan commented Feb 12, 2020

small edit: this is actually a golang issue -- filed cockroachdb/cockroach#45001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants