Skip to content

Commit 633d885

Browse files
XuHuaiyuti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#55333
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 0ff1860 commit 633d885

11 files changed

+987
-4
lines changed

pkg/planner/cascades/implementation_rules.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,16 @@ func (*ImplProjection) OnImplement(expr *memo.GroupExpr, reqProp *property.Physi
152152
return nil, nil
153153
}
154154
proj := plannercore.PhysicalProjection{
155+
<<<<<<< HEAD
155156
Exprs: logicProj.Exprs,
156157
CalculateNoDelay: logicProj.CalculateNoDelay,
157158
AvoidColumnEvaluator: logicProj.AvoidColumnEvaluator,
158159
}.Init(logicProj.SCtx(), logicProp.Stats.ScaleByExpectCnt(reqProp.ExpectedCnt), logicProj.SelectBlockOffset(), childProp)
160+
=======
161+
Exprs: logicProj.Exprs,
162+
CalculateNoDelay: logicProj.CalculateNoDelay,
163+
}.Init(logicProj.SCtx(), logicProp.Stats.ScaleByExpectCnt(reqProp.ExpectedCnt), logicProj.QueryBlockOffset(), childProp)
164+
>>>>>>> 004b442fb9a (pkg/planner: set proj.AvoidColumnEvaluator in postOptimize (#55333))
159165
proj.SetSchema(logicProp.Schema)
160166
return []memo.Implementation{impl.NewProjectionImpl(proj)}, nil
161167
}

pkg/planner/core/exhaust_physical_plans.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,16 @@ func (*LogicalJoin) constructInnerProj(proj *LogicalProjection, child PhysicalPl
11701170
return child
11711171
}
11721172
physicalProj := PhysicalProjection{
1173+
<<<<<<< HEAD
11731174
Exprs: proj.Exprs,
11741175
CalculateNoDelay: proj.CalculateNoDelay,
11751176
AvoidColumnEvaluator: proj.AvoidColumnEvaluator,
11761177
}.Init(proj.SCtx(), proj.StatsInfo(), proj.SelectBlockOffset(), nil)
1178+
=======
1179+
Exprs: proj.Exprs,
1180+
CalculateNoDelay: proj.CalculateNoDelay,
1181+
}.Init(proj.SCtx(), proj.StatsInfo(), proj.QueryBlockOffset(), prop)
1182+
>>>>>>> 004b442fb9a (pkg/planner: set proj.AvoidColumnEvaluator in postOptimize (#55333))
11771183
physicalProj.SetChildren(child)
11781184
return physicalProj
11791185
}
@@ -2663,11 +2669,18 @@ func (p *LogicalProjection) exhaustPhysicalPlans(prop *property.PhysicalProperty
26632669
ret := make([]PhysicalPlan, 0, len(newProps))
26642670
for _, newProp := range newProps {
26652671
proj := PhysicalProjection{
2672+
<<<<<<< HEAD
26662673
Exprs: p.Exprs,
26672674
CalculateNoDelay: p.CalculateNoDelay,
26682675
AvoidColumnEvaluator: p.AvoidColumnEvaluator,
26692676
}.Init(p.SCtx(), p.StatsInfo().ScaleByExpectCnt(prop.ExpectedCnt), p.SelectBlockOffset(), newProp)
26702677
proj.SetSchema(p.schema)
2678+
=======
2679+
Exprs: p.Exprs,
2680+
CalculateNoDelay: p.CalculateNoDelay,
2681+
}.Init(ctx, p.StatsInfo().ScaleByExpectCnt(prop.ExpectedCnt), p.QueryBlockOffset(), newProp)
2682+
proj.SetSchema(p.Schema())
2683+
>>>>>>> 004b442fb9a (pkg/planner: set proj.AvoidColumnEvaluator in postOptimize (#55333))
26712684
ret = append(ret, proj)
26722685
}
26732686
return ret, true, nil

pkg/planner/core/logical_plan_builder.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ func (b *PlanBuilder) buildExpand(p LogicalPlan, gbyItems []expression.Expressio
260260
}
261261
proj.SetSchema(projSchema)
262262
proj.SetChildren(p)
263-
// since expand will ref original col and make some change, do the copy in executor rather than ref the same chunk.column.
264-
proj.AvoidColumnEvaluator = true
265263
proj.Proj4Expand = true
266264
newGbyItems := expression.RestoreGbyExpression(distinctGbyCols, gbyExprsRefPos)
267265

@@ -2085,8 +2083,13 @@ func (b *PlanBuilder) buildProjection4Union(_ context.Context, u *LogicalUnionAl
20852083
}
20862084
}
20872085
b.optFlag |= flagEliminateProjection
2086+
<<<<<<< HEAD
20882087
proj := LogicalProjection{Exprs: exprs, AvoidColumnEvaluator: true}.Init(b.ctx, b.getSelectOffset())
20892088
proj.SetSchema(u.schema.Clone())
2089+
=======
2090+
proj := logicalop.LogicalProjection{Exprs: exprs}.Init(b.ctx, b.getSelectOffset())
2091+
proj.SetSchema(u.Schema().Clone())
2092+
>>>>>>> 004b442fb9a (pkg/planner: set proj.AvoidColumnEvaluator in postOptimize (#55333))
20902093
// reset the schema type to make the "not null" flag right.
20912094
for i, expr := range exprs {
20922095
proj.schema.Columns[i].RetType = expr.GetType()
@@ -8027,7 +8030,11 @@ func (b *PlanBuilder) buildProjection4CTEUnion(_ context.Context, seed LogicalPl
80278030
}
80288031
}
80298032
b.optFlag |= flagEliminateProjection
8033+
<<<<<<< HEAD
80308034
proj := LogicalProjection{Exprs: exprs, AvoidColumnEvaluator: true}.Init(b.ctx, b.getSelectOffset())
8035+
=======
8036+
proj := logicalop.LogicalProjection{Exprs: exprs}.Init(b.ctx, b.getSelectOffset())
8037+
>>>>>>> 004b442fb9a (pkg/planner: set proj.AvoidColumnEvaluator in postOptimize (#55333))
80318038
proj.SetSchema(resSchema)
80328039
proj.SetChildren(recur)
80338040
return proj, nil

pkg/planner/core/logical_union_all.go

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package core
16+
17+
import (
18+
"github.com/pingcap/tidb/pkg/expression"
19+
"github.com/pingcap/tidb/pkg/planner/core/base"
20+
"github.com/pingcap/tidb/pkg/planner/core/operator/logicalop"
21+
"github.com/pingcap/tidb/pkg/planner/property"
22+
"github.com/pingcap/tidb/pkg/planner/util"
23+
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
24+
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace/logicaltrace"
25+
"github.com/pingcap/tidb/pkg/planner/util/utilfuncp"
26+
"github.com/pingcap/tidb/pkg/util/plancodec"
27+
)
28+
29+
// LogicalUnionAll represents LogicalUnionAll plan.
30+
type LogicalUnionAll struct {
31+
logicalop.LogicalSchemaProducer
32+
}
33+
34+
// Init initializes LogicalUnionAll.
35+
func (p LogicalUnionAll) Init(ctx base.PlanContext, offset int) *LogicalUnionAll {
36+
p.BaseLogicalPlan = logicalop.NewBaseLogicalPlan(ctx, plancodec.TypeUnion, &p, offset)
37+
return &p
38+
}
39+
40+
// *************************** start implementation of logicalPlan interface ***************************
41+
42+
// HashCode inherits BaseLogicalPlan.LogicalPlan.<0th> implementation.
43+
44+
// PredicatePushDown implements base.LogicalPlan.<1st> interface.
45+
func (p *LogicalUnionAll) PredicatePushDown(predicates []expression.Expression, opt *optimizetrace.LogicalOptimizeOp) (ret []expression.Expression, retPlan base.LogicalPlan) {
46+
for i, proj := range p.Children() {
47+
newExprs := make([]expression.Expression, 0, len(predicates))
48+
newExprs = append(newExprs, predicates...)
49+
retCond, newChild := proj.PredicatePushDown(newExprs, opt)
50+
utilfuncp.AddSelection(p, newChild, retCond, i, opt)
51+
}
52+
return nil, p
53+
}
54+
55+
// PruneColumns implements base.LogicalPlan.<2nd> interface.
56+
func (p *LogicalUnionAll) PruneColumns(parentUsedCols []*expression.Column, opt *optimizetrace.LogicalOptimizeOp) (base.LogicalPlan, error) {
57+
eCtx := p.SCtx().GetExprCtx().GetEvalCtx()
58+
used := expression.GetUsedList(eCtx, parentUsedCols, p.Schema())
59+
hasBeenUsed := false
60+
for i := range used {
61+
hasBeenUsed = hasBeenUsed || used[i]
62+
if hasBeenUsed {
63+
break
64+
}
65+
}
66+
if !hasBeenUsed {
67+
parentUsedCols = make([]*expression.Column, len(p.Schema().Columns))
68+
copy(parentUsedCols, p.Schema().Columns)
69+
for i := range used {
70+
used[i] = true
71+
}
72+
}
73+
var err error
74+
for i, child := range p.Children() {
75+
p.Children()[i], err = child.PruneColumns(parentUsedCols, opt)
76+
if err != nil {
77+
return nil, err
78+
}
79+
}
80+
81+
prunedColumns := make([]*expression.Column, 0)
82+
for i := len(used) - 1; i >= 0; i-- {
83+
if !used[i] {
84+
prunedColumns = append(prunedColumns, p.Schema().Columns[i])
85+
p.Schema().Columns = append(p.Schema().Columns[:i], p.Schema().Columns[i+1:]...)
86+
}
87+
}
88+
logicaltrace.AppendColumnPruneTraceStep(p, prunedColumns, opt)
89+
if hasBeenUsed {
90+
// It's possible that the child operator adds extra columns to the schema.
91+
// Currently, (*LogicalAggregation).PruneColumns() might do this.
92+
// But we don't need such columns, so we add an extra Projection to prune this column when this happened.
93+
for i, child := range p.Children() {
94+
if p.Schema().Len() < child.Schema().Len() {
95+
schema := p.Schema().Clone()
96+
exprs := make([]expression.Expression, len(p.Schema().Columns))
97+
for j, col := range schema.Columns {
98+
exprs[j] = col
99+
}
100+
proj := logicalop.LogicalProjection{Exprs: exprs}.Init(p.SCtx(), p.QueryBlockOffset())
101+
proj.SetSchema(schema)
102+
103+
proj.SetChildren(child)
104+
p.Children()[i] = proj
105+
}
106+
}
107+
}
108+
return p, nil
109+
}
110+
111+
// FindBestTask inherits BaseLogicalPlan.LogicalPlan.<3rd> implementation.
112+
113+
// BuildKeyInfo inherits BaseLogicalPlan.LogicalPlan.<4th> implementation.
114+
115+
// PushDownTopN implements the base.LogicalPlan.<5th> interface.
116+
func (p *LogicalUnionAll) PushDownTopN(topNLogicalPlan base.LogicalPlan, opt *optimizetrace.LogicalOptimizeOp) base.LogicalPlan {
117+
var topN *logicalop.LogicalTopN
118+
if topNLogicalPlan != nil {
119+
topN = topNLogicalPlan.(*logicalop.LogicalTopN)
120+
}
121+
for i, child := range p.Children() {
122+
var newTopN *logicalop.LogicalTopN
123+
if topN != nil {
124+
newTopN = logicalop.LogicalTopN{Count: topN.Count + topN.Offset, PreferLimitToCop: topN.PreferLimitToCop}.Init(p.SCtx(), topN.QueryBlockOffset())
125+
for _, by := range topN.ByItems {
126+
newTopN.ByItems = append(newTopN.ByItems, &util.ByItems{Expr: by.Expr, Desc: by.Desc})
127+
}
128+
// newTopN to push down Union's child
129+
appendNewTopNTraceStep(topN, p, opt)
130+
}
131+
p.Children()[i] = child.PushDownTopN(newTopN, opt)
132+
}
133+
if topN != nil {
134+
return topN.AttachChild(p, opt)
135+
}
136+
return p
137+
}
138+
139+
// DeriveTopN inherits BaseLogicalPlan.LogicalPlan.<6th> implementation.
140+
141+
// PredicateSimplification inherits BaseLogicalPlan.LogicalPlan.<7th> implementation.
142+
143+
// ConstantPropagation inherits BaseLogicalPlan.LogicalPlan.<8th> implementation.
144+
145+
// PullUpConstantPredicates inherits BaseLogicalPlan.LogicalPlan.<9th> implementation.
146+
147+
// RecursiveDeriveStats inherits BaseLogicalPlan.LogicalPlan.<10th> implementation.
148+
149+
// DeriveStats implement base.LogicalPlan.<11th> interface.
150+
func (p *LogicalUnionAll) DeriveStats(childStats []*property.StatsInfo, selfSchema *expression.Schema, _ []*expression.Schema, _ [][]*expression.Column) (*property.StatsInfo, error) {
151+
if p.StatsInfo() != nil {
152+
return p.StatsInfo(), nil
153+
}
154+
p.SetStats(&property.StatsInfo{
155+
ColNDVs: make(map[int64]float64, selfSchema.Len()),
156+
})
157+
for _, childProfile := range childStats {
158+
p.StatsInfo().RowCount += childProfile.RowCount
159+
for _, col := range selfSchema.Columns {
160+
p.StatsInfo().ColNDVs[col.UniqueID] += childProfile.ColNDVs[col.UniqueID]
161+
}
162+
}
163+
return p.StatsInfo(), nil
164+
}
165+
166+
// ExtractColGroups inherits BaseLogicalPlan.LogicalPlan.<12th> implementation.
167+
168+
// PreparePossibleProperties implements base.LogicalPlan.<13th> interface.
169+
170+
// ExhaustPhysicalPlans implements base.LogicalPlan.<14th> interface.
171+
func (p *LogicalUnionAll) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]base.PhysicalPlan, bool, error) {
172+
return exhaustUnionAllPhysicalPlans(p, prop)
173+
}
174+
175+
// ExtractCorrelatedCols inherits BaseLogicalPlan.LogicalPlan.<15th> implementation.
176+
177+
// MaxOneRow inherits BaseLogicalPlan.LogicalPlan.<16th> implementation.
178+
179+
// Children inherits BaseLogicalPlan.LogicalPlan.<17th> implementation.
180+
181+
// SetChildren inherits BaseLogicalPlan.LogicalPlan.<18th> implementation.
182+
183+
// SetChild inherits BaseLogicalPlan.LogicalPlan.<19th> implementation.
184+
185+
// RollBackTaskMap inherits BaseLogicalPlan.LogicalPlan.<20th> implementation.
186+
187+
// CanPushToCop inherits BaseLogicalPlan.LogicalPlan.<21st> implementation.
188+
189+
// ExtractFD inherits BaseLogicalPlan.LogicalPlan.<22nd> implementation.
190+
191+
// GetBaseLogicalPlan inherits BaseLogicalPlan.LogicalPlan.<23rd> implementation.
192+
193+
// ConvertOuterToInnerJoin inherits BaseLogicalPlan.LogicalPlan.<24th> implementation.
194+
195+
// *************************** end implementation of logicalPlan interface ***************************

0 commit comments

Comments
 (0)