Skip to content

Commit cceec97

Browse files
authored
planner: Set minimum cost to avoid parent multiplication cost discrepancies (pingcap#56387) (pingcap#56412)
ref pingcap#55126
1 parent 02029fd commit cceec97

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

pkg/planner/core/casetest/testdata/plan_normalized_suite_out.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@
426426
"Plan": [
427427
" TableReader root ",
428428
" └─ExchangeSender cop[tiflash] ",
429-
" └─Selection cop[tiflash] gt(test.t1.a, ?)",
430-
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), gt(test.t1.c, ?), keep order:false"
429+
" └─Selection cop[tiflash] gt(test.t1.c, ?)",
430+
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), gt(test.t1.b, ?), keep order:false"
431431
]
432432
},
433433
{
@@ -443,8 +443,8 @@
443443
"Plan": [
444444
" TableReader root ",
445445
" └─ExchangeSender cop[tiflash] ",
446-
" └─Selection cop[tiflash] gt(test.t1.a, ?), or(lt(test.t1.a, ?), lt(test.t1.b, ?))",
447-
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), keep order:false"
446+
" └─Selection cop[tiflash] gt(test.t1.b, ?), or(lt(test.t1.a, ?), lt(test.t1.b, ?))",
447+
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), keep order:false"
448448
]
449449
},
450450
{
@@ -461,8 +461,8 @@
461461
"Plan": [
462462
" TableReader root ",
463463
" └─ExchangeSender cop[tiflash] ",
464-
" └─Selection cop[tiflash] gt(test.t1.b, ?), gt(test.t1.c, ?), or(gt(test.t1.a, ?), lt(test.t1.b, ?))",
465-
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.a, ?), keep order:false"
464+
" └─Selection cop[tiflash] gt(test.t1.a, ?), gt(test.t1.c, ?), or(gt(test.t1.a, ?), lt(test.t1.b, ?))",
465+
" └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), keep order:false"
466466
]
467467
},
468468
{

pkg/planner/core/plan_cost_ver2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ func scanCostVer2(option *PlanCostOption, rows, rowSize float64, scanFactor cost
798798
}
799799
return newCostVer2(option, scanFactor,
800800
// rows * log(row-size) * scanFactor, log2 from experiments
801-
rows*math.Log2(rowSize)*scanFactor.Value,
801+
rows*max(math.Log2(rowSize), 0)*scanFactor.Value,
802802
func() string { return fmt.Sprintf("scan(%v*logrowsize(%v)*%v)", rows, rowSize, scanFactor) })
803803
}
804804

@@ -852,7 +852,7 @@ func orderCostVer2(option *PlanCostOption, rows, n float64, byItems []*util.ByIt
852852
rows*float64(numFuncs)*cpuFactor.Value,
853853
func() string { return fmt.Sprintf("exprCPU(%v*%v*%v)", rows, numFuncs, cpuFactor) })
854854
orderCost := newCostVer2(option, cpuFactor,
855-
rows*math.Log2(n)*cpuFactor.Value,
855+
max(rows*math.Log2(n), 0)*cpuFactor.Value,
856856
func() string { return fmt.Sprintf("orderCPU(%v*log(%v)*%v)", rows, n, cpuFactor) })
857857
return sumCostVer2(exprCost, orderCost)
858858
}

pkg/planner/core/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,11 @@ func (p *PhysicalIndexJoin) attach2Task(tasks ...task) task {
310310
// RowSize for cost model ver2 is simplified, always use this function to calculate row size.
311311
func getAvgRowSize(stats *property.StatsInfo, cols []*expression.Column) (size float64) {
312312
if stats.HistColl != nil {
313-
size = cardinality.GetAvgRowSizeListInDisk(stats.HistColl, cols)
313+
size = max(cardinality.GetAvgRowSizeListInDisk(stats.HistColl, cols), 0)
314314
} else {
315315
// Estimate using just the type info.
316316
for _, col := range cols {
317-
size += float64(chunk.EstimateTypeWidth(col.GetType()))
317+
size += max(float64(chunk.EstimateTypeWidth(col.GetType())), 0)
318318
}
319319
}
320320
return

0 commit comments

Comments
 (0)