Skip to content

Commit

Permalink
ddl, partition: fix create strictly increasing range partition (pingc…
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 authored Jul 24, 2024
1 parent 47179ae commit a251dad
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
42 changes: 33 additions & 9 deletions pkg/ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3474,37 +3474,61 @@ func checkTwoRangeColumns(ctx sessionctx.Context, curr, prev *model.PartitionDef
// PARTITION p1 VALUES LESS THAN (10,20,'mmm')
// PARTITION p2 VALUES LESS THAN (15,30,'sss')
colInfo := findColumnByName(pi.Columns[i].L, tbInfo)
succ, err := parseAndEvalBoolExpr(ctx.GetExprCtx(), curr.LessThan[i], prev.LessThan[i], colInfo, tbInfo)
cmp, err := parseAndEvalBoolExpr(ctx.GetExprCtx(), curr.LessThan[i], prev.LessThan[i], colInfo, tbInfo)
if err != nil {
return false, err
}

if succ {
if cmp > 0 {
return true, nil
}

if cmp < 0 {
return false, nil
}
}
return false, nil
}

func parseAndEvalBoolExpr(ctx expression.BuildContext, l, r string, colInfo *model.ColumnInfo, tbInfo *model.TableInfo) (bool, error) {
// equal, return 0
// greater, return 1
// less, return -1
func parseAndEvalBoolExpr(ctx expression.BuildContext, l, r string, colInfo *model.ColumnInfo, tbInfo *model.TableInfo) (int64, error) {
lexpr, err := expression.ParseSimpleExpr(ctx, l, expression.WithTableInfo("", tbInfo), expression.WithCastExprTo(&colInfo.FieldType))
if err != nil {
return false, err
return 0, err
}
rexpr, err := expression.ParseSimpleExpr(ctx, r, expression.WithTableInfo("", tbInfo), expression.WithCastExprTo(&colInfo.FieldType))
if err != nil {
return false, err
return 0, err
}
e, err := expression.NewFunctionBase(ctx, ast.GT, field_types.NewFieldType(mysql.TypeLonglong), lexpr, rexpr)

e, err := expression.NewFunctionBase(ctx, ast.EQ, field_types.NewFieldType(mysql.TypeLonglong), lexpr, rexpr)
if err != nil {
return false, err
return 0, err
}
e.SetCharsetAndCollation(colInfo.GetCharset(), colInfo.GetCollate())
res, _, err1 := e.EvalInt(ctx.GetEvalCtx(), chunk.Row{})
if err1 != nil {
return false, err1
return 0, err1
}
if res == 1 {
return 0, nil
}

e, err = expression.NewFunctionBase(ctx, ast.GT, field_types.NewFieldType(mysql.TypeLonglong), lexpr, rexpr)
if err != nil {
return 0, err
}
e.SetCharsetAndCollation(colInfo.GetCharset(), colInfo.GetCollate())
res, _, err1 = e.EvalInt(ctx.GetEvalCtx(), chunk.Row{})
if err1 != nil {
return 0, err1
}
if res > 0 {
return 1, nil
}
return res > 0, nil
return -1, nil
}

func checkCharsetAndCollation(cs string, co string) error {
Expand Down
11 changes: 11 additions & 0 deletions tests/integrationtest/r/ddl/partition.result
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,14 @@ Error 1740 (HY000): Table to exchange with partition has foreign key references:
alter table child drop foreign key fk_1;
alter table child drop key fk_1;
alter table child_with_partition exchange partition p1 with table child;
drop table if exists t;
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'), PARTITION `p20240521A` VALUES LESS THAN ('A','2024-05-21 00:00:00'));
Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'), PARTITION `p20240521Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'));
Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'), PARTITION `p20240521Z` VALUES LESS THAN ('Z','2024-05-21 00:00:00'));
drop table t;
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'));
alter table t add partition (PARTITION `p20240521A` VALUES LESS THAN ('A','2024-05-21 00:00:00'));
Error 1493 (HY000): VALUES LESS THAN value must be strictly increasing for each partition
alter table t add partition (PARTITION `p20240521Z` VALUES LESS THAN ('Z','2024-05-21 00:00:00'));
13 changes: 13 additions & 0 deletions tests/integrationtest/t/ddl/partition.test
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,16 @@ alter table child drop foreign key fk_1;
alter table child drop key fk_1;
alter table child_with_partition exchange partition p1 with table child;

drop table if exists t;
-- error 1493
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'), PARTITION `p20240521A` VALUES LESS THAN ('A','2024-05-21 00:00:00'));
-- error 1493
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'), PARTITION `p20240521Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'));
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'), PARTITION `p20240521Z` VALUES LESS THAN ('Z','2024-05-21 00:00:00'));

drop table t;
create table t(a int, b datetime, c varchar(8)) PARTITION BY RANGE COLUMNS(`c`,`b`) (PARTITION `p20240520Z` VALUES LESS THAN ('Z','2024-05-20 00:00:00'));
-- error 1493
alter table t add partition (PARTITION `p20240521A` VALUES LESS THAN ('A','2024-05-21 00:00:00'));
alter table t add partition (PARTITION `p20240521Z` VALUES LESS THAN ('Z','2024-05-21 00:00:00'));

0 comments on commit a251dad

Please sign in to comment.