Skip to content

Commit

Permalink
planner: remove the same projection when to InjectExtraProjection
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <[email protected]>
  • Loading branch information
hawkingrei committed Jun 22, 2024
1 parent 5197678 commit 8f7c860
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 107 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,8 @@
"└─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection 8000.00 mpp[tiflash] test.t.a, Column#4",
" └─Projection 8000.00 mpp[tiflash] Column#4, test.t.a",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#7, funcs:sum(Column#5)->Column#4, funcs:firstrow(Column#6)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#5, test.t.a->Column#6, test.t.a->Column#7",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#6, funcs:sum(Column#5)->Column#4, funcs:firstrow(Column#6)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#5, test.t.a->Column#6",
" └─ExchangeReceiver 10000.00 mpp[tiflash] ",
" └─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary]",
" └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1278,8 +1278,8 @@
"└─ExchangeSender 2.00 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection 2.00 mpp[tiflash] test.table_1.id, Column#4, Column#5",
" └─Projection 2.00 mpp[tiflash] div(Column#4, cast(case(eq(Column#20, 0), 1, Column#20), decimal(20,0) BINARY))->Column#4, div(Column#5, cast(case(eq(Column#21, 0), 1, Column#21), decimal(20,0) BINARY))->Column#5, test.table_1.id",
" └─HashAgg 2.00 mpp[tiflash] group by:Column#34, funcs:count(Column#29)->Column#20, funcs:sum(Column#30)->Column#4, funcs:count(Column#31)->Column#21, funcs:sum(Column#32)->Column#5, funcs:firstrow(Column#33)->test.table_1.id",
" └─Projection 2.00 mpp[tiflash] plus(test.table_1.value, 1)->Column#29, plus(test.table_1.value, 1)->Column#30, test.table_1.value->Column#31, test.table_1.value->Column#32, test.table_1.id->Column#33, test.table_1.id->Column#34",
" └─HashAgg 2.00 mpp[tiflash] group by:Column#33, funcs:count(Column#29)->Column#20, funcs:sum(Column#30)->Column#4, funcs:count(Column#31)->Column#21, funcs:sum(Column#32)->Column#5, funcs:firstrow(Column#33)->test.table_1.id",
" └─Projection 2.00 mpp[tiflash] plus(test.table_1.value, 1)->Column#29, plus(test.table_1.value, 1)->Column#30, test.table_1.value->Column#31, test.table_1.value->Column#32, test.table_1.id->Column#33",
" └─ExchangeReceiver 2.00 mpp[tiflash] ",
" └─ExchangeSender 2.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.table_1.id, collate: binary]",
" └─TableFullScan 2.00 mpp[tiflash] table:table_1 keep order:false"
Expand Down Expand Up @@ -2124,8 +2124,8 @@
" │ └─Selection 9990.00 mpp[tiflash] not(isnull(test.t.id))",
" │ └─TableFullScan 10000.00 mpp[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo",
" └─Projection(Probe) 7992.00 mpp[tiflash] Column#11, test.t.id",
" └─HashAgg 7992.00 mpp[tiflash] group by:Column#39, funcs:sum(Column#37)->Column#11, funcs:firstrow(Column#38)->test.t.id",
" └─Projection 9990.00 mpp[tiflash] cast(test.t.id, decimal(10,0) BINARY)->Column#37, test.t.id->Column#38, test.t.id->Column#39",
" └─HashAgg 7992.00 mpp[tiflash] group by:Column#38, funcs:sum(Column#37)->Column#11, funcs:firstrow(Column#38)->test.t.id",
" └─Projection 9990.00 mpp[tiflash] cast(test.t.id, decimal(10,0) BINARY)->Column#37, test.t.id->Column#38",
" └─Projection 9990.00 mpp[tiflash] test.t.id, test.t.id",
" └─HashJoin 9990.00 mpp[tiflash] inner join, equal:[eq(test.t.id, test.t.id)]",
" ├─Projection(Build) 7992.00 mpp[tiflash] test.t.id, Column#13",
Expand Down Expand Up @@ -2430,8 +2430,8 @@
" │ └─Selection 9990.00 mpp[tiflash] not(isnull(test.t.id))",
" │ └─TableFullScan 10000.00 mpp[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo",
" └─Projection(Probe) 7992.00 mpp[tiflash] Column#11, test.t.id",
" └─HashAgg 7992.00 mpp[tiflash] group by:Column#34, funcs:sum(Column#32)->Column#11, funcs:firstrow(Column#33)->test.t.id",
" └─Projection 9990.00 mpp[tiflash] cast(test.t.id, decimal(10,0) BINARY)->Column#32, test.t.id->Column#33, test.t.id->Column#34",
" └─HashAgg 7992.00 mpp[tiflash] group by:Column#33, funcs:sum(Column#32)->Column#11, funcs:firstrow(Column#33)->test.t.id",
" └─Projection 9990.00 mpp[tiflash] cast(test.t.id, decimal(10,0) BINARY)->Column#32, test.t.id->Column#33",
" └─ExchangeReceiver 9990.00 mpp[tiflash] ",
" └─ExchangeSender 9990.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.id, collate: binary]",
" └─Projection 9990.00 mpp[tiflash] test.t.id, test.t.id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"└─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection 8000.00 mpp[tiflash] test.t.a, Column#5",
" └─Projection 8000.00 mpp[tiflash] Column#5, test.t.a",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#10, Column#11, funcs:sum(Column#8)->Column#5, funcs:firstrow(Column#9)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#8, test.t.a->Column#9, test.t.a->Column#10, test.t.c->Column#11",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#10, Column#9, funcs:sum(Column#8)->Column#5, funcs:firstrow(Column#9)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#8, test.t.a->Column#9, test.t.c->Column#10",
" └─ExchangeReceiver 10000.00 mpp[tiflash] ",
" └─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary], [name: test.t.c, collate: binary]",
" └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
Expand Down Expand Up @@ -72,8 +72,8 @@
"└─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection 8000.00 mpp[tiflash] test.t.a, Column#5",
" └─Projection 8000.00 mpp[tiflash] Column#5, test.t.a",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#8, Column#9, funcs:sum(Column#6)->Column#5, funcs:firstrow(Column#7)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#6, test.t.a->Column#7, test.t.a->Column#8, test.t.c->Column#9",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#7, Column#8, funcs:sum(Column#6)->Column#5, funcs:firstrow(Column#7)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#6, test.t.a->Column#7, test.t.c->Column#8",
" └─ExchangeReceiver 10000.00 mpp[tiflash] ",
" └─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary], [name: test.t.c, collate: binary]",
" └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
Expand Down Expand Up @@ -329,8 +329,8 @@
"└─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection 8000.00 mpp[tiflash] test.t.a, Column#5",
" └─Projection 8000.00 mpp[tiflash] Column#5, test.t.a",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#8, Column#9, funcs:sum(Column#6)->Column#5, funcs:firstrow(Column#7)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#6, test.t.a->Column#7, test.t.a->Column#8, test.t.c->Column#9",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#7, Column#8, funcs:sum(Column#6)->Column#5, funcs:firstrow(Column#7)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#6, test.t.a->Column#7, test.t.c->Column#8",
" └─ExchangeReceiver 10000.00 mpp[tiflash] ",
" └─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary], [name: test.t.c, collate: binary]",
" └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
Expand Down Expand Up @@ -375,8 +375,8 @@
"└─ExchangeSender 2666.67 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection 2666.67 mpp[tiflash] test.t.a, Column#5",
" └─Projection 2666.67 mpp[tiflash] Column#5, test.t.a",
" └─HashAgg 2666.67 mpp[tiflash] group by:Column#8, Column#9, funcs:sum(Column#6)->Column#5, funcs:firstrow(Column#7)->test.t.a",
" └─Projection 3333.33 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#6, test.t.a->Column#7, test.t.a->Column#8, test.t.c->Column#9",
" └─HashAgg 2666.67 mpp[tiflash] group by:Column#7, Column#8, funcs:sum(Column#6)->Column#5, funcs:firstrow(Column#7)->test.t.a",
" └─Projection 3333.33 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#6, test.t.a->Column#7, test.t.c->Column#8",
" └─ExchangeReceiver 3333.33 mpp[tiflash] ",
" └─ExchangeSender 3333.33 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary], [name: test.t.c, collate: binary]",
" └─Selection 3333.33 mpp[tiflash] gt(test.t.a, 1)",
Expand Down Expand Up @@ -513,8 +513,8 @@
"└─ExchangeSender 8000.00 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection 8000.00 mpp[tiflash] test.t.a, Column#5",
" └─Projection 8000.00 mpp[tiflash] Column#5, test.t.a",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#10, Column#11, funcs:sum(Column#8)->Column#5, funcs:firstrow(Column#9)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#8, test.t.a->Column#9, test.t.a->Column#10, test.t.c->Column#11",
" └─HashAgg 8000.00 mpp[tiflash] group by:Column#10, Column#9, funcs:sum(Column#8)->Column#5, funcs:firstrow(Column#9)->test.t.a",
" └─Projection 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#8, test.t.a->Column#9, test.t.c->Column#10",
" └─ExchangeReceiver 10000.00 mpp[tiflash] ",
" └─ExchangeSender 10000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary], [name: test.t.c, collate: binary]",
" └─TableFullScan 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
Expand Down Expand Up @@ -623,8 +623,8 @@
" ├─ExchangeReceiver(Build) 7992.00 mpp[tiflash] ",
" │ └─ExchangeSender 7992.00 mpp[tiflash] ExchangeType: Broadcast, Compression: FAST",
" │ └─Projection 7992.00 mpp[tiflash] Column#9, test.t.b",
" │ └─HashAgg 7992.00 mpp[tiflash] group by:Column#26, funcs:sum(Column#24)->Column#9, funcs:firstrow(Column#25)->test.t.b",
" │ └─Projection 9990.00 mpp[tiflash] cast(test.t.a, decimal(10,0) BINARY)->Column#24, test.t.b->Column#25, test.t.b->Column#26",
" │ └─HashAgg 7992.00 mpp[tiflash] group by:Column#25, funcs:sum(Column#24)->Column#9, funcs:firstrow(Column#25)->test.t.b",
" │ └─Projection 9990.00 mpp[tiflash] cast(test.t.a, decimal(10,0) BINARY)->Column#24, test.t.b->Column#25",
" │ └─ExchangeReceiver 9990.00 mpp[tiflash] ",
" │ └─ExchangeSender 9990.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.b, collate: binary]",
" │ └─Selection 9990.00 mpp[tiflash] not(isnull(test.t.b))",
Expand Down Expand Up @@ -1172,8 +1172,8 @@
"└─ExchangeSender_30 8000.00 mpp[tiflash] ExchangeType: PassThrough",
" └─Projection_5 8000.00 mpp[tiflash] test.t.a, Column#5",
" └─Projection_29 8000.00 mpp[tiflash] Column#5, test.t.a",
" └─HashAgg_27 8000.00 mpp[tiflash] group by:Column#10, Column#11, funcs:sum(Column#8)->Column#5, funcs:firstrow(Column#9)->test.t.a",
" └─Projection_32 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#8, test.t.a->Column#9, test.t.a->Column#10, test.t.c->Column#11",
" └─HashAgg_27 8000.00 mpp[tiflash] group by:Column#10, Column#9, funcs:sum(Column#8)->Column#5, funcs:firstrow(Column#9)->test.t.a",
" └─Projection_32 10000.00 mpp[tiflash] cast(test.t.b, decimal(10,0) BINARY)->Column#8, test.t.a->Column#9, test.t.c->Column#10",
" └─ExchangeReceiver_23 10000.00 mpp[tiflash] ",
" └─ExchangeSender_22 10000.00 mpp[tiflash] ExchangeType: HashPartition, Compression: FAST, Hash Cols: [name: test.t.a, collate: binary], [name: test.t.c, collate: binary]",
" └─TableFullScan_21 10000.00 mpp[tiflash] table:t keep order:false, stats:pseudo"
Expand Down Expand Up @@ -3597,15 +3597,15 @@
"TopN 20.00 root Column#10, offset:0, count:20",
"└─HashAgg 63.95 root group by:test.tbl_16.col_100, funcs:avg(Column#11, Column#12)->Column#10",
" └─PartitionUnion 63.95 root ",
" ├─StreamAgg 31.98 root group by:Column#22, funcs:count(Column#19)->Column#11, funcs:sum(Column#20)->Column#12, funcs:firstrow(Column#21)->test.tbl_16.col_100",
" │ └─Projection 39.97 root test.tbl_16.col_100->Column#19, cast(test.tbl_16.col_100, decimal(8,0) UNSIGNED BINARY)->Column#20, test.tbl_16.col_100->Column#21, test.tbl_16.col_100->Column#22",
" ├─StreamAgg 31.98 root group by:Column#19, funcs:count(Column#19)->Column#11, funcs:sum(Column#20)->Column#12, funcs:firstrow(Column#21)->test.tbl_16.col_100",
" │ └─Projection 39.97 root test.tbl_16.col_100->Column#19, cast(test.tbl_16.col_100, decimal(8,0) UNSIGNED BINARY)->Column#20, test.tbl_16.col_100->Column#21",
" │ └─Sort 39.97 root test.tbl_16.col_100",
" │ └─TableReader 39.97 root MppVersion: 2, data:ExchangeSender",
" │ └─ExchangeSender 39.97 mpp[tiflash] ExchangeType: PassThrough",
" │ └─Selection 39.97 mpp[tiflash] or(eq(test.tbl_16.col_100, 10672141), in(test.tbl_16.col_104, \"yfEG1t!*b\", \"C1*bqx_qyO\", \"vQ^yUpKHr&j#~\"))",
" │ └─TableFullScan 10000.00 mpp[tiflash] table:tbl_16, partition:p0 pushed down filter:empty, keep order:false, stats:pseudo",
" └─StreamAgg 31.98 root group by:Column#26, funcs:count(Column#23)->Column#11, funcs:sum(Column#24)->Column#12, funcs:firstrow(Column#25)->test.tbl_16.col_100",
" └─Projection 39.97 root test.tbl_16.col_100->Column#23, cast(test.tbl_16.col_100, decimal(8,0) UNSIGNED BINARY)->Column#24, test.tbl_16.col_100->Column#25, test.tbl_16.col_100->Column#26",
" └─StreamAgg 31.98 root group by:Column#22, funcs:count(Column#22)->Column#11, funcs:sum(Column#23)->Column#12, funcs:firstrow(Column#24)->test.tbl_16.col_100",
" └─Projection 39.97 root test.tbl_16.col_100->Column#22, cast(test.tbl_16.col_100, decimal(8,0) UNSIGNED BINARY)->Column#23, test.tbl_16.col_100->Column#24",
" └─Sort 39.97 root test.tbl_16.col_100",
" └─TableReader 39.97 root MppVersion: 2, data:ExchangeSender",
" └─ExchangeSender 39.97 mpp[tiflash] ExchangeType: PassThrough",
Expand Down
3 changes: 0 additions & 3 deletions pkg/planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,6 @@ func mergeContinuousSelections(p base.PhysicalPlan) {
func postOptimize(ctx context.Context, sctx base.PlanContext, plan base.PhysicalPlan) base.PhysicalPlan {
// some cases from update optimize will require avoiding projection elimination.
// see comments ahead of call of DoOptimize in function of buildUpdate().
if !sctx.GetSessionVars().InRestrictedSQL {
fmt.Println("postOptimize")
}
plan = eliminatePhysicalProjection(plan)
plan = InjectExtraProjection(plan)
mergeContinuousSelections(plan)
Expand Down
8 changes: 4 additions & 4 deletions pkg/planner/core/rule_inject_extra_projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ func InjectProjBelowAgg(aggPlan base.PhysicalPlan, aggFuncs []*aggregation.AggFu
}

for i, item := range groupByItems {
if _, isCnst := item.(*expression.Constant); isCnst {
it := item
if _, isCnst := it.(*expression.Constant); isCnst {
continue
}
idx := slices.IndexFunc(projExprs, func(a expression.Expression) bool {
return a.Equal(ectx, item)
return a.Equal(ectx, it)
})
if idx < 0 {
projExprs = append(projExprs, item)
projExprs = append(projExprs, it)
newArg := &expression.Column{
UniqueID: aggPlan.SCtx().GetSessionVars().AllocPlanColumnID(),
RetType: item.GetType(ectx),
Expand All @@ -192,7 +193,6 @@ func InjectProjBelowAgg(aggPlan base.PhysicalPlan, aggFuncs []*aggregation.AggFu
cursor++
} else {
groupByItems[i] = projSchemaCols[idx]
cursor++
}
}

Expand Down
Loading

0 comments on commit 8f7c860

Please sign in to comment.