Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#53590
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
guo-shaoge authored and ti-chi-bot committed May 28, 2024
1 parent 6a6455a commit 3d5dd84
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,14 @@ func RefineComparedConstant(ctx sessionctx.Context, targetFieldType types.FieldT
targetFieldType = *types.NewFieldType(mysql.TypeLonglong)
}
var intDatum types.Datum
<<<<<<< HEAD
intDatum, err = dt.ConvertTo(sc, &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)
>>>>>>> 68d12954fe4 (expression: fix wrong result when convert float to unsigned (#53590))
if err != nil {
if terror.ErrorEqual(err, types.ErrOverflow) {
return &Constant{
Expand Down
30 changes: 30 additions & 0 deletions tests/integrationtest/r/expression/cast.result
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,33 @@ select null as a union all select 'a' as a;
a
NULL
a
drop table if exists t0;
create table t0(c0 tinyint(1) unsigned not null );
insert into t0 values (1);
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
c0
1
select t0.c0 > -1.194192591e9 from t0;
t0.c0 > -1.194192591e9
1
select t0.c0 < -1.194192591e9 from t0;
t0.c0 < -1.194192591e9
0
select -1.194192591e9 > t0.c0 from t0;
-1.194192591e9 > t0.c0
0
select -1.194192591e9 < t0.c0 from t0;
-1.194192591e9 < t0.c0
1
select t0.c0 > 1.194192591e9 from t0;
t0.c0 > 1.194192591e9
0
select t0.c0 < 1.194192591e9 from t0;
t0.c0 < 1.194192591e9
1
select 1.194192591e9 > t0.c0 from t0;
1.194192591e9 > t0.c0
1
select 1.194192591e9 < t0.c0 from t0;
1.194192591e9 < t0.c0
0
14 changes: 14 additions & 0 deletions tests/integrationtest/t/expression/cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ select cast('61qw' as decimal);
explain select null as a union all select 'a' as a;
--sorted_result
select null as a union all select 'a' as a;

# TestNegFloatConvertToUnsigned
drop table if exists t0;
create table t0(c0 tinyint(1) unsigned not null );
insert into t0 values (1);
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
select t0.c0 > -1.194192591e9 from t0;
select t0.c0 < -1.194192591e9 from t0;
select -1.194192591e9 > t0.c0 from t0;
select -1.194192591e9 < t0.c0 from t0;
select t0.c0 > 1.194192591e9 from t0;
select t0.c0 < 1.194192591e9 from t0;
select 1.194192591e9 > t0.c0 from t0;
select 1.194192591e9 < t0.c0 from t0;

0 comments on commit 3d5dd84

Please sign in to comment.