Skip to content

Commit

Permalink
statistics: fix "data too long" error when dumping stats from table w…
Browse files Browse the repository at this point in the history
…ith new collation data (#27033) (#27300)
  • Loading branch information
ti-srebot authored Sep 3, 2021
1 parent 485415e commit 4bb5d21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,12 @@ func (h *Handle) histogramFromStorage(reader *statsReader, tableID int64, colID
} else {
sc := &stmtctx.StatementContext{TimeZone: time.UTC}
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.
// We change it to TypeBlob to bypass the length check here.
if tp.EvalType() == types.ETString && tp.Tp != mysql.TypeEnum && tp.Tp != mysql.TypeSet {
tp = types.NewFieldType(mysql.TypeBlob)
}
lowerBound, err = d.ConvertTo(sc, tp)
if err != nil {
return nil, errors.Trace(err)
Expand Down
22 changes: 22 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/testkit"
)

Expand Down Expand Up @@ -2154,3 +2155,24 @@ func (s *testStatsSuite) TestStatsCacheUpdateSkip(c *C) {
statsTbl2 := h.GetTableStats(tableInfo)
c.Assert(statsTbl1, Equals, statsTbl2)
}

func (s *testSerialStatsSuite) TestLoadHistogramWithCollate(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
testKit.MustExec("use test")
testKit.MustExec("drop table if exists t")
testKit.MustExec("create table t(a varchar(10) collate utf8mb4_unicode_ci);")
testKit.MustExec("insert into t values('abcdefghij');")
testKit.MustExec("insert into t values('abcdufghij');")
testKit.MustExec("analyze table t with 0 topn;")
do := s.do
h := do.StatsHandle()
is := do.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tblInfo := tbl.Meta()
_, err = h.TableStatsFromStorage(tblInfo, tblInfo.ID, true, 0)
c.Assert(err, IsNil)
}

0 comments on commit 4bb5d21

Please sign in to comment.