Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
also fix pkg/kv/kv.go
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv committed Jun 7, 2021
1 parent 8339023 commit 602d10b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package kv
import (
"bytes"
"fmt"
"math"
"sort"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -352,7 +353,7 @@ func (kvcodec *tableKVEncoder) AddRecord(
}
if isAutoIncCol {
// TODO use auto incremental type
_ = kvcodec.tbl.RebaseAutoID(kvcodec.se, value.GetInt64(), false, autoid.RowIDAllocType)
_ = kvcodec.tbl.RebaseAutoID(kvcodec.se, getAutoRecordID(value, &col.FieldType), false, autoid.RowIDAllocType)
}
}

Expand Down Expand Up @@ -384,6 +385,21 @@ func (kvcodec *tableKVEncoder) AddRecord(
return Pairs(pairs), size, nil
}

// get record value for auto-increment field
//
// See: https://github.com/pingcap/tidb/blob/47f0f15b14ed54fc2222f3e304e29df7b05e6805/executor/insert_common.go#L781-L852
// TODO: merge this with pkg/lightning/backend/kv/sql2kv.go
func getAutoRecordID(d types.Datum, target *types.FieldType) int64 {
switch target.Tp {
case mysql.TypeFloat, mysql.TypeDouble:
return int64(math.Round(d.GetFloat64()))
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
return d.GetInt64()
default:
panic(fmt.Sprintf("unsupported auto-increment field type '%d'", target.Tp))
}
}

// RemoveRecord encode a row of data into KV pairs.
func (kvcodec *tableKVEncoder) RemoveRecord(
row []types.Datum,
Expand Down

0 comments on commit 602d10b

Please sign in to comment.