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

expression: fix wrong result when convert float to unsigned #53590

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion pkg/expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,10 @@ func RefineComparedConstant(ctx BuildContext, targetFieldType types.FieldType, c
targetFieldType = *types.NewFieldType(mysql.TypeLonglong)
}
var intDatum types.Datum
intDatum, err = dt.ConvertTo(evalCtx.TypeCtx(), &targetFieldType)
// Disable AllowNegativeToUnsigned to make sure return 0 when underflow happens.
oriTypeCtx := evalCtx.TypeCtx()
newTypeCtx := oriTypeCtx.WithFlags(oriTypeCtx.Flags().WithAllowNegativeToUnsigned(false))
intDatum, err = dt.ConvertTo(newTypeCtx, &targetFieldType)
if err != nil {
if terror.ErrorEqual(err, types.ErrOverflow) {
return &Constant{
Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/integration_test/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 = 26,
shard_count = 27,
deps = [
"//pkg/config",
"//pkg/domain",
Expand Down
11 changes: 11 additions & 0 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3032,3 +3032,14 @@ func TestIssue43527(t *testing.T) {
"SELECT @total := @total + d FROM (SELECT d FROM test) AS temp, (SELECT @total := b FROM test) AS T1 where @total >= 100",
).Check(testkit.Rows("200", "300", "400", "500"))
}

guo-shaoge marked this conversation as resolved.
Show resolved Hide resolved
func TestNegFloatConvertToUnsigned(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t0")
tk.MustExec("create table t0(c0 tinyint(1) unsigned not null )")
tk.MustExec("insert into t0 values (1)")
tk.MustQuery("select * from t0 where case 0 when (t0.c0)>(-1.194192591e9) then null else 1 end;").Check(testkit.Rows("1"))
tk.MustQuery("select (t0.c0)>(-1.194192591e9) from t0;").Check(testkit.Rows("1"))
}