Skip to content

Commit cd1e029

Browse files
authored
expression: fix wrong result when convert float to unsigned (pingcap#53590) (pingcap#53612)
close pingcap#41736
1 parent c2f973a commit cd1e029

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

expression/builtin_compare.go

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

sessionctx/stmtctx/stmtctx.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ type StatementContext struct {
151151
// IsDDLJobInQueue is used to mark whether the DDL job is put into the queue.
152152
// If IsDDLJobInQueue is true, it means the DDL job is in the queue of storage, and it can be handled by the DDL worker.
153153
IsDDLJobInQueue bool
154+
IsRefineComparedConstant bool
154155
DDLJobID int64
155156
InInsertStmt bool
156157
InUpdateStmt bool
@@ -1097,7 +1098,7 @@ func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails {
10971098
// This is the case for `insert`, `update`, `alter table`, `create table` and `load data infile` statements, when not in strict SQL mode.
10981099
// see https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
10991100
func (sc *StatementContext) ShouldClipToZero() bool {
1100-
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue
1101+
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue || sc.IsRefineComparedConstant
11011102
}
11021103

11031104
// 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)