Skip to content

Commit

Permalink
infoschema: avoid strings.toUpper in the IsClusterTableByName (pingca…
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored Feb 19, 2025
1 parent d851b15 commit 7a4c20e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
31 changes: 24 additions & 7 deletions pkg/infoschema/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/intest"
"github.com/pingcap/tidb/pkg/util/sem"
)

Expand Down Expand Up @@ -77,11 +78,15 @@ var memTableToAllTiDBClusterTables = map[string]string{
TableTiDBPlanCache: ClusterTableTiDBPlanCache,
}

var memTableToAllTiDBClusterTablesWithLowerCase = make(map[string]string)

// memTableToDDLOwnerClusterTables means add memory table to cluster table that will send cop request to DDL owner node.
var memTableToDDLOwnerClusterTables = map[string]string{
TableTiFlashReplica: TableTiFlashReplica,
}

var memTableToDDLOwnerClusterTablesWithLowerCase = make(map[string]string)

// ClusterTableCopDestination means the destination that cluster tables will send cop requests to.
type ClusterTableCopDestination int

Expand All @@ -103,6 +108,7 @@ func GetClusterTableCopDestination(tableName string) ClusterTableCopDestination
func init() {
var addrCol = columnInfo{name: util.ClusterTableInstanceColumnName, tp: mysql.TypeVarchar, size: 64}
for memTableName, clusterMemTableName := range memTableToAllTiDBClusterTables {
memTableToAllTiDBClusterTablesWithLowerCase[strings.ToLower(memTableName)] = strings.ToLower(clusterMemTableName)
memTableCols := tableNameToColumns[memTableName]
if len(memTableCols) == 0 {
continue
Expand All @@ -112,23 +118,34 @@ func init() {
cols = append(cols, memTableCols...)
tableNameToColumns[clusterMemTableName] = cols
}
for memTableName, clusterMemTableName := range memTableToDDLOwnerClusterTables {
memTableToDDLOwnerClusterTablesWithLowerCase[strings.ToLower(memTableName)] = strings.ToLower(clusterMemTableName)
}
}

// IsClusterTableByName used to check whether the table is a cluster memory table.
// Export for PhysicalTableScan.ExplainID
func IsClusterTableByName(dbName, tableName string) bool {
dbName = strings.ToUpper(dbName)
intest.AssertFunc(func() bool {
return dbName == strings.ToLower(dbName)
})
switch dbName {
case util.InformationSchemaName.O, util.PerformanceSchemaName.O:
tableName = strings.ToUpper(tableName)
for _, name := range memTableToAllTiDBClusterTables {
name = strings.ToUpper(name)
case util.InformationSchemaName.L, util.PerformanceSchemaName.L:
intest.AssertFunc(func() bool {
return tableName == strings.ToLower(tableName)
})
for _, name := range memTableToAllTiDBClusterTablesWithLowerCase {
intest.AssertFunc(func() bool {
return name == strings.ToLower(name)
})
if name == tableName {
return true
}
}
for _, name := range memTableToDDLOwnerClusterTables {
name = strings.ToUpper(name)
for _, name := range memTableToDDLOwnerClusterTablesWithLowerCase {
intest.AssertFunc(func() bool {
return name == strings.ToLower(name)
})
if name == tableName {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2493,7 +2493,7 @@ func createInfoSchemaTable(_ autoid.Allocators, _ func() (pools.Resource, error)
columns[i] = table.ToColumn(col)
}
tp := table.VirtualTable
if IsClusterTableByName(util.InformationSchemaName.O, meta.Name.O) {
if IsClusterTableByName(util.InformationSchemaName.L, meta.Name.L) {
tp = table.ClusterTable
}
return &infoschemaTable{meta: meta, cols: columns, tp: tp}, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (p *PhysicalTableScan) ExplainID() fmt.Stringer {

// TP overrides the TP in order to match different range.
func (p *PhysicalTableScan) TP() string {
if infoschema.IsClusterTableByName(p.DBName.O, p.Table.Name.O) {
if infoschema.IsClusterTableByName(p.DBName.L, p.Table.Name.L) {
return plancodec.TypeMemTableScan
} else if p.isChildOfIndexLookUp {
return plancodec.TypeTableRowIDScan
Expand All @@ -190,7 +190,7 @@ func (p *PhysicalTableScan) ExplainNormalizedInfo() string {

// OperatorInfo implements dataAccesser interface.
func (p *PhysicalTableScan) OperatorInfo(normalized bool) string {
if infoschema.IsClusterTableByName(p.DBName.O, p.Table.Name.O) {
if infoschema.IsClusterTableByName(p.DBName.L, p.Table.Name.L) {
return ""
}

Expand Down

0 comments on commit 7a4c20e

Please sign in to comment.