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

*: replace compareDatum by compare, PR 11 #30465

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions expression/distsql_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tipb/go-tipb"
)
Expand All @@ -40,7 +41,7 @@ func PbTypeToFieldType(tp *tipb.FieldType) *types.FieldType {
Flen: int(tp.Flen),
Decimal: int(tp.Decimal),
Charset: tp.Charset,
Collate: protoToCollation(tp.Collate),
Collate: collate.ProtoToCollation(tp.Collate),
Elems: tp.Elems,
}
}
Expand Down Expand Up @@ -1216,7 +1217,7 @@ func convertUint(val []byte) (*Constant, error) {

func convertString(val []byte, tp *tipb.FieldType) (*Constant, error) {
var d types.Datum
d.SetBytesAsString(val, protoToCollation(tp.Collate), uint32(tp.Flen))
d.SetBytesAsString(val, collate.ProtoToCollation(tp.Collate), uint32(tp.Flen))
return &Constant{Value: d, RetType: types.NewFieldType(mysql.TypeVarString)}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion expression/distsql_builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ func toPBFieldType(ft *types.FieldType) *tipb.FieldType {
Flen: int32(ft.Flen),
Decimal: int32(ft.Decimal),
Charset: ft.Charset,
Collate: collationToProto(ft.Collate),
Collate: collate.CollationToProto(ft.Collate),
Elems: ft.Elems,
}
}
Expand Down
33 changes: 2 additions & 31 deletions expression/expr_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -157,7 +156,7 @@ func ToPBFieldType(ft *types.FieldType) *tipb.FieldType {
Flen: int32(ft.Flen),
Decimal: int32(ft.Decimal),
Charset: ft.Charset,
Collate: collationToProto(ft.Collate),
Collate: collate.CollationToProto(ft.Collate),
Elems: ft.Elems,
}
}
Expand All @@ -170,39 +169,11 @@ func FieldTypeFromPB(ft *tipb.FieldType) *types.FieldType {
Flen: int(ft.Flen),
Decimal: int(ft.Decimal),
Charset: ft.Charset,
Collate: protoToCollation(ft.Collate),
Collate: collate.ProtoToCollation(ft.Collate),
Elems: ft.Elems,
}
}

func collationToProto(c string) int32 {
if coll, err := charset.GetCollationByName(c); err == nil {
return collate.RewriteNewCollationIDIfNeeded(int32(coll.ID))
}
v := collate.RewriteNewCollationIDIfNeeded(int32(mysql.DefaultCollationID))
logutil.BgLogger().Warn(
"Unable to get collation ID by name, use ID of the default collation instead",
zap.String("name", c),
zap.Int32("default collation ID", v),
zap.String("default collation", mysql.DefaultCollationName),
)
return v
}

func protoToCollation(c int32) string {
coll, err := charset.GetCollationByID(int(collate.RestoreCollationIDIfNeeded(c)))
if err == nil {
return coll.Name
}
logutil.BgLogger().Warn(
"Unable to get collation name from ID, use name of the default collation instead",
zap.Int32("id", c),
zap.Int("default collation ID", mysql.DefaultCollationID),
zap.String("default collation", mysql.DefaultCollationName),
)
return mysql.DefaultCollationName
}

func (pc PbConverter) columnToPBExpr(column *Column) *tipb.Expr {
if !pc.client.IsRequestTypeSupported(kv.ReqTypeSelect, int64(tipb.ExprType_ColumnRef)) {
return nil
Expand Down
7 changes: 3 additions & 4 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/pingcap/tidb/types"
driver "github.com/pingcap/tidb/types/parser_driver"
tidbutil "github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/math"
"github.com/pingcap/tidb/util/plancodec"
Expand Down Expand Up @@ -1213,8 +1214,7 @@ func getNameValuePairs(stmtCtx *stmtctx.StatementContext, tbl *model.TableInfo,
}
}
// The converted result must be same as original datum.
// Compare them based on the dVal's type.
cmp, err := dVal.CompareDatum(stmtCtx, &d)
cmp, err := dVal.Compare(stmtCtx, &d, collate.GetCollator(col.Collate))
if err != nil {
return nil, false
} else if cmp != 0 {
Expand All @@ -1235,8 +1235,7 @@ func getPointGetValue(stmtCtx *stmtctx.StatementContext, col *model.ColumnInfo,
return nil
}
// The converted result must be same as original datum.
// Compare them based on the dVal's type.
cmp, err := dVal.CompareDatum(stmtCtx, d)
cmp, err := dVal.Compare(stmtCtx, d, collate.GetCollator(col.Collate))
if err != nil || cmp != 0 {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion statistics/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *sampleItemSorter) Len() int {

func (s *sampleItemSorter) Less(i, j int) bool {
var cmp int
cmp, s.err = s.items[i].Value.CompareDatum(s.sc, &s.items[j].Value)
cmp, s.err = s.items[i].Value.Compare(s.sc, &s.items[j].Value, collate.GetBinaryCollator())
if s.err != nil {
return true
}
Expand Down
5 changes: 3 additions & 2 deletions store/mockstore/mockcopr/topn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tipb/go-tipb"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func (t *topNSorter) Less(i, j int) bool {
v1 := t.rows[i].key[index]
v2 := t.rows[j].key[index]

ret, err := v1.CompareDatum(t.sc, &v2)
ret, err := v1.Compare(t.sc, &v2, collate.GetCollator(collate.ProtoToCollation(by.Expr.FieldType.Collate)))
if err != nil {
t.err = errors.Trace(err)
return true
Expand Down Expand Up @@ -98,7 +99,7 @@ func (t *topNHeap) Less(i, j int) bool {
v1 := t.rows[i].key[index]
v2 := t.rows[j].key[index]

ret, err := v1.CompareDatum(t.sc, &v2)
ret, err := v1.Compare(t.sc, &v2, collate.GetCollator(collate.ProtoToCollation(by.Expr.FieldType.Collate)))
if err != nil {
t.err = errors.Trace(err)
return true
Expand Down
5 changes: 3 additions & 2 deletions store/mockstore/unistore/cophandler/topn.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
tipb "github.com/pingcap/tipb/go-tipb"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func (t *topNSorter) Less(i, j int) bool {
v1 := t.rows[i].key[index]
v2 := t.rows[j].key[index]

ret, err := v1.CompareDatum(t.sc, &v2)
ret, err := v1.Compare(t.sc, &v2, collate.GetCollator(collate.ProtoToCollation(by.Expr.FieldType.Collate)))
if err != nil {
t.err = errors.Trace(err)
return true
Expand Down Expand Up @@ -105,7 +106,7 @@ func (t *topNHeap) Less(i, j int) bool {
if expression.FieldTypeFromPB(by.GetExpr().GetFieldType()).Tp == mysql.TypeEnum {
ret = types.CompareUint64(v1.GetUint64(), v2.GetUint64())
} else {
ret, err = v1.CompareDatum(t.sc, &v2)
ret, err = v1.Compare(t.sc, &v2, collate.GetCollator(collate.ProtoToCollation(by.Expr.FieldType.Collate)))
if err != nil {
t.err = errors.Trace(err)
return true
Expand Down
5 changes: 3 additions & 2 deletions testkit/trequire/trequire.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import (

"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
"github.com/stretchr/testify/require"
)

// DatumEqual verifies that the actual value is equal to the expected value.
// DatumEqual verifies that the actual value is equal to the expected value. For string datum, they are compared by the binary collation.
func DatumEqual(t *testing.T, expected, actual types.Datum, msgAndArgs ...interface{}) {
sc := new(stmtctx.StatementContext)
res, err := actual.CompareDatum(sc, &expected)
res, err := actual.Compare(sc, &expected, collate.GetBinaryCollator())
require.NoError(t, err, msgAndArgs)
require.Zero(t, res, msgAndArgs)
}
30 changes: 30 additions & 0 deletions util/collate/collate.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,36 @@ func IsBinCollation(collate string) bool {
collate == charset.CollationUTF8 || collate == charset.CollationUTF8MB4
}

// CollationToProto converts collation from string to int32(used by protocol).
func CollationToProto(c string) int32 {
if coll, err := charset.GetCollationByName(c); err == nil {
return RewriteNewCollationIDIfNeeded(int32(coll.ID))
}
v := RewriteNewCollationIDIfNeeded(int32(mysql.DefaultCollationID))
logutil.BgLogger().Warn(
"Unable to get collation ID by name, use ID of the default collation instead",
zap.String("name", c),
zap.Int32("default collation ID", v),
zap.String("default collation", mysql.DefaultCollationName),
)
return v
}

// ProtoToCollation converts collation from int32(used by protocol) to string.
func ProtoToCollation(c int32) string {
coll, err := charset.GetCollationByID(int(RestoreCollationIDIfNeeded(c)))
if err == nil {
return coll.Name
}
logutil.BgLogger().Warn(
"Unable to get collation name from ID, use name of the default collation instead",
zap.Int32("id", c),
zap.Int("default collation ID", mysql.DefaultCollationID),
zap.String("default collation", mysql.DefaultCollationName),
)
return mysql.DefaultCollationName
}

func init() {
newCollatorMap = make(map[string]Collator)
newCollatorIDMap = make(map[int]Collator)
Expand Down
3 changes: 2 additions & 1 deletion util/ranger/ranger.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/types"
driver "github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
)

func validInterval(sctx sessionctx.Context, low, high *point) (bool, error) {
Expand Down Expand Up @@ -121,7 +122,7 @@ func convertPoint(sctx sessionctx.Context, point *point, tp *types.FieldType) (*
return point, errors.Trace(err)
}
}
valCmpCasted, err := point.value.CompareDatum(sc, &casted)
valCmpCasted, err := point.value.Compare(sc, &casted, collate.GetCollator(tp.Collate))
if err != nil {
return point, errors.Trace(err)
}
Expand Down