Skip to content

Commit

Permalink
add test for expression index
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaox1n committed May 16, 2020
1 parent 40db78a commit c559f12
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
43 changes: 39 additions & 4 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ func (s *testIntegrationSuite5) TestAlterTableDropPartition(c *C) {
tk.MustGetErrCode("alter table t1 drop partition p2", tmysql.ErrOnlyOnRangeListPartition)
}

func (s *testIntegrationSuite5) TestAlterTableExchangePartition(c *C) {
func (s *testIntegrationSuite4) TestAlterTableExchangePartition(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists e")
Expand All @@ -792,7 +792,7 @@ func (s *testIntegrationSuite5) TestAlterTableExchangePartition(c *C) {
tk.MustExec("ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2")
tk.MustQuery("select * from e2").Check(testkit.Rows("16"))
tk.MustQuery("select * from e").Check(testkit.Rows("1669", "337", "2005"))
// validation check for range partition
// validation test for range partition
tk.MustGetErrCode("ALTER TABLE e EXCHANGE PARTITION p1 WITH TABLE e2", tmysql.ErrRowDoesNotMatchPartition)
tk.MustGetErrCode("ALTER TABLE e EXCHANGE PARTITION p2 WITH TABLE e2", tmysql.ErrRowDoesNotMatchPartition)
tk.MustGetErrCode("ALTER TABLE e EXCHANGE PARTITION p3 WITH TABLE e2", tmysql.ErrRowDoesNotMatchPartition)
Expand All @@ -811,19 +811,22 @@ func (s *testIntegrationSuite5) TestAlterTableExchangePartition(c *C) {
tk.MustQuery("select * from e3 partition(p0)").Check(testkit.Rows())
tk.MustQuery("select * from e2").Check(testkit.Rows("1", "5"))

// check validation fot hash partition
// validation test fot hash partition
tk.MustGetErrCode("ALTER TABLE e3 EXCHANGE PARTITION p0 WITH TABLE e2", tmysql.ErrRowDoesNotMatchPartition)
tk.MustGetErrCode("ALTER TABLE e3 EXCHANGE PARTITION p2 WITH TABLE e2", tmysql.ErrRowDoesNotMatchPartition)
tk.MustGetErrCode("ALTER TABLE e3 EXCHANGE PARTITION p3 WITH TABLE e2", tmysql.ErrRowDoesNotMatchPartition)

// without validation test
tk.MustExec("ALTER TABLE e3 EXCHANGE PARTITION p0 with TABLE e2 WITHOUT VALIDATION")

tk.MustQuery("select * from e3 partition(p0)").Check(testkit.Rows("1", "5"))
tk.MustQuery("select * from e2").Check(testkit.Rows())

// expression index test for hash partition

}

func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {

type testCase struct {
ptSQL string
ntSQL string
Expand Down Expand Up @@ -991,6 +994,12 @@ func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {
"alter table pt25 exchange partition p0 with table nt25;",
nil,
},
{
"create table pt26 (id int not null, lname varchar(30), fname varchar(100) generated always as (concat(lname, ' ')) virtual) partition by hash(id) partitions 1;",
"create table nt26 (id int not null, lname varchar(30), fname varchar(100) generated always as (concat(id, ' ')) virtual);",
"alter table pt26 exchange partition p0 with table nt26;",
nil,
},
}

tk := testkit.NewTestKit(c, s.store)
Expand All @@ -1016,6 +1025,32 @@ func (s *testIntegrationSuite4) TestExchangePartitionTableCompatiable(c *C) {

}

func (s *testIntegrationSuite7) TestExchangePartitionExpressIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists pt1;")
tk.MustExec("create table pt1(a int, b int, c int) PARTITION BY hash (a) partitions 1;")
tk.MustExec("alter table pt1 add index idx((a+c));")

tk.MustExec("drop table if exists nt1;")
tk.MustExec("create table nt1(a int, b int, c int);")
tk.MustGetErrCode("alter table pt1 exchange partition p0 with table nt1;", tmysql.ErrTablesDifferentMetadata)

tk.MustExec("alter table nt1 add column (`_V$_idx_0` bigint(20) generated always as (a+b) virtual);")
tk.MustGetErrCode("alter table pt1 exchange partition p0 with table nt1;", tmysql.ErrTablesDifferentMetadata)

// test different expression index when expression return same field type
tk.MustExec("alter table nt1 drop column `_V$_idx_0`;")
tk.MustExec("alter table nt1 add index idx((b-c));")
tk.MustExec("alter table pt1 exchange partition p0 with table nt1;")

// test different expression index when expression return different field type
tk.MustExec("alter table nt1 drop index idx;")
tk.MustExec("alter table nt1 add index idx((concat(a, b)));")
tk.MustGetErrCode("alter table pt1 exchange partition p0 with table nt1;", tmysql.ErrTablesDifferentMetadata)

}

func (s *testIntegrationSuite4) TestAddPartitionTooManyPartitions(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
1 change: 1 addition & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,7 @@ func checkTableDefCompatible(source *model.TableInfo, target *model.TableInfo) e
return ErrUnsupportedOnGeneratedColumn.GenWithStackByArgs("Exchanging partitions for non-generated columns")
}
if sourceCol.Name.L != targetCol.Name.L ||
sourceCol.Hidden != targetCol.Hidden ||
!checkFielTypeCompatible(&sourceCol.FieldType, &targetCol.FieldType) {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,9 @@ func checkExchangePartitionRecordValidation(w *worker, pi *model.PartitionInfo,

switch pi.Type {
case model.PartitionTypeHash:
if pi.Num == 1 {
return nil
}
sql = fmt.Sprintf("select 1 from `%s`.`%s` where mod(%s, %d) != %d limit 1", schemaName.L, tableName.L, pi.Expr, pi.Num, index)
break
case model.PartitionTypeRange:
Expand Down

0 comments on commit c559f12

Please sign in to comment.