Skip to content

Commit fdbdfbf

Browse files
committed
Add support for PARTITION BY HASH in CREATE|ALTER TABLE statements
1 parent 09cf4fe commit fdbdfbf

File tree

4 files changed

+4132
-4025
lines changed

4 files changed

+4132
-4025
lines changed

ast.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ func (node *DDL) walkSubtree(visit Visit) error {
10461046
const (
10471047
ReorganizeStr = "reorganize partition"
10481048
PartitionByRangeStr = "partition by range"
1049+
PartitionByHashStr = "partition by hash"
10491050
AddPartitionStr = "add partition"
10501051
DropPartitionStr = "drop partition"
10511052
DiscardPartitionTablespaceStr = "discard partition"
@@ -1073,6 +1074,7 @@ type PartitionSpec struct {
10731074
Table TableName
10741075
Validation string
10751076
Expr Expr
1077+
IsLinear bool
10761078
IsColumns bool
10771079
ColumnList Columns
10781080
Definitions []*PartitionDefinition
@@ -1127,6 +1129,16 @@ func (node *PartitionSpec) Format(buf *TrackedBuffer) {
11271129
} else {
11281130
buf.Myprintf("%s (%v) (", node.Action, node.Expr)
11291131
}
1132+
case PartitionByHashStr:
1133+
if node.IsLinear {
1134+
buf.Myprintf("partition by linear hash (%v)", node.Expr)
1135+
} else {
1136+
buf.Myprintf("%s (%v)", node.Action, node.Expr)
1137+
}
1138+
if node.Number != "" {
1139+
buf.Myprintf(" partitions %s", node.Number)
1140+
}
1141+
return
11301142
default:
11311143
panic("unimplemented")
11321144
}

parse_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,12 @@ var (
871871
}, {
872872
input: "alter table a partition by range (id) (partition p0 values less than (10), partition p1 values less than (maxvalue))",
873873
output: "alter table a partition by range (id) (partition p0 values less than (10), partition p1 values less than (maxvalue))",
874+
}, {
875+
input: "alter table tbl_hash_a partition by hash (col_a) partitions 8",
876+
output: "alter table tbl_hash_a partition by hash (col_a) partitions 8",
877+
}, {
878+
input: "alter table tbl_hash_b partition by linear hash (col_b) partitions 4",
879+
output: "alter table tbl_hash_b partition by linear hash (col_b) partitions 4",
874880
}, {
875881
input: "alter table tbl_part add partition (partition p2 values less than (100))",
876882
output: "alter table tbl_part add partition (partition p2 values less than (100))",
@@ -2831,6 +2837,12 @@ func TestCreateTable(t *testing.T) {
28312837
"create table t (id int) partition by range (id) (partition p0 values less than (10) engine =)",
28322838
// STORAGE ENGINE option requires the ENGINE keyword.
28332839
"create table t (id int) partition by range (id) (partition p0 values less than (10) storage = InnoDB)",
2840+
// HASH expression must be parenthesized.
2841+
"create table t (id int) partition by hash id partitions 8",
2842+
// PARTITIONS requires an integer value.
2843+
"create table t (id int) partition by hash (id) partitions",
2844+
// PARTITIONS keyword must not be misspelled.
2845+
"create table t (id int) partition by hash (id) partition 8",
28342846
}
28352847
for _, sql := range invalidCreateTablePartitionSQL {
28362848
tree, err := Parse(sql)
@@ -3135,6 +3147,24 @@ func TestCreateTable(t *testing.T) {
31353147
"\tid bigint unsigned not null auto_increment,\n" +
31363148
"\tprimary key (id)\n" +
31373149
") partition by range (id) (partition p0 values less than (100) engine InnoDB, partition p1 values less than (maxvalue) engine InnoDB)",
3150+
}, {
3151+
input: "create table tbl_hash_main (\n" +
3152+
" col_a bigint unsigned not null,\n" +
3153+
" primary key (col_a)\n" +
3154+
") partition by hash (col_a) partitions 10",
3155+
output: "create table tbl_hash_main (\n" +
3156+
"\tcol_a bigint unsigned not null,\n" +
3157+
"\tprimary key (col_a)\n" +
3158+
") partition by hash (col_a) partitions 10",
3159+
}, {
3160+
input: "create table tbl_hash_linear (\n" +
3161+
" col_b bigint unsigned not null,\n" +
3162+
" primary key (col_b)\n" +
3163+
") partition by linear hash (col_b) partitions 6",
3164+
output: "create table tbl_hash_linear (\n" +
3165+
"\tcol_b bigint unsigned not null,\n" +
3166+
"\tprimary key (col_b)\n" +
3167+
") partition by linear hash (col_b) partitions 6",
31383168
}, {
31393169
input: "create table t_part (\n" +
31403170
" c_id bigint unsigned not null,\n" +

0 commit comments

Comments
 (0)