Skip to content

Commit

Permalink
infoschema: add table_id to table and add tidb_indexes table. (#9183) (
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored and zz-jason committed Mar 23, 2019
1 parent 8fb6dd6 commit 8da8632
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (s *testSuite) TestAggregation(c *C) {

result = tk.MustQuery("select count(*) from information_schema.columns")
// When adding new memory columns in information_schema, please update this variable.
columnCountOfAllInformationSchemaTables := "759"
columnCountOfAllInformationSchemaTables := "769"
result.Check(testkit.Rows(columnCountOfAllInformationSchemaTables))

tk.MustExec("drop table if exists t1")
Expand Down
80 changes: 80 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
tableTableSpaces = "TABLESPACES"
tableCollationCharacterSetApplicability = "COLLATION_CHARACTER_SET_APPLICABILITY"
tableProcesslist = "PROCESSLIST"
tableTiDBIndexes = "TIDB_INDEXES"
)

type columnInfo struct {
Expand Down Expand Up @@ -147,6 +148,7 @@ var tablesCols = []columnInfo{
{"CHECK_SUM", mysql.TypeLonglong, 21, 0, nil, nil},
{"CREATE_OPTIONS", mysql.TypeVarchar, 255, 0, nil, nil},
{"TABLE_COMMENT", mysql.TypeVarchar, 2048, 0, nil, nil},
{"TIDB_TABLE_ID", mysql.TypeLonglong, 21, 0, nil, nil},
}

// See: http://dev.mysql.com/doc/refman/5.7/en/columns-table.html
Expand Down Expand Up @@ -530,6 +532,18 @@ var tableProcesslistCols = []columnInfo{
{"Info", mysql.TypeString, 512, 0, nil, nil},
}

var tableTiDBIndexesCols = []columnInfo{
{"TABLE_SCHEMA", mysql.TypeVarchar, 64, 0, nil, nil},
{"TABLE_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"NON_UNIQUE", mysql.TypeLonglong, 21, 0, nil, nil},
{"KEY_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"SEQ_IN_INDEX", mysql.TypeLonglong, 21, 0, nil, nil},
{"COLUMN_NAME", mysql.TypeVarchar, 64, 0, nil, nil},
{"SUB_PART", mysql.TypeLonglong, 21, 0, nil, nil},
{"INDEX_COMMENT", mysql.TypeVarchar, 2048, 0, nil, nil},
{"INDEX_ID", mysql.TypeLonglong, 21, 0, nil, nil},
}

func dataForCharacterSets() (records [][]types.Datum) {

charsets := charset.GetAllCharsets()
Expand Down Expand Up @@ -911,13 +925,76 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
nil, // CHECKSUM
"", // CREATE_OPTIONS
table.Comment, // TABLE_COMMENT
table.ID, // TIDB_TABLE_ID
)
rows = append(rows, record)
}
}
return rows, nil
}

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(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
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
}
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
col.Name.O, // COLUMN_NAME
subPart, // SUB_PART
idxInfo.Comment, // INDEX_COMMENT
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 @@ -1305,6 +1382,7 @@ var tableNameToColumns = map[string][]columnInfo{
tableTableSpaces: tableTableSpacesCols,
tableCollationCharacterSetApplicability: tableCollationCharacterSetApplicabilityCols,
tableProcesslist: tableProcesslistCols,
tableTiDBIndexes: tableTiDBIndexesCols,
}

func createInfoSchemaTable(handle *Handle, meta *model.TableInfo) *infoschemaTable {
Expand Down Expand Up @@ -1350,6 +1428,8 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
fullRows = dataForSchemata(dbs)
case tableTables:
fullRows, err = dataForTables(ctx, dbs)
case tableTiDBIndexes:
fullRows, err = dataForIndexes(ctx, dbs)
case tableColumns:
fullRows = dataForColumns(ctx, dbs)
case tableStatistics:
Expand Down
20 changes: 20 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package infoschema_test

import (
"strconv"

. "github.com/pingcap/check"
"github.com/pingcap/parser/auth"
"github.com/pingcap/tidb/infoschema"
Expand Down Expand Up @@ -191,3 +193,21 @@ func (s *testSuite) TestProfiling(c *C) {
tk.MustExec("set @@profiling=1")
tk.MustQuery("select * from information_schema.profiling").Check(testkit.Rows("0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"))
}

func (s *testSuite) TestTableIDAndIndexID(c *C) {
testleak.BeforeTest()
defer testleak.AfterTest(c)()
store, err := mockstore.NewMockTikvStore()
c.Assert(err, IsNil)
defer store.Close()
session.SetStatsLease(0)
do, err := session.BootstrapSession(store)
c.Assert(err, IsNil)
defer do.Close()
tk := testkit.NewTestKit(c, store)
tk.MustExec("create table test.t (a int, b int, primary key(a), key k1(b))")
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 * from information_schema.tidb_indexes where table_schema = 'test' and table_name = 't'").Check(testkit.Rows("test t 0 PRIMARY 1 a <nil> 0", "test t 1 k1 1 b <nil> 1"))
}

0 comments on commit 8da8632

Please sign in to comment.