Skip to content

Commit

Permalink
explain: use partition name in original case (#32817)
Browse files Browse the repository at this point in the history
close #32719
  • Loading branch information
mjonss authored Mar 7, 2022
1 parent 62e2078 commit 6dbf2de
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 2 deletions.
169 changes: 169 additions & 0 deletions executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3429,3 +3429,172 @@ func TestIssue27346(t *testing.T) {
"672436701 3937.1887880628115 -7373106839136381229",
"831809724 3937.1887880628115 -4426441253940231780"))
}

func TestPartitionTableExplain(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("create database TestPartitionTableExplain")
tk.MustExec("use TestPartitionTableExplain")
tk.MustExec("set @@tidb_partition_prune_mode = 'static'")
tk.MustExec(`create table t (a int primary key, b int, key (b)) partition by hash(a) (partition P0, partition p1, partition P2)`)
tk.MustExec(`create table t2 (a int, b int)`)
tk.MustExec(`insert into t values (1,1),(2,2),(3,3)`)
tk.MustExec(`insert into t2 values (1,1),(2,2),(3,3)`)
tk.MustExec(`analyze table t`)
tk.MustExec(`analyze table t2`)
tk.MustQuery(`explain format = 'brief' select * from t`).Check(testkit.Rows(
"PartitionUnion 3.00 root ",
"├─TableReader 1.00 root data:TableFullScan",
"│ └─TableFullScan 1.00 cop[tikv] table:t, partition:P0 keep order:false",
"├─TableReader 1.00 root data:TableFullScan",
"│ └─TableFullScan 1.00 cop[tikv] table:t, partition:p1 keep order:false",
"└─TableReader 1.00 root data:TableFullScan",
" └─TableFullScan 1.00 cop[tikv] table:t, partition:P2 keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t partition(P0,p1)`).Check(testkit.Rows(
"PartitionUnion 2.00 root ",
"├─TableReader 1.00 root data:TableFullScan",
"│ └─TableFullScan 1.00 cop[tikv] table:t, partition:P0 keep order:false",
"└─TableReader 1.00 root data:TableFullScan",
" └─TableFullScan 1.00 cop[tikv] table:t, partition:p1 keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a = 1`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:p1 handle:1"))
tk.MustQuery(`explain format = 'brief' select * from t where a = 2`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:P2 handle:2"))
// above ^^ is enough for Issue32719, the below vv for completeness
tk.MustQuery(`explain format = 'brief' select * from t where a = 1 OR a = 2`).Check(testkit.Rows(
"PartitionUnion 2.00 root ",
"├─Batch_Point_Get 1.00 root table:t handle:[1 2], keep order:false, desc:false",
"└─Batch_Point_Get 1.00 root table:t handle:[1 2], keep order:false, desc:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a IN (2,3,4)`).Check(testkit.Rows("Batch_Point_Get 3.00 root table:t handle:[2 3 4], keep order:false, desc:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a IN (2,3)`).Check(testkit.Rows("Batch_Point_Get 2.00 root table:t handle:[2 3], keep order:false, desc:false"))
// above ^^ is for completeness, the below vv is enough for Issue32719
tk.MustQuery(`explain format = 'brief' select * from t where b = 1`).Check(testkit.Rows(
"PartitionUnion 1.00 root ",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P0, index:b(b) range:[1,1], keep order:false",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:p1, index:b(b) range:[1,1], keep order:false",
"└─IndexReader 1.00 root index:IndexRangeScan",
" └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P2, index:b(b) range:[1,1], keep order:false"))
// The below vvv is for completeness
tk.MustQuery(`explain format = 'brief' select * from t where b = 2`).Check(testkit.Rows(
"PartitionUnion 1.00 root ",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P0, index:b(b) range:[2,2], keep order:false",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:p1, index:b(b) range:[2,2], keep order:false",
"└─IndexReader 1.00 root index:IndexRangeScan",
" └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P2, index:b(b) range:[2,2], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where b = 1 OR b = 2`).Check(testkit.Rows(
"PartitionUnion 2.00 root ",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P0, index:b(b) range:[1,2], keep order:false",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:p1, index:b(b) range:[1,2], keep order:false",
"└─IndexReader 1.00 root index:IndexRangeScan",
" └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P2, index:b(b) range:[1,2], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where b IN (2,3,4)`).Check(testkit.Rows(
"PartitionUnion 2.00 root ",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P0, index:b(b) range:[2,2], [3,3], [4,4], keep order:false",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:p1, index:b(b) range:[2,2], [3,3], [4,4], keep order:false",
"└─IndexReader 1.00 root index:IndexRangeScan",
" └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P2, index:b(b) range:[2,2], [3,3], [4,4], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where b IN (2,3)`).Check(testkit.Rows(
"PartitionUnion 2.00 root ",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P0, index:b(b) range:[2,2], [3,3], keep order:false",
"├─IndexReader 1.00 root index:IndexRangeScan",
"│ └─IndexRangeScan 1.00 cop[tikv] table:t, partition:p1, index:b(b) range:[2,2], [3,3], keep order:false",
"└─IndexReader 1.00 root index:IndexRangeScan",
" └─IndexRangeScan 1.00 cop[tikv] table:t, partition:P2, index:b(b) range:[2,2], [3,3], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t,t2 where t2.a = 1 and t2.b = t.b`).Check(testkit.Rows(
"Projection 1.00 root testpartitiontableexplain.t.a, testpartitiontableexplain.t.b, testpartitiontableexplain.t2.a, testpartitiontableexplain.t2.b",
"└─HashJoin 1.00 root inner join, equal:[eq(testpartitiontableexplain.t2.b, testpartitiontableexplain.t.b)]",
" ├─TableReader(Build) 1.00 root data:Selection",
" │ └─Selection 1.00 cop[tikv] eq(testpartitiontableexplain.t2.a, 1), not(isnull(testpartitiontableexplain.t2.b))",
" │ └─TableFullScan 3.00 cop[tikv] table:t2 keep order:false",
" └─PartitionUnion(Probe) 3.00 root ",
" ├─IndexReader 1.00 root index:IndexFullScan",
" │ └─IndexFullScan 1.00 cop[tikv] table:t, partition:P0, index:b(b) keep order:false",
" ├─IndexReader 1.00 root index:IndexFullScan",
" │ └─IndexFullScan 1.00 cop[tikv] table:t, partition:p1, index:b(b) keep order:false",
" └─IndexReader 1.00 root index:IndexFullScan",
" └─IndexFullScan 1.00 cop[tikv] table:t, partition:P2, index:b(b) keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t partition (p1),t2 where t2.a = 1 and t2.b = t.b`).Check(testkit.Rows(
"IndexJoin 1.00 root inner join, inner:IndexReader, outer key:testpartitiontableexplain.t2.b, inner key:testpartitiontableexplain.t.b, equal cond:eq(testpartitiontableexplain.t2.b, testpartitiontableexplain.t.b)",
"├─TableReader(Build) 1.00 root data:Selection",
"│ └─Selection 1.00 cop[tikv] eq(testpartitiontableexplain.t2.a, 1), not(isnull(testpartitiontableexplain.t2.b))",
"│ └─TableFullScan 3.00 cop[tikv] table:t2 keep order:false",
"└─IndexReader(Probe) 1.00 root index:Selection",
" └─Selection 1.00 cop[tikv] not(isnull(testpartitiontableexplain.t.b))",
" └─IndexRangeScan 1.00 cop[tikv] table:t, partition:p1, index:b(b) range: decided by [eq(testpartitiontableexplain.t.b, testpartitiontableexplain.t2.b)], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t,t2 where t2.a = 1 and t2.b = t.b and t.a = 1`).Check(testkit.Rows(
"HashJoin 1.00 root inner join, equal:[eq(testpartitiontableexplain.t.b, testpartitiontableexplain.t2.b)]",
"├─TableReader(Build) 1.00 root data:Selection",
"│ └─Selection 1.00 cop[tikv] eq(testpartitiontableexplain.t2.a, 1), not(isnull(testpartitiontableexplain.t2.b))",
"│ └─TableFullScan 3.00 cop[tikv] table:t2 keep order:false",
"└─Selection(Probe) 1.00 root not(isnull(testpartitiontableexplain.t.b))",
" └─Point_Get 1.00 root table:t, partition:p1 handle:1"))

tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'")
tk.MustExec(`analyze table t`)
tk.MustQuery(`explain format = 'brief' select * from t`).Check(testkit.Rows(
"TableReader 3.00 root partition:all data:TableFullScan",
"└─TableFullScan 3.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t partition(P0,p1)`).Check(testkit.Rows(
"TableReader 3.00 root partition:P0,p1 data:TableFullScan",
"└─TableFullScan 3.00 cop[tikv] table:t keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a = 1`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:p1 handle:1"))
tk.MustQuery(`explain format = 'brief' select * from t where a = 2`).Check(testkit.Rows("Point_Get 1.00 root table:t, partition:P2 handle:2"))
tk.MustQuery(`explain format = 'brief' select * from t where a = 1 OR a = 2`).Check(testkit.Rows(
"TableReader 2.00 root partition:p1,P2 data:TableRangeScan",
"└─TableRangeScan 2.00 cop[tikv] table:t range:[1,1], [2,2], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a IN (2,3,4)`).Check(testkit.Rows("Batch_Point_Get 3.00 root table:t handle:[2 3 4], keep order:false, desc:false"))
tk.MustQuery(`explain format = 'brief' select * from t where a IN (2,3)`).Check(testkit.Rows("Batch_Point_Get 2.00 root table:t handle:[2 3], keep order:false, desc:false"))
tk.MustQuery(`explain format = 'brief' select * from t where b = 1`).Check(testkit.Rows(
"IndexReader 1.00 root partition:all index:IndexRangeScan",
"└─IndexRangeScan 1.00 cop[tikv] table:t, index:b(b) range:[1,1], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t partition (P0,p1) where b = 1`).Check(testkit.Rows(
"IndexReader 1.00 root partition:P0,p1 index:IndexRangeScan",
"└─IndexRangeScan 1.00 cop[tikv] table:t, index:b(b) range:[1,1], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where b = 1 OR b = 2`).Check(testkit.Rows(
"IndexReader 2.00 root partition:all index:IndexRangeScan",
"└─IndexRangeScan 2.00 cop[tikv] table:t, index:b(b) range:[1,2], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t partition (p1,P2) where b = 1 OR b = 2`).Check(testkit.Rows(
"IndexReader 2.00 root partition:p1,P2 index:IndexRangeScan",
"└─IndexRangeScan 2.00 cop[tikv] table:t, index:b(b) range:[1,2], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where b IN (2,3,4)`).Check(testkit.Rows(
"IndexReader 2.00 root partition:all index:IndexRangeScan",
"└─IndexRangeScan 2.00 cop[tikv] table:t, index:b(b) range:[2,2], [3,3], [4,4], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t where b IN (2,3)`).Check(testkit.Rows(
"IndexReader 2.00 root partition:all index:IndexRangeScan",
"└─IndexRangeScan 2.00 cop[tikv] table:t, index:b(b) range:[2,2], [3,3], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t,t2 where t2.a = 1 and t2.b = t.b`).Check(testkit.Rows(
"Projection 1.00 root testpartitiontableexplain.t.a, testpartitiontableexplain.t.b, testpartitiontableexplain.t2.a, testpartitiontableexplain.t2.b",
"└─IndexJoin 1.00 root inner join, inner:IndexReader, outer key:testpartitiontableexplain.t2.b, inner key:testpartitiontableexplain.t.b, equal cond:eq(testpartitiontableexplain.t2.b, testpartitiontableexplain.t.b)",
" ├─TableReader(Build) 1.00 root data:Selection",
" │ └─Selection 1.00 cop[tikv] eq(testpartitiontableexplain.t2.a, 1), not(isnull(testpartitiontableexplain.t2.b))",
" │ └─TableFullScan 3.00 cop[tikv] table:t2 keep order:false",
" └─IndexReader(Probe) 1.00 root partition:all index:Selection",
" └─Selection 1.00 cop[tikv] not(isnull(testpartitiontableexplain.t.b))",
" └─IndexRangeScan 1.00 cop[tikv] table:t, index:b(b) range: decided by [eq(testpartitiontableexplain.t.b, testpartitiontableexplain.t2.b)], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t partition (p1),t2 where t2.a = 1 and t2.b = t.b`).Check(testkit.Rows(
"Projection 1.00 root testpartitiontableexplain.t.a, testpartitiontableexplain.t.b, testpartitiontableexplain.t2.a, testpartitiontableexplain.t2.b",
"└─IndexJoin 1.00 root inner join, inner:IndexReader, outer key:testpartitiontableexplain.t2.b, inner key:testpartitiontableexplain.t.b, equal cond:eq(testpartitiontableexplain.t2.b, testpartitiontableexplain.t.b)",
" ├─TableReader(Build) 1.00 root data:Selection",
" │ └─Selection 1.00 cop[tikv] eq(testpartitiontableexplain.t2.a, 1), not(isnull(testpartitiontableexplain.t2.b))",
" │ └─TableFullScan 3.00 cop[tikv] table:t2 keep order:false",
" └─IndexReader(Probe) 1.00 root partition:p1 index:Selection",
" └─Selection 1.00 cop[tikv] not(isnull(testpartitiontableexplain.t.b))",
" └─IndexRangeScan 1.00 cop[tikv] table:t, index:b(b) range: decided by [eq(testpartitiontableexplain.t.b, testpartitiontableexplain.t2.b)], keep order:false"))
tk.MustQuery(`explain format = 'brief' select * from t,t2 where t2.a = 1 and t2.b = t.b and t.a = 1`).Check(testkit.Rows(
"HashJoin 1.00 root inner join, equal:[eq(testpartitiontableexplain.t.b, testpartitiontableexplain.t2.b)]",
"├─TableReader(Build) 1.00 root data:Selection",
"│ └─Selection 1.00 cop[tikv] eq(testpartitiontableexplain.t2.a, 1), not(isnull(testpartitiontableexplain.t2.b))",
"│ └─TableFullScan 3.00 cop[tikv] table:t2 keep order:false",
"└─TableReader(Probe) 1.00 root partition:p1 data:Selection",
" └─Selection 1.00 cop[tikv] not(isnull(testpartitiontableexplain.t.b))",
" └─TableRangeScan 1.00 cop[tikv] table:t range:[1,1], keep order:false"))
}
2 changes: 1 addition & 1 deletion parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ func (pi *PartitionInfo) GetNameByID(id int64) string {
// see https://github.com/pingcap/parser/pull/1072 for the benchmark.
for i := range definitions {
if id == definitions[i].ID {
return definitions[i].Name.L
return definitions[i].Name.O
}
}
return ""
Expand Down
2 changes: 1 addition & 1 deletion planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (p *PointGetPlan) AccessObject(normalized bool) string {
buffer.WriteString(", partition:?")
} else {
buffer.WriteString(", partition:")
buffer.WriteString(p.PartitionInfo.Name.L)
buffer.WriteString(p.PartitionInfo.Name.O)
}
}
if p.IndexInfo != nil {
Expand Down

0 comments on commit 6dbf2de

Please sign in to comment.