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

*: fix bug of same type plans with different plan ... (#19519) #19684

Merged
merged 6 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 17 additions & 5 deletions expression/aggregation/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// ExplainAggFunc generates explain information for a aggregation function.
func ExplainAggFunc(agg *AggFuncDesc) string {
func ExplainAggFunc(agg *AggFuncDesc, normalized bool) string {
var buffer bytes.Buffer
fmt.Fprintf(&buffer, "%s(", agg.Name)
if agg.HasDistinct {
Expand All @@ -32,11 +32,19 @@ func ExplainAggFunc(agg *AggFuncDesc) string {
if len(agg.OrderByItems) > 0 {
buffer.WriteString(" order by ")
for i, item := range agg.OrderByItems {
order := "asc"
if item.Desc {
order = "desc"
if normalized {
fmt.Fprintf(&buffer, "%s desc", item.Expr.ExplainNormalizedInfo())
} else {
fmt.Fprintf(&buffer, "%s desc", item.Expr.ExplainInfo())
}
} else {
if normalized {
fmt.Fprintf(&buffer, "%s asc", item.Expr.ExplainNormalizedInfo())
} else {
fmt.Fprintf(&buffer, "%s asc", item.Expr.ExplainInfo())
}
}
fmt.Fprintf(&buffer, "%s %s", item.Expr.ExplainInfo(), order)
if i+1 < len(agg.OrderByItems) {
buffer.WriteString(", ")
}
Expand All @@ -46,7 +54,11 @@ func ExplainAggFunc(agg *AggFuncDesc) string {
} else if i != 0 {
buffer.WriteString(", ")
}
buffer.WriteString(arg.ExplainInfo())
if normalized {
buffer.WriteString(arg.ExplainNormalizedInfo())
} else {
buffer.WriteString(arg.ExplainInfo())
}
}
buffer.WriteString(")")
return buffer.String()
Expand Down
5 changes: 4 additions & 1 deletion expression/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ func (col *Column) ExplainInfo() string {

// ExplainNormalizedInfo implements the Expression interface.
func (col *Column) ExplainNormalizedInfo() string {
return col.ExplainInfo()
if col.OrigName != "" {
return col.OrigName
}
return "?"
}

// ExplainInfo implements the Expression interface.
Expand Down
2 changes: 1 addition & 1 deletion planner/core/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (d *planDigester) normalizePlanTree(p PhysicalPlan) {
}

func (d *planDigester) normalizePlan(p PhysicalPlan, isRoot bool, depth int) {
plancodec.NormalizePlanNode(depth, p.ID(), p.TP(), isRoot, p.ExplainNormalizedInfo(), &d.buf)
plancodec.NormalizePlanNode(depth, p.TP(), isRoot, p.ExplainNormalizedInfo(), &d.buf)
d.encodedPlans[p.ID()] = true

depth++
Expand Down
22 changes: 17 additions & 5 deletions planner/core/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func (p *PhysicalIndexScan) AccessObject() string {
func (p *PhysicalIndexScan) OperatorInfo(normalized bool) string {
buffer := bytes.NewBufferString("")
if len(p.rangeInfo) > 0 {
fmt.Fprintf(buffer, "range: decided by %v, ", p.rangeInfo)
if !normalized {
fmt.Fprintf(buffer, "range: decided by %v, ", p.rangeInfo)
}
} else if p.haveCorCol() {
if normalized {
fmt.Fprintf(buffer, "range: decided by %s, ", expression.SortedExplainNormalizedExpressionList(p.AccessCondition))
Expand Down Expand Up @@ -249,7 +251,7 @@ func (p *PhysicalTableReader) ExplainInfo() string {

// ExplainNormalizedInfo implements Plan interface.
func (p *PhysicalTableReader) ExplainNormalizedInfo() string {
return p.ExplainInfo()
return ""
}

// ExplainInfo implements Plan interface.
Expand Down Expand Up @@ -335,7 +337,13 @@ func (p *basePhysicalAgg) explainInfo(normalized bool) string {
}
for i := 0; i < len(p.AggFuncs); i++ {
builder.WriteString("funcs:")
fmt.Fprintf(builder, "%v->%v", aggregation.ExplainAggFunc(p.AggFuncs[i]), p.schema.Columns[i])
var colName string
if normalized {
colName = p.schema.Columns[i].ExplainNormalizedInfo()
} else {
colName = p.schema.Columns[i].ExplainInfo()
}
fmt.Fprintf(builder, "%v->%v", aggregation.ExplainAggFunc(p.AggFuncs[i], normalized), colName)
if i+1 < len(p.AggFuncs) {
builder.WriteString(", ")
}
Expand All @@ -360,7 +368,11 @@ func (p *PhysicalIndexJoin) explainInfo(normalized bool) string {
}

buffer := bytes.NewBufferString(p.JoinType.String())
fmt.Fprintf(buffer, ", inner:%s", p.Children()[p.InnerChildIdx].ExplainID())
if normalized {
fmt.Fprintf(buffer, ", inner:%s", p.Children()[p.InnerChildIdx].TP())
} else {
fmt.Fprintf(buffer, ", inner:%s", p.Children()[p.InnerChildIdx].ExplainID())
}
if len(p.OuterJoinKeys) > 0 {
fmt.Fprintf(buffer, ", outer key:%s",
expression.ExplainColumnList(p.OuterJoinKeys))
Expand Down Expand Up @@ -623,7 +635,7 @@ func (p *LogicalAggregation) ExplainInfo() string {
if len(p.AggFuncs) > 0 {
buffer.WriteString("funcs:")
for i, agg := range p.AggFuncs {
buffer.WriteString(aggregation.ExplainAggFunc(agg))
buffer.WriteString(aggregation.ExplainAggFunc(agg, false))
if i+1 < len(p.AggFuncs) {
buffer.WriteString(", ")
}
Expand Down
70 changes: 69 additions & 1 deletion planner/core/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,56 @@ func (s *testPlanNormalize) TestEncodeDecodePlan(c *C) {
func (s *testPlanNormalize) TestNormalizedDigest(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1,t2")
tk.MustExec("drop table if exists t1,t2, bmsql_order_line, bmsql_district,bmsql_stock")
tk.MustExec("create table t1 (a int key,b int,c int, index (b));")
tk.MustExec("create table t2 (a int key,b int,c int, index (b));")
tk.MustExec(`CREATE TABLE bmsql_order_line (
ol_w_id int(11) NOT NULL,
ol_d_id int(11) NOT NULL,
ol_o_id int(11) NOT NULL,
ol_number int(11) NOT NULL,
ol_i_id int(11) NOT NULL,
ol_delivery_d timestamp NULL DEFAULT NULL,
ol_amount decimal(6,2) DEFAULT NULL,
ol_supply_w_id int(11) DEFAULT NULL,
ol_quantity int(11) DEFAULT NULL,
ol_dist_info char(24) DEFAULT NULL,
PRIMARY KEY ( ol_w_id , ol_d_id , ol_o_id , ol_number )
);`)
tk.MustExec(`CREATE TABLE bmsql_district (
d_w_id int(11) NOT NULL,
d_id int(11) NOT NULL,
d_ytd decimal(12,2) DEFAULT NULL,
d_tax decimal(4,4) DEFAULT NULL,
d_next_o_id int(11) DEFAULT NULL,
d_name varchar(10) DEFAULT NULL,
d_street_1 varchar(20) DEFAULT NULL,
d_street_2 varchar(20) DEFAULT NULL,
d_city varchar(20) DEFAULT NULL,
d_state char(2) DEFAULT NULL,
d_zip char(9) DEFAULT NULL,
PRIMARY KEY ( d_w_id , d_id )
);`)
tk.MustExec(`CREATE TABLE bmsql_stock (
s_w_id int(11) NOT NULL,
s_i_id int(11) NOT NULL,
s_quantity int(11) DEFAULT NULL,
s_ytd int(11) DEFAULT NULL,
s_order_cnt int(11) DEFAULT NULL,
s_remote_cnt int(11) DEFAULT NULL,
s_data varchar(50) DEFAULT NULL,
s_dist_01 char(24) DEFAULT NULL,
s_dist_02 char(24) DEFAULT NULL,
s_dist_03 char(24) DEFAULT NULL,
s_dist_04 char(24) DEFAULT NULL,
s_dist_05 char(24) DEFAULT NULL,
s_dist_06 char(24) DEFAULT NULL,
s_dist_07 char(24) DEFAULT NULL,
s_dist_08 char(24) DEFAULT NULL,
s_dist_09 char(24) DEFAULT NULL,
s_dist_10 char(24) DEFAULT NULL,
PRIMARY KEY ( s_w_id , s_i_id )
);`)
normalizedDigestCases := []struct {
sql1 string
sql2 string
Expand Down Expand Up @@ -197,6 +244,27 @@ func (s *testPlanNormalize) TestNormalizedDigest(c *C) {
sql2: "select count(1) as num,a from t1 where a=2 group by a union select count(1) as num,a from t1 where a=4 group by a;",
isSame: true,
},
{
sql1: `SELECT COUNT(*) AS low_stock
FROM
(
SELECT *
FROM bmsql_stock
WHERE s_w_id = 1
AND s_quantity < 2
AND s_i_id IN ( SELECT /*+ TIDB_INLJ(bmsql_order_line) */ ol_i_id FROM bmsql_district JOIN bmsql_order_line ON ol_w_id = d_w_id AND ol_d_id = d_id AND ol_o_id >= d_next_o_id - 20 AND ol_o_id < d_next_o_id WHERE d_w_id = 1 AND d_id = 2 )
) AS L;`,
sql2: `SELECT COUNT(*) AS low_stock
FROM
(
SELECT *
FROM bmsql_stock
WHERE s_w_id = 5
AND s_quantity < 6
AND s_i_id IN ( SELECT /*+ TIDB_INLJ(bmsql_order_line) */ ol_i_id FROM bmsql_district JOIN bmsql_order_line ON ol_w_id = d_w_id AND ol_d_id = d_id AND ol_o_id >= d_next_o_id - 70 AND ol_o_id < d_next_o_id WHERE d_w_id = 5 AND d_id = 6 )
) AS L;`,
isSame: true,
},
}
for _, testCase := range normalizedDigestCases {
testNormalizeDigest(tk, c, testCase.sql1, testCase.sql2, testCase.isSame)
Expand Down
Loading