| 609 | } |
| 610 | |
| 611 | func TestAutoRandom(t *testing.T) { |
| 612 | testCases := []struct { |
| 613 | name string |
| 614 | sql string |
| 615 | shardBits int |
| 616 | rangeBits int |
| 617 | }{ |
| 618 | {"bare", "CREATE TABLE t (id bigint AUTO_RANDOM, PRIMARY KEY (id))", 0, 0}, |
| 619 | {"shard bits", "CREATE TABLE t (id bigint AUTO_RANDOM(5), PRIMARY KEY (id))", 5, 0}, |
| 620 | {"shard and range", "CREATE TABLE t (id bigint AUTO_RANDOM(5, 54), PRIMARY KEY (id))", 5, 54}, |
| 621 | {"tidb comment", "CREATE TABLE t (id bigint /*T![auto_rand] AUTO_RANDOM(5) */ NOT NULL, PRIMARY KEY (id))", 5, 0}, |
| 622 | } |
| 623 | |
| 624 | for _, tc := range testCases { |
| 625 | t.Run(tc.name, func(t *testing.T) { |
| 626 | tree, err := ParseDDL(tc.sql, ParserModeMysql) |
| 627 | if err != nil { |
| 628 | t.Fatalf("parse error: %v", err) |
| 629 | } |
| 630 | ddl := tree.(*DDL) |
| 631 | col := ddl.TableSpec.Columns[0] |
| 632 | if !bool(col.Type.AutoRandom) { |
| 633 | t.Error("expected AutoRandom=true") |
| 634 | } |
| 635 | if col.Type.AutoRandomShardBits != tc.shardBits { |
| 636 | t.Errorf("expected ShardBits=%d, got %d", tc.shardBits, col.Type.AutoRandomShardBits) |
| 637 | } |
| 638 | if col.Type.AutoRandomRange != tc.rangeBits { |
| 639 | t.Errorf("expected Range=%d, got %d", tc.rangeBits, col.Type.AutoRandomRange) |
| 640 | } |
| 641 | }) |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | func TestTiDBComments(t *testing.T) { |
| 646 | testCases := []struct { |