Skip to content

sql/mysql: add AUTO_RANDOM support for TiDB#3657

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

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

Conversation

@negachov
Copy link
Copy Markdown

@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
Copy Markdown

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

@cc-yoshimoto
Copy link
Copy Markdown

@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
Copy Markdown
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
Copy Markdown
Author

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

@negachov
Copy link
Copy Markdown
Author

I have also added support for NONCLUSTERED.

…ants) across inspect, migrate, diff, and HCL layers. Partitions are parsed from SHOW CREATE TABLE, round-tripped through HCL, and emitted as SQL. Partition changes are detected as drop-and-recreate.

Add TiDB AUTO_RANDOM column attribute with shard/range bits, including
inspection from CREATE TABLE, HCL serialization, column diff detection,
and unsupported-change guards for modification and removal.
Enable incremental partition changes via ALTER TABLE instead of
requiring full table drop-and-recreate. The diff layer now emits
AddAttr/DropAttr/ModifyAttr for partitions, and the migration layer
generates optimal SQL: ADD PARTITION for appended definitions,
DROP PARTITION for removals, full PARTITION BY for type/key changes,
and REMOVE PARTITIONING for complete removal.
…ables

Add REORGANIZE PARTITION support for splitting, merging, and changing
partition boundaries without full table replacement. Also fix schema
inspect to detect partitioned tables via CREATE_OPTIONS so that
SHOW CREATE TABLE is always executed for them.
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