Skip to content

Comments

sql/mysql: add AUTO_RANDOM support for TiDB#3657

Open
negachov wants to merge 3 commits intoariga:masterfrom
negachov:add_tidb_auto_random
Open

sql/mysql: add AUTO_RANDOM support for TiDB#3657
negachov wants to merge 3 commits intoariga:masterfrom
negachov:add_tidb_auto_random

Conversation

@negachov
Copy link

@negachov negachov commented Feb 5, 2026

Add end-to-end support for TiDB's AUTO_RANDOM column attribute, which generates random unique IDs on BIGINT primary key columns to avoid write hotspots.

  • Inspect: parse AUTO_RANDOM(shard_bits[, range_bits]) from CREATE TABLE statement, supporting TiDB v5 through v8+ (v8 no longer populates the EXTRA column in
    INFORMATION_SCHEMA)
  • Diff: detect addition and parameter changes of AUTO_RANDOM; ignore removal (unsupported by TiDB)
  • Migrate: generate AUTO_RANDOM(S) / AUTO_RANDOM(S, R) in CREATE TABLE and ALTER TABLE
  • HCL: auto_random (shard bits) and auto_random_range (range bits) attributes with validation and round-trip support
  • Normalize RangeBits=64 (TiDB default) to 0 across all paths to prevent spurious diffs
  • Add TiDB v8.5.5 to integration test matrix

We are planning to contribute more TiDB-specific improvements. Would it be welcome to send these as incremental PRs?

@Jaakkonen
Copy link

This seems to be much better than my #3659 adding this same support in a much more naive way.

@cc-yoshimoto
Copy link

@a8m Could you please review this PR? Thanks!

- Tighten reAutoRandom regex to only match TiDB /*T![auto_rand] comments
- Add checkUnsupportedChanges for AUTO_RANDOM modification/removal detection
- Refactor ColumnChange to detect AUTO_RANDOM removal (previously ignored)
- Replace magic numbers with AutoRandom constants
- Improve error messages in setCollate, setAutoRandom, setAutoIncrement
- Add defensive nil checks in priority() and patchColumn()
- Fix bytesToBitLiteral to handle >8 byte inputs
- Rename shadowed variable in flat()
@negachov negachov force-pushed the add_tidb_auto_random branch from fae9f64 to 1b77502 Compare February 9, 2026 11:25
@negachov
Copy link
Author

  column "id" {                                                                                                                                                                 
    null        = false                                                                                                                                                         
    type        = bigint                                                                                                                                                        
    auto_random = 5
  }
  ↓
  CREATE TABLE `users` (`id` bigint NOT NULL AUTO_RANDOM(5), `name` varchar(255) NOT NULL, PRIMARY KEY (`id`))

@negachov
Copy link
Author

Hi @giautm , could you take a look at this PR when you get a chance? Happy to address any feedback. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants