Skip to content

Commit

Permalink
expression: round the frac part for ParseTimeFromFloat64 (#56340)
Browse files Browse the repository at this point in the history
close #56339
  • Loading branch information
gengliqi authored Nov 18, 2024
1 parent afe8a09 commit 9512c96
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/types/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
Expand Down
6 changes: 6 additions & 0 deletions tests/integrationtest/r/expression/issues.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion tests/integrationtest/t/expression/issues.test
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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';

0 comments on commit 9512c96

Please sign in to comment.