Skip to content

pkg/ddl: fix exchange partition metadata mismatch for shard_row_id_bits#66632

Merged
ti-chi-bot[bot] merged 3 commits intopingcap:masterfrom
fzzf678:codex/issue-66077-master
Mar 3, 2026
Merged

pkg/ddl: fix exchange partition metadata mismatch for shard_row_id_bits#66632
ti-chi-bot[bot] merged 3 commits intopingcap:masterfrom
fzzf678:codex/issue-66077-master

Conversation

@fzzf678
Copy link
Contributor

@fzzf678 fzzf678 commented Mar 2, 2026

What problem does this PR solve?

Issue Number: close #66077

Problem Summary:
ALTER TABLE ... EXCHANGE PARTITION could incorrectly return [ddl:1736] Tables have different definitions when two tables have the same visible schema but differ in internal MaxShardRowIDBits metadata.

What changed and how does it work?

When tidb_shard_row_id_bits is set in session/global variables, CREATE TABLE previously only populated TableInfo.ShardRowIDBits but left TableInfo.MaxShardRowIDBits as 0. This makes metadata inconsistent with /*T! SHARD_ROW_ID_BITS=... */ which always sets both fields, and breaks the table definition compatibility check used by EXCHANGE PARTITION.

This PR sets MaxShardRowIDBits = ShardRowIDBits when applying default shard row id bits during CREATE TABLE, and adds a regression unit test using the SQL from the issue.

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.

Fix `ALTER TABLE ... EXCHANGE PARTITION` mistakenly reporting `Tables have different definitions` when `SHARD_ROW_ID_BITS` is enabled.

Summary by CodeRabbit

  • Bug Fixes

    • Max shard row ID bits now mirrors configured shard row ID bits for default non-clustered, non-temporary tables.
  • Tests

    • Added a test covering partition exchange behavior when shard row ID bits are non-default and partition definitions differ.

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/needs-triage-completed labels Mar 2, 2026
@pantheon-ai
Copy link

pantheon-ai bot commented Mar 2, 2026

Review Complete

Findings: 0 issues
Posted: 0
Duplicates/Skipped: 0

@ti-chi-bot ti-chi-bot bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 2, 2026
@tiprow
Copy link

tiprow bot commented Mar 2, 2026

Hi @fzzf678. 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.

@coderabbitai
Copy link

coderabbitai bot commented Mar 2, 2026

📝 Walkthrough

Walkthrough

Sets tbInfo.MaxShardRowIDBits = tbInfo.ShardRowIDBits for non-clustered, non-temporary tables in table-build flow; adds a regression test exercising exchange partition with a non-default shard_row_id_bits value.

Changes

Cohort / File(s) Summary
Core Fix
pkg/ddl/create_table.go
Assigns tbInfo.MaxShardRowIDBits = tbInfo.ShardRowIDBits in BuildTableInfoWithStmt when the table is non-clustered and not temporary.
Tests
pkg/ddl/tests/partition/db_partition_test.go
Adds TestIssue66077ExchangePartitionDifferentDefinitionsWithShardRowIDBits to validate exchanging partitions when shard_row_id_bits is non-default (test appears duplicated in the file).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

ok-to-test

Suggested reviewers

  • bb7133
  • xhebox
  • wjhuang2016

Poem

🐰 I hopped through code and set a bit just right,
Now partitions swap without a fright,
Max and Shard align in tune,
Tests hop happily under the moon,
A tiny fix — a rabbit's delight.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: fixing exchange partition metadata mismatch for shard_row_id_bits. It is specific, concise, and directly related to the primary code change.
Description check ✅ Passed The description includes the required template sections: Issue Number (close #66077), Problem Summary, What Changed and How It Works, Check List with Unit test selected, and Release note. All required information is present and complete.
Linked Issues check ✅ Passed The PR addresses the objective from issue #66077: setting MaxShardRowIDBits = ShardRowIDBits during CREATE TABLE to ensure consistent metadata. The change in create_table.go implements this fix, and the added test validates the scenario where EXCHANGE PARTITION fails due to metadata mismatch.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the shard_row_id_bits metadata issue. The code change in create_table.go adds one line setting MaxShardRowIDBits, and the test validates the fix without introducing unrelated modifications.

✏️ 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
Member

@YangKeao YangKeao left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Mar 2, 2026
@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.4907%. Comparing base (255b3fb) to head (81f2f9d).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #66632        +/-   ##
================================================
+ Coverage   77.6807%   79.4907%   +1.8100%     
================================================
  Files          2008       1956        -52     
  Lines        549265     537094     -12171     
================================================
+ Hits         426673     426940       +267     
+ Misses       120920     108691     -12229     
+ Partials       1672       1463       -209     
Flag Coverage Δ
integration 47.4935% <100.0000%> (-0.7025%) ⬇️
unit 76.6625% <100.0000%> (+0.3336%) ⬆️

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

Components Coverage Δ
dumpling 56.7974% <ø> (ø)
parser ∅ <ø> (∅)
br 66.3651% <ø> (+5.4820%) ⬆️
🚀 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.

Copy link

@pantheon-ai pantheon-ai bot left a comment

Choose a reason for hiding this comment

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

✅ Code looks good. No issues found.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Mar 2, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wjhuang2016, YangKeao

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 approved lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Mar 2, 2026
@ti-chi-bot
Copy link

ti-chi-bot bot commented Mar 2, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-03-02 09:20:49.573337585 +0000 UTC m=+177094.151416769: ☑️ agreed by YangKeao.
  • 2026-03-02 12:34:35.1552691 +0000 UTC m=+188719.733348284: ☑️ agreed by wjhuang2016.

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.

🧹 Nitpick comments (1)
pkg/ddl/tests/partition/db_partition_test.go (1)

4179-4182: Make exchange-partition enablement explicit for test determinism.

To avoid dependence on environment defaults, set tidb_enable_exchange_partition in this test explicitly.

Suggested patch
 func TestIssue66077ExchangePartitionDifferentDefinitionsWithShardRowIDBits(t *testing.T) {
 	store := testkit.CreateMockStore(t)
 	tk := testkit.NewTestKit(t, store)
+	tk.MustExec("set @@session.tidb_enable_exchange_partition = 1")
+	defer tk.MustExec("set @@session.tidb_enable_exchange_partition = 0")
 	tk.MustExec("set @@session.tidb_enable_clustered_index = off")
 	tk.MustExec("set @@session.tidb_shard_row_id_bits = 4")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/ddl/tests/partition/db_partition_test.go` around lines 4179 - 4182, The
test relies on the session variable tidb_enable_exchange_partition but doesn't
set it explicitly; update the test initialized via testkit.NewTestKit(t, store)
to call tk.MustExec to set @@session.tidb_enable_exchange_partition (e.g., to
ON) before the create database sbtest statement so the test is deterministic
across environments—add the tk.MustExec("set
@@session.tidb_enable_exchange_partition = on") call alongside the other
tk.MustExec lines.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pkg/ddl/tests/partition/db_partition_test.go`:
- Around line 4179-4182: The test relies on the session variable
tidb_enable_exchange_partition but doesn't set it explicitly; update the test
initialized via testkit.NewTestKit(t, store) to call tk.MustExec to set
@@session.tidb_enable_exchange_partition (e.g., to ON) before the create
database sbtest statement so the test is deterministic across environments—add
the tk.MustExec("set @@session.tidb_enable_exchange_partition = on") call
alongside the other tk.MustExec lines.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9f94cce and 5f7656b.

📒 Files selected for processing (1)
  • pkg/ddl/tests/partition/db_partition_test.go

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.

🧹 Nitpick comments (1)
pkg/ddl/tests/partition/db_partition_test.go (1)

4182-4183: Make DB setup idempotent to avoid flaky reruns.

Using a fixed DB name with create database sbtest can fail in shared-store/rerun scenarios. Consider dropping it first.

♻️ Proposed change
-	tk.MustExec("create database sbtest")
+	tk.MustExec("drop database if exists sbtest")
+	tk.MustExec("create database sbtest")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/ddl/tests/partition/db_partition_test.go` around lines 4182 - 4183, The
test uses a fixed database name via tk.MustExec("create database sbtest") which
can fail on reruns; make the DB setup idempotent by ensuring the database is
removed or created conditionally before use: update the setup calls around
tk.MustExec to either run a drop (e.g., DROP DATABASE IF EXISTS sbtest) prior to
create or replace the create with a conditional form (CREATE DATABASE IF NOT
EXISTS sbtest), then continue with tk.MustExec("use sbtest") so reruns won’t
fail.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@pkg/ddl/tests/partition/db_partition_test.go`:
- Around line 4182-4183: The test uses a fixed database name via
tk.MustExec("create database sbtest") which can fail on reruns; make the DB
setup idempotent by ensuring the database is removed or created conditionally
before use: update the setup calls around tk.MustExec to either run a drop
(e.g., DROP DATABASE IF EXISTS sbtest) prior to create or replace the create
with a conditional form (CREATE DATABASE IF NOT EXISTS sbtest), then continue
with tk.MustExec("use sbtest") so reruns won’t fail.

ℹ️ Review info

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f7656b and 81f2f9d.

📒 Files selected for processing (1)
  • pkg/ddl/tests/partition/db_partition_test.go

@fzzf678
Copy link
Contributor Author

fzzf678 commented Mar 2, 2026

/retest

@tiprow
Copy link

tiprow bot commented Mar 2, 2026

@fzzf678: 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.

@hawkingrei
Copy link
Member

/retest

1 similar comment
@fzzf678
Copy link
Contributor Author

fzzf678 commented Mar 3, 2026

/retest

@tiprow
Copy link

tiprow bot commented Mar 3, 2026

@fzzf678: 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.

@ti-chi-bot ti-chi-bot bot merged commit dde5ab3 into pingcap:master Mar 3, 2026
34 checks passed
@ti-chi-bot
Copy link

ti-chi-bot bot commented Mar 3, 2026

@fzzf678: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-integration-realcluster-test-next-gen 81f2f9d link unknown /test pull-integration-realcluster-test-next-gen

Full PR test history. Your PR dashboard.

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. I understand the commands that are listed here.

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

Labels

approved lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exchange partition report "Tables have different definitions" error

4 participants