Skip to content

Commit ebcbe18

Browse files
committed
ddl: error or skip unsupported partition-related DDLs (pingcap#10672)
* ddl: error or skip unsupported partition-related DDLs * go.mod: stop replacing parser since the PR is merged * ddl: addressed comment
1 parent 8dd4a27 commit ebcbe18

File tree

7 files changed

+143
-66
lines changed

7 files changed

+143
-66
lines changed

ddl/db_integration_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,17 +1525,17 @@ func (s *testIntegrationSuite3) TestAlterAlgorithm(c *C) {
15251525
s.tk.MustExec("alter table t rename index idx_c to idx_c1, ALGORITHM=DEFAULT")
15261526

15271527
// partition.
1528-
s.assertAlterWarnExec(c, "alter table t truncate partition p1, ALGORITHM=COPY")
1529-
s.assertAlterErrorExec(c, "alter table t truncate partition p2, ALGORITHM=INPLACE")
1530-
s.tk.MustExec("alter table t truncate partition p3, ALGORITHM=INSTANT")
1528+
s.assertAlterWarnExec(c, "alter table t ALGORITHM=COPY, truncate partition p1")
1529+
s.assertAlterErrorExec(c, "alter table t ALGORITHM=INPLACE, truncate partition p2")
1530+
s.tk.MustExec("alter table t ALGORITHM=INSTANT, truncate partition p3")
15311531

15321532
s.assertAlterWarnExec(c, "alter table t add partition (partition p4 values less than (2002)), ALGORITHM=COPY")
15331533
s.assertAlterErrorExec(c, "alter table t add partition (partition p5 values less than (3002)), ALGORITHM=INPLACE")
15341534
s.tk.MustExec("alter table t add partition (partition p6 values less than (4002)), ALGORITHM=INSTANT")
15351535

1536-
s.assertAlterWarnExec(c, "alter table t drop partition p4, ALGORITHM=COPY")
1537-
s.assertAlterErrorExec(c, "alter table t drop partition p5, ALGORITHM=INPLACE")
1538-
s.tk.MustExec("alter table t drop partition p6, ALGORITHM=INSTANT")
1536+
s.assertAlterWarnExec(c, "alter table t ALGORITHM=COPY, drop partition p4")
1537+
s.assertAlterErrorExec(c, "alter table t ALGORITHM=INPLACE, drop partition p5")
1538+
s.tk.MustExec("alter table t ALGORITHM=INSTANT, drop partition p6")
15391539

15401540
// Table options
15411541
s.assertAlterWarnExec(c, "alter table t comment = 'test', ALGORITHM=COPY")

ddl/db_partition_test.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
. "github.com/pingcap/check"
2525
"github.com/pingcap/errors"
26+
"github.com/pingcap/parser/ast"
2627
"github.com/pingcap/parser/model"
2728
tmysql "github.com/pingcap/parser/mysql"
2829
"github.com/pingcap/parser/terror"
@@ -324,14 +325,18 @@ create table log_message_1 (
324325
cases := []testCase{
325326
{
326327
"create table t (id int) partition by range columns (id);",
327-
ddl.ErrPartitionsMustBeDefined,
328+
ast.ErrPartitionsMustBeDefined,
328329
},
329330
{
330331
"create table t (id int) partition by range columns (id) (partition p0 values less than (1, 2));",
331-
ddl.ErrPartitionColumnList,
332+
ast.ErrPartitionColumnList,
332333
},
333334
{
334335
"create table t (a int) partition by range columns (b) (partition p0 values less than (1, 2));",
336+
ast.ErrPartitionColumnList,
337+
},
338+
{
339+
"create table t (a int) partition by range columns (b) (partition p0 values less than (1));",
335340
ddl.ErrFieldNotFoundPart,
336341
},
337342
{
@@ -371,7 +376,10 @@ create table log_message_1 (
371376
}
372377
for i, t := range cases {
373378
_, err := tk.Exec(t.sql)
374-
c.Assert(t.err.Equal(err), IsTrue, Commentf("case %d fail, sql = %s", i, t.sql))
379+
c.Assert(t.err.Equal(err), IsTrue, Commentf(
380+
"case %d fail, sql = `%s`\nexpected error = `%v`\n actual error = `%v`",
381+
i, t.sql, t.err, err,
382+
))
375383
}
376384

377385
tk.MustExec("create table t1 (a int, b char(3)) partition by range columns (a, b) (" +
@@ -495,6 +503,15 @@ func (s *testIntegrationSuite5) TestAlterTableAddPartition(c *C) {
495503
partition p5 values less than maxvalue
496504
);`
497505
assertErrorCode(c, tk, sql7, tmysql.ErrSameNamePartition)
506+
507+
sql8 := "alter table table3 add partition (partition p6);"
508+
assertErrorCode(c, tk, sql8, tmysql.ErrPartitionRequiresValues)
509+
510+
sql9 := "alter table table3 add partition (partition p7 values in (2018));"
511+
assertErrorCode(c, tk, sql9, tmysql.ErrPartitionWrongValues)
512+
513+
sql10 := "alter table table3 add partition partitions 4;"
514+
assertErrorCode(c, tk, sql10, tmysql.ErrPartitionsMustBeDefined)
498515
}
499516

500517
func (s *testIntegrationSuite6) TestAlterTableDropPartition(c *C) {
@@ -800,7 +817,7 @@ func (s *testIntegrationSuite6) TestTruncatePartitionAndDropTable(c *C) {
800817
tk.MustExec("drop table if exists t5;")
801818
tk.MustExec("set @@session.tidb_enable_table_partition=1;")
802819
tk.MustExec(`create table t5(
803-
id int, name varchar(50),
820+
id int, name varchar(50),
804821
purchased date
805822
)
806823
partition by range( year(purchased) ) (
@@ -1456,3 +1473,25 @@ func (s *testIntegrationSuite4) TestPartitionErrorCode(c *C) {
14561473
_, err = tk.Exec("alter table t_part coalesce partition 4;")
14571474
c.Assert(ddl.ErrCoalesceOnlyOnHashPartition.Equal(err), IsTrue)
14581475
}
1476+
1477+
func (s *testIntegrationSuite3) TestUnsupportedPartitionManagementDDLs(c *C) {
1478+
tk := testkit.NewTestKit(c, s.store)
1479+
tk.MustExec("use test;")
1480+
tk.MustExec("drop table if exists test_1465;")
1481+
tk.MustExec(`
1482+
create table test_1465 (a int)
1483+
partition by range(a) (
1484+
partition p1 values less than (10),
1485+
partition p2 values less than (20),
1486+
partition p3 values less than (30)
1487+
);
1488+
`)
1489+
1490+
_, err := tk.Exec("alter table test_1465 truncate partition p1, p2")
1491+
c.Assert(err, ErrorMatches, ".*can't run multi schema change")
1492+
_, err = tk.Exec("alter table test_1465 drop partition p1, p2")
1493+
c.Assert(err, ErrorMatches, ".*can't run multi schema change")
1494+
1495+
_, err = tk.Exec("alter table test_1465 partition by hash(a)")
1496+
c.Assert(err, ErrorMatches, ".*alter table partition is unsupported")
1497+
}

ddl/ddl.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ var (
194194

195195
// ErrNotAllowedTypeInPartition returns not allowed type error when creating table partiton with unsupport expression type.
196196
ErrNotAllowedTypeInPartition = terror.ClassDDL.New(codeErrFieldTypeNotAllowedAsPartitionField, mysql.MySQLErrName[mysql.ErrFieldTypeNotAllowedAsPartitionField])
197-
// ErrPartitionsMustBeDefined returns each partition must be defined.
198-
ErrPartitionsMustBeDefined = terror.ClassDDL.New(codePartitionsMustBeDefined, "For RANGE partitions each partition must be defined")
199197
// ErrPartitionMgmtOnNonpartitioned returns it's not a partition table.
200198
ErrPartitionMgmtOnNonpartitioned = terror.ClassDDL.New(codePartitionMgmtOnNonpartitioned, "Partition management on a not partitioned table is not possible")
201199
// ErrDropPartitionNonExistent returns error in list of partition.
@@ -206,14 +204,10 @@ var (
206204
ErrRangeNotIncreasing = terror.ClassDDL.New(codeRangeNotIncreasing, "VALUES LESS THAN value must be strictly increasing for each partition")
207205
// ErrPartitionMaxvalue returns maxvalue can only be used in last partition definition.
208206
ErrPartitionMaxvalue = terror.ClassDDL.New(codePartitionMaxvalue, "MAXVALUE can only be used in last partition definition")
209-
// ErrTooManyValues returns cannot have more than one value for this type of partitioning.
210-
ErrTooManyValues = terror.ClassDDL.New(codeErrTooManyValues, mysql.MySQLErrName[mysql.ErrTooManyValues])
211207
//ErrDropLastPartition returns cannot remove all partitions, use drop table instead.
212208
ErrDropLastPartition = terror.ClassDDL.New(codeDropLastPartition, mysql.MySQLErrName[mysql.ErrDropLastPartition])
213209
//ErrTooManyPartitions returns too many partitions were defined.
214210
ErrTooManyPartitions = terror.ClassDDL.New(codeTooManyPartitions, mysql.MySQLErrName[mysql.ErrTooManyPartitions])
215-
//ErrNoParts returns no partition were defined.
216-
ErrNoParts = terror.ClassDDL.New(codeNoParts, mysql.MySQLErrName[mysql.ErrNoParts])
217211
//ErrPartitionFunctionIsNotAllowed returns this partition function is not allowed.
218212
ErrPartitionFunctionIsNotAllowed = terror.ClassDDL.New(codePartitionFunctionIsNotAllowed, mysql.MySQLErrName[mysql.ErrPartitionFunctionIsNotAllowed])
219213
// ErrPartitionFuncNotAllowed returns partition function returns the wrong type.
@@ -235,8 +229,6 @@ var (
235229
ErrTableCantHandleFt = terror.ClassDDL.New(codeErrTableCantHandleFt, mysql.MySQLErrName[mysql.ErrTableCantHandleFt])
236230
// ErrFieldNotFoundPart returns an error when 'partition by columns' are not found in table columns.
237231
ErrFieldNotFoundPart = terror.ClassDDL.New(codeFieldNotFoundPart, mysql.MySQLErrName[mysql.ErrFieldNotFoundPart])
238-
// ErrPartitionColumnList returns "Inconsistency in usage of column lists for partitioning".
239-
ErrPartitionColumnList = terror.ClassDDL.New(codePartitionColumnList, mysql.MySQLErrName[mysql.ErrPartitionColumnList])
240232
)
241233

242234
// DDL is responsible for updating schema in data store and maintaining in-memory InfoSchema cache.
@@ -750,6 +742,14 @@ const (
750742
codeFieldNotFoundPart = terror.ErrCode(mysql.ErrFieldNotFoundPart)
751743
codePartitionColumnList = terror.ErrCode(mysql.ErrPartitionColumnList)
752744
codeOnlyOnRangeListPartition = terror.ErrCode(mysql.ErrOnlyOnRangeListPartition)
745+
codePartitionRequiresValues = terror.ErrCode(mysql.ErrPartitionRequiresValues)
746+
codePartitionWrongNoPart = terror.ErrCode(mysql.ErrPartitionWrongNoPart)
747+
codePartitionWrongNoSubpart = terror.ErrCode(mysql.ErrPartitionWrongNoSubpart)
748+
codePartitionWrongValues = terror.ErrCode(mysql.ErrPartitionWrongValues)
749+
codeRowSinglePartitionField = terror.ErrCode(mysql.ErrRowSinglePartitionField)
750+
codeSubpartition = terror.ErrCode(mysql.ErrSubpartition)
751+
codeSystemVersioningWrongPartitions = terror.ErrCode(mysql.ErrSystemVersioningWrongPartitions)
752+
codeWrongPartitionTypeExpectedSystemTime = terror.ErrCode(mysql.ErrWrongPartitionTypeExpectedSystemTime)
753753
)
754754

755755
func init() {
@@ -813,6 +813,14 @@ func init() {
813813
codeInvalidDefaultValue: mysql.ErrInvalidDefault,
814814
codeErrGeneratedColumnRefAutoInc: mysql.ErrGeneratedColumnRefAutoInc,
815815
codeOnlyOnRangeListPartition: mysql.ErrOnlyOnRangeListPartition,
816+
codePartitionRequiresValues: mysql.ErrPartitionRequiresValues,
817+
codePartitionWrongNoPart: mysql.ErrPartitionWrongNoPart,
818+
codePartitionWrongNoSubpart: mysql.ErrPartitionWrongNoSubpart,
819+
codePartitionWrongValues: mysql.ErrPartitionWrongValues,
820+
codeRowSinglePartitionField: mysql.ErrRowSinglePartitionField,
821+
codeSubpartition: mysql.ErrSubpartition,
822+
codeSystemVersioningWrongPartitions: mysql.ErrSystemVersioningWrongPartitions,
823+
codeWrongPartitionTypeExpectedSystemTime: mysql.ErrWrongPartitionTypeExpectedSystemTime,
816824
}
817825
terror.ErrClassToMySQLCodes[terror.ClassDDL] = ddlMySQLErrCodes
818826
}

ddl/ddl_api.go

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,12 +1546,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15461546
// Range columns partition key supports multiple data types with integer、datetime、string.
15471547
defs := pi.Definitions
15481548
if len(defs) < 1 {
1549-
return errors.Trace(ErrPartitionsMustBeDefined)
1549+
return ast.ErrPartitionsMustBeDefined.GenWithStackByArgs("RANGE")
15501550
}
15511551

15521552
curr := &defs[0]
15531553
if len(curr.LessThan) != len(pi.Columns) {
1554-
return errors.Trace(ErrPartitionColumnList)
1554+
return errors.Trace(ast.ErrPartitionColumnList)
15551555
}
15561556
for i := 1; i < len(defs); i++ {
15571557
prev, curr := curr, &defs[i]
@@ -1568,7 +1568,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15681568

15691569
func checkTwoRangeColumns(ctx sessionctx.Context, curr, prev *model.PartitionDefinition, pi *model.PartitionInfo, tbInfo *model.TableInfo) (bool, error) {
15701570
if len(curr.LessThan) != len(pi.Columns) {
1571-
return false, errors.Trace(ErrPartitionColumnList)
1571+
return false, errors.Trace(ast.ErrPartitionColumnList)
15721572
}
15731573
for i := 0; i < len(pi.Columns); i++ {
15741574
// Special handling for MAXVALUE.
@@ -1775,8 +1775,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
17751775
validSpecs = append(validSpecs, spec)
17761776
}
17771777

1778-
if len(validSpecs) != 1 {
1779-
// TODO: Hanlde len(validSpecs) == 0.
1778+
if len(validSpecs) > 1 {
17801779
// Now we only allow one schema changing at the same time.
17811780
return nil, errRunMultiSchemaChanges
17821781
}
@@ -1863,6 +1862,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18631862
err = ErrUnsupportedModifyPrimaryKey.GenWithStackByArgs("drop")
18641863
case ast.AlterTableRenameIndex:
18651864
err = d.RenameIndex(ctx, ident, spec)
1865+
case ast.AlterTablePartition:
1866+
// Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1867+
err = errors.New("alter table partition is unsupported")
18661868
case ast.AlterTableOption:
18671869
for i, opt := range spec.Options {
18681870
switch opt.Tp {
@@ -2089,10 +2091,6 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
20892091
if meta.GetPartitionInfo() == nil {
20902092
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
20912093
}
2092-
// We don't support add hash type partition now.
2093-
if meta.Partition.Type == model.PartitionTypeHash {
2094-
return errors.Trace(ErrUnsupportedAddPartition)
2095-
}
20962094

20972095
partInfo, err := buildPartitionInfo(meta, d, spec)
20982096
if err != nil {
@@ -2144,20 +2142,28 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21442142
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
21452143
}
21462144

2147-
// Coalesce partition can only be used on hash/key partitions.
2148-
if meta.Partition.Type == model.PartitionTypeRange {
2149-
return errors.Trace(ErrCoalesceOnlyOnHashPartition)
2150-
}
2151-
2145+
switch meta.Partition.Type {
21522146
// We don't support coalesce partitions hash type partition now.
2153-
if meta.Partition.Type == model.PartitionTypeHash {
2147+
case model.PartitionTypeHash:
21542148
return errors.Trace(ErrUnsupportedCoalescePartition)
2149+
2150+
// Key type partition cannot be constructed currently, ignoring it for now.
2151+
case model.PartitionTypeKey:
2152+
2153+
// Coalesce partition can only be used on hash/key partitions.
2154+
default:
2155+
return errors.Trace(ErrCoalesceOnlyOnHashPartition)
21552156
}
21562157

21572158
return errors.Trace(err)
21582159
}
21592160

21602161
func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
2162+
// TODO: Support truncate multiple partitions
2163+
if len(spec.PartitionNames) != 1 {
2164+
return errRunMultiSchemaChanges
2165+
}
2166+
21612167
is := d.infoHandle.Get()
21622168
schema, ok := is.SchemaByName(ident.Schema)
21632169
if !ok {
@@ -2173,7 +2179,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21732179
}
21742180

21752181
var pid int64
2176-
pid, err = tables.FindPartitionByName(meta, spec.Name)
2182+
pid, err = tables.FindPartitionByName(meta, spec.PartitionNames[0].L)
21772183
if err != nil {
21782184
return errors.Trace(err)
21792185
}
@@ -2195,6 +2201,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21952201
}
21962202

21972203
func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
2204+
// TODO: Support drop multiple partitions
2205+
if len(spec.PartitionNames) != 1 {
2206+
return errRunMultiSchemaChanges
2207+
}
2208+
21982209
is := d.infoHandle.Get()
21992210
schema, ok := is.SchemaByName(ident.Schema)
22002211
if !ok {
@@ -2208,7 +2219,9 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22082219
if meta.GetPartitionInfo() == nil {
22092220
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
22102221
}
2211-
err = checkDropTablePartition(meta, spec.Name)
2222+
2223+
partName := spec.PartitionNames[0].L
2224+
err = checkDropTablePartition(meta, partName)
22122225
if err != nil {
22132226
return errors.Trace(err)
22142227
}
@@ -2218,7 +2231,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
22182231
TableID: meta.ID,
22192232
Type: model.ActionDropTablePartition,
22202233
BinlogInfo: &model.HistoryInfo{},
2221-
Args: []interface{}{spec.Name},
2234+
Args: []interface{}{partName},
22222235
}
22232236

22242237
err = d.doDDLJob(ctx, job)
@@ -3180,9 +3193,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
31803193
}
31813194

31823195
func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec) (*model.PartitionInfo, error) {
3183-
if meta.Partition.Type == model.PartitionTypeRange && len(spec.PartDefinitions) == 0 {
3184-
return nil, errors.Trace(ErrPartitionsMustBeDefined)
3196+
if meta.Partition.Type == model.PartitionTypeRange {
3197+
if len(spec.PartDefinitions) == 0 {
3198+
return nil, ast.ErrPartitionsMustBeDefined.GenWithStackByArgs(meta.Partition.Type)
3199+
}
3200+
} else {
3201+
// we don't support ADD PARTITION for all other partition types yet.
3202+
return nil, errors.Trace(ErrUnsupportedAddPartition)
31853203
}
3204+
31863205
part := &model.PartitionInfo{
31873206
Type: meta.Partition.Type,
31883207
Expr: meta.Partition.Expr,
@@ -3195,7 +3214,12 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31953214
}
31963215
buf := new(bytes.Buffer)
31973216
for ith, def := range spec.PartDefinitions {
3198-
for _, expr := range def.LessThan {
3217+
if err := def.Clause.Validate(part.Type, len(part.Columns)); err != nil {
3218+
return nil, err
3219+
}
3220+
// For RANGE partition only VALUES LESS THAN should be possible.
3221+
clause := def.Clause.(*ast.PartitionDefinitionClauseLessThan)
3222+
for _, expr := range clause.Exprs {
31993223
tp := expr.GetType().Tp
32003224
if len(part.Columns) == 0 {
32013225
// Partition by range.
@@ -3210,14 +3234,15 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
32103234
}
32113235
// Partition by range columns if len(part.Columns) != 0.
32123236
}
3237+
comment, _ := def.Comment()
32133238
piDef := model.PartitionDefinition{
32143239
Name: def.Name,
32153240
ID: genIDs[ith],
3216-
Comment: def.Comment,
3241+
Comment: comment,
32173242
}
32183243

32193244
buf := new(bytes.Buffer)
3220-
for _, expr := range def.LessThan {
3245+
for _, expr := range clause.Exprs {
32213246
expr.Format(buf)
32223247
piDef.LessThan = append(piDef.LessThan, buf.String())
32233248
buf.Reset()

0 commit comments

Comments
 (0)