Skip to content

Commit dd4bc63

Browse files
add alter partition support (#22584)
support alter table add/drop/truncate/redefine partitions Approved by: @iamlinjunhong, @ouyuanning, @aunjgr
1 parent 9dea9cb commit dd4bc63

File tree

15 files changed

+2605
-965
lines changed

15 files changed

+2605
-965
lines changed

pkg/partitionservice/service.go

Lines changed: 311 additions & 49 deletions
Large diffs are not rendered by default.

pkg/partitionservice/service_list.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ func (s *Service) getMetadataByListType(
4141
def,
4242
desc,
4343
partition.PartitionMethod_List,
44-
func(p *tree.Partition) string {
45-
ctx := tree.NewFmtCtx(
46-
dialect.MYSQL,
47-
tree.WithQuoteIdentifier(),
48-
tree.WithSingleQuoteString(),
49-
)
50-
p.Values.Format(ctx)
51-
return ctx.String()
52-
},
44+
getExpr,
5345
)
5446
}

pkg/partitionservice/service_range.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ func (s *Service) getMetadataByRangeType(
4141
def,
4242
desc,
4343
partition.PartitionMethod_Range,
44-
func(p *tree.Partition) string {
45-
ctx := tree.NewFmtCtx(
46-
dialect.MYSQL,
47-
tree.WithQuoteIdentifier(),
48-
tree.WithSingleQuoteString(),
49-
)
50-
p.Values.Format(ctx)
51-
return ctx.String()
52-
},
44+
getExpr,
5345
)
5446
}

pkg/partitionservice/service_test.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,103 @@ func TestDelete(t *testing.T) {
5151
assert.NoError(t, s.Create(ctx, tableID, stmt, txnOp))
5252

5353
require.NoError(t, txnOp.Commit(ctx))
54-
require.Empty(t, s.mu.tables)
5554

5655
require.NoError(t, s.Delete(ctx, tableID, nil))
5756
},
5857
)
5958
}
6059

60+
func TestRedefine(t *testing.T) {
61+
runTestPartitionServiceTest(
62+
func(
63+
ctx context.Context,
64+
txnOp client.TxnOperator,
65+
s *Service,
66+
store PartitionStorage,
67+
) {
68+
require.Error(t, s.Redefine(ctx, 1, nil, txnOp))
69+
},
70+
)
71+
}
72+
73+
func TestRenamePartition(t *testing.T) {
74+
runTestPartitionServiceTest(
75+
func(
76+
ctx context.Context,
77+
txnOp client.TxnOperator,
78+
s *Service,
79+
store PartitionStorage,
80+
) {
81+
require.Error(t, s.Rename(ctx, 1, "old", "new", txnOp))
82+
},
83+
)
84+
}
85+
86+
func TestAddPartitions(t *testing.T) {
87+
runTestPartitionServiceTest(
88+
func(
89+
ctx context.Context,
90+
txnOp client.TxnOperator,
91+
s *Service,
92+
store PartitionStorage,
93+
) {
94+
require.Error(t, s.AddPartitions(ctx, 1, nil, txnOp))
95+
96+
tableID := uint64(1)
97+
num := uint64(2)
98+
columns := []string{"a"}
99+
def := newTestTablePartitionDefine(1, columns, []types.T{types.T_int8}, num, partition.PartitionMethod_Hash)
100+
memStore := store.(*memStorage)
101+
memStore.addUncommittedTable(def)
102+
103+
stmt := newTestHashOption(t, columns[0], num)
104+
assert.NoError(t, s.Create(ctx, tableID, stmt, txnOp))
105+
require.NoError(t, txnOp.Commit(ctx))
106+
107+
require.Error(t, s.AddPartitions(ctx, tableID, nil, txnOp))
108+
},
109+
)
110+
}
111+
112+
func TestDropPartitions(t *testing.T) {
113+
runTestPartitionServiceTest(
114+
func(
115+
ctx context.Context,
116+
txnOp client.TxnOperator,
117+
s *Service,
118+
store PartitionStorage,
119+
) {
120+
require.Error(t, s.DropPartitions(ctx, 1, nil, txnOp))
121+
122+
tableID := uint64(1)
123+
num := uint64(2)
124+
columns := []string{"a"}
125+
def := newTestTablePartitionDefine(1, columns, []types.T{types.T_int8}, num, partition.PartitionMethod_Hash)
126+
memStore := store.(*memStorage)
127+
memStore.addUncommittedTable(def)
128+
129+
stmt := newTestHashOption(t, columns[0], num)
130+
assert.NoError(t, s.Create(ctx, tableID, stmt, txnOp))
131+
require.NoError(t, txnOp.Commit(ctx))
132+
133+
require.Error(t, s.DropPartitions(ctx, tableID, nil, txnOp))
134+
},
135+
)
136+
}
137+
138+
func TestTruncatePartitions(t *testing.T) {
139+
runTestPartitionServiceTest(
140+
func(
141+
ctx context.Context,
142+
txnOp client.TxnOperator,
143+
s *Service,
144+
store PartitionStorage,
145+
) {
146+
require.Error(t, s.TruncatePartitions(ctx, 1, nil, txnOp))
147+
},
148+
)
149+
}
150+
61151
func TestIterResult(t *testing.T) {
62152
res := PruneResult{
63153
batches: make([]*batch.Batch, 10),

0 commit comments

Comments
 (0)