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

table/tables: fix load partition when upgrade from an old TiDB #17971

Merged
merged 5 commits into from
Jun 12, 2020
Merged
Changes from all 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
25 changes: 22 additions & 3 deletions table/tables/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ type ForRangePruning struct {
}

// dataForRangePruning extracts the less than parts from 'partition p0 less than xx ... partitoin p1 less than ...'
func dataForRangePruning(pi *model.PartitionInfo) (*ForRangePruning, error) {
func dataForRangePruning(sctx sessionctx.Context, pi *model.PartitionInfo) (*ForRangePruning, error) {
var maxValue bool
var unsigned bool
lessThan := make([]int64, len(pi.Definitions))
Expand All @@ -182,7 +182,12 @@ func dataForRangePruning(pi *model.PartitionInfo) (*ForRangePruning, error) {
unsigned = true
}
if err != nil {
return nil, errors.WithStack(err)
val, ok := fixOldVersionPartitionInfo(sctx, pi.Definitions[i].LessThan[0])
if !ok {
logutil.BgLogger().Error("wrong partition definition", zap.String("less than", pi.Definitions[i].LessThan[0]))
return nil, errors.WithStack(err)
}
lessThan[i] = val
}
}
}
Expand All @@ -193,6 +198,20 @@ func dataForRangePruning(pi *model.PartitionInfo) (*ForRangePruning, error) {
}, nil
}

func fixOldVersionPartitionInfo(sctx sessionctx.Context, str string) (int64, bool) {
// less than value should be calculate to integer before persistent.
// Old version TiDB may not do it and store the raw expression.
tmp, err := parseSimpleExprWithNames(parser.New(), sctx, str, nil, nil)
if err != nil {
return 0, false
}
ret, isNull, err := tmp.EvalInt(sctx, chunk.Row{})

This comment was marked as resolved.

if err != nil || isNull {
return 0, false
}
return ret, true
}

// rangePartitionString returns the partition string for a range typed partition.
func rangePartitionString(pi *model.PartitionInfo) string {
// partition by range expr
Expand Down Expand Up @@ -240,7 +259,7 @@ func generateRangePartitionExpr(ctx sessionctx.Context, pi *model.PartitionInfo,

switch len(pi.Columns) {
case 0:
tmp, err := dataForRangePruning(pi)
tmp, err := dataForRangePruning(ctx, pi)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down