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

builtins: fix incorrect timezone conversions for extract and date_trunc #42267

Merged

Conversation

otan
Copy link
Contributor

@otan otan commented Nov 7, 2019

Resolves #42244, fixes more of #42006.

Turns out date_trunc is more broken than I thought, and MakeDTimestampTZFromDate actually not ignoring the argument has revealed a lot of weird usages of it. I also missed a test case with a negative timezone in my earlier PR, which also resolves another bug here. Wonder why govet didn't catch these unused arguments.


extract: Due to actually taking loc into account for
MakeDTimestampTZFromDate (as opposed to ignoring the argument before) in
827c97e04041e280a4413984d80cee99520c1d27, this exposed other bugs.

date_trunc: timestamp was incorrect because it tried to adapt to the
timezone, even though it is returning a timestamp without time zone
type. Furthermore, the date needed an additional offset for a given
timezone as the given date is in UTC, but we need to truncate it in
the correct timezone. This meant that my fix in the previous PR only
fixed positive UTC offsets, but not negative UTC offsets.

This PR fixes the immediate issue of extract and date_trunc,
and I will make an audit of all other incorrect usages in an upcoming PR.

There will be an upcoming PR to fix more other bugs found in the Plus
and Minus expressions with the MakeDTimestampTZFromDate change.

Release note (bug fix): Previously, <date>:date when context
local timestamp is set would result in the previous day
(<date-1>::date>) if the timezone is less than UTC+00:00 due to a
recently introduced bug. This change fixes this.

Release note (bug fix): Previously, date_trunc for timestamp were
incorrect if in a local timezone was set. This change fixes this.

Release note (bug fix): Previously, date_trunc for date types were
incorrect with a negative timezone offset in a local timezone. This
change fixes this.

@otan otan requested review from rafiss and a team November 7, 2019 13:46
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@otan otan force-pushed the otan-cleanup_dtimestamp_bugs branch from 4877aa2 to d89c232 Compare November 7, 2019 14:06
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks -- lgtm! just a few small questions but feel free to merge after that.

for the commit message, I think it would be a bit better to just have one release note, that synthesizes all the fixes. I don't think you need the full level of detail in the release note area; that might be fine for the commit message body, but that is more up to you.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan and @rafiss)


pkg/sql/logictest/testdata/logic_test/datetime, line 1390 at r1 (raw file):

select date_trunc('day', '2011-01-02 01:30:00'::timestamp)
----
2011-01-02 00:00:00 +0000 +0000

nice tests! I didn't check myself, but let's just make sure Postgres does the same (apart from the quick where we show the tz offset for non-tz types)


pkg/sql/logictest/testdata/logic_test/datetime, line 1512 at r1 (raw file):

UTC

subtest regression_42244

I actually haven't seen this syntax before! could you educate me about what it means/does?

Copy link
Contributor Author

@otan otan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)


pkg/sql/logictest/testdata/logic_test/datetime, line 1390 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nice tests! I didn't check myself, but let's just make sure Postgres does the same (apart from the quick where we show the tz offset for non-tz types)

yep, that's where i got the inspiration from :)


pkg/sql/logictest/testdata/logic_test/datetime, line 1512 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

I actually haven't seen this syntax before! could you educate me about what it means/does?

afaik it's just labelling when it fails, it'll give you the specific failure.

…trunc`

`extract`: Due to actually taking `loc` into account for
`MakeDTimestampTZFromDate` (as opposed to ignoring the argument before) in
`827c97e04041e280a4413984d80cee99520c1d27`, this exposed other bugs.

`date_trunc`: `timestamp` was incorrect because it tried to adapt to the
timezone, even though it is returning a timestamp without time zone
type. Furthermore, the `date` needed an additional `offset` for a given
timezone as the given `date` is in UTC, but we need to truncate it in
the correct timezone. This meant that my fix in the previous PR only
fixed positive UTC offsets, but not negative UTC offsets.

This PR fixes the immediate issue of `extract` and `date_trunc`,
and I will make an audit of all other incorrect usages in an upcoming PR.

There will be an upcoming PR to fix more other bugs found in the Plus
and Minus expressions with the MakeDTimestampTZFromDate change.

Release note (bug fix): Previously:
* `<date>:date` when context local timestamp is set would result in
the previous day (`<date-1>::date>`) if the timezone is less than
UTC+00:00 due to a recently introduced bug.
* `date_trunc` for timestamp were incorrect if in a local timezone
was set.
* `date_trunc` for date types were incorrect with a negative timezone
offset in a local timezone.

This change fixes all these above issues.
@otan otan force-pushed the otan-cleanup_dtimestamp_bugs branch from d89c232 to 6a1e714 Compare November 8, 2019 18:39
@otan
Copy link
Contributor Author

otan commented Nov 8, 2019

bors r+

craig bot pushed a commit that referenced this pull request Nov 8, 2019
42267: builtins: fix incorrect timezone conversions for `extract` and `date_trunc` r=otan a=otan

Resolves #42244, fixes more of #42006.

Turns out `date_trunc` is more broken than I thought, and `MakeDTimestampTZFromDate` actually *not* ignoring the argument has revealed a lot of weird usages of it. I also missed a test case with a negative timezone in my earlier PR, which also resolves another bug here. Wonder why govet didn't catch these unused arguments.

---
`extract`: Due to actually taking `loc` into account for
`MakeDTimestampTZFromDate` (as opposed to ignoring the argument before) in
`827c97e04041e280a4413984d80cee99520c1d27`, this exposed other bugs.

`date_trunc`: `timestamp` was incorrect because it tried to adapt to the
timezone, even though it is returning a timestamp without time zone
type. Furthermore, the `date` needed an additional `offset` for a given
timezone as the given `date` is in UTC, but we need to truncate it in
the correct timezone. This meant that my fix in the previous PR only
fixed positive UTC offsets, but not negative UTC offsets.

This PR fixes the immediate issue of `extract` and `date_trunc`,
and I will make an audit of all other incorrect usages in an upcoming PR.

There will be an upcoming PR to fix more other bugs found in the Plus
and Minus expressions with the MakeDTimestampTZFromDate change.

Release note (bug fix): Previously, `<date>:date` when context
local timestamp is set would result in the previous day
(`<date-1>::date>`) if the timezone is less than UTC+00:00 due to a
recently introduced bug. This change fixes this.

Release note (bug fix): Previously, date_trunc for timestamp were
incorrect if in a local timezone was set. This change fixes this.

Release note (bug fix): Previously, date_trunc for date types were
incorrect with a negative timezone offset in a local timezone. This
change fixes this.

Co-authored-by: Oliver Tan <otan@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented Nov 8, 2019

Build succeeded

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

Successfully merging this pull request may close these issues.

sql: incorrect handling of timezones when extracting day from current date
3 participants