Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema: add TIDB_ROW_ID_SHARDING_INFO for table table. #13418

Merged
merged 3 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"net/http"
"sort"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -177,6 +178,7 @@ var tablesCols = []columnInfo{
{"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},
{"TIDB_ROW_ID_SHARDING_INFO", mysql.TypeVarchar, 255, 0, nil, nil},
}

// See: http://dev.mysql.com/doc/refman/5.7/en/columns-table.html
Expand Down Expand Up @@ -1303,6 +1305,8 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
if rowCount != 0 {
avgRowLength = dataLength / rowCount
}

shardingInfo := GetShardingInfo(schema, table)
record := types.MakeDatums(
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
Expand All @@ -1326,6 +1330,7 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
createOptions, // CREATE_OPTIONS
table.Comment, // TABLE_COMMENT
table.ID, // TIDB_TABLE_ID
shardingInfo, // TIDB_ROW_ID_SHARDING_INFO
)
rows = append(rows, record)
} else {
Expand All @@ -1352,6 +1357,7 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
nil, // CREATE_OPTIONS
"VIEW", // TABLE_COMMENT
table.ID, // TIDB_TABLE_ID
nil, // TIDB_ROW_ID_SHARDING_INFO
)
rows = append(rows, record)
}
Expand All @@ -1360,6 +1366,28 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
return rows, nil
}

// GetShardingInfo returns a nil or description string for the sharding information of given TableInfo.
// The returned description string may be:
// - "NOT_SHARDED": for tables that SHARD_ROW_ID_BITS is not specified.
// - "NOT_SHARDED(PK_IS_HANDLE)": for tables that is primary key is row id.
// - "SHARD_BITS={bit_number}": for tables that with SHARD_ROW_ID_BITS.
// The returned nil indicates that sharding information is not suitable for the table(for example, when the table is a View).
// This function is exported for unit test.
func GetShardingInfo(dbInfo *model.DBInfo, tableInfo *model.TableInfo) interface{} {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djshow832 I suggest changing the return type to string and returning "NIL" if it's not suitable.

Why this is better?

I still prefer to nil because it will be converted as NULL of MySQL, but string "nil" will not.

if dbInfo == nil || tableInfo == nil || tableInfo.IsView() || util.IsMemOrSysDB(dbInfo.Name.L) {
return nil
}
shardingInfo := "NOT_SHARDED"
if tableInfo.PKIsHandle {
shardingInfo = "NOT_SHARDED(PK_IS_HANDLE)"
bb7133 marked this conversation as resolved.
Show resolved Hide resolved
} else {
bb7133 marked this conversation as resolved.
Show resolved Hide resolved
if tableInfo.ShardRowIDBits > 0 {
shardingInfo = "SHARD_BITS=" + strconv.Itoa(int(tableInfo.ShardRowIDBits))
}
}
return shardingInfo
}

func dataForIndexes(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
checker := privilege.GetPrivilegeManager(ctx)
var rows [][]types.Datum
Expand Down
42 changes: 42 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,48 @@ func (s *testTableSuite) TestTableIDAndIndexID(c *C) {
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"))
}

func (s *testTableSuite) TestTableRowIDShardingInfo(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("DROP DATABASE IF EXISTS `sharding_info_test_db`")
tk.MustExec("CREATE DATABASE `sharding_info_test_db`")

assertShardingInfo := func(tableName string, expectInfo interface{}) {
querySQL := fmt.Sprintf("select tidb_row_id_sharding_info from information_schema.tables where table_schema = 'sharding_info_test_db' and table_name = '%s'", tableName)
info := tk.MustQuery(querySQL).Rows()[0][0]
if expectInfo == nil {
c.Assert(info, Equals, "<nil>")
} else {
c.Assert(info, Equals, expectInfo)
}
}
tk.MustExec("CREATE TABLE `sharding_info_test_db`.`t1` (a int)")
assertShardingInfo("t1", "NOT_SHARDED")

tk.MustExec("CREATE TABLE `sharding_info_test_db`.`t2` (a int key)")
assertShardingInfo("t2", "NOT_SHARDED(PK_IS_HANDLE)")

tk.MustExec("CREATE TABLE `sharding_info_test_db`.`t3` (a int) SHARD_ROW_ID_BITS=4")
assertShardingInfo("t3", "SHARD_BITS=4")

tk.MustExec("CREATE VIEW `sharding_info_test_db`.`tv` AS select 1")
assertShardingInfo("tv", nil)

testFunc := func(dbName string, expectInfo interface{}) {
dbInfo := model.DBInfo{Name: model.NewCIStr(dbName)}
tableInfo := model.TableInfo{}

info := infoschema.GetShardingInfo(&dbInfo, &tableInfo)
c.Assert(info, Equals, expectInfo)
}

testFunc("information_schema", nil)
testFunc("mysql", nil)
testFunc("performance_schema", nil)
testFunc("uucc", "NOT_SHARDED")

tk.MustExec("DROP DATABASE `sharding_info_test_db`")
}

func (s *testTableSuite) TestSlowQuery(c *C) {
tk := testkit.NewTestKit(c, s.store)
// Prepare slow log file.
Expand Down