Skip to content

Commit d9c5bf6

Browse files
authored
expression: fix wrong result when convert float to unsigned (#53590) (#53621)
close #41736
1 parent 4399d4a commit d9c5bf6

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

expression/builtin_compare.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,12 @@ func RefineComparedConstant(ctx sessionctx.Context, targetFieldType types.FieldT
14811481
targetFieldType = *types.NewFieldType(mysql.TypeLonglong)
14821482
}
14831483
var intDatum types.Datum
1484+
// To make sure return zero when underflow happens.
1485+
oriFlag := sc.IsRefineComparedConstant
1486+
sc.IsRefineComparedConstant = true
1487+
defer func() {
1488+
sc.IsRefineComparedConstant = oriFlag
1489+
}()
14841490
intDatum, err = dt.ConvertTo(sc, &targetFieldType)
14851491
if err != nil {
14861492
if terror.ErrorEqual(err, types.ErrOverflow) {

sessionctx/stmtctx/stmtctx.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ type StatementContext struct {
181181
IsStaleness bool
182182
InRestrictedSQL bool
183183
ViewDepth int32
184+
185+
IsRefineComparedConstant bool
186+
184187
// mu struct holds variables that change during execution.
185188
mu struct {
186189
sync.Mutex
@@ -994,7 +997,7 @@ func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails {
994997
// This is the case for `insert`, `update`, `alter table`, `create table` and `load data infile` statements, when not in strict SQL mode.
995998
// see https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
996999
func (sc *StatementContext) ShouldClipToZero() bool {
997-
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue
1000+
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue || sc.IsRefineComparedConstant
9981001
}
9991002

10001003
// ShouldIgnoreOverflowError indicates whether we should ignore the error when type conversion overflows,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
drop table if exists t0;
2+
create table t0(c0 tinyint(1) unsigned not null );
3+
insert into t0 values (1);
4+
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
5+
c0
6+
1
7+
select t0.c0 > -1.194192591e9 from t0;
8+
t0.c0 > -1.194192591e9
9+
1
10+
select t0.c0 < -1.194192591e9 from t0;
11+
t0.c0 < -1.194192591e9
12+
0
13+
select -1.194192591e9 > t0.c0 from t0;
14+
-1.194192591e9 > t0.c0
15+
0
16+
select -1.194192591e9 < t0.c0 from t0;
17+
-1.194192591e9 < t0.c0
18+
1
19+
select t0.c0 > 1.194192591e9 from t0;
20+
t0.c0 > 1.194192591e9
21+
0
22+
select t0.c0 < 1.194192591e9 from t0;
23+
t0.c0 < 1.194192591e9
24+
1
25+
select 1.194192591e9 > t0.c0 from t0;
26+
1.194192591e9 > t0.c0
27+
1
28+
select 1.194192591e9 < t0.c0 from t0;
29+
1.194192591e9 < t0.c0
30+
0
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TestNegFloatConvertToUnsigned
2+
drop table if exists t0;
3+
create table t0(c0 tinyint(1) unsigned not null );
4+
insert into t0 values (1);
5+
select * from t0 where case 0 when t0.c0 > -1.194192591e9 then null else 1 end;
6+
select t0.c0 > -1.194192591e9 from t0;
7+
select t0.c0 < -1.194192591e9 from t0;
8+
select -1.194192591e9 > t0.c0 from t0;
9+
select -1.194192591e9 < t0.c0 from t0;
10+
select t0.c0 > 1.194192591e9 from t0;
11+
select t0.c0 < 1.194192591e9 from t0;
12+
select 1.194192591e9 > t0.c0 from t0;
13+
select 1.194192591e9 < t0.c0 from t0;

0 commit comments

Comments
 (0)