Skip to content

Commit

Permalink
ddl: fix create partition table error under NO_UNSIGNED_SUBTRACTION (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Jun 18, 2021
1 parent 47aea09 commit 47b470d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
14 changes: 8 additions & 6 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,15 +1594,17 @@ func checkTableInfoValidWithStmt(ctx sessionctx.Context, tbInfo *model.TableInfo
if err := checkGeneratedColumn(s.Cols); err != nil {
return errors.Trace(err)
}
if tbInfo.Partition != nil && s.Partition != nil {
if tbInfo.Partition != nil {
if err := checkPartitionDefinitionConstraints(ctx, tbInfo); err != nil {
return errors.Trace(err)
}
if err := checkPartitionFuncType(ctx, s.Partition.Expr, tbInfo); err != nil {
return errors.Trace(err)
}
if err := checkPartitioningKeysConstraints(ctx, s, tbInfo); err != nil {
return errors.Trace(err)
if s.Partition != nil {
if err := checkPartitionFuncType(ctx, s.Partition.Expr, tbInfo); err != nil {
return errors.Trace(err)
}
if err := checkPartitioningKeysConstraints(ctx, s, tbInfo); err != nil {
return errors.Trace(err)
}
}
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func checkPartitionValuesIsInt(ctx sessionctx.Context, def *ast.PartitionDefinit
switch val.Kind() {
case types.KindUint64, types.KindNull:
case types.KindInt64:
if mysql.HasUnsignedFlag(tp.Flag) && val.GetInt64() < 0 {
if !ctx.GetSessionVars().SQLMode.HasNoUnsignedSubtractionMode() && mysql.HasUnsignedFlag(tp.Flag) && val.GetInt64() < 0 {
return ErrPartitionConstDomain.GenWithStackByArgs()
}
default:
Expand Down Expand Up @@ -666,7 +666,8 @@ func checkRangePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)
if strings.EqualFold(defs[len(defs)-1].LessThan[0], partitionMaxValue) {
defs = defs[:len(defs)-1]
}
isUnsigned := isColUnsigned(cols, pi)
// treat partition value under NoUnsignedSubtractionMode as signed
isUnsigned := isColUnsigned(cols, pi) && !ctx.GetSessionVars().SQLMode.HasNoUnsignedSubtractionMode()
var prevRangeValue interface{}
for i := 0; i < len(defs); i++ {
if strings.EqualFold(defs[i].LessThan[0], partitionMaxValue) {
Expand Down
37 changes: 37 additions & 0 deletions table/tables/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,43 @@ func (ts *testSuite) TestCreatePartitionTableNotSupport(c *C) {
c.Assert(ddl.ErrPartitionFunctionIsNotAllowed.Equal(err), IsTrue)
}

// issue 24880
func (ts *testSuite) TestRangePartitionUnderNoUnsignedSub(c *C) {
tk := testkit.NewTestKitWithInit(c, ts.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists tu;")
tk.MustExec("SET @@sql_mode='NO_UNSIGNED_SUBTRACTION';")
tk.MustExec(`CREATE TABLE tu (c1 BIGINT UNSIGNED) PARTITION BY RANGE(c1 - 10) (
PARTITION p0 VALUES LESS THAN (-5),
PARTITION p1 VALUES LESS THAN (0),
PARTITION p2 VALUES LESS THAN (5),
PARTITION p3 VALUES LESS THAN (10),
PARTITION p4 VALUES LESS THAN (MAXVALUE)
);`)
// currently not support insert records whose partition value is negative
ErrMsg1 := "[types:1690]BIGINT UNSIGNED value is out of range in '(tu.c1 - 10)'"
tk.MustGetErrMsg("insert into tu values (0);", ErrMsg1)
tk.MustGetErrMsg("insert into tu values (cast(1 as unsigned));", ErrMsg1)
tk.MustExec(("insert into tu values (cast(9223372036854775807 as unsigned));"))
// MySQL will not support c1 value bigger than 9223372036854775817 in this case
tk.MustExec(("insert into tu values (cast(18446744073709551615 as unsigned));"))

// test `create table like`
ErrMsg2 := "[types:1690]BIGINT UNSIGNED value is out of range in '(tu2.c1 - 10)'"
tk.MustExec(`CREATE TABLE tu2 like tu;`)
// currently not support insert records whose partition value is negative
tk.MustGetErrMsg("insert into tu2 values (0);", ErrMsg2)
tk.MustGetErrMsg("insert into tu2 values (cast(1 as unsigned));", ErrMsg2)
tk.MustExec(("insert into tu2 values (cast(9223372036854775807 as unsigned));"))
// MySQL will not support c1 value bigger than 9223372036854775817 in this case
tk.MustExec(("insert into tu2 values (cast(18446744073709551615 as unsigned));"))

// compatible with MySQL
ErrMsg3 := "[ddl:1493]VALUES LESS THAN value must be strictly increasing for each partition"
tk.MustExec("SET @@sql_mode='';")
tk.MustGetErrMsg(`CREATE TABLE tu3 like tu;`, ErrMsg3)
}

func (ts *testSuite) TestIntUint(c *C) {
tk := testkit.NewTestKitWithInit(c, ts.store)
tk.MustExec("use test")
Expand Down

0 comments on commit 47b470d

Please sign in to comment.