Skip to content

Commit

Permalink
expression: from_unixtime accept 64-bit integers (#22616)
Browse files Browse the repository at this point in the history
close #22206
  • Loading branch information
TszKitLo40 authored Feb 10, 2022
1 parent 82add9c commit 92f5a63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1694,15 +1694,17 @@ func (c *fromUnixTimeFunctionClass) getFunction(ctx sessionctx.Context, args []E
}

func evalFromUnixTime(ctx sessionctx.Context, fsp int, unixTimeStamp *types.MyDecimal) (res types.Time, isNull bool, err error) {
// 0 <= unixTimeStamp <= INT32_MAX
// 0 <= unixTimeStamp <= 32536771199.999999
if unixTimeStamp.IsNegative() {
return res, true, nil
}
integralPart, err := unixTimeStamp.ToInt()
if err != nil && !terror.ErrorEqual(err, types.ErrTruncated) {
return res, true, err
}
if integralPart > int64(math.MaxInt32) {
// The max integralPart should not be larger than 32536771199.
// Refer to https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-28.html
if integralPart > 32536771199 {
return res, true, nil
}
// Split the integral part and fractional part of a decimal timestamp.
Expand Down
18 changes: 18 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7028,3 +7028,21 @@ func TestIssue29708(t *testing.T) {
{"b"},
})
}

func TestIssue22206(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tz := tk.Session().GetSessionVars().StmtCtx.TimeZone
result := tk.MustQuery("select from_unixtime(32536771199.999999)")
unixTime := time.Unix(32536771199, 999999000).In(tz).String()[:26]
result.Check(testkit.Rows(unixTime))
result = tk.MustQuery("select from_unixtime('32536771200.000000')")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select from_unixtime(5000000000);")
unixTime = time.Unix(5000000000, 0).In(tz).String()[:19]
result.Check(testkit.Rows(unixTime))
}

0 comments on commit 92f5a63

Please sign in to comment.