Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#48815
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
crazycs520 authored and ti-chi-bot committed Nov 23, 2023
1 parent 5232881 commit 1512228
Show file tree
Hide file tree
Showing 3 changed files with 3,877 additions and 8 deletions.
43 changes: 35 additions & 8 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,20 @@ func getPartitionIntervalFromTable(ctx sessionctx.Context, tbInfo *model.TableIn
}

// comparePartitionAstAndModel compares a generated *ast.PartitionOptions and a *model.PartitionInfo
func comparePartitionAstAndModel(ctx sessionctx.Context, pAst *ast.PartitionOptions, pModel *model.PartitionInfo) error {
func comparePartitionAstAndModel(ctx sessionctx.Context, pAst *ast.PartitionOptions, pModel *model.PartitionInfo, partCol *model.ColumnInfo) error {
a := pAst.Definitions
m := pModel.Definitions
if len(pAst.Definitions) != len(pModel.Definitions) {
return dbterror.ErrGeneralUnsupportedDDL.GenWithStackByArgs("INTERVAL partitioning: number of partitions generated != partition defined (%d != %d)", len(a), len(m))
}

evalFn := func(expr ast.ExprNode) (types.Datum, error) {
val, err := expression.EvalAstExpr(ctx, ast.NewValueExpr(expr, "", ""))
if err != nil || partCol == nil {
return val, err
}
return val.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &partCol.FieldType)
}
for i := range pAst.Definitions {
// Allow options to differ! (like Placement Rules)
// Allow names to differ!
Expand All @@ -739,16 +747,19 @@ func comparePartitionAstAndModel(ctx sessionctx.Context, pAst *ast.PartitionOpti
if len(lessThan) > 1 && lessThan[:1] == "'" && lessThan[len(lessThan)-1:] == "'" {
lessThan = driver.UnwrapFromSingleQuotes(lessThan)
}
cmpExpr := &ast.BinaryOperationExpr{
Op: opcode.EQ,
L: ast.NewValueExpr(lessThan, "", ""),
R: generatedExpr,
lessThanVal, err := evalFn(ast.NewValueExpr(lessThan, "", ""))
if err != nil {
return err
}
cmp, err := expression.EvalAstExpr(ctx, cmpExpr)
generatedExprVal, err := evalFn(generatedExpr)
if err != nil {
return err
}
if cmp.GetInt64() != 1 {
cmp, err := lessThanVal.Compare(ctx.GetSessionVars().StmtCtx.TypeCtx(), &generatedExprVal, collate.GetBinaryCollator())
if err != nil {
return err
}
if cmp != 0 {
return dbterror.ErrGeneralUnsupportedDDL.GenWithStackByArgs(fmt.Sprintf("INTERVAL partitioning: LESS THAN for partition %s differs between generated and defined", m[i].Name.O))
}
}
Expand Down Expand Up @@ -923,7 +934,7 @@ func generatePartitionDefinitionsFromInterval(ctx sessionctx.Context, partOption
// Seems valid, so keep the defined so that the user defined names are kept etc.
partOptions.Definitions = definedPartDefs
} else if len(tbInfo.Partition.Definitions) > 0 {
err := comparePartitionAstAndModel(ctx, partOptions, tbInfo.Partition)
err := comparePartitionAstAndModel(ctx, partOptions, tbInfo.Partition, partCol)
if err != nil {
return err
}
Expand Down Expand Up @@ -997,6 +1008,12 @@ func GeneratePartDefsFromInterval(ctx sessionctx.Context, tp ast.AlterTableType,
if err != nil {
return err
}
if partCol != nil {
lastVal, err = lastVal.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &partCol.FieldType)
if err != nil {
return err
}
}
var partDefs []*ast.PartitionDefinition
if len(partitionOptions.Definitions) != 0 {
partDefs = partitionOptions.Definitions
Expand Down Expand Up @@ -1040,7 +1057,17 @@ func GeneratePartDefsFromInterval(ctx sessionctx.Context, tp ast.AlterTableType,
if err != nil {
return err
}
<<<<<<< HEAD:ddl/partition.go
cmp, err := currVal.Compare(ctx.GetSessionVars().StmtCtx, &lastVal, collate.GetBinaryCollator())
=======
if partCol != nil {
currVal, err = currVal.ConvertTo(ctx.GetSessionVars().StmtCtx.TypeCtx(), &partCol.FieldType)
if err != nil {
return err
}
}
cmp, err := currVal.Compare(ctx.GetSessionVars().StmtCtx.TypeCtx(), &lastVal, collate.GetBinaryCollator())
>>>>>>> 522cd038678 (ddl: fix issue of alter last partition failed when partition column is datetime (#48815)):pkg/ddl/partition.go
if err != nil {
return err
}
Expand Down
46 changes: 46 additions & 0 deletions pkg/ddl/tests/partition/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "partition_test",
timeout = "short",
srcs = [
"db_partition_test.go",
"main_test.go",
],
flaky = True,
shard_count = 48,
deps = [
"//pkg/config",
"//pkg/ddl",
"//pkg/ddl/testutil",
"//pkg/ddl/util/callback",
"//pkg/domain",
"//pkg/errno",
"//pkg/kv",
"//pkg/parser/ast",
"//pkg/parser/model",
"//pkg/parser/mysql",
"//pkg/parser/terror",
"//pkg/session",
"//pkg/sessionctx",
"//pkg/sessionctx/variable",
"//pkg/sessiontxn",
"//pkg/store/mockstore",
"//pkg/table",
"//pkg/table/tables",
"//pkg/tablecodec",
"//pkg/testkit",
"//pkg/testkit/external",
"//pkg/testkit/testsetup",
"//pkg/types",
"//pkg/util/codec",
"//pkg/util/dbterror",
"//pkg/util/logutil",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
"@org_uber_go_zap//:zap",
],
)
Loading

0 comments on commit 1512228

Please sign in to comment.