From 05e2fb5f7bef18244cbec276899101d9e8c7ff25 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Thu, 8 Aug 2024 11:16:08 +0800 Subject: [PATCH 1/4] fix cast to decimal(10, 0) Signed-off-by: guo-shaoge --- dbms/src/Functions/FunctionsTiDBConversion.h | 5 +++-- tests/fullstack-test/expr/cast_as_decimal.test | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index ec56e599b4c..d1b9710d758 100755 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -951,12 +951,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; diff --git a/tests/fullstack-test/expr/cast_as_decimal.test b/tests/fullstack-test/expr/cast_as_decimal.test index aa85c592ec4..2343c7a8695 100644 --- a/tests/fullstack-test/expr/cast_as_decimal.test +++ b/tests/fullstack-test/expr/cast_as_decimal.test @@ -35,3 +35,14 @@ mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; +------------------------------------+ | 20221010101010.123 | +------------------------------------+ + +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; +mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; SELECT cast(d as decimal) from t2; ++--------------------+ +| cast(d as decimal) | ++--------------------+ +| -1 | ++--------------------+ From a2d9e3488d10564909ba813e526fc89e4d41c4a3 Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Thu, 8 Aug 2024 11:19:48 +0800 Subject: [PATCH 2/4] refine case Signed-off-by: guo-shaoge --- tests/fullstack-test/expr/cast_as_decimal.test | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fullstack-test/expr/cast_as_decimal.test b/tests/fullstack-test/expr/cast_as_decimal.test index 2343c7a8695..04ecb583093 100644 --- a/tests/fullstack-test/expr/cast_as_decimal.test +++ b/tests/fullstack-test/expr/cast_as_decimal.test @@ -40,6 +40,7 @@ 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) | From a860a8b6e35fb7bc67001d5e48feb6696a73d48f Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Thu, 8 Aug 2024 14:02:32 +0800 Subject: [PATCH 3/4] fix case Signed-off-by: guo-shaoge --- tests/fullstack-test/expr/cast_as_decimal.test | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/fullstack-test/expr/cast_as_decimal.test b/tests/fullstack-test/expr/cast_as_decimal.test index 04ecb583093..11978fcc9b0 100644 --- a/tests/fullstack-test/expr/cast_as_decimal.test +++ b/tests/fullstack-test/expr/cast_as_decimal.test @@ -36,6 +36,7 @@ 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); @@ -47,3 +48,15 @@ mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; +--------------------+ | -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 | ++-------------+------------+------+ From aad65d89ee455e68d9a8ea85bbe22e5b7536de8f Mon Sep 17 00:00:00 2001 From: guo-shaoge Date: Thu, 8 Aug 2024 19:40:02 +0800 Subject: [PATCH 4/4] case Signed-off-by: guo-shaoge --- tests/fullstack-test/expr/cast_as_decimal.test | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/fullstack-test/expr/cast_as_decimal.test b/tests/fullstack-test/expr/cast_as_decimal.test index 11978fcc9b0..a375433b843 100644 --- a/tests/fullstack-test/expr/cast_as_decimal.test +++ b/tests/fullstack-test/expr/cast_as_decimal.test @@ -42,7 +42,7 @@ 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; +mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 1; SELECT cast(d as decimal) from test.t2; +--------------------+ | cast(d as decimal) | +--------------------+ @@ -50,11 +50,11 @@ mysql> set @@tidb_isolation_read_engines='tiflash'; set @@tidb_enforce_mpp = 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); +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 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 | +-------------+------------+------+