diff --git a/table/column.go b/table/column.go index 5e5e7b92a6f71..5bc63ff27c777 100644 --- a/table/column.go +++ b/table/column.go @@ -29,6 +29,7 @@ import ( "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/types/json" + "github.com/pingcap/tidb/util/charset" "github.com/pingcap/tidb/util/hack" log "github.com/sirupsen/logrus" ) @@ -370,7 +371,13 @@ func GetZeroValue(col *model.ColumnInfo) types.Datum { d.SetFloat64(0) case mysql.TypeNewDecimal: d.SetMysqlDecimal(new(types.MyDecimal)) - case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar: + case mysql.TypeString: + if col.Flen > 0 && col.Charset == charset.CharsetBin { + d.SetBytes(make([]byte, col.Flen)) + } else { + d.SetString("") + } + case mysql.TypeVarString, mysql.TypeVarchar: d.SetString("") case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob: d.SetBytes([]byte{}) diff --git a/table/column_test.go b/table/column_test.go index 29e6d7d9b8706..45d2d5bdaf529 100644 --- a/table/column_test.go +++ b/table/column_test.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" + "github.com/pingcap/tidb/util/charset" "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/testleak" ) @@ -193,6 +194,15 @@ func (t *testTableSuite) TestGetZeroValue(c *C) { types.NewFieldType(mysql.TypeEnum), types.NewDatum(types.Enum{}), }, + { + &types.FieldType{ + Tp: mysql.TypeString, + Flen: 2, + Charset: charset.CharsetBin, + Collate: charset.CollationBin, + }, + types.NewDatum(make([]byte, 2)), + }, } sc := new(stmtctx.StatementContext) for _, tt := range tests {