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: fix wrong task types when calculating cost in some cases #43637

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 17 additions & 14 deletions planner/core/casetest/testdata/enforce_mpp_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@
{
"SQL": "explain format='verbose' select /*+ read_from_storage(tiflash[t]) */ count(*) from t where a=1",
"Plan": [
"StreamAgg_10 1.00 64007.91 root funcs:count(1)->Column#4",
"└─TableReader_24 10.00 63508.91 root data:Selection_23",
" └─Selection_23 10.00 952000.00 cop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_22 10000.00 928000.00 cop[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo"
"StreamAgg_20 1.00 63520.28 root funcs:count(Column#6)->Column#4",
"└─TableReader_21 1.00 63470.38 root data:StreamAgg_9",
" └─StreamAgg_9 1.00 952024.00 batchCop[tiflash] funcs:count(1)->Column#6",
" └─Selection_19 10.00 952000.00 batchCop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_18 10000.00 928000.00 batchCop[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo"
],
"Warn": null
},
Expand Down Expand Up @@ -91,10 +92,11 @@
{
"SQL": "explain format='verbose' select /*+ read_from_storage(tiflash[t]) */ count(*) from t where a=1",
"Plan": [
"StreamAgg_12 1.00 64007.91 root funcs:count(1)->Column#4",
"└─TableReader_31 10.00 63508.91 root data:Selection_30",
" └─Selection_30 10.00 952000.00 cop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_29 10000.00 928000.00 cop[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo"
"StreamAgg_27 1.00 63520.28 root funcs:count(Column#7)->Column#4",
"└─TableReader_28 1.00 63470.38 root data:StreamAgg_11",
" └─StreamAgg_11 1.00 952024.00 batchCop[tiflash] funcs:count(1)->Column#7",
" └─Selection_26 10.00 952000.00 batchCop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_25 10000.00 928000.00 batchCop[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo"
],
"Warn": null
},
Expand Down Expand Up @@ -126,10 +128,11 @@
{
"SQL": "explain format='verbose' select /*+ read_from_storage(tiflash[t]) */ count(*) from t where a=1",
"Plan": [
"StreamAgg_12 1.00 64007.91 root funcs:count(1)->Column#4",
"└─TableReader_31 10.00 63508.91 root data:Selection_30",
" └─Selection_30 10.00 952000.00 cop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_29 10000.00 928000.00 cop[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo"
"StreamAgg_27 1.00 63520.28 root funcs:count(Column#7)->Column#4",
"└─TableReader_28 1.00 63470.38 root data:StreamAgg_11",
" └─StreamAgg_11 1.00 952024.00 batchCop[tiflash] funcs:count(1)->Column#7",
" └─Selection_26 10.00 952000.00 batchCop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_25 10000.00 928000.00 batchCop[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo"
],
"Warn": null
},
Expand Down Expand Up @@ -165,8 +168,8 @@
"Plan": [
"StreamAgg_27 1.00 49.90 root funcs:count(Column#7)->Column#4",
"└─TableReader_28 1.00 0.00 root data:StreamAgg_11",
" └─StreamAgg_11 1.00 1427024.00 batchCop[tiflash] funcs:count(1)->Column#7",
" └─Selection_26 10.00 1427000.00 batchCop[tiflash] eq(test.t.a, 1)",
" └─StreamAgg_11 1.00 952024.00 batchCop[tiflash] funcs:count(1)->Column#7",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected

" └─Selection_26 10.00 952000.00 batchCop[tiflash] eq(test.t.a, 1)",
" └─TableFullScan_25 10000.00 928000.00 batchCop[tiflash] table:t pushed down filter:empty, keep order:false, stats:pseudo"
],
"Warn": null
Expand Down
11 changes: 11 additions & 0 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ func getTaskPlanCost(t task, op *physicalOptimizeOp) (float64, bool, error) {
taskType = property.RootTaskType
case *copTask: // no need to know whether the task is single-read or double-read, so both CopSingleReadTaskType and CopDoubleReadTaskType are OK
taskType = property.CopSingleReadTaskType

// TiFlash can run cop task as well, check whether this cop task will run on TiKV or TiFlash.
if t.(*copTask).tablePlan != nil {
leafNode := t.(*copTask).tablePlan
for len(leafNode.Children()) > 0 {
leafNode = leafNode.Children()[0]
}
if tblScan, isScan := leafNode.(*PhysicalTableScan); isScan && tblScan.StoreType == kv.TiFlash {
taskType = property.MppTaskType
}
}
case *mppTask:
taskType = property.MppTaskType
default:
Expand Down
3 changes: 3 additions & 0 deletions planner/core/plan_cost_ver1.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,9 @@ func getOperatorActRows(operator PhysicalPlan) float64 {
func getCardinality(operator PhysicalPlan, costFlag uint64) float64 {
if hasCostFlag(costFlag, CostFlagUseTrueCardinality) {
actualProbeCnt := operator.getActualProbeCnt(operator.SCtx().GetSessionVars().StmtCtx.RuntimeStatsColl)
if actualProbeCnt == 0 {
return 0
}
return getOperatorActRows(operator) / float64(actualProbeCnt)
}
rows := operator.StatsCount()
Expand Down
3 changes: 3 additions & 0 deletions planner/core/plan_cost_ver2.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func (p *PhysicalProjection) getPlanCostVer2(taskType property.TaskType, option
inputRows := getCardinality(p.children[0], option.CostFlag)
cpuFactor := getTaskCPUFactorVer2(p, taskType)
concurrency := float64(p.ctx.GetSessionVars().ProjectionConcurrency())
if concurrency == 0 {
concurrency = 1 // un-parallel execution
}

projCost := filterCostVer2(option, inputRows, p.Exprs, cpuFactor)

Expand Down