From b453a17d8809bf884fe9e07ce9800ea3933ab2cc Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Mon, 20 Jan 2025 12:21:08 +0100 Subject: [PATCH] [release-19.0] Always return a valid timezone in cursor (#17546) (#17549) --- go/vt/vtgate/engine/fake_vcursor_test.go | 2 +- go/vt/vtgate/evalengine/fn_time.go | 4 ++-- go/vt/vtgate/safe_session.go | 9 ++++++--- go/vt/vtgate/safe_session_test.go | 14 ++++++++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/go/vt/vtgate/engine/fake_vcursor_test.go b/go/vt/vtgate/engine/fake_vcursor_test.go index 08a40c0d835..330cd725920 100644 --- a/go/vt/vtgate/engine/fake_vcursor_test.go +++ b/go/vt/vtgate/engine/fake_vcursor_test.go @@ -140,7 +140,7 @@ func (t *noopVCursor) Environment() *vtenv.Environment { } func (t *noopVCursor) TimeZone() *time.Location { - return nil + return time.Local } func (t *noopVCursor) SQLMode() string { diff --git a/go/vt/vtgate/evalengine/fn_time.go b/go/vt/vtgate/evalengine/fn_time.go index 8245de7669f..f01d7c7f9bb 100644 --- a/go/vt/vtgate/evalengine/fn_time.go +++ b/go/vt/vtgate/evalengine/fn_time.go @@ -232,7 +232,7 @@ func (call *builtinNow) constant() bool { func (call *builtinSysdate) eval(env *ExpressionEnv) (eval, error) { now := SystemTime() - if tz := env.currentTimezone(); tz != nil { + if tz := env.currentTimezone(); tz != time.Local { now = now.In(tz) } return newEvalDateTime(datetime.NewDateTimeFromStd(now), int(call.prec), false), nil @@ -673,7 +673,7 @@ func (b *builtinFromUnixtime) eval(env *ExpressionEnv) (eval, error) { } t := time.Unix(sec, frac) - if tz := env.currentTimezone(); tz != nil { + if tz := env.currentTimezone(); tz != time.Local { t = t.In(tz) } diff --git a/go/vt/vtgate/safe_session.go b/go/vt/vtgate/safe_session.go index 45fff46f629..54aa3f752de 100644 --- a/go/vt/vtgate/safe_session.go +++ b/go/vt/vtgate/safe_session.go @@ -26,7 +26,6 @@ import ( "google.golang.org/protobuf/proto" "vitess.io/vitess/go/mysql/datetime" - "vitess.io/vitess/go/vt/sqlparser" "vitess.io/vitess/go/vt/srvtopo" "vitess.io/vitess/go/vt/sysvars" @@ -567,9 +566,13 @@ func (session *SafeSession) TimeZone() *time.Location { session.mu.Unlock() if !ok { - return nil + return time.Local + } + + loc, err := datetime.ParseTimeZone(tz) + if err != nil { + return time.Local } - loc, _ := datetime.ParseTimeZone(tz) return loc } diff --git a/go/vt/vtgate/safe_session_test.go b/go/vt/vtgate/safe_session_test.go index 21bb2d6697a..199b9239bf2 100644 --- a/go/vt/vtgate/safe_session_test.go +++ b/go/vt/vtgate/safe_session_test.go @@ -72,6 +72,10 @@ func TestTimeZone(t *testing.T) { tz string want string }{ + { + tz: "", + want: time.Local.String(), + }, { tz: "Europe/Amsterdam", want: "Europe/Amsterdam", @@ -82,16 +86,18 @@ func TestTimeZone(t *testing.T) { }, { tz: "foo", - want: (*time.Location)(nil).String(), + want: time.Local.String(), }, } for _, tc := range testCases { t.Run(tc.tz, func(t *testing.T) { + sysvars := map[string]string{} + if tc.tz != "" { + sysvars["time_zone"] = tc.tz + } session := NewSafeSession(&vtgatepb.Session{ - SystemVariables: map[string]string{ - "time_zone": tc.tz, - }, + SystemVariables: sysvars, }) assert.Equal(t, tc.want, session.TimeZone().String())