From e96161ac9d2b1711718e93d9c7de43e6422f7d54 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 25 Apr 2023 20:01:51 +0800 Subject: [PATCH] truncate should always be treated as warning in cast decimal as decimal (#7350) (#7378) close pingcap/tiflash#7348 --- dbms/src/Functions/FunctionsTiDBConversion.h | 4 +++- .../Functions/tests/gtest_tidb_conversion.cpp | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index e1ba48f696e..9b2cab4994b 100755 --- a/dbms/src/Functions/FunctionsTiDBConversion.h +++ b/dbms/src/Functions/FunctionsTiDBConversion.h @@ -951,8 +951,8 @@ struct TiDBConvertToDecimal } else if (v_scale > scale) { - context.getDAGContext()->handleTruncateError("cast decimal as decimal"); const bool need_to_round = ((value < 0 ? -value : value) % scale_mul) >= (scale_mul / 2); + auto old_value = value; value /= scale_mul; if (need_to_round) { @@ -961,6 +961,8 @@ struct TiDBConvertToDecimal else ++value; } + if (old_value != value * scale_mul) + context.getDAGContext()->appendWarning("Truncate in cast decimal as decimal"); } else { diff --git a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp index 941ac240640..be8e6eae77e 100644 --- a/dbms/src/Functions/tests/gtest_tidb_conversion.cpp +++ b/dbms/src/Functions/tests/gtest_tidb_conversion.cpp @@ -1308,6 +1308,22 @@ try } CATCH +TEST_F(TestTidbConversion, truncateInCastDecimalAsDecimal) +try +{ + DAGContext * dag_context = context.getDAGContext(); + UInt64 ori_flags = dag_context->getFlags(); + dag_context->addFlag(TiDBSQLFlags::IN_INSERT_STMT | TiDBSQLFlags::IN_UPDATE_OR_DELETE_STMT); + dag_context->clearWarnings(); + + ASSERT_COLUMN_EQ(createColumn(std::make_tuple(5, 2), {"1.23", "1.56", "1.01", "1.00", "-1.23", "-1.56", "-1.01", "-1.00"}), + executeFunction(func_name, {createColumn(std::make_tuple(5, 4), {"1.2300", "1.5600", "1.0056", "1.0023", "-1.2300", "-1.5600", "-1.0056", "-1.0023"}), createCastTypeConstColumn("Decimal(5,2)")})); + ASSERT_EQ(dag_context->getWarningCount(), 4); + dag_context->setFlags(ori_flags); + dag_context->clearWarnings(); +} +CATCH + TEST_F(TestTidbConversion, castDecimalAsDecimalWithRound) try {