Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: fix wrong result when convert float to unsigned (#53590) #53622

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,12 @@ func RefineComparedConstant(ctx sessionctx.Context, targetFieldType types.FieldT
targetFieldType = *types.NewFieldType(mysql.TypeLonglong)
}
var intDatum types.Datum
// To make sure return zero when underflow happens.
oriFlag := sc.IsRefineComparedConstant
sc.IsRefineComparedConstant = true
defer func() {
sc.IsRefineComparedConstant = oriFlag
}()
intDatum, err = dt.ConvertTo(sc, &targetFieldType)
if err != nil {
if terror.ErrorEqual(err, types.ErrOverflow) {
Expand Down
5 changes: 4 additions & 1 deletion sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ type StatementContext struct {
SkipUTF8Check bool
SkipASCIICheck bool
SkipUTF8MB4Check bool

IsRefineComparedConstant bool

// If the select statement was like 'select * from t as of timestamp ...' or in a stale read transaction
// or is affected by the tidb_read_staleness session variable, then the statement will be makred as isStaleness
// in stmtCtx
Expand Down Expand Up @@ -775,7 +778,7 @@ func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails {
// This is the case for `insert`, `update`, `alter table`, `create table` and `load data infile` statements, when not in strict SQL mode.
// see https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
func (sc *StatementContext) ShouldClipToZero() bool {
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue || sc.IsRefineComparedConstant
}

// ShouldIgnoreOverflowError indicates whether we should ignore the error when type conversion overflows,
Expand Down
30 changes: 30 additions & 0 deletions tests/integrationtest/r/expression/cast.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
13 changes: 13 additions & 0 deletions tests/integrationtest/t/expression/cast.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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;