From 99f4d94c2eea044c7f7e0f699e6a1ea03aed39d2 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 10 May 2024 13:29:39 +0800 Subject: [PATCH] executor,types: Fix truncate function behavior when second param is large negative (#53075) (#53125) close pingcap/tidb#52978 --- pkg/executor/test/issuetest/BUILD.bazel | 2 +- pkg/executor/test/issuetest/executor_issue_test.go | 12 ++++++++++++ pkg/types/helper.go | 6 ++++++ pkg/types/helper_test.go | 2 ++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/executor/test/issuetest/BUILD.bazel b/pkg/executor/test/issuetest/BUILD.bazel index 3810bcaefa89c..03b06badefecd 100644 --- a/pkg/executor/test/issuetest/BUILD.bazel +++ b/pkg/executor/test/issuetest/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 17, + shard_count = 18, deps = [ "//pkg/autoid_service", "//pkg/config", diff --git a/pkg/executor/test/issuetest/executor_issue_test.go b/pkg/executor/test/issuetest/executor_issue_test.go index f47d02e592c57..44f1b0399a078 100644 --- a/pkg/executor/test/issuetest/executor_issue_test.go +++ b/pkg/executor/test/issuetest/executor_issue_test.go @@ -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") +} diff --git a/pkg/types/helper.go b/pkg/types/helper.go index e3cc4aaf68cd9..4f457a1d2b97e 100644 --- a/pkg/types/helper.go +++ b/pkg/types/helper.go @@ -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 } diff --git a/pkg/types/helper_test.go b/pkg/types/helper_test.go index dc3fc9093aad3..a5092e515cefb 100644 --- a/pkg/types/helper_test.go +++ b/pkg/types/helper_test.go @@ -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)