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: Set minimum cost to avoid parent multiplication cost discrepancies #56387

Merged
merged 13 commits into from
Sep 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -419,17 +419,17 @@
" TableReader root ",
" └─ExchangeSender cop[tiflash] ",
" └─Projection cop[tiflash] test.t1.a",
" └─Selection cop[tiflash] gt(test.t1.b, ?)",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), keep order:false"
" └─Selection cop[tiflash] gt(test.t1.a, ?)",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), keep order:false"
]
},
{
"SQL": "explain select * from t1 where a>1 and b>1 and c>1",
"Plan": [
" TableReader root ",
" └─ExchangeSender cop[tiflash] ",
" └─Selection cop[tiflash] gt(test.t1.b, ?)",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), gt(test.t1.c, ?), keep order:false"
" └─Selection cop[tiflash] gt(test.t1.c, ?)",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), gt(test.t1.b, ?), keep order:false"
]
},
{
Expand All @@ -445,17 +445,17 @@
"Plan": [
" TableReader root ",
" └─ExchangeSender cop[tiflash] ",
" └─Selection cop[tiflash] gt(test.t1.b, ?), or(lt(test.t1.a, ?), lt(test.t1.b, ?))",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), keep order:false"
" └─Selection cop[tiflash] gt(test.t1.a, ?), or(lt(test.t1.a, ?), lt(test.t1.b, ?))",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), keep order:false"
]
},
{
"SQL": "explain select * from t1 where (a<1 or b<2) and (a>3 and b>3) and c>1",
"Plan": [
" TableReader root ",
" └─ExchangeSender cop[tiflash] ",
" └─Selection cop[tiflash] gt(test.t1.a, ?), gt(test.t1.c, ?), or(lt(test.t1.a, ?), lt(test.t1.b, ?))",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), keep order:false"
" └─Selection cop[tiflash] gt(test.t1.b, ?), gt(test.t1.c, ?), or(lt(test.t1.a, ?), lt(test.t1.b, ?))",
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), keep order:false"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/plan_cost_ver2.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ func scanCostVer2(option *optimizetrace.PlanCostOption, rows, rowSize float64, s
}
return costusage.NewCostVer2(option, scanFactor,
// rows * log(row-size) * scanFactor, log2 from experiments
rows*math.Log2(rowSize)*scanFactor.Value,
rows*max(math.Log2(rowSize), 0)*scanFactor.Value,
func() string { return fmt.Sprintf("scan(%v*logrowsize(%v)*%v)", rows, rowSize, scanFactor) })
}

Expand Down Expand Up @@ -874,7 +874,7 @@ func orderCostVer2(option *optimizetrace.PlanCostOption, rows, n float64, byItem
rows*float64(numFuncs)*cpuFactor.Value,
func() string { return fmt.Sprintf("exprCPU(%v*%v*%v)", rows, numFuncs, cpuFactor) })
orderCost := costusage.NewCostVer2(option, cpuFactor,
rows*math.Log2(n)*cpuFactor.Value,
max(rows*math.Log2(n), 0)*cpuFactor.Value,
func() string { return fmt.Sprintf("orderCPU(%v*log(%v)*%v)", rows, n, cpuFactor) })
return costusage.SumCostVer2(exprCost, orderCost)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ func (p *PhysicalIndexJoin) Attach2Task(tasks ...base.Task) base.Task {
// RowSize for cost model ver2 is simplified, always use this function to calculate row size.
func getAvgRowSize(stats *property.StatsInfo, cols []*expression.Column) (size float64) {
if stats.HistColl != nil {
size = cardinality.GetAvgRowSizeDataInDiskByRows(stats.HistColl, cols)
size = max(cardinality.GetAvgRowSizeDataInDiskByRows(stats.HistColl, cols), 0)
} else {
// Estimate using just the type info.
for _, col := range cols {
size += float64(chunk.EstimateTypeWidth(col.GetStaticType()))
size += max(float64(chunk.EstimateTypeWidth(col.GetStaticType())), 0)
}
}
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/util/costusage/cost_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type CostVer2 struct {

// GetCost returns the cost value of the costVer2
func (c *CostVer2) GetCost() float64 {
return c.cost
return max(c.cost, 0)
}

// GetTrace returns the trace of current costVer2
Expand Down