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

partition: check partition definition with unsigned option #23428

Merged
merged 6 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 16 additions & 1 deletion ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,19 @@ create table log_message_1 (
},
{
"create table t1 (a bigint unsigned) partition by list (a) (partition p0 values in (10, 20, 30, -1));",
ddl.ErrWrongTypeColumnValue,
ddl.ErrPartitionConstDomain,
},
{
"create table t1 (a bigint unsigned) partition by range (a) (partition p0 values less than (-1));",
ddl.ErrPartitionConstDomain,
},
{
"create table t1 (a int unsigned) partition by range (a) (partition p0 values less than (-1));",
ddl.ErrPartitionConstDomain,
},
{
"create table t1 (a tinyint(20) unsigned) partition by range (a) (partition p0 values less than (-1));",
ddl.ErrPartitionConstDomain,
},
{
"CREATE TABLE new (a TIMESTAMP NOT NULL PRIMARY KEY) PARTITION BY RANGE (a % 2) (PARTITION p VALUES LESS THAN (20080819));",
Expand Down Expand Up @@ -568,6 +580,9 @@ create table log_message_1 (
tk.MustExec(`create table t(a int) partition by range columns (a) (
partition p0 values less than (10),
partition p1 values less than (20));`)

tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t(a int) partition by range (a) (partition p0 values less than (18446744073709551615));`)
}

func (s *testIntegrationSuite1) TestDisableTablePartition(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ var (
ErrDropLastPartition = dbterror.ClassDDL.NewStd(mysql.ErrDropLastPartition)
// ErrTooManyPartitions returns too many partitions were defined.
ErrTooManyPartitions = dbterror.ClassDDL.NewStd(mysql.ErrTooManyPartitions)
// ErrPartitionConstDomain returns partition constant is out of partition function domain
xiongjiwei marked this conversation as resolved.
Show resolved Hide resolved
ErrPartitionConstDomain = dbterror.ClassDDL.NewStd(mysql.ErrPartitionConstDomain)
// ErrPartitionFunctionIsNotAllowed returns this partition function is not allowed.
ErrPartitionFunctionIsNotAllowed = dbterror.ClassDDL.NewStd(mysql.ErrPartitionFunctionIsNotAllowed)
// ErrPartitionFuncNotAllowed returns partition function returns the wrong type.
Expand Down
29 changes: 17 additions & 12 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func buildRangePartitionDefinitions(ctx sessionctx.Context, defs []*ast.Partitio

func checkPartitionValuesIsInt(ctx sessionctx.Context, def *ast.PartitionDefinition, exprs []ast.ExprNode, tbInfo *model.TableInfo) error {
tp := types.NewFieldType(mysql.TypeLonglong)
if isRangePartitionColUnsignedBigint(tbInfo.Columns, tbInfo.Partition) {
if isColUnsigned(tbInfo.Columns, tbInfo.Partition) {
tp.Flag |= mysql.UnsignedFlag
}
for _, exp := range exprs {
Expand All @@ -505,12 +505,17 @@ func checkPartitionValuesIsInt(ctx sessionctx.Context, def *ast.PartitionDefinit
return err
}
switch val.Kind() {
case types.KindInt64, types.KindUint64, types.KindNull:
case types.KindUint64, types.KindNull:
case types.KindInt64:
if mysql.HasUnsignedFlag(tp.Flag) && val.GetInt64() < 0 {
return ErrPartitionConstDomain.GenWithStackByArgs()
}
default:
return ErrValuesIsNotIntType.GenWithStackByArgs(def.Name)
}

_, err = val.ConvertTo(ctx.GetSessionVars().StmtCtx, tp)
if err != nil {
if err != nil && !types.ErrOverflow.Equal(err) {
return ErrWrongTypeColumnValue.GenWithStackByArgs()
}
}
Expand Down Expand Up @@ -644,14 +649,14 @@ func checkRangePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)
if strings.EqualFold(defs[len(defs)-1].LessThan[0], partitionMaxValue) {
defs = defs[:len(defs)-1]
}
isUnsignedBigint := isRangePartitionColUnsignedBigint(cols, pi)
isUnsigned := isColUnsigned(cols, pi)
var prevRangeValue interface{}
for i := 0; i < len(defs); i++ {
if strings.EqualFold(defs[i].LessThan[0], partitionMaxValue) {
return errors.Trace(ErrPartitionMaxvalue)
}

currentRangeValue, fromExpr, err := getRangeValue(ctx, defs[i].LessThan[0], isUnsignedBigint)
currentRangeValue, fromExpr, err := getRangeValue(ctx, defs[i].LessThan[0], isUnsigned)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -665,7 +670,7 @@ func checkRangePartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)
continue
}

if isUnsignedBigint {
if isUnsigned {
if currentRangeValue.(uint64) <= prevRangeValue.(uint64) {
return errors.Trace(ErrRangeNotIncreasing)
}
Expand Down Expand Up @@ -707,7 +712,7 @@ func formatListPartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)
cols := make([]*model.ColumnInfo, 0, len(pi.Columns))
if len(pi.Columns) == 0 {
tp := types.NewFieldType(mysql.TypeLonglong)
if isRangePartitionColUnsignedBigint(tblInfo.Columns, tblInfo.Partition) {
if isColUnsigned(tblInfo.Columns, tblInfo.Partition) {
tp.Flag |= mysql.UnsignedFlag
}
colTps = []*types.FieldType{tp}
Expand Down Expand Up @@ -758,9 +763,9 @@ func formatListPartitionValue(ctx sessionctx.Context, tblInfo *model.TableInfo)

// getRangeValue gets an integer from the range value string.
// The returned boolean value indicates whether the input string is a constant expression.
func getRangeValue(ctx sessionctx.Context, str string, unsignedBigint bool) (interface{}, bool, error) {
func getRangeValue(ctx sessionctx.Context, str string, unsigned bool) (interface{}, bool, error) {
// Unsigned bigint was converted to uint64 handle.
if unsignedBigint {
if unsigned {
if value, err := strconv.ParseUint(str, 10, 64); err == nil {
return value, false, nil
}
Expand Down Expand Up @@ -1649,10 +1654,10 @@ func (cns columnNameSlice) At(i int) string {
return cns[i].Name.L
}

// isRangePartitionColUnsignedBigint returns true if the partitioning key column type is unsigned bigint type.
func isRangePartitionColUnsignedBigint(cols []*model.ColumnInfo, pi *model.PartitionInfo) bool {
// isColUnsigned returns true if the partitioning key column type is unsigned bigint type.
xiongjiwei marked this conversation as resolved.
Show resolved Hide resolved
func isColUnsigned(cols []*model.ColumnInfo, pi *model.PartitionInfo) bool {
for _, col := range cols {
isUnsigned := col.Tp == mysql.TypeLonglong && mysql.HasUnsignedFlag(col.Flag)
isUnsigned := mysql.HasUnsignedFlag(col.Flag)
if isUnsigned && strings.Contains(strings.ToLower(pi.Expr), col.Name.L) {
return true
}
Expand Down
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ error = '''
Duplicate partition name %-.192s
'''

["ddl:1563"]
error = '''
Partition constant is out of partition function domain
'''

["ddl:1564"]
error = '''
This partition function is not allowed
Expand Down