Skip to content

Commit f42c039

Browse files
committed
ddl: error or skip unsupported partition-related DDLs
1 parent 018f1d0 commit f42c039

File tree

7 files changed

+138
-62
lines changed

7 files changed

+138
-62
lines changed

ddl/db_integration_test.go

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

14461446
// partition.
1447-
s.assertAlterWarnExec(c, "alter table t truncate partition p1, ALGORITHM=COPY")
1448-
s.assertAlterErrorExec(c, "alter table t truncate partition p2, ALGORITHM=INPLACE")
1449-
s.tk.MustExec("alter table t truncate partition p3, ALGORITHM=INSTANT")
1447+
s.assertAlterWarnExec(c, "alter table t ALGORITHM=COPY, truncate partition p1")
1448+
s.assertAlterErrorExec(c, "alter table t ALGORITHM=INPLACE, truncate partition p2")
1449+
s.tk.MustExec("alter table t ALGORITHM=INSTANT, truncate partition p3")
14501450

14511451
s.assertAlterWarnExec(c, "alter table t add partition (partition p4 values less than (2002)), ALGORITHM=COPY")
14521452
s.assertAlterErrorExec(c, "alter table t add partition (partition p5 values less than (3002)), ALGORITHM=INPLACE")
14531453
s.tk.MustExec("alter table t add partition (partition p6 values less than (4002)), ALGORITHM=INSTANT")
14541454

1455-
s.assertAlterWarnExec(c, "alter table t drop partition p4, ALGORITHM=COPY")
1456-
s.assertAlterErrorExec(c, "alter table t drop partition p5, ALGORITHM=INPLACE")
1457-
s.tk.MustExec("alter table t drop partition p6, ALGORITHM=INSTANT")
1455+
s.assertAlterWarnExec(c, "alter table t ALGORITHM=COPY, drop partition p4")
1456+
s.assertAlterErrorExec(c, "alter table t ALGORITHM=INPLACE, drop partition p5")
1457+
s.tk.MustExec("alter table t ALGORITHM=INSTANT, drop partition p6")
14581458

14591459
// Table options
14601460
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 *testIntegrationSuite5) TestAlterTableDropPartition(c *C) {
@@ -797,7 +814,7 @@ func (s *testIntegrationSuite5) TestTruncatePartitionAndDropTable(c *C) {
797814
tk.MustExec("drop table if exists t5;")
798815
tk.MustExec("set @@session.tidb_enable_table_partition=1;")
799816
tk.MustExec(`create table t5(
800-
id int, name varchar(50),
817+
id int, name varchar(50),
801818
purchased date
802819
)
803820
partition by range( year(purchased) ) (
@@ -1453,3 +1470,25 @@ func (s *testIntegrationSuite3) TestPartitionErrorCode(c *C) {
14531470
_, err = tk.Exec("alter table t_part coalesce partition 4;")
14541471
c.Assert(ddl.ErrCoalesceOnlyOnHashPartition.Equal(err), IsTrue)
14551472
}
1473+
1474+
func (s *testIntegrationSuite3) TestUnsupportedPartitionManagementDDLs(c *C) {
1475+
tk := testkit.NewTestKit(c, s.store)
1476+
tk.MustExec("use test;")
1477+
tk.MustExec("drop table if exists test_1465;")
1478+
tk.MustExec(`
1479+
create table test_1465 (a int)
1480+
partition by range(a) (
1481+
partition p1 values less than (10),
1482+
partition p2 values less than (20),
1483+
partition p3 values less than (30)
1484+
);
1485+
`)
1486+
1487+
_, err := tk.Exec("alter table test_1465 truncate partition p1, p2")
1488+
c.Assert(err, ErrorMatches, ".*can't run multi schema change")
1489+
_, err = tk.Exec("alter table test_1465 drop partition p1, p2")
1490+
c.Assert(err, ErrorMatches, ".*can't run multi schema change")
1491+
1492+
_, err = tk.Exec("alter table test_1465 partition by hash(a)")
1493+
c.Assert(err, ErrorMatches, ".*alter table partition is unsupported")
1494+
}

ddl/ddl.go

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

193193
// ErrNotAllowedTypeInPartition returns not allowed type error when creating table partiton with unsupport expression type.
194194
ErrNotAllowedTypeInPartition = terror.ClassDDL.New(codeErrFieldTypeNotAllowedAsPartitionField, mysql.MySQLErrName[mysql.ErrFieldTypeNotAllowedAsPartitionField])
195-
// ErrPartitionsMustBeDefined returns each partition must be defined.
196-
ErrPartitionsMustBeDefined = terror.ClassDDL.New(codePartitionsMustBeDefined, "For RANGE partitions each partition must be defined")
197195
// ErrPartitionMgmtOnNonpartitioned returns it's not a partition table.
198196
ErrPartitionMgmtOnNonpartitioned = terror.ClassDDL.New(codePartitionMgmtOnNonpartitioned, "Partition management on a not partitioned table is not possible")
199197
// ErrDropPartitionNonExistent returns error in list of partition.
@@ -204,14 +202,10 @@ var (
204202
ErrRangeNotIncreasing = terror.ClassDDL.New(codeRangeNotIncreasing, "VALUES LESS THAN value must be strictly increasing for each partition")
205203
// ErrPartitionMaxvalue returns maxvalue can only be used in last partition definition.
206204
ErrPartitionMaxvalue = terror.ClassDDL.New(codePartitionMaxvalue, "MAXVALUE can only be used in last partition definition")
207-
// ErrTooManyValues returns cannot have more than one value for this type of partitioning.
208-
ErrTooManyValues = terror.ClassDDL.New(codeErrTooManyValues, mysql.MySQLErrName[mysql.ErrTooManyValues])
209205
//ErrDropLastPartition returns cannot remove all partitions, use drop table instead.
210206
ErrDropLastPartition = terror.ClassDDL.New(codeDropLastPartition, mysql.MySQLErrName[mysql.ErrDropLastPartition])
211207
//ErrTooManyPartitions returns too many partitions were defined.
212208
ErrTooManyPartitions = terror.ClassDDL.New(codeTooManyPartitions, mysql.MySQLErrName[mysql.ErrTooManyPartitions])
213-
//ErrNoParts returns no partition were defined.
214-
ErrNoParts = terror.ClassDDL.New(codeNoParts, mysql.MySQLErrName[mysql.ErrNoParts])
215209
//ErrPartitionFunctionIsNotAllowed returns this partition function is not allowed.
216210
ErrPartitionFunctionIsNotAllowed = terror.ClassDDL.New(codePartitionFunctionIsNotAllowed, mysql.MySQLErrName[mysql.ErrPartitionFunctionIsNotAllowed])
217211
// ErrPartitionFuncNotAllowed returns partition function returns the wrong type.
@@ -233,8 +227,6 @@ var (
233227
ErrTableCantHandleFt = terror.ClassDDL.New(codeErrTableCantHandleFt, mysql.MySQLErrName[mysql.ErrTableCantHandleFt])
234228
// ErrFieldNotFoundPart returns an error when 'partition by columns' are not found in table columns.
235229
ErrFieldNotFoundPart = terror.ClassDDL.New(codeFieldNotFoundPart, mysql.MySQLErrName[mysql.ErrFieldNotFoundPart])
236-
// ErrPartitionColumnList returns "Inconsistency in usage of column lists for partitioning".
237-
ErrPartitionColumnList = terror.ClassDDL.New(codePartitionColumnList, mysql.MySQLErrName[mysql.ErrPartitionColumnList])
238230
)
239231

240232
// DDL is responsible for updating schema in data store and maintaining in-memory InfoSchema cache.
@@ -731,6 +723,14 @@ const (
731723
codeNotSupportedAlterOperation = terror.ErrCode(mysql.ErrAlterOperationNotSupportedReason)
732724
codeFieldNotFoundPart = terror.ErrCode(mysql.ErrFieldNotFoundPart)
733725
codePartitionColumnList = terror.ErrCode(mysql.ErrPartitionColumnList)
726+
codePartitionRequiresValues = terror.ErrCode(mysql.ErrPartitionRequiresValues)
727+
codePartitionWrongNoPart = terror.ErrCode(mysql.ErrPartitionWrongNoPart)
728+
codePartitionWrongNoSubpart = terror.ErrCode(mysql.ErrPartitionWrongNoSubpart)
729+
codePartitionWrongValues = terror.ErrCode(mysql.ErrPartitionWrongValues)
730+
codeRowSinglePartitionField = terror.ErrCode(mysql.ErrRowSinglePartitionField)
731+
codeSubpartition = terror.ErrCode(mysql.ErrSubpartition)
732+
codeSystemVersioningWrongPartitions = terror.ErrCode(mysql.ErrSystemVersioningWrongPartitions)
733+
codeWrongPartitionTypeExpectedSystemTime = terror.ErrCode(mysql.ErrWrongPartitionTypeExpectedSystemTime)
734734
)
735735

736736
func init() {
@@ -793,6 +793,14 @@ func init() {
793793
codePartitionColumnList: mysql.ErrPartitionColumnList,
794794
codeInvalidDefaultValue: mysql.ErrInvalidDefault,
795795
codeErrGeneratedColumnRefAutoInc: mysql.ErrGeneratedColumnRefAutoInc,
796+
codePartitionRequiresValues: mysql.ErrPartitionRequiresValues,
797+
codePartitionWrongNoPart: mysql.ErrPartitionWrongNoPart,
798+
codePartitionWrongNoSubpart: mysql.ErrPartitionWrongNoSubpart,
799+
codePartitionWrongValues: mysql.ErrPartitionWrongValues,
800+
codeRowSinglePartitionField: mysql.ErrRowSinglePartitionField,
801+
codeSubpartition: mysql.ErrSubpartition,
802+
codeSystemVersioningWrongPartitions: mysql.ErrSystemVersioningWrongPartitions,
803+
codeWrongPartitionTypeExpectedSystemTime: mysql.ErrWrongPartitionTypeExpectedSystemTime,
796804
}
797805
terror.ErrClassToMySQLCodes[terror.ClassDDL] = ddlMySQLErrCodes
798806
}

ddl/ddl_api.go

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,12 +1517,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15171517
// Range columns partition key supports multiple data types with integer、datetime、string.
15181518
defs := pi.Definitions
15191519
if len(defs) < 1 {
1520-
return errors.Trace(ErrPartitionsMustBeDefined)
1520+
return ast.ErrPartitionsMustBeDefined.GenWithStackByArgs("RANGE")
15211521
}
15221522

15231523
curr := &defs[0]
15241524
if len(curr.LessThan) != len(pi.Columns) {
1525-
return errors.Trace(ErrPartitionColumnList)
1525+
return errors.Trace(ast.ErrPartitionColumnList)
15261526
}
15271527
for i := 1; i < len(defs); i++ {
15281528
prev, curr := curr, &defs[i]
@@ -1539,7 +1539,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15391539

15401540
func checkTwoRangeColumns(ctx sessionctx.Context, curr, prev *model.PartitionDefinition, pi *model.PartitionInfo, tbInfo *model.TableInfo) (bool, error) {
15411541
if len(curr.LessThan) != len(pi.Columns) {
1542-
return false, errors.Trace(ErrPartitionColumnList)
1542+
return false, errors.Trace(ast.ErrPartitionColumnList)
15431543
}
15441544
for i := 0; i < len(pi.Columns); i++ {
15451545
// Special handling for MAXVALUE.
@@ -1747,8 +1747,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
17471747
validSpecs = append(validSpecs, spec)
17481748
}
17491749

1750-
if len(validSpecs) != 1 {
1751-
// TODO: Hanlde len(validSpecs) == 0.
1750+
if len(validSpecs) > 1 {
17521751
// Now we only allow one schema changing at the same time.
17531752
return nil, errRunMultiSchemaChanges
17541753
}
@@ -1835,6 +1834,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18351834
err = ErrUnsupportedModifyPrimaryKey.GenWithStackByArgs("drop")
18361835
case ast.AlterTableRenameIndex:
18371836
err = d.RenameIndex(ctx, ident, spec)
1837+
case ast.AlterTablePartition:
1838+
// Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1839+
err = errors.New("alter table partition is unsupported")
18381840
case ast.AlterTableOption:
18391841
for i, opt := range spec.Options {
18401842
switch opt.Tp {
@@ -2060,10 +2062,6 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
20602062
if meta.GetPartitionInfo() == nil {
20612063
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
20622064
}
2063-
// We don't support add hash type partition now.
2064-
if meta.Partition.Type == model.PartitionTypeHash {
2065-
return errors.Trace(ErrUnsupportedAddPartition)
2066-
}
20672065

20682066
partInfo, err := buildPartitionInfo(meta, d, spec)
20692067
if err != nil {
@@ -2115,20 +2113,27 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21152113
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
21162114
}
21172115

2116+
switch meta.Partition.Type {
21182117
// Coalesce partition can only be used on hash/key partitions.
2119-
if meta.Partition.Type == model.PartitionTypeRange {
2118+
default:
21202119
return errors.Trace(ErrCoalesceOnlyOnHashPartition)
2121-
}
21222120

21232121
// We don't support coalesce partitions hash type partition now.
2124-
if meta.Partition.Type == model.PartitionTypeHash {
2122+
case model.PartitionTypeHash:
21252123
return errors.Trace(ErrUnsupportedCoalescePartition)
2124+
2125+
case model.PartitionTypeKey:
21262126
}
21272127

21282128
return errors.Trace(err)
21292129
}
21302130

21312131
func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
2132+
// TODO: Support truncate multiple partitions
2133+
if len(spec.PartitionNames) != 1 {
2134+
return errRunMultiSchemaChanges
2135+
}
2136+
21322137
is := d.infoHandle.Get()
21332138
schema, ok := is.SchemaByName(ident.Schema)
21342139
if !ok {
@@ -2144,7 +2149,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21442149
}
21452150

21462151
var pid int64
2147-
pid, err = tables.FindPartitionByName(meta, spec.Name)
2152+
pid, err = tables.FindPartitionByName(meta, spec.PartitionNames[0].L)
21482153
if err != nil {
21492154
return errors.Trace(err)
21502155
}
@@ -2166,6 +2171,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21662171
}
21672172

21682173
func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
2174+
// TODO: Support drop multiple partitions
2175+
if len(spec.PartitionNames) != 1 {
2176+
return errRunMultiSchemaChanges
2177+
}
2178+
21692179
is := d.infoHandle.Get()
21702180
schema, ok := is.SchemaByName(ident.Schema)
21712181
if !ok {
@@ -2179,7 +2189,9 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21792189
if meta.GetPartitionInfo() == nil {
21802190
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
21812191
}
2182-
err = checkDropTablePartition(meta, spec.Name)
2192+
2193+
partName := spec.PartitionNames[0].L
2194+
err = checkDropTablePartition(meta, partName)
21832195
if err != nil {
21842196
return errors.Trace(err)
21852197
}
@@ -2189,7 +2201,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21892201
TableID: meta.ID,
21902202
Type: model.ActionDropTablePartition,
21912203
BinlogInfo: &model.HistoryInfo{},
2192-
Args: []interface{}{spec.Name},
2204+
Args: []interface{}{partName},
21932205
}
21942206

21952207
err = d.doDDLJob(ctx, job)
@@ -3147,9 +3159,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
31473159
}
31483160

31493161
func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec) (*model.PartitionInfo, error) {
3150-
if meta.Partition.Type == model.PartitionTypeRange && len(spec.PartDefinitions) == 0 {
3151-
return nil, errors.Trace(ErrPartitionsMustBeDefined)
3162+
if meta.Partition.Type == model.PartitionTypeRange {
3163+
if len(spec.PartDefinitions) == 0 {
3164+
return nil, ast.ErrPartitionsMustBeDefined.GenWithStackByArgs(meta.Partition.Type)
3165+
}
3166+
} else {
3167+
// we don't support ADD PARTITION for all other partition types yet.
3168+
return nil, errors.Trace(ErrUnsupportedAddPartition)
31523169
}
3170+
31533171
part := &model.PartitionInfo{
31543172
Type: meta.Partition.Type,
31553173
Expr: meta.Partition.Expr,
@@ -3158,7 +3176,12 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31583176
}
31593177
buf := new(bytes.Buffer)
31603178
for _, def := range spec.PartDefinitions {
3161-
for _, expr := range def.LessThan {
3179+
if err := def.Clause.Validate(part.Type, len(part.Columns)); err != nil {
3180+
return nil, errors.Trace(err)
3181+
}
3182+
// For RANGE partition only VALUES LESS THAN should be possible.
3183+
clause := def.Clause.(*ast.PartitionDefinitionClauseLessThan)
3184+
for _, expr := range clause.Exprs {
31623185
tp := expr.GetType().Tp
31633186
if len(part.Columns) == 0 {
31643187
// Partition by range.
@@ -3177,14 +3200,15 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31773200
if err1 != nil {
31783201
return nil, errors.Trace(err1)
31793202
}
3203+
comment, _ := def.Comment()
31803204
piDef := model.PartitionDefinition{
31813205
Name: def.Name,
31823206
ID: pid,
3183-
Comment: def.Comment,
3207+
Comment: comment,
31843208
}
31853209

31863210
buf := new(bytes.Buffer)
3187-
for _, expr := range def.LessThan {
3211+
for _, expr := range clause.Exprs {
31883212
expr.Format(buf)
31893213
piDef.LessThan = append(piDef.LessThan, buf.String())
31903214
buf.Reset()

0 commit comments

Comments
 (0)