Skip to content

Commit 2bd7d13

Browse files
guo-shaogeti-chi-bot
authored andcommitted
expression: fix wrong result when convert float to unsigned (pingcap#53590)
close pingcap#41736
1 parent 30c2ca0 commit 2bd7d13

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

pkg/expression/builtin_compare.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,10 @@ func RefineComparedConstant(ctx BuildContext, targetFieldType types.FieldType, c
14281428
targetFieldType = *types.NewFieldType(mysql.TypeLonglong)
14291429
}
14301430
var intDatum types.Datum
1431-
intDatum, err = dt.ConvertTo(evalCtx.TypeCtx(), &targetFieldType)
1431+
// Disable AllowNegativeToUnsigned to make sure return 0 when underflow happens.
1432+
oriTypeCtx := evalCtx.TypeCtx()
1433+
newTypeCtx := oriTypeCtx.WithFlags(oriTypeCtx.Flags().WithAllowNegativeToUnsigned(false))
1434+
intDatum, err = dt.ConvertTo(newTypeCtx, &targetFieldType)
14321435
if err != nil {
14331436
if terror.ErrorEqual(err, types.ErrOverflow) {
14341437
return &Constant{

tests/integrationtest/r/expression/cast.result

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,33 @@ select null as a union all select 'a' as a;
118118
a
119119
NULL
120120
a
121+
drop table if exists t0;
122+
create table t0(c0 tinyint(1) unsigned not null );
123+
insert into t0 values (1);
124+
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
125+
c0
126+
1
127+
select t0.c0 > -1.194192591e9 from t0;
128+
t0.c0 > -1.194192591e9
129+
1
130+
select t0.c0 < -1.194192591e9 from t0;
131+
t0.c0 < -1.194192591e9
132+
0
133+
select -1.194192591e9 > t0.c0 from t0;
134+
-1.194192591e9 > t0.c0
135+
0
136+
select -1.194192591e9 < t0.c0 from t0;
137+
-1.194192591e9 < t0.c0
138+
1
139+
select t0.c0 > 1.194192591e9 from t0;
140+
t0.c0 > 1.194192591e9
141+
0
142+
select t0.c0 < 1.194192591e9 from t0;
143+
t0.c0 < 1.194192591e9
144+
1
145+
select 1.194192591e9 > t0.c0 from t0;
146+
1.194192591e9 > t0.c0
147+
1
148+
select 1.194192591e9 < t0.c0 from t0;
149+
1.194192591e9 < t0.c0
150+
0

tests/integrationtest/t/expression/cast.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,17 @@ select cast(cast('14:15' as time) as year) = YEAR(CURDATE());
7373
explain select null as a union all select 'a' as a;
7474
--sorted_result
7575
select null as a union all select 'a' as a;
76+
77+
# TestNegFloatConvertToUnsigned
78+
drop table if exists t0;
79+
create table t0(c0 tinyint(1) unsigned not null );
80+
insert into t0 values (1);
81+
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
82+
select t0.c0 > -1.194192591e9 from t0;
83+
select t0.c0 < -1.194192591e9 from t0;
84+
select -1.194192591e9 > t0.c0 from t0;
85+
select -1.194192591e9 < t0.c0 from t0;
86+
select t0.c0 > 1.194192591e9 from t0;
87+
select t0.c0 < 1.194192591e9 from t0;
88+
select 1.194192591e9 > t0.c0 from t0;
89+
select 1.194192591e9 < t0.c0 from t0;

0 commit comments

Comments
 (0)