diff --git a/dbms/src/Functions/FunctionsTiDBConversion.h b/dbms/src/Functions/FunctionsTiDBConversion.h index c3bbc453d0b..ad786947e1f 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 2d0f555543c..cd6a3f1065a 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 {