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: use infoschema api to get table info (#57574) #57614

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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
}