Skip to content

Commit

Permalink
column: fix zero value for binary type (#6213)
Browse files Browse the repository at this point in the history
Zero value for BINARY type should be byte slice with length.
  • Loading branch information
coocood authored Apr 3, 2018
1 parent ccf6da1 commit 42f9dda
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{})
Expand Down
10 changes: 10 additions & 0 deletions table/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 42f9dda

Please sign in to comment.