From cceec974ddc4fd3d84963a312e5d74eab088ed80 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 30 Sep 2024 15:30:15 +0800 Subject: [PATCH] planner: Set minimum cost to avoid parent multiplication cost discrepancies (#56387) (#56412) ref pingcap/tidb#55126 --- .../casetest/testdata/plan_normalized_suite_out.json | 12 ++++++------ pkg/planner/core/plan_cost_ver2.go | 4 ++-- pkg/planner/core/task.go | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/planner/core/casetest/testdata/plan_normalized_suite_out.json b/pkg/planner/core/casetest/testdata/plan_normalized_suite_out.json index 0f5a427ec57d6..d9ad1168af8c3 100644 --- a/pkg/planner/core/casetest/testdata/plan_normalized_suite_out.json +++ b/pkg/planner/core/casetest/testdata/plan_normalized_suite_out.json @@ -426,8 +426,8 @@ "Plan": [ " TableReader root ", " └─ExchangeSender cop[tiflash] ", - " └─Selection cop[tiflash] gt(test.t1.a, ?)", - " └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), 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" ] }, { @@ -443,8 +443,8 @@ "Plan": [ " TableReader root ", " └─ExchangeSender cop[tiflash] ", - " └─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" + " └─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" ] }, { @@ -461,8 +461,8 @@ "Plan": [ " TableReader root ", " └─ExchangeSender cop[tiflash] ", - " └─Selection cop[tiflash] gt(test.t1.b, ?), gt(test.t1.c, ?), or(gt(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, ?), gt(test.t1.c, ?), or(gt(test.t1.a, ?), lt(test.t1.b, ?))", + " └─TableFullScan cop[tiflash] table:t1, range:[?,?], pushed down filter:gt(test.t1.b, ?), keep order:false" ] }, { diff --git a/pkg/planner/core/plan_cost_ver2.go b/pkg/planner/core/plan_cost_ver2.go index fd44e4aed344d..a443427e0fae9 100644 --- a/pkg/planner/core/plan_cost_ver2.go +++ b/pkg/planner/core/plan_cost_ver2.go @@ -798,7 +798,7 @@ func scanCostVer2(option *PlanCostOption, rows, rowSize float64, scanFactor cost } return 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) }) } @@ -852,7 +852,7 @@ func orderCostVer2(option *PlanCostOption, rows, n float64, byItems []*util.ByIt rows*float64(numFuncs)*cpuFactor.Value, func() string { return fmt.Sprintf("exprCPU(%v*%v*%v)", rows, numFuncs, cpuFactor) }) orderCost := 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 sumCostVer2(exprCost, orderCost) } diff --git a/pkg/planner/core/task.go b/pkg/planner/core/task.go index 3a109a1294a0f..ae47862b67e02 100644 --- a/pkg/planner/core/task.go +++ b/pkg/planner/core/task.go @@ -310,11 +310,11 @@ func (p *PhysicalIndexJoin) attach2Task(tasks ...task) 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.GetAvgRowSizeListInDisk(stats.HistColl, cols) + size = max(cardinality.GetAvgRowSizeListInDisk(stats.HistColl, cols), 0) } else { // Estimate using just the type info. for _, col := range cols { - size += float64(chunk.EstimateTypeWidth(col.GetType())) + size += max(float64(chunk.EstimateTypeWidth(col.GetType())), 0) } } return