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

executor,types: Fix truncate function behavior when second param is large negative (#53075) #53124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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