Skip to content

Commit

Permalink
statistics: use infoschema api to get table info (#57574)
Browse files Browse the repository at this point in the history
close #57573
  • Loading branch information
hawkingrei authored Nov 21, 2024
1 parent 2f8d1f6 commit d42a36d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 35 deletions.
1 change: 0 additions & 1 deletion pkg/statistics/handle/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/infoschema",
"//pkg/infoschema/context",
"//pkg/kv",
"//pkg/meta/model",
"//pkg/parser/terror",
Expand Down
41 changes: 7 additions & 34 deletions pkg/statistics/handle/util/table_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ package util

import (
"context"
"sync"

"github.com/pingcap/tidb/pkg/infoschema"
infoschemacontext "github.com/pingcap/tidb/pkg/infoschema/context"
"github.com/pingcap/tidb/pkg/table"
"github.com/pingcap/tidb/pkg/util/intest"
)

// TableInfoGetter is used to get table meta info.
Expand All @@ -33,44 +30,20 @@ type TableInfoGetter interface {

// tableInfoGetterImpl is used to get table meta info.
type tableInfoGetterImpl struct {
// pid2tid is the map from partition ID to table ID.
pid2tid map[int64]int64
// schemaVersion is the version of information schema when `pid2tid` is built.
schemaVersion int64
mu sync.RWMutex
}

// NewTableInfoGetter creates a TableInfoGetter.
func NewTableInfoGetter() TableInfoGetter {
return &tableInfoGetterImpl{pid2tid: map[int64]int64{}}
return &tableInfoGetterImpl{}
}

// TableInfoByID returns the table info specified by the physicalID.
// If the physicalID is corresponding to a partition, return its parent table.
func (c *tableInfoGetterImpl) TableInfoByID(is infoschema.InfoSchema, physicalID int64) (table.Table, bool) {
c.mu.Lock()
defer c.mu.Unlock()
if is.SchemaMetaVersion() != c.schemaVersion {
c.schemaVersion = is.SchemaMetaVersion()
c.pid2tid = buildPartitionID2TableID(is)
func (*tableInfoGetterImpl) TableInfoByID(is infoschema.InfoSchema, physicalID int64) (table.Table, bool) {
tbl, ok := is.TableByID(context.Background(), physicalID)
if ok {
return tbl, true
}
if id, ok := c.pid2tid[physicalID]; ok {
return is.TableByID(context.Background(), id)
}
return is.TableByID(context.Background(), physicalID)
}

func buildPartitionID2TableID(is infoschema.InfoSchema) map[int64]int64 {
mapper := make(map[int64]int64)
rs := is.ListTablesWithSpecialAttribute(infoschemacontext.PartitionAttribute)
for _, db := range rs {
for _, tbl := range db.TableInfos {
pi := tbl.GetPartitionInfo()
intest.AssertNotNil(pi)
for _, def := range pi.Definitions {
mapper[def.ID] = tbl.ID
}
}
}
return mapper
tbl, _, _ = is.FindTableByPartitionID(physicalID)
return tbl, tbl != nil
}

0 comments on commit d42a36d

Please sign in to comment.