From c8fa259b2e26eebc274a6637f1ee5dcc13102177 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Sat, 28 Mar 2020 22:14:05 +0800 Subject: [PATCH] expression: `sec_to_time` is not compatible with MySQL when the argument is a scientific notation number (#15614) --- expression/builtin_time.go | 2 +- expression/builtin_time_vec.go | 2 +- expression/integration_test.go | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/expression/builtin_time.go b/expression/builtin_time.go index cbb345ce2b..811b76a0b4 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -5981,7 +5981,7 @@ func (b *builtinSecToTimeSig) evalDuration(row chunk.Row) (types.Duration, bool, secondDemical = float64(second) + demical var dur types.Duration - dur, err = types.ParseDuration(b.ctx.GetSessionVars().StmtCtx, fmt.Sprintf("%s%02d:%02d:%v", negative, hour, minute, secondDemical), int8(b.tp.Decimal)) + dur, err = types.ParseDuration(b.ctx.GetSessionVars().StmtCtx, fmt.Sprintf("%s%02d:%02d:%s", negative, hour, minute, strconv.FormatFloat(secondDemical, 'f', -1, 64)), int8(b.tp.Decimal)) if err != nil { return types.Duration{}, err != nil, err } diff --git a/expression/builtin_time_vec.go b/expression/builtin_time_vec.go index 53e778a1cc..9766e903e9 100644 --- a/expression/builtin_time_vec.go +++ b/expression/builtin_time_vec.go @@ -1869,7 +1869,7 @@ func (b *builtinSecToTimeSig) vecEvalDuration(input *chunk.Chunk, result *chunk. second = seconds % 60 } secondDemical := float64(second) + demical - duration, err := types.ParseDuration(b.ctx.GetSessionVars().StmtCtx, fmt.Sprintf("%s%02d:%02d:%v", negative, hour, minute, secondDemical), int8(b.tp.Decimal)) + duration, err := types.ParseDuration(b.ctx.GetSessionVars().StmtCtx, fmt.Sprintf("%s%02d:%02d:%s", negative, hour, minute, strconv.FormatFloat(secondDemical, 'f', -1, 64)), int8(b.tp.Decimal)) if err != nil { return err } diff --git a/expression/integration_test.go b/expression/integration_test.go index b189b78412..9092254b22 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4967,6 +4967,14 @@ func (s *testIntegrationSuite) TestInvalidEndingStatement(c *C) { assertParseErr(`drop table if exists t"`) } +func (s *testIntegrationSuite) TestIssue15613(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustQuery("select sec_to_time(1e-4)").Check(testkit.Rows("00:00:00.000100")) + tk.MustQuery("select sec_to_time(1e-5)").Check(testkit.Rows("00:00:00.000010")) + tk.MustQuery("select sec_to_time(1e-6)").Check(testkit.Rows("00:00:00.000001")) + tk.MustQuery("select sec_to_time(1e-7)").Check(testkit.Rows("00:00:00.000000")) +} + func (s *testIntegrationSuite) TestIssue10675(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test")