diff --git a/pkg/executor/slow_query.go b/pkg/executor/slow_query.go index 0b8256bbba78c..5b18e9f15d844 100644 --- a/pkg/executor/slow_query.go +++ b/pkg/executor/slow_query.go @@ -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 @@ -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. @@ -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 { @@ -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) { @@ -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 @@ -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) { diff --git a/pkg/executor/slow_query_sql_test.go b/pkg/executor/slow_query_sql_test.go index 6f9d0c92ffd3e..704a8c4576d6c 100644 --- a/pkg/executor/slow_query_sql_test.go +++ b/pkg/executor/slow_query_sql_test.go @@ -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) {