Skip to content

Commit

Permalink
executor,types: Fix truncate function behavior when second param is l…
Browse files Browse the repository at this point in the history
…arge negative (#53075) (#53125)

close #52978
  • Loading branch information
ti-chi-bot authored May 10, 2024
1 parent 12c763f commit 99f4d94
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/executor/test/issuetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 17,
shard_count = 18,
deps = [
"//pkg/autoid_service",
"//pkg/config",
Expand Down
12 changes: 12 additions & 0 deletions pkg/executor/test/issuetest/executor_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,15 @@ func TestIssue42662(t *testing.T) {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/executor/issue42662_1"))
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/util/servermemorylimit/issue42662_2"))
}

func TestIssue52978(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int)")
tk.MustExec("insert into t values (-1790816583),(2049821819), (-1366665321), (536581933), (-1613686445)")
tk.MustQuery("select min(truncate(cast(-26340 as double), ref_11.a)) as c3 from t as ref_11;").Check(testkit.Rows("-26340"))
tk.MustExec("drop table if exists t")
}
6 changes: 6 additions & 0 deletions pkg/types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func Truncate(f float64, dec int) float64 {
if math.IsInf(tmp, 0) {
return f
}
if shift == 0.0 {
if math.IsNaN(f) {
return f
}
return 0.0
}
return math.Trunc(tmp) / shift
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/types/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func TestTruncate(t *testing.T) {
{123.45, 1, 123.4},
{123.45, 2, 123.45},
{123.45, 3, 123.450},
{123.45, -400, 0.0},
{123.45, 400, 123.45},
}
for _, tt := range tests {
res := Truncate(tt.f, tt.dec)
Expand Down

0 comments on commit 99f4d94

Please sign in to comment.