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: Fix partition value type check (#21016) #21136

Merged
merged 4 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
91 changes: 91 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1883,3 +1883,94 @@ func (s *testIntegrationSuite3) TestCommitWhenSchemaChange(c *C) {
tk.MustExec("admin check table schema_change")
tk.MustQuery("select * from schema_change").Check(testkit.Rows())
}

func (s *testIntegrationSuite7) TestCreatePartitionTableWithWrongType(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
var err error
_, err = tk.Exec(`create table t(
b int(10)
) partition by range columns (b) (
partition p0 values less than (0x10),
partition p3 values less than (0x20)
)`)
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec(`create table t(
b int(10)
) partition by range columns (b) (
partition p0 values less than ('g'),
partition p3 values less than ('k')
)`)
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec(`create table t(
b char(10)
) partition by range columns (b) (
partition p0 values less than (30),
partition p3 values less than (60)
)`)
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec(`create table t(
b datetime
) partition by range columns (b) (
partition p0 values less than ('g'),
partition p3 values less than ('m')
)`)
c.Assert(err, NotNil)
}

func (s *testIntegrationSuite7) TestAddPartitionForTableWithWrongType(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop tables if exists t_int, t_char, t_date")
tk.MustExec(`create table t_int(b int(10))
partition by range columns (b) (
partition p0 values less than (10)
)`)

tk.MustExec(`create table t_char(b char(10))
partition by range columns (b) (
partition p0 values less than ('a')
)`)

tk.MustExec(`create table t_date(b datetime)
partition by range columns (b) (
partition p0 values less than ('2020-09-01')
)`)

var err error

_, err = tk.Exec("alter table t_int add partition (partition p1 values less than ('g'))")
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec("alter table t_int add partition (partition p1 values less than (0x20))")
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec("alter table t_char add partition (partition p1 values less than (0x20))")
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec("alter table t_char add partition (partition p1 values less than (10))")
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec("alter table t_date add partition (partition p1 values less than ('m'))")
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec("alter table t_date add partition (partition p1 values less than (0x20))")
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)

_, err = tk.Exec("alter table t_date add partition (partition p1 values less than (20))")
c.Assert(err, NotNil)
c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)
}
24 changes: 23 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4346,10 +4346,32 @@ func checkRangeColumnsTypeAndValuesMatch(ctx sessionctx.Context, meta *model.Tab
}

// Check val.ConvertTo(colType) doesn't work, so we need this case by case check.
vkind := val.Kind()
switch colType.Tp {
case mysql.TypeDate, mysql.TypeDatetime:
switch val.Kind() {
switch vkind {
case types.KindString, types.KindBytes:
if _, err := val.ConvertTo(ctx.GetSessionVars().StmtCtx, colType); err != nil {
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
default:
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
switch vkind {
case types.KindInt64, types.KindUint64, types.KindNull:
default:
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
case mysql.TypeFloat, mysql.TypeDouble:
switch vkind {
case types.KindFloat32, types.KindFloat64, types.KindNull:
default:
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
case mysql.TypeString, mysql.TypeVarString:
switch vkind {
case types.KindString, types.KindBytes, types.KindNull:
default:
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
Expand Down