From 82502cc5e10cd7a23aa9d93abbee2b7b2bba40ab Mon Sep 17 00:00:00 2001 From: Evan Zhou Date: Tue, 1 Sep 2020 15:50:25 +0800 Subject: [PATCH] *: information_schema.tables add TIDB_PK_TYPE column for clustered index (#19642) Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com> --- executor/infoschema_reader.go | 9 ++++++++- executor/infoschema_reader_test.go | 13 +++++++++++++ infoschema/tables.go | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 77f9bf8649..abbe65eda1 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -436,7 +436,7 @@ func (e *memtableRetriever) setDataFromTables(ctx sessionctx.Context, schemas [] if checker != nil && !checker.RequestVerification(ctx.GetSessionVars().ActiveRoles, schema.Name.L, table.Name.L, "", mysql.AllPrivMask) { continue } - + pkType := "NON-CLUSTERED" if !table.IsView() { if table.GetPartitionInfo() != nil { createOptions = "partitioned" @@ -474,6 +474,11 @@ func (e *memtableRetriever) setDataFromTables(ctx sessionctx.Context, schemas [] default: tableType = "BASE TABLE" } + if table.PKIsHandle { + pkType = "INT CLUSTERED" + } else if table.IsCommonHandle { + pkType = "COMMON CLUSTERED" + } shardingInfo := infoschema.GetShardingInfo(schema, table) record := types.MakeDatums( infoschema.CatalogVal, // TABLE_CATALOG @@ -499,6 +504,7 @@ func (e *memtableRetriever) setDataFromTables(ctx sessionctx.Context, schemas [] table.Comment, // TABLE_COMMENT table.ID, // TIDB_TABLE_ID shardingInfo, // TIDB_ROW_ID_SHARDING_INFO + pkType, // TIDB_PK_TYPE ) rows = append(rows, record) } else { @@ -526,6 +532,7 @@ func (e *memtableRetriever) setDataFromTables(ctx sessionctx.Context, schemas [] "VIEW", // TABLE_COMMENT table.ID, // TIDB_TABLE_ID nil, // TIDB_ROW_ID_SHARDING_INFO + pkType, // TIDB_PK_TYPE ) rows = append(rows, record) } diff --git a/executor/infoschema_reader_test.go b/executor/infoschema_reader_test.go index 89d5064c95..8d2736d96c 100644 --- a/executor/infoschema_reader_test.go +++ b/executor/infoschema_reader_test.go @@ -822,3 +822,16 @@ func (s *testInfoschemaTableSuite) TestTiFlashSystemTables(c *C) { err = tk.QueryToErr("select * from information_schema.TIFLASH_SEGMENTS;") c.Assert(err.Error(), Equals, "Etcd addrs not found") } + +func (s *testInfoschemaTableSuite) TestTablesPKType(c *C) { + tk := testkit.NewTestKitWithInit(c, s.store) + tk.MustExec("create table t_int (a int primary key, b int)") + tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_int'").Check(testkit.Rows("INT CLUSTERED")) + tk.MustExec("set @@tidb_enable_clustered_index = 0") + tk.MustExec("create table t_implicit (a varchar(64) primary key, b int)") + tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_implicit'").Check(testkit.Rows("NON-CLUSTERED")) + tk.MustExec("set @@tidb_enable_clustered_index = 1") + tk.MustExec("create table t_common (a varchar(64) primary key, b int)") + tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_common'").Check(testkit.Rows("COMMON CLUSTERED")) + tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'INFORMATION_SCHEMA' and table_name = 'TABLES'").Check(testkit.Rows("NON-CLUSTERED")) +} diff --git a/infoschema/tables.go b/infoschema/tables.go index eb677d4562..6470649e43 100755 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -304,6 +304,7 @@ var tablesCols = []columnInfo{ {name: "TABLE_COMMENT", tp: mysql.TypeVarchar, size: 2048}, {name: "TIDB_TABLE_ID", tp: mysql.TypeLonglong, size: 21}, {name: "TIDB_ROW_ID_SHARDING_INFO", tp: mysql.TypeVarchar, size: 255}, + {name: "TIDB_PK_TYPE", tp: mysql.TypeVarchar, size: 64}, } // See: http://dev.mysql.com/doc/refman/5.7/en/columns-table.html