From 16e6bd29e1d96296ba8925f760371b544b91edf8 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Fri, 9 Aug 2024 14:02:12 +0800 Subject: [PATCH 1/2] This is an automated cherry-pick of #9300 Signed-off-by: ti-chi-bot --- dbms/src/Functions/FunctionsTiDBConversion.h | 8 +++++- .../fullstack-test/expr/cast_as_decimal.test | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index c4baf909e57..4298a4f2a87 100644 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -952,12 +952,18 @@ struct TiDBConvertToDecimal } else if (v_scale > scale) { +<<<<<<< HEAD context.getDAGContext()->handleTruncateError("cast decimal as decimal"); 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; +>>>>>>> 0861ee62af (fix cast to decimal wrong sign (#9300)) value /= scale_mul; if (need_to_round) { - if (value < 0) + if (neg) --value; else ++value; diff --git a/tests/fullstack-test/expr/cast_as_decimal.test b/tests/fullstack-test/expr/cast_as_decimal.test index aa85c592ec4..a375433b843 100644 --- a/tests/fullstack-test/expr/cast_as_decimal.test +++ b/tests/fullstack-test/expr/cast_as_decimal.test @@ -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 test.t2; ++--------------------+ +| cast(d as decimal) | ++--------------------+ +| -1 | ++--------------------+ + +mysql> drop table if exists test.t2; +mysql> create table test.t2 (c1 int not null, c2 int not null, primary key(c1) CLUSTERED); +mysql> alter table test.t2 set tiflash replica 1; +mysql> insert into test.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 test.t2; ++-------------+------------+------+ +| c2 | c1 | c2 | ++-------------+------------+------+ +| -1113200806 | 1486109909 | -1 | ++-------------+------------+------+ From 9e3838d35e4db9d1a97bfc283da80ab52495883e Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Tue, 24 Sep 2024 17:01:22 +0800 Subject: [PATCH 2/2] fix conflict Signed-off-by: guo-shaoge --- dbms/src/Functions/FunctionsTiDBConversion.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index 4298a4f2a87..15a8d371de4 100644 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -952,14 +952,9 @@ struct TiDBConvertToDecimal } else if (v_scale > scale) { -<<<<<<< HEAD context.getDAGContext()->handleTruncateError("cast decimal as decimal"); - 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; ->>>>>>> 0861ee62af (fix cast to decimal wrong sign (#9300)) value /= scale_mul; if (need_to_round) {