Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: Truncate FSP instead of rounding for utc_timestamp() | tidb-test=pr/2405 #56431

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions pkg/expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,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.ModeHalfUp)
result, err := convertTimeToMysqlTime(now, int(fsp), types.ModeTruncate)
if err != nil {
return types.ZeroTime, true, err
}
Expand All @@ -2082,7 +2082,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.ModeHalfUp)
result, err := convertTimeToMysqlTime(now, 0, types.ModeTruncate)
if err != nil {
return types.ZeroTime, true, err
}
Expand Down Expand Up @@ -2404,7 +2404,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.ModeHalfUp)
result, err := convertTimeToMysqlTime(nowTs.UTC(), fsp, types.ModeTruncate)
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.ModeHalfUp)
t, err := convertTimeToMysqlTime(now, 0, types.ModeTruncate)
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.ModeHalfUp)
t, err := convertTimeToMysqlTime(now, int(ds[i]), types.ModeTruncate)
if err != nil {
return err
}
Expand Down
21 changes: 16 additions & 5 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,17 +1433,28 @@ func TestIssue9710(t *testing.T) {
}

for {
rs := tk.MustQuery("select now(), now(6), unix_timestamp(), unix_timestamp(now())")
rs := tk.MustQuery("select now(), now(6), unix_timestamp(), unix_timestamp(now()), unix_timestamp(now(6)), utc_timestamp(), utc_timestamp(6), sysdate(), sysdate(6)")
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