Skip to content

Commit

Permalink
Merge pull request #28 from pingcap/master
Browse files Browse the repository at this point in the history
executor: Migrate dataForIndexes from package infoschema to exector (…
  • Loading branch information
sthagen authored Mar 5, 2020
2 parents 6c5f716 + debe5cb commit 697820b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 83 deletions.
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,7 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo
},
}
case strings.ToLower(infoschema.TableSchemata),
strings.ToLower(infoschema.TableTiDBIndexes),
strings.ToLower(infoschema.TableViews),
strings.ToLower(infoschema.TableEngines),
strings.ToLower(infoschema.TableCollations),
Expand Down
74 changes: 74 additions & 0 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package executor

import (
"context"
"fmt"
"sort"

"github.com/pingcap/parser/charset"
Expand Down Expand Up @@ -51,6 +52,8 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
switch e.table.Name.O {
case infoschema.TableSchemata:
e.rows = dataForSchemata(sctx, dbs)
case infoschema.TableTiDBIndexes:
e.rows, err = dataForIndexes(sctx, dbs)
case infoschema.TableViews:
e.rows, err = dataForViews(sctx, dbs)
case infoschema.TableEngines:
Expand Down Expand Up @@ -126,6 +129,77 @@ func dataForSchemata(ctx sessionctx.Context, schemas []*model.DBInfo) [][]types.
return rows
}

func dataForIndexes(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
checker := privilege.GetPrivilegeManager(ctx)
var rows [][]types.Datum
for _, schema := range schemas {
for _, tb := range schema.Tables {
if checker != nil && !checker.RequestVerification(ctx.GetSessionVars().ActiveRoles, schema.Name.L, tb.Name.L, "", mysql.AllPrivMask) {
continue
}

if tb.PKIsHandle {
var pkCol *model.ColumnInfo
for _, col := range tb.Cols() {
if mysql.HasPriKeyFlag(col.Flag) {
pkCol = col
break
}
}
record := types.MakeDatums(
schema.Name.O, // TABLE_SCHEMA
tb.Name.O, // TABLE_NAME
0, // NON_UNIQUE
"PRIMARY", // KEY_NAME
1, // SEQ_IN_INDEX
pkCol.Name.O, // COLUMN_NAME
nil, // SUB_PART
"", // INDEX_COMMENT
"NULL", // Expression
0, // INDEX_ID
)
rows = append(rows, record)
}
for _, idxInfo := range tb.Indices {
if idxInfo.State != model.StatePublic {
continue
}
for i, col := range idxInfo.Columns {
nonUniq := 1
if idxInfo.Unique {
nonUniq = 0
}
var subPart interface{}
if col.Length != types.UnspecifiedLength {
subPart = col.Length
}
colName := col.Name.O
expression := "NULL"
tblCol := tb.Columns[col.Offset]
if tblCol.Hidden {
colName = "NULL"
expression = fmt.Sprintf("(%s)", tblCol.GeneratedExprString)
}
record := types.MakeDatums(
schema.Name.O, // TABLE_SCHEMA
tb.Name.O, // TABLE_NAME
nonUniq, // NON_UNIQUE
idxInfo.Name.O, // KEY_NAME
i+1, // SEQ_IN_INDEX
colName, // COLUMN_NAME
subPart, // SUB_PART
idxInfo.Comment, // INDEX_COMMENT
expression, // Expression
idxInfo.ID, // INDEX_ID
)
rows = append(rows, record)
}
}
}
}
return rows, nil
}

func dataForViews(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
checker := privilege.GetPrivilegeManager(ctx)
var rows [][]types.Datum
Expand Down
7 changes: 7 additions & 0 deletions executor/infoschema_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func (s *testInfoschemaTableSuite) TestSchemataTables(c *C) {
testkit.Rows("def INFORMATION_SCHEMA utf8mb4 utf8mb4_bin <nil>", "def mysql utf8mb4 utf8mb4_bin <nil>"))
}

func (s *testInfoschemaTableSuite) TestTableIDAndIndexID(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("drop table if exists test.t")
tk.MustExec("create table test.t (a int, b int, primary key(a), key k1(b))")
tk.MustQuery("select index_id from information_schema.tidb_indexes where table_schema = 'test' and table_name = 't'").Check(testkit.Rows("0", "1"))
}

func (s *testInfoschemaTableSuite) TestSchemataCharacterSet(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("CREATE DATABASE `foo` DEFAULT CHARACTER SET = 'utf8mb4'")
Expand Down
92 changes: 10 additions & 82 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ const (
// TableCollationCharacterSetApplicability is the string constant of infoschema memory table.
TableCollationCharacterSetApplicability = "COLLATION_CHARACTER_SET_APPLICABILITY"
tableProcesslist = "PROCESSLIST"
tableTiDBIndexes = "TIDB_INDEXES"
tableTiDBHotRegions = "TIDB_HOT_REGIONS"
tableTiKVStoreStatus = "TIKV_STORE_STATUS"
tableAnalyzeStatus = "ANALYZE_STATUS"
tableTiKVRegionStatus = "TIKV_REGION_STATUS"
tableTiKVRegionPeers = "TIKV_REGION_PEERS"
tableTiDBServersInfo = "TIDB_SERVERS_INFO"
// TableTiDBIndexes is the string constant of infoschema table
TableTiDBIndexes = "TIDB_INDEXES"
tableTiDBHotRegions = "TIDB_HOT_REGIONS"
tableTiKVStoreStatus = "TIKV_STORE_STATUS"
tableAnalyzeStatus = "ANALYZE_STATUS"
tableTiKVRegionStatus = "TIKV_REGION_STATUS"
tableTiKVRegionPeers = "TIKV_REGION_PEERS"
tableTiDBServersInfo = "TIDB_SERVERS_INFO"
// TableSlowQuery is the string constant of slow query memory table.
TableSlowQuery = "SLOW_QUERY"
// TableClusterInfo is the string constant of cluster info memory table.
Expand Down Expand Up @@ -161,7 +162,7 @@ var tableIDMap = map[string]int64{
tableTableSpaces: autoid.InformationSchemaDBID + 31,
TableCollationCharacterSetApplicability: autoid.InformationSchemaDBID + 32,
tableProcesslist: autoid.InformationSchemaDBID + 33,
tableTiDBIndexes: autoid.InformationSchemaDBID + 34,
TableTiDBIndexes: autoid.InformationSchemaDBID + 34,
TableSlowQuery: autoid.InformationSchemaDBID + 35,
tableTiDBHotRegions: autoid.InformationSchemaDBID + 36,
tableTiKVStoreStatus: autoid.InformationSchemaDBID + 37,
Expand Down Expand Up @@ -1451,77 +1452,6 @@ func GetShardingInfo(dbInfo *model.DBInfo, tableInfo *model.TableInfo) interface
return shardingInfo
}

func dataForIndexes(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
checker := privilege.GetPrivilegeManager(ctx)
var rows [][]types.Datum
for _, schema := range schemas {
for _, tb := range schema.Tables {
if checker != nil && !checker.RequestVerification(ctx.GetSessionVars().ActiveRoles, schema.Name.L, tb.Name.L, "", mysql.AllPrivMask) {
continue
}

if tb.PKIsHandle {
var pkCol *model.ColumnInfo
for _, col := range tb.Cols() {
if mysql.HasPriKeyFlag(col.Flag) {
pkCol = col
break
}
}
record := types.MakeDatums(
schema.Name.O, // TABLE_SCHEMA
tb.Name.O, // TABLE_NAME
0, // NON_UNIQUE
"PRIMARY", // KEY_NAME
1, // SEQ_IN_INDEX
pkCol.Name.O, // COLUMN_NAME
nil, // SUB_PART
"", // INDEX_COMMENT
"NULL", // Expression
0, // INDEX_ID
)
rows = append(rows, record)
}
for _, idxInfo := range tb.Indices {
if idxInfo.State != model.StatePublic {
continue
}
for i, col := range idxInfo.Columns {
nonUniq := 1
if idxInfo.Unique {
nonUniq = 0
}
var subPart interface{}
if col.Length != types.UnspecifiedLength {
subPart = col.Length
}
colName := col.Name.O
expression := "NULL"
tblCol := tb.Columns[col.Offset]
if tblCol.Hidden {
colName = "NULL"
expression = fmt.Sprintf("(%s)", tblCol.GeneratedExprString)
}
record := types.MakeDatums(
schema.Name.O, // TABLE_SCHEMA
tb.Name.O, // TABLE_NAME
nonUniq, // NON_UNIQUE
idxInfo.Name.O, // KEY_NAME
i+1, // SEQ_IN_INDEX
colName, // COLUMN_NAME
subPart, // SUB_PART
idxInfo.Comment, // INDEX_COMMENT
expression, // Expression
idxInfo.ID, // INDEX_ID
)
rows = append(rows, record)
}
}
}
}
return rows, nil
}

func dataForColumns(ctx sessionctx.Context, schemas []*model.DBInfo) [][]types.Datum {
checker := privilege.GetPrivilegeManager(ctx)
var rows [][]types.Datum
Expand Down Expand Up @@ -2377,7 +2307,7 @@ var tableNameToColumns = map[string][]columnInfo{
tableTableSpaces: tableTableSpacesCols,
TableCollationCharacterSetApplicability: tableCollationCharacterSetApplicabilityCols,
tableProcesslist: tableProcesslistCols,
tableTiDBIndexes: tableTiDBIndexesCols,
TableTiDBIndexes: tableTiDBIndexesCols,
TableSlowQuery: slowQueryCols,
tableTiDBHotRegions: tableTiDBHotRegionsCols,
tableTiKVStoreStatus: tableTiKVStoreStatusCols,
Expand Down Expand Up @@ -2440,8 +2370,6 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
switch it.meta.Name.O {
case tableTables:
fullRows, err = dataForTables(ctx, dbs)
case tableTiDBIndexes:
fullRows, err = dataForIndexes(ctx, dbs)
case tableColumns:
fullRows = dataForColumns(ctx, dbs)
case tableStatistics:
Expand Down
1 change: 0 additions & 1 deletion infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,6 @@ func (s *testTableSuite) TestTableIDAndIndexID(c *C) {
tblID, err := strconv.Atoi(tk.MustQuery("select tidb_table_id from information_schema.tables where table_schema = 'test' and table_name = 't'").Rows()[0][0].(string))
c.Assert(err, IsNil)
c.Assert(tblID, Greater, 0)
tk.MustQuery("select index_id from information_schema.tidb_indexes where table_schema = 'test' and table_name = 't'").Check(testkit.Rows("0", "1"))
}

func prepareSlowLogfile(c *C, slowLogFileName string) {
Expand Down

0 comments on commit 697820b

Please sign in to comment.