Skip to content

Commit f602ee2

Browse files
authored
expression: fix wrong result when convert float to unsigned (#53590) (#53622)
close #41736
1 parent 9541258 commit f602ee2

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
@@ -132,6 +132,9 @@ type StatementContext struct {
132132
SkipUTF8Check bool
133133
SkipASCIICheck bool
134134
SkipUTF8MB4Check bool
135+
136+
IsRefineComparedConstant bool
137+
135138
// If the select statement was like 'select * from t as of timestamp ...' or in a stale read transaction
136139
// or is affected by the tidb_read_staleness session variable, then the statement will be makred as isStaleness
137140
// in stmtCtx
@@ -775,7 +778,7 @@ func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails {
775778
// This is the case for `insert`, `update`, `alter table`, `create table` and `load data infile` statements, when not in strict SQL mode.
776779
// see https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
777780
func (sc *StatementContext) ShouldClipToZero() bool {
778-
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue
781+
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue || sc.IsRefineComparedConstant
779782
}
780783

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