Skip to content

Commit e3478a5

Browse files
authored
planner: fix get wrong cost with cost tracer (#61196)
close #61155
1 parent a1b06fb commit e3478a5

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

pkg/planner/core/casetest/tpch/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_test(
1616
"//pkg/testkit/testdata",
1717
"//pkg/testkit/testmain",
1818
"//pkg/testkit/testsetup",
19+
"@com_github_stretchr_testify//require",
1920
"@org_uber_go_goleak//:goleak",
2021
],
2122
)

pkg/planner/core/casetest/tpch/tpch_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/pingcap/tidb/pkg/testkit"
2121
"github.com/pingcap/tidb/pkg/testkit/testdata"
22+
"github.com/stretchr/testify/require"
2223
)
2324

2425
func TestQ1(t *testing.T) {
@@ -186,7 +187,8 @@ CREATE TABLE lineitem (
186187
testkit.LoadTableStats("orders_stats.json", dom)
187188
testkit.SetTiFlashReplica(t, dom, "olap", "orders")
188189
testkit.SetTiFlashReplica(t, dom, "olap", "lineitem")
189-
q4 := `explain format='brief' select
190+
briefFormat := `explain format='brief' `
191+
q4 := `select
190192
o_orderpriority,
191193
count(*) as order_count
192194
from
@@ -203,7 +205,7 @@ where
203205
and l_commitdate < l_receiptdate )
204206
group by o_orderpriority
205207
order by o_orderpriority`
206-
tk.MustQuery(q4).Check(testkit.Rows(
208+
tk.MustQuery(briefFormat + q4).Check(testkit.Rows(
207209
"Sort 1.00 root olap.orders.o_orderpriority",
208210
"└─Projection 1.00 root olap.orders.o_orderpriority, Column#26",
209211
" └─HashAgg 1.00 root group by:olap.orders.o_orderpriority, funcs:count(1)->Column#26, funcs:firstrow(olap.orders.o_orderpriority)->olap.orders.o_orderpriority",
@@ -215,9 +217,10 @@ order by o_orderpriority`
215217
" └─Selection 45161741.07 cop[tikv] lt(olap.lineitem.l_commitdate, olap.lineitem.l_receiptdate)",
216218
" └─TableRangeScan 56452176.33 cop[tikv] table:lineitem range: decided by [eq(olap.lineitem.l_orderkey, olap.orders.o_orderkey)], keep order:false",
217219
))
220+
checkCost(t, tk, q4)
218221
// https://github.com/pingcap/tidb/issues/60991
219222
tk.MustExec(`set @@session.tidb_enforce_mpp=1;`)
220-
tk.MustQuery(q4).Check(testkit.Rows("Sort 1.00 root olap.orders.o_orderpriority",
223+
tk.MustQuery(briefFormat + q4).Check(testkit.Rows("Sort 1.00 root olap.orders.o_orderpriority",
221224
"└─TableReader 1.00 root MppVersion: 3, data:ExchangeSender",
222225
" └─ExchangeSender 1.00 mpp[tiflash] ExchangeType: PassThrough",
223226
" └─Projection 1.00 mpp[tiflash] olap.orders.o_orderpriority, Column#26",
@@ -236,6 +239,21 @@ order by o_orderpriority`
236239
" └─Projection 4799991767.20 mpp[tiflash] olap.lineitem.l_orderkey",
237240
" └─Selection 4799991767.20 mpp[tiflash] lt(olap.lineitem.l_commitdate, olap.lineitem.l_receiptdate)",
238241
" └─TableFullScan 5999989709.00 mpp[tiflash] table:lineitem pushed down filter:empty, keep order:false"))
242+
checkCost(t, tk, q4)
243+
}
244+
245+
// check the cost trace's cost and verbose's cost. they should be the same.
246+
// it is from https://github.com/pingcap/tidb/issues/61155
247+
func checkCost(t *testing.T, tk *testkit.TestKit, q4 string) {
248+
costTraceFormat := `explain format='cost_trace' `
249+
verboseFormat := `explain format='verbose' `
250+
costTraceRows := tk.MustQuery(costTraceFormat + q4)
251+
verboseRows := tk.MustQuery(verboseFormat + q4)
252+
require.Equal(t, len(costTraceRows.Rows()), len(verboseRows.Rows()))
253+
for i := 0; i < len(costTraceRows.Rows()); i++ {
254+
// check id / estRows / estCost. they should be the same one
255+
require.Equal(t, costTraceRows.Rows()[i][:3], verboseRows.Rows()[i][:3])
256+
}
239257
}
240258

241259
func TestQ9(t *testing.T) {

pkg/planner/core/common_plans.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,16 +1022,6 @@ func (e *Explain) RenderResult() error {
10221022
}
10231023
}
10241024

1025-
if strings.ToLower(e.Format) == types.ExplainFormatCostTrace {
1026-
if pp, ok := e.TargetPlan.(base.PhysicalPlan); ok {
1027-
// trigger getPlanCost again with CostFlagTrace to record all cost formulas
1028-
if _, err := getPlanCost(pp, property.RootTaskType,
1029-
optimizetrace.NewDefaultPlanCostOption().WithCostFlag(costusage.CostFlagRecalculate|costusage.CostFlagTrace)); err != nil {
1030-
return err
1031-
}
1032-
}
1033-
}
1034-
10351025
switch strings.ToLower(e.Format) {
10361026
case types.ExplainFormatROW, types.ExplainFormatBrief, types.ExplainFormatVerbose, types.ExplainFormatTrueCardCost, types.ExplainFormatCostTrace, types.ExplainFormatPlanCache:
10371027
if e.Rows == nil || e.Analyze {

pkg/planner/core/planbuilder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5549,6 +5549,13 @@ func (b *PlanBuilder) buildExplain(ctx context.Context, explain *ast.ExplainStmt
55495549

55505550
var targetPlan base.Plan
55515551
if explain.Stmt != nil && !explain.Explore {
5552+
if strings.EqualFold(explain.Format, types.ExplainFormatCostTrace) {
5553+
origin := sctx.GetSessionVars().StmtCtx.EnableOptimizeTrace
5554+
sctx.GetSessionVars().StmtCtx.EnableOptimizeTrace = true
5555+
defer func() {
5556+
sctx.GetSessionVars().StmtCtx.EnableOptimizeTrace = origin
5557+
}()
5558+
}
55525559
nodeW := resolve.NewNodeWWithCtx(explain.Stmt, b.resolveCtx)
55535560
targetPlan, _, err = OptimizeAstNode(ctx, sctx, nodeW, b.is)
55545561
if err != nil {

0 commit comments

Comments
 (0)