From 8b028b899d562496bb1ae2dd7d64bdfd5882b7ac Mon Sep 17 00:00:00 2001 From: wjHuang Date: Tue, 23 Nov 2021 17:49:50 +0800 Subject: [PATCH] *: replace compareDatum by compare (#30060) --- executor/replace.go | 8 ++------ expression/builtin_compare.go | 3 ++- expression/builtin_other_test.go | 3 ++- expression/distsql_builtin_test.go | 3 ++- store/mockstore/unistore/cophandler/cop_handler_test.go | 5 +++-- table/column_test.go | 5 +++-- util/codec/codec_test.go | 5 +++-- util/profile/flamegraph_test.go | 3 ++- 8 files changed, 19 insertions(+), 16 deletions(-) diff --git a/executor/replace.go b/executor/replace.go index cd7a3d44a7bb4..a9b29707f9ade 100644 --- a/executor/replace.go +++ b/executor/replace.go @@ -23,12 +23,12 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta/autoid" - "github.com/pingcap/tidb/parser/charset" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/chunk" + "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/memory" "go.uber.org/zap" @@ -99,11 +99,7 @@ func (e *ReplaceExec) EqualDatumsAsBinary(sc *stmtctx.StatementContext, a []type return false, nil } for i, ai := range a { - collation := ai.Collation() - // We should use binary collation to compare datum, otherwise the result will be incorrect - ai.SetCollation(charset.CollationBin) - v, err := ai.CompareDatum(sc, &b[i]) - ai.SetCollation(collation) + v, err := ai.Compare(sc, &b[i], collate.GetBinaryCollator()) if err != nil { return false, errors.Trace(err) } diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index dce8d2cdd3ba0..1090ca0e14eea 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -26,6 +26,7 @@ import ( "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/types/json" "github.com/pingcap/tidb/util/chunk" + "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tipb/go-tipb" ) @@ -1378,7 +1379,7 @@ func RefineComparedConstant(ctx sessionctx.Context, targetFieldType types.FieldT } return con, false } - c, err := intDatum.CompareDatum(sc, &con.Value) + c, err := intDatum.Compare(sc, &con.Value, collate.GetBinaryCollator()) if err != nil { return con, false } diff --git a/expression/builtin_other_test.go b/expression/builtin_other_test.go index 7979f7d2720a6..58263af992365 100644 --- a/expression/builtin_other_test.go +++ b/expression/builtin_other_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/chunk" + "github.com/pingcap/tidb/util/collate" "github.com/stretchr/testify/require" ) @@ -198,7 +199,7 @@ func TestValues(t *testing.T) { ret, err = evalBuiltinFunc(sig, chunk.Row{}) require.NoError(t, err) - cmp, err := ret.CompareDatum(nil, &currInsertValues[1]) + cmp, err := ret.Compare(nil, &currInsertValues[1], collate.GetBinaryCollator()) require.NoError(t, err) require.Equal(t, 0, cmp) } diff --git a/expression/distsql_builtin_test.go b/expression/distsql_builtin_test.go index c08c728d63446..06ced0d27211f 100644 --- a/expression/distsql_builtin_test.go +++ b/expression/distsql_builtin_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/types/json" "github.com/pingcap/tidb/util/chunk" "github.com/pingcap/tidb/util/codec" + "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tipb/go-tipb" "github.com/stretchr/testify/require" ) @@ -787,7 +788,7 @@ func TestEval(t *testing.T) { result, err := expr.Eval(row) require.NoError(t, err) require.Equal(t, tt.result.Kind(), result.Kind()) - cmp, err := result.CompareDatum(sc, &tt.result) + cmp, err := result.Compare(sc, &tt.result, collate.GetCollator(fieldTps[0].Collate)) require.NoError(t, err) require.Equal(t, 0, cmp) } diff --git a/store/mockstore/unistore/cophandler/cop_handler_test.go b/store/mockstore/unistore/cophandler/cop_handler_test.go index 3bd39184865cc..4e8462c8be651 100644 --- a/store/mockstore/unistore/cophandler/cop_handler_test.go +++ b/store/mockstore/unistore/cophandler/cop_handler_test.go @@ -36,6 +36,7 @@ import ( "github.com/pingcap/tidb/tablecodec" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/codec" + "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/rowcodec" "github.com/pingcap/tipb/go-tipb" "github.com/stretchr/testify/require" @@ -339,10 +340,10 @@ func TestPointGet(t *testing.T) { // verify the returned rows value as input expectedRow := data.rows[handle] - eq, err := returnedRow[0].CompareDatum(nil, &expectedRow[0]) + eq, err := returnedRow[0].Compare(nil, &expectedRow[0], collate.GetBinaryCollator()) require.NoError(t, err) require.Equal(t, 0, eq) - eq, err = returnedRow[1].CompareDatum(nil, &expectedRow[1]) + eq, err = returnedRow[1].Compare(nil, &expectedRow[1], collate.GetBinaryCollator()) require.NoError(t, err) require.Equal(t, 0, eq) } diff --git a/table/column_test.go b/table/column_test.go index 434e6341a515c..7c860feeae3d6 100644 --- a/table/column_test.go +++ b/table/column_test.go @@ -27,6 +27,7 @@ import ( "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/types/json" + "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/mock" "github.com/stretchr/testify/require" ) @@ -126,7 +127,7 @@ func TestHandleBadNull(t *testing.T) { d := types.Datum{} err := col.HandleBadNull(&d, sc) require.NoError(t, err) - cmp, err := d.CompareDatum(sc, &types.Datum{}) + cmp, err := d.Compare(sc, &types.Datum{}, collate.GetBinaryCollator()) require.NoError(t, err) require.Equal(t, 0, cmp) @@ -255,7 +256,7 @@ func TestGetZeroValue(t *testing.T) { colInfo := &model.ColumnInfo{FieldType: *tt.ft} zv := GetZeroValue(colInfo) require.Equal(t, tt.value.Kind(), zv.Kind()) - cmp, err := zv.CompareDatum(sc, &tt.value) + cmp, err := zv.Compare(sc, &tt.value, collate.GetCollator(tt.ft.Collate)) require.NoError(t, err) require.Equal(t, 0, cmp) }) diff --git a/util/codec/codec_test.go b/util/codec/codec_test.go index ac41a148160cd..fef959a0c9c8b 100644 --- a/util/codec/codec_test.go +++ b/util/codec/codec_test.go @@ -30,6 +30,7 @@ import ( "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/types/json" "github.com/pingcap/tidb/util/chunk" + "github.com/pingcap/tidb/util/collate" "github.com/stretchr/testify/require" ) @@ -987,7 +988,7 @@ func TestDecodeOneToChunk(t *testing.T) { require.True(t, expect.IsNull()) } else { if got.Kind() != types.KindMysqlDecimal { - cmp, err := got.CompareDatum(sc, &expect) + cmp, err := got.Compare(sc, &expect, collate.GetCollator(tp.Collate)) require.NoError(t, err) require.Equalf(t, 0, cmp, "expect: %v, got %v", expect, got) } else { @@ -1110,7 +1111,7 @@ func TestDecodeRange(t *testing.T) { datums1, _, err := DecodeRange(rowData, len(datums), nil, nil) require.NoError(t, err) for i, datum := range datums1 { - cmp, err := datum.CompareDatum(nil, &datums[i]) + cmp, err := datum.Compare(nil, &datums[i], collate.GetBinaryCollator()) require.NoError(t, err) require.Equal(t, 0, cmp) } diff --git a/util/profile/flamegraph_test.go b/util/profile/flamegraph_test.go index 634e1286ef8cf..dfc83d3b27654 100644 --- a/util/profile/flamegraph_test.go +++ b/util/profile/flamegraph_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/pingcap/tidb/types" + "github.com/pingcap/tidb/util/collate" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -91,7 +92,7 @@ func TestProfileToDatum(t *testing.T) { comment = fmt.Sprintf("row %2d, actual (%s), expected (%s)", i, rowStr, expectStr) equal := true for j, r := range row { - v, err := r.CompareDatum(nil, &datums[i][j]) + v, err := r.Compare(nil, &datums[i][j], collate.GetBinaryCollator()) if v != 0 || err != nil { equal = false break