Skip to content

planner: handle the selection between the join group#64535

Merged
ti-chi-bot[bot] merged 4 commits intopingcap:masterfrom
Reminiscent:handle-selection-in-join-order
Jan 22, 2026
Merged

planner: handle the selection between the join group#64535
ti-chi-bot[bot] merged 4 commits intopingcap:masterfrom
Reminiscent:handle-selection-in-join-order

Conversation

@Reminiscent
Copy link
Copy Markdown
Contributor

@Reminiscent Reminiscent commented Nov 18, 2025

What problem does this PR solve?

Issue Number: close #59972

Problem Summary:
Handle the selection between the join group. Let more join group to participate the join order phase.

What changed and how does it work?

The selection plan node prevent the function extractJoinGroup to get more join groups.
So when we detect the selection node in the extractJoinGroup, we'll store them.
And we'll add them in the correct position(as low as possible) when we rebuild the join node.
Add the variables to keep the default behavior.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-tests-checked sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed do-not-merge/needs-tests-checked labels Nov 18, 2025
@tiprow
Copy link
Copy Markdown

tiprow bot commented Nov 18, 2025

Hi @Reminiscent. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Details

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 kubernetes-sigs/prow repository.

@Reminiscent
Copy link
Copy Markdown
Contributor Author

@winoros PTAL

@codecov
Copy link
Copy Markdown

codecov bot commented Nov 18, 2025

Codecov Report

❌ Patch coverage is 95.55556% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.6002%. Comparing base (93c65e2) to head (851b039).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #64535        +/-   ##
================================================
- Coverage   77.7973%   77.6002%   -0.1972%     
================================================
  Files          1993       1916        -77     
  Lines        544116     531805     -12311     
================================================
- Hits         423308     412682     -10626     
+ Misses       119149     119117        -32     
+ Partials       1659          6      -1653     
Flag Coverage Δ
integration 41.5304% <93.3333%> (-6.6588%) ⬇️
unit 76.7156% <83.3333%> (+0.2972%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 56.7974% <ø> (ø)
parser ∅ <ø> (∅)
br 48.7617% <ø> (-12.2825%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Nov 19, 2025
group []base.LogicalPlan
joinOrderHintInfo []*h.PlanHints
eqEdges []*expression.ScalarFunction
otherConds []expression.Expression
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be here. There's no need to create new slice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update. PTAL

@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 8, 2026
@Reminiscent Reminiscent requested a review from winoros January 8, 2026 08:46
@Reminiscent
Copy link
Copy Markdown
Contributor Author

/retest

@tiprow
Copy link
Copy Markdown

tiprow bot commented Jan 8, 2026

@Reminiscent: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

Details

In response to this:

/retest

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 kubernetes-sigs/prow repository.

@Reminiscent Reminiscent force-pushed the handle-selection-in-join-order branch from 32a4b05 to ca633a9 Compare January 9, 2026 01:43
// to otherConds and continue extracting the join group from the child.
if selection, isSelection := p.(*logicalop.LogicalSelection); isSelection && p.SCtx().GetSessionVars().TiDBOptJoinReorderSel {
child := selection.Children()[0]
if _, isChildJoin := child.(*logicalop.LogicalJoin); isChildJoin {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also handle the volatile function here. Not sure it has been handled somewhere else.

			if slices.ContainsFunc(selection.Conditions, expression.IsMutableEffectsExpr) {
				return &joinGroupResult{
					group:              []base.LogicalPlan{p},
					joinOrderHintInfo:  joinOrderHintInfo,
					basicJoinGroupInfo: &basicJoinGroupInfo{},
				}
			}

Copy link
Copy Markdown
Contributor

@fixdb fixdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also test cases where the SELECTION has correlated subuqery?

@Reminiscent
Copy link
Copy Markdown
Contributor Author

@fixdb updated. PTAL

@Reminiscent Reminiscent requested a review from fixdb January 16, 2026 07:30
@fixdb
Copy link
Copy Markdown
Contributor

fixdb commented Jan 16, 2026

Did you push your latest commit?

@Reminiscent Reminiscent force-pushed the handle-selection-in-join-order branch from ca633a9 to 72740c9 Compare January 16, 2026 08:19
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 16, 2026
@Reminiscent
Copy link
Copy Markdown
Contributor Author

Did you push your latest commit?

Sorry. Rebased and pushed. PTAL @fixdb


// Check if the current plan is a Selection. If its child is a join, add the selection conditions
// to otherConds and continue extracting the join group from the child.
if selection, isSelection := p.(*logicalop.LogicalSelection); isSelection && p.SCtx().GetSessionVars().TiDBOptJoinReorderSel && allowReorderThroughSelection(selection.Conditions) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not !slices.ContainsFunc(selection.Conditions, expression.IsMutableEffectsExpr) but define a allowReorderThroughSelection?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I was thinking of making it a function so that it would be easier to add more conditions in the future (if needed) and keep it more flexible. However, for the current situation, you’re right that the approach you suggested is simpler and clearer. I’ve already updated it accordingly. PTAL @fixdb

@fixdb
Copy link
Copy Markdown
Contributor

fixdb commented Jan 18, 2026

/retest

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jan 21, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Jan 21, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-01-20 10:17:38.190114208 +0000 UTC m=+489085.804071074: ☑️ agreed by AilinKid.
  • 2026-01-21 00:25:23.09100542 +0000 UTC m=+539950.704962276: ☑️ agreed by fixdb.

@Reminiscent Reminiscent force-pushed the handle-selection-in-join-order branch from 584c14f to 851b039 Compare January 21, 2026 08:20
@Reminiscent
Copy link
Copy Markdown
Contributor Author

@yudongusa PTAL

@Reminiscent
Copy link
Copy Markdown
Contributor Author

/assign @yudongusa

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Jan 21, 2026

@Reminiscent: GitHub didn't allow me to assign the following users: yudongusa.

Note that only pingcap members with read permissions, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

Details

In response to this:

/assign @yudongusa

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 kubernetes-sigs/prow repository.

@Reminiscent
Copy link
Copy Markdown
Contributor Author

/retest

@tiprow
Copy link
Copy Markdown

tiprow bot commented Jan 22, 2026

@Reminiscent: PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test.

Details

In response to this:

/retest

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 kubernetes-sigs/prow repository.

@Reminiscent
Copy link
Copy Markdown
Contributor Author

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Jan 22, 2026
Copy link
Copy Markdown

@yudongusa yudongusa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open a document PR on sysvar


// Check if the current plan is a Selection. If its child is a join, add the selection conditions
// to otherConds and continue extracting the join group from the child.
// Join reorder may distribute/push down conditions during constructing the new join tree.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CODEX has tried his best to come up with false case, but failed. We may some confidence though.

• 我尝试构造 “master 正确 / PR 开启 tidb_opt_join_reorder_sel=1 错误” 的用例,但目前没复现出结果差异。下面是我实际试过的两个候选 case,SET_VAR=0/1 输出一致:

  Case A(Selection 依赖 outer join 两侧)

  select /*+ SET_VAR(tidb_opt_join_reorder_sel=0) */ x.id, x.a, x.v
  from (
    select t1.id, t1.a, t2.v
    from t1 left join t2 on t1.id=t2.id
    where t1.a + ifnull(t2.v,0) > 0
  ) x
  join t3 on x.id = t3.id
  order by x.id;

  select /*+ SET_VAR(tidb_opt_join_reorder_sel=1) */ x.id, x.a, x.v
  from (
    select t1.id, t1.a, t2.v
    from t1 left join t2 on t1.id=t2.id
    where t1.a + ifnull(t2.v,0) > 0
  ) x
  join t3 on x.id = t3.id
  order by x.id;

  输出一致:

  1  1   0
  2 -5  10
  3  1  NULL

  Case B(非空拒绝性不强的条件)

  select /*+ SET_VAR(tidb_opt_join_reorder_sel=0) */ x.id, x.a, x.v
  from (
    select t1.id, t1.a, t2.v
    from t1 left join t2 on t1.id=t2.id
    where t2.v is null or t1.a > 0
  ) x
  join t3 on x.id = t3.id
  order by x.id;

  select /*+ SET_VAR(tidb_opt_join_reorder_sel=1) */ x.id, x.a, x.v
  from (
    select t1.id, t1.a, t2.v
    from t1 left join t2 on t1.id=t2.id
    where t2.v is null or t1.a > 0
  ) x
  join t3 on x.id = t3.id
  order by x.id;

  输出一致:

  1  1  0
  3  1  NULL

@AilinKid please use English in the comments.

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Jan 22, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AilinKid, fixdb, yudongusa

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added the approved label Jan 22, 2026
@ti-chi-bot ti-chi-bot bot merged commit fb3c1cf into pingcap:master Jan 22, 2026
31 checks passed
@Reminiscent Reminiscent deleted the handle-selection-in-join-order branch January 22, 2026 04:25
@AilinKid
Copy link
Copy Markdown
Contributor

#64535 (comment)

get, updated

@Reminiscent
Copy link
Copy Markdown
Contributor Author

/cherrypick release-8.5

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jan 23, 2026
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Copy Markdown
Member

@Reminiscent: new pull request created to branch release-8.5: #65742.
But this PR has conflicts, please resolve them!

Details

In response to this:

/cherrypick release-8.5

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

planner: selection splits the join group to two parts, forbidding join reorder to get best order

6 participants