From aaa770ac718438225ce7602548bdfa534a0ee45e Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Tue, 11 May 2021 23:37:39 +0800 Subject: [PATCH] statistics: fix a bug which causes panic when using the clustered index and the new collation (#21379) (#21408) --- expression/integration_test.go | 18 ++++++++++++++++++ statistics/histogram.go | 12 ++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index ae7f4eddea2de..0f145e41d2765 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -7748,6 +7748,24 @@ func (s *testIntegrationSerialSuite) TestCollationIndexJoin(c *C) { tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 Optimizer Hint /*+ INL_MERGE_JOIN(t2) */ is inapplicable")) } +func (s *testIntegrationSerialSuite) TestIssue20876(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + tk := testkit.NewTestKit(c, s.store) + 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")) +} + func (s *testIntegrationSuite) TestIssue19892(c *C) { defer s.cleanEnv(c) tk := testkit.NewTestKit(c, s.store) diff --git a/statistics/histogram.go b/statistics/histogram.go index 76cbfe1732d07..af3861a2184a3 100644 --- a/statistics/histogram.go +++ b/statistics/histogram.go @@ -793,18 +793,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 {