Skip to content

Commit 353afa0

Browse files
yibin87terry1purcell
authored andcommitted
executor,types: Fix truncate function behavior when second param is large negative (pingcap#53075)
close pingcap#52978
1 parent 9bf6532 commit 353afa0

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

pkg/executor/test/issuetest/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ go_test(
88
"main_test.go",
99
],
1010
flaky = True,
11-
shard_count = 17,
11+
shard_count = 18,
1212
deps = [
1313
"//pkg/autoid_service",
1414
"//pkg/config",

pkg/executor/test/issuetest/executor_issue_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,15 @@ func TestIssue51874(t *testing.T) {
635635
tk.MustExec("insert into t2 values (10), (100)")
636636
tk.MustQuery("select (select sum(a) over () from t2 limit 1) from t;").Check(testkit.Rows("10", "2"))
637637
}
638+
639+
func TestIssue52978(t *testing.T) {
640+
store := testkit.CreateMockStore(t)
641+
tk := testkit.NewTestKit(t, store)
642+
643+
tk.MustExec("use test")
644+
tk.MustExec("drop table if exists t")
645+
tk.MustExec("create table t (a int)")
646+
tk.MustExec("insert into t values (-1790816583),(2049821819), (-1366665321), (536581933), (-1613686445)")
647+
tk.MustQuery("select min(truncate(cast(-26340 as double), ref_11.a)) as c3 from t as ref_11;").Check(testkit.Rows("-26340"))
648+
tk.MustExec("drop table if exists t")
649+
}

pkg/types/helper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ func Truncate(f float64, dec int) float64 {
5858
if math.IsInf(tmp, 0) {
5959
return f
6060
}
61+
if shift == 0.0 {
62+
if math.IsNaN(f) {
63+
return f
64+
}
65+
return 0.0
66+
}
6167
return math.Trunc(tmp) / shift
6268
}
6369

pkg/types/helper_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func TestTruncate(t *testing.T) {
5252
{123.45, 1, 123.4},
5353
{123.45, 2, 123.45},
5454
{123.45, 3, 123.450},
55+
{123.45, -400, 0.0},
56+
{123.45, 400, 123.45},
5557
}
5658
for _, tt := range tests {
5759
res := Truncate(tt.f, tt.dec)

0 commit comments

Comments
 (0)