From 8da86327ecf9a96bd0ff8037aee8ed6aad295204 Mon Sep 17 00:00:00 2001 From: lysu Date: Sat, 23 Mar 2019 09:53:49 +0800 Subject: [PATCH] infoschema: add table_id to table and add tidb_indexes table. (#9183) (#9862) --- executor/aggregate_test.go | 2 +- infoschema/tables.go | 80 ++++++++++++++++++++++++++++++++++++++ infoschema/tables_test.go | 20 ++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index 9fdd7c0d8c3e3..85fda82978812 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -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") diff --git a/infoschema/tables.go b/infoschema/tables.go index 3a47bc917656f..d2076a91fd140 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -66,6 +66,7 @@ const ( tableTableSpaces = "TABLESPACES" tableCollationCharacterSetApplicability = "COLLATION_CHARACTER_SET_APPLICABILITY" tableProcesslist = "PROCESSLIST" + tableTiDBIndexes = "TIDB_INDEXES" ) type columnInfo struct { @@ -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 @@ -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() @@ -911,6 +925,7 @@ 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) } @@ -918,6 +933,68 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D 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 @@ -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 { @@ -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: diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index dfe826ea595c4..3f7ae81d9aa49 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -14,6 +14,8 @@ package infoschema_test import ( + "strconv" + . "github.com/pingcap/check" "github.com/pingcap/parser/auth" "github.com/pingcap/tidb/infoschema" @@ -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 0", "test t 1 k1 1 b 1")) +}