Skip to content

Commit

Permalink
Reverted changes from pingcap#56431
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonss committed Oct 3, 2024
1 parent d25cd4f commit 1d01a4a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
15 changes: 8 additions & 7 deletions pkg/expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,11 +1733,12 @@ func evalFromUnixTime(ctx EvalContext, fsp int, unixTimeStamp *types.MyDecimal)

tc := typeCtx(ctx)
tmp := time.Unix(integralPart, fractionalPart).In(tc.Location())
roundMode := types.ModeHalfUp
if sqlMode(ctx).HasTimeTruncateFractional() {
roundMode = types.ModeTruncate
var t types.Time
truncateMode := types.ModeHalfUp
if ctx.SQLMode().HasTimeTruncateFractional() {
truncateMode = types.ModeTruncate
}
t, err := convertTimeToMysqlTime(tmp, fsp, roundMode)
t, err = convertTimeToMysqlTime(tmp, fsp, truncateMode)
if err != nil {
return res, true, err
}
Expand Down Expand Up @@ -2064,7 +2065,7 @@ func (b *builtinSysDateWithFspSig) evalTime(ctx EvalContext, row chunk.Row) (val

loc := location(ctx)
now := time.Now().In(loc)
result, err := convertTimeToMysqlTime(now, int(fsp), types.ModeTruncate)
result, err := convertTimeToMysqlTime(now, int(fsp), types.ModeHalfUp)
if err != nil {
return types.ZeroTime, true, err
}
Expand All @@ -2086,7 +2087,7 @@ func (b *builtinSysDateWithoutFspSig) Clone() builtinFunc {
func (b *builtinSysDateWithoutFspSig) evalTime(ctx EvalContext, row chunk.Row) (val types.Time, isNull bool, err error) {
tz := location(ctx)
now := time.Now().In(tz)
result, err := convertTimeToMysqlTime(now, 0, types.ModeTruncate)
result, err := convertTimeToMysqlTime(now, 0, types.ModeHalfUp)
if err != nil {
return types.ZeroTime, true, err
}
Expand Down Expand Up @@ -2408,7 +2409,7 @@ func evalUTCTimestampWithFsp(ctx EvalContext, fsp int) (types.Time, bool, error)
if err != nil {
return types.ZeroTime, true, err
}
result, err := convertTimeToMysqlTime(nowTs.UTC(), fsp, types.ModeTruncate)
result, err := convertTimeToMysqlTime(nowTs.UTC(), fsp, types.ModeHalfUp)
if err != nil {
return types.ZeroTime, true, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/expression/builtin_time_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (b *builtinSysDateWithoutFspSig) vecEvalTime(ctx EvalContext, input *chunk.

result.ResizeTime(n, false)
times := result.Times()
t, err := convertTimeToMysqlTime(now, 0, types.ModeTruncate)
t, err := convertTimeToMysqlTime(now, 0, types.ModeHalfUp)
if err != nil {
return err
}
Expand Down Expand Up @@ -765,7 +765,7 @@ func (b *builtinSysDateWithFspSig) vecEvalTime(ctx EvalContext, input *chunk.Chu
if result.IsNull(i) {
continue
}
t, err := convertTimeToMysqlTime(now, int(ds[i]), types.ModeTruncate)
t, err := convertTimeToMysqlTime(now, int(ds[i]), types.ModeHalfUp)
if err != nil {
return err
}
Expand Down
21 changes: 5 additions & 16 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,28 +1433,17 @@ func TestIssue9710(t *testing.T) {
}

for {
rs := tk.MustQuery("select now(), now(6), unix_timestamp(), unix_timestamp(now()), unix_timestamp(now(6)), utc_timestamp(), utc_timestamp(6), sysdate(), sysdate(6)")
rs := tk.MustQuery("select now(), now(6), unix_timestamp(), unix_timestamp(now())")
s, ms := getSAndMS(rs.Rows()[0][1].(string))
if ms < 500000 {
time.Sleep(time.Second / 10)
continue
}

s1, _ := getSAndMS(rs.Rows()[0][0].(string))
require.Equal(t, s, s1) // now() will truncate the result instead of rounding it

require.Equal(t, rs.Rows()[0][2], rs.Rows()[0][3]) // unix_timestamp() will truncate the result
ut, _ := getSAndMS(rs.Rows()[0][2].(string))
ut6, _ := getSAndMS(rs.Rows()[0][4].(string))
require.Equal(t, ut, ut6) // unix_timestamp() will truncate the result

u0, _ := getSAndMS(rs.Rows()[0][5].(string))
u6, _ := getSAndMS(rs.Rows()[0][6].(string))
require.Equal(t, u0, u6) // utc_timestamp() will truncate the result
s0, _ := getSAndMS(rs.Rows()[0][7].(string))
s6, _ := getSAndMS(rs.Rows()[0][8].(string))
require.Equal(t, s0, s6) // sysdate() will truncate the result

if ms < 500000 {
time.Sleep(time.Second / 10)
continue
}
break
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integrationtest/r/expression/time.result
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,10 @@ from_unixtime(dc)
update t set d = from_unixtime(dc), dt = from_unixtime(dc), dt2 = from_unixtime(dc), dt6 = from_unixtime(dc), ts = from_unixtime(dc), ts2 = from_unixtime(dc), ts6 = from_unixtime(dc);
select * from t order by id;
id d dt dt2 dt6 ts ts2 ts6 time_t dc
1 2024-01-01 2024-01-01 00:00:00 2023-12-31 23:59:59.99 2023-12-31 23:59:59.987679 2024-01-01 00:00:00 2023-12-31 23:59:59.99 2023-12-31 23:59:59.987679 1704067200 1704067199.987678900
1 2023-12-31 2024-01-01 00:00:00 2023-12-31 23:59:59.99 2023-12-31 23:59:59.987679 2024-01-01 00:00:00 2023-12-31 23:59:59.99 2023-12-31 23:59:59.987679 1704067200 1704067199.987678900
2 2023-12-31 2023-12-31 23:59:59 2023-12-31 23:59:59.12 2023-12-31 23:59:59.123432 2023-12-31 23:59:59 2023-12-31 23:59:59.12 2023-12-31 23:59:59.123432 1704067199 1704067199.123432100
3 2023-12-31 2023-12-31 23:59:59 2023-12-31 23:59:59.50 2023-12-31 23:59:59.499999 2023-12-31 23:59:59 2023-12-31 23:59:59.50 2023-12-31 23:59:59.499999 1704067199 1704067199.499999000
4 2024-01-01 2024-01-01 00:00:00 2023-12-31 23:59:59.50 2023-12-31 23:59:59.500000 2024-01-01 00:00:00 2023-12-31 23:59:59.50 2023-12-31 23:59:59.500000 1704067199 1704067199.499999600
4 2023-12-31 2024-01-01 00:00:00 2023-12-31 23:59:59.50 2023-12-31 23:59:59.500000 2024-01-01 00:00:00 2023-12-31 23:59:59.50 2023-12-31 23:59:59.500000 1704067199 1704067199.499999600
truncate table t;
set sql_mode=concat(@@sql_mode,",TIME_TRUNCATE_FRACTIONAL");
insert into t values
Expand Down

0 comments on commit 1d01a4a

Please sign in to comment.