Skip to content

Commit

Permalink
Fix cast negative number string as unsigned int (#1762) (#1768)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Apr 15, 2021
1 parent 0f70eaa commit d42e2a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 9 additions & 3 deletions dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,19 +405,25 @@ struct TiDBConvertToInteger
context.getDAGContext()->handleTruncateError("cast str as int");
return static_cast<T>(0);
}
if constexpr (to_unsigned)
bool is_negative = false;
if (int_string.data[0] == '-')
{
is_negative = true;
}
if (!is_negative)
{
auto [value, err] = toUInt<T>(int_string);
if (err == OVERFLOW_ERR)
context.getDAGContext()->handleOverflowError("cast str as int");
return value;
return static_cast<T>(value);
}
else
{
/// TODO: append warning CastAsSignedOverflow if try to cast negative value to unsigned
auto [value, err] = toInt<T>(int_string);
if (err == OVERFLOW_ERR)
context.getDAGContext()->handleOverflowError("cast str as int");
return value;
return static_cast<T>(value);
}
}

Expand Down
12 changes: 8 additions & 4 deletions tests/fullstack-test/expr/cast_string_as_int.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ mysql> insert into test.t values('2006-01')

func> wait_table test t

mysql> set @@tidb_isolation_read_engines='tiflash'

# Issue https://github.com/pingcap/tics/issues/1469

mysql> select * from test.t where cast(a as signed) = -123
mysql> set @@tidb_isolation_read_engines='tiflash'; select * from test.t where cast(a as signed) = -123
+------+
| a |
+------+
| -123 |
+------+
mysql> set @@tidb_isolation_read_engines='tiflash'; select * from test.t where cast(a as unsigned) = cast(-123 as unsigned)
+------+
| a |
+------+
| -123 |
+------+
mysql> select * from test.t where cast(a as signed) = 2006
mysql> set @@tidb_isolation_read_engines='tiflash'; select * from test.t where cast(a as signed) = 2006
+---------+
| a |
+---------+
Expand Down

0 comments on commit d42e2a0

Please sign in to comment.