diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index 5019be879e8..03e91d12a3f 100644 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -405,19 +405,25 @@ struct TiDBConvertToInteger context.getDAGContext()->handleTruncateError("cast str as int"); return static_cast(0); } - if constexpr (to_unsigned) + bool is_negative = false; + if (int_string.data[0] == '-') + { + is_negative = true; + } + if (!is_negative) { auto [value, err] = toUInt(int_string); if (err == OVERFLOW_ERR) context.getDAGContext()->handleOverflowError("cast str as int"); - return value; + return static_cast(value); } else { + /// TODO: append warning CastAsSignedOverflow if try to cast negative value to unsigned auto [value, err] = toInt(int_string); if (err == OVERFLOW_ERR) context.getDAGContext()->handleOverflowError("cast str as int"); - return value; + return static_cast(value); } } diff --git a/tests/fullstack-test/expr/cast_string_as_int.test b/tests/fullstack-test/expr/cast_string_as_int.test index 999b0dfd776..88fd235a488 100644 --- a/tests/fullstack-test/expr/cast_string_as_int.test +++ b/tests/fullstack-test/expr/cast_string_as_int.test @@ -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 | +---------+