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

statistics: fix incorrect datetime value when loading stats (#40084) #40138

Open
wants to merge 3 commits into
base: release-5.4
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,9 @@ func (h *Handle) histogramFromStorage(reader *statsReader, tableID int64, colID
lowerBound = rows[i].GetDatum(2, &fields[2].Column.FieldType)
upperBound = rows[i].GetDatum(3, &fields[3].Column.FieldType)
} else {
sc := &stmtctx.StatementContext{TimeZone: time.UTC}
// Invalid date values may be inserted into table under some relaxed sql mode. Those values may exist in statistics.
// Hence, when reading statistics, we should skip invalid date check. See #39336.
sc := &stmtctx.StatementContext{TimeZone: time.UTC, AllowInvalidDate: true, IgnoreZeroInDate: true}
d := rows[i].GetDatum(2, &fields[2].Column.FieldType)
// When there's new collation data, the length of bounds of histogram(the collate key) might be
// longer than the FieldType.Flen of this column.
Expand Down
32 changes: 32 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3137,3 +3137,35 @@ func (s *testStatsSuite) TestIncrementalModifyCountUpdate(c *C) {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/injectBaseCount"), IsNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/injectBaseModifyCount"), IsNil)
}

func TestIssue39336(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`
create table t1 (
a datetime(3) default null,
b int
) partition by range (b) (
partition p0 values less than (1000),
partition p1 values less than (maxvalue)
)`)
tk.MustExec("set @@sql_mode=''")
tk.MustExec("set @@tidb_analyze_version=2")
tk.MustExec("set @@tidb_partition_prune_mode='dynamic'")
tk.MustExec(`
insert into t1 values
('1000-00-09 00:00:00.000', 1),
('1000-00-06 00:00:00.000', 1),
('1000-00-06 00:00:00.000', 1),
('2022-11-23 14:24:30.000', 1),
('2022-11-23 14:24:32.000', 1),
('2022-11-23 14:24:33.000', 1),
('2022-11-23 14:24:35.000', 1),
('2022-11-23 14:25:08.000', 1001),
('2022-11-23 14:25:09.000', 1001)`)
tk.MustExec("analyze table t1 with 0 topn")
rows := tk.MustQuery("show analyze status where job_info like 'merge global stats%'").Rows()
require.Len(t, rows, 1)
require.Equal(t, "finished", rows[0][7])
}