Skip to content

Commit

Permalink
executor: fix time zone issue when querying slow log (#58455)
Browse files Browse the repository at this point in the history
close #58452
  • Loading branch information
lcwangchao authored Dec 24, 2024
1 parent 392fb75 commit f2db9c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pkg/executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ func (e *slowQueryRetriever) setColumnValue(sctx sessionctx.Context, row []types
sctx.GetSessionVars().StmtCtx.AppendWarning(err)
return false
}
timeValue := types.NewTime(types.FromGoTime(t), mysql.TypeTimestamp, types.MaxFsp)
timeValue := types.NewTime(types.FromGoTime(t.In(tz)), mysql.TypeTimestamp, types.MaxFsp)
return checker.isTimeValid(timeValue)
}
return true
Expand Down Expand Up @@ -926,9 +926,9 @@ func ParseTime(s string) (time.Time, error) {
}

type logFile struct {
file *os.File // The opened file handle
start time.Time // The start time of the log file
compressed bool // The file is compressed or not
file *os.File // The opened file handle
start types.Time // The start time of the log file
compressed bool // The file is compressed or not
}

// getAllFiles is used to get all slow-log needed to parse, it is exported for test.
Expand Down Expand Up @@ -984,7 +984,8 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
if err != nil {
return handleErr(err)
}
start := types.NewTime(types.FromGoTime(fileStartTime), mysql.TypeDatetime, types.MaxFsp)
tz := sctx.GetSessionVars().Location()
start := types.NewTime(types.FromGoTime(fileStartTime.In(tz)), mysql.TypeDatetime, types.MaxFsp)
if e.checker.enableTimeCheck {
notInAllTimeRanges := true
for _, tr := range e.checker.timeRanges {
Expand All @@ -1007,7 +1008,7 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
return handleErr(err)
}
if e.checker.enableTimeCheck {
end := types.NewTime(types.FromGoTime(fileEndTime), mysql.TypeDatetime, types.MaxFsp)
end := types.NewTime(types.FromGoTime(fileEndTime.In(tz)), mysql.TypeDatetime, types.MaxFsp)
inTimeRanges := false
for _, tr := range e.checker.timeRanges {
if !(start.Compare(tr.endTime) > 0 || end.Compare(tr.startTime) < 0) {
Expand All @@ -1026,7 +1027,7 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
}
logFiles = append(logFiles, logFile{
file: file,
start: fileStartTime,
start: start,
compressed: compressed,
})
skip = true
Expand All @@ -1049,9 +1050,9 @@ func (e *slowQueryRetriever) getAllFiles(ctx context.Context, sctx sessionctx.Co
ret = append(ret, file)
continue
}
start := types.NewTime(types.FromGoTime(logFiles[i].start), mysql.TypeDatetime, types.MaxFsp)
start := logFiles[i].start
// use next file.start as endTime
end := types.NewTime(types.FromGoTime(logFiles[i+1].start), mysql.TypeDatetime, types.MaxFsp)
end := logFiles[i+1].start
inTimeRanges := false
for _, tr := range e.checker.timeRanges {
if !(start.Compare(tr.endTime) > 0 || end.Compare(tr.startTime) < 0) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/executor/slow_query_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ SELECT original_sql, bind_sql, default_db, status, create_time, update_time, cha
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"))
tk.MustQuery("select count(plan_digest) from `information_schema`.`slow_query` where time > '2022-04-29 17:50:00'").Check(testkit.Rows("0"))
tk.MustQuery("select count(*) from `information_schema`.`slow_query` where time < '2010-01-02 15:04:05'").Check(testkit.Rows("0"))

// test the time zone change cases, see issue: https://github.com/pingcap/tidb/issues/58452
tk.MustExec("set @@time_zone='UTC'")
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"))
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"))
tk.MustExec("set @@time_zone='+10:00'")
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"))
}

func TestIssue37066(t *testing.T) {
Expand Down

0 comments on commit f2db9c4

Please sign in to comment.