Skip to content

Commit

Permalink
infoschema: fix TIDB_ROW_ID_SHARDING_INFO for composite primary key (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Apr 15, 2024
1 parent 69d7770 commit eff809f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
16 changes: 7 additions & 9 deletions pkg/infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1713,18 +1713,16 @@ func GetShardingInfo(dbInfo model.CIStr, tableInfo *model.TableInfo) any {
return nil
}
shardingInfo := "NOT_SHARDED"
if tableInfo.PKIsHandle {
if tableInfo.ContainsAutoRandomBits() {
shardingInfo = "PK_AUTO_RANDOM_BITS=" + strconv.Itoa(int(tableInfo.AutoRandomBits))
rangeBits := tableInfo.AutoRandomRangeBits
if rangeBits != 0 && rangeBits != autoid.AutoRandomRangeBitsDefault {
shardingInfo = fmt.Sprintf("%s, RANGE BITS=%d", shardingInfo, rangeBits)
}
} else {
shardingInfo = "NOT_SHARDED(PK_IS_HANDLE)"
if tableInfo.ContainsAutoRandomBits() {
shardingInfo = "PK_AUTO_RANDOM_BITS=" + strconv.Itoa(int(tableInfo.AutoRandomBits))
rangeBits := tableInfo.AutoRandomRangeBits
if rangeBits != 0 && rangeBits != autoid.AutoRandomRangeBitsDefault {
shardingInfo = fmt.Sprintf("%s, RANGE BITS=%d", shardingInfo, rangeBits)
}
} else if tableInfo.ShardRowIDBits > 0 {
shardingInfo = "SHARD_BITS=" + strconv.Itoa(int(tableInfo.ShardRowIDBits))
} else if tableInfo.PKIsHandle {
shardingInfo = "NOT_SHARDED(PK_IS_HANDLE)"
}
return shardingInfo
}
Expand Down
5 changes: 5 additions & 0 deletions tests/integrationtest/r/ddl/db_integration.result
Original file line number Diff line number Diff line change
Expand Up @@ -1298,3 +1298,8 @@ create table t(a int);
alter table t add column b int as ((grouping(a))) stored;
Error 1111 (HY000): Invalid use of group function
drop table t;
drop table if exists t;
create table t (a bigint auto_random, b int, c char(255), primary key(a, b) clustered);
select TIDB_ROW_ID_SHARDING_INFO from information_schema.tables where TABLE_SCHEMA = 'ddl__db_integration' and TABLE_NAME = 't';
TIDB_ROW_ID_SHARDING_INFO
PK_AUTO_RANDOM_BITS=5
5 changes: 5 additions & 0 deletions tests/integrationtest/t/ddl/db_integration.test
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,8 @@ create table t(a int);
--error 1111
alter table t add column b int as ((grouping(a))) stored;
drop table t;

# Test composite primary key should have correct TIDB_ROW_ID_SHARDING_INFO.
drop table if exists t;
create table t (a bigint auto_random, b int, c char(255), primary key(a, b) clustered);
select TIDB_ROW_ID_SHARDING_INFO from information_schema.tables where TABLE_SCHEMA = 'ddl__db_integration' and TABLE_NAME = 't';

0 comments on commit eff809f

Please sign in to comment.