Skip to content

Commit fb7fd47

Browse files
authored
ddl: error or skip unsupported partition-related DDLs (#10672)
* ddl: error or skip unsupported partition-related DDLs * go.mod: stop replacing parser since the PR is merged * ddl: addressed comment
1 parent 84f5148 commit fb7fd47

File tree

7 files changed

+141
-66
lines changed

7 files changed

+141
-66
lines changed

ddl/db_integration_test.go

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

14571457
// partition.
1458-
s.assertAlterWarnExec(c, "alter table t truncate partition p1, ALGORITHM=COPY")
1459-
s.assertAlterErrorExec(c, "alter table t truncate partition p2, ALGORITHM=INPLACE")
1460-
s.tk.MustExec("alter table t truncate partition p3, ALGORITHM=INSTANT")
1458+
s.assertAlterWarnExec(c, "alter table t ALGORITHM=COPY, truncate partition p1")
1459+
s.assertAlterErrorExec(c, "alter table t ALGORITHM=INPLACE, truncate partition p2")
1460+
s.tk.MustExec("alter table t ALGORITHM=INSTANT, truncate partition p3")
14611461

14621462
s.assertAlterWarnExec(c, "alter table t add partition (partition p4 values less than (2002)), ALGORITHM=COPY")
14631463
s.assertAlterErrorExec(c, "alter table t add partition (partition p5 values less than (3002)), ALGORITHM=INPLACE")
14641464
s.tk.MustExec("alter table t add partition (partition p6 values less than (4002)), ALGORITHM=INSTANT")
14651465

1466-
s.assertAlterWarnExec(c, "alter table t drop partition p4, ALGORITHM=COPY")
1467-
s.assertAlterErrorExec(c, "alter table t drop partition p5, ALGORITHM=INPLACE")
1468-
s.tk.MustExec("alter table t drop partition p6, ALGORITHM=INSTANT")
1466+
s.assertAlterWarnExec(c, "alter table t ALGORITHM=COPY, drop partition p4")
1467+
s.assertAlterErrorExec(c, "alter table t ALGORITHM=INPLACE, drop partition p5")
1468+
s.tk.MustExec("alter table t ALGORITHM=INSTANT, drop partition p6")
14691469

14701470
// Table options
14711471
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: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,12 +1510,12 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15101510
// Range columns partition key supports multiple data types with integer、datetime、string.
15111511
defs := pi.Definitions
15121512
if len(defs) < 1 {
1513-
return errors.Trace(ErrPartitionsMustBeDefined)
1513+
return ast.ErrPartitionsMustBeDefined.GenWithStackByArgs("RANGE")
15141514
}
15151515

15161516
curr := &defs[0]
15171517
if len(curr.LessThan) != len(pi.Columns) {
1518-
return errors.Trace(ErrPartitionColumnList)
1518+
return errors.Trace(ast.ErrPartitionColumnList)
15191519
}
15201520
for i := 1; i < len(defs); i++ {
15211521
prev, curr := curr, &defs[i]
@@ -1532,7 +1532,7 @@ func checkRangeColumnsPartitionValue(ctx sessionctx.Context, tbInfo *model.Table
15321532

15331533
func checkTwoRangeColumns(ctx sessionctx.Context, curr, prev *model.PartitionDefinition, pi *model.PartitionInfo, tbInfo *model.TableInfo) (bool, error) {
15341534
if len(curr.LessThan) != len(pi.Columns) {
1535-
return false, errors.Trace(ErrPartitionColumnList)
1535+
return false, errors.Trace(ast.ErrPartitionColumnList)
15361536
}
15371537
for i := 0; i < len(pi.Columns); i++ {
15381538
// Special handling for MAXVALUE.
@@ -1740,8 +1740,7 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec)
17401740
validSpecs = append(validSpecs, spec)
17411741
}
17421742

1743-
if len(validSpecs) != 1 {
1744-
// TODO: Hanlde len(validSpecs) == 0.
1743+
if len(validSpecs) > 1 {
17451744
// Now we only allow one schema changing at the same time.
17461745
return nil, errRunMultiSchemaChanges
17471746
}
@@ -1828,6 +1827,9 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
18281827
err = ErrUnsupportedModifyPrimaryKey.GenWithStackByArgs("drop")
18291828
case ast.AlterTableRenameIndex:
18301829
err = d.RenameIndex(ctx, ident, spec)
1830+
case ast.AlterTablePartition:
1831+
// Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
1832+
err = errors.New("alter table partition is unsupported")
18311833
case ast.AlterTableOption:
18321834
for i, opt := range spec.Options {
18331835
switch opt.Tp {
@@ -2053,10 +2055,6 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
20532055
if meta.GetPartitionInfo() == nil {
20542056
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
20552057
}
2056-
// We don't support add hash type partition now.
2057-
if meta.Partition.Type == model.PartitionTypeHash {
2058-
return errors.Trace(ErrUnsupportedAddPartition)
2059-
}
20602058

20612059
partInfo, err := buildPartitionInfo(meta, d, spec)
20622060
if err != nil {
@@ -2108,20 +2106,28 @@ func (d *ddl) CoalescePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
21082106
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
21092107
}
21102108

2111-
// Coalesce partition can only be used on hash/key partitions.
2112-
if meta.Partition.Type == model.PartitionTypeRange {
2113-
return errors.Trace(ErrCoalesceOnlyOnHashPartition)
2114-
}
2115-
2109+
switch meta.Partition.Type {
21162110
// We don't support coalesce partitions hash type partition now.
2117-
if meta.Partition.Type == model.PartitionTypeHash {
2111+
case model.PartitionTypeHash:
21182112
return errors.Trace(ErrUnsupportedCoalescePartition)
2113+
2114+
// Key type partition cannot be constructed currently, ignoring it for now.
2115+
case model.PartitionTypeKey:
2116+
2117+
// Coalesce partition can only be used on hash/key partitions.
2118+
default:
2119+
return errors.Trace(ErrCoalesceOnlyOnHashPartition)
21192120
}
21202121

21212122
return errors.Trace(err)
21222123
}
21232124

21242125
func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
2126+
// TODO: Support truncate multiple partitions
2127+
if len(spec.PartitionNames) != 1 {
2128+
return errRunMultiSchemaChanges
2129+
}
2130+
21252131
is := d.infoHandle.Get()
21262132
schema, ok := is.SchemaByName(ident.Schema)
21272133
if !ok {
@@ -2137,7 +2143,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21372143
}
21382144

21392145
var pid int64
2140-
pid, err = tables.FindPartitionByName(meta, spec.Name)
2146+
pid, err = tables.FindPartitionByName(meta, spec.PartitionNames[0].L)
21412147
if err != nil {
21422148
return errors.Trace(err)
21432149
}
@@ -2159,6 +2165,11 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp
21592165
}
21602166

21612167
func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
2168+
// TODO: Support drop multiple partitions
2169+
if len(spec.PartitionNames) != 1 {
2170+
return errRunMultiSchemaChanges
2171+
}
2172+
21622173
is := d.infoHandle.Get()
21632174
schema, ok := is.SchemaByName(ident.Schema)
21642175
if !ok {
@@ -2172,7 +2183,9 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21722183
if meta.GetPartitionInfo() == nil {
21732184
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
21742185
}
2175-
err = checkDropTablePartition(meta, spec.Name)
2186+
2187+
partName := spec.PartitionNames[0].L
2188+
err = checkDropTablePartition(meta, partName)
21762189
if err != nil {
21772190
return errors.Trace(err)
21782191
}
@@ -2182,7 +2195,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *
21822195
TableID: meta.ID,
21832196
Type: model.ActionDropTablePartition,
21842197
BinlogInfo: &model.HistoryInfo{},
2185-
Args: []interface{}{spec.Name},
2198+
Args: []interface{}{partName},
21862199
}
21872200

21882201
err = d.doDDLJob(ctx, job)
@@ -3140,9 +3153,15 @@ func validateCommentLength(vars *variable.SessionVars, comment string, maxLen in
31403153
}
31413154

31423155
func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec) (*model.PartitionInfo, error) {
3143-
if meta.Partition.Type == model.PartitionTypeRange && len(spec.PartDefinitions) == 0 {
3144-
return nil, errors.Trace(ErrPartitionsMustBeDefined)
3156+
if meta.Partition.Type == model.PartitionTypeRange {
3157+
if len(spec.PartDefinitions) == 0 {
3158+
return nil, ast.ErrPartitionsMustBeDefined.GenWithStackByArgs(meta.Partition.Type)
3159+
}
3160+
} else {
3161+
// we don't support ADD PARTITION for all other partition types yet.
3162+
return nil, errors.Trace(ErrUnsupportedAddPartition)
31453163
}
3164+
31463165
part := &model.PartitionInfo{
31473166
Type: meta.Partition.Type,
31483167
Expr: meta.Partition.Expr,
@@ -3151,7 +3170,12 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31513170
}
31523171
buf := new(bytes.Buffer)
31533172
for _, def := range spec.PartDefinitions {
3154-
for _, expr := range def.LessThan {
3173+
if err := def.Clause.Validate(part.Type, len(part.Columns)); err != nil {
3174+
return nil, errors.Trace(err)
3175+
}
3176+
// For RANGE partition only VALUES LESS THAN should be possible.
3177+
clause := def.Clause.(*ast.PartitionDefinitionClauseLessThan)
3178+
for _, expr := range clause.Exprs {
31553179
tp := expr.GetType().Tp
31563180
if len(part.Columns) == 0 {
31573181
// Partition by range.
@@ -3170,14 +3194,15 @@ func buildPartitionInfo(meta *model.TableInfo, d *ddl, spec *ast.AlterTableSpec)
31703194
if err1 != nil {
31713195
return nil, errors.Trace(err1)
31723196
}
3197+
comment, _ := def.Comment()
31733198
piDef := model.PartitionDefinition{
31743199
Name: def.Name,
31753200
ID: pid,
3176-
Comment: def.Comment,
3201+
Comment: comment,
31773202
}
31783203

31793204
buf := new(bytes.Buffer)
3180-
for _, expr := range def.LessThan {
3205+
for _, expr := range clause.Exprs {
31813206
expr.Format(buf)
31823207
piDef.LessThan = append(piDef.LessThan, buf.String())
31833208
buf.Reset()

0 commit comments

Comments
 (0)