Skip to content

Commit

Permalink
planner: refacor code to split base package (#45529)
Browse files Browse the repository at this point in the history
ref #44940
  • Loading branch information
hawkingrei authored Jul 25, 2023
1 parent 552d319 commit b74c572
Show file tree
Hide file tree
Showing 55 changed files with 1,308 additions and 1,235 deletions.
2 changes: 1 addition & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5051,7 +5051,7 @@ func (b *executorBuilder) buildShuffle(v *plannercore.PhysicalShuffle) *ShuffleE
for _, dataSource := range v.DataSources {
stub := plannercore.PhysicalShuffleReceiverStub{
DataSource: dataSource,
}.Init(b.ctx, dataSource.Stats(), dataSource.SelectBlockOffset(), nil)
}.Init(b.ctx, dataSource.StatsInfo(), dataSource.SelectBlockOffset(), nil)
stub.SetSchema(dataSource.Schema())
stubs = append(stubs, stub)
}
Expand Down
2 changes: 1 addition & 1 deletion planner/cascades/enforcer_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (*OrderEnforcer) OnEnforce(reqProp *property.PhysicalProperty, child memo.I
childPlan := child.GetPlan()
sort := plannercore.PhysicalSort{
ByItems: make([]*util.ByItems, 0, len(reqProp.SortItems)),
}.Init(childPlan.SCtx(), childPlan.Stats(), childPlan.SelectBlockOffset(), &property.PhysicalProperty{ExpectedCnt: math.MaxFloat64})
}.Init(childPlan.SCtx(), childPlan.StatsInfo(), childPlan.SelectBlockOffset(), &property.PhysicalProperty{ExpectedCnt: math.MaxFloat64})
for _, item := range reqProp.SortItems {
item := &util.ByItems{
Expr: item.Col,
Expand Down
1 change: 1 addition & 0 deletions planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ go_library(
"//parser/terror",
"//parser/types",
"//planner/core/internal",
"//planner/core/internal/base",
"//planner/core/metrics",
"//planner/funcdep",
"//planner/property",
Expand Down
16 changes: 8 additions & 8 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ func (e *Explain) RenderResult() error {
e.Rows = append(e.Rows, []string{hint.RestoreOptimizerHints(hints)})
case types.ExplainFormatBinary:
flat := FlattenPhysicalPlan(e.TargetPlan, false)
str := BinaryPlanStrFromFlatPlan(e.ctx, flat)
str := BinaryPlanStrFromFlatPlan(e.SCtx(), flat)
e.Rows = append(e.Rows, []string{str})
case types.ExplainFormatTiDBJSON:
flat := FlattenPhysicalPlan(e.TargetPlan, true)
Expand Down Expand Up @@ -1096,7 +1096,7 @@ func (e *Explain) prepareOperatorInfo(p Plan, taskType, id string) {
if strings.ToLower(e.Format) == types.ExplainFormatTrueCardCost || strings.ToLower(e.Format) == types.ExplainFormatCostTrace {
row = append(row, costFormula)
}
actRows, analyzeInfo, memoryInfo, diskInfo := getRuntimeInfoStr(e.ctx, p, e.RuntimeStatsColl)
actRows, analyzeInfo, memoryInfo, diskInfo := getRuntimeInfoStr(e.SCtx(), p, e.RuntimeStatsColl)
row = append(row, actRows, taskType, accessObject, analyzeInfo, operatorInfo, memoryInfo, diskInfo)
} else {
row = []string{id, estRows}
Expand Down Expand Up @@ -1128,7 +1128,7 @@ func (e *Explain) prepareOperatorInfoForJSONFormat(p Plan, taskType, id string,
}

if e.Analyze || e.RuntimeStatsColl != nil {
jsonRow.ActRows, jsonRow.ExecuteInfo, jsonRow.MemoryInfo, jsonRow.DiskInfo = getRuntimeInfoStr(e.ctx, p, e.RuntimeStatsColl)
jsonRow.ActRows, jsonRow.ExecuteInfo, jsonRow.MemoryInfo, jsonRow.DiskInfo = getRuntimeInfoStr(e.SCtx(), p, e.RuntimeStatsColl)
}
return jsonRow
}
Expand All @@ -1150,7 +1150,7 @@ func (e *Explain) getOperatorInfo(p Plan, id string) (estRows, estCost, costForm
costFormula = "N/A"
if isPhysicalPlan {
estRows = strconv.FormatFloat(pp.getEstRowCountForDisplay(), 'f', 2, 64)
if e.ctx != nil && e.ctx.GetSessionVars().CostModelVersion == modelVer2 {
if e.SCtx() != nil && e.SCtx().GetSessionVars().CostModelVersion == modelVer2 {
costVer2, _ := pp.getPlanCostVer2(property.RootTaskType, NewDefaultPlanCostOption())
estCost = strconv.FormatFloat(costVer2.cost, 'f', 2, 64)
if costVer2.trace != nil {
Expand All @@ -1160,16 +1160,16 @@ func (e *Explain) getOperatorInfo(p Plan, id string) (estRows, estCost, costForm
planCost, _ := getPlanCost(pp, property.RootTaskType, NewDefaultPlanCostOption())
estCost = strconv.FormatFloat(planCost, 'f', 2, 64)
}
} else if si := p.statsInfo(); si != nil {
} else if si := p.StatsInfo(); si != nil {
estRows = strconv.FormatFloat(si.RowCount, 'f', 2, 64)
}

if plan, ok := p.(dataAccesser); ok {
accessObject = plan.AccessObject().String()
operatorInfo = plan.OperatorInfo(false)
} else {
if pa, ok := p.(partitionAccesser); ok && e.ctx != nil {
accessObject = pa.accessObject(e.ctx).String()
if pa, ok := p.(partitionAccesser); ok && e.SCtx() != nil {
accessObject = pa.accessObject(e.SCtx()).String()
}
operatorInfo = p.ExplainInfo()
}
Expand Down Expand Up @@ -1263,7 +1263,7 @@ func binaryOpFromFlatOp(explainCtx sessionctx.Context, op *FlatOperator, out *ti
p := op.Origin.(PhysicalPlan)
out.Cost, _ = getPlanCost(p, property.RootTaskType, NewDefaultPlanCostOption())
out.EstRows = p.getEstRowCountForDisplay()
} else if statsInfo := op.Origin.statsInfo(); statsInfo != nil {
} else if statsInfo := op.Origin.StatsInfo(); statsInfo != nil {
out.EstRows = statsInfo.RowCount
}

Expand Down
14 changes: 7 additions & 7 deletions planner/core/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func EncodeFlatPlan(flat *FlatPhysicalPlan) string {
failpoint.Inject("mockPlanRowCount", func(val failpoint.Value) {
selectPlan, _ := flat.Main.GetSelectPlan()
for _, op := range selectPlan {
op.Origin.statsInfo().RowCount = float64(val.(int))
op.Origin.StatsInfo().RowCount = float64(val.(int))
}
})
pn := encoderPool.Get().(*planEncoder)
Expand Down Expand Up @@ -71,7 +71,7 @@ func EncodeFlatPlan(flat *FlatPhysicalPlan) string {
var estRows float64
if op.IsPhysicalPlan {
estRows = op.Origin.(PhysicalPlan).getEstRowCountForDisplay()
} else if statsInfo := p.statsInfo(); statsInfo != nil {
} else if statsInfo := p.StatsInfo(); statsInfo != nil {
estRows = statsInfo.RowCount
}
plancodec.EncodePlanNode(
Expand Down Expand Up @@ -103,7 +103,7 @@ func encodeFlatPlanTree(flatTree FlatPlanTree, offset int, buf *bytes.Buffer) {
var estRows float64
if op.IsPhysicalPlan {
estRows = op.Origin.(PhysicalPlan).getEstRowCountForDisplay()
} else if statsInfo := p.statsInfo(); statsInfo != nil {
} else if statsInfo := p.StatsInfo(); statsInfo != nil {
estRows = statsInfo.RowCount
}
plancodec.EncodePlanNode(
Expand Down Expand Up @@ -164,7 +164,7 @@ func EncodePlan(p Plan) string {
selectPlan := getSelectPlan(p)
if selectPlan != nil {
failpoint.Inject("mockPlanRowCount", func(val failpoint.Value) {
selectPlan.statsInfo().RowCount = float64(val.(int))
selectPlan.StatsInfo().RowCount = float64(val.(int))
})
}
return pn.encodePlanTree(p)
Expand Down Expand Up @@ -193,8 +193,8 @@ func (pn *planEncoder) encodeCTEPlan() {
taskTypeInfo := plancodec.EncodeTaskType(true, kv.TiKV)
actRows, analyzeInfo, memoryInfo, diskInfo := getRuntimeInfoStr(x.SCtx(), x, nil)
rowCount := 0.0
if statsInfo := x.statsInfo(); statsInfo != nil {
rowCount = x.statsInfo().RowCount
if statsInfo := x.StatsInfo(); statsInfo != nil {
rowCount = x.StatsInfo().RowCount
}
plancodec.EncodePlanNode(0, strconv.Itoa(x.CTE.IDForStorage), plancodec.TypeCTEDefinition, rowCount, taskTypeInfo, x.ExplainInfo(), actRows, analyzeInfo, memoryInfo, diskInfo, &pn.buf)
pn.encodePlan(x.SeedPlan, true, kv.TiKV, 1)
Expand All @@ -211,7 +211,7 @@ func (pn *planEncoder) encodePlan(p Plan, isRoot bool, store kv.StoreType, depth
rowCount := 0.0
if pp, ok := p.(PhysicalPlan); ok {
rowCount = pp.getEstRowCountForDisplay()
} else if statsInfo := p.statsInfo(); statsInfo != nil {
} else if statsInfo := p.StatsInfo(); statsInfo != nil {
rowCount = statsInfo.RowCount
}
plancodec.EncodePlanNode(depth, strconv.Itoa(p.ID()), p.TP(), rowCount, taskTypeInfo, p.ExplainInfo(), actRows, analyzeInfo, memoryInfo, diskInfo, &pn.buf)
Expand Down
Loading

0 comments on commit b74c572

Please sign in to comment.