From 5131514ebb8b11bdd6decebf359e8be26eab54db Mon Sep 17 00:00:00 2001 From: wjHuang Date: Tue, 1 Dec 2020 14:08:41 +0800 Subject: [PATCH] statistics: fix a bug which causes panic when using the clustered index and the new collation (#21379) Signed-off-by: wjhuang2016 --- expression/integration_test.go | 19 +++++++++++++++++++ statistics/histogram.go | 12 ++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index b26b3c117e000..85bd6fb3313bd 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -8029,3 +8029,22 @@ func (s *testIntegrationSuite) TestIssue19892(c *C) { tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00")) } } + +func (s *testIntegrationSerialSuite) TestIssue20876(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("set @@tidb_enable_clustered_index=1;") + tk.MustExec("use test") + tk.MustExec("drop table if exists t;") + tk.MustExec("CREATE TABLE `t` (" + + " `a` char(10) COLLATE utf8mb4_unicode_ci NOT NULL," + + " `b` char(20) COLLATE utf8mb4_general_ci NOT NULL," + + " `c` int(11) NOT NULL," + + " PRIMARY KEY (`a`,`b`,`c`)," + + " KEY `idx` (`a`)" + + ")") + tk.MustExec("insert into t values ('#', 'C', 10), ('$', 'c', 20), ('$', 'c', 30), ('a', 'a', 10), ('A', 'A', 30)") + tk.MustExec("analyze table t") + tk.MustQuery("select * from t where a='#';").Check(testkit.Rows("# C 10")) +} diff --git a/statistics/histogram.go b/statistics/histogram.go index f263ff30c803e..2961e783e3688 100644 --- a/statistics/histogram.go +++ b/statistics/histogram.go @@ -820,18 +820,10 @@ func (c *Column) GetColumnRowCount(sc *stmtctx.StatementContext, ranges []*range highVal := *rg.HighVal[0].Clone() lowVal := *rg.LowVal[0].Clone() if highVal.Kind() == types.KindString { - highVal.SetBytesAsString(collate.GetCollator( - highVal.Collation()).Key(highVal.GetString()), - highVal.Collation(), - uint32(highVal.Length()), - ) + highVal.SetBytes(collate.GetCollator(highVal.Collation()).Key(highVal.GetString())) } if lowVal.Kind() == types.KindString { - lowVal.SetBytesAsString(collate.GetCollator( - lowVal.Collation()).Key(lowVal.GetString()), - lowVal.Collation(), - uint32(lowVal.Length()), - ) + lowVal.SetBytes(collate.GetCollator(lowVal.Collation()).Key(lowVal.GetString())) } cmp, err := lowVal.CompareDatum(sc, &highVal) if err != nil {