From 9512c9630e74741502acc22de09df2f29b82aa7d Mon Sep 17 00:00:00 2001 From: Liqi Geng Date: Mon, 18 Nov 2024 19:54:20 +0800 Subject: [PATCH] expression: round the frac part for `ParseTimeFromFloat64` (#56340) close pingcap/tidb#56339 --- pkg/types/time.go | 2 +- pkg/types/time_test.go | 3 ++- tests/integrationtest/r/expression/issues.result | 6 ++++++ tests/integrationtest/t/expression/issues.test | 8 +++++++- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/types/time.go b/pkg/types/time.go index e2d915df86489..778ed31ee66a7 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -2669,7 +2669,7 @@ func ParseTimeFromFloat64(ctx Context, f float64) (Time, error) { } if t.Type() == mysql.TypeDatetime { // US part is only kept when the integral part is recognized as datetime. - fracPart := uint32((f - float64(intPart)) * 1000000.0) + fracPart := uint32(math.Round((f - float64(intPart)) * 1000000.0)) ct := t.CoreTime() ct.setMicrosecond(fracPart) t.SetCoreTime(ct) diff --git a/pkg/types/time_test.go b/pkg/types/time_test.go index d015f5fb209b3..00484a84a0225 100644 --- a/pkg/types/time_test.go +++ b/pkg/types/time_test.go @@ -1814,7 +1814,8 @@ func TestParseTimeFromFloat64(t *testing.T) { {0.0, mysql.TypeDate, 0, 0, 0, 0, 0, 0, 0, nil}, {20000102030405, mysql.TypeDatetime, 2000, 1, 2, 3, 4, 5, 0, nil}, {20000102030405.015625, mysql.TypeDatetime, 2000, 1, 2, 3, 4, 5, 15625, nil}, - {20000102030405.0078125, mysql.TypeDatetime, 2000, 1, 2, 3, 4, 5, 7812, nil}, + {20000102030405.0078125, mysql.TypeDatetime, 2000, 1, 2, 3, 4, 5, 7813, nil}, + {121212131313.99998, mysql.TypeDatetime, 2012, 12, 12, 13, 13, 13, 999985, nil}, {2000, mysql.TypeDatetime, 0, 0, 0, 0, 0, 0, 0, types.ErrTruncatedWrongVal}, {20000000000000, mysql.TypeDatetime, 2000, 0, 0, 0, 0, 0, 0, nil}, } diff --git a/tests/integrationtest/r/expression/issues.result b/tests/integrationtest/r/expression/issues.result index 127b1bd2180ed..0a6bbf58f7988 100644 --- a/tests/integrationtest/r/expression/issues.result +++ b/tests/integrationtest/r/expression/issues.result @@ -3236,3 +3236,9 @@ INSERT INTO test.t VALUES (0); SELECT c0 FROM test.t WHERE CAST(ATAN2(((t.c0) IS NULL), (- (''))) AS TIME); c0 0 +drop table if exists test.t; +create table test.t(a double); +insert into test.t values('20000102030405.0078125'); +select * from test.t where date_add(a, interval 1 second) = '2000-01-02 03:04:06.007813'; +a +20000102030405.008 diff --git a/tests/integrationtest/t/expression/issues.test b/tests/integrationtest/t/expression/issues.test index a8e11fc25a5d4..28dfc33368890 100644 --- a/tests/integrationtest/t/expression/issues.test +++ b/tests/integrationtest/t/expression/issues.test @@ -2187,4 +2187,10 @@ SELECT IFNULL(id, 'abcdef') FROM test.t; DROP TABLE IF EXISTS test.t; CREATE TABLE test.t (c0 decimal(10,0)); INSERT INTO test.t VALUES (0); -SELECT c0 FROM test.t WHERE CAST(ATAN2(((t.c0) IS NULL), (- (''))) AS TIME); \ No newline at end of file +SELECT c0 FROM test.t WHERE CAST(ATAN2(((t.c0) IS NULL), (- (''))) AS TIME); + +# TestIssue56339 +drop table if exists test.t; +create table test.t(a double); +insert into test.t values('20000102030405.0078125'); +select * from test.t where date_add(a, interval 1 second) = '2000-01-02 03:04:06.007813';