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

sql: fix current_timestamp behaviour with time zone set #43012

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/timestamp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ SELECT extract(timezone FROM '2001-01-01 13:00:00+01:15'::timestamptz)
----
10800


subtest regression_41776

statement ok
Expand All @@ -203,3 +202,24 @@ query T
SELECT '2001-01-01 00:00:00'::TIMESTAMP::TIMESTAMPTZ
----
2001-01-01 00:00:00 +0100 +0100

# test that current_timestamp is correct in different timezones.
subtest current_timestamp_correct_in_timezone

statement ok
set time zone +3

statement ok
create table current_timestamp_test (a timestamp, b timestamptz)

statement ok
insert into current_timestamp_test values (current_timestamp, current_timestamp)

statement ok
set time zone 0

# a was written at an interval 3 hours ahead, and should persist that way.
# b will remember the timezone, so should be "constant" for comparison's sake.
query TT
select * from current_timestamp_test WHERE a - interval '3h' <> b
----
7 changes: 5 additions & 2 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,7 @@ func (ctx *EvalContext) GetTxnTimestamp(precision time.Duration) *DTimestampTZ {
if !ctx.PrepareOnly && ctx.TxnTimestamp.IsZero() {
panic(errors.AssertionFailedf("zero transaction timestamp in EvalContext"))
}
return MakeDTimestampTZ(ctx.TxnTimestamp, precision)
return MakeDTimestampTZ(ctx.GetRelativeParseTime(), precision)
}

// GetTxnTimestampNoZone retrieves the current transaction timestamp as per
Expand All @@ -2943,7 +2943,10 @@ func (ctx *EvalContext) GetTxnTimestampNoZone(precision time.Duration) *DTimesta
if !ctx.PrepareOnly && ctx.TxnTimestamp.IsZero() {
panic(errors.AssertionFailedf("zero transaction timestamp in EvalContext"))
}
return MakeDTimestamp(ctx.TxnTimestamp, precision)
// Move the time to UTC, but keeping the location's time.
t := ctx.GetRelativeParseTime()
_, offsetSecs := t.Zone()
return MakeDTimestamp(t.Add(time.Second*time.Duration(offsetSecs)).In(time.UTC), precision)
}

// SetTxnTimestamp sets the corresponding timestamp in the EvalContext.
Expand Down