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

Fix cast negative number string as unsigned int (#1762) #1768

Merged
merged 2 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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