Skip to content

Commit 3ac9806

Browse files
authored
executor: fix time zone issue when querying slow log (#58455) (#58577)
close #58452
1 parent a148775 commit 3ac9806

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

pkg/executor/slow_query.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ func (e *slowQueryRetriever) setColumnValue(sctx sessionctx.Context, row []types
752752
sctx.GetSessionVars().StmtCtx.AppendWarning(err)
753753
return false
754754
}
755-
timeValue := types.NewTime(types.FromGoTime(t), mysql.TypeTimestamp, types.MaxFsp)
755+
timeValue := types.NewTime(types.FromGoTime(t.In(tz)), mysql.TypeTimestamp, types.MaxFsp)
756756
return checker.isTimeValid(timeValue)
757757
}
758758
return true
@@ -926,9 +926,9 @@ func ParseTime(s string) (time.Time, error) {
926926
}
927927

928928
type logFile struct {
929-
file *os.File // The opened file handle
930-
start time.Time // The start time of the log file
931-
compressed bool // The file is compressed or not
929+
file *os.File // The opened file handle
930+
start types.Time // The start time of the log file
931+
compressed bool // The file is compressed or not
932932
}
933933

934934
// getAllFiles is used to get all slow-log needed to parse, it is exported for test.
@@ -984,7 +984,8 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
984984
if err != nil {
985985
return handleErr(err)
986986
}
987-
start := types.NewTime(types.FromGoTime(fileStartTime), mysql.TypeDatetime, types.MaxFsp)
987+
tz := sctx.GetSessionVars().Location()
988+
start := types.NewTime(types.FromGoTime(fileStartTime.In(tz)), mysql.TypeDatetime, types.MaxFsp)
988989
if e.checker.enableTimeCheck {
989990
notInAllTimeRanges := true
990991
for _, tr := range e.checker.timeRanges {
@@ -1007,7 +1008,7 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
10071008
return handleErr(err)
10081009
}
10091010
if e.checker.enableTimeCheck {
1010-
end := types.NewTime(types.FromGoTime(fileEndTime), mysql.TypeDatetime, types.MaxFsp)
1011+
end := types.NewTime(types.FromGoTime(fileEndTime.In(tz)), mysql.TypeDatetime, types.MaxFsp)
10111012
inTimeRanges := false
10121013
for _, tr := range e.checker.timeRanges {
10131014
if !(start.Compare(tr.endTime) > 0 || end.Compare(tr.startTime) < 0) {
@@ -1026,7 +1027,7 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
10261027
}
10271028
logFiles = append(logFiles, logFile{
10281029
file: file,
1029-
start: fileStartTime,
1030+
start: start,
10301031
compressed: compressed,
10311032
})
10321033
skip = true
@@ -1049,9 +1050,9 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
10491050
ret = append(ret, file)
10501051
continue
10511052
}
1052-
start := types.NewTime(types.FromGoTime(logFiles[i].start), mysql.TypeDatetime, types.MaxFsp)
1053+
start := logFiles[i].start
10531054
// use next file.start as endTime
1054-
end := types.NewTime(types.FromGoTime(logFiles[i+1].start), mysql.TypeDatetime, types.MaxFsp)
1055+
end := logFiles[i+1].start
10551056
inTimeRanges := false
10561057
for _, tr := range e.checker.timeRanges {
10571058
if !(start.Compare(tr.endTime) > 0 || end.Compare(tr.startTime) < 0) {

pkg/executor/slow_query_sql_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ SELECT original_sql, bind_sql, default_db, status, create_time, update_time, cha
304304
tk.MustQuery("select count(plan_digest) from `information_schema`.`slow_query` where time > '2019-10-13 20:08:13' and time < now();").Check(testkit.Rows("3"))
305305
tk.MustQuery("select count(plan_digest) from `information_schema`.`slow_query` where time > '2022-04-29 17:50:00'").Check(testkit.Rows("0"))
306306
tk.MustQuery("select count(*) from `information_schema`.`slow_query` where time < '2010-01-02 15:04:05'").Check(testkit.Rows("0"))
307+
308+
// test the time zone change cases, see issue: https://github.com/pingcap/tidb/issues/58452
309+
tk.MustExec("set @@time_zone='UTC'")
310+
tk.MustQuery("select count(*) from `information_schema`.`slow_query` where time > '2020-10-13 12:08:13' and time < '2020-10-13 13:08:13'").Check(testkit.Rows("1"))
311+
tk.MustQuery("select count(plan_digest) from `information_schema`.`slow_query` where time > '2020-10-13 12:08:13' and time < '2020-10-13 13:08:13'").Check(testkit.Rows("1"))
312+
tk.MustExec("set @@time_zone='+10:00'")
313+
tk.MustQuery("select count(*) from `information_schema`.`slow_query` where time > '2022-04-21 16:44:54' and time < '2022-04-21 16:44:55'").Check(testkit.Rows("1"))
307314
}
308315

309316
func TestIssue37066(t *testing.T) {

0 commit comments

Comments
 (0)