Skip to content

sessionctx/variable: add sysvar for new join reorder impl#66792

Open
guo-shaoge wants to merge 1 commit intopingcap:masterfrom
guo-shaoge:cdc_sysvar
Open

sessionctx/variable: add sysvar for new join reorder impl#66792
guo-shaoge wants to merge 1 commit intopingcap:masterfrom
guo-shaoge:cdc_sysvar

Conversation

@guo-shaoge
Copy link
Collaborator

@guo-shaoge guo-shaoge commented Mar 9, 2026

What problem does this PR solve?

Issue Number: close #66793

Problem Summary: this PR add a new sys var for new join reorder impl, and it will be used in the next PR: #66349

What changed and how does it work?

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.

sessionctx/variable: add sysvar for new join reorder impl

Summary by CodeRabbit

  • New Features
    • New system variable tidb_opt_enable_advanced_join_reorder introduced for query optimization control. Enabled by default. Can be configured globally, at session scope, or per-query using SET_VAR hints.

Signed-off-by: guo-shaoge <shaoge1994@163.com>
@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Mar 9, 2026
@pantheon-ai
Copy link

pantheon-ai bot commented Mar 9, 2026

Review Complete

Findings: 1 issues
Posted: 1
Duplicates/Skipped: 1

ℹ️ Learn more details on Pantheon AI.

@ti-chi-bot ti-chi-bot bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Mar 9, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 9, 2026

📝 Walkthrough

Walkthrough

A new system variable tidb_opt_enable_advanced_join_reorder is introduced to the TiDB session and system variable framework. The variable is defined with constant names, integrated into the SessionVars struct, registered within the system variable configuration, marked as hint-updatable, and covered by unit and integration tests.

Changes

Cohort / File(s) Summary
Variable Definition
pkg/sessionctx/vardef/tidb_vars.go
Added constants TiDBOptEnableAdvancedJoinReorder ("tidb_opt_enable_advanced_join_reorder") and DefTiDBOptEnableAdvancedJoinReorder (true) to define the new system variable name and default value.
Session Variables
pkg/sessionctx/variable/session.go
Added TiDBOptEnableAdvancedJoinReorder bool field to SessionVars struct and initialized it in NewSessionVars to the default value from vardef.
System Variable Registration
pkg/sessionctx/variable/sysvar.go
Registered the new variable as a system variable with global-session scope, Bool type, and SetSession callback to update the corresponding SessionVars field.
Variable Handling
pkg/sessionctx/variable/setvar_affect.go
Added "tidb_opt_enable_advanced_join_reorder" entry to isHintUpdatableVerified map to allow modifications via SET_VAR hints during planner preprocessing.
Tests
pkg/sessionctx/variable/varsutil_test.go, tests/integrationtest/t/sessionctx/setvar.test, tests/integrationtest/r/sessionctx/setvar.result
Added unit tests validating field initialization and modification, and integration tests exercising set and query operations with SET_VAR hints.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Hops of joy through join reorder lands,
With advanced paths and clever hands,
A boolean flag now takes the stage,
To optimize each query page,
Where rabbit-fast decisions bloom!

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The PR description is incomplete and does not follow the template requirements. The issue number section references 'close #xxx' instead of an actual issue number, and the 'What changed and how does it work?' section is missing entirely. Replace 'close #xxx' with the actual issue number (close #66793), and fill in the 'What changed and how does it work?' section with a detailed explanation of the variable addition and its purpose.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a new system variable for join reorder implementation to the sessionctx/variable package.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@pkg/sessionctx/variable/setvar_affect.go`:
- Line 101: The sysvar "tidb_opt_enable_advanced_join_reorder" is listed as a
verified SET_VAR even though the planner does not read
SessionVars.TiDBOptEnableAdvancedJoinReorder; remove this entry from the
verified map in setvar_affect.go (or mark it unverified) so /*+ SET_VAR(...) */
does not falsely appear supported, or alternatively implement planner-side
consumption by having the planner read
SessionVars.TiDBOptEnableAdvancedJoinReorder before re-adding it to the verified
list.

In `@pkg/sessionctx/variable/sysvar.go`:
- Around line 2589-2592: The sysvar vardef.TiDBOptEnableAdvancedJoinReorder is
being registered and mutates SessionVars.TiDBOptEnableAdvancedJoinReorder but no
optimizer code consumes it; either remove/defer this registration or add the
consumer. Fix by either (A) removing the registration entry that references
vardef.TiDBOptEnableAdvancedJoinReorder and TiDBOptOn.set logic so the knob is
not user-visible, or (B) implement the consumer in the optimizer code paths to
read SessionVars.TiDBOptEnableAdvancedJoinReorder (where join reordering
decisions are made) and act on it; update tests to reflect the change. Ensure
changes reference the registration block that constructs the sysvar and the
SessionVars.TiDBOptEnableAdvancedJoinReorder field so reviewers can locate the
edits.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4ea40931-8805-447b-8dc4-2f2348186ebc

📥 Commits

Reviewing files that changed from the base of the PR and between 571b516 and 3a60fef.

📒 Files selected for processing (7)
  • pkg/sessionctx/vardef/tidb_vars.go
  • pkg/sessionctx/variable/session.go
  • pkg/sessionctx/variable/setvar_affect.go
  • pkg/sessionctx/variable/sysvar.go
  • pkg/sessionctx/variable/varsutil_test.go
  • tests/integrationtest/r/sessionctx/setvar.result
  • tests/integrationtest/t/sessionctx/setvar.test

"tidb_opt_projection_push_down": {},
"tidb_enable_vectorized_expression": {},
"tidb_opt_join_reorder_threshold": {},
"tidb_opt_enable_advanced_join_reorder": {},
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -nC2 --type=go '\bTiDBOptEnableAdvancedJoinReorder\b|tidb_opt_enable_advanced_join_reorder' pkg tests

Repository: pingcap/tidb

Length of output: 3763


Don't mark this SET_VAR as verified until the planner actually consumes it.

In the provided context, this knob is only wired into the session/sysvar layer (pkg/sessionctx/variable/session.go:1227-1228, 2349-2351 and pkg/sessionctx/variable/sysvar.go:2589-2591). A search of the codebase confirms there is no planner-side read of SessionVars.TiDBOptEnableAdvancedJoinReorder, so adding it to the verified list makes /*+ SET_VAR(...) */ look supported even though it won't affect join ordering yet.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/sessionctx/variable/setvar_affect.go` at line 101, The sysvar
"tidb_opt_enable_advanced_join_reorder" is listed as a verified SET_VAR even
though the planner does not read SessionVars.TiDBOptEnableAdvancedJoinReorder;
remove this entry from the verified map in setvar_affect.go (or mark it
unverified) so /*+ SET_VAR(...) */ does not falsely appear supported, or
alternatively implement planner-side consumption by having the planner read
SessionVars.TiDBOptEnableAdvancedJoinReorder before re-adding it to the verified
list.

Comment on lines +2589 to +2592
{Scope: vardef.ScopeGlobal | vardef.ScopeSession, Name: vardef.TiDBOptEnableAdvancedJoinReorder, Value: BoolToOnOff(vardef.DefTiDBOptEnableAdvancedJoinReorder), Type: vardef.TypeBool, SetSession: func(s *SessionVars, val string) error {
s.TiDBOptEnableAdvancedJoinReorder = TiDBOptOn(val)
return nil
}},
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify whether the new sysvar is consumed outside variable plumbing/tests.
rg -n -C2 --type=go 'TiDBOptEnableAdvancedJoinReorder|tidb_opt_enable_advanced_join_reorder' \
  -g '!tests/**' -g '!**/*_test.go'

Repository: pingcap/tidb

Length of output: 3187


Don't register a user-visible knob without implementation.

This sysvar is exposed to users (global/session scope) and settable, but no optimizer code actually consumes SessionVars.TiDBOptEnableAdvancedJoinReorder yet. This creates a misleading no-op setting. Land the consumer code in this PR, or defer the sysvar registration until the feature is ready.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/sessionctx/variable/sysvar.go` around lines 2589 - 2592, The sysvar
vardef.TiDBOptEnableAdvancedJoinReorder is being registered and mutates
SessionVars.TiDBOptEnableAdvancedJoinReorder but no optimizer code consumes it;
either remove/defer this registration or add the consumer. Fix by either (A)
removing the registration entry that references
vardef.TiDBOptEnableAdvancedJoinReorder and TiDBOptOn.set logic so the knob is
not user-visible, or (B) implement the consumer in the optimizer code paths to
read SessionVars.TiDBOptEnableAdvancedJoinReorder (where join reordering
decisions are made) and act on it; update tests to reflect the change. Ensure
changes reference the registration block that constructs the sysvar and the
SessionVars.TiDBOptEnableAdvancedJoinReorder field so reviewers can locate the
edits.

@codecov
Copy link

codecov bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.5564%. Comparing base (8833679) to head (3a60fef).
⚠️ Report is 38 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #66792        +/-   ##
================================================
- Coverage   77.6870%   77.5564%   -0.1306%     
================================================
  Files          2007       1930        -77     
  Lines        549532     543497      -6035     
================================================
- Hits         426915     421517      -5398     
- Misses       120957     121956       +999     
+ Partials       1660         24      -1636     
Flag Coverage Δ
integration 41.2143% <100.0000%> (-6.9685%) ⬇️
unit 76.5609% <100.0000%> (+0.2213%) ⬆️

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

Components Coverage Δ
dumpling 56.7974% <ø> (ø)
parser ∅ <ø> (∅)
br 48.7729% <ø> (-12.1126%) ⬇️
🚀 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.

s.TiDBOptJoinReorderThreshold = tidbOptPositiveInt32(val, vardef.DefTiDBOptJoinReorderThreshold)
return nil
}},
{Scope: vardef.ScopeGlobal | vardef.ScopeSession, Name: vardef.TiDBOptEnableAdvancedJoinReorder, Value: BoolToOnOff(vardef.DefTiDBOptEnableAdvancedJoinReorder), Type: vardef.TypeBool, SetSession: func(s *SessionVars, val string) error {
Copy link

Choose a reason for hiding this comment

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

[P2] Sysvar tidb_opt_enable_advanced_join_reorder is added but never consulted by the planner

Why: The sysvar is settable/readable with a default of true, but no planner code path reads SessionVars.TiDBOptEnableAdvancedJoinReorder. The advanced join reorder (joinorder.Optimize) is only activated through the enableCDCJoinReorder failpoint, so in production the sysvar is a complete no-op. This creates user-visible confusion: a user can SET tidb_opt_enable_advanced_join_reorder=ON/OFF with no observable effect.

Evidence:

  • pkg/sessionctx/variable/sysvar.go:2589: SetSession: s.TiDBOptEnableAdvancedJoinReorder = TiDBOptOn(val) — field is written but never read back by the planner.
  • pkg/planner/core/rule_join_reorder.go:260: failpoint.Inject("enableCDCJoinReorder" ...) — the only gate for the new framework is this failpoint, not the sysvar.
  • pkg/sessionctx/vardef/tidb_vars.go:1528: Default is true, making the no-op more misleading.

Suggested fix: Either wire the sysvar into the planner branch condition (replacing or complementing the failpoint), or keep the sysvar hidden/unexported until the new join reorder implementation is production-ready.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will use this variable in next PR: #66349

Copy link

@pantheon-ai pantheon-ai bot Mar 9, 2026

Choose a reason for hiding this comment

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

Thanks for the clarification! A staged approach makes sense. One small thing worth considering before #66349 lands: the current default is true (DefTiDBOptEnableAdvancedJoinReorder = true), which means users on this version will see the sysvar is "enabled" but it has no effect. Setting the default to false here and flipping it to true once the planner integration is complete in #66349 would avoid any potential confusion in the interim. Not a blocker — just something to keep in mind.

ℹ️ Learn more details on Pantheon AI.

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Mar 9, 2026
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Mar 9, 2026
@ti-chi-bot
Copy link

ti-chi-bot bot commented Mar 9, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-03-09 03:02:14.476321133 +0000 UTC m=+232765.988378784: ☑️ agreed by AilinKid.
  • 2026-03-09 03:49:53.211615625 +0000 UTC m=+235624.723673286: ☑️ agreed by hawkingrei.

@hawkingrei
Copy link
Member

/retest

@ti-chi-bot
Copy link

ti-chi-bot bot commented Mar 9, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: AilinKid, hawkingrei, qw4990
Once this PR has been reviewed and has the lgtm label, please assign benmeadowcroft for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found 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

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

Labels

lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add sysvar for new join reorder impl

4 participants