From 57a461993d8a35aef96292a0b7f745194a9967e0 Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Fri, 11 Oct 2019 19:02:03 +0800 Subject: [PATCH] infoschema: handle partition table for `tables` (#12591) --- infoschema/tables.go | 18 ++++++++++++++---- infoschema/tables_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/infoschema/tables.go b/infoschema/tables.go index 133078e780394..b998c368e2300 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -990,7 +990,7 @@ func getColLengthAllTables(ctx sessionctx.Context) (map[tableHistID]uint64, erro return colLengthMap, nil } -func getDataAndIndexLength(info *model.TableInfo, rowCount uint64, columnLengthMap map[tableHistID]uint64) (uint64, uint64) { +func getDataAndIndexLength(info *model.TableInfo, physicalID int64, rowCount uint64, columnLengthMap map[tableHistID]uint64) (uint64, uint64) { columnLength := make(map[string]uint64) for _, col := range info.Columns { if col.State != model.StatePublic { @@ -1000,7 +1000,7 @@ func getDataAndIndexLength(info *model.TableInfo, rowCount uint64, columnLengthM if length != types.VarStorageLen { columnLength[col.Name.L] = rowCount * uint64(length) } else { - length := columnLengthMap[tableHistID{tableID: info.ID, histID: col.ID}] + length := columnLengthMap[tableHistID{tableID: physicalID, histID: col.ID}] columnLength[col.Name.L] = length } } @@ -1158,8 +1158,18 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D } } - rowCount := tableRowsMap[table.ID] - dataLength, indexLength := getDataAndIndexLength(table, rowCount, colLengthMap) + var rowCount, dataLength, indexLength uint64 + if table.GetPartitionInfo() == nil { + rowCount = tableRowsMap[table.ID] + dataLength, indexLength = getDataAndIndexLength(table, table.ID, rowCount, colLengthMap) + } else { + for _, pi := range table.GetPartitionInfo().Definitions { + rowCount += tableRowsMap[pi.ID] + parDataLen, parIndexLen := getDataAndIndexLength(table, pi.ID, tableRowsMap[pi.ID], colLengthMap) + dataLength += parDataLen + indexLength += parIndexLen + } + } avgRowLength := uint64(0) if rowCount != 0 { avgRowLength = dataLength / rowCount diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index b91a907701da5..08da19bbfcd68 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -174,6 +174,16 @@ func (s *testTableSuite) TestDataForTableStatsField(c *C) { c.Assert(h.Update(is), IsNil) tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t'").Check( testkit.Rows("2 17 34 2")) + + // Test partition table. + tk.MustExec("drop table if exists t") + tk.MustExec(`CREATE TABLE t (a int, b int, c varchar(5), primary key(a), index idx(c)) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6), PARTITION p1 VALUES LESS THAN (11), PARTITION p2 VALUES LESS THAN (16))`) + h.HandleDDLEvent(<-h.DDLEventCh()) + tk.MustExec(`insert into t(a, b, c) values(1, 2, "c"), (7, 3, "d"), (12, 4, "e")`) + c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil) + c.Assert(h.Update(is), IsNil) + tk.MustQuery("select table_rows, avg_row_length, data_length, index_length from information_schema.tables where table_name='t'").Check( + testkit.Rows("3 17 51 3")) } func (s *testTableSuite) TestCharacterSetCollations(c *C) {