Skip to content

Commit

Permalink
expression: fix casting year 0 to string 0000 (#21469)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichn-hu authored Dec 15, 2020
1 parent 25daaee commit 67d66d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7119,6 +7119,15 @@ func (s *testSuite) Test17780(c *C) {
tk.MustQuery("select count(*) from t0 where c0 = 0").Check(testkit.Rows("0"))
}

func (s *testSuite) TestIssue9918(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a year)")
tk.MustExec("insert into t values(0)")
tk.MustQuery("select cast(a as char) from t").Check(testkit.Rows("0000"))
}

func (s *testSuite) Test13004(c *C) {
tk := testkit.NewTestKit(c, s.store)
// see https://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html, timestamp here actually produces a datetime
Expand Down
6 changes: 5 additions & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,15 @@ func (b *builtinCastIntAsStringSig) evalString(row chunk.Row) (res string, isNul
if isNull || err != nil {
return res, isNull, err
}
if !mysql.HasUnsignedFlag(b.args[0].GetType().Flag) {
tp := b.args[0].GetType()
if !mysql.HasUnsignedFlag(tp.Flag) {
res = strconv.FormatInt(val, 10)
} else {
res = strconv.FormatUint(uint64(val), 10)
}
if tp.Tp == mysql.TypeYear && res == "0" {
res = "0000"
}
res, err = types.ProduceStrWithSpecifiedTp(res, b.tp, b.ctx.GetSessionVars().StmtCtx, false)
if err != nil {
return res, false, err
Expand Down
7 changes: 6 additions & 1 deletion expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,9 @@ func (b *builtinCastIntAsStringSig) vecEvalString(input *chunk.Chunk, result *ch
return err
}

isUnsigned := mysql.HasUnsignedFlag(b.args[0].GetType().Flag)
tp := b.args[0].GetType()
isUnsigned := mysql.HasUnsignedFlag(tp.Flag)
isYearType := tp.Tp == mysql.TypeYear
result.ReserveString(n)
i64s := buf.Int64s()
for i := 0; i < n; i++ {
Expand All @@ -650,6 +652,9 @@ func (b *builtinCastIntAsStringSig) vecEvalString(input *chunk.Chunk, result *ch
} else {
str = strconv.FormatUint(uint64(i64s[i]), 10)
}
if isYearType && str == "0" {
str = "0000"
}
str, err = types.ProduceStrWithSpecifiedTp(str, b.tp, b.ctx.GetSessionVars().StmtCtx, false)
if err != nil {
return err
Expand Down

0 comments on commit 67d66d6

Please sign in to comment.