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

Revert "ddl: fix create partition table error under NO_UNSIGNED_SUBTRACTION" (#26935) #27053

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 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 !ctx.GetSessionVars().SQLMode.HasNoUnsignedSubtractionMode() && mysql.HasUnsignedFlag(tp.Flag) && val.GetInt64() < 0 {
if mysql.HasUnsignedFlag(tp.Flag) && val.GetInt64() < 0 {
return ErrPartitionConstDomain.GenWithStackByArgs()
}
default:
Expand Down Expand Up @@ -666,8 +666,7 @@ func checkRangePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)
if strings.EqualFold(defs[len(defs)-1].LessThan[0], partitionMaxValue) {
defs = defs[:len(defs)-1]
}
// treat partition value under NoUnsignedSubtractionMode as signed
isUnsigned := isColUnsigned(cols, pi) && !ctx.GetSessionVars().SQLMode.HasNoUnsignedSubtractionMode()
isUnsigned := isColUnsigned(cols, pi)
var prevRangeValue interface{}
for i := 0; i < len(defs); i++ {
if strings.EqualFold(defs[i].LessThan[0], partitionMaxValue) {
Expand Down
49 changes: 18 additions & 31 deletions table/tables/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/ddl"
mysql "github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx/binloginfo"
"github.com/pingcap/tidb/table"
Expand Down Expand Up @@ -447,41 +448,27 @@ func (ts *testSuite) TestCreatePartitionTableNotSupport(c *C) {
c.Assert(ddl.ErrPartitionFunctionIsNotAllowed.Equal(err), IsTrue)
}

// issue 24880
func (ts *testSuite) TestRangePartitionUnderNoUnsignedSub(c *C) {
func (ts *testSuite) TestRangePartitionUnderNoUnsigned(c *C) {
tk := testkit.NewTestKitWithInit(c, ts.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t2;")
tk.MustExec("drop table if exists tu;")
defer tk.MustExec("drop table if exists t2;")
defer 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)
tk.MustExec(`create table t2 (a bigint unsigned) partition by range (a) (
partition p1 values less than (0),
partition p2 values less than (1),
partition p3 values less than (18446744073709551614),
partition p4 values less than (18446744073709551615),
partition p5 values less than maxvalue);`)
tk.MustExec("insert into t2 values(10);")
tk.MustGetErrCode(`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));`, mysql.ErrPartitionConstDomain)
}

func (ts *testSuite) TestIntUint(c *C) {
Expand Down