Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl:support alter table drop partition #6460

Merged
merged 21 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ const (
AlterTableRenameIndex
AlterTableForce
AlterTableAddPartitions
AlterTableDropPartition

// TODO: Add more actions
)
Expand Down Expand Up @@ -877,7 +878,7 @@ func (n *TruncateTableStmt) Accept(v Visitor) (Node, bool) {

// PartitionDefinition defines a single partition.
type PartitionDefinition struct {
Name string
Name model.CIStr
LessThan []ExprNode
MaxValue bool
Comment string
Expand Down
215 changes: 178 additions & 37 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,11 +1570,11 @@ func (s *testDBSuite) TestCreateTableWithPartition(c *C) {
}
c.Assert(part.Definitions, HasLen, 3)
c.Assert(part.Definitions[0].LessThan[0], Equals, "10")
c.Assert(part.Definitions[0].Name, Equals, "p0")
c.Assert(part.Definitions[0].Name.L, Equals, "p0")
c.Assert(part.Definitions[1].LessThan[0], Equals, "20")
c.Assert(part.Definitions[1].Name, Equals, "p1")
c.Assert(part.Definitions[1].Name.L, Equals, "p1")
c.Assert(part.Definitions[2].LessThan[0], Equals, "MAXVALUE")
c.Assert(part.Definitions[2].Name, Equals, "p2")
c.Assert(part.Definitions[2].Name.L, Equals, "p2")

s.tk.MustExec("drop table if exists employees;")
sql1 := `create table employees (
Expand Down Expand Up @@ -2392,8 +2392,10 @@ func (s *testDBSuite) TestBackwardCompatibility(c *C) {
}

func (s *testDBSuite) TestAlterTableAddPartition(c *C) {
s.tk.MustExec("use test")
s.tk.MustExec("drop table if employees")
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1")
s.tk.MustExec("drop table if exists employees;")
s.tk.MustExec(`create table employees (
id int not null,
hired date not null
Expand All @@ -2405,43 +2407,44 @@ func (s *testDBSuite) TestAlterTableAddPartition(c *C) {
);`)
s.tk.MustExec(`alter table employees add partition (
partition p4 values less than (2010),
partition p5 values less than maxvalue
partition p5 values less than MAXVALUE
);`)

ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("tp"))
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("employees"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().Partition, NotNil)
part := tbl.Meta().Partition
c.Assert(part.Type, Equals, model.PartitionTypeRange)
c.Assert(part.Expr, Equals, "`hired`")

c.Assert(part.Expr, Equals, "year(`hired`)")
c.Assert(part.Definitions, HasLen, 5)
c.Assert(part.Definitions[0].LessThan[0], Equals, "1991")
c.Assert(part.Definitions[0].Name, Equals, "p1")
c.Assert(part.Definitions[0].Name, Equals, model.NewCIStr("p1"))
c.Assert(part.Definitions[1].LessThan[0], Equals, "1996")
c.Assert(part.Definitions[1].Name, Equals, "p2")
c.Assert(part.Definitions[1].Name, Equals, model.NewCIStr("p2"))
c.Assert(part.Definitions[2].LessThan[0], Equals, "2001")
c.Assert(part.Definitions[2].Name, Equals, "p3")
c.Assert(part.Definitions[2].Name, Equals, model.NewCIStr("p3"))
c.Assert(part.Definitions[3].LessThan[0], Equals, "2010")
c.Assert(part.Definitions[3].Name, Equals, "p4")
c.Assert(part.Definitions[4].LessThan[0], Equals, "maxvalue")
c.Assert(part.Definitions[4].Name, Equals, "p5")
c.Assert(part.Definitions[3].Name, Equals, model.NewCIStr("p4"))
c.Assert(part.Definitions[4].LessThan[0], Equals, "MAXVALUE")
c.Assert(part.Definitions[4].Name, Equals, model.NewCIStr("p5"))

s.tk.MustExec("drop table if t1")
s.tk.MustExec("create table t1(a int)")
sql1 := `alter table t1 add partition (
s.tk.MustExec("drop table if exists table1;")
s.tk.MustExec("create table table1(a int)")
sql1 := `alter table table1 add partition (
partition p1 values less than (2010),
partition p2 values less than maxvalue
);`
s.testErrorCode(c, sql1, mysql.ErrPartitionMgmtOnNonpartitioned)
s.testErrorCode(c, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned)

sql2 := "alter table t1 add partition"
s.testErrorCode(c, sql2, mysql.ErrPartitionsMustBeDefined)
sql2 := "alter table table1 add partition"
s.testErrorCode(c, sql2, tmysql.ErrPartitionsMustBeDefined)

s.tk.MustExec("drop table if exists table2;")
s.tk.MustExec(`create table table2 (

s.tk.MustExec("drop table if t2")
s.tk.MustExec(`create table t2 (
id int not null,
hired date not null
)
Expand All @@ -2450,41 +2453,179 @@ func (s *testDBSuite) TestAlterTableAddPartition(c *C) {
partition p2 values less than maxvalue
);`)

sql3 := `alter table t2 add partition (
sql3 := `alter table table2 add partition (
partition p3 values less than (2010)
);`
s.testErrorCode(c, sql3, mysql.ErrPartitionMaxvalue)
s.testErrorCode(c, sql3, tmysql.ErrPartitionMaxvalue)

s.tk.MustExec("drop table if t3")
s.tk.MustExec(`create table t3 (
s.tk.MustExec("drop table if exists table3;")
s.tk.MustExec(`create table table3 (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p3 values less than (2001)
partition p2 values less than (2001)
);`)

sql4 := `alter table t3 add partition (
sql4 := `alter table table3 add partition (
partition p3 values less than (1993)
);`
s.testErrorCode(c, sql4, mysql.ErrRangeNotIncreasing)
s.testErrorCode(c, sql4, tmysql.ErrRangeNotIncreasing)

sql5 := `alter table t3 add partition (
sql5 := `alter table table3 add partition (
partition p1 values less than (1993)
);`
s.testErrorCode(c, sql5, mysql.ErrSameNamePartition)
s.testErrorCode(c, sql5, tmysql.ErrSameNamePartition)

sql6 := `alter table t3 add partition (
sql6 := `alter table table3 add partition (
partition p1 values less than (1993),
partition p1 values less than (1995)
);`
s.testErrorCode(c, sql6, mysql.ErrSameNamePartition)
s.testErrorCode(c, sql6, tmysql.ErrSameNamePartition)

sql7 := `alter table t3 add partition (
sql7 := `alter table table3 add partition (
partition p4 values less than (1993),
partition p1 values less than (1995)
partition p5 values less than maxvalue,
partition p1 values less than (1995),
partition p5 values less than maxvalue
);`
s.testErrorCode(c, sql7, mysql.ErrSameNamePartition)
s.testErrorCode(c, sql7, tmysql.ErrSameNamePartition)
}

func (s *testDBSuite) TestAlterTableDropPartition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1")
s.tk.MustExec("drop table if exists employees")
s.tk.MustExec(`create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1991),
partition p2 values less than (1996),
partition p3 values less than (2001)
);`)

s.tk.MustExec("alter table employees drop partition p3;")
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("employees"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().GetPartitionInfo(), NotNil)
part := tbl.Meta().Partition
c.Assert(part.Type, Equals, model.PartitionTypeRange)
c.Assert(part.Expr, Equals, "`hired`")
c.Assert(part.Definitions, HasLen, 2)
c.Assert(part.Definitions[0].LessThan[0], Equals, "1991")
c.Assert(part.Definitions[0].Name, Equals, model.NewCIStr("p1"))
c.Assert(part.Definitions[1].LessThan[0], Equals, "1996")
c.Assert(part.Definitions[1].Name, Equals, model.NewCIStr("p2"))

s.tk.MustExec("drop table if exists table1;")
s.tk.MustExec("create table table1 (a int);")
sql1 := "alter table table1 drop partition p10;"
s.testErrorCode(c, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned)

s.tk.MustExec("drop table if exists table2;")
s.tk.MustExec(`create table table2 (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p2 values less than (1996),
partition p3 values less than (2001)
);`)
sql2 := "alter table table2 drop partition p10;"
s.testErrorCode(c, sql2, tmysql.ErrDropPartitionNonExistent)

s.tk.MustExec("drop table if exists table3;")
s.tk.MustExec(`create table table3 (
id int not null
)
partition by range( id ) (
partition p1 values less than (1991)
);`)
sql3 := "alter table table3 drop partition p1;"
s.testErrorCode(c, sql3, tmysql.ErrDropLastPartition)

s.tk.MustExec("drop table if exists table4;")
s.tk.MustExec(`create table table4 (
id int not null
)
partition by range( id ) (
partition p1 values less than (10),
partition p2 values less than (20),
partition p3 values less than MAXVALUE
);`)

s.tk.MustExec("alter table table4 drop partition p2;")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("table4"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().GetPartitionInfo(), NotNil)
part = tbl.Meta().Partition
c.Assert(part.Type, Equals, model.PartitionTypeRange)
c.Assert(part.Expr, Equals, "`id`")
c.Assert(part.Definitions, HasLen, 2)
c.Assert(part.Definitions[0].LessThan[0], Equals, "10")
c.Assert(part.Definitions[0].Name, Equals, model.NewCIStr("p1"))
c.Assert(part.Definitions[1].LessThan[0], Equals, "MAXVALUE")
c.Assert(part.Definitions[1].Name, Equals, model.NewCIStr("p3"))

s.tk.MustExec("drop table if exists tr;")
s.tk.MustExec(` create table tr(
id int, name varchar(50),
purchased date
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a key for purchased? And check the index rows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zimulala Need to wait for this PR #6814 to complete before adding it.

)
partition by range( year(purchased) ) (
partition p0 values less than (1990),
partition p1 values less than (1995),
partition p2 values less than (2000),
partition p3 values less than (2005),
partition p4 values less than (2010),
partition p5 values less than (2015)
);`)
s.tk.MustExec(`INSERT INTO tr VALUES
(1, 'desk organiser', '2003-10-15'),
(2, 'alarm clock', '1997-11-05'),
(3, 'chair', '2009-03-10'),
(4, 'bookcase', '1989-01-10'),
(5, 'exercise bike', '2014-05-09'),
(6, 'sofa', '1987-06-05'),
(7, 'espresso maker', '2011-11-22'),
(8, 'aquarium', '1992-08-04'),
(9, 'study desk', '2006-09-16'),
(10, 'lava lamp', '1998-12-25');`)
result := s.tk.MustQuery("select * from tr where purchased between '1995-01-01' and '1999-12-31';")
result.Check(testkit.Rows(`2 alarm clock 1997-11-05`, `10 lava lamp 1998-12-25`))
s.tk.MustExec("alter table tr drop partition p2;")
result = s.tk.MustQuery("select * from tr where purchased between '1995-01-01' and '1999-12-31';")
result.Check(testkit.Rows())

result = s.tk.MustQuery("select * from tr where purchased between '2010-01-01' and '2014-12-31';")
result.Check(testkit.Rows(`5 exercise bike 2014-05-09`, `7 espresso maker 2011-11-22`))
s.tk.MustExec("alter table tr drop partition p5;")
result = s.tk.MustQuery("select * from tr where purchased between '2010-01-01' and '2014-12-31';")
result.Check(testkit.Rows())

s.tk.MustExec("alter table tr drop partition p4;")
result = s.tk.MustQuery("select * from tr where purchased between '2005-01-01' and '2009-12-31';")
result.Check(testkit.Rows())

s.tk.MustExec("drop table if exists table4;")
s.tk.MustExec(`create table table4 (
id int not null
)
partition by range( id ) (
partition Par1 values less than (1991),
partition pAR2 values less than (1992),
partition Par3 values less than (1995),
partition PaR5 values less than (1996)
);`)
s.tk.MustExec("alter table table4 drop partition Par2;")
s.tk.MustExec("alter table table4 drop partition PAR5;")
sql4 := "alter table table4 drop partition PAR0;"
s.testErrorCode(c, sql4, tmysql.ErrDropPartitionNonExistent)
}
4 changes: 4 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ var (
ErrPartitionMaxvalue = terror.ClassDDL.New(codePartitionMaxvalue, "MAXVALUE can only be used in last partition definition")
// ErrTooManyValues returns cannot have more than one value for this type of partitioning.
ErrTooManyValues = terror.ClassDDL.New(codeErrTooManyValues, mysql.MySQLErrName[mysql.ErrTooManyValues])
//ErrDropLastPartition returns cannot remove all partitions, use drop table instead.
ErrDropLastPartition = terror.ClassDDL.New(codeDropLastPartition, mysql.MySQLErrName[mysql.ErrDropLastPartition])
)

// DDL is responsible for updating schema in data store and maintaining in-memory InfoSchema cache.
Expand Down Expand Up @@ -574,6 +576,7 @@ const (
codeRangeNotIncreasing = terror.ErrCode(mysql.ErrRangeNotIncreasing)
codePartitionMaxvalue = terror.ErrCode(mysql.ErrPartitionMaxvalue)
codeErrTooManyValues = terror.ErrCode(mysql.ErrTooManyValues)
codeDropLastPartition = terror.ErrCode(mysql.ErrDropLastPartition)
)

func init() {
Expand Down Expand Up @@ -613,6 +616,7 @@ func init() {
codeRangeNotIncreasing: mysql.ErrRangeNotIncreasing,
codePartitionMaxvalue: mysql.ErrPartitionMaxvalue,
codeErrTooManyValues: mysql.ErrTooManyValues,
codeDropLastPartition: mysql.ErrDropLastPartition,
}
terror.ErrClassToMySQLCodes[terror.ClassDDL] = ddlMySQLErrCodes
}
39 changes: 38 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,8 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
err = d.DropColumn(ctx, ident, spec.OldColumnName.Name)
case ast.AlterTableDropIndex:
err = d.DropIndex(ctx, ident, model.NewCIStr(spec.Name))
case ast.AlterTableDropPartition:
err = d.DropTablePartition(ctx, ident, spec)
case ast.AlterTableAddConstraint:
constr := spec.Constraint
switch spec.Constraint.Tp {
Expand Down Expand Up @@ -1211,7 +1213,7 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
}

meta := t.Meta()
if meta.GetPartitionInfo() == nil && meta.Partition == nil {
if meta.GetPartitionInfo() == nil {
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
}
partInfo, err := buildPartitionInfo(meta, d, spec)
Expand Down Expand Up @@ -1242,6 +1244,41 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
return errors.Trace(err)
}

func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
is := d.infoHandle.Get()
schema, ok := is.SchemaByName(ident.Schema)
if !ok {
return errors.Trace(infoschema.ErrDatabaseNotExists.GenByArgs(schema))
}
t, err := is.TableByName(ident.Schema, ident.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ident.Schema, ident.Name))
}
meta := t.Meta()
if meta.GetPartitionInfo() == nil {
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
}
err = checkDropTablePartition(meta, spec.Name)
if err != nil {
return errors.Trace(err)
}

job := &model.Job{
SchemaID: schema.ID,
TableID: meta.ID,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the tableID or partitionID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the TableID.

Type: model.ActionDropTablePartition,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{spec.Name},
}

err = d.doDDLJob(ctx, job)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if err != nil {
return errors.Trace(err)
}
err = d.callHookOnChanged(err)
return errors.Trace(err)
}

// DropColumn will drop a column from the table, now we don't support drop the column with index covered.
func (d *ddl) DropColumn(ctx sessionctx.Context, ti ast.Ident, colName model.CIStr) error {
is := d.GetInformationSchema(ctx)
Expand Down
Loading