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

executor: support show table partition regions (#17229) #17294

Merged
merged 5 commits into from
May 22, 2020
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
77 changes: 75 additions & 2 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4389,16 +4389,52 @@ func (s *testSplitTable) TestShowTableRegion(c *C) {
p := tbl.Meta().GetPartitionInfo().Definitions[i]
c.Assert(rows[i*4+0][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[i*4+1][1], Equals, fmt.Sprintf("t_%d_r_1000000", p.ID))

c.Assert(rows[i*4+2][1], Equals, fmt.Sprintf("t_%d_r_1200000", p.ID))
c.Assert(rows[i*4+3][1], Equals, fmt.Sprintf("t_%d_r_1400000", p.ID))
c.Assert(rows[i*4+4][1], Equals, fmt.Sprintf("t_%d_r_1600000", p.ID))
c.Assert(rows[i*4+5][1], Equals, fmt.Sprintf("t_%d_r_1800000", p.ID))

c.Assert(rows[i*4+6][1], Equals, fmt.Sprintf("t_%d_r_2000000", p.ID))
c.Assert(rows[i*4+7][1], Equals, fmt.Sprintf("t_%d_r_3000000", p.ID))
}

// Test for show table partition regions.
for i := 0; i < 4; i++ {
re = tk.MustQuery(fmt.Sprintf("show table t partition (p%v) regions", i))
rows = re.Rows()
c.Assert(len(rows), Equals, 4)
p := tbl.Meta().GetPartitionInfo().Definitions[i]
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_1000000", p.ID))
c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_2000000", p.ID))
c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_3000000", p.ID))
}
re = tk.MustQuery("show table t partition (p0, p4) regions")
rows = re.Rows()
c.Assert(len(rows), Equals, 12)
p := tbl.Meta().GetPartitionInfo().Definitions[0]
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_1000000", p.ID))
c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_2000000", p.ID))
c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_3000000", p.ID))
p = tbl.Meta().GetPartitionInfo().Definitions[4]
c.Assert(rows[4][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[5][1], Equals, fmt.Sprintf("t_%d_r_1000000", p.ID))
c.Assert(rows[6][1], Equals, fmt.Sprintf("t_%d_r_1200000", p.ID))
c.Assert(rows[7][1], Equals, fmt.Sprintf("t_%d_r_1400000", p.ID))
c.Assert(rows[8][1], Equals, fmt.Sprintf("t_%d_r_1600000", p.ID))
c.Assert(rows[9][1], Equals, fmt.Sprintf("t_%d_r_1800000", p.ID))
c.Assert(rows[10][1], Equals, fmt.Sprintf("t_%d_r_2000000", p.ID))
c.Assert(rows[11][1], Equals, fmt.Sprintf("t_%d_r_3000000", p.ID))
// Test for duplicate partition names.
re = tk.MustQuery("show table t partition (p0, p0, p0) regions")
rows = re.Rows()
c.Assert(len(rows), Equals, 4)
p = tbl.Meta().GetPartitionInfo().Definitions[0]
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_1000000", p.ID))
c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_2000000", p.ID))
c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_3000000", p.ID))

// Test split partition table index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int,b int,index idx(a)) partition by hash(a) partitions 5;")
Expand Down Expand Up @@ -4455,6 +4491,39 @@ func (s *testSplitTable) TestShowTableRegion(c *C) {
c.Assert(rows[i*8+11][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
}

// Test show table partition region on unknown-partition.
err := tk.QueryToErr("show table t partition (p_unknown) index idx regions")
c.Assert(terror.ErrorEqual(err, table.ErrUnknownPartition), IsTrue)

// Test show table partition index.
for i := 0; i < 4; i++ {
re = tk.MustQuery(fmt.Sprintf("show table t partition (p%v) index idx regions", i))
rows = re.Rows()
c.Assert(len(rows), Equals, 4)
p := tbl.Meta().GetPartitionInfo().Definitions[i]
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[1][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[2][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[3][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
}
re = tk.MustQuery("show table t partition (p3,p4) index idx regions")
rows = re.Rows()
c.Assert(len(rows), Equals, 12)
p = tbl.Meta().GetPartitionInfo().Definitions[3]
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[1][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[2][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[3][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
p = tbl.Meta().GetPartitionInfo().Definitions[4]
c.Assert(rows[4][1], Equals, fmt.Sprintf("t_%d_", p.ID))
c.Assert(rows[5][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[6][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[7][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[8][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[9][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[10][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))
c.Assert(rows[11][1], Matches, fmt.Sprintf("t_%d_i_1_.*", p.ID))

// Test split for the second index.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int,b int,index idx(a), index idx2(b))")
Expand All @@ -4467,6 +4536,10 @@ func (s *testSplitTable) TestShowTableRegion(c *C) {
c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_", tbl.Meta().ID))
c.Assert(rows[2][1], Matches, fmt.Sprintf("t_%d_i_2_.*", tbl.Meta().ID))
c.Assert(rows[3][1], Matches, fmt.Sprintf("t_%d_i_2_.*", tbl.Meta().ID))

// Test show table partition region on non-partition table.
err = tk.QueryToErr("show table t partition (p3,p4) index idx regions")
c.Assert(terror.ErrorEqual(err, plannercore.ErrPartitionClauseOnNonpartitioned), IsTrue)
}

func testGetTableByName(c *C, ctx sessionctx.Context, db, table string) table.Table {
Expand Down
66 changes: 34 additions & 32 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -1468,16 +1469,37 @@ func (e *ShowExec) fetchShowTableRegions() error {
return errors.Trace(err)
}

physicalIDs := []int64{}
if pi := tb.Meta().GetPartitionInfo(); pi != nil {
for _, name := range e.Table.PartitionNames {
pid, err := tables.FindPartitionByName(tb.Meta(), name.L)
if err != nil {
return err
}
physicalIDs = append(physicalIDs, pid)
}
if len(physicalIDs) == 0 {
for _, p := range pi.Definitions {
physicalIDs = append(physicalIDs, p.ID)
}
}
} else {
if len(e.Table.PartitionNames) != 0 {
return plannercore.ErrPartitionClauseOnNonpartitioned
}
physicalIDs = append(physicalIDs, tb.Meta().ID)
}

// Get table regions from from pd, not from regionCache, because the region cache maybe outdated.
var regions []regionMeta
if len(e.IndexName.L) != 0 {
indexInfo := tb.Meta().FindIndexByName(e.IndexName.L)
if indexInfo == nil {
return plannercore.ErrKeyDoesNotExist.GenWithStackByArgs(e.IndexName, tb.Meta().Name)
}
regions, err = getTableIndexRegions(tb, indexInfo, tikvStore, splitStore)
regions, err = getTableIndexRegions(indexInfo, physicalIDs, tikvStore, splitStore)
} else {
regions, err = getTableRegions(tb, tikvStore, splitStore)
regions, err = getTableRegions(tb, physicalIDs, tikvStore, splitStore)
}

if err != nil {
Expand All @@ -1487,48 +1509,28 @@ func (e *ShowExec) fetchShowTableRegions() error {
return nil
}

func getTableRegions(tb table.Table, tikvStore tikv.Storage, splitStore kv.SplittableStore) ([]regionMeta, error) {
if info := tb.Meta().GetPartitionInfo(); info != nil {
return getPartitionTableRegions(info, tb.(table.PartitionedTable), tikvStore, splitStore)
}
return getPhysicalTableRegions(tb.Meta().ID, tb.Meta(), tikvStore, splitStore, nil)
}

func getTableIndexRegions(tb table.Table, indexInfo *model.IndexInfo, tikvStore tikv.Storage, splitStore kv.SplittableStore) ([]regionMeta, error) {
if info := tb.Meta().GetPartitionInfo(); info != nil {
return getPartitionIndexRegions(info, tb.(table.PartitionedTable), indexInfo, tikvStore, splitStore)
}
return getPhysicalIndexRegions(tb.Meta().ID, indexInfo, tikvStore, splitStore, nil)
}

func getPartitionTableRegions(info *model.PartitionInfo, tbl table.PartitionedTable, tikvStore tikv.Storage, splitStore kv.SplittableStore) ([]regionMeta, error) {
regions := make([]regionMeta, 0, len(info.Definitions))
func getTableRegions(tb table.Table, physicalIDs []int64, tikvStore tikv.Storage, splitStore kv.SplittableStore) ([]regionMeta, error) {
regions := make([]regionMeta, 0, len(physicalIDs))
uniqueRegionMap := make(map[uint64]struct{})
for _, def := range info.Definitions {
pid := def.ID
partition := tbl.GetPartition(pid)
partition.GetPhysicalID()
partitionRegions, err := getPhysicalTableRegions(partition.GetPhysicalID(), tbl.Meta(), tikvStore, splitStore, uniqueRegionMap)
for _, id := range physicalIDs {
rs, err := getPhysicalTableRegions(id, tb.Meta(), tikvStore, splitStore, uniqueRegionMap)
if err != nil {
return nil, err
}
regions = append(regions, partitionRegions...)
regions = append(regions, rs...)
}
return regions, nil
}

func getPartitionIndexRegions(info *model.PartitionInfo, tbl table.PartitionedTable, indexInfo *model.IndexInfo, tikvStore tikv.Storage, splitStore kv.SplittableStore) ([]regionMeta, error) {
var regions []regionMeta
func getTableIndexRegions(indexInfo *model.IndexInfo, physicalIDs []int64, tikvStore tikv.Storage, splitStore kv.SplittableStore) ([]regionMeta, error) {
regions := make([]regionMeta, 0, len(physicalIDs))
uniqueRegionMap := make(map[uint64]struct{})
for _, def := range info.Definitions {
pid := def.ID
partition := tbl.GetPartition(pid)
partition.GetPhysicalID()
partitionRegions, err := getPhysicalIndexRegions(partition.GetPhysicalID(), indexInfo, tikvStore, splitStore, uniqueRegionMap)
for _, id := range physicalIDs {
rs, err := getPhysicalIndexRegions(id, indexInfo, tikvStore, splitStore, uniqueRegionMap)
if err != nil {
return nil, err
}
regions = append(regions, partitionRegions...)
regions = append(regions, rs...)
}
return regions, nil
}
Expand Down