Skip to content

Commit

Permalink
table,tablecodec,util/types: make it possible to read empty enum value (
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Jul 12, 2017
1 parent 9e6cc3a commit a0dfa04
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
24 changes: 24 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1984,3 +1984,27 @@ func (s *testSuite) TestFuncREPEAT(c *C) {
r = tk.MustQuery("SELECT REPEAT(a, 16777217), REPEAT(b, 16777217), REPEAT(c, 16777217), REPEAT(d, 16777217), REPEAT(e, 16777217), REPEAT(f, 16777217) FROM table_string;")
r.Check(testkit.Rows("<nil> <nil> <nil> <nil> <nil> <nil>"))
}

func (s *testSuite) TestEmptyEnum(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer func() {
s.cleanEnv(c)
testleak.AfterTest(c)()
}()
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (e enum('Y', 'N'))")
tk.MustExec("set sql_mode='STRICT_TRANS_TABLES'")
_, err := tk.Exec("insert into t values (0)")
c.Assert(terror.ErrorEqual(err, types.ErrTruncated), IsTrue)
_, err = tk.Exec("insert into t values ('abc')")
c.Assert(terror.ErrorEqual(err, types.ErrTruncated), IsTrue)

tk.MustExec("set sql_mode=''")
tk.MustExec("insert into t values (0)")
tk.MustQuery("select * from t").Check(testkit.Rows(""))
tk.MustExec("insert into t values ('abc')")
tk.MustQuery("select * from t").Check(testkit.Rows("", ""))
tk.MustExec("insert into t values (null)")
tk.MustQuery("select * from t").Check(testkit.Rows("", "", "<nil>"))
}
2 changes: 2 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ func GetZeroValue(col *model.ColumnInfo) types.Datum {
d.SetMysqlBit(types.Bit{Value: 0, Width: types.MinBitWidth})
case mysql.TypeSet:
d.SetMysqlSet(types.Set{})
case mysql.TypeEnum:
d.SetMysqlEnum(types.Enum{})
}
return d
}
4 changes: 4 additions & 0 deletions table/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func (s *testColumnSuite) TestGetZeroValue(c *C) {
types.NewFieldType(mysql.TypeSet),
types.NewDatum(types.Set{}),
},
{
types.NewFieldType(mysql.TypeEnum),
types.NewDatum(types.Enum{}),
},
}
sc := new(variable.StatementContext)
for _, tt := range tests {
Expand Down
6 changes: 2 additions & 4 deletions tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,8 @@ func unflatten(datum types.Datum, ft *types.FieldType, loc *time.Location) (type
datum.SetValue(dur)
return datum, nil
case mysql.TypeEnum:
enum, err := types.ParseEnumValue(ft.Elems, datum.GetUint64())
if err != nil {
return datum, errors.Trace(err)
}
// ignore error deliberately, to read empty enum value.
enum, _ := types.ParseEnumValue(ft.Elems, datum.GetUint64())
datum.SetValue(enum)
return datum, nil
case mysql.TypeSet:
Expand Down
5 changes: 3 additions & 2 deletions util/types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,9 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) {
c.Assert(v, DeepEquals, Enum{Name: "b", Value: 2})
_, err = Convert("d", ft)
c.Assert(err, NotNil)
_, err = Convert(4, ft)
c.Assert(err, NotNil)
v, err = Convert(4, ft)
c.Assert(terror.ErrorEqual(err, ErrTruncated), IsTrue)
c.Assert(v, DeepEquals, Enum{})

ft = NewFieldType(mysql.TypeSet)
ft.Elems = []string{"a", "b", "c"}
Expand Down
4 changes: 2 additions & 2 deletions util/types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,10 +1201,10 @@ func (d *Datum) convertToMysqlEnum(sc *variable.StatementContext, target *FieldT
e, err = ParseEnumValue(target.Elems, uintDatum.GetUint64())
}
if err != nil {
return invalidConv(d, target.Tp)
err = errors.Wrap(err, ErrTruncated)
}
ret.SetValue(e)
return ret, nil
return ret, err
}

func (d *Datum) convertToMysqlSet(sc *variable.StatementContext, target *FieldType) (Datum, error) {
Expand Down

0 comments on commit a0dfa04

Please sign in to comment.