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 to decimal wrong sign #9300

Merged
merged 7 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 3 additions & 2 deletions dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -1012,12 +1012,13 @@ struct TiDBConvertToDecimal
}
else if (v_scale > scale)
{
const bool need_to_round = ((value < 0 ? -value : value) % scale_mul) >= (scale_mul / 2);
const bool neg = (value < 0);
const bool need_to_round = ((neg ? -value : value) % scale_mul) >= (scale_mul / 2);
auto old_value = value;
value /= scale_mul;
if (need_to_round)
{
if (value < 0)
if (neg)
--value;
else
++value;
Expand Down
25 changes: 25 additions & 0 deletions tests/fullstack-test/expr/cast_as_decimal.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,28 @@ mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1;
+------------------------------------+
| 20221010101010.123 |
+------------------------------------+

mysql> drop table if exists test.t2;
mysql> create table test.t2(d decimal(11, 4));
mysql> alter table test.t2 set tiflash replica 1;
mysql> insert into test.t2 values(-0.741);
mysql> alter table test.t2 set tiflash replica 1;
func> wait_table test t2
mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; SELECT cast(d as decimal) from t2;
+--------------------+
| cast(d as decimal) |
+--------------------+
| -1 |
+--------------------+

mysql> drop table if exists test.t2;
mysql> create table t2 (c1 int not null, c2 int not null, primary key(c1) CLUSTERED);
mysql> alter table t2 set tiflash replica 1;
mysql> insert into t2 (c1,c2) values (1486109909, -1113200806);
func> wait_table test t2
mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; select c2, c1, cast( (c2 / cast(c1 as signed)) as decimal) as c2 from t2;
+-------------+------------+------+
| c2 | c1 | c2 |
+-------------+------------+------+
| -1113200806 | 1486109909 | -1 |
+-------------+------------+------+