-
Notifications
You must be signed in to change notification settings - Fork 6k
planner: fix get wrong cost with cost tracer #61196
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
Conversation
Signed-off-by: Weizhen Wang <[email protected]>
Signed-off-by: Weizhen Wang <[email protected]>
Signed-off-by: Weizhen Wang <[email protected]>
Co-authored-by: Copilot <[email protected]>
d64104a
to
a64b07d
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #61196 +/- ##
================================================
+ Coverage 73.1823% 73.4846% +0.3022%
================================================
Files 1724 1725 +1
Lines 477675 479888 +2213
================================================
+ Hits 349574 352644 +3070
+ Misses 106688 105835 -853
+ Partials 21413 21409 -4
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an issue where EXPLAIN FORMAT='cost_trace'
would calculate plan cost twice (with a polluted row count on the second pass). It enables the optimize-tracing flag only during the initial build and removes the redundant recalculation, then adds a test helper to verify cost consistency.
- Enable
StmtCtx.EnableOptimizeTrace
only whenFormat=cost_trace
inbuildExplain
- Remove the second
getPlanCost
invocation inRenderResult
- Add
checkCost
inTestQ1
to comparecost_trace
vsverbose
outputs and update test dependencies
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
File | Description |
---|---|
pkg/planner/core/planbuilder.go | Wrap EnableOptimizeTrace in a conditional and defer restore for cost_trace |
pkg/planner/core/common_plans.go | Remove the extra getPlanCost call for cost_trace |
pkg/planner/core/casetest/tpch/tpch_test.go | Introduce checkCost helper, update TestQ1 to assert cost consistency |
pkg/planner/core/casetest/tpch/BUILD.bazel | Add @com_github_stretchr_testify//require to test dependencies |
Comments suppressed due to low confidence (3)
pkg/planner/core/planbuilder.go:5553
- [nitpick] The variable name
origin
is ambiguous; consider renaming it tooldEnableOptimizeTrace
or similar to clarify its purpose.
origin := sctx.GetSessionVars().StmtCtx.EnableOptimizeTrace
pkg/planner/core/common_plans.go:1024
- After removing the redundant cost-recalculation block, review and clean up any now-unused imports (e.g.,
optimizetrace
,costusage
) to keep the file tidy.
switch strings.ToLower(e.Format) {
pkg/planner/core/casetest/tpch/tpch_test.go:223
- Consider adding a
checkCost(t, tk, q4)
call after this MPP-mode query to ensure cost_trace and verbose outputs remain consistent whentidb_enforce_mpp
is enabled.
tk.MustQuery(briefFormat + q4).Check(testkit.Rows("Sort 1.00 root olap.orders.o_orderpriority",
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: AilinKid, qw4990 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/cherrypick release-8.5 |
Signed-off-by: ti-chi-bot <[email protected]>
@hawkingrei: new pull request created to branch In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Signed-off-by: ti-chi-bot <[email protected]>
@hawkingrei: new pull request created to branch In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Signed-off-by: ti-chi-bot <[email protected]>
@hawkingrei: new pull request created to branch In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
/cherrypick release-8.1 |
@hawkingrei: new pull request could not be created: failed to create pull request against pingcap/tidb#release-7.5 from head ti-chi-bot:cherry-pick-61196-to-release-7.5: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-7.5."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"} In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Signed-off-by: ti-chi-bot <[email protected]>
@hawkingrei: new pull request could not be created: failed to create pull request against pingcap/tidb#release-8.1 from head ti-chi-bot:cherry-pick-61196-to-release-8.1: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-8.1."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"} In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Signed-off-by: ti-chi-bot <[email protected]>
/cherrypick release-7.5 |
In response to a cherrypick label: new pull request could not be created: failed to create pull request against pingcap/tidb#release-8.1 from head ti-chi-bot:cherry-pick-61196-to-release-8.1: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-8.1."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"} |
Signed-off-by: ti-chi-bot <[email protected]>
In response to a cherrypick label: new pull request could not be created: failed to create pull request against pingcap/tidb#release-7.5 from head ti-chi-bot:cherry-pick-61196-to-release-7.5: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-7.5."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"} |
Signed-off-by: ti-chi-bot <[email protected]>
@hawkingrei: new pull request could not be created: failed to create pull request against pingcap/tidb#release-7.5 from head ti-chi-bot:cherry-pick-61196-to-release-7.5: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for ti-chi-bot:cherry-pick-61196-to-release-7.5."}],"documentation_url":"https://docs.github.com/rest/pulls/pulls#create-a-pull-request","status":"422"} In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Signed-off-by: ti-chi-bot <[email protected]>
Signed-off-by: ti-chi-bot <[email protected]>
Signed-off-by: ti-chi-bot <[email protected]>
Signed-off-by: ti-chi-bot <[email protected]>
What problem does this PR solve?
Issue Number: close #61155
Problem Summary:
When we use the cost trace's explain, it will calculate the cost twice. Unfortunately, when calculating secondly, the row count has been polluted by the first.
the first calculation:
1500000000
will become the5.645217633333338e+07
, becausepostOptimize
‘stryEnableLateMaterialization
can change therow count
in the first plan generate.tidb/pkg/planner/core/tiflash_selection_late_materialization.go
Lines 283 to 290 in 7419112
the second calculation:
So two formats have different costs.
What changed and how does it work?
Generate the cost trace in the first calculation. Remove the second calculation.
Check List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.