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 "data too long" error when dumping stats from table with new collation data (#27033) #27299

Merged
merged 4 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion statistics/handle/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (h *Handle) DumpStatsToJSONBySnapshot(dbName string, tableInfo *model.Table
}

func (h *Handle) tableStatsToJSON(dbName string, tableInfo *model.TableInfo, physicalID int64, snapshot uint64) (*JSONTable, error) {
tbl, err := h.tableStatsFromStorage(tableInfo, physicalID, true, snapshot)
tbl, err := h.TableStatsFromStorage(tableInfo, physicalID, true, snapshot)
if err != nil || tbl == nil {
return nil, err
}
Expand Down
12 changes: 9 additions & 3 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (h *Handle) Update(is infoschema.InfoSchema) error {
if oldTbl, ok := oldCache.tables[physicalID]; ok && oldTbl.Version >= version && tableInfo.UpdateTS == oldTbl.TblInfoUpdateTS {
continue
}
tbl, err := h.tableStatsFromStorage(tableInfo, physicalID, false, 0)
tbl, err := h.TableStatsFromStorage(tableInfo, physicalID, false, 0)
// Error is not nil may mean that there are some ddl changes on this table, we will not update it.
if err != nil {
logutil.BgLogger().Error("[stats] error occurred when read table stats", zap.String("table", tableInfo.Name.O), zap.Error(err))
Expand Down Expand Up @@ -531,8 +531,8 @@ func (h *Handle) columnStatsFromStorage(reader *statsReader, row chunk.Row, tabl
return nil
}

// tableStatsFromStorage loads table stats info from storage.
func (h *Handle) tableStatsFromStorage(tableInfo *model.TableInfo, physicalID int64, loadAll bool, snapshot uint64) (_ *statistics.Table, err error) {
// TableStatsFromStorage loads table stats info from storage.
func (h *Handle) TableStatsFromStorage(tableInfo *model.TableInfo, physicalID int64, loadAll bool, snapshot uint64) (_ *statistics.Table, err error) {
reader, err := h.getStatsReader(snapshot)
if err != nil {
return nil, err
Expand Down Expand Up @@ -705,6 +705,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
29 changes: 29 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 @@ -576,3 +577,31 @@ func (s *testStatsSuite) TestStatsCacheUpdateSkip(c *C) {
statsTbl2 := h.GetTableStats(tableInfo)
c.Assert(statsTbl1, Equals, statsTbl2)
}

var _ = SerialSuites(&statsSerialSuite{})

type statsSerialSuite struct {
store kv.Storage
do *domain.Domain
}

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)
}