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 (pingcap#53075)

close pingcap#52978
  • Loading branch information
yibin87 authored and terry1purcell committed May 17, 2024
1 parent 9bf6532 commit 353afa0
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 @@ -635,3 +635,15 @@ func TestIssue51874(t *testing.T) {
tk.MustExec("insert into t2 values (10), (100)")
tk.MustQuery("select (select sum(a) over () from t2 limit 1) from t;").Check(testkit.Rows("10", "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 353afa0

Please sign in to comment.