Skip to content

Commit

Permalink
table/tables: fix load partition when upgrade from an old TiDB (#17971)
Browse files Browse the repository at this point in the history
#17983

Signed-off-by: sre-bot <sre-bot@pingcap.com>
Co-authored-by: tiancaiamao <tiancaiamao@gmail.com>
  • Loading branch information
sre-bot and tiancaiamao authored Jun 15, 2020
1 parent 1b12e01 commit e93590e
Showing 1 changed file with 22 additions and 3 deletions.
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{})
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

0 comments on commit e93590e

Please sign in to comment.