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

planner: projection don't new unnecessary column #13406

Merged
merged 11 commits into from
Nov 20, 2019
22 changes: 11 additions & 11 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,18 +632,18 @@ func (s *testSuiteAgg) TestInjectProjBelowTopN(c *C) {
tk.MustExec("create table t (i int);")
tk.MustExec("insert into t values (1), (1), (1),(2),(3),(2),(3),(2),(3);")
tk.MustQuery("explain select * from t order by i + 1").Check(testkit.Rows(
"Projection_8 10000.00 root Column#3",
"└─Sort_4 10000.00 root Column#4:asc",
" └─Projection_9 10000.00 root Column#3, plus(Column#3, 1)",
"Projection_8 10000.00 root Column#1",
"└─Sort_4 10000.00 root Column#3:asc",
" └─Projection_9 10000.00 root Column#1, plus(Column#1, 1)",
" └─TableReader_7 10000.00 root data:TableScan_6",
" └─TableScan_6 10000.00 cop[tikv] table:t, range:[-inf,+inf], keep order:false, stats:pseudo"))
rs := tk.MustQuery("select * from t order by i + 1 ")
rs.Check(testkit.Rows(
"1", "1", "1", "2", "2", "2", "3", "3", "3"))
tk.MustQuery("explain select * from t order by i + 1 limit 2").Check(testkit.Rows(
"Projection_15 2.00 root Column#3",
"└─TopN_7 2.00 root Column#4:asc, offset:0, count:2",
" └─Projection_16 2.00 root Column#3, plus(Column#1, 1)",
"Projection_15 2.00 root Column#1",
"└─TopN_7 2.00 root Column#3:asc, offset:0, count:2",
" └─Projection_16 2.00 root Column#1, plus(Column#1, 1)",
" └─TableReader_12 2.00 root data:TopN_11",
" └─TopN_11 2.00 cop[tikv] plus(Column#1, 1):asc, offset:0, count:2",
" └─TableScan_10 10000.00 cop[tikv] table:t, range:[-inf,+inf], keep order:false, stats:pseudo"))
Expand Down Expand Up @@ -755,30 +755,30 @@ func (s *testSuiteAgg) TestIssue12759HashAggCalledByApply(c *C) {

// make sure the plan is Apply -> Apply -> Apply -> HashAgg, and the count of Apply is equal to HashAggFinalConcurrency-1.
tk.MustQuery("explain select /*+ hash_agg() */ sum(a), (select NULL from test where tt.a = test.a limit 1),(select NULL from test where tt.a = test.a limit 1),(select NULL from test where tt.a = test.a limit 1) from test tt;").Check(testkit.Rows("" +
"Projection_28 1.00 root Column#3, Column#7, Column#11, Column#15]\n" +
"Projection_28 1.00 root Column#3, Column#6, Column#9, Column#12]\n" +
"[└─Apply_30 1.00 root CARTESIAN left outer join, inner:Projection_65]\n" +
"[ ├─Apply_32 1.00 root CARTESIAN left outer join, inner:Projection_54]\n" +
"[ │ ├─Apply_34 1.00 root CARTESIAN left outer join, inner:Projection_43]\n" +
"[ │ │ ├─HashAgg_39 1.00 root funcs:sum(Column#26), firstrow(Column#27)]\n" +
"[ │ │ ├─HashAgg_39 1.00 root funcs:sum(Column#22), firstrow(Column#23)]\n" +
"[ │ │ │ └─TableReader_40 1.00 root data:HashAgg_35]\n" +
"[ │ │ │ └─HashAgg_35 1.00 cop[tikv] funcs:sum(Column#1), firstrow(Column#1)]\n" +
"[ │ │ │ └─TableScan_38 10000.00 cop[tikv] table:tt, range:[-inf,+inf], keep order:false, stats:pseudo]\n" +
"[ │ │ └─Projection_43 1.00 root NULL]\n" +
"[ │ │ └─Limit_44 1.00 root offset:0, count:1]\n" +
"[ │ │ └─TableReader_50 1.00 root data:Limit_49]\n" +
"[ │ │ └─Limit_49 1.00 cop[tikv] offset:0, count:1]\n" +
"[ │ │ └─Selection_48 1.00 cop[tikv] eq(Column#1, Column#5)]\n" +
"[ │ │ └─Selection_48 1.00 cop[tikv] eq(Column#1, Column#4)]\n" +
"[ │ │ └─TableScan_47 1000.00 cop[tikv] table:test, range:[-inf,+inf], keep order:false, stats:pseudo]\n" +
"[ │ └─Projection_54 1.00 root NULL]\n" +
"[ │ └─Limit_55 1.00 root offset:0, count:1]\n" +
"[ │ └─TableReader_61 1.00 root data:Limit_60]\n" +
"[ │ └─Limit_60 1.00 cop[tikv] offset:0, count:1]\n" +
"[ │ └─Selection_59 1.00 cop[tikv] eq(Column#1, Column#9)]\n" +
"[ │ └─Selection_59 1.00 cop[tikv] eq(Column#1, Column#7)]\n" +
"[ │ └─TableScan_58 1000.00 cop[tikv] table:test, range:[-inf,+inf], keep order:false, stats:pseudo]\n" +
"[ └─Projection_65 1.00 root NULL]\n" +
"[ └─Limit_66 1.00 root offset:0, count:1]\n" +
"[ └─TableReader_72 1.00 root data:Limit_71]\n" +
"[ └─Limit_71 1.00 cop[tikv] offset:0, count:1]\n" +
"[ └─Selection_70 1.00 cop[tikv] eq(Column#1, Column#13)]\n" +
"[ └─Selection_70 1.00 cop[tikv] eq(Column#1, Column#10)]\n" +
"[ └─TableScan_69 1000.00 cop[tikv] table:test, range:[-inf,+inf], keep order:false, stats:pseudo"))
}
4 changes: 2 additions & 2 deletions executor/merge_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (s *testSuite2) TestMergeJoin(c *C) {
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t value(1),(2)")
tk.MustQuery("explain select /*+ TIDB_SMJ(t1, t2) */ * from t t1 join t t2 order by t1.a, t2.a").Check(testkit.Rows(
"Sort_6 100000000.00 root Column#5:asc, Column#6:asc",
"Sort_6 100000000.00 root Column#1:asc, Column#3:asc",
"└─MergeJoin_9 100000000.00 root inner join",
" ├─TableReader_11 10000.00 root data:TableScan_10",
" │ └─TableScan_10 10000.00 cop[tikv] table:t1, range:[-inf,+inf], keep order:false, stats:pseudo",
Expand All @@ -349,7 +349,7 @@ func (s *testSuite2) TestMergeJoin(c *C) {
tk.MustExec("create table s(a int, b int)")
tk.MustExec("insert into s values(1,1)")
tk.MustQuery("explain select /*+ TIDB_SMJ(t, s) */ a in (select a from s where s.b >= t.b) from t").Check(testkit.Rows(
"Projection_7 10000.00 root Column#8",
"Projection_7 10000.00 root Column#7",
"└─MergeJoin_8 10000.00 root left outer semi join, other cond:eq(Column#1, Column#4), ge(Column#5, Column#2)",
" ├─TableReader_10 10000.00 root data:TableScan_9",
" │ └─TableScan_9 10000.00 cop[tikv] table:t, range:[-inf,+inf], keep order:false, stats:pseudo",
Expand Down
4 changes: 2 additions & 2 deletions planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (s *testAnalyzeSuite) TestEstimation(c *C) {
c.Assert(h.DumpStatsDeltaToKV(handle.DumpAll), IsNil)
c.Assert(h.Update(dom.InfoSchema()), IsNil)
testKit.MustQuery("explain select count(*) from t group by a").Check(testkit.Rows(
"HashAgg_9 2.00 root group by:Column#6, funcs:count(Column#5)",
"HashAgg_9 2.00 root group by:Column#5, funcs:count(Column#4)",
"└─TableReader_10 2.00 root data:HashAgg_5",
" └─HashAgg_5 2.00 cop[tikv] group by:Column#1, funcs:count(1)",
" └─TableScan_8 8.00 cop[tikv] table:t, range:[-inf,+inf], keep order:false",
Expand Down Expand Up @@ -700,7 +700,7 @@ func (s *testAnalyzeSuite) TestCorrelatedEstimation(c *C) {
tk.MustExec("analyze table t")
tk.MustQuery("explain select t.c in (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t;").
Check(testkit.Rows(
"Projection_11 10.00 root Column#15",
"Projection_11 10.00 root Column#14",
"└─Apply_13 10.00 root CARTESIAN left outer semi join, inner:StreamAgg_22, other cond:eq(Column#3, Column#13)",
" ├─TableReader_15 10.00 root data:TableScan_14",
" │ └─TableScan_14 10.00 cop[tikv] table:t, range:[-inf,+inf], keep order:false",
Expand Down
11 changes: 7 additions & 4 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,17 +790,20 @@ func (b *PlanBuilder) buildProjectionField(ctx context.Context, p LogicalPlan, f
return nil, nil, err
}
}
newCol := &expression.Column{
UniqueID: b.ctx.GetSessionVars().AllocPlanColumnID(),
RetType: expr.GetType(),
}
name := &types.FieldName{
TblName: tblName,
OrigTblName: origTblName,
ColName: colName,
OrigColName: origColName,
DBName: dbName,
}
if isCol {
return col, name, nil
}
newCol := &expression.Column{
UniqueID: b.ctx.GetSessionVars().AllocPlanColumnID(),
RetType: expr.GetType(),
}
return newCol, name, nil
}

Expand Down
Loading